1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/file.h>
7 #include <linux/slab.h>
8 #include <linux/namei.h>
9 #include <linux/poll.h>
10 #include <linux/io_uring.h>
12 #include <uapi/linux/io_uring.h>
14 #include "io_uring_types.h"
19 #define IO_BUFFER_LIST_BUF_PER_PAGE (PAGE_SIZE / sizeof(struct io_uring_buf))
23 struct io_provide_buf {
32 static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
35 if (ctx->io_bl && bgid < BGID_ARRAY)
36 return &ctx->io_bl[bgid];
38 return xa_load(&ctx->io_bl_xa, bgid);
41 void __io_kbuf_recycle(struct io_kiocb *req, unsigned issue_flags)
43 struct io_ring_ctx *ctx = req->ctx;
44 struct io_buffer_list *bl;
45 struct io_buffer *buf;
48 * We don't need to recycle for REQ_F_BUFFER_RING, we can just clear
49 * the flag and hence ensure that bl->head doesn't get incremented.
50 * If the tail has already been incremented, hang on to it.
52 if (req->flags & REQ_F_BUFFER_RING) {
54 if (req->flags & REQ_F_PARTIAL_IO) {
55 req->buf_list->head++;
58 req->buf_index = req->buf_list->bgid;
59 req->flags &= ~REQ_F_BUFFER_RING;
65 io_ring_submit_lock(ctx, issue_flags);
68 bl = io_buffer_get_list(ctx, buf->bgid);
69 list_add(&buf->list, &bl->buf_list);
70 req->flags &= ~REQ_F_BUFFER_SELECTED;
71 req->buf_index = buf->bgid;
73 io_ring_submit_unlock(ctx, issue_flags);
76 static int io_buffer_add_list(struct io_ring_ctx *ctx,
77 struct io_buffer_list *bl, unsigned int bgid)
80 if (bgid < BGID_ARRAY)
83 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
86 unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
91 * We can add this buffer back to two lists:
93 * 1) The io_buffers_cache list. This one is protected by the
94 * ctx->uring_lock. If we already hold this lock, add back to this
95 * list as we can grab it from issue as well.
96 * 2) The io_buffers_comp list. This one is protected by the
97 * ctx->completion_lock.
99 * We migrate buffers from the comp_list to the issue cache list
102 if (req->flags & REQ_F_BUFFER_RING) {
103 /* no buffers to recycle for this case */
104 cflags = __io_put_kbuf_list(req, NULL);
105 } else if (issue_flags & IO_URING_F_UNLOCKED) {
106 struct io_ring_ctx *ctx = req->ctx;
108 spin_lock(&ctx->completion_lock);
109 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
110 spin_unlock(&ctx->completion_lock);
112 lockdep_assert_held(&req->ctx->uring_lock);
114 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
119 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
120 struct io_buffer_list *bl)
122 if (!list_empty(&bl->buf_list)) {
123 struct io_buffer *kbuf;
125 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
126 list_del(&kbuf->list);
127 if (*len > kbuf->len)
129 req->flags |= REQ_F_BUFFER_SELECTED;
131 req->buf_index = kbuf->bid;
132 return u64_to_user_ptr(kbuf->addr);
137 static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
138 struct io_buffer_list *bl,
139 unsigned int issue_flags)
141 struct io_uring_buf_ring *br = bl->buf_ring;
142 struct io_uring_buf *buf;
143 __u16 head = bl->head;
145 if (unlikely(smp_load_acquire(&br->tail) == head))
149 if (head < IO_BUFFER_LIST_BUF_PER_PAGE) {
150 buf = &br->bufs[head];
152 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
153 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
154 buf = page_address(bl->buf_pages[index]);
159 req->flags |= REQ_F_BUFFER_RING;
161 req->buf_index = buf->bid;
163 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
165 * If we came in unlocked, we have no choice but to consume the
166 * buffer here. This does mean it'll be pinned until the IO
167 * completes. But coming in unlocked means we're in io-wq
168 * context, hence there should be no further retry. For the
169 * locked case, the caller must ensure to call the commit when
170 * the transfer completes (or if we get -EAGAIN and must poll
173 req->buf_list = NULL;
176 return u64_to_user_ptr(buf->addr);
179 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
180 unsigned int issue_flags)
182 struct io_ring_ctx *ctx = req->ctx;
183 struct io_buffer_list *bl;
184 void __user *ret = NULL;
186 io_ring_submit_lock(req->ctx, issue_flags);
188 bl = io_buffer_get_list(ctx, req->buf_index);
190 if (bl->buf_nr_pages)
191 ret = io_ring_buffer_select(req, len, bl, issue_flags);
193 ret = io_provided_buffer_select(req, len, bl);
195 io_ring_submit_unlock(req->ctx, issue_flags);
199 static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
203 ctx->io_bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list),
208 for (i = 0; i < BGID_ARRAY; i++) {
209 INIT_LIST_HEAD(&ctx->io_bl[i].buf_list);
210 ctx->io_bl[i].bgid = i;
216 static int __io_remove_buffers(struct io_ring_ctx *ctx,
217 struct io_buffer_list *bl, unsigned nbufs)
221 /* shouldn't happen */
225 if (bl->buf_nr_pages) {
228 i = bl->buf_ring->tail - bl->head;
229 for (j = 0; j < bl->buf_nr_pages; j++)
230 unpin_user_page(bl->buf_pages[j]);
231 kvfree(bl->buf_pages);
232 bl->buf_pages = NULL;
233 bl->buf_nr_pages = 0;
234 /* make sure it's seen as empty */
235 INIT_LIST_HEAD(&bl->buf_list);
239 /* the head kbuf is the list itself */
240 while (!list_empty(&bl->buf_list)) {
241 struct io_buffer *nxt;
243 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
244 list_del(&nxt->list);
254 void io_destroy_buffers(struct io_ring_ctx *ctx)
256 struct io_buffer_list *bl;
260 for (i = 0; i < BGID_ARRAY; i++) {
263 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
266 xa_for_each(&ctx->io_bl_xa, index, bl) {
267 xa_erase(&ctx->io_bl_xa, bl->bgid);
268 __io_remove_buffers(ctx, bl, -1U);
272 while (!list_empty(&ctx->io_buffers_pages)) {
275 page = list_first_entry(&ctx->io_buffers_pages, struct page, lru);
276 list_del_init(&page->lru);
281 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
283 struct io_provide_buf *p = io_kiocb_to_cmd(req);
286 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
290 tmp = READ_ONCE(sqe->fd);
291 if (!tmp || tmp > USHRT_MAX)
294 memset(p, 0, sizeof(*p));
296 p->bgid = READ_ONCE(sqe->buf_group);
300 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
302 struct io_provide_buf *p = io_kiocb_to_cmd(req);
303 struct io_ring_ctx *ctx = req->ctx;
304 struct io_buffer_list *bl;
307 io_ring_submit_lock(ctx, issue_flags);
310 bl = io_buffer_get_list(ctx, p->bgid);
313 /* can't use provide/remove buffers command on mapped buffers */
314 if (!bl->buf_nr_pages)
315 ret = __io_remove_buffers(ctx, bl, p->nbufs);
320 /* complete before unlock, IOPOLL may need the lock */
321 io_req_set_res(req, ret, 0);
322 __io_req_complete(req, issue_flags);
323 io_ring_submit_unlock(ctx, issue_flags);
324 return IOU_ISSUE_SKIP_COMPLETE;
327 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
329 unsigned long size, tmp_check;
330 struct io_provide_buf *p = io_kiocb_to_cmd(req);
333 if (sqe->rw_flags || sqe->splice_fd_in)
336 tmp = READ_ONCE(sqe->fd);
337 if (!tmp || tmp > USHRT_MAX)
340 p->addr = READ_ONCE(sqe->addr);
341 p->len = READ_ONCE(sqe->len);
343 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
346 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
349 size = (unsigned long)p->len * p->nbufs;
350 if (!access_ok(u64_to_user_ptr(p->addr), size))
353 p->bgid = READ_ONCE(sqe->buf_group);
354 tmp = READ_ONCE(sqe->off);
361 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
363 struct io_buffer *buf;
368 * Completions that don't happen inline (eg not under uring_lock) will
369 * add to ->io_buffers_comp. If we don't have any free buffers, check
370 * the completion list and splice those entries first.
372 if (!list_empty_careful(&ctx->io_buffers_comp)) {
373 spin_lock(&ctx->completion_lock);
374 if (!list_empty(&ctx->io_buffers_comp)) {
375 list_splice_init(&ctx->io_buffers_comp,
376 &ctx->io_buffers_cache);
377 spin_unlock(&ctx->completion_lock);
380 spin_unlock(&ctx->completion_lock);
384 * No free buffers and no completion entries either. Allocate a new
385 * page worth of buffer entries and add those to our freelist.
387 page = alloc_page(GFP_KERNEL_ACCOUNT);
391 list_add(&page->lru, &ctx->io_buffers_pages);
393 buf = page_address(page);
394 bufs_in_page = PAGE_SIZE / sizeof(*buf);
395 while (bufs_in_page) {
396 list_add_tail(&buf->list, &ctx->io_buffers_cache);
404 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
405 struct io_buffer_list *bl)
407 struct io_buffer *buf;
408 u64 addr = pbuf->addr;
409 int i, bid = pbuf->bid;
411 for (i = 0; i < pbuf->nbufs; i++) {
412 if (list_empty(&ctx->io_buffers_cache) &&
413 io_refill_buffer_cache(ctx))
415 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
417 list_move_tail(&buf->list, &bl->buf_list);
419 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
421 buf->bgid = pbuf->bgid;
427 return i ? 0 : -ENOMEM;
430 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
432 struct io_provide_buf *p = io_kiocb_to_cmd(req);
433 struct io_ring_ctx *ctx = req->ctx;
434 struct io_buffer_list *bl;
437 io_ring_submit_lock(ctx, issue_flags);
439 if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
440 ret = io_init_bl_list(ctx);
445 bl = io_buffer_get_list(ctx, p->bgid);
447 bl = kzalloc(sizeof(*bl), GFP_KERNEL);
452 INIT_LIST_HEAD(&bl->buf_list);
453 ret = io_buffer_add_list(ctx, bl, p->bgid);
459 /* can't add buffers via this command for a mapped buffer ring */
460 if (bl->buf_nr_pages) {
465 ret = io_add_buffers(ctx, p, bl);
469 /* complete before unlock, IOPOLL may need the lock */
470 io_req_set_res(req, ret, 0);
471 __io_req_complete(req, issue_flags);
472 io_ring_submit_unlock(ctx, issue_flags);
473 return IOU_ISSUE_SKIP_COMPLETE;
476 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
478 struct io_uring_buf_ring *br;
479 struct io_uring_buf_reg reg;
480 struct io_buffer_list *bl, *free_bl = NULL;
484 if (copy_from_user(®, arg, sizeof(reg)))
487 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
491 if (reg.ring_addr & ~PAGE_MASK)
493 if (!is_power_of_2(reg.ring_entries))
496 /* cannot disambiguate full vs empty due to head/tail size */
497 if (reg.ring_entries >= 65536)
500 if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
501 int ret = io_init_bl_list(ctx);
506 bl = io_buffer_get_list(ctx, reg.bgid);
508 /* if mapped buffer ring OR classic exists, don't allow */
509 if (bl->buf_nr_pages || !list_empty(&bl->buf_list))
512 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
517 pages = io_pin_pages(reg.ring_addr,
518 struct_size(br, bufs, reg.ring_entries),
522 return PTR_ERR(pages);
525 br = page_address(pages[0]);
526 bl->buf_pages = pages;
527 bl->buf_nr_pages = nr_pages;
528 bl->nr_entries = reg.ring_entries;
530 bl->mask = reg.ring_entries - 1;
531 io_buffer_add_list(ctx, bl, reg.bgid);
535 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
537 struct io_uring_buf_reg reg;
538 struct io_buffer_list *bl;
540 if (copy_from_user(®, arg, sizeof(reg)))
542 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
545 bl = io_buffer_get_list(ctx, reg.bgid);
548 if (!bl->buf_nr_pages)
551 __io_remove_buffers(ctx, bl, -1U);
552 if (bl->bgid >= BGID_ARRAY) {
553 xa_erase(&ctx->io_bl_xa, bl->bgid);