io_uring: cancelable uring_cmd
[linux-block.git] / include / linux / io_uring.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _LINUX_IO_URING_H
3 #define _LINUX_IO_URING_H
4
5 #include <linux/sched.h>
6 #include <linux/xarray.h>
7 #include <uapi/linux/io_uring.h>
8
9 enum io_uring_cmd_flags {
10         IO_URING_F_COMPLETE_DEFER       = 1,
11         IO_URING_F_UNLOCKED             = 2,
12         /* the request is executed from poll, it should not be freed */
13         IO_URING_F_MULTISHOT            = 4,
14         /* executed by io-wq */
15         IO_URING_F_IOWQ                 = 8,
16         /* int's last bit, sign checks are usually faster than a bit test */
17         IO_URING_F_NONBLOCK             = INT_MIN,
18
19         /* ctx state flags, for URING_CMD */
20         IO_URING_F_SQE128               = (1 << 8),
21         IO_URING_F_CQE32                = (1 << 9),
22         IO_URING_F_IOPOLL               = (1 << 10),
23
24         /* set when uring wants to cancel a previously issued command */
25         IO_URING_F_CANCEL               = (1 << 11),
26 };
27
28 /* only top 8 bits of sqe->uring_cmd_flags for kernel internal use */
29 #define IORING_URING_CMD_CANCELABLE     (1U << 30)
30 #define IORING_URING_CMD_POLLED         (1U << 31)
31
32 struct io_uring_cmd {
33         struct file     *file;
34         const struct io_uring_sqe *sqe;
35         union {
36                 /* callback to defer completions to task context */
37                 void (*task_work_cb)(struct io_uring_cmd *cmd, unsigned);
38                 /* used for polled completion */
39                 void *cookie;
40         };
41         u32             cmd_op;
42         u32             flags;
43         u8              pdu[32]; /* available inline for free use */
44 };
45
46 static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)
47 {
48         return sqe->cmd;
49 }
50
51 #if defined(CONFIG_IO_URING)
52 int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
53                               struct iov_iter *iter, void *ioucmd);
54 void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret, ssize_t res2,
55                         unsigned issue_flags);
56 struct sock *io_uring_get_socket(struct file *file);
57 void __io_uring_cancel(bool cancel_all);
58 void __io_uring_free(struct task_struct *tsk);
59 void io_uring_unreg_ringfd(void);
60 const char *io_uring_get_opcode(u8 opcode);
61 void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
62                             void (*task_work_cb)(struct io_uring_cmd *, unsigned),
63                             unsigned flags);
64 /* users should follow semantics of IOU_F_TWQ_LAZY_WAKE */
65 void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd,
66                         void (*task_work_cb)(struct io_uring_cmd *, unsigned));
67
68 static inline void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
69                         void (*task_work_cb)(struct io_uring_cmd *, unsigned))
70 {
71         __io_uring_cmd_do_in_task(ioucmd, task_work_cb, 0);
72 }
73
74 static inline void io_uring_files_cancel(void)
75 {
76         if (current->io_uring) {
77                 io_uring_unreg_ringfd();
78                 __io_uring_cancel(false);
79         }
80 }
81 static inline void io_uring_task_cancel(void)
82 {
83         if (current->io_uring)
84                 __io_uring_cancel(true);
85 }
86 static inline void io_uring_free(struct task_struct *tsk)
87 {
88         if (tsk->io_uring)
89                 __io_uring_free(tsk);
90 }
91 int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags);
92 void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
93                 unsigned int issue_flags);
94 struct task_struct *io_uring_cmd_get_task(struct io_uring_cmd *cmd);
95 #else
96 static inline int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
97                               struct iov_iter *iter, void *ioucmd)
98 {
99         return -EOPNOTSUPP;
100 }
101 static inline void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret,
102                 ssize_t ret2, unsigned issue_flags)
103 {
104 }
105 static inline void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
106                         void (*task_work_cb)(struct io_uring_cmd *, unsigned))
107 {
108 }
109 static inline void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd,
110                         void (*task_work_cb)(struct io_uring_cmd *, unsigned))
111 {
112 }
113 static inline struct sock *io_uring_get_socket(struct file *file)
114 {
115         return NULL;
116 }
117 static inline void io_uring_task_cancel(void)
118 {
119 }
120 static inline void io_uring_files_cancel(void)
121 {
122 }
123 static inline void io_uring_free(struct task_struct *tsk)
124 {
125 }
126 static inline const char *io_uring_get_opcode(u8 opcode)
127 {
128         return "";
129 }
130 static inline int io_uring_cmd_sock(struct io_uring_cmd *cmd,
131                                     unsigned int issue_flags)
132 {
133         return -EOPNOTSUPP;
134 }
135 static inline void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
136                 unsigned int issue_flags)
137 {
138 }
139 static inline struct task_struct *io_uring_cmd_get_task(struct io_uring_cmd *cmd)
140 {
141         return NULL;
142 }
143 #endif
144
145 #endif