Merge tag 'vfio-v6.0-rc1' of https://github.com/awilliam/linux-vfio
[linux-2.6-microblaze.git] / include / linux / io_uring_types.h
1 #ifndef IO_URING_TYPES_H
2 #define IO_URING_TYPES_H
3
4 #include <linux/blkdev.h>
5 #include <linux/task_work.h>
6 #include <linux/bitmap.h>
7 #include <linux/llist.h>
8 #include <uapi/linux/io_uring.h>
9
10 struct io_wq_work_node {
11         struct io_wq_work_node *next;
12 };
13
14 struct io_wq_work_list {
15         struct io_wq_work_node *first;
16         struct io_wq_work_node *last;
17 };
18
19 struct io_wq_work {
20         struct io_wq_work_node list;
21         unsigned flags;
22         /* place it here instead of io_kiocb as it fills padding and saves 4B */
23         int cancel_seq;
24 };
25
26 struct io_fixed_file {
27         /* file * with additional FFS_* flags */
28         unsigned long file_ptr;
29 };
30
31 struct io_file_table {
32         struct io_fixed_file *files;
33         unsigned long *bitmap;
34         unsigned int alloc_hint;
35 };
36
37 struct io_notif;
38 struct io_notif_slot;
39
40 struct io_hash_bucket {
41         spinlock_t              lock;
42         struct hlist_head       list;
43 } ____cacheline_aligned_in_smp;
44
45 struct io_hash_table {
46         struct io_hash_bucket   *hbs;
47         unsigned                hash_bits;
48 };
49
50 /*
51  * Arbitrary limit, can be raised if need be
52  */
53 #define IO_RINGFD_REG_MAX 16
54
55 struct io_uring_task {
56         /* submission side */
57         int                             cached_refs;
58         const struct io_ring_ctx        *last;
59         struct io_wq                    *io_wq;
60         struct file                     *registered_rings[IO_RINGFD_REG_MAX];
61
62         struct xarray                   xa;
63         struct wait_queue_head          wait;
64         atomic_t                        in_idle;
65         atomic_t                        inflight_tracked;
66         struct percpu_counter           inflight;
67
68         struct { /* task_work */
69                 struct llist_head       task_list;
70                 struct callback_head    task_work;
71         } ____cacheline_aligned_in_smp;
72 };
73
74 struct io_uring {
75         u32 head ____cacheline_aligned_in_smp;
76         u32 tail ____cacheline_aligned_in_smp;
77 };
78
79 /*
80  * This data is shared with the application through the mmap at offsets
81  * IORING_OFF_SQ_RING and IORING_OFF_CQ_RING.
82  *
83  * The offsets to the member fields are published through struct
84  * io_sqring_offsets when calling io_uring_setup.
85  */
86 struct io_rings {
87         /*
88          * Head and tail offsets into the ring; the offsets need to be
89          * masked to get valid indices.
90          *
91          * The kernel controls head of the sq ring and the tail of the cq ring,
92          * and the application controls tail of the sq ring and the head of the
93          * cq ring.
94          */
95         struct io_uring         sq, cq;
96         /*
97          * Bitmasks to apply to head and tail offsets (constant, equals
98          * ring_entries - 1)
99          */
100         u32                     sq_ring_mask, cq_ring_mask;
101         /* Ring sizes (constant, power of 2) */
102         u32                     sq_ring_entries, cq_ring_entries;
103         /*
104          * Number of invalid entries dropped by the kernel due to
105          * invalid index stored in array
106          *
107          * Written by the kernel, shouldn't be modified by the
108          * application (i.e. get number of "new events" by comparing to
109          * cached value).
110          *
111          * After a new SQ head value was read by the application this
112          * counter includes all submissions that were dropped reaching
113          * the new SQ head (and possibly more).
114          */
115         u32                     sq_dropped;
116         /*
117          * Runtime SQ flags
118          *
119          * Written by the kernel, shouldn't be modified by the
120          * application.
121          *
122          * The application needs a full memory barrier before checking
123          * for IORING_SQ_NEED_WAKEUP after updating the sq tail.
124          */
125         atomic_t                sq_flags;
126         /*
127          * Runtime CQ flags
128          *
129          * Written by the application, shouldn't be modified by the
130          * kernel.
131          */
132         u32                     cq_flags;
133         /*
134          * Number of completion events lost because the queue was full;
135          * this should be avoided by the application by making sure
136          * there are not more requests pending than there is space in
137          * the completion queue.
138          *
139          * Written by the kernel, shouldn't be modified by the
140          * application (i.e. get number of "new events" by comparing to
141          * cached value).
142          *
143          * As completion events come in out of order this counter is not
144          * ordered with any other data.
145          */
146         u32                     cq_overflow;
147         /*
148          * Ring buffer of completion events.
149          *
150          * The kernel writes completion events fresh every time they are
151          * produced, so the application is allowed to modify pending
152          * entries.
153          */
154         struct io_uring_cqe     cqes[] ____cacheline_aligned_in_smp;
155 };
156
157 struct io_restriction {
158         DECLARE_BITMAP(register_op, IORING_REGISTER_LAST);
159         DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
160         u8 sqe_flags_allowed;
161         u8 sqe_flags_required;
162         bool registered;
163 };
164
165 struct io_submit_link {
166         struct io_kiocb         *head;
167         struct io_kiocb         *last;
168 };
169
170 struct io_submit_state {
171         /* inline/task_work completion list, under ->uring_lock */
172         struct io_wq_work_node  free_list;
173         /* batch completion logic */
174         struct io_wq_work_list  compl_reqs;
175         struct io_submit_link   link;
176
177         bool                    plug_started;
178         bool                    need_plug;
179         unsigned short          submit_nr;
180         struct blk_plug         plug;
181 };
182
183 struct io_ev_fd {
184         struct eventfd_ctx      *cq_ev_fd;
185         unsigned int            eventfd_async: 1;
186         struct rcu_head         rcu;
187 };
188
189 struct io_alloc_cache {
190         struct hlist_head       list;
191         unsigned int            nr_cached;
192 };
193
194 struct io_ring_ctx {
195         /* const or read-mostly hot data */
196         struct {
197                 struct percpu_ref       refs;
198
199                 struct io_rings         *rings;
200                 unsigned int            flags;
201                 enum task_work_notify_mode      notify_method;
202                 unsigned int            compat: 1;
203                 unsigned int            drain_next: 1;
204                 unsigned int            restricted: 1;
205                 unsigned int            off_timeout_used: 1;
206                 unsigned int            drain_active: 1;
207                 unsigned int            drain_disabled: 1;
208                 unsigned int            has_evfd: 1;
209                 unsigned int            syscall_iopoll: 1;
210         } ____cacheline_aligned_in_smp;
211
212         /* submission data */
213         struct {
214                 struct mutex            uring_lock;
215
216                 /*
217                  * Ring buffer of indices into array of io_uring_sqe, which is
218                  * mmapped by the application using the IORING_OFF_SQES offset.
219                  *
220                  * This indirection could e.g. be used to assign fixed
221                  * io_uring_sqe entries to operations and only submit them to
222                  * the queue when needed.
223                  *
224                  * The kernel modifies neither the indices array nor the entries
225                  * array.
226                  */
227                 u32                     *sq_array;
228                 struct io_uring_sqe     *sq_sqes;
229                 unsigned                cached_sq_head;
230                 unsigned                sq_entries;
231
232                 /*
233                  * Fixed resources fast path, should be accessed only under
234                  * uring_lock, and updated through io_uring_register(2)
235                  */
236                 struct io_rsrc_node     *rsrc_node;
237                 int                     rsrc_cached_refs;
238                 atomic_t                cancel_seq;
239                 struct io_file_table    file_table;
240                 unsigned                nr_user_files;
241                 unsigned                nr_user_bufs;
242                 struct io_mapped_ubuf   **user_bufs;
243                 struct io_notif_slot    *notif_slots;
244                 unsigned                nr_notif_slots;
245
246                 struct io_submit_state  submit_state;
247
248                 struct io_buffer_list   *io_bl;
249                 struct xarray           io_bl_xa;
250                 struct list_head        io_buffers_cache;
251
252                 struct io_hash_table    cancel_table_locked;
253                 struct list_head        cq_overflow_list;
254                 struct io_alloc_cache   apoll_cache;
255                 struct io_alloc_cache   netmsg_cache;
256         } ____cacheline_aligned_in_smp;
257
258         /* IRQ completion list, under ->completion_lock */
259         struct io_wq_work_list  locked_free_list;
260         unsigned int            locked_free_nr;
261
262         const struct cred       *sq_creds;      /* cred used for __io_sq_thread() */
263         struct io_sq_data       *sq_data;       /* if using sq thread polling */
264
265         struct wait_queue_head  sqo_sq_wait;
266         struct list_head        sqd_list;
267
268         unsigned long           check_cq;
269
270         unsigned int            file_alloc_start;
271         unsigned int            file_alloc_end;
272
273         struct xarray           personalities;
274         u32                     pers_next;
275
276         struct {
277                 /*
278                  * We cache a range of free CQEs we can use, once exhausted it
279                  * should go through a slower range setup, see __io_get_cqe()
280                  */
281                 struct io_uring_cqe     *cqe_cached;
282                 struct io_uring_cqe     *cqe_sentinel;
283
284                 unsigned                cached_cq_tail;
285                 unsigned                cq_entries;
286                 struct io_ev_fd __rcu   *io_ev_fd;
287                 struct wait_queue_head  cq_wait;
288                 unsigned                cq_extra;
289         } ____cacheline_aligned_in_smp;
290
291         struct {
292                 spinlock_t              completion_lock;
293
294                 /*
295                  * ->iopoll_list is protected by the ctx->uring_lock for
296                  * io_uring instances that don't use IORING_SETUP_SQPOLL.
297                  * For SQPOLL, only the single threaded io_sq_thread() will
298                  * manipulate the list, hence no extra locking is needed there.
299                  */
300                 struct io_wq_work_list  iopoll_list;
301                 struct io_hash_table    cancel_table;
302                 bool                    poll_multi_queue;
303
304                 struct list_head        io_buffers_comp;
305         } ____cacheline_aligned_in_smp;
306
307         /* timeouts */
308         struct {
309                 spinlock_t              timeout_lock;
310                 atomic_t                cq_timeouts;
311                 struct list_head        timeout_list;
312                 struct list_head        ltimeout_list;
313                 unsigned                cq_last_tm_flush;
314         } ____cacheline_aligned_in_smp;
315
316         /* Keep this last, we don't need it for the fast path */
317
318         struct io_restriction           restrictions;
319         struct task_struct              *submitter_task;
320
321         /* slow path rsrc auxilary data, used by update/register */
322         struct io_rsrc_node             *rsrc_backup_node;
323         struct io_mapped_ubuf           *dummy_ubuf;
324         struct io_rsrc_data             *file_data;
325         struct io_rsrc_data             *buf_data;
326
327         struct delayed_work             rsrc_put_work;
328         struct llist_head               rsrc_put_llist;
329         struct list_head                rsrc_ref_list;
330         spinlock_t                      rsrc_ref_lock;
331
332         struct list_head                io_buffers_pages;
333
334         #if defined(CONFIG_UNIX)
335                 struct socket           *ring_sock;
336         #endif
337         /* hashed buffered write serialization */
338         struct io_wq_hash               *hash_map;
339
340         /* Only used for accounting purposes */
341         struct user_struct              *user;
342         struct mm_struct                *mm_account;
343
344         /* ctx exit and cancelation */
345         struct llist_head               fallback_llist;
346         struct delayed_work             fallback_work;
347         struct work_struct              exit_work;
348         struct list_head                tctx_list;
349         struct completion               ref_comp;
350
351         /* io-wq management, e.g. thread count */
352         u32                             iowq_limits[2];
353         bool                            iowq_limits_set;
354
355         struct list_head                defer_list;
356         unsigned                        sq_thread_idle;
357         /* protected by ->completion_lock */
358         unsigned                        evfd_last_cq_tail;
359 };
360
361 enum {
362         REQ_F_FIXED_FILE_BIT    = IOSQE_FIXED_FILE_BIT,
363         REQ_F_IO_DRAIN_BIT      = IOSQE_IO_DRAIN_BIT,
364         REQ_F_LINK_BIT          = IOSQE_IO_LINK_BIT,
365         REQ_F_HARDLINK_BIT      = IOSQE_IO_HARDLINK_BIT,
366         REQ_F_FORCE_ASYNC_BIT   = IOSQE_ASYNC_BIT,
367         REQ_F_BUFFER_SELECT_BIT = IOSQE_BUFFER_SELECT_BIT,
368         REQ_F_CQE_SKIP_BIT      = IOSQE_CQE_SKIP_SUCCESS_BIT,
369
370         /* first byte is taken by user flags, shift it to not overlap */
371         REQ_F_FAIL_BIT          = 8,
372         REQ_F_INFLIGHT_BIT,
373         REQ_F_CUR_POS_BIT,
374         REQ_F_NOWAIT_BIT,
375         REQ_F_LINK_TIMEOUT_BIT,
376         REQ_F_NEED_CLEANUP_BIT,
377         REQ_F_POLLED_BIT,
378         REQ_F_BUFFER_SELECTED_BIT,
379         REQ_F_BUFFER_RING_BIT,
380         REQ_F_REISSUE_BIT,
381         REQ_F_CREDS_BIT,
382         REQ_F_REFCOUNT_BIT,
383         REQ_F_ARM_LTIMEOUT_BIT,
384         REQ_F_ASYNC_DATA_BIT,
385         REQ_F_SKIP_LINK_CQES_BIT,
386         REQ_F_SINGLE_POLL_BIT,
387         REQ_F_DOUBLE_POLL_BIT,
388         REQ_F_PARTIAL_IO_BIT,
389         REQ_F_CQE32_INIT_BIT,
390         REQ_F_APOLL_MULTISHOT_BIT,
391         REQ_F_CLEAR_POLLIN_BIT,
392         REQ_F_HASH_LOCKED_BIT,
393         /* keep async read/write and isreg together and in order */
394         REQ_F_SUPPORT_NOWAIT_BIT,
395         REQ_F_ISREG_BIT,
396
397         /* not a real bit, just to check we're not overflowing the space */
398         __REQ_F_LAST_BIT,
399 };
400
401 enum {
402         /* ctx owns file */
403         REQ_F_FIXED_FILE        = BIT(REQ_F_FIXED_FILE_BIT),
404         /* drain existing IO first */
405         REQ_F_IO_DRAIN          = BIT(REQ_F_IO_DRAIN_BIT),
406         /* linked sqes */
407         REQ_F_LINK              = BIT(REQ_F_LINK_BIT),
408         /* doesn't sever on completion < 0 */
409         REQ_F_HARDLINK          = BIT(REQ_F_HARDLINK_BIT),
410         /* IOSQE_ASYNC */
411         REQ_F_FORCE_ASYNC       = BIT(REQ_F_FORCE_ASYNC_BIT),
412         /* IOSQE_BUFFER_SELECT */
413         REQ_F_BUFFER_SELECT     = BIT(REQ_F_BUFFER_SELECT_BIT),
414         /* IOSQE_CQE_SKIP_SUCCESS */
415         REQ_F_CQE_SKIP          = BIT(REQ_F_CQE_SKIP_BIT),
416
417         /* fail rest of links */
418         REQ_F_FAIL              = BIT(REQ_F_FAIL_BIT),
419         /* on inflight list, should be cancelled and waited on exit reliably */
420         REQ_F_INFLIGHT          = BIT(REQ_F_INFLIGHT_BIT),
421         /* read/write uses file position */
422         REQ_F_CUR_POS           = BIT(REQ_F_CUR_POS_BIT),
423         /* must not punt to workers */
424         REQ_F_NOWAIT            = BIT(REQ_F_NOWAIT_BIT),
425         /* has or had linked timeout */
426         REQ_F_LINK_TIMEOUT      = BIT(REQ_F_LINK_TIMEOUT_BIT),
427         /* needs cleanup */
428         REQ_F_NEED_CLEANUP      = BIT(REQ_F_NEED_CLEANUP_BIT),
429         /* already went through poll handler */
430         REQ_F_POLLED            = BIT(REQ_F_POLLED_BIT),
431         /* buffer already selected */
432         REQ_F_BUFFER_SELECTED   = BIT(REQ_F_BUFFER_SELECTED_BIT),
433         /* buffer selected from ring, needs commit */
434         REQ_F_BUFFER_RING       = BIT(REQ_F_BUFFER_RING_BIT),
435         /* caller should reissue async */
436         REQ_F_REISSUE           = BIT(REQ_F_REISSUE_BIT),
437         /* supports async reads/writes */
438         REQ_F_SUPPORT_NOWAIT    = BIT(REQ_F_SUPPORT_NOWAIT_BIT),
439         /* regular file */
440         REQ_F_ISREG             = BIT(REQ_F_ISREG_BIT),
441         /* has creds assigned */
442         REQ_F_CREDS             = BIT(REQ_F_CREDS_BIT),
443         /* skip refcounting if not set */
444         REQ_F_REFCOUNT          = BIT(REQ_F_REFCOUNT_BIT),
445         /* there is a linked timeout that has to be armed */
446         REQ_F_ARM_LTIMEOUT      = BIT(REQ_F_ARM_LTIMEOUT_BIT),
447         /* ->async_data allocated */
448         REQ_F_ASYNC_DATA        = BIT(REQ_F_ASYNC_DATA_BIT),
449         /* don't post CQEs while failing linked requests */
450         REQ_F_SKIP_LINK_CQES    = BIT(REQ_F_SKIP_LINK_CQES_BIT),
451         /* single poll may be active */
452         REQ_F_SINGLE_POLL       = BIT(REQ_F_SINGLE_POLL_BIT),
453         /* double poll may active */
454         REQ_F_DOUBLE_POLL       = BIT(REQ_F_DOUBLE_POLL_BIT),
455         /* request has already done partial IO */
456         REQ_F_PARTIAL_IO        = BIT(REQ_F_PARTIAL_IO_BIT),
457         /* fast poll multishot mode */
458         REQ_F_APOLL_MULTISHOT   = BIT(REQ_F_APOLL_MULTISHOT_BIT),
459         /* ->extra1 and ->extra2 are initialised */
460         REQ_F_CQE32_INIT        = BIT(REQ_F_CQE32_INIT_BIT),
461         /* recvmsg special flag, clear EPOLLIN */
462         REQ_F_CLEAR_POLLIN      = BIT(REQ_F_CLEAR_POLLIN_BIT),
463         /* hashed into ->cancel_hash_locked, protected by ->uring_lock */
464         REQ_F_HASH_LOCKED       = BIT(REQ_F_HASH_LOCKED_BIT),
465 };
466
467 typedef void (*io_req_tw_func_t)(struct io_kiocb *req, bool *locked);
468
469 struct io_task_work {
470         struct llist_node               node;
471         io_req_tw_func_t                func;
472 };
473
474 struct io_cqe {
475         __u64   user_data;
476         __s32   res;
477         /* fd initially, then cflags for completion */
478         union {
479                 __u32   flags;
480                 int     fd;
481         };
482 };
483
484 /*
485  * Each request type overlays its private data structure on top of this one.
486  * They must not exceed this one in size.
487  */
488 struct io_cmd_data {
489         struct file             *file;
490         /* each command gets 56 bytes of data */
491         __u8                    data[56];
492 };
493
494 #define io_kiocb_to_cmd(req)    ((void *) &(req)->cmd)
495 #define cmd_to_io_kiocb(ptr)    ((struct io_kiocb *) ptr)
496
497 struct io_kiocb {
498         union {
499                 /*
500                  * NOTE! Each of the io_kiocb union members has the file pointer
501                  * as the first entry in their struct definition. So you can
502                  * access the file pointer through any of the sub-structs,
503                  * or directly as just 'file' in this struct.
504                  */
505                 struct file             *file;
506                 struct io_cmd_data      cmd;
507         };
508
509         u8                              opcode;
510         /* polled IO has completed */
511         u8                              iopoll_completed;
512         /*
513          * Can be either a fixed buffer index, or used with provided buffers.
514          * For the latter, before issue it points to the buffer group ID,
515          * and after selection it points to the buffer ID itself.
516          */
517         u16                             buf_index;
518         unsigned int                    flags;
519
520         struct io_cqe                   cqe;
521
522         struct io_ring_ctx              *ctx;
523         struct task_struct              *task;
524
525         struct io_rsrc_node             *rsrc_node;
526
527         union {
528                 /* store used ubuf, so we can prevent reloading */
529                 struct io_mapped_ubuf   *imu;
530
531                 /* stores selected buf, valid IFF REQ_F_BUFFER_SELECTED is set */
532                 struct io_buffer        *kbuf;
533
534                 /*
535                  * stores buffer ID for ring provided buffers, valid IFF
536                  * REQ_F_BUFFER_RING is set.
537                  */
538                 struct io_buffer_list   *buf_list;
539         };
540
541         union {
542                 /* used by request caches, completion batching and iopoll */
543                 struct io_wq_work_node  comp_list;
544                 /* cache ->apoll->events */
545                 __poll_t apoll_events;
546         };
547         atomic_t                        refs;
548         atomic_t                        poll_refs;
549         struct io_task_work             io_task_work;
550         /* for polled requests, i.e. IORING_OP_POLL_ADD and async armed poll */
551         union {
552                 struct hlist_node       hash_node;
553                 struct {
554                         u64             extra1;
555                         u64             extra2;
556                 };
557         };
558         /* internal polling, see IORING_FEAT_FAST_POLL */
559         struct async_poll               *apoll;
560         /* opcode allocated if it needs to store data for async defer */
561         void                            *async_data;
562         /* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */
563         struct io_kiocb                 *link;
564         /* custom credentials, valid IFF REQ_F_CREDS is set */
565         const struct cred               *creds;
566         struct io_wq_work               work;
567 };
568
569 struct io_overflow_cqe {
570         struct list_head list;
571         struct io_uring_cqe cqe;
572 };
573
574 #endif