]>
Commit | Line | Data |
---|---|---|
99f15d8d JA |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | #include <linux/kernel.h> | |
3 | #include <linux/errno.h> | |
4 | #include <linux/file.h> | |
5 | #include <linux/io_uring.h> | |
2a584012 | 6 | #include <linux/security.h> |
9cda70f6 | 7 | #include <linux/nospec.h> |
99f15d8d JA |
8 | |
9 | #include <uapi/linux/io_uring.h> | |
8e9fad0e | 10 | #include <uapi/asm-generic/ioctls.h> |
99f15d8d | 11 | |
99f15d8d | 12 | #include "io_uring.h" |
a9216fac | 13 | #include "rsrc.h" |
99f15d8d JA |
14 | #include "uring_cmd.h" |
15 | ||
93b8cc60 ML |
16 | static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd, |
17 | unsigned int issue_flags) | |
18 | { | |
19 | struct io_kiocb *req = cmd_to_io_kiocb(cmd); | |
20 | struct io_ring_ctx *ctx = req->ctx; | |
21 | ||
22 | if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) | |
23 | return; | |
24 | ||
25 | cmd->flags &= ~IORING_URING_CMD_CANCELABLE; | |
26 | io_ring_submit_lock(ctx, issue_flags); | |
27 | hlist_del(&req->hash_node); | |
28 | io_ring_submit_unlock(ctx, issue_flags); | |
29 | } | |
30 | ||
31 | /* | |
32 | * Mark this command as concelable, then io_uring_try_cancel_uring_cmd() | |
33 | * will try to cancel this issued command by sending ->uring_cmd() with | |
34 | * issue_flags of IO_URING_F_CANCEL. | |
35 | * | |
36 | * The command is guaranteed to not be done when calling ->uring_cmd() | |
37 | * with IO_URING_F_CANCEL, but it is driver's responsibility to deal | |
38 | * with race between io_uring canceling and normal completion. | |
39 | */ | |
40 | void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, | |
41 | unsigned int issue_flags) | |
42 | { | |
43 | struct io_kiocb *req = cmd_to_io_kiocb(cmd); | |
44 | struct io_ring_ctx *ctx = req->ctx; | |
45 | ||
46 | if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) { | |
47 | cmd->flags |= IORING_URING_CMD_CANCELABLE; | |
48 | io_ring_submit_lock(ctx, issue_flags); | |
49 | hlist_add_head(&req->hash_node, &ctx->cancelable_uring_cmd); | |
50 | io_ring_submit_unlock(ctx, issue_flags); | |
51 | } | |
52 | } | |
53 | EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable); | |
54 | ||
55 | struct task_struct *io_uring_cmd_get_task(struct io_uring_cmd *cmd) | |
56 | { | |
57 | return cmd_to_io_kiocb(cmd)->task; | |
58 | } | |
59 | EXPORT_SYMBOL_GPL(io_uring_cmd_get_task); | |
60 | ||
a282967c | 61 | static void io_uring_cmd_work(struct io_kiocb *req, struct io_tw_state *ts) |
99f15d8d | 62 | { |
f2ccb5ae | 63 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
a282967c | 64 | unsigned issue_flags = ts->locked ? 0 : IO_URING_F_UNLOCKED; |
99f15d8d | 65 | |
9d2789ac | 66 | ioucmd->task_work_cb(ioucmd, issue_flags); |
99f15d8d JA |
67 | } |
68 | ||
5f3139fc PB |
69 | void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd, |
70 | void (*task_work_cb)(struct io_uring_cmd *, unsigned), | |
71 | unsigned flags) | |
99f15d8d JA |
72 | { |
73 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
74 | ||
75 | ioucmd->task_work_cb = task_work_cb; | |
76 | req->io_task_work.func = io_uring_cmd_work; | |
5f3139fc PB |
77 | __io_req_task_work_add(req, flags); |
78 | } | |
79 | EXPORT_SYMBOL_GPL(__io_uring_cmd_do_in_task); | |
80 | ||
81 | void io_uring_cmd_do_in_task_lazy(struct io_uring_cmd *ioucmd, | |
82 | void (*task_work_cb)(struct io_uring_cmd *, unsigned)) | |
83 | { | |
84 | __io_uring_cmd_do_in_task(ioucmd, task_work_cb, IOU_F_TWQ_LAZY_WAKE); | |
99f15d8d | 85 | } |
5f3139fc | 86 | EXPORT_SYMBOL_GPL(io_uring_cmd_do_in_task_lazy); |
99f15d8d JA |
87 | |
88 | static inline void io_req_set_cqe32_extra(struct io_kiocb *req, | |
89 | u64 extra1, u64 extra2) | |
90 | { | |
b24c5d75 PB |
91 | req->big_cqe.extra1 = extra1; |
92 | req->big_cqe.extra2 = extra2; | |
99f15d8d JA |
93 | } |
94 | ||
95 | /* | |
96 | * Called by consumers of io_uring_cmd, if they originally returned | |
97 | * -EIOCBQUEUED upon receiving the command. | |
98 | */ | |
9d2789ac JA |
99 | void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, ssize_t res2, |
100 | unsigned issue_flags) | |
99f15d8d JA |
101 | { |
102 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
103 | ||
93b8cc60 ML |
104 | io_uring_cmd_del_cancelable(ioucmd, issue_flags); |
105 | ||
99f15d8d JA |
106 | if (ret < 0) |
107 | req_set_fail(req); | |
108 | ||
ff2557b7 | 109 | io_req_set_res(req, ret, 0); |
99f15d8d JA |
110 | if (req->ctx->flags & IORING_SETUP_CQE32) |
111 | io_req_set_cqe32_extra(req, res2, 0); | |
27a67079 | 112 | if (req->ctx->flags & IORING_SETUP_IOPOLL) { |
5756a3a7 KJ |
113 | /* order with io_iopoll_req_issued() checking ->iopoll_complete */ |
114 | smp_store_release(&req->iopoll_completed, 1); | |
27a67079 JA |
115 | } else { |
116 | struct io_tw_state ts = { | |
117 | .locked = !(issue_flags & IO_URING_F_UNLOCKED), | |
118 | }; | |
119 | io_req_task_complete(req, &ts); | |
120 | } | |
99f15d8d JA |
121 | } |
122 | EXPORT_SYMBOL_GPL(io_uring_cmd_done); | |
123 | ||
124 | int io_uring_cmd_prep_async(struct io_kiocb *req) | |
125 | { | |
f2ccb5ae | 126 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
99f15d8d | 127 | |
fd9b8547 BL |
128 | memcpy(req->async_data, ioucmd->sqe, uring_sqe_size(req->ctx)); |
129 | ioucmd->sqe = req->async_data; | |
99f15d8d JA |
130 | return 0; |
131 | } | |
132 | ||
133 | int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
134 | { | |
f2ccb5ae | 135 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
99f15d8d | 136 | |
9cda70f6 | 137 | if (sqe->__pad1) |
99f15d8d | 138 | return -EINVAL; |
9cda70f6 AG |
139 | |
140 | ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags); | |
528ce678 | 141 | if (ioucmd->flags & ~IORING_URING_CMD_MASK) |
9cda70f6 AG |
142 | return -EINVAL; |
143 | ||
144 | if (ioucmd->flags & IORING_URING_CMD_FIXED) { | |
145 | struct io_ring_ctx *ctx = req->ctx; | |
146 | u16 index; | |
147 | ||
148 | req->buf_index = READ_ONCE(sqe->buf_index); | |
149 | if (unlikely(req->buf_index >= ctx->nr_user_bufs)) | |
150 | return -EFAULT; | |
151 | index = array_index_nospec(req->buf_index, ctx->nr_user_bufs); | |
152 | req->imu = ctx->user_bufs[index]; | |
153 | io_req_set_rsrc_node(req, ctx, 0); | |
154 | } | |
fd9b8547 | 155 | ioucmd->sqe = sqe; |
99f15d8d JA |
156 | ioucmd->cmd_op = READ_ONCE(sqe->cmd_op); |
157 | return 0; | |
158 | } | |
159 | ||
160 | int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags) | |
161 | { | |
f2ccb5ae | 162 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
99f15d8d JA |
163 | struct io_ring_ctx *ctx = req->ctx; |
164 | struct file *file = req->file; | |
165 | int ret; | |
166 | ||
03b3d6be | 167 | if (!file->f_op->uring_cmd) |
99f15d8d JA |
168 | return -EOPNOTSUPP; |
169 | ||
2a584012 LC |
170 | ret = security_uring_cmd(ioucmd); |
171 | if (ret) | |
172 | return ret; | |
173 | ||
99f15d8d JA |
174 | if (ctx->flags & IORING_SETUP_SQE128) |
175 | issue_flags |= IO_URING_F_SQE128; | |
176 | if (ctx->flags & IORING_SETUP_CQE32) | |
177 | issue_flags |= IO_URING_F_CQE32; | |
5756a3a7 | 178 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
03b3d6be JA |
179 | if (!file->f_op->uring_cmd_iopoll) |
180 | return -EOPNOTSUPP; | |
99f15d8d | 181 | issue_flags |= IO_URING_F_IOPOLL; |
5756a3a7 KJ |
182 | req->iopoll_completed = 0; |
183 | WRITE_ONCE(ioucmd->cookie, NULL); | |
184 | } | |
99f15d8d | 185 | |
99f15d8d JA |
186 | ret = file->f_op->uring_cmd(ioucmd, issue_flags); |
187 | if (ret == -EAGAIN) { | |
188 | if (!req_has_async_data(req)) { | |
189 | if (io_alloc_async_data(req)) | |
190 | return -ENOMEM; | |
191 | io_uring_cmd_prep_async(req); | |
192 | } | |
193 | return -EAGAIN; | |
194 | } | |
195 | ||
196 | if (ret != -EIOCBQUEUED) { | |
3ed159c9 AG |
197 | if (ret < 0) |
198 | req_set_fail(req); | |
199 | io_req_set_res(req, ret, 0); | |
a9c3eda7 | 200 | return ret; |
99f15d8d JA |
201 | } |
202 | ||
203 | return IOU_ISSUE_SKIP_COMPLETE; | |
204 | } | |
a9216fac AG |
205 | |
206 | int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, | |
207 | struct iov_iter *iter, void *ioucmd) | |
208 | { | |
209 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
210 | ||
211 | return io_import_fixed(rw, iter, req->imu, ubuf, len); | |
212 | } | |
213 | EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed); | |
8e9fad0e BL |
214 | |
215 | int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) | |
216 | { | |
217 | struct socket *sock = cmd->file->private_data; | |
218 | struct sock *sk = sock->sk; | |
219 | struct proto *prot = READ_ONCE(sk->sk_prot); | |
220 | int ret, arg = 0; | |
221 | ||
222 | if (!prot || !prot->ioctl) | |
223 | return -EOPNOTSUPP; | |
224 | ||
225 | switch (cmd->sqe->cmd_op) { | |
226 | case SOCKET_URING_OP_SIOCINQ: | |
227 | ret = prot->ioctl(sk, SIOCINQ, &arg); | |
228 | if (ret) | |
229 | return ret; | |
230 | return arg; | |
231 | case SOCKET_URING_OP_SIOCOUTQ: | |
232 | ret = prot->ioctl(sk, SIOCOUTQ, &arg); | |
233 | if (ret) | |
234 | return ret; | |
235 | return arg; | |
236 | default: | |
237 | return -EOPNOTSUPP; | |
238 | } | |
239 | } | |
240 | EXPORT_SYMBOL_GPL(io_uring_cmd_sock); |