xref: /linux/include/linux/io_uring_types.h (revision 8e8cd9a4c6dc006e9bd51b8ad0079fb4576b48d3)
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 	struct list_head		defer_list;
523 	unsigned			nr_drained;
524 
525 	/* protected by ->completion_lock */
526 	unsigned			nr_req_allocated;
527 
528 #ifdef CONFIG_NET_RX_BUSY_POLL
529 	struct list_head	napi_list;	/* track busy poll napi_id */
530 	spinlock_t		napi_lock;	/* napi_list lock */
531 
532 	/* napi busy poll default timeout */
533 	ktime_t			napi_busy_poll_dt;
534 	bool			napi_prefer_busy_poll;
535 	u8			napi_track_mode;
536 
537 	DECLARE_HASHTABLE(napi_ht, 4);
538 #endif
539 
540 	struct io_uring_bpf_ops		*bpf_ops;
541 
542 	/*
543 	 * Protection for resize vs mmap races - both the mmap and resize
544 	 * side will need to grab this lock, to prevent either side from
545 	 * being run concurrently with the other.
546 	 */
547 	struct mutex			mmap_lock;
548 
549 	struct io_mapped_region		sq_region;
550 	struct io_mapped_region		ring_region;
551 	/* used for optimised request parameter and wait argument passing  */
552 	struct io_mapped_region		param_region;
553 
554 	struct kcov_common_handle_id	kcov_handle;
555 };
556 
557 /*
558  * Token indicating function is called in task work context:
559  * ctx->uring_lock is held and any completions generated will be flushed.
560  * ONLY core io_uring.c should instantiate this struct.
561  */
562 struct io_tw_state {
563 	bool cancel;
564 };
565 /* Alias to use in code that doesn't instantiate struct io_tw_state */
566 typedef struct io_tw_state io_tw_token_t;
567 
568 enum {
569 	REQ_F_FIXED_FILE_BIT	= IOSQE_FIXED_FILE_BIT,
570 	REQ_F_IO_DRAIN_BIT	= IOSQE_IO_DRAIN_BIT,
571 	REQ_F_LINK_BIT		= IOSQE_IO_LINK_BIT,
572 	REQ_F_HARDLINK_BIT	= IOSQE_IO_HARDLINK_BIT,
573 	REQ_F_FORCE_ASYNC_BIT	= IOSQE_ASYNC_BIT,
574 	REQ_F_BUFFER_SELECT_BIT	= IOSQE_BUFFER_SELECT_BIT,
575 	REQ_F_CQE_SKIP_BIT	= IOSQE_CQE_SKIP_SUCCESS_BIT,
576 
577 	/* first byte is taken by user flags, shift it to not overlap */
578 	REQ_F_FAIL_BIT		= 8,
579 	REQ_F_INFLIGHT_BIT,
580 	REQ_F_CUR_POS_BIT,
581 	REQ_F_NOWAIT_BIT,
582 	REQ_F_LINK_TIMEOUT_BIT,
583 	REQ_F_NEED_CLEANUP_BIT,
584 	REQ_F_POLLED_BIT,
585 	REQ_F_HYBRID_IOPOLL_STATE_BIT,
586 	REQ_F_BUFFER_SELECTED_BIT,
587 	REQ_F_BUFFER_RING_BIT,
588 	REQ_F_REISSUE_BIT,
589 	REQ_F_CREDS_BIT,
590 	REQ_F_REFCOUNT_BIT,
591 	REQ_F_ARM_LTIMEOUT_BIT,
592 	REQ_F_ASYNC_DATA_BIT,
593 	REQ_F_SKIP_LINK_CQES_BIT,
594 	REQ_F_SINGLE_POLL_BIT,
595 	REQ_F_DOUBLE_POLL_BIT,
596 	REQ_F_MULTISHOT_BIT,
597 	REQ_F_APOLL_MULTISHOT_BIT,
598 	REQ_F_CLEAR_POLLIN_BIT,
599 	/* keep async read/write and isreg together and in order */
600 	REQ_F_SUPPORT_NOWAIT_BIT,
601 	REQ_F_ISREG_BIT,
602 	REQ_F_POLL_NO_LAZY_BIT,
603 	REQ_F_CAN_POLL_BIT,
604 	REQ_F_BL_EMPTY_BIT,
605 	REQ_F_BL_NO_RECYCLE_BIT,
606 	REQ_F_BUFFERS_COMMIT_BIT,
607 	REQ_F_BUF_NODE_BIT,
608 	REQ_F_BUF_MORE_BIT,
609 	REQ_F_HAS_METADATA_BIT,
610 	REQ_F_IMPORT_BUFFER_BIT,
611 	REQ_F_SQE_COPIED_BIT,
612 	REQ_F_IOPOLL_BIT,
613 
614 	/* not a real bit, just to check we're not overflowing the space */
615 	__REQ_F_LAST_BIT,
616 };
617 
618 typedef u64 __bitwise io_req_flags_t;
619 #define IO_REQ_FLAG(bitno)	((__force io_req_flags_t) BIT_ULL((bitno)))
620 
621 enum {
622 	/* ctx owns file */
623 	REQ_F_FIXED_FILE	= IO_REQ_FLAG(REQ_F_FIXED_FILE_BIT),
624 	/* drain existing IO first */
625 	REQ_F_IO_DRAIN		= IO_REQ_FLAG(REQ_F_IO_DRAIN_BIT),
626 	/* linked sqes */
627 	REQ_F_LINK		= IO_REQ_FLAG(REQ_F_LINK_BIT),
628 	/* doesn't sever on completion < 0 */
629 	REQ_F_HARDLINK		= IO_REQ_FLAG(REQ_F_HARDLINK_BIT),
630 	/* IOSQE_ASYNC */
631 	REQ_F_FORCE_ASYNC	= IO_REQ_FLAG(REQ_F_FORCE_ASYNC_BIT),
632 	/* IOSQE_BUFFER_SELECT */
633 	REQ_F_BUFFER_SELECT	= IO_REQ_FLAG(REQ_F_BUFFER_SELECT_BIT),
634 	/* IOSQE_CQE_SKIP_SUCCESS */
635 	REQ_F_CQE_SKIP		= IO_REQ_FLAG(REQ_F_CQE_SKIP_BIT),
636 
637 	/* fail rest of links */
638 	REQ_F_FAIL		= IO_REQ_FLAG(REQ_F_FAIL_BIT),
639 	/* on inflight list, should be cancelled and waited on exit reliably */
640 	REQ_F_INFLIGHT		= IO_REQ_FLAG(REQ_F_INFLIGHT_BIT),
641 	/* read/write uses file position */
642 	REQ_F_CUR_POS		= IO_REQ_FLAG(REQ_F_CUR_POS_BIT),
643 	/* must not punt to workers */
644 	REQ_F_NOWAIT		= IO_REQ_FLAG(REQ_F_NOWAIT_BIT),
645 	/* has or had linked timeout */
646 	REQ_F_LINK_TIMEOUT	= IO_REQ_FLAG(REQ_F_LINK_TIMEOUT_BIT),
647 	/* needs cleanup */
648 	REQ_F_NEED_CLEANUP	= IO_REQ_FLAG(REQ_F_NEED_CLEANUP_BIT),
649 	/* already went through poll handler */
650 	REQ_F_POLLED		= IO_REQ_FLAG(REQ_F_POLLED_BIT),
651 	/* every req only blocks once in hybrid poll */
652 	REQ_F_IOPOLL_STATE        = IO_REQ_FLAG(REQ_F_HYBRID_IOPOLL_STATE_BIT),
653 	/* buffer already selected */
654 	REQ_F_BUFFER_SELECTED	= IO_REQ_FLAG(REQ_F_BUFFER_SELECTED_BIT),
655 	/* buffer selected from ring, needs commit */
656 	REQ_F_BUFFER_RING	= IO_REQ_FLAG(REQ_F_BUFFER_RING_BIT),
657 	/* caller should reissue async */
658 	REQ_F_REISSUE		= IO_REQ_FLAG(REQ_F_REISSUE_BIT),
659 	/* supports async reads/writes */
660 	REQ_F_SUPPORT_NOWAIT	= IO_REQ_FLAG(REQ_F_SUPPORT_NOWAIT_BIT),
661 	/* regular file */
662 	REQ_F_ISREG		= IO_REQ_FLAG(REQ_F_ISREG_BIT),
663 	/* has creds assigned */
664 	REQ_F_CREDS		= IO_REQ_FLAG(REQ_F_CREDS_BIT),
665 	/* skip refcounting if not set */
666 	REQ_F_REFCOUNT		= IO_REQ_FLAG(REQ_F_REFCOUNT_BIT),
667 	/* there is a linked timeout that has to be armed */
668 	REQ_F_ARM_LTIMEOUT	= IO_REQ_FLAG(REQ_F_ARM_LTIMEOUT_BIT),
669 	/* ->async_data allocated */
670 	REQ_F_ASYNC_DATA	= IO_REQ_FLAG(REQ_F_ASYNC_DATA_BIT),
671 	/* don't post CQEs while failing linked requests */
672 	REQ_F_SKIP_LINK_CQES	= IO_REQ_FLAG(REQ_F_SKIP_LINK_CQES_BIT),
673 	/* single poll may be active */
674 	REQ_F_SINGLE_POLL	= IO_REQ_FLAG(REQ_F_SINGLE_POLL_BIT),
675 	/* double poll may active */
676 	REQ_F_DOUBLE_POLL	= IO_REQ_FLAG(REQ_F_DOUBLE_POLL_BIT),
677 	/* request posts multiple completions, should be set at prep time */
678 	REQ_F_MULTISHOT		= IO_REQ_FLAG(REQ_F_MULTISHOT_BIT),
679 	/* fast poll multishot mode */
680 	REQ_F_APOLL_MULTISHOT	= IO_REQ_FLAG(REQ_F_APOLL_MULTISHOT_BIT),
681 	/* recvmsg special flag, clear EPOLLIN */
682 	REQ_F_CLEAR_POLLIN	= IO_REQ_FLAG(REQ_F_CLEAR_POLLIN_BIT),
683 	/* don't use lazy poll wake for this request */
684 	REQ_F_POLL_NO_LAZY	= IO_REQ_FLAG(REQ_F_POLL_NO_LAZY_BIT),
685 	/* file is pollable */
686 	REQ_F_CAN_POLL		= IO_REQ_FLAG(REQ_F_CAN_POLL_BIT),
687 	/* buffer list was empty after selection of buffer */
688 	REQ_F_BL_EMPTY		= IO_REQ_FLAG(REQ_F_BL_EMPTY_BIT),
689 	/* don't recycle provided buffers for this request */
690 	REQ_F_BL_NO_RECYCLE	= IO_REQ_FLAG(REQ_F_BL_NO_RECYCLE_BIT),
691 	/* buffer ring head needs incrementing on put */
692 	REQ_F_BUFFERS_COMMIT	= IO_REQ_FLAG(REQ_F_BUFFERS_COMMIT_BIT),
693 	/* buf node is valid */
694 	REQ_F_BUF_NODE		= IO_REQ_FLAG(REQ_F_BUF_NODE_BIT),
695 	/* incremental buffer consumption, more space available */
696 	REQ_F_BUF_MORE		= IO_REQ_FLAG(REQ_F_BUF_MORE_BIT),
697 	/* request has read/write metadata assigned */
698 	REQ_F_HAS_METADATA	= IO_REQ_FLAG(REQ_F_HAS_METADATA_BIT),
699 	/*
700 	 * For vectored fixed buffers, resolve iovec to registered buffers.
701 	 * For SEND_ZC, whether to import buffers (i.e. the first issue).
702 	 */
703 	REQ_F_IMPORT_BUFFER	= IO_REQ_FLAG(REQ_F_IMPORT_BUFFER_BIT),
704 	/* ->sqe_copy() has been called, if necessary */
705 	REQ_F_SQE_COPIED	= IO_REQ_FLAG(REQ_F_SQE_COPIED_BIT),
706 	/* request must be iopolled to completion (set in ->issue()) */
707 	REQ_F_IOPOLL		= IO_REQ_FLAG(REQ_F_IOPOLL_BIT),
708 };
709 
710 struct io_tw_req {
711 	struct io_kiocb *req;
712 };
713 
714 typedef void (*io_req_tw_func_t)(struct io_tw_req tw_req, io_tw_token_t tw);
715 
716 struct io_task_work {
717 	struct llist_node		node;
718 	io_req_tw_func_t		func;
719 };
720 
721 struct io_cqe {
722 	__u64	user_data;
723 	__s32	res;
724 	/* fd initially, then cflags for completion */
725 	union {
726 		__u32	flags;
727 		int	fd;
728 	};
729 };
730 
731 /*
732  * Each request type overlays its private data structure on top of this one.
733  * They must not exceed this one in size.
734  */
735 struct io_cmd_data {
736 	struct file		*file;
737 	/* each command gets 56 bytes of data */
738 	__u8			data[56];
739 };
740 
io_kiocb_cmd_sz_check(size_t cmd_sz)741 static inline void io_kiocb_cmd_sz_check(size_t cmd_sz)
742 {
743 	BUILD_BUG_ON(cmd_sz > sizeof(struct io_cmd_data));
744 }
745 #define io_kiocb_to_cmd(req, cmd_type) ( \
746 	io_kiocb_cmd_sz_check(sizeof(cmd_type)) , \
747 	((cmd_type *)&(req)->cmd) \
748 )
749 
cmd_to_io_kiocb(void * ptr)750 static inline struct io_kiocb *cmd_to_io_kiocb(void *ptr)
751 {
752 	return ptr;
753 }
754 
755 struct io_kiocb {
756 	union {
757 		/*
758 		 * NOTE! Each of the io_kiocb union members has the file pointer
759 		 * as the first entry in their struct definition. So you can
760 		 * access the file pointer through any of the sub-structs,
761 		 * or directly as just 'file' in this struct.
762 		 */
763 		struct file		*file;
764 		struct io_cmd_data	cmd;
765 	};
766 
767 	u8				opcode;
768 	/* polled IO has completed */
769 	u8				iopoll_completed;
770 	/*
771 	 * Can be either a fixed buffer index, or used with provided buffers.
772 	 * For the latter, it points to the selected buffer ID.
773 	 */
774 	u16				buf_index;
775 
776 	/* REQ_F_* flags */
777 	io_req_flags_t			flags;
778 
779 	struct io_cqe			cqe;
780 
781 	struct io_ring_ctx		*ctx;
782 	struct io_uring_task		*tctx;
783 
784 	union {
785 		/* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
786 		struct io_buffer	*kbuf;
787 
788 		struct io_rsrc_node	*buf_node;
789 	};
790 
791 	union {
792 		/* used by request caches, completion batching and iopoll */
793 		struct io_wq_work_node	comp_list;
794 		/* cache ->apoll->events */
795 		__poll_t apoll_events;
796 	};
797 
798 	struct io_rsrc_node		*file_node;
799 
800 	atomic_t			refs;
801 	bool				cancel_seq_set;
802 
803 	union {
804 		struct io_task_work	io_task_work;
805 		/* For IOPOLL setup queues, with hybrid polling */
806 		u64                     iopoll_start;
807 	};
808 
809 	union {
810 		/*
811 		 * for polled requests, i.e. IORING_OP_POLL_ADD and async armed
812 		 * poll
813 		 */
814 		struct hlist_node	hash_node;
815 		/* IOPOLL completion handling */
816 		struct list_head	iopoll_node;
817 		/* for private io_kiocb freeing */
818 		struct rcu_head		rcu_head;
819 	};
820 	/* internal polling, see IORING_FEAT_FAST_POLL */
821 	struct async_poll		*apoll;
822 	/* opcode allocated if it needs to store data for async defer */
823 	void				*async_data;
824 	/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
825 	atomic_t			poll_refs;
826 	struct io_kiocb			*link;
827 	/* custom credentials, valid IFF REQ_F_CREDS is set */
828 	const struct cred		*creds;
829 	struct io_wq_work		work;
830 
831 	struct io_big_cqe {
832 		u64			extra1;
833 		u64			extra2;
834 	} big_cqe;
835 };
836 
837 struct io_overflow_cqe {
838 	struct list_head list;
839 	struct io_uring_cqe cqe;
840 };
841 #endif
842