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 /* BIDs are addressed by a 16-bit field in a CQE */
23 #define MAX_BIDS_PER_BGID (1 << 16)
25 struct io_provide_buf {
34 static struct io_buffer_list *__io_buffer_get_list(struct io_ring_ctx *ctx,
35 struct io_buffer_list *bl,
38 if (bl && bgid < BGID_ARRAY)
41 return xa_load(&ctx->io_bl_xa, bgid);
44 static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
47 lockdep_assert_held(&ctx->uring_lock);
49 return __io_buffer_get_list(ctx, ctx->io_bl, bgid);
52 static int io_buffer_add_list(struct io_ring_ctx *ctx,
53 struct io_buffer_list *bl, unsigned int bgid)
56 * Store buffer group ID and finally mark the list as visible.
57 * The normal lookup doesn't care about the visibility as we're
58 * always under the ->uring_lock, but the RCU lookup from mmap does.
61 smp_store_release(&bl->is_ready, 1);
63 if (bgid < BGID_ARRAY)
66 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
69 void io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
71 struct io_ring_ctx *ctx = req->ctx;
72 struct io_buffer_list *bl;
73 struct io_buffer *buf;
76 * For legacy provided buffer mode, don't recycle if we already did
77 * IO to this buffer. For ring-mapped provided buffer mode, we should
78 * increment ring->head to explicitly monopolize the buffer to avoid
81 if (req->flags & REQ_F_PARTIAL_IO)
84 io_ring_submit_lock(ctx, issue_flags);
87 bl = io_buffer_get_list(ctx, buf->bgid);
88 list_add(&buf->list, &bl->buf_list);
89 req->flags &= ~REQ_F_BUFFER_SELECTED;
90 req->buf_index = buf->bgid;
92 io_ring_submit_unlock(ctx, issue_flags);
96 unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
101 * We can add this buffer back to two lists:
103 * 1) The io_buffers_cache list. This one is protected by the
104 * ctx->uring_lock. If we already hold this lock, add back to this
105 * list as we can grab it from issue as well.
106 * 2) The io_buffers_comp list. This one is protected by the
107 * ctx->completion_lock.
109 * We migrate buffers from the comp_list to the issue cache list
112 if (req->flags & REQ_F_BUFFER_RING) {
113 /* no buffers to recycle for this case */
114 cflags = __io_put_kbuf_list(req, NULL);
115 } else if (issue_flags & IO_URING_F_UNLOCKED) {
116 struct io_ring_ctx *ctx = req->ctx;
118 spin_lock(&ctx->completion_lock);
119 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
120 spin_unlock(&ctx->completion_lock);
122 lockdep_assert_held(&req->ctx->uring_lock);
124 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
129 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
130 struct io_buffer_list *bl)
132 if (!list_empty(&bl->buf_list)) {
133 struct io_buffer *kbuf;
135 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
136 list_del(&kbuf->list);
137 if (*len == 0 || *len > kbuf->len)
139 req->flags |= REQ_F_BUFFER_SELECTED;
141 req->buf_index = kbuf->bid;
142 return u64_to_user_ptr(kbuf->addr);
147 static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
148 struct io_buffer_list *bl,
149 unsigned int issue_flags)
151 struct io_uring_buf_ring *br = bl->buf_ring;
152 struct io_uring_buf *buf;
153 __u16 head = bl->head;
155 if (unlikely(smp_load_acquire(&br->tail) == head))
159 /* mmaped buffers are always contig */
160 if (bl->is_mmap || head < IO_BUFFER_LIST_BUF_PER_PAGE) {
161 buf = &br->bufs[head];
163 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
164 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
165 buf = page_address(bl->buf_pages[index]);
168 if (*len == 0 || *len > buf->len)
170 req->flags |= REQ_F_BUFFER_RING;
172 req->buf_index = buf->bid;
174 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
176 * If we came in unlocked, we have no choice but to consume the
177 * buffer here, otherwise nothing ensures that the buffer won't
178 * get used by others. This does mean it'll be pinned until the
179 * IO completes, coming in unlocked means we're being called from
180 * io-wq context and there may be further retries in async hybrid
181 * mode. For the locked case, the caller must call commit when
182 * the transfer completes (or if we get -EAGAIN and must poll of
185 req->buf_list = NULL;
188 return u64_to_user_ptr(buf->addr);
191 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
192 unsigned int issue_flags)
194 struct io_ring_ctx *ctx = req->ctx;
195 struct io_buffer_list *bl;
196 void __user *ret = NULL;
198 io_ring_submit_lock(req->ctx, issue_flags);
200 bl = io_buffer_get_list(ctx, req->buf_index);
203 ret = io_ring_buffer_select(req, len, bl, issue_flags);
205 ret = io_provided_buffer_select(req, len, bl);
207 io_ring_submit_unlock(req->ctx, issue_flags);
211 static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
213 struct io_buffer_list *bl;
216 bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list), GFP_KERNEL);
220 for (i = 0; i < BGID_ARRAY; i++) {
221 INIT_LIST_HEAD(&bl[i].buf_list);
225 smp_store_release(&ctx->io_bl, bl);
229 static int __io_remove_buffers(struct io_ring_ctx *ctx,
230 struct io_buffer_list *bl, unsigned nbufs)
234 /* shouldn't happen */
239 i = bl->buf_ring->tail - bl->head;
241 folio_put(virt_to_folio(bl->buf_ring));
244 } else if (bl->buf_nr_pages) {
247 for (j = 0; j < bl->buf_nr_pages; j++)
248 unpin_user_page(bl->buf_pages[j]);
249 kvfree(bl->buf_pages);
250 bl->buf_pages = NULL;
251 bl->buf_nr_pages = 0;
253 /* make sure it's seen as empty */
254 INIT_LIST_HEAD(&bl->buf_list);
259 /* protects io_buffers_cache */
260 lockdep_assert_held(&ctx->uring_lock);
262 while (!list_empty(&bl->buf_list)) {
263 struct io_buffer *nxt;
265 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
266 list_move(&nxt->list, &ctx->io_buffers_cache);
275 void io_destroy_buffers(struct io_ring_ctx *ctx)
277 struct io_buffer_list *bl;
281 for (i = 0; i < BGID_ARRAY; i++) {
284 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
287 xa_for_each(&ctx->io_bl_xa, index, bl) {
288 xa_erase(&ctx->io_bl_xa, bl->bgid);
289 __io_remove_buffers(ctx, bl, -1U);
293 while (!list_empty(&ctx->io_buffers_pages)) {
296 page = list_first_entry(&ctx->io_buffers_pages, struct page, lru);
297 list_del_init(&page->lru);
302 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
304 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
307 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
311 tmp = READ_ONCE(sqe->fd);
312 if (!tmp || tmp > MAX_BIDS_PER_BGID)
315 memset(p, 0, sizeof(*p));
317 p->bgid = READ_ONCE(sqe->buf_group);
321 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
323 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
324 struct io_ring_ctx *ctx = req->ctx;
325 struct io_buffer_list *bl;
328 io_ring_submit_lock(ctx, issue_flags);
331 bl = io_buffer_get_list(ctx, p->bgid);
334 /* can't use provide/remove buffers command on mapped buffers */
336 ret = __io_remove_buffers(ctx, bl, p->nbufs);
338 io_ring_submit_unlock(ctx, issue_flags);
341 io_req_set_res(req, ret, 0);
345 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
347 unsigned long size, tmp_check;
348 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
351 if (sqe->rw_flags || sqe->splice_fd_in)
354 tmp = READ_ONCE(sqe->fd);
355 if (!tmp || tmp > MAX_BIDS_PER_BGID)
358 p->addr = READ_ONCE(sqe->addr);
359 p->len = READ_ONCE(sqe->len);
361 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
364 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
367 size = (unsigned long)p->len * p->nbufs;
368 if (!access_ok(u64_to_user_ptr(p->addr), size))
371 p->bgid = READ_ONCE(sqe->buf_group);
372 tmp = READ_ONCE(sqe->off);
375 if (tmp + p->nbufs > MAX_BIDS_PER_BGID)
381 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
383 struct io_buffer *buf;
388 * Completions that don't happen inline (eg not under uring_lock) will
389 * add to ->io_buffers_comp. If we don't have any free buffers, check
390 * the completion list and splice those entries first.
392 if (!list_empty_careful(&ctx->io_buffers_comp)) {
393 spin_lock(&ctx->completion_lock);
394 if (!list_empty(&ctx->io_buffers_comp)) {
395 list_splice_init(&ctx->io_buffers_comp,
396 &ctx->io_buffers_cache);
397 spin_unlock(&ctx->completion_lock);
400 spin_unlock(&ctx->completion_lock);
404 * No free buffers and no completion entries either. Allocate a new
405 * page worth of buffer entries and add those to our freelist.
407 page = alloc_page(GFP_KERNEL_ACCOUNT);
411 list_add(&page->lru, &ctx->io_buffers_pages);
413 buf = page_address(page);
414 bufs_in_page = PAGE_SIZE / sizeof(*buf);
415 while (bufs_in_page) {
416 list_add_tail(&buf->list, &ctx->io_buffers_cache);
424 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
425 struct io_buffer_list *bl)
427 struct io_buffer *buf;
428 u64 addr = pbuf->addr;
429 int i, bid = pbuf->bid;
431 for (i = 0; i < pbuf->nbufs; i++) {
432 if (list_empty(&ctx->io_buffers_cache) &&
433 io_refill_buffer_cache(ctx))
435 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
437 list_move_tail(&buf->list, &bl->buf_list);
439 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
441 buf->bgid = pbuf->bgid;
447 return i ? 0 : -ENOMEM;
450 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
452 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
453 struct io_ring_ctx *ctx = req->ctx;
454 struct io_buffer_list *bl;
457 io_ring_submit_lock(ctx, issue_flags);
459 if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
460 ret = io_init_bl_list(ctx);
465 bl = io_buffer_get_list(ctx, p->bgid);
467 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
472 INIT_LIST_HEAD(&bl->buf_list);
473 ret = io_buffer_add_list(ctx, bl, p->bgid);
476 * Doesn't need rcu free as it was never visible, but
477 * let's keep it consistent throughout. Also can't
478 * be a lower indexed array group, as adding one
479 * where lookup failed cannot happen.
481 if (p->bgid >= BGID_ARRAY)
488 /* can't add buffers via this command for a mapped buffer ring */
494 ret = io_add_buffers(ctx, p, bl);
496 io_ring_submit_unlock(ctx, issue_flags);
500 io_req_set_res(req, ret, 0);
504 static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
505 struct io_buffer_list *bl)
507 struct io_uring_buf_ring *br;
511 pages = io_pin_pages(reg->ring_addr,
512 flex_array_size(br, bufs, reg->ring_entries),
515 return PTR_ERR(pages);
518 * Apparently some 32-bit boxes (ARM) will return highmem pages,
519 * which then need to be mapped. We could support that, but it'd
520 * complicate the code and slowdown the common cases quite a bit.
521 * So just error out, returning -EINVAL just like we did on kernels
522 * that didn't support mapped buffer rings.
524 for (i = 0; i < nr_pages; i++)
525 if (PageHighMem(pages[i]))
528 br = page_address(pages[0]);
531 * On platforms that have specific aliasing requirements, SHM_COLOUR
532 * is set and we must guarantee that the kernel and user side align
533 * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and
534 * the application mmap's the provided ring buffer. Fail the request
535 * if we, by chance, don't end up with aligned addresses. The app
536 * should use IOU_PBUF_RING_MMAP instead, and liburing will handle
537 * this transparently.
539 if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1))
542 bl->buf_pages = pages;
543 bl->buf_nr_pages = nr_pages;
549 for (i = 0; i < nr_pages; i++)
550 unpin_user_page(pages[i]);
555 static int io_alloc_pbuf_ring(struct io_uring_buf_reg *reg,
556 struct io_buffer_list *bl)
558 gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
562 ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
563 ptr = (void *) __get_free_pages(gfp, get_order(ring_size));
573 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
575 struct io_uring_buf_reg reg;
576 struct io_buffer_list *bl, *free_bl = NULL;
579 lockdep_assert_held(&ctx->uring_lock);
581 if (copy_from_user(®, arg, sizeof(reg)))
584 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
586 if (reg.flags & ~IOU_PBUF_RING_MMAP)
588 if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
591 if (reg.ring_addr & ~PAGE_MASK)
598 if (!is_power_of_2(reg.ring_entries))
601 /* cannot disambiguate full vs empty due to head/tail size */
602 if (reg.ring_entries >= 65536)
605 if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
606 int ret = io_init_bl_list(ctx);
611 bl = io_buffer_get_list(ctx, reg.bgid);
613 /* if mapped buffer ring OR classic exists, don't allow */
614 if (bl->is_mapped || !list_empty(&bl->buf_list))
617 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
622 if (!(reg.flags & IOU_PBUF_RING_MMAP))
623 ret = io_pin_pbuf_ring(®, bl);
625 ret = io_alloc_pbuf_ring(®, bl);
628 bl->nr_entries = reg.ring_entries;
629 bl->mask = reg.ring_entries - 1;
631 io_buffer_add_list(ctx, bl, reg.bgid);
635 kfree_rcu(free_bl, rcu);
639 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
641 struct io_uring_buf_reg reg;
642 struct io_buffer_list *bl;
644 lockdep_assert_held(&ctx->uring_lock);
646 if (copy_from_user(®, arg, sizeof(reg)))
648 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
653 bl = io_buffer_get_list(ctx, reg.bgid);
659 __io_remove_buffers(ctx, bl, -1U);
660 if (bl->bgid >= BGID_ARRAY) {
661 xa_erase(&ctx->io_bl_xa, bl->bgid);
667 void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid)
669 struct io_buffer_list *bl;
671 bl = __io_buffer_get_list(ctx, smp_load_acquire(&ctx->io_bl), bgid);
674 * Ensure the list is fully setup. Only strictly needed for RCU lookup
675 * via mmap, and in that case only for the array indexed groups. For
676 * the xarray lookups, it's either visible and ready, or not at all.
678 if (!smp_load_acquire(&bl->is_ready))
680 if (!bl || !bl->is_mmap)