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 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
67 bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
69 struct io_ring_ctx *ctx = req->ctx;
70 struct io_buffer_list *bl;
71 struct io_buffer *buf;
73 io_ring_submit_lock(ctx, issue_flags);
76 bl = io_buffer_get_list(ctx, buf->bgid);
77 list_add(&buf->list, &bl->buf_list);
78 req->flags &= ~REQ_F_BUFFER_SELECTED;
79 req->buf_index = buf->bgid;
81 io_ring_submit_unlock(ctx, issue_flags);
85 void __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
88 * We can add this buffer back to two lists:
90 * 1) The io_buffers_cache list. This one is protected by the
91 * ctx->uring_lock. If we already hold this lock, add back to this
92 * list as we can grab it from issue as well.
93 * 2) The io_buffers_comp list. This one is protected by the
94 * ctx->completion_lock.
96 * We migrate buffers from the comp_list to the issue cache list
99 if (issue_flags & IO_URING_F_UNLOCKED) {
100 struct io_ring_ctx *ctx = req->ctx;
102 spin_lock(&ctx->completion_lock);
103 __io_put_kbuf_list(req, &ctx->io_buffers_comp);
104 spin_unlock(&ctx->completion_lock);
106 lockdep_assert_held(&req->ctx->uring_lock);
108 __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
112 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
113 struct io_buffer_list *bl)
115 if (!list_empty(&bl->buf_list)) {
116 struct io_buffer *kbuf;
118 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
119 list_del(&kbuf->list);
120 if (*len == 0 || *len > kbuf->len)
122 if (list_empty(&bl->buf_list))
123 req->flags |= REQ_F_BL_EMPTY;
124 req->flags |= REQ_F_BUFFER_SELECTED;
126 req->buf_index = kbuf->bid;
127 return u64_to_user_ptr(kbuf->addr);
132 static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
133 struct io_buffer_list *bl,
134 unsigned int issue_flags)
136 struct io_uring_buf_ring *br = bl->buf_ring;
137 __u16 tail, head = bl->head;
138 struct io_uring_buf *buf;
140 tail = smp_load_acquire(&br->tail);
141 if (unlikely(tail == head))
144 if (head + 1 == tail)
145 req->flags |= REQ_F_BL_EMPTY;
148 /* mmaped buffers are always contig */
149 if (bl->is_mmap || 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]);
157 if (*len == 0 || *len > buf->len)
159 req->flags |= REQ_F_BUFFER_RING;
161 req->buf_index = buf->bid;
163 if (issue_flags & IO_URING_F_UNLOCKED || !io_file_can_poll(req)) {
165 * If we came in unlocked, we have no choice but to consume the
166 * buffer here, otherwise nothing ensures that the buffer won't
167 * get used by others. This does mean it'll be pinned until the
168 * IO completes, coming in unlocked means we're being called from
169 * io-wq context and there may be further retries in async hybrid
170 * mode. For the locked case, the caller must call commit when
171 * the transfer completes (or if we get -EAGAIN and must poll of
174 req->buf_list = NULL;
177 return u64_to_user_ptr(buf->addr);
180 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
181 unsigned int issue_flags)
183 struct io_ring_ctx *ctx = req->ctx;
184 struct io_buffer_list *bl;
185 void __user *ret = NULL;
187 io_ring_submit_lock(req->ctx, issue_flags);
189 bl = io_buffer_get_list(ctx, req->buf_index);
192 ret = io_ring_buffer_select(req, len, bl, issue_flags);
194 ret = io_provided_buffer_select(req, len, bl);
196 io_ring_submit_unlock(req->ctx, issue_flags);
201 * Mark the given mapped range as free for reuse
203 static void io_kbuf_mark_free(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
205 struct io_buf_free *ibf;
207 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
208 if (bl->buf_ring == ibf->mem) {
214 /* can't happen... */
218 static int __io_remove_buffers(struct io_ring_ctx *ctx,
219 struct io_buffer_list *bl, unsigned nbufs)
223 /* shouldn't happen */
227 if (bl->is_buf_ring) {
228 i = bl->buf_ring->tail - bl->head;
231 * io_kbuf_list_free() will free the page(s) at
234 io_kbuf_mark_free(ctx, bl);
237 } else if (bl->buf_nr_pages) {
240 for (j = 0; j < bl->buf_nr_pages; j++)
241 unpin_user_page(bl->buf_pages[j]);
242 kvfree(bl->buf_pages);
243 bl->buf_pages = NULL;
244 bl->buf_nr_pages = 0;
246 /* make sure it's seen as empty */
247 INIT_LIST_HEAD(&bl->buf_list);
252 /* protects io_buffers_cache */
253 lockdep_assert_held(&ctx->uring_lock);
255 while (!list_empty(&bl->buf_list)) {
256 struct io_buffer *nxt;
258 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
259 list_move(&nxt->list, &ctx->io_buffers_cache);
268 void io_destroy_buffers(struct io_ring_ctx *ctx)
270 struct io_buffer_list *bl;
271 struct list_head *item, *tmp;
272 struct io_buffer *buf;
275 xa_for_each(&ctx->io_bl_xa, index, bl) {
276 xa_erase(&ctx->io_bl_xa, bl->bgid);
277 __io_remove_buffers(ctx, bl, -1U);
282 * Move deferred locked entries to cache before pruning
284 spin_lock(&ctx->completion_lock);
285 if (!list_empty(&ctx->io_buffers_comp))
286 list_splice_init(&ctx->io_buffers_comp, &ctx->io_buffers_cache);
287 spin_unlock(&ctx->completion_lock);
289 list_for_each_safe(item, tmp, &ctx->io_buffers_cache) {
290 buf = list_entry(item, struct io_buffer, list);
291 kmem_cache_free(io_buf_cachep, buf);
295 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
297 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
300 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
304 tmp = READ_ONCE(sqe->fd);
305 if (!tmp || tmp > MAX_BIDS_PER_BGID)
308 memset(p, 0, sizeof(*p));
310 p->bgid = READ_ONCE(sqe->buf_group);
314 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
316 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
317 struct io_ring_ctx *ctx = req->ctx;
318 struct io_buffer_list *bl;
321 io_ring_submit_lock(ctx, issue_flags);
324 bl = io_buffer_get_list(ctx, p->bgid);
327 /* can't use provide/remove buffers command on mapped buffers */
328 if (!bl->is_buf_ring)
329 ret = __io_remove_buffers(ctx, bl, p->nbufs);
331 io_ring_submit_unlock(ctx, issue_flags);
334 io_req_set_res(req, ret, 0);
338 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
340 unsigned long size, tmp_check;
341 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
344 if (sqe->rw_flags || sqe->splice_fd_in)
347 tmp = READ_ONCE(sqe->fd);
348 if (!tmp || tmp > MAX_BIDS_PER_BGID)
351 p->addr = READ_ONCE(sqe->addr);
352 p->len = READ_ONCE(sqe->len);
354 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
357 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
360 size = (unsigned long)p->len * p->nbufs;
361 if (!access_ok(u64_to_user_ptr(p->addr), size))
364 p->bgid = READ_ONCE(sqe->buf_group);
365 tmp = READ_ONCE(sqe->off);
368 if (tmp + p->nbufs > MAX_BIDS_PER_BGID)
374 #define IO_BUFFER_ALLOC_BATCH 64
376 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
378 struct io_buffer *bufs[IO_BUFFER_ALLOC_BATCH];
382 * Completions that don't happen inline (eg not under uring_lock) will
383 * add to ->io_buffers_comp. If we don't have any free buffers, check
384 * the completion list and splice those entries first.
386 if (!list_empty_careful(&ctx->io_buffers_comp)) {
387 spin_lock(&ctx->completion_lock);
388 if (!list_empty(&ctx->io_buffers_comp)) {
389 list_splice_init(&ctx->io_buffers_comp,
390 &ctx->io_buffers_cache);
391 spin_unlock(&ctx->completion_lock);
394 spin_unlock(&ctx->completion_lock);
398 * No free buffers and no completion entries either. Allocate a new
399 * batch of buffer entries and add those to our freelist.
402 allocated = kmem_cache_alloc_bulk(io_buf_cachep, GFP_KERNEL_ACCOUNT,
403 ARRAY_SIZE(bufs), (void **) bufs);
404 if (unlikely(!allocated)) {
406 * Bulk alloc is all-or-nothing. If we fail to get a batch,
407 * retry single alloc to be on the safe side.
409 bufs[0] = kmem_cache_alloc(io_buf_cachep, GFP_KERNEL);
416 list_add_tail(&bufs[--allocated]->list, &ctx->io_buffers_cache);
421 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
422 struct io_buffer_list *bl)
424 struct io_buffer *buf;
425 u64 addr = pbuf->addr;
426 int i, bid = pbuf->bid;
428 for (i = 0; i < pbuf->nbufs; i++) {
429 if (list_empty(&ctx->io_buffers_cache) &&
430 io_refill_buffer_cache(ctx))
432 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
434 list_move_tail(&buf->list, &bl->buf_list);
436 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
438 buf->bgid = pbuf->bgid;
444 return i ? 0 : -ENOMEM;
447 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
449 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
450 struct io_ring_ctx *ctx = req->ctx;
451 struct io_buffer_list *bl;
454 io_ring_submit_lock(ctx, issue_flags);
456 bl = io_buffer_get_list(ctx, p->bgid);
458 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
463 INIT_LIST_HEAD(&bl->buf_list);
464 ret = io_buffer_add_list(ctx, bl, p->bgid);
467 * Doesn't need rcu free as it was never visible, but
468 * let's keep it consistent throughout.
474 /* can't add buffers via this command for a mapped buffer ring */
475 if (bl->is_buf_ring) {
480 ret = io_add_buffers(ctx, p, bl);
482 io_ring_submit_unlock(ctx, issue_flags);
486 io_req_set_res(req, ret, 0);
490 static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
491 struct io_buffer_list *bl)
493 struct io_uring_buf_ring *br;
497 pages = io_pin_pages(reg->ring_addr,
498 flex_array_size(br, bufs, reg->ring_entries),
501 return PTR_ERR(pages);
504 * Apparently some 32-bit boxes (ARM) will return highmem pages,
505 * which then need to be mapped. We could support that, but it'd
506 * complicate the code and slowdown the common cases quite a bit.
507 * So just error out, returning -EINVAL just like we did on kernels
508 * that didn't support mapped buffer rings.
510 for (i = 0; i < nr_pages; i++)
511 if (PageHighMem(pages[i]))
514 br = page_address(pages[0]);
517 * On platforms that have specific aliasing requirements, SHM_COLOUR
518 * is set and we must guarantee that the kernel and user side align
519 * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and
520 * the application mmap's the provided ring buffer. Fail the request
521 * if we, by chance, don't end up with aligned addresses. The app
522 * should use IOU_PBUF_RING_MMAP instead, and liburing will handle
523 * this transparently.
525 if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1))
528 bl->buf_pages = pages;
529 bl->buf_nr_pages = nr_pages;
535 for (i = 0; i < nr_pages; i++)
536 unpin_user_page(pages[i]);
542 * See if we have a suitable region that we can reuse, rather than allocate
543 * both a new io_buf_free and mem region again. We leave it on the list as
544 * even a reused entry will need freeing at ring release.
546 static struct io_buf_free *io_lookup_buf_free_entry(struct io_ring_ctx *ctx,
549 struct io_buf_free *ibf, *best = NULL;
552 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
555 if (ibf->inuse || ibf->size < ring_size)
557 dist = ibf->size - ring_size;
558 if (!best || dist < best_dist) {
569 static int io_alloc_pbuf_ring(struct io_ring_ctx *ctx,
570 struct io_uring_buf_reg *reg,
571 struct io_buffer_list *bl)
573 struct io_buf_free *ibf;
577 ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
579 /* Reuse existing entry, if we can */
580 ibf = io_lookup_buf_free_entry(ctx, ring_size);
582 ptr = io_mem_alloc(ring_size);
586 /* Allocate and store deferred free entry */
587 ibf = kmalloc(sizeof(*ibf), GFP_KERNEL_ACCOUNT);
593 ibf->size = ring_size;
594 hlist_add_head(&ibf->list, &ctx->io_buf_list);
597 bl->buf_ring = ibf->mem;
603 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
605 struct io_uring_buf_reg reg;
606 struct io_buffer_list *bl, *free_bl = NULL;
609 lockdep_assert_held(&ctx->uring_lock);
611 if (copy_from_user(®, arg, sizeof(reg)))
614 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
616 if (reg.flags & ~IOU_PBUF_RING_MMAP)
618 if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
621 if (reg.ring_addr & ~PAGE_MASK)
628 if (!is_power_of_2(reg.ring_entries))
631 /* cannot disambiguate full vs empty due to head/tail size */
632 if (reg.ring_entries >= 65536)
635 bl = io_buffer_get_list(ctx, reg.bgid);
637 /* if mapped buffer ring OR classic exists, don't allow */
638 if (bl->is_buf_ring || !list_empty(&bl->buf_list))
641 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
646 if (!(reg.flags & IOU_PBUF_RING_MMAP))
647 ret = io_pin_pbuf_ring(®, bl);
649 ret = io_alloc_pbuf_ring(ctx, ®, bl);
652 bl->nr_entries = reg.ring_entries;
653 bl->mask = reg.ring_entries - 1;
655 io_buffer_add_list(ctx, bl, reg.bgid);
659 kfree_rcu(free_bl, rcu);
663 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
665 struct io_uring_buf_reg reg;
666 struct io_buffer_list *bl;
668 lockdep_assert_held(&ctx->uring_lock);
670 if (copy_from_user(®, arg, sizeof(reg)))
672 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
677 bl = io_buffer_get_list(ctx, reg.bgid);
680 if (!bl->is_buf_ring)
683 __io_remove_buffers(ctx, bl, -1U);
684 xa_erase(&ctx->io_bl_xa, bl->bgid);
689 int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg)
691 struct io_uring_buf_status buf_status;
692 struct io_buffer_list *bl;
695 if (copy_from_user(&buf_status, arg, sizeof(buf_status)))
698 for (i = 0; i < ARRAY_SIZE(buf_status.resv); i++)
699 if (buf_status.resv[i])
702 bl = io_buffer_get_list(ctx, buf_status.buf_group);
705 if (!bl->is_buf_ring)
708 buf_status.head = bl->head;
709 if (copy_to_user(arg, &buf_status, sizeof(buf_status)))
715 void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid)
717 struct io_buffer_list *bl;
719 bl = __io_buffer_get_list(ctx, bgid);
721 if (!bl || !bl->is_mmap)
728 * Called at or after ->release(), free the mmap'ed buffers that we used
729 * for memory mapped provided buffer rings.
731 void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx)
733 struct io_buf_free *ibf;
734 struct hlist_node *tmp;
736 hlist_for_each_entry_safe(ibf, tmp, &ctx->io_buf_list, list) {
737 hlist_del(&ibf->list);
738 io_mem_free(ibf->mem);