1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/errno.h>
4 #include <linux/file.h>
5 #include <linux/mm.h>
6 #include <linux/slab.h>
7 #include <linux/nospec.h>
8 #include <linux/io_uring.h>
9
10 #include <uapi/linux/io_uring.h>
11
12 #include "io_uring.h"
13 #include "tctx.h"
14 #include "bpf_filter.h"
15
io_init_wq_offload(struct io_ring_ctx * ctx,struct task_struct * task)16 static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
17 struct task_struct *task)
18 {
19 struct io_wq_hash *hash;
20 struct io_wq_data data;
21 unsigned int concurrency;
22
23 mutex_lock(&ctx->uring_lock);
24 hash = ctx->hash_map;
25 if (!hash) {
26 hash = kzalloc_obj(*hash);
27 if (!hash) {
28 mutex_unlock(&ctx->uring_lock);
29 return ERR_PTR(-ENOMEM);
30 }
31 refcount_set(&hash->refs, 1);
32 init_waitqueue_head(&hash->wait);
33 ctx->hash_map = hash;
34 }
35 mutex_unlock(&ctx->uring_lock);
36
37 data.hash = hash;
38 data.task = task;
39
40 /* Do QD, or 4 * CPUS, whatever is smallest */
41 concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
42
43 return io_wq_create(concurrency, &data);
44 }
45
io_uring_free_tctx(struct task_struct * tsk)46 void io_uring_free_tctx(struct task_struct *tsk)
47 {
48 struct io_uring_task *tctx = tsk->io_uring;
49 struct io_tctx_node *node;
50 unsigned long index;
51
52 /*
53 * Fault injection forcing allocation errors in the xa_store() path
54 * can lead to xa_empty() returning false, even though no actual
55 * node is stored in the xarray. Until that gets sorted out, attempt
56 * an iteration here and warn if any entries are found.
57 */
58 if (tctx) {
59 xa_for_each(&tctx->xa, index, node) {
60 WARN_ON_ONCE(1);
61 break;
62 }
63 WARN_ON_ONCE(tctx->io_wq);
64 WARN_ON_ONCE(tctx->cached_refs);
65
66 percpu_counter_destroy(&tctx->inflight);
67 kfree(tctx);
68 tsk->io_uring = NULL;
69 }
70 }
71
__io_uring_free(struct task_struct * tsk)72 void __io_uring_free(struct task_struct *tsk)
73 {
74 io_uring_free_tctx(tsk);
75 if (tsk->io_uring_restrict) {
76 io_put_bpf_filters(tsk->io_uring_restrict);
77 kfree(tsk->io_uring_restrict);
78 tsk->io_uring_restrict = NULL;
79 }
80 }
81
io_uring_alloc_task_context(struct task_struct * task,struct io_ring_ctx * ctx)82 __cold struct io_uring_task *io_uring_alloc_task_context(struct task_struct *task,
83 struct io_ring_ctx *ctx)
84 {
85 struct io_uring_task *tctx;
86 int ret;
87
88 tctx = kzalloc_obj(*tctx);
89 if (unlikely(!tctx))
90 return ERR_PTR(-ENOMEM);
91
92 ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
93 if (unlikely(ret)) {
94 kfree(tctx);
95 return ERR_PTR(ret);
96 }
97
98 tctx->io_wq = io_init_wq_offload(ctx, task);
99 if (IS_ERR(tctx->io_wq)) {
100 ret = PTR_ERR(tctx->io_wq);
101 percpu_counter_destroy(&tctx->inflight);
102 kfree(tctx);
103 return ERR_PTR(ret);
104 }
105
106 tctx->task = task;
107 xa_init(&tctx->xa);
108 init_waitqueue_head(&tctx->wait);
109 atomic_set(&tctx->in_cancel, 0);
110 atomic_set(&tctx->inflight_tracked, 0);
111 mpscq_init(&tctx->task_list, &tctx->task_head);
112 INIT_WORK(&tctx->fallback_work, io_tctx_fallback_work);
113 init_task_work(&tctx->task_work, tctx_task_work);
114 return tctx;
115 }
116
io_tctx_install_node(struct io_ring_ctx * ctx,struct io_uring_task * tctx)117 static int io_tctx_install_node(struct io_ring_ctx *ctx,
118 struct io_uring_task *tctx)
119 {
120 struct io_tctx_node *node;
121 int ret;
122
123 if (xa_load(&tctx->xa, (unsigned long)ctx))
124 return 0;
125
126 node = kmalloc_obj(*node);
127 if (!node)
128 return -ENOMEM;
129 node->ctx = ctx;
130 node->task = current;
131
132 ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
133 node, GFP_KERNEL));
134 if (ret) {
135 kfree(node);
136 return ret;
137 }
138
139 mutex_lock(&ctx->tctx_lock);
140 list_add(&node->ctx_node, &ctx->tctx_list);
141 mutex_unlock(&ctx->tctx_lock);
142 return 0;
143 }
144
__io_uring_add_tctx_node(struct io_ring_ctx * ctx)145 int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
146 {
147 struct io_uring_task *tctx = current->io_uring;
148 bool new_tctx = false;
149 int ret;
150
151 if (unlikely(!tctx)) {
152 tctx = io_uring_alloc_task_context(current, ctx);
153 if (IS_ERR(tctx))
154 return PTR_ERR(tctx);
155 new_tctx = true;
156
157 if (data_race(ctx->int_flags) & IO_RING_F_IOWQ_LIMITS_SET) {
158 unsigned int limits[2];
159
160 mutex_lock(&ctx->uring_lock);
161 limits[0] = ctx->iowq_limits[0];
162 limits[1] = ctx->iowq_limits[1];
163 mutex_unlock(&ctx->uring_lock);
164
165 ret = io_wq_max_workers(tctx->io_wq, limits);
166 if (ret)
167 goto err_free;
168 }
169 }
170
171 /*
172 * Re-activate io-wq keepalive on any new io_uring usage. The wq may have
173 * been marked for idle-exit when the task temporarily had no active
174 * io_uring instances.
175 */
176 if (tctx->io_wq)
177 io_wq_set_exit_on_idle(tctx->io_wq, false);
178
179 if (new_tctx)
180 current->io_uring = tctx;
181
182 ret = io_tctx_install_node(ctx, tctx);
183 if (!ret)
184 return 0;
185 err_free:
186 if (new_tctx) {
187 current->io_uring = NULL;
188 if (tctx->io_wq) {
189 io_wq_exit_start(tctx->io_wq);
190 io_wq_put_and_exit(tctx->io_wq);
191 }
192 percpu_counter_destroy(&tctx->inflight);
193 kfree(tctx);
194 }
195 return ret;
196 }
197
__io_uring_add_tctx_node_from_submit(struct io_ring_ctx * ctx)198 int __io_uring_add_tctx_node_from_submit(struct io_ring_ctx *ctx)
199 {
200 int ret;
201
202 if (ctx->flags & IORING_SETUP_SINGLE_ISSUER
203 && ctx->submitter_task != current)
204 return -EEXIST;
205
206 ret = __io_uring_add_tctx_node(ctx);
207 if (ret)
208 return ret;
209
210 current->io_uring->last = ctx;
211 return 0;
212 }
213
214 /*
215 * Remove this io_uring_file -> task mapping.
216 */
io_uring_del_tctx_node(unsigned long index)217 __cold void io_uring_del_tctx_node(unsigned long index)
218 {
219 struct io_uring_task *tctx = current->io_uring;
220 struct io_tctx_node *node;
221
222 if (!tctx)
223 return;
224 node = xa_erase(&tctx->xa, index);
225 if (!node)
226 return;
227
228 WARN_ON_ONCE(current != node->task);
229 WARN_ON_ONCE(list_empty(&node->ctx_node));
230
231 mutex_lock(&node->ctx->tctx_lock);
232 list_del(&node->ctx_node);
233 mutex_unlock(&node->ctx->tctx_lock);
234
235 if (tctx->last == node->ctx)
236 tctx->last = NULL;
237 kfree(node);
238
239 if (xa_empty(&tctx->xa) && tctx->io_wq)
240 io_wq_set_exit_on_idle(tctx->io_wq, true);
241 }
242
io_uring_clean_tctx(struct io_uring_task * tctx)243 __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
244 {
245 struct io_wq *wq = tctx->io_wq;
246 struct io_tctx_node *node;
247 unsigned long index;
248
249 xa_for_each(&tctx->xa, index, node) {
250 io_uring_del_tctx_node(index);
251 cond_resched();
252 }
253 if (wq) {
254 /*
255 * Must be after io_uring_del_tctx_node() (removes nodes under
256 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
257 */
258 io_wq_put_and_exit(wq);
259 tctx->io_wq = NULL;
260 }
261 }
262
io_uring_unreg_ringfd(void)263 void io_uring_unreg_ringfd(void)
264 {
265 struct io_uring_task *tctx = current->io_uring;
266 int i;
267
268 for (i = 0; i < IO_RINGFD_REG_MAX; i++) {
269 if (tctx->registered_rings[i]) {
270 fput(tctx->registered_rings[i]);
271 tctx->registered_rings[i] = NULL;
272 }
273 }
274 }
275
io_ring_add_registered_file(struct io_uring_task * tctx,struct file * file,int start,int end)276 int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file,
277 int start, int end)
278 {
279 int offset, idx;
280 for (offset = start; offset < end; offset++) {
281 idx = array_index_nospec(offset, IO_RINGFD_REG_MAX);
282 if (tctx->registered_rings[idx])
283 continue;
284
285 tctx->registered_rings[idx] = file;
286 return idx;
287 }
288 return -EBUSY;
289 }
290
io_ring_add_registered_fd(struct io_uring_task * tctx,int fd,int start,int end)291 static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd,
292 int start, int end)
293 {
294 struct file *file;
295 int offset;
296
297 file = fget(fd);
298 if (!file) {
299 return -EBADF;
300 } else if (!io_is_uring_fops(file)) {
301 fput(file);
302 return -EOPNOTSUPP;
303 }
304 offset = io_ring_add_registered_file(tctx, file, start, end);
305 if (offset < 0)
306 fput(file);
307 return offset;
308 }
309
310 /*
311 * Register a ring fd to avoid fdget/fdput for each io_uring_enter()
312 * invocation. User passes in an array of struct io_uring_rsrc_update
313 * with ->data set to the ring_fd, and ->offset given for the desired
314 * index. If no index is desired, application may set ->offset == -1U
315 * and we'll find an available index. Returns number of entries
316 * successfully processed, or < 0 on error if none were processed.
317 */
io_ringfd_register(struct io_ring_ctx * ctx,void __user * __arg,unsigned nr_args)318 int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg,
319 unsigned nr_args)
320 {
321 struct io_uring_rsrc_update __user *arg = __arg;
322 struct io_uring_rsrc_update reg;
323 struct io_uring_task *tctx;
324 int ret, i;
325
326 if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
327 return -EINVAL;
328
329 mutex_unlock(&ctx->uring_lock);
330 ret = __io_uring_add_tctx_node(ctx);
331 mutex_lock(&ctx->uring_lock);
332 if (ret)
333 return ret;
334
335 tctx = current->io_uring;
336 for (i = 0; i < nr_args; i++) {
337 int start, end;
338
339 if (copy_from_user(®, &arg[i], sizeof(reg))) {
340 ret = -EFAULT;
341 break;
342 }
343
344 if (reg.resv) {
345 ret = -EINVAL;
346 break;
347 }
348
349 if (reg.offset == -1U) {
350 start = 0;
351 end = IO_RINGFD_REG_MAX;
352 } else {
353 if (reg.offset >= IO_RINGFD_REG_MAX) {
354 ret = -EINVAL;
355 break;
356 }
357 start = reg.offset;
358 end = start + 1;
359 }
360
361 ret = io_ring_add_registered_fd(tctx, reg.data, start, end);
362 if (ret < 0)
363 break;
364
365 reg.offset = ret;
366 if (copy_to_user(&arg[i], ®, sizeof(reg))) {
367 fput(tctx->registered_rings[reg.offset]);
368 tctx->registered_rings[reg.offset] = NULL;
369 ret = -EFAULT;
370 break;
371 }
372 }
373
374 return i ? i : ret;
375 }
376
io_ringfd_unregister(struct io_ring_ctx * ctx,void __user * __arg,unsigned nr_args)377 int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg,
378 unsigned nr_args)
379 {
380 struct io_uring_rsrc_update __user *arg = __arg;
381 struct io_uring_task *tctx = current->io_uring;
382 struct io_uring_rsrc_update reg;
383 int ret = 0, i;
384
385 if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
386 return -EINVAL;
387 if (!tctx)
388 return 0;
389
390 for (i = 0; i < nr_args; i++) {
391 if (copy_from_user(®, &arg[i], sizeof(reg))) {
392 ret = -EFAULT;
393 break;
394 }
395 if (reg.resv || reg.data || reg.offset >= IO_RINGFD_REG_MAX) {
396 ret = -EINVAL;
397 break;
398 }
399
400 reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX);
401 if (tctx->registered_rings[reg.offset]) {
402 fput(tctx->registered_rings[reg.offset]);
403 tctx->registered_rings[reg.offset] = NULL;
404 }
405 }
406
407 return i ? i : ret;
408 }
409
__io_uring_fork(struct task_struct * tsk)410 int __io_uring_fork(struct task_struct *tsk)
411 {
412 struct io_restriction *res, *src = tsk->io_uring_restrict;
413
414 /* Don't leave it dangling on error */
415 tsk->io_uring_restrict = NULL;
416
417 res = kzalloc_obj(*res, GFP_KERNEL_ACCOUNT);
418 if (!res)
419 return -ENOMEM;
420
421 tsk->io_uring_restrict = res;
422 io_restriction_clone(res, src);
423 return 0;
424 }
425