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 unsigned long check_cq;
341 atomic_t cq_timeouts;
342 struct wait_queue_head cq_wait;
343 } ____cacheline_aligned_in_smp;
347 spinlock_t timeout_lock;
348 struct list_head timeout_list;
349 struct list_head ltimeout_list;
350 unsigned cq_last_tm_flush;
351 } ____cacheline_aligned_in_smp;
353 spinlock_t completion_lock;
355 struct list_head io_buffers_comp;
356 struct list_head cq_overflow_list;
358 struct hlist_head waitid_list;
361 struct hlist_head futex_list;
362 struct io_alloc_cache futex_cache;
365 const struct cred *sq_creds; /* cred used for __io_sq_thread() */
366 struct io_sq_data *sq_data; /* if using sq thread polling */
368 struct wait_queue_head sqo_sq_wait;
369 struct list_head sqd_list;
371 unsigned int file_alloc_start;
372 unsigned int file_alloc_end;
374 struct list_head io_buffers_cache;
376 /* Keep this last, we don't need it for the fast path */
377 struct wait_queue_head poll_wq;
378 struct io_restriction restrictions;
381 struct xarray personalities;
383 /* hashed buffered write serialization */
384 struct io_wq_hash *hash_map;
386 /* Only used for accounting purposes */
387 struct user_struct *user;
388 struct mm_struct *mm_account;
390 /* ctx exit and cancelation */
391 struct llist_head fallback_llist;
392 struct delayed_work fallback_work;
393 struct work_struct exit_work;
394 struct list_head tctx_list;
395 struct completion ref_comp;
397 /* io-wq management, e.g. thread count */
400 struct callback_head poll_wq_task_work;
401 struct list_head defer_list;
403 struct io_alloc_cache msg_cache;
406 #ifdef CONFIG_NET_RX_BUSY_POLL
407 struct list_head napi_list; /* track busy poll napi_id */
408 spinlock_t napi_lock; /* napi_list lock */
410 /* napi busy poll default timeout */
411 ktime_t napi_busy_poll_dt;
412 bool napi_prefer_busy_poll;
415 DECLARE_HASHTABLE(napi_ht, 4);
418 /* protected by ->completion_lock */
419 unsigned evfd_last_cq_tail;
422 * Protection for resize vs mmap races - both the mmap and resize
423 * side will need to grab this lock, to prevent either side from
424 * being run concurrently with the other.
426 struct mutex resize_lock;
429 * If IORING_SETUP_NO_MMAP is used, then the below holds
430 * the gup'ed pages for the two rings, and the sqes.
432 unsigned short n_ring_pages;
433 unsigned short n_sqe_pages;
434 struct page **ring_pages;
435 struct page **sqe_pages;
437 /* used for optimised request parameter and wait argument passing */
438 struct io_mapped_region param_region;
445 REQ_F_FIXED_FILE_BIT = IOSQE_FIXED_FILE_BIT,
446 REQ_F_IO_DRAIN_BIT = IOSQE_IO_DRAIN_BIT,
447 REQ_F_LINK_BIT = IOSQE_IO_LINK_BIT,
448 REQ_F_HARDLINK_BIT = IOSQE_IO_HARDLINK_BIT,
449 REQ_F_FORCE_ASYNC_BIT = IOSQE_ASYNC_BIT,
450 REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
451 REQ_F_CQE_SKIP_BIT = IOSQE_CQE_SKIP_SUCCESS_BIT,
453 /* first byte is taken by user flags, shift it to not overlap */
458 REQ_F_LINK_TIMEOUT_BIT,
459 REQ_F_NEED_CLEANUP_BIT,
461 REQ_F_HYBRID_IOPOLL_STATE_BIT,
462 REQ_F_BUFFER_SELECTED_BIT,
463 REQ_F_BUFFER_RING_BIT,
467 REQ_F_ARM_LTIMEOUT_BIT,
468 REQ_F_ASYNC_DATA_BIT,
469 REQ_F_SKIP_LINK_CQES_BIT,
470 REQ_F_SINGLE_POLL_BIT,
471 REQ_F_DOUBLE_POLL_BIT,
472 REQ_F_APOLL_MULTISHOT_BIT,
473 REQ_F_CLEAR_POLLIN_BIT,
474 /* keep async read/write and isreg together and in order */
475 REQ_F_SUPPORT_NOWAIT_BIT,
477 REQ_F_POLL_NO_LAZY_BIT,
480 REQ_F_BL_NO_RECYCLE_BIT,
481 REQ_F_BUFFERS_COMMIT_BIT,
484 /* not a real bit, just to check we're not overflowing the space */
488 typedef u64 __bitwise io_req_flags_t;
489 #define IO_REQ_FLAG(bitno) ((__force io_req_flags_t) BIT_ULL((bitno)))
493 REQ_F_FIXED_FILE = IO_REQ_FLAG(REQ_F_FIXED_FILE_BIT),
494 /* drain existing IO first */
495 REQ_F_IO_DRAIN = IO_REQ_FLAG(REQ_F_IO_DRAIN_BIT),
497 REQ_F_LINK = IO_REQ_FLAG(REQ_F_LINK_BIT),
498 /* doesn't sever on completion < 0 */
499 REQ_F_HARDLINK = IO_REQ_FLAG(REQ_F_HARDLINK_BIT),
501 REQ_F_FORCE_ASYNC = IO_REQ_FLAG(REQ_F_FORCE_ASYNC_BIT),
502 /* IOSQE_BUFFER_SELECT */
503 REQ_F_BUFFER_SELECT = IO_REQ_FLAG(REQ_F_BUFFER_SELECT_BIT),
504 /* IOSQE_CQE_SKIP_SUCCESS */
505 REQ_F_CQE_SKIP = IO_REQ_FLAG(REQ_F_CQE_SKIP_BIT),
507 /* fail rest of links */
508 REQ_F_FAIL = IO_REQ_FLAG(REQ_F_FAIL_BIT),
509 /* on inflight list, should be cancelled and waited on exit reliably */
510 REQ_F_INFLIGHT = IO_REQ_FLAG(REQ_F_INFLIGHT_BIT),
511 /* read/write uses file position */
512 REQ_F_CUR_POS = IO_REQ_FLAG(REQ_F_CUR_POS_BIT),
513 /* must not punt to workers */
514 REQ_F_NOWAIT = IO_REQ_FLAG(REQ_F_NOWAIT_BIT),
515 /* has or had linked timeout */
516 REQ_F_LINK_TIMEOUT = IO_REQ_FLAG(REQ_F_LINK_TIMEOUT_BIT),
518 REQ_F_NEED_CLEANUP = IO_REQ_FLAG(REQ_F_NEED_CLEANUP_BIT),
519 /* already went through poll handler */
520 REQ_F_POLLED = IO_REQ_FLAG(REQ_F_POLLED_BIT),
521 /* every req only blocks once in hybrid poll */
522 REQ_F_IOPOLL_STATE = IO_REQ_FLAG(REQ_F_HYBRID_IOPOLL_STATE_BIT),
523 /* buffer already selected */
524 REQ_F_BUFFER_SELECTED = IO_REQ_FLAG(REQ_F_BUFFER_SELECTED_BIT),
525 /* buffer selected from ring, needs commit */
526 REQ_F_BUFFER_RING = IO_REQ_FLAG(REQ_F_BUFFER_RING_BIT),
527 /* caller should reissue async */
528 REQ_F_REISSUE = IO_REQ_FLAG(REQ_F_REISSUE_BIT),
529 /* supports async reads/writes */
530 REQ_F_SUPPORT_NOWAIT = IO_REQ_FLAG(REQ_F_SUPPORT_NOWAIT_BIT),
532 REQ_F_ISREG = IO_REQ_FLAG(REQ_F_ISREG_BIT),
533 /* has creds assigned */
534 REQ_F_CREDS = IO_REQ_FLAG(REQ_F_CREDS_BIT),
535 /* skip refcounting if not set */
536 REQ_F_REFCOUNT = IO_REQ_FLAG(REQ_F_REFCOUNT_BIT),
537 /* there is a linked timeout that has to be armed */
538 REQ_F_ARM_LTIMEOUT = IO_REQ_FLAG(REQ_F_ARM_LTIMEOUT_BIT),
539 /* ->async_data allocated */
540 REQ_F_ASYNC_DATA = IO_REQ_FLAG(REQ_F_ASYNC_DATA_BIT),
541 /* don't post CQEs while failing linked requests */
542 REQ_F_SKIP_LINK_CQES = IO_REQ_FLAG(REQ_F_SKIP_LINK_CQES_BIT),
543 /* single poll may be active */
544 REQ_F_SINGLE_POLL = IO_REQ_FLAG(REQ_F_SINGLE_POLL_BIT),
545 /* double poll may active */
546 REQ_F_DOUBLE_POLL = IO_REQ_FLAG(REQ_F_DOUBLE_POLL_BIT),
547 /* fast poll multishot mode */
548 REQ_F_APOLL_MULTISHOT = IO_REQ_FLAG(REQ_F_APOLL_MULTISHOT_BIT),
549 /* recvmsg special flag, clear EPOLLIN */
550 REQ_F_CLEAR_POLLIN = IO_REQ_FLAG(REQ_F_CLEAR_POLLIN_BIT),
551 /* don't use lazy poll wake for this request */
552 REQ_F_POLL_NO_LAZY = IO_REQ_FLAG(REQ_F_POLL_NO_LAZY_BIT),
553 /* file is pollable */
554 REQ_F_CAN_POLL = IO_REQ_FLAG(REQ_F_CAN_POLL_BIT),
555 /* buffer list was empty after selection of buffer */
556 REQ_F_BL_EMPTY = IO_REQ_FLAG(REQ_F_BL_EMPTY_BIT),
557 /* don't recycle provided buffers for this request */
558 REQ_F_BL_NO_RECYCLE = IO_REQ_FLAG(REQ_F_BL_NO_RECYCLE_BIT),
559 /* buffer ring head needs incrementing on put */
560 REQ_F_BUFFERS_COMMIT = IO_REQ_FLAG(REQ_F_BUFFERS_COMMIT_BIT),
561 /* buf node is valid */
562 REQ_F_BUF_NODE = IO_REQ_FLAG(REQ_F_BUF_NODE_BIT),
565 typedef void (*io_req_tw_func_t)(struct io_kiocb *req, struct io_tw_state *ts);
567 struct io_task_work {
568 struct llist_node node;
569 io_req_tw_func_t func;
575 /* fd initially, then cflags for completion */
583 * Each request type overlays its private data structure on top of this one.
584 * They must not exceed this one in size.
588 /* each command gets 56 bytes of data */
592 static inline void io_kiocb_cmd_sz_check(size_t cmd_sz)
594 BUILD_BUG_ON(cmd_sz > sizeof(struct io_cmd_data));
596 #define io_kiocb_to_cmd(req, cmd_type) ( \
597 io_kiocb_cmd_sz_check(sizeof(cmd_type)) , \
598 ((cmd_type *)&(req)->cmd) \
600 #define cmd_to_io_kiocb(ptr) ((struct io_kiocb *) ptr)
605 * NOTE! Each of the io_kiocb union members has the file pointer
606 * as the first entry in their struct definition. So you can
607 * access the file pointer through any of the sub-structs,
608 * or directly as just 'file' in this struct.
611 struct io_cmd_data cmd;
615 /* polled IO has completed */
618 * Can be either a fixed buffer index, or used with provided buffers.
619 * For the latter, before issue it points to the buffer group ID,
620 * and after selection it points to the buffer ID itself.
627 io_req_flags_t flags;
631 struct io_ring_ctx *ctx;
632 struct io_uring_task *tctx;
635 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
636 struct io_buffer *kbuf;
639 * stores buffer ID for ring provided buffers, valid IFF
640 * REQ_F_BUFFER_RING is set.
642 struct io_buffer_list *buf_list;
644 struct io_rsrc_node *buf_node;
648 /* used by request caches, completion batching and iopoll */
649 struct io_wq_work_node comp_list;
650 /* cache ->apoll->events */
651 __poll_t apoll_events;
654 struct io_rsrc_node *file_node;
658 struct io_task_work io_task_work;
661 * for polled requests, i.e. IORING_OP_POLL_ADD and async armed
664 struct hlist_node hash_node;
665 /* For IOPOLL setup queues, with hybrid polling */
668 /* internal polling, see IORING_FEAT_FAST_POLL */
669 struct async_poll *apoll;
670 /* opcode allocated if it needs to store data for async defer */
672 /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
674 struct io_kiocb *link;
675 /* custom credentials, valid IFF REQ_F_CREDS is set */
676 const struct cred *creds;
677 struct io_wq_work work;
685 struct io_overflow_cqe {
686 struct list_head list;
687 struct io_uring_cqe cqe;
690 static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx)
692 return ctx->flags & IORING_SETUP_CQE32;