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 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
87 struct io_buffer_list *bl)
89 if (!list_empty(&bl->buf_list)) {
90 struct io_buffer *kbuf;
92 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
93 list_del(&kbuf->list);
96 req->flags |= REQ_F_BUFFER_SELECTED;
98 req->buf_index = kbuf->bid;
99 return u64_to_user_ptr(kbuf->addr);
104 static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
105 struct io_buffer_list *bl,
106 unsigned int issue_flags)
108 struct io_uring_buf_ring *br = bl->buf_ring;
109 struct io_uring_buf *buf;
110 __u16 head = bl->head;
112 if (unlikely(smp_load_acquire(&br->tail) == head))
116 if (head < IO_BUFFER_LIST_BUF_PER_PAGE) {
117 buf = &br->bufs[head];
119 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
120 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
121 buf = page_address(bl->buf_pages[index]);
126 req->flags |= REQ_F_BUFFER_RING;
128 req->buf_index = buf->bid;
130 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
132 * If we came in unlocked, we have no choice but to consume the
133 * buffer here. This does mean it'll be pinned until the IO
134 * completes. But coming in unlocked means we're in io-wq
135 * context, hence there should be no further retry. For the
136 * locked case, the caller must ensure to call the commit when
137 * the transfer completes (or if we get -EAGAIN and must poll
140 req->buf_list = NULL;
143 return u64_to_user_ptr(buf->addr);
146 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
147 unsigned int issue_flags)
149 struct io_ring_ctx *ctx = req->ctx;
150 struct io_buffer_list *bl;
151 void __user *ret = NULL;
153 io_ring_submit_lock(req->ctx, issue_flags);
155 bl = io_buffer_get_list(ctx, req->buf_index);
157 if (bl->buf_nr_pages)
158 ret = io_ring_buffer_select(req, len, bl, issue_flags);
160 ret = io_provided_buffer_select(req, len, bl);
162 io_ring_submit_unlock(req->ctx, issue_flags);
166 static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
170 ctx->io_bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list),
175 for (i = 0; i < BGID_ARRAY; i++) {
176 INIT_LIST_HEAD(&ctx->io_bl[i].buf_list);
177 ctx->io_bl[i].bgid = i;
183 static int __io_remove_buffers(struct io_ring_ctx *ctx,
184 struct io_buffer_list *bl, unsigned nbufs)
188 /* shouldn't happen */
192 if (bl->buf_nr_pages) {
195 i = bl->buf_ring->tail - bl->head;
196 for (j = 0; j < bl->buf_nr_pages; j++)
197 unpin_user_page(bl->buf_pages[j]);
198 kvfree(bl->buf_pages);
199 bl->buf_pages = NULL;
200 bl->buf_nr_pages = 0;
201 /* make sure it's seen as empty */
202 INIT_LIST_HEAD(&bl->buf_list);
206 /* the head kbuf is the list itself */
207 while (!list_empty(&bl->buf_list)) {
208 struct io_buffer *nxt;
210 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
211 list_del(&nxt->list);
221 void io_destroy_buffers(struct io_ring_ctx *ctx)
223 struct io_buffer_list *bl;
227 for (i = 0; i < BGID_ARRAY; i++) {
230 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
233 xa_for_each(&ctx->io_bl_xa, index, bl) {
234 xa_erase(&ctx->io_bl_xa, bl->bgid);
235 __io_remove_buffers(ctx, bl, -1U);
239 while (!list_empty(&ctx->io_buffers_pages)) {
242 page = list_first_entry(&ctx->io_buffers_pages, struct page, lru);
243 list_del_init(&page->lru);
248 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
250 struct io_provide_buf *p = io_kiocb_to_cmd(req);
253 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
257 tmp = READ_ONCE(sqe->fd);
258 if (!tmp || tmp > USHRT_MAX)
261 memset(p, 0, sizeof(*p));
263 p->bgid = READ_ONCE(sqe->buf_group);
267 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
269 struct io_provide_buf *p = io_kiocb_to_cmd(req);
270 struct io_ring_ctx *ctx = req->ctx;
271 struct io_buffer_list *bl;
274 io_ring_submit_lock(ctx, issue_flags);
277 bl = io_buffer_get_list(ctx, p->bgid);
280 /* can't use provide/remove buffers command on mapped buffers */
281 if (!bl->buf_nr_pages)
282 ret = __io_remove_buffers(ctx, bl, p->nbufs);
287 /* complete before unlock, IOPOLL may need the lock */
288 io_req_set_res(req, ret, 0);
289 __io_req_complete(req, issue_flags);
290 io_ring_submit_unlock(ctx, issue_flags);
291 return IOU_ISSUE_SKIP_COMPLETE;
294 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
296 unsigned long size, tmp_check;
297 struct io_provide_buf *p = io_kiocb_to_cmd(req);
300 if (sqe->rw_flags || sqe->splice_fd_in)
303 tmp = READ_ONCE(sqe->fd);
304 if (!tmp || tmp > USHRT_MAX)
307 p->addr = READ_ONCE(sqe->addr);
308 p->len = READ_ONCE(sqe->len);
310 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
313 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
316 size = (unsigned long)p->len * p->nbufs;
317 if (!access_ok(u64_to_user_ptr(p->addr), size))
320 p->bgid = READ_ONCE(sqe->buf_group);
321 tmp = READ_ONCE(sqe->off);
328 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
330 struct io_buffer *buf;
335 * Completions that don't happen inline (eg not under uring_lock) will
336 * add to ->io_buffers_comp. If we don't have any free buffers, check
337 * the completion list and splice those entries first.
339 if (!list_empty_careful(&ctx->io_buffers_comp)) {
340 spin_lock(&ctx->completion_lock);
341 if (!list_empty(&ctx->io_buffers_comp)) {
342 list_splice_init(&ctx->io_buffers_comp,
343 &ctx->io_buffers_cache);
344 spin_unlock(&ctx->completion_lock);
347 spin_unlock(&ctx->completion_lock);
351 * No free buffers and no completion entries either. Allocate a new
352 * page worth of buffer entries and add those to our freelist.
354 page = alloc_page(GFP_KERNEL_ACCOUNT);
358 list_add(&page->lru, &ctx->io_buffers_pages);
360 buf = page_address(page);
361 bufs_in_page = PAGE_SIZE / sizeof(*buf);
362 while (bufs_in_page) {
363 list_add_tail(&buf->list, &ctx->io_buffers_cache);
371 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
372 struct io_buffer_list *bl)
374 struct io_buffer *buf;
375 u64 addr = pbuf->addr;
376 int i, bid = pbuf->bid;
378 for (i = 0; i < pbuf->nbufs; i++) {
379 if (list_empty(&ctx->io_buffers_cache) &&
380 io_refill_buffer_cache(ctx))
382 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
384 list_move_tail(&buf->list, &bl->buf_list);
386 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
388 buf->bgid = pbuf->bgid;
394 return i ? 0 : -ENOMEM;
397 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
399 struct io_provide_buf *p = io_kiocb_to_cmd(req);
400 struct io_ring_ctx *ctx = req->ctx;
401 struct io_buffer_list *bl;
404 io_ring_submit_lock(ctx, issue_flags);
406 if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
407 ret = io_init_bl_list(ctx);
412 bl = io_buffer_get_list(ctx, p->bgid);
414 bl = kzalloc(sizeof(*bl), GFP_KERNEL);
419 INIT_LIST_HEAD(&bl->buf_list);
420 ret = io_buffer_add_list(ctx, bl, p->bgid);
426 /* can't add buffers via this command for a mapped buffer ring */
427 if (bl->buf_nr_pages) {
432 ret = io_add_buffers(ctx, p, bl);
436 /* complete before unlock, IOPOLL may need the lock */
437 io_req_set_res(req, ret, 0);
438 __io_req_complete(req, issue_flags);
439 io_ring_submit_unlock(ctx, issue_flags);
440 return IOU_ISSUE_SKIP_COMPLETE;
443 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
445 struct io_uring_buf_ring *br;
446 struct io_uring_buf_reg reg;
447 struct io_buffer_list *bl, *free_bl = NULL;
451 if (copy_from_user(®, arg, sizeof(reg)))
454 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
458 if (reg.ring_addr & ~PAGE_MASK)
460 if (!is_power_of_2(reg.ring_entries))
463 /* cannot disambiguate full vs empty due to head/tail size */
464 if (reg.ring_entries >= 65536)
467 if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
468 int ret = io_init_bl_list(ctx);
473 bl = io_buffer_get_list(ctx, reg.bgid);
475 /* if mapped buffer ring OR classic exists, don't allow */
476 if (bl->buf_nr_pages || !list_empty(&bl->buf_list))
479 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
484 pages = io_pin_pages(reg.ring_addr,
485 struct_size(br, bufs, reg.ring_entries),
489 return PTR_ERR(pages);
492 br = page_address(pages[0]);
493 bl->buf_pages = pages;
494 bl->buf_nr_pages = nr_pages;
495 bl->nr_entries = reg.ring_entries;
497 bl->mask = reg.ring_entries - 1;
498 io_buffer_add_list(ctx, bl, reg.bgid);
502 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
504 struct io_uring_buf_reg reg;
505 struct io_buffer_list *bl;
507 if (copy_from_user(®, arg, sizeof(reg)))
509 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
512 bl = io_buffer_get_list(ctx, reg.bgid);
515 if (!bl->buf_nr_pages)
518 __io_remove_buffers(ctx, bl, -1U);
519 if (bl->bgid >= BGID_ARRAY) {
520 xa_erase(&ctx->io_bl_xa, bl->bgid);