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