1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
5 #include <linux/file.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>
13 #include <uapi/linux/io_uring.h>
16 #include "openclose.h"
19 struct io_rsrc_update {
27 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
28 struct io_mapped_ubuf **pimu,
29 struct page **last_hpage);
31 #define IO_RSRC_REF_BATCH 100
34 #define IORING_MAX_FIXED_FILES (1U << 20)
35 #define IORING_MAX_REG_BUFFERS (1U << 14)
37 void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
38 __must_hold(&ctx->uring_lock)
40 if (ctx->rsrc_cached_refs) {
41 io_rsrc_put_node(ctx->rsrc_node, ctx->rsrc_cached_refs);
42 ctx->rsrc_cached_refs = 0;
46 static inline void __io_unaccount_mem(struct user_struct *user,
47 unsigned long nr_pages)
49 atomic_long_sub(nr_pages, &user->locked_vm);
52 static inline int __io_account_mem(struct user_struct *user,
53 unsigned long nr_pages)
55 unsigned long page_limit, cur_pages, new_pages;
57 /* Don't allow more pages than we can safely lock */
58 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
60 cur_pages = atomic_long_read(&user->locked_vm);
62 new_pages = cur_pages + nr_pages;
63 if (new_pages > page_limit)
65 } while (!atomic_long_try_cmpxchg(&user->locked_vm,
66 &cur_pages, new_pages));
70 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
73 __io_unaccount_mem(ctx->user, nr_pages);
76 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
79 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
84 ret = __io_account_mem(ctx->user, nr_pages);
90 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
95 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
96 void __user *arg, unsigned index)
98 struct iovec __user *src;
102 struct compat_iovec __user *ciovs;
103 struct compat_iovec ciov;
105 ciovs = (struct compat_iovec __user *) arg;
106 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
109 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
110 dst->iov_len = ciov.iov_len;
114 src = (struct iovec __user *) arg;
115 if (copy_from_user(dst, &src[index], sizeof(*dst)))
120 static int io_buffer_validate(struct iovec *iov)
122 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
125 * Don't impose further limits on the size and buffer
126 * constraints here, we'll -EINVAL later when IO is
127 * submitted if they are wrong.
130 return iov->iov_len ? -EFAULT : 0;
134 /* arbitrary limit, but we need something */
135 if (iov->iov_len > SZ_1G)
138 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
144 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
146 struct io_mapped_ubuf *imu = *slot;
149 if (imu != ctx->dummy_ubuf) {
150 for (i = 0; i < imu->nr_bvecs; i++)
151 unpin_user_page(imu->bvec[i].bv_page);
153 io_unaccount_mem(ctx, imu->acct_pages);
159 void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
160 __must_hold(&ctx->uring_lock)
162 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
163 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
166 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
168 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
169 struct io_ring_ctx *ctx = rsrc_data->ctx;
170 struct io_rsrc_put *prsrc, *tmp;
172 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
173 list_del(&prsrc->list);
176 if (ctx->flags & IORING_SETUP_IOPOLL) {
177 mutex_lock(&ctx->uring_lock);
178 io_post_aux_cqe(ctx, prsrc->tag, 0, 0, true);
179 mutex_unlock(&ctx->uring_lock);
181 io_post_aux_cqe(ctx, prsrc->tag, 0, 0, true);
185 rsrc_data->do_put(ctx, prsrc);
189 io_rsrc_node_destroy(ref_node);
190 if (atomic_dec_and_test(&rsrc_data->refs))
191 complete(&rsrc_data->done);
194 void io_rsrc_put_work(struct work_struct *work)
196 struct io_ring_ctx *ctx;
197 struct llist_node *node;
199 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
200 node = llist_del_all(&ctx->rsrc_put_llist);
203 struct io_rsrc_node *ref_node;
204 struct llist_node *next = node->next;
206 ref_node = llist_entry(node, struct io_rsrc_node, llist);
207 __io_rsrc_put_work(ref_node);
212 void io_wait_rsrc_data(struct io_rsrc_data *data)
214 if (data && !atomic_dec_and_test(&data->refs))
215 wait_for_completion(&data->done);
218 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
220 percpu_ref_exit(&ref_node->refs);
224 static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
226 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
227 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
229 bool first_add = false;
230 unsigned long delay = HZ;
232 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
235 /* if we are mid-quiesce then do not delay */
236 if (node->rsrc_data->quiesce)
239 while (!list_empty(&ctx->rsrc_ref_list)) {
240 node = list_first_entry(&ctx->rsrc_ref_list,
241 struct io_rsrc_node, node);
242 /* recycle ref nodes in order */
245 list_del(&node->node);
246 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
248 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
251 mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
254 static struct io_rsrc_node *io_rsrc_node_alloc(void)
256 struct io_rsrc_node *ref_node;
258 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
262 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
267 INIT_LIST_HEAD(&ref_node->node);
268 INIT_LIST_HEAD(&ref_node->rsrc_list);
269 ref_node->done = false;
273 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
274 struct io_rsrc_data *data_to_kill)
275 __must_hold(&ctx->uring_lock)
277 WARN_ON_ONCE(!ctx->rsrc_backup_node);
278 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
280 io_rsrc_refs_drop(ctx);
283 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
285 rsrc_node->rsrc_data = data_to_kill;
286 spin_lock_irq(&ctx->rsrc_ref_lock);
287 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
288 spin_unlock_irq(&ctx->rsrc_ref_lock);
290 atomic_inc(&data_to_kill->refs);
291 percpu_ref_kill(&rsrc_node->refs);
292 ctx->rsrc_node = NULL;
295 if (!ctx->rsrc_node) {
296 ctx->rsrc_node = ctx->rsrc_backup_node;
297 ctx->rsrc_backup_node = NULL;
301 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
303 if (ctx->rsrc_backup_node)
305 ctx->rsrc_backup_node = io_rsrc_node_alloc();
306 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
309 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
310 struct io_ring_ctx *ctx)
314 /* As we may drop ->uring_lock, other task may have started quiesce */
318 data->quiesce = true;
320 ret = io_rsrc_node_switch_start(ctx);
323 io_rsrc_node_switch(ctx, data);
325 /* kill initial ref, already quiesced if zero */
326 if (atomic_dec_and_test(&data->refs))
328 mutex_unlock(&ctx->uring_lock);
329 flush_delayed_work(&ctx->rsrc_put_work);
330 ret = wait_for_completion_interruptible(&data->done);
332 mutex_lock(&ctx->uring_lock);
333 if (atomic_read(&data->refs) > 0) {
335 * it has been revived by another thread while
338 mutex_unlock(&ctx->uring_lock);
344 atomic_inc(&data->refs);
345 /* wait for all works potentially completing data->done */
346 flush_delayed_work(&ctx->rsrc_put_work);
347 reinit_completion(&data->done);
349 ret = io_run_task_work_sig();
350 mutex_lock(&ctx->uring_lock);
352 data->quiesce = false;
357 static void io_free_page_table(void **table, size_t size)
359 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
361 for (i = 0; i < nr_tables; i++)
366 static void io_rsrc_data_free(struct io_rsrc_data *data)
368 size_t size = data->nr * sizeof(data->tags[0][0]);
371 io_free_page_table((void **)data->tags, size);
375 static __cold void **io_alloc_page_table(size_t size)
377 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
378 size_t init_size = size;
381 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
385 for (i = 0; i < nr_tables; i++) {
386 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
388 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
390 io_free_page_table(table, init_size);
398 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
399 rsrc_put_fn *do_put, u64 __user *utags,
400 unsigned nr, struct io_rsrc_data **pdata)
402 struct io_rsrc_data *data;
406 data = kzalloc(sizeof(*data), GFP_KERNEL);
409 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
417 data->do_put = do_put;
420 for (i = 0; i < nr; i++) {
421 u64 *tag_slot = io_get_tag_slot(data, i);
423 if (copy_from_user(tag_slot, &utags[i],
429 atomic_set(&data->refs, 1);
430 init_completion(&data->done);
434 io_rsrc_data_free(data);
438 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
439 struct io_uring_rsrc_update2 *up,
442 u64 __user *tags = u64_to_user_ptr(up->tags);
443 __s32 __user *fds = u64_to_user_ptr(up->data);
444 struct io_rsrc_data *data = ctx->file_data;
445 struct io_fixed_file *file_slot;
449 bool needs_switch = false;
453 if (up->offset + nr_args > ctx->nr_user_files)
456 for (done = 0; done < nr_args; done++) {
459 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
460 copy_from_user(&fd, &fds[done], sizeof(fd))) {
464 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
468 if (fd == IORING_REGISTER_FILES_SKIP)
471 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
472 file_slot = io_fixed_file_slot(&ctx->file_table, i);
474 if (file_slot->file_ptr) {
475 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
476 err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
479 file_slot->file_ptr = 0;
480 io_file_bitmap_clear(&ctx->file_table, i);
490 * Don't allow io_uring instances to be registered. If
491 * UNIX isn't enabled, then this causes a reference
492 * cycle and this instance can never get freed. If UNIX
493 * is enabled we'll handle it just fine, but there's
494 * still no point in allowing a ring fd as it doesn't
495 * support regular read/write anyway.
497 if (io_is_uring_fops(file)) {
502 err = io_scm_file_account(ctx, file);
507 *io_get_tag_slot(data, i) = tag;
508 io_fixed_file_set(file_slot, file);
509 io_file_bitmap_set(&ctx->file_table, i);
514 io_rsrc_node_switch(ctx, data);
515 return done ? done : err;
518 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
519 struct io_uring_rsrc_update2 *up,
520 unsigned int nr_args)
522 u64 __user *tags = u64_to_user_ptr(up->tags);
523 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
524 struct page *last_hpage = NULL;
525 bool needs_switch = false;
531 if (up->offset + nr_args > ctx->nr_user_bufs)
534 for (done = 0; done < nr_args; done++) {
535 struct io_mapped_ubuf *imu;
536 int offset = up->offset + done;
539 err = io_copy_iov(ctx, &iov, iovs, done);
542 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
546 err = io_buffer_validate(&iov);
549 if (!iov.iov_base && tag) {
553 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
557 i = array_index_nospec(offset, ctx->nr_user_bufs);
558 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
559 err = io_queue_rsrc_removal(ctx->buf_data, i,
560 ctx->rsrc_node, ctx->user_bufs[i]);
562 io_buffer_unmap(ctx, &imu);
565 ctx->user_bufs[i] = ctx->dummy_ubuf;
569 ctx->user_bufs[i] = imu;
570 *io_get_tag_slot(ctx->buf_data, offset) = tag;
574 io_rsrc_node_switch(ctx, ctx->buf_data);
575 return done ? done : err;
578 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
579 struct io_uring_rsrc_update2 *up,
585 if (check_add_overflow(up->offset, nr_args, &tmp))
587 err = io_rsrc_node_switch_start(ctx);
592 case IORING_RSRC_FILE:
593 return __io_sqe_files_update(ctx, up, nr_args);
594 case IORING_RSRC_BUFFER:
595 return __io_sqe_buffers_update(ctx, up, nr_args);
600 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
603 struct io_uring_rsrc_update2 up;
607 memset(&up, 0, sizeof(up));
608 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
610 if (up.resv || up.resv2)
612 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
615 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
616 unsigned size, unsigned type)
618 struct io_uring_rsrc_update2 up;
620 if (size != sizeof(up))
622 if (copy_from_user(&up, arg, sizeof(up)))
624 if (!up.nr || up.resv || up.resv2)
626 return __io_register_rsrc_update(ctx, type, &up, up.nr);
629 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
630 unsigned int size, unsigned int type)
632 struct io_uring_rsrc_register rr;
634 /* keep it extendible */
635 if (size != sizeof(rr))
638 memset(&rr, 0, sizeof(rr));
639 if (copy_from_user(&rr, arg, size))
641 if (!rr.nr || rr.resv2)
643 if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
647 case IORING_RSRC_FILE:
648 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
650 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
651 rr.nr, u64_to_user_ptr(rr.tags));
652 case IORING_RSRC_BUFFER:
653 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
655 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
656 rr.nr, u64_to_user_ptr(rr.tags));
661 int io_rsrc_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
663 struct io_rsrc_update *up = io_kiocb_to_cmd(req);
665 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
667 if (sqe->rw_flags || sqe->splice_fd_in)
670 up->offset = READ_ONCE(sqe->off);
671 up->nr_args = READ_ONCE(sqe->len);
674 up->arg = READ_ONCE(sqe->addr);
675 up->type = READ_ONCE(sqe->ioprio);
679 static int io_files_update_with_index_alloc(struct io_kiocb *req,
680 unsigned int issue_flags)
682 struct io_rsrc_update *up = io_kiocb_to_cmd(req);
683 __s32 __user *fds = u64_to_user_ptr(up->arg);
688 if (!req->ctx->file_data)
691 for (done = 0; done < up->nr_args; done++) {
692 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
702 ret = io_fixed_fd_install(req, issue_flags, file,
703 IORING_FILE_INDEX_ALLOC);
706 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
707 __io_close_fixed(req->ctx, issue_flags, ret);
718 static int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
720 struct io_rsrc_update *up = io_kiocb_to_cmd(req);
721 struct io_ring_ctx *ctx = req->ctx;
722 struct io_uring_rsrc_update2 up2;
725 up2.offset = up->offset;
732 if (up->offset == IORING_FILE_INDEX_ALLOC) {
733 ret = io_files_update_with_index_alloc(req, issue_flags);
735 io_ring_submit_lock(ctx, issue_flags);
736 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
738 io_ring_submit_unlock(ctx, issue_flags);
743 io_req_set_res(req, ret, 0);
747 int io_rsrc_update(struct io_kiocb *req, unsigned int issue_flags)
749 struct io_rsrc_update *up = io_kiocb_to_cmd(req);
752 case IORING_RSRC_UPDATE_FILES:
753 return io_files_update(req, issue_flags);
758 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
759 struct io_rsrc_node *node, void *rsrc)
761 u64 *tag_slot = io_get_tag_slot(data, idx);
762 struct io_rsrc_put *prsrc;
764 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
768 prsrc->tag = *tag_slot;
771 list_add(&prsrc->list, &node->rsrc_list);
775 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
777 #if !defined(IO_URING_SCM_ALL)
780 for (i = 0; i < ctx->nr_user_files; i++) {
781 struct file *file = io_file_from_index(&ctx->file_table, i);
785 if (io_fixed_file_slot(&ctx->file_table, i)->file_ptr & FFS_SCM)
787 io_file_bitmap_clear(&ctx->file_table, i);
792 #if defined(CONFIG_UNIX)
793 if (ctx->ring_sock) {
794 struct sock *sock = ctx->ring_sock->sk;
797 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
801 io_free_file_tables(&ctx->file_table);
802 io_rsrc_data_free(ctx->file_data);
803 ctx->file_data = NULL;
804 ctx->nr_user_files = 0;
807 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
809 unsigned nr = ctx->nr_user_files;
816 * Quiesce may unlock ->uring_lock, and while it's not held
817 * prevent new requests using the table.
819 ctx->nr_user_files = 0;
820 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
821 ctx->nr_user_files = nr;
823 __io_sqe_files_unregister(ctx);
828 * Ensure the UNIX gc is aware of our file set, so we are certain that
829 * the io_uring can be safely unregistered on process exit, even if we have
830 * loops in the file referencing. We account only files that can hold other
831 * files because otherwise they can't form a loop and so are not interesting
834 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
836 #if defined(CONFIG_UNIX)
837 struct sock *sk = ctx->ring_sock->sk;
838 struct sk_buff_head *head = &sk->sk_receive_queue;
839 struct scm_fp_list *fpl;
842 if (likely(!io_file_need_scm(file)))
846 * See if we can merge this file into an existing skb SCM_RIGHTS
847 * file set. If there's no room, fall back to allocating a new skb
850 spin_lock_irq(&head->lock);
851 skb = skb_peek(head);
852 if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
853 __skb_unlink(skb, head);
856 spin_unlock_irq(&head->lock);
859 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
863 skb = alloc_skb(0, GFP_KERNEL);
869 fpl->user = get_uid(current_user());
870 fpl->max = SCM_MAX_FD;
873 UNIXCB(skb).fp = fpl;
875 skb->destructor = unix_destruct_scm;
876 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
879 fpl = UNIXCB(skb).fp;
880 fpl->fp[fpl->count++] = get_file(file);
881 unix_inflight(fpl->user, file);
882 skb_queue_head(head, skb);
888 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
890 struct file *file = prsrc->file;
891 #if defined(CONFIG_UNIX)
892 struct sock *sock = ctx->ring_sock->sk;
893 struct sk_buff_head list, *head = &sock->sk_receive_queue;
897 if (!io_file_need_scm(file)) {
902 __skb_queue_head_init(&list);
905 * Find the skb that holds this file in its SCM_RIGHTS. When found,
906 * remove this entry and rearrange the file array.
908 skb = skb_dequeue(head);
910 struct scm_fp_list *fp;
913 for (i = 0; i < fp->count; i++) {
916 if (fp->fp[i] != file)
919 unix_notinflight(fp->user, fp->fp[i]);
920 left = fp->count - 1 - i;
922 memmove(&fp->fp[i], &fp->fp[i + 1],
923 left * sizeof(struct file *));
930 __skb_queue_tail(&list, skb);
940 __skb_queue_tail(&list, skb);
942 skb = skb_dequeue(head);
945 if (skb_peek(&list)) {
946 spin_lock_irq(&head->lock);
947 while ((skb = __skb_dequeue(&list)) != NULL)
948 __skb_queue_tail(head, skb);
949 spin_unlock_irq(&head->lock);
956 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
957 unsigned nr_args, u64 __user *tags)
959 __s32 __user *fds = (__s32 __user *) arg;
968 if (nr_args > IORING_MAX_FIXED_FILES)
970 if (nr_args > rlimit(RLIMIT_NOFILE))
972 ret = io_rsrc_node_switch_start(ctx);
975 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
980 if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
981 io_rsrc_data_free(ctx->file_data);
982 ctx->file_data = NULL;
986 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
987 struct io_fixed_file *file_slot;
989 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
993 /* allow sparse sets */
994 if (!fds || fd == -1) {
996 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
1003 if (unlikely(!file))
1007 * Don't allow io_uring instances to be registered. If UNIX
1008 * isn't enabled, then this causes a reference cycle and this
1009 * instance can never get freed. If UNIX is enabled we'll
1010 * handle it just fine, but there's still no point in allowing
1011 * a ring fd as it doesn't support regular read/write anyway.
1013 if (io_is_uring_fops(file)) {
1017 ret = io_scm_file_account(ctx, file);
1022 file_slot = io_fixed_file_slot(&ctx->file_table, i);
1023 io_fixed_file_set(file_slot, file);
1024 io_file_bitmap_set(&ctx->file_table, i);
1027 /* default it to the whole table */
1028 io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
1029 io_rsrc_node_switch(ctx, NULL);
1032 __io_sqe_files_unregister(ctx);
1036 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
1038 io_buffer_unmap(ctx, &prsrc->buf);
1042 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1046 for (i = 0; i < ctx->nr_user_bufs; i++)
1047 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
1048 kfree(ctx->user_bufs);
1049 io_rsrc_data_free(ctx->buf_data);
1050 ctx->user_bufs = NULL;
1051 ctx->buf_data = NULL;
1052 ctx->nr_user_bufs = 0;
1055 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1057 unsigned nr = ctx->nr_user_bufs;
1064 * Quiesce may unlock ->uring_lock, and while it's not held
1065 * prevent new requests using the table.
1067 ctx->nr_user_bufs = 0;
1068 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
1069 ctx->nr_user_bufs = nr;
1071 __io_sqe_buffers_unregister(ctx);
1076 * Not super efficient, but this is just a registration time. And we do cache
1077 * the last compound head, so generally we'll only do a full search if we don't
1080 * We check if the given compound head page has already been accounted, to
1081 * avoid double accounting it. This allows us to account the full size of the
1082 * page, not just the constituent pages of a huge page.
1084 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
1085 int nr_pages, struct page *hpage)
1089 /* check current page array */
1090 for (i = 0; i < nr_pages; i++) {
1091 if (!PageCompound(pages[i]))
1093 if (compound_head(pages[i]) == hpage)
1097 /* check previously registered pages */
1098 for (i = 0; i < ctx->nr_user_bufs; i++) {
1099 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
1101 for (j = 0; j < imu->nr_bvecs; j++) {
1102 if (!PageCompound(imu->bvec[j].bv_page))
1104 if (compound_head(imu->bvec[j].bv_page) == hpage)
1112 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1113 int nr_pages, struct io_mapped_ubuf *imu,
1114 struct page **last_hpage)
1118 imu->acct_pages = 0;
1119 for (i = 0; i < nr_pages; i++) {
1120 if (!PageCompound(pages[i])) {
1125 hpage = compound_head(pages[i]);
1126 if (hpage == *last_hpage)
1128 *last_hpage = hpage;
1129 if (headpage_already_acct(ctx, pages, i, hpage))
1131 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1135 if (!imu->acct_pages)
1138 ret = io_account_mem(ctx, imu->acct_pages);
1140 imu->acct_pages = 0;
1144 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1146 unsigned long start, end, nr_pages;
1147 struct vm_area_struct **vmas = NULL;
1148 struct page **pages = NULL;
1149 int i, pret, ret = -ENOMEM;
1151 end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1152 start = ubuf >> PAGE_SHIFT;
1153 nr_pages = end - start;
1155 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1159 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
1165 mmap_read_lock(current->mm);
1166 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1168 if (pret == nr_pages) {
1169 /* don't support file backed memory */
1170 for (i = 0; i < nr_pages; i++) {
1171 struct vm_area_struct *vma = vmas[i];
1173 if (vma_is_shmem(vma))
1176 !is_file_hugepages(vma->vm_file)) {
1183 ret = pret < 0 ? pret : -EFAULT;
1185 mmap_read_unlock(current->mm);
1188 * if we did partial map, or found file backed vmas,
1189 * release any pages we did get
1192 unpin_user_pages(pages, pret);
1200 pages = ERR_PTR(ret);
1205 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1206 struct io_mapped_ubuf **pimu,
1207 struct page **last_hpage)
1209 struct io_mapped_ubuf *imu = NULL;
1210 struct page **pages = NULL;
1213 int ret, nr_pages, i;
1215 *pimu = ctx->dummy_ubuf;
1220 pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1222 if (IS_ERR(pages)) {
1223 ret = PTR_ERR(pages);
1228 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1232 ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1234 unpin_user_pages(pages, nr_pages);
1238 off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1239 size = iov->iov_len;
1240 for (i = 0; i < nr_pages; i++) {
1243 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1244 imu->bvec[i].bv_page = pages[i];
1245 imu->bvec[i].bv_len = vec_len;
1246 imu->bvec[i].bv_offset = off;
1250 /* store original address for later verification */
1251 imu->ubuf = (unsigned long) iov->iov_base;
1252 imu->ubuf_end = imu->ubuf + iov->iov_len;
1253 imu->nr_bvecs = nr_pages;
1263 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1265 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1266 return ctx->user_bufs ? 0 : -ENOMEM;
1269 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1270 unsigned int nr_args, u64 __user *tags)
1272 struct page *last_hpage = NULL;
1273 struct io_rsrc_data *data;
1277 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1281 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1283 ret = io_rsrc_node_switch_start(ctx);
1286 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
1289 ret = io_buffers_map_alloc(ctx, nr_args);
1291 io_rsrc_data_free(data);
1295 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1297 ret = io_copy_iov(ctx, &iov, arg, i);
1300 ret = io_buffer_validate(&iov);
1304 memset(&iov, 0, sizeof(iov));
1307 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1312 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1318 WARN_ON_ONCE(ctx->buf_data);
1320 ctx->buf_data = data;
1322 __io_sqe_buffers_unregister(ctx);
1324 io_rsrc_node_switch(ctx, NULL);
1328 int io_import_fixed(int ddir, struct iov_iter *iter,
1329 struct io_mapped_ubuf *imu,
1330 u64 buf_addr, size_t len)
1335 if (WARN_ON_ONCE(!imu))
1337 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1339 /* not inside the mapped region */
1340 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1344 * May not be a start of buffer, set size appropriately
1345 * and advance us to the beginning.
1347 offset = buf_addr - imu->ubuf;
1348 iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1352 * Don't use iov_iter_advance() here, as it's really slow for
1353 * using the latter parts of a big fixed buffer - it iterates
1354 * over each segment manually. We can cheat a bit here, because
1357 * 1) it's a BVEC iter, we set it up
1358 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1359 * first and last bvec
1361 * So just find our index, and adjust the iterator afterwards.
1362 * If the offset is within the first bvec (or the whole first
1363 * bvec, just use iov_iter_advance(). This makes it easier
1364 * since we can just skip the first segment, which may not
1365 * be PAGE_SIZE aligned.
1367 const struct bio_vec *bvec = imu->bvec;
1369 if (offset <= bvec->bv_len) {
1370 iov_iter_advance(iter, offset);
1372 unsigned long seg_skip;
1374 /* skip first vec */
1375 offset -= bvec->bv_len;
1376 seg_skip = 1 + (offset >> PAGE_SHIFT);
1378 iter->bvec = bvec + seg_skip;
1379 iter->nr_segs -= seg_skip;
1380 iter->count -= bvec->bv_len + offset;
1381 iter->iov_offset = offset & ~PAGE_MASK;