Merge tag 'integrity-v6.4' of git://git.kernel.org/pub/scm/linux/kernel/git/zohar...
[linux-block.git] / io_uring / uring_cmd.c
CommitLineData
99f15d8d
JA
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.h>
2a584012 6#include <linux/security.h>
9cda70f6 7#include <linux/nospec.h>
99f15d8d
JA
8
9#include <uapi/linux/io_uring.h>
10
99f15d8d 11#include "io_uring.h"
a9216fac 12#include "rsrc.h"
99f15d8d
JA
13#include "uring_cmd.h"
14
a282967c 15static void io_uring_cmd_work(struct io_kiocb *req, struct io_tw_state *ts)
99f15d8d 16{
f2ccb5ae 17 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
a282967c 18 unsigned issue_flags = ts->locked ? 0 : IO_URING_F_UNLOCKED;
99f15d8d 19
9d2789ac 20 ioucmd->task_work_cb(ioucmd, issue_flags);
99f15d8d
JA
21}
22
23void io_uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
9d2789ac 24 void (*task_work_cb)(struct io_uring_cmd *, unsigned))
99f15d8d
JA
25{
26 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
27
28 ioucmd->task_work_cb = task_work_cb;
29 req->io_task_work.func = io_uring_cmd_work;
30 io_req_task_work_add(req);
31}
32EXPORT_SYMBOL_GPL(io_uring_cmd_complete_in_task);
33
34static inline void io_req_set_cqe32_extra(struct io_kiocb *req,
35 u64 extra1, u64 extra2)
36{
37 req->extra1 = extra1;
38 req->extra2 = extra2;
39 req->flags |= REQ_F_CQE32_INIT;
40}
41
42/*
43 * Called by consumers of io_uring_cmd, if they originally returned
44 * -EIOCBQUEUED upon receiving the command.
45 */
9d2789ac
JA
46void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, ssize_t res2,
47 unsigned issue_flags)
99f15d8d
JA
48{
49 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
50
51 if (ret < 0)
52 req_set_fail(req);
53
ff2557b7 54 io_req_set_res(req, ret, 0);
99f15d8d
JA
55 if (req->ctx->flags & IORING_SETUP_CQE32)
56 io_req_set_cqe32_extra(req, res2, 0);
27a67079 57 if (req->ctx->flags & IORING_SETUP_IOPOLL) {
5756a3a7
KJ
58 /* order with io_iopoll_req_issued() checking ->iopoll_complete */
59 smp_store_release(&req->iopoll_completed, 1);
27a67079
JA
60 } else {
61 struct io_tw_state ts = {
62 .locked = !(issue_flags & IO_URING_F_UNLOCKED),
63 };
64 io_req_task_complete(req, &ts);
65 }
99f15d8d
JA
66}
67EXPORT_SYMBOL_GPL(io_uring_cmd_done);
68
69int io_uring_cmd_prep_async(struct io_kiocb *req)
70{
f2ccb5ae 71 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
99f15d8d
JA
72 size_t cmd_size;
73
9c71d39a
SM
74 BUILD_BUG_ON(uring_cmd_pdu_size(0) != 16);
75 BUILD_BUG_ON(uring_cmd_pdu_size(1) != 80);
76
99f15d8d
JA
77 cmd_size = uring_cmd_pdu_size(req->ctx->flags & IORING_SETUP_SQE128);
78
79 memcpy(req->async_data, ioucmd->cmd, cmd_size);
758d5d64 80 ioucmd->cmd = req->async_data;
99f15d8d
JA
81 return 0;
82}
83
84int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
85{
f2ccb5ae 86 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
99f15d8d 87
9cda70f6 88 if (sqe->__pad1)
99f15d8d 89 return -EINVAL;
9cda70f6
AG
90
91 ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags);
92 if (ioucmd->flags & ~IORING_URING_CMD_FIXED)
93 return -EINVAL;
94
95 if (ioucmd->flags & IORING_URING_CMD_FIXED) {
96 struct io_ring_ctx *ctx = req->ctx;
97 u16 index;
98
99 req->buf_index = READ_ONCE(sqe->buf_index);
100 if (unlikely(req->buf_index >= ctx->nr_user_bufs))
101 return -EFAULT;
102 index = array_index_nospec(req->buf_index, ctx->nr_user_bufs);
103 req->imu = ctx->user_bufs[index];
104 io_req_set_rsrc_node(req, ctx, 0);
105 }
99f15d8d
JA
106 ioucmd->cmd = sqe->cmd;
107 ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
108 return 0;
109}
110
111int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
112{
f2ccb5ae 113 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
99f15d8d
JA
114 struct io_ring_ctx *ctx = req->ctx;
115 struct file *file = req->file;
116 int ret;
117
03b3d6be 118 if (!file->f_op->uring_cmd)
99f15d8d
JA
119 return -EOPNOTSUPP;
120
2a584012
LC
121 ret = security_uring_cmd(ioucmd);
122 if (ret)
123 return ret;
124
99f15d8d
JA
125 if (ctx->flags & IORING_SETUP_SQE128)
126 issue_flags |= IO_URING_F_SQE128;
127 if (ctx->flags & IORING_SETUP_CQE32)
128 issue_flags |= IO_URING_F_CQE32;
5756a3a7 129 if (ctx->flags & IORING_SETUP_IOPOLL) {
03b3d6be
JA
130 if (!file->f_op->uring_cmd_iopoll)
131 return -EOPNOTSUPP;
99f15d8d 132 issue_flags |= IO_URING_F_IOPOLL;
5756a3a7
KJ
133 req->iopoll_completed = 0;
134 WRITE_ONCE(ioucmd->cookie, NULL);
135 }
99f15d8d 136
99f15d8d
JA
137 ret = file->f_op->uring_cmd(ioucmd, issue_flags);
138 if (ret == -EAGAIN) {
139 if (!req_has_async_data(req)) {
140 if (io_alloc_async_data(req))
141 return -ENOMEM;
142 io_uring_cmd_prep_async(req);
143 }
144 return -EAGAIN;
145 }
146
147 if (ret != -EIOCBQUEUED) {
3ed159c9
AG
148 if (ret < 0)
149 req_set_fail(req);
150 io_req_set_res(req, ret, 0);
a9c3eda7 151 return ret;
99f15d8d
JA
152 }
153
154 return IOU_ISSUE_SKIP_COMPLETE;
155}
a9216fac
AG
156
157int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
158 struct iov_iter *iter, void *ioucmd)
159{
160 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
161
162 return io_import_fixed(rw, iter, req->imu, ubuf, len);
163}
164EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed);