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 kmem_cache *io_buf_cachep;
27 struct io_provide_buf {
37 struct hlist_node list;
43 static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
46 if (ctx->io_bl && bgid < BGID_ARRAY)
47 return &ctx->io_bl[bgid];
49 return xa_load(&ctx->io_bl_xa, bgid);
52 static int io_buffer_add_list(struct io_ring_ctx *ctx,
53 struct io_buffer_list *bl, unsigned int bgid)
56 if (bgid < BGID_ARRAY)
59 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
62 bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
64 struct io_ring_ctx *ctx = req->ctx;
65 struct io_buffer_list *bl;
66 struct io_buffer *buf;
69 * For legacy provided buffer mode, don't recycle if we already did
70 * IO to this buffer. For ring-mapped provided buffer mode, we should
71 * increment ring->head to explicitly monopolize the buffer to avoid
74 if (req->flags & REQ_F_PARTIAL_IO)
77 io_ring_submit_lock(ctx, issue_flags);
80 bl = io_buffer_get_list(ctx, buf->bgid);
81 list_add(&buf->list, &bl->buf_list);
82 req->flags &= ~REQ_F_BUFFER_SELECTED;
83 req->buf_index = buf->bgid;
85 io_ring_submit_unlock(ctx, issue_flags);
89 unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
94 * We can add this buffer back to two lists:
96 * 1) The io_buffers_cache list. This one is protected by the
97 * ctx->uring_lock. If we already hold this lock, add back to this
98 * list as we can grab it from issue as well.
99 * 2) The io_buffers_comp list. This one is protected by the
100 * ctx->completion_lock.
102 * We migrate buffers from the comp_list to the issue cache list
105 if (req->flags & REQ_F_BUFFER_RING) {
106 /* no buffers to recycle for this case */
107 cflags = __io_put_kbuf_list(req, NULL);
108 } else if (issue_flags & IO_URING_F_UNLOCKED) {
109 struct io_ring_ctx *ctx = req->ctx;
111 spin_lock(&ctx->completion_lock);
112 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
113 spin_unlock(&ctx->completion_lock);
115 lockdep_assert_held(&req->ctx->uring_lock);
117 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
122 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
123 struct io_buffer_list *bl)
125 if (!list_empty(&bl->buf_list)) {
126 struct io_buffer *kbuf;
128 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
129 list_del(&kbuf->list);
130 if (*len == 0 || *len > kbuf->len)
132 req->flags |= REQ_F_BUFFER_SELECTED;
134 req->buf_index = kbuf->bid;
135 return u64_to_user_ptr(kbuf->addr);
140 static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
141 struct io_buffer_list *bl,
142 unsigned int issue_flags)
144 struct io_uring_buf_ring *br = bl->buf_ring;
145 struct io_uring_buf *buf;
146 __u16 head = bl->head;
148 if (unlikely(smp_load_acquire(&br->tail) == head))
152 /* mmaped buffers are always contig */
153 if (bl->is_mmap || head < IO_BUFFER_LIST_BUF_PER_PAGE) {
154 buf = &br->bufs[head];
156 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
157 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
158 buf = page_address(bl->buf_pages[index]);
161 if (*len == 0 || *len > buf->len)
163 req->flags |= REQ_F_BUFFER_RING;
165 req->buf_index = buf->bid;
167 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
169 * If we came in unlocked, we have no choice but to consume the
170 * buffer here, otherwise nothing ensures that the buffer won't
171 * get used by others. This does mean it'll be pinned until the
172 * IO completes, coming in unlocked means we're being called from
173 * io-wq context and there may be further retries in async hybrid
174 * mode. For the locked case, the caller must call commit when
175 * the transfer completes (or if we get -EAGAIN and must poll of
178 req->buf_list = NULL;
181 return u64_to_user_ptr(buf->addr);
184 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
185 unsigned int issue_flags)
187 struct io_ring_ctx *ctx = req->ctx;
188 struct io_buffer_list *bl;
189 void __user *ret = NULL;
191 io_ring_submit_lock(req->ctx, issue_flags);
193 bl = io_buffer_get_list(ctx, req->buf_index);
196 ret = io_ring_buffer_select(req, len, bl, issue_flags);
198 ret = io_provided_buffer_select(req, len, bl);
200 io_ring_submit_unlock(req->ctx, issue_flags);
204 static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
208 ctx->io_bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list),
213 for (i = 0; i < BGID_ARRAY; i++) {
214 INIT_LIST_HEAD(&ctx->io_bl[i].buf_list);
215 ctx->io_bl[i].bgid = i;
222 * Mark the given mapped range as free for reuse
224 static void io_kbuf_mark_free(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
226 struct io_buf_free *ibf;
228 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
229 if (bl->buf_ring == ibf->mem) {
235 /* can't happen... */
239 static int __io_remove_buffers(struct io_ring_ctx *ctx,
240 struct io_buffer_list *bl, unsigned nbufs)
244 /* shouldn't happen */
249 i = bl->buf_ring->tail - bl->head;
252 * io_kbuf_list_free() will free the page(s) at
255 io_kbuf_mark_free(ctx, bl);
258 } else if (bl->buf_nr_pages) {
261 for (j = 0; j < bl->buf_nr_pages; j++)
262 unpin_user_page(bl->buf_pages[j]);
263 kvfree(bl->buf_pages);
264 bl->buf_pages = NULL;
265 bl->buf_nr_pages = 0;
267 /* make sure it's seen as empty */
268 INIT_LIST_HEAD(&bl->buf_list);
273 /* protects io_buffers_cache */
274 lockdep_assert_held(&ctx->uring_lock);
276 while (!list_empty(&bl->buf_list)) {
277 struct io_buffer *nxt;
279 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
280 list_move(&nxt->list, &ctx->io_buffers_cache);
289 void io_destroy_buffers(struct io_ring_ctx *ctx)
291 struct io_buffer_list *bl;
292 struct list_head *item, *tmp;
293 struct io_buffer *buf;
297 for (i = 0; i < BGID_ARRAY; i++) {
300 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
303 xa_for_each(&ctx->io_bl_xa, index, bl) {
304 xa_erase(&ctx->io_bl_xa, bl->bgid);
305 __io_remove_buffers(ctx, bl, -1U);
309 list_for_each_safe(item, tmp, &ctx->io_buffers_cache) {
310 buf = list_entry(item, struct io_buffer, list);
311 kmem_cache_free(io_buf_cachep, buf);
315 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
317 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
320 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
324 tmp = READ_ONCE(sqe->fd);
325 if (!tmp || tmp > MAX_BIDS_PER_BGID)
328 memset(p, 0, sizeof(*p));
330 p->bgid = READ_ONCE(sqe->buf_group);
334 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
336 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
337 struct io_ring_ctx *ctx = req->ctx;
338 struct io_buffer_list *bl;
341 io_ring_submit_lock(ctx, issue_flags);
344 bl = io_buffer_get_list(ctx, p->bgid);
347 /* can't use provide/remove buffers command on mapped buffers */
349 ret = __io_remove_buffers(ctx, bl, p->nbufs);
351 io_ring_submit_unlock(ctx, issue_flags);
354 io_req_set_res(req, ret, 0);
358 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
360 unsigned long size, tmp_check;
361 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
364 if (sqe->rw_flags || sqe->splice_fd_in)
367 tmp = READ_ONCE(sqe->fd);
368 if (!tmp || tmp > MAX_BIDS_PER_BGID)
371 p->addr = READ_ONCE(sqe->addr);
372 p->len = READ_ONCE(sqe->len);
374 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
377 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
380 size = (unsigned long)p->len * p->nbufs;
381 if (!access_ok(u64_to_user_ptr(p->addr), size))
384 p->bgid = READ_ONCE(sqe->buf_group);
385 tmp = READ_ONCE(sqe->off);
388 if (tmp + p->nbufs > MAX_BIDS_PER_BGID)
394 #define IO_BUFFER_ALLOC_BATCH 64
396 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
398 struct io_buffer *bufs[IO_BUFFER_ALLOC_BATCH];
402 * Completions that don't happen inline (eg not under uring_lock) will
403 * add to ->io_buffers_comp. If we don't have any free buffers, check
404 * the completion list and splice those entries first.
406 if (!list_empty_careful(&ctx->io_buffers_comp)) {
407 spin_lock(&ctx->completion_lock);
408 if (!list_empty(&ctx->io_buffers_comp)) {
409 list_splice_init(&ctx->io_buffers_comp,
410 &ctx->io_buffers_cache);
411 spin_unlock(&ctx->completion_lock);
414 spin_unlock(&ctx->completion_lock);
418 * No free buffers and no completion entries either. Allocate a new
419 * batch of buffer entries and add those to our freelist.
422 allocated = kmem_cache_alloc_bulk(io_buf_cachep, GFP_KERNEL_ACCOUNT,
423 ARRAY_SIZE(bufs), (void **) bufs);
424 if (unlikely(!allocated)) {
426 * Bulk alloc is all-or-nothing. If we fail to get a batch,
427 * retry single alloc to be on the safe side.
429 bufs[0] = kmem_cache_alloc(io_buf_cachep, GFP_KERNEL);
436 list_add_tail(&bufs[--allocated]->list, &ctx->io_buffers_cache);
441 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
442 struct io_buffer_list *bl)
444 struct io_buffer *buf;
445 u64 addr = pbuf->addr;
446 int i, bid = pbuf->bid;
448 for (i = 0; i < pbuf->nbufs; i++) {
449 if (list_empty(&ctx->io_buffers_cache) &&
450 io_refill_buffer_cache(ctx))
452 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
454 list_move_tail(&buf->list, &bl->buf_list);
456 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
458 buf->bgid = pbuf->bgid;
464 return i ? 0 : -ENOMEM;
467 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
469 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
470 struct io_ring_ctx *ctx = req->ctx;
471 struct io_buffer_list *bl;
474 io_ring_submit_lock(ctx, issue_flags);
476 if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
477 ret = io_init_bl_list(ctx);
482 bl = io_buffer_get_list(ctx, p->bgid);
484 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
489 INIT_LIST_HEAD(&bl->buf_list);
490 ret = io_buffer_add_list(ctx, bl, p->bgid);
496 /* can't add buffers via this command for a mapped buffer ring */
502 ret = io_add_buffers(ctx, p, bl);
504 io_ring_submit_unlock(ctx, issue_flags);
508 io_req_set_res(req, ret, 0);
512 static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
513 struct io_buffer_list *bl)
515 struct io_uring_buf_ring *br;
519 pages = io_pin_pages(reg->ring_addr,
520 flex_array_size(br, bufs, reg->ring_entries),
523 return PTR_ERR(pages);
526 * Apparently some 32-bit boxes (ARM) will return highmem pages,
527 * which then need to be mapped. We could support that, but it'd
528 * complicate the code and slowdown the common cases quite a bit.
529 * So just error out, returning -EINVAL just like we did on kernels
530 * that didn't support mapped buffer rings.
532 for (i = 0; i < nr_pages; i++)
533 if (PageHighMem(pages[i]))
536 br = page_address(pages[0]);
539 * On platforms that have specific aliasing requirements, SHM_COLOUR
540 * is set and we must guarantee that the kernel and user side align
541 * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and
542 * the application mmap's the provided ring buffer. Fail the request
543 * if we, by chance, don't end up with aligned addresses. The app
544 * should use IOU_PBUF_RING_MMAP instead, and liburing will handle
545 * this transparently.
547 if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1))
550 bl->buf_pages = pages;
551 bl->buf_nr_pages = nr_pages;
557 for (i = 0; i < nr_pages; i++)
558 unpin_user_page(pages[i]);
564 * See if we have a suitable region that we can reuse, rather than allocate
565 * both a new io_buf_free and mem region again. We leave it on the list as
566 * even a reused entry will need freeing at ring release.
568 static struct io_buf_free *io_lookup_buf_free_entry(struct io_ring_ctx *ctx,
571 struct io_buf_free *ibf, *best = NULL;
574 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
577 if (ibf->inuse || ibf->size < ring_size)
579 dist = ibf->size - ring_size;
580 if (!best || dist < best_dist) {
591 static int io_alloc_pbuf_ring(struct io_ring_ctx *ctx,
592 struct io_uring_buf_reg *reg,
593 struct io_buffer_list *bl)
595 struct io_buf_free *ibf;
599 ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
601 /* Reuse existing entry, if we can */
602 ibf = io_lookup_buf_free_entry(ctx, ring_size);
604 ptr = io_mem_alloc(ring_size);
608 /* Allocate and store deferred free entry */
609 ibf = kmalloc(sizeof(*ibf), GFP_KERNEL_ACCOUNT);
615 ibf->size = ring_size;
616 hlist_add_head(&ibf->list, &ctx->io_buf_list);
619 bl->buf_ring = ibf->mem;
625 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
627 struct io_uring_buf_reg reg;
628 struct io_buffer_list *bl, *free_bl = NULL;
631 if (copy_from_user(®, arg, sizeof(reg)))
634 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
636 if (reg.flags & ~IOU_PBUF_RING_MMAP)
638 if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
641 if (reg.ring_addr & ~PAGE_MASK)
648 if (!is_power_of_2(reg.ring_entries))
651 /* cannot disambiguate full vs empty due to head/tail size */
652 if (reg.ring_entries >= 65536)
655 if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
656 int ret = io_init_bl_list(ctx);
661 bl = io_buffer_get_list(ctx, reg.bgid);
663 /* if mapped buffer ring OR classic exists, don't allow */
664 if (bl->is_mapped || !list_empty(&bl->buf_list))
667 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
672 if (!(reg.flags & IOU_PBUF_RING_MMAP))
673 ret = io_pin_pbuf_ring(®, bl);
675 ret = io_alloc_pbuf_ring(ctx, ®, bl);
678 bl->nr_entries = reg.ring_entries;
679 bl->mask = reg.ring_entries - 1;
681 io_buffer_add_list(ctx, bl, reg.bgid);
689 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
691 struct io_uring_buf_reg reg;
692 struct io_buffer_list *bl;
694 if (copy_from_user(®, arg, sizeof(reg)))
696 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
701 bl = io_buffer_get_list(ctx, reg.bgid);
707 __io_remove_buffers(ctx, bl, -1U);
708 if (bl->bgid >= BGID_ARRAY) {
709 xa_erase(&ctx->io_bl_xa, bl->bgid);
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);
720 if (!bl || !bl->is_mmap)
727 * Called at or after ->release(), free the mmap'ed buffers that we used
728 * for memory mapped provided buffer rings.
730 void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx)
732 struct io_buf_free *ibf;
733 struct hlist_node *tmp;
735 hlist_for_each_entry_safe(ibf, tmp, &ctx->io_buf_list, list) {
736 hlist_del(&ibf->list);
737 io_mem_free(ibf->mem);