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