1 #ifndef IO_URING_TYPES_H
2 #define IO_URING_TYPES_H
4 #include <linux/blkdev.h>
5 #include <linux/hashtable.h>
6 #include <linux/task_work.h>
7 #include <linux/bitmap.h>
8 #include <linux/llist.h>
9 #include <uapi/linux/io_uring.h>
13 * A hint to not wake right away but delay until there are enough of
14 * tw's queued to match the number of CQEs the task is waiting for.
16 * Must not be used with requests generating more than one CQE.
17 * It's also ignored unless IORING_SETUP_DEFER_TASKRUN is set.
19 IOU_F_TWQ_LAZY_WAKE = 1,
22 enum io_uring_cmd_flags {
23 IO_URING_F_COMPLETE_DEFER = 1,
24 IO_URING_F_UNLOCKED = 2,
25 /* the request is executed from poll, it should not be freed */
26 IO_URING_F_MULTISHOT = 4,
27 /* executed by io-wq */
29 /* int's last bit, sign checks are usually faster than a bit test */
30 IO_URING_F_NONBLOCK = INT_MIN,
32 /* ctx state flags, for URING_CMD */
33 IO_URING_F_SQE128 = (1 << 8),
34 IO_URING_F_CQE32 = (1 << 9),
35 IO_URING_F_IOPOLL = (1 << 10),
37 /* set when uring wants to cancel a previously issued command */
38 IO_URING_F_CANCEL = (1 << 11),
39 IO_URING_F_COMPAT = (1 << 12),
40 IO_URING_F_TASK_DEAD = (1 << 13),
43 struct io_wq_work_node {
44 struct io_wq_work_node *next;
47 struct io_wq_work_list {
48 struct io_wq_work_node *first;
49 struct io_wq_work_node *last;
53 struct io_wq_work_node list;
55 /* place it here instead of io_kiocb as it fills padding and saves 4B */
61 struct io_rsrc_node **nodes;
64 struct io_file_table {
65 struct io_rsrc_data data;
66 unsigned long *bitmap;
67 unsigned int alloc_hint;
70 struct io_hash_bucket {
71 struct hlist_head list;
72 } ____cacheline_aligned_in_smp;
74 struct io_hash_table {
75 struct io_hash_bucket *hbs;
79 struct io_mapped_region {
86 * Arbitrary limit, can be raised if need be
88 #define IO_RINGFD_REG_MAX 16
90 struct io_uring_task {
93 const struct io_ring_ctx *last;
94 struct task_struct *task;
96 struct file *registered_rings[IO_RINGFD_REG_MAX];
99 struct wait_queue_head wait;
101 atomic_t inflight_tracked;
102 struct percpu_counter inflight;
104 struct { /* task_work */
105 struct llist_head task_list;
106 struct callback_head task_work;
107 } ____cacheline_aligned_in_smp;
116 * This data is shared with the application through the mmap at offsets
117 * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
119 * The offsets to the member fields are published through struct
120 * io_sqring_offsets when calling io_uring_setup.
124 * Head and tail offsets into the ring; the offsets need to be
125 * masked to get valid indices.
127 * The kernel controls head of the sq ring and the tail of the cq ring,
128 * and the application controls tail of the sq ring and the head of the
131 struct io_uring sq, cq;
133 * Bitmasks to apply to head and tail offsets (constant, equals
136 u32 sq_ring_mask, cq_ring_mask;
137 /* Ring sizes (constant, power of 2) */
138 u32 sq_ring_entries, cq_ring_entries;
140 * Number of invalid entries dropped by the kernel due to
141 * invalid index stored in array
143 * Written by the kernel, shouldn't be modified by the
144 * application (i.e. get number of "new events" by comparing to
147 * After a new SQ head value was read by the application this
148 * counter includes all submissions that were dropped reaching
149 * the new SQ head (and possibly more).
155 * Written by the kernel, shouldn't be modified by the
158 * The application needs a full memory barrier before checking
159 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
165 * Written by the application, shouldn't be modified by the
170 * Number of completion events lost because the queue was full;
171 * this should be avoided by the application by making sure
172 * there are not more requests pending than there is space in
173 * the completion queue.
175 * Written by the kernel, shouldn't be modified by the
176 * application (i.e. get number of "new events" by comparing to
179 * As completion events come in out of order this counter is not
180 * ordered with any other data.
184 * Ring buffer of completion events.
186 * The kernel writes completion events fresh every time they are
187 * produced, so the application is allowed to modify pending
190 struct io_uring_cqe cqes[] ____cacheline_aligned_in_smp;
193 struct io_restriction {
194 DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
195 DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
196 u8 sqe_flags_allowed;
197 u8 sqe_flags_required;
201 struct io_submit_link {
202 struct io_kiocb *head;
203 struct io_kiocb *last;
206 struct io_submit_state {
207 /* inline/task_work completion list, under ->uring_lock */
208 struct io_wq_work_node free_list;
209 /* batch completion logic */
210 struct io_wq_work_list compl_reqs;
211 struct io_submit_link link;
216 unsigned short submit_nr;
217 struct blk_plug plug;
220 struct io_alloc_cache {
222 unsigned int nr_cached;
223 unsigned int max_cached;
228 /* const or read-mostly hot data */
231 unsigned int drain_next: 1;
232 unsigned int restricted: 1;
233 unsigned int off_timeout_used: 1;
234 unsigned int drain_active: 1;
235 unsigned int has_evfd: 1;
236 /* all CQEs should be posted only by the submitter task */
237 unsigned int task_complete: 1;
238 unsigned int lockless_cq: 1;
239 unsigned int syscall_iopoll: 1;
240 unsigned int poll_activated: 1;
241 unsigned int drain_disabled: 1;
242 unsigned int compat: 1;
243 unsigned int iowq_limits_set : 1;
245 struct task_struct *submitter_task;
246 struct io_rings *rings;
247 struct percpu_ref refs;
250 enum tk_offsets clock_offset;
252 enum task_work_notify_mode notify_method;
253 unsigned sq_thread_idle;
254 } ____cacheline_aligned_in_smp;
256 /* submission data */
258 struct mutex uring_lock;
261 * Ring buffer of indices into array of io_uring_sqe, which is
262 * mmapped by the application using the IORING_OFF_SQES offset.
264 * This indirection could e.g. be used to assign fixed
265 * io_uring_sqe entries to operations and only submit them to
266 * the queue when needed.
268 * The kernel modifies neither the indices array nor the entries
272 struct io_uring_sqe *sq_sqes;
273 unsigned cached_sq_head;
277 * Fixed resources fast path, should be accessed only under
278 * uring_lock, and updated through io_uring_register(2)
283 * ->iopoll_list is protected by the ctx->uring_lock for
284 * io_uring instances that don't use IORING_SETUP_SQPOLL.
285 * For SQPOLL, only the single threaded io_sq_thread() will
286 * manipulate the list, hence no extra locking is needed there.
288 bool poll_multi_queue;
289 struct io_wq_work_list iopoll_list;
291 struct io_file_table file_table;
292 struct io_rsrc_data buf_table;
294 struct io_submit_state submit_state;
296 struct xarray io_bl_xa;
298 struct io_hash_table cancel_table;
299 struct io_alloc_cache apoll_cache;
300 struct io_alloc_cache netmsg_cache;
301 struct io_alloc_cache rw_cache;
302 struct io_alloc_cache uring_cache;
305 * Any cancelable uring_cmd is added to this list in
306 * ->uring_cmd() by io_uring_cmd_insert_cancelable()
308 struct hlist_head cancelable_uring_cmd;
310 * For Hybrid IOPOLL, runtime in hybrid polling, without
313 u64 hybrid_poll_time;
314 } ____cacheline_aligned_in_smp;
318 * We cache a range of free CQEs we can use, once exhausted it
319 * should go through a slower range setup, see __io_get_cqe()
321 struct io_uring_cqe *cqe_cached;
322 struct io_uring_cqe *cqe_sentinel;
324 unsigned cached_cq_tail;
326 struct io_ev_fd __rcu *io_ev_fd;
331 } ____cacheline_aligned_in_smp;
334 * task_work and async notification delivery cacheline. Expected to
335 * regularly bounce b/w CPUs.
338 struct llist_head work_llist;
339 struct llist_head retry_llist;
340 unsigned long check_cq;
342 atomic_t cq_timeouts;
343 struct wait_queue_head cq_wait;
344 } ____cacheline_aligned_in_smp;
348 spinlock_t timeout_lock;
349 struct list_head timeout_list;
350 struct list_head ltimeout_list;
351 unsigned cq_last_tm_flush;
352 } ____cacheline_aligned_in_smp;
354 spinlock_t completion_lock;
356 struct list_head io_buffers_comp;
357 struct list_head cq_overflow_list;
359 struct hlist_head waitid_list;
362 struct hlist_head futex_list;
363 struct io_alloc_cache futex_cache;
366 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
367 struct io_sq_data *sq_data; /* if using sq thread polling */
369 struct wait_queue_head sqo_sq_wait;
370 struct list_head sqd_list;
372 unsigned int file_alloc_start;
373 unsigned int file_alloc_end;
375 struct list_head io_buffers_cache;
377 /* Keep this last, we don't need it for the fast path */
378 struct wait_queue_head poll_wq;
379 struct io_restriction restrictions;
382 struct xarray personalities;
384 /* hashed buffered write serialization */
385 struct io_wq_hash *hash_map;
387 /* Only used for accounting purposes */
388 struct user_struct *user;
389 struct mm_struct *mm_account;
391 /* ctx exit and cancelation */
392 struct llist_head fallback_llist;
393 struct delayed_work fallback_work;
394 struct work_struct exit_work;
395 struct list_head tctx_list;
396 struct completion ref_comp;
398 /* io-wq management, e.g. thread count */
401 struct callback_head poll_wq_task_work;
402 struct list_head defer_list;
404 struct io_alloc_cache msg_cache;
407 #ifdef CONFIG_NET_RX_BUSY_POLL
408 struct list_head napi_list; /* track busy poll napi_id */
409 spinlock_t napi_lock; /* napi_list lock */
411 /* napi busy poll default timeout */
412 ktime_t napi_busy_poll_dt;
413 bool napi_prefer_busy_poll;
416 DECLARE_HASHTABLE(napi_ht, 4);
419 /* protected by ->completion_lock */
420 unsigned evfd_last_cq_tail;
423 * Protection for resize vs mmap races - both the mmap and resize
424 * side will need to grab this lock, to prevent either side from
425 * being run concurrently with the other.
427 struct mutex resize_lock;
430 * If IORING_SETUP_NO_MMAP is used, then the below holds
431 * the gup'ed pages for the two rings, and the sqes.
433 unsigned short n_ring_pages;
434 unsigned short n_sqe_pages;
435 struct page **ring_pages;
436 struct page **sqe_pages;
438 /* used for optimised request parameter and wait argument passing */
439 struct io_mapped_region param_region;
446 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
447 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
448 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
449 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
450 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
451 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
452 REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT,
454 /* first byte is taken by user flags, shift it to not overlap */
459 REQ_F_LINK_TIMEOUT_BIT,
460 REQ_F_NEED_CLEANUP_BIT,
462 REQ_F_HYBRID_IOPOLL_STATE_BIT,
463 REQ_F_BUFFER_SELECTED_BIT,
464 REQ_F_BUFFER_RING_BIT,
468 REQ_F_ARM_LTIMEOUT_BIT,
469 REQ_F_ASYNC_DATA_BIT,
470 REQ_F_SKIP_LINK_CQES_BIT,
471 REQ_F_SINGLE_POLL_BIT,
472 REQ_F_DOUBLE_POLL_BIT,
473 REQ_F_APOLL_MULTISHOT_BIT,
474 REQ_F_CLEAR_POLLIN_BIT,
475 /* keep async read/write and isreg together and in order */
476 REQ_F_SUPPORT_NOWAIT_BIT,
478 REQ_F_POLL_NO_LAZY_BIT,
481 REQ_F_BL_NO_RECYCLE_BIT,
482 REQ_F_BUFFERS_COMMIT_BIT,
485 /* not a real bit, just to check we're not overflowing the space */
489 typedef u64 __bitwise io_req_flags_t;
490 #define IO_REQ_FLAG(bitno) ((__force io_req_flags_t) BIT_ULL((bitno)))
494 REQ_F_FIXED_FILE = IO_REQ_FLAG(REQ_F_FIXED_FILE_BIT),
495 /* drain existing IO first */
496 REQ_F_IO_DRAIN = IO_REQ_FLAG(REQ_F_IO_DRAIN_BIT),
498 REQ_F_LINK = IO_REQ_FLAG(REQ_F_LINK_BIT),
499 /* doesn't sever on completion < 0 */
500 REQ_F_HARDLINK = IO_REQ_FLAG(REQ_F_HARDLINK_BIT),
502 REQ_F_FORCE_ASYNC = IO_REQ_FLAG(REQ_F_FORCE_ASYNC_BIT),
503 /* IOSQE_BUFFER_SELECT */
504 REQ_F_BUFFER_SELECT = IO_REQ_FLAG(REQ_F_BUFFER_SELECT_BIT),
505 /* IOSQE_CQE_SKIP_SUCCESS */
506 REQ_F_CQE_SKIP = IO_REQ_FLAG(REQ_F_CQE_SKIP_BIT),
508 /* fail rest of links */
509 REQ_F_FAIL = IO_REQ_FLAG(REQ_F_FAIL_BIT),
510 /* on inflight list, should be cancelled and waited on exit reliably */
511 REQ_F_INFLIGHT = IO_REQ_FLAG(REQ_F_INFLIGHT_BIT),
512 /* read/write uses file position */
513 REQ_F_CUR_POS = IO_REQ_FLAG(REQ_F_CUR_POS_BIT),
514 /* must not punt to workers */
515 REQ_F_NOWAIT = IO_REQ_FLAG(REQ_F_NOWAIT_BIT),
516 /* has or had linked timeout */
517 REQ_F_LINK_TIMEOUT = IO_REQ_FLAG(REQ_F_LINK_TIMEOUT_BIT),
519 REQ_F_NEED_CLEANUP = IO_REQ_FLAG(REQ_F_NEED_CLEANUP_BIT),
520 /* already went through poll handler */
521 REQ_F_POLLED = IO_REQ_FLAG(REQ_F_POLLED_BIT),
522 /* every req only blocks once in hybrid poll */
523 REQ_F_IOPOLL_STATE = IO_REQ_FLAG(REQ_F_HYBRID_IOPOLL_STATE_BIT),
524 /* buffer already selected */
525 REQ_F_BUFFER_SELECTED = IO_REQ_FLAG(REQ_F_BUFFER_SELECTED_BIT),
526 /* buffer selected from ring, needs commit */
527 REQ_F_BUFFER_RING = IO_REQ_FLAG(REQ_F_BUFFER_RING_BIT),
528 /* caller should reissue async */
529 REQ_F_REISSUE = IO_REQ_FLAG(REQ_F_REISSUE_BIT),
530 /* supports async reads/writes */
531 REQ_F_SUPPORT_NOWAIT = IO_REQ_FLAG(REQ_F_SUPPORT_NOWAIT_BIT),
533 REQ_F_ISREG = IO_REQ_FLAG(REQ_F_ISREG_BIT),
534 /* has creds assigned */
535 REQ_F_CREDS = IO_REQ_FLAG(REQ_F_CREDS_BIT),
536 /* skip refcounting if not set */
537 REQ_F_REFCOUNT = IO_REQ_FLAG(REQ_F_REFCOUNT_BIT),
538 /* there is a linked timeout that has to be armed */
539 REQ_F_ARM_LTIMEOUT = IO_REQ_FLAG(REQ_F_ARM_LTIMEOUT_BIT),
540 /* ->async_data allocated */
541 REQ_F_ASYNC_DATA = IO_REQ_FLAG(REQ_F_ASYNC_DATA_BIT),
542 /* don't post CQEs while failing linked requests */
543 REQ_F_SKIP_LINK_CQES = IO_REQ_FLAG(REQ_F_SKIP_LINK_CQES_BIT),
544 /* single poll may be active */
545 REQ_F_SINGLE_POLL = IO_REQ_FLAG(REQ_F_SINGLE_POLL_BIT),
546 /* double poll may active */
547 REQ_F_DOUBLE_POLL = IO_REQ_FLAG(REQ_F_DOUBLE_POLL_BIT),
548 /* fast poll multishot mode */
549 REQ_F_APOLL_MULTISHOT = IO_REQ_FLAG(REQ_F_APOLL_MULTISHOT_BIT),
550 /* recvmsg special flag, clear EPOLLIN */
551 REQ_F_CLEAR_POLLIN = IO_REQ_FLAG(REQ_F_CLEAR_POLLIN_BIT),
552 /* don't use lazy poll wake for this request */
553 REQ_F_POLL_NO_LAZY = IO_REQ_FLAG(REQ_F_POLL_NO_LAZY_BIT),
554 /* file is pollable */
555 REQ_F_CAN_POLL = IO_REQ_FLAG(REQ_F_CAN_POLL_BIT),
556 /* buffer list was empty after selection of buffer */
557 REQ_F_BL_EMPTY = IO_REQ_FLAG(REQ_F_BL_EMPTY_BIT),
558 /* don't recycle provided buffers for this request */
559 REQ_F_BL_NO_RECYCLE = IO_REQ_FLAG(REQ_F_BL_NO_RECYCLE_BIT),
560 /* buffer ring head needs incrementing on put */
561 REQ_F_BUFFERS_COMMIT = IO_REQ_FLAG(REQ_F_BUFFERS_COMMIT_BIT),
562 /* buf node is valid */
563 REQ_F_BUF_NODE = IO_REQ_FLAG(REQ_F_BUF_NODE_BIT),
566 typedef void (*io_req_tw_func_t)(struct io_kiocb *req, struct io_tw_state *ts);
568 struct io_task_work {
569 struct llist_node node;
570 io_req_tw_func_t func;
576 /* fd initially, then cflags for completion */
584 * Each request type overlays its private data structure on top of this one.
585 * They must not exceed this one in size.
589 /* each command gets 56 bytes of data */
593 static inline void io_kiocb_cmd_sz_check(size_t cmd_sz)
595 BUILD_BUG_ON(cmd_sz > sizeof(struct io_cmd_data));
597 #define io_kiocb_to_cmd(req, cmd_type) ( \
598 io_kiocb_cmd_sz_check(sizeof(cmd_type)) , \
599 ((cmd_type *)&(req)->cmd) \
601 #define cmd_to_io_kiocb(ptr) ((struct io_kiocb *) ptr)
606 * NOTE! Each of the io_kiocb union members has the file pointer
607 * as the first entry in their struct definition. So you can
608 * access the file pointer through any of the sub-structs,
609 * or directly as just 'file' in this struct.
612 struct io_cmd_data cmd;
616 /* polled IO has completed */
619 * Can be either a fixed buffer index, or used with provided buffers.
620 * For the latter, before issue it points to the buffer group ID,
621 * and after selection it points to the buffer ID itself.
628 io_req_flags_t flags;
632 struct io_ring_ctx *ctx;
633 struct io_uring_task *tctx;
636 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
637 struct io_buffer *kbuf;
640 * stores buffer ID for ring provided buffers, valid IFF
641 * REQ_F_BUFFER_RING is set.
643 struct io_buffer_list *buf_list;
645 struct io_rsrc_node *buf_node;
649 /* used by request caches, completion batching and iopoll */
650 struct io_wq_work_node comp_list;
651 /* cache ->apoll->events */
652 __poll_t apoll_events;
655 struct io_rsrc_node *file_node;
659 struct io_task_work io_task_work;
662 * for polled requests, i.e. IORING_OP_POLL_ADD and async armed
665 struct hlist_node hash_node;
666 /* For IOPOLL setup queues, with hybrid polling */
669 /* internal polling, see IORING_FEAT_FAST_POLL */
670 struct async_poll *apoll;
671 /* opcode allocated if it needs to store data for async defer */
673 /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
675 struct io_kiocb *link;
676 /* custom credentials, valid IFF REQ_F_CREDS is set */
677 const struct cred *creds;
678 struct io_wq_work work;
686 struct io_overflow_cqe {
687 struct list_head list;
688 struct io_uring_cqe cqe;
691 static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx)
693 return ctx->flags & IORING_SETUP_CQE32;