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