[RFC PATCH v3 1/3] io_uring: add helper for uring_cmd completion in submitter-task
Kanchan Joshi
joshi.k at samsung.com
Tue Mar 16 14:01:24 GMT 2021
Completion of a uring_cmd ioctl may involve referencing certain
ioctl-specific fields, requiring original subitter context.
Introduce 'uring_cmd_complete_in_task' that driver can use for this
purpose. The API facilitates task-work infra, while driver gets to
implement cmd-specific handling in a callback.
Signed-off-by: Kanchan Joshi <joshi.k at samsung.com>
---
fs/io_uring.c | 36 ++++++++++++++++++++++++++++++++----
include/linux/io_uring.h | 8 ++++++++
2 files changed, 40 insertions(+), 4 deletions(-)
diff --git a/fs/io_uring.c b/fs/io_uring.c
index 583f8fd735d8..ca459ea9cb83 100644
--- a/fs/io_uring.c
+++ b/fs/io_uring.c
@@ -772,9 +772,12 @@ struct io_kiocb {
/* use only after cleaning per-op data, see io_clean_op() */
struct io_completion compl;
};
-
- /* opcode allocated if it needs to store data for async defer */
- void *async_data;
+ union {
+ /* opcode allocated if it needs to store data for async defer */
+ void *async_data;
+ /* used for uring-cmd, when driver needs to update in task */
+ void (*driver_cb)(struct io_uring_cmd *cmd);
+ };
u8 opcode;
/* polled IO has completed */
u8 iopoll_completed;
@@ -1716,7 +1719,7 @@ static void io_dismantle_req(struct io_kiocb *req)
{
io_clean_op(req);
- if (req->async_data)
+ if (io_op_defs[req->opcode].async_size && req->async_data)
kfree(req->async_data);
if (req->file)
io_put_file(req, req->file, (req->flags & REQ_F_FIXED_FILE));
@@ -2032,6 +2035,31 @@ static void io_req_task_submit(struct callback_head *cb)
__io_req_task_submit(req);
}
+static void uring_cmd_work(struct callback_head *cb)
+{
+ struct io_kiocb *req = container_of(cb, struct io_kiocb, task_work);
+ struct io_uring_cmd *cmd = &req->uring_cmd;
+
+ req->driver_cb(cmd);
+}
+int uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
+ void (*driver_cb)(struct io_uring_cmd *))
+{
+ int ret;
+ struct io_kiocb *req = container_of(ioucmd, struct io_kiocb, uring_cmd);
+
+ req->driver_cb = driver_cb;
+ req->task_work.func = uring_cmd_work;
+ ret = io_req_task_work_add(req);
+ if (unlikely(ret)) {
+ req->result = -ECANCELED;
+ percpu_ref_get(&req->ctx->refs);
+ io_req_task_work_add_fallback(req, io_req_task_cancel);
+ }
+ return ret;
+}
+EXPORT_SYMBOL(uring_cmd_complete_in_task);
+
static void io_req_task_queue(struct io_kiocb *req)
{
int ret;
diff --git a/include/linux/io_uring.h b/include/linux/io_uring.h
index e0a31354eff1..559f41d0f19a 100644
--- a/include/linux/io_uring.h
+++ b/include/linux/io_uring.h
@@ -41,6 +41,8 @@ struct io_uring_cmd {
#if defined(CONFIG_IO_URING)
void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret);
+int uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
+ void (*driver_cb)(struct io_uring_cmd *));
struct sock *io_uring_get_socket(struct file *file);
void __io_uring_task_cancel(void);
void __io_uring_files_cancel(struct files_struct *files);
@@ -65,6 +67,12 @@ static inline void io_uring_free(struct task_struct *tsk)
static inline void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret)
{
}
+
+int uring_cmd_complete_in_task(struct io_uring_cmd *ioucmd,
+ void (*driver_cb)(struct io_uring_cmd *))
+{
+ return -1;
+}
static inline struct sock *io_uring_get_socket(struct file *file)
{
return NULL;
--
2.25.1
More information about the Linux-nvme
mailing list