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