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
;
50 struct io_tctx_node
*node
;
54 * Fault injection forcing allocation errors in the xa_store() path
55 * can lead to xa_empty() returning false, even though no actual
56 * node is stored in the xarray. Until that gets sorted out, attempt
57 * an iteration here and warn if any entries are found.
59 xa_for_each(&tctx
->xa
, index
, node
) {
63 WARN_ON_ONCE(tctx
->io_wq
);
64 WARN_ON_ONCE(tctx
->cached_refs
);
66 percpu_counter_destroy(&tctx
->inflight
);
71 __cold
int io_uring_alloc_task_context(struct task_struct
*task
,
72 struct io_ring_ctx
*ctx
)
74 struct io_uring_task
*tctx
;
77 tctx
= kzalloc(sizeof(*tctx
), GFP_KERNEL
);
81 ret
= percpu_counter_init(&tctx
->inflight
, 0, GFP_KERNEL
);
87 tctx
->io_wq
= io_init_wq_offload(ctx
, task
);
88 if (IS_ERR(tctx
->io_wq
)) {
89 ret
= PTR_ERR(tctx
->io_wq
);
90 percpu_counter_destroy(&tctx
->inflight
);
97 init_waitqueue_head(&tctx
->wait
);
98 atomic_set(&tctx
->in_cancel
, 0);
99 atomic_set(&tctx
->inflight_tracked
, 0);
100 task
->io_uring
= tctx
;
101 init_llist_head(&tctx
->task_list
);
102 init_task_work(&tctx
->task_work
, tctx_task_work
);
106 int __io_uring_add_tctx_node(struct io_ring_ctx
*ctx
)
108 struct io_uring_task
*tctx
= current
->io_uring
;
109 struct io_tctx_node
*node
;
112 if (unlikely(!tctx
)) {
113 ret
= io_uring_alloc_task_context(current
, ctx
);
117 tctx
= current
->io_uring
;
118 if (ctx
->iowq_limits_set
) {
119 unsigned int limits
[2] = { ctx
->iowq_limits
[0],
120 ctx
->iowq_limits
[1], };
122 ret
= io_wq_max_workers(tctx
->io_wq
, limits
);
127 if (!xa_load(&tctx
->xa
, (unsigned long)ctx
)) {
128 node
= kmalloc(sizeof(*node
), GFP_KERNEL
);
132 node
->task
= current
;
134 ret
= xa_err(xa_store(&tctx
->xa
, (unsigned long)ctx
,
141 mutex_lock(&ctx
->uring_lock
);
142 list_add(&node
->ctx_node
, &ctx
->tctx_list
);
143 mutex_unlock(&ctx
->uring_lock
);
148 int __io_uring_add_tctx_node_from_submit(struct io_ring_ctx
*ctx
)
152 if (ctx
->flags
& IORING_SETUP_SINGLE_ISSUER
153 && ctx
->submitter_task
!= current
)
156 ret
= __io_uring_add_tctx_node(ctx
);
160 current
->io_uring
->last
= ctx
;
165 * Remove this io_uring_file -> task mapping.
167 __cold
void io_uring_del_tctx_node(unsigned long index
)
169 struct io_uring_task
*tctx
= current
->io_uring
;
170 struct io_tctx_node
*node
;
174 node
= xa_erase(&tctx
->xa
, index
);
178 WARN_ON_ONCE(current
!= node
->task
);
179 WARN_ON_ONCE(list_empty(&node
->ctx_node
));
181 mutex_lock(&node
->ctx
->uring_lock
);
182 list_del(&node
->ctx_node
);
183 mutex_unlock(&node
->ctx
->uring_lock
);
185 if (tctx
->last
== node
->ctx
)
190 __cold
void io_uring_clean_tctx(struct io_uring_task
*tctx
)
192 struct io_wq
*wq
= tctx
->io_wq
;
193 struct io_tctx_node
*node
;
196 xa_for_each(&tctx
->xa
, index
, node
) {
197 io_uring_del_tctx_node(index
);
202 * Must be after io_uring_del_tctx_node() (removes nodes under
203 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
205 io_wq_put_and_exit(wq
);
210 void io_uring_unreg_ringfd(void)
212 struct io_uring_task
*tctx
= current
->io_uring
;
215 for (i
= 0; i
< IO_RINGFD_REG_MAX
; i
++) {
216 if (tctx
->registered_rings
[i
]) {
217 fput(tctx
->registered_rings
[i
]);
218 tctx
->registered_rings
[i
] = NULL
;
223 int io_ring_add_registered_file(struct io_uring_task
*tctx
, struct file
*file
,
227 for (offset
= start
; offset
< end
; offset
++) {
228 offset
= array_index_nospec(offset
, IO_RINGFD_REG_MAX
);
229 if (tctx
->registered_rings
[offset
])
232 tctx
->registered_rings
[offset
] = file
;
238 static int io_ring_add_registered_fd(struct io_uring_task
*tctx
, int fd
,
247 } else if (!io_is_uring_fops(file
)) {
251 offset
= io_ring_add_registered_file(tctx
, file
, start
, end
);
258 * Register a ring fd to avoid fdget/fdput for each io_uring_enter()
259 * invocation. User passes in an array of struct io_uring_rsrc_update
260 * with ->data set to the ring_fd, and ->offset given for the desired
261 * index. If no index is desired, application may set ->offset == -1U
262 * and we'll find an available index. Returns number of entries
263 * successfully processed, or < 0 on error if none were processed.
265 int io_ringfd_register(struct io_ring_ctx
*ctx
, void __user
*__arg
,
268 struct io_uring_rsrc_update __user
*arg
= __arg
;
269 struct io_uring_rsrc_update reg
;
270 struct io_uring_task
*tctx
;
273 if (!nr_args
|| nr_args
> IO_RINGFD_REG_MAX
)
276 mutex_unlock(&ctx
->uring_lock
);
277 ret
= __io_uring_add_tctx_node(ctx
);
278 mutex_lock(&ctx
->uring_lock
);
282 tctx
= current
->io_uring
;
283 for (i
= 0; i
< nr_args
; i
++) {
286 if (copy_from_user(®
, &arg
[i
], sizeof(reg
))) {
296 if (reg
.offset
== -1U) {
298 end
= IO_RINGFD_REG_MAX
;
300 if (reg
.offset
>= IO_RINGFD_REG_MAX
) {
308 ret
= io_ring_add_registered_fd(tctx
, reg
.data
, start
, end
);
313 if (copy_to_user(&arg
[i
], ®
, sizeof(reg
))) {
314 fput(tctx
->registered_rings
[reg
.offset
]);
315 tctx
->registered_rings
[reg
.offset
] = NULL
;
324 int io_ringfd_unregister(struct io_ring_ctx
*ctx
, void __user
*__arg
,
327 struct io_uring_rsrc_update __user
*arg
= __arg
;
328 struct io_uring_task
*tctx
= current
->io_uring
;
329 struct io_uring_rsrc_update reg
;
332 if (!nr_args
|| nr_args
> IO_RINGFD_REG_MAX
)
337 for (i
= 0; i
< nr_args
; i
++) {
338 if (copy_from_user(®
, &arg
[i
], sizeof(reg
))) {
342 if (reg
.resv
|| reg
.data
|| reg
.offset
>= IO_RINGFD_REG_MAX
) {
347 reg
.offset
= array_index_nospec(reg
.offset
, IO_RINGFD_REG_MAX
);
348 if (tctx
->registered_rings
[reg
.offset
]) {
349 fput(tctx
->registered_rings
[reg
.offset
]);
350 tctx
->registered_rings
[reg
.offset
] = NULL
;