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