Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/fs.h>
5#include <linux/file.h>
6#include <linux/mm.h>
7#include <linux/slab.h>
8#include <linux/namei.h>
9#include <linux/poll.h>
10#include <linux/io_uring.h>
11
12#include <uapi/linux/io_uring.h>
13
14#include "io_uring.h"
15#include "opdef.h"
16#include "kbuf.h"
17
18#define IO_BUFFER_LIST_BUF_PER_PAGE (PAGE_SIZE / sizeof(struct io_uring_buf))
19
20#define BGID_ARRAY 64
21
22/* BIDs are addressed by a 16-bit field in a CQE */
23#define MAX_BIDS_PER_BGID (1 << 16)
24
25struct kmem_cache *io_buf_cachep;
26
27struct io_provide_buf {
28 struct file *file;
29 __u64 addr;
30 __u32 len;
31 __u32 bgid;
32 __u32 nbufs;
33 __u16 bid;
34};
35
36struct io_buf_free {
37 struct hlist_node list;
38 void *mem;
39 size_t size;
40 int inuse;
41};
42
43static struct io_buffer_list *__io_buffer_get_list(struct io_ring_ctx *ctx,
44 struct io_buffer_list *bl,
45 unsigned int bgid)
46{
47 if (bl && bgid < BGID_ARRAY)
48 return &bl[bgid];
49
50 return xa_load(&ctx->io_bl_xa, bgid);
51}
52
53static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
54 unsigned int bgid)
55{
56 lockdep_assert_held(&ctx->uring_lock);
57
58 return __io_buffer_get_list(ctx, ctx->io_bl, bgid);
59}
60
61static int io_buffer_add_list(struct io_ring_ctx *ctx,
62 struct io_buffer_list *bl, unsigned int bgid)
63{
64 /*
65 * Store buffer group ID and finally mark the list as visible.
66 * The normal lookup doesn't care about the visibility as we're
67 * always under the ->uring_lock, but the RCU lookup from mmap does.
68 */
69 bl->bgid = bgid;
70 smp_store_release(&bl->is_ready, 1);
71
72 if (bgid < BGID_ARRAY)
73 return 0;
74
75 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
76}
77
78bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
79{
80 struct io_ring_ctx *ctx = req->ctx;
81 struct io_buffer_list *bl;
82 struct io_buffer *buf;
83
84 /*
85 * For legacy provided buffer mode, don't recycle if we already did
86 * IO to this buffer. For ring-mapped provided buffer mode, we should
87 * increment ring->head to explicitly monopolize the buffer to avoid
88 * multiple use.
89 */
90 if (req->flags & REQ_F_PARTIAL_IO)
91 return false;
92
93 io_ring_submit_lock(ctx, issue_flags);
94
95 buf = req->kbuf;
96 bl = io_buffer_get_list(ctx, buf->bgid);
97 list_add(&buf->list, &bl->buf_list);
98 req->flags &= ~REQ_F_BUFFER_SELECTED;
99 req->buf_index = buf->bgid;
100
101 io_ring_submit_unlock(ctx, issue_flags);
102 return true;
103}
104
105unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
106{
107 unsigned int cflags;
108
109 /*
110 * We can add this buffer back to two lists:
111 *
112 * 1) The io_buffers_cache list. This one is protected by the
113 * ctx->uring_lock. If we already hold this lock, add back to this
114 * list as we can grab it from issue as well.
115 * 2) The io_buffers_comp list. This one is protected by the
116 * ctx->completion_lock.
117 *
118 * We migrate buffers from the comp_list to the issue cache list
119 * when we need one.
120 */
121 if (req->flags & REQ_F_BUFFER_RING) {
122 /* no buffers to recycle for this case */
123 cflags = __io_put_kbuf_list(req, NULL);
124 } else if (issue_flags & IO_URING_F_UNLOCKED) {
125 struct io_ring_ctx *ctx = req->ctx;
126
127 spin_lock(&ctx->completion_lock);
128 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
129 spin_unlock(&ctx->completion_lock);
130 } else {
131 lockdep_assert_held(&req->ctx->uring_lock);
132
133 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
134 }
135 return cflags;
136}
137
138static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
139 struct io_buffer_list *bl)
140{
141 if (!list_empty(&bl->buf_list)) {
142 struct io_buffer *kbuf;
143
144 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
145 list_del(&kbuf->list);
146 if (*len == 0 || *len > kbuf->len)
147 *len = kbuf->len;
148 req->flags |= REQ_F_BUFFER_SELECTED;
149 req->kbuf = kbuf;
150 req->buf_index = kbuf->bid;
151 return u64_to_user_ptr(kbuf->addr);
152 }
153 return NULL;
154}
155
156static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
157 struct io_buffer_list *bl,
158 unsigned int issue_flags)
159{
160 struct io_uring_buf_ring *br = bl->buf_ring;
161 struct io_uring_buf *buf;
162 __u16 head = bl->head;
163
164 if (unlikely(smp_load_acquire(&br->tail) == head))
165 return NULL;
166
167 head &= bl->mask;
168 /* mmaped buffers are always contig */
169 if (bl->is_mmap || head < IO_BUFFER_LIST_BUF_PER_PAGE) {
170 buf = &br->bufs[head];
171 } else {
172 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
173 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
174 buf = page_address(bl->buf_pages[index]);
175 buf += off;
176 }
177 if (*len == 0 || *len > buf->len)
178 *len = buf->len;
179 req->flags |= REQ_F_BUFFER_RING;
180 req->buf_list = bl;
181 req->buf_index = buf->bid;
182
183 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
184 /*
185 * If we came in unlocked, we have no choice but to consume the
186 * buffer here, otherwise nothing ensures that the buffer won't
187 * get used by others. This does mean it'll be pinned until the
188 * IO completes, coming in unlocked means we're being called from
189 * io-wq context and there may be further retries in async hybrid
190 * mode. For the locked case, the caller must call commit when
191 * the transfer completes (or if we get -EAGAIN and must poll of
192 * retry).
193 */
194 req->buf_list = NULL;
195 bl->head++;
196 }
197 return u64_to_user_ptr(buf->addr);
198}
199
200void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
201 unsigned int issue_flags)
202{
203 struct io_ring_ctx *ctx = req->ctx;
204 struct io_buffer_list *bl;
205 void __user *ret = NULL;
206
207 io_ring_submit_lock(req->ctx, issue_flags);
208
209 bl = io_buffer_get_list(ctx, req->buf_index);
210 if (likely(bl)) {
211 if (bl->is_mapped)
212 ret = io_ring_buffer_select(req, len, bl, issue_flags);
213 else
214 ret = io_provided_buffer_select(req, len, bl);
215 }
216 io_ring_submit_unlock(req->ctx, issue_flags);
217 return ret;
218}
219
220static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
221{
222 struct io_buffer_list *bl;
223 int i;
224
225 bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list), GFP_KERNEL);
226 if (!bl)
227 return -ENOMEM;
228
229 for (i = 0; i < BGID_ARRAY; i++) {
230 INIT_LIST_HEAD(&bl[i].buf_list);
231 bl[i].bgid = i;
232 }
233
234 smp_store_release(&ctx->io_bl, bl);
235 return 0;
236}
237
238/*
239 * Mark the given mapped range as free for reuse
240 */
241static void io_kbuf_mark_free(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
242{
243 struct io_buf_free *ibf;
244
245 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
246 if (bl->buf_ring == ibf->mem) {
247 ibf->inuse = 0;
248 return;
249 }
250 }
251
252 /* can't happen... */
253 WARN_ON_ONCE(1);
254}
255
256static int __io_remove_buffers(struct io_ring_ctx *ctx,
257 struct io_buffer_list *bl, unsigned nbufs)
258{
259 unsigned i = 0;
260
261 /* shouldn't happen */
262 if (!nbufs)
263 return 0;
264
265 if (bl->is_mapped) {
266 i = bl->buf_ring->tail - bl->head;
267 if (bl->is_mmap) {
268 /*
269 * io_kbuf_list_free() will free the page(s) at
270 * ->release() time.
271 */
272 io_kbuf_mark_free(ctx, bl);
273 bl->buf_ring = NULL;
274 bl->is_mmap = 0;
275 } else if (bl->buf_nr_pages) {
276 int j;
277
278 for (j = 0; j < bl->buf_nr_pages; j++)
279 unpin_user_page(bl->buf_pages[j]);
280 kvfree(bl->buf_pages);
281 bl->buf_pages = NULL;
282 bl->buf_nr_pages = 0;
283 }
284 /* make sure it's seen as empty */
285 INIT_LIST_HEAD(&bl->buf_list);
286 bl->is_mapped = 0;
287 return i;
288 }
289
290 /* protects io_buffers_cache */
291 lockdep_assert_held(&ctx->uring_lock);
292
293 while (!list_empty(&bl->buf_list)) {
294 struct io_buffer *nxt;
295
296 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
297 list_move(&nxt->list, &ctx->io_buffers_cache);
298 if (++i == nbufs)
299 return i;
300 cond_resched();
301 }
302
303 return i;
304}
305
306void io_destroy_buffers(struct io_ring_ctx *ctx)
307{
308 struct io_buffer_list *bl;
309 struct list_head *item, *tmp;
310 struct io_buffer *buf;
311 unsigned long index;
312 int i;
313
314 for (i = 0; i < BGID_ARRAY; i++) {
315 if (!ctx->io_bl)
316 break;
317 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
318 }
319
320 xa_for_each(&ctx->io_bl_xa, index, bl) {
321 xa_erase(&ctx->io_bl_xa, bl->bgid);
322 __io_remove_buffers(ctx, bl, -1U);
323 kfree_rcu(bl, rcu);
324 }
325
326 /*
327 * Move deferred locked entries to cache before pruning
328 */
329 spin_lock(&ctx->completion_lock);
330 if (!list_empty(&ctx->io_buffers_comp))
331 list_splice_init(&ctx->io_buffers_comp, &ctx->io_buffers_cache);
332 spin_unlock(&ctx->completion_lock);
333
334 list_for_each_safe(item, tmp, &ctx->io_buffers_cache) {
335 buf = list_entry(item, struct io_buffer, list);
336 kmem_cache_free(io_buf_cachep, buf);
337 }
338}
339
340int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
341{
342 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
343 u64 tmp;
344
345 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
346 sqe->splice_fd_in)
347 return -EINVAL;
348
349 tmp = READ_ONCE(sqe->fd);
350 if (!tmp || tmp > MAX_BIDS_PER_BGID)
351 return -EINVAL;
352
353 memset(p, 0, sizeof(*p));
354 p->nbufs = tmp;
355 p->bgid = READ_ONCE(sqe->buf_group);
356 return 0;
357}
358
359int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
360{
361 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
362 struct io_ring_ctx *ctx = req->ctx;
363 struct io_buffer_list *bl;
364 int ret = 0;
365
366 io_ring_submit_lock(ctx, issue_flags);
367
368 ret = -ENOENT;
369 bl = io_buffer_get_list(ctx, p->bgid);
370 if (bl) {
371 ret = -EINVAL;
372 /* can't use provide/remove buffers command on mapped buffers */
373 if (!bl->is_mapped)
374 ret = __io_remove_buffers(ctx, bl, p->nbufs);
375 }
376 io_ring_submit_unlock(ctx, issue_flags);
377 if (ret < 0)
378 req_set_fail(req);
379 io_req_set_res(req, ret, 0);
380 return IOU_OK;
381}
382
383int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
384{
385 unsigned long size, tmp_check;
386 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
387 u64 tmp;
388
389 if (sqe->rw_flags || sqe->splice_fd_in)
390 return -EINVAL;
391
392 tmp = READ_ONCE(sqe->fd);
393 if (!tmp || tmp > MAX_BIDS_PER_BGID)
394 return -E2BIG;
395 p->nbufs = tmp;
396 p->addr = READ_ONCE(sqe->addr);
397 p->len = READ_ONCE(sqe->len);
398
399 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
400 &size))
401 return -EOVERFLOW;
402 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
403 return -EOVERFLOW;
404
405 size = (unsigned long)p->len * p->nbufs;
406 if (!access_ok(u64_to_user_ptr(p->addr), size))
407 return -EFAULT;
408
409 p->bgid = READ_ONCE(sqe->buf_group);
410 tmp = READ_ONCE(sqe->off);
411 if (tmp > USHRT_MAX)
412 return -E2BIG;
413 if (tmp + p->nbufs > MAX_BIDS_PER_BGID)
414 return -EINVAL;
415 p->bid = tmp;
416 return 0;
417}
418
419#define IO_BUFFER_ALLOC_BATCH 64
420
421static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
422{
423 struct io_buffer *bufs[IO_BUFFER_ALLOC_BATCH];
424 int allocated;
425
426 /*
427 * Completions that don't happen inline (eg not under uring_lock) will
428 * add to ->io_buffers_comp. If we don't have any free buffers, check
429 * the completion list and splice those entries first.
430 */
431 if (!list_empty_careful(&ctx->io_buffers_comp)) {
432 spin_lock(&ctx->completion_lock);
433 if (!list_empty(&ctx->io_buffers_comp)) {
434 list_splice_init(&ctx->io_buffers_comp,
435 &ctx->io_buffers_cache);
436 spin_unlock(&ctx->completion_lock);
437 return 0;
438 }
439 spin_unlock(&ctx->completion_lock);
440 }
441
442 /*
443 * No free buffers and no completion entries either. Allocate a new
444 * batch of buffer entries and add those to our freelist.
445 */
446
447 allocated = kmem_cache_alloc_bulk(io_buf_cachep, GFP_KERNEL_ACCOUNT,
448 ARRAY_SIZE(bufs), (void **) bufs);
449 if (unlikely(!allocated)) {
450 /*
451 * Bulk alloc is all-or-nothing. If we fail to get a batch,
452 * retry single alloc to be on the safe side.
453 */
454 bufs[0] = kmem_cache_alloc(io_buf_cachep, GFP_KERNEL);
455 if (!bufs[0])
456 return -ENOMEM;
457 allocated = 1;
458 }
459
460 while (allocated)
461 list_add_tail(&bufs[--allocated]->list, &ctx->io_buffers_cache);
462
463 return 0;
464}
465
466static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
467 struct io_buffer_list *bl)
468{
469 struct io_buffer *buf;
470 u64 addr = pbuf->addr;
471 int i, bid = pbuf->bid;
472
473 for (i = 0; i < pbuf->nbufs; i++) {
474 if (list_empty(&ctx->io_buffers_cache) &&
475 io_refill_buffer_cache(ctx))
476 break;
477 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
478 list);
479 list_move_tail(&buf->list, &bl->buf_list);
480 buf->addr = addr;
481 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
482 buf->bid = bid;
483 buf->bgid = pbuf->bgid;
484 addr += pbuf->len;
485 bid++;
486 cond_resched();
487 }
488
489 return i ? 0 : -ENOMEM;
490}
491
492int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
493{
494 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
495 struct io_ring_ctx *ctx = req->ctx;
496 struct io_buffer_list *bl;
497 int ret = 0;
498
499 io_ring_submit_lock(ctx, issue_flags);
500
501 if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
502 ret = io_init_bl_list(ctx);
503 if (ret)
504 goto err;
505 }
506
507 bl = io_buffer_get_list(ctx, p->bgid);
508 if (unlikely(!bl)) {
509 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
510 if (!bl) {
511 ret = -ENOMEM;
512 goto err;
513 }
514 INIT_LIST_HEAD(&bl->buf_list);
515 ret = io_buffer_add_list(ctx, bl, p->bgid);
516 if (ret) {
517 /*
518 * Doesn't need rcu free as it was never visible, but
519 * let's keep it consistent throughout. Also can't
520 * be a lower indexed array group, as adding one
521 * where lookup failed cannot happen.
522 */
523 if (p->bgid >= BGID_ARRAY)
524 kfree_rcu(bl, rcu);
525 else
526 WARN_ON_ONCE(1);
527 goto err;
528 }
529 }
530 /* can't add buffers via this command for a mapped buffer ring */
531 if (bl->is_mapped) {
532 ret = -EINVAL;
533 goto err;
534 }
535
536 ret = io_add_buffers(ctx, p, bl);
537err:
538 io_ring_submit_unlock(ctx, issue_flags);
539
540 if (ret < 0)
541 req_set_fail(req);
542 io_req_set_res(req, ret, 0);
543 return IOU_OK;
544}
545
546static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
547 struct io_buffer_list *bl)
548{
549 struct io_uring_buf_ring *br;
550 struct page **pages;
551 int i, nr_pages;
552
553 pages = io_pin_pages(reg->ring_addr,
554 flex_array_size(br, bufs, reg->ring_entries),
555 &nr_pages);
556 if (IS_ERR(pages))
557 return PTR_ERR(pages);
558
559 /*
560 * Apparently some 32-bit boxes (ARM) will return highmem pages,
561 * which then need to be mapped. We could support that, but it'd
562 * complicate the code and slowdown the common cases quite a bit.
563 * So just error out, returning -EINVAL just like we did on kernels
564 * that didn't support mapped buffer rings.
565 */
566 for (i = 0; i < nr_pages; i++)
567 if (PageHighMem(pages[i]))
568 goto error_unpin;
569
570 br = page_address(pages[0]);
571#ifdef SHM_COLOUR
572 /*
573 * On platforms that have specific aliasing requirements, SHM_COLOUR
574 * is set and we must guarantee that the kernel and user side align
575 * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and
576 * the application mmap's the provided ring buffer. Fail the request
577 * if we, by chance, don't end up with aligned addresses. The app
578 * should use IOU_PBUF_RING_MMAP instead, and liburing will handle
579 * this transparently.
580 */
581 if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1))
582 goto error_unpin;
583#endif
584 bl->buf_pages = pages;
585 bl->buf_nr_pages = nr_pages;
586 bl->buf_ring = br;
587 bl->is_mapped = 1;
588 bl->is_mmap = 0;
589 return 0;
590error_unpin:
591 for (i = 0; i < nr_pages; i++)
592 unpin_user_page(pages[i]);
593 kvfree(pages);
594 return -EINVAL;
595}
596
597/*
598 * See if we have a suitable region that we can reuse, rather than allocate
599 * both a new io_buf_free and mem region again. We leave it on the list as
600 * even a reused entry will need freeing at ring release.
601 */
602static struct io_buf_free *io_lookup_buf_free_entry(struct io_ring_ctx *ctx,
603 size_t ring_size)
604{
605 struct io_buf_free *ibf, *best = NULL;
606 size_t best_dist;
607
608 hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
609 size_t dist;
610
611 if (ibf->inuse || ibf->size < ring_size)
612 continue;
613 dist = ibf->size - ring_size;
614 if (!best || dist < best_dist) {
615 best = ibf;
616 if (!dist)
617 break;
618 best_dist = dist;
619 }
620 }
621
622 return best;
623}
624
625static int io_alloc_pbuf_ring(struct io_ring_ctx *ctx,
626 struct io_uring_buf_reg *reg,
627 struct io_buffer_list *bl)
628{
629 struct io_buf_free *ibf;
630 size_t ring_size;
631 void *ptr;
632
633 ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
634
635 /* Reuse existing entry, if we can */
636 ibf = io_lookup_buf_free_entry(ctx, ring_size);
637 if (!ibf) {
638 ptr = io_mem_alloc(ring_size);
639 if (IS_ERR(ptr))
640 return PTR_ERR(ptr);
641
642 /* Allocate and store deferred free entry */
643 ibf = kmalloc(sizeof(*ibf), GFP_KERNEL_ACCOUNT);
644 if (!ibf) {
645 io_mem_free(ptr);
646 return -ENOMEM;
647 }
648 ibf->mem = ptr;
649 ibf->size = ring_size;
650 hlist_add_head(&ibf->list, &ctx->io_buf_list);
651 }
652 ibf->inuse = 1;
653 bl->buf_ring = ibf->mem;
654 bl->is_mapped = 1;
655 bl->is_mmap = 1;
656 return 0;
657}
658
659int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
660{
661 struct io_uring_buf_reg reg;
662 struct io_buffer_list *bl, *free_bl = NULL;
663 int ret;
664
665 lockdep_assert_held(&ctx->uring_lock);
666
667 if (copy_from_user(®, arg, sizeof(reg)))
668 return -EFAULT;
669
670 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
671 return -EINVAL;
672 if (reg.flags & ~IOU_PBUF_RING_MMAP)
673 return -EINVAL;
674 if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
675 if (!reg.ring_addr)
676 return -EFAULT;
677 if (reg.ring_addr & ~PAGE_MASK)
678 return -EINVAL;
679 } else {
680 if (reg.ring_addr)
681 return -EINVAL;
682 }
683
684 if (!is_power_of_2(reg.ring_entries))
685 return -EINVAL;
686
687 /* cannot disambiguate full vs empty due to head/tail size */
688 if (reg.ring_entries >= 65536)
689 return -EINVAL;
690
691 if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
692 int ret = io_init_bl_list(ctx);
693 if (ret)
694 return ret;
695 }
696
697 bl = io_buffer_get_list(ctx, reg.bgid);
698 if (bl) {
699 /* if mapped buffer ring OR classic exists, don't allow */
700 if (bl->is_mapped || !list_empty(&bl->buf_list))
701 return -EEXIST;
702 } else {
703 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
704 if (!bl)
705 return -ENOMEM;
706 }
707
708 if (!(reg.flags & IOU_PBUF_RING_MMAP))
709 ret = io_pin_pbuf_ring(®, bl);
710 else
711 ret = io_alloc_pbuf_ring(ctx, ®, bl);
712
713 if (!ret) {
714 bl->nr_entries = reg.ring_entries;
715 bl->mask = reg.ring_entries - 1;
716
717 io_buffer_add_list(ctx, bl, reg.bgid);
718 return 0;
719 }
720
721 kfree_rcu(free_bl, rcu);
722 return ret;
723}
724
725int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
726{
727 struct io_uring_buf_reg reg;
728 struct io_buffer_list *bl;
729
730 lockdep_assert_held(&ctx->uring_lock);
731
732 if (copy_from_user(®, arg, sizeof(reg)))
733 return -EFAULT;
734 if (reg.resv[0] || reg.resv[1] || reg.resv[2])
735 return -EINVAL;
736 if (reg.flags)
737 return -EINVAL;
738
739 bl = io_buffer_get_list(ctx, reg.bgid);
740 if (!bl)
741 return -ENOENT;
742 if (!bl->is_mapped)
743 return -EINVAL;
744
745 __io_remove_buffers(ctx, bl, -1U);
746 if (bl->bgid >= BGID_ARRAY) {
747 xa_erase(&ctx->io_bl_xa, bl->bgid);
748 kfree_rcu(bl, rcu);
749 }
750 return 0;
751}
752
753int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg)
754{
755 struct io_uring_buf_status buf_status;
756 struct io_buffer_list *bl;
757 int i;
758
759 if (copy_from_user(&buf_status, arg, sizeof(buf_status)))
760 return -EFAULT;
761
762 for (i = 0; i < ARRAY_SIZE(buf_status.resv); i++)
763 if (buf_status.resv[i])
764 return -EINVAL;
765
766 bl = io_buffer_get_list(ctx, buf_status.buf_group);
767 if (!bl)
768 return -ENOENT;
769 if (!bl->is_mapped)
770 return -EINVAL;
771
772 buf_status.head = bl->head;
773 if (copy_to_user(arg, &buf_status, sizeof(buf_status)))
774 return -EFAULT;
775
776 return 0;
777}
778
779void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid)
780{
781 struct io_buffer_list *bl;
782
783 bl = __io_buffer_get_list(ctx, smp_load_acquire(&ctx->io_bl), bgid);
784
785 if (!bl || !bl->is_mmap)
786 return NULL;
787 /*
788 * Ensure the list is fully setup. Only strictly needed for RCU lookup
789 * via mmap, and in that case only for the array indexed groups. For
790 * the xarray lookups, it's either visible and ready, or not at all.
791 */
792 if (!smp_load_acquire(&bl->is_ready))
793 return NULL;
794
795 return bl->buf_ring;
796}
797
798/*
799 * Called at or after ->release(), free the mmap'ed buffers that we used
800 * for memory mapped provided buffer rings.
801 */
802void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx)
803{
804 struct io_buf_free *ibf;
805 struct hlist_node *tmp;
806
807 hlist_for_each_entry_safe(ibf, tmp, &ctx->io_buf_list, list) {
808 hlist_del(&ibf->list);
809 io_mem_free(ibf->mem);
810 kfree(ibf);
811 }
812}
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/kernel.h>
3#include <linux/errno.h>
4#include <linux/fs.h>
5#include <linux/file.h>
6#include <linux/mm.h>
7#include <linux/slab.h>
8#include <linux/namei.h>
9#include <linux/poll.h>
10#include <linux/io_uring.h>
11
12#include <uapi/linux/io_uring.h>
13
14#include "io_uring.h"
15#include "opdef.h"
16#include "kbuf.h"
17
18#define IO_BUFFER_LIST_BUF_PER_PAGE (PAGE_SIZE / sizeof(struct io_uring_buf))
19
20#define BGID_ARRAY 64
21
22struct io_provide_buf {
23 struct file *file;
24 __u64 addr;
25 __u32 len;
26 __u32 bgid;
27 __u16 nbufs;
28 __u16 bid;
29};
30
31static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
32 unsigned int bgid)
33{
34 if (ctx->io_bl && bgid < BGID_ARRAY)
35 return &ctx->io_bl[bgid];
36
37 return xa_load(&ctx->io_bl_xa, bgid);
38}
39
40static int io_buffer_add_list(struct io_ring_ctx *ctx,
41 struct io_buffer_list *bl, unsigned int bgid)
42{
43 bl->bgid = bgid;
44 if (bgid < BGID_ARRAY)
45 return 0;
46
47 return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
48}
49
50void io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
51{
52 struct io_ring_ctx *ctx = req->ctx;
53 struct io_buffer_list *bl;
54 struct io_buffer *buf;
55
56 /*
57 * For legacy provided buffer mode, don't recycle if we already did
58 * IO to this buffer. For ring-mapped provided buffer mode, we should
59 * increment ring->head to explicitly monopolize the buffer to avoid
60 * multiple use.
61 */
62 if (req->flags & REQ_F_PARTIAL_IO)
63 return;
64
65 io_ring_submit_lock(ctx, issue_flags);
66
67 buf = req->kbuf;
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;
72
73 io_ring_submit_unlock(ctx, issue_flags);
74 return;
75}
76
77unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
78{
79 unsigned int cflags;
80
81 /*
82 * We can add this buffer back to two lists:
83 *
84 * 1) The io_buffers_cache list. This one is protected by the
85 * ctx->uring_lock. If we already hold this lock, add back to this
86 * list as we can grab it from issue as well.
87 * 2) The io_buffers_comp list. This one is protected by the
88 * ctx->completion_lock.
89 *
90 * We migrate buffers from the comp_list to the issue cache list
91 * when we need one.
92 */
93 if (req->flags & REQ_F_BUFFER_RING) {
94 /* no buffers to recycle for this case */
95 cflags = __io_put_kbuf_list(req, NULL);
96 } else if (issue_flags & IO_URING_F_UNLOCKED) {
97 struct io_ring_ctx *ctx = req->ctx;
98
99 spin_lock(&ctx->completion_lock);
100 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
101 spin_unlock(&ctx->completion_lock);
102 } else {
103 lockdep_assert_held(&req->ctx->uring_lock);
104
105 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
106 }
107 return cflags;
108}
109
110static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
111 struct io_buffer_list *bl)
112{
113 if (!list_empty(&bl->buf_list)) {
114 struct io_buffer *kbuf;
115
116 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
117 list_del(&kbuf->list);
118 if (*len == 0 || *len > kbuf->len)
119 *len = kbuf->len;
120 req->flags |= REQ_F_BUFFER_SELECTED;
121 req->kbuf = kbuf;
122 req->buf_index = kbuf->bid;
123 return u64_to_user_ptr(kbuf->addr);
124 }
125 return NULL;
126}
127
128static void __user *io_ring_buffer_select(struct io_kiocb *req, size_t *len,
129 struct io_buffer_list *bl,
130 unsigned int issue_flags)
131{
132 struct io_uring_buf_ring *br = bl->buf_ring;
133 struct io_uring_buf *buf;
134 __u16 head = bl->head;
135
136 if (unlikely(smp_load_acquire(&br->tail) == head))
137 return NULL;
138
139 head &= bl->mask;
140 if (head < IO_BUFFER_LIST_BUF_PER_PAGE) {
141 buf = &br->bufs[head];
142 } else {
143 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
144 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
145 buf = page_address(bl->buf_pages[index]);
146 buf += off;
147 }
148 if (*len == 0 || *len > buf->len)
149 *len = buf->len;
150 req->flags |= REQ_F_BUFFER_RING;
151 req->buf_list = bl;
152 req->buf_index = buf->bid;
153
154 if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
155 /*
156 * If we came in unlocked, we have no choice but to consume the
157 * buffer here, otherwise nothing ensures that the buffer won't
158 * get used by others. This does mean it'll be pinned until the
159 * IO completes, coming in unlocked means we're being called from
160 * io-wq context and there may be further retries in async hybrid
161 * mode. For the locked case, the caller must call commit when
162 * the transfer completes (or if we get -EAGAIN and must poll of
163 * retry).
164 */
165 req->buf_list = NULL;
166 bl->head++;
167 }
168 return u64_to_user_ptr(buf->addr);
169}
170
171void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
172 unsigned int issue_flags)
173{
174 struct io_ring_ctx *ctx = req->ctx;
175 struct io_buffer_list *bl;
176 void __user *ret = NULL;
177
178 io_ring_submit_lock(req->ctx, issue_flags);
179
180 bl = io_buffer_get_list(ctx, req->buf_index);
181 if (likely(bl)) {
182 if (bl->buf_nr_pages)
183 ret = io_ring_buffer_select(req, len, bl, issue_flags);
184 else
185 ret = io_provided_buffer_select(req, len, bl);
186 }
187 io_ring_submit_unlock(req->ctx, issue_flags);
188 return ret;
189}
190
191static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
192{
193 int i;
194
195 ctx->io_bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list),
196 GFP_KERNEL);
197 if (!ctx->io_bl)
198 return -ENOMEM;
199
200 for (i = 0; i < BGID_ARRAY; i++) {
201 INIT_LIST_HEAD(&ctx->io_bl[i].buf_list);
202 ctx->io_bl[i].bgid = i;
203 }
204
205 return 0;
206}
207
208static int __io_remove_buffers(struct io_ring_ctx *ctx,
209 struct io_buffer_list *bl, unsigned nbufs)
210{
211 unsigned i = 0;
212
213 /* shouldn't happen */
214 if (!nbufs)
215 return 0;
216
217 if (bl->buf_nr_pages) {
218 int j;
219
220 i = bl->buf_ring->tail - bl->head;
221 for (j = 0; j < bl->buf_nr_pages; j++)
222 unpin_user_page(bl->buf_pages[j]);
223 kvfree(bl->buf_pages);
224 bl->buf_pages = NULL;
225 bl->buf_nr_pages = 0;
226 /* make sure it's seen as empty */
227 INIT_LIST_HEAD(&bl->buf_list);
228 return i;
229 }
230
231 /* the head kbuf is the list itself */
232 while (!list_empty(&bl->buf_list)) {
233 struct io_buffer *nxt;
234
235 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
236 list_del(&nxt->list);
237 if (++i == nbufs)
238 return i;
239 cond_resched();
240 }
241 i++;
242
243 return i;
244}
245
246void io_destroy_buffers(struct io_ring_ctx *ctx)
247{
248 struct io_buffer_list *bl;
249 unsigned long index;
250 int i;
251
252 for (i = 0; i < BGID_ARRAY; i++) {
253 if (!ctx->io_bl)
254 break;
255 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
256 }
257
258 xa_for_each(&ctx->io_bl_xa, index, bl) {
259 xa_erase(&ctx->io_bl_xa, bl->bgid);
260 __io_remove_buffers(ctx, bl, -1U);
261 kfree(bl);
262 }
263
264 while (!list_empty(&ctx->io_buffers_pages)) {
265 struct page *page;
266
267 page = list_first_entry(&ctx->io_buffers_pages, struct page, lru);
268 list_del_init(&page->lru);
269 __free_page(page);
270 }
271}
272
273int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
274{
275 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
276 u64 tmp;
277
278 if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
279 sqe->splice_fd_in)
280 return -EINVAL;
281
282 tmp = READ_ONCE(sqe->fd);
283 if (!tmp || tmp > USHRT_MAX)
284 return -EINVAL;
285
286 memset(p, 0, sizeof(*p));
287 p->nbufs = tmp;
288 p->bgid = READ_ONCE(sqe->buf_group);
289 return 0;
290}
291
292int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
293{
294 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
295 struct io_ring_ctx *ctx = req->ctx;
296 struct io_buffer_list *bl;
297 int ret = 0;
298
299 io_ring_submit_lock(ctx, issue_flags);
300
301 ret = -ENOENT;
302 bl = io_buffer_get_list(ctx, p->bgid);
303 if (bl) {
304 ret = -EINVAL;
305 /* can't use provide/remove buffers command on mapped buffers */
306 if (!bl->buf_nr_pages)
307 ret = __io_remove_buffers(ctx, bl, p->nbufs);
308 }
309 io_ring_submit_unlock(ctx, issue_flags);
310 if (ret < 0)
311 req_set_fail(req);
312 io_req_set_res(req, ret, 0);
313 return IOU_OK;
314}
315
316int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
317{
318 unsigned long size, tmp_check;
319 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
320 u64 tmp;
321
322 if (sqe->rw_flags || sqe->splice_fd_in)
323 return -EINVAL;
324
325 tmp = READ_ONCE(sqe->fd);
326 if (!tmp || tmp > USHRT_MAX)
327 return -E2BIG;
328 p->nbufs = tmp;
329 p->addr = READ_ONCE(sqe->addr);
330 p->len = READ_ONCE(sqe->len);
331
332 if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
333 &size))
334 return -EOVERFLOW;
335 if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
336 return -EOVERFLOW;
337
338 size = (unsigned long)p->len * p->nbufs;
339 if (!access_ok(u64_to_user_ptr(p->addr), size))
340 return -EFAULT;
341
342 p->bgid = READ_ONCE(sqe->buf_group);
343 tmp = READ_ONCE(sqe->off);
344 if (tmp > USHRT_MAX)
345 return -E2BIG;
346 if (tmp + p->nbufs >= USHRT_MAX)
347 return -EINVAL;
348 p->bid = tmp;
349 return 0;
350}
351
352static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
353{
354 struct io_buffer *buf;
355 struct page *page;
356 int bufs_in_page;
357
358 /*
359 * Completions that don't happen inline (eg not under uring_lock) will
360 * add to ->io_buffers_comp. If we don't have any free buffers, check
361 * the completion list and splice those entries first.
362 */
363 if (!list_empty_careful(&ctx->io_buffers_comp)) {
364 spin_lock(&ctx->completion_lock);
365 if (!list_empty(&ctx->io_buffers_comp)) {
366 list_splice_init(&ctx->io_buffers_comp,
367 &ctx->io_buffers_cache);
368 spin_unlock(&ctx->completion_lock);
369 return 0;
370 }
371 spin_unlock(&ctx->completion_lock);
372 }
373
374 /*
375 * No free buffers and no completion entries either. Allocate a new
376 * page worth of buffer entries and add those to our freelist.
377 */
378 page = alloc_page(GFP_KERNEL_ACCOUNT);
379 if (!page)
380 return -ENOMEM;
381
382 list_add(&page->lru, &ctx->io_buffers_pages);
383
384 buf = page_address(page);
385 bufs_in_page = PAGE_SIZE / sizeof(*buf);
386 while (bufs_in_page) {
387 list_add_tail(&buf->list, &ctx->io_buffers_cache);
388 buf++;
389 bufs_in_page--;
390 }
391
392 return 0;
393}
394
395static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
396 struct io_buffer_list *bl)
397{
398 struct io_buffer *buf;
399 u64 addr = pbuf->addr;
400 int i, bid = pbuf->bid;
401
402 for (i = 0; i < pbuf->nbufs; i++) {
403 if (list_empty(&ctx->io_buffers_cache) &&
404 io_refill_buffer_cache(ctx))
405 break;
406 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
407 list);
408 list_move_tail(&buf->list, &bl->buf_list);
409 buf->addr = addr;
410 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
411 buf->bid = bid;
412 buf->bgid = pbuf->bgid;
413 addr += pbuf->len;
414 bid++;
415 cond_resched();
416 }
417
418 return i ? 0 : -ENOMEM;
419}
420
421int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
422{
423 struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
424 struct io_ring_ctx *ctx = req->ctx;
425 struct io_buffer_list *bl;
426 int ret = 0;
427
428 io_ring_submit_lock(ctx, issue_flags);
429
430 if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
431 ret = io_init_bl_list(ctx);
432 if (ret)
433 goto err;
434 }
435
436 bl = io_buffer_get_list(ctx, p->bgid);
437 if (unlikely(!bl)) {
438 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
439 if (!bl) {
440 ret = -ENOMEM;
441 goto err;
442 }
443 INIT_LIST_HEAD(&bl->buf_list);
444 ret = io_buffer_add_list(ctx, bl, p->bgid);
445 if (ret) {
446 kfree(bl);
447 goto err;
448 }
449 }
450 /* can't add buffers via this command for a mapped buffer ring */
451 if (bl->buf_nr_pages) {
452 ret = -EINVAL;
453 goto err;
454 }
455
456 ret = io_add_buffers(ctx, p, bl);
457err:
458 io_ring_submit_unlock(ctx, issue_flags);
459
460 if (ret < 0)
461 req_set_fail(req);
462 io_req_set_res(req, ret, 0);
463 return IOU_OK;
464}
465
466int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
467{
468 struct io_uring_buf_ring *br;
469 struct io_uring_buf_reg reg;
470 struct io_buffer_list *bl, *free_bl = NULL;
471 struct page **pages;
472 int nr_pages;
473
474 if (copy_from_user(®, arg, sizeof(reg)))
475 return -EFAULT;
476
477 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
478 return -EINVAL;
479 if (!reg.ring_addr)
480 return -EFAULT;
481 if (reg.ring_addr & ~PAGE_MASK)
482 return -EINVAL;
483 if (!is_power_of_2(reg.ring_entries))
484 return -EINVAL;
485
486 /* cannot disambiguate full vs empty due to head/tail size */
487 if (reg.ring_entries >= 65536)
488 return -EINVAL;
489
490 if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
491 int ret = io_init_bl_list(ctx);
492 if (ret)
493 return ret;
494 }
495
496 bl = io_buffer_get_list(ctx, reg.bgid);
497 if (bl) {
498 /* if mapped buffer ring OR classic exists, don't allow */
499 if (bl->buf_nr_pages || !list_empty(&bl->buf_list))
500 return -EEXIST;
501 } else {
502 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
503 if (!bl)
504 return -ENOMEM;
505 }
506
507 pages = io_pin_pages(reg.ring_addr,
508 struct_size(br, bufs, reg.ring_entries),
509 &nr_pages);
510 if (IS_ERR(pages)) {
511 kfree(free_bl);
512 return PTR_ERR(pages);
513 }
514
515 br = page_address(pages[0]);
516 bl->buf_pages = pages;
517 bl->buf_nr_pages = nr_pages;
518 bl->nr_entries = reg.ring_entries;
519 bl->buf_ring = br;
520 bl->mask = reg.ring_entries - 1;
521 io_buffer_add_list(ctx, bl, reg.bgid);
522 return 0;
523}
524
525int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
526{
527 struct io_uring_buf_reg reg;
528 struct io_buffer_list *bl;
529
530 if (copy_from_user(®, arg, sizeof(reg)))
531 return -EFAULT;
532 if (reg.pad || reg.resv[0] || reg.resv[1] || reg.resv[2])
533 return -EINVAL;
534
535 bl = io_buffer_get_list(ctx, reg.bgid);
536 if (!bl)
537 return -ENOENT;
538 if (!bl->buf_nr_pages)
539 return -EINVAL;
540
541 __io_remove_buffers(ctx, bl, -1U);
542 if (bl->bgid >= BGID_ARRAY) {
543 xa_erase(&ctx->io_bl_xa, bl->bgid);
544 kfree(bl);
545 }
546 return 0;
547}