GNU Linux-libre 6.8.7-gnu
[releases.git] / io_uring / kbuf.c
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 /* BIDs are addressed by a 16-bit field in a CQE */
21 #define MAX_BIDS_PER_BGID (1 << 16)
22
23 struct kmem_cache *io_buf_cachep;
24
25 struct io_provide_buf {
26         struct file                     *file;
27         __u64                           addr;
28         __u32                           len;
29         __u32                           bgid;
30         __u32                           nbufs;
31         __u16                           bid;
32 };
33
34 struct io_buf_free {
35         struct hlist_node               list;
36         void                            *mem;
37         size_t                          size;
38         int                             inuse;
39 };
40
41 static inline struct io_buffer_list *__io_buffer_get_list(struct io_ring_ctx *ctx,
42                                                           unsigned int bgid)
43 {
44         return xa_load(&ctx->io_bl_xa, bgid);
45 }
46
47 static inline struct io_buffer_list *io_buffer_get_list(struct io_ring_ctx *ctx,
48                                                         unsigned int bgid)
49 {
50         lockdep_assert_held(&ctx->uring_lock);
51
52         return __io_buffer_get_list(ctx, bgid);
53 }
54
55 static int io_buffer_add_list(struct io_ring_ctx *ctx,
56                               struct io_buffer_list *bl, unsigned int bgid)
57 {
58         /*
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.
62          */
63         bl->bgid = bgid;
64         atomic_set(&bl->refs, 1);
65         return xa_err(xa_store(&ctx->io_bl_xa, bgid, bl, GFP_KERNEL));
66 }
67
68 bool io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags)
69 {
70         struct io_ring_ctx *ctx = req->ctx;
71         struct io_buffer_list *bl;
72         struct io_buffer *buf;
73
74         /*
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
78          * multiple use.
79          */
80         if (req->flags & REQ_F_PARTIAL_IO)
81                 return false;
82
83         io_ring_submit_lock(ctx, issue_flags);
84
85         buf = req->kbuf;
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;
90
91         io_ring_submit_unlock(ctx, issue_flags);
92         return true;
93 }
94
95 unsigned int __io_put_kbuf(struct io_kiocb *req, unsigned issue_flags)
96 {
97         unsigned int cflags;
98
99         /*
100          * We can add this buffer back to two lists:
101          *
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.
107          *
108          * We migrate buffers from the comp_list to the issue cache list
109          * when we need one.
110          */
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;
116
117                 spin_lock(&ctx->completion_lock);
118                 cflags = __io_put_kbuf_list(req, &ctx->io_buffers_comp);
119                 spin_unlock(&ctx->completion_lock);
120         } else {
121                 lockdep_assert_held(&req->ctx->uring_lock);
122
123                 cflags = __io_put_kbuf_list(req, &req->ctx->io_buffers_cache);
124         }
125         return cflags;
126 }
127
128 static void __user *io_provided_buffer_select(struct io_kiocb *req, size_t *len,
129                                               struct io_buffer_list *bl)
130 {
131         if (!list_empty(&bl->buf_list)) {
132                 struct io_buffer *kbuf;
133
134                 kbuf = list_first_entry(&bl->buf_list, struct io_buffer, list);
135                 list_del(&kbuf->list);
136                 if (*len == 0 || *len > kbuf->len)
137                         *len = kbuf->len;
138                 req->flags |= REQ_F_BUFFER_SELECTED;
139                 req->kbuf = kbuf;
140                 req->buf_index = kbuf->bid;
141                 return u64_to_user_ptr(kbuf->addr);
142         }
143         return NULL;
144 }
145
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)
149 {
150         struct io_uring_buf_ring *br = bl->buf_ring;
151         struct io_uring_buf *buf;
152         __u16 head = bl->head;
153
154         if (unlikely(smp_load_acquire(&br->tail) == head))
155                 return NULL;
156
157         head &= bl->mask;
158         /* mmaped buffers are always contig */
159         if (bl->is_mmap || head < IO_BUFFER_LIST_BUF_PER_PAGE) {
160                 buf = &br->bufs[head];
161         } else {
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]);
165                 buf += off;
166         }
167         if (*len == 0 || *len > buf->len)
168                 *len = buf->len;
169         req->flags |= REQ_F_BUFFER_RING;
170         req->buf_list = bl;
171         req->buf_index = buf->bid;
172
173         if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
174                 /*
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
182                  * retry).
183                  */
184                 req->buf_list = NULL;
185                 bl->head++;
186         }
187         return u64_to_user_ptr(buf->addr);
188 }
189
190 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
191                               unsigned int issue_flags)
192 {
193         struct io_ring_ctx *ctx = req->ctx;
194         struct io_buffer_list *bl;
195         void __user *ret = NULL;
196
197         io_ring_submit_lock(req->ctx, issue_flags);
198
199         bl = io_buffer_get_list(ctx, req->buf_index);
200         if (likely(bl)) {
201                 if (bl->is_mapped)
202                         ret = io_ring_buffer_select(req, len, bl, issue_flags);
203                 else
204                         ret = io_provided_buffer_select(req, len, bl);
205         }
206         io_ring_submit_unlock(req->ctx, issue_flags);
207         return ret;
208 }
209
210 /*
211  * Mark the given mapped range as free for reuse
212  */
213 static void io_kbuf_mark_free(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
214 {
215         struct io_buf_free *ibf;
216
217         hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
218                 if (bl->buf_ring == ibf->mem) {
219                         ibf->inuse = 0;
220                         return;
221                 }
222         }
223
224         /* can't happen... */
225         WARN_ON_ONCE(1);
226 }
227
228 static int __io_remove_buffers(struct io_ring_ctx *ctx,
229                                struct io_buffer_list *bl, unsigned nbufs)
230 {
231         unsigned i = 0;
232
233         /* shouldn't happen */
234         if (!nbufs)
235                 return 0;
236
237         if (bl->is_mapped) {
238                 i = bl->buf_ring->tail - bl->head;
239                 if (bl->is_mmap) {
240                         /*
241                          * io_kbuf_list_free() will free the page(s) at
242                          * ->release() time.
243                          */
244                         io_kbuf_mark_free(ctx, bl);
245                         bl->buf_ring = NULL;
246                         bl->is_mmap = 0;
247                 } else if (bl->buf_nr_pages) {
248                         int j;
249
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;
255                 }
256                 /* make sure it's seen as empty */
257                 INIT_LIST_HEAD(&bl->buf_list);
258                 bl->is_mapped = 0;
259                 return i;
260         }
261
262         /* protects io_buffers_cache */
263         lockdep_assert_held(&ctx->uring_lock);
264
265         while (!list_empty(&bl->buf_list)) {
266                 struct io_buffer *nxt;
267
268                 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
269                 list_move(&nxt->list, &ctx->io_buffers_cache);
270                 if (++i == nbufs)
271                         return i;
272                 cond_resched();
273         }
274
275         return i;
276 }
277
278 void io_put_bl(struct io_ring_ctx *ctx, struct io_buffer_list *bl)
279 {
280         if (atomic_dec_and_test(&bl->refs)) {
281                 __io_remove_buffers(ctx, bl, -1U);
282                 kfree_rcu(bl, rcu);
283         }
284 }
285
286 void io_destroy_buffers(struct io_ring_ctx *ctx)
287 {
288         struct io_buffer_list *bl;
289         struct list_head *item, *tmp;
290         struct io_buffer *buf;
291         unsigned long index;
292
293         xa_for_each(&ctx->io_bl_xa, index, bl) {
294                 xa_erase(&ctx->io_bl_xa, bl->bgid);
295                 io_put_bl(ctx, bl);
296         }
297
298         /*
299          * Move deferred locked entries to cache before pruning
300          */
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);
305
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);
309         }
310 }
311
312 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
313 {
314         struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
315         u64 tmp;
316
317         if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
318             sqe->splice_fd_in)
319                 return -EINVAL;
320
321         tmp = READ_ONCE(sqe->fd);
322         if (!tmp || tmp > MAX_BIDS_PER_BGID)
323                 return -EINVAL;
324
325         memset(p, 0, sizeof(*p));
326         p->nbufs = tmp;
327         p->bgid = READ_ONCE(sqe->buf_group);
328         return 0;
329 }
330
331 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
332 {
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;
336         int ret = 0;
337
338         io_ring_submit_lock(ctx, issue_flags);
339
340         ret = -ENOENT;
341         bl = io_buffer_get_list(ctx, p->bgid);
342         if (bl) {
343                 ret = -EINVAL;
344                 /* can't use provide/remove buffers command on mapped buffers */
345                 if (!bl->is_mapped)
346                         ret = __io_remove_buffers(ctx, bl, p->nbufs);
347         }
348         io_ring_submit_unlock(ctx, issue_flags);
349         if (ret < 0)
350                 req_set_fail(req);
351         io_req_set_res(req, ret, 0);
352         return IOU_OK;
353 }
354
355 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
356 {
357         unsigned long size, tmp_check;
358         struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
359         u64 tmp;
360
361         if (sqe->rw_flags || sqe->splice_fd_in)
362                 return -EINVAL;
363
364         tmp = READ_ONCE(sqe->fd);
365         if (!tmp || tmp > MAX_BIDS_PER_BGID)
366                 return -E2BIG;
367         p->nbufs = tmp;
368         p->addr = READ_ONCE(sqe->addr);
369         p->len = READ_ONCE(sqe->len);
370
371         if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
372                                 &size))
373                 return -EOVERFLOW;
374         if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
375                 return -EOVERFLOW;
376
377         size = (unsigned long)p->len * p->nbufs;
378         if (!access_ok(u64_to_user_ptr(p->addr), size))
379                 return -EFAULT;
380
381         p->bgid = READ_ONCE(sqe->buf_group);
382         tmp = READ_ONCE(sqe->off);
383         if (tmp > USHRT_MAX)
384                 return -E2BIG;
385         if (tmp + p->nbufs > MAX_BIDS_PER_BGID)
386                 return -EINVAL;
387         p->bid = tmp;
388         return 0;
389 }
390
391 #define IO_BUFFER_ALLOC_BATCH 64
392
393 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
394 {
395         struct io_buffer *bufs[IO_BUFFER_ALLOC_BATCH];
396         int allocated;
397
398         /*
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.
402          */
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);
409                         return 0;
410                 }
411                 spin_unlock(&ctx->completion_lock);
412         }
413
414         /*
415          * No free buffers and no completion entries either. Allocate a new
416          * batch of buffer entries and add those to our freelist.
417          */
418
419         allocated = kmem_cache_alloc_bulk(io_buf_cachep, GFP_KERNEL_ACCOUNT,
420                                           ARRAY_SIZE(bufs), (void **) bufs);
421         if (unlikely(!allocated)) {
422                 /*
423                  * Bulk alloc is all-or-nothing. If we fail to get a batch,
424                  * retry single alloc to be on the safe side.
425                  */
426                 bufs[0] = kmem_cache_alloc(io_buf_cachep, GFP_KERNEL);
427                 if (!bufs[0])
428                         return -ENOMEM;
429                 allocated = 1;
430         }
431
432         while (allocated)
433                 list_add_tail(&bufs[--allocated]->list, &ctx->io_buffers_cache);
434
435         return 0;
436 }
437
438 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
439                           struct io_buffer_list *bl)
440 {
441         struct io_buffer *buf;
442         u64 addr = pbuf->addr;
443         int i, bid = pbuf->bid;
444
445         for (i = 0; i < pbuf->nbufs; i++) {
446                 if (list_empty(&ctx->io_buffers_cache) &&
447                     io_refill_buffer_cache(ctx))
448                         break;
449                 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
450                                         list);
451                 list_move_tail(&buf->list, &bl->buf_list);
452                 buf->addr = addr;
453                 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
454                 buf->bid = bid;
455                 buf->bgid = pbuf->bgid;
456                 addr += pbuf->len;
457                 bid++;
458                 cond_resched();
459         }
460
461         return i ? 0 : -ENOMEM;
462 }
463
464 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
465 {
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;
469         int ret = 0;
470
471         io_ring_submit_lock(ctx, issue_flags);
472
473         bl = io_buffer_get_list(ctx, p->bgid);
474         if (unlikely(!bl)) {
475                 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
476                 if (!bl) {
477                         ret = -ENOMEM;
478                         goto err;
479                 }
480                 INIT_LIST_HEAD(&bl->buf_list);
481                 ret = io_buffer_add_list(ctx, bl, p->bgid);
482                 if (ret) {
483                         /*
484                          * Doesn't need rcu free as it was never visible, but
485                          * let's keep it consistent throughout.
486                          */
487                         kfree_rcu(bl, rcu);
488                         goto err;
489                 }
490         }
491         /* can't add buffers via this command for a mapped buffer ring */
492         if (bl->is_mapped) {
493                 ret = -EINVAL;
494                 goto err;
495         }
496
497         ret = io_add_buffers(ctx, p, bl);
498 err:
499         io_ring_submit_unlock(ctx, issue_flags);
500
501         if (ret < 0)
502                 req_set_fail(req);
503         io_req_set_res(req, ret, 0);
504         return IOU_OK;
505 }
506
507 static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
508                             struct io_buffer_list *bl)
509 {
510         struct io_uring_buf_ring *br;
511         struct page **pages;
512         int i, nr_pages;
513
514         pages = io_pin_pages(reg->ring_addr,
515                              flex_array_size(br, bufs, reg->ring_entries),
516                              &nr_pages);
517         if (IS_ERR(pages))
518                 return PTR_ERR(pages);
519
520         /*
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.
526          */
527         for (i = 0; i < nr_pages; i++)
528                 if (PageHighMem(pages[i]))
529                         goto error_unpin;
530
531         br = page_address(pages[0]);
532 #ifdef SHM_COLOUR
533         /*
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.
541          */
542         if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1))
543                 goto error_unpin;
544 #endif
545         bl->buf_pages = pages;
546         bl->buf_nr_pages = nr_pages;
547         bl->buf_ring = br;
548         bl->is_mapped = 1;
549         bl->is_mmap = 0;
550         return 0;
551 error_unpin:
552         for (i = 0; i < nr_pages; i++)
553                 unpin_user_page(pages[i]);
554         kvfree(pages);
555         return -EINVAL;
556 }
557
558 /*
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.
562  */
563 static struct io_buf_free *io_lookup_buf_free_entry(struct io_ring_ctx *ctx,
564                                                     size_t ring_size)
565 {
566         struct io_buf_free *ibf, *best = NULL;
567         size_t best_dist;
568
569         hlist_for_each_entry(ibf, &ctx->io_buf_list, list) {
570                 size_t dist;
571
572                 if (ibf->inuse || ibf->size < ring_size)
573                         continue;
574                 dist = ibf->size - ring_size;
575                 if (!best || dist < best_dist) {
576                         best = ibf;
577                         if (!dist)
578                                 break;
579                         best_dist = dist;
580                 }
581         }
582
583         return best;
584 }
585
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)
589 {
590         struct io_buf_free *ibf;
591         size_t ring_size;
592         void *ptr;
593
594         ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
595
596         /* Reuse existing entry, if we can */
597         ibf = io_lookup_buf_free_entry(ctx, ring_size);
598         if (!ibf) {
599                 ptr = io_mem_alloc(ring_size);
600                 if (IS_ERR(ptr))
601                         return PTR_ERR(ptr);
602
603                 /* Allocate and store deferred free entry */
604                 ibf = kmalloc(sizeof(*ibf), GFP_KERNEL_ACCOUNT);
605                 if (!ibf) {
606                         io_mem_free(ptr);
607                         return -ENOMEM;
608                 }
609                 ibf->mem = ptr;
610                 ibf->size = ring_size;
611                 hlist_add_head(&ibf->list, &ctx->io_buf_list);
612         }
613         ibf->inuse = 1;
614         bl->buf_ring = ibf->mem;
615         bl->is_mapped = 1;
616         bl->is_mmap = 1;
617         return 0;
618 }
619
620 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
621 {
622         struct io_uring_buf_reg reg;
623         struct io_buffer_list *bl, *free_bl = NULL;
624         int ret;
625
626         lockdep_assert_held(&ctx->uring_lock);
627
628         if (copy_from_user(&reg, arg, sizeof(reg)))
629                 return -EFAULT;
630
631         if (reg.resv[0] || reg.resv[1] || reg.resv[2])
632                 return -EINVAL;
633         if (reg.flags & ~IOU_PBUF_RING_MMAP)
634                 return -EINVAL;
635         if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
636                 if (!reg.ring_addr)
637                         return -EFAULT;
638                 if (reg.ring_addr & ~PAGE_MASK)
639                         return -EINVAL;
640         } else {
641                 if (reg.ring_addr)
642                         return -EINVAL;
643         }
644
645         if (!is_power_of_2(reg.ring_entries))
646                 return -EINVAL;
647
648         /* cannot disambiguate full vs empty due to head/tail size */
649         if (reg.ring_entries >= 65536)
650                 return -EINVAL;
651
652         bl = io_buffer_get_list(ctx, reg.bgid);
653         if (bl) {
654                 /* if mapped buffer ring OR classic exists, don't allow */
655                 if (bl->is_mapped || !list_empty(&bl->buf_list))
656                         return -EEXIST;
657         } else {
658                 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
659                 if (!bl)
660                         return -ENOMEM;
661         }
662
663         if (!(reg.flags & IOU_PBUF_RING_MMAP))
664                 ret = io_pin_pbuf_ring(&reg, bl);
665         else
666                 ret = io_alloc_pbuf_ring(ctx, &reg, bl);
667
668         if (!ret) {
669                 bl->nr_entries = reg.ring_entries;
670                 bl->mask = reg.ring_entries - 1;
671
672                 io_buffer_add_list(ctx, bl, reg.bgid);
673                 return 0;
674         }
675
676         kfree_rcu(free_bl, rcu);
677         return ret;
678 }
679
680 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
681 {
682         struct io_uring_buf_reg reg;
683         struct io_buffer_list *bl;
684
685         lockdep_assert_held(&ctx->uring_lock);
686
687         if (copy_from_user(&reg, arg, sizeof(reg)))
688                 return -EFAULT;
689         if (reg.resv[0] || reg.resv[1] || reg.resv[2])
690                 return -EINVAL;
691         if (reg.flags)
692                 return -EINVAL;
693
694         bl = io_buffer_get_list(ctx, reg.bgid);
695         if (!bl)
696                 return -ENOENT;
697         if (!bl->is_mapped)
698                 return -EINVAL;
699
700         xa_erase(&ctx->io_bl_xa, bl->bgid);
701         io_put_bl(ctx, bl);
702         return 0;
703 }
704
705 int io_register_pbuf_status(struct io_ring_ctx *ctx, void __user *arg)
706 {
707         struct io_uring_buf_status buf_status;
708         struct io_buffer_list *bl;
709         int i;
710
711         if (copy_from_user(&buf_status, arg, sizeof(buf_status)))
712                 return -EFAULT;
713
714         for (i = 0; i < ARRAY_SIZE(buf_status.resv); i++)
715                 if (buf_status.resv[i])
716                         return -EINVAL;
717
718         bl = io_buffer_get_list(ctx, buf_status.buf_group);
719         if (!bl)
720                 return -ENOENT;
721         if (!bl->is_mapped)
722                 return -EINVAL;
723
724         buf_status.head = bl->head;
725         if (copy_to_user(arg, &buf_status, sizeof(buf_status)))
726                 return -EFAULT;
727
728         return 0;
729 }
730
731 struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx,
732                                       unsigned long bgid)
733 {
734         struct io_buffer_list *bl;
735         bool ret;
736
737         /*
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.
747          */
748         rcu_read_lock();
749         bl = xa_load(&ctx->io_bl_xa, bgid);
750         /* must be a mmap'able buffer ring and have pages */
751         ret = false;
752         if (bl && bl->is_mmap)
753                 ret = atomic_inc_not_zero(&bl->refs);
754         rcu_read_unlock();
755
756         if (ret)
757                 return bl;
758
759         return ERR_PTR(-EINVAL);
760 }
761
762 /*
763  * Called at or after ->release(), free the mmap'ed buffers that we used
764  * for memory mapped provided buffer rings.
765  */
766 void io_kbuf_mmap_list_free(struct io_ring_ctx *ctx)
767 {
768         struct io_buf_free *ibf;
769         struct hlist_node *tmp;
770
771         hlist_for_each_entry_safe(ibf, tmp, &ctx->io_buf_list, list) {
772                 hlist_del(&ibf->list);
773                 io_mem_free(ibf->mem);
774                 kfree(ibf);
775         }
776 }