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