uring_cmd.c 9.4 KB

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