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> | |
b66509b8 | 5 | #include <linux/io_uring/cmd.h> |
2a584012 | 6 | #include <linux/security.h> |
9cda70f6 | 7 | #include <linux/nospec.h> |
99f15d8d JA |
8 | |
9 | #include <uapi/linux/io_uring.h> | |
10 | ||
99f15d8d | 11 | #include "io_uring.h" |
414d0f45 | 12 | #include "alloc_cache.h" |
a9216fac | 13 | #include "rsrc.h" |
99f15d8d JA |
14 | #include "uring_cmd.h" |
15 | ||
3a4689ac PB |
16 | void io_cmd_cache_free(const void *entry) |
17 | { | |
18 | struct io_async_cmd *ac = (struct io_async_cmd *)entry; | |
19 | ||
20 | io_vec_free(&ac->vec); | |
21 | kfree(ac); | |
22 | } | |
23 | ||
d10f19df JA |
24 | static void io_req_uring_cleanup(struct io_kiocb *req, unsigned int issue_flags) |
25 | { | |
26 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); | |
5f14404b PB |
27 | struct io_async_cmd *ac = req->async_data; |
28 | struct io_uring_cmd_data *cache = &ac->data; | |
d10f19df | 29 | |
3347fa65 JA |
30 | if (cache->op_data) { |
31 | kfree(cache->op_data); | |
32 | cache->op_data = NULL; | |
33 | } | |
d10f19df JA |
34 | |
35 | if (issue_flags & IO_URING_F_UNLOCKED) | |
36 | return; | |
3a4689ac PB |
37 | |
38 | io_alloc_cache_vec_kasan(&ac->vec); | |
39 | if (ac->vec.nr > IO_VEC_CACHE_SOFT_CAP) | |
40 | io_vec_free(&ac->vec); | |
41 | ||
575e7b06 | 42 | if (io_alloc_cache_put(&req->ctx->cmd_cache, cache)) { |
d10f19df JA |
43 | ioucmd->sqe = NULL; |
44 | req->async_data = NULL; | |
3a4689ac | 45 | req->flags &= ~(REQ_F_ASYNC_DATA|REQ_F_NEED_CLEANUP); |
d10f19df JA |
46 | } |
47 | } | |
48 | ||
3a4689ac PB |
49 | void io_uring_cmd_cleanup(struct io_kiocb *req) |
50 | { | |
51 | io_req_uring_cleanup(req, 0); | |
52 | } | |
53 | ||
da12d9ab | 54 | bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx, |
f03baece | 55 | struct io_uring_task *tctx, bool cancel_all) |
da12d9ab PB |
56 | { |
57 | struct hlist_node *tmp; | |
58 | struct io_kiocb *req; | |
59 | bool ret = false; | |
60 | ||
61 | lockdep_assert_held(&ctx->uring_lock); | |
62 | ||
63 | hlist_for_each_entry_safe(req, tmp, &ctx->cancelable_uring_cmd, | |
64 | hash_node) { | |
65 | struct io_uring_cmd *cmd = io_kiocb_to_cmd(req, | |
66 | struct io_uring_cmd); | |
67 | struct file *file = req->file; | |
68 | ||
b6f58a3f | 69 | if (!cancel_all && req->tctx != tctx) |
da12d9ab PB |
70 | continue; |
71 | ||
72 | if (cmd->flags & IORING_URING_CMD_CANCELABLE) { | |
e1eef2e5 PB |
73 | file->f_op->uring_cmd(cmd, IO_URING_F_CANCEL | |
74 | IO_URING_F_COMPLETE_DEFER); | |
da12d9ab PB |
75 | ret = true; |
76 | } | |
77 | } | |
78 | io_submit_flush_completions(ctx); | |
79 | return ret; | |
80 | } | |
81 | ||
93b8cc60 ML |
82 | static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd, |
83 | unsigned int issue_flags) | |
84 | { | |
85 | struct io_kiocb *req = cmd_to_io_kiocb(cmd); | |
86 | struct io_ring_ctx *ctx = req->ctx; | |
87 | ||
88 | if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) | |
89 | return; | |
90 | ||
91 | cmd->flags &= ~IORING_URING_CMD_CANCELABLE; | |
92 | io_ring_submit_lock(ctx, issue_flags); | |
93 | hlist_del(&req->hash_node); | |
94 | io_ring_submit_unlock(ctx, issue_flags); | |
95 | } | |
96 | ||
97 | /* | |
98 | * Mark this command as concelable, then io_uring_try_cancel_uring_cmd() | |
99 | * will try to cancel this issued command by sending ->uring_cmd() with | |
100 | * issue_flags of IO_URING_F_CANCEL. | |
101 | * | |
102 | * The command is guaranteed to not be done when calling ->uring_cmd() | |
103 | * with IO_URING_F_CANCEL, but it is driver's responsibility to deal | |
104 | * with race between io_uring canceling and normal completion. | |
105 | */ | |
106 | void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd, | |
107 | unsigned int issue_flags) | |
108 | { | |
109 | struct io_kiocb *req = cmd_to_io_kiocb(cmd); | |
110 | struct io_ring_ctx *ctx = req->ctx; | |
111 | ||
112 | if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) { | |
113 | cmd->flags |= IORING_URING_CMD_CANCELABLE; | |
114 | io_ring_submit_lock(ctx, issue_flags); | |
115 | hlist_add_head(&req->hash_node, &ctx->cancelable_uring_cmd); | |
116 | io_ring_submit_unlock(ctx, issue_flags); | |
117 | } | |
118 | } | |
119 | EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable); | |
120 | ||
bcf8a029 | 121 | static void io_uring_cmd_work(struct io_kiocb *req, io_tw_token_t tw) |
99f15d8d | 122 | { |
f2ccb5ae | 123 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
df3b8ca6 PB |
124 | unsigned int flags = IO_URING_F_COMPLETE_DEFER; |
125 | ||
bab4b2cc | 126 | if (io_should_terminate_tw()) |
df3b8ca6 | 127 | flags |= IO_URING_F_TASK_DEAD; |
e1eef2e5 | 128 | |
8e5b3b89 | 129 | /* task_work executor checks the deffered list completion */ |
df3b8ca6 | 130 | ioucmd->task_work_cb(ioucmd, flags); |
99f15d8d JA |
131 | } |
132 | ||
5f3139fc PB |
133 | void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd, |
134 | void (*task_work_cb)(struct io_uring_cmd *, unsigned), | |
135 | unsigned flags) | |
99f15d8d JA |
136 | { |
137 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
138 | ||
139 | ioucmd->task_work_cb = task_work_cb; | |
140 | req->io_task_work.func = io_uring_cmd_work; | |
5f3139fc PB |
141 | __io_req_task_work_add(req, flags); |
142 | } | |
143 | EXPORT_SYMBOL_GPL(__io_uring_cmd_do_in_task); | |
144 | ||
99f15d8d JA |
145 | static inline void io_req_set_cqe32_extra(struct io_kiocb *req, |
146 | u64 extra1, u64 extra2) | |
147 | { | |
b24c5d75 PB |
148 | req->big_cqe.extra1 = extra1; |
149 | req->big_cqe.extra2 = extra2; | |
99f15d8d JA |
150 | } |
151 | ||
152 | /* | |
153 | * Called by consumers of io_uring_cmd, if they originally returned | |
154 | * -EIOCBQUEUED upon receiving the command. | |
155 | */ | |
a07d2d79 | 156 | void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, u64 res2, |
9d2789ac | 157 | unsigned issue_flags) |
99f15d8d JA |
158 | { |
159 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
160 | ||
93b8cc60 ML |
161 | io_uring_cmd_del_cancelable(ioucmd, issue_flags); |
162 | ||
99f15d8d JA |
163 | if (ret < 0) |
164 | req_set_fail(req); | |
165 | ||
ff2557b7 | 166 | io_req_set_res(req, ret, 0); |
99f15d8d JA |
167 | if (req->ctx->flags & IORING_SETUP_CQE32) |
168 | io_req_set_cqe32_extra(req, res2, 0); | |
d10f19df | 169 | io_req_uring_cleanup(req, issue_flags); |
27a67079 | 170 | if (req->ctx->flags & IORING_SETUP_IOPOLL) { |
5756a3a7 KJ |
171 | /* order with io_iopoll_req_issued() checking ->iopoll_complete */ |
172 | smp_store_release(&req->iopoll_completed, 1); | |
e1eef2e5 PB |
173 | } else if (issue_flags & IO_URING_F_COMPLETE_DEFER) { |
174 | if (WARN_ON_ONCE(issue_flags & IO_URING_F_UNLOCKED)) | |
175 | return; | |
6edd953b | 176 | io_req_complete_defer(req); |
27a67079 | 177 | } else { |
6edd953b PB |
178 | req->io_task_work.func = io_req_task_complete; |
179 | io_req_task_work_add(req); | |
27a67079 | 180 | } |
99f15d8d JA |
181 | } |
182 | EXPORT_SYMBOL_GPL(io_uring_cmd_done); | |
183 | ||
d10f19df JA |
184 | static int io_uring_cmd_prep_setup(struct io_kiocb *req, |
185 | const struct io_uring_sqe *sqe) | |
99f15d8d | 186 | { |
f2ccb5ae | 187 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
5f14404b | 188 | struct io_async_cmd *ac; |
99f15d8d | 189 | |
5f14404b PB |
190 | /* see io_uring_cmd_get_async_data() */ |
191 | BUILD_BUG_ON(offsetof(struct io_async_cmd, data) != 0); | |
192 | ||
193 | ac = io_uring_alloc_async_data(&req->ctx->cmd_cache, req); | |
194 | if (!ac) | |
5eff57fa | 195 | return -ENOMEM; |
5f14404b | 196 | ac->data.op_data = NULL; |
5eff57fa | 197 | |
d6211ebb JA |
198 | /* |
199 | * Unconditionally cache the SQE for now - this is only needed for | |
200 | * requests that go async, but prep handlers must ensure that any | |
201 | * sqe data is stable beyond prep. Since uring_cmd is special in | |
202 | * that it doesn't read in per-op data, play it safe and ensure that | |
203 | * any SQE data is stable beyond prep. This can later get relaxed. | |
204 | */ | |
296e1696 PB |
205 | memcpy(ac->sqes, sqe, uring_sqe_size(req->ctx)); |
206 | ioucmd->sqe = ac->sqes; | |
5eff57fa | 207 | return 0; |
99f15d8d JA |
208 | } |
209 | ||
210 | int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) | |
211 | { | |
f2ccb5ae | 212 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
99f15d8d | 213 | |
9cda70f6 | 214 | if (sqe->__pad1) |
99f15d8d | 215 | return -EINVAL; |
9cda70f6 AG |
216 | |
217 | ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags); | |
528ce678 | 218 | if (ioucmd->flags & ~IORING_URING_CMD_MASK) |
9cda70f6 AG |
219 | return -EINVAL; |
220 | ||
5d309914 PB |
221 | if (ioucmd->flags & IORING_URING_CMD_FIXED) |
222 | req->buf_index = READ_ONCE(sqe->buf_index); | |
223 | ||
99f15d8d | 224 | ioucmd->cmd_op = READ_ONCE(sqe->cmd_op); |
d10f19df JA |
225 | |
226 | return io_uring_cmd_prep_setup(req, sqe); | |
99f15d8d JA |
227 | } |
228 | ||
229 | int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags) | |
230 | { | |
f2ccb5ae | 231 | struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd); |
99f15d8d JA |
232 | struct io_ring_ctx *ctx = req->ctx; |
233 | struct file *file = req->file; | |
234 | int ret; | |
235 | ||
03b3d6be | 236 | if (!file->f_op->uring_cmd) |
99f15d8d JA |
237 | return -EOPNOTSUPP; |
238 | ||
2a584012 LC |
239 | ret = security_uring_cmd(ioucmd); |
240 | if (ret) | |
241 | return ret; | |
242 | ||
99f15d8d JA |
243 | if (ctx->flags & IORING_SETUP_SQE128) |
244 | issue_flags |= IO_URING_F_SQE128; | |
245 | if (ctx->flags & IORING_SETUP_CQE32) | |
246 | issue_flags |= IO_URING_F_CQE32; | |
0bba6fcc | 247 | if (io_is_compat(ctx)) |
5fea44a6 | 248 | issue_flags |= IO_URING_F_COMPAT; |
5756a3a7 | 249 | if (ctx->flags & IORING_SETUP_IOPOLL) { |
03b3d6be JA |
250 | if (!file->f_op->uring_cmd_iopoll) |
251 | return -EOPNOTSUPP; | |
99f15d8d | 252 | issue_flags |= IO_URING_F_IOPOLL; |
5756a3a7 | 253 | req->iopoll_completed = 0; |
63166b81 | 254 | if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) { |
255 | /* make sure every req only blocks once */ | |
256 | req->flags &= ~REQ_F_IOPOLL_STATE; | |
257 | req->iopoll_start = ktime_get_ns(); | |
258 | } | |
5756a3a7 | 259 | } |
99f15d8d | 260 | |
99f15d8d | 261 | ret = file->f_op->uring_cmd(ioucmd, issue_flags); |
d6211ebb JA |
262 | if (ret == -EAGAIN || ret == -EIOCBQUEUED) |
263 | return ret; | |
d10f19df JA |
264 | if (ret < 0) |
265 | req_set_fail(req); | |
266 | io_req_uring_cleanup(req, issue_flags); | |
267 | io_req_set_res(req, ret, 0); | |
8bb9d6cc | 268 | return IOU_COMPLETE; |
99f15d8d | 269 | } |
a9216fac AG |
270 | |
271 | int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, | |
0c542a69 CSM |
272 | struct iov_iter *iter, |
273 | struct io_uring_cmd *ioucmd, | |
69d483d5 | 274 | unsigned int issue_flags) |
a9216fac AG |
275 | { |
276 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
277 | ||
6faaf6e0 PB |
278 | if (WARN_ON_ONCE(!(ioucmd->flags & IORING_URING_CMD_FIXED))) |
279 | return -EINVAL; | |
280 | ||
5d309914 | 281 | return io_import_reg_buf(req, iter, ubuf, len, rw, issue_flags); |
a9216fac AG |
282 | } |
283 | EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed); | |
8e9fad0e | 284 | |
ef490275 PB |
285 | int io_uring_cmd_import_fixed_vec(struct io_uring_cmd *ioucmd, |
286 | const struct iovec __user *uvec, | |
287 | size_t uvec_segs, | |
288 | int ddir, struct iov_iter *iter, | |
289 | unsigned issue_flags) | |
290 | { | |
291 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
292 | struct io_async_cmd *ac = req->async_data; | |
293 | int ret; | |
294 | ||
6faaf6e0 PB |
295 | if (WARN_ON_ONCE(!(ioucmd->flags & IORING_URING_CMD_FIXED))) |
296 | return -EINVAL; | |
297 | ||
ef490275 PB |
298 | ret = io_prep_reg_iovec(req, &ac->vec, uvec, uvec_segs); |
299 | if (ret) | |
300 | return ret; | |
301 | ||
302 | return io_import_reg_vec(ddir, iter, req, &ac->vec, uvec_segs, | |
303 | issue_flags); | |
304 | } | |
305 | EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed_vec); | |
306 | ||
6746ee4c PB |
307 | void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd) |
308 | { | |
309 | struct io_kiocb *req = cmd_to_io_kiocb(ioucmd); | |
310 | ||
311 | io_req_queue_iowq(req); | |
312 | } |