msg_ring.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/slab.h>
  6. #include <linux/nospec.h>
  7. #include <linux/io_uring.h>
  8. #include <uapi/linux/io_uring.h>
  9. #include "io_uring.h"
  10. #include "rsrc.h"
  11. #include "filetable.h"
  12. #include "alloc_cache.h"
  13. #include "msg_ring.h"
  14. /* All valid masks for MSG_RING */
  15. #define IORING_MSG_RING_MASK (IORING_MSG_RING_CQE_SKIP | \
  16. IORING_MSG_RING_FLAGS_PASS)
  17. struct io_msg {
  18. struct file *file;
  19. struct file *src_file;
  20. struct callback_head tw;
  21. u64 user_data;
  22. u32 len;
  23. u32 cmd;
  24. u32 src_fd;
  25. union {
  26. u32 dst_fd;
  27. u32 cqe_flags;
  28. };
  29. u32 flags;
  30. };
  31. static void io_double_unlock_ctx(struct io_ring_ctx *octx)
  32. {
  33. mutex_unlock(&octx->uring_lock);
  34. }
  35. static int io_double_lock_ctx(struct io_ring_ctx *octx,
  36. unsigned int issue_flags)
  37. {
  38. /*
  39. * To ensure proper ordering between the two ctxs, we can only
  40. * attempt a trylock on the target. If that fails and we already have
  41. * the source ctx lock, punt to io-wq.
  42. */
  43. if (!(issue_flags & IO_URING_F_UNLOCKED)) {
  44. if (!mutex_trylock(&octx->uring_lock))
  45. return -EAGAIN;
  46. return 0;
  47. }
  48. mutex_lock(&octx->uring_lock);
  49. return 0;
  50. }
  51. void io_msg_ring_cleanup(struct io_kiocb *req)
  52. {
  53. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  54. if (WARN_ON_ONCE(!msg->src_file))
  55. return;
  56. fput(msg->src_file);
  57. msg->src_file = NULL;
  58. }
  59. static inline bool io_msg_need_remote(struct io_ring_ctx *target_ctx)
  60. {
  61. return target_ctx->task_complete;
  62. }
  63. static void io_msg_tw_complete(struct io_kiocb *req, struct io_tw_state *ts)
  64. {
  65. struct io_ring_ctx *ctx = req->ctx;
  66. io_add_aux_cqe(ctx, req->cqe.user_data, req->cqe.res, req->cqe.flags);
  67. if (spin_trylock(&ctx->msg_lock)) {
  68. if (io_alloc_cache_put(&ctx->msg_cache, req))
  69. req = NULL;
  70. spin_unlock(&ctx->msg_lock);
  71. }
  72. if (req)
  73. kmem_cache_free(req_cachep, req);
  74. percpu_ref_put(&ctx->refs);
  75. }
  76. static int io_msg_remote_post(struct io_ring_ctx *ctx, struct io_kiocb *req,
  77. int res, u32 cflags, u64 user_data)
  78. {
  79. req->task = READ_ONCE(ctx->submitter_task);
  80. if (!req->task) {
  81. kmem_cache_free(req_cachep, req);
  82. return -EOWNERDEAD;
  83. }
  84. req->cqe.user_data = user_data;
  85. io_req_set_res(req, res, cflags);
  86. percpu_ref_get(&ctx->refs);
  87. req->ctx = ctx;
  88. req->io_task_work.func = io_msg_tw_complete;
  89. io_req_task_work_add_remote(req, ctx, IOU_F_TWQ_LAZY_WAKE);
  90. return 0;
  91. }
  92. static struct io_kiocb *io_msg_get_kiocb(struct io_ring_ctx *ctx)
  93. {
  94. struct io_kiocb *req = NULL;
  95. if (spin_trylock(&ctx->msg_lock)) {
  96. req = io_alloc_cache_get(&ctx->msg_cache);
  97. spin_unlock(&ctx->msg_lock);
  98. if (req)
  99. return req;
  100. }
  101. return kmem_cache_alloc(req_cachep, GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO);
  102. }
  103. static int io_msg_data_remote(struct io_kiocb *req)
  104. {
  105. struct io_ring_ctx *target_ctx = req->file->private_data;
  106. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  107. struct io_kiocb *target;
  108. u32 flags = 0;
  109. target = io_msg_get_kiocb(req->ctx);
  110. if (unlikely(!target))
  111. return -ENOMEM;
  112. if (msg->flags & IORING_MSG_RING_FLAGS_PASS)
  113. flags = msg->cqe_flags;
  114. return io_msg_remote_post(target_ctx, target, msg->len, flags,
  115. msg->user_data);
  116. }
  117. static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags)
  118. {
  119. struct io_ring_ctx *target_ctx = req->file->private_data;
  120. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  121. u32 flags = 0;
  122. int ret;
  123. if (msg->src_fd || msg->flags & ~IORING_MSG_RING_FLAGS_PASS)
  124. return -EINVAL;
  125. if (!(msg->flags & IORING_MSG_RING_FLAGS_PASS) && msg->dst_fd)
  126. return -EINVAL;
  127. if (target_ctx->flags & IORING_SETUP_R_DISABLED)
  128. return -EBADFD;
  129. if (io_msg_need_remote(target_ctx))
  130. return io_msg_data_remote(req);
  131. if (msg->flags & IORING_MSG_RING_FLAGS_PASS)
  132. flags = msg->cqe_flags;
  133. ret = -EOVERFLOW;
  134. if (target_ctx->flags & IORING_SETUP_IOPOLL) {
  135. if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
  136. return -EAGAIN;
  137. }
  138. if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, flags))
  139. ret = 0;
  140. if (target_ctx->flags & IORING_SETUP_IOPOLL)
  141. io_double_unlock_ctx(target_ctx);
  142. return ret;
  143. }
  144. static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
  145. {
  146. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  147. struct io_ring_ctx *ctx = req->ctx;
  148. struct file *file = NULL;
  149. int idx = msg->src_fd;
  150. io_ring_submit_lock(ctx, issue_flags);
  151. if (likely(idx < ctx->nr_user_files)) {
  152. idx = array_index_nospec(idx, ctx->nr_user_files);
  153. file = io_file_from_index(&ctx->file_table, idx);
  154. if (file)
  155. get_file(file);
  156. }
  157. io_ring_submit_unlock(ctx, issue_flags);
  158. return file;
  159. }
  160. static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags)
  161. {
  162. struct io_ring_ctx *target_ctx = req->file->private_data;
  163. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  164. struct file *src_file = msg->src_file;
  165. int ret;
  166. if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
  167. return -EAGAIN;
  168. ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd);
  169. if (ret < 0)
  170. goto out_unlock;
  171. msg->src_file = NULL;
  172. req->flags &= ~REQ_F_NEED_CLEANUP;
  173. if (msg->flags & IORING_MSG_RING_CQE_SKIP)
  174. goto out_unlock;
  175. /*
  176. * If this fails, the target still received the file descriptor but
  177. * wasn't notified of the fact. This means that if this request
  178. * completes with -EOVERFLOW, then the sender must ensure that a
  179. * later IORING_OP_MSG_RING delivers the message.
  180. */
  181. if (!io_post_aux_cqe(target_ctx, msg->user_data, ret, 0))
  182. ret = -EOVERFLOW;
  183. out_unlock:
  184. io_double_unlock_ctx(target_ctx);
  185. return ret;
  186. }
  187. static void io_msg_tw_fd_complete(struct callback_head *head)
  188. {
  189. struct io_msg *msg = container_of(head, struct io_msg, tw);
  190. struct io_kiocb *req = cmd_to_io_kiocb(msg);
  191. int ret = -EOWNERDEAD;
  192. if (!(current->flags & PF_EXITING))
  193. ret = io_msg_install_complete(req, IO_URING_F_UNLOCKED);
  194. if (ret < 0)
  195. req_set_fail(req);
  196. io_req_queue_tw_complete(req, ret);
  197. }
  198. static int io_msg_fd_remote(struct io_kiocb *req)
  199. {
  200. struct io_ring_ctx *ctx = req->file->private_data;
  201. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  202. struct task_struct *task = READ_ONCE(ctx->submitter_task);
  203. if (unlikely(!task))
  204. return -EOWNERDEAD;
  205. init_task_work(&msg->tw, io_msg_tw_fd_complete);
  206. if (task_work_add(task, &msg->tw, TWA_SIGNAL))
  207. return -EOWNERDEAD;
  208. return IOU_ISSUE_SKIP_COMPLETE;
  209. }
  210. static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
  211. {
  212. struct io_ring_ctx *target_ctx = req->file->private_data;
  213. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  214. struct io_ring_ctx *ctx = req->ctx;
  215. struct file *src_file = msg->src_file;
  216. if (msg->len)
  217. return -EINVAL;
  218. if (target_ctx == ctx)
  219. return -EINVAL;
  220. if (target_ctx->flags & IORING_SETUP_R_DISABLED)
  221. return -EBADFD;
  222. if (!src_file) {
  223. src_file = io_msg_grab_file(req, issue_flags);
  224. if (!src_file)
  225. return -EBADF;
  226. msg->src_file = src_file;
  227. req->flags |= REQ_F_NEED_CLEANUP;
  228. }
  229. if (io_msg_need_remote(target_ctx))
  230. return io_msg_fd_remote(req);
  231. return io_msg_install_complete(req, issue_flags);
  232. }
  233. int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
  234. {
  235. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  236. if (unlikely(sqe->buf_index || sqe->personality))
  237. return -EINVAL;
  238. msg->src_file = NULL;
  239. msg->user_data = READ_ONCE(sqe->off);
  240. msg->len = READ_ONCE(sqe->len);
  241. msg->cmd = READ_ONCE(sqe->addr);
  242. msg->src_fd = READ_ONCE(sqe->addr3);
  243. msg->dst_fd = READ_ONCE(sqe->file_index);
  244. msg->flags = READ_ONCE(sqe->msg_ring_flags);
  245. if (msg->flags & ~IORING_MSG_RING_MASK)
  246. return -EINVAL;
  247. return 0;
  248. }
  249. int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
  250. {
  251. struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
  252. int ret;
  253. ret = -EBADFD;
  254. if (!io_is_uring_fops(req->file))
  255. goto done;
  256. switch (msg->cmd) {
  257. case IORING_MSG_DATA:
  258. ret = io_msg_ring_data(req, issue_flags);
  259. break;
  260. case IORING_MSG_SEND_FD:
  261. ret = io_msg_send_fd(req, issue_flags);
  262. break;
  263. default:
  264. ret = -EINVAL;
  265. break;
  266. }
  267. done:
  268. if (ret < 0) {
  269. if (ret == -EAGAIN || ret == IOU_ISSUE_SKIP_COMPLETE)
  270. return ret;
  271. req_set_fail(req);
  272. }
  273. io_req_set_res(req, ret, 0);
  274. return IOU_OK;
  275. }
  276. void io_msg_cache_free(const void *entry)
  277. {
  278. struct io_kiocb *req = (struct io_kiocb *) entry;
  279. kmem_cache_free(req_cachep, req);
  280. }