xref: /linux/include/linux/io_uring_types.h (revision ecb6817b769fd7d71af2c9f446188649020b2e53)
1 #ifndef IO_URING_TYPES_H
2 #define IO_URING_TYPES_H
3 
4 #include <linux/blk_plug.h>
5 #include <linux/hashtable.h>
6 #include <linux/task_work.h>
7 #include <linux/bitmap.h>
8 #include <linux/llist.h>
9 #include <linux/uio.h>
10 #include <uapi/linux/io_uring.h>
11 
12 struct iou_loop_params;
13 struct io_uring_bpf_ops;
14 
15 enum {
16 	/*
17 	 * A hint to not wake right away but delay until there are enough of
18 	 * tw's queued to match the number of CQEs the task is waiting for.
19 	 *
20 	 * Must not be used with requests generating more than one CQE.
21 	 * It's also ignored unless IORING_SETUP_DEFER_TASKRUN is set.
22 	 */
23 	IOU_F_TWQ_LAZY_WAKE			= 1,
24 
25 	/*
26 	 * Set when task_work is queued from a waitqueue wakeup handler, where
27 	 * an arbitrary provider waitqueue lock is held. Signaling the CQ ring
28 	 * eventfd inline from there can recurse back into that lock through
29 	 * epoll, so the eventfd signal must be deferred.
30 	 */
31 	IOU_F_TWQ_IN_WAKE			= 2,
32 };
33 
34 enum io_uring_cmd_flags {
35 	IO_URING_F_COMPLETE_DEFER	= 1,
36 	IO_URING_F_UNLOCKED		= 2,
37 	/* the request is executed from poll, it should not be freed */
38 	IO_URING_F_MULTISHOT		= 4,
39 	/* executed by io-wq */
40 	IO_URING_F_IOWQ			= 8,
41 	/* executed inline from syscall */
42 	IO_URING_F_INLINE		= 16,
43 	/* int's last bit, sign checks are usually faster than a bit test */
44 	IO_URING_F_NONBLOCK		= INT_MIN,
45 
46 	/* ctx state flags, for URING_CMD */
47 	IO_URING_F_SQE128		= (1 << 8),
48 	IO_URING_F_CQE32		= (1 << 9),
49 	IO_URING_F_IOPOLL		= (1 << 10),
50 
51 	/* set when uring wants to cancel a previously issued command */
52 	IO_URING_F_CANCEL		= (1 << 11),
53 	IO_URING_F_COMPAT		= (1 << 12),
54 };
55 
56 enum {
57 	IO_BUF_DEST	= 1 << ITER_DEST,
58 	IO_BUF_SOURCE	= 1 << ITER_SOURCE,
59 };
60 
61 struct iou_loop_params;
62 
63 struct io_wq_work_node {
64 	struct io_wq_work_node *next;
65 };
66 
67 struct io_wq_work_list {
68 	struct io_wq_work_node *first;
69 	struct io_wq_work_node *last;
70 };
71 
72 /*
73  * Lockless multi-producer, single-consumer FIFO queue, see
74  * io_uring/mpscq.h for the implementation and rules. Defined here so
75  * that it can be embedded in io_ring_ctx. This is the producer side
76  * only - the consumer cursor is kept separately, on a cacheline that
77  * isn't dirtied by the producers.
78  */
79 struct mpscq {
80 	struct llist_node	*tail;		/* producers */
81 	struct llist_node	stub;
82 };
83 
84 struct io_wq_work {
85 	struct io_wq_work_node list;
86 	atomic_t flags;
87 	/* place it here instead of io_kiocb as it fills padding and saves 4B */
88 	int cancel_seq;
89 };
90 
91 struct io_rsrc_data {
92 	unsigned int			nr;
93 	struct io_rsrc_node		**nodes;
94 };
95 
96 struct io_file_table {
97 	struct io_rsrc_data data;
98 	unsigned long *bitmap;
99 	unsigned int alloc_hint;
100 };
101 
102 struct io_hash_bucket {
103 	struct hlist_head	list;
104 } ____cacheline_aligned_in_smp;
105 
106 struct io_hash_table {
107 	struct io_hash_bucket	*hbs;
108 	unsigned		hash_bits;
109 };
110 
111 struct io_mapped_region {
112 	struct page		**pages;
113 	void			*ptr;
114 	unsigned		nr_pages;
115 	unsigned		flags;
116 };
117 
118 /*
119  * Return value from io_buffer_list selection, to avoid stashing it in
120  * struct io_kiocb. For legacy/classic provided buffers, keeping a reference
121  * across execution contexts are fine. But for ring provided buffers, the
122  * list may go away as soon as ->uring_lock is dropped. As the io_kiocb
123  * persists, it's better to just keep the buffer local for those cases.
124  */
125 struct io_br_sel {
126 	struct io_buffer_list *buf_list;
127 	/*
128 	 * Some selection parts return the user address, others return an error.
129 	 */
130 	union {
131 		void __user *addr;
132 		ssize_t val;
133 	};
134 };
135 
136 
137 /*
138  * Arbitrary limit, can be raised if need be
139  */
140 #define IO_RINGFD_REG_MAX 16
141 
142 struct io_uring_task {
143 	/* submission side */
144 	int				cached_refs;
145 	const struct io_ring_ctx 	*last;
146 	struct task_struct		*task;
147 	struct io_wq			*io_wq;
148 	/*
149 	 * Consumer cursor for ->task_list. Only popped by the task itself,
150 	 * or by ->fallback_work once the task can no longer run task_work.
151 	 */
152 	struct llist_node		*task_head;
153 	struct file			*registered_rings[IO_RINGFD_REG_MAX];
154 
155 	struct xarray			xa;
156 	struct wait_queue_head		wait;
157 	atomic_t			in_cancel;
158 	atomic_t			inflight_tracked;
159 	struct percpu_counter		inflight;
160 
161 	/* drains ->task_list once the task can no longer run task_work */
162 	struct work_struct		fallback_work;
163 
164 	struct { /* task_work */
165 		struct mpscq		task_list;
166 		struct callback_head	task_work;
167 	} ____cacheline_aligned_in_smp;
168 };
169 
170 struct iou_vec {
171 	union {
172 		struct iovec	*iovec;
173 		struct bio_vec	*bvec;
174 	};
175 	unsigned		nr; /* number of struct iovec it can hold */
176 };
177 
178 struct io_uring {
179 	u32 head;
180 	u32 tail;
181 };
182 
183 /*
184  * This data is shared with the application through the mmap at offsets
185  * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
186  *
187  * The offsets to the member fields are published through struct
188  * io_sqring_offsets when calling io_uring_setup.
189  */
190 struct io_rings {
191 	/*
192 	 * Head and tail offsets into the ring; the offsets need to be
193 	 * masked to get valid indices.
194 	 *
195 	 * The kernel controls head of the sq ring and the tail of the cq ring,
196 	 * and the application controls tail of the sq ring and the head of the
197 	 * cq ring.
198 	 */
199 	struct io_uring		sq, cq;
200 	/*
201 	 * Bitmasks to apply to head and tail offsets (constant, equals
202 	 * ring_entries - 1)
203 	 */
204 	u32			sq_ring_mask, cq_ring_mask;
205 	/* Ring sizes (constant, power of 2) */
206 	u32			sq_ring_entries, cq_ring_entries;
207 	/*
208 	 * Number of invalid entries dropped by the kernel due to
209 	 * invalid index stored in array
210 	 *
211 	 * Written by the kernel, shouldn't be modified by the
212 	 * application (i.e. get number of "new events" by comparing to
213 	 * cached value).
214 	 *
215 	 * After a new SQ head value was read by the application this
216 	 * counter includes all submissions that were dropped reaching
217 	 * the new SQ head (and possibly more).
218 	 */
219 	u32			sq_dropped;
220 	/*
221 	 * Runtime SQ flags
222 	 *
223 	 * Written by the kernel, shouldn't be modified by the
224 	 * application.
225 	 *
226 	 * The application needs a full memory barrier before checking
227 	 * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
228 	 */
229 	atomic_t		sq_flags;
230 	/*
231 	 * Runtime CQ flags
232 	 *
233 	 * Written by the application, shouldn't be modified by the
234 	 * kernel.
235 	 */
236 	u32			cq_flags;
237 	/*
238 	 * Number of completion events lost because the queue was full;
239 	 * this should be avoided by the application by making sure
240 	 * there are not more requests pending than there is space in
241 	 * the completion queue.
242 	 *
243 	 * Written by the kernel, shouldn't be modified by the
244 	 * application (i.e. get number of "new events" by comparing to
245 	 * cached value).
246 	 *
247 	 * As completion events come in out of order this counter is not
248 	 * ordered with any other data.
249 	 */
250 	u32			cq_overflow;
251 	/*
252 	 * Ring buffer of completion events.
253 	 *
254 	 * The kernel writes completion events fresh every time they are
255 	 * produced, so the application is allowed to modify pending
256 	 * entries.
257 	 */
258 	struct io_uring_cqe	cqes[] ____cacheline_aligned_in_smp;
259 };
260 
261 struct io_bpf_filter;
262 struct io_bpf_filters {
263 	refcount_t refs;	/* ref for ->bpf_filters */
264 	spinlock_t lock;	/* protects ->bpf_filters modifications */
265 	struct io_bpf_filter __rcu **filters;
266 	struct rcu_head rcu_head;
267 };
268 
269 struct io_restriction {
270 	DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
271 	DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
272 	struct io_bpf_filters *bpf_filters;
273 	/* ->bpf_filters needs COW on modification */
274 	bool bpf_filters_cow;
275 	u8 sqe_flags_allowed;
276 	u8 sqe_flags_required;
277 	/* IORING_OP_* restrictions exist */
278 	bool op_registered;
279 	/* IORING_REGISTER_* restrictions exist */
280 	bool reg_registered;
281 };
282 
283 struct io_submit_link {
284 	struct io_kiocb		*head;
285 	struct io_kiocb		*last;
286 };
287 
288 struct io_submit_state {
289 	/* inline/task_work completion list, under ->uring_lock */
290 	struct io_wq_work_node	free_list;
291 	/* batch completion logic */
292 	struct io_wq_work_list	compl_reqs;
293 	struct io_submit_link	link;
294 
295 	bool			plug_started;
296 	bool			need_plug;
297 	bool			cq_flush;
298 	unsigned short		submit_nr;
299 	struct blk_plug		plug;
300 };
301 
302 struct io_alloc_cache {
303 	void			**entries;
304 	unsigned int		nr_cached;
305 	unsigned int		max_cached;
306 	unsigned int		elem_size;
307 	unsigned int		init_clear;
308 };
309 
310 enum {
311 	IO_RING_F_DRAIN_NEXT		= BIT(0),
312 	IO_RING_F_OP_RESTRICTED		= BIT(1),
313 	IO_RING_F_REG_RESTRICTED	= BIT(2),
314 	IO_RING_F_OFF_TIMEOUT_USED	= BIT(3),
315 	IO_RING_F_DRAIN_ACTIVE		= BIT(4),
316 	IO_RING_F_HAS_EVFD		= BIT(5),
317 	/* all CQEs should be posted only by the submitter task */
318 	IO_RING_F_TASK_COMPLETE		= BIT(6),
319 	IO_RING_F_LOCKLESS_CQ		= BIT(7),
320 	IO_RING_F_SYSCALL_IOPOLL	= BIT(8),
321 	IO_RING_F_POLL_ACTIVATED	= BIT(9),
322 	IO_RING_F_DRAIN_DISABLED	= BIT(10),
323 	IO_RING_F_COMPAT		= BIT(11),
324 	IO_RING_F_IOWQ_LIMITS_SET	= BIT(12),
325 	/* restrictions were inherited from the task that created the ring */
326 	IO_RING_F_RESTRICT_INHERITED	= BIT(13),
327 };
328 
329 struct iou_ctx {};
330 
331 struct io_ring_ctx {
332 	/* const or read-mostly hot data */
333 	struct {
334 		/* ring setup flags */
335 		unsigned int		flags;
336 		/* internal state flags IO_RING_F_* flags , mostly read-only */
337 		unsigned int		int_flags;
338 
339 		struct task_struct	*submitter_task;
340 		struct io_rings		*rings;
341 		/* cache of ->restrictions.bpf_filters->filters */
342 		struct io_bpf_filter __rcu	**bpf_filters;
343 		struct percpu_ref	refs;
344 
345 		clockid_t		clockid;
346 		enum tk_offsets		clock_offset;
347 
348 		enum task_work_notify_mode	notify_method;
349 		unsigned			sq_thread_idle;
350 	} ____cacheline_aligned_in_smp;
351 
352 	/* submission data */
353 	struct {
354 		struct mutex		uring_lock;
355 
356 		/*
357 		 * Ring buffer of indices into array of io_uring_sqe, which is
358 		 * mmapped by the application using the IORING_OFF_SQES offset.
359 		 *
360 		 * This indirection could e.g. be used to assign fixed
361 		 * io_uring_sqe entries to operations and only submit them to
362 		 * the queue when needed.
363 		 *
364 		 * The kernel modifies neither the indices array nor the entries
365 		 * array.
366 		 */
367 		u32			*sq_array;
368 		struct io_uring_sqe	*sq_sqes;
369 		unsigned		cached_sq_head;
370 		unsigned		sq_entries;
371 
372 		/*
373 		 * Fixed resources fast path, should be accessed only under
374 		 * uring_lock, and updated through io_uring_register(2)
375 		 */
376 		atomic_t		cancel_seq;
377 
378 		/*
379 		 * ->iopoll_list is protected by the ctx->uring_lock for
380 		 * io_uring instances that don't use IORING_SETUP_SQPOLL.
381 		 * For SQPOLL, only the single threaded io_sq_thread() will
382 		 * manipulate the list, hence no extra locking is needed there.
383 		 */
384 		bool			poll_multi_queue;
385 		struct list_head	iopoll_list;
386 
387 		/*
388 		 * Consumer cursor for ->work_list, protected by ->uring_lock.
389 		 * Deliberately kept away from the producer side of the queue,
390 		 * as it's written for every popped entry, and the producer
391 		 * cacheline is contended enough as it is.
392 		 */
393 		struct llist_node	*work_head;
394 
395 		struct io_file_table	file_table;
396 		struct io_rsrc_data	buf_table;
397 		struct io_alloc_cache	node_cache;
398 		struct io_alloc_cache	imu_cache;
399 
400 		struct io_submit_state	submit_state;
401 
402 		/*
403 		 * Modifications are protected by ->uring_lock and ->mmap_lock.
404 		 * The buffer list's io mapped region should be stable once
405 		 * published.
406 		 */
407 		struct xarray		io_bl_xa;
408 
409 		struct io_hash_table	cancel_table;
410 		struct io_alloc_cache	apoll_cache;
411 		struct io_alloc_cache	netmsg_cache;
412 		struct io_alloc_cache	rw_cache;
413 		struct io_alloc_cache	cmd_cache;
414 
415 		int (*loop_step)(struct iou_ctx *,
416 				 struct iou_loop_params *);
417 
418 		/*
419 		 * Any cancelable uring_cmd is added to this list in
420 		 * ->uring_cmd() by io_uring_cmd_insert_cancelable()
421 		 */
422 		struct hlist_head	cancelable_uring_cmd;
423 		/*
424 		 * For Hybrid IOPOLL, runtime in hybrid polling, without
425 		 * scheduling time
426 		 */
427 		u64					hybrid_poll_time;
428 	} ____cacheline_aligned_in_smp;
429 
430 	struct {
431 		/*
432 		 * We cache a range of free CQEs we can use, once exhausted it
433 		 * should go through a slower range setup, see __io_get_cqe()
434 		 */
435 		struct io_uring_cqe	*cqe_cached;
436 		struct io_uring_cqe	*cqe_sentinel;
437 
438 		unsigned		cached_cq_tail;
439 		unsigned		cq_entries;
440 		struct io_ev_fd	__rcu	*io_ev_fd;
441 
442 		void			*cq_wait_arg;
443 		size_t			cq_wait_size;
444 	} ____cacheline_aligned_in_smp;
445 
446 	/*
447 	 * task_work and async notification delivery cacheline. Expected to
448 	 * regularly bounce b/w CPUs.
449 	 */
450 	struct {
451 		struct io_rings	__rcu	*rings_rcu;
452 		struct mpscq		work_list;
453 		unsigned long		check_cq;
454 		atomic_t		cq_wait_nr;
455 		atomic_t		cq_timeouts;
456 		struct wait_queue_head	cq_wait;
457 	} ____cacheline_aligned_in_smp;
458 
459 	/* timeouts */
460 	struct {
461 		raw_spinlock_t		timeout_lock;
462 		struct list_head	timeout_list;
463 		struct list_head	ltimeout_list;
464 		unsigned		cq_last_tm_flush;
465 	} ____cacheline_aligned_in_smp;
466 
467 	spinlock_t		completion_lock;
468 
469 	struct list_head	cq_overflow_list;
470 
471 	struct hlist_head	waitid_list;
472 
473 #ifdef CONFIG_FUTEX
474 	struct hlist_head	futex_list;
475 	struct io_alloc_cache	futex_cache;
476 #endif
477 
478 	const struct cred	*sq_creds;	/* cred used for __io_sq_thread() */
479 	struct io_sq_data	*sq_data;	/* if using sq thread polling */
480 
481 	struct wait_queue_head	sqo_sq_wait;
482 	struct list_head	sqd_list;
483 
484 	unsigned int		file_alloc_start;
485 	unsigned int		file_alloc_end;
486 
487 	/* Keep this last, we don't need it for the fast path */
488 	struct wait_queue_head		poll_wq;
489 	struct io_restriction		restrictions;
490 
491 	/* Stores zcrx object pointers of type struct io_zcrx_ifq */
492 	struct xarray			zcrx_ctxs;
493 
494 	/* Used for accounting references on pages in registered buffers */
495 	struct xarray		hpage_acct;
496 
497 	u32			pers_next;
498 	struct xarray		personalities;
499 
500 	/* hashed buffered write serialization */
501 	struct io_wq_hash		*hash_map;
502 
503 	/* Only used for accounting purposes */
504 	struct user_struct		*user;
505 	struct mm_struct		*mm_account;
506 
507 	/*
508 	 * List of tctx nodes for this ctx, protected by tctx_lock. For
509 	 * cancelation purposes, nests under uring_lock.
510 	 */
511 	struct list_head		tctx_list;
512 	struct mutex			tctx_lock;
513 
514 	/* ctx exit and cancelation */
515 	struct work_struct		exit_work;
516 	struct completion		ref_comp;
517 
518 	/* io-wq management, e.g. thread count */
519 	u32				iowq_limits[2];
520 
521 	struct callback_head		poll_wq_task_work;
522 	/* deferred ->poll_wq wake up for local task_work */
523 	struct rcu_head			poll_wq_rcu;
524 	atomic_t			poll_wq_rcu_pending;
525 	struct list_head		defer_list;
526 	unsigned			nr_drained;
527 	/* drain request dispatched from ->defer_list, until it completes */
528 	struct io_kiocb			*drain_req;
529 
530 	/* protected by ->completion_lock */
531 	unsigned			nr_req_allocated;
532 	/* pending SEND_ZC notifications, protected by ->uring_lock */
533 	unsigned			nr_notifs;
534 
535 #ifdef CONFIG_NET_RX_BUSY_POLL
536 	struct list_head	napi_list;	/* track busy poll napi_id */
537 	spinlock_t		napi_lock;	/* napi_list lock */
538 
539 	/* napi busy poll default timeout */
540 	ktime_t			napi_busy_poll_dt;
541 	bool			napi_prefer_busy_poll;
542 	u8			napi_track_mode;
543 
544 	DECLARE_HASHTABLE(napi_ht, 4);
545 #endif
546 
547 	struct io_uring_bpf_ops		*bpf_ops;
548 
549 	/*
550 	 * Protection for resize vs mmap races - both the mmap and resize
551 	 * side will need to grab this lock, to prevent either side from
552 	 * being run concurrently with the other.
553 	 */
554 	struct mutex			mmap_lock;
555 
556 	struct io_mapped_region		sq_region;
557 	struct io_mapped_region		ring_region;
558 	/* used for optimised request parameter and wait argument passing  */
559 	struct io_mapped_region		param_region;
560 
561 	struct kcov_common_handle_id	kcov_handle;
562 };
563 
564 /*
565  * Token indicating function is called in task work context:
566  * ctx->uring_lock is held and any completions generated will be flushed.
567  * ONLY core io_uring.c should instantiate this struct.
568  */
569 struct io_tw_state {
570 	bool cancel;
571 };
572 /* Alias to use in code that doesn't instantiate struct io_tw_state */
573 typedef struct io_tw_state io_tw_token_t;
574 
575 enum {
576 	REQ_F_FIXED_FILE_BIT	= IOSQE_FIXED_FILE_BIT,
577 	REQ_F_IO_DRAIN_BIT	= IOSQE_IO_DRAIN_BIT,
578 	REQ_F_LINK_BIT		= IOSQE_IO_LINK_BIT,
579 	REQ_F_HARDLINK_BIT	= IOSQE_IO_HARDLINK_BIT,
580 	REQ_F_FORCE_ASYNC_BIT	= IOSQE_ASYNC_BIT,
581 	REQ_F_BUFFER_SELECT_BIT	= IOSQE_BUFFER_SELECT_BIT,
582 	REQ_F_CQE_SKIP_BIT	= IOSQE_CQE_SKIP_SUCCESS_BIT,
583 
584 	/* first byte is taken by user flags, shift it to not overlap */
585 	REQ_F_FAIL_BIT		= 8,
586 	REQ_F_INFLIGHT_BIT,
587 	REQ_F_CUR_POS_BIT,
588 	REQ_F_NOWAIT_BIT,
589 	REQ_F_LINK_TIMEOUT_BIT,
590 	REQ_F_NEED_CLEANUP_BIT,
591 	REQ_F_POLLED_BIT,
592 	REQ_F_HYBRID_IOPOLL_STATE_BIT,
593 	REQ_F_BUFFER_SELECTED_BIT,
594 	REQ_F_BUFFER_RING_BIT,
595 	REQ_F_REISSUE_BIT,
596 	REQ_F_CREDS_BIT,
597 	REQ_F_REFCOUNT_BIT,
598 	REQ_F_ARM_LTIMEOUT_BIT,
599 	REQ_F_ASYNC_DATA_BIT,
600 	REQ_F_SKIP_LINK_CQES_BIT,
601 	REQ_F_SINGLE_POLL_BIT,
602 	REQ_F_DOUBLE_POLL_BIT,
603 	REQ_F_MULTISHOT_BIT,
604 	REQ_F_APOLL_MULTISHOT_BIT,
605 	REQ_F_CLEAR_POLLIN_BIT,
606 	/* keep async read/write and isreg together and in order */
607 	REQ_F_SUPPORT_NOWAIT_BIT,
608 	REQ_F_ISREG_BIT,
609 	REQ_F_POLL_NO_LAZY_BIT,
610 	REQ_F_CAN_POLL_BIT,
611 	REQ_F_BL_EMPTY_BIT,
612 	REQ_F_BL_NO_RECYCLE_BIT,
613 	REQ_F_BUFFERS_COMMIT_BIT,
614 	REQ_F_BUF_NODE_BIT,
615 	REQ_F_BUF_MORE_BIT,
616 	REQ_F_HAS_METADATA_BIT,
617 	REQ_F_IMPORT_BUFFER_BIT,
618 	REQ_F_SQE_COPIED_BIT,
619 	REQ_F_IOPOLL_BIT,
620 
621 	/* not a real bit, just to check we're not overflowing the space */
622 	__REQ_F_LAST_BIT,
623 };
624 
625 typedef u64 __bitwise io_req_flags_t;
626 #define IO_REQ_FLAG(bitno)	((__force io_req_flags_t) BIT_ULL((bitno)))
627 
628 enum {
629 	/* ctx owns file */
630 	REQ_F_FIXED_FILE	= IO_REQ_FLAG(REQ_F_FIXED_FILE_BIT),
631 	/* drain existing IO first */
632 	REQ_F_IO_DRAIN		= IO_REQ_FLAG(REQ_F_IO_DRAIN_BIT),
633 	/* linked sqes */
634 	REQ_F_LINK		= IO_REQ_FLAG(REQ_F_LINK_BIT),
635 	/* doesn't sever on completion < 0 */
636 	REQ_F_HARDLINK		= IO_REQ_FLAG(REQ_F_HARDLINK_BIT),
637 	/* IOSQE_ASYNC */
638 	REQ_F_FORCE_ASYNC	= IO_REQ_FLAG(REQ_F_FORCE_ASYNC_BIT),
639 	/* IOSQE_BUFFER_SELECT */
640 	REQ_F_BUFFER_SELECT	= IO_REQ_FLAG(REQ_F_BUFFER_SELECT_BIT),
641 	/* IOSQE_CQE_SKIP_SUCCESS */
642 	REQ_F_CQE_SKIP		= IO_REQ_FLAG(REQ_F_CQE_SKIP_BIT),
643 
644 	/* fail rest of links */
645 	REQ_F_FAIL		= IO_REQ_FLAG(REQ_F_FAIL_BIT),
646 	/* on inflight list, should be cancelled and waited on exit reliably */
647 	REQ_F_INFLIGHT		= IO_REQ_FLAG(REQ_F_INFLIGHT_BIT),
648 	/* read/write uses file position */
649 	REQ_F_CUR_POS		= IO_REQ_FLAG(REQ_F_CUR_POS_BIT),
650 	/* must not punt to workers */
651 	REQ_F_NOWAIT		= IO_REQ_FLAG(REQ_F_NOWAIT_BIT),
652 	/* has or had linked timeout */
653 	REQ_F_LINK_TIMEOUT	= IO_REQ_FLAG(REQ_F_LINK_TIMEOUT_BIT),
654 	/* needs cleanup */
655 	REQ_F_NEED_CLEANUP	= IO_REQ_FLAG(REQ_F_NEED_CLEANUP_BIT),
656 	/* already went through poll handler */
657 	REQ_F_POLLED		= IO_REQ_FLAG(REQ_F_POLLED_BIT),
658 	/* every req only blocks once in hybrid poll */
659 	REQ_F_IOPOLL_STATE        = IO_REQ_FLAG(REQ_F_HYBRID_IOPOLL_STATE_BIT),
660 	/* buffer already selected */
661 	REQ_F_BUFFER_SELECTED	= IO_REQ_FLAG(REQ_F_BUFFER_SELECTED_BIT),
662 	/* buffer selected from ring, needs commit */
663 	REQ_F_BUFFER_RING	= IO_REQ_FLAG(REQ_F_BUFFER_RING_BIT),
664 	/* caller should reissue async */
665 	REQ_F_REISSUE		= IO_REQ_FLAG(REQ_F_REISSUE_BIT),
666 	/* supports async reads/writes */
667 	REQ_F_SUPPORT_NOWAIT	= IO_REQ_FLAG(REQ_F_SUPPORT_NOWAIT_BIT),
668 	/* regular file */
669 	REQ_F_ISREG		= IO_REQ_FLAG(REQ_F_ISREG_BIT),
670 	/* has creds assigned */
671 	REQ_F_CREDS		= IO_REQ_FLAG(REQ_F_CREDS_BIT),
672 	/* skip refcounting if not set */
673 	REQ_F_REFCOUNT		= IO_REQ_FLAG(REQ_F_REFCOUNT_BIT),
674 	/* there is a linked timeout that has to be armed */
675 	REQ_F_ARM_LTIMEOUT	= IO_REQ_FLAG(REQ_F_ARM_LTIMEOUT_BIT),
676 	/* ->async_data allocated */
677 	REQ_F_ASYNC_DATA	= IO_REQ_FLAG(REQ_F_ASYNC_DATA_BIT),
678 	/* don't post CQEs while failing linked requests */
679 	REQ_F_SKIP_LINK_CQES	= IO_REQ_FLAG(REQ_F_SKIP_LINK_CQES_BIT),
680 	/* single poll may be active */
681 	REQ_F_SINGLE_POLL	= IO_REQ_FLAG(REQ_F_SINGLE_POLL_BIT),
682 	/* double poll may active */
683 	REQ_F_DOUBLE_POLL	= IO_REQ_FLAG(REQ_F_DOUBLE_POLL_BIT),
684 	/* request posts multiple completions, should be set at prep time */
685 	REQ_F_MULTISHOT		= IO_REQ_FLAG(REQ_F_MULTISHOT_BIT),
686 	/* fast poll multishot mode */
687 	REQ_F_APOLL_MULTISHOT	= IO_REQ_FLAG(REQ_F_APOLL_MULTISHOT_BIT),
688 	/* recvmsg special flag, clear EPOLLIN */
689 	REQ_F_CLEAR_POLLIN	= IO_REQ_FLAG(REQ_F_CLEAR_POLLIN_BIT),
690 	/* don't use lazy poll wake for this request */
691 	REQ_F_POLL_NO_LAZY	= IO_REQ_FLAG(REQ_F_POLL_NO_LAZY_BIT),
692 	/* file is pollable */
693 	REQ_F_CAN_POLL		= IO_REQ_FLAG(REQ_F_CAN_POLL_BIT),
694 	/* buffer list was empty after selection of buffer */
695 	REQ_F_BL_EMPTY		= IO_REQ_FLAG(REQ_F_BL_EMPTY_BIT),
696 	/* don't recycle provided buffers for this request */
697 	REQ_F_BL_NO_RECYCLE	= IO_REQ_FLAG(REQ_F_BL_NO_RECYCLE_BIT),
698 	/* buffer ring head needs incrementing on put */
699 	REQ_F_BUFFERS_COMMIT	= IO_REQ_FLAG(REQ_F_BUFFERS_COMMIT_BIT),
700 	/* buf node is valid */
701 	REQ_F_BUF_NODE		= IO_REQ_FLAG(REQ_F_BUF_NODE_BIT),
702 	/* incremental buffer consumption, more space available */
703 	REQ_F_BUF_MORE		= IO_REQ_FLAG(REQ_F_BUF_MORE_BIT),
704 	/* request has read/write metadata assigned */
705 	REQ_F_HAS_METADATA	= IO_REQ_FLAG(REQ_F_HAS_METADATA_BIT),
706 	/*
707 	 * For vectored fixed buffers, resolve iovec to registered buffers.
708 	 * For SEND_ZC, whether to import buffers (i.e. the first issue).
709 	 */
710 	REQ_F_IMPORT_BUFFER	= IO_REQ_FLAG(REQ_F_IMPORT_BUFFER_BIT),
711 	/* ->sqe_copy() has been called, if necessary */
712 	REQ_F_SQE_COPIED	= IO_REQ_FLAG(REQ_F_SQE_COPIED_BIT),
713 	/* request must be iopolled to completion (set in ->issue()) */
714 	REQ_F_IOPOLL		= IO_REQ_FLAG(REQ_F_IOPOLL_BIT),
715 };
716 
717 struct io_tw_req {
718 	struct io_kiocb *req;
719 };
720 
721 typedef void (*io_req_tw_func_t)(struct io_tw_req tw_req, io_tw_token_t tw);
722 
723 struct io_task_work {
724 	struct llist_node		node;
725 	io_req_tw_func_t		func;
726 };
727 
728 struct io_cqe {
729 	__u64	user_data;
730 	__s32	res;
731 	/* fd initially, then cflags for completion */
732 	union {
733 		__u32	flags;
734 		int	fd;
735 	};
736 };
737 
738 /*
739  * Each request type overlays its private data structure on top of this one.
740  * They must not exceed this one in size.
741  */
742 struct io_cmd_data {
743 	struct file		*file;
744 	/* each command gets 56 bytes of data */
745 	__u8			data[56];
746 };
747 
748 static inline void io_kiocb_cmd_sz_check(size_t cmd_sz)
749 {
750 	BUILD_BUG_ON(cmd_sz > sizeof(struct io_cmd_data));
751 }
752 #define io_kiocb_to_cmd(req, cmd_type) ( \
753 	io_kiocb_cmd_sz_check(sizeof(cmd_type)) , \
754 	((cmd_type *)&(req)->cmd) \
755 )
756 
757 static inline struct io_kiocb *cmd_to_io_kiocb(void *ptr)
758 {
759 	return ptr;
760 }
761 
762 struct io_kiocb {
763 	union {
764 		/*
765 		 * NOTE! Each of the io_kiocb union members has the file pointer
766 		 * as the first entry in their struct definition. So you can
767 		 * access the file pointer through any of the sub-structs,
768 		 * or directly as just 'file' in this struct.
769 		 */
770 		struct file		*file;
771 		struct io_cmd_data	cmd;
772 	};
773 
774 	u8				opcode;
775 	/* polled IO has completed */
776 	u8				iopoll_completed;
777 	/*
778 	 * Can be either a fixed buffer index, or used with provided buffers.
779 	 * For the latter, it points to the selected buffer ID.
780 	 */
781 	u16				buf_index;
782 
783 	/* REQ_F_* flags */
784 	io_req_flags_t			flags;
785 
786 	struct io_cqe			cqe;
787 
788 	struct io_ring_ctx		*ctx;
789 	struct io_uring_task		*tctx;
790 
791 	union {
792 		/* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
793 		struct io_buffer	*kbuf;
794 
795 		struct io_rsrc_node	*buf_node;
796 	};
797 
798 	union {
799 		/* used by request caches, completion batching and iopoll */
800 		struct io_wq_work_node	comp_list;
801 		/* cache ->apoll->events */
802 		__poll_t apoll_events;
803 	};
804 
805 	struct io_rsrc_node		*file_node;
806 
807 	atomic_t			refs;
808 	bool				cancel_seq_set;
809 
810 	union {
811 		struct io_task_work	io_task_work;
812 		/* For IOPOLL setup queues, with hybrid polling */
813 		u64                     iopoll_start;
814 	};
815 
816 	union {
817 		/*
818 		 * for polled requests, i.e. IORING_OP_POLL_ADD and async armed
819 		 * poll
820 		 */
821 		struct hlist_node	hash_node;
822 		/* IOPOLL completion handling */
823 		struct list_head	iopoll_node;
824 		/* for private io_kiocb freeing */
825 		struct rcu_head		rcu_head;
826 	};
827 	/* internal polling, see IORING_FEAT_FAST_POLL */
828 	struct async_poll		*apoll;
829 	/* opcode allocated if it needs to store data for async defer */
830 	void				*async_data;
831 	/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
832 	atomic_t			poll_refs;
833 	struct io_kiocb			*link;
834 	/* custom credentials, valid IFF REQ_F_CREDS is set */
835 	const struct cred		*creds;
836 	struct io_wq_work		work;
837 
838 	struct io_big_cqe {
839 		u64			extra1;
840 		u64			extra2;
841 	} big_cqe;
842 };
843 
844 struct io_overflow_cqe {
845 	struct list_head list;
846 	struct io_uring_cqe cqe;
847 };
848 #endif
849