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