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 bool napi_prefer_busy_poll; 144 #endif 145 }; 146 147 static inline struct io_rings *io_get_rings(struct io_ring_ctx *ctx) 148 { 149 return rcu_dereference_check(ctx->rings_rcu, 150 lockdep_is_held(&ctx->uring_lock) || 151 lockdep_is_held(&ctx->completion_lock)); 152 } 153 154 static inline bool io_should_wake(struct io_wait_queue *iowq) 155 { 156 struct io_ring_ctx *ctx = iowq->ctx; 157 struct io_rings *rings; 158 int dist; 159 160 guard(rcu)(); 161 rings = io_get_rings(ctx); 162 163 /* 164 * Wake up if we have enough events, or if a timeout occurred since we 165 * started waiting. For timeouts, we always want to return to userspace, 166 * regardless of event count. 167 */ 168 dist = READ_ONCE(rings->cq.tail) - (int) iowq->cq_tail; 169 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; 170 } 171 172 #define IORING_MAX_ENTRIES 32768 173 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) 174 175 int io_prepare_config(struct io_ctx_config *config); 176 177 bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow, bool cqe32); 178 void io_req_defer_failed(struct io_kiocb *req, s32 res); 179 bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 180 void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 181 bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags); 182 bool io_req_post_cqe32(struct io_kiocb *req, struct io_uring_cqe src_cqe[2]); 183 void __io_commit_cqring_flush(struct io_ring_ctx *ctx); 184 185 unsigned io_linked_nr(struct io_kiocb *req); 186 void io_req_track_inflight(struct io_kiocb *req); 187 struct file *io_file_get_normal(struct io_kiocb *req, int fd); 188 struct file *io_file_get_fixed(struct io_kiocb *req, int fd, 189 unsigned issue_flags); 190 struct file *io_uring_ctx_get_file(unsigned int fd, bool registered); 191 192 void io_req_task_queue(struct io_kiocb *req); 193 void io_req_task_complete(struct io_tw_req tw_req, io_tw_token_t tw); 194 void io_req_task_queue_fail(struct io_kiocb *req, int ret); 195 void io_req_task_submit(struct io_tw_req tw_req, io_tw_token_t tw); 196 __cold void io_uring_drop_tctx_refs(struct task_struct *task); 197 198 int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file, 199 int start, int end); 200 void io_queue_iowq(struct io_kiocb *req); 201 202 int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw); 203 int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr); 204 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin); 205 __cold void io_iopoll_try_reap_events(struct io_ring_ctx *ctx); 206 void __io_submit_flush_completions(struct io_ring_ctx *ctx); 207 208 bool io_wq_put_work(struct io_wq_work *work, struct io_wq_work **linked); 209 void io_wq_free_work(struct io_wq_work *work); 210 void io_wq_submit_work(struct io_wq_work *work); 211 212 void io_free_req(struct io_kiocb *req); 213 void io_queue_next(struct io_kiocb *req); 214 void io_task_refs_refill(struct io_uring_task *tctx); 215 bool __io_alloc_req_refill(struct io_ring_ctx *ctx); 216 217 void io_activate_pollwq(struct io_ring_ctx *ctx); 218 void io_restriction_clone(struct io_restriction *dst, struct io_restriction *src); 219 void io_poison_req(struct io_kiocb *req); 220 221 static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx) 222 { 223 #if defined(CONFIG_PROVE_LOCKING) 224 lockdep_assert(in_task()); 225 226 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) 227 lockdep_assert_held(&ctx->uring_lock); 228 229 if (ctx->flags & IORING_SETUP_IOPOLL) { 230 lockdep_assert_held(&ctx->uring_lock); 231 } else if (!(ctx->int_flags & IO_RING_F_TASK_COMPLETE)) { 232 lockdep_assert_held(&ctx->completion_lock); 233 } else if (ctx->submitter_task) { 234 /* 235 * ->submitter_task may be NULL and we can still post a CQE, 236 * if the ring has been setup with IORING_SETUP_R_DISABLED. 237 * Not from an SQE, as those cannot be submitted, but via 238 * updating tagged resources. 239 */ 240 if (!percpu_ref_is_dying(&ctx->refs)) 241 lockdep_assert(current == ctx->submitter_task); 242 } 243 #endif 244 } 245 246 static inline bool io_is_compat(struct io_ring_ctx *ctx) 247 { 248 return IS_ENABLED(CONFIG_COMPAT) && unlikely(ctx->int_flags & IO_RING_F_COMPAT); 249 } 250 251 static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) 252 { 253 if (!wq_list_empty(&ctx->submit_state.compl_reqs) || 254 ctx->submit_state.cq_flush) 255 __io_submit_flush_completions(ctx); 256 } 257 258 #define io_for_each_link(pos, head) \ 259 for (pos = (head); pos; pos = pos->link) 260 261 static inline bool io_get_cqe_overflow(struct io_ring_ctx *ctx, 262 struct io_uring_cqe **ret, 263 bool overflow, bool cqe32) 264 { 265 io_lockdep_assert_cq_locked(ctx); 266 267 if (unlikely(ctx->cqe_sentinel - ctx->cqe_cached < (cqe32 + 1))) { 268 if (unlikely(!io_cqe_cache_refill(ctx, overflow, cqe32))) 269 return false; 270 } 271 *ret = ctx->cqe_cached; 272 ctx->cached_cq_tail++; 273 ctx->cqe_cached++; 274 if (ctx->flags & IORING_SETUP_CQE32) { 275 ctx->cqe_cached++; 276 } else if (cqe32 && ctx->flags & IORING_SETUP_CQE_MIXED) { 277 ctx->cqe_cached++; 278 ctx->cached_cq_tail++; 279 } 280 WARN_ON_ONCE(ctx->cqe_cached > ctx->cqe_sentinel); 281 return true; 282 } 283 284 static inline bool io_get_cqe(struct io_ring_ctx *ctx, struct io_uring_cqe **ret, 285 bool cqe32) 286 { 287 return io_get_cqe_overflow(ctx, ret, false, cqe32); 288 } 289 290 static inline bool io_defer_get_uncommited_cqe(struct io_ring_ctx *ctx, 291 struct io_uring_cqe **cqe_ret) 292 { 293 io_lockdep_assert_cq_locked(ctx); 294 295 ctx->submit_state.cq_flush = true; 296 return io_get_cqe(ctx, cqe_ret, ctx->flags & IORING_SETUP_CQE_MIXED); 297 } 298 299 static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx, 300 struct io_kiocb *req) 301 { 302 bool is_cqe32 = req->cqe.flags & IORING_CQE_F_32; 303 struct io_uring_cqe *cqe; 304 305 /* 306 * If we can't get a cq entry, userspace overflowed the submission 307 * (by quite a lot). 308 */ 309 if (unlikely(!io_get_cqe(ctx, &cqe, is_cqe32))) 310 return false; 311 312 memcpy(cqe, &req->cqe, sizeof(*cqe)); 313 if (ctx->flags & IORING_SETUP_CQE32 || is_cqe32) { 314 memcpy(cqe->big_cqe, &req->big_cqe, sizeof(*cqe)); 315 memset(&req->big_cqe, 0, sizeof(req->big_cqe)); 316 } 317 318 if (trace_io_uring_complete_enabled()) 319 trace_call__io_uring_complete(req->ctx, req, cqe); 320 return true; 321 } 322 323 static inline ssize_t io_fixup_restart_res(ssize_t ret) 324 { 325 switch (ret) { 326 case -ERESTARTSYS: 327 case -ERESTARTNOINTR: 328 case -ERESTARTNOHAND: 329 case -ERESTART_RESTARTBLOCK: 330 /* 331 * We can't just restart the syscall, since previously 332 * submitted sqes may already be in progress. Just fail 333 * this IO with EINTR. 334 */ 335 return -EINTR; 336 default: 337 return ret; 338 } 339 } 340 341 static inline void *io_uring_alloc_async_data(struct io_alloc_cache *cache, 342 struct io_kiocb *req) 343 { 344 if (cache) { 345 req->async_data = io_cache_alloc(cache, GFP_KERNEL); 346 } else { 347 const struct io_issue_def *def = &io_issue_defs[req->opcode]; 348 349 WARN_ON_ONCE(!def->async_size); 350 req->async_data = kmalloc(def->async_size, GFP_KERNEL); 351 } 352 if (req->async_data) 353 req->flags |= REQ_F_ASYNC_DATA; 354 return req->async_data; 355 } 356 357 static inline bool req_has_async_data(struct io_kiocb *req) 358 { 359 return req->flags & REQ_F_ASYNC_DATA; 360 } 361 362 static inline void io_req_async_data_clear(struct io_kiocb *req, 363 io_req_flags_t extra_flags) 364 { 365 req->flags &= ~(REQ_F_ASYNC_DATA|extra_flags); 366 req->async_data = NULL; 367 } 368 369 static inline void io_req_async_data_free(struct io_kiocb *req) 370 { 371 kfree(req->async_data); 372 io_req_async_data_clear(req, 0); 373 } 374 375 static inline void io_put_file(struct io_kiocb *req) 376 { 377 if (!(req->flags & REQ_F_FIXED_FILE) && req->file) 378 fput(req->file); 379 } 380 381 static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx, 382 unsigned issue_flags) 383 { 384 lockdep_assert_held(&ctx->uring_lock); 385 if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) 386 mutex_unlock(&ctx->uring_lock); 387 } 388 389 static inline void io_ring_submit_lock(struct io_ring_ctx *ctx, 390 unsigned issue_flags) 391 { 392 /* 393 * "Normal" inline submissions always hold the uring_lock, since we 394 * grab it from the system call. Same is true for the SQPOLL offload. 395 * The only exception is when we've detached the request and issue it 396 * from an async worker thread, grab the lock for that case. 397 */ 398 if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) 399 mutex_lock(&ctx->uring_lock); 400 lockdep_assert_held(&ctx->uring_lock); 401 } 402 403 /* 404 * For a handler that is run by io-wq and parks the request on a list where 405 * cancellations find it, rather than completing it. Whoever tried to cancel 406 * the request before that found it running. It has flagged the work item, 407 * has searched the list in vain, and won't come back. To be called with 408 * the lock the search takes held: if the search came first, the flag is 409 * visible. For most lists that is ->uring_lock, timeouts use ->timeout_lock. 410 */ 411 static inline bool __io_wq_cancel_seen(struct io_kiocb *req, 412 unsigned int issue_flags) 413 { 414 return (issue_flags & IO_URING_F_IOWQ) && 415 (atomic_read(&req->work.flags) & IO_WQ_WORK_CANCEL); 416 } 417 418 static inline bool io_wq_cancel_seen(struct io_kiocb *req, 419 unsigned int issue_flags) 420 { 421 lockdep_assert_held(&req->ctx->uring_lock); 422 423 return __io_wq_cancel_seen(req, issue_flags); 424 } 425 426 static inline void io_commit_cqring(struct io_ring_ctx *ctx) 427 { 428 /* order cqe stores with ring update */ 429 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); 430 } 431 432 static inline void __io_wq_wake(struct wait_queue_head *wq) 433 { 434 /* 435 * 436 * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter 437 * set in the mask so that if we recurse back into our own poll 438 * waitqueue handlers, we know we have a dependency between eventfd or 439 * epoll and should terminate multishot poll at that point. 440 */ 441 if (wq_has_sleeper(wq)) 442 __wake_up(wq, TASK_NORMAL, 0, poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); 443 } 444 445 static inline void io_poll_wq_wake(struct io_ring_ctx *ctx) 446 { 447 __io_wq_wake(&ctx->poll_wq); 448 } 449 450 static inline void io_cqring_wake(struct io_ring_ctx *ctx) 451 { 452 /* 453 * Trigger waitqueue handler on all waiters on our waitqueue. This 454 * won't necessarily wake up all the tasks, io_should_wake() will make 455 * that decision. 456 */ 457 458 __io_wq_wake(&ctx->cq_wait); 459 } 460 461 static inline bool __io_sqring_full(struct io_ring_ctx *ctx) 462 { 463 struct io_rings *r = io_get_rings(ctx); 464 465 /* 466 * SQPOLL must use the actual sqring head, as using the cached_sq_head 467 * is race prone if the SQPOLL thread has grabbed entries but not yet 468 * committed them to the ring. For !SQPOLL, this doesn't matter, but 469 * since this helper is just used for SQPOLL sqring waits (or POLLOUT), 470 * just read the actual sqring head unconditionally. 471 */ 472 return READ_ONCE(r->sq.tail) - READ_ONCE(r->sq.head) == ctx->sq_entries; 473 } 474 475 static inline bool io_sqring_full(struct io_ring_ctx *ctx) 476 { 477 guard(rcu)(); 478 return __io_sqring_full(ctx); 479 } 480 481 static inline unsigned int __io_sqring_entries(struct io_ring_ctx *ctx) 482 { 483 struct io_rings *rings = io_get_rings(ctx); 484 unsigned int entries; 485 486 /* make sure SQ entry isn't read before tail */ 487 entries = smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; 488 return min(entries, ctx->sq_entries); 489 } 490 491 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) 492 { 493 guard(rcu)(); 494 return __io_sqring_entries(ctx); 495 } 496 497 /* 498 * Don't complete immediately but use deferred completion infrastructure. 499 * Protected by ->uring_lock and can only be used either with 500 * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex. 501 */ 502 static inline void io_req_complete_defer(struct io_kiocb *req) 503 __must_hold(&req->ctx->uring_lock) 504 { 505 struct io_submit_state *state = &req->ctx->submit_state; 506 507 lockdep_assert_held(&req->ctx->uring_lock); 508 509 wq_list_add_tail(&req->comp_list, &state->compl_reqs); 510 } 511 512 #define SHOULD_FLUSH_MASK (IO_RING_F_OFF_TIMEOUT_USED | \ 513 IO_RING_F_HAS_EVFD | IO_RING_F_POLL_ACTIVATED) 514 515 static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx) 516 { 517 if (unlikely(data_race(ctx->int_flags) & SHOULD_FLUSH_MASK)) 518 __io_commit_cqring_flush(ctx); 519 } 520 521 static inline void io_get_task_refs(int nr) 522 { 523 struct io_uring_task *tctx = current->io_uring; 524 525 tctx->cached_refs -= nr; 526 if (unlikely(tctx->cached_refs < 0)) 527 io_task_refs_refill(tctx); 528 } 529 530 static inline bool io_req_cache_empty(struct io_ring_ctx *ctx) 531 { 532 return !ctx->submit_state.free_list.next; 533 } 534 535 extern struct kmem_cache *req_cachep; 536 537 static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx) 538 { 539 struct io_kiocb *req; 540 541 req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list); 542 wq_stack_extract(&ctx->submit_state.free_list); 543 return req; 544 } 545 546 static inline bool io_alloc_req(struct io_ring_ctx *ctx, struct io_kiocb **req) 547 { 548 if (unlikely(io_req_cache_empty(ctx))) { 549 if (!__io_alloc_req_refill(ctx)) 550 return false; 551 } 552 *req = io_extract_req(ctx); 553 return true; 554 } 555 556 static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res) 557 { 558 io_req_set_res(req, res, 0); 559 req->io_task_work.func = io_req_task_complete; 560 io_req_task_work_add(req); 561 } 562 563 static inline bool io_file_can_poll(struct io_kiocb *req) 564 { 565 if (req->flags & REQ_F_CAN_POLL) 566 return true; 567 if (req->file && file_can_poll(req->file)) { 568 req->flags |= REQ_F_CAN_POLL; 569 return true; 570 } 571 return false; 572 } 573 574 static inline bool io_is_uring_cmd(const struct io_kiocb *req) 575 { 576 return req->opcode == IORING_OP_URING_CMD || 577 req->opcode == IORING_OP_URING_CMD128; 578 } 579 580 static inline ktime_t io_get_time(struct io_ring_ctx *ctx) 581 { 582 if (ctx->clockid == CLOCK_MONOTONIC) 583 return ktime_get(); 584 585 return ktime_get_with_offset(ctx->clock_offset); 586 } 587 588 enum { 589 IO_CHECK_CQ_OVERFLOW_BIT, 590 IO_CHECK_CQ_DROPPED_BIT, 591 }; 592 593 static inline bool io_has_work(struct io_ring_ctx *ctx) 594 { 595 return test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq) || 596 io_local_work_pending(ctx); 597 } 598 #endif 599