GNU Linux-libre 6.1.90-gnu
[releases.git] / io_uring / msg_ring.c
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
9 #include <uapi/linux/io_uring.h>
10
11 #include "io_uring.h"
12 #include "rsrc.h"
13 #include "filetable.h"
14 #include "msg_ring.h"
15
16 struct io_msg {
17         struct file                     *file;
18         struct file                     *src_file;
19         u64 user_data;
20         u32 len;
21         u32 cmd;
22         u32 src_fd;
23         u32 dst_fd;
24         u32 flags;
25 };
26
27 static void io_double_unlock_ctx(struct io_ring_ctx *octx)
28 {
29         mutex_unlock(&octx->uring_lock);
30 }
31
32 static int io_double_lock_ctx(struct io_ring_ctx *octx,
33                               unsigned int issue_flags)
34 {
35         /*
36          * To ensure proper ordering between the two ctxs, we can only
37          * attempt a trylock on the target. If that fails and we already have
38          * the source ctx lock, punt to io-wq.
39          */
40         if (!(issue_flags & IO_URING_F_UNLOCKED)) {
41                 if (!mutex_trylock(&octx->uring_lock))
42                         return -EAGAIN;
43                 return 0;
44         }
45         mutex_lock(&octx->uring_lock);
46         return 0;
47 }
48
49 void io_msg_ring_cleanup(struct io_kiocb *req)
50 {
51         struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
52
53         if (WARN_ON_ONCE(!msg->src_file))
54                 return;
55
56         fput(msg->src_file);
57         msg->src_file = NULL;
58 }
59
60 static int io_msg_ring_data(struct io_kiocb *req, unsigned int issue_flags)
61 {
62         struct io_ring_ctx *target_ctx = req->file->private_data;
63         struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
64         int ret;
65
66         if (msg->src_fd || msg->dst_fd || msg->flags)
67                 return -EINVAL;
68         if (target_ctx->flags & IORING_SETUP_R_DISABLED)
69                 return -EBADFD;
70
71         ret = -EOVERFLOW;
72         if (target_ctx->flags & IORING_SETUP_IOPOLL) {
73                 if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
74                         return -EAGAIN;
75                 if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0, true))
76                         ret = 0;
77                 io_double_unlock_ctx(target_ctx);
78         } else {
79                 if (io_post_aux_cqe(target_ctx, msg->user_data, msg->len, 0, true))
80                         ret = 0;
81         }
82
83         return ret;
84 }
85
86 static struct file *io_msg_grab_file(struct io_kiocb *req, unsigned int issue_flags)
87 {
88         struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
89         struct io_ring_ctx *ctx = req->ctx;
90         struct file *file = NULL;
91         unsigned long file_ptr;
92         int idx = msg->src_fd;
93
94         io_ring_submit_lock(ctx, issue_flags);
95         if (likely(idx < ctx->nr_user_files)) {
96                 idx = array_index_nospec(idx, ctx->nr_user_files);
97                 file_ptr = io_fixed_file_slot(&ctx->file_table, idx)->file_ptr;
98                 file = (struct file *) (file_ptr & FFS_MASK);
99                 if (file)
100                         get_file(file);
101         }
102         io_ring_submit_unlock(ctx, issue_flags);
103         return file;
104 }
105
106 static int io_msg_install_complete(struct io_kiocb *req, unsigned int issue_flags)
107 {
108         struct io_ring_ctx *target_ctx = req->file->private_data;
109         struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
110         struct file *src_file = msg->src_file;
111         int ret;
112
113         if (unlikely(io_double_lock_ctx(target_ctx, issue_flags)))
114                 return -EAGAIN;
115
116         ret = __io_fixed_fd_install(target_ctx, src_file, msg->dst_fd);
117         if (ret < 0)
118                 goto out_unlock;
119
120         msg->src_file = NULL;
121         req->flags &= ~REQ_F_NEED_CLEANUP;
122
123         if (msg->flags & IORING_MSG_RING_CQE_SKIP)
124                 goto out_unlock;
125         /*
126          * If this fails, the target still received the file descriptor but
127          * wasn't notified of the fact. This means that if this request
128          * completes with -EOVERFLOW, then the sender must ensure that a
129          * later IORING_OP_MSG_RING delivers the message.
130          */
131         if (!io_post_aux_cqe(target_ctx, msg->user_data, ret, 0, true))
132                 ret = -EOVERFLOW;
133 out_unlock:
134         io_double_unlock_ctx(target_ctx);
135         return ret;
136 }
137
138 static int io_msg_send_fd(struct io_kiocb *req, unsigned int issue_flags)
139 {
140         struct io_ring_ctx *target_ctx = req->file->private_data;
141         struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
142         struct io_ring_ctx *ctx = req->ctx;
143         struct file *src_file = msg->src_file;
144
145         if (target_ctx == ctx)
146                 return -EINVAL;
147         if (!src_file) {
148                 src_file = io_msg_grab_file(req, issue_flags);
149                 if (!src_file)
150                         return -EBADF;
151                 msg->src_file = src_file;
152                 req->flags |= REQ_F_NEED_CLEANUP;
153         }
154         return io_msg_install_complete(req, issue_flags);
155 }
156
157 int io_msg_ring_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
158 {
159         struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
160
161         if (unlikely(sqe->buf_index || sqe->personality))
162                 return -EINVAL;
163
164         msg->src_file = NULL;
165         msg->user_data = READ_ONCE(sqe->off);
166         msg->len = READ_ONCE(sqe->len);
167         msg->cmd = READ_ONCE(sqe->addr);
168         msg->src_fd = READ_ONCE(sqe->addr3);
169         msg->dst_fd = READ_ONCE(sqe->file_index);
170         msg->flags = READ_ONCE(sqe->msg_ring_flags);
171         if (msg->flags & ~IORING_MSG_RING_CQE_SKIP)
172                 return -EINVAL;
173
174         return 0;
175 }
176
177 int io_msg_ring(struct io_kiocb *req, unsigned int issue_flags)
178 {
179         struct io_msg *msg = io_kiocb_to_cmd(req, struct io_msg);
180         int ret;
181
182         ret = -EBADFD;
183         if (!io_is_uring_fops(req->file))
184                 goto done;
185
186         switch (msg->cmd) {
187         case IORING_MSG_DATA:
188                 ret = io_msg_ring_data(req, issue_flags);
189                 break;
190         case IORING_MSG_SEND_FD:
191                 ret = io_msg_send_fd(req, issue_flags);
192                 break;
193         default:
194                 ret = -EINVAL;
195                 break;
196         }
197
198 done:
199         if (ret == -EAGAIN)
200                 return -EAGAIN;
201         if (ret < 0)
202                 req_set_fail(req);
203         io_req_set_res(req, ret, 0);
204         return IOU_OK;
205 }