io_uring: shut io_prep_async_work warning
[platform/kernel/linux-starfive.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 #define BGID_ARRAY      64
21
22 struct io_provide_buf {
23         struct file                     *file;
24         __u64                           addr;
25         __u32                           len;
26         __u32                           bgid;
27         __u16                           nbufs;
28         __u16                           bid;
29 };
30
31 static 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
40 static 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
50 void 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
77 unsigned 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
110 static 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
128 static 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         /* mmaped buffers are always contig */
141         if (bl->is_mmap || head < IO_BUFFER_LIST_BUF_PER_PAGE) {
142                 buf = &br->bufs[head];
143         } else {
144                 int off = head & (IO_BUFFER_LIST_BUF_PER_PAGE - 1);
145                 int index = head / IO_BUFFER_LIST_BUF_PER_PAGE;
146                 buf = page_address(bl->buf_pages[index]);
147                 buf += off;
148         }
149         if (*len == 0 || *len > buf->len)
150                 *len = buf->len;
151         req->flags |= REQ_F_BUFFER_RING;
152         req->buf_list = bl;
153         req->buf_index = buf->bid;
154
155         if (issue_flags & IO_URING_F_UNLOCKED || !file_can_poll(req->file)) {
156                 /*
157                  * If we came in unlocked, we have no choice but to consume the
158                  * buffer here, otherwise nothing ensures that the buffer won't
159                  * get used by others. This does mean it'll be pinned until the
160                  * IO completes, coming in unlocked means we're being called from
161                  * io-wq context and there may be further retries in async hybrid
162                  * mode. For the locked case, the caller must call commit when
163                  * the transfer completes (or if we get -EAGAIN and must poll of
164                  * retry).
165                  */
166                 req->buf_list = NULL;
167                 bl->head++;
168         }
169         return u64_to_user_ptr(buf->addr);
170 }
171
172 void __user *io_buffer_select(struct io_kiocb *req, size_t *len,
173                               unsigned int issue_flags)
174 {
175         struct io_ring_ctx *ctx = req->ctx;
176         struct io_buffer_list *bl;
177         void __user *ret = NULL;
178
179         io_ring_submit_lock(req->ctx, issue_flags);
180
181         bl = io_buffer_get_list(ctx, req->buf_index);
182         if (likely(bl)) {
183                 if (bl->is_mapped)
184                         ret = io_ring_buffer_select(req, len, bl, issue_flags);
185                 else
186                         ret = io_provided_buffer_select(req, len, bl);
187         }
188         io_ring_submit_unlock(req->ctx, issue_flags);
189         return ret;
190 }
191
192 static __cold int io_init_bl_list(struct io_ring_ctx *ctx)
193 {
194         int i;
195
196         ctx->io_bl = kcalloc(BGID_ARRAY, sizeof(struct io_buffer_list),
197                                 GFP_KERNEL);
198         if (!ctx->io_bl)
199                 return -ENOMEM;
200
201         for (i = 0; i < BGID_ARRAY; i++) {
202                 INIT_LIST_HEAD(&ctx->io_bl[i].buf_list);
203                 ctx->io_bl[i].bgid = i;
204         }
205
206         return 0;
207 }
208
209 static int __io_remove_buffers(struct io_ring_ctx *ctx,
210                                struct io_buffer_list *bl, unsigned nbufs)
211 {
212         unsigned i = 0;
213
214         /* shouldn't happen */
215         if (!nbufs)
216                 return 0;
217
218         if (bl->is_mapped) {
219                 i = bl->buf_ring->tail - bl->head;
220                 if (bl->is_mmap) {
221                         if (bl->buf_ring) {
222                                 struct page *page;
223
224                                 page = virt_to_head_page(bl->buf_ring);
225                                 if (put_page_testzero(page))
226                                         free_compound_page(page);
227                                 bl->buf_ring = NULL;
228                         }
229                         bl->is_mmap = 0;
230                 } else if (bl->buf_nr_pages) {
231                         int j;
232
233                         for (j = 0; j < bl->buf_nr_pages; j++)
234                                 unpin_user_page(bl->buf_pages[j]);
235                         kvfree(bl->buf_pages);
236                         bl->buf_pages = NULL;
237                         bl->buf_nr_pages = 0;
238                 }
239                 /* make sure it's seen as empty */
240                 INIT_LIST_HEAD(&bl->buf_list);
241                 bl->is_mapped = 0;
242                 return i;
243         }
244
245         /* the head kbuf is the list itself */
246         while (!list_empty(&bl->buf_list)) {
247                 struct io_buffer *nxt;
248
249                 nxt = list_first_entry(&bl->buf_list, struct io_buffer, list);
250                 list_del(&nxt->list);
251                 if (++i == nbufs)
252                         return i;
253                 cond_resched();
254         }
255         i++;
256
257         return i;
258 }
259
260 void io_destroy_buffers(struct io_ring_ctx *ctx)
261 {
262         struct io_buffer_list *bl;
263         unsigned long index;
264         int i;
265
266         for (i = 0; i < BGID_ARRAY; i++) {
267                 if (!ctx->io_bl)
268                         break;
269                 __io_remove_buffers(ctx, &ctx->io_bl[i], -1U);
270         }
271
272         xa_for_each(&ctx->io_bl_xa, index, bl) {
273                 xa_erase(&ctx->io_bl_xa, bl->bgid);
274                 __io_remove_buffers(ctx, bl, -1U);
275                 kfree(bl);
276         }
277
278         while (!list_empty(&ctx->io_buffers_pages)) {
279                 struct page *page;
280
281                 page = list_first_entry(&ctx->io_buffers_pages, struct page, lru);
282                 list_del_init(&page->lru);
283                 __free_page(page);
284         }
285 }
286
287 int io_remove_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
288 {
289         struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
290         u64 tmp;
291
292         if (sqe->rw_flags || sqe->addr || sqe->len || sqe->off ||
293             sqe->splice_fd_in)
294                 return -EINVAL;
295
296         tmp = READ_ONCE(sqe->fd);
297         if (!tmp || tmp > USHRT_MAX)
298                 return -EINVAL;
299
300         memset(p, 0, sizeof(*p));
301         p->nbufs = tmp;
302         p->bgid = READ_ONCE(sqe->buf_group);
303         return 0;
304 }
305
306 int io_remove_buffers(struct io_kiocb *req, unsigned int issue_flags)
307 {
308         struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
309         struct io_ring_ctx *ctx = req->ctx;
310         struct io_buffer_list *bl;
311         int ret = 0;
312
313         io_ring_submit_lock(ctx, issue_flags);
314
315         ret = -ENOENT;
316         bl = io_buffer_get_list(ctx, p->bgid);
317         if (bl) {
318                 ret = -EINVAL;
319                 /* can't use provide/remove buffers command on mapped buffers */
320                 if (!bl->is_mapped)
321                         ret = __io_remove_buffers(ctx, bl, p->nbufs);
322         }
323         io_ring_submit_unlock(ctx, issue_flags);
324         if (ret < 0)
325                 req_set_fail(req);
326         io_req_set_res(req, ret, 0);
327         return IOU_OK;
328 }
329
330 int io_provide_buffers_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
331 {
332         unsigned long size, tmp_check;
333         struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
334         u64 tmp;
335
336         if (sqe->rw_flags || sqe->splice_fd_in)
337                 return -EINVAL;
338
339         tmp = READ_ONCE(sqe->fd);
340         if (!tmp || tmp > USHRT_MAX)
341                 return -E2BIG;
342         p->nbufs = tmp;
343         p->addr = READ_ONCE(sqe->addr);
344         p->len = READ_ONCE(sqe->len);
345
346         if (check_mul_overflow((unsigned long)p->len, (unsigned long)p->nbufs,
347                                 &size))
348                 return -EOVERFLOW;
349         if (check_add_overflow((unsigned long)p->addr, size, &tmp_check))
350                 return -EOVERFLOW;
351
352         size = (unsigned long)p->len * p->nbufs;
353         if (!access_ok(u64_to_user_ptr(p->addr), size))
354                 return -EFAULT;
355
356         p->bgid = READ_ONCE(sqe->buf_group);
357         tmp = READ_ONCE(sqe->off);
358         if (tmp > USHRT_MAX)
359                 return -E2BIG;
360         if (tmp + p->nbufs >= USHRT_MAX)
361                 return -EINVAL;
362         p->bid = tmp;
363         return 0;
364 }
365
366 static int io_refill_buffer_cache(struct io_ring_ctx *ctx)
367 {
368         struct io_buffer *buf;
369         struct page *page;
370         int bufs_in_page;
371
372         /*
373          * Completions that don't happen inline (eg not under uring_lock) will
374          * add to ->io_buffers_comp. If we don't have any free buffers, check
375          * the completion list and splice those entries first.
376          */
377         if (!list_empty_careful(&ctx->io_buffers_comp)) {
378                 spin_lock(&ctx->completion_lock);
379                 if (!list_empty(&ctx->io_buffers_comp)) {
380                         list_splice_init(&ctx->io_buffers_comp,
381                                                 &ctx->io_buffers_cache);
382                         spin_unlock(&ctx->completion_lock);
383                         return 0;
384                 }
385                 spin_unlock(&ctx->completion_lock);
386         }
387
388         /*
389          * No free buffers and no completion entries either. Allocate a new
390          * page worth of buffer entries and add those to our freelist.
391          */
392         page = alloc_page(GFP_KERNEL_ACCOUNT);
393         if (!page)
394                 return -ENOMEM;
395
396         list_add(&page->lru, &ctx->io_buffers_pages);
397
398         buf = page_address(page);
399         bufs_in_page = PAGE_SIZE / sizeof(*buf);
400         while (bufs_in_page) {
401                 list_add_tail(&buf->list, &ctx->io_buffers_cache);
402                 buf++;
403                 bufs_in_page--;
404         }
405
406         return 0;
407 }
408
409 static int io_add_buffers(struct io_ring_ctx *ctx, struct io_provide_buf *pbuf,
410                           struct io_buffer_list *bl)
411 {
412         struct io_buffer *buf;
413         u64 addr = pbuf->addr;
414         int i, bid = pbuf->bid;
415
416         for (i = 0; i < pbuf->nbufs; i++) {
417                 if (list_empty(&ctx->io_buffers_cache) &&
418                     io_refill_buffer_cache(ctx))
419                         break;
420                 buf = list_first_entry(&ctx->io_buffers_cache, struct io_buffer,
421                                         list);
422                 list_move_tail(&buf->list, &bl->buf_list);
423                 buf->addr = addr;
424                 buf->len = min_t(__u32, pbuf->len, MAX_RW_COUNT);
425                 buf->bid = bid;
426                 buf->bgid = pbuf->bgid;
427                 addr += pbuf->len;
428                 bid++;
429                 cond_resched();
430         }
431
432         return i ? 0 : -ENOMEM;
433 }
434
435 int io_provide_buffers(struct io_kiocb *req, unsigned int issue_flags)
436 {
437         struct io_provide_buf *p = io_kiocb_to_cmd(req, struct io_provide_buf);
438         struct io_ring_ctx *ctx = req->ctx;
439         struct io_buffer_list *bl;
440         int ret = 0;
441
442         io_ring_submit_lock(ctx, issue_flags);
443
444         if (unlikely(p->bgid < BGID_ARRAY && !ctx->io_bl)) {
445                 ret = io_init_bl_list(ctx);
446                 if (ret)
447                         goto err;
448         }
449
450         bl = io_buffer_get_list(ctx, p->bgid);
451         if (unlikely(!bl)) {
452                 bl = kzalloc(sizeof(*bl), GFP_KERNEL_ACCOUNT);
453                 if (!bl) {
454                         ret = -ENOMEM;
455                         goto err;
456                 }
457                 INIT_LIST_HEAD(&bl->buf_list);
458                 ret = io_buffer_add_list(ctx, bl, p->bgid);
459                 if (ret) {
460                         kfree(bl);
461                         goto err;
462                 }
463         }
464         /* can't add buffers via this command for a mapped buffer ring */
465         if (bl->is_mapped) {
466                 ret = -EINVAL;
467                 goto err;
468         }
469
470         ret = io_add_buffers(ctx, p, bl);
471 err:
472         io_ring_submit_unlock(ctx, issue_flags);
473
474         if (ret < 0)
475                 req_set_fail(req);
476         io_req_set_res(req, ret, 0);
477         return IOU_OK;
478 }
479
480 static int io_pin_pbuf_ring(struct io_uring_buf_reg *reg,
481                             struct io_buffer_list *bl)
482 {
483         struct io_uring_buf_ring *br;
484         struct page **pages;
485         int nr_pages;
486
487         pages = io_pin_pages(reg->ring_addr,
488                              flex_array_size(br, bufs, reg->ring_entries),
489                              &nr_pages);
490         if (IS_ERR(pages))
491                 return PTR_ERR(pages);
492
493         br = page_address(pages[0]);
494 #ifdef SHM_COLOUR
495         /*
496          * On platforms that have specific aliasing requirements, SHM_COLOUR
497          * is set and we must guarantee that the kernel and user side align
498          * nicely. We cannot do that if IOU_PBUF_RING_MMAP isn't set and
499          * the application mmap's the provided ring buffer. Fail the request
500          * if we, by chance, don't end up with aligned addresses. The app
501          * should use IOU_PBUF_RING_MMAP instead, and liburing will handle
502          * this transparently.
503          */
504         if ((reg->ring_addr | (unsigned long) br) & (SHM_COLOUR - 1)) {
505                 int i;
506
507                 for (i = 0; i < nr_pages; i++)
508                         unpin_user_page(pages[i]);
509                 return -EINVAL;
510         }
511 #endif
512         bl->buf_pages = pages;
513         bl->buf_nr_pages = nr_pages;
514         bl->buf_ring = br;
515         bl->is_mapped = 1;
516         bl->is_mmap = 0;
517         return 0;
518 }
519
520 static int io_alloc_pbuf_ring(struct io_uring_buf_reg *reg,
521                               struct io_buffer_list *bl)
522 {
523         gfp_t gfp = GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP;
524         size_t ring_size;
525         void *ptr;
526
527         ring_size = reg->ring_entries * sizeof(struct io_uring_buf_ring);
528         ptr = (void *) __get_free_pages(gfp, get_order(ring_size));
529         if (!ptr)
530                 return -ENOMEM;
531
532         bl->buf_ring = ptr;
533         bl->is_mapped = 1;
534         bl->is_mmap = 1;
535         return 0;
536 }
537
538 int io_register_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
539 {
540         struct io_uring_buf_reg reg;
541         struct io_buffer_list *bl, *free_bl = NULL;
542         int ret;
543
544         if (copy_from_user(&reg, arg, sizeof(reg)))
545                 return -EFAULT;
546
547         if (reg.resv[0] || reg.resv[1] || reg.resv[2])
548                 return -EINVAL;
549         if (reg.flags & ~IOU_PBUF_RING_MMAP)
550                 return -EINVAL;
551         if (!(reg.flags & IOU_PBUF_RING_MMAP)) {
552                 if (!reg.ring_addr)
553                         return -EFAULT;
554                 if (reg.ring_addr & ~PAGE_MASK)
555                         return -EINVAL;
556         } else {
557                 if (reg.ring_addr)
558                         return -EINVAL;
559         }
560
561         if (!is_power_of_2(reg.ring_entries))
562                 return -EINVAL;
563
564         /* cannot disambiguate full vs empty due to head/tail size */
565         if (reg.ring_entries >= 65536)
566                 return -EINVAL;
567
568         if (unlikely(reg.bgid < BGID_ARRAY && !ctx->io_bl)) {
569                 int ret = io_init_bl_list(ctx);
570                 if (ret)
571                         return ret;
572         }
573
574         bl = io_buffer_get_list(ctx, reg.bgid);
575         if (bl) {
576                 /* if mapped buffer ring OR classic exists, don't allow */
577                 if (bl->is_mapped || !list_empty(&bl->buf_list))
578                         return -EEXIST;
579         } else {
580                 free_bl = bl = kzalloc(sizeof(*bl), GFP_KERNEL);
581                 if (!bl)
582                         return -ENOMEM;
583         }
584
585         if (!(reg.flags & IOU_PBUF_RING_MMAP))
586                 ret = io_pin_pbuf_ring(&reg, bl);
587         else
588                 ret = io_alloc_pbuf_ring(&reg, bl);
589
590         if (!ret) {
591                 bl->nr_entries = reg.ring_entries;
592                 bl->mask = reg.ring_entries - 1;
593
594                 io_buffer_add_list(ctx, bl, reg.bgid);
595                 return 0;
596         }
597
598         kfree(free_bl);
599         return ret;
600 }
601
602 int io_unregister_pbuf_ring(struct io_ring_ctx *ctx, void __user *arg)
603 {
604         struct io_uring_buf_reg reg;
605         struct io_buffer_list *bl;
606
607         if (copy_from_user(&reg, arg, sizeof(reg)))
608                 return -EFAULT;
609         if (reg.resv[0] || reg.resv[1] || reg.resv[2])
610                 return -EINVAL;
611         if (reg.flags)
612                 return -EINVAL;
613
614         bl = io_buffer_get_list(ctx, reg.bgid);
615         if (!bl)
616                 return -ENOENT;
617         if (!bl->is_mapped)
618                 return -EINVAL;
619
620         __io_remove_buffers(ctx, bl, -1U);
621         if (bl->bgid >= BGID_ARRAY) {
622                 xa_erase(&ctx->io_bl_xa, bl->bgid);
623                 kfree(bl);
624         }
625         return 0;
626 }
627
628 void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid)
629 {
630         struct io_buffer_list *bl;
631
632         bl = io_buffer_get_list(ctx, bgid);
633         if (!bl || !bl->is_mmap)
634                 return NULL;
635
636         return bl->buf_ring;
637 }