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