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