io_uring: improve rsrc quiesce refs checks
[platform/kernel/linux-starfive.git] / io_uring / rsrc.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/nospec.h>
9 #include <linux/hugetlb.h>
10 #include <linux/compat.h>
11 #include <linux/io_uring.h>
12
13 #include <uapi/linux/io_uring.h>
14
15 #include "io_uring.h"
16 #include "openclose.h"
17 #include "rsrc.h"
18
19 struct io_rsrc_update {
20         struct file                     *file;
21         u64                             arg;
22         u32                             nr_args;
23         u32                             offset;
24 };
25
26 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
27                                   struct io_mapped_ubuf **pimu,
28                                   struct page **last_hpage);
29
30 #define IO_RSRC_REF_BATCH       100
31
32 /* only define max */
33 #define IORING_MAX_FIXED_FILES  (1U << 20)
34 #define IORING_MAX_REG_BUFFERS  (1U << 14)
35
36 void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
37         __must_hold(&ctx->uring_lock)
38 {
39         if (ctx->rsrc_cached_refs) {
40                 io_rsrc_put_node(ctx->rsrc_node, ctx->rsrc_cached_refs);
41                 ctx->rsrc_cached_refs = 0;
42         }
43 }
44
45 int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
46 {
47         unsigned long page_limit, cur_pages, new_pages;
48
49         if (!nr_pages)
50                 return 0;
51
52         /* Don't allow more pages than we can safely lock */
53         page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
54
55         cur_pages = atomic_long_read(&user->locked_vm);
56         do {
57                 new_pages = cur_pages + nr_pages;
58                 if (new_pages > page_limit)
59                         return -ENOMEM;
60         } while (!atomic_long_try_cmpxchg(&user->locked_vm,
61                                           &cur_pages, new_pages));
62         return 0;
63 }
64
65 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
66 {
67         if (ctx->user)
68                 __io_unaccount_mem(ctx->user, nr_pages);
69
70         if (ctx->mm_account)
71                 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
72 }
73
74 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
75 {
76         int ret;
77
78         if (ctx->user) {
79                 ret = __io_account_mem(ctx->user, nr_pages);
80                 if (ret)
81                         return ret;
82         }
83
84         if (ctx->mm_account)
85                 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
86
87         return 0;
88 }
89
90 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
91                        void __user *arg, unsigned index)
92 {
93         struct iovec __user *src;
94
95 #ifdef CONFIG_COMPAT
96         if (ctx->compat) {
97                 struct compat_iovec __user *ciovs;
98                 struct compat_iovec ciov;
99
100                 ciovs = (struct compat_iovec __user *) arg;
101                 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
102                         return -EFAULT;
103
104                 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
105                 dst->iov_len = ciov.iov_len;
106                 return 0;
107         }
108 #endif
109         src = (struct iovec __user *) arg;
110         if (copy_from_user(dst, &src[index], sizeof(*dst)))
111                 return -EFAULT;
112         return 0;
113 }
114
115 static int io_buffer_validate(struct iovec *iov)
116 {
117         unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
118
119         /*
120          * Don't impose further limits on the size and buffer
121          * constraints here, we'll -EINVAL later when IO is
122          * submitted if they are wrong.
123          */
124         if (!iov->iov_base)
125                 return iov->iov_len ? -EFAULT : 0;
126         if (!iov->iov_len)
127                 return -EFAULT;
128
129         /* arbitrary limit, but we need something */
130         if (iov->iov_len > SZ_1G)
131                 return -EFAULT;
132
133         if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
134                 return -EOVERFLOW;
135
136         return 0;
137 }
138
139 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
140 {
141         struct io_mapped_ubuf *imu = *slot;
142         unsigned int i;
143
144         if (imu != ctx->dummy_ubuf) {
145                 for (i = 0; i < imu->nr_bvecs; i++)
146                         unpin_user_page(imu->bvec[i].bv_page);
147                 if (imu->acct_pages)
148                         io_unaccount_mem(ctx, imu->acct_pages);
149                 kvfree(imu);
150         }
151         *slot = NULL;
152 }
153
154 void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
155         __must_hold(&ctx->uring_lock)
156 {
157         ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
158         percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
159 }
160
161 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
162 {
163         struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
164         struct io_ring_ctx *ctx = rsrc_data->ctx;
165         struct io_rsrc_put *prsrc, *tmp;
166
167         list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
168                 list_del(&prsrc->list);
169
170                 if (prsrc->tag) {
171                         if (ctx->flags & IORING_SETUP_IOPOLL) {
172                                 mutex_lock(&ctx->uring_lock);
173                                 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
174                                 mutex_unlock(&ctx->uring_lock);
175                         } else {
176                                 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
177                         }
178                 }
179
180                 rsrc_data->do_put(ctx, prsrc);
181                 kfree(prsrc);
182         }
183
184         io_rsrc_node_destroy(ref_node);
185         if (atomic_dec_and_test(&rsrc_data->refs))
186                 complete(&rsrc_data->done);
187 }
188
189 void io_rsrc_put_work(struct work_struct *work)
190 {
191         struct io_ring_ctx *ctx;
192         struct llist_node *node;
193
194         ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
195         node = llist_del_all(&ctx->rsrc_put_llist);
196
197         while (node) {
198                 struct io_rsrc_node *ref_node;
199                 struct llist_node *next = node->next;
200
201                 ref_node = llist_entry(node, struct io_rsrc_node, llist);
202                 __io_rsrc_put_work(ref_node);
203                 node = next;
204         }
205 }
206
207 void io_wait_rsrc_data(struct io_rsrc_data *data)
208 {
209         if (data && !atomic_dec_and_test(&data->refs))
210                 wait_for_completion(&data->done);
211 }
212
213 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
214 {
215         percpu_ref_exit(&ref_node->refs);
216         kfree(ref_node);
217 }
218
219 static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
220 {
221         struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
222         struct io_ring_ctx *ctx = node->rsrc_data->ctx;
223         unsigned long flags;
224         bool first_add = false;
225         unsigned long delay = HZ;
226
227         spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
228         node->done = true;
229
230         /* if we are mid-quiesce then do not delay */
231         if (node->rsrc_data->quiesce)
232                 delay = 0;
233
234         while (!list_empty(&ctx->rsrc_ref_list)) {
235                 node = list_first_entry(&ctx->rsrc_ref_list,
236                                             struct io_rsrc_node, node);
237                 /* recycle ref nodes in order */
238                 if (!node->done)
239                         break;
240                 list_del(&node->node);
241                 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
242         }
243         spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
244
245         if (first_add)
246                 mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
247 }
248
249 static struct io_rsrc_node *io_rsrc_node_alloc(void)
250 {
251         struct io_rsrc_node *ref_node;
252
253         ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
254         if (!ref_node)
255                 return NULL;
256
257         if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
258                             0, GFP_KERNEL)) {
259                 kfree(ref_node);
260                 return NULL;
261         }
262         INIT_LIST_HEAD(&ref_node->node);
263         INIT_LIST_HEAD(&ref_node->rsrc_list);
264         ref_node->done = false;
265         return ref_node;
266 }
267
268 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
269                          struct io_rsrc_data *data_to_kill)
270         __must_hold(&ctx->uring_lock)
271 {
272         WARN_ON_ONCE(!ctx->rsrc_backup_node);
273         WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
274
275         io_rsrc_refs_drop(ctx);
276
277         if (data_to_kill) {
278                 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
279
280                 rsrc_node->rsrc_data = data_to_kill;
281                 spin_lock_irq(&ctx->rsrc_ref_lock);
282                 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
283                 spin_unlock_irq(&ctx->rsrc_ref_lock);
284
285                 atomic_inc(&data_to_kill->refs);
286                 percpu_ref_kill(&rsrc_node->refs);
287                 ctx->rsrc_node = NULL;
288         }
289
290         if (!ctx->rsrc_node) {
291                 ctx->rsrc_node = ctx->rsrc_backup_node;
292                 ctx->rsrc_backup_node = NULL;
293         }
294 }
295
296 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
297 {
298         if (ctx->rsrc_backup_node)
299                 return 0;
300         ctx->rsrc_backup_node = io_rsrc_node_alloc();
301         return ctx->rsrc_backup_node ? 0 : -ENOMEM;
302 }
303
304 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
305                                       struct io_ring_ctx *ctx)
306 {
307         int ret;
308
309         /* As we may drop ->uring_lock, other task may have started quiesce */
310         if (data->quiesce)
311                 return -ENXIO;
312
313         data->quiesce = true;
314         do {
315                 ret = io_rsrc_node_switch_start(ctx);
316                 if (ret)
317                         break;
318                 io_rsrc_node_switch(ctx, data);
319
320                 /* kill initial ref, already quiesced if zero */
321                 if (atomic_dec_and_test(&data->refs))
322                         break;
323                 mutex_unlock(&ctx->uring_lock);
324
325                 ret = io_run_task_work_sig(ctx);
326                 if (ret < 0)
327                         goto reinit;
328
329                 flush_delayed_work(&ctx->rsrc_put_work);
330                 ret = wait_for_completion_interruptible(&data->done);
331                 if (!ret) {
332                         mutex_lock(&ctx->uring_lock);
333                         if (atomic_read(&data->refs) <= 0)
334                                 break;
335                         /*
336                          * it has been revived by another thread while
337                          * we were unlocked
338                          */
339                         mutex_unlock(&ctx->uring_lock);
340                 }
341 reinit:
342                 atomic_inc(&data->refs);
343                 /* wait for all works potentially completing data->done */
344                 flush_delayed_work(&ctx->rsrc_put_work);
345                 reinit_completion(&data->done);
346
347                 mutex_lock(&ctx->uring_lock);
348         } while (ret >= 0);
349         data->quiesce = false;
350
351         return ret;
352 }
353
354 static void io_free_page_table(void **table, size_t size)
355 {
356         unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
357
358         for (i = 0; i < nr_tables; i++)
359                 kfree(table[i]);
360         kfree(table);
361 }
362
363 static void io_rsrc_data_free(struct io_rsrc_data *data)
364 {
365         size_t size = data->nr * sizeof(data->tags[0][0]);
366
367         if (data->tags)
368                 io_free_page_table((void **)data->tags, size);
369         kfree(data);
370 }
371
372 static __cold void **io_alloc_page_table(size_t size)
373 {
374         unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
375         size_t init_size = size;
376         void **table;
377
378         table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
379         if (!table)
380                 return NULL;
381
382         for (i = 0; i < nr_tables; i++) {
383                 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
384
385                 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
386                 if (!table[i]) {
387                         io_free_page_table(table, init_size);
388                         return NULL;
389                 }
390                 size -= this_size;
391         }
392         return table;
393 }
394
395 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
396                                      rsrc_put_fn *do_put, u64 __user *utags,
397                                      unsigned nr, struct io_rsrc_data **pdata)
398 {
399         struct io_rsrc_data *data;
400         int ret = -ENOMEM;
401         unsigned i;
402
403         data = kzalloc(sizeof(*data), GFP_KERNEL);
404         if (!data)
405                 return -ENOMEM;
406         data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
407         if (!data->tags) {
408                 kfree(data);
409                 return -ENOMEM;
410         }
411
412         data->nr = nr;
413         data->ctx = ctx;
414         data->do_put = do_put;
415         if (utags) {
416                 ret = -EFAULT;
417                 for (i = 0; i < nr; i++) {
418                         u64 *tag_slot = io_get_tag_slot(data, i);
419
420                         if (copy_from_user(tag_slot, &utags[i],
421                                            sizeof(*tag_slot)))
422                                 goto fail;
423                 }
424         }
425
426         atomic_set(&data->refs, 1);
427         init_completion(&data->done);
428         *pdata = data;
429         return 0;
430 fail:
431         io_rsrc_data_free(data);
432         return ret;
433 }
434
435 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
436                                  struct io_uring_rsrc_update2 *up,
437                                  unsigned nr_args)
438 {
439         u64 __user *tags = u64_to_user_ptr(up->tags);
440         __s32 __user *fds = u64_to_user_ptr(up->data);
441         struct io_rsrc_data *data = ctx->file_data;
442         struct io_fixed_file *file_slot;
443         struct file *file;
444         int fd, i, err = 0;
445         unsigned int done;
446         bool needs_switch = false;
447
448         if (!ctx->file_data)
449                 return -ENXIO;
450         if (up->offset + nr_args > ctx->nr_user_files)
451                 return -EINVAL;
452
453         for (done = 0; done < nr_args; done++) {
454                 u64 tag = 0;
455
456                 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
457                     copy_from_user(&fd, &fds[done], sizeof(fd))) {
458                         err = -EFAULT;
459                         break;
460                 }
461                 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
462                         err = -EINVAL;
463                         break;
464                 }
465                 if (fd == IORING_REGISTER_FILES_SKIP)
466                         continue;
467
468                 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
469                 file_slot = io_fixed_file_slot(&ctx->file_table, i);
470
471                 if (file_slot->file_ptr) {
472                         file = (struct file *)(file_slot->file_ptr & FFS_MASK);
473                         err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
474                         if (err)
475                                 break;
476                         file_slot->file_ptr = 0;
477                         io_file_bitmap_clear(&ctx->file_table, i);
478                         needs_switch = true;
479                 }
480                 if (fd != -1) {
481                         file = fget(fd);
482                         if (!file) {
483                                 err = -EBADF;
484                                 break;
485                         }
486                         /*
487                          * Don't allow io_uring instances to be registered. If
488                          * UNIX isn't enabled, then this causes a reference
489                          * cycle and this instance can never get freed. If UNIX
490                          * is enabled we'll handle it just fine, but there's
491                          * still no point in allowing a ring fd as it doesn't
492                          * support regular read/write anyway.
493                          */
494                         if (io_is_uring_fops(file)) {
495                                 fput(file);
496                                 err = -EBADF;
497                                 break;
498                         }
499                         err = io_scm_file_account(ctx, file);
500                         if (err) {
501                                 fput(file);
502                                 break;
503                         }
504                         *io_get_tag_slot(data, i) = tag;
505                         io_fixed_file_set(file_slot, file);
506                         io_file_bitmap_set(&ctx->file_table, i);
507                 }
508         }
509
510         if (needs_switch)
511                 io_rsrc_node_switch(ctx, data);
512         return done ? done : err;
513 }
514
515 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
516                                    struct io_uring_rsrc_update2 *up,
517                                    unsigned int nr_args)
518 {
519         u64 __user *tags = u64_to_user_ptr(up->tags);
520         struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
521         struct page *last_hpage = NULL;
522         bool needs_switch = false;
523         __u32 done;
524         int i, err;
525
526         if (!ctx->buf_data)
527                 return -ENXIO;
528         if (up->offset + nr_args > ctx->nr_user_bufs)
529                 return -EINVAL;
530
531         for (done = 0; done < nr_args; done++) {
532                 struct io_mapped_ubuf *imu;
533                 int offset = up->offset + done;
534                 u64 tag = 0;
535
536                 err = io_copy_iov(ctx, &iov, iovs, done);
537                 if (err)
538                         break;
539                 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
540                         err = -EFAULT;
541                         break;
542                 }
543                 err = io_buffer_validate(&iov);
544                 if (err)
545                         break;
546                 if (!iov.iov_base && tag) {
547                         err = -EINVAL;
548                         break;
549                 }
550                 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
551                 if (err)
552                         break;
553
554                 i = array_index_nospec(offset, ctx->nr_user_bufs);
555                 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
556                         err = io_queue_rsrc_removal(ctx->buf_data, i,
557                                                     ctx->rsrc_node, ctx->user_bufs[i]);
558                         if (unlikely(err)) {
559                                 io_buffer_unmap(ctx, &imu);
560                                 break;
561                         }
562                         ctx->user_bufs[i] = ctx->dummy_ubuf;
563                         needs_switch = true;
564                 }
565
566                 ctx->user_bufs[i] = imu;
567                 *io_get_tag_slot(ctx->buf_data, offset) = tag;
568         }
569
570         if (needs_switch)
571                 io_rsrc_node_switch(ctx, ctx->buf_data);
572         return done ? done : err;
573 }
574
575 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
576                                      struct io_uring_rsrc_update2 *up,
577                                      unsigned nr_args)
578 {
579         __u32 tmp;
580         int err;
581
582         if (check_add_overflow(up->offset, nr_args, &tmp))
583                 return -EOVERFLOW;
584         err = io_rsrc_node_switch_start(ctx);
585         if (err)
586                 return err;
587
588         switch (type) {
589         case IORING_RSRC_FILE:
590                 return __io_sqe_files_update(ctx, up, nr_args);
591         case IORING_RSRC_BUFFER:
592                 return __io_sqe_buffers_update(ctx, up, nr_args);
593         }
594         return -EINVAL;
595 }
596
597 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
598                              unsigned nr_args)
599 {
600         struct io_uring_rsrc_update2 up;
601
602         if (!nr_args)
603                 return -EINVAL;
604         memset(&up, 0, sizeof(up));
605         if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
606                 return -EFAULT;
607         if (up.resv || up.resv2)
608                 return -EINVAL;
609         return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
610 }
611
612 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
613                             unsigned size, unsigned type)
614 {
615         struct io_uring_rsrc_update2 up;
616
617         if (size != sizeof(up))
618                 return -EINVAL;
619         if (copy_from_user(&up, arg, sizeof(up)))
620                 return -EFAULT;
621         if (!up.nr || up.resv || up.resv2)
622                 return -EINVAL;
623         return __io_register_rsrc_update(ctx, type, &up, up.nr);
624 }
625
626 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
627                             unsigned int size, unsigned int type)
628 {
629         struct io_uring_rsrc_register rr;
630
631         /* keep it extendible */
632         if (size != sizeof(rr))
633                 return -EINVAL;
634
635         memset(&rr, 0, sizeof(rr));
636         if (copy_from_user(&rr, arg, size))
637                 return -EFAULT;
638         if (!rr.nr || rr.resv2)
639                 return -EINVAL;
640         if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
641                 return -EINVAL;
642
643         switch (type) {
644         case IORING_RSRC_FILE:
645                 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
646                         break;
647                 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
648                                              rr.nr, u64_to_user_ptr(rr.tags));
649         case IORING_RSRC_BUFFER:
650                 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
651                         break;
652                 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
653                                                rr.nr, u64_to_user_ptr(rr.tags));
654         }
655         return -EINVAL;
656 }
657
658 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
659 {
660         struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
661
662         if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
663                 return -EINVAL;
664         if (sqe->rw_flags || sqe->splice_fd_in)
665                 return -EINVAL;
666
667         up->offset = READ_ONCE(sqe->off);
668         up->nr_args = READ_ONCE(sqe->len);
669         if (!up->nr_args)
670                 return -EINVAL;
671         up->arg = READ_ONCE(sqe->addr);
672         return 0;
673 }
674
675 static int io_files_update_with_index_alloc(struct io_kiocb *req,
676                                             unsigned int issue_flags)
677 {
678         struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
679         __s32 __user *fds = u64_to_user_ptr(up->arg);
680         unsigned int done;
681         struct file *file;
682         int ret, fd;
683
684         if (!req->ctx->file_data)
685                 return -ENXIO;
686
687         for (done = 0; done < up->nr_args; done++) {
688                 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
689                         ret = -EFAULT;
690                         break;
691                 }
692
693                 file = fget(fd);
694                 if (!file) {
695                         ret = -EBADF;
696                         break;
697                 }
698                 ret = io_fixed_fd_install(req, issue_flags, file,
699                                           IORING_FILE_INDEX_ALLOC);
700                 if (ret < 0)
701                         break;
702                 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
703                         __io_close_fixed(req->ctx, issue_flags, ret);
704                         ret = -EFAULT;
705                         break;
706                 }
707         }
708
709         if (done)
710                 return done;
711         return ret;
712 }
713
714 int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
715 {
716         struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
717         struct io_ring_ctx *ctx = req->ctx;
718         struct io_uring_rsrc_update2 up2;
719         int ret;
720
721         up2.offset = up->offset;
722         up2.data = up->arg;
723         up2.nr = 0;
724         up2.tags = 0;
725         up2.resv = 0;
726         up2.resv2 = 0;
727
728         if (up->offset == IORING_FILE_INDEX_ALLOC) {
729                 ret = io_files_update_with_index_alloc(req, issue_flags);
730         } else {
731                 io_ring_submit_lock(ctx, issue_flags);
732                 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
733                                                 &up2, up->nr_args);
734                 io_ring_submit_unlock(ctx, issue_flags);
735         }
736
737         if (ret < 0)
738                 req_set_fail(req);
739         io_req_set_res(req, ret, 0);
740         return IOU_OK;
741 }
742
743 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
744                           struct io_rsrc_node *node, void *rsrc)
745 {
746         u64 *tag_slot = io_get_tag_slot(data, idx);
747         struct io_rsrc_put *prsrc;
748
749         prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
750         if (!prsrc)
751                 return -ENOMEM;
752
753         prsrc->tag = *tag_slot;
754         *tag_slot = 0;
755         prsrc->rsrc = rsrc;
756         list_add(&prsrc->list, &node->rsrc_list);
757         return 0;
758 }
759
760 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
761 {
762         int i;
763
764         for (i = 0; i < ctx->nr_user_files; i++) {
765                 struct file *file = io_file_from_index(&ctx->file_table, i);
766
767                 /* skip scm accounted files, they'll be freed by ->ring_sock */
768                 if (!file || io_file_need_scm(file))
769                         continue;
770                 io_file_bitmap_clear(&ctx->file_table, i);
771                 fput(file);
772         }
773
774 #if defined(CONFIG_UNIX)
775         if (ctx->ring_sock) {
776                 struct sock *sock = ctx->ring_sock->sk;
777                 struct sk_buff *skb;
778
779                 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
780                         kfree_skb(skb);
781         }
782 #endif
783         io_free_file_tables(&ctx->file_table);
784         io_rsrc_data_free(ctx->file_data);
785         ctx->file_data = NULL;
786         ctx->nr_user_files = 0;
787 }
788
789 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
790 {
791         unsigned nr = ctx->nr_user_files;
792         int ret;
793
794         if (!ctx->file_data)
795                 return -ENXIO;
796
797         /*
798          * Quiesce may unlock ->uring_lock, and while it's not held
799          * prevent new requests using the table.
800          */
801         ctx->nr_user_files = 0;
802         ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
803         ctx->nr_user_files = nr;
804         if (!ret)
805                 __io_sqe_files_unregister(ctx);
806         return ret;
807 }
808
809 /*
810  * Ensure the UNIX gc is aware of our file set, so we are certain that
811  * the io_uring can be safely unregistered on process exit, even if we have
812  * loops in the file referencing. We account only files that can hold other
813  * files because otherwise they can't form a loop and so are not interesting
814  * for GC.
815  */
816 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
817 {
818 #if defined(CONFIG_UNIX)
819         struct sock *sk = ctx->ring_sock->sk;
820         struct sk_buff_head *head = &sk->sk_receive_queue;
821         struct scm_fp_list *fpl;
822         struct sk_buff *skb;
823
824         if (likely(!io_file_need_scm(file)))
825                 return 0;
826
827         /*
828          * See if we can merge this file into an existing skb SCM_RIGHTS
829          * file set. If there's no room, fall back to allocating a new skb
830          * and filling it in.
831          */
832         spin_lock_irq(&head->lock);
833         skb = skb_peek(head);
834         if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
835                 __skb_unlink(skb, head);
836         else
837                 skb = NULL;
838         spin_unlock_irq(&head->lock);
839
840         if (!skb) {
841                 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
842                 if (!fpl)
843                         return -ENOMEM;
844
845                 skb = alloc_skb(0, GFP_KERNEL);
846                 if (!skb) {
847                         kfree(fpl);
848                         return -ENOMEM;
849                 }
850
851                 fpl->user = get_uid(current_user());
852                 fpl->max = SCM_MAX_FD;
853                 fpl->count = 0;
854
855                 UNIXCB(skb).fp = fpl;
856                 skb->sk = sk;
857                 skb->scm_io_uring = 1;
858                 skb->destructor = unix_destruct_scm;
859                 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
860         }
861
862         fpl = UNIXCB(skb).fp;
863         fpl->fp[fpl->count++] = get_file(file);
864         unix_inflight(fpl->user, file);
865         skb_queue_head(head, skb);
866         fput(file);
867 #endif
868         return 0;
869 }
870
871 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
872 {
873         struct file *file = prsrc->file;
874 #if defined(CONFIG_UNIX)
875         struct sock *sock = ctx->ring_sock->sk;
876         struct sk_buff_head list, *head = &sock->sk_receive_queue;
877         struct sk_buff *skb;
878         int i;
879
880         if (!io_file_need_scm(file)) {
881                 fput(file);
882                 return;
883         }
884
885         __skb_queue_head_init(&list);
886
887         /*
888          * Find the skb that holds this file in its SCM_RIGHTS. When found,
889          * remove this entry and rearrange the file array.
890          */
891         skb = skb_dequeue(head);
892         while (skb) {
893                 struct scm_fp_list *fp;
894
895                 fp = UNIXCB(skb).fp;
896                 for (i = 0; i < fp->count; i++) {
897                         int left;
898
899                         if (fp->fp[i] != file)
900                                 continue;
901
902                         unix_notinflight(fp->user, fp->fp[i]);
903                         left = fp->count - 1 - i;
904                         if (left) {
905                                 memmove(&fp->fp[i], &fp->fp[i + 1],
906                                                 left * sizeof(struct file *));
907                         }
908                         fp->count--;
909                         if (!fp->count) {
910                                 kfree_skb(skb);
911                                 skb = NULL;
912                         } else {
913                                 __skb_queue_tail(&list, skb);
914                         }
915                         fput(file);
916                         file = NULL;
917                         break;
918                 }
919
920                 if (!file)
921                         break;
922
923                 __skb_queue_tail(&list, skb);
924
925                 skb = skb_dequeue(head);
926         }
927
928         if (skb_peek(&list)) {
929                 spin_lock_irq(&head->lock);
930                 while ((skb = __skb_dequeue(&list)) != NULL)
931                         __skb_queue_tail(head, skb);
932                 spin_unlock_irq(&head->lock);
933         }
934 #else
935         fput(file);
936 #endif
937 }
938
939 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
940                           unsigned nr_args, u64 __user *tags)
941 {
942         __s32 __user *fds = (__s32 __user *) arg;
943         struct file *file;
944         int fd, ret;
945         unsigned i;
946
947         if (ctx->file_data)
948                 return -EBUSY;
949         if (!nr_args)
950                 return -EINVAL;
951         if (nr_args > IORING_MAX_FIXED_FILES)
952                 return -EMFILE;
953         if (nr_args > rlimit(RLIMIT_NOFILE))
954                 return -EMFILE;
955         ret = io_rsrc_node_switch_start(ctx);
956         if (ret)
957                 return ret;
958         ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
959                                  &ctx->file_data);
960         if (ret)
961                 return ret;
962
963         if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
964                 io_rsrc_data_free(ctx->file_data);
965                 ctx->file_data = NULL;
966                 return -ENOMEM;
967         }
968
969         for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
970                 struct io_fixed_file *file_slot;
971
972                 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
973                         ret = -EFAULT;
974                         goto fail;
975                 }
976                 /* allow sparse sets */
977                 if (!fds || fd == -1) {
978                         ret = -EINVAL;
979                         if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
980                                 goto fail;
981                         continue;
982                 }
983
984                 file = fget(fd);
985                 ret = -EBADF;
986                 if (unlikely(!file))
987                         goto fail;
988
989                 /*
990                  * Don't allow io_uring instances to be registered. If UNIX
991                  * isn't enabled, then this causes a reference cycle and this
992                  * instance can never get freed. If UNIX is enabled we'll
993                  * handle it just fine, but there's still no point in allowing
994                  * a ring fd as it doesn't support regular read/write anyway.
995                  */
996                 if (io_is_uring_fops(file)) {
997                         fput(file);
998                         goto fail;
999                 }
1000                 ret = io_scm_file_account(ctx, file);
1001                 if (ret) {
1002                         fput(file);
1003                         goto fail;
1004                 }
1005                 file_slot = io_fixed_file_slot(&ctx->file_table, i);
1006                 io_fixed_file_set(file_slot, file);
1007                 io_file_bitmap_set(&ctx->file_table, i);
1008         }
1009
1010         /* default it to the whole table */
1011         io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
1012         io_rsrc_node_switch(ctx, NULL);
1013         return 0;
1014 fail:
1015         __io_sqe_files_unregister(ctx);
1016         return ret;
1017 }
1018
1019 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
1020 {
1021         io_buffer_unmap(ctx, &prsrc->buf);
1022         prsrc->buf = NULL;
1023 }
1024
1025 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1026 {
1027         unsigned int i;
1028
1029         for (i = 0; i < ctx->nr_user_bufs; i++)
1030                 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
1031         kfree(ctx->user_bufs);
1032         io_rsrc_data_free(ctx->buf_data);
1033         ctx->user_bufs = NULL;
1034         ctx->buf_data = NULL;
1035         ctx->nr_user_bufs = 0;
1036 }
1037
1038 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1039 {
1040         unsigned nr = ctx->nr_user_bufs;
1041         int ret;
1042
1043         if (!ctx->buf_data)
1044                 return -ENXIO;
1045
1046         /*
1047          * Quiesce may unlock ->uring_lock, and while it's not held
1048          * prevent new requests using the table.
1049          */
1050         ctx->nr_user_bufs = 0;
1051         ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
1052         ctx->nr_user_bufs = nr;
1053         if (!ret)
1054                 __io_sqe_buffers_unregister(ctx);
1055         return ret;
1056 }
1057
1058 /*
1059  * Not super efficient, but this is just a registration time. And we do cache
1060  * the last compound head, so generally we'll only do a full search if we don't
1061  * match that one.
1062  *
1063  * We check if the given compound head page has already been accounted, to
1064  * avoid double accounting it. This allows us to account the full size of the
1065  * page, not just the constituent pages of a huge page.
1066  */
1067 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
1068                                   int nr_pages, struct page *hpage)
1069 {
1070         int i, j;
1071
1072         /* check current page array */
1073         for (i = 0; i < nr_pages; i++) {
1074                 if (!PageCompound(pages[i]))
1075                         continue;
1076                 if (compound_head(pages[i]) == hpage)
1077                         return true;
1078         }
1079
1080         /* check previously registered pages */
1081         for (i = 0; i < ctx->nr_user_bufs; i++) {
1082                 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
1083
1084                 for (j = 0; j < imu->nr_bvecs; j++) {
1085                         if (!PageCompound(imu->bvec[j].bv_page))
1086                                 continue;
1087                         if (compound_head(imu->bvec[j].bv_page) == hpage)
1088                                 return true;
1089                 }
1090         }
1091
1092         return false;
1093 }
1094
1095 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1096                                  int nr_pages, struct io_mapped_ubuf *imu,
1097                                  struct page **last_hpage)
1098 {
1099         int i, ret;
1100
1101         imu->acct_pages = 0;
1102         for (i = 0; i < nr_pages; i++) {
1103                 if (!PageCompound(pages[i])) {
1104                         imu->acct_pages++;
1105                 } else {
1106                         struct page *hpage;
1107
1108                         hpage = compound_head(pages[i]);
1109                         if (hpage == *last_hpage)
1110                                 continue;
1111                         *last_hpage = hpage;
1112                         if (headpage_already_acct(ctx, pages, i, hpage))
1113                                 continue;
1114                         imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1115                 }
1116         }
1117
1118         if (!imu->acct_pages)
1119                 return 0;
1120
1121         ret = io_account_mem(ctx, imu->acct_pages);
1122         if (ret)
1123                 imu->acct_pages = 0;
1124         return ret;
1125 }
1126
1127 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1128 {
1129         unsigned long start, end, nr_pages;
1130         struct vm_area_struct **vmas = NULL;
1131         struct page **pages = NULL;
1132         int i, pret, ret = -ENOMEM;
1133
1134         end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1135         start = ubuf >> PAGE_SHIFT;
1136         nr_pages = end - start;
1137
1138         pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1139         if (!pages)
1140                 goto done;
1141
1142         vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
1143                               GFP_KERNEL);
1144         if (!vmas)
1145                 goto done;
1146
1147         ret = 0;
1148         mmap_read_lock(current->mm);
1149         pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1150                               pages, vmas);
1151         if (pret == nr_pages) {
1152                 /* don't support file backed memory */
1153                 for (i = 0; i < nr_pages; i++) {
1154                         struct vm_area_struct *vma = vmas[i];
1155
1156                         if (vma_is_shmem(vma))
1157                                 continue;
1158                         if (vma->vm_file &&
1159                             !is_file_hugepages(vma->vm_file)) {
1160                                 ret = -EOPNOTSUPP;
1161                                 break;
1162                         }
1163                 }
1164                 *npages = nr_pages;
1165         } else {
1166                 ret = pret < 0 ? pret : -EFAULT;
1167         }
1168         mmap_read_unlock(current->mm);
1169         if (ret) {
1170                 /*
1171                  * if we did partial map, or found file backed vmas,
1172                  * release any pages we did get
1173                  */
1174                 if (pret > 0)
1175                         unpin_user_pages(pages, pret);
1176                 goto done;
1177         }
1178         ret = 0;
1179 done:
1180         kvfree(vmas);
1181         if (ret < 0) {
1182                 kvfree(pages);
1183                 pages = ERR_PTR(ret);
1184         }
1185         return pages;
1186 }
1187
1188 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1189                                   struct io_mapped_ubuf **pimu,
1190                                   struct page **last_hpage)
1191 {
1192         struct io_mapped_ubuf *imu = NULL;
1193         struct page **pages = NULL;
1194         unsigned long off;
1195         size_t size;
1196         int ret, nr_pages, i;
1197
1198         *pimu = ctx->dummy_ubuf;
1199         if (!iov->iov_base)
1200                 return 0;
1201
1202         ret = -ENOMEM;
1203         pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1204                                 &nr_pages);
1205         if (IS_ERR(pages)) {
1206                 ret = PTR_ERR(pages);
1207                 pages = NULL;
1208                 goto done;
1209         }
1210
1211         imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1212         if (!imu)
1213                 goto done;
1214
1215         ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1216         if (ret) {
1217                 unpin_user_pages(pages, nr_pages);
1218                 goto done;
1219         }
1220
1221         off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1222         size = iov->iov_len;
1223         for (i = 0; i < nr_pages; i++) {
1224                 size_t vec_len;
1225
1226                 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1227                 imu->bvec[i].bv_page = pages[i];
1228                 imu->bvec[i].bv_len = vec_len;
1229                 imu->bvec[i].bv_offset = off;
1230                 off = 0;
1231                 size -= vec_len;
1232         }
1233         /* store original address for later verification */
1234         imu->ubuf = (unsigned long) iov->iov_base;
1235         imu->ubuf_end = imu->ubuf + iov->iov_len;
1236         imu->nr_bvecs = nr_pages;
1237         *pimu = imu;
1238         ret = 0;
1239 done:
1240         if (ret)
1241                 kvfree(imu);
1242         kvfree(pages);
1243         return ret;
1244 }
1245
1246 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1247 {
1248         ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1249         return ctx->user_bufs ? 0 : -ENOMEM;
1250 }
1251
1252 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1253                             unsigned int nr_args, u64 __user *tags)
1254 {
1255         struct page *last_hpage = NULL;
1256         struct io_rsrc_data *data;
1257         int i, ret;
1258         struct iovec iov;
1259
1260         BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1261
1262         if (ctx->user_bufs)
1263                 return -EBUSY;
1264         if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1265                 return -EINVAL;
1266         ret = io_rsrc_node_switch_start(ctx);
1267         if (ret)
1268                 return ret;
1269         ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
1270         if (ret)
1271                 return ret;
1272         ret = io_buffers_map_alloc(ctx, nr_args);
1273         if (ret) {
1274                 io_rsrc_data_free(data);
1275                 return ret;
1276         }
1277
1278         for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1279                 if (arg) {
1280                         ret = io_copy_iov(ctx, &iov, arg, i);
1281                         if (ret)
1282                                 break;
1283                         ret = io_buffer_validate(&iov);
1284                         if (ret)
1285                                 break;
1286                 } else {
1287                         memset(&iov, 0, sizeof(iov));
1288                 }
1289
1290                 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1291                         ret = -EINVAL;
1292                         break;
1293                 }
1294
1295                 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1296                                              &last_hpage);
1297                 if (ret)
1298                         break;
1299         }
1300
1301         WARN_ON_ONCE(ctx->buf_data);
1302
1303         ctx->buf_data = data;
1304         if (ret)
1305                 __io_sqe_buffers_unregister(ctx);
1306         else
1307                 io_rsrc_node_switch(ctx, NULL);
1308         return ret;
1309 }
1310
1311 int io_import_fixed(int ddir, struct iov_iter *iter,
1312                            struct io_mapped_ubuf *imu,
1313                            u64 buf_addr, size_t len)
1314 {
1315         u64 buf_end;
1316         size_t offset;
1317
1318         if (WARN_ON_ONCE(!imu))
1319                 return -EFAULT;
1320         if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1321                 return -EFAULT;
1322         /* not inside the mapped region */
1323         if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1324                 return -EFAULT;
1325
1326         /*
1327          * May not be a start of buffer, set size appropriately
1328          * and advance us to the beginning.
1329          */
1330         offset = buf_addr - imu->ubuf;
1331         iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1332
1333         if (offset) {
1334                 /*
1335                  * Don't use iov_iter_advance() here, as it's really slow for
1336                  * using the latter parts of a big fixed buffer - it iterates
1337                  * over each segment manually. We can cheat a bit here, because
1338                  * we know that:
1339                  *
1340                  * 1) it's a BVEC iter, we set it up
1341                  * 2) all bvecs are PAGE_SIZE in size, except potentially the
1342                  *    first and last bvec
1343                  *
1344                  * So just find our index, and adjust the iterator afterwards.
1345                  * If the offset is within the first bvec (or the whole first
1346                  * bvec, just use iov_iter_advance(). This makes it easier
1347                  * since we can just skip the first segment, which may not
1348                  * be PAGE_SIZE aligned.
1349                  */
1350                 const struct bio_vec *bvec = imu->bvec;
1351
1352                 if (offset <= bvec->bv_len) {
1353                         iov_iter_advance(iter, offset);
1354                 } else {
1355                         unsigned long seg_skip;
1356
1357                         /* skip first vec */
1358                         offset -= bvec->bv_len;
1359                         seg_skip = 1 + (offset >> PAGE_SHIFT);
1360
1361                         iter->bvec = bvec + seg_skip;
1362                         iter->nr_segs -= seg_skip;
1363                         iter->count -= bvec->bv_len + offset;
1364                         iter->iov_offset = offset & ~PAGE_MASK;
1365                 }
1366         }
1367
1368         return 0;
1369 }