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