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