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