Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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/cmd.h>
6#include <linux/io_uring/net.h>
7#include <linux/security.h>
8#include <linux/nospec.h>
9#include <net/sock.h>
10
11#include <uapi/linux/io_uring.h>
12#include <asm/ioctls.h>
13
14#include "io_uring.h"
15#include "alloc_cache.h"
16#include "rsrc.h"
17#include "uring_cmd.h"
18
19static void io_req_uring_cleanup(struct io_kiocb *req, unsigned int issue_flags)
20{
21 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
22 struct io_uring_cmd_data *cache = req->async_data;
23
24 if (cache->op_data) {
25 kfree(cache->op_data);
26 cache->op_data = NULL;
27 }
28
29 if (issue_flags & IO_URING_F_UNLOCKED)
30 return;
31 if (io_alloc_cache_put(&req->ctx->uring_cache, cache)) {
32 ioucmd->sqe = NULL;
33 req->async_data = NULL;
34 req->flags &= ~REQ_F_ASYNC_DATA;
35 }
36}
37
38bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx,
39 struct io_uring_task *tctx, bool cancel_all)
40{
41 struct hlist_node *tmp;
42 struct io_kiocb *req;
43 bool ret = false;
44
45 lockdep_assert_held(&ctx->uring_lock);
46
47 hlist_for_each_entry_safe(req, tmp, &ctx->cancelable_uring_cmd,
48 hash_node) {
49 struct io_uring_cmd *cmd = io_kiocb_to_cmd(req,
50 struct io_uring_cmd);
51 struct file *file = req->file;
52
53 if (!cancel_all && req->tctx != tctx)
54 continue;
55
56 if (cmd->flags & IORING_URING_CMD_CANCELABLE) {
57 /* ->sqe isn't available if no async data */
58 if (!req_has_async_data(req))
59 cmd->sqe = NULL;
60 file->f_op->uring_cmd(cmd, IO_URING_F_CANCEL |
61 IO_URING_F_COMPLETE_DEFER);
62 ret = true;
63 }
64 }
65 io_submit_flush_completions(ctx);
66 return ret;
67}
68
69static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd,
70 unsigned int issue_flags)
71{
72 struct io_kiocb *req = cmd_to_io_kiocb(cmd);
73 struct io_ring_ctx *ctx = req->ctx;
74
75 if (!(cmd->flags & IORING_URING_CMD_CANCELABLE))
76 return;
77
78 cmd->flags &= ~IORING_URING_CMD_CANCELABLE;
79 io_ring_submit_lock(ctx, issue_flags);
80 hlist_del(&req->hash_node);
81 io_ring_submit_unlock(ctx, issue_flags);
82}
83
84/*
85 * Mark this command as concelable, then io_uring_try_cancel_uring_cmd()
86 * will try to cancel this issued command by sending ->uring_cmd() with
87 * issue_flags of IO_URING_F_CANCEL.
88 *
89 * The command is guaranteed to not be done when calling ->uring_cmd()
90 * with IO_URING_F_CANCEL, but it is driver's responsibility to deal
91 * with race between io_uring canceling and normal completion.
92 */
93void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
94 unsigned int issue_flags)
95{
96 struct io_kiocb *req = cmd_to_io_kiocb(cmd);
97 struct io_ring_ctx *ctx = req->ctx;
98
99 if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) {
100 cmd->flags |= IORING_URING_CMD_CANCELABLE;
101 io_ring_submit_lock(ctx, issue_flags);
102 hlist_add_head(&req->hash_node, &ctx->cancelable_uring_cmd);
103 io_ring_submit_unlock(ctx, issue_flags);
104 }
105}
106EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable);
107
108static void io_uring_cmd_work(struct io_kiocb *req, struct io_tw_state *ts)
109{
110 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
111 unsigned int flags = IO_URING_F_COMPLETE_DEFER;
112
113 if (io_should_terminate_tw())
114 flags |= IO_URING_F_TASK_DEAD;
115
116 /* task_work executor checks the deffered list completion */
117 ioucmd->task_work_cb(ioucmd, flags);
118}
119
120void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
121 void (*task_work_cb)(struct io_uring_cmd *, unsigned),
122 unsigned flags)
123{
124 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
125
126 ioucmd->task_work_cb = task_work_cb;
127 req->io_task_work.func = io_uring_cmd_work;
128 __io_req_task_work_add(req, flags);
129}
130EXPORT_SYMBOL_GPL(__io_uring_cmd_do_in_task);
131
132static inline void io_req_set_cqe32_extra(struct io_kiocb *req,
133 u64 extra1, u64 extra2)
134{
135 req->big_cqe.extra1 = extra1;
136 req->big_cqe.extra2 = extra2;
137}
138
139/*
140 * Called by consumers of io_uring_cmd, if they originally returned
141 * -EIOCBQUEUED upon receiving the command.
142 */
143void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, u64 res2,
144 unsigned issue_flags)
145{
146 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
147
148 io_uring_cmd_del_cancelable(ioucmd, issue_flags);
149
150 if (ret < 0)
151 req_set_fail(req);
152
153 io_req_set_res(req, ret, 0);
154 if (req->ctx->flags & IORING_SETUP_CQE32)
155 io_req_set_cqe32_extra(req, res2, 0);
156 io_req_uring_cleanup(req, issue_flags);
157 if (req->ctx->flags & IORING_SETUP_IOPOLL) {
158 /* order with io_iopoll_req_issued() checking ->iopoll_complete */
159 smp_store_release(&req->iopoll_completed, 1);
160 } else if (issue_flags & IO_URING_F_COMPLETE_DEFER) {
161 if (WARN_ON_ONCE(issue_flags & IO_URING_F_UNLOCKED))
162 return;
163 io_req_complete_defer(req);
164 } else {
165 req->io_task_work.func = io_req_task_complete;
166 io_req_task_work_add(req);
167 }
168}
169EXPORT_SYMBOL_GPL(io_uring_cmd_done);
170
171static int io_uring_cmd_prep_setup(struct io_kiocb *req,
172 const struct io_uring_sqe *sqe)
173{
174 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
175 struct io_uring_cmd_data *cache;
176
177 cache = io_uring_alloc_async_data(&req->ctx->uring_cache, req);
178 if (!cache)
179 return -ENOMEM;
180 cache->op_data = NULL;
181
182 if (!(req->flags & REQ_F_FORCE_ASYNC)) {
183 /* defer memcpy until we need it */
184 ioucmd->sqe = sqe;
185 return 0;
186 }
187
188 memcpy(cache->sqes, sqe, uring_sqe_size(req->ctx));
189 ioucmd->sqe = cache->sqes;
190 return 0;
191}
192
193int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
194{
195 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
196
197 if (sqe->__pad1)
198 return -EINVAL;
199
200 ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags);
201 if (ioucmd->flags & ~IORING_URING_CMD_MASK)
202 return -EINVAL;
203
204 if (ioucmd->flags & IORING_URING_CMD_FIXED) {
205 struct io_ring_ctx *ctx = req->ctx;
206 struct io_rsrc_node *node;
207 u16 index = READ_ONCE(sqe->buf_index);
208
209 node = io_rsrc_node_lookup(&ctx->buf_table, index);
210 if (unlikely(!node))
211 return -EFAULT;
212 /*
213 * Pi node upfront, prior to io_uring_cmd_import_fixed()
214 * being called. This prevents destruction of the mapped buffer
215 * we'll need at actual import time.
216 */
217 io_req_assign_buf_node(req, node);
218 }
219 ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
220
221 return io_uring_cmd_prep_setup(req, sqe);
222}
223
224int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
225{
226 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
227 struct io_ring_ctx *ctx = req->ctx;
228 struct file *file = req->file;
229 int ret;
230
231 if (!file->f_op->uring_cmd)
232 return -EOPNOTSUPP;
233
234 ret = security_uring_cmd(ioucmd);
235 if (ret)
236 return ret;
237
238 if (ctx->flags & IORING_SETUP_SQE128)
239 issue_flags |= IO_URING_F_SQE128;
240 if (ctx->flags & IORING_SETUP_CQE32)
241 issue_flags |= IO_URING_F_CQE32;
242 if (ctx->compat)
243 issue_flags |= IO_URING_F_COMPAT;
244 if (ctx->flags & IORING_SETUP_IOPOLL) {
245 if (!file->f_op->uring_cmd_iopoll)
246 return -EOPNOTSUPP;
247 issue_flags |= IO_URING_F_IOPOLL;
248 req->iopoll_completed = 0;
249 }
250
251 ret = file->f_op->uring_cmd(ioucmd, issue_flags);
252 if (ret == -EAGAIN) {
253 struct io_uring_cmd_data *cache = req->async_data;
254
255 if (ioucmd->sqe != (void *) cache)
256 memcpy(cache->sqes, ioucmd->sqe, uring_sqe_size(req->ctx));
257 return -EAGAIN;
258 } else if (ret == -EIOCBQUEUED) {
259 return -EIOCBQUEUED;
260 }
261
262 if (ret < 0)
263 req_set_fail(req);
264 io_req_uring_cleanup(req, issue_flags);
265 io_req_set_res(req, ret, 0);
266 return IOU_OK;
267}
268
269int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
270 struct iov_iter *iter, void *ioucmd)
271{
272 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
273 struct io_rsrc_node *node = req->buf_node;
274
275 /* Must have had rsrc_node assigned at prep time */
276 if (node)
277 return io_import_fixed(rw, iter, node->buf, ubuf, len);
278
279 return -EFAULT;
280}
281EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed);
282
283void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd)
284{
285 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
286
287 io_req_queue_iowq(req);
288}
289
290static inline int io_uring_cmd_getsockopt(struct socket *sock,
291 struct io_uring_cmd *cmd,
292 unsigned int issue_flags)
293{
294 bool compat = !!(issue_flags & IO_URING_F_COMPAT);
295 int optlen, optname, level, err;
296 void __user *optval;
297
298 level = READ_ONCE(cmd->sqe->level);
299 if (level != SOL_SOCKET)
300 return -EOPNOTSUPP;
301
302 optval = u64_to_user_ptr(READ_ONCE(cmd->sqe->optval));
303 optname = READ_ONCE(cmd->sqe->optname);
304 optlen = READ_ONCE(cmd->sqe->optlen);
305
306 err = do_sock_getsockopt(sock, compat, level, optname,
307 USER_SOCKPTR(optval),
308 KERNEL_SOCKPTR(&optlen));
309 if (err)
310 return err;
311
312 /* On success, return optlen */
313 return optlen;
314}
315
316static inline int io_uring_cmd_setsockopt(struct socket *sock,
317 struct io_uring_cmd *cmd,
318 unsigned int issue_flags)
319{
320 bool compat = !!(issue_flags & IO_URING_F_COMPAT);
321 int optname, optlen, level;
322 void __user *optval;
323 sockptr_t optval_s;
324
325 optval = u64_to_user_ptr(READ_ONCE(cmd->sqe->optval));
326 optname = READ_ONCE(cmd->sqe->optname);
327 optlen = READ_ONCE(cmd->sqe->optlen);
328 level = READ_ONCE(cmd->sqe->level);
329 optval_s = USER_SOCKPTR(optval);
330
331 return do_sock_setsockopt(sock, compat, level, optname, optval_s,
332 optlen);
333}
334
335#if defined(CONFIG_NET)
336int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags)
337{
338 struct socket *sock = cmd->file->private_data;
339 struct sock *sk = sock->sk;
340 struct proto *prot = READ_ONCE(sk->sk_prot);
341 int ret, arg = 0;
342
343 if (!prot || !prot->ioctl)
344 return -EOPNOTSUPP;
345
346 switch (cmd->cmd_op) {
347 case SOCKET_URING_OP_SIOCINQ:
348 ret = prot->ioctl(sk, SIOCINQ, &arg);
349 if (ret)
350 return ret;
351 return arg;
352 case SOCKET_URING_OP_SIOCOUTQ:
353 ret = prot->ioctl(sk, SIOCOUTQ, &arg);
354 if (ret)
355 return ret;
356 return arg;
357 case SOCKET_URING_OP_GETSOCKOPT:
358 return io_uring_cmd_getsockopt(sock, cmd, issue_flags);
359 case SOCKET_URING_OP_SETSOCKOPT:
360 return io_uring_cmd_setsockopt(sock, cmd, issue_flags);
361 default:
362 return -EOPNOTSUPP;
363 }
364}
365EXPORT_SYMBOL_GPL(io_uring_cmd_sock);
366#endif