1 #ifndef IOU_CORE_H 2 #define IOU_CORE_H 3 4 #include <linux/errno.h> 5 #include <linux/lockdep.h> 6 #include <linux/resume_user_mode.h> 7 #include <linux/kasan.h> 8 #include <linux/poll.h> 9 #include <linux/io_uring_types.h> 10 #include <uapi/linux/eventpoll.h> 11 #include "alloc_cache.h" 12 #include "io-wq.h" 13 #include "slist.h" 14 #include "filetable.h" 15 #include "opdef.h" 16 17 #ifndef CREATE_TRACE_POINTS 18 #include <trace/events/io_uring.h> 19 #endif 20 21 enum { 22 IOU_OK = 0, /* deprecated, use IOU_COMPLETE */ 23 IOU_COMPLETE = 0, 24 25 IOU_ISSUE_SKIP_COMPLETE = -EIOCBQUEUED, 26 27 /* 28 * The request has more work to do and should be retried. io_uring will 29 * attempt to wait on the file for eligible opcodes, but otherwise 30 * it'll be handed to iowq for blocking execution. It works for normal 31 * requests as well as for the multi shot mode. 32 */ 33 IOU_RETRY = -EAGAIN, 34 35 /* 36 * Requeue the task_work to restart operations on this request. The 37 * actual value isn't important, should just be not an otherwise 38 * valid error code, yet less than -MAX_ERRNO and valid internally. 39 */ 40 IOU_REQUEUE = -3072, 41 }; 42 43 struct io_wait_queue { 44 struct wait_queue_entry wq; 45 struct io_ring_ctx *ctx; 46 unsigned cq_tail; 47 unsigned cq_min_tail; 48 unsigned nr_timeouts; 49 int hit_timeout; 50 ktime_t min_timeout; 51 ktime_t timeout; 52 struct hrtimer t; 53 54 #ifdef CONFIG_NET_RX_BUSY_POLL 55 ktime_t napi_busy_poll_dt; 56 bool napi_prefer_busy_poll; 57 #endif 58 }; 59 60 static inline bool io_should_wake(struct io_wait_queue *iowq) 61 { 62 struct io_ring_ctx *ctx = iowq->ctx; 63 int dist = READ_ONCE(ctx->rings->cq.tail) - (int) iowq->cq_tail; 64 65 /* 66 * Wake up if we have enough events, or if a timeout occurred since we 67 * started waiting. For timeouts, we always want to return to userspace, 68 * regardless of event count. 69 */ 70 return dist >= 0 || atomic_read(&ctx->cq_timeouts) != iowq->nr_timeouts; 71 } 72 73 #define IORING_MAX_ENTRIES 32768 74 #define IORING_MAX_CQ_ENTRIES (2 * IORING_MAX_ENTRIES) 75 76 unsigned long rings_size(unsigned int flags, unsigned int sq_entries, 77 unsigned int cq_entries, size_t *sq_offset); 78 int io_uring_fill_params(unsigned entries, struct io_uring_params *p); 79 bool io_cqe_cache_refill(struct io_ring_ctx *ctx, bool overflow); 80 int io_run_task_work_sig(struct io_ring_ctx *ctx); 81 void io_req_defer_failed(struct io_kiocb *req, s32 res); 82 bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 83 void io_add_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflags); 84 bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags); 85 void __io_commit_cqring_flush(struct io_ring_ctx *ctx); 86 87 struct file *io_file_get_normal(struct io_kiocb *req, int fd); 88 struct file *io_file_get_fixed(struct io_kiocb *req, int fd, 89 unsigned issue_flags); 90 91 void __io_req_task_work_add(struct io_kiocb *req, unsigned flags); 92 void io_req_task_work_add_remote(struct io_kiocb *req, unsigned flags); 93 void io_req_task_queue(struct io_kiocb *req); 94 void io_req_task_complete(struct io_kiocb *req, io_tw_token_t tw); 95 void io_req_task_queue_fail(struct io_kiocb *req, int ret); 96 void io_req_task_submit(struct io_kiocb *req, io_tw_token_t tw); 97 struct llist_node *io_handle_tw_list(struct llist_node *node, unsigned int *count, unsigned int max_entries); 98 struct llist_node *tctx_task_work_run(struct io_uring_task *tctx, unsigned int max_entries, unsigned int *count); 99 void tctx_task_work(struct callback_head *cb); 100 __cold void io_uring_cancel_generic(bool cancel_all, struct io_sq_data *sqd); 101 int io_uring_alloc_task_context(struct task_struct *task, 102 struct io_ring_ctx *ctx); 103 104 int io_ring_add_registered_file(struct io_uring_task *tctx, struct file *file, 105 int start, int end); 106 void io_req_queue_iowq(struct io_kiocb *req); 107 108 int io_poll_issue(struct io_kiocb *req, io_tw_token_t tw); 109 int io_submit_sqes(struct io_ring_ctx *ctx, unsigned int nr); 110 int io_do_iopoll(struct io_ring_ctx *ctx, bool force_nonspin); 111 void __io_submit_flush_completions(struct io_ring_ctx *ctx); 112 113 struct io_wq_work *io_wq_free_work(struct io_wq_work *work); 114 void io_wq_submit_work(struct io_wq_work *work); 115 116 void io_free_req(struct io_kiocb *req); 117 void io_queue_next(struct io_kiocb *req); 118 void io_task_refs_refill(struct io_uring_task *tctx); 119 bool __io_alloc_req_refill(struct io_ring_ctx *ctx); 120 121 bool io_match_task_safe(struct io_kiocb *head, struct io_uring_task *tctx, 122 bool cancel_all); 123 124 void io_activate_pollwq(struct io_ring_ctx *ctx); 125 126 static inline void io_lockdep_assert_cq_locked(struct io_ring_ctx *ctx) 127 { 128 #if defined(CONFIG_PROVE_LOCKING) 129 lockdep_assert(in_task()); 130 131 if (ctx->flags & IORING_SETUP_DEFER_TASKRUN) 132 lockdep_assert_held(&ctx->uring_lock); 133 134 if (ctx->flags & IORING_SETUP_IOPOLL) { 135 lockdep_assert_held(&ctx->uring_lock); 136 } else if (!ctx->task_complete) { 137 lockdep_assert_held(&ctx->completion_lock); 138 } else if (ctx->submitter_task) { 139 /* 140 * ->submitter_task may be NULL and we can still post a CQE, 141 * if the ring has been setup with IORING_SETUP_R_DISABLED. 142 * Not from an SQE, as those cannot be submitted, but via 143 * updating tagged resources. 144 */ 145 if (!percpu_ref_is_dying(&ctx->refs)) 146 lockdep_assert(current == ctx->submitter_task); 147 } 148 #endif 149 } 150 151 static inline bool io_is_compat(struct io_ring_ctx *ctx) 152 { 153 return IS_ENABLED(CONFIG_COMPAT) && unlikely(ctx->compat); 154 } 155 156 static inline void io_req_task_work_add(struct io_kiocb *req) 157 { 158 __io_req_task_work_add(req, 0); 159 } 160 161 static inline void io_submit_flush_completions(struct io_ring_ctx *ctx) 162 { 163 if (!wq_list_empty(&ctx->submit_state.compl_reqs) || 164 ctx->submit_state.cq_flush) 165 __io_submit_flush_completions(ctx); 166 } 167 168 #define io_for_each_link(pos, head) \ 169 for (pos = (head); pos; pos = pos->link) 170 171 static inline bool io_get_cqe_overflow(struct io_ring_ctx *ctx, 172 struct io_uring_cqe **ret, 173 bool overflow) 174 { 175 io_lockdep_assert_cq_locked(ctx); 176 177 if (unlikely(ctx->cqe_cached >= ctx->cqe_sentinel)) { 178 if (unlikely(!io_cqe_cache_refill(ctx, overflow))) 179 return false; 180 } 181 *ret = ctx->cqe_cached; 182 ctx->cached_cq_tail++; 183 ctx->cqe_cached++; 184 if (ctx->flags & IORING_SETUP_CQE32) 185 ctx->cqe_cached++; 186 return true; 187 } 188 189 static inline bool io_get_cqe(struct io_ring_ctx *ctx, struct io_uring_cqe **ret) 190 { 191 return io_get_cqe_overflow(ctx, ret, false); 192 } 193 194 static inline bool io_defer_get_uncommited_cqe(struct io_ring_ctx *ctx, 195 struct io_uring_cqe **cqe_ret) 196 { 197 io_lockdep_assert_cq_locked(ctx); 198 199 ctx->cq_extra++; 200 ctx->submit_state.cq_flush = true; 201 return io_get_cqe(ctx, cqe_ret); 202 } 203 204 static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx, 205 struct io_kiocb *req) 206 { 207 struct io_uring_cqe *cqe; 208 209 /* 210 * If we can't get a cq entry, userspace overflowed the 211 * submission (by quite a lot). Increment the overflow count in 212 * the ring. 213 */ 214 if (unlikely(!io_get_cqe(ctx, &cqe))) 215 return false; 216 217 218 memcpy(cqe, &req->cqe, sizeof(*cqe)); 219 if (ctx->flags & IORING_SETUP_CQE32) { 220 memcpy(cqe->big_cqe, &req->big_cqe, sizeof(*cqe)); 221 memset(&req->big_cqe, 0, sizeof(req->big_cqe)); 222 } 223 224 if (trace_io_uring_complete_enabled()) 225 trace_io_uring_complete(req->ctx, req, cqe); 226 return true; 227 } 228 229 static inline void req_set_fail(struct io_kiocb *req) 230 { 231 req->flags |= REQ_F_FAIL; 232 if (req->flags & REQ_F_CQE_SKIP) { 233 req->flags &= ~REQ_F_CQE_SKIP; 234 req->flags |= REQ_F_SKIP_LINK_CQES; 235 } 236 } 237 238 static inline void io_req_set_res(struct io_kiocb *req, s32 res, u32 cflags) 239 { 240 req->cqe.res = res; 241 req->cqe.flags = cflags; 242 } 243 244 static inline void *io_uring_alloc_async_data(struct io_alloc_cache *cache, 245 struct io_kiocb *req) 246 { 247 if (cache) { 248 req->async_data = io_cache_alloc(cache, GFP_KERNEL); 249 } else { 250 const struct io_issue_def *def = &io_issue_defs[req->opcode]; 251 252 WARN_ON_ONCE(!def->async_size); 253 req->async_data = kmalloc(def->async_size, GFP_KERNEL); 254 } 255 if (req->async_data) 256 req->flags |= REQ_F_ASYNC_DATA; 257 return req->async_data; 258 } 259 260 static inline bool req_has_async_data(struct io_kiocb *req) 261 { 262 return req->flags & REQ_F_ASYNC_DATA; 263 } 264 265 static inline void io_put_file(struct io_kiocb *req) 266 { 267 if (!(req->flags & REQ_F_FIXED_FILE) && req->file) 268 fput(req->file); 269 } 270 271 static inline void io_ring_submit_unlock(struct io_ring_ctx *ctx, 272 unsigned issue_flags) 273 { 274 lockdep_assert_held(&ctx->uring_lock); 275 if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) 276 mutex_unlock(&ctx->uring_lock); 277 } 278 279 static inline void io_ring_submit_lock(struct io_ring_ctx *ctx, 280 unsigned issue_flags) 281 { 282 /* 283 * "Normal" inline submissions always hold the uring_lock, since we 284 * grab it from the system call. Same is true for the SQPOLL offload. 285 * The only exception is when we've detached the request and issue it 286 * from an async worker thread, grab the lock for that case. 287 */ 288 if (unlikely(issue_flags & IO_URING_F_UNLOCKED)) 289 mutex_lock(&ctx->uring_lock); 290 lockdep_assert_held(&ctx->uring_lock); 291 } 292 293 static inline void io_commit_cqring(struct io_ring_ctx *ctx) 294 { 295 /* order cqe stores with ring update */ 296 smp_store_release(&ctx->rings->cq.tail, ctx->cached_cq_tail); 297 } 298 299 static inline void io_poll_wq_wake(struct io_ring_ctx *ctx) 300 { 301 if (wq_has_sleeper(&ctx->poll_wq)) 302 __wake_up(&ctx->poll_wq, TASK_NORMAL, 0, 303 poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); 304 } 305 306 static inline void io_cqring_wake(struct io_ring_ctx *ctx) 307 { 308 /* 309 * Trigger waitqueue handler on all waiters on our waitqueue. This 310 * won't necessarily wake up all the tasks, io_should_wake() will make 311 * that decision. 312 * 313 * Pass in EPOLLIN|EPOLL_URING_WAKE as the poll wakeup key. The latter 314 * set in the mask so that if we recurse back into our own poll 315 * waitqueue handlers, we know we have a dependency between eventfd or 316 * epoll and should terminate multishot poll at that point. 317 */ 318 if (wq_has_sleeper(&ctx->cq_wait)) 319 __wake_up(&ctx->cq_wait, TASK_NORMAL, 0, 320 poll_to_key(EPOLL_URING_WAKE | EPOLLIN)); 321 } 322 323 static inline bool io_sqring_full(struct io_ring_ctx *ctx) 324 { 325 struct io_rings *r = ctx->rings; 326 327 /* 328 * SQPOLL must use the actual sqring head, as using the cached_sq_head 329 * is race prone if the SQPOLL thread has grabbed entries but not yet 330 * committed them to the ring. For !SQPOLL, this doesn't matter, but 331 * since this helper is just used for SQPOLL sqring waits (or POLLOUT), 332 * just read the actual sqring head unconditionally. 333 */ 334 return READ_ONCE(r->sq.tail) - READ_ONCE(r->sq.head) == ctx->sq_entries; 335 } 336 337 static inline unsigned int io_sqring_entries(struct io_ring_ctx *ctx) 338 { 339 struct io_rings *rings = ctx->rings; 340 unsigned int entries; 341 342 /* make sure SQ entry isn't read before tail */ 343 entries = smp_load_acquire(&rings->sq.tail) - ctx->cached_sq_head; 344 return min(entries, ctx->sq_entries); 345 } 346 347 static inline int io_run_task_work(void) 348 { 349 bool ret = false; 350 351 /* 352 * Always check-and-clear the task_work notification signal. With how 353 * signaling works for task_work, we can find it set with nothing to 354 * run. We need to clear it for that case, like get_signal() does. 355 */ 356 if (test_thread_flag(TIF_NOTIFY_SIGNAL)) 357 clear_notify_signal(); 358 /* 359 * PF_IO_WORKER never returns to userspace, so check here if we have 360 * notify work that needs processing. 361 */ 362 if (current->flags & PF_IO_WORKER) { 363 if (test_thread_flag(TIF_NOTIFY_RESUME)) { 364 __set_current_state(TASK_RUNNING); 365 resume_user_mode_work(NULL); 366 } 367 if (current->io_uring) { 368 unsigned int count = 0; 369 370 __set_current_state(TASK_RUNNING); 371 tctx_task_work_run(current->io_uring, UINT_MAX, &count); 372 if (count) 373 ret = true; 374 } 375 } 376 if (task_work_pending(current)) { 377 __set_current_state(TASK_RUNNING); 378 task_work_run(); 379 ret = true; 380 } 381 382 return ret; 383 } 384 385 static inline bool io_local_work_pending(struct io_ring_ctx *ctx) 386 { 387 return !llist_empty(&ctx->work_llist) || !llist_empty(&ctx->retry_llist); 388 } 389 390 static inline bool io_task_work_pending(struct io_ring_ctx *ctx) 391 { 392 return task_work_pending(current) || io_local_work_pending(ctx); 393 } 394 395 static inline void io_tw_lock(struct io_ring_ctx *ctx, io_tw_token_t tw) 396 { 397 lockdep_assert_held(&ctx->uring_lock); 398 } 399 400 /* 401 * Don't complete immediately but use deferred completion infrastructure. 402 * Protected by ->uring_lock and can only be used either with 403 * IO_URING_F_COMPLETE_DEFER or inside a tw handler holding the mutex. 404 */ 405 static inline void io_req_complete_defer(struct io_kiocb *req) 406 __must_hold(&req->ctx->uring_lock) 407 { 408 struct io_submit_state *state = &req->ctx->submit_state; 409 410 lockdep_assert_held(&req->ctx->uring_lock); 411 412 wq_list_add_tail(&req->comp_list, &state->compl_reqs); 413 } 414 415 static inline void io_commit_cqring_flush(struct io_ring_ctx *ctx) 416 { 417 if (unlikely(ctx->off_timeout_used || ctx->drain_active || 418 ctx->has_evfd || ctx->poll_activated)) 419 __io_commit_cqring_flush(ctx); 420 } 421 422 static inline void io_get_task_refs(int nr) 423 { 424 struct io_uring_task *tctx = current->io_uring; 425 426 tctx->cached_refs -= nr; 427 if (unlikely(tctx->cached_refs < 0)) 428 io_task_refs_refill(tctx); 429 } 430 431 static inline bool io_req_cache_empty(struct io_ring_ctx *ctx) 432 { 433 return !ctx->submit_state.free_list.next; 434 } 435 436 extern struct kmem_cache *req_cachep; 437 438 static inline struct io_kiocb *io_extract_req(struct io_ring_ctx *ctx) 439 { 440 struct io_kiocb *req; 441 442 req = container_of(ctx->submit_state.free_list.next, struct io_kiocb, comp_list); 443 wq_stack_extract(&ctx->submit_state.free_list); 444 return req; 445 } 446 447 static inline bool io_alloc_req(struct io_ring_ctx *ctx, struct io_kiocb **req) 448 { 449 if (unlikely(io_req_cache_empty(ctx))) { 450 if (!__io_alloc_req_refill(ctx)) 451 return false; 452 } 453 *req = io_extract_req(ctx); 454 return true; 455 } 456 457 static inline bool io_allowed_defer_tw_run(struct io_ring_ctx *ctx) 458 { 459 return likely(ctx->submitter_task == current); 460 } 461 462 static inline bool io_allowed_run_tw(struct io_ring_ctx *ctx) 463 { 464 return likely(!(ctx->flags & IORING_SETUP_DEFER_TASKRUN) || 465 ctx->submitter_task == current); 466 } 467 468 /* 469 * Terminate the request if either of these conditions are true: 470 * 471 * 1) It's being executed by the original task, but that task is marked 472 * with PF_EXITING as it's exiting. 473 * 2) PF_KTHREAD is set, in which case the invoker of the task_work is 474 * our fallback task_work. 475 */ 476 static inline bool io_should_terminate_tw(void) 477 { 478 return current->flags & (PF_KTHREAD | PF_EXITING); 479 } 480 481 static inline void io_req_queue_tw_complete(struct io_kiocb *req, s32 res) 482 { 483 io_req_set_res(req, res, 0); 484 req->io_task_work.func = io_req_task_complete; 485 io_req_task_work_add(req); 486 } 487 488 /* 489 * IORING_SETUP_SQE128 contexts allocate twice the normal SQE size for each 490 * slot. 491 */ 492 static inline size_t uring_sqe_size(struct io_ring_ctx *ctx) 493 { 494 if (ctx->flags & IORING_SETUP_SQE128) 495 return 2 * sizeof(struct io_uring_sqe); 496 return sizeof(struct io_uring_sqe); 497 } 498 499 static inline bool io_file_can_poll(struct io_kiocb *req) 500 { 501 if (req->flags & REQ_F_CAN_POLL) 502 return true; 503 if (req->file && file_can_poll(req->file)) { 504 req->flags |= REQ_F_CAN_POLL; 505 return true; 506 } 507 return false; 508 } 509 510 static inline ktime_t io_get_time(struct io_ring_ctx *ctx) 511 { 512 if (ctx->clockid == CLOCK_MONOTONIC) 513 return ktime_get(); 514 515 return ktime_get_with_offset(ctx->clock_offset); 516 } 517 518 enum { 519 IO_CHECK_CQ_OVERFLOW_BIT, 520 IO_CHECK_CQ_DROPPED_BIT, 521 }; 522 523 static inline bool io_has_work(struct io_ring_ctx *ctx) 524 { 525 return test_bit(IO_CHECK_CQ_OVERFLOW_BIT, &ctx->check_cq) || 526 io_local_work_pending(ctx); 527 } 528 #endif 529