io_uring: optimise io_req_local_work_add
[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);
5756a3a7
KJ
57 if (req->ctx->flags & IORING_SETUP_IOPOLL)
58 /* order with io_iopoll_req_issued() checking ->iopoll_complete */
59 smp_store_release(&req->iopoll_completed, 1);
60 else
9d2789ac 61 io_req_complete_post(req, issue_flags);
99f15d8d
JA
62}
63EXPORT_SYMBOL_GPL(io_uring_cmd_done);
64
65int io_uring_cmd_prep_async(struct io_kiocb *req)
66{
f2ccb5ae 67 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
99f15d8d
JA
68 size_t cmd_size;
69
9c71d39a
SM
70 BUILD_BUG_ON(uring_cmd_pdu_size(0) != 16);
71 BUILD_BUG_ON(uring_cmd_pdu_size(1) != 80);
72
99f15d8d
JA
73 cmd_size = uring_cmd_pdu_size(req->ctx->flags & IORING_SETUP_SQE128);
74
75 memcpy(req->async_data, ioucmd->cmd, cmd_size);
758d5d64 76 ioucmd->cmd = req->async_data;
99f15d8d
JA
77 return 0;
78}
79
80int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
81{
f2ccb5ae 82 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
99f15d8d 83
9cda70f6 84 if (sqe->__pad1)
99f15d8d 85 return -EINVAL;
9cda70f6
AG
86
87 ioucmd->flags = READ_ONCE(sqe->uring_cmd_flags);
88 if (ioucmd->flags & ~IORING_URING_CMD_FIXED)
89 return -EINVAL;
90
91 if (ioucmd->flags & IORING_URING_CMD_FIXED) {
92 struct io_ring_ctx *ctx = req->ctx;
93 u16 index;
94
95 req->buf_index = READ_ONCE(sqe->buf_index);
96 if (unlikely(req->buf_index >= ctx->nr_user_bufs))
97 return -EFAULT;
98 index = array_index_nospec(req->buf_index, ctx->nr_user_bufs);
99 req->imu = ctx->user_bufs[index];
100 io_req_set_rsrc_node(req, ctx, 0);
101 }
99f15d8d
JA
102 ioucmd->cmd = sqe->cmd;
103 ioucmd->cmd_op = READ_ONCE(sqe->cmd_op);
104 return 0;
105}
106
107int io_uring_cmd(struct io_kiocb *req, unsigned int issue_flags)
108{
f2ccb5ae 109 struct io_uring_cmd *ioucmd = io_kiocb_to_cmd(req, struct io_uring_cmd);
99f15d8d
JA
110 struct io_ring_ctx *ctx = req->ctx;
111 struct file *file = req->file;
112 int ret;
113
03b3d6be 114 if (!file->f_op->uring_cmd)
99f15d8d
JA
115 return -EOPNOTSUPP;
116
2a584012
LC
117 ret = security_uring_cmd(ioucmd);
118 if (ret)
119 return ret;
120
99f15d8d
JA
121 if (ctx->flags & IORING_SETUP_SQE128)
122 issue_flags |= IO_URING_F_SQE128;
123 if (ctx->flags & IORING_SETUP_CQE32)
124 issue_flags |= IO_URING_F_CQE32;
5756a3a7 125 if (ctx->flags & IORING_SETUP_IOPOLL) {
03b3d6be
JA
126 if (!file->f_op->uring_cmd_iopoll)
127 return -EOPNOTSUPP;
99f15d8d 128 issue_flags |= IO_URING_F_IOPOLL;
5756a3a7
KJ
129 req->iopoll_completed = 0;
130 WRITE_ONCE(ioucmd->cookie, NULL);
131 }
99f15d8d 132
99f15d8d
JA
133 ret = file->f_op->uring_cmd(ioucmd, issue_flags);
134 if (ret == -EAGAIN) {
135 if (!req_has_async_data(req)) {
136 if (io_alloc_async_data(req))
137 return -ENOMEM;
138 io_uring_cmd_prep_async(req);
139 }
140 return -EAGAIN;
141 }
142
143 if (ret != -EIOCBQUEUED) {
3ed159c9
AG
144 if (ret < 0)
145 req_set_fail(req);
146 io_req_set_res(req, ret, 0);
a9c3eda7 147 return ret;
99f15d8d
JA
148 }
149
150 return IOU_ISSUE_SKIP_COMPLETE;
151}
a9216fac
AG
152
153int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,
154 struct iov_iter *iter, void *ioucmd)
155{
156 struct io_kiocb *req = cmd_to_io_kiocb(ioucmd);
157
158 return io_import_fixed(rw, iter, req->imu, ubuf, len);
159}
160EXPORT_SYMBOL_GPL(io_uring_cmd_import_fixed);