xref: /linux/io_uring/tctx.c (revision a905ff43362e80590e54f6ea9ddb2d1a998aa01c)
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 
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 
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_rcu(tctx, rcu);
68 		tsk->io_uring = NULL;
69 	}
70 }
71 
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 
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_flags(&tctx->xa, XA_FLAGS_ACCOUNT);
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 
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, GFP_KERNEL_ACCOUNT);
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 
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 		xa_destroy(&tctx->xa);
193 		percpu_counter_destroy(&tctx->inflight);
194 		kfree(tctx);
195 	}
196 	return ret;
197 }
198 
199 int __io_uring_add_tctx_node_from_submit(struct io_ring_ctx *ctx)
200 {
201 	int ret;
202 
203 	if (ctx->flags & IORING_SETUP_SINGLE_ISSUER
204 	    && ctx->submitter_task != current)
205 		return -EEXIST;
206 
207 	ret = __io_uring_add_tctx_node(ctx);
208 	if (ret)
209 		return ret;
210 
211 	current->io_uring->last = ctx;
212 	return 0;
213 }
214 
215 /*
216  * Remove this io_uring_file -> task mapping.
217  */
218 __cold void io_uring_del_tctx_node(unsigned long index)
219 {
220 	struct io_uring_task *tctx = current->io_uring;
221 	struct io_tctx_node *node;
222 
223 	if (!tctx)
224 		return;
225 	node = xa_erase(&tctx->xa, index);
226 	if (!node)
227 		return;
228 
229 	WARN_ON_ONCE(current != node->task);
230 	WARN_ON_ONCE(list_empty(&node->ctx_node));
231 
232 	mutex_lock(&node->ctx->tctx_lock);
233 	list_del(&node->ctx_node);
234 	/* keeps the ctx until the unlock is done, see io_ring_exit_work() */
235 	rcu_read_lock();
236 	mutex_unlock(&node->ctx->tctx_lock);
237 	rcu_read_unlock();
238 
239 	if (tctx->last == node->ctx)
240 		tctx->last = NULL;
241 	kfree(node);
242 
243 	if (xa_empty(&tctx->xa) && tctx->io_wq)
244 		io_wq_set_exit_on_idle(tctx->io_wq, true);
245 }
246 
247 __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
248 {
249 	struct io_wq *wq = tctx->io_wq;
250 	struct io_tctx_node *node;
251 	unsigned long index;
252 
253 	xa_for_each(&tctx->xa, index, node) {
254 		io_uring_del_tctx_node(index);
255 		cond_resched();
256 	}
257 	/* A failed xa_store() leaves nodes with no entry behind. */
258 	xa_destroy(&tctx->xa);
259 	if (wq) {
260 		/*
261 		 * Must be after io_uring_del_tctx_node() (removes nodes under
262 		 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
263 		 */
264 		io_wq_put_and_exit(wq);
265 		tctx->io_wq = NULL;
266 	}
267 }
268 
269 void io_uring_unreg_ringfd(void)
270 {
271 	struct io_uring_task *tctx = current->io_uring;
272 	int i;
273 
274 	for (i = 0; i < IO_RINGFD_REG_MAX; i++) {
275 		if (tctx->registered_rings[i]) {
276 			fput(tctx->registered_rings[i]);
277 			tctx->registered_rings[i] = NULL;
278 		}
279 	}
280 }
281 
282 int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file,
283 				     int start, int end)
284 {
285 	int offset, idx;
286 	for (offset = start; offset < end; offset++) {
287 		idx = array_index_nospec(offset, IO_RINGFD_REG_MAX);
288 		if (tctx->registered_rings[idx])
289 			continue;
290 
291 		tctx->registered_rings[idx] = file;
292 		return idx;
293 	}
294 	return -EBUSY;
295 }
296 
297 static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd,
298 				     int start, int end)
299 {
300 	struct file *file;
301 	int offset;
302 
303 	file = fget(fd);
304 	if (!file) {
305 		return -EBADF;
306 	} else if (!io_is_uring_fops(file)) {
307 		fput(file);
308 		return -EOPNOTSUPP;
309 	}
310 	offset = io_ring_add_registered_file(tctx, file, start, end);
311 	if (offset < 0)
312 		fput(file);
313 	return offset;
314 }
315 
316 /*
317  * Register a ring fd to avoid fdget/fdput for each io_uring_enter()
318  * invocation. User passes in an array of struct io_uring_rsrc_update
319  * with ->data set to the ring_fd, and ->offset given for the desired
320  * index. If no index is desired, application may set ->offset == -1U
321  * and we'll find an available index. Returns number of entries
322  * successfully processed, or < 0 on error if none were processed.
323  */
324 int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg,
325 		       unsigned nr_args)
326 {
327 	struct io_uring_rsrc_update __user *arg = __arg;
328 	struct io_uring_rsrc_update reg;
329 	struct io_uring_task *tctx;
330 	int ret, i;
331 
332 	if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
333 		return -EINVAL;
334 
335 	mutex_unlock(&ctx->uring_lock);
336 	ret = __io_uring_add_tctx_node(ctx);
337 	mutex_lock(&ctx->uring_lock);
338 	if (ret)
339 		return ret;
340 
341 	tctx = current->io_uring;
342 	for (i = 0; i < nr_args; i++) {
343 		int start, end;
344 
345 		if (copy_from_user(&reg, &arg[i], sizeof(reg))) {
346 			ret = -EFAULT;
347 			break;
348 		}
349 
350 		if (reg.resv) {
351 			ret = -EINVAL;
352 			break;
353 		}
354 
355 		if (reg.offset == -1U) {
356 			start = 0;
357 			end = IO_RINGFD_REG_MAX;
358 		} else {
359 			if (reg.offset >= IO_RINGFD_REG_MAX) {
360 				ret = -EINVAL;
361 				break;
362 			}
363 			start = reg.offset;
364 			end = start + 1;
365 		}
366 
367 		ret = io_ring_add_registered_fd(tctx, reg.data, start, end);
368 		if (ret < 0)
369 			break;
370 
371 		reg.offset = ret;
372 		if (copy_to_user(&arg[i], &reg, sizeof(reg))) {
373 			fput(tctx->registered_rings[reg.offset]);
374 			tctx->registered_rings[reg.offset] = NULL;
375 			ret = -EFAULT;
376 			break;
377 		}
378 	}
379 
380 	return i ? i : ret;
381 }
382 
383 int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg,
384 			 unsigned nr_args)
385 {
386 	struct io_uring_rsrc_update __user *arg = __arg;
387 	struct io_uring_task *tctx = current->io_uring;
388 	struct io_uring_rsrc_update reg;
389 	int ret = 0, i;
390 
391 	if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
392 		return -EINVAL;
393 	if (!tctx)
394 		return 0;
395 
396 	for (i = 0; i < nr_args; i++) {
397 		if (copy_from_user(&reg, &arg[i], sizeof(reg))) {
398 			ret = -EFAULT;
399 			break;
400 		}
401 		if (reg.resv || reg.data || reg.offset >= IO_RINGFD_REG_MAX) {
402 			ret = -EINVAL;
403 			break;
404 		}
405 
406 		reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX);
407 		if (tctx->registered_rings[reg.offset]) {
408 			fput(tctx->registered_rings[reg.offset]);
409 			tctx->registered_rings[reg.offset] = NULL;
410 		}
411 	}
412 
413 	return i ? i : ret;
414 }
415 
416 int __io_uring_fork(struct task_struct *tsk)
417 {
418 	struct io_restriction *res, *src = tsk->io_uring_restrict;
419 
420 	/* Don't leave it dangling on error */
421 	tsk->io_uring_restrict = NULL;
422 
423 	res = kzalloc_obj(*res, GFP_KERNEL_ACCOUNT);
424 	if (!res)
425 		return -ENOMEM;
426 
427 	tsk->io_uring_restrict = res;
428 	io_restriction_clone(res, src);
429 	return 0;
430 }
431