1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef IOU_CORE_H 3 #define IOU_CORE_H 4 5 #include <linux/errno.h> 6 #include <linux/file.h> 7 #include <linux/lockdep.h> 8 #include <linux/resume_user_mode.h> 9 #include <linux/poll.h> 10 #include <linux/io_uring_types.h> 11 #include <uapi/linux/eventpoll.h> 12 #include "alloc_cache.h" 13 #include "io-wq.h" 14 #include "slist.h" 15 #include "tw.h" 16 #include "opdef.h" 17 18 #ifndef CREATE_TRACE_POINTS 19 #include <trace/events/io_uring.h> 20 #endif 21 22 struct io_rings_layout { 23 /* size of CQ + headers + SQ offset array */ 24 size_t rings_size; 25 size_t sq_size; 26 27 size_t sq_array_offset; 28 }; 29 30 struct io_ctx_config { 31 struct io_uring_params p; 32 struct io_rings_layout layout; 33 struct io_uring_params __user *uptr; 34 }; 35 36 #define IORING_FEAT_FLAGS (IORING_FEAT_SINGLE_MMAP |\ 37 IORING_FEAT_NODROP |\ 38 IORING_FEAT_SUBMIT_STABLE |\ 39 IORING_FEAT_RW_CUR_POS |\ 40 IORING_FEAT_CUR_PERSONALITY |\ 41 IORING_FEAT_FAST_POLL |\ 42 IORING_FEAT_POLL_32BITS |\ 43 IORING_FEAT_SQPOLL_NONFIXED |\ 44 IORING_FEAT_EXT_ARG |\ 45 IORING_FEAT_NATIVE_WORKERS |\ 46 IORING_FEAT_RSRC_TAGS |\ 47 IORING_FEAT_CQE_SKIP |\ 48 IORING_FEAT_LINKED_FILE |\ 49 IORING_FEAT_REG_REG_RING |\ 50 IORING_FEAT_RECVSEND_BUNDLE |\ 51 IORING_FEAT_MIN_TIMEOUT |\ 52 IORING_FEAT_RW_ATTR |\ 53 IORING_FEAT_NO_IOWAIT |\ 54 IORING_FEAT_REGBUF_TYPES) 55 56 #define IORING_SETUP_FLAGS (IORING_SETUP_IOPOLL |\ 57 IORING_SETUP_SQPOLL |\ 58 IORING_SETUP_SQ_AFF |\ 59 IORING_SETUP_CQSIZE |\ 60 IORING_SETUP_CLAMP |\ 61 IORING_SETUP_ATTACH_WQ |\ 62 IORING_SETUP_R_DISABLED |\ 63 IORING_SETUP_SUBMIT_ALL |\ 64 IORING_SETUP_COOP_TASKRUN |\ 65 IORING_SETUP_TASKRUN_FLAG |\ 66 IORING_SETUP_SQE128 |\ 67 IORING_SETUP_CQE32 |\ 68 IORING_SETUP_SINGLE_ISSUER |\ 69 IORING_SETUP_DEFER_TASKRUN |\ 70 IORING_SETUP_NO_MMAP |\ 71 IORING_SETUP_REGISTERED_FD_ONLY |\ 72 IORING_SETUP_NO_SQARRAY |\ 73 IORING_SETUP_HYBRID_IOPOLL |\ 74 IORING_SETUP_CQE_MIXED |\ 75 IORING_SETUP_SQE_MIXED |\ 76 IORING_SETUP_SQ_REWIND) 77 78 #define IORING_ENTER_FLAGS (IORING_ENTER_GETEVENTS |\ 79 IORING_ENTER_SQ_WAKEUP |\ 80 IORING_ENTER_SQ_WAIT |\ 81 IORING_ENTER_EXT_ARG |\ 82 IORING_ENTER_REGISTERED_RING |\ 83 IORING_ENTER_ABS_TIMER |\ 84 IORING_ENTER_EXT_ARG_REG |\ 85 IORING_ENTER_NO_IOWAIT) 86 87 88 #define SQE_VALID_FLAGS (IOSQE_FIXED_FILE |\ 89 IOSQE_IO_DRAIN |\ 90 IOSQE_IO_LINK |\ 91 IOSQE_IO_HARDLINK |\ 92 IOSQE_ASYNC |\ 93 IOSQE_BUFFER_SELECT |\ 94 IOSQE_CQE_SKIP_SUCCESS) 95 96 #define IO_REQ_LINK_FLAGS (REQ_F_LINK | REQ_F_HARDLINK) 97 98 /* 99 * Complaint timeout for io_uring cancelation exits, and for io-wq exit 100 * worker waiting. 101 */ 102 #define IO_URING_EXIT_WAIT_MAX (HZ * 60 * 5) 103 104 enum { 105 IOU_COMPLETE = 0, 106 107 IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED, 108 109 /* 110 * The request has more work to do and should be retried. io_uring will 111 * attempt to wait on the file for eligible opcodes, but otherwise 112 * it'll be handed to iowq for blocking execution. It works for normal 113 * requests as well as for the multi shot mode. 114 */ 115 IOU_RETRY = -EAGAIN, 116 117 /* 118 * Requeue the task_work to restart operations on this request. The 119 * actual value isn't important, should just be not an otherwise 120 * valid error code, yet less than -MAX_ERRNO and valid internally. 121 */ 122 IOU_REQUEUE = -3072, 123 }; 124 125 struct io_defer_entry { 126 struct list_head list; 127 struct io_kiocb *req; 128 }; 129 130 struct io_wait_queue { 131 struct wait_queue_entry wq; 132 struct io_ring_ctx *ctx; 133 unsigned cq_tail; 134 unsigned cq_min_tail; 135 unsigned nr_timeouts; 136 int hit_timeout; 137 ktime_t min_timeout; 138 ktime_t timeout; 139 struct hrtimer t; 140 141 #ifdef CONFIG_NET_RX_BUSY_POLL 142 ktime_t napi_busy_poll_dt; 143 ktime_t napi_busy_poll_end; 144 bool napi_prefer_busy_poll; 145 #endif 146 }; 147 148 static inline struct io_rings *io_get_rings(struct io_ring_ctx *ctx) 149 { 150 return rcu_dereference_check(ctx->rings_rcu, 151 lockdep_is_held(&ctx->uring_lock) || 152 lockdep_is_held(&ctx->completion_lock)); 153 } 154 155 static inline bool io_should_wake(struct io_wait_queue *iowq) 156 { 157 struct io_ring_ctx *ctx = iowq->ctx; 158 struct io_rings *rings; 159 int dist; 160 161 guard(rcu)(); 162 rings = io_get_rings(ctx); 163 164 /* 165 * Wake up if we have enough events, or if a timeout occurred since we 166 * started waiting. For timeouts, we always want to return to userspace, 167 * regardless of event count. 168 */ 169 dist = READ_ONCE(rings->cq.tail) - (int) iowq->cq_tail; 170 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; 171 } 172 173 #define IORING_MAX_ENTRIES 32768 174 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) 175 176 int io_prepare_config(struct io_ctx_config *config); 177 178 bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32); 179 void io_req_defer_failed(struct io_kiocb *req, s32 res); 180 bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 181 void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 182 bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags); 183 bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe src_cqe[2]); 184 void __io_commit_cqring_flush(struct io_ring_ctx *ctx); 185 186 unsigned io_linked_nr(struct io_kiocb *req); 187 void io_req_track_inflight(struct io_kiocb *req); 188 struct file *io_file_get_normal(struct io_kiocb *req, int fd); 189 struct file *io_file_get_fixed(struct io_kiocb *req, int fd, 190 unsigned issue_flags); 191 struct file *io_uring_ctx_get_file(unsigned int fd, bool registered); 192 193 void io_req_task_queue(struct io_kiocb *req); 194 void io_req_task_complete(struct io_tw_req tw_req, io_tw_token_t tw); 195 void io_req_task_queue_fail(struct io_kiocb *req, int ret); 196 void io_req_task_submit(struct io_tw_req tw_req, io_tw_token_t tw); 197 __cold void io_uring_drop_tctx_refs(struct task_struct *task); 198 199 int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file, 200 int start, int end); 201 void io_queue_iowq(struct io_kiocb *req); 202 203 int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw); 204 int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr); 205 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin); 206 __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx); 207 void __io_submit_flush_completions(struct io_ring_ctx *ctx); 208 209 bool io_wq_put_work(struct io_wq_work *work, struct io_wq_work **linked); 210 void io_wq_free_work(struct io_wq_work *work); 211 void io_wq_submit_work(struct io_wq_work *work); 212 213 void io_free_req(struct io_kiocb *req); 214 void io_queue_next(struct io_kiocb *req); 215 void io_task_refs_refill(struct io_uring_task *tctx); 216 bool __io_alloc_req_refill(struct io_ring_ctx *ctx); 217 218 void io_activate_pollwq(struct io_ring_ctx *ctx); 219 void io_restriction_clone(struct io_restriction *dst, struct io_restriction *src); 220 void io_poison_req(struct io_kiocb *req); 221 222 static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx) 223 { 224 #if defined(CONFIG_PROVE_LOCKING) 225 lockdep_assert(in_task()); 226 227 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) 228 lockdep_assert_held(&ctx->uring_lock); 229 230 if (ctx->flags & IORING_SETUP_IOPOLL) { 231 lockdep_assert_held(&ctx->uring_lock); 232 } else if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) { 233 lockdep_assert_held(&ctx->completion_lock); 234 } else if (ctx->submitter_task) { 235 /* 236 * ->submitter_task may be NULL and we can still post a CQE, 237 * if the ring has been setup with IORING_SETUP_R_DISABLED. 238 * Not from an SQE, as those cannot be submitted, but via 239 * updating tagged resources. 240 */ 241 if (!percpu_ref_is_dying(&ctx->refs)) 242 lockdep_assert(current == ctx->submitter_task); 243 } 244 #endif 245 } 246 247 static inline bool io_is_compat(struct io_ring_ctx *ctx) 248 { 249 return IS_ENABLED(CONFIG_COMPAT) && unlikely(ctx->int_flags & IO_RING_F_COMPAT); 250 } 251 252 static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) 253 { 254 if (!wq_list_empty(&ctx->submit_state.compl_reqs) || 255 ctx->submit_state.cq_flush) 256 __io_submit_flush_completions(ctx); 257 } 258 259 #define io_for_each_link(pos, head) \ 260 for (pos = (head); pos; pos = pos->link) 261 262 static inline bool io_get_cqe_overflow(struct io_ring_ctx *ctx, 263 struct io_uring_cqe **ret, 264 bool overflow, bool cqe32) 265 { 266 io_lockdep_assert_cq_locked(ctx); 267 268 if (unlikely(ctx->cqe_sentinel - ctx->cqe_cached < (cqe32 + 1))) { 269 if (unlikely(!io_cqe_cache_refill(ctx, overflow, cqe32))) 270 return false; 271 } 272 *ret = ctx->cqe_cached; 273 ctx->cached_cq_tail++; 274 ctx->cqe_cached++; 275 if (ctx->flags & IORING_SETUP_CQE32) { 276 ctx->cqe_cached++; 277 } else if (cqe32 && ctx->flags & IORING_SETUP_CQE_MIXED) { 278 ctx->cqe_cached++; 279 ctx->cached_cq_tail++; 280 } 281 WARN_ON_ONCE(ctx->cqe_cached > ctx->cqe_sentinel); 282 return true; 283 } 284 285 static inline bool io_get_cqe(struct io_ring_ctx *ctx, struct io_uring_cqe **ret, 286 bool cqe32) 287 { 288 return io_get_cqe_overflow(ctx, ret, false, cqe32); 289 } 290 291 static inline bool io_defer_get_uncommited_cqe(struct io_ring_ctx *ctx, 292 struct io_uring_cqe **cqe_ret) 293 { 294 io_lockdep_assert_cq_locked(ctx); 295 296 ctx->submit_state.cq_flush = true; 297 return io_get_cqe(ctx, cqe_ret, ctx->flags & IORING_SETUP_CQE_MIXED); 298 } 299 300 static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx, 301 struct io_kiocb *req) 302 { 303 bool is_cqe32 = req->cqe.flags & IORING_CQE_F_32; 304 struct io_uring_cqe *cqe; 305 306 /* 307 * If we can't get a cq entry, userspace overflowed the submission 308 * (by quite a lot). 309 */ 310 if (unlikely(!io_get_cqe(ctx, &cqe, is_cqe32))) 311 return false; 312 313 memcpy(cqe, &req->cqe, sizeof(*cqe)); 314 if (ctx->flags & IORING_SETUP_CQE32 || is_cqe32) { 315 memcpy(cqe->big_cqe, &req->big_cqe, sizeof(*cqe)); 316 memset(&req->big_cqe, 0, sizeof(req->big_cqe)); 317 } 318 319 if (trace_io_uring_complete_enabled()) 320 trace_call__io_uring_complete(req->ctx, req, cqe); 321 return true; 322 } 323 324 static inline ssize_t io_fixup_restart_res(ssize_t ret) 325 { 326 switch (ret) { 327 case -ERESTARTSYS: 328 case -ERESTARTNOINTR: 329 case -ERESTARTNOHAND: 330 case -ERESTART_RESTARTBLOCK: 331 /* 332 * We can't just restart the syscall, since previously 333 * submitted sqes may already be in progress. Just fail 334 * this IO with EINTR. 335 */ 336 return -EINTR; 337 default: 338 return ret; 339 } 340 } 341 342 static inline void *io_uring_alloc_async_data(struct io_alloc_cache *cache, 343 struct io_kiocb *req) 344 { 345 if (cache) { 346 req->async_data = io_cache_alloc(cache, GFP_KERNEL); 347 } else { 348 const struct io_issue_def *def = &io_issue_defs[req->opcode]; 349 350 WARN_ON_ONCE(!def->async_size); 351 req->async_data = kmalloc(def->async_size, GFP_KERNEL); 352 } 353 if (req->async_data) 354 req->flags |= REQ_F_ASYNC_DATA; 355 return req->async_data; 356 } 357 358 static inline bool req_has_async_data(struct io_kiocb *req) 359 { 360 return req->flags & REQ_F_ASYNC_DATA; 361 } 362 363 static inline void io_req_async_data_clear(struct io_kiocb *req, 364 io_req_flags_t extra_flags) 365 { 366 req->flags &= ~(REQ_F_ASYNC_DATA|extra_flags); 367 req->async_data = NULL; 368 } 369 370 static inline void io_req_async_data_free(struct io_kiocb *req) 371 { 372 kfree(req->async_data); 373 io_req_async_data_clear(req, 0); 374 } 375 376 static inline void io_put_file(struct io_kiocb *req) 377 { 378 if (!(req->flags & REQ_F_FIXED_FILE) && req->file) 379 fput(req->file); 380 } 381 382 static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx, 383 unsigned issue_flags) 384 { 385 lockdep_assert_held(&ctx->uring_lock); 386 if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) 387 mutex_unlock(&ctx->uring_lock); 388 } 389 390 static inline void io_ring_submit_lock(struct io_ring_ctx *ctx, 391 unsigned issue_flags) 392 { 393 /* 394 * "Normal" inline submissions always hold the uring_lock, since we 395 * grab it from the system call. Same is true for the SQPOLL offload. 396 * The only exception is when we've detached the request and issue it 397 * from an async worker thread, grab the lock for that case. 398 */ 399 if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) 400 mutex_lock(&ctx->uring_lock); 401 lockdep_assert_held(&ctx->uring_lock); 402 } 403 404 /* 405 * For a handler that is run by io-wq and parks the request on a list where 406 * cancellations find it, rather than completing it. Whoever tried to cancel 407 * the request before that found it running. It has flagged the work item, 408 * has searched the list in vain, and won't come back. To be called with 409 * the lock the search takes held: if the search came first, the flag is 410 * visible. For most lists that is ->uring_lock, timeouts use ->timeout_lock. 411 */ 412 static inline bool __io_wq_cancel_seen(struct io_kiocb *req, 413 unsigned int issue_flags) 414 { 415 return (issue_flags & IO_URING_F_IOWQ) && 416 (atomic_read(&req->work.flags) & IO_WQ_WORK_CANCEL); 417 } 418 419 static inline bool io_wq_cancel_seen(struct io_kiocb *req, 420 unsigned int issue_flags) 421 { 422 lockdep_assert_held(&req->ctx->uring_lock); 423 424 return __io_wq_cancel_seen(req, issue_flags); 425 } 426 427 static inline void io_commit_cqring(struct io_ring_ctx *ctx) 428 { 429 /* order cqe stores with ring update */ 430 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); 431 } 432 433 static inline void __io_wq_wake(struct wait_queue_head *wq) 434 { 435 /* 436 * 437 * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter 438 * set in the mask so that if we recurse back into our own poll 439 * waitqueue handlers, we know we have a dependency between eventfd or 440 * epoll and should terminate multishot poll at that point. 441 */ 442 if (wq_has_sleeper(wq)) 443 __wake_up(wq, TASK_NORMAL, 0, poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); 444 } 445 446 static inline void io_poll_wq_wake(struct io_ring_ctx *ctx) 447 { 448 __io_wq_wake(&ctx->poll_wq); 449 } 450 451 static inline void io_cqring_wake(struct io_ring_ctx *ctx) 452 { 453 /* 454 * Trigger waitqueue handler on all waiters on our waitqueue. This 455 * won't necessarily wake up all the tasks, io_should_wake() will make 456 * that decision. 457 */ 458 459 __io_wq_wake(&ctx->cq_wait); 460 } 461 462 static inline bool __io_sqring_full(struct io_ring_ctx *ctx) 463 { 464 struct io_rings *r = io_get_rings(ctx); 465 466 /* 467 * SQPOLL must use the actual sqring head, as using the cached_sq_head 468 * is race prone if the SQPOLL thread has grabbed entries but not yet 469 * committed them to the ring. For !SQPOLL, this doesn't matter, but 470 * since this helper is just used for SQPOLL sqring waits (or POLLOUT), 471 * just read the actual sqring head unconditionally. 472 */ 473 return READ_ONCE(r->sq.tail) - READ_ONCE(r->sq.head) == ctx->sq_entries; 474 } 475 476 static inline bool io_sqring_full(struct io_ring_ctx *ctx) 477 { 478 guard(rcu)(); 479 return __io_sqring_full(ctx); 480 } 481 482 static inline unsigned int __io_sqring_entries(struct io_ring_ctx *ctx) 483 { 484 struct io_rings *rings = io_get_rings(ctx); 485 unsigned int entries; 486 487 /* make sure SQ entry isn't read before tail */ 488 entries = smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; 489 return min(entries, ctx->sq_entries); 490 } 491 492 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) 493 { 494 guard(rcu)(); 495 return __io_sqring_entries(ctx); 496 } 497 498 /* 499 * Don't complete immediately but use deferred completion infrastructure. 500 * Protected by ->uring_lock and can only be used either with 501 * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex. 502 */ 503 static inline void io_req_complete_defer(struct io_kiocb *req) 504 __must_hold(&req->ctx->uring_lock) 505 { 506 struct io_submit_state *state = &req->ctx->submit_state; 507 508 lockdep_assert_held(&req->ctx->uring_lock); 509 510 wq_list_add_tail(&req->comp_list, &state->compl_reqs); 511 } 512 513 #define SHOULD_FLUSH_MASK (IO_RING_F_OFF_TIMEOUT_USED | \ 514 IO_RING_F_HAS_EVFD | IO_RING_F_POLL_ACTIVATED) 515 516 static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx) 517 { 518 if (unlikely(data_race(ctx->int_flags) & SHOULD_FLUSH_MASK)) 519 __io_commit_cqring_flush(ctx); 520 } 521 522 static inline void io_get_task_refs(int nr) 523 { 524 struct io_uring_task *tctx = current->io_uring; 525 526 tctx->cached_refs -= nr; 527 if (unlikely(tctx->cached_refs < 0)) 528 io_task_refs_refill(tctx); 529 } 530 531 static inline bool io_req_cache_empty(struct io_ring_ctx *ctx) 532 { 533 return !ctx->submit_state.free_list.next; 534 } 535 536 extern struct kmem_cache *req_cachep; 537 538 static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx) 539 { 540 struct io_kiocb *req; 541 542 req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list); 543 wq_stack_extract(&ctx->submit_state.free_list); 544 return req; 545 } 546 547 static inline bool io_alloc_req(struct io_ring_ctx *ctx, struct io_kiocb **req) 548 { 549 if (unlikely(io_req_cache_empty(ctx))) { 550 if (!__io_alloc_req_refill(ctx)) 551 return false; 552 } 553 *req = io_extract_req(ctx); 554 return true; 555 } 556 557 static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res) 558 { 559 io_req_set_res(req, res, 0); 560 req->io_task_work.func = io_req_task_complete; 561 io_req_task_work_add(req); 562 } 563 564 static inline bool io_file_can_poll(struct io_kiocb *req) 565 { 566 if (req->flags & REQ_F_CAN_POLL) 567 return true; 568 if (req->file && file_can_poll(req->file)) { 569 req->flags |= REQ_F_CAN_POLL; 570 return true; 571 } 572 return false; 573 } 574 575 static inline bool io_is_uring_cmd(const struct io_kiocb *req) 576 { 577 return req->opcode == IORING_OP_URING_CMD || 578 req->opcode == IORING_OP_URING_CMD128; 579 } 580 581 static inline ktime_t io_get_time(struct io_ring_ctx *ctx) 582 { 583 if (ctx->clockid == CLOCK_MONOTONIC) 584 return ktime_get(); 585 586 return ktime_get_with_offset(ctx->clock_offset); 587 } 588 589 enum { 590 IO_CHECK_CQ_OVERFLOW_BIT, 591 IO_CHECK_CQ_DROPPED_BIT, 592 }; 593 594 static inline bool io_has_work(struct io_ring_ctx *ctx) 595 { 596 return test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq) || 597 io_local_work_pending(ctx); 598 } 599 #endif 600