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