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 void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
27 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
28 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
29 struct io_mapped_ubuf **pimu,
30 struct page **last_hpage);
33 #define IORING_MAX_FIXED_FILES (1U << 20)
34 #define IORING_MAX_REG_BUFFERS (1U << 14)
36 static const struct io_mapped_ubuf dummy_ubuf = {
37 /* set invalid range, so io_import_fixed() fails meeting it */
42 int __io_account_mem(struct user_struct *user, unsigned long nr_pages)
44 unsigned long page_limit, cur_pages, new_pages;
49 /* Don't allow more pages than we can safely lock */
50 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
52 cur_pages = atomic_long_read(&user->locked_vm);
54 new_pages = cur_pages + nr_pages;
55 if (new_pages > page_limit)
57 } while (!atomic_long_try_cmpxchg(&user->locked_vm,
58 &cur_pages, new_pages));
62 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
65 __io_unaccount_mem(ctx->user, nr_pages);
68 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
71 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
76 ret = __io_account_mem(ctx->user, nr_pages);
82 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
87 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
88 void __user *arg, unsigned index)
90 struct iovec __user *src;
94 struct compat_iovec __user *ciovs;
95 struct compat_iovec ciov;
97 ciovs = (struct compat_iovec __user *) arg;
98 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
101 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
102 dst->iov_len = ciov.iov_len;
106 src = (struct iovec __user *) arg;
107 if (copy_from_user(dst, &src[index], sizeof(*dst)))
112 static int io_buffer_validate(struct iovec *iov)
114 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
117 * Don't impose further limits on the size and buffer
118 * constraints here, we'll -EINVAL later when IO is
119 * submitted if they are wrong.
122 return iov->iov_len ? -EFAULT : 0;
126 /* arbitrary limit, but we need something */
127 if (iov->iov_len > SZ_1G)
130 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
136 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
138 struct io_mapped_ubuf *imu = *slot;
141 if (imu != &dummy_ubuf) {
142 for (i = 0; i < imu->nr_bvecs; i++)
143 unpin_user_page(imu->bvec[i].bv_page);
145 io_unaccount_mem(ctx, imu->acct_pages);
151 static void io_rsrc_put_work(struct io_rsrc_node *node)
153 struct io_rsrc_put *prsrc = &node->item;
156 io_post_aux_cqe(node->ctx, prsrc->tag, 0, 0);
158 switch (node->type) {
159 case IORING_RSRC_FILE:
160 io_rsrc_file_put(node->ctx, prsrc);
162 case IORING_RSRC_BUFFER:
163 io_rsrc_buf_put(node->ctx, prsrc);
171 void io_rsrc_node_destroy(struct io_ring_ctx *ctx, struct io_rsrc_node *node)
173 if (!io_alloc_cache_put(&ctx->rsrc_node_cache, &node->cache))
177 void io_rsrc_node_ref_zero(struct io_rsrc_node *node)
178 __must_hold(&node->ctx->uring_lock)
180 struct io_ring_ctx *ctx = node->ctx;
182 while (!list_empty(&ctx->rsrc_ref_list)) {
183 node = list_first_entry(&ctx->rsrc_ref_list,
184 struct io_rsrc_node, node);
185 /* recycle ref nodes in order */
188 list_del(&node->node);
190 if (likely(!node->empty))
191 io_rsrc_put_work(node);
192 io_rsrc_node_destroy(ctx, node);
194 if (list_empty(&ctx->rsrc_ref_list) && unlikely(ctx->rsrc_quiesce))
195 wake_up_all(&ctx->rsrc_quiesce_wq);
198 struct io_rsrc_node *io_rsrc_node_alloc(struct io_ring_ctx *ctx)
200 struct io_rsrc_node *ref_node;
201 struct io_cache_entry *entry;
203 entry = io_alloc_cache_get(&ctx->rsrc_node_cache);
205 ref_node = container_of(entry, struct io_rsrc_node, cache);
207 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
218 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
219 struct io_ring_ctx *ctx)
221 struct io_rsrc_node *backup;
225 /* As We may drop ->uring_lock, other task may have started quiesce */
229 backup = io_rsrc_node_alloc(ctx);
232 ctx->rsrc_node->empty = true;
233 ctx->rsrc_node->type = -1;
234 list_add_tail(&ctx->rsrc_node->node, &ctx->rsrc_ref_list);
235 io_put_rsrc_node(ctx, ctx->rsrc_node);
236 ctx->rsrc_node = backup;
238 if (list_empty(&ctx->rsrc_ref_list))
241 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
242 atomic_set(&ctx->cq_wait_nr, 1);
247 data->quiesce = true;
249 prepare_to_wait(&ctx->rsrc_quiesce_wq, &we, TASK_INTERRUPTIBLE);
250 mutex_unlock(&ctx->uring_lock);
252 ret = io_run_task_work_sig(ctx);
254 mutex_lock(&ctx->uring_lock);
255 if (list_empty(&ctx->rsrc_ref_list))
261 __set_current_state(TASK_RUNNING);
262 mutex_lock(&ctx->uring_lock);
264 } while (!list_empty(&ctx->rsrc_ref_list));
266 finish_wait(&ctx->rsrc_quiesce_wq, &we);
267 data->quiesce = false;
270 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) {
271 atomic_set(&ctx->cq_wait_nr, 0);
277 static void io_free_page_table(void **table, size_t size)
279 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
281 for (i = 0; i < nr_tables; i++)
286 static void io_rsrc_data_free(struct io_rsrc_data *data)
288 size_t size = data->nr * sizeof(data->tags[0][0]);
291 io_free_page_table((void **)data->tags, size);
295 static __cold void **io_alloc_page_table(size_t size)
297 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
298 size_t init_size = size;
301 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
305 for (i = 0; i < nr_tables; i++) {
306 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
308 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
310 io_free_page_table(table, init_size);
318 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx, int type,
320 unsigned nr, struct io_rsrc_data **pdata)
322 struct io_rsrc_data *data;
326 data = kzalloc(sizeof(*data), GFP_KERNEL);
329 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
337 data->rsrc_type = type;
340 for (i = 0; i < nr; i++) {
341 u64 *tag_slot = io_get_tag_slot(data, i);
343 if (copy_from_user(tag_slot, &utags[i],
351 io_rsrc_data_free(data);
355 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
356 struct io_uring_rsrc_update2 *up,
359 u64 __user *tags = u64_to_user_ptr(up->tags);
360 __s32 __user *fds = u64_to_user_ptr(up->data);
361 struct io_rsrc_data *data = ctx->file_data;
362 struct io_fixed_file *file_slot;
368 if (up->offset + nr_args > ctx->nr_user_files)
371 for (done = 0; done < nr_args; done++) {
374 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
375 copy_from_user(&fd, &fds[done], sizeof(fd))) {
379 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
383 if (fd == IORING_REGISTER_FILES_SKIP)
386 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
387 file_slot = io_fixed_file_slot(&ctx->file_table, i);
389 if (file_slot->file_ptr) {
390 err = io_queue_rsrc_removal(data, i,
391 io_slot_file(file_slot));
394 file_slot->file_ptr = 0;
395 io_file_bitmap_clear(&ctx->file_table, i);
398 struct file *file = fget(fd);
405 * Don't allow io_uring instances to be registered. If
406 * UNIX isn't enabled, then this causes a reference
407 * cycle and this instance can never get freed. If UNIX
408 * is enabled we'll handle it just fine, but there's
409 * still no point in allowing a ring fd as it doesn't
410 * support regular read/write anyway.
412 if (io_is_uring_fops(file)) {
417 err = io_scm_file_account(ctx, file);
422 *io_get_tag_slot(data, i) = tag;
423 io_fixed_file_set(file_slot, file);
424 io_file_bitmap_set(&ctx->file_table, i);
427 return done ? done : err;
430 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
431 struct io_uring_rsrc_update2 *up,
432 unsigned int nr_args)
434 u64 __user *tags = u64_to_user_ptr(up->tags);
435 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
436 struct page *last_hpage = NULL;
442 if (up->offset + nr_args > ctx->nr_user_bufs)
445 for (done = 0; done < nr_args; done++) {
446 struct io_mapped_ubuf *imu;
449 err = io_copy_iov(ctx, &iov, iovs, done);
452 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
456 err = io_buffer_validate(&iov);
459 if (!iov.iov_base && tag) {
463 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
467 i = array_index_nospec(up->offset + done, ctx->nr_user_bufs);
468 if (ctx->user_bufs[i] != &dummy_ubuf) {
469 err = io_queue_rsrc_removal(ctx->buf_data, i,
472 io_buffer_unmap(ctx, &imu);
475 ctx->user_bufs[i] = (struct io_mapped_ubuf *)&dummy_ubuf;
478 ctx->user_bufs[i] = imu;
479 *io_get_tag_slot(ctx->buf_data, i) = tag;
481 return done ? done : err;
484 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
485 struct io_uring_rsrc_update2 *up,
490 lockdep_assert_held(&ctx->uring_lock);
492 if (check_add_overflow(up->offset, nr_args, &tmp))
496 case IORING_RSRC_FILE:
497 return __io_sqe_files_update(ctx, up, nr_args);
498 case IORING_RSRC_BUFFER:
499 return __io_sqe_buffers_update(ctx, up, nr_args);
504 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
507 struct io_uring_rsrc_update2 up;
511 memset(&up, 0, sizeof(up));
512 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
514 if (up.resv || up.resv2)
516 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
519 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
520 unsigned size, unsigned type)
522 struct io_uring_rsrc_update2 up;
524 if (size != sizeof(up))
526 if (copy_from_user(&up, arg, sizeof(up)))
528 if (!up.nr || up.resv || up.resv2)
530 return __io_register_rsrc_update(ctx, type, &up, up.nr);
533 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
534 unsigned int size, unsigned int type)
536 struct io_uring_rsrc_register rr;
538 /* keep it extendible */
539 if (size != sizeof(rr))
542 memset(&rr, 0, sizeof(rr));
543 if (copy_from_user(&rr, arg, size))
545 if (!rr.nr || rr.resv2)
547 if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
551 case IORING_RSRC_FILE:
552 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
554 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
555 rr.nr, u64_to_user_ptr(rr.tags));
556 case IORING_RSRC_BUFFER:
557 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
559 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
560 rr.nr, u64_to_user_ptr(rr.tags));
565 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
567 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
569 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
571 if (sqe->rw_flags || sqe->splice_fd_in)
574 up->offset = READ_ONCE(sqe->off);
575 up->nr_args = READ_ONCE(sqe->len);
578 up->arg = READ_ONCE(sqe->addr);
582 static int io_files_update_with_index_alloc(struct io_kiocb *req,
583 unsigned int issue_flags)
585 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
586 __s32 __user *fds = u64_to_user_ptr(up->arg);
591 if (!req->ctx->file_data)
594 for (done = 0; done < up->nr_args; done++) {
595 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
605 ret = io_fixed_fd_install(req, issue_flags, file,
606 IORING_FILE_INDEX_ALLOC);
609 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
610 __io_close_fixed(req->ctx, issue_flags, ret);
621 int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
623 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
624 struct io_ring_ctx *ctx = req->ctx;
625 struct io_uring_rsrc_update2 up2;
628 up2.offset = up->offset;
635 if (up->offset == IORING_FILE_INDEX_ALLOC) {
636 ret = io_files_update_with_index_alloc(req, issue_flags);
638 io_ring_submit_lock(ctx, issue_flags);
639 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
641 io_ring_submit_unlock(ctx, issue_flags);
646 io_req_set_res(req, ret, 0);
650 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx, void *rsrc)
652 struct io_ring_ctx *ctx = data->ctx;
653 struct io_rsrc_node *node = ctx->rsrc_node;
654 u64 *tag_slot = io_get_tag_slot(data, idx);
656 ctx->rsrc_node = io_rsrc_node_alloc(ctx);
657 if (unlikely(!ctx->rsrc_node)) {
658 ctx->rsrc_node = node;
662 node->item.rsrc = rsrc;
663 node->type = data->rsrc_type;
664 node->item.tag = *tag_slot;
666 list_add_tail(&node->node, &ctx->rsrc_ref_list);
667 io_put_rsrc_node(ctx, node);
671 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
675 for (i = 0; i < ctx->nr_user_files; i++) {
676 struct file *file = io_file_from_index(&ctx->file_table, i);
678 /* skip scm accounted files, they'll be freed by ->ring_sock */
679 if (!file || io_file_need_scm(file))
681 io_file_bitmap_clear(&ctx->file_table, i);
685 #if defined(CONFIG_UNIX)
686 if (ctx->ring_sock) {
687 struct sock *sock = ctx->ring_sock->sk;
690 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
694 io_free_file_tables(&ctx->file_table);
695 io_file_table_set_alloc_range(ctx, 0, 0);
696 io_rsrc_data_free(ctx->file_data);
697 ctx->file_data = NULL;
698 ctx->nr_user_files = 0;
701 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
703 unsigned nr = ctx->nr_user_files;
710 * Quiesce may unlock ->uring_lock, and while it's not held
711 * prevent new requests using the table.
713 ctx->nr_user_files = 0;
714 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
715 ctx->nr_user_files = nr;
717 __io_sqe_files_unregister(ctx);
722 * Ensure the UNIX gc is aware of our file set, so we are certain that
723 * the io_uring can be safely unregistered on process exit, even if we have
724 * loops in the file referencing. We account only files that can hold other
725 * files because otherwise they can't form a loop and so are not interesting
728 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
730 #if defined(CONFIG_UNIX)
731 struct sock *sk = ctx->ring_sock->sk;
732 struct sk_buff_head *head = &sk->sk_receive_queue;
733 struct scm_fp_list *fpl;
736 if (likely(!io_file_need_scm(file)))
740 * See if we can merge this file into an existing skb SCM_RIGHTS
741 * file set. If there's no room, fall back to allocating a new skb
744 spin_lock_irq(&head->lock);
745 skb = skb_peek(head);
746 if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
747 __skb_unlink(skb, head);
750 spin_unlock_irq(&head->lock);
753 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
757 skb = alloc_skb(0, GFP_KERNEL);
763 fpl->user = get_uid(current_user());
764 fpl->max = SCM_MAX_FD;
767 UNIXCB(skb).fp = fpl;
769 skb->destructor = io_uring_destruct_scm;
770 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
773 fpl = UNIXCB(skb).fp;
774 fpl->fp[fpl->count++] = get_file(file);
775 unix_inflight(fpl->user, file);
776 skb_queue_head(head, skb);
782 static __cold void io_rsrc_file_scm_put(struct io_ring_ctx *ctx, struct file *file)
784 #if defined(CONFIG_UNIX)
785 struct sock *sock = ctx->ring_sock->sk;
786 struct sk_buff_head list, *head = &sock->sk_receive_queue;
790 __skb_queue_head_init(&list);
793 * Find the skb that holds this file in its SCM_RIGHTS. When found,
794 * remove this entry and rearrange the file array.
796 skb = skb_dequeue(head);
798 struct scm_fp_list *fp;
801 for (i = 0; i < fp->count; i++) {
804 if (fp->fp[i] != file)
807 unix_notinflight(fp->user, fp->fp[i]);
808 left = fp->count - 1 - i;
810 memmove(&fp->fp[i], &fp->fp[i + 1],
811 left * sizeof(struct file *));
818 __skb_queue_tail(&list, skb);
828 __skb_queue_tail(&list, skb);
830 skb = skb_dequeue(head);
833 if (skb_peek(&list)) {
834 spin_lock_irq(&head->lock);
835 while ((skb = __skb_dequeue(&list)) != NULL)
836 __skb_queue_tail(head, skb);
837 spin_unlock_irq(&head->lock);
842 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
844 struct file *file = prsrc->file;
846 if (likely(!io_file_need_scm(file)))
849 io_rsrc_file_scm_put(ctx, file);
852 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
853 unsigned nr_args, u64 __user *tags)
855 __s32 __user *fds = (__s32 __user *) arg;
864 if (nr_args > IORING_MAX_FIXED_FILES)
866 if (nr_args > rlimit(RLIMIT_NOFILE))
868 ret = io_rsrc_data_alloc(ctx, IORING_RSRC_FILE, tags, nr_args,
873 if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
874 io_rsrc_data_free(ctx->file_data);
875 ctx->file_data = NULL;
879 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
880 struct io_fixed_file *file_slot;
882 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
886 /* allow sparse sets */
887 if (!fds || fd == -1) {
889 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
900 * Don't allow io_uring instances to be registered. If UNIX
901 * isn't enabled, then this causes a reference cycle and this
902 * instance can never get freed. If UNIX is enabled we'll
903 * handle it just fine, but there's still no point in allowing
904 * a ring fd as it doesn't support regular read/write anyway.
906 if (io_is_uring_fops(file)) {
910 ret = io_scm_file_account(ctx, file);
915 file_slot = io_fixed_file_slot(&ctx->file_table, i);
916 io_fixed_file_set(file_slot, file);
917 io_file_bitmap_set(&ctx->file_table, i);
920 /* default it to the whole table */
921 io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
924 __io_sqe_files_unregister(ctx);
928 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
930 io_buffer_unmap(ctx, &prsrc->buf);
934 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
938 for (i = 0; i < ctx->nr_user_bufs; i++)
939 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
940 kfree(ctx->user_bufs);
941 io_rsrc_data_free(ctx->buf_data);
942 ctx->user_bufs = NULL;
943 ctx->buf_data = NULL;
944 ctx->nr_user_bufs = 0;
947 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
949 unsigned nr = ctx->nr_user_bufs;
956 * Quiesce may unlock ->uring_lock, and while it's not held
957 * prevent new requests using the table.
959 ctx->nr_user_bufs = 0;
960 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
961 ctx->nr_user_bufs = nr;
963 __io_sqe_buffers_unregister(ctx);
968 * Not super efficient, but this is just a registration time. And we do cache
969 * the last compound head, so generally we'll only do a full search if we don't
972 * We check if the given compound head page has already been accounted, to
973 * avoid double accounting it. This allows us to account the full size of the
974 * page, not just the constituent pages of a huge page.
976 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
977 int nr_pages, struct page *hpage)
981 /* check current page array */
982 for (i = 0; i < nr_pages; i++) {
983 if (!PageCompound(pages[i]))
985 if (compound_head(pages[i]) == hpage)
989 /* check previously registered pages */
990 for (i = 0; i < ctx->nr_user_bufs; i++) {
991 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
993 for (j = 0; j < imu->nr_bvecs; j++) {
994 if (!PageCompound(imu->bvec[j].bv_page))
996 if (compound_head(imu->bvec[j].bv_page) == hpage)
1004 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1005 int nr_pages, struct io_mapped_ubuf *imu,
1006 struct page **last_hpage)
1010 imu->acct_pages = 0;
1011 for (i = 0; i < nr_pages; i++) {
1012 if (!PageCompound(pages[i])) {
1017 hpage = compound_head(pages[i]);
1018 if (hpage == *last_hpage)
1020 *last_hpage = hpage;
1021 if (headpage_already_acct(ctx, pages, i, hpage))
1023 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1027 if (!imu->acct_pages)
1030 ret = io_account_mem(ctx, imu->acct_pages);
1032 imu->acct_pages = 0;
1036 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1038 unsigned long start, end, nr_pages;
1039 struct page **pages = NULL;
1040 int pret, ret = -ENOMEM;
1042 end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1043 start = ubuf >> PAGE_SHIFT;
1044 nr_pages = end - start;
1046 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1051 mmap_read_lock(current->mm);
1052 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1054 if (pret == nr_pages)
1057 ret = pret < 0 ? pret : -EFAULT;
1059 mmap_read_unlock(current->mm);
1061 /* if we did partial map, release any pages we did get */
1063 unpin_user_pages(pages, pret);
1070 pages = ERR_PTR(ret);
1075 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1076 struct io_mapped_ubuf **pimu,
1077 struct page **last_hpage)
1079 struct io_mapped_ubuf *imu = NULL;
1080 struct page **pages = NULL;
1083 int ret, nr_pages, i;
1084 struct folio *folio = NULL;
1086 *pimu = (struct io_mapped_ubuf *)&dummy_ubuf;
1091 pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1093 if (IS_ERR(pages)) {
1094 ret = PTR_ERR(pages);
1099 /* If it's a huge page, try to coalesce them into a single bvec entry */
1101 folio = page_folio(pages[0]);
1102 for (i = 1; i < nr_pages; i++) {
1104 * Pages must be consecutive and on the same folio for
1107 if (page_folio(pages[i]) != folio ||
1108 pages[i] != pages[i - 1] + 1) {
1115 * The pages are bound to the folio, it doesn't
1116 * actually unpin them but drops all but one reference,
1117 * which is usually put down by io_buffer_unmap().
1118 * Note, needs a better helper.
1120 unpin_user_pages(&pages[1], nr_pages - 1);
1125 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1129 ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1131 unpin_user_pages(pages, nr_pages);
1135 off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1136 size = iov->iov_len;
1137 /* store original address for later verification */
1138 imu->ubuf = (unsigned long) iov->iov_base;
1139 imu->ubuf_end = imu->ubuf + iov->iov_len;
1140 imu->nr_bvecs = nr_pages;
1145 bvec_set_page(&imu->bvec[0], pages[0], size, off);
1148 for (i = 0; i < nr_pages; i++) {
1151 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1152 bvec_set_page(&imu->bvec[i], pages[i], vec_len, off);
1163 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1165 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1166 return ctx->user_bufs ? 0 : -ENOMEM;
1169 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1170 unsigned int nr_args, u64 __user *tags)
1172 struct page *last_hpage = NULL;
1173 struct io_rsrc_data *data;
1177 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1181 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1183 ret = io_rsrc_data_alloc(ctx, IORING_RSRC_BUFFER, tags, nr_args, &data);
1186 ret = io_buffers_map_alloc(ctx, nr_args);
1188 io_rsrc_data_free(data);
1192 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1194 ret = io_copy_iov(ctx, &iov, arg, i);
1197 ret = io_buffer_validate(&iov);
1201 memset(&iov, 0, sizeof(iov));
1204 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1209 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1215 WARN_ON_ONCE(ctx->buf_data);
1217 ctx->buf_data = data;
1219 __io_sqe_buffers_unregister(ctx);
1223 int io_import_fixed(int ddir, struct iov_iter *iter,
1224 struct io_mapped_ubuf *imu,
1225 u64 buf_addr, size_t len)
1230 if (WARN_ON_ONCE(!imu))
1232 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1234 /* not inside the mapped region */
1235 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1239 * Might not be a start of buffer, set size appropriately
1240 * and advance us to the beginning.
1242 offset = buf_addr - imu->ubuf;
1243 iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1247 * Don't use iov_iter_advance() here, as it's really slow for
1248 * using the latter parts of a big fixed buffer - it iterates
1249 * over each segment manually. We can cheat a bit here, because
1252 * 1) it's a BVEC iter, we set it up
1253 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1254 * first and last bvec
1256 * So just find our index, and adjust the iterator afterwards.
1257 * If the offset is within the first bvec (or the whole first
1258 * bvec, just use iov_iter_advance(). This makes it easier
1259 * since we can just skip the first segment, which may not
1260 * be PAGE_SIZE aligned.
1262 const struct bio_vec *bvec = imu->bvec;
1264 if (offset < bvec->bv_len) {
1266 * Note, huge pages buffers consists of one large
1267 * bvec entry and should always go this way. The other
1268 * branch doesn't expect non PAGE_SIZE'd chunks.
1271 iter->nr_segs = bvec->bv_len;
1272 iter->count -= offset;
1273 iter->iov_offset = offset;
1275 unsigned long seg_skip;
1277 /* skip first vec */
1278 offset -= bvec->bv_len;
1279 seg_skip = 1 + (offset >> PAGE_SHIFT);
1281 iter->bvec = bvec + seg_skip;
1282 iter->nr_segs -= seg_skip;
1283 iter->count -= bvec->bv_len + offset;
1284 iter->iov_offset = offset & ~PAGE_MASK;