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 static inline void __io_unaccount_mem(struct user_struct *user,
46 unsigned long nr_pages)
48 atomic_long_sub(nr_pages, &user->locked_vm);
51 static inline int __io_account_mem(struct user_struct *user,
52 unsigned long nr_pages)
54 unsigned long page_limit, cur_pages, new_pages;
56 /* Don't allow more pages than we can safely lock */
57 page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
60 cur_pages = atomic_long_read(&user->locked_vm);
61 new_pages = cur_pages + nr_pages;
62 if (new_pages > page_limit)
64 } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages,
65 new_pages) != cur_pages);
70 static void io_unaccount_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
73 __io_unaccount_mem(ctx->user, nr_pages);
76 atomic64_sub(nr_pages, &ctx->mm_account->pinned_vm);
79 static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)
84 ret = __io_account_mem(ctx->user, nr_pages);
90 atomic64_add(nr_pages, &ctx->mm_account->pinned_vm);
95 static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst,
96 void __user *arg, unsigned index)
98 struct iovec __user *src;
102 struct compat_iovec __user *ciovs;
103 struct compat_iovec ciov;
105 ciovs = (struct compat_iovec __user *) arg;
106 if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov)))
109 dst->iov_base = u64_to_user_ptr((u64)ciov.iov_base);
110 dst->iov_len = ciov.iov_len;
114 src = (struct iovec __user *) arg;
115 if (copy_from_user(dst, &src[index], sizeof(*dst)))
120 static int io_buffer_validate(struct iovec *iov)
122 unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE - 1);
125 * Don't impose further limits on the size and buffer
126 * constraints here, we'll -EINVAL later when IO is
127 * submitted if they are wrong.
130 return iov->iov_len ? -EFAULT : 0;
134 /* arbitrary limit, but we need something */
135 if (iov->iov_len > SZ_1G)
138 if (check_add_overflow((unsigned long)iov->iov_base, acct_len, &tmp))
144 static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_mapped_ubuf **slot)
146 struct io_mapped_ubuf *imu = *slot;
149 if (imu != ctx->dummy_ubuf) {
150 for (i = 0; i < imu->nr_bvecs; i++)
151 unpin_user_page(imu->bvec[i].bv_page);
153 io_unaccount_mem(ctx, imu->acct_pages);
159 void io_rsrc_refs_refill(struct io_ring_ctx *ctx)
160 __must_hold(&ctx->uring_lock)
162 ctx->rsrc_cached_refs += IO_RSRC_REF_BATCH;
163 percpu_ref_get_many(&ctx->rsrc_node->refs, IO_RSRC_REF_BATCH);
166 static void __io_rsrc_put_work(struct io_rsrc_node *ref_node)
168 struct io_rsrc_data *rsrc_data = ref_node->rsrc_data;
169 struct io_ring_ctx *ctx = rsrc_data->ctx;
170 struct io_rsrc_put *prsrc, *tmp;
172 list_for_each_entry_safe(prsrc, tmp, &ref_node->rsrc_list, list) {
173 list_del(&prsrc->list);
176 if (ctx->flags & IORING_SETUP_IOPOLL) {
177 mutex_lock(&ctx->uring_lock);
178 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
179 mutex_unlock(&ctx->uring_lock);
181 io_post_aux_cqe(ctx, prsrc->tag, 0, 0);
185 rsrc_data->do_put(ctx, prsrc);
189 io_rsrc_node_destroy(ref_node);
190 if (atomic_dec_and_test(&rsrc_data->refs))
191 complete(&rsrc_data->done);
194 void io_rsrc_put_work(struct work_struct *work)
196 struct io_ring_ctx *ctx;
197 struct llist_node *node;
199 ctx = container_of(work, struct io_ring_ctx, rsrc_put_work.work);
200 node = llist_del_all(&ctx->rsrc_put_llist);
203 struct io_rsrc_node *ref_node;
204 struct llist_node *next = node->next;
206 ref_node = llist_entry(node, struct io_rsrc_node, llist);
207 __io_rsrc_put_work(ref_node);
212 void io_wait_rsrc_data(struct io_rsrc_data *data)
214 if (data && !atomic_dec_and_test(&data->refs))
215 wait_for_completion(&data->done);
218 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node)
220 percpu_ref_exit(&ref_node->refs);
224 static __cold void io_rsrc_node_ref_zero(struct percpu_ref *ref)
226 struct io_rsrc_node *node = container_of(ref, struct io_rsrc_node, refs);
227 struct io_ring_ctx *ctx = node->rsrc_data->ctx;
229 bool first_add = false;
230 unsigned long delay = HZ;
232 spin_lock_irqsave(&ctx->rsrc_ref_lock, flags);
235 /* if we are mid-quiesce then do not delay */
236 if (node->rsrc_data->quiesce)
239 while (!list_empty(&ctx->rsrc_ref_list)) {
240 node = list_first_entry(&ctx->rsrc_ref_list,
241 struct io_rsrc_node, node);
242 /* recycle ref nodes in order */
245 list_del(&node->node);
246 first_add |= llist_add(&node->llist, &ctx->rsrc_put_llist);
248 spin_unlock_irqrestore(&ctx->rsrc_ref_lock, flags);
251 mod_delayed_work(system_wq, &ctx->rsrc_put_work, delay);
254 static struct io_rsrc_node *io_rsrc_node_alloc(void)
256 struct io_rsrc_node *ref_node;
258 ref_node = kzalloc(sizeof(*ref_node), GFP_KERNEL);
262 if (percpu_ref_init(&ref_node->refs, io_rsrc_node_ref_zero,
267 INIT_LIST_HEAD(&ref_node->node);
268 INIT_LIST_HEAD(&ref_node->rsrc_list);
269 ref_node->done = false;
273 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
274 struct io_rsrc_data *data_to_kill)
275 __must_hold(&ctx->uring_lock)
277 WARN_ON_ONCE(!ctx->rsrc_backup_node);
278 WARN_ON_ONCE(data_to_kill && !ctx->rsrc_node);
280 io_rsrc_refs_drop(ctx);
283 struct io_rsrc_node *rsrc_node = ctx->rsrc_node;
285 rsrc_node->rsrc_data = data_to_kill;
286 spin_lock_irq(&ctx->rsrc_ref_lock);
287 list_add_tail(&rsrc_node->node, &ctx->rsrc_ref_list);
288 spin_unlock_irq(&ctx->rsrc_ref_lock);
290 atomic_inc(&data_to_kill->refs);
291 percpu_ref_kill(&rsrc_node->refs);
292 ctx->rsrc_node = NULL;
295 if (!ctx->rsrc_node) {
296 ctx->rsrc_node = ctx->rsrc_backup_node;
297 ctx->rsrc_backup_node = NULL;
301 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx)
303 if (ctx->rsrc_backup_node)
305 ctx->rsrc_backup_node = io_rsrc_node_alloc();
306 return ctx->rsrc_backup_node ? 0 : -ENOMEM;
309 __cold static int io_rsrc_ref_quiesce(struct io_rsrc_data *data,
310 struct io_ring_ctx *ctx)
314 /* As we may drop ->uring_lock, other task may have started quiesce */
318 data->quiesce = true;
320 ret = io_rsrc_node_switch_start(ctx);
323 io_rsrc_node_switch(ctx, data);
325 /* kill initial ref, already quiesced if zero */
326 if (atomic_dec_and_test(&data->refs))
328 mutex_unlock(&ctx->uring_lock);
329 flush_delayed_work(&ctx->rsrc_put_work);
330 ret = wait_for_completion_interruptible(&data->done);
332 mutex_lock(&ctx->uring_lock);
333 if (atomic_read(&data->refs) > 0) {
335 * it has been revived by another thread while
338 mutex_unlock(&ctx->uring_lock);
344 atomic_inc(&data->refs);
345 /* wait for all works potentially completing data->done */
346 flush_delayed_work(&ctx->rsrc_put_work);
347 reinit_completion(&data->done);
349 ret = io_run_task_work_sig();
350 mutex_lock(&ctx->uring_lock);
352 data->quiesce = false;
357 static void io_free_page_table(void **table, size_t size)
359 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
361 for (i = 0; i < nr_tables; i++)
366 static void io_rsrc_data_free(struct io_rsrc_data *data)
368 size_t size = data->nr * sizeof(data->tags[0][0]);
371 io_free_page_table((void **)data->tags, size);
375 static __cold void **io_alloc_page_table(size_t size)
377 unsigned i, nr_tables = DIV_ROUND_UP(size, PAGE_SIZE);
378 size_t init_size = size;
381 table = kcalloc(nr_tables, sizeof(*table), GFP_KERNEL_ACCOUNT);
385 for (i = 0; i < nr_tables; i++) {
386 unsigned int this_size = min_t(size_t, size, PAGE_SIZE);
388 table[i] = kzalloc(this_size, GFP_KERNEL_ACCOUNT);
390 io_free_page_table(table, init_size);
398 __cold static int io_rsrc_data_alloc(struct io_ring_ctx *ctx,
399 rsrc_put_fn *do_put, u64 __user *utags,
400 unsigned nr, struct io_rsrc_data **pdata)
402 struct io_rsrc_data *data;
406 data = kzalloc(sizeof(*data), GFP_KERNEL);
409 data->tags = (u64 **)io_alloc_page_table(nr * sizeof(data->tags[0][0]));
417 data->do_put = do_put;
420 for (i = 0; i < nr; i++) {
421 u64 *tag_slot = io_get_tag_slot(data, i);
423 if (copy_from_user(tag_slot, &utags[i],
429 atomic_set(&data->refs, 1);
430 init_completion(&data->done);
434 io_rsrc_data_free(data);
438 static int __io_sqe_files_update(struct io_ring_ctx *ctx,
439 struct io_uring_rsrc_update2 *up,
442 u64 __user *tags = u64_to_user_ptr(up->tags);
443 __s32 __user *fds = u64_to_user_ptr(up->data);
444 struct io_rsrc_data *data = ctx->file_data;
445 struct io_fixed_file *file_slot;
449 bool needs_switch = false;
453 if (up->offset + nr_args > ctx->nr_user_files)
456 for (done = 0; done < nr_args; done++) {
459 if ((tags && copy_from_user(&tag, &tags[done], sizeof(tag))) ||
460 copy_from_user(&fd, &fds[done], sizeof(fd))) {
464 if ((fd == IORING_REGISTER_FILES_SKIP || fd == -1) && tag) {
468 if (fd == IORING_REGISTER_FILES_SKIP)
471 i = array_index_nospec(up->offset + done, ctx->nr_user_files);
472 file_slot = io_fixed_file_slot(&ctx->file_table, i);
474 if (file_slot->file_ptr) {
475 file = (struct file *)(file_slot->file_ptr & FFS_MASK);
476 err = io_queue_rsrc_removal(data, i, ctx->rsrc_node, file);
479 file_slot->file_ptr = 0;
480 io_file_bitmap_clear(&ctx->file_table, i);
490 * Don't allow io_uring instances to be registered. If
491 * UNIX isn't enabled, then this causes a reference
492 * cycle and this instance can never get freed. If UNIX
493 * is enabled we'll handle it just fine, but there's
494 * still no point in allowing a ring fd as it doesn't
495 * support regular read/write anyway.
497 if (io_is_uring_fops(file)) {
502 err = io_scm_file_account(ctx, file);
507 *io_get_tag_slot(data, i) = tag;
508 io_fixed_file_set(file_slot, file);
509 io_file_bitmap_set(&ctx->file_table, i);
514 io_rsrc_node_switch(ctx, data);
515 return done ? done : err;
518 static int __io_sqe_buffers_update(struct io_ring_ctx *ctx,
519 struct io_uring_rsrc_update2 *up,
520 unsigned int nr_args)
522 u64 __user *tags = u64_to_user_ptr(up->tags);
523 struct iovec iov, __user *iovs = u64_to_user_ptr(up->data);
524 struct page *last_hpage = NULL;
525 bool needs_switch = false;
531 if (up->offset + nr_args > ctx->nr_user_bufs)
534 for (done = 0; done < nr_args; done++) {
535 struct io_mapped_ubuf *imu;
536 int offset = up->offset + done;
539 err = io_copy_iov(ctx, &iov, iovs, done);
542 if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) {
546 err = io_buffer_validate(&iov);
549 if (!iov.iov_base && tag) {
553 err = io_sqe_buffer_register(ctx, &iov, &imu, &last_hpage);
557 i = array_index_nospec(offset, ctx->nr_user_bufs);
558 if (ctx->user_bufs[i] != ctx->dummy_ubuf) {
559 err = io_queue_rsrc_removal(ctx->buf_data, i,
560 ctx->rsrc_node, ctx->user_bufs[i]);
562 io_buffer_unmap(ctx, &imu);
565 ctx->user_bufs[i] = ctx->dummy_ubuf;
569 ctx->user_bufs[i] = imu;
570 *io_get_tag_slot(ctx->buf_data, offset) = tag;
574 io_rsrc_node_switch(ctx, ctx->buf_data);
575 return done ? done : err;
578 static int __io_register_rsrc_update(struct io_ring_ctx *ctx, unsigned type,
579 struct io_uring_rsrc_update2 *up,
585 if (check_add_overflow(up->offset, nr_args, &tmp))
587 err = io_rsrc_node_switch_start(ctx);
592 case IORING_RSRC_FILE:
593 return __io_sqe_files_update(ctx, up, nr_args);
594 case IORING_RSRC_BUFFER:
595 return __io_sqe_buffers_update(ctx, up, nr_args);
600 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
603 struct io_uring_rsrc_update2 up;
607 memset(&up, 0, sizeof(up));
608 if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update)))
610 if (up.resv || up.resv2)
612 return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args);
615 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
616 unsigned size, unsigned type)
618 struct io_uring_rsrc_update2 up;
620 if (size != sizeof(up))
622 if (copy_from_user(&up, arg, sizeof(up)))
624 if (!up.nr || up.resv || up.resv2)
626 return __io_register_rsrc_update(ctx, type, &up, up.nr);
629 __cold int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
630 unsigned int size, unsigned int type)
632 struct io_uring_rsrc_register rr;
634 /* keep it extendible */
635 if (size != sizeof(rr))
638 memset(&rr, 0, sizeof(rr));
639 if (copy_from_user(&rr, arg, size))
641 if (!rr.nr || rr.resv2)
643 if (rr.flags & ~IORING_RSRC_REGISTER_SPARSE)
647 case IORING_RSRC_FILE:
648 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
650 return io_sqe_files_register(ctx, u64_to_user_ptr(rr.data),
651 rr.nr, u64_to_user_ptr(rr.tags));
652 case IORING_RSRC_BUFFER:
653 if (rr.flags & IORING_RSRC_REGISTER_SPARSE && rr.data)
655 return io_sqe_buffers_register(ctx, u64_to_user_ptr(rr.data),
656 rr.nr, u64_to_user_ptr(rr.tags));
661 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
663 struct io_rsrc_update *up = io_kiocb_to_cmd(req);
665 if (unlikely(req->flags & (REQ_F_FIXED_FILE | REQ_F_BUFFER_SELECT)))
667 if (sqe->rw_flags || sqe->splice_fd_in)
670 up->offset = READ_ONCE(sqe->off);
671 up->nr_args = READ_ONCE(sqe->len);
674 up->arg = READ_ONCE(sqe->addr);
678 static int io_files_update_with_index_alloc(struct io_kiocb *req,
679 unsigned int issue_flags)
681 struct io_rsrc_update *up = io_kiocb_to_cmd(req);
682 __s32 __user *fds = u64_to_user_ptr(up->arg);
687 if (!req->ctx->file_data)
690 for (done = 0; done < up->nr_args; done++) {
691 if (copy_from_user(&fd, &fds[done], sizeof(fd))) {
701 ret = io_fixed_fd_install(req, issue_flags, file,
702 IORING_FILE_INDEX_ALLOC);
705 if (copy_to_user(&fds[done], &ret, sizeof(ret))) {
706 __io_close_fixed(req, issue_flags, ret);
717 int io_files_update(struct io_kiocb *req, unsigned int issue_flags)
719 struct io_rsrc_update *up = io_kiocb_to_cmd(req);
720 struct io_ring_ctx *ctx = req->ctx;
721 struct io_uring_rsrc_update2 up2;
724 up2.offset = up->offset;
731 if (up->offset == IORING_FILE_INDEX_ALLOC) {
732 ret = io_files_update_with_index_alloc(req, issue_flags);
734 io_ring_submit_lock(ctx, issue_flags);
735 ret = __io_register_rsrc_update(ctx, IORING_RSRC_FILE,
737 io_ring_submit_unlock(ctx, issue_flags);
742 io_req_set_res(req, ret, 0);
746 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
747 struct io_rsrc_node *node, void *rsrc)
749 u64 *tag_slot = io_get_tag_slot(data, idx);
750 struct io_rsrc_put *prsrc;
752 prsrc = kzalloc(sizeof(*prsrc), GFP_KERNEL);
756 prsrc->tag = *tag_slot;
759 list_add(&prsrc->list, &node->rsrc_list);
763 void __io_sqe_files_unregister(struct io_ring_ctx *ctx)
765 #if !defined(IO_URING_SCM_ALL)
768 for (i = 0; i < ctx->nr_user_files; i++) {
769 struct file *file = io_file_from_index(&ctx->file_table, i);
773 if (io_fixed_file_slot(&ctx->file_table, i)->file_ptr & FFS_SCM)
775 io_file_bitmap_clear(&ctx->file_table, i);
780 #if defined(CONFIG_UNIX)
781 if (ctx->ring_sock) {
782 struct sock *sock = ctx->ring_sock->sk;
785 while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL)
789 io_free_file_tables(&ctx->file_table);
790 io_rsrc_data_free(ctx->file_data);
791 ctx->file_data = NULL;
792 ctx->nr_user_files = 0;
795 int io_sqe_files_unregister(struct io_ring_ctx *ctx)
797 unsigned nr = ctx->nr_user_files;
804 * Quiesce may unlock ->uring_lock, and while it's not held
805 * prevent new requests using the table.
807 ctx->nr_user_files = 0;
808 ret = io_rsrc_ref_quiesce(ctx->file_data, ctx);
809 ctx->nr_user_files = nr;
811 __io_sqe_files_unregister(ctx);
816 * Ensure the UNIX gc is aware of our file set, so we are certain that
817 * the io_uring can be safely unregistered on process exit, even if we have
818 * loops in the file referencing. We account only files that can hold other
819 * files because otherwise they can't form a loop and so are not interesting
822 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file)
824 #if defined(CONFIG_UNIX)
825 struct sock *sk = ctx->ring_sock->sk;
826 struct sk_buff_head *head = &sk->sk_receive_queue;
827 struct scm_fp_list *fpl;
830 if (likely(!io_file_need_scm(file)))
834 * See if we can merge this file into an existing skb SCM_RIGHTS
835 * file set. If there's no room, fall back to allocating a new skb
838 spin_lock_irq(&head->lock);
839 skb = skb_peek(head);
840 if (skb && UNIXCB(skb).fp->count < SCM_MAX_FD)
841 __skb_unlink(skb, head);
844 spin_unlock_irq(&head->lock);
847 fpl = kzalloc(sizeof(*fpl), GFP_KERNEL);
851 skb = alloc_skb(0, GFP_KERNEL);
857 fpl->user = get_uid(current_user());
858 fpl->max = SCM_MAX_FD;
861 UNIXCB(skb).fp = fpl;
863 skb->destructor = unix_destruct_scm;
864 refcount_add(skb->truesize, &sk->sk_wmem_alloc);
867 fpl = UNIXCB(skb).fp;
868 fpl->fp[fpl->count++] = get_file(file);
869 unix_inflight(fpl->user, file);
870 skb_queue_head(head, skb);
876 static void io_rsrc_file_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
878 struct file *file = prsrc->file;
879 #if defined(CONFIG_UNIX)
880 struct sock *sock = ctx->ring_sock->sk;
881 struct sk_buff_head list, *head = &sock->sk_receive_queue;
885 if (!io_file_need_scm(file)) {
890 __skb_queue_head_init(&list);
893 * Find the skb that holds this file in its SCM_RIGHTS. When found,
894 * remove this entry and rearrange the file array.
896 skb = skb_dequeue(head);
898 struct scm_fp_list *fp;
901 for (i = 0; i < fp->count; i++) {
904 if (fp->fp[i] != file)
907 unix_notinflight(fp->user, fp->fp[i]);
908 left = fp->count - 1 - i;
910 memmove(&fp->fp[i], &fp->fp[i + 1],
911 left * sizeof(struct file *));
918 __skb_queue_tail(&list, skb);
928 __skb_queue_tail(&list, skb);
930 skb = skb_dequeue(head);
933 if (skb_peek(&list)) {
934 spin_lock_irq(&head->lock);
935 while ((skb = __skb_dequeue(&list)) != NULL)
936 __skb_queue_tail(head, skb);
937 spin_unlock_irq(&head->lock);
944 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
945 unsigned nr_args, u64 __user *tags)
947 __s32 __user *fds = (__s32 __user *) arg;
956 if (nr_args > IORING_MAX_FIXED_FILES)
958 if (nr_args > rlimit(RLIMIT_NOFILE))
960 ret = io_rsrc_node_switch_start(ctx);
963 ret = io_rsrc_data_alloc(ctx, io_rsrc_file_put, tags, nr_args,
968 if (!io_alloc_file_tables(&ctx->file_table, nr_args)) {
969 io_rsrc_data_free(ctx->file_data);
970 ctx->file_data = NULL;
974 for (i = 0; i < nr_args; i++, ctx->nr_user_files++) {
975 struct io_fixed_file *file_slot;
977 if (fds && copy_from_user(&fd, &fds[i], sizeof(fd))) {
981 /* allow sparse sets */
982 if (!fds || fd == -1) {
984 if (unlikely(*io_get_tag_slot(ctx->file_data, i)))
995 * Don't allow io_uring instances to be registered. If UNIX
996 * isn't enabled, then this causes a reference cycle and this
997 * instance can never get freed. If UNIX is enabled we'll
998 * handle it just fine, but there's still no point in allowing
999 * a ring fd as it doesn't support regular read/write anyway.
1001 if (io_is_uring_fops(file)) {
1005 ret = io_scm_file_account(ctx, file);
1010 file_slot = io_fixed_file_slot(&ctx->file_table, i);
1011 io_fixed_file_set(file_slot, file);
1012 io_file_bitmap_set(&ctx->file_table, i);
1015 io_rsrc_node_switch(ctx, NULL);
1018 __io_sqe_files_unregister(ctx);
1022 static void io_rsrc_buf_put(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc)
1024 io_buffer_unmap(ctx, &prsrc->buf);
1028 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1032 for (i = 0; i < ctx->nr_user_bufs; i++)
1033 io_buffer_unmap(ctx, &ctx->user_bufs[i]);
1034 kfree(ctx->user_bufs);
1035 io_rsrc_data_free(ctx->buf_data);
1036 ctx->user_bufs = NULL;
1037 ctx->buf_data = NULL;
1038 ctx->nr_user_bufs = 0;
1041 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx)
1043 unsigned nr = ctx->nr_user_bufs;
1050 * Quiesce may unlock ->uring_lock, and while it's not held
1051 * prevent new requests using the table.
1053 ctx->nr_user_bufs = 0;
1054 ret = io_rsrc_ref_quiesce(ctx->buf_data, ctx);
1055 ctx->nr_user_bufs = nr;
1057 __io_sqe_buffers_unregister(ctx);
1062 * Not super efficient, but this is just a registration time. And we do cache
1063 * the last compound head, so generally we'll only do a full search if we don't
1066 * We check if the given compound head page has already been accounted, to
1067 * avoid double accounting it. This allows us to account the full size of the
1068 * page, not just the constituent pages of a huge page.
1070 static bool headpage_already_acct(struct io_ring_ctx *ctx, struct page **pages,
1071 int nr_pages, struct page *hpage)
1075 /* check current page array */
1076 for (i = 0; i < nr_pages; i++) {
1077 if (!PageCompound(pages[i]))
1079 if (compound_head(pages[i]) == hpage)
1083 /* check previously registered pages */
1084 for (i = 0; i < ctx->nr_user_bufs; i++) {
1085 struct io_mapped_ubuf *imu = ctx->user_bufs[i];
1087 for (j = 0; j < imu->nr_bvecs; j++) {
1088 if (!PageCompound(imu->bvec[j].bv_page))
1090 if (compound_head(imu->bvec[j].bv_page) == hpage)
1098 static int io_buffer_account_pin(struct io_ring_ctx *ctx, struct page **pages,
1099 int nr_pages, struct io_mapped_ubuf *imu,
1100 struct page **last_hpage)
1104 imu->acct_pages = 0;
1105 for (i = 0; i < nr_pages; i++) {
1106 if (!PageCompound(pages[i])) {
1111 hpage = compound_head(pages[i]);
1112 if (hpage == *last_hpage)
1114 *last_hpage = hpage;
1115 if (headpage_already_acct(ctx, pages, i, hpage))
1117 imu->acct_pages += page_size(hpage) >> PAGE_SHIFT;
1121 if (!imu->acct_pages)
1124 ret = io_account_mem(ctx, imu->acct_pages);
1126 imu->acct_pages = 0;
1130 struct page **io_pin_pages(unsigned long ubuf, unsigned long len, int *npages)
1132 unsigned long start, end, nr_pages;
1133 struct vm_area_struct **vmas = NULL;
1134 struct page **pages = NULL;
1135 int i, pret, ret = -ENOMEM;
1137 end = (ubuf + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1138 start = ubuf >> PAGE_SHIFT;
1139 nr_pages = end - start;
1141 pages = kvmalloc_array(nr_pages, sizeof(struct page *), GFP_KERNEL);
1145 vmas = kvmalloc_array(nr_pages, sizeof(struct vm_area_struct *),
1151 mmap_read_lock(current->mm);
1152 pret = pin_user_pages(ubuf, nr_pages, FOLL_WRITE | FOLL_LONGTERM,
1154 if (pret == nr_pages) {
1155 /* don't support file backed memory */
1156 for (i = 0; i < nr_pages; i++) {
1157 struct vm_area_struct *vma = vmas[i];
1159 if (vma_is_shmem(vma))
1162 !is_file_hugepages(vma->vm_file)) {
1169 ret = pret < 0 ? pret : -EFAULT;
1171 mmap_read_unlock(current->mm);
1174 * if we did partial map, or found file backed vmas,
1175 * release any pages we did get
1178 unpin_user_pages(pages, pret);
1186 pages = ERR_PTR(ret);
1191 static int io_sqe_buffer_register(struct io_ring_ctx *ctx, struct iovec *iov,
1192 struct io_mapped_ubuf **pimu,
1193 struct page **last_hpage)
1195 struct io_mapped_ubuf *imu = NULL;
1196 struct page **pages = NULL;
1199 int ret, nr_pages, i;
1201 *pimu = ctx->dummy_ubuf;
1206 pages = io_pin_pages((unsigned long) iov->iov_base, iov->iov_len,
1208 if (IS_ERR(pages)) {
1209 ret = PTR_ERR(pages);
1214 imu = kvmalloc(struct_size(imu, bvec, nr_pages), GFP_KERNEL);
1218 ret = io_buffer_account_pin(ctx, pages, nr_pages, imu, last_hpage);
1220 unpin_user_pages(pages, nr_pages);
1224 off = (unsigned long) iov->iov_base & ~PAGE_MASK;
1225 size = iov->iov_len;
1226 for (i = 0; i < nr_pages; i++) {
1229 vec_len = min_t(size_t, size, PAGE_SIZE - off);
1230 imu->bvec[i].bv_page = pages[i];
1231 imu->bvec[i].bv_len = vec_len;
1232 imu->bvec[i].bv_offset = off;
1236 /* store original address for later verification */
1237 imu->ubuf = (unsigned long) iov->iov_base;
1238 imu->ubuf_end = imu->ubuf + iov->iov_len;
1239 imu->nr_bvecs = nr_pages;
1249 static int io_buffers_map_alloc(struct io_ring_ctx *ctx, unsigned int nr_args)
1251 ctx->user_bufs = kcalloc(nr_args, sizeof(*ctx->user_bufs), GFP_KERNEL);
1252 return ctx->user_bufs ? 0 : -ENOMEM;
1255 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
1256 unsigned int nr_args, u64 __user *tags)
1258 struct page *last_hpage = NULL;
1259 struct io_rsrc_data *data;
1263 BUILD_BUG_ON(IORING_MAX_REG_BUFFERS >= (1u << 16));
1267 if (!nr_args || nr_args > IORING_MAX_REG_BUFFERS)
1269 ret = io_rsrc_node_switch_start(ctx);
1272 ret = io_rsrc_data_alloc(ctx, io_rsrc_buf_put, tags, nr_args, &data);
1275 ret = io_buffers_map_alloc(ctx, nr_args);
1277 io_rsrc_data_free(data);
1281 for (i = 0; i < nr_args; i++, ctx->nr_user_bufs++) {
1283 ret = io_copy_iov(ctx, &iov, arg, i);
1286 ret = io_buffer_validate(&iov);
1290 memset(&iov, 0, sizeof(iov));
1293 if (!iov.iov_base && *io_get_tag_slot(data, i)) {
1298 ret = io_sqe_buffer_register(ctx, &iov, &ctx->user_bufs[i],
1304 WARN_ON_ONCE(ctx->buf_data);
1306 ctx->buf_data = data;
1308 __io_sqe_buffers_unregister(ctx);
1310 io_rsrc_node_switch(ctx, NULL);
1314 int io_import_fixed(int ddir, struct iov_iter *iter,
1315 struct io_mapped_ubuf *imu,
1316 u64 buf_addr, size_t len)
1321 if (WARN_ON_ONCE(!imu))
1323 if (unlikely(check_add_overflow(buf_addr, (u64)len, &buf_end)))
1325 /* not inside the mapped region */
1326 if (unlikely(buf_addr < imu->ubuf || buf_end > imu->ubuf_end))
1330 * May not be a start of buffer, set size appropriately
1331 * and advance us to the beginning.
1333 offset = buf_addr - imu->ubuf;
1334 iov_iter_bvec(iter, ddir, imu->bvec, imu->nr_bvecs, offset + len);
1338 * Don't use iov_iter_advance() here, as it's really slow for
1339 * using the latter parts of a big fixed buffer - it iterates
1340 * over each segment manually. We can cheat a bit here, because
1343 * 1) it's a BVEC iter, we set it up
1344 * 2) all bvecs are PAGE_SIZE in size, except potentially the
1345 * first and last bvec
1347 * So just find our index, and adjust the iterator afterwards.
1348 * If the offset is within the first bvec (or the whole first
1349 * bvec, just use iov_iter_advance(). This makes it easier
1350 * since we can just skip the first segment, which may not
1351 * be PAGE_SIZE aligned.
1353 const struct bio_vec *bvec = imu->bvec;
1355 if (offset <= bvec->bv_len) {
1356 iov_iter_advance(iter, offset);
1358 unsigned long seg_skip;
1360 /* skip first vec */
1361 offset -= bvec->bv_len;
1362 seg_skip = 1 + (offset >> PAGE_SHIFT);
1364 iter->bvec = bvec + seg_skip;
1365 iter->nr_segs -= seg_skip;
1366 iter->count -= bvec->bv_len + offset;
1367 iter->iov_offset = offset & ~PAGE_MASK;