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, true);
174 mutex_unlock(&ctx->uring_lock);
176 io_post_aux_cqe(ctx, prsrc->tag, 0, 0, true);
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_wait_rsrc_data(struct io_rsrc_data *data)
209 if (data && !atomic_dec_and_test(&data->refs))
210 wait_for_completion(&data->done);
213 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
215 percpu_ref_exit(&ref_node->refs);
219 static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
221 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
222 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
224 bool first_add = false;
225 unsigned long delay = HZ;
227 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
230 /* if we are mid-quiesce then do not delay */
231 if (node->rsrc_data->quiesce)
234 while (!list_empty(&ctx->rsrc_ref_list)) {
235 node = list_first_entry(&ctx->rsrc_ref_list,
236 struct io_rsrc_node, node);
237 /* recycle ref nodes in order */
240 list_del(&node->node);
241 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
243 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
246 mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
249 static struct io_rsrc_node *io_rsrc_node_alloc(void)
251 struct io_rsrc_node *ref_node;
253 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
257 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
262 INIT_LIST_HEAD(&ref_node->node);
263 INIT_LIST_HEAD(&ref_node->rsrc_list);
264 ref_node->done = false;
268 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
269 struct io_rsrc_data *data_to_kill)
270 __must_hold(&ctx->uring_lock)
272 WARN_ON_ONCE(!ctx->rsrc_backup_node);
273 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
275 io_rsrc_refs_drop(ctx);
278 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
280 rsrc_node->rsrc_data = data_to_kill;
281 spin_lock_irq(&ctx->rsrc_ref_lock);
282 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
283 spin_unlock_irq(&ctx->rsrc_ref_lock);
285 atomic_inc(&data_to_kill->refs);
286 percpu_ref_kill(&rsrc_node->refs);
287 ctx->rsrc_node = NULL;
290 if (!ctx->rsrc_node) {
291 ctx->rsrc_node = ctx->rsrc_backup_node;
292 ctx->rsrc_backup_node = NULL;
296 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
298 if (ctx->rsrc_backup_node)
300 ctx->rsrc_backup_node = io_rsrc_node_alloc();
301 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
304 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
305 struct io_ring_ctx *ctx)
309 /* As we may drop ->uring_lock, other task may have started quiesce */
313 data->quiesce = true;
315 ret = io_rsrc_node_switch_start(ctx);
318 io_rsrc_node_switch(ctx, data);
320 /* kill initial ref, already quiesced if zero */
321 if (atomic_dec_and_test(&data->refs))
323 mutex_unlock(&ctx->uring_lock);
324 flush_delayed_work(&ctx->rsrc_put_work);
325 ret = wait_for_completion_interruptible(&data->done);
327 mutex_lock(&ctx->uring_lock);
328 if (atomic_read(&data->refs) > 0) {
330 * it has been revived by another thread while
333 mutex_unlock(&ctx->uring_lock);
339 atomic_inc(&data->refs);
340 /* wait for all works potentially completing data->done */
341 flush_delayed_work(&ctx->rsrc_put_work);
342 reinit_completion(&data->done);
344 ret = io_run_task_work_sig(ctx);
345 mutex_lock(&ctx->uring_lock);
347 data->quiesce = false;
352 static void io_free_page_table(void **table, size_t size)
354 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
356 for (i = 0; i < nr_tables; i++)
361 static void io_rsrc_data_free(struct io_rsrc_data *data)
363 size_t size = data->nr * sizeof(data->tags[0][0]);
366 io_free_page_table((void **)data->tags, size);
370 static __cold void **io_alloc_page_table(size_t size)
372 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
373 size_t init_size = size;
376 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
380 for (i = 0; i < nr_tables; i++) {
381 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
383 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
385 io_free_page_table(table, init_size);
393 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
394 rsrc_put_fn *do_put, u64 __user *utags,
395 unsigned nr, struct io_rsrc_data **pdata)
397 struct io_rsrc_data *data;
401 data = kzalloc(sizeof(*data), GFP_KERNEL);
404 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
412 data->do_put = do_put;
415 for (i = 0; i < nr; i++) {
416 u64 *tag_slot = io_get_tag_slot(data, i);
418 if (copy_from_user(tag_slot, &utags[i],
424 atomic_set(&data->refs, 1);
425 init_completion(&data->done);
429 io_rsrc_data_free(data);
433 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
434 struct io_uring_rsrc_update2 *up,
437 u64 __user *tags = u64_to_user_ptr(up->tags);
438 __s32 __user *fds = u64_to_user_ptr(up->data);
439 struct io_rsrc_data *data = ctx->file_data;
440 struct io_fixed_file *file_slot;
444 bool needs_switch = false;
448 if (up->offset + nr_args > ctx->nr_user_files)
451 for (done = 0; done < nr_args; done++) {
454 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
455 copy_from_user(&fd, &fds[done], sizeof(fd))) {
459 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
463 if (fd == IORING_REGISTER_FILES_SKIP)
466 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
467 file_slot = io_fixed_file_slot(&ctx->file_table, i);
469 if (file_slot->file_ptr) {
470 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
471 err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
474 file_slot->file_ptr = 0;
475 io_file_bitmap_clear(&ctx->file_table, i);
485 * Don't allow io_uring instances to be registered. If
486 * UNIX isn't enabled, then this causes a reference
487 * cycle and this instance can never get freed. If UNIX
488 * is enabled we'll handle it just fine, but there's
489 * still no point in allowing a ring fd as it doesn't
490 * support regular read/write anyway.
492 if (io_is_uring_fops(file)) {
497 err = io_scm_file_account(ctx, file);
502 *io_get_tag_slot(data, i) = tag;
503 io_fixed_file_set(file_slot, file);
504 io_file_bitmap_set(&ctx->file_table, i);
509 io_rsrc_node_switch(ctx, data);
510 return done ? done : err;
513 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
514 struct io_uring_rsrc_update2 *up,
515 unsigned int nr_args)
517 u64 __user *tags = u64_to_user_ptr(up->tags);
518 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
519 struct page *last_hpage = NULL;
520 bool needs_switch = false;
526 if (up->offset + nr_args > ctx->nr_user_bufs)
529 for (done = 0; done < nr_args; done++) {
530 struct io_mapped_ubuf *imu;
531 int offset = up->offset + done;
534 err = io_copy_iov(ctx, &iov, iovs, done);
537 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
541 err = io_buffer_validate(&iov);
544 if (!iov.iov_base && tag) {
548 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
552 i = array_index_nospec(offset, ctx->nr_user_bufs);
553 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
554 err = io_queue_rsrc_removal(ctx->buf_data, i,
555 ctx->rsrc_node, ctx->user_bufs[i]);
557 io_buffer_unmap(ctx, &imu);
560 ctx->user_bufs[i] = ctx->dummy_ubuf;
564 ctx->user_bufs[i] = imu;
565 *io_get_tag_slot(ctx->buf_data, i) = tag;
569 io_rsrc_node_switch(ctx, ctx->buf_data);
570 return done ? done : err;
573 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
574 struct io_uring_rsrc_update2 *up,
580 if (check_add_overflow(up->offset, nr_args, &tmp))
582 err = io_rsrc_node_switch_start(ctx);
587 case IORING_RSRC_FILE:
588 return __io_sqe_files_update(ctx, up, nr_args);
589 case IORING_RSRC_BUFFER:
590 return __io_sqe_buffers_update(ctx, up, nr_args);
595 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
598 struct io_uring_rsrc_update2 up;
602 memset(&up, 0, sizeof(up));
603 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
605 if (up.resv || up.resv2)
607 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
610 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
611 unsigned size, unsigned type)
613 struct io_uring_rsrc_update2 up;
615 if (size != sizeof(up))
617 if (copy_from_user(&up, arg, sizeof(up)))
619 if (!up.nr || up.resv || up.resv2)
621 return __io_register_rsrc_update(ctx, type, &up, up.nr);
624 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
625 unsigned int size, unsigned int type)
627 struct io_uring_rsrc_register rr;
629 /* keep it extendible */
630 if (size != sizeof(rr))
633 memset(&rr, 0, sizeof(rr));
634 if (copy_from_user(&rr, arg, size))
636 if (!rr.nr || rr.resv2)
638 if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
642 case IORING_RSRC_FILE:
643 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
645 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
646 rr.nr, u64_to_user_ptr(rr.tags));
647 case IORING_RSRC_BUFFER:
648 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
650 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
651 rr.nr, u64_to_user_ptr(rr.tags));
656 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
658 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
660 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
662 if (sqe->rw_flags || sqe->splice_fd_in)
665 up->offset = READ_ONCE(sqe->off);
666 up->nr_args = READ_ONCE(sqe->len);
669 up->arg = READ_ONCE(sqe->addr);
673 static int io_files_update_with_index_alloc(struct io_kiocb *req,
674 unsigned int issue_flags)
676 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
677 __s32 __user *fds = u64_to_user_ptr(up->arg);
682 if (!req->ctx->file_data)
685 for (done = 0; done < up->nr_args; done++) {
686 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
696 ret = io_fixed_fd_install(req, issue_flags, file,
697 IORING_FILE_INDEX_ALLOC);
700 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
701 __io_close_fixed(req->ctx, issue_flags, ret);
712 int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
714 struct io_rsrc_update *up = io_kiocb_to_cmd(req, struct io_rsrc_update);
715 struct io_ring_ctx *ctx = req->ctx;
716 struct io_uring_rsrc_update2 up2;
719 up2.offset = up->offset;
726 if (up->offset == IORING_FILE_INDEX_ALLOC) {
727 ret = io_files_update_with_index_alloc(req, issue_flags);
729 io_ring_submit_lock(ctx, issue_flags);
730 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
732 io_ring_submit_unlock(ctx, issue_flags);
737 io_req_set_res(req, ret, 0);
741 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
742 struct io_rsrc_node *node, void *rsrc)
744 u64 *tag_slot = io_get_tag_slot(data, idx);
745 struct io_rsrc_put *prsrc;
747 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
751 prsrc->tag = *tag_slot;
754 list_add(&prsrc->list, &node->rsrc_list);
758 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
762 for (i = 0; i < ctx->nr_user_files; i++) {
763 struct file *file = io_file_from_index(&ctx->file_table, i);
765 /* skip scm accounted files, they'll be freed by ->ring_sock */
766 if (!file || io_file_need_scm(file))
768 io_file_bitmap_clear(&ctx->file_table, i);
772 #if defined(CONFIG_UNIX)
773 if (ctx->ring_sock) {
774 struct sock *sock = ctx->ring_sock->sk;
777 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
781 io_free_file_tables(&ctx->file_table);
782 io_file_table_set_alloc_range(ctx, 0, 0);
783 io_rsrc_data_free(ctx->file_data);
784 ctx->file_data = NULL;
785 ctx->nr_user_files = 0;
788 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
790 unsigned nr = ctx->nr_user_files;
797 * Quiesce may unlock ->uring_lock, and while it's not held
798 * prevent new requests using the table.
800 ctx->nr_user_files = 0;
801 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
802 ctx->nr_user_files = nr;
804 __io_sqe_files_unregister(ctx);
809 * Ensure the UNIX gc is aware of our file set, so we are certain that
810 * the io_uring can be safely unregistered on process exit, even if we have
811 * loops in the file referencing. We account only files that can hold other
812 * files because otherwise they can't form a loop and so are not interesting
815 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
817 #if defined(CONFIG_UNIX)
818 struct sock *sk = ctx->ring_sock->sk;
819 struct sk_buff_head *head = &sk->sk_receive_queue;
820 struct scm_fp_list *fpl;
823 if (likely(!io_file_need_scm(file)))
827 * See if we can merge this file into an existing skb SCM_RIGHTS
828 * file set. If there's no room, fall back to allocating a new skb
831 spin_lock_irq(&head->lock);
832 skb = skb_peek(head);
833 if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
834 __skb_unlink(skb, head);
837 spin_unlock_irq(&head->lock);
840 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
844 skb = alloc_skb(0, GFP_KERNEL);
850 fpl->user = get_uid(current_user());
851 fpl->max = SCM_MAX_FD;
854 UNIXCB(skb).fp = fpl;
856 skb->scm_io_uring = 1;
857 skb->destructor = unix_destruct_scm;
858 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
861 fpl = UNIXCB(skb).fp;
862 fpl->fp[fpl->count++] = get_file(file);
863 unix_inflight(fpl->user, file);
864 skb_queue_head(head, skb);
870 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
872 struct file *file = prsrc->file;
873 #if defined(CONFIG_UNIX)
874 struct sock *sock = ctx->ring_sock->sk;
875 struct sk_buff_head list, *head = &sock->sk_receive_queue;
879 if (!io_file_need_scm(file)) {
884 __skb_queue_head_init(&list);
887 * Find the skb that holds this file in its SCM_RIGHTS. When found,
888 * remove this entry and rearrange the file array.
890 skb = skb_dequeue(head);
892 struct scm_fp_list *fp;
895 for (i = 0; i < fp->count; i++) {
898 if (fp->fp[i] != file)
901 unix_notinflight(fp->user, fp->fp[i]);
902 left = fp->count - 1 - i;
904 memmove(&fp->fp[i], &fp->fp[i + 1],
905 left * sizeof(struct file *));
912 __skb_queue_tail(&list, skb);
922 __skb_queue_tail(&list, skb);
924 skb = skb_dequeue(head);
927 if (skb_peek(&list)) {
928 spin_lock_irq(&head->lock);
929 while ((skb = __skb_dequeue(&list)) != NULL)
930 __skb_queue_tail(head, skb);
931 spin_unlock_irq(&head->lock);
938 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
939 unsigned nr_args, u64 __user *tags)
941 __s32 __user *fds = (__s32 __user *) arg;
950 if (nr_args > IORING_MAX_FIXED_FILES)
952 if (nr_args > rlimit(RLIMIT_NOFILE))
954 ret = io_rsrc_node_switch_start(ctx);
957 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
962 if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
963 io_rsrc_data_free(ctx->file_data);
964 ctx->file_data = NULL;
968 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
969 struct io_fixed_file *file_slot;
971 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
975 /* allow sparse sets */
976 if (!fds || fd == -1) {
978 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
989 * Don't allow io_uring instances to be registered. If UNIX
990 * isn't enabled, then this causes a reference cycle and this
991 * instance can never get freed. If UNIX is enabled we'll
992 * handle it just fine, but there's still no point in allowing
993 * a ring fd as it doesn't support regular read/write anyway.
995 if (io_is_uring_fops(file)) {
999 ret = io_scm_file_account(ctx, file);
1004 file_slot = io_fixed_file_slot(&ctx->file_table, i);
1005 io_fixed_file_set(file_slot, file);
1006 io_file_bitmap_set(&ctx->file_table, i);
1009 /* default it to the whole table */
1010 io_file_table_set_alloc_range(ctx, 0, ctx->nr_user_files);
1011 io_rsrc_node_switch(ctx, NULL);
1014 __io_sqe_files_unregister(ctx);
1018 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
1020 io_buffer_unmap(ctx, &prsrc->buf);
1024 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1028 for (i = 0; i < ctx->nr_user_bufs; i++)
1029 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
1030 kfree(ctx->user_bufs);
1031 io_rsrc_data_free(ctx->buf_data);
1032 ctx->user_bufs = NULL;
1033 ctx->buf_data = NULL;
1034 ctx->nr_user_bufs = 0;
1037 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1039 unsigned nr = ctx->nr_user_bufs;
1046 * Quiesce may unlock ->uring_lock, and while it's not held
1047 * prevent new requests using the table.
1049 ctx->nr_user_bufs = 0;
1050 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
1051 ctx->nr_user_bufs = nr;
1053 __io_sqe_buffers_unregister(ctx);
1058 * Not super efficient, but this is just a registration time. And we do cache
1059 * the last compound head, so generally we'll only do a full search if we don't
1062 * We check if the given compound head page has already been accounted, to
1063 * avoid double accounting it. This allows us to account the full size of the
1064 * page, not just the constituent pages of a huge page.
1066 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
1067 int nr_pages, struct page *hpage)
1071 /* check current page array */
1072 for (i = 0; i < nr_pages; i++) {
1073 if (!PageCompound(pages[i]))
1075 if (compound_head(pages[i]) == hpage)
1079 /* check previously registered pages */
1080 for (i = 0; i < ctx->nr_user_bufs; i++) {
1081 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
1083 for (j = 0; j < imu->nr_bvecs; j++) {
1084 if (!PageCompound(imu->bvec[j].bv_page))
1086 if (compound_head(imu->bvec[j].bv_page) == hpage)
1094 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1095 int nr_pages, struct io_mapped_ubuf *imu,
1096 struct page **last_hpage)
1100 imu->acct_pages = 0;
1101 for (i = 0; i < nr_pages; i++) {
1102 if (!PageCompound(pages[i])) {
1107 hpage = compound_head(pages[i]);
1108 if (hpage == *last_hpage)
1110 *last_hpage = hpage;
1111 if (headpage_already_acct(ctx, pages, i, hpage))
1113 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1117 if (!imu->acct_pages)
1120 ret = io_account_mem(ctx, imu->acct_pages);
1122 imu->acct_pages = 0;
1126 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1128 unsigned long start, end, nr_pages;
1129 struct vm_area_struct **vmas = NULL;
1130 struct page **pages = NULL;
1131 int i, pret, ret = -ENOMEM;
1133 end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1134 start = ubuf >> PAGE_SHIFT;
1135 nr_pages = end - start;
1137 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1141 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
1147 mmap_read_lock(current->mm);
1148 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1150 if (pret == nr_pages) {
1151 struct file *file = vmas[0]->vm_file;
1153 /* don't support file backed memory */
1154 for (i = 0; i < nr_pages; i++) {
1155 if (vmas[i]->vm_file != file) {
1161 if (!vma_is_shmem(vmas[i]) && !is_file_hugepages(file)) {
1168 ret = pret < 0 ? pret : -EFAULT;
1170 mmap_read_unlock(current->mm);
1173 * if we did partial map, or found file backed vmas,
1174 * release any pages we did get
1177 unpin_user_pages(pages, pret);
1185 pages = ERR_PTR(ret);
1190 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1191 struct io_mapped_ubuf **pimu,
1192 struct page **last_hpage)
1194 struct io_mapped_ubuf *imu = NULL;
1195 struct page **pages = NULL;
1198 int ret, nr_pages, i;
1200 *pimu = ctx->dummy_ubuf;
1205 pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1207 if (IS_ERR(pages)) {
1208 ret = PTR_ERR(pages);
1213 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1217 ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1219 unpin_user_pages(pages, nr_pages);
1223 off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1224 size = iov->iov_len;
1225 for (i = 0; i < nr_pages; i++) {
1228 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1229 imu->bvec[i].bv_page = pages[i];
1230 imu->bvec[i].bv_len = vec_len;
1231 imu->bvec[i].bv_offset = off;
1235 /* store original address for later verification */
1236 imu->ubuf = (unsigned long) iov->iov_base;
1237 imu->ubuf_end = imu->ubuf + iov->iov_len;
1238 imu->nr_bvecs = nr_pages;
1248 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1250 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1251 return ctx->user_bufs ? 0 : -ENOMEM;
1254 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1255 unsigned int nr_args, u64 __user *tags)
1257 struct page *last_hpage = NULL;
1258 struct io_rsrc_data *data;
1262 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1266 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1268 ret = io_rsrc_node_switch_start(ctx);
1271 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
1274 ret = io_buffers_map_alloc(ctx, nr_args);
1276 io_rsrc_data_free(data);
1280 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1282 ret = io_copy_iov(ctx, &iov, arg, i);
1285 ret = io_buffer_validate(&iov);
1289 memset(&iov, 0, sizeof(iov));
1292 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1297 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1303 WARN_ON_ONCE(ctx->buf_data);
1305 ctx->buf_data = data;
1307 __io_sqe_buffers_unregister(ctx);
1309 io_rsrc_node_switch(ctx, NULL);
1313 int io_import_fixed(int ddir, struct iov_iter *iter,
1314 struct io_mapped_ubuf *imu,
1315 u64 buf_addr, size_t len)
1320 if (WARN_ON_ONCE(!imu))
1322 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1324 /* not inside the mapped region */
1325 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1329 * May not be a start of buffer, set size appropriately
1330 * and advance us to the beginning.
1332 offset = buf_addr - imu->ubuf;
1333 iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1337 * Don't use iov_iter_advance() here, as it's really slow for
1338 * using the latter parts of a big fixed buffer - it iterates
1339 * over each segment manually. We can cheat a bit here, because
1342 * 1) it's a BVEC iter, we set it up
1343 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1344 * first and last bvec
1346 * So just find our index, and adjust the iterator afterwards.
1347 * If the offset is within the first bvec (or the whole first
1348 * bvec, just use iov_iter_advance(). This makes it easier
1349 * since we can just skip the first segment, which may not
1350 * be PAGE_SIZE aligned.
1352 const struct bio_vec *bvec = imu->bvec;
1354 if (offset < bvec->bv_len) {
1355 iov_iter_advance(iter, offset);
1357 unsigned long seg_skip;
1359 /* skip first vec */
1360 offset -= bvec->bv_len;
1361 seg_skip = 1 + (offset >> PAGE_SHIFT);
1363 iter->bvec = bvec + seg_skip;
1364 iter->nr_segs -= seg_skip;
1365 iter->count -= bvec->bv_len + offset;
1366 iter->iov_offset = offset & ~PAGE_MASK;