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