1 // SPDX-License-Identifier: GPL-2.0
5 #include <net/af_unix.h>
7 #define IO_RSRC_TAG_TABLE_SHIFT (PAGE_SHIFT - 3)
8 #define IO_RSRC_TAG_TABLE_MAX (1U << IO_RSRC_TAG_TABLE_SHIFT)
9 #define IO_RSRC_TAG_TABLE_MASK (IO_RSRC_TAG_TABLE_MAX - 1)
13 IORING_RSRC_BUFFER = 1,
17 struct list_head list;
22 struct io_mapped_ubuf *buf;
26 typedef void (rsrc_put_fn)(struct io_ring_ctx *ctx, struct io_rsrc_put *prsrc);
29 struct io_ring_ctx *ctx;
35 struct completion done;
40 struct percpu_ref refs;
41 struct list_head node;
42 struct list_head rsrc_list;
43 struct io_rsrc_data *rsrc_data;
44 struct llist_node llist;
48 struct io_mapped_ubuf {
51 unsigned int nr_bvecs;
52 unsigned long acct_pages;
53 struct bio_vec bvec[];
56 void io_rsrc_put_work(struct work_struct *work);
57 void io_rsrc_refs_refill(struct io_ring_ctx *ctx);
58 void io_wait_rsrc_data(struct io_rsrc_data *data);
59 void io_rsrc_node_destroy(struct io_rsrc_node *ref_node);
60 void io_rsrc_refs_drop(struct io_ring_ctx *ctx);
61 int io_rsrc_node_switch_start(struct io_ring_ctx *ctx);
62 int io_queue_rsrc_removal(struct io_rsrc_data *data, unsigned idx,
63 struct io_rsrc_node *node, void *rsrc);
64 void io_rsrc_node_switch(struct io_ring_ctx *ctx,
65 struct io_rsrc_data *data_to_kill);
67 int io_import_fixed(int ddir, struct iov_iter *iter,
68 struct io_mapped_ubuf *imu,
69 u64 buf_addr, size_t len);
71 void __io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
72 int io_sqe_buffers_unregister(struct io_ring_ctx *ctx);
73 int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,
74 unsigned int nr_args, u64 __user *tags);
75 void __io_sqe_files_unregister(struct io_ring_ctx *ctx);
76 int io_sqe_files_unregister(struct io_ring_ctx *ctx);
77 int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,
78 unsigned nr_args, u64 __user *tags);
80 int __io_scm_file_account(struct io_ring_ctx *ctx, struct file *file);
82 #if defined(CONFIG_UNIX)
83 static inline bool io_file_need_scm(struct file *filp)
85 return !!unix_get_socket(filp);
88 static inline bool io_file_need_scm(struct file *filp)
94 static inline int io_scm_file_account(struct io_ring_ctx *ctx,
97 if (likely(!io_file_need_scm(file)))
99 return __io_scm_file_account(ctx, file);
102 int io_register_files_update(struct io_ring_ctx *ctx, void __user *arg,
104 int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg,
105 unsigned size, unsigned type);
106 int io_register_rsrc(struct io_ring_ctx *ctx, void __user *arg,
107 unsigned int size, unsigned int type);
109 static inline void io_rsrc_put_node(struct io_rsrc_node *node, int nr)
111 percpu_ref_put_many(&node->refs, nr);
114 static inline void io_req_put_rsrc(struct io_kiocb *req)
117 io_rsrc_put_node(req->rsrc_node, 1);
120 static inline void io_req_put_rsrc_locked(struct io_kiocb *req,
121 struct io_ring_ctx *ctx)
122 __must_hold(&ctx->uring_lock)
124 struct io_rsrc_node *node = req->rsrc_node;
127 if (node == ctx->rsrc_node)
128 ctx->rsrc_cached_refs++;
130 io_rsrc_put_node(node, 1);
134 static inline void io_charge_rsrc_node(struct io_ring_ctx *ctx)
136 ctx->rsrc_cached_refs--;
137 if (unlikely(ctx->rsrc_cached_refs < 0))
138 io_rsrc_refs_refill(ctx);
141 static inline void io_req_set_rsrc_node(struct io_kiocb *req,
142 struct io_ring_ctx *ctx,
143 unsigned int issue_flags)
145 if (!req->rsrc_node) {
146 req->rsrc_node = ctx->rsrc_node;
148 if (!(issue_flags & IO_URING_F_UNLOCKED)) {
149 lockdep_assert_held(&ctx->uring_lock);
151 io_charge_rsrc_node(ctx);
153 percpu_ref_get(&req->rsrc_node->refs);
158 static inline u64 *io_get_tag_slot(struct io_rsrc_data *data, unsigned int idx)
160 unsigned int off = idx & IO_RSRC_TAG_TABLE_MASK;
161 unsigned int table_idx = idx >> IO_RSRC_TAG_TABLE_SHIFT;
163 return &data->tags[table_idx][off];
166 int io_files_update(struct io_kiocb *req, unsigned int issue_flags);
167 int io_files_update_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
169 int __io_account_mem(struct user_struct *user, unsigned long nr_pages);
171 static inline void __io_unaccount_mem(struct user_struct *user,
172 unsigned long nr_pages)
174 atomic_long_sub(nr_pages, &user->locked_vm);