1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/file.h>
6 #include <linux/slab.h>
7 #include <linux/nospec.h>
8 #include <linux/io_uring.h>
10 #include <uapi/linux/io_uring.h>
15 static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
16 struct task_struct *task)
18 struct io_wq_hash *hash;
19 struct io_wq_data data;
20 unsigned int concurrency;
22 mutex_lock(&ctx->uring_lock);
25 hash = kzalloc(sizeof(*hash), GFP_KERNEL);
27 mutex_unlock(&ctx->uring_lock);
28 return ERR_PTR(-ENOMEM);
30 refcount_set(&hash->refs, 1);
31 init_waitqueue_head(&hash->wait);
34 mutex_unlock(&ctx->uring_lock);
38 data.free_work = io_wq_free_work;
39 data.do_work = io_wq_submit_work;
41 /* Do QD, or 4 * CPUS, whatever is smallest */
42 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
44 return io_wq_create(concurrency, &data);
47 void __io_uring_free(struct task_struct *tsk)
49 struct io_uring_task *tctx = tsk->io_uring;
51 WARN_ON_ONCE(!xa_empty(&tctx->xa));
52 WARN_ON_ONCE(tctx->io_wq);
53 WARN_ON_ONCE(tctx->cached_refs);
55 percpu_counter_destroy(&tctx->inflight);
60 __cold int io_uring_alloc_task_context(struct task_struct *task,
61 struct io_ring_ctx *ctx)
63 struct io_uring_task *tctx;
66 tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
70 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
76 tctx->io_wq = io_init_wq_offload(ctx, task);
77 if (IS_ERR(tctx->io_wq)) {
78 ret = PTR_ERR(tctx->io_wq);
79 percpu_counter_destroy(&tctx->inflight);
85 init_waitqueue_head(&tctx->wait);
86 atomic_set(&tctx->in_idle, 0);
87 atomic_set(&tctx->inflight_tracked, 0);
88 task->io_uring = tctx;
89 init_llist_head(&tctx->task_list);
90 init_task_work(&tctx->task_work, tctx_task_work);
94 static int io_register_submitter(struct io_ring_ctx *ctx)
98 mutex_lock(&ctx->uring_lock);
99 if (!ctx->submitter_task)
100 ctx->submitter_task = get_task_struct(current);
101 else if (ctx->submitter_task != current)
103 mutex_unlock(&ctx->uring_lock);
108 int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
110 struct io_uring_task *tctx = current->io_uring;
111 struct io_tctx_node *node;
114 if (unlikely(!tctx)) {
115 ret = io_uring_alloc_task_context(current, ctx);
119 tctx = current->io_uring;
120 if (ctx->iowq_limits_set) {
121 unsigned int limits[2] = { ctx->iowq_limits[0],
122 ctx->iowq_limits[1], };
124 ret = io_wq_max_workers(tctx->io_wq, limits);
129 if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
130 node = kmalloc(sizeof(*node), GFP_KERNEL);
134 node->task = current;
136 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
143 mutex_lock(&ctx->uring_lock);
144 list_add(&node->ctx_node, &ctx->tctx_list);
145 mutex_unlock(&ctx->uring_lock);
150 int __io_uring_add_tctx_node_from_submit(struct io_ring_ctx *ctx)
154 if (ctx->flags & IORING_SETUP_SINGLE_ISSUER) {
155 ret = io_register_submitter(ctx);
160 ret = __io_uring_add_tctx_node(ctx);
164 current->io_uring->last = ctx;
169 * Remove this io_uring_file -> task mapping.
171 __cold void io_uring_del_tctx_node(unsigned long index)
173 struct io_uring_task *tctx = current->io_uring;
174 struct io_tctx_node *node;
178 node = xa_erase(&tctx->xa, index);
182 WARN_ON_ONCE(current != node->task);
183 WARN_ON_ONCE(list_empty(&node->ctx_node));
185 mutex_lock(&node->ctx->uring_lock);
186 list_del(&node->ctx_node);
187 mutex_unlock(&node->ctx->uring_lock);
189 if (tctx->last == node->ctx)
194 __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
196 struct io_wq *wq = tctx->io_wq;
197 struct io_tctx_node *node;
200 xa_for_each(&tctx->xa, index, node) {
201 io_uring_del_tctx_node(index);
206 * Must be after io_uring_del_tctx_node() (removes nodes under
207 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
209 io_wq_put_and_exit(wq);
214 void io_uring_unreg_ringfd(void)
216 struct io_uring_task *tctx = current->io_uring;
219 for (i = 0; i < IO_RINGFD_REG_MAX; i++) {
220 if (tctx->registered_rings[i]) {
221 fput(tctx->registered_rings[i]);
222 tctx->registered_rings[i] = NULL;
227 static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd,
233 for (offset = start; offset < end; offset++) {
234 offset = array_index_nospec(offset, IO_RINGFD_REG_MAX);
235 if (tctx->registered_rings[offset])
241 } else if (!io_is_uring_fops(file)) {
245 tctx->registered_rings[offset] = file;
253 * Register a ring fd to avoid fdget/fdput for each io_uring_enter()
254 * invocation. User passes in an array of struct io_uring_rsrc_update
255 * with ->data set to the ring_fd, and ->offset given for the desired
256 * index. If no index is desired, application may set ->offset == -1U
257 * and we'll find an available index. Returns number of entries
258 * successfully processed, or < 0 on error if none were processed.
260 int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg,
263 struct io_uring_rsrc_update __user *arg = __arg;
264 struct io_uring_rsrc_update reg;
265 struct io_uring_task *tctx;
268 if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
271 mutex_unlock(&ctx->uring_lock);
272 ret = __io_uring_add_tctx_node(ctx);
273 mutex_lock(&ctx->uring_lock);
277 tctx = current->io_uring;
278 for (i = 0; i < nr_args; i++) {
281 if (copy_from_user(®, &arg[i], sizeof(reg))) {
291 if (reg.offset == -1U) {
293 end = IO_RINGFD_REG_MAX;
295 if (reg.offset >= IO_RINGFD_REG_MAX) {
303 ret = io_ring_add_registered_fd(tctx, reg.data, start, end);
308 if (copy_to_user(&arg[i], ®, sizeof(reg))) {
309 fput(tctx->registered_rings[reg.offset]);
310 tctx->registered_rings[reg.offset] = NULL;
319 int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg,
322 struct io_uring_rsrc_update __user *arg = __arg;
323 struct io_uring_task *tctx = current->io_uring;
324 struct io_uring_rsrc_update reg;
327 if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
332 for (i = 0; i < nr_args; i++) {
333 if (copy_from_user(®, &arg[i], sizeof(reg))) {
337 if (reg.resv || reg.data || reg.offset >= IO_RINGFD_REG_MAX) {
342 reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX);
343 if (tctx->registered_rings[reg.offset]) {
344 fput(tctx->registered_rings[reg.offset]);
345 tctx->registered_rings[reg.offset] = NULL;