uring_cmd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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/security.h>
  7. #include <linux/nospec.h>
  8. #include <uapi/linux/io_uring.h>
  9. #include "io_uring.h"
  10. #include "alloc_cache.h"
  11. #include "rsrc.h"
  12. #include "kbuf.h"
  13. #include "uring_cmd.h"
  14. #include "poll.h"
  15. void io_cmd_cache_free(const void *entry)
  16. {
  17. struct io_async_cmd *ac = (struct io_async_cmd *)entry;
  18. io_vec_free(&ac->vec);
  19. kfree(ac);
  20. }
  21. static void io_req_uring_cleanup(struct io_kiocb *req, unsigned int issue_flags)
  22. {
  23. struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
  24. struct io_async_cmd *ac = req->async_data;
  25. if (issue_flags & IO_URING_F_UNLOCKED)
  26. return;
  27. io_alloc_cache_vec_kasan(&ac->vec);
  28. if (ac->vec.nr > IO_VEC_CACHE_SOFT_CAP)
  29. io_vec_free(&ac->vec);
  30. if (io_alloc_cache_put(&req->ctx->cmd_cache, ac)) {
  31. ioucmd->sqe = NULL;
  32. io_req_async_data_clear(req, REQ_F_NEED_CLEANUP);
  33. }
  34. }
  35. void io_uring_cmd_cleanup(struct io_kiocb *req)
  36. {
  37. io_req_uring_cleanup(req, 0);
  38. }
  39. bool io_uring_try_cancel_uring_cmd(struct io_ring_ctx *ctx,
  40. struct io_uring_task *tctx, bool cancel_all)
  41. {
  42. struct hlist_node *tmp;
  43. struct io_kiocb *req;
  44. bool ret = false;
  45. lockdep_assert_held(&ctx->uring_lock);
  46. hlist_for_each_entry_safe(req, tmp, &ctx->cancelable_uring_cmd,
  47. hash_node) {
  48. struct io_uring_cmd *cmd = io_kiocb_to_cmd(req,
  49. struct io_uring_cmd);
  50. struct file *file = req->file;
  51. if (!cancel_all && req->tctx != tctx)
  52. continue;
  53. if (cmd->flags & IORING_URING_CMD_CANCELABLE) {
  54. file->f_op->uring_cmd(cmd, IO_URING_F_CANCEL |
  55. IO_URING_F_COMPLETE_DEFER);
  56. ret = true;
  57. }
  58. }
  59. io_submit_flush_completions(ctx);
  60. return ret;
  61. }
  62. static void io_uring_cmd_del_cancelable(struct io_uring_cmd *cmd,
  63. unsigned int issue_flags)
  64. {
  65. struct io_kiocb *req = cmd_to_io_kiocb(cmd);
  66. struct io_ring_ctx *ctx = req->ctx;
  67. if (!(cmd->flags & IORING_URING_CMD_CANCELABLE))
  68. return;
  69. cmd->flags &= ~IORING_URING_CMD_CANCELABLE;
  70. io_ring_submit_lock(ctx, issue_flags);
  71. hlist_del(&req->hash_node);
  72. io_ring_submit_unlock(ctx, issue_flags);
  73. }
  74. /*
  75. * Mark this command as concelable, then io_uring_try_cancel_uring_cmd()
  76. * will try to cancel this issued command by sending ->uring_cmd() with
  77. * issue_flags of IO_URING_F_CANCEL.
  78. *
  79. * The command is guaranteed to not be done when calling ->uring_cmd()
  80. * with IO_URING_F_CANCEL, but it is driver's responsibility to deal
  81. * with race between io_uring canceling and normal completion.
  82. */
  83. void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,
  84. unsigned int issue_flags)
  85. {
  86. struct io_kiocb *req = cmd_to_io_kiocb(cmd);
  87. struct io_ring_ctx *ctx = req->ctx;
  88. /*
  89. * Doing cancelations on IOPOLL requests are not supported. Both
  90. * because they can't get canceled in the block stack, but also
  91. * because iopoll completion data overlaps with the hash_node used
  92. * for tracking.
  93. */
  94. if (ctx->flags & IORING_SETUP_IOPOLL)
  95. return;
  96. if (!(cmd->flags & IORING_URING_CMD_CANCELABLE)) {
  97. cmd->flags |= IORING_URING_CMD_CANCELABLE;
  98. io_ring_submit_lock(ctx, issue_flags);
  99. hlist_add_head(&req->hash_node, &ctx->cancelable_uring_cmd);
  100. io_ring_submit_unlock(ctx, issue_flags);
  101. }
  102. }
  103. EXPORT_SYMBOL_GPL(io_uring_cmd_mark_cancelable);
  104. void __io_uring_cmd_do_in_task(struct io_uring_cmd *ioucmd,
  105. io_req_tw_func_t task_work_cb,
  106. unsigned flags)
  107. {
  108. struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
  109. if (WARN_ON_ONCE(req->flags & REQ_F_APOLL_MULTISHOT))
  110. return;
  111. req->io_task_work.func = task_work_cb;
  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, s32 ret, u64 res2,
  126. unsigned issue_flags, bool is_cqe32)
  127. {
  128. struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
  129. if (WARN_ON_ONCE(req->flags & REQ_F_APOLL_MULTISHOT))
  130. return;
  131. io_uring_cmd_del_cancelable(ioucmd, issue_flags);
  132. if (ret < 0)
  133. req_set_fail(req);
  134. io_req_set_res(req, ret, 0);
  135. if (is_cqe32) {
  136. if (req->ctx->flags & IORING_SETUP_CQE_MIXED)
  137. req->cqe.flags |= IORING_CQE_F_32;
  138. io_req_set_cqe32_extra(req, res2, 0);
  139. }
  140. io_req_uring_cleanup(req, issue_flags);
  141. if (req->ctx->flags & IORING_SETUP_IOPOLL) {
  142. /* order with io_iopoll_req_issued() checking ->iopoll_complete */
  143. smp_store_release(&req->iopoll_completed, 1);
  144. } else if (issue_flags & IO_URING_F_COMPLETE_DEFER) {
  145. if (WARN_ON_ONCE(issue_flags & IO_URING_F_UNLOCKED))
  146. return;
  147. io_req_complete_defer(req);
  148. } else {
  149. req->io_task_work.func = io_req_task_complete;
  150. io_req_task_work_add(req);
  151. }
  152. }
  153. EXPORT_SYMBOL_GPL(__io_uring_cmd_done);
  154. int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  155. {
  156. struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
  157. struct io_async_cmd *ac;
  158. if (sqe->__pad1)
  159. return -EINVAL;
  160. ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags);
  161. if (ioucmd->flags & ~IORING_URING_CMD_MASK)
  162. return -EINVAL;
  163. if (ioucmd->flags & IORING_URING_CMD_FIXED) {
  164. if (ioucmd->flags & IORING_URING_CMD_MULTISHOT)
  165. return -EINVAL;
  166. req->buf_index = READ_ONCE(sqe->buf_index);
  167. }
  168. if (!!(ioucmd->flags & IORING_URING_CMD_MULTISHOT) !=
  169. !!(req->flags & REQ_F_BUFFER_SELECT))
  170. return -EINVAL;
  171. ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
  172. ac = io_uring_alloc_async_data(&req->ctx->cmd_cache, req);
  173. if (!ac)
  174. return -ENOMEM;
  175. ioucmd->sqe = sqe;
  176. return 0;
  177. }
  178. /*
  179. * IORING_SETUP_SQE128 contexts allocate twice the normal SQE size for each
  180. * slot.
  181. */
  182. static inline size_t uring_sqe_size(struct io_kiocb *req)
  183. {
  184. if (req->ctx->flags & IORING_SETUP_SQE128 ||
  185. req->opcode == IORING_OP_URING_CMD128)
  186. return 2 * sizeof(struct io_uring_sqe);
  187. return sizeof(struct io_uring_sqe);
  188. }
  189. void io_uring_cmd_sqe_copy(struct io_kiocb *req)
  190. {
  191. struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
  192. struct io_async_cmd *ac = req->async_data;
  193. /* Should not happen, as REQ_F_SQE_COPIED covers this */
  194. if (WARN_ON_ONCE(ioucmd->sqe == ac->sqes))
  195. return;
  196. memcpy(ac->sqes, ioucmd->sqe, uring_sqe_size(req));
  197. ioucmd->sqe = ac->sqes;
  198. }
  199. int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
  200. {
  201. struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
  202. struct io_ring_ctx *ctx = req->ctx;
  203. struct file *file = req->file;
  204. int ret;
  205. if (!file->f_op->uring_cmd)
  206. return -EOPNOTSUPP;
  207. ret = security_uring_cmd(ioucmd);
  208. if (ret)
  209. return ret;
  210. if (ctx->flags & IORING_SETUP_SQE128 ||
  211. req->opcode == IORING_OP_URING_CMD128)
  212. issue_flags |= IO_URING_F_SQE128;
  213. if (ctx->flags & (IORING_SETUP_CQE32 | IORING_SETUP_CQE_MIXED))
  214. issue_flags |= IO_URING_F_CQE32;
  215. if (io_is_compat(ctx))
  216. issue_flags |= IO_URING_F_COMPAT;
  217. if (ctx->flags & IORING_SETUP_IOPOLL) {
  218. if (!file->f_op->uring_cmd_iopoll)
  219. return -EOPNOTSUPP;
  220. issue_flags |= IO_URING_F_IOPOLL;
  221. req->iopoll_completed = 0;
  222. if (ctx->flags & IORING_SETUP_HYBRID_IOPOLL) {
  223. /* make sure every req only blocks once */
  224. req->flags &= ~REQ_F_IOPOLL_STATE;
  225. req->iopoll_start = ktime_get_ns();
  226. }
  227. }
  228. ret = file->f_op->uring_cmd(ioucmd, issue_flags);
  229. if (ioucmd->flags & IORING_URING_CMD_MULTISHOT) {
  230. if (ret >= 0)
  231. return IOU_ISSUE_SKIP_COMPLETE;
  232. }
  233. if (ret == -EAGAIN) {
  234. ioucmd->flags |= IORING_URING_CMD_REISSUE;
  235. return ret;
  236. }
  237. if (ret == -EIOCBQUEUED)
  238. return ret;
  239. if (ret < 0)
  240. req_set_fail(req);
  241. io_req_uring_cleanup(req, issue_flags);
  242. io_req_set_res(req, ret, 0);
  243. return IOU_COMPLETE;
  244. }
  245. int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
  246. struct iov_iter *iter,
  247. struct io_uring_cmd *ioucmd,
  248. unsigned int issue_flags)
  249. {
  250. struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
  251. if (WARN_ON_ONCE(!(ioucmd->flags & IORING_URING_CMD_FIXED)))
  252. return -EINVAL;
  253. return io_import_reg_buf(req, iter, ubuf, len, rw, issue_flags);
  254. }
  255. EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed);
  256. int io_uring_cmd_import_fixed_vec(struct io_uring_cmd *ioucmd,
  257. const struct iovec __user *uvec,
  258. size_t uvec_segs,
  259. int ddir, struct iov_iter *iter,
  260. unsigned issue_flags)
  261. {
  262. struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
  263. struct io_async_cmd *ac = req->async_data;
  264. int ret;
  265. if (WARN_ON_ONCE(!(ioucmd->flags & IORING_URING_CMD_FIXED)))
  266. return -EINVAL;
  267. ret = io_prep_reg_iovec(req, &ac->vec, uvec, uvec_segs);
  268. if (ret)
  269. return ret;
  270. return io_import_reg_vec(ddir, iter, req, &ac->vec, uvec_segs,
  271. issue_flags);
  272. }
  273. EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed_vec);
  274. void io_uring_cmd_issue_blocking(struct io_uring_cmd *ioucmd)
  275. {
  276. struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
  277. io_req_queue_iowq(req);
  278. }
  279. int io_cmd_poll_multishot(struct io_uring_cmd *cmd,
  280. unsigned int issue_flags, __poll_t mask)
  281. {
  282. struct io_kiocb *req = cmd_to_io_kiocb(cmd);
  283. int ret;
  284. if (likely(req->flags & REQ_F_APOLL_MULTISHOT))
  285. return 0;
  286. req->flags |= REQ_F_APOLL_MULTISHOT;
  287. mask &= ~EPOLLONESHOT;
  288. ret = io_arm_apoll(req, issue_flags, mask);
  289. return ret == IO_APOLL_OK ? -EIOCBQUEUED : -ECANCELED;
  290. }
  291. bool io_uring_cmd_post_mshot_cqe32(struct io_uring_cmd *cmd,
  292. unsigned int issue_flags,
  293. struct io_uring_cqe cqe[2])
  294. {
  295. struct io_kiocb *req = cmd_to_io_kiocb(cmd);
  296. if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_MULTISHOT)))
  297. return false;
  298. return io_req_post_cqe32(req, cqe);
  299. }
  300. /*
  301. * Work with io_uring_mshot_cmd_post_cqe() together for committing the
  302. * provided buffer upfront
  303. */
  304. struct io_br_sel io_uring_cmd_buffer_select(struct io_uring_cmd *ioucmd,
  305. unsigned buf_group, size_t *len,
  306. unsigned int issue_flags)
  307. {
  308. struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
  309. if (!(ioucmd->flags & IORING_URING_CMD_MULTISHOT))
  310. return (struct io_br_sel) { .val = -EINVAL };
  311. if (WARN_ON_ONCE(!io_do_buffer_select(req)))
  312. return (struct io_br_sel) { .val = -EINVAL };
  313. return io_buffer_select(req, len, buf_group, issue_flags);
  314. }
  315. EXPORT_SYMBOL_GPL(io_uring_cmd_buffer_select);
  316. /*
  317. * Return true if this multishot uring_cmd needs to be completed, otherwise
  318. * the event CQE is posted successfully.
  319. *
  320. * This function must use `struct io_br_sel` returned from
  321. * io_uring_cmd_buffer_select() for committing the buffer in the same
  322. * uring_cmd submission context.
  323. */
  324. bool io_uring_mshot_cmd_post_cqe(struct io_uring_cmd *ioucmd,
  325. struct io_br_sel *sel, unsigned int issue_flags)
  326. {
  327. struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
  328. unsigned int cflags = 0;
  329. if (!(ioucmd->flags & IORING_URING_CMD_MULTISHOT))
  330. return true;
  331. if (sel->val > 0) {
  332. cflags = io_put_kbuf(req, sel->val, sel->buf_list);
  333. if (io_req_post_cqe(req, sel->val, cflags | IORING_CQE_F_MORE))
  334. return false;
  335. }
  336. io_kbuf_recycle(req, sel->buf_list, issue_flags);
  337. if (sel->val < 0)
  338. req_set_fail(req);
  339. io_req_set_res(req, sel->val, cflags);
  340. return true;
  341. }
  342. EXPORT_SYMBOL_GPL(io_uring_mshot_cmd_post_cqe);