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