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