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