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);
31 #define IORING_MAX_FIXED_FILES (1U << 20)
32 #define IORING_MAX_REG_BUFFERS (1U << 14)
34 int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
36 unsigned long page_limit, cur_pages, new_pages;
41 /* Don't allow more pages than we can safely lock */
42 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
44 cur_pages = atomic_long_read(&user->locked_vm);
46 new_pages = cur_pages + nr_pages;
47 if (new_pages > page_limit)
49 } while (!atomic_long_try_cmpxchg(&user->locked_vm,
50 &cur_pages, new_pages));
54 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
57 __io_unaccount_mem(ctx->user, nr_pages);
60 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
63 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
68 ret = __io_account_mem(ctx->user, nr_pages);
74 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
79 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
80 void __user *arg, unsigned index)
82 struct iovec __user *src;
86 struct compat_iovec __user *ciovs;
87 struct compat_iovec ciov;
89 ciovs = (struct compat_iovec __user *) arg;
90 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
93 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
94 dst->iov_len = ciov.iov_len;
98 src = (struct iovec __user *) arg;
99 if (copy_from_user(dst, &src[index], sizeof(*dst)))
104 static int io_buffer_validate(struct iovec *iov)
106 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
109 * Don't impose further limits on the size and buffer
110 * constraints here, we'll -EINVAL later when IO is
111 * submitted if they are wrong.
114 return iov->iov_len ? -EFAULT : 0;
118 /* arbitrary limit, but we need something */
119 if (iov->iov_len > SZ_1G)
122 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
128 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
130 struct io_mapped_ubuf *imu = *slot;
133 if (imu != ctx->dummy_ubuf) {
134 for (i = 0; i < imu->nr_bvecs; i++)
135 unpin_user_page(imu->bvec[i].bv_page);
137 io_unaccount_mem(ctx, imu->acct_pages);
143 static void io_rsrc_put_work_one(struct io_rsrc_data *rsrc_data,
144 struct io_rsrc_put *prsrc)
146 struct io_ring_ctx *ctx = rsrc_data->ctx;
149 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
150 rsrc_data->do_put(ctx, prsrc);
153 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
155 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
156 struct io_rsrc_put *prsrc, *tmp;
158 if (ref_node->inline_items)
159 io_rsrc_put_work_one(rsrc_data, &ref_node->item);
161 list_for_each_entry_safe(prsrc, tmp, &ref_node->item_list, list) {
162 list_del(&prsrc->list);
163 io_rsrc_put_work_one(rsrc_data, prsrc);
167 io_rsrc_node_destroy(rsrc_data->ctx, ref_node);
170 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
172 if (!io_alloc_cache_put(&ctx->rsrc_node_cache, &node->cache))
176 void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
177 __must_hold(&node->rsrc_data->ctx->uring_lock)
179 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
181 while (!list_empty(&ctx->rsrc_ref_list)) {
182 node = list_first_entry(&ctx->rsrc_ref_list,
183 struct io_rsrc_node, node);
184 /* recycle ref nodes in order */
187 list_del(&node->node);
188 __io_rsrc_put_work(node);
190 if (list_empty(&ctx->rsrc_ref_list) && unlikely(ctx->rsrc_quiesce))
191 wake_up_all(&ctx->rsrc_quiesce_wq);
194 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
196 struct io_rsrc_node *ref_node;
197 struct io_cache_entry *entry;
199 entry = io_alloc_cache_get(&ctx->rsrc_node_cache);
201 ref_node = container_of(entry, struct io_rsrc_node, cache);
203 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
208 ref_node->rsrc_data = NULL;
210 INIT_LIST_HEAD(&ref_node->node);
211 INIT_LIST_HEAD(&ref_node->item_list);
212 ref_node->inline_items = 0;
216 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
217 struct io_rsrc_data *data_to_kill)
218 __must_hold(&ctx->uring_lock)
220 struct io_rsrc_node *node = ctx->rsrc_node;
221 struct io_rsrc_node *backup = io_rsrc_node_alloc(ctx);
223 if (WARN_ON_ONCE(!backup))
226 node->rsrc_data = data_to_kill;
227 list_add_tail(&node->node, &ctx->rsrc_ref_list);
229 io_put_rsrc_node(ctx, node);
230 ctx->rsrc_node = backup;
233 int __io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
235 struct io_rsrc_node *node = kzalloc(sizeof(*node), GFP_KERNEL);
239 io_alloc_cache_put(&ctx->rsrc_node_cache, &node->cache);
243 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
244 struct io_ring_ctx *ctx)
249 /* As we may drop ->uring_lock, other task may have started quiesce */
252 ret = io_rsrc_node_switch_start(ctx);
255 io_rsrc_node_switch(ctx, data);
257 if (list_empty(&ctx->rsrc_ref_list))
260 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
261 atomic_set(&ctx->cq_wait_nr, 1);
266 data->quiesce = true;
268 prepare_to_wait(&ctx->rsrc_quiesce_wq, &we, TASK_INTERRUPTIBLE);
269 mutex_unlock(&ctx->uring_lock);
271 ret = io_run_task_work_sig(ctx);
273 mutex_lock(&ctx->uring_lock);
274 if (list_empty(&ctx->rsrc_ref_list))
280 __set_current_state(TASK_RUNNING);
281 mutex_lock(&ctx->uring_lock);
283 } while (!list_empty(&ctx->rsrc_ref_list));
285 finish_wait(&ctx->rsrc_quiesce_wq, &we);
286 data->quiesce = false;
289 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
290 atomic_set(&ctx->cq_wait_nr, 0);
296 static void io_free_page_table(void **table, size_t size)
298 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
300 for (i = 0; i < nr_tables; i++)
305 static void io_rsrc_data_free(struct io_rsrc_data *data)
307 size_t size = data->nr * sizeof(data->tags[0][0]);
310 io_free_page_table((void **)data->tags, size);
314 static __cold void **io_alloc_page_table(size_t size)
316 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
317 size_t init_size = size;
320 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
324 for (i = 0; i < nr_tables; i++) {
325 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
327 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
329 io_free_page_table(table, init_size);
337 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
338 rsrc_put_fn *do_put, u64 __user *utags,
339 unsigned nr, struct io_rsrc_data **pdata)
341 struct io_rsrc_data *data;
345 data = kzalloc(sizeof(*data), GFP_KERNEL);
348 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
356 data->do_put = do_put;
359 for (i = 0; i < nr; i++) {
360 u64 *tag_slot = io_get_tag_slot(data, i);
362 if (copy_from_user(tag_slot, &utags[i],
370 io_rsrc_data_free(data);
374 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
375 struct io_uring_rsrc_update2 *up,
378 u64 __user *tags = u64_to_user_ptr(up->tags);
379 __s32 __user *fds = u64_to_user_ptr(up->data);
380 struct io_rsrc_data *data = ctx->file_data;
381 struct io_fixed_file *file_slot;
385 bool needs_switch = false;
389 if (up->offset + nr_args > ctx->nr_user_files)
392 for (done = 0; done < nr_args; done++) {
395 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
396 copy_from_user(&fd, &fds[done], sizeof(fd))) {
400 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
404 if (fd == IORING_REGISTER_FILES_SKIP)
407 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
408 file_slot = io_fixed_file_slot(&ctx->file_table, i);
410 if (file_slot->file_ptr) {
411 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
412 err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
415 file_slot->file_ptr = 0;
416 io_file_bitmap_clear(&ctx->file_table, i);
426 * Don't allow io_uring instances to be registered. If
427 * UNIX isn't enabled, then this causes a reference
428 * cycle and this instance can never get freed. If UNIX
429 * is enabled we'll handle it just fine, but there's
430 * still no point in allowing a ring fd as it doesn't
431 * support regular read/write anyway.
433 if (io_is_uring_fops(file)) {
438 err = io_scm_file_account(ctx, file);
443 *io_get_tag_slot(data, i) = tag;
444 io_fixed_file_set(file_slot, file);
445 io_file_bitmap_set(&ctx->file_table, i);
450 io_rsrc_node_switch(ctx, data);
451 return done ? done : err;
454 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
455 struct io_uring_rsrc_update2 *up,
456 unsigned int nr_args)
458 u64 __user *tags = u64_to_user_ptr(up->tags);
459 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
460 struct page *last_hpage = NULL;
461 bool needs_switch = false;
467 if (up->offset + nr_args > ctx->nr_user_bufs)
470 for (done = 0; done < nr_args; done++) {
471 struct io_mapped_ubuf *imu;
472 int offset = up->offset + done;
475 err = io_copy_iov(ctx, &iov, iovs, done);
478 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
482 err = io_buffer_validate(&iov);
485 if (!iov.iov_base && tag) {
489 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
493 i = array_index_nospec(offset, ctx->nr_user_bufs);
494 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
495 err = io_queue_rsrc_removal(ctx->buf_data, i,
496 ctx->rsrc_node, ctx->user_bufs[i]);
498 io_buffer_unmap(ctx, &imu);
501 ctx->user_bufs[i] = ctx->dummy_ubuf;
505 ctx->user_bufs[i] = imu;
506 *io_get_tag_slot(ctx->buf_data, i) = tag;
510 io_rsrc_node_switch(ctx, ctx->buf_data);
511 return done ? done : err;
514 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
515 struct io_uring_rsrc_update2 *up,
521 lockdep_assert_held(&ctx->uring_lock);
523 if (check_add_overflow(up->offset, nr_args, &tmp))
525 err = io_rsrc_node_switch_start(ctx);
530 case IORING_RSRC_FILE:
531 return __io_sqe_files_update(ctx, up, nr_args);
532 case IORING_RSRC_BUFFER:
533 return __io_sqe_buffers_update(ctx, up, nr_args);
538 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
541 struct io_uring_rsrc_update2 up;
545 memset(&up, 0, sizeof(up));
546 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
548 if (up.resv || up.resv2)
550 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
553 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
554 unsigned size, unsigned type)
556 struct io_uring_rsrc_update2 up;
558 if (size != sizeof(up))
560 if (copy_from_user(&up, arg, sizeof(up)))
562 if (!up.nr || up.resv || up.resv2)
564 return __io_register_rsrc_update(ctx, type, &up, up.nr);
567 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
568 unsigned int size, unsigned int type)
570 struct io_uring_rsrc_register rr;
572 /* keep it extendible */
573 if (size != sizeof(rr))
576 memset(&rr, 0, sizeof(rr));
577 if (copy_from_user(&rr, arg, size))
579 if (!rr.nr || rr.resv2)
581 if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
585 case IORING_RSRC_FILE:
586 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
588 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
589 rr.nr, u64_to_user_ptr(rr.tags));
590 case IORING_RSRC_BUFFER:
591 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
593 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
594 rr.nr, u64_to_user_ptr(rr.tags));
599 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
601 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
603 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
605 if (sqe->rw_flags || sqe->splice_fd_in)
608 up->offset = READ_ONCE(sqe->off);
609 up->nr_args = READ_ONCE(sqe->len);
612 up->arg = READ_ONCE(sqe->addr);
616 static int io_files_update_with_index_alloc(struct io_kiocb *req,
617 unsigned int issue_flags)
619 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
620 __s32 __user *fds = u64_to_user_ptr(up->arg);
625 if (!req->ctx->file_data)
628 for (done = 0; done < up->nr_args; done++) {
629 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
639 ret = io_fixed_fd_install(req, issue_flags, file,
640 IORING_FILE_INDEX_ALLOC);
643 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
644 __io_close_fixed(req->ctx, issue_flags, ret);
655 int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
657 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
658 struct io_ring_ctx *ctx = req->ctx;
659 struct io_uring_rsrc_update2 up2;
662 up2.offset = up->offset;
669 if (up->offset == IORING_FILE_INDEX_ALLOC) {
670 ret = io_files_update_with_index_alloc(req, issue_flags);
672 io_ring_submit_lock(ctx, issue_flags);
673 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
675 io_ring_submit_unlock(ctx, issue_flags);
680 io_req_set_res(req, ret, 0);
684 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
685 struct io_rsrc_node *node, void *rsrc)
687 u64 *tag_slot = io_get_tag_slot(data, idx);
688 struct io_rsrc_put *prsrc;
689 bool inline_item = true;
691 if (!node->inline_items) {
693 node->inline_items++;
695 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
701 prsrc->tag = *tag_slot;
705 list_add(&prsrc->list, &node->item_list);
709 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
713 for (i = 0; i < ctx->nr_user_files; i++) {
714 struct file *file = io_file_from_index(&ctx->file_table, i);
716 /* skip scm accounted files, they'll be freed by ->ring_sock */
717 if (!file || io_file_need_scm(file))
719 io_file_bitmap_clear(&ctx->file_table, i);
723 #if defined(CONFIG_UNIX)
724 if (ctx->ring_sock) {
725 struct sock *sock = ctx->ring_sock->sk;
728 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
732 io_free_file_tables(&ctx->file_table);
733 io_file_table_set_alloc_range(ctx, 0, 0);
734 io_rsrc_data_free(ctx->file_data);
735 ctx->file_data = NULL;
736 ctx->nr_user_files = 0;
739 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
741 unsigned nr = ctx->nr_user_files;
748 * Quiesce may unlock ->uring_lock, and while it's not held
749 * prevent new requests using the table.
751 ctx->nr_user_files = 0;
752 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
753 ctx->nr_user_files = nr;
755 __io_sqe_files_unregister(ctx);
760 * Ensure the UNIX gc is aware of our file set, so we are certain that
761 * the io_uring can be safely unregistered on process exit, even if we have
762 * loops in the file referencing. We account only files that can hold other
763 * files because otherwise they can't form a loop and so are not interesting
766 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
768 #if defined(CONFIG_UNIX)
769 struct sock *sk = ctx->ring_sock->sk;
770 struct sk_buff_head *head = &sk->sk_receive_queue;
771 struct scm_fp_list *fpl;
774 if (likely(!io_file_need_scm(file)))
778 * See if we can merge this file into an existing skb SCM_RIGHTS
779 * file set. If there's no room, fall back to allocating a new skb
782 spin_lock_irq(&head->lock);
783 skb = skb_peek(head);
784 if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
785 __skb_unlink(skb, head);
788 spin_unlock_irq(&head->lock);
791 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
795 skb = alloc_skb(0, GFP_KERNEL);
801 fpl->user = get_uid(current_user());
802 fpl->max = SCM_MAX_FD;
805 UNIXCB(skb).fp = fpl;
807 skb->scm_io_uring = 1;
808 skb->destructor = unix_destruct_scm;
809 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
812 fpl = UNIXCB(skb).fp;
813 fpl->fp[fpl->count++] = get_file(file);
814 unix_inflight(fpl->user, file);
815 skb_queue_head(head, skb);
821 static __cold void io_rsrc_file_scm_put(struct io_ring_ctx *ctx, struct file *file)
823 #if defined(CONFIG_UNIX)
824 struct sock *sock = ctx->ring_sock->sk;
825 struct sk_buff_head list, *head = &sock->sk_receive_queue;
829 __skb_queue_head_init(&list);
832 * Find the skb that holds this file in its SCM_RIGHTS. When found,
833 * remove this entry and rearrange the file array.
835 skb = skb_dequeue(head);
837 struct scm_fp_list *fp;
840 for (i = 0; i < fp->count; i++) {
843 if (fp->fp[i] != file)
846 unix_notinflight(fp->user, fp->fp[i]);
847 left = fp->count - 1 - i;
849 memmove(&fp->fp[i], &fp->fp[i + 1],
850 left * sizeof(struct file *));
857 __skb_queue_tail(&list, skb);
867 __skb_queue_tail(&list, skb);
869 skb = skb_dequeue(head);
872 if (skb_peek(&list)) {
873 spin_lock_irq(&head->lock);
874 while ((skb = __skb_dequeue(&list)) != NULL)
875 __skb_queue_tail(head, skb);
876 spin_unlock_irq(&head->lock);
881 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
883 struct file *file = prsrc->file;
885 if (likely(!io_file_need_scm(file)))
888 io_rsrc_file_scm_put(ctx, file);
891 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
892 unsigned nr_args, u64 __user *tags)
894 __s32 __user *fds = (__s32 __user *) arg;
903 if (nr_args > IORING_MAX_FIXED_FILES)
905 if (nr_args > rlimit(RLIMIT_NOFILE))
907 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
912 if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
913 io_rsrc_data_free(ctx->file_data);
914 ctx->file_data = NULL;
918 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
919 struct io_fixed_file *file_slot;
921 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
925 /* allow sparse sets */
926 if (!fds || fd == -1) {
928 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
939 * Don't allow io_uring instances to be registered. If UNIX
940 * isn't enabled, then this causes a reference cycle and this
941 * instance can never get freed. If UNIX is enabled we'll
942 * handle it just fine, but there's still no point in allowing
943 * a ring fd as it doesn't support regular read/write anyway.
945 if (io_is_uring_fops(file)) {
949 ret = io_scm_file_account(ctx, file);
954 file_slot = io_fixed_file_slot(&ctx->file_table, i);
955 io_fixed_file_set(file_slot, file);
956 io_file_bitmap_set(&ctx->file_table, i);
959 /* default it to the whole table */
960 io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
963 __io_sqe_files_unregister(ctx);
967 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
969 io_buffer_unmap(ctx, &prsrc->buf);
973 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
977 for (i = 0; i < ctx->nr_user_bufs; i++)
978 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
979 kfree(ctx->user_bufs);
980 io_rsrc_data_free(ctx->buf_data);
981 ctx->user_bufs = NULL;
982 ctx->buf_data = NULL;
983 ctx->nr_user_bufs = 0;
986 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
988 unsigned nr = ctx->nr_user_bufs;
995 * Quiesce may unlock ->uring_lock, and while it's not held
996 * prevent new requests using the table.
998 ctx->nr_user_bufs = 0;
999 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
1000 ctx->nr_user_bufs = nr;
1002 __io_sqe_buffers_unregister(ctx);
1007 * Not super efficient, but this is just a registration time. And we do cache
1008 * the last compound head, so generally we'll only do a full search if we don't
1011 * We check if the given compound head page has already been accounted, to
1012 * avoid double accounting it. This allows us to account the full size of the
1013 * page, not just the constituent pages of a huge page.
1015 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
1016 int nr_pages, struct page *hpage)
1020 /* check current page array */
1021 for (i = 0; i < nr_pages; i++) {
1022 if (!PageCompound(pages[i]))
1024 if (compound_head(pages[i]) == hpage)
1028 /* check previously registered pages */
1029 for (i = 0; i < ctx->nr_user_bufs; i++) {
1030 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
1032 for (j = 0; j < imu->nr_bvecs; j++) {
1033 if (!PageCompound(imu->bvec[j].bv_page))
1035 if (compound_head(imu->bvec[j].bv_page) == hpage)
1043 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1044 int nr_pages, struct io_mapped_ubuf *imu,
1045 struct page **last_hpage)
1049 imu->acct_pages = 0;
1050 for (i = 0; i < nr_pages; i++) {
1051 if (!PageCompound(pages[i])) {
1056 hpage = compound_head(pages[i]);
1057 if (hpage == *last_hpage)
1059 *last_hpage = hpage;
1060 if (headpage_already_acct(ctx, pages, i, hpage))
1062 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1066 if (!imu->acct_pages)
1069 ret = io_account_mem(ctx, imu->acct_pages);
1071 imu->acct_pages = 0;
1075 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1077 unsigned long start, end, nr_pages;
1078 struct vm_area_struct **vmas = NULL;
1079 struct page **pages = NULL;
1080 int i, pret, ret = -ENOMEM;
1082 end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1083 start = ubuf >> PAGE_SHIFT;
1084 nr_pages = end - start;
1086 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1090 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
1096 mmap_read_lock(current->mm);
1097 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1099 if (pret == nr_pages) {
1100 struct file *file = vmas[0]->vm_file;
1102 /* don't support file backed memory */
1103 for (i = 0; i < nr_pages; i++) {
1104 if (vmas[i]->vm_file != file) {
1110 if (!vma_is_shmem(vmas[i]) && !is_file_hugepages(file)) {
1117 ret = pret < 0 ? pret : -EFAULT;
1119 mmap_read_unlock(current->mm);
1122 * if we did partial map, or found file backed vmas,
1123 * release any pages we did get
1126 unpin_user_pages(pages, pret);
1134 pages = ERR_PTR(ret);
1139 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1140 struct io_mapped_ubuf **pimu,
1141 struct page **last_hpage)
1143 struct io_mapped_ubuf *imu = NULL;
1144 struct page **pages = NULL;
1147 int ret, nr_pages, i;
1148 struct folio *folio = NULL;
1150 *pimu = ctx->dummy_ubuf;
1155 pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1157 if (IS_ERR(pages)) {
1158 ret = PTR_ERR(pages);
1163 /* If it's a huge page, try to coalesce them into a single bvec entry */
1165 folio = page_folio(pages[0]);
1166 for (i = 1; i < nr_pages; i++) {
1167 if (page_folio(pages[i]) != folio) {
1174 * The pages are bound to the folio, it doesn't
1175 * actually unpin them but drops all but one reference,
1176 * which is usually put down by io_buffer_unmap().
1177 * Note, needs a better helper.
1179 unpin_user_pages(&pages[1], nr_pages - 1);
1184 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1188 ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1190 unpin_user_pages(pages, nr_pages);
1194 off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1195 size = iov->iov_len;
1196 /* store original address for later verification */
1197 imu->ubuf = (unsigned long) iov->iov_base;
1198 imu->ubuf_end = imu->ubuf + iov->iov_len;
1199 imu->nr_bvecs = nr_pages;
1204 bvec_set_page(&imu->bvec[0], pages[0], size, off);
1207 for (i = 0; i < nr_pages; i++) {
1210 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1211 bvec_set_page(&imu->bvec[i], pages[i], vec_len, off);
1222 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1224 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1225 return ctx->user_bufs ? 0 : -ENOMEM;
1228 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1229 unsigned int nr_args, u64 __user *tags)
1231 struct page *last_hpage = NULL;
1232 struct io_rsrc_data *data;
1236 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1240 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1242 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
1245 ret = io_buffers_map_alloc(ctx, nr_args);
1247 io_rsrc_data_free(data);
1251 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1253 ret = io_copy_iov(ctx, &iov, arg, i);
1256 ret = io_buffer_validate(&iov);
1260 memset(&iov, 0, sizeof(iov));
1263 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1268 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1274 WARN_ON_ONCE(ctx->buf_data);
1276 ctx->buf_data = data;
1278 __io_sqe_buffers_unregister(ctx);
1282 int io_import_fixed(int ddir, struct iov_iter *iter,
1283 struct io_mapped_ubuf *imu,
1284 u64 buf_addr, size_t len)
1289 if (WARN_ON_ONCE(!imu))
1291 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1293 /* not inside the mapped region */
1294 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1298 * Might not be a start of buffer, set size appropriately
1299 * and advance us to the beginning.
1301 offset = buf_addr - imu->ubuf;
1302 iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1306 * Don't use iov_iter_advance() here, as it's really slow for
1307 * using the latter parts of a big fixed buffer - it iterates
1308 * over each segment manually. We can cheat a bit here, because
1311 * 1) it's a BVEC iter, we set it up
1312 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1313 * first and last bvec
1315 * So just find our index, and adjust the iterator afterwards.
1316 * If the offset is within the first bvec (or the whole first
1317 * bvec, just use iov_iter_advance(). This makes it easier
1318 * since we can just skip the first segment, which may not
1319 * be PAGE_SIZE aligned.
1321 const struct bio_vec *bvec = imu->bvec;
1323 if (offset <= bvec->bv_len) {
1325 * Note, huge pages buffers consists of one large
1326 * bvec entry and should always go this way. The other
1327 * branch doesn't expect non PAGE_SIZE'd chunks.
1330 iter->nr_segs = bvec->bv_len;
1331 iter->count -= offset;
1332 iter->iov_offset = offset;
1334 unsigned long seg_skip;
1336 /* skip first vec */
1337 offset -= bvec->bv_len;
1338 seg_skip = 1 + (offset >> PAGE_SHIFT);
1340 iter->bvec = bvec + seg_skip;
1341 iter->nr_segs -= seg_skip;
1342 iter->count -= bvec->bv_len + offset;
1343 iter->iov_offset = offset & ~PAGE_MASK;