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 {
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);
30 #define IO_RSRC_REF_BATCH 100
33 #define IORING_MAX_FIXED_FILES (1U << 20)
34 #define IORING_MAX_REG_BUFFERS (1U << 14)
36 void io_rsrc_refs_drop(struct io_ring_ctx *ctx)
37 __must_hold(&ctx->uring_lock)
39 if (ctx->rsrc_cached_refs) {
40 io_rsrc_put_node(ctx->rsrc_node, ctx->rsrc_cached_refs);
41 ctx->rsrc_cached_refs = 0;
45 int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
47 unsigned long page_limit, cur_pages, new_pages;
52 /* Don't allow more pages than we can safely lock */
53 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
55 cur_pages = atomic_long_read(&user->locked_vm);
57 new_pages = cur_pages + nr_pages;
58 if (new_pages > page_limit)
60 } while (!atomic_long_try_cmpxchg(&user->locked_vm,
61 &cur_pages, new_pages));
65 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
68 __io_unaccount_mem(ctx->user, nr_pages);
71 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
74 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
79 ret = __io_account_mem(ctx->user, nr_pages);
85 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
90 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
91 void __user *arg, unsigned index)
93 struct iovec __user *src;
97 struct compat_iovec __user *ciovs;
98 struct compat_iovec ciov;
100 ciovs = (struct compat_iovec __user *) arg;
101 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
104 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
105 dst->iov_len = ciov.iov_len;
109 src = (struct iovec __user *) arg;
110 if (copy_from_user(dst, &src[index], sizeof(*dst)))
115 static int io_buffer_validate(struct iovec *iov)
117 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
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.
125 return iov->iov_len ? -EFAULT : 0;
129 /* arbitrary limit, but we need something */
130 if (iov->iov_len > SZ_1G)
133 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
139 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
141 struct io_mapped_ubuf *imu = *slot;
144 if (imu != ctx->dummy_ubuf) {
145 for (i = 0; i < imu->nr_bvecs; i++)
146 unpin_user_page(imu->bvec[i].bv_page);
148 io_unaccount_mem(ctx, imu->acct_pages);
154 void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
155 __must_hold(&ctx->uring_lock)
157 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
158 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
161 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
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;
167 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
168 list_del(&prsrc->list);
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);
176 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
180 rsrc_data->do_put(ctx, prsrc);
184 io_rsrc_node_destroy(ref_node);
185 if (atomic_dec_and_test(&rsrc_data->refs))
186 complete(&rsrc_data->done);
189 void io_rsrc_put_work(struct work_struct *work)
191 struct io_ring_ctx *ctx;
192 struct llist_node *node;
194 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
195 node = llist_del_all(&ctx->rsrc_put_llist);
198 struct io_rsrc_node *ref_node;
199 struct llist_node *next = node->next;
201 ref_node = llist_entry(node, struct io_rsrc_node, llist);
202 __io_rsrc_put_work(ref_node);
207 void io_rsrc_put_tw(struct callback_head *cb)
209 struct io_ring_ctx *ctx = container_of(cb, struct io_ring_ctx,
212 io_rsrc_put_work(&ctx->rsrc_put_work.work);
215 void io_wait_rsrc_data(struct io_rsrc_data *data)
217 if (data && !atomic_dec_and_test(&data->refs))
218 wait_for_completion(&data->done);
221 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
223 percpu_ref_exit(&ref_node->refs);
227 static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
229 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
230 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
232 bool first_add = false;
233 unsigned long delay = HZ;
235 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
238 /* if we are mid-quiesce then do not delay */
239 if (node->rsrc_data->quiesce)
242 while (!list_empty(&ctx->rsrc_ref_list)) {
243 node = list_first_entry(&ctx->rsrc_ref_list,
244 struct io_rsrc_node, node);
245 /* recycle ref nodes in order */
248 list_del(&node->node);
249 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
251 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
256 if (ctx->submitter_task) {
257 if (!task_work_add(ctx->submitter_task, &ctx->rsrc_put_tw,
261 mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
264 static struct io_rsrc_node *io_rsrc_node_alloc(void)
266 struct io_rsrc_node *ref_node;
268 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
272 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
277 INIT_LIST_HEAD(&ref_node->node);
278 INIT_LIST_HEAD(&ref_node->rsrc_list);
279 ref_node->done = false;
283 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
284 struct io_rsrc_data *data_to_kill)
285 __must_hold(&ctx->uring_lock)
287 WARN_ON_ONCE(!ctx->rsrc_backup_node);
288 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
290 io_rsrc_refs_drop(ctx);
293 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
295 rsrc_node->rsrc_data = data_to_kill;
296 spin_lock_irq(&ctx->rsrc_ref_lock);
297 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
298 spin_unlock_irq(&ctx->rsrc_ref_lock);
300 atomic_inc(&data_to_kill->refs);
301 percpu_ref_kill(&rsrc_node->refs);
302 ctx->rsrc_node = NULL;
305 if (!ctx->rsrc_node) {
306 ctx->rsrc_node = ctx->rsrc_backup_node;
307 ctx->rsrc_backup_node = NULL;
311 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
313 if (ctx->rsrc_backup_node)
315 ctx->rsrc_backup_node = io_rsrc_node_alloc();
316 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
319 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
320 struct io_ring_ctx *ctx)
324 /* As we may drop ->uring_lock, other task may have started quiesce */
327 ret = io_rsrc_node_switch_start(ctx);
330 io_rsrc_node_switch(ctx, data);
332 /* kill initial ref, already quiesced if zero */
333 if (atomic_dec_and_test(&data->refs))
336 data->quiesce = true;
337 mutex_unlock(&ctx->uring_lock);
339 ret = io_run_task_work_sig(ctx);
341 atomic_inc(&data->refs);
342 /* wait for all works potentially completing data->done */
343 flush_delayed_work(&ctx->rsrc_put_work);
344 reinit_completion(&data->done);
345 mutex_lock(&ctx->uring_lock);
349 flush_delayed_work(&ctx->rsrc_put_work);
350 ret = wait_for_completion_interruptible(&data->done);
352 mutex_lock(&ctx->uring_lock);
353 if (atomic_read(&data->refs) <= 0)
356 * it has been revived by another thread while
359 mutex_unlock(&ctx->uring_lock);
362 data->quiesce = false;
367 static void io_free_page_table(void **table, size_t size)
369 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
371 for (i = 0; i < nr_tables; i++)
376 static void io_rsrc_data_free(struct io_rsrc_data *data)
378 size_t size = data->nr * sizeof(data->tags[0][0]);
381 io_free_page_table((void **)data->tags, size);
385 static __cold void **io_alloc_page_table(size_t size)
387 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
388 size_t init_size = size;
391 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
395 for (i = 0; i < nr_tables; i++) {
396 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
398 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
400 io_free_page_table(table, init_size);
408 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
409 rsrc_put_fn *do_put, u64 __user *utags,
410 unsigned nr, struct io_rsrc_data **pdata)
412 struct io_rsrc_data *data;
416 data = kzalloc(sizeof(*data), GFP_KERNEL);
419 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
427 data->do_put = do_put;
430 for (i = 0; i < nr; i++) {
431 u64 *tag_slot = io_get_tag_slot(data, i);
433 if (copy_from_user(tag_slot, &utags[i],
439 atomic_set(&data->refs, 1);
440 init_completion(&data->done);
444 io_rsrc_data_free(data);
448 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
449 struct io_uring_rsrc_update2 *up,
452 u64 __user *tags = u64_to_user_ptr(up->tags);
453 __s32 __user *fds = u64_to_user_ptr(up->data);
454 struct io_rsrc_data *data = ctx->file_data;
455 struct io_fixed_file *file_slot;
459 bool needs_switch = false;
463 if (up->offset + nr_args > ctx->nr_user_files)
466 for (done = 0; done < nr_args; done++) {
469 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
470 copy_from_user(&fd, &fds[done], sizeof(fd))) {
474 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
478 if (fd == IORING_REGISTER_FILES_SKIP)
481 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
482 file_slot = io_fixed_file_slot(&ctx->file_table, i);
484 if (file_slot->file_ptr) {
485 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
486 err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
489 file_slot->file_ptr = 0;
490 io_file_bitmap_clear(&ctx->file_table, i);
500 * Don't allow io_uring instances to be registered. If
501 * UNIX isn't enabled, then this causes a reference
502 * cycle and this instance can never get freed. If UNIX
503 * is enabled we'll handle it just fine, but there's
504 * still no point in allowing a ring fd as it doesn't
505 * support regular read/write anyway.
507 if (io_is_uring_fops(file)) {
512 err = io_scm_file_account(ctx, file);
517 *io_get_tag_slot(data, i) = tag;
518 io_fixed_file_set(file_slot, file);
519 io_file_bitmap_set(&ctx->file_table, i);
524 io_rsrc_node_switch(ctx, data);
525 return done ? done : err;
528 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
529 struct io_uring_rsrc_update2 *up,
530 unsigned int nr_args)
532 u64 __user *tags = u64_to_user_ptr(up->tags);
533 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
534 struct page *last_hpage = NULL;
535 bool needs_switch = false;
541 if (up->offset + nr_args > ctx->nr_user_bufs)
544 for (done = 0; done < nr_args; done++) {
545 struct io_mapped_ubuf *imu;
546 int offset = up->offset + done;
549 err = io_copy_iov(ctx, &iov, iovs, done);
552 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
556 err = io_buffer_validate(&iov);
559 if (!iov.iov_base && tag) {
563 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
567 i = array_index_nospec(offset, ctx->nr_user_bufs);
568 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
569 err = io_queue_rsrc_removal(ctx->buf_data, i,
570 ctx->rsrc_node, ctx->user_bufs[i]);
572 io_buffer_unmap(ctx, &imu);
575 ctx->user_bufs[i] = ctx->dummy_ubuf;
579 ctx->user_bufs[i] = imu;
580 *io_get_tag_slot(ctx->buf_data, offset) = tag;
584 io_rsrc_node_switch(ctx, ctx->buf_data);
585 return done ? done : err;
588 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
589 struct io_uring_rsrc_update2 *up,
595 if (check_add_overflow(up->offset, nr_args, &tmp))
597 err = io_rsrc_node_switch_start(ctx);
602 case IORING_RSRC_FILE:
603 return __io_sqe_files_update(ctx, up, nr_args);
604 case IORING_RSRC_BUFFER:
605 return __io_sqe_buffers_update(ctx, up, nr_args);
610 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
613 struct io_uring_rsrc_update2 up;
617 memset(&up, 0, sizeof(up));
618 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
620 if (up.resv || up.resv2)
622 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
625 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
626 unsigned size, unsigned type)
628 struct io_uring_rsrc_update2 up;
630 if (size != sizeof(up))
632 if (copy_from_user(&up, arg, sizeof(up)))
634 if (!up.nr || up.resv || up.resv2)
636 return __io_register_rsrc_update(ctx, type, &up, up.nr);
639 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
640 unsigned int size, unsigned int type)
642 struct io_uring_rsrc_register rr;
644 /* keep it extendible */
645 if (size != sizeof(rr))
648 memset(&rr, 0, sizeof(rr));
649 if (copy_from_user(&rr, arg, size))
651 if (!rr.nr || rr.resv2)
653 if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
657 case IORING_RSRC_FILE:
658 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
660 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
661 rr.nr, u64_to_user_ptr(rr.tags));
662 case IORING_RSRC_BUFFER:
663 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
665 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
666 rr.nr, u64_to_user_ptr(rr.tags));
671 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
673 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
675 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
677 if (sqe->rw_flags || sqe->splice_fd_in)
680 up->offset = READ_ONCE(sqe->off);
681 up->nr_args = READ_ONCE(sqe->len);
684 up->arg = READ_ONCE(sqe->addr);
688 static int io_files_update_with_index_alloc(struct io_kiocb *req,
689 unsigned int issue_flags)
691 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
692 __s32 __user *fds = u64_to_user_ptr(up->arg);
697 if (!req->ctx->file_data)
700 for (done = 0; done < up->nr_args; done++) {
701 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
711 ret = io_fixed_fd_install(req, issue_flags, file,
712 IORING_FILE_INDEX_ALLOC);
715 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
716 __io_close_fixed(req->ctx, issue_flags, ret);
727 int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
729 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
730 struct io_ring_ctx *ctx = req->ctx;
731 struct io_uring_rsrc_update2 up2;
734 up2.offset = up->offset;
741 if (up->offset == IORING_FILE_INDEX_ALLOC) {
742 ret = io_files_update_with_index_alloc(req, issue_flags);
744 io_ring_submit_lock(ctx, issue_flags);
745 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
747 io_ring_submit_unlock(ctx, issue_flags);
752 io_req_set_res(req, ret, 0);
756 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
757 struct io_rsrc_node *node, void *rsrc)
759 u64 *tag_slot = io_get_tag_slot(data, idx);
760 struct io_rsrc_put *prsrc;
762 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
766 prsrc->tag = *tag_slot;
769 list_add(&prsrc->list, &node->rsrc_list);
773 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
777 for (i = 0; i < ctx->nr_user_files; i++) {
778 struct file *file = io_file_from_index(&ctx->file_table, i);
780 /* skip scm accounted files, they'll be freed by ->ring_sock */
781 if (!file || io_file_need_scm(file))
783 io_file_bitmap_clear(&ctx->file_table, i);
787 #if defined(CONFIG_UNIX)
788 if (ctx->ring_sock) {
789 struct sock *sock = ctx->ring_sock->sk;
792 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
796 io_free_file_tables(&ctx->file_table);
797 io_rsrc_data_free(ctx->file_data);
798 ctx->file_data = NULL;
799 ctx->nr_user_files = 0;
802 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
804 unsigned nr = ctx->nr_user_files;
811 * Quiesce may unlock ->uring_lock, and while it's not held
812 * prevent new requests using the table.
814 ctx->nr_user_files = 0;
815 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
816 ctx->nr_user_files = nr;
818 __io_sqe_files_unregister(ctx);
823 * Ensure the UNIX gc is aware of our file set, so we are certain that
824 * the io_uring can be safely unregistered on process exit, even if we have
825 * loops in the file referencing. We account only files that can hold other
826 * files because otherwise they can't form a loop and so are not interesting
829 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
831 #if defined(CONFIG_UNIX)
832 struct sock *sk = ctx->ring_sock->sk;
833 struct sk_buff_head *head = &sk->sk_receive_queue;
834 struct scm_fp_list *fpl;
837 if (likely(!io_file_need_scm(file)))
841 * See if we can merge this file into an existing skb SCM_RIGHTS
842 * file set. If there's no room, fall back to allocating a new skb
845 spin_lock_irq(&head->lock);
846 skb = skb_peek(head);
847 if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
848 __skb_unlink(skb, head);
851 spin_unlock_irq(&head->lock);
854 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
858 skb = alloc_skb(0, GFP_KERNEL);
864 fpl->user = get_uid(current_user());
865 fpl->max = SCM_MAX_FD;
868 UNIXCB(skb).fp = fpl;
870 skb->scm_io_uring = 1;
871 skb->destructor = unix_destruct_scm;
872 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
875 fpl = UNIXCB(skb).fp;
876 fpl->fp[fpl->count++] = get_file(file);
877 unix_inflight(fpl->user, file);
878 skb_queue_head(head, skb);
884 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
886 struct file *file = prsrc->file;
887 #if defined(CONFIG_UNIX)
888 struct sock *sock = ctx->ring_sock->sk;
889 struct sk_buff_head list, *head = &sock->sk_receive_queue;
893 if (!io_file_need_scm(file)) {
898 __skb_queue_head_init(&list);
901 * Find the skb that holds this file in its SCM_RIGHTS. When found,
902 * remove this entry and rearrange the file array.
904 skb = skb_dequeue(head);
906 struct scm_fp_list *fp;
909 for (i = 0; i < fp->count; i++) {
912 if (fp->fp[i] != file)
915 unix_notinflight(fp->user, fp->fp[i]);
916 left = fp->count - 1 - i;
918 memmove(&fp->fp[i], &fp->fp[i + 1],
919 left * sizeof(struct file *));
926 __skb_queue_tail(&list, skb);
936 __skb_queue_tail(&list, skb);
938 skb = skb_dequeue(head);
941 if (skb_peek(&list)) {
942 spin_lock_irq(&head->lock);
943 while ((skb = __skb_dequeue(&list)) != NULL)
944 __skb_queue_tail(head, skb);
945 spin_unlock_irq(&head->lock);
952 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
953 unsigned nr_args, u64 __user *tags)
955 __s32 __user *fds = (__s32 __user *) arg;
964 if (nr_args > IORING_MAX_FIXED_FILES)
966 if (nr_args > rlimit(RLIMIT_NOFILE))
968 ret = io_rsrc_node_switch_start(ctx);
971 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
976 if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
977 io_rsrc_data_free(ctx->file_data);
978 ctx->file_data = NULL;
982 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
983 struct io_fixed_file *file_slot;
985 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
989 /* allow sparse sets */
990 if (!fds || fd == -1) {
992 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
1003 * Don't allow io_uring instances to be registered. If UNIX
1004 * isn't enabled, then this causes a reference cycle and this
1005 * instance can never get freed. If UNIX is enabled we'll
1006 * handle it just fine, but there's still no point in allowing
1007 * a ring fd as it doesn't support regular read/write anyway.
1009 if (io_is_uring_fops(file)) {
1013 ret = io_scm_file_account(ctx, file);
1018 file_slot = io_fixed_file_slot(&ctx->file_table, i);
1019 io_fixed_file_set(file_slot, file);
1020 io_file_bitmap_set(&ctx->file_table, i);
1023 /* default it to the whole table */
1024 io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
1025 io_rsrc_node_switch(ctx, NULL);
1028 __io_sqe_files_unregister(ctx);
1032 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
1034 io_buffer_unmap(ctx, &prsrc->buf);
1038 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1042 for (i = 0; i < ctx->nr_user_bufs; i++)
1043 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
1044 kfree(ctx->user_bufs);
1045 io_rsrc_data_free(ctx->buf_data);
1046 ctx->user_bufs = NULL;
1047 ctx->buf_data = NULL;
1048 ctx->nr_user_bufs = 0;
1051 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1053 unsigned nr = ctx->nr_user_bufs;
1060 * Quiesce may unlock ->uring_lock, and while it's not held
1061 * prevent new requests using the table.
1063 ctx->nr_user_bufs = 0;
1064 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
1065 ctx->nr_user_bufs = nr;
1067 __io_sqe_buffers_unregister(ctx);
1072 * Not super efficient, but this is just a registration time. And we do cache
1073 * the last compound head, so generally we'll only do a full search if we don't
1076 * We check if the given compound head page has already been accounted, to
1077 * avoid double accounting it. This allows us to account the full size of the
1078 * page, not just the constituent pages of a huge page.
1080 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
1081 int nr_pages, struct page *hpage)
1085 /* check current page array */
1086 for (i = 0; i < nr_pages; i++) {
1087 if (!PageCompound(pages[i]))
1089 if (compound_head(pages[i]) == hpage)
1093 /* check previously registered pages */
1094 for (i = 0; i < ctx->nr_user_bufs; i++) {
1095 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
1097 for (j = 0; j < imu->nr_bvecs; j++) {
1098 if (!PageCompound(imu->bvec[j].bv_page))
1100 if (compound_head(imu->bvec[j].bv_page) == hpage)
1108 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1109 int nr_pages, struct io_mapped_ubuf *imu,
1110 struct page **last_hpage)
1114 imu->acct_pages = 0;
1115 for (i = 0; i < nr_pages; i++) {
1116 if (!PageCompound(pages[i])) {
1121 hpage = compound_head(pages[i]);
1122 if (hpage == *last_hpage)
1124 *last_hpage = hpage;
1125 if (headpage_already_acct(ctx, pages, i, hpage))
1127 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1131 if (!imu->acct_pages)
1134 ret = io_account_mem(ctx, imu->acct_pages);
1136 imu->acct_pages = 0;
1140 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1142 unsigned long start, end, nr_pages;
1143 struct vm_area_struct **vmas = NULL;
1144 struct page **pages = NULL;
1145 int i, pret, ret = -ENOMEM;
1147 end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1148 start = ubuf >> PAGE_SHIFT;
1149 nr_pages = end - start;
1151 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1155 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
1161 mmap_read_lock(current->mm);
1162 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1164 if (pret == nr_pages) {
1165 /* don't support file backed memory */
1166 for (i = 0; i < nr_pages; i++) {
1167 struct vm_area_struct *vma = vmas[i];
1169 if (vma_is_shmem(vma))
1172 !is_file_hugepages(vma->vm_file)) {
1179 ret = pret < 0 ? pret : -EFAULT;
1181 mmap_read_unlock(current->mm);
1184 * if we did partial map, or found file backed vmas,
1185 * release any pages we did get
1188 unpin_user_pages(pages, pret);
1196 pages = ERR_PTR(ret);
1201 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1202 struct io_mapped_ubuf **pimu,
1203 struct page **last_hpage)
1205 struct io_mapped_ubuf *imu = NULL;
1206 struct page **pages = NULL;
1209 int ret, nr_pages, i;
1211 *pimu = ctx->dummy_ubuf;
1216 pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1218 if (IS_ERR(pages)) {
1219 ret = PTR_ERR(pages);
1224 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1228 ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1230 unpin_user_pages(pages, nr_pages);
1234 off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1235 size = iov->iov_len;
1236 for (i = 0; i < nr_pages; i++) {
1239 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1240 bvec_set_page(&imu->bvec[i], pages[i], vec_len, off);
1244 /* store original address for later verification */
1245 imu->ubuf = (unsigned long) iov->iov_base;
1246 imu->ubuf_end = imu->ubuf + iov->iov_len;
1247 imu->nr_bvecs = nr_pages;
1257 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1259 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1260 return ctx->user_bufs ? 0 : -ENOMEM;
1263 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1264 unsigned int nr_args, u64 __user *tags)
1266 struct page *last_hpage = NULL;
1267 struct io_rsrc_data *data;
1271 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1275 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1277 ret = io_rsrc_node_switch_start(ctx);
1280 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
1283 ret = io_buffers_map_alloc(ctx, nr_args);
1285 io_rsrc_data_free(data);
1289 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1291 ret = io_copy_iov(ctx, &iov, arg, i);
1294 ret = io_buffer_validate(&iov);
1298 memset(&iov, 0, sizeof(iov));
1301 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1306 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1312 WARN_ON_ONCE(ctx->buf_data);
1314 ctx->buf_data = data;
1316 __io_sqe_buffers_unregister(ctx);
1318 io_rsrc_node_switch(ctx, NULL);
1322 int io_import_fixed(int ddir, struct iov_iter *iter,
1323 struct io_mapped_ubuf *imu,
1324 u64 buf_addr, size_t len)
1329 if (WARN_ON_ONCE(!imu))
1331 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1333 /* not inside the mapped region */
1334 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1338 * May not be a start of buffer, set size appropriately
1339 * and advance us to the beginning.
1341 offset = buf_addr - imu->ubuf;
1342 iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1346 * Don't use iov_iter_advance() here, as it's really slow for
1347 * using the latter parts of a big fixed buffer - it iterates
1348 * over each segment manually. We can cheat a bit here, because
1351 * 1) it's a BVEC iter, we set it up
1352 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1353 * first and last bvec
1355 * So just find our index, and adjust the iterator afterwards.
1356 * If the offset is within the first bvec (or the whole first
1357 * bvec, just use iov_iter_advance(). This makes it easier
1358 * since we can just skip the first segment, which may not
1359 * be PAGE_SIZE aligned.
1361 const struct bio_vec *bvec = imu->bvec;
1363 if (offset <= bvec->bv_len) {
1364 iov_iter_advance(iter, offset);
1366 unsigned long seg_skip;
1368 /* skip first vec */
1369 offset -= bvec->bv_len;
1370 seg_skip = 1 + (offset >> PAGE_SHIFT);
1372 iter->bvec = bvec + seg_skip;
1373 iter->nr_segs -= seg_skip;
1374 iter->count -= bvec->bv_len + offset;
1375 iter->iov_offset = offset & ~PAGE_MASK;