David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Shared application/kernel submission and completion ring pairs, for |
| 4 | * supporting fast/efficient IO. |
| 5 | * |
| 6 | * A note on the read/write ordering memory barriers that are matched between |
| 7 | * the application and kernel side. |
| 8 | * |
| 9 | * After the application reads the CQ ring tail, it must use an |
| 10 | * appropriate smp_rmb() to pair with the smp_wmb() the kernel uses |
| 11 | * before writing the tail (using smp_load_acquire to read the tail will |
| 12 | * do). It also needs a smp_mb() before updating CQ head (ordering the |
| 13 | * entry load(s) with the head store), pairing with an implicit barrier |
| 14 | * through a control-dependency in io_get_cqring (smp_store_release to |
| 15 | * store head will do). Failure to do so could lead to reading invalid |
| 16 | * CQ entries. |
| 17 | * |
| 18 | * Likewise, the application must use an appropriate smp_wmb() before |
| 19 | * writing the SQ tail (ordering SQ entry stores with the tail store), |
| 20 | * which pairs with smp_load_acquire in io_get_sqring (smp_store_release |
| 21 | * to store the tail will do). And it needs a barrier ordering the SQ |
| 22 | * head load before writing new SQ entries (smp_load_acquire to read |
| 23 | * head will do). |
| 24 | * |
| 25 | * When using the SQ poll thread (IORING_SETUP_SQPOLL), the application |
| 26 | * needs to check the SQ flags for IORING_SQ_NEED_WAKEUP *after* |
| 27 | * updating the SQ tail; a full memory barrier smp_mb() is needed |
| 28 | * between. |
| 29 | * |
| 30 | * Also see the examples in the liburing library: |
| 31 | * |
| 32 | * git://git.kernel.dk/liburing |
| 33 | * |
| 34 | * io_uring also uses READ/WRITE_ONCE() for _any_ store or load that happens |
| 35 | * from data shared between the kernel and application. This is done both |
| 36 | * for ordering purposes, but also to ensure that once a value is loaded from |
| 37 | * data that the application could potentially modify, it remains stable. |
| 38 | * |
| 39 | * Copyright (C) 2018-2019 Jens Axboe |
| 40 | * Copyright (c) 2018-2019 Christoph Hellwig |
| 41 | */ |
| 42 | #include <linux/kernel.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/syscalls.h> |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/refcount.h> |
| 48 | #include <linux/uio.h> |
| 49 | |
| 50 | #include <linux/sched/signal.h> |
| 51 | #include <linux/fs.h> |
| 52 | #include <linux/file.h> |
| 53 | #include <linux/fdtable.h> |
| 54 | #include <linux/mm.h> |
| 55 | #include <linux/mman.h> |
| 56 | #include <linux/mmu_context.h> |
| 57 | #include <linux/percpu.h> |
| 58 | #include <linux/slab.h> |
| 59 | #include <linux/workqueue.h> |
| 60 | #include <linux/kthread.h> |
| 61 | #include <linux/blkdev.h> |
| 62 | #include <linux/bvec.h> |
| 63 | #include <linux/net.h> |
| 64 | #include <net/sock.h> |
| 65 | #include <net/af_unix.h> |
| 66 | #include <net/scm.h> |
| 67 | #include <linux/anon_inodes.h> |
| 68 | #include <linux/sched/mm.h> |
| 69 | #include <linux/uaccess.h> |
| 70 | #include <linux/nospec.h> |
| 71 | #include <linux/sizes.h> |
| 72 | #include <linux/hugetlb.h> |
| 73 | |
| 74 | #include <uapi/linux/io_uring.h> |
| 75 | |
| 76 | #include "internal.h" |
| 77 | |
| 78 | #define IORING_MAX_ENTRIES 32768 |
| 79 | #define IORING_MAX_FIXED_FILES 1024 |
| 80 | |
| 81 | struct io_uring { |
| 82 | u32 head ____cacheline_aligned_in_smp; |
| 83 | u32 tail ____cacheline_aligned_in_smp; |
| 84 | }; |
| 85 | |
| 86 | /* |
| 87 | * This data is shared with the application through the mmap at offsets |
| 88 | * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING. |
| 89 | * |
| 90 | * The offsets to the member fields are published through struct |
| 91 | * io_sqring_offsets when calling io_uring_setup. |
| 92 | */ |
| 93 | struct io_rings { |
| 94 | /* |
| 95 | * Head and tail offsets into the ring; the offsets need to be |
| 96 | * masked to get valid indices. |
| 97 | * |
| 98 | * The kernel controls head of the sq ring and the tail of the cq ring, |
| 99 | * and the application controls tail of the sq ring and the head of the |
| 100 | * cq ring. |
| 101 | */ |
| 102 | struct io_uring sq, cq; |
| 103 | /* |
| 104 | * Bitmasks to apply to head and tail offsets (constant, equals |
| 105 | * ring_entries - 1) |
| 106 | */ |
| 107 | u32 sq_ring_mask, cq_ring_mask; |
| 108 | /* Ring sizes (constant, power of 2) */ |
| 109 | u32 sq_ring_entries, cq_ring_entries; |
| 110 | /* |
| 111 | * Number of invalid entries dropped by the kernel due to |
| 112 | * invalid index stored in array |
| 113 | * |
| 114 | * Written by the kernel, shouldn't be modified by the |
| 115 | * application (i.e. get number of "new events" by comparing to |
| 116 | * cached value). |
| 117 | * |
| 118 | * After a new SQ head value was read by the application this |
| 119 | * counter includes all submissions that were dropped reaching |
| 120 | * the new SQ head (and possibly more). |
| 121 | */ |
| 122 | u32 sq_dropped; |
| 123 | /* |
| 124 | * Runtime flags |
| 125 | * |
| 126 | * Written by the kernel, shouldn't be modified by the |
| 127 | * application. |
| 128 | * |
| 129 | * The application needs a full memory barrier before checking |
| 130 | * for IORING_SQ_NEED_WAKEUP after updating the sq tail. |
| 131 | */ |
| 132 | u32 sq_flags; |
| 133 | /* |
| 134 | * Number of completion events lost because the queue was full; |
| 135 | * this should be avoided by the application by making sure |
| 136 | * there are not more requests pending thatn there is space in |
| 137 | * the completion queue. |
| 138 | * |
| 139 | * Written by the kernel, shouldn't be modified by the |
| 140 | * application (i.e. get number of "new events" by comparing to |
| 141 | * cached value). |
| 142 | * |
| 143 | * As completion events come in out of order this counter is not |
| 144 | * ordered with any other data. |
| 145 | */ |
| 146 | u32 cq_overflow; |
| 147 | /* |
| 148 | * Ring buffer of completion events. |
| 149 | * |
| 150 | * The kernel writes completion events fresh every time they are |
| 151 | * produced, so the application is allowed to modify pending |
| 152 | * entries. |
| 153 | */ |
| 154 | struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp; |
| 155 | }; |
| 156 | |
| 157 | struct io_mapped_ubuf { |
| 158 | u64 ubuf; |
| 159 | size_t len; |
| 160 | struct bio_vec *bvec; |
| 161 | unsigned int nr_bvecs; |
| 162 | }; |
| 163 | |
| 164 | struct async_list { |
| 165 | spinlock_t lock; |
| 166 | atomic_t cnt; |
| 167 | struct list_head list; |
| 168 | |
| 169 | struct file *file; |
| 170 | off_t io_start; |
| 171 | size_t io_len; |
| 172 | }; |
| 173 | |
| 174 | struct io_ring_ctx { |
| 175 | struct { |
| 176 | struct percpu_ref refs; |
| 177 | } ____cacheline_aligned_in_smp; |
| 178 | |
| 179 | struct { |
| 180 | unsigned int flags; |
| 181 | bool compat; |
| 182 | bool account_mem; |
| 183 | |
| 184 | /* |
| 185 | * Ring buffer of indices into array of io_uring_sqe, which is |
| 186 | * mmapped by the application using the IORING_OFF_SQES offset. |
| 187 | * |
| 188 | * This indirection could e.g. be used to assign fixed |
| 189 | * io_uring_sqe entries to operations and only submit them to |
| 190 | * the queue when needed. |
| 191 | * |
| 192 | * The kernel modifies neither the indices array nor the entries |
| 193 | * array. |
| 194 | */ |
| 195 | u32 *sq_array; |
| 196 | unsigned cached_sq_head; |
| 197 | unsigned sq_entries; |
| 198 | unsigned sq_mask; |
| 199 | unsigned sq_thread_idle; |
| 200 | unsigned cached_sq_dropped; |
| 201 | struct io_uring_sqe *sq_sqes; |
| 202 | |
| 203 | struct list_head defer_list; |
| 204 | struct list_head timeout_list; |
| 205 | } ____cacheline_aligned_in_smp; |
| 206 | |
| 207 | /* IO offload */ |
| 208 | struct workqueue_struct *sqo_wq[2]; |
| 209 | struct task_struct *sqo_thread; /* if using sq thread polling */ |
| 210 | struct mm_struct *sqo_mm; |
| 211 | wait_queue_head_t sqo_wait; |
| 212 | struct completion sqo_thread_started; |
| 213 | |
| 214 | struct { |
| 215 | unsigned cached_cq_tail; |
| 216 | atomic_t cached_cq_overflow; |
| 217 | unsigned cq_entries; |
| 218 | unsigned cq_mask; |
| 219 | struct wait_queue_head cq_wait; |
| 220 | struct fasync_struct *cq_fasync; |
| 221 | struct eventfd_ctx *cq_ev_fd; |
| 222 | atomic_t cq_timeouts; |
| 223 | } ____cacheline_aligned_in_smp; |
| 224 | |
| 225 | struct io_rings *rings; |
| 226 | |
| 227 | /* |
| 228 | * If used, fixed file set. Writers must ensure that ->refs is dead, |
| 229 | * readers must ensure that ->refs is alive as long as the file* is |
| 230 | * used. Only updated through io_uring_register(2). |
| 231 | */ |
| 232 | struct file **user_files; |
| 233 | unsigned nr_user_files; |
| 234 | |
| 235 | /* if used, fixed mapped user buffers */ |
| 236 | unsigned nr_user_bufs; |
| 237 | struct io_mapped_ubuf *user_bufs; |
| 238 | |
| 239 | struct user_struct *user; |
| 240 | |
| 241 | struct cred *creds; |
| 242 | |
| 243 | struct completion ctx_done; |
| 244 | |
| 245 | struct { |
| 246 | struct mutex uring_lock; |
| 247 | wait_queue_head_t wait; |
| 248 | } ____cacheline_aligned_in_smp; |
| 249 | |
| 250 | struct { |
| 251 | spinlock_t completion_lock; |
| 252 | bool poll_multi_file; |
| 253 | /* |
| 254 | * ->poll_list is protected by the ctx->uring_lock for |
| 255 | * io_uring instances that don't use IORING_SETUP_SQPOLL. |
| 256 | * For SQPOLL, only the single threaded io_sq_thread() will |
| 257 | * manipulate the list, hence no extra locking is needed there. |
| 258 | */ |
| 259 | struct list_head poll_list; |
| 260 | struct list_head cancel_list; |
| 261 | } ____cacheline_aligned_in_smp; |
| 262 | |
| 263 | struct async_list pending_async[2]; |
| 264 | |
| 265 | #if defined(CONFIG_UNIX) |
| 266 | struct socket *ring_sock; |
| 267 | #endif |
| 268 | }; |
| 269 | |
| 270 | struct sqe_submit { |
| 271 | const struct io_uring_sqe *sqe; |
| 272 | unsigned short index; |
| 273 | u32 sequence; |
| 274 | bool has_user; |
| 275 | bool needs_lock; |
| 276 | bool needs_fixed_file; |
| 277 | }; |
| 278 | |
| 279 | /* |
| 280 | * First field must be the file pointer in all the |
| 281 | * iocb unions! See also 'struct kiocb' in <linux/fs.h> |
| 282 | */ |
| 283 | struct io_poll_iocb { |
| 284 | struct file *file; |
| 285 | struct wait_queue_head *head; |
| 286 | __poll_t events; |
| 287 | bool done; |
| 288 | bool canceled; |
| 289 | struct wait_queue_entry wait; |
| 290 | }; |
| 291 | |
| 292 | struct io_timeout { |
| 293 | struct file *file; |
| 294 | struct hrtimer timer; |
| 295 | }; |
| 296 | |
| 297 | /* |
| 298 | * NOTE! Each of the iocb union members has the file pointer |
| 299 | * as the first entry in their struct definition. So you can |
| 300 | * access the file pointer through any of the sub-structs, |
| 301 | * or directly as just 'ki_filp' in this struct. |
| 302 | */ |
| 303 | struct io_kiocb { |
| 304 | union { |
| 305 | struct file *file; |
| 306 | struct kiocb rw; |
| 307 | struct io_poll_iocb poll; |
| 308 | struct io_timeout timeout; |
| 309 | }; |
| 310 | |
| 311 | struct sqe_submit submit; |
| 312 | |
| 313 | struct io_ring_ctx *ctx; |
| 314 | struct list_head list; |
| 315 | struct list_head link_list; |
| 316 | unsigned int flags; |
| 317 | refcount_t refs; |
| 318 | #define REQ_F_NOWAIT 1 /* must not punt to workers */ |
| 319 | #define REQ_F_IOPOLL_COMPLETED 2 /* polled IO has completed */ |
| 320 | #define REQ_F_FIXED_FILE 4 /* ctx owns file */ |
| 321 | #define REQ_F_SEQ_PREV 8 /* sequential with previous */ |
| 322 | #define REQ_F_IO_DRAIN 16 /* drain existing IO first */ |
| 323 | #define REQ_F_IO_DRAINED 32 /* drain done */ |
| 324 | #define REQ_F_LINK 64 /* linked sqes */ |
| 325 | #define REQ_F_LINK_DONE 128 /* linked sqes done */ |
| 326 | #define REQ_F_FAIL_LINK 256 /* fail rest of links */ |
| 327 | #define REQ_F_SHADOW_DRAIN 512 /* link-drain shadow req */ |
| 328 | #define REQ_F_TIMEOUT 1024 /* timeout request */ |
| 329 | #define REQ_F_ISREG 2048 /* regular file */ |
| 330 | #define REQ_F_MUST_PUNT 4096 /* must be punted even for NONBLOCK */ |
| 331 | #define REQ_F_TIMEOUT_NOSEQ 8192 /* no timeout sequence */ |
| 332 | u64 user_data; |
| 333 | u32 result; |
| 334 | u32 sequence; |
| 335 | |
| 336 | struct work_struct work; |
| 337 | }; |
| 338 | |
| 339 | #define IO_PLUG_THRESHOLD 2 |
| 340 | #define IO_IOPOLL_BATCH 8 |
| 341 | |
| 342 | struct io_submit_state { |
| 343 | struct blk_plug plug; |
| 344 | |
| 345 | /* |
| 346 | * io_kiocb alloc cache |
| 347 | */ |
| 348 | void *reqs[IO_IOPOLL_BATCH]; |
| 349 | unsigned int free_reqs; |
| 350 | unsigned int cur_req; |
| 351 | |
| 352 | /* |
| 353 | * File reference cache |
| 354 | */ |
| 355 | struct file *file; |
| 356 | unsigned int fd; |
| 357 | unsigned int has_refs; |
| 358 | unsigned int used_refs; |
| 359 | unsigned int ios_left; |
| 360 | }; |
| 361 | |
| 362 | static void io_sq_wq_submit_work(struct work_struct *work); |
| 363 | static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, |
| 364 | long res); |
| 365 | static void __io_free_req(struct io_kiocb *req); |
| 366 | |
| 367 | static struct kmem_cache *req_cachep; |
| 368 | |
| 369 | static const struct file_operations io_uring_fops; |
| 370 | |
| 371 | struct sock *io_uring_get_socket(struct file *file) |
| 372 | { |
| 373 | #if defined(CONFIG_UNIX) |
| 374 | if (file->f_op == &io_uring_fops) { |
| 375 | struct io_ring_ctx *ctx = file->private_data; |
| 376 | |
| 377 | return ctx->ring_sock->sk; |
| 378 | } |
| 379 | #endif |
| 380 | return NULL; |
| 381 | } |
| 382 | EXPORT_SYMBOL(io_uring_get_socket); |
| 383 | |
| 384 | static void io_ring_ctx_ref_free(struct percpu_ref *ref) |
| 385 | { |
| 386 | struct io_ring_ctx *ctx = container_of(ref, struct io_ring_ctx, refs); |
| 387 | |
| 388 | complete(&ctx->ctx_done); |
| 389 | } |
| 390 | |
| 391 | static struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) |
| 392 | { |
| 393 | struct io_ring_ctx *ctx; |
| 394 | int i; |
| 395 | |
| 396 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
| 397 | if (!ctx) |
| 398 | return NULL; |
| 399 | |
| 400 | if (percpu_ref_init(&ctx->refs, io_ring_ctx_ref_free, |
| 401 | PERCPU_REF_ALLOW_REINIT, GFP_KERNEL)) { |
| 402 | kfree(ctx); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | ctx->flags = p->flags; |
| 407 | init_waitqueue_head(&ctx->cq_wait); |
| 408 | init_completion(&ctx->ctx_done); |
| 409 | init_completion(&ctx->sqo_thread_started); |
| 410 | mutex_init(&ctx->uring_lock); |
| 411 | init_waitqueue_head(&ctx->wait); |
| 412 | for (i = 0; i < ARRAY_SIZE(ctx->pending_async); i++) { |
| 413 | spin_lock_init(&ctx->pending_async[i].lock); |
| 414 | INIT_LIST_HEAD(&ctx->pending_async[i].list); |
| 415 | atomic_set(&ctx->pending_async[i].cnt, 0); |
| 416 | } |
| 417 | spin_lock_init(&ctx->completion_lock); |
| 418 | INIT_LIST_HEAD(&ctx->poll_list); |
| 419 | INIT_LIST_HEAD(&ctx->cancel_list); |
| 420 | INIT_LIST_HEAD(&ctx->defer_list); |
| 421 | INIT_LIST_HEAD(&ctx->timeout_list); |
| 422 | return ctx; |
| 423 | } |
| 424 | |
| 425 | static inline bool __io_sequence_defer(struct io_ring_ctx *ctx, |
| 426 | struct io_kiocb *req) |
| 427 | { |
| 428 | return req->sequence != ctx->cached_cq_tail + ctx->cached_sq_dropped |
| 429 | + atomic_read(&ctx->cached_cq_overflow); |
| 430 | } |
| 431 | |
| 432 | static inline bool io_sequence_defer(struct io_ring_ctx *ctx, |
| 433 | struct io_kiocb *req) |
| 434 | { |
| 435 | if ((req->flags & (REQ_F_IO_DRAIN|REQ_F_IO_DRAINED)) != REQ_F_IO_DRAIN) |
| 436 | return false; |
| 437 | |
| 438 | return __io_sequence_defer(ctx, req); |
| 439 | } |
| 440 | |
| 441 | static struct io_kiocb *io_get_deferred_req(struct io_ring_ctx *ctx) |
| 442 | { |
| 443 | struct io_kiocb *req; |
| 444 | |
| 445 | req = list_first_entry_or_null(&ctx->defer_list, struct io_kiocb, list); |
| 446 | if (req && !io_sequence_defer(ctx, req)) { |
| 447 | list_del_init(&req->list); |
| 448 | return req; |
| 449 | } |
| 450 | |
| 451 | return NULL; |
| 452 | } |
| 453 | |
| 454 | static struct io_kiocb *io_get_timeout_req(struct io_ring_ctx *ctx) |
| 455 | { |
| 456 | struct io_kiocb *req; |
| 457 | |
| 458 | req = list_first_entry_or_null(&ctx->timeout_list, struct io_kiocb, list); |
| 459 | if (req) { |
| 460 | if (req->flags & REQ_F_TIMEOUT_NOSEQ) |
| 461 | return NULL; |
| 462 | if (!__io_sequence_defer(ctx, req)) { |
| 463 | list_del_init(&req->list); |
| 464 | return req; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | static void __io_commit_cqring(struct io_ring_ctx *ctx) |
| 472 | { |
| 473 | struct io_rings *rings = ctx->rings; |
| 474 | |
| 475 | if (ctx->cached_cq_tail != READ_ONCE(rings->cq.tail)) { |
| 476 | /* order cqe stores with ring update */ |
| 477 | smp_store_release(&rings->cq.tail, ctx->cached_cq_tail); |
| 478 | |
| 479 | if (wq_has_sleeper(&ctx->cq_wait)) { |
| 480 | wake_up_interruptible(&ctx->cq_wait); |
| 481 | kill_fasync(&ctx->cq_fasync, SIGIO, POLL_IN); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | static inline void io_queue_async_work(struct io_ring_ctx *ctx, |
| 487 | struct io_kiocb *req) |
| 488 | { |
| 489 | int rw = 0; |
| 490 | |
| 491 | if (req->submit.sqe) { |
| 492 | switch (req->submit.sqe->opcode) { |
| 493 | case IORING_OP_WRITEV: |
| 494 | case IORING_OP_WRITE_FIXED: |
| 495 | rw = !(req->rw.ki_flags & IOCB_DIRECT); |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | queue_work(ctx->sqo_wq[rw], &req->work); |
| 501 | } |
| 502 | |
| 503 | static void io_kill_timeout(struct io_kiocb *req) |
| 504 | { |
| 505 | int ret; |
| 506 | |
| 507 | ret = hrtimer_try_to_cancel(&req->timeout.timer); |
| 508 | if (ret != -1) { |
| 509 | atomic_inc(&req->ctx->cq_timeouts); |
| 510 | list_del(&req->list); |
| 511 | io_cqring_fill_event(req->ctx, req->user_data, 0); |
| 512 | __io_free_req(req); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | static void io_kill_timeouts(struct io_ring_ctx *ctx) |
| 517 | { |
| 518 | struct io_kiocb *req, *tmp; |
| 519 | |
| 520 | spin_lock_irq(&ctx->completion_lock); |
| 521 | list_for_each_entry_safe(req, tmp, &ctx->timeout_list, list) |
| 522 | io_kill_timeout(req); |
| 523 | spin_unlock_irq(&ctx->completion_lock); |
| 524 | } |
| 525 | |
| 526 | static void io_commit_cqring(struct io_ring_ctx *ctx) |
| 527 | { |
| 528 | struct io_kiocb *req; |
| 529 | |
| 530 | while ((req = io_get_timeout_req(ctx)) != NULL) |
| 531 | io_kill_timeout(req); |
| 532 | |
| 533 | __io_commit_cqring(ctx); |
| 534 | |
| 535 | while ((req = io_get_deferred_req(ctx)) != NULL) { |
| 536 | if (req->flags & REQ_F_SHADOW_DRAIN) { |
| 537 | /* Just for drain, free it. */ |
| 538 | __io_free_req(req); |
| 539 | continue; |
| 540 | } |
| 541 | req->flags |= REQ_F_IO_DRAINED; |
| 542 | io_queue_async_work(ctx, req); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | static struct io_uring_cqe *io_get_cqring(struct io_ring_ctx *ctx) |
| 547 | { |
| 548 | struct io_rings *rings = ctx->rings; |
| 549 | unsigned tail; |
| 550 | |
| 551 | tail = ctx->cached_cq_tail; |
| 552 | /* |
| 553 | * writes to the cq entry need to come after reading head; the |
| 554 | * control dependency is enough as we're using WRITE_ONCE to |
| 555 | * fill the cq entry |
| 556 | */ |
| 557 | if (tail - READ_ONCE(rings->cq.head) == rings->cq_ring_entries) |
| 558 | return NULL; |
| 559 | |
| 560 | ctx->cached_cq_tail++; |
| 561 | return &rings->cqes[tail & ctx->cq_mask]; |
| 562 | } |
| 563 | |
| 564 | static void io_cqring_fill_event(struct io_ring_ctx *ctx, u64 ki_user_data, |
| 565 | long res) |
| 566 | { |
| 567 | struct io_uring_cqe *cqe; |
| 568 | |
| 569 | /* |
| 570 | * If we can't get a cq entry, userspace overflowed the |
| 571 | * submission (by quite a lot). Increment the overflow count in |
| 572 | * the ring. |
| 573 | */ |
| 574 | cqe = io_get_cqring(ctx); |
| 575 | if (cqe) { |
| 576 | WRITE_ONCE(cqe->user_data, ki_user_data); |
| 577 | WRITE_ONCE(cqe->res, res); |
| 578 | WRITE_ONCE(cqe->flags, 0); |
| 579 | } else { |
| 580 | WRITE_ONCE(ctx->rings->cq_overflow, |
| 581 | atomic_inc_return(&ctx->cached_cq_overflow)); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | static void io_cqring_ev_posted(struct io_ring_ctx *ctx) |
| 586 | { |
| 587 | if (waitqueue_active(&ctx->wait)) |
| 588 | wake_up(&ctx->wait); |
| 589 | if (waitqueue_active(&ctx->sqo_wait)) |
| 590 | wake_up(&ctx->sqo_wait); |
| 591 | if (ctx->cq_ev_fd) |
| 592 | eventfd_signal(ctx->cq_ev_fd, 1); |
| 593 | } |
| 594 | |
| 595 | static void io_cqring_add_event(struct io_ring_ctx *ctx, u64 user_data, |
| 596 | long res) |
| 597 | { |
| 598 | unsigned long flags; |
| 599 | |
| 600 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 601 | io_cqring_fill_event(ctx, user_data, res); |
| 602 | io_commit_cqring(ctx); |
| 603 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 604 | |
| 605 | io_cqring_ev_posted(ctx); |
| 606 | } |
| 607 | |
| 608 | static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, |
| 609 | struct io_submit_state *state) |
| 610 | { |
| 611 | gfp_t gfp = GFP_KERNEL | __GFP_NOWARN; |
| 612 | struct io_kiocb *req; |
| 613 | |
| 614 | if (!percpu_ref_tryget(&ctx->refs)) |
| 615 | return NULL; |
| 616 | |
| 617 | if (!state) { |
| 618 | req = kmem_cache_alloc(req_cachep, gfp); |
| 619 | if (unlikely(!req)) |
| 620 | goto out; |
| 621 | } else if (!state->free_reqs) { |
| 622 | size_t sz; |
| 623 | int ret; |
| 624 | |
| 625 | sz = min_t(size_t, state->ios_left, ARRAY_SIZE(state->reqs)); |
| 626 | ret = kmem_cache_alloc_bulk(req_cachep, gfp, sz, state->reqs); |
| 627 | |
| 628 | /* |
| 629 | * Bulk alloc is all-or-nothing. If we fail to get a batch, |
| 630 | * retry single alloc to be on the safe side. |
| 631 | */ |
| 632 | if (unlikely(ret <= 0)) { |
| 633 | state->reqs[0] = kmem_cache_alloc(req_cachep, gfp); |
| 634 | if (!state->reqs[0]) |
| 635 | goto out; |
| 636 | ret = 1; |
| 637 | } |
| 638 | state->free_reqs = ret - 1; |
| 639 | state->cur_req = 1; |
| 640 | req = state->reqs[0]; |
| 641 | } else { |
| 642 | req = state->reqs[state->cur_req]; |
| 643 | state->free_reqs--; |
| 644 | state->cur_req++; |
| 645 | } |
| 646 | |
| 647 | req->file = NULL; |
| 648 | req->ctx = ctx; |
| 649 | req->flags = 0; |
| 650 | /* one is dropped after submission, the other at completion */ |
| 651 | refcount_set(&req->refs, 2); |
| 652 | req->result = 0; |
| 653 | return req; |
| 654 | out: |
| 655 | percpu_ref_put(&ctx->refs); |
| 656 | return NULL; |
| 657 | } |
| 658 | |
| 659 | static void io_free_req_many(struct io_ring_ctx *ctx, void **reqs, int *nr) |
| 660 | { |
| 661 | if (*nr) { |
| 662 | kmem_cache_free_bulk(req_cachep, *nr, reqs); |
| 663 | percpu_ref_put_many(&ctx->refs, *nr); |
| 664 | *nr = 0; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | static void __io_free_req(struct io_kiocb *req) |
| 669 | { |
| 670 | if (req->file && !(req->flags & REQ_F_FIXED_FILE)) |
| 671 | fput(req->file); |
| 672 | percpu_ref_put(&req->ctx->refs); |
| 673 | kmem_cache_free(req_cachep, req); |
| 674 | } |
| 675 | |
| 676 | static void io_req_link_next(struct io_kiocb *req) |
| 677 | { |
| 678 | struct io_kiocb *nxt; |
| 679 | |
| 680 | /* |
| 681 | * The list should never be empty when we are called here. But could |
| 682 | * potentially happen if the chain is messed up, check to be on the |
| 683 | * safe side. |
| 684 | */ |
| 685 | nxt = list_first_entry_or_null(&req->link_list, struct io_kiocb, list); |
| 686 | if (nxt) { |
| 687 | list_del(&nxt->list); |
| 688 | if (!list_empty(&req->link_list)) { |
| 689 | INIT_LIST_HEAD(&nxt->link_list); |
| 690 | list_splice(&req->link_list, &nxt->link_list); |
| 691 | nxt->flags |= REQ_F_LINK; |
| 692 | } |
| 693 | |
| 694 | nxt->flags |= REQ_F_LINK_DONE; |
| 695 | INIT_WORK(&nxt->work, io_sq_wq_submit_work); |
| 696 | io_queue_async_work(req->ctx, nxt); |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | /* |
| 701 | * Called if REQ_F_LINK is set, and we fail the head request |
| 702 | */ |
| 703 | static void io_fail_links(struct io_kiocb *req) |
| 704 | { |
| 705 | struct io_kiocb *link; |
| 706 | |
| 707 | while (!list_empty(&req->link_list)) { |
| 708 | link = list_first_entry(&req->link_list, struct io_kiocb, list); |
| 709 | list_del(&link->list); |
| 710 | |
| 711 | io_cqring_add_event(req->ctx, link->user_data, -ECANCELED); |
| 712 | __io_free_req(link); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static void io_free_req(struct io_kiocb *req) |
| 717 | { |
| 718 | /* |
| 719 | * If LINK is set, we have dependent requests in this chain. If we |
| 720 | * didn't fail this request, queue the first one up, moving any other |
| 721 | * dependencies to the next request. In case of failure, fail the rest |
| 722 | * of the chain. |
| 723 | */ |
| 724 | if (req->flags & REQ_F_LINK) { |
| 725 | if (req->flags & REQ_F_FAIL_LINK) |
| 726 | io_fail_links(req); |
| 727 | else |
| 728 | io_req_link_next(req); |
| 729 | } |
| 730 | |
| 731 | __io_free_req(req); |
| 732 | } |
| 733 | |
| 734 | static void io_put_req(struct io_kiocb *req) |
| 735 | { |
| 736 | if (refcount_dec_and_test(&req->refs)) |
| 737 | io_free_req(req); |
| 738 | } |
| 739 | |
| 740 | static unsigned io_cqring_events(struct io_rings *rings) |
| 741 | { |
| 742 | /* See comment at the top of this file */ |
| 743 | smp_rmb(); |
| 744 | return READ_ONCE(rings->cq.tail) - READ_ONCE(rings->cq.head); |
| 745 | } |
| 746 | |
| 747 | static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) |
| 748 | { |
| 749 | struct io_rings *rings = ctx->rings; |
| 750 | |
| 751 | /* make sure SQ entry isn't read before tail */ |
| 752 | return smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Find and free completed poll iocbs |
| 757 | */ |
| 758 | static void io_iopoll_complete(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 759 | struct list_head *done) |
| 760 | { |
| 761 | void *reqs[IO_IOPOLL_BATCH]; |
| 762 | struct io_kiocb *req; |
| 763 | int to_free; |
| 764 | |
| 765 | to_free = 0; |
| 766 | while (!list_empty(done)) { |
| 767 | req = list_first_entry(done, struct io_kiocb, list); |
| 768 | list_del(&req->list); |
| 769 | |
| 770 | io_cqring_fill_event(ctx, req->user_data, req->result); |
| 771 | (*nr_events)++; |
| 772 | |
| 773 | if (refcount_dec_and_test(&req->refs)) { |
| 774 | /* If we're not using fixed files, we have to pair the |
| 775 | * completion part with the file put. Use regular |
| 776 | * completions for those, only batch free for fixed |
| 777 | * file and non-linked commands. |
| 778 | */ |
| 779 | if ((req->flags & (REQ_F_FIXED_FILE|REQ_F_LINK)) == |
| 780 | REQ_F_FIXED_FILE) { |
| 781 | reqs[to_free++] = req; |
| 782 | if (to_free == ARRAY_SIZE(reqs)) |
| 783 | io_free_req_many(ctx, reqs, &to_free); |
| 784 | } else { |
| 785 | io_free_req(req); |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | io_commit_cqring(ctx); |
| 791 | io_free_req_many(ctx, reqs, &to_free); |
| 792 | } |
| 793 | |
| 794 | static int io_do_iopoll(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 795 | long min) |
| 796 | { |
| 797 | struct io_kiocb *req, *tmp; |
| 798 | LIST_HEAD(done); |
| 799 | bool spin; |
| 800 | int ret; |
| 801 | |
| 802 | /* |
| 803 | * Only spin for completions if we don't have multiple devices hanging |
| 804 | * off our complete list, and we're under the requested amount. |
| 805 | */ |
| 806 | spin = !ctx->poll_multi_file && *nr_events < min; |
| 807 | |
| 808 | ret = 0; |
| 809 | list_for_each_entry_safe(req, tmp, &ctx->poll_list, list) { |
| 810 | struct kiocb *kiocb = &req->rw; |
| 811 | |
| 812 | /* |
| 813 | * Move completed entries to our local list. If we find a |
| 814 | * request that requires polling, break out and complete |
| 815 | * the done list first, if we have entries there. |
| 816 | */ |
| 817 | if (req->flags & REQ_F_IOPOLL_COMPLETED) { |
| 818 | list_move_tail(&req->list, &done); |
| 819 | continue; |
| 820 | } |
| 821 | if (!list_empty(&done)) |
| 822 | break; |
| 823 | |
| 824 | ret = kiocb->ki_filp->f_op->iopoll(kiocb, spin); |
| 825 | if (ret < 0) |
| 826 | break; |
| 827 | |
| 828 | if (ret && spin) |
| 829 | spin = false; |
| 830 | ret = 0; |
| 831 | } |
| 832 | |
| 833 | if (!list_empty(&done)) |
| 834 | io_iopoll_complete(ctx, nr_events, &done); |
| 835 | |
| 836 | return ret; |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Poll for a mininum of 'min' events. Note that if min == 0 we consider that a |
| 841 | * non-spinning poll check - we'll still enter the driver poll loop, but only |
| 842 | * as a non-spinning completion check. |
| 843 | */ |
| 844 | static int io_iopoll_getevents(struct io_ring_ctx *ctx, unsigned int *nr_events, |
| 845 | long min) |
| 846 | { |
| 847 | while (!list_empty(&ctx->poll_list) && !need_resched()) { |
| 848 | int ret; |
| 849 | |
| 850 | ret = io_do_iopoll(ctx, nr_events, min); |
| 851 | if (ret < 0) |
| 852 | return ret; |
| 853 | if (!min || *nr_events >= min) |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | return 1; |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * We can't just wait for polled events to come to us, we have to actively |
| 862 | * find and complete them. |
| 863 | */ |
| 864 | static void io_iopoll_reap_events(struct io_ring_ctx *ctx) |
| 865 | { |
| 866 | if (!(ctx->flags & IORING_SETUP_IOPOLL)) |
| 867 | return; |
| 868 | |
| 869 | mutex_lock(&ctx->uring_lock); |
| 870 | while (!list_empty(&ctx->poll_list)) { |
| 871 | unsigned int nr_events = 0; |
| 872 | |
| 873 | io_iopoll_getevents(ctx, &nr_events, 1); |
| 874 | |
| 875 | /* |
| 876 | * Ensure we allow local-to-the-cpu processing to take place, |
| 877 | * in this case we need to ensure that we reap all events. |
| 878 | */ |
| 879 | cond_resched(); |
| 880 | } |
| 881 | mutex_unlock(&ctx->uring_lock); |
| 882 | } |
| 883 | |
| 884 | static int __io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 885 | long min) |
| 886 | { |
| 887 | int iters = 0, ret = 0; |
| 888 | |
| 889 | do { |
| 890 | int tmin = 0; |
| 891 | |
| 892 | /* |
| 893 | * Don't enter poll loop if we already have events pending. |
| 894 | * If we do, we can potentially be spinning for commands that |
| 895 | * already triggered a CQE (eg in error). |
| 896 | */ |
| 897 | if (io_cqring_events(ctx->rings)) |
| 898 | break; |
| 899 | |
| 900 | /* |
| 901 | * If a submit got punted to a workqueue, we can have the |
| 902 | * application entering polling for a command before it gets |
| 903 | * issued. That app will hold the uring_lock for the duration |
| 904 | * of the poll right here, so we need to take a breather every |
| 905 | * now and then to ensure that the issue has a chance to add |
| 906 | * the poll to the issued list. Otherwise we can spin here |
| 907 | * forever, while the workqueue is stuck trying to acquire the |
| 908 | * very same mutex. |
| 909 | */ |
| 910 | if (!(++iters & 7)) { |
| 911 | mutex_unlock(&ctx->uring_lock); |
| 912 | mutex_lock(&ctx->uring_lock); |
| 913 | } |
| 914 | |
| 915 | if (*nr_events < min) |
| 916 | tmin = min - *nr_events; |
| 917 | |
| 918 | ret = io_iopoll_getevents(ctx, nr_events, tmin); |
| 919 | if (ret <= 0) |
| 920 | break; |
| 921 | ret = 0; |
| 922 | } while (min && !*nr_events && !need_resched()); |
| 923 | |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | static int io_iopoll_check(struct io_ring_ctx *ctx, unsigned *nr_events, |
| 928 | long min) |
| 929 | { |
| 930 | int ret; |
| 931 | |
| 932 | /* |
| 933 | * We disallow the app entering submit/complete with polling, but we |
| 934 | * still need to lock the ring to prevent racing with polled issue |
| 935 | * that got punted to a workqueue. |
| 936 | */ |
| 937 | mutex_lock(&ctx->uring_lock); |
| 938 | ret = __io_iopoll_check(ctx, nr_events, min); |
| 939 | mutex_unlock(&ctx->uring_lock); |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | static void kiocb_end_write(struct io_kiocb *req) |
| 944 | { |
| 945 | /* |
| 946 | * Tell lockdep we inherited freeze protection from submission |
| 947 | * thread. |
| 948 | */ |
| 949 | if (req->flags & REQ_F_ISREG) { |
| 950 | struct inode *inode = file_inode(req->file); |
| 951 | |
| 952 | __sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE); |
| 953 | } |
| 954 | file_end_write(req->file); |
| 955 | } |
| 956 | |
| 957 | static void io_complete_rw(struct kiocb *kiocb, long res, long res2) |
| 958 | { |
| 959 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
| 960 | |
| 961 | if (kiocb->ki_flags & IOCB_WRITE) |
| 962 | kiocb_end_write(req); |
| 963 | |
| 964 | if ((req->flags & REQ_F_LINK) && res != req->result) |
| 965 | req->flags |= REQ_F_FAIL_LINK; |
| 966 | io_cqring_add_event(req->ctx, req->user_data, res); |
| 967 | io_put_req(req); |
| 968 | } |
| 969 | |
| 970 | static void io_complete_rw_iopoll(struct kiocb *kiocb, long res, long res2) |
| 971 | { |
| 972 | struct io_kiocb *req = container_of(kiocb, struct io_kiocb, rw); |
| 973 | |
| 974 | if (kiocb->ki_flags & IOCB_WRITE) |
| 975 | kiocb_end_write(req); |
| 976 | |
| 977 | if ((req->flags & REQ_F_LINK) && res != req->result) |
| 978 | req->flags |= REQ_F_FAIL_LINK; |
| 979 | req->result = res; |
| 980 | if (res != -EAGAIN) |
| 981 | req->flags |= REQ_F_IOPOLL_COMPLETED; |
| 982 | } |
| 983 | |
| 984 | /* |
| 985 | * After the iocb has been issued, it's safe to be found on the poll list. |
| 986 | * Adding the kiocb to the list AFTER submission ensures that we don't |
| 987 | * find it from a io_iopoll_getevents() thread before the issuer is done |
| 988 | * accessing the kiocb cookie. |
| 989 | */ |
| 990 | static void io_iopoll_req_issued(struct io_kiocb *req) |
| 991 | { |
| 992 | struct io_ring_ctx *ctx = req->ctx; |
| 993 | |
| 994 | /* |
| 995 | * Track whether we have multiple files in our lists. This will impact |
| 996 | * how we do polling eventually, not spinning if we're on potentially |
| 997 | * different devices. |
| 998 | */ |
| 999 | if (list_empty(&ctx->poll_list)) { |
| 1000 | ctx->poll_multi_file = false; |
| 1001 | } else if (!ctx->poll_multi_file) { |
| 1002 | struct io_kiocb *list_req; |
| 1003 | |
| 1004 | list_req = list_first_entry(&ctx->poll_list, struct io_kiocb, |
| 1005 | list); |
| 1006 | if (list_req->rw.ki_filp != req->rw.ki_filp) |
| 1007 | ctx->poll_multi_file = true; |
| 1008 | } |
| 1009 | |
| 1010 | /* |
| 1011 | * For fast devices, IO may have already completed. If it has, add |
| 1012 | * it to the front so we find it first. |
| 1013 | */ |
| 1014 | if (req->flags & REQ_F_IOPOLL_COMPLETED) |
| 1015 | list_add(&req->list, &ctx->poll_list); |
| 1016 | else |
| 1017 | list_add_tail(&req->list, &ctx->poll_list); |
| 1018 | } |
| 1019 | |
| 1020 | static void io_file_put(struct io_submit_state *state) |
| 1021 | { |
| 1022 | if (state->file) { |
| 1023 | int diff = state->has_refs - state->used_refs; |
| 1024 | |
| 1025 | if (diff) |
| 1026 | fput_many(state->file, diff); |
| 1027 | state->file = NULL; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * Get as many references to a file as we have IOs left in this submission, |
| 1033 | * assuming most submissions are for one file, or at least that each file |
| 1034 | * has more than one submission. |
| 1035 | */ |
| 1036 | static struct file *io_file_get(struct io_submit_state *state, int fd) |
| 1037 | { |
| 1038 | if (!state) |
| 1039 | return fget(fd); |
| 1040 | |
| 1041 | if (state->file) { |
| 1042 | if (state->fd == fd) { |
| 1043 | state->used_refs++; |
| 1044 | state->ios_left--; |
| 1045 | return state->file; |
| 1046 | } |
| 1047 | io_file_put(state); |
| 1048 | } |
| 1049 | state->file = fget_many(fd, state->ios_left); |
| 1050 | if (!state->file) |
| 1051 | return NULL; |
| 1052 | |
| 1053 | state->fd = fd; |
| 1054 | state->has_refs = state->ios_left; |
| 1055 | state->used_refs = 1; |
| 1056 | state->ios_left--; |
| 1057 | return state->file; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * If we tracked the file through the SCM inflight mechanism, we could support |
| 1062 | * any file. For now, just ensure that anything potentially problematic is done |
| 1063 | * inline. |
| 1064 | */ |
| 1065 | static bool io_file_supports_async(struct file *file) |
| 1066 | { |
| 1067 | umode_t mode = file_inode(file)->i_mode; |
| 1068 | |
| 1069 | if (S_ISBLK(mode) || S_ISCHR(mode)) |
| 1070 | return true; |
| 1071 | if (S_ISREG(mode) && file->f_op != &io_uring_fops) |
| 1072 | return true; |
| 1073 | |
| 1074 | return false; |
| 1075 | } |
| 1076 | |
| 1077 | static int io_prep_rw(struct io_kiocb *req, const struct sqe_submit *s, |
| 1078 | bool force_nonblock) |
| 1079 | { |
| 1080 | const struct io_uring_sqe *sqe = s->sqe; |
| 1081 | struct io_ring_ctx *ctx = req->ctx; |
| 1082 | struct kiocb *kiocb = &req->rw; |
| 1083 | unsigned ioprio; |
| 1084 | int ret; |
| 1085 | |
| 1086 | if (!req->file) |
| 1087 | return -EBADF; |
| 1088 | |
| 1089 | if (S_ISREG(file_inode(req->file)->i_mode)) |
| 1090 | req->flags |= REQ_F_ISREG; |
| 1091 | |
| 1092 | /* |
| 1093 | * If the file doesn't support async, mark it as REQ_F_MUST_PUNT so |
| 1094 | * we know to async punt it even if it was opened O_NONBLOCK |
| 1095 | */ |
| 1096 | if (force_nonblock && !io_file_supports_async(req->file)) { |
| 1097 | req->flags |= REQ_F_MUST_PUNT; |
| 1098 | return -EAGAIN; |
| 1099 | } |
| 1100 | |
| 1101 | kiocb->ki_pos = READ_ONCE(sqe->off); |
| 1102 | kiocb->ki_flags = iocb_flags(kiocb->ki_filp); |
| 1103 | kiocb->ki_hint = ki_hint_validate(file_write_hint(kiocb->ki_filp)); |
| 1104 | |
| 1105 | ioprio = READ_ONCE(sqe->ioprio); |
| 1106 | if (ioprio) { |
| 1107 | ret = ioprio_check_cap(ioprio); |
| 1108 | if (ret) |
| 1109 | return ret; |
| 1110 | |
| 1111 | kiocb->ki_ioprio = ioprio; |
| 1112 | } else |
| 1113 | kiocb->ki_ioprio = get_current_ioprio(); |
| 1114 | |
| 1115 | ret = kiocb_set_rw_flags(kiocb, READ_ONCE(sqe->rw_flags)); |
| 1116 | if (unlikely(ret)) |
| 1117 | return ret; |
| 1118 | |
| 1119 | /* don't allow async punt if RWF_NOWAIT was requested */ |
| 1120 | if ((kiocb->ki_flags & IOCB_NOWAIT) || |
| 1121 | (req->file->f_flags & O_NONBLOCK)) |
| 1122 | req->flags |= REQ_F_NOWAIT; |
| 1123 | |
| 1124 | if (force_nonblock) |
| 1125 | kiocb->ki_flags |= IOCB_NOWAIT; |
| 1126 | |
| 1127 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 1128 | if (!(kiocb->ki_flags & IOCB_DIRECT) || |
| 1129 | !kiocb->ki_filp->f_op->iopoll) |
| 1130 | return -EOPNOTSUPP; |
| 1131 | |
| 1132 | kiocb->ki_flags |= IOCB_HIPRI; |
| 1133 | kiocb->ki_complete = io_complete_rw_iopoll; |
| 1134 | req->result = 0; |
| 1135 | } else { |
| 1136 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1137 | return -EINVAL; |
| 1138 | kiocb->ki_complete = io_complete_rw; |
| 1139 | } |
| 1140 | return 0; |
| 1141 | } |
| 1142 | |
| 1143 | static inline void io_rw_done(struct kiocb *kiocb, ssize_t ret) |
| 1144 | { |
| 1145 | switch (ret) { |
| 1146 | case -EIOCBQUEUED: |
| 1147 | break; |
| 1148 | case -ERESTARTSYS: |
| 1149 | case -ERESTARTNOINTR: |
| 1150 | case -ERESTARTNOHAND: |
| 1151 | case -ERESTART_RESTARTBLOCK: |
| 1152 | /* |
| 1153 | * We can't just restart the syscall, since previously |
| 1154 | * submitted sqes may already be in progress. Just fail this |
| 1155 | * IO with EINTR. |
| 1156 | */ |
| 1157 | ret = -EINTR; |
| 1158 | /* fall through */ |
| 1159 | default: |
| 1160 | kiocb->ki_complete(kiocb, ret, 0); |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | static int io_import_fixed(struct io_ring_ctx *ctx, int rw, |
| 1165 | const struct io_uring_sqe *sqe, |
| 1166 | struct iov_iter *iter) |
| 1167 | { |
| 1168 | size_t len = READ_ONCE(sqe->len); |
| 1169 | struct io_mapped_ubuf *imu; |
| 1170 | unsigned index, buf_index; |
| 1171 | size_t offset; |
| 1172 | u64 buf_addr; |
| 1173 | |
| 1174 | /* attempt to use fixed buffers without having provided iovecs */ |
| 1175 | if (unlikely(!ctx->user_bufs)) |
| 1176 | return -EFAULT; |
| 1177 | |
| 1178 | buf_index = READ_ONCE(sqe->buf_index); |
| 1179 | if (unlikely(buf_index >= ctx->nr_user_bufs)) |
| 1180 | return -EFAULT; |
| 1181 | |
| 1182 | index = array_index_nospec(buf_index, ctx->nr_user_bufs); |
| 1183 | imu = &ctx->user_bufs[index]; |
| 1184 | buf_addr = READ_ONCE(sqe->addr); |
| 1185 | |
| 1186 | /* overflow */ |
| 1187 | if (buf_addr + len < buf_addr) |
| 1188 | return -EFAULT; |
| 1189 | /* not inside the mapped region */ |
| 1190 | if (buf_addr < imu->ubuf || buf_addr + len > imu->ubuf + imu->len) |
| 1191 | return -EFAULT; |
| 1192 | |
| 1193 | /* |
| 1194 | * May not be a start of buffer, set size appropriately |
| 1195 | * and advance us to the beginning. |
| 1196 | */ |
| 1197 | offset = buf_addr - imu->ubuf; |
| 1198 | iov_iter_bvec(iter, rw, imu->bvec, imu->nr_bvecs, offset + len); |
| 1199 | |
| 1200 | if (offset) { |
| 1201 | /* |
| 1202 | * Don't use iov_iter_advance() here, as it's really slow for |
| 1203 | * using the latter parts of a big fixed buffer - it iterates |
| 1204 | * over each segment manually. We can cheat a bit here, because |
| 1205 | * we know that: |
| 1206 | * |
| 1207 | * 1) it's a BVEC iter, we set it up |
| 1208 | * 2) all bvecs are PAGE_SIZE in size, except potentially the |
| 1209 | * first and last bvec |
| 1210 | * |
| 1211 | * So just find our index, and adjust the iterator afterwards. |
| 1212 | * If the offset is within the first bvec (or the whole first |
| 1213 | * bvec, just use iov_iter_advance(). This makes it easier |
| 1214 | * since we can just skip the first segment, which may not |
| 1215 | * be PAGE_SIZE aligned. |
| 1216 | */ |
| 1217 | const struct bio_vec *bvec = imu->bvec; |
| 1218 | |
| 1219 | if (offset <= bvec->bv_len) { |
| 1220 | iov_iter_advance(iter, offset); |
| 1221 | } else { |
| 1222 | unsigned long seg_skip; |
| 1223 | |
| 1224 | /* skip first vec */ |
| 1225 | offset -= bvec->bv_len; |
| 1226 | seg_skip = 1 + (offset >> PAGE_SHIFT); |
| 1227 | |
| 1228 | iter->bvec = bvec + seg_skip; |
| 1229 | iter->nr_segs -= seg_skip; |
| 1230 | iter->count -= bvec->bv_len + offset; |
| 1231 | iter->iov_offset = offset & ~PAGE_MASK; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | return len; |
| 1236 | } |
| 1237 | |
| 1238 | static ssize_t io_import_iovec(struct io_ring_ctx *ctx, int rw, |
| 1239 | const struct sqe_submit *s, struct iovec **iovec, |
| 1240 | struct iov_iter *iter) |
| 1241 | { |
| 1242 | const struct io_uring_sqe *sqe = s->sqe; |
| 1243 | void __user *buf = u64_to_user_ptr(READ_ONCE(sqe->addr)); |
| 1244 | size_t sqe_len = READ_ONCE(sqe->len); |
| 1245 | u8 opcode; |
| 1246 | |
| 1247 | /* |
| 1248 | * We're reading ->opcode for the second time, but the first read |
| 1249 | * doesn't care whether it's _FIXED or not, so it doesn't matter |
| 1250 | * whether ->opcode changes concurrently. The first read does care |
| 1251 | * about whether it is a READ or a WRITE, so we don't trust this read |
| 1252 | * for that purpose and instead let the caller pass in the read/write |
| 1253 | * flag. |
| 1254 | */ |
| 1255 | opcode = READ_ONCE(sqe->opcode); |
| 1256 | if (opcode == IORING_OP_READ_FIXED || |
| 1257 | opcode == IORING_OP_WRITE_FIXED) { |
| 1258 | ssize_t ret = io_import_fixed(ctx, rw, sqe, iter); |
| 1259 | *iovec = NULL; |
| 1260 | return ret; |
| 1261 | } |
| 1262 | |
| 1263 | if (!s->has_user) |
| 1264 | return -EFAULT; |
| 1265 | |
| 1266 | #ifdef CONFIG_COMPAT |
| 1267 | if (ctx->compat) |
| 1268 | return compat_import_iovec(rw, buf, sqe_len, UIO_FASTIOV, |
| 1269 | iovec, iter); |
| 1270 | #endif |
| 1271 | |
| 1272 | return import_iovec(rw, buf, sqe_len, UIO_FASTIOV, iovec, iter); |
| 1273 | } |
| 1274 | |
| 1275 | static inline bool io_should_merge(struct async_list *al, struct kiocb *kiocb) |
| 1276 | { |
| 1277 | if (al->file == kiocb->ki_filp) { |
| 1278 | off_t start, end; |
| 1279 | |
| 1280 | /* |
| 1281 | * Allow merging if we're anywhere in the range of the same |
| 1282 | * page. Generally this happens for sub-page reads or writes, |
| 1283 | * and it's beneficial to allow the first worker to bring the |
| 1284 | * page in and the piggy backed work can then work on the |
| 1285 | * cached page. |
| 1286 | */ |
| 1287 | start = al->io_start & PAGE_MASK; |
| 1288 | end = (al->io_start + al->io_len + PAGE_SIZE - 1) & PAGE_MASK; |
| 1289 | if (kiocb->ki_pos >= start && kiocb->ki_pos <= end) |
| 1290 | return true; |
| 1291 | } |
| 1292 | |
| 1293 | al->file = NULL; |
| 1294 | return false; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Make a note of the last file/offset/direction we punted to async |
| 1299 | * context. We'll use this information to see if we can piggy back a |
| 1300 | * sequential request onto the previous one, if it's still hasn't been |
| 1301 | * completed by the async worker. |
| 1302 | */ |
| 1303 | static void io_async_list_note(int rw, struct io_kiocb *req, size_t len) |
| 1304 | { |
| 1305 | struct async_list *async_list = &req->ctx->pending_async[rw]; |
| 1306 | struct kiocb *kiocb = &req->rw; |
| 1307 | struct file *filp = kiocb->ki_filp; |
| 1308 | |
| 1309 | if (io_should_merge(async_list, kiocb)) { |
| 1310 | unsigned long max_bytes; |
| 1311 | |
| 1312 | /* Use 8x RA size as a decent limiter for both reads/writes */ |
| 1313 | max_bytes = filp->f_ra.ra_pages << (PAGE_SHIFT + 3); |
| 1314 | if (!max_bytes) |
| 1315 | max_bytes = VM_READAHEAD_PAGES << (PAGE_SHIFT + 3); |
| 1316 | |
| 1317 | /* If max len are exceeded, reset the state */ |
| 1318 | if (async_list->io_len + len <= max_bytes) { |
| 1319 | req->flags |= REQ_F_SEQ_PREV; |
| 1320 | async_list->io_len += len; |
| 1321 | } else { |
| 1322 | async_list->file = NULL; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | /* New file? Reset state. */ |
| 1327 | if (async_list->file != filp) { |
| 1328 | async_list->io_start = kiocb->ki_pos; |
| 1329 | async_list->io_len = len; |
| 1330 | async_list->file = filp; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | * For files that don't have ->read_iter() and ->write_iter(), handle them |
| 1336 | * by looping over ->read() or ->write() manually. |
| 1337 | */ |
| 1338 | static ssize_t loop_rw_iter(int rw, struct file *file, struct kiocb *kiocb, |
| 1339 | struct iov_iter *iter) |
| 1340 | { |
| 1341 | ssize_t ret = 0; |
| 1342 | |
| 1343 | /* |
| 1344 | * Don't support polled IO through this interface, and we can't |
| 1345 | * support non-blocking either. For the latter, this just causes |
| 1346 | * the kiocb to be handled from an async context. |
| 1347 | */ |
| 1348 | if (kiocb->ki_flags & IOCB_HIPRI) |
| 1349 | return -EOPNOTSUPP; |
| 1350 | if (kiocb->ki_flags & IOCB_NOWAIT) |
| 1351 | return -EAGAIN; |
| 1352 | |
| 1353 | while (iov_iter_count(iter)) { |
| 1354 | struct iovec iovec = iov_iter_iovec(iter); |
| 1355 | ssize_t nr; |
| 1356 | |
| 1357 | if (rw == READ) { |
| 1358 | nr = file->f_op->read(file, iovec.iov_base, |
| 1359 | iovec.iov_len, &kiocb->ki_pos); |
| 1360 | } else { |
| 1361 | nr = file->f_op->write(file, iovec.iov_base, |
| 1362 | iovec.iov_len, &kiocb->ki_pos); |
| 1363 | } |
| 1364 | |
| 1365 | if (nr < 0) { |
| 1366 | if (!ret) |
| 1367 | ret = nr; |
| 1368 | break; |
| 1369 | } |
| 1370 | ret += nr; |
| 1371 | if (nr != iovec.iov_len) |
| 1372 | break; |
| 1373 | iov_iter_advance(iter, nr); |
| 1374 | } |
| 1375 | |
| 1376 | return ret; |
| 1377 | } |
| 1378 | |
| 1379 | static int io_read(struct io_kiocb *req, const struct sqe_submit *s, |
| 1380 | bool force_nonblock) |
| 1381 | { |
| 1382 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1383 | struct kiocb *kiocb = &req->rw; |
| 1384 | struct iov_iter iter; |
| 1385 | struct file *file; |
| 1386 | size_t iov_count; |
| 1387 | ssize_t read_size, ret; |
| 1388 | |
| 1389 | ret = io_prep_rw(req, s, force_nonblock); |
| 1390 | if (ret) |
| 1391 | return ret; |
| 1392 | file = kiocb->ki_filp; |
| 1393 | |
| 1394 | if (unlikely(!(file->f_mode & FMODE_READ))) |
| 1395 | return -EBADF; |
| 1396 | |
| 1397 | ret = io_import_iovec(req->ctx, READ, s, &iovec, &iter); |
| 1398 | if (ret < 0) |
| 1399 | return ret; |
| 1400 | |
| 1401 | read_size = ret; |
| 1402 | if (req->flags & REQ_F_LINK) |
| 1403 | req->result = read_size; |
| 1404 | |
| 1405 | iov_count = iov_iter_count(&iter); |
| 1406 | ret = rw_verify_area(READ, file, &kiocb->ki_pos, iov_count); |
| 1407 | if (!ret) { |
| 1408 | ssize_t ret2; |
| 1409 | |
| 1410 | if (file->f_op->read_iter) |
| 1411 | ret2 = call_read_iter(file, kiocb, &iter); |
| 1412 | else |
| 1413 | ret2 = loop_rw_iter(READ, file, kiocb, &iter); |
| 1414 | |
| 1415 | /* |
| 1416 | * In case of a short read, punt to async. This can happen |
| 1417 | * if we have data partially cached. Alternatively we can |
| 1418 | * return the short read, in which case the application will |
| 1419 | * need to issue another SQE and wait for it. That SQE will |
| 1420 | * need async punt anyway, so it's more efficient to do it |
| 1421 | * here. |
| 1422 | */ |
| 1423 | if (force_nonblock && !(req->flags & REQ_F_NOWAIT) && |
| 1424 | (req->flags & REQ_F_ISREG) && |
| 1425 | ret2 > 0 && ret2 < read_size) |
| 1426 | ret2 = -EAGAIN; |
| 1427 | /* Catch -EAGAIN return for forced non-blocking submission */ |
| 1428 | if (!force_nonblock || ret2 != -EAGAIN) { |
| 1429 | io_rw_done(kiocb, ret2); |
| 1430 | } else { |
| 1431 | /* |
| 1432 | * If ->needs_lock is true, we're already in async |
| 1433 | * context. |
| 1434 | */ |
| 1435 | if (!s->needs_lock) |
| 1436 | io_async_list_note(READ, req, iov_count); |
| 1437 | ret = -EAGAIN; |
| 1438 | } |
| 1439 | } |
| 1440 | kfree(iovec); |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
| 1444 | static int io_write(struct io_kiocb *req, const struct sqe_submit *s, |
| 1445 | bool force_nonblock) |
| 1446 | { |
| 1447 | struct iovec inline_vecs[UIO_FASTIOV], *iovec = inline_vecs; |
| 1448 | struct kiocb *kiocb = &req->rw; |
| 1449 | struct iov_iter iter; |
| 1450 | struct file *file; |
| 1451 | size_t iov_count; |
| 1452 | ssize_t ret; |
| 1453 | |
| 1454 | ret = io_prep_rw(req, s, force_nonblock); |
| 1455 | if (ret) |
| 1456 | return ret; |
| 1457 | |
| 1458 | file = kiocb->ki_filp; |
| 1459 | if (unlikely(!(file->f_mode & FMODE_WRITE))) |
| 1460 | return -EBADF; |
| 1461 | |
| 1462 | ret = io_import_iovec(req->ctx, WRITE, s, &iovec, &iter); |
| 1463 | if (ret < 0) |
| 1464 | return ret; |
| 1465 | |
| 1466 | if (req->flags & REQ_F_LINK) |
| 1467 | req->result = ret; |
| 1468 | |
| 1469 | iov_count = iov_iter_count(&iter); |
| 1470 | |
| 1471 | ret = -EAGAIN; |
| 1472 | if (force_nonblock && !(kiocb->ki_flags & IOCB_DIRECT)) { |
| 1473 | /* If ->needs_lock is true, we're already in async context. */ |
| 1474 | if (!s->needs_lock) |
| 1475 | io_async_list_note(WRITE, req, iov_count); |
| 1476 | goto out_free; |
| 1477 | } |
| 1478 | |
| 1479 | ret = rw_verify_area(WRITE, file, &kiocb->ki_pos, iov_count); |
| 1480 | if (!ret) { |
| 1481 | ssize_t ret2; |
| 1482 | |
| 1483 | /* |
| 1484 | * Open-code file_start_write here to grab freeze protection, |
| 1485 | * which will be released by another thread in |
| 1486 | * io_complete_rw(). Fool lockdep by telling it the lock got |
| 1487 | * released so that it doesn't complain about the held lock when |
| 1488 | * we return to userspace. |
| 1489 | */ |
| 1490 | if (req->flags & REQ_F_ISREG) { |
| 1491 | __sb_start_write(file_inode(file)->i_sb, |
| 1492 | SB_FREEZE_WRITE, true); |
| 1493 | __sb_writers_release(file_inode(file)->i_sb, |
| 1494 | SB_FREEZE_WRITE); |
| 1495 | } |
| 1496 | kiocb->ki_flags |= IOCB_WRITE; |
| 1497 | |
| 1498 | if (file->f_op->write_iter) |
| 1499 | ret2 = call_write_iter(file, kiocb, &iter); |
| 1500 | else |
| 1501 | ret2 = loop_rw_iter(WRITE, file, kiocb, &iter); |
| 1502 | if (!force_nonblock || ret2 != -EAGAIN) { |
| 1503 | io_rw_done(kiocb, ret2); |
| 1504 | } else { |
| 1505 | /* |
| 1506 | * If ->needs_lock is true, we're already in async |
| 1507 | * context. |
| 1508 | */ |
| 1509 | if (!s->needs_lock) |
| 1510 | io_async_list_note(WRITE, req, iov_count); |
| 1511 | ret = -EAGAIN; |
| 1512 | } |
| 1513 | } |
| 1514 | out_free: |
| 1515 | kfree(iovec); |
| 1516 | return ret; |
| 1517 | } |
| 1518 | |
| 1519 | /* |
| 1520 | * IORING_OP_NOP just posts a completion event, nothing else. |
| 1521 | */ |
| 1522 | static int io_nop(struct io_kiocb *req, u64 user_data) |
| 1523 | { |
| 1524 | struct io_ring_ctx *ctx = req->ctx; |
| 1525 | long err = 0; |
| 1526 | |
| 1527 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1528 | return -EINVAL; |
| 1529 | |
| 1530 | io_cqring_add_event(ctx, user_data, err); |
| 1531 | io_put_req(req); |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | static int io_prep_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1536 | { |
| 1537 | struct io_ring_ctx *ctx = req->ctx; |
| 1538 | |
| 1539 | if (!req->file) |
| 1540 | return -EBADF; |
| 1541 | |
| 1542 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1543 | return -EINVAL; |
| 1544 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 1545 | return -EINVAL; |
| 1546 | |
| 1547 | return 0; |
| 1548 | } |
| 1549 | |
| 1550 | static int io_fsync(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1551 | bool force_nonblock) |
| 1552 | { |
| 1553 | loff_t sqe_off = READ_ONCE(sqe->off); |
| 1554 | loff_t sqe_len = READ_ONCE(sqe->len); |
| 1555 | loff_t end = sqe_off + sqe_len; |
| 1556 | unsigned fsync_flags; |
| 1557 | int ret; |
| 1558 | |
| 1559 | fsync_flags = READ_ONCE(sqe->fsync_flags); |
| 1560 | if (unlikely(fsync_flags & ~IORING_FSYNC_DATASYNC)) |
| 1561 | return -EINVAL; |
| 1562 | |
| 1563 | ret = io_prep_fsync(req, sqe); |
| 1564 | if (ret) |
| 1565 | return ret; |
| 1566 | |
| 1567 | /* fsync always requires a blocking context */ |
| 1568 | if (force_nonblock) |
| 1569 | return -EAGAIN; |
| 1570 | |
| 1571 | ret = vfs_fsync_range(req->rw.ki_filp, sqe_off, |
| 1572 | end > 0 ? end : LLONG_MAX, |
| 1573 | fsync_flags & IORING_FSYNC_DATASYNC); |
| 1574 | |
| 1575 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1576 | req->flags |= REQ_F_FAIL_LINK; |
| 1577 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
| 1578 | io_put_req(req); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | static int io_prep_sfr(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1583 | { |
| 1584 | struct io_ring_ctx *ctx = req->ctx; |
| 1585 | int ret = 0; |
| 1586 | |
| 1587 | if (!req->file) |
| 1588 | return -EBADF; |
| 1589 | |
| 1590 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1591 | return -EINVAL; |
| 1592 | if (unlikely(sqe->addr || sqe->ioprio || sqe->buf_index)) |
| 1593 | return -EINVAL; |
| 1594 | |
| 1595 | return ret; |
| 1596 | } |
| 1597 | |
| 1598 | static int io_sync_file_range(struct io_kiocb *req, |
| 1599 | const struct io_uring_sqe *sqe, |
| 1600 | bool force_nonblock) |
| 1601 | { |
| 1602 | loff_t sqe_off; |
| 1603 | loff_t sqe_len; |
| 1604 | unsigned flags; |
| 1605 | int ret; |
| 1606 | |
| 1607 | ret = io_prep_sfr(req, sqe); |
| 1608 | if (ret) |
| 1609 | return ret; |
| 1610 | |
| 1611 | /* sync_file_range always requires a blocking context */ |
| 1612 | if (force_nonblock) |
| 1613 | return -EAGAIN; |
| 1614 | |
| 1615 | sqe_off = READ_ONCE(sqe->off); |
| 1616 | sqe_len = READ_ONCE(sqe->len); |
| 1617 | flags = READ_ONCE(sqe->sync_range_flags); |
| 1618 | |
| 1619 | ret = sync_file_range(req->rw.ki_filp, sqe_off, sqe_len, flags); |
| 1620 | |
| 1621 | if (ret < 0 && (req->flags & REQ_F_LINK)) |
| 1622 | req->flags |= REQ_F_FAIL_LINK; |
| 1623 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
| 1624 | io_put_req(req); |
| 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | #if defined(CONFIG_NET) |
| 1629 | static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1630 | bool force_nonblock, |
| 1631 | long (*fn)(struct socket *, struct user_msghdr __user *, |
| 1632 | unsigned int)) |
| 1633 | { |
| 1634 | struct socket *sock; |
| 1635 | int ret; |
| 1636 | |
| 1637 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1638 | return -EINVAL; |
| 1639 | |
| 1640 | sock = sock_from_file(req->file, &ret); |
| 1641 | if (sock) { |
| 1642 | struct user_msghdr __user *msg; |
| 1643 | unsigned flags; |
| 1644 | |
| 1645 | flags = READ_ONCE(sqe->msg_flags); |
| 1646 | if (flags & MSG_DONTWAIT) |
| 1647 | req->flags |= REQ_F_NOWAIT; |
| 1648 | else if (force_nonblock) |
| 1649 | flags |= MSG_DONTWAIT; |
| 1650 | |
| 1651 | msg = (struct user_msghdr __user *) (unsigned long) |
| 1652 | READ_ONCE(sqe->addr); |
| 1653 | |
| 1654 | ret = fn(sock, msg, flags); |
| 1655 | if (force_nonblock && ret == -EAGAIN) |
| 1656 | return ret; |
| 1657 | } |
| 1658 | |
| 1659 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
| 1660 | io_put_req(req); |
| 1661 | return 0; |
| 1662 | } |
| 1663 | #endif |
| 1664 | |
| 1665 | static int io_sendmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1666 | bool force_nonblock) |
| 1667 | { |
| 1668 | #if defined(CONFIG_NET) |
| 1669 | return io_send_recvmsg(req, sqe, force_nonblock, __sys_sendmsg_sock); |
| 1670 | #else |
| 1671 | return -EOPNOTSUPP; |
| 1672 | #endif |
| 1673 | } |
| 1674 | |
| 1675 | static int io_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, |
| 1676 | bool force_nonblock) |
| 1677 | { |
| 1678 | #if defined(CONFIG_NET) |
| 1679 | return io_send_recvmsg(req, sqe, force_nonblock, __sys_recvmsg_sock); |
| 1680 | #else |
| 1681 | return -EOPNOTSUPP; |
| 1682 | #endif |
| 1683 | } |
| 1684 | |
| 1685 | static void io_poll_remove_one(struct io_kiocb *req) |
| 1686 | { |
| 1687 | struct io_poll_iocb *poll = &req->poll; |
| 1688 | |
| 1689 | spin_lock(&poll->head->lock); |
| 1690 | WRITE_ONCE(poll->canceled, true); |
| 1691 | if (!list_empty(&poll->wait.entry)) { |
| 1692 | list_del_init(&poll->wait.entry); |
| 1693 | io_queue_async_work(req->ctx, req); |
| 1694 | } |
| 1695 | spin_unlock(&poll->head->lock); |
| 1696 | |
| 1697 | list_del_init(&req->list); |
| 1698 | } |
| 1699 | |
| 1700 | static void io_poll_remove_all(struct io_ring_ctx *ctx) |
| 1701 | { |
| 1702 | struct io_kiocb *req; |
| 1703 | |
| 1704 | spin_lock_irq(&ctx->completion_lock); |
| 1705 | while (!list_empty(&ctx->cancel_list)) { |
| 1706 | req = list_first_entry(&ctx->cancel_list, struct io_kiocb,list); |
| 1707 | io_poll_remove_one(req); |
| 1708 | } |
| 1709 | spin_unlock_irq(&ctx->completion_lock); |
| 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * Find a running poll command that matches one specified in sqe->addr, |
| 1714 | * and remove it if found. |
| 1715 | */ |
| 1716 | static int io_poll_remove(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1717 | { |
| 1718 | struct io_ring_ctx *ctx = req->ctx; |
| 1719 | struct io_kiocb *poll_req, *next; |
| 1720 | int ret = -ENOENT; |
| 1721 | |
| 1722 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1723 | return -EINVAL; |
| 1724 | if (sqe->ioprio || sqe->off || sqe->len || sqe->buf_index || |
| 1725 | sqe->poll_events) |
| 1726 | return -EINVAL; |
| 1727 | |
| 1728 | spin_lock_irq(&ctx->completion_lock); |
| 1729 | list_for_each_entry_safe(poll_req, next, &ctx->cancel_list, list) { |
| 1730 | if (READ_ONCE(sqe->addr) == poll_req->user_data) { |
| 1731 | io_poll_remove_one(poll_req); |
| 1732 | ret = 0; |
| 1733 | break; |
| 1734 | } |
| 1735 | } |
| 1736 | spin_unlock_irq(&ctx->completion_lock); |
| 1737 | |
| 1738 | io_cqring_add_event(req->ctx, sqe->user_data, ret); |
| 1739 | io_put_req(req); |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | static void io_poll_complete(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 1744 | __poll_t mask) |
| 1745 | { |
| 1746 | req->poll.done = true; |
| 1747 | io_cqring_fill_event(ctx, req->user_data, mangle_poll(mask)); |
| 1748 | io_commit_cqring(ctx); |
| 1749 | } |
| 1750 | |
| 1751 | static void io_poll_complete_work(struct work_struct *work) |
| 1752 | { |
| 1753 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 1754 | struct io_poll_iocb *poll = &req->poll; |
| 1755 | struct poll_table_struct pt = { ._key = poll->events }; |
| 1756 | struct io_ring_ctx *ctx = req->ctx; |
| 1757 | const struct cred *old_cred; |
| 1758 | __poll_t mask = 0; |
| 1759 | |
| 1760 | old_cred = override_creds(ctx->creds); |
| 1761 | |
| 1762 | if (!READ_ONCE(poll->canceled)) |
| 1763 | mask = vfs_poll(poll->file, &pt) & poll->events; |
| 1764 | |
| 1765 | /* |
| 1766 | * Note that ->ki_cancel callers also delete iocb from active_reqs after |
| 1767 | * calling ->ki_cancel. We need the ctx_lock roundtrip here to |
| 1768 | * synchronize with them. In the cancellation case the list_del_init |
| 1769 | * itself is not actually needed, but harmless so we keep it in to |
| 1770 | * avoid further branches in the fast path. |
| 1771 | */ |
| 1772 | spin_lock_irq(&ctx->completion_lock); |
| 1773 | if (!mask && !READ_ONCE(poll->canceled)) { |
| 1774 | add_wait_queue(poll->head, &poll->wait); |
| 1775 | spin_unlock_irq(&ctx->completion_lock); |
| 1776 | goto out; |
| 1777 | } |
| 1778 | list_del_init(&req->list); |
| 1779 | io_poll_complete(ctx, req, mask); |
| 1780 | spin_unlock_irq(&ctx->completion_lock); |
| 1781 | |
| 1782 | io_cqring_ev_posted(ctx); |
| 1783 | io_put_req(req); |
| 1784 | out: |
| 1785 | revert_creds(old_cred); |
| 1786 | } |
| 1787 | |
| 1788 | static int io_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, |
| 1789 | void *key) |
| 1790 | { |
| 1791 | struct io_poll_iocb *poll = container_of(wait, struct io_poll_iocb, |
| 1792 | wait); |
| 1793 | struct io_kiocb *req = container_of(poll, struct io_kiocb, poll); |
| 1794 | struct io_ring_ctx *ctx = req->ctx; |
| 1795 | __poll_t mask = key_to_poll(key); |
| 1796 | unsigned long flags; |
| 1797 | |
| 1798 | /* for instances that support it check for an event match first: */ |
| 1799 | if (mask && !(mask & poll->events)) |
| 1800 | return 0; |
| 1801 | |
| 1802 | list_del_init(&poll->wait.entry); |
| 1803 | |
| 1804 | if (mask && spin_trylock_irqsave(&ctx->completion_lock, flags)) { |
| 1805 | list_del(&req->list); |
| 1806 | io_poll_complete(ctx, req, mask); |
| 1807 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1808 | |
| 1809 | io_cqring_ev_posted(ctx); |
| 1810 | io_put_req(req); |
| 1811 | } else { |
| 1812 | io_queue_async_work(ctx, req); |
| 1813 | } |
| 1814 | |
| 1815 | return 1; |
| 1816 | } |
| 1817 | |
| 1818 | struct io_poll_table { |
| 1819 | struct poll_table_struct pt; |
| 1820 | struct io_kiocb *req; |
| 1821 | int error; |
| 1822 | }; |
| 1823 | |
| 1824 | static void io_poll_queue_proc(struct file *file, struct wait_queue_head *head, |
| 1825 | struct poll_table_struct *p) |
| 1826 | { |
| 1827 | struct io_poll_table *pt = container_of(p, struct io_poll_table, pt); |
| 1828 | |
| 1829 | if (unlikely(pt->req->poll.head)) { |
| 1830 | pt->error = -EINVAL; |
| 1831 | return; |
| 1832 | } |
| 1833 | |
| 1834 | pt->error = 0; |
| 1835 | pt->req->poll.head = head; |
| 1836 | add_wait_queue(head, &pt->req->poll.wait); |
| 1837 | } |
| 1838 | |
| 1839 | static int io_poll_add(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1840 | { |
| 1841 | struct io_poll_iocb *poll = &req->poll; |
| 1842 | struct io_ring_ctx *ctx = req->ctx; |
| 1843 | struct io_poll_table ipt; |
| 1844 | bool cancel = false; |
| 1845 | __poll_t mask; |
| 1846 | u16 events; |
| 1847 | |
| 1848 | if (unlikely(req->ctx->flags & IORING_SETUP_IOPOLL)) |
| 1849 | return -EINVAL; |
| 1850 | if (sqe->addr || sqe->ioprio || sqe->off || sqe->len || sqe->buf_index) |
| 1851 | return -EINVAL; |
| 1852 | if (!poll->file) |
| 1853 | return -EBADF; |
| 1854 | |
| 1855 | req->submit.sqe = NULL; |
| 1856 | INIT_WORK(&req->work, io_poll_complete_work); |
| 1857 | events = READ_ONCE(sqe->poll_events); |
| 1858 | poll->events = demangle_poll(events) | EPOLLERR | EPOLLHUP; |
| 1859 | |
| 1860 | poll->head = NULL; |
| 1861 | poll->done = false; |
| 1862 | poll->canceled = false; |
| 1863 | |
| 1864 | ipt.pt._qproc = io_poll_queue_proc; |
| 1865 | ipt.pt._key = poll->events; |
| 1866 | ipt.req = req; |
| 1867 | ipt.error = -EINVAL; /* same as no support for IOCB_CMD_POLL */ |
| 1868 | |
| 1869 | /* initialized the list so that we can do list_empty checks */ |
| 1870 | INIT_LIST_HEAD(&poll->wait.entry); |
| 1871 | init_waitqueue_func_entry(&poll->wait, io_poll_wake); |
| 1872 | |
| 1873 | INIT_LIST_HEAD(&req->list); |
| 1874 | |
| 1875 | mask = vfs_poll(poll->file, &ipt.pt) & poll->events; |
| 1876 | |
| 1877 | spin_lock_irq(&ctx->completion_lock); |
| 1878 | if (likely(poll->head)) { |
| 1879 | spin_lock(&poll->head->lock); |
| 1880 | if (unlikely(list_empty(&poll->wait.entry))) { |
| 1881 | if (ipt.error) |
| 1882 | cancel = true; |
| 1883 | ipt.error = 0; |
| 1884 | mask = 0; |
| 1885 | } |
| 1886 | if (mask || ipt.error) |
| 1887 | list_del_init(&poll->wait.entry); |
| 1888 | else if (cancel) |
| 1889 | WRITE_ONCE(poll->canceled, true); |
| 1890 | else if (!poll->done) /* actually waiting for an event */ |
| 1891 | list_add_tail(&req->list, &ctx->cancel_list); |
| 1892 | spin_unlock(&poll->head->lock); |
| 1893 | } |
| 1894 | if (mask) { /* no async, we'd stolen it */ |
| 1895 | ipt.error = 0; |
| 1896 | io_poll_complete(ctx, req, mask); |
| 1897 | } |
| 1898 | spin_unlock_irq(&ctx->completion_lock); |
| 1899 | |
| 1900 | if (mask) { |
| 1901 | io_cqring_ev_posted(ctx); |
| 1902 | io_put_req(req); |
| 1903 | } |
| 1904 | return ipt.error; |
| 1905 | } |
| 1906 | |
| 1907 | static enum hrtimer_restart io_timeout_fn(struct hrtimer *timer) |
| 1908 | { |
| 1909 | struct io_ring_ctx *ctx; |
| 1910 | struct io_kiocb *req, *prev; |
| 1911 | unsigned long flags; |
| 1912 | |
| 1913 | req = container_of(timer, struct io_kiocb, timeout.timer); |
| 1914 | ctx = req->ctx; |
| 1915 | atomic_inc(&ctx->cq_timeouts); |
| 1916 | |
| 1917 | spin_lock_irqsave(&ctx->completion_lock, flags); |
| 1918 | /* |
| 1919 | * Adjust the reqs sequence before the current one because it |
| 1920 | * will consume a slot in the cq_ring and the the cq_tail pointer |
| 1921 | * will be increased, otherwise other timeout reqs may return in |
| 1922 | * advance without waiting for enough wait_nr. |
| 1923 | */ |
| 1924 | prev = req; |
| 1925 | list_for_each_entry_continue_reverse(prev, &ctx->timeout_list, list) |
| 1926 | prev->sequence++; |
| 1927 | list_del(&req->list); |
| 1928 | |
| 1929 | io_cqring_fill_event(ctx, req->user_data, -ETIME); |
| 1930 | io_commit_cqring(ctx); |
| 1931 | spin_unlock_irqrestore(&ctx->completion_lock, flags); |
| 1932 | |
| 1933 | io_cqring_ev_posted(ctx); |
| 1934 | |
| 1935 | io_put_req(req); |
| 1936 | return HRTIMER_NORESTART; |
| 1937 | } |
| 1938 | |
| 1939 | static int io_timeout(struct io_kiocb *req, const struct io_uring_sqe *sqe) |
| 1940 | { |
| 1941 | unsigned count; |
| 1942 | struct io_ring_ctx *ctx = req->ctx; |
| 1943 | struct list_head *entry; |
| 1944 | struct timespec64 ts; |
| 1945 | unsigned span = 0; |
| 1946 | |
| 1947 | if (unlikely(ctx->flags & IORING_SETUP_IOPOLL)) |
| 1948 | return -EINVAL; |
| 1949 | if (sqe->flags || sqe->ioprio || sqe->buf_index || sqe->timeout_flags || |
| 1950 | sqe->len != 1) |
| 1951 | return -EINVAL; |
| 1952 | |
| 1953 | if (get_timespec64(&ts, u64_to_user_ptr(sqe->addr))) |
| 1954 | return -EFAULT; |
| 1955 | |
| 1956 | req->flags |= REQ_F_TIMEOUT; |
| 1957 | |
| 1958 | /* |
| 1959 | * sqe->off holds how many events that need to occur for this |
| 1960 | * timeout event to be satisfied. If it isn't set, then this is |
| 1961 | * a pure timeout request, sequence isn't used. |
| 1962 | */ |
| 1963 | count = READ_ONCE(sqe->off); |
| 1964 | if (!count) { |
| 1965 | req->flags |= REQ_F_TIMEOUT_NOSEQ; |
| 1966 | spin_lock_irq(&ctx->completion_lock); |
| 1967 | entry = ctx->timeout_list.prev; |
| 1968 | goto add; |
| 1969 | } |
| 1970 | |
| 1971 | req->sequence = ctx->cached_sq_head + count - 1; |
| 1972 | /* reuse it to store the count */ |
| 1973 | req->submit.sequence = count; |
| 1974 | |
| 1975 | /* |
| 1976 | * Insertion sort, ensuring the first entry in the list is always |
| 1977 | * the one we need first. |
| 1978 | */ |
| 1979 | spin_lock_irq(&ctx->completion_lock); |
| 1980 | list_for_each_prev(entry, &ctx->timeout_list) { |
| 1981 | struct io_kiocb *nxt = list_entry(entry, struct io_kiocb, list); |
| 1982 | unsigned nxt_sq_head; |
| 1983 | long long tmp, tmp_nxt; |
| 1984 | |
| 1985 | if (nxt->flags & REQ_F_TIMEOUT_NOSEQ) |
| 1986 | continue; |
| 1987 | |
| 1988 | /* |
| 1989 | * Since cached_sq_head + count - 1 can overflow, use type long |
| 1990 | * long to store it. |
| 1991 | */ |
| 1992 | tmp = (long long)ctx->cached_sq_head + count - 1; |
| 1993 | nxt_sq_head = nxt->sequence - nxt->submit.sequence + 1; |
| 1994 | tmp_nxt = (long long)nxt_sq_head + nxt->submit.sequence - 1; |
| 1995 | |
| 1996 | /* |
| 1997 | * cached_sq_head may overflow, and it will never overflow twice |
| 1998 | * once there is some timeout req still be valid. |
| 1999 | */ |
| 2000 | if (ctx->cached_sq_head < nxt_sq_head) |
| 2001 | tmp += UINT_MAX; |
| 2002 | |
| 2003 | if (tmp > tmp_nxt) |
| 2004 | break; |
| 2005 | |
| 2006 | /* |
| 2007 | * Sequence of reqs after the insert one and itself should |
| 2008 | * be adjusted because each timeout req consumes a slot. |
| 2009 | */ |
| 2010 | span++; |
| 2011 | nxt->sequence++; |
| 2012 | } |
| 2013 | req->sequence -= span; |
| 2014 | add: |
| 2015 | list_add(&req->list, entry); |
| 2016 | spin_unlock_irq(&ctx->completion_lock); |
| 2017 | |
| 2018 | hrtimer_init(&req->timeout.timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 2019 | req->timeout.timer.function = io_timeout_fn; |
| 2020 | hrtimer_start(&req->timeout.timer, timespec64_to_ktime(ts), |
| 2021 | HRTIMER_MODE_REL); |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
| 2025 | static int io_req_defer(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 2026 | const struct io_uring_sqe *sqe) |
| 2027 | { |
| 2028 | struct io_uring_sqe *sqe_copy; |
| 2029 | |
| 2030 | if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) |
| 2031 | return 0; |
| 2032 | |
| 2033 | sqe_copy = kmalloc(sizeof(*sqe_copy), GFP_KERNEL); |
| 2034 | if (!sqe_copy) |
| 2035 | return -EAGAIN; |
| 2036 | |
| 2037 | spin_lock_irq(&ctx->completion_lock); |
| 2038 | if (!io_sequence_defer(ctx, req) && list_empty(&ctx->defer_list)) { |
| 2039 | spin_unlock_irq(&ctx->completion_lock); |
| 2040 | kfree(sqe_copy); |
| 2041 | return 0; |
| 2042 | } |
| 2043 | |
| 2044 | memcpy(sqe_copy, sqe, sizeof(*sqe_copy)); |
| 2045 | req->submit.sqe = sqe_copy; |
| 2046 | |
| 2047 | INIT_WORK(&req->work, io_sq_wq_submit_work); |
| 2048 | list_add_tail(&req->list, &ctx->defer_list); |
| 2049 | spin_unlock_irq(&ctx->completion_lock); |
| 2050 | return -EIOCBQUEUED; |
| 2051 | } |
| 2052 | |
| 2053 | static int __io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 2054 | const struct sqe_submit *s, bool force_nonblock) |
| 2055 | { |
| 2056 | int ret, opcode; |
| 2057 | |
| 2058 | req->user_data = READ_ONCE(s->sqe->user_data); |
| 2059 | |
| 2060 | if (unlikely(s->index >= ctx->sq_entries)) |
| 2061 | return -EINVAL; |
| 2062 | |
| 2063 | opcode = READ_ONCE(s->sqe->opcode); |
| 2064 | switch (opcode) { |
| 2065 | case IORING_OP_NOP: |
| 2066 | ret = io_nop(req, req->user_data); |
| 2067 | break; |
| 2068 | case IORING_OP_READV: |
| 2069 | if (unlikely(s->sqe->buf_index)) |
| 2070 | return -EINVAL; |
| 2071 | ret = io_read(req, s, force_nonblock); |
| 2072 | break; |
| 2073 | case IORING_OP_WRITEV: |
| 2074 | if (unlikely(s->sqe->buf_index)) |
| 2075 | return -EINVAL; |
| 2076 | ret = io_write(req, s, force_nonblock); |
| 2077 | break; |
| 2078 | case IORING_OP_READ_FIXED: |
| 2079 | ret = io_read(req, s, force_nonblock); |
| 2080 | break; |
| 2081 | case IORING_OP_WRITE_FIXED: |
| 2082 | ret = io_write(req, s, force_nonblock); |
| 2083 | break; |
| 2084 | case IORING_OP_FSYNC: |
| 2085 | ret = io_fsync(req, s->sqe, force_nonblock); |
| 2086 | break; |
| 2087 | case IORING_OP_POLL_ADD: |
| 2088 | ret = io_poll_add(req, s->sqe); |
| 2089 | break; |
| 2090 | case IORING_OP_POLL_REMOVE: |
| 2091 | ret = io_poll_remove(req, s->sqe); |
| 2092 | break; |
| 2093 | case IORING_OP_SYNC_FILE_RANGE: |
| 2094 | ret = io_sync_file_range(req, s->sqe, force_nonblock); |
| 2095 | break; |
| 2096 | case IORING_OP_SENDMSG: |
| 2097 | ret = io_sendmsg(req, s->sqe, force_nonblock); |
| 2098 | break; |
| 2099 | case IORING_OP_RECVMSG: |
| 2100 | ret = io_recvmsg(req, s->sqe, force_nonblock); |
| 2101 | break; |
| 2102 | case IORING_OP_TIMEOUT: |
| 2103 | ret = io_timeout(req, s->sqe); |
| 2104 | break; |
| 2105 | default: |
| 2106 | ret = -EINVAL; |
| 2107 | break; |
| 2108 | } |
| 2109 | |
| 2110 | if (ret) |
| 2111 | return ret; |
| 2112 | |
| 2113 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 2114 | if (req->result == -EAGAIN) |
| 2115 | return -EAGAIN; |
| 2116 | |
| 2117 | /* workqueue context doesn't hold uring_lock, grab it now */ |
| 2118 | if (s->needs_lock) |
| 2119 | mutex_lock(&ctx->uring_lock); |
| 2120 | io_iopoll_req_issued(req); |
| 2121 | if (s->needs_lock) |
| 2122 | mutex_unlock(&ctx->uring_lock); |
| 2123 | } |
| 2124 | |
| 2125 | return 0; |
| 2126 | } |
| 2127 | |
| 2128 | static struct async_list *io_async_list_from_sqe(struct io_ring_ctx *ctx, |
| 2129 | const struct io_uring_sqe *sqe) |
| 2130 | { |
| 2131 | switch (sqe->opcode) { |
| 2132 | case IORING_OP_READV: |
| 2133 | case IORING_OP_READ_FIXED: |
| 2134 | return &ctx->pending_async[READ]; |
| 2135 | case IORING_OP_WRITEV: |
| 2136 | case IORING_OP_WRITE_FIXED: |
| 2137 | return &ctx->pending_async[WRITE]; |
| 2138 | default: |
| 2139 | return NULL; |
| 2140 | } |
| 2141 | } |
| 2142 | |
| 2143 | static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe) |
| 2144 | { |
| 2145 | u8 opcode = READ_ONCE(sqe->opcode); |
| 2146 | |
| 2147 | return !(opcode == IORING_OP_READ_FIXED || |
| 2148 | opcode == IORING_OP_WRITE_FIXED); |
| 2149 | } |
| 2150 | |
| 2151 | static void io_sq_wq_submit_work(struct work_struct *work) |
| 2152 | { |
| 2153 | struct io_kiocb *req = container_of(work, struct io_kiocb, work); |
| 2154 | struct io_ring_ctx *ctx = req->ctx; |
| 2155 | struct mm_struct *cur_mm = NULL; |
| 2156 | struct async_list *async_list; |
| 2157 | const struct cred *old_cred; |
| 2158 | LIST_HEAD(req_list); |
| 2159 | mm_segment_t old_fs; |
| 2160 | int ret; |
| 2161 | |
| 2162 | old_cred = override_creds(ctx->creds); |
| 2163 | async_list = io_async_list_from_sqe(ctx, req->submit.sqe); |
| 2164 | restart: |
| 2165 | do { |
| 2166 | struct sqe_submit *s = &req->submit; |
| 2167 | const struct io_uring_sqe *sqe = s->sqe; |
| 2168 | unsigned int flags = req->flags; |
| 2169 | |
| 2170 | /* Ensure we clear previously set non-block flag */ |
| 2171 | req->rw.ki_flags &= ~IOCB_NOWAIT; |
| 2172 | |
| 2173 | ret = 0; |
| 2174 | if (io_sqe_needs_user(sqe) && !cur_mm) { |
| 2175 | if (!mmget_not_zero(ctx->sqo_mm)) { |
| 2176 | ret = -EFAULT; |
| 2177 | } else { |
| 2178 | cur_mm = ctx->sqo_mm; |
| 2179 | use_mm(cur_mm); |
| 2180 | old_fs = get_fs(); |
| 2181 | set_fs(USER_DS); |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | if (!ret) { |
| 2186 | s->has_user = cur_mm != NULL; |
| 2187 | s->needs_lock = true; |
| 2188 | do { |
| 2189 | ret = __io_submit_sqe(ctx, req, s, false); |
| 2190 | /* |
| 2191 | * We can get EAGAIN for polled IO even though |
| 2192 | * we're forcing a sync submission from here, |
| 2193 | * since we can't wait for request slots on the |
| 2194 | * block side. |
| 2195 | */ |
| 2196 | if (ret != -EAGAIN) |
| 2197 | break; |
| 2198 | cond_resched(); |
| 2199 | } while (1); |
| 2200 | } |
| 2201 | |
| 2202 | /* drop submission reference */ |
| 2203 | io_put_req(req); |
| 2204 | |
| 2205 | if (ret) { |
| 2206 | io_cqring_add_event(ctx, sqe->user_data, ret); |
| 2207 | io_put_req(req); |
| 2208 | } |
| 2209 | |
| 2210 | /* async context always use a copy of the sqe */ |
| 2211 | kfree(sqe); |
| 2212 | |
| 2213 | /* req from defer and link list needn't decrease async cnt */ |
| 2214 | if (flags & (REQ_F_IO_DRAINED | REQ_F_LINK_DONE)) |
| 2215 | goto out; |
| 2216 | |
| 2217 | if (!async_list) |
| 2218 | break; |
| 2219 | if (!list_empty(&req_list)) { |
| 2220 | req = list_first_entry(&req_list, struct io_kiocb, |
| 2221 | list); |
| 2222 | list_del(&req->list); |
| 2223 | continue; |
| 2224 | } |
| 2225 | if (list_empty(&async_list->list)) |
| 2226 | break; |
| 2227 | |
| 2228 | req = NULL; |
| 2229 | spin_lock(&async_list->lock); |
| 2230 | if (list_empty(&async_list->list)) { |
| 2231 | spin_unlock(&async_list->lock); |
| 2232 | break; |
| 2233 | } |
| 2234 | list_splice_init(&async_list->list, &req_list); |
| 2235 | spin_unlock(&async_list->lock); |
| 2236 | |
| 2237 | req = list_first_entry(&req_list, struct io_kiocb, list); |
| 2238 | list_del(&req->list); |
| 2239 | } while (req); |
| 2240 | |
| 2241 | /* |
| 2242 | * Rare case of racing with a submitter. If we find the count has |
| 2243 | * dropped to zero AND we have pending work items, then restart |
| 2244 | * the processing. This is a tiny race window. |
| 2245 | */ |
| 2246 | if (async_list) { |
| 2247 | ret = atomic_dec_return(&async_list->cnt); |
| 2248 | while (!ret && !list_empty(&async_list->list)) { |
| 2249 | spin_lock(&async_list->lock); |
| 2250 | atomic_inc(&async_list->cnt); |
| 2251 | list_splice_init(&async_list->list, &req_list); |
| 2252 | spin_unlock(&async_list->lock); |
| 2253 | |
| 2254 | if (!list_empty(&req_list)) { |
| 2255 | req = list_first_entry(&req_list, |
| 2256 | struct io_kiocb, list); |
| 2257 | list_del(&req->list); |
| 2258 | goto restart; |
| 2259 | } |
| 2260 | ret = atomic_dec_return(&async_list->cnt); |
| 2261 | } |
| 2262 | } |
| 2263 | |
| 2264 | out: |
| 2265 | if (cur_mm) { |
| 2266 | set_fs(old_fs); |
| 2267 | unuse_mm(cur_mm); |
| 2268 | mmput(cur_mm); |
| 2269 | } |
| 2270 | revert_creds(old_cred); |
| 2271 | } |
| 2272 | |
| 2273 | /* |
| 2274 | * See if we can piggy back onto previously submitted work, that is still |
| 2275 | * running. We currently only allow this if the new request is sequential |
| 2276 | * to the previous one we punted. |
| 2277 | */ |
| 2278 | static bool io_add_to_prev_work(struct async_list *list, struct io_kiocb *req) |
| 2279 | { |
| 2280 | bool ret; |
| 2281 | |
| 2282 | if (!list) |
| 2283 | return false; |
| 2284 | if (!(req->flags & REQ_F_SEQ_PREV)) |
| 2285 | return false; |
| 2286 | if (!atomic_read(&list->cnt)) |
| 2287 | return false; |
| 2288 | |
| 2289 | ret = true; |
| 2290 | spin_lock(&list->lock); |
| 2291 | list_add_tail(&req->list, &list->list); |
| 2292 | /* |
| 2293 | * Ensure we see a simultaneous modification from io_sq_wq_submit_work() |
| 2294 | */ |
| 2295 | smp_mb(); |
| 2296 | if (!atomic_read(&list->cnt)) { |
| 2297 | list_del_init(&req->list); |
| 2298 | ret = false; |
| 2299 | } |
| 2300 | spin_unlock(&list->lock); |
| 2301 | return ret; |
| 2302 | } |
| 2303 | |
| 2304 | static bool io_op_needs_file(const struct io_uring_sqe *sqe) |
| 2305 | { |
| 2306 | int op = READ_ONCE(sqe->opcode); |
| 2307 | |
| 2308 | switch (op) { |
| 2309 | case IORING_OP_NOP: |
| 2310 | case IORING_OP_POLL_REMOVE: |
| 2311 | case IORING_OP_TIMEOUT: |
| 2312 | return false; |
| 2313 | default: |
| 2314 | return true; |
| 2315 | } |
| 2316 | } |
| 2317 | |
| 2318 | static int io_req_set_file(struct io_ring_ctx *ctx, const struct sqe_submit *s, |
| 2319 | struct io_submit_state *state, struct io_kiocb *req) |
| 2320 | { |
| 2321 | unsigned flags; |
| 2322 | int fd; |
| 2323 | |
| 2324 | flags = READ_ONCE(s->sqe->flags); |
| 2325 | fd = READ_ONCE(s->sqe->fd); |
| 2326 | |
| 2327 | if (flags & IOSQE_IO_DRAIN) |
| 2328 | req->flags |= REQ_F_IO_DRAIN; |
| 2329 | /* |
| 2330 | * All io need record the previous position, if LINK vs DARIN, |
| 2331 | * it can be used to mark the position of the first IO in the |
| 2332 | * link list. |
| 2333 | */ |
| 2334 | req->sequence = s->sequence; |
| 2335 | |
| 2336 | if (!io_op_needs_file(s->sqe)) |
| 2337 | return 0; |
| 2338 | |
| 2339 | if (flags & IOSQE_FIXED_FILE) { |
| 2340 | if (unlikely(!ctx->user_files || |
| 2341 | (unsigned) fd >= ctx->nr_user_files)) |
| 2342 | return -EBADF; |
| 2343 | req->file = ctx->user_files[fd]; |
| 2344 | req->flags |= REQ_F_FIXED_FILE; |
| 2345 | } else { |
| 2346 | if (s->needs_fixed_file) |
| 2347 | return -EBADF; |
| 2348 | req->file = io_file_get(state, fd); |
| 2349 | if (unlikely(!req->file)) |
| 2350 | return -EBADF; |
| 2351 | } |
| 2352 | |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
| 2356 | static int __io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 2357 | struct sqe_submit *s) |
| 2358 | { |
| 2359 | int ret; |
| 2360 | |
| 2361 | ret = __io_submit_sqe(ctx, req, s, true); |
| 2362 | |
| 2363 | /* |
| 2364 | * We async punt it if the file wasn't marked NOWAIT, or if the file |
| 2365 | * doesn't support non-blocking read/write attempts |
| 2366 | */ |
| 2367 | if (ret == -EAGAIN && (!(req->flags & REQ_F_NOWAIT) || |
| 2368 | (req->flags & REQ_F_MUST_PUNT))) { |
| 2369 | struct io_uring_sqe *sqe_copy; |
| 2370 | |
| 2371 | sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
| 2372 | if (sqe_copy) { |
| 2373 | struct async_list *list; |
| 2374 | |
| 2375 | s->sqe = sqe_copy; |
| 2376 | memcpy(&req->submit, s, sizeof(*s)); |
| 2377 | list = io_async_list_from_sqe(ctx, s->sqe); |
| 2378 | if (!io_add_to_prev_work(list, req)) { |
| 2379 | if (list) |
| 2380 | atomic_inc(&list->cnt); |
| 2381 | INIT_WORK(&req->work, io_sq_wq_submit_work); |
| 2382 | io_queue_async_work(ctx, req); |
| 2383 | } |
| 2384 | |
| 2385 | /* |
| 2386 | * Queued up for async execution, worker will release |
| 2387 | * submit reference when the iocb is actually submitted. |
| 2388 | */ |
| 2389 | return 0; |
| 2390 | } |
| 2391 | } |
| 2392 | |
| 2393 | /* drop submission reference */ |
| 2394 | io_put_req(req); |
| 2395 | |
| 2396 | /* and drop final reference, if we failed */ |
| 2397 | if (ret) { |
| 2398 | io_cqring_add_event(ctx, req->user_data, ret); |
| 2399 | if (req->flags & REQ_F_LINK) |
| 2400 | req->flags |= REQ_F_FAIL_LINK; |
| 2401 | io_put_req(req); |
| 2402 | } |
| 2403 | |
| 2404 | return ret; |
| 2405 | } |
| 2406 | |
| 2407 | static int io_queue_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 2408 | struct sqe_submit *s) |
| 2409 | { |
| 2410 | int ret; |
| 2411 | |
| 2412 | ret = io_req_defer(ctx, req, s->sqe); |
| 2413 | if (ret) { |
| 2414 | if (ret != -EIOCBQUEUED) { |
| 2415 | io_free_req(req); |
| 2416 | io_cqring_add_event(ctx, s->sqe->user_data, ret); |
| 2417 | } |
| 2418 | return 0; |
| 2419 | } |
| 2420 | |
| 2421 | return __io_queue_sqe(ctx, req, s); |
| 2422 | } |
| 2423 | |
| 2424 | static int io_queue_link_head(struct io_ring_ctx *ctx, struct io_kiocb *req, |
| 2425 | struct sqe_submit *s, struct io_kiocb *shadow) |
| 2426 | { |
| 2427 | int ret; |
| 2428 | int need_submit = false; |
| 2429 | |
| 2430 | if (!shadow) |
| 2431 | return io_queue_sqe(ctx, req, s); |
| 2432 | |
| 2433 | /* |
| 2434 | * Mark the first IO in link list as DRAIN, let all the following |
| 2435 | * IOs enter the defer list. all IO needs to be completed before link |
| 2436 | * list. |
| 2437 | */ |
| 2438 | req->flags |= REQ_F_IO_DRAIN; |
| 2439 | ret = io_req_defer(ctx, req, s->sqe); |
| 2440 | if (ret) { |
| 2441 | if (ret != -EIOCBQUEUED) { |
| 2442 | io_free_req(req); |
| 2443 | __io_free_req(shadow); |
| 2444 | io_cqring_add_event(ctx, s->sqe->user_data, ret); |
| 2445 | return 0; |
| 2446 | } |
| 2447 | } else { |
| 2448 | /* |
| 2449 | * If ret == 0 means that all IOs in front of link io are |
| 2450 | * running done. let's queue link head. |
| 2451 | */ |
| 2452 | need_submit = true; |
| 2453 | } |
| 2454 | |
| 2455 | /* Insert shadow req to defer_list, blocking next IOs */ |
| 2456 | spin_lock_irq(&ctx->completion_lock); |
| 2457 | list_add_tail(&shadow->list, &ctx->defer_list); |
| 2458 | spin_unlock_irq(&ctx->completion_lock); |
| 2459 | |
| 2460 | if (need_submit) |
| 2461 | return __io_queue_sqe(ctx, req, s); |
| 2462 | |
| 2463 | return 0; |
| 2464 | } |
| 2465 | |
| 2466 | #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE|IOSQE_IO_DRAIN|IOSQE_IO_LINK) |
| 2467 | |
| 2468 | static void io_submit_sqe(struct io_ring_ctx *ctx, struct sqe_submit *s, |
| 2469 | struct io_submit_state *state, struct io_kiocb **link) |
| 2470 | { |
| 2471 | struct io_uring_sqe *sqe_copy; |
| 2472 | struct io_kiocb *req; |
| 2473 | int ret; |
| 2474 | |
| 2475 | /* enforce forwards compatibility on users */ |
| 2476 | if (unlikely(s->sqe->flags & ~SQE_VALID_FLAGS)) { |
| 2477 | ret = -EINVAL; |
| 2478 | goto err; |
| 2479 | } |
| 2480 | |
| 2481 | req = io_get_req(ctx, state); |
| 2482 | if (unlikely(!req)) { |
| 2483 | ret = -EAGAIN; |
| 2484 | goto err; |
| 2485 | } |
| 2486 | |
| 2487 | ret = io_req_set_file(ctx, s, state, req); |
| 2488 | if (unlikely(ret)) { |
| 2489 | err_req: |
| 2490 | io_free_req(req); |
| 2491 | err: |
| 2492 | io_cqring_add_event(ctx, s->sqe->user_data, ret); |
| 2493 | return; |
| 2494 | } |
| 2495 | |
| 2496 | req->user_data = s->sqe->user_data; |
| 2497 | |
| 2498 | /* |
| 2499 | * If we already have a head request, queue this one for async |
| 2500 | * submittal once the head completes. If we don't have a head but |
| 2501 | * IOSQE_IO_LINK is set in the sqe, start a new head. This one will be |
| 2502 | * submitted sync once the chain is complete. If none of those |
| 2503 | * conditions are true (normal request), then just queue it. |
| 2504 | */ |
| 2505 | if (*link) { |
| 2506 | struct io_kiocb *prev = *link; |
| 2507 | |
| 2508 | sqe_copy = kmemdup(s->sqe, sizeof(*sqe_copy), GFP_KERNEL); |
| 2509 | if (!sqe_copy) { |
| 2510 | ret = -EAGAIN; |
| 2511 | goto err_req; |
| 2512 | } |
| 2513 | |
| 2514 | s->sqe = sqe_copy; |
| 2515 | memcpy(&req->submit, s, sizeof(*s)); |
| 2516 | list_add_tail(&req->list, &prev->link_list); |
| 2517 | } else if (s->sqe->flags & IOSQE_IO_LINK) { |
| 2518 | req->flags |= REQ_F_LINK; |
| 2519 | |
| 2520 | memcpy(&req->submit, s, sizeof(*s)); |
| 2521 | INIT_LIST_HEAD(&req->link_list); |
| 2522 | *link = req; |
| 2523 | } else { |
| 2524 | io_queue_sqe(ctx, req, s); |
| 2525 | } |
| 2526 | } |
| 2527 | |
| 2528 | /* |
| 2529 | * Batched submission is done, ensure local IO is flushed out. |
| 2530 | */ |
| 2531 | static void io_submit_state_end(struct io_submit_state *state) |
| 2532 | { |
| 2533 | blk_finish_plug(&state->plug); |
| 2534 | io_file_put(state); |
| 2535 | if (state->free_reqs) |
| 2536 | kmem_cache_free_bulk(req_cachep, state->free_reqs, |
| 2537 | &state->reqs[state->cur_req]); |
| 2538 | } |
| 2539 | |
| 2540 | /* |
| 2541 | * Start submission side cache. |
| 2542 | */ |
| 2543 | static void io_submit_state_start(struct io_submit_state *state, |
| 2544 | struct io_ring_ctx *ctx, unsigned max_ios) |
| 2545 | { |
| 2546 | blk_start_plug(&state->plug); |
| 2547 | state->free_reqs = 0; |
| 2548 | state->file = NULL; |
| 2549 | state->ios_left = max_ios; |
| 2550 | } |
| 2551 | |
| 2552 | static void io_commit_sqring(struct io_ring_ctx *ctx) |
| 2553 | { |
| 2554 | struct io_rings *rings = ctx->rings; |
| 2555 | |
| 2556 | if (ctx->cached_sq_head != READ_ONCE(rings->sq.head)) { |
| 2557 | /* |
| 2558 | * Ensure any loads from the SQEs are done at this point, |
| 2559 | * since once we write the new head, the application could |
| 2560 | * write new data to them. |
| 2561 | */ |
| 2562 | smp_store_release(&rings->sq.head, ctx->cached_sq_head); |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | /* |
| 2567 | * Fetch an sqe, if one is available. Note that s->sqe will point to memory |
| 2568 | * that is mapped by userspace. This means that care needs to be taken to |
| 2569 | * ensure that reads are stable, as we cannot rely on userspace always |
| 2570 | * being a good citizen. If members of the sqe are validated and then later |
| 2571 | * used, it's important that those reads are done through READ_ONCE() to |
| 2572 | * prevent a re-load down the line. |
| 2573 | */ |
| 2574 | static bool io_get_sqring(struct io_ring_ctx *ctx, struct sqe_submit *s) |
| 2575 | { |
| 2576 | struct io_rings *rings = ctx->rings; |
| 2577 | u32 *sq_array = ctx->sq_array; |
| 2578 | unsigned head; |
| 2579 | |
| 2580 | /* |
| 2581 | * The cached sq head (or cq tail) serves two purposes: |
| 2582 | * |
| 2583 | * 1) allows us to batch the cost of updating the user visible |
| 2584 | * head updates. |
| 2585 | * 2) allows the kernel side to track the head on its own, even |
| 2586 | * though the application is the one updating it. |
| 2587 | */ |
| 2588 | head = ctx->cached_sq_head; |
| 2589 | /* make sure SQ entry isn't read before tail */ |
| 2590 | if (head == smp_load_acquire(&rings->sq.tail)) |
| 2591 | return false; |
| 2592 | |
| 2593 | head = READ_ONCE(sq_array[head & ctx->sq_mask]); |
| 2594 | if (head < ctx->sq_entries) { |
| 2595 | s->index = head; |
| 2596 | s->sqe = &ctx->sq_sqes[head]; |
| 2597 | s->sequence = ctx->cached_sq_head; |
| 2598 | ctx->cached_sq_head++; |
| 2599 | return true; |
| 2600 | } |
| 2601 | |
| 2602 | /* drop invalid entries */ |
| 2603 | ctx->cached_sq_head++; |
| 2604 | ctx->cached_sq_dropped++; |
| 2605 | WRITE_ONCE(rings->sq_dropped, ctx->cached_sq_dropped); |
| 2606 | return false; |
| 2607 | } |
| 2608 | |
| 2609 | static int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr, |
| 2610 | bool has_user, bool mm_fault) |
| 2611 | { |
| 2612 | struct io_submit_state state, *statep = NULL; |
| 2613 | struct io_kiocb *link = NULL; |
| 2614 | struct io_kiocb *shadow_req = NULL; |
| 2615 | bool prev_was_link = false; |
| 2616 | int i, submitted = 0; |
| 2617 | |
| 2618 | if (nr > IO_PLUG_THRESHOLD) { |
| 2619 | io_submit_state_start(&state, ctx, nr); |
| 2620 | statep = &state; |
| 2621 | } |
| 2622 | |
| 2623 | for (i = 0; i < nr; i++) { |
| 2624 | struct sqe_submit s; |
| 2625 | |
| 2626 | if (!io_get_sqring(ctx, &s)) |
| 2627 | break; |
| 2628 | |
| 2629 | /* |
| 2630 | * If previous wasn't linked and we have a linked command, |
| 2631 | * that's the end of the chain. Submit the previous link. |
| 2632 | */ |
| 2633 | if (!prev_was_link && link) { |
| 2634 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
| 2635 | link = NULL; |
| 2636 | shadow_req = NULL; |
| 2637 | } |
| 2638 | prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0; |
| 2639 | |
| 2640 | if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) { |
| 2641 | if (!shadow_req) { |
| 2642 | shadow_req = io_get_req(ctx, NULL); |
| 2643 | if (unlikely(!shadow_req)) |
| 2644 | goto out; |
| 2645 | shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN); |
| 2646 | refcount_dec(&shadow_req->refs); |
| 2647 | } |
| 2648 | shadow_req->sequence = s.sequence; |
| 2649 | } |
| 2650 | |
| 2651 | out: |
| 2652 | if (unlikely(mm_fault)) { |
| 2653 | io_cqring_add_event(ctx, s.sqe->user_data, |
| 2654 | -EFAULT); |
| 2655 | } else { |
| 2656 | s.has_user = has_user; |
| 2657 | s.needs_lock = true; |
| 2658 | s.needs_fixed_file = true; |
| 2659 | io_submit_sqe(ctx, &s, statep, &link); |
| 2660 | submitted++; |
| 2661 | } |
| 2662 | } |
| 2663 | |
| 2664 | if (link) |
| 2665 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
| 2666 | if (statep) |
| 2667 | io_submit_state_end(&state); |
| 2668 | |
| 2669 | return submitted; |
| 2670 | } |
| 2671 | |
| 2672 | static int io_sq_thread(void *data) |
| 2673 | { |
| 2674 | struct io_ring_ctx *ctx = data; |
| 2675 | struct mm_struct *cur_mm = NULL; |
| 2676 | const struct cred *old_cred; |
| 2677 | mm_segment_t old_fs; |
| 2678 | DEFINE_WAIT(wait); |
| 2679 | unsigned inflight; |
| 2680 | unsigned long timeout; |
| 2681 | |
| 2682 | complete(&ctx->sqo_thread_started); |
| 2683 | |
| 2684 | old_fs = get_fs(); |
| 2685 | set_fs(USER_DS); |
| 2686 | old_cred = override_creds(ctx->creds); |
| 2687 | |
| 2688 | timeout = inflight = 0; |
| 2689 | while (!kthread_should_park()) { |
| 2690 | bool mm_fault = false; |
| 2691 | unsigned int to_submit; |
| 2692 | |
| 2693 | if (inflight) { |
| 2694 | unsigned nr_events = 0; |
| 2695 | |
| 2696 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 2697 | /* |
| 2698 | * inflight is the count of the maximum possible |
| 2699 | * entries we submitted, but it can be smaller |
| 2700 | * if we dropped some of them. If we don't have |
| 2701 | * poll entries available, then we know that we |
| 2702 | * have nothing left to poll for. Reset the |
| 2703 | * inflight count to zero in that case. |
| 2704 | */ |
| 2705 | mutex_lock(&ctx->uring_lock); |
| 2706 | if (!list_empty(&ctx->poll_list)) |
| 2707 | __io_iopoll_check(ctx, &nr_events, 0); |
| 2708 | else |
| 2709 | inflight = 0; |
| 2710 | mutex_unlock(&ctx->uring_lock); |
| 2711 | } else { |
| 2712 | /* |
| 2713 | * Normal IO, just pretend everything completed. |
| 2714 | * We don't have to poll completions for that. |
| 2715 | */ |
| 2716 | nr_events = inflight; |
| 2717 | } |
| 2718 | |
| 2719 | inflight -= nr_events; |
| 2720 | if (!inflight) |
| 2721 | timeout = jiffies + ctx->sq_thread_idle; |
| 2722 | } |
| 2723 | |
| 2724 | to_submit = io_sqring_entries(ctx); |
| 2725 | if (!to_submit) { |
| 2726 | /* |
| 2727 | * We're polling. If we're within the defined idle |
| 2728 | * period, then let us spin without work before going |
| 2729 | * to sleep. |
| 2730 | */ |
| 2731 | if (inflight || !time_after(jiffies, timeout)) { |
| 2732 | cond_resched(); |
| 2733 | continue; |
| 2734 | } |
| 2735 | |
| 2736 | /* |
| 2737 | * Drop cur_mm before scheduling, we can't hold it for |
| 2738 | * long periods (or over schedule()). Do this before |
| 2739 | * adding ourselves to the waitqueue, as the unuse/drop |
| 2740 | * may sleep. |
| 2741 | */ |
| 2742 | if (cur_mm) { |
| 2743 | unuse_mm(cur_mm); |
| 2744 | mmput(cur_mm); |
| 2745 | cur_mm = NULL; |
| 2746 | } |
| 2747 | |
| 2748 | prepare_to_wait(&ctx->sqo_wait, &wait, |
| 2749 | TASK_INTERRUPTIBLE); |
| 2750 | |
| 2751 | /* Tell userspace we may need a wakeup call */ |
| 2752 | ctx->rings->sq_flags |= IORING_SQ_NEED_WAKEUP; |
| 2753 | /* make sure to read SQ tail after writing flags */ |
| 2754 | smp_mb(); |
| 2755 | |
| 2756 | to_submit = io_sqring_entries(ctx); |
| 2757 | if (!to_submit) { |
| 2758 | if (kthread_should_park()) { |
| 2759 | finish_wait(&ctx->sqo_wait, &wait); |
| 2760 | break; |
| 2761 | } |
| 2762 | if (signal_pending(current)) |
| 2763 | flush_signals(current); |
| 2764 | schedule(); |
| 2765 | finish_wait(&ctx->sqo_wait, &wait); |
| 2766 | |
| 2767 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 2768 | continue; |
| 2769 | } |
| 2770 | finish_wait(&ctx->sqo_wait, &wait); |
| 2771 | |
| 2772 | ctx->rings->sq_flags &= ~IORING_SQ_NEED_WAKEUP; |
| 2773 | } |
| 2774 | |
| 2775 | /* Unless all new commands are FIXED regions, grab mm */ |
| 2776 | if (!cur_mm) { |
| 2777 | mm_fault = !mmget_not_zero(ctx->sqo_mm); |
| 2778 | if (!mm_fault) { |
| 2779 | use_mm(ctx->sqo_mm); |
| 2780 | cur_mm = ctx->sqo_mm; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | to_submit = min(to_submit, ctx->sq_entries); |
| 2785 | inflight += io_submit_sqes(ctx, to_submit, cur_mm != NULL, |
| 2786 | mm_fault); |
| 2787 | |
| 2788 | /* Commit SQ ring head once we've consumed all SQEs */ |
| 2789 | io_commit_sqring(ctx); |
| 2790 | } |
| 2791 | |
| 2792 | set_fs(old_fs); |
| 2793 | if (cur_mm) { |
| 2794 | unuse_mm(cur_mm); |
| 2795 | mmput(cur_mm); |
| 2796 | } |
| 2797 | revert_creds(old_cred); |
| 2798 | |
| 2799 | kthread_parkme(); |
| 2800 | |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
| 2804 | static int io_ring_submit(struct io_ring_ctx *ctx, unsigned int to_submit) |
| 2805 | { |
| 2806 | struct io_submit_state state, *statep = NULL; |
| 2807 | struct io_kiocb *link = NULL; |
| 2808 | struct io_kiocb *shadow_req = NULL; |
| 2809 | bool prev_was_link = false; |
| 2810 | int i, submit = 0; |
| 2811 | |
| 2812 | if (to_submit > IO_PLUG_THRESHOLD) { |
| 2813 | io_submit_state_start(&state, ctx, to_submit); |
| 2814 | statep = &state; |
| 2815 | } |
| 2816 | |
| 2817 | for (i = 0; i < to_submit; i++) { |
| 2818 | struct sqe_submit s; |
| 2819 | |
| 2820 | if (!io_get_sqring(ctx, &s)) |
| 2821 | break; |
| 2822 | |
| 2823 | /* |
| 2824 | * If previous wasn't linked and we have a linked command, |
| 2825 | * that's the end of the chain. Submit the previous link. |
| 2826 | */ |
| 2827 | if (!prev_was_link && link) { |
| 2828 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
| 2829 | link = NULL; |
| 2830 | shadow_req = NULL; |
| 2831 | } |
| 2832 | prev_was_link = (s.sqe->flags & IOSQE_IO_LINK) != 0; |
| 2833 | |
| 2834 | if (link && (s.sqe->flags & IOSQE_IO_DRAIN)) { |
| 2835 | if (!shadow_req) { |
| 2836 | shadow_req = io_get_req(ctx, NULL); |
| 2837 | if (unlikely(!shadow_req)) |
| 2838 | goto out; |
| 2839 | shadow_req->flags |= (REQ_F_IO_DRAIN | REQ_F_SHADOW_DRAIN); |
| 2840 | refcount_dec(&shadow_req->refs); |
| 2841 | } |
| 2842 | shadow_req->sequence = s.sequence; |
| 2843 | } |
| 2844 | |
| 2845 | out: |
| 2846 | s.has_user = true; |
| 2847 | s.needs_lock = false; |
| 2848 | s.needs_fixed_file = false; |
| 2849 | submit++; |
| 2850 | io_submit_sqe(ctx, &s, statep, &link); |
| 2851 | } |
| 2852 | |
| 2853 | if (link) |
| 2854 | io_queue_link_head(ctx, link, &link->submit, shadow_req); |
| 2855 | if (statep) |
| 2856 | io_submit_state_end(statep); |
| 2857 | |
| 2858 | io_commit_sqring(ctx); |
| 2859 | |
| 2860 | return submit; |
| 2861 | } |
| 2862 | |
| 2863 | struct io_wait_queue { |
| 2864 | struct wait_queue_entry wq; |
| 2865 | struct io_ring_ctx *ctx; |
| 2866 | unsigned to_wait; |
| 2867 | unsigned nr_timeouts; |
| 2868 | }; |
| 2869 | |
| 2870 | static inline bool io_should_wake(struct io_wait_queue *iowq) |
| 2871 | { |
| 2872 | struct io_ring_ctx *ctx = iowq->ctx; |
| 2873 | |
| 2874 | /* |
| 2875 | * Wake up if we have enough events, or if a timeout occured since we |
| 2876 | * started waiting. For timeouts, we always want to return to userspace, |
| 2877 | * regardless of event count. |
| 2878 | */ |
| 2879 | return io_cqring_events(ctx->rings) >= iowq->to_wait || |
| 2880 | atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; |
| 2881 | } |
| 2882 | |
| 2883 | static int io_wake_function(struct wait_queue_entry *curr, unsigned int mode, |
| 2884 | int wake_flags, void *key) |
| 2885 | { |
| 2886 | struct io_wait_queue *iowq = container_of(curr, struct io_wait_queue, |
| 2887 | wq); |
| 2888 | |
| 2889 | if (!io_should_wake(iowq)) |
| 2890 | return -1; |
| 2891 | |
| 2892 | return autoremove_wake_function(curr, mode, wake_flags, key); |
| 2893 | } |
| 2894 | |
| 2895 | /* |
| 2896 | * Wait until events become available, if we don't already have some. The |
| 2897 | * application must reap them itself, as they reside on the shared cq ring. |
| 2898 | */ |
| 2899 | static int io_cqring_wait(struct io_ring_ctx *ctx, int min_events, |
| 2900 | const sigset_t __user *sig, size_t sigsz) |
| 2901 | { |
| 2902 | struct io_wait_queue iowq = { |
| 2903 | .wq = { |
| 2904 | .private = current, |
| 2905 | .func = io_wake_function, |
| 2906 | .entry = LIST_HEAD_INIT(iowq.wq.entry), |
| 2907 | }, |
| 2908 | .ctx = ctx, |
| 2909 | .to_wait = min_events, |
| 2910 | }; |
| 2911 | struct io_rings *rings = ctx->rings; |
| 2912 | int ret; |
| 2913 | |
| 2914 | if (io_cqring_events(rings) >= min_events) |
| 2915 | return 0; |
| 2916 | |
| 2917 | if (sig) { |
| 2918 | #ifdef CONFIG_COMPAT |
| 2919 | if (in_compat_syscall()) |
| 2920 | ret = set_compat_user_sigmask((const compat_sigset_t __user *)sig, |
| 2921 | sigsz); |
| 2922 | else |
| 2923 | #endif |
| 2924 | ret = set_user_sigmask(sig, sigsz); |
| 2925 | |
| 2926 | if (ret) |
| 2927 | return ret; |
| 2928 | } |
| 2929 | |
| 2930 | ret = 0; |
| 2931 | iowq.nr_timeouts = atomic_read(&ctx->cq_timeouts); |
| 2932 | do { |
| 2933 | prepare_to_wait_exclusive(&ctx->wait, &iowq.wq, |
| 2934 | TASK_INTERRUPTIBLE); |
| 2935 | if (io_should_wake(&iowq)) |
| 2936 | break; |
| 2937 | schedule(); |
| 2938 | if (signal_pending(current)) { |
| 2939 | ret = -ERESTARTSYS; |
| 2940 | break; |
| 2941 | } |
| 2942 | } while (1); |
| 2943 | finish_wait(&ctx->wait, &iowq.wq); |
| 2944 | |
| 2945 | restore_saved_sigmask_unless(ret == -ERESTARTSYS); |
| 2946 | if (ret == -ERESTARTSYS) |
| 2947 | ret = -EINTR; |
| 2948 | |
| 2949 | return READ_ONCE(rings->cq.head) == READ_ONCE(rings->cq.tail) ? ret : 0; |
| 2950 | } |
| 2951 | |
| 2952 | static void __io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 2953 | { |
| 2954 | #if defined(CONFIG_UNIX) |
| 2955 | if (ctx->ring_sock) { |
| 2956 | struct sock *sock = ctx->ring_sock->sk; |
| 2957 | struct sk_buff *skb; |
| 2958 | |
| 2959 | while ((skb = skb_dequeue(&sock->sk_receive_queue)) != NULL) |
| 2960 | kfree_skb(skb); |
| 2961 | } |
| 2962 | #else |
| 2963 | int i; |
| 2964 | |
| 2965 | for (i = 0; i < ctx->nr_user_files; i++) |
| 2966 | fput(ctx->user_files[i]); |
| 2967 | #endif |
| 2968 | } |
| 2969 | |
| 2970 | static int io_sqe_files_unregister(struct io_ring_ctx *ctx) |
| 2971 | { |
| 2972 | if (!ctx->user_files) |
| 2973 | return -ENXIO; |
| 2974 | |
| 2975 | __io_sqe_files_unregister(ctx); |
| 2976 | kfree(ctx->user_files); |
| 2977 | ctx->user_files = NULL; |
| 2978 | ctx->nr_user_files = 0; |
| 2979 | return 0; |
| 2980 | } |
| 2981 | |
| 2982 | static void io_sq_thread_stop(struct io_ring_ctx *ctx) |
| 2983 | { |
| 2984 | if (ctx->sqo_thread) { |
| 2985 | wait_for_completion(&ctx->sqo_thread_started); |
| 2986 | /* |
| 2987 | * The park is a bit of a work-around, without it we get |
| 2988 | * warning spews on shutdown with SQPOLL set and affinity |
| 2989 | * set to a single CPU. |
| 2990 | */ |
| 2991 | kthread_park(ctx->sqo_thread); |
| 2992 | kthread_stop(ctx->sqo_thread); |
| 2993 | ctx->sqo_thread = NULL; |
| 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | static void io_finish_async(struct io_ring_ctx *ctx) |
| 2998 | { |
| 2999 | int i; |
| 3000 | |
| 3001 | io_sq_thread_stop(ctx); |
| 3002 | |
| 3003 | for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) { |
| 3004 | if (ctx->sqo_wq[i]) { |
| 3005 | destroy_workqueue(ctx->sqo_wq[i]); |
| 3006 | ctx->sqo_wq[i] = NULL; |
| 3007 | } |
| 3008 | } |
| 3009 | } |
| 3010 | |
| 3011 | #if defined(CONFIG_UNIX) |
| 3012 | static void io_destruct_skb(struct sk_buff *skb) |
| 3013 | { |
| 3014 | struct io_ring_ctx *ctx = skb->sk->sk_user_data; |
| 3015 | int i; |
| 3016 | |
| 3017 | for (i = 0; i < ARRAY_SIZE(ctx->sqo_wq); i++) |
| 3018 | if (ctx->sqo_wq[i]) |
| 3019 | flush_workqueue(ctx->sqo_wq[i]); |
| 3020 | |
| 3021 | unix_destruct_scm(skb); |
| 3022 | } |
| 3023 | |
| 3024 | /* |
| 3025 | * Ensure the UNIX gc is aware of our file set, so we are certain that |
| 3026 | * the io_uring can be safely unregistered on process exit, even if we have |
| 3027 | * loops in the file referencing. |
| 3028 | */ |
| 3029 | static int __io_sqe_files_scm(struct io_ring_ctx *ctx, int nr, int offset) |
| 3030 | { |
| 3031 | struct sock *sk = ctx->ring_sock->sk; |
| 3032 | struct scm_fp_list *fpl; |
| 3033 | struct sk_buff *skb; |
| 3034 | int i; |
| 3035 | |
| 3036 | if (!capable(CAP_SYS_RESOURCE) && !capable(CAP_SYS_ADMIN)) { |
| 3037 | unsigned long inflight = ctx->user->unix_inflight + nr; |
| 3038 | |
| 3039 | if (inflight > task_rlimit(current, RLIMIT_NOFILE)) |
| 3040 | return -EMFILE; |
| 3041 | } |
| 3042 | |
| 3043 | fpl = kzalloc(sizeof(*fpl), GFP_KERNEL); |
| 3044 | if (!fpl) |
| 3045 | return -ENOMEM; |
| 3046 | |
| 3047 | skb = alloc_skb(0, GFP_KERNEL); |
| 3048 | if (!skb) { |
| 3049 | kfree(fpl); |
| 3050 | return -ENOMEM; |
| 3051 | } |
| 3052 | |
| 3053 | skb->sk = sk; |
| 3054 | skb->destructor = io_destruct_skb; |
| 3055 | |
| 3056 | fpl->user = get_uid(ctx->user); |
| 3057 | for (i = 0; i < nr; i++) { |
| 3058 | fpl->fp[i] = get_file(ctx->user_files[i + offset]); |
| 3059 | unix_inflight(fpl->user, fpl->fp[i]); |
| 3060 | } |
| 3061 | |
| 3062 | fpl->max = fpl->count = nr; |
| 3063 | UNIXCB(skb).fp = fpl; |
| 3064 | refcount_add(skb->truesize, &sk->sk_wmem_alloc); |
| 3065 | skb_queue_head(&sk->sk_receive_queue, skb); |
| 3066 | |
| 3067 | for (i = 0; i < nr; i++) |
| 3068 | fput(fpl->fp[i]); |
| 3069 | |
| 3070 | return 0; |
| 3071 | } |
| 3072 | |
| 3073 | /* |
| 3074 | * If UNIX sockets are enabled, fd passing can cause a reference cycle which |
| 3075 | * causes regular reference counting to break down. We rely on the UNIX |
| 3076 | * garbage collection to take care of this problem for us. |
| 3077 | */ |
| 3078 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 3079 | { |
| 3080 | unsigned left, total; |
| 3081 | int ret = 0; |
| 3082 | |
| 3083 | total = 0; |
| 3084 | left = ctx->nr_user_files; |
| 3085 | while (left) { |
| 3086 | unsigned this_files = min_t(unsigned, left, SCM_MAX_FD); |
| 3087 | |
| 3088 | ret = __io_sqe_files_scm(ctx, this_files, total); |
| 3089 | if (ret) |
| 3090 | break; |
| 3091 | left -= this_files; |
| 3092 | total += this_files; |
| 3093 | } |
| 3094 | |
| 3095 | if (!ret) |
| 3096 | return 0; |
| 3097 | |
| 3098 | while (total < ctx->nr_user_files) { |
| 3099 | fput(ctx->user_files[total]); |
| 3100 | total++; |
| 3101 | } |
| 3102 | |
| 3103 | return ret; |
| 3104 | } |
| 3105 | #else |
| 3106 | static int io_sqe_files_scm(struct io_ring_ctx *ctx) |
| 3107 | { |
| 3108 | return 0; |
| 3109 | } |
| 3110 | #endif |
| 3111 | |
| 3112 | static int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, |
| 3113 | unsigned nr_args) |
| 3114 | { |
| 3115 | __s32 __user *fds = (__s32 __user *) arg; |
| 3116 | int fd, ret = 0; |
| 3117 | unsigned i; |
| 3118 | |
| 3119 | if (ctx->user_files) |
| 3120 | return -EBUSY; |
| 3121 | if (!nr_args) |
| 3122 | return -EINVAL; |
| 3123 | if (nr_args > IORING_MAX_FIXED_FILES) |
| 3124 | return -EMFILE; |
| 3125 | |
| 3126 | ctx->user_files = kcalloc(nr_args, sizeof(struct file *), GFP_KERNEL); |
| 3127 | if (!ctx->user_files) |
| 3128 | return -ENOMEM; |
| 3129 | |
| 3130 | for (i = 0; i < nr_args; i++) { |
| 3131 | ret = -EFAULT; |
| 3132 | if (copy_from_user(&fd, &fds[i], sizeof(fd))) |
| 3133 | break; |
| 3134 | |
| 3135 | ctx->user_files[i] = fget(fd); |
| 3136 | |
| 3137 | ret = -EBADF; |
| 3138 | if (!ctx->user_files[i]) |
| 3139 | break; |
| 3140 | /* |
| 3141 | * Don't allow io_uring instances to be registered. If UNIX |
| 3142 | * isn't enabled, then this causes a reference cycle and this |
| 3143 | * instance can never get freed. If UNIX is enabled we'll |
| 3144 | * handle it just fine, but there's still no point in allowing |
| 3145 | * a ring fd as it doesn't support regular read/write anyway. |
| 3146 | */ |
| 3147 | if (ctx->user_files[i]->f_op == &io_uring_fops) { |
| 3148 | fput(ctx->user_files[i]); |
| 3149 | break; |
| 3150 | } |
| 3151 | ctx->nr_user_files++; |
| 3152 | ret = 0; |
| 3153 | } |
| 3154 | |
| 3155 | if (ret) { |
| 3156 | for (i = 0; i < ctx->nr_user_files; i++) |
| 3157 | fput(ctx->user_files[i]); |
| 3158 | |
| 3159 | kfree(ctx->user_files); |
| 3160 | ctx->user_files = NULL; |
| 3161 | ctx->nr_user_files = 0; |
| 3162 | return ret; |
| 3163 | } |
| 3164 | |
| 3165 | ret = io_sqe_files_scm(ctx); |
| 3166 | if (ret) |
| 3167 | io_sqe_files_unregister(ctx); |
| 3168 | |
| 3169 | return ret; |
| 3170 | } |
| 3171 | |
| 3172 | static int io_sq_offload_start(struct io_ring_ctx *ctx, |
| 3173 | struct io_uring_params *p) |
| 3174 | { |
| 3175 | int ret; |
| 3176 | |
| 3177 | init_waitqueue_head(&ctx->sqo_wait); |
| 3178 | mmgrab(current->mm); |
| 3179 | ctx->sqo_mm = current->mm; |
| 3180 | |
| 3181 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 3182 | ret = -EPERM; |
| 3183 | if (!capable(CAP_SYS_ADMIN)) |
| 3184 | goto err; |
| 3185 | |
| 3186 | ctx->sq_thread_idle = msecs_to_jiffies(p->sq_thread_idle); |
| 3187 | if (!ctx->sq_thread_idle) |
| 3188 | ctx->sq_thread_idle = HZ; |
| 3189 | |
| 3190 | if (p->flags & IORING_SETUP_SQ_AFF) { |
| 3191 | int cpu = p->sq_thread_cpu; |
| 3192 | |
| 3193 | ret = -EINVAL; |
| 3194 | if (cpu >= nr_cpu_ids) |
| 3195 | goto err; |
| 3196 | if (!cpu_online(cpu)) |
| 3197 | goto err; |
| 3198 | |
| 3199 | ctx->sqo_thread = kthread_create_on_cpu(io_sq_thread, |
| 3200 | ctx, cpu, |
| 3201 | "io_uring-sq"); |
| 3202 | } else { |
| 3203 | ctx->sqo_thread = kthread_create(io_sq_thread, ctx, |
| 3204 | "io_uring-sq"); |
| 3205 | } |
| 3206 | if (IS_ERR(ctx->sqo_thread)) { |
| 3207 | ret = PTR_ERR(ctx->sqo_thread); |
| 3208 | ctx->sqo_thread = NULL; |
| 3209 | goto err; |
| 3210 | } |
| 3211 | wake_up_process(ctx->sqo_thread); |
| 3212 | } else if (p->flags & IORING_SETUP_SQ_AFF) { |
| 3213 | /* Can't have SQ_AFF without SQPOLL */ |
| 3214 | ret = -EINVAL; |
| 3215 | goto err; |
| 3216 | } |
| 3217 | |
| 3218 | /* Do QD, or 2 * CPUS, whatever is smallest */ |
| 3219 | ctx->sqo_wq[0] = alloc_workqueue("io_ring-wq", |
| 3220 | WQ_UNBOUND | WQ_FREEZABLE, |
| 3221 | min(ctx->sq_entries - 1, 2 * num_online_cpus())); |
| 3222 | if (!ctx->sqo_wq[0]) { |
| 3223 | ret = -ENOMEM; |
| 3224 | goto err; |
| 3225 | } |
| 3226 | |
| 3227 | /* |
| 3228 | * This is for buffered writes, where we want to limit the parallelism |
| 3229 | * due to file locking in file systems. As "normal" buffered writes |
| 3230 | * should parellelize on writeout quite nicely, limit us to having 2 |
| 3231 | * pending. This avoids massive contention on the inode when doing |
| 3232 | * buffered async writes. |
| 3233 | */ |
| 3234 | ctx->sqo_wq[1] = alloc_workqueue("io_ring-write-wq", |
| 3235 | WQ_UNBOUND | WQ_FREEZABLE, 2); |
| 3236 | if (!ctx->sqo_wq[1]) { |
| 3237 | ret = -ENOMEM; |
| 3238 | goto err; |
| 3239 | } |
| 3240 | |
| 3241 | return 0; |
| 3242 | err: |
| 3243 | io_finish_async(ctx); |
| 3244 | mmdrop(ctx->sqo_mm); |
| 3245 | ctx->sqo_mm = NULL; |
| 3246 | return ret; |
| 3247 | } |
| 3248 | |
| 3249 | static void io_unaccount_mem(struct user_struct *user, unsigned long nr_pages) |
| 3250 | { |
| 3251 | atomic_long_sub(nr_pages, &user->locked_vm); |
| 3252 | } |
| 3253 | |
| 3254 | static int io_account_mem(struct user_struct *user, unsigned long nr_pages) |
| 3255 | { |
| 3256 | unsigned long page_limit, cur_pages, new_pages; |
| 3257 | |
| 3258 | /* Don't allow more pages than we can safely lock */ |
| 3259 | page_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 3260 | |
| 3261 | do { |
| 3262 | cur_pages = atomic_long_read(&user->locked_vm); |
| 3263 | new_pages = cur_pages + nr_pages; |
| 3264 | if (new_pages > page_limit) |
| 3265 | return -ENOMEM; |
| 3266 | } while (atomic_long_cmpxchg(&user->locked_vm, cur_pages, |
| 3267 | new_pages) != cur_pages); |
| 3268 | |
| 3269 | return 0; |
| 3270 | } |
| 3271 | |
| 3272 | static void io_mem_free(void *ptr) |
| 3273 | { |
| 3274 | struct page *page; |
| 3275 | |
| 3276 | if (!ptr) |
| 3277 | return; |
| 3278 | |
| 3279 | page = virt_to_head_page(ptr); |
| 3280 | if (put_page_testzero(page)) |
| 3281 | free_compound_page(page); |
| 3282 | } |
| 3283 | |
| 3284 | static void *io_mem_alloc(size_t size) |
| 3285 | { |
| 3286 | gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP | |
| 3287 | __GFP_NORETRY; |
| 3288 | |
| 3289 | return (void *) __get_free_pages(gfp_flags, get_order(size)); |
| 3290 | } |
| 3291 | |
| 3292 | static unsigned long rings_size(unsigned sq_entries, unsigned cq_entries, |
| 3293 | size_t *sq_offset) |
| 3294 | { |
| 3295 | struct io_rings *rings; |
| 3296 | size_t off, sq_array_size; |
| 3297 | |
| 3298 | off = struct_size(rings, cqes, cq_entries); |
| 3299 | if (off == SIZE_MAX) |
| 3300 | return SIZE_MAX; |
| 3301 | |
| 3302 | #ifdef CONFIG_SMP |
| 3303 | off = ALIGN(off, SMP_CACHE_BYTES); |
| 3304 | if (off == 0) |
| 3305 | return SIZE_MAX; |
| 3306 | #endif |
| 3307 | |
| 3308 | sq_array_size = array_size(sizeof(u32), sq_entries); |
| 3309 | if (sq_array_size == SIZE_MAX) |
| 3310 | return SIZE_MAX; |
| 3311 | |
| 3312 | if (check_add_overflow(off, sq_array_size, &off)) |
| 3313 | return SIZE_MAX; |
| 3314 | |
| 3315 | if (sq_offset) |
| 3316 | *sq_offset = off; |
| 3317 | |
| 3318 | return off; |
| 3319 | } |
| 3320 | |
| 3321 | static unsigned long ring_pages(unsigned sq_entries, unsigned cq_entries) |
| 3322 | { |
| 3323 | size_t pages; |
| 3324 | |
| 3325 | pages = (size_t)1 << get_order( |
| 3326 | rings_size(sq_entries, cq_entries, NULL)); |
| 3327 | pages += (size_t)1 << get_order( |
| 3328 | array_size(sizeof(struct io_uring_sqe), sq_entries)); |
| 3329 | |
| 3330 | return pages; |
| 3331 | } |
| 3332 | |
| 3333 | static int io_sqe_buffer_unregister(struct io_ring_ctx *ctx) |
| 3334 | { |
| 3335 | int i, j; |
| 3336 | |
| 3337 | if (!ctx->user_bufs) |
| 3338 | return -ENXIO; |
| 3339 | |
| 3340 | for (i = 0; i < ctx->nr_user_bufs; i++) { |
| 3341 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 3342 | |
| 3343 | for (j = 0; j < imu->nr_bvecs; j++) |
| 3344 | put_user_page(imu->bvec[j].bv_page); |
| 3345 | |
| 3346 | if (ctx->account_mem) |
| 3347 | io_unaccount_mem(ctx->user, imu->nr_bvecs); |
| 3348 | kvfree(imu->bvec); |
| 3349 | imu->nr_bvecs = 0; |
| 3350 | } |
| 3351 | |
| 3352 | kfree(ctx->user_bufs); |
| 3353 | ctx->user_bufs = NULL; |
| 3354 | ctx->nr_user_bufs = 0; |
| 3355 | return 0; |
| 3356 | } |
| 3357 | |
| 3358 | static int io_copy_iov(struct io_ring_ctx *ctx, struct iovec *dst, |
| 3359 | void __user *arg, unsigned index) |
| 3360 | { |
| 3361 | struct iovec __user *src; |
| 3362 | |
| 3363 | #ifdef CONFIG_COMPAT |
| 3364 | if (ctx->compat) { |
| 3365 | struct compat_iovec __user *ciovs; |
| 3366 | struct compat_iovec ciov; |
| 3367 | |
| 3368 | ciovs = (struct compat_iovec __user *) arg; |
| 3369 | if (copy_from_user(&ciov, &ciovs[index], sizeof(ciov))) |
| 3370 | return -EFAULT; |
| 3371 | |
| 3372 | dst->iov_base = (void __user *) (unsigned long) ciov.iov_base; |
| 3373 | dst->iov_len = ciov.iov_len; |
| 3374 | return 0; |
| 3375 | } |
| 3376 | #endif |
| 3377 | src = (struct iovec __user *) arg; |
| 3378 | if (copy_from_user(dst, &src[index], sizeof(*dst))) |
| 3379 | return -EFAULT; |
| 3380 | return 0; |
| 3381 | } |
| 3382 | |
| 3383 | static int io_sqe_buffer_register(struct io_ring_ctx *ctx, void __user *arg, |
| 3384 | unsigned nr_args) |
| 3385 | { |
| 3386 | struct vm_area_struct **vmas = NULL; |
| 3387 | struct page **pages = NULL; |
| 3388 | int i, j, got_pages = 0; |
| 3389 | int ret = -EINVAL; |
| 3390 | |
| 3391 | if (ctx->user_bufs) |
| 3392 | return -EBUSY; |
| 3393 | if (!nr_args || nr_args > UIO_MAXIOV) |
| 3394 | return -EINVAL; |
| 3395 | |
| 3396 | ctx->user_bufs = kcalloc(nr_args, sizeof(struct io_mapped_ubuf), |
| 3397 | GFP_KERNEL); |
| 3398 | if (!ctx->user_bufs) |
| 3399 | return -ENOMEM; |
| 3400 | |
| 3401 | for (i = 0; i < nr_args; i++) { |
| 3402 | struct io_mapped_ubuf *imu = &ctx->user_bufs[i]; |
| 3403 | unsigned long off, start, end, ubuf; |
| 3404 | int pret, nr_pages; |
| 3405 | struct iovec iov; |
| 3406 | size_t size; |
| 3407 | |
| 3408 | ret = io_copy_iov(ctx, &iov, arg, i); |
| 3409 | if (ret) |
| 3410 | goto err; |
| 3411 | |
| 3412 | /* |
| 3413 | * Don't impose further limits on the size and buffer |
| 3414 | * constraints here, we'll -EINVAL later when IO is |
| 3415 | * submitted if they are wrong. |
| 3416 | */ |
| 3417 | ret = -EFAULT; |
| 3418 | if (!iov.iov_base || !iov.iov_len) |
| 3419 | goto err; |
| 3420 | |
| 3421 | /* arbitrary limit, but we need something */ |
| 3422 | if (iov.iov_len > SZ_1G) |
| 3423 | goto err; |
| 3424 | |
| 3425 | ubuf = (unsigned long) iov.iov_base; |
| 3426 | end = (ubuf + iov.iov_len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 3427 | start = ubuf >> PAGE_SHIFT; |
| 3428 | nr_pages = end - start; |
| 3429 | |
| 3430 | if (ctx->account_mem) { |
| 3431 | ret = io_account_mem(ctx->user, nr_pages); |
| 3432 | if (ret) |
| 3433 | goto err; |
| 3434 | } |
| 3435 | |
| 3436 | ret = 0; |
| 3437 | if (!pages || nr_pages > got_pages) { |
| 3438 | kfree(vmas); |
| 3439 | kfree(pages); |
| 3440 | pages = kvmalloc_array(nr_pages, sizeof(struct page *), |
| 3441 | GFP_KERNEL); |
| 3442 | vmas = kvmalloc_array(nr_pages, |
| 3443 | sizeof(struct vm_area_struct *), |
| 3444 | GFP_KERNEL); |
| 3445 | if (!pages || !vmas) { |
| 3446 | ret = -ENOMEM; |
| 3447 | if (ctx->account_mem) |
| 3448 | io_unaccount_mem(ctx->user, nr_pages); |
| 3449 | goto err; |
| 3450 | } |
| 3451 | got_pages = nr_pages; |
| 3452 | } |
| 3453 | |
| 3454 | imu->bvec = kvmalloc_array(nr_pages, sizeof(struct bio_vec), |
| 3455 | GFP_KERNEL); |
| 3456 | ret = -ENOMEM; |
| 3457 | if (!imu->bvec) { |
| 3458 | if (ctx->account_mem) |
| 3459 | io_unaccount_mem(ctx->user, nr_pages); |
| 3460 | goto err; |
| 3461 | } |
| 3462 | |
| 3463 | ret = 0; |
| 3464 | down_read(¤t->mm->mmap_sem); |
| 3465 | pret = get_user_pages(ubuf, nr_pages, |
| 3466 | FOLL_WRITE | FOLL_LONGTERM, |
| 3467 | pages, vmas); |
| 3468 | if (pret == nr_pages) { |
| 3469 | /* don't support file backed memory */ |
| 3470 | for (j = 0; j < nr_pages; j++) { |
| 3471 | struct vm_area_struct *vma = vmas[j]; |
| 3472 | |
| 3473 | if (vma->vm_file && |
| 3474 | !is_file_hugepages(vma->vm_file)) { |
| 3475 | ret = -EOPNOTSUPP; |
| 3476 | break; |
| 3477 | } |
| 3478 | } |
| 3479 | } else { |
| 3480 | ret = pret < 0 ? pret : -EFAULT; |
| 3481 | } |
| 3482 | up_read(¤t->mm->mmap_sem); |
| 3483 | if (ret) { |
| 3484 | /* |
| 3485 | * if we did partial map, or found file backed vmas, |
| 3486 | * release any pages we did get |
| 3487 | */ |
| 3488 | if (pret > 0) |
| 3489 | put_user_pages(pages, pret); |
| 3490 | if (ctx->account_mem) |
| 3491 | io_unaccount_mem(ctx->user, nr_pages); |
| 3492 | kvfree(imu->bvec); |
| 3493 | goto err; |
| 3494 | } |
| 3495 | |
| 3496 | off = ubuf & ~PAGE_MASK; |
| 3497 | size = iov.iov_len; |
| 3498 | for (j = 0; j < nr_pages; j++) { |
| 3499 | size_t vec_len; |
| 3500 | |
| 3501 | vec_len = min_t(size_t, size, PAGE_SIZE - off); |
| 3502 | imu->bvec[j].bv_page = pages[j]; |
| 3503 | imu->bvec[j].bv_len = vec_len; |
| 3504 | imu->bvec[j].bv_offset = off; |
| 3505 | off = 0; |
| 3506 | size -= vec_len; |
| 3507 | } |
| 3508 | /* store original address for later verification */ |
| 3509 | imu->ubuf = ubuf; |
| 3510 | imu->len = iov.iov_len; |
| 3511 | imu->nr_bvecs = nr_pages; |
| 3512 | |
| 3513 | ctx->nr_user_bufs++; |
| 3514 | } |
| 3515 | kvfree(pages); |
| 3516 | kvfree(vmas); |
| 3517 | return 0; |
| 3518 | err: |
| 3519 | kvfree(pages); |
| 3520 | kvfree(vmas); |
| 3521 | io_sqe_buffer_unregister(ctx); |
| 3522 | return ret; |
| 3523 | } |
| 3524 | |
| 3525 | static int io_eventfd_register(struct io_ring_ctx *ctx, void __user *arg) |
| 3526 | { |
| 3527 | __s32 __user *fds = arg; |
| 3528 | int fd; |
| 3529 | |
| 3530 | if (ctx->cq_ev_fd) |
| 3531 | return -EBUSY; |
| 3532 | |
| 3533 | if (copy_from_user(&fd, fds, sizeof(*fds))) |
| 3534 | return -EFAULT; |
| 3535 | |
| 3536 | ctx->cq_ev_fd = eventfd_ctx_fdget(fd); |
| 3537 | if (IS_ERR(ctx->cq_ev_fd)) { |
| 3538 | int ret = PTR_ERR(ctx->cq_ev_fd); |
| 3539 | ctx->cq_ev_fd = NULL; |
| 3540 | return ret; |
| 3541 | } |
| 3542 | |
| 3543 | return 0; |
| 3544 | } |
| 3545 | |
| 3546 | static int io_eventfd_unregister(struct io_ring_ctx *ctx) |
| 3547 | { |
| 3548 | if (ctx->cq_ev_fd) { |
| 3549 | eventfd_ctx_put(ctx->cq_ev_fd); |
| 3550 | ctx->cq_ev_fd = NULL; |
| 3551 | return 0; |
| 3552 | } |
| 3553 | |
| 3554 | return -ENXIO; |
| 3555 | } |
| 3556 | |
| 3557 | static void io_ring_ctx_free(struct io_ring_ctx *ctx) |
| 3558 | { |
| 3559 | io_finish_async(ctx); |
| 3560 | if (ctx->sqo_mm) |
| 3561 | mmdrop(ctx->sqo_mm); |
| 3562 | |
| 3563 | io_iopoll_reap_events(ctx); |
| 3564 | io_sqe_buffer_unregister(ctx); |
| 3565 | io_sqe_files_unregister(ctx); |
| 3566 | io_eventfd_unregister(ctx); |
| 3567 | |
| 3568 | #if defined(CONFIG_UNIX) |
| 3569 | if (ctx->ring_sock) { |
| 3570 | ctx->ring_sock->file = NULL; /* so that iput() is called */ |
| 3571 | sock_release(ctx->ring_sock); |
| 3572 | } |
| 3573 | #endif |
| 3574 | |
| 3575 | io_mem_free(ctx->rings); |
| 3576 | io_mem_free(ctx->sq_sqes); |
| 3577 | |
| 3578 | percpu_ref_exit(&ctx->refs); |
| 3579 | if (ctx->account_mem) |
| 3580 | io_unaccount_mem(ctx->user, |
| 3581 | ring_pages(ctx->sq_entries, ctx->cq_entries)); |
| 3582 | free_uid(ctx->user); |
| 3583 | if (ctx->creds) |
| 3584 | put_cred(ctx->creds); |
| 3585 | kfree(ctx); |
| 3586 | } |
| 3587 | |
| 3588 | static __poll_t io_uring_poll(struct file *file, poll_table *wait) |
| 3589 | { |
| 3590 | struct io_ring_ctx *ctx = file->private_data; |
| 3591 | __poll_t mask = 0; |
| 3592 | |
| 3593 | poll_wait(file, &ctx->cq_wait, wait); |
| 3594 | /* |
| 3595 | * synchronizes with barrier from wq_has_sleeper call in |
| 3596 | * io_commit_cqring |
| 3597 | */ |
| 3598 | smp_rmb(); |
| 3599 | if (READ_ONCE(ctx->rings->sq.tail) - ctx->cached_sq_head != |
| 3600 | ctx->rings->sq_ring_entries) |
| 3601 | mask |= EPOLLOUT | EPOLLWRNORM; |
| 3602 | if (READ_ONCE(ctx->rings->cq.head) != ctx->cached_cq_tail) |
| 3603 | mask |= EPOLLIN | EPOLLRDNORM; |
| 3604 | |
| 3605 | return mask; |
| 3606 | } |
| 3607 | |
| 3608 | static int io_uring_fasync(int fd, struct file *file, int on) |
| 3609 | { |
| 3610 | struct io_ring_ctx *ctx = file->private_data; |
| 3611 | |
| 3612 | return fasync_helper(fd, file, on, &ctx->cq_fasync); |
| 3613 | } |
| 3614 | |
| 3615 | static void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx) |
| 3616 | { |
| 3617 | mutex_lock(&ctx->uring_lock); |
| 3618 | percpu_ref_kill(&ctx->refs); |
| 3619 | mutex_unlock(&ctx->uring_lock); |
| 3620 | |
| 3621 | io_kill_timeouts(ctx); |
| 3622 | io_poll_remove_all(ctx); |
| 3623 | io_iopoll_reap_events(ctx); |
| 3624 | wait_for_completion(&ctx->ctx_done); |
| 3625 | io_ring_ctx_free(ctx); |
| 3626 | } |
| 3627 | |
| 3628 | static int io_uring_release(struct inode *inode, struct file *file) |
| 3629 | { |
| 3630 | struct io_ring_ctx *ctx = file->private_data; |
| 3631 | |
| 3632 | file->private_data = NULL; |
| 3633 | io_ring_ctx_wait_and_kill(ctx); |
| 3634 | return 0; |
| 3635 | } |
| 3636 | |
| 3637 | static int io_uring_mmap(struct file *file, struct vm_area_struct *vma) |
| 3638 | { |
| 3639 | loff_t offset = (loff_t) vma->vm_pgoff << PAGE_SHIFT; |
| 3640 | unsigned long sz = vma->vm_end - vma->vm_start; |
| 3641 | struct io_ring_ctx *ctx = file->private_data; |
| 3642 | unsigned long pfn; |
| 3643 | struct page *page; |
| 3644 | void *ptr; |
| 3645 | |
| 3646 | switch (offset) { |
| 3647 | case IORING_OFF_SQ_RING: |
| 3648 | case IORING_OFF_CQ_RING: |
| 3649 | ptr = ctx->rings; |
| 3650 | break; |
| 3651 | case IORING_OFF_SQES: |
| 3652 | ptr = ctx->sq_sqes; |
| 3653 | break; |
| 3654 | default: |
| 3655 | return -EINVAL; |
| 3656 | } |
| 3657 | |
| 3658 | page = virt_to_head_page(ptr); |
| 3659 | if (sz > page_size(page)) |
| 3660 | return -EINVAL; |
| 3661 | |
| 3662 | pfn = virt_to_phys(ptr) >> PAGE_SHIFT; |
| 3663 | return remap_pfn_range(vma, vma->vm_start, pfn, sz, vma->vm_page_prot); |
| 3664 | } |
| 3665 | |
| 3666 | SYSCALL_DEFINE6(io_uring_enter, unsigned int, fd, u32, to_submit, |
| 3667 | u32, min_complete, u32, flags, const sigset_t __user *, sig, |
| 3668 | size_t, sigsz) |
| 3669 | { |
| 3670 | struct io_ring_ctx *ctx; |
| 3671 | long ret = -EBADF; |
| 3672 | int submitted = 0; |
| 3673 | struct fd f; |
| 3674 | |
| 3675 | if (flags & ~(IORING_ENTER_GETEVENTS | IORING_ENTER_SQ_WAKEUP)) |
| 3676 | return -EINVAL; |
| 3677 | |
| 3678 | f = fdget(fd); |
| 3679 | if (!f.file) |
| 3680 | return -EBADF; |
| 3681 | |
| 3682 | ret = -EOPNOTSUPP; |
| 3683 | if (f.file->f_op != &io_uring_fops) |
| 3684 | goto out_fput; |
| 3685 | |
| 3686 | ret = -ENXIO; |
| 3687 | ctx = f.file->private_data; |
| 3688 | if (!percpu_ref_tryget(&ctx->refs)) |
| 3689 | goto out_fput; |
| 3690 | |
| 3691 | /* |
| 3692 | * For SQ polling, the thread will do all submissions and completions. |
| 3693 | * Just return the requested submit count, and wake the thread if |
| 3694 | * we were asked to. |
| 3695 | */ |
| 3696 | ret = 0; |
| 3697 | if (ctx->flags & IORING_SETUP_SQPOLL) { |
| 3698 | if (flags & IORING_ENTER_SQ_WAKEUP) |
| 3699 | wake_up(&ctx->sqo_wait); |
| 3700 | submitted = to_submit; |
| 3701 | } else if (to_submit) { |
| 3702 | to_submit = min(to_submit, ctx->sq_entries); |
| 3703 | |
| 3704 | mutex_lock(&ctx->uring_lock); |
| 3705 | submitted = io_ring_submit(ctx, to_submit); |
| 3706 | mutex_unlock(&ctx->uring_lock); |
| 3707 | } |
| 3708 | if (flags & IORING_ENTER_GETEVENTS) { |
| 3709 | unsigned nr_events = 0; |
| 3710 | |
| 3711 | min_complete = min(min_complete, ctx->cq_entries); |
| 3712 | |
| 3713 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
| 3714 | ret = io_iopoll_check(ctx, &nr_events, min_complete); |
| 3715 | } else { |
| 3716 | ret = io_cqring_wait(ctx, min_complete, sig, sigsz); |
| 3717 | } |
| 3718 | } |
| 3719 | |
| 3720 | percpu_ref_put(&ctx->refs); |
| 3721 | out_fput: |
| 3722 | fdput(f); |
| 3723 | return submitted ? submitted : ret; |
| 3724 | } |
| 3725 | |
| 3726 | static const struct file_operations io_uring_fops = { |
| 3727 | .release = io_uring_release, |
| 3728 | .mmap = io_uring_mmap, |
| 3729 | .poll = io_uring_poll, |
| 3730 | .fasync = io_uring_fasync, |
| 3731 | }; |
| 3732 | |
| 3733 | static int io_allocate_scq_urings(struct io_ring_ctx *ctx, |
| 3734 | struct io_uring_params *p) |
| 3735 | { |
| 3736 | struct io_rings *rings; |
| 3737 | size_t size, sq_array_offset; |
| 3738 | |
| 3739 | size = rings_size(p->sq_entries, p->cq_entries, &sq_array_offset); |
| 3740 | if (size == SIZE_MAX) |
| 3741 | return -EOVERFLOW; |
| 3742 | |
| 3743 | rings = io_mem_alloc(size); |
| 3744 | if (!rings) |
| 3745 | return -ENOMEM; |
| 3746 | |
| 3747 | ctx->rings = rings; |
| 3748 | ctx->sq_array = (u32 *)((char *)rings + sq_array_offset); |
| 3749 | rings->sq_ring_mask = p->sq_entries - 1; |
| 3750 | rings->cq_ring_mask = p->cq_entries - 1; |
| 3751 | rings->sq_ring_entries = p->sq_entries; |
| 3752 | rings->cq_ring_entries = p->cq_entries; |
| 3753 | ctx->sq_mask = rings->sq_ring_mask; |
| 3754 | ctx->cq_mask = rings->cq_ring_mask; |
| 3755 | ctx->sq_entries = rings->sq_ring_entries; |
| 3756 | ctx->cq_entries = rings->cq_ring_entries; |
| 3757 | |
| 3758 | size = array_size(sizeof(struct io_uring_sqe), p->sq_entries); |
| 3759 | if (size == SIZE_MAX) |
| 3760 | return -EOVERFLOW; |
| 3761 | |
| 3762 | ctx->sq_sqes = io_mem_alloc(size); |
| 3763 | if (!ctx->sq_sqes) |
| 3764 | return -ENOMEM; |
| 3765 | |
| 3766 | return 0; |
| 3767 | } |
| 3768 | |
| 3769 | /* |
| 3770 | * Allocate an anonymous fd, this is what constitutes the application |
| 3771 | * visible backing of an io_uring instance. The application mmaps this |
| 3772 | * fd to gain access to the SQ/CQ ring details. If UNIX sockets are enabled, |
| 3773 | * we have to tie this fd to a socket for file garbage collection purposes. |
| 3774 | */ |
| 3775 | static int io_uring_get_fd(struct io_ring_ctx *ctx) |
| 3776 | { |
| 3777 | struct file *file; |
| 3778 | int ret; |
| 3779 | |
| 3780 | #if defined(CONFIG_UNIX) |
| 3781 | ret = sock_create_kern(&init_net, PF_UNIX, SOCK_RAW, IPPROTO_IP, |
| 3782 | &ctx->ring_sock); |
| 3783 | if (ret) |
| 3784 | return ret; |
| 3785 | #endif |
| 3786 | |
| 3787 | ret = get_unused_fd_flags(O_RDWR | O_CLOEXEC); |
| 3788 | if (ret < 0) |
| 3789 | goto err; |
| 3790 | |
| 3791 | file = anon_inode_getfile("[io_uring]", &io_uring_fops, ctx, |
| 3792 | O_RDWR | O_CLOEXEC); |
| 3793 | if (IS_ERR(file)) { |
| 3794 | put_unused_fd(ret); |
| 3795 | ret = PTR_ERR(file); |
| 3796 | goto err; |
| 3797 | } |
| 3798 | |
| 3799 | #if defined(CONFIG_UNIX) |
| 3800 | ctx->ring_sock->file = file; |
| 3801 | ctx->ring_sock->sk->sk_user_data = ctx; |
| 3802 | #endif |
| 3803 | fd_install(ret, file); |
| 3804 | return ret; |
| 3805 | err: |
| 3806 | #if defined(CONFIG_UNIX) |
| 3807 | sock_release(ctx->ring_sock); |
| 3808 | ctx->ring_sock = NULL; |
| 3809 | #endif |
| 3810 | return ret; |
| 3811 | } |
| 3812 | |
| 3813 | static int io_uring_create(unsigned entries, struct io_uring_params *p) |
| 3814 | { |
| 3815 | struct user_struct *user = NULL; |
| 3816 | struct io_ring_ctx *ctx; |
| 3817 | bool account_mem; |
| 3818 | int ret; |
| 3819 | |
| 3820 | if (!entries || entries > IORING_MAX_ENTRIES) |
| 3821 | return -EINVAL; |
| 3822 | |
| 3823 | /* |
| 3824 | * Use twice as many entries for the CQ ring. It's possible for the |
| 3825 | * application to drive a higher depth than the size of the SQ ring, |
| 3826 | * since the sqes are only used at submission time. This allows for |
| 3827 | * some flexibility in overcommitting a bit. |
| 3828 | */ |
| 3829 | p->sq_entries = roundup_pow_of_two(entries); |
| 3830 | p->cq_entries = 2 * p->sq_entries; |
| 3831 | |
| 3832 | user = get_uid(current_user()); |
| 3833 | account_mem = !capable(CAP_IPC_LOCK); |
| 3834 | |
| 3835 | if (account_mem) { |
| 3836 | ret = io_account_mem(user, |
| 3837 | ring_pages(p->sq_entries, p->cq_entries)); |
| 3838 | if (ret) { |
| 3839 | free_uid(user); |
| 3840 | return ret; |
| 3841 | } |
| 3842 | } |
| 3843 | |
| 3844 | ctx = io_ring_ctx_alloc(p); |
| 3845 | if (!ctx) { |
| 3846 | if (account_mem) |
| 3847 | io_unaccount_mem(user, ring_pages(p->sq_entries, |
| 3848 | p->cq_entries)); |
| 3849 | free_uid(user); |
| 3850 | return -ENOMEM; |
| 3851 | } |
| 3852 | ctx->compat = in_compat_syscall(); |
| 3853 | ctx->account_mem = account_mem; |
| 3854 | ctx->user = user; |
| 3855 | |
| 3856 | ctx->creds = prepare_creds(); |
| 3857 | if (!ctx->creds) { |
| 3858 | ret = -ENOMEM; |
| 3859 | goto err; |
| 3860 | } |
| 3861 | |
| 3862 | ret = io_allocate_scq_urings(ctx, p); |
| 3863 | if (ret) |
| 3864 | goto err; |
| 3865 | |
| 3866 | ret = io_sq_offload_start(ctx, p); |
| 3867 | if (ret) |
| 3868 | goto err; |
| 3869 | |
| 3870 | memset(&p->sq_off, 0, sizeof(p->sq_off)); |
| 3871 | p->sq_off.head = offsetof(struct io_rings, sq.head); |
| 3872 | p->sq_off.tail = offsetof(struct io_rings, sq.tail); |
| 3873 | p->sq_off.ring_mask = offsetof(struct io_rings, sq_ring_mask); |
| 3874 | p->sq_off.ring_entries = offsetof(struct io_rings, sq_ring_entries); |
| 3875 | p->sq_off.flags = offsetof(struct io_rings, sq_flags); |
| 3876 | p->sq_off.dropped = offsetof(struct io_rings, sq_dropped); |
| 3877 | p->sq_off.array = (char *)ctx->sq_array - (char *)ctx->rings; |
| 3878 | |
| 3879 | memset(&p->cq_off, 0, sizeof(p->cq_off)); |
| 3880 | p->cq_off.head = offsetof(struct io_rings, cq.head); |
| 3881 | p->cq_off.tail = offsetof(struct io_rings, cq.tail); |
| 3882 | p->cq_off.ring_mask = offsetof(struct io_rings, cq_ring_mask); |
| 3883 | p->cq_off.ring_entries = offsetof(struct io_rings, cq_ring_entries); |
| 3884 | p->cq_off.overflow = offsetof(struct io_rings, cq_overflow); |
| 3885 | p->cq_off.cqes = offsetof(struct io_rings, cqes); |
| 3886 | |
| 3887 | /* |
| 3888 | * Install ring fd as the very last thing, so we don't risk someone |
| 3889 | * having closed it before we finish setup |
| 3890 | */ |
| 3891 | ret = io_uring_get_fd(ctx); |
| 3892 | if (ret < 0) |
| 3893 | goto err; |
| 3894 | |
| 3895 | p->features = IORING_FEAT_SINGLE_MMAP; |
| 3896 | return ret; |
| 3897 | err: |
| 3898 | io_ring_ctx_wait_and_kill(ctx); |
| 3899 | return ret; |
| 3900 | } |
| 3901 | |
| 3902 | /* |
| 3903 | * Sets up an aio uring context, and returns the fd. Applications asks for a |
| 3904 | * ring size, we return the actual sq/cq ring sizes (among other things) in the |
| 3905 | * params structure passed in. |
| 3906 | */ |
| 3907 | static long io_uring_setup(u32 entries, struct io_uring_params __user *params) |
| 3908 | { |
| 3909 | struct io_uring_params p; |
| 3910 | long ret; |
| 3911 | int i; |
| 3912 | |
| 3913 | if (copy_from_user(&p, params, sizeof(p))) |
| 3914 | return -EFAULT; |
| 3915 | for (i = 0; i < ARRAY_SIZE(p.resv); i++) { |
| 3916 | if (p.resv[i]) |
| 3917 | return -EINVAL; |
| 3918 | } |
| 3919 | |
| 3920 | if (p.flags & ~(IORING_SETUP_IOPOLL | IORING_SETUP_SQPOLL | |
| 3921 | IORING_SETUP_SQ_AFF)) |
| 3922 | return -EINVAL; |
| 3923 | |
| 3924 | ret = io_uring_create(entries, &p); |
| 3925 | if (ret < 0) |
| 3926 | return ret; |
| 3927 | |
| 3928 | if (copy_to_user(params, &p, sizeof(p))) |
| 3929 | return -EFAULT; |
| 3930 | |
| 3931 | return ret; |
| 3932 | } |
| 3933 | |
| 3934 | SYSCALL_DEFINE2(io_uring_setup, u32, entries, |
| 3935 | struct io_uring_params __user *, params) |
| 3936 | { |
| 3937 | return io_uring_setup(entries, params); |
| 3938 | } |
| 3939 | |
| 3940 | static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode, |
| 3941 | void __user *arg, unsigned nr_args) |
| 3942 | __releases(ctx->uring_lock) |
| 3943 | __acquires(ctx->uring_lock) |
| 3944 | { |
| 3945 | int ret; |
| 3946 | |
| 3947 | /* |
| 3948 | * We're inside the ring mutex, if the ref is already dying, then |
| 3949 | * someone else killed the ctx or is already going through |
| 3950 | * io_uring_register(). |
| 3951 | */ |
| 3952 | if (percpu_ref_is_dying(&ctx->refs)) |
| 3953 | return -ENXIO; |
| 3954 | |
| 3955 | percpu_ref_kill(&ctx->refs); |
| 3956 | |
| 3957 | /* |
| 3958 | * Drop uring mutex before waiting for references to exit. If another |
| 3959 | * thread is currently inside io_uring_enter() it might need to grab |
| 3960 | * the uring_lock to make progress. If we hold it here across the drain |
| 3961 | * wait, then we can deadlock. It's safe to drop the mutex here, since |
| 3962 | * no new references will come in after we've killed the percpu ref. |
| 3963 | */ |
| 3964 | mutex_unlock(&ctx->uring_lock); |
| 3965 | wait_for_completion(&ctx->ctx_done); |
| 3966 | mutex_lock(&ctx->uring_lock); |
| 3967 | |
| 3968 | switch (opcode) { |
| 3969 | case IORING_REGISTER_BUFFERS: |
| 3970 | ret = io_sqe_buffer_register(ctx, arg, nr_args); |
| 3971 | break; |
| 3972 | case IORING_UNREGISTER_BUFFERS: |
| 3973 | ret = -EINVAL; |
| 3974 | if (arg || nr_args) |
| 3975 | break; |
| 3976 | ret = io_sqe_buffer_unregister(ctx); |
| 3977 | break; |
| 3978 | case IORING_REGISTER_FILES: |
| 3979 | ret = io_sqe_files_register(ctx, arg, nr_args); |
| 3980 | break; |
| 3981 | case IORING_UNREGISTER_FILES: |
| 3982 | ret = -EINVAL; |
| 3983 | if (arg || nr_args) |
| 3984 | break; |
| 3985 | ret = io_sqe_files_unregister(ctx); |
| 3986 | break; |
| 3987 | case IORING_REGISTER_EVENTFD: |
| 3988 | ret = -EINVAL; |
| 3989 | if (nr_args != 1) |
| 3990 | break; |
| 3991 | ret = io_eventfd_register(ctx, arg); |
| 3992 | break; |
| 3993 | case IORING_UNREGISTER_EVENTFD: |
| 3994 | ret = -EINVAL; |
| 3995 | if (arg || nr_args) |
| 3996 | break; |
| 3997 | ret = io_eventfd_unregister(ctx); |
| 3998 | break; |
| 3999 | default: |
| 4000 | ret = -EINVAL; |
| 4001 | break; |
| 4002 | } |
| 4003 | |
| 4004 | /* bring the ctx back to life */ |
| 4005 | reinit_completion(&ctx->ctx_done); |
| 4006 | percpu_ref_reinit(&ctx->refs); |
| 4007 | return ret; |
| 4008 | } |
| 4009 | |
| 4010 | SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, |
| 4011 | void __user *, arg, unsigned int, nr_args) |
| 4012 | { |
| 4013 | struct io_ring_ctx *ctx; |
| 4014 | long ret = -EBADF; |
| 4015 | struct fd f; |
| 4016 | |
| 4017 | f = fdget(fd); |
| 4018 | if (!f.file) |
| 4019 | return -EBADF; |
| 4020 | |
| 4021 | ret = -EOPNOTSUPP; |
| 4022 | if (f.file->f_op != &io_uring_fops) |
| 4023 | goto out_fput; |
| 4024 | |
| 4025 | ctx = f.file->private_data; |
| 4026 | |
| 4027 | mutex_lock(&ctx->uring_lock); |
| 4028 | ret = __io_uring_register(ctx, opcode, arg, nr_args); |
| 4029 | mutex_unlock(&ctx->uring_lock); |
| 4030 | out_fput: |
| 4031 | fdput(f); |
| 4032 | return ret; |
| 4033 | } |
| 4034 | |
| 4035 | static int __init io_uring_init(void) |
| 4036 | { |
| 4037 | req_cachep = KMEM_CACHE(io_kiocb, SLAB_HWCACHE_ALIGN | SLAB_PANIC); |
| 4038 | return 0; |
| 4039 | }; |
| 4040 | __initcall(io_uring_init); |