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))
20 /* BIDs are addressed by a 16-bit field in a CQE */
21 #define MAX_BIDS_PER_BGID (1 << 16)
23 struct kmem_cache *io_buf_cachep;
25 struct io_provide_buf {
35 struct hlist_node list;
41 static inline struct io_buffer_list *__io_buffer_get_list(struct io_ring_ctx *ctx,
44 return xa_load(&ctx->io_bl_xa, bgid);
47 static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
50 lockdep_assert_held(&ctx->uring_lock);
52 return __io_buffer_get_list(ctx, bgid);
55 static int io_buffer_add_list(struct io_ring_ctx *ctx,
56 struct io_buffer_list *bl, unsigned int bgid)
59 * Store buffer group ID and finally mark the list as visible.
60 * The normal lookup doesn't care about the visibility as we're
61 * always under the ->uring_lock, but the RCU lookup from mmap does.
64 atomic_set(&bl->refs, 1);
65 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
68 bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
70 struct io_ring_ctx *ctx = req->ctx;
71 struct io_buffer_list *bl;
72 struct io_buffer *buf;
75 * For legacy provided buffer mode, don't recycle if we already did
76 * IO to this buffer. For ring-mapped provided buffer mode, we should
77 * increment ring->head to explicitly monopolize the buffer to avoid
80 if (req->flags & REQ_F_PARTIAL_IO)
83 io_ring_submit_lock(ctx, issue_flags);
86 bl = io_buffer_get_list(ctx, buf->bgid);
87 list_add(&buf->list, &bl->buf_list);
88 req->flags &= ~REQ_F_BUFFER_SELECTED;
89 req->buf_index = buf->bgid;
91 io_ring_submit_unlock(ctx, issue_flags);
95 unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
100 * We can add this buffer back to two lists:
102 * 1) The io_buffers_cache list. This one is protected by the
103 * ctx->uring_lock. If we already hold this lock, add back to this
104 * list as we can grab it from issue as well.
105 * 2) The io_buffers_comp list. This one is protected by the
106 * ctx->completion_lock.
108 * We migrate buffers from the comp_list to the issue cache list
111 if (req->flags & REQ_F_BUFFER_RING) {
112 /* no buffers to recycle for this case */
113 cflags = __io_put_kbuf_list(req, NULL);
114 } else if (issue_flags & IO_URING_F_UNLOCKED) {
115 struct io_ring_ctx *ctx = req->ctx;
117 spin_lock(&ctx->completion_lock);
118 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
119 spin_unlock(&ctx->completion_lock);
121 lockdep_assert_held(&req->ctx->uring_lock);
123 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
128 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
129 struct io_buffer_list *bl)
131 if (!list_empty(&bl->buf_list)) {
132 struct io_buffer *kbuf;
134 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
135 list_del(&kbuf->list);
136 if (*len == 0 || *len > kbuf->len)
138 req->flags |= REQ_F_BUFFER_SELECTED;
140 req->buf_index = kbuf->bid;
141 return u64_to_user_ptr(kbuf->addr);
146 static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
147 struct io_buffer_list *bl,
148 unsigned int issue_flags)
150 struct io_uring_buf_ring *br = bl->buf_ring;
151 struct io_uring_buf *buf;
152 __u16 head = bl->head;
154 if (unlikely(smp_load_acquire(&br->tail) == head))
158 /* mmaped buffers are always contig */
159 if (bl->is_mmap || head < IO_BUFFER_LIST_BUF_PER_PAGE) {
160 buf = &br->bufs[head];
162 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
163 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
164 buf = page_address(bl->buf_pages[index]);
167 if (*len == 0 || *len > buf->len)
169 req->flags |= REQ_F_BUFFER_RING;
171 req->buf_index = buf->bid;
173 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
175 * If we came in unlocked, we have no choice but to consume the
176 * buffer here, otherwise nothing ensures that the buffer won't
177 * get used by others. This does mean it'll be pinned until the
178 * IO completes, coming in unlocked means we're being called from
179 * io-wq context and there may be further retries in async hybrid
180 * mode. For the locked case, the caller must call commit when
181 * the transfer completes (or if we get -EAGAIN and must poll of
184 req->buf_list = NULL;
187 return u64_to_user_ptr(buf->addr);
190 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
191 unsigned int issue_flags)
193 struct io_ring_ctx *ctx = req->ctx;
194 struct io_buffer_list *bl;
195 void __user *ret = NULL;
197 io_ring_submit_lock(req->ctx, issue_flags);
199 bl = io_buffer_get_list(ctx, req->buf_index);
202 ret = io_ring_buffer_select(req, len, bl, issue_flags);
204 ret = io_provided_buffer_select(req, len, bl);
206 io_ring_submit_unlock(req->ctx, issue_flags);
211 * Mark the given mapped range as free for reuse
213 static void io_kbuf_mark_free(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
215 struct io_buf_free *ibf;
217 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
218 if (bl->buf_ring == ibf->mem) {
224 /* can't happen... */
228 static int __io_remove_buffers(struct io_ring_ctx *ctx,
229 struct io_buffer_list *bl, unsigned nbufs)
233 /* shouldn't happen */
238 i = bl->buf_ring->tail - bl->head;
241 * io_kbuf_list_free() will free the page(s) at
244 io_kbuf_mark_free(ctx, bl);
247 } else if (bl->buf_nr_pages) {
250 for (j = 0; j < bl->buf_nr_pages; j++)
251 unpin_user_page(bl->buf_pages[j]);
252 kvfree(bl->buf_pages);
253 bl->buf_pages = NULL;
254 bl->buf_nr_pages = 0;
256 /* make sure it's seen as empty */
257 INIT_LIST_HEAD(&bl->buf_list);
262 /* protects io_buffers_cache */
263 lockdep_assert_held(&ctx->uring_lock);
265 while (!list_empty(&bl->buf_list)) {
266 struct io_buffer *nxt;
268 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
269 list_move(&nxt->list, &ctx->io_buffers_cache);
278 void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
280 if (atomic_dec_and_test(&bl->refs)) {
281 __io_remove_buffers(ctx, bl, -1U);
286 void io_destroy_buffers(struct io_ring_ctx *ctx)
288 struct io_buffer_list *bl;
289 struct list_head *item, *tmp;
290 struct io_buffer *buf;
293 xa_for_each(&ctx->io_bl_xa, index, bl) {
294 xa_erase(&ctx->io_bl_xa, bl->bgid);
299 * Move deferred locked entries to cache before pruning
301 spin_lock(&ctx->completion_lock);
302 if (!list_empty(&ctx->io_buffers_comp))
303 list_splice_init(&ctx->io_buffers_comp, &ctx->io_buffers_cache);
304 spin_unlock(&ctx->completion_lock);
306 list_for_each_safe(item, tmp, &ctx->io_buffers_cache) {
307 buf = list_entry(item, struct io_buffer, list);
308 kmem_cache_free(io_buf_cachep, buf);
312 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
314 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
317 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
321 tmp = READ_ONCE(sqe->fd);
322 if (!tmp || tmp > MAX_BIDS_PER_BGID)
325 memset(p, 0, sizeof(*p));
327 p->bgid = READ_ONCE(sqe->buf_group);
331 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
333 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
334 struct io_ring_ctx *ctx = req->ctx;
335 struct io_buffer_list *bl;
338 io_ring_submit_lock(ctx, issue_flags);
341 bl = io_buffer_get_list(ctx, p->bgid);
344 /* can't use provide/remove buffers command on mapped buffers */
346 ret = __io_remove_buffers(ctx, bl, p->nbufs);
348 io_ring_submit_unlock(ctx, issue_flags);
351 io_req_set_res(req, ret, 0);
355 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
357 unsigned long size, tmp_check;
358 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
361 if (sqe->rw_flags || sqe->splice_fd_in)
364 tmp = READ_ONCE(sqe->fd);
365 if (!tmp || tmp > MAX_BIDS_PER_BGID)
368 p->addr = READ_ONCE(sqe->addr);
369 p->len = READ_ONCE(sqe->len);
371 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
374 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
377 size = (unsigned long)p->len * p->nbufs;
378 if (!access_ok(u64_to_user_ptr(p->addr), size))
381 p->bgid = READ_ONCE(sqe->buf_group);
382 tmp = READ_ONCE(sqe->off);
385 if (tmp + p->nbufs > MAX_BIDS_PER_BGID)
391 #define IO_BUFFER_ALLOC_BATCH 64
393 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
395 struct io_buffer *bufs[IO_BUFFER_ALLOC_BATCH];
399 * Completions that don't happen inline (eg not under uring_lock) will
400 * add to ->io_buffers_comp. If we don't have any free buffers, check
401 * the completion list and splice those entries first.
403 if (!list_empty_careful(&ctx->io_buffers_comp)) {
404 spin_lock(&ctx->completion_lock);
405 if (!list_empty(&ctx->io_buffers_comp)) {
406 list_splice_init(&ctx->io_buffers_comp,
407 &ctx->io_buffers_cache);
408 spin_unlock(&ctx->completion_lock);
411 spin_unlock(&ctx->completion_lock);
415 * No free buffers and no completion entries either. Allocate a new
416 * batch of buffer entries and add those to our freelist.
419 allocated = kmem_cache_alloc_bulk(io_buf_cachep, GFP_KERNEL_ACCOUNT,
420 ARRAY_SIZE(bufs), (void **) bufs);
421 if (unlikely(!allocated)) {
423 * Bulk alloc is all-or-nothing. If we fail to get a batch,
424 * retry single alloc to be on the safe side.
426 bufs[0] = kmem_cache_alloc(io_buf_cachep, GFP_KERNEL);
433 list_add_tail(&bufs[--allocated]->list, &ctx->io_buffers_cache);
438 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
439 struct io_buffer_list *bl)
441 struct io_buffer *buf;
442 u64 addr = pbuf->addr;
443 int i, bid = pbuf->bid;
445 for (i = 0; i < pbuf->nbufs; i++) {
446 if (list_empty(&ctx->io_buffers_cache) &&
447 io_refill_buffer_cache(ctx))
449 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
451 list_move_tail(&buf->list, &bl->buf_list);
453 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
455 buf->bgid = pbuf->bgid;
461 return i ? 0 : -ENOMEM;
464 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
466 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
467 struct io_ring_ctx *ctx = req->ctx;
468 struct io_buffer_list *bl;
471 io_ring_submit_lock(ctx, issue_flags);
473 bl = io_buffer_get_list(ctx, p->bgid);
475 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
480 INIT_LIST_HEAD(&bl->buf_list);
481 ret = io_buffer_add_list(ctx, bl, p->bgid);
484 * Doesn't need rcu free as it was never visible, but
485 * let's keep it consistent throughout.
491 /* can't add buffers via this command for a mapped buffer ring */
497 ret = io_add_buffers(ctx, p, bl);
499 io_ring_submit_unlock(ctx, issue_flags);
503 io_req_set_res(req, ret, 0);
507 static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
508 struct io_buffer_list *bl)
510 struct io_uring_buf_ring *br;
514 pages = io_pin_pages(reg->ring_addr,
515 flex_array_size(br, bufs, reg->ring_entries),
518 return PTR_ERR(pages);
521 * Apparently some 32-bit boxes (ARM) will return highmem pages,
522 * which then need to be mapped. We could support that, but it'd
523 * complicate the code and slowdown the common cases quite a bit.
524 * So just error out, returning -EINVAL just like we did on kernels
525 * that didn't support mapped buffer rings.
527 for (i = 0; i < nr_pages; i++)
528 if (PageHighMem(pages[i]))
531 br = page_address(pages[0]);
534 * On platforms that have specific aliasing requirements, SHM_COLOUR
535 * is set and we must guarantee that the kernel and user side align
536 * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and
537 * the application mmap's the provided ring buffer. Fail the request
538 * if we, by chance, don't end up with aligned addresses. The app
539 * should use IOU_PBUF_RING_MMAP instead, and liburing will handle
540 * this transparently.
542 if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1))
545 bl->buf_pages = pages;
546 bl->buf_nr_pages = nr_pages;
552 for (i = 0; i < nr_pages; i++)
553 unpin_user_page(pages[i]);
559 * See if we have a suitable region that we can reuse, rather than allocate
560 * both a new io_buf_free and mem region again. We leave it on the list as
561 * even a reused entry will need freeing at ring release.
563 static struct io_buf_free *io_lookup_buf_free_entry(struct io_ring_ctx *ctx,
566 struct io_buf_free *ibf, *best = NULL;
569 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
572 if (ibf->inuse || ibf->size < ring_size)
574 dist = ibf->size - ring_size;
575 if (!best || dist < best_dist) {
586 static int io_alloc_pbuf_ring(struct io_ring_ctx *ctx,
587 struct io_uring_buf_reg *reg,
588 struct io_buffer_list *bl)
590 struct io_buf_free *ibf;
594 ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
596 /* Reuse existing entry, if we can */
597 ibf = io_lookup_buf_free_entry(ctx, ring_size);
599 ptr = io_mem_alloc(ring_size);
603 /* Allocate and store deferred free entry */
604 ibf = kmalloc(sizeof(*ibf), GFP_KERNEL_ACCOUNT);
610 ibf->size = ring_size;
611 hlist_add_head(&ibf->list, &ctx->io_buf_list);
614 bl->buf_ring = ibf->mem;
620 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
622 struct io_uring_buf_reg reg;
623 struct io_buffer_list *bl, *free_bl = NULL;
626 lockdep_assert_held(&ctx->uring_lock);
628 if (copy_from_user(®, arg, sizeof(reg)))
631 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
633 if (reg.flags & ~IOU_PBUF_RING_MMAP)
635 if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
638 if (reg.ring_addr & ~PAGE_MASK)
645 if (!is_power_of_2(reg.ring_entries))
648 /* cannot disambiguate full vs empty due to head/tail size */
649 if (reg.ring_entries >= 65536)
652 bl = io_buffer_get_list(ctx, reg.bgid);
654 /* if mapped buffer ring OR classic exists, don't allow */
655 if (bl->is_mapped || !list_empty(&bl->buf_list))
658 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
663 if (!(reg.flags & IOU_PBUF_RING_MMAP))
664 ret = io_pin_pbuf_ring(®, bl);
666 ret = io_alloc_pbuf_ring(ctx, ®, bl);
669 bl->nr_entries = reg.ring_entries;
670 bl->mask = reg.ring_entries - 1;
672 io_buffer_add_list(ctx, bl, reg.bgid);
676 kfree_rcu(free_bl, rcu);
680 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
682 struct io_uring_buf_reg reg;
683 struct io_buffer_list *bl;
685 lockdep_assert_held(&ctx->uring_lock);
687 if (copy_from_user(®, arg, sizeof(reg)))
689 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
694 bl = io_buffer_get_list(ctx, reg.bgid);
700 xa_erase(&ctx->io_bl_xa, bl->bgid);
705 int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg)
707 struct io_uring_buf_status buf_status;
708 struct io_buffer_list *bl;
711 if (copy_from_user(&buf_status, arg, sizeof(buf_status)))
714 for (i = 0; i < ARRAY_SIZE(buf_status.resv); i++)
715 if (buf_status.resv[i])
718 bl = io_buffer_get_list(ctx, buf_status.buf_group);
724 buf_status.head = bl->head;
725 if (copy_to_user(arg, &buf_status, sizeof(buf_status)))
731 struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx,
734 struct io_buffer_list *bl;
738 * We have to be a bit careful here - we're inside mmap and cannot grab
739 * the uring_lock. This means the buffer_list could be simultaneously
740 * going away, if someone is trying to be sneaky. Look it up under rcu
741 * so we know it's not going away, and attempt to grab a reference to
742 * it. If the ref is already zero, then fail the mapping. If successful,
743 * the caller will call io_put_bl() to drop the the reference at at the
744 * end. This may then safely free the buffer_list (and drop the pages)
745 * at that point, vm_insert_pages() would've already grabbed the
746 * necessary vma references.
749 bl = xa_load(&ctx->io_bl_xa, bgid);
750 /* must be a mmap'able buffer ring and have pages */
752 if (bl && bl->is_mmap)
753 ret = atomic_inc_not_zero(&bl->refs);
759 return ERR_PTR(-EINVAL);
763 * Called at or after ->release(), free the mmap'ed buffers that we used
764 * for memory mapped provided buffer rings.
766 void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx)
768 struct io_buf_free *ibf;
769 struct hlist_node *tmp;
771 hlist_for_each_entry_safe(ibf, tmp, &ctx->io_buf_list, list) {
772 hlist_del(&ibf->list);
773 io_mem_free(ibf->mem);