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