xref: /linux-6.15/io_uring/tctx.c (revision 97c96e9f)
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 
15 static struct io_wq *io_init_wq_offload(struct io_ring_ctx *ctx,
16 					struct task_struct *task)
17 {
18 	struct io_wq_hash *hash;
19 	struct io_wq_data data;
20 	unsigned int concurrency;
21 
22 	mutex_lock(&ctx->uring_lock);
23 	hash = ctx->hash_map;
24 	if (!hash) {
25 		hash = kzalloc(sizeof(*hash), GFP_KERNEL);
26 		if (!hash) {
27 			mutex_unlock(&ctx->uring_lock);
28 			return ERR_PTR(-ENOMEM);
29 		}
30 		refcount_set(&hash->refs, 1);
31 		init_waitqueue_head(&hash->wait);
32 		ctx->hash_map = hash;
33 	}
34 	mutex_unlock(&ctx->uring_lock);
35 
36 	data.hash = hash;
37 	data.task = task;
38 	data.free_work = io_wq_free_work;
39 	data.do_work = io_wq_submit_work;
40 
41 	/* Do QD, or 4 * CPUS, whatever is smallest */
42 	concurrency = min(ctx->sq_entries, 4 * num_online_cpus());
43 
44 	return io_wq_create(concurrency, &data);
45 }
46 
47 void __io_uring_free(struct task_struct *tsk)
48 {
49 	struct io_uring_task *tctx = tsk->io_uring;
50 
51 	WARN_ON_ONCE(!xa_empty(&tctx->xa));
52 	WARN_ON_ONCE(tctx->io_wq);
53 	WARN_ON_ONCE(tctx->cached_refs);
54 
55 	percpu_counter_destroy(&tctx->inflight);
56 	kfree(tctx);
57 	tsk->io_uring = NULL;
58 }
59 
60 __cold int io_uring_alloc_task_context(struct task_struct *task,
61 				       struct io_ring_ctx *ctx)
62 {
63 	struct io_uring_task *tctx;
64 	int ret;
65 
66 	tctx = kzalloc(sizeof(*tctx), GFP_KERNEL);
67 	if (unlikely(!tctx))
68 		return -ENOMEM;
69 
70 	ret = percpu_counter_init(&tctx->inflight, 0, GFP_KERNEL);
71 	if (unlikely(ret)) {
72 		kfree(tctx);
73 		return ret;
74 	}
75 
76 	tctx->io_wq = io_init_wq_offload(ctx, task);
77 	if (IS_ERR(tctx->io_wq)) {
78 		ret = PTR_ERR(tctx->io_wq);
79 		percpu_counter_destroy(&tctx->inflight);
80 		kfree(tctx);
81 		return ret;
82 	}
83 
84 	xa_init(&tctx->xa);
85 	init_waitqueue_head(&tctx->wait);
86 	atomic_set(&tctx->in_idle, 0);
87 	atomic_set(&tctx->inflight_tracked, 0);
88 	task->io_uring = tctx;
89 	init_llist_head(&tctx->task_list);
90 	init_task_work(&tctx->task_work, tctx_task_work);
91 	return 0;
92 }
93 
94 static int io_register_submitter(struct io_ring_ctx *ctx)
95 {
96 	int ret = 0;
97 
98 	mutex_lock(&ctx->uring_lock);
99 	if (!ctx->submitter_task)
100 		ctx->submitter_task = get_task_struct(current);
101 	else if (ctx->submitter_task != current)
102 		ret = -EEXIST;
103 	mutex_unlock(&ctx->uring_lock);
104 
105 	return ret;
106 }
107 
108 int __io_uring_add_tctx_node(struct io_ring_ctx *ctx)
109 {
110 	struct io_uring_task *tctx = current->io_uring;
111 	struct io_tctx_node *node;
112 	int ret;
113 
114 	if (unlikely(!tctx)) {
115 		ret = io_uring_alloc_task_context(current, ctx);
116 		if (unlikely(ret))
117 			return ret;
118 
119 		tctx = current->io_uring;
120 		if (ctx->iowq_limits_set) {
121 			unsigned int limits[2] = { ctx->iowq_limits[0],
122 						   ctx->iowq_limits[1], };
123 
124 			ret = io_wq_max_workers(tctx->io_wq, limits);
125 			if (ret)
126 				return ret;
127 		}
128 	}
129 	if (!xa_load(&tctx->xa, (unsigned long)ctx)) {
130 		node = kmalloc(sizeof(*node), GFP_KERNEL);
131 		if (!node)
132 			return -ENOMEM;
133 		node->ctx = ctx;
134 		node->task = current;
135 
136 		ret = xa_err(xa_store(&tctx->xa, (unsigned long)ctx,
137 					node, GFP_KERNEL));
138 		if (ret) {
139 			kfree(node);
140 			return ret;
141 		}
142 
143 		mutex_lock(&ctx->uring_lock);
144 		list_add(&node->ctx_node, &ctx->tctx_list);
145 		mutex_unlock(&ctx->uring_lock);
146 	}
147 	return 0;
148 }
149 
150 int __io_uring_add_tctx_node_from_submit(struct io_ring_ctx *ctx)
151 {
152 	int ret;
153 
154 	if (ctx->flags & IORING_SETUP_SINGLE_ISSUER) {
155 		ret = io_register_submitter(ctx);
156 		if (ret)
157 			return ret;
158 	}
159 
160 	ret = __io_uring_add_tctx_node(ctx);
161 	if (ret)
162 		return ret;
163 
164 	current->io_uring->last = ctx;
165 	return 0;
166 }
167 
168 /*
169  * Remove this io_uring_file -> task mapping.
170  */
171 __cold void io_uring_del_tctx_node(unsigned long index)
172 {
173 	struct io_uring_task *tctx = current->io_uring;
174 	struct io_tctx_node *node;
175 
176 	if (!tctx)
177 		return;
178 	node = xa_erase(&tctx->xa, index);
179 	if (!node)
180 		return;
181 
182 	WARN_ON_ONCE(current != node->task);
183 	WARN_ON_ONCE(list_empty(&node->ctx_node));
184 
185 	mutex_lock(&node->ctx->uring_lock);
186 	list_del(&node->ctx_node);
187 	mutex_unlock(&node->ctx->uring_lock);
188 
189 	if (tctx->last == node->ctx)
190 		tctx->last = NULL;
191 	kfree(node);
192 }
193 
194 __cold void io_uring_clean_tctx(struct io_uring_task *tctx)
195 {
196 	struct io_wq *wq = tctx->io_wq;
197 	struct io_tctx_node *node;
198 	unsigned long index;
199 
200 	xa_for_each(&tctx->xa, index, node) {
201 		io_uring_del_tctx_node(index);
202 		cond_resched();
203 	}
204 	if (wq) {
205 		/*
206 		 * Must be after io_uring_del_tctx_node() (removes nodes under
207 		 * uring_lock) to avoid race with io_uring_try_cancel_iowq().
208 		 */
209 		io_wq_put_and_exit(wq);
210 		tctx->io_wq = NULL;
211 	}
212 }
213 
214 void io_uring_unreg_ringfd(void)
215 {
216 	struct io_uring_task *tctx = current->io_uring;
217 	int i;
218 
219 	for (i = 0; i < IO_RINGFD_REG_MAX; i++) {
220 		if (tctx->registered_rings[i]) {
221 			fput(tctx->registered_rings[i]);
222 			tctx->registered_rings[i] = NULL;
223 		}
224 	}
225 }
226 
227 static int io_ring_add_registered_fd(struct io_uring_task *tctx, int fd,
228 				     int start, int end)
229 {
230 	struct file *file;
231 	int offset;
232 
233 	for (offset = start; offset < end; offset++) {
234 		offset = array_index_nospec(offset, IO_RINGFD_REG_MAX);
235 		if (tctx->registered_rings[offset])
236 			continue;
237 
238 		file = fget(fd);
239 		if (!file) {
240 			return -EBADF;
241 		} else if (!io_is_uring_fops(file)) {
242 			fput(file);
243 			return -EOPNOTSUPP;
244 		}
245 		tctx->registered_rings[offset] = file;
246 		return offset;
247 	}
248 
249 	return -EBUSY;
250 }
251 
252 /*
253  * Register a ring fd to avoid fdget/fdput for each io_uring_enter()
254  * invocation. User passes in an array of struct io_uring_rsrc_update
255  * with ->data set to the ring_fd, and ->offset given for the desired
256  * index. If no index is desired, application may set ->offset == -1U
257  * and we'll find an available index. Returns number of entries
258  * successfully processed, or < 0 on error if none were processed.
259  */
260 int io_ringfd_register(struct io_ring_ctx *ctx, void __user *__arg,
261 		       unsigned nr_args)
262 {
263 	struct io_uring_rsrc_update __user *arg = __arg;
264 	struct io_uring_rsrc_update reg;
265 	struct io_uring_task *tctx;
266 	int ret, i;
267 
268 	if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
269 		return -EINVAL;
270 
271 	mutex_unlock(&ctx->uring_lock);
272 	ret = __io_uring_add_tctx_node(ctx);
273 	mutex_lock(&ctx->uring_lock);
274 	if (ret)
275 		return ret;
276 
277 	tctx = current->io_uring;
278 	for (i = 0; i < nr_args; i++) {
279 		int start, end;
280 
281 		if (copy_from_user(&reg, &arg[i], sizeof(reg))) {
282 			ret = -EFAULT;
283 			break;
284 		}
285 
286 		if (reg.resv) {
287 			ret = -EINVAL;
288 			break;
289 		}
290 
291 		if (reg.offset == -1U) {
292 			start = 0;
293 			end = IO_RINGFD_REG_MAX;
294 		} else {
295 			if (reg.offset >= IO_RINGFD_REG_MAX) {
296 				ret = -EINVAL;
297 				break;
298 			}
299 			start = reg.offset;
300 			end = start + 1;
301 		}
302 
303 		ret = io_ring_add_registered_fd(tctx, reg.data, start, end);
304 		if (ret < 0)
305 			break;
306 
307 		reg.offset = ret;
308 		if (copy_to_user(&arg[i], &reg, sizeof(reg))) {
309 			fput(tctx->registered_rings[reg.offset]);
310 			tctx->registered_rings[reg.offset] = NULL;
311 			ret = -EFAULT;
312 			break;
313 		}
314 	}
315 
316 	return i ? i : ret;
317 }
318 
319 int io_ringfd_unregister(struct io_ring_ctx *ctx, void __user *__arg,
320 			 unsigned nr_args)
321 {
322 	struct io_uring_rsrc_update __user *arg = __arg;
323 	struct io_uring_task *tctx = current->io_uring;
324 	struct io_uring_rsrc_update reg;
325 	int ret = 0, i;
326 
327 	if (!nr_args || nr_args > IO_RINGFD_REG_MAX)
328 		return -EINVAL;
329 	if (!tctx)
330 		return 0;
331 
332 	for (i = 0; i < nr_args; i++) {
333 		if (copy_from_user(&reg, &arg[i], sizeof(reg))) {
334 			ret = -EFAULT;
335 			break;
336 		}
337 		if (reg.resv || reg.data || reg.offset >= IO_RINGFD_REG_MAX) {
338 			ret = -EINVAL;
339 			break;
340 		}
341 
342 		reg.offset = array_index_nospec(reg.offset, IO_RINGFD_REG_MAX);
343 		if (tctx->registered_rings[reg.offset]) {
344 			fput(tctx->registered_rings[reg.offset]);
345 			tctx->registered_rings[reg.offset] = NULL;
346 		}
347 	}
348 
349 	return i ? i : ret;
350 }
351