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