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>
18 #define IO_BUFFER_LIST_BUF_PER_PAGE (PAGE_SIZE / sizeof(struct io_uring_buf))
22 struct io_provide_buf {
31 static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
34 if (ctx->io_bl && bgid < BGID_ARRAY)
35 return &ctx->io_bl[bgid];
37 return xa_load(&ctx->io_bl_xa, bgid);
40 static int io_buffer_add_list(struct io_ring_ctx *ctx,
41 struct io_buffer_list *bl, unsigned int bgid)
44 if (bgid < BGID_ARRAY)
47 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
50 void io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
52 struct io_ring_ctx *ctx = req->ctx;
53 struct io_buffer_list *bl;
54 struct io_buffer *buf;
57 * For legacy provided buffer mode, don't recycle if we already did
58 * IO to this buffer. For ring-mapped provided buffer mode, we should
59 * increment ring->head to explicitly monopolize the buffer to avoid
62 if (req->flags & REQ_F_PARTIAL_IO)
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);
77 unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
82 * We can add this buffer back to two lists:
84 * 1) The io_buffers_cache list. This one is protected by the
85 * ctx->uring_lock. If we already hold this lock, add back to this
86 * list as we can grab it from issue as well.
87 * 2) The io_buffers_comp list. This one is protected by the
88 * ctx->completion_lock.
90 * We migrate buffers from the comp_list to the issue cache list
93 if (req->flags & REQ_F_BUFFER_RING) {
94 /* no buffers to recycle for this case */
95 cflags = __io_put_kbuf_list(req, NULL);
96 } else if (issue_flags & IO_URING_F_UNLOCKED) {
97 struct io_ring_ctx *ctx = req->ctx;
99 spin_lock(&ctx->completion_lock);
100 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
101 spin_unlock(&ctx->completion_lock);
103 lockdep_assert_held(&req->ctx->uring_lock);
105 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
110 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
111 struct io_buffer_list *bl)
113 if (!list_empty(&bl->buf_list)) {
114 struct io_buffer *kbuf;
116 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
117 list_del(&kbuf->list);
118 if (*len == 0 || *len > kbuf->len)
120 req->flags |= REQ_F_BUFFER_SELECTED;
122 req->buf_index = kbuf->bid;
123 return u64_to_user_ptr(kbuf->addr);
128 static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
129 struct io_buffer_list *bl,
130 unsigned int issue_flags)
132 struct io_uring_buf_ring *br = bl->buf_ring;
133 struct io_uring_buf *buf;
134 __u16 head = bl->head;
136 if (unlikely(smp_load_acquire(&br->tail) == head))
140 if (head < IO_BUFFER_LIST_BUF_PER_PAGE) {
141 buf = &br->bufs[head];
143 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
144 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
145 buf = page_address(bl->buf_pages[index]);
148 if (*len == 0 || *len > buf->len)
150 req->flags |= REQ_F_BUFFER_RING;
152 req->buf_index = buf->bid;
154 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
156 * If we came in unlocked, we have no choice but to consume the
157 * buffer here, otherwise nothing ensures that the buffer won't
158 * get used by others. This does mean it'll be pinned until the
159 * IO completes, coming in unlocked means we're being called from
160 * io-wq context and there may be further retries in async hybrid
161 * mode. For the locked case, the caller must call commit when
162 * the transfer completes (or if we get -EAGAIN and must poll of
165 req->buf_list = NULL;
168 return u64_to_user_ptr(buf->addr);
171 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
172 unsigned int issue_flags)
174 struct io_ring_ctx *ctx = req->ctx;
175 struct io_buffer_list *bl;
176 void __user *ret = NULL;
178 io_ring_submit_lock(req->ctx, issue_flags);
180 bl = io_buffer_get_list(ctx, req->buf_index);
182 if (bl->buf_nr_pages)
183 ret = io_ring_buffer_select(req, len, bl, issue_flags);
185 ret = io_provided_buffer_select(req, len, bl);
187 io_ring_submit_unlock(req->ctx, issue_flags);
191 static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
195 ctx->io_bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list),
200 for (i = 0; i < BGID_ARRAY; i++) {
201 INIT_LIST_HEAD(&ctx->io_bl[i].buf_list);
202 ctx->io_bl[i].bgid = i;
208 static int __io_remove_buffers(struct io_ring_ctx *ctx,
209 struct io_buffer_list *bl, unsigned nbufs)
213 /* shouldn't happen */
217 if (bl->buf_nr_pages) {
220 i = bl->buf_ring->tail - bl->head;
221 for (j = 0; j < bl->buf_nr_pages; j++)
222 unpin_user_page(bl->buf_pages[j]);
223 kvfree(bl->buf_pages);
224 bl->buf_pages = NULL;
225 bl->buf_nr_pages = 0;
226 /* make sure it's seen as empty */
227 INIT_LIST_HEAD(&bl->buf_list);
231 /* protects io_buffers_cache */
232 lockdep_assert_held(&ctx->uring_lock);
234 while (!list_empty(&bl->buf_list)) {
235 struct io_buffer *nxt;
237 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
238 list_move(&nxt->list, &ctx->io_buffers_cache);
247 void io_destroy_buffers(struct io_ring_ctx *ctx)
249 struct io_buffer_list *bl;
253 for (i = 0; i < BGID_ARRAY; i++) {
256 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
259 xa_for_each(&ctx->io_bl_xa, index, bl) {
260 xa_erase(&ctx->io_bl_xa, bl->bgid);
261 __io_remove_buffers(ctx, bl, -1U);
265 while (!list_empty(&ctx->io_buffers_pages)) {
268 page = list_first_entry(&ctx->io_buffers_pages, struct page, lru);
269 list_del_init(&page->lru);
274 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
276 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
279 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
283 tmp = READ_ONCE(sqe->fd);
284 if (!tmp || tmp > USHRT_MAX)
287 memset(p, 0, sizeof(*p));
289 p->bgid = READ_ONCE(sqe->buf_group);
293 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
295 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
296 struct io_ring_ctx *ctx = req->ctx;
297 struct io_buffer_list *bl;
300 io_ring_submit_lock(ctx, issue_flags);
303 bl = io_buffer_get_list(ctx, p->bgid);
306 /* can't use provide/remove buffers command on mapped buffers */
307 if (!bl->buf_nr_pages)
308 ret = __io_remove_buffers(ctx, bl, p->nbufs);
313 /* complete before unlock, IOPOLL may need the lock */
314 io_req_set_res(req, ret, 0);
315 __io_req_complete(req, issue_flags);
316 io_ring_submit_unlock(ctx, issue_flags);
317 return IOU_ISSUE_SKIP_COMPLETE;
320 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
322 unsigned long size, tmp_check;
323 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
326 if (sqe->rw_flags || sqe->splice_fd_in)
329 tmp = READ_ONCE(sqe->fd);
330 if (!tmp || tmp > USHRT_MAX)
333 p->addr = READ_ONCE(sqe->addr);
334 p->len = READ_ONCE(sqe->len);
336 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
339 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
342 size = (unsigned long)p->len * p->nbufs;
343 if (!access_ok(u64_to_user_ptr(p->addr), size))
346 p->bgid = READ_ONCE(sqe->buf_group);
347 tmp = READ_ONCE(sqe->off);
350 if (tmp + p->nbufs >= USHRT_MAX)
356 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
358 struct io_buffer *buf;
363 * Completions that don't happen inline (eg not under uring_lock) will
364 * add to ->io_buffers_comp. If we don't have any free buffers, check
365 * the completion list and splice those entries first.
367 if (!list_empty_careful(&ctx->io_buffers_comp)) {
368 spin_lock(&ctx->completion_lock);
369 if (!list_empty(&ctx->io_buffers_comp)) {
370 list_splice_init(&ctx->io_buffers_comp,
371 &ctx->io_buffers_cache);
372 spin_unlock(&ctx->completion_lock);
375 spin_unlock(&ctx->completion_lock);
379 * No free buffers and no completion entries either. Allocate a new
380 * page worth of buffer entries and add those to our freelist.
382 page = alloc_page(GFP_KERNEL_ACCOUNT);
386 list_add(&page->lru, &ctx->io_buffers_pages);
388 buf = page_address(page);
389 bufs_in_page = PAGE_SIZE / sizeof(*buf);
390 while (bufs_in_page) {
391 list_add_tail(&buf->list, &ctx->io_buffers_cache);
399 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
400 struct io_buffer_list *bl)
402 struct io_buffer *buf;
403 u64 addr = pbuf->addr;
404 int i, bid = pbuf->bid;
406 for (i = 0; i < pbuf->nbufs; i++) {
407 if (list_empty(&ctx->io_buffers_cache) &&
408 io_refill_buffer_cache(ctx))
410 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
412 list_move_tail(&buf->list, &bl->buf_list);
414 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
416 buf->bgid = pbuf->bgid;
422 return i ? 0 : -ENOMEM;
425 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
427 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
428 struct io_ring_ctx *ctx = req->ctx;
429 struct io_buffer_list *bl;
432 io_ring_submit_lock(ctx, issue_flags);
434 if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
435 ret = io_init_bl_list(ctx);
440 bl = io_buffer_get_list(ctx, p->bgid);
442 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
447 INIT_LIST_HEAD(&bl->buf_list);
448 ret = io_buffer_add_list(ctx, bl, p->bgid);
454 /* can't add buffers via this command for a mapped buffer ring */
455 if (bl->buf_nr_pages) {
460 ret = io_add_buffers(ctx, p, bl);
464 /* complete before unlock, IOPOLL may need the lock */
465 io_req_set_res(req, ret, 0);
466 __io_req_complete(req, issue_flags);
467 io_ring_submit_unlock(ctx, issue_flags);
468 return IOU_ISSUE_SKIP_COMPLETE;
471 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
473 struct io_uring_buf_ring *br;
474 struct io_uring_buf_reg reg;
475 struct io_buffer_list *bl, *free_bl = NULL;
479 if (copy_from_user(®, arg, sizeof(reg)))
482 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
486 if (reg.ring_addr & ~PAGE_MASK)
488 if (!is_power_of_2(reg.ring_entries))
491 /* cannot disambiguate full vs empty due to head/tail size */
492 if (reg.ring_entries >= 65536)
495 if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
496 int ret = io_init_bl_list(ctx);
501 bl = io_buffer_get_list(ctx, reg.bgid);
503 /* if mapped buffer ring OR classic exists, don't allow */
504 if (bl->buf_nr_pages || !list_empty(&bl->buf_list))
507 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
512 pages = io_pin_pages(reg.ring_addr,
513 flex_array_size(br, bufs, reg.ring_entries),
517 return PTR_ERR(pages);
520 br = page_address(pages[0]);
521 bl->buf_pages = pages;
522 bl->buf_nr_pages = nr_pages;
523 bl->nr_entries = reg.ring_entries;
525 bl->mask = reg.ring_entries - 1;
526 io_buffer_add_list(ctx, bl, reg.bgid);
530 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
532 struct io_uring_buf_reg reg;
533 struct io_buffer_list *bl;
535 if (copy_from_user(®, arg, sizeof(reg)))
537 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
540 bl = io_buffer_get_list(ctx, reg.bgid);
543 if (!bl->buf_nr_pages)
546 __io_remove_buffers(ctx, bl, -1U);
547 if (bl->bgid >= BGID_ARRAY) {
548 xa_erase(&ctx->io_bl_xa, bl->bgid);