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