io_uring: cancelable uring_cmd
Subsystems (2)
PATCH 1/1 io_uring: cancelable uring_cmd
uring_cmd may never complete, such as ublk, in which uring cmd isn't completed until one new block request is coming from ublk block device.
Add cancelable uring_cmd to provide mechanism to driver to cancel pending commands in its own way.
Add API of io_uring_cmd_mark_cancelable() for driver to mark one command as cancelable, then io_uring will cancel this command in io_uring_cancel_generic(). Driver callback is provided for canceling command in driver's way, meantime driver gets notified with exiting of io_uring task or context.
---
ublk patches:
https://github.com/ming1/linux/commits/uring_exit_and_ublk
include/linux/io_uring.h | 22 +++++++++++++++++- include/linux/io_uring_types.h | 6 +++++ include/uapi/linux/io_uring.h | 7 ++++-- io_uring/io_uring.c | 30 ++++++++++++++++++++++++ io_uring/uring_cmd.c | 42 ++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-)
include/linux/io_uring.h +21 -1
@@ -22,6 +22,9 @@ enum io_uring_cmd_flags { IO_URING_F_IOPOLL = (1 << 10), }; +typedef void (uring_cmd_cancel_fn )(struct io_uring_cmd *, +unsigned int issue_flags,struct task_struct *task); +struct io_uring_cmd {struct file *file;const struct io_uring_sqe *sqe;
@@ -33,7 +36,17 @@ struct io_uring_cmd { };u32 cmd_op;u32 flags; -u8 pdu[32 ];/* available inline for free use */ + +/* less than 32 is available for cancelable cmd */ +union { +u8 pdu[32 ];/* available inline for free use */ + +struct { +/* available inline for free use */ +u8 __pdu[32 -sizeof (uring_cmd_cancel_fn *)]; +uring_cmd_cancel_fn *cancel_fn; + }; + }; };static inline const void *io_uring_sqe_cmd (const struct io_uring_sqe *sqe)
@@ -82,6 +95,8 @@ static inline void io_uring_free(struct task_struct *tsk) __io_uring_free(tsk); }int io_uring_cmd_sock (struct io_uring_cmd *cmd,unsigned int issue_flags); +int io_uring_cmd_mark_cancelable (struct io_uring_cmd *cmd, +unsigned int issue_flags,uring_cmd_cancel_fn *fn);#else static inline int io_uring_cmd_import_fixed (u64 ubuf,unsigned long len,int rw,struct iov_iter *iter,void *ioucmd)
@@ -122,6 +137,11 @@ static inline int io_uring_cmd_sock(struct io_uring_cmd *cmd, {return -EOPNOTSUPP ; } +static inline int io_uring_cmd_mark_cancelable (struct io_uring_cmd *cmd, +unsigned int issue_flags,uring_cmd_cancel_fn *fn) +{ +return -EOPNOTSUPP ; +}#endif #endif
include/linux/io_uring_types.h +6 -0
@@ -265,6 +265,12 @@ struct io_ring_ctx {*/ struct io_wq_work_list iopoll_list;bool poll_multi_queue; + +/* +* Any cancelable uring_cmd is added to this list in +* ->uring_cmd() by io_uring_cmd_insert_cancelable() +*/ +struct hlist_head cancelable_uring_cmd; }____cacheline_aligned_in_smp ;struct {
include/uapi/linux/io_uring.h +5 -2
@@ -249,10 +249,13 @@ enum io_uring_op {* sqe->uring_cmd_flags * IORING_URING_CMD_FIXED use registered buffer; pass this flag * along with setting sqe->buf_index. +* IORING_URING_CANCELABLE not for userspace * IORING_URING_CMD_POLLED driver use only */ -#define IORING_URING_CMD_FIXED (1U << 0) -#define IORING_URING_CMD_POLLED (1U << 31) +#define IORING_URING_CMD_FIXED (1U << 0) +/* set by driver, and handled by io_uring to cancel this cmd */ +#define IORING_URING_CMD_CANCELABLE (1U << 30) +#define IORING_URING_CMD_POLLED (1U << 31)/*
io_uring/io_uring.c +30 -0
@@ -3256,6 +3256,35 @@ static __cold bool io_uring_try_cancel_iowq(struct io_ring_ctx *ctx)return ret; } +static bool io_uring_try_cancel_uring_cmd (struct io_ring_ctx *ctx, +struct task_struct *task, +bool cancel_all) +{ +struct hlist_node *tmp; +struct io_kiocb *req; +bool ret = false; + +mutex_lock (&ctx->uring_lock); +hlist_for_each_entry_safe (req, tmp, &ctx->cancelable_uring_cmd, + hash_node) { +struct io_uring_cmd *cmd =io_kiocb_to_cmd (req, + struct io_uring_cmd); + +if (!cancel_all && req->task != task) +continue ; + +/* safe to call ->cancel_fn() since cmd isn't done yet */ +if (cmd->flags &IORING_URING_CMD_CANCELABLE ) { + cmd->cancel_fn (cmd,0 , task); + ret = true; + } + } +io_submit_flush_completions (ctx); +mutex_unlock (&ctx->uring_lock); + +return ret; +} +static __cold bool io_uring_try_cancel_requests (struct io_ring_ctx *ctx,struct task_struct *task,bool cancel_all)
@@ -3307,6 +3336,7 @@ static __cold bool io_uring_try_cancel_requests(struct io_ring_ctx *ctx, ret |=io_kill_timeouts (ctx, task, cancel_all);if (task) ret |=io_run_task_work () >0 ; + ret |=io_uring_try_cancel_uring_cmd (ctx, task, cancel_all);return ret; }
io_uring/uring_cmd.c +42 -0
54 lines not on this page · raw message
Signature · 1 line
2.40.1
Quoted 32 lines · Ming Lei
uring_cmdmay never complete, such as ublk, in which uring cmd isn't completed until one new block request is coming from ublk block device.Add cancelable
uring_cmdto provide mechanism to driver to cancel pending commands in its own way.Add API of
io_uring_cmd_mark_cancelable()for driver to mark one command as cancelable, thenio_uringwill cancel this command inio_uring_cancel_generic(). Driver callback is provided for canceling command in driver's way, meantime driver gets notified with exiting ofio_uringtask or context.Suggested-by Jens Axboe <[email protected]>Signed-off-by Ming Lei <[email protected]>---
ublk patches:
https://github.com/ming1/linux/commits/uring_exit_and_ublk
include/linux/io_uring.h | 22 +++++++++++++++++- include/linux/io_uring_types.h | 6 +++++ include/uapi/linux/io_uring.h | 7 ++++-- io_uring/io_uring.c | 30 ++++++++++++++++++++++++ io_uring/uring_cmd.c | 42 ++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-)include/linux/io_uring.h +0
-0
include/linux/io_uring.h enum io_uring_cmd_flags { · line 27
Quoted 6 lines
include/linux/io_uring.h +3
-0@@ -22,6 +22,9 @@ enum io_uring_cmd_flags { IO_URING_F_IOPOLL = (1 << 10), }; +typedef void (uring_cmd_cancel_fn )(struct io_uring_cmd *, +unsigned int issue_flags,struct task_struct *task); +
Hi Ming,
I wonder if uring_cmd_cancel shouldn't just be a new file operation, pairing with f_op->uring_cmd. it would, of course, also mean don't need to pass it here occupying the pdu or explicitly registering it. iiuc, would also allow you to drop the flag and just assume it is cancelable if the operation exists, further simplifying the code.
Quoted 10 lines
include/linux/io_uring.h +10
-0+static bool io_uring_try_cancel_uring_cmd (struct io_ring_ctx *ctx, +struct task_struct *task, +bool cancel_all) +{ +struct hlist_node *tmp; +struct io_kiocb *req; +bool ret = false; + +mutex_lock (&ctx->uring_lock); + hlist_for_each_entry_safe(req, tmp, &ctx->cancelable_uring_cmd,
include/linux/io_uring.h enum io_uring_cmd_flags {
Quoted 10 lines
include/linux/io_uring.h +10
-0+ hash_node) { +struct io_uring_cmd *cmd =io_kiocb_to_cmd (req, + struct io_uring_cmd); + +if (!cancel_all && req->task != task) +continue ; + +/* safe to call ->cancel_fn() since cmd isn't done yet */ +if (cmd->flags &IORING_URING_CMD_CANCELABLE ) { + cmd->cancel_fn (cmd,0 , task);
I find it weird to pass task here. Also, it seems you use it only to sanity check it is the same as ubq->ubq_daemon. Can you just recover it from cmd_to_io_kiocb(cmd)->task? it should be guaranteed to be the same as task by the check immediately before.
Thanks,
Signature · 1 line
Gabriel Krisman Bertazi
Hello Gabriel,
Quoted 43 lines · Gabriel Krisman Bertazi
Quoted 32 lines · Ming Lei
uring_cmdmay never complete, such as ublk, in which uring cmd isn't completed until one new block request is coming from ublk block device.Add cancelable
uring_cmdto provide mechanism to driver to cancel pending commands in its own way.Add API of
io_uring_cmd_mark_cancelable()for driver to mark one command as cancelable, thenio_uringwill cancel this command inio_uring_cancel_generic(). Driver callback is provided for canceling command in driver's way, meantime driver gets notified with exiting ofio_uringtask or context.Suggested-by Jens Axboe <[email protected]>Signed-off-by Ming Lei <[email protected]>---
ublk patches:
https://github.com/ming1/linux/commits/uring_exit_and_ublk
include/linux/io_uring.h | 22 +++++++++++++++++- include/linux/io_uring_types.h | 6 +++++ include/uapi/linux/io_uring.h | 7 ++++-- io_uring/io_uring.c | 30 ++++++++++++++++++++++++ io_uring/uring_cmd.c | 42 ++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-)include/linux/io_uring.h +0
-0include/linux/io_uring.h enum io_uring_cmd_flags { · line 27
Quoted 6 lines
include/linux/io_uring.h +3
-0@@ -22,6 +22,9 @@ enum io_uring_cmd_flags { IO_URING_F_IOPOLL = (1 << 10), }; +typedef void (uring_cmd_cancel_fn )(struct io_uring_cmd *, +unsigned int issue_flags,struct task_struct *task); +Hi Ming,
Quoted 5 lines
I wonder if
uring_cmd_cancelshouldn't just be a new file operation, pairing withf_op->uring_cmd. it would, of course, also mean don't need to pass it here occupying the pdu or explicitly registering it. iiuc, would also allow you to drop the flag and just assume it is cancelable if the operation exists, further simplifying the code.
If there are more such use cases, it is probably not a bad idea to add new operation for canceling command.
But definitely there are not, so not good to add one new operation now, since new operation field is added to all drivers/FSs actually, 99% of them shouldn't pay for such cost.
Also I don't see how much simplification is made with new operation, cause the approach in this patch just needs one flag & callback for canceling, both are freely available, only driver with such feature needs to pay the extra callback cost(8bytes in uring_cmd->pdu[]).
Quoted 18 lines
Quoted 18 lines
include/linux/io_uring.h +17
-0+static bool io_uring_try_cancel_uring_cmd (struct io_ring_ctx *ctx, +struct task_struct *task, +bool cancel_all) +{ +struct hlist_node *tmp; +struct io_kiocb *req; +bool ret = false; + +mutex_lock (&ctx->uring_lock); +hlist_for_each_entry_safe (req, tmp, &ctx->cancelable_uring_cmd, + hash_node) { +struct io_uring_cmd *cmd =io_kiocb_to_cmd (req, + struct io_uring_cmd); + +if (!cancel_all && req->task != task) +continue ; +
Quoted 8 lines
include/linux/io_uring.h
Quoted 3 lines
include/linux/io_uring.h +3
-0+/* safe to call ->cancel_fn() since cmd isn't done yet */ +if (cmd->flags &IORING_URING_CMD_CANCELABLE ) { + cmd->cancel_fn (cmd,0 , task);I find it weird to pass task here. Also, it seems you use it only to sanity check it is the same as
ubq->ubq_daemon. Can you just recover it from cmd_to_io_kiocb(cmd)->task? it should be guaranteed to be the same as task by the check immediately before.
'task' parameter is very important for ublk use case, in future I plan to support multiple tasks per queue(io_uring_ctx) for ublk queue for relaxing the current (more stict) limit of single task/context for single queue. So when one task is exiting, ublk driver will just need to cancel commands queued from this task, not necessarily to cancel the whole queue/device.
Also cmd_to_io_kiocb(cmd)->task shouldn't work since io_kiocb can be thought as being not exported to driver strictly speaking.
Thanks,
Ming
Quoted 55 lines · Ming Lei
Hello Gabriel,
Quoted 43 lines · Gabriel Krisman Bertazi
Quoted 32 lines · Ming Lei
uring_cmdmay never complete, such as ublk, in which uring cmd isn't completed until one new block request is coming from ublk block device.Add cancelable
uring_cmdto provide mechanism to driver to cancel pending commands in its own way.Add API of
io_uring_cmd_mark_cancelable()for driver to mark one command as cancelable, thenio_uringwill cancel this command inio_uring_cancel_generic(). Driver callback is provided for canceling command in driver's way, meantime driver gets notified with exiting ofio_uringtask or context.Suggested-by Jens Axboe <[email protected]>Signed-off-by Ming Lei <[email protected]>---
ublk patches:
https://github.com/ming1/linux/commits/uring_exit_and_ublk
include/linux/io_uring.h | 22 +++++++++++++++++- include/linux/io_uring_types.h | 6 +++++ include/uapi/linux/io_uring.h | 7 ++++-- io_uring/io_uring.c | 30 ++++++++++++++++++++++++ io_uring/uring_cmd.c | 42 ++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 3 deletions(-)include/linux/io_uring.h +0
-0include/linux/io_uring.h enum io_uring_cmd_flags { · line 27
Quoted 6 lines
include/linux/io_uring.h +3
-0@@ -22,6 +22,9 @@ enum io_uring_cmd_flags { IO_URING_F_IOPOLL = (1 << 10), }; +typedef void (uring_cmd_cancel_fn )(struct io_uring_cmd *, +unsigned int issue_flags,struct task_struct *task); +Hi Ming,
Quoted 5 lines
I wonder if
uring_cmd_cancelshouldn't just be a new file operation, pairing withf_op->uring_cmd. it would, of course, also mean don't need to pass it here occupying the pdu or explicitly registering it. iiuc, would also allow you to drop the flag and just assume it is cancelable if the operation exists, further simplifying the code.If there are more such use cases, it is probably not a bad idea to add new operation for canceling command.
Quoted 8 lines
But definitely there are not, so not good to add one new operation now, since new operation field is added to all
drivers/FSsactually, 99% of them shouldn't pay for such cost.Also I don't see how much simplification is made with new operation, cause the approach in this patch just needs one flag & callback for canceling, both are freely available, only driver with such feature needs to pay the extra callback
cost(8bytes in uring_cmd->pdu[]).
Another way is to reserve some cmd op range for io_uring use, then we can define CANCEL_CMD and pass it to ->uring_cmd(), such as reserving the following range:
_IOWR(0xFF, 0, 0xFF) ~ _IOWR(0xFF, 0x7F, 0xFF) //user visible _IOWR(0xFF, 0x80, 0xFF) ~ _IOWR(0xFF, 0xFF, 0xFF) //io_uring internal
even SOCKET_URING_OP_SIOCINQ/SOCKET_URING_OP_SIOCOUTQ can be covered since they are just merged to v6.6-rc1, then we have fixed cmd op range for io_uring for future use cases.
Jens & Gabriel, which way do you think better?
Quoted 35 lines
Quoted 19 lines
Quoted 19 lines
include/linux/io_uring.h +17
-0+static bool io_uring_try_cancel_uring_cmd (struct io_ring_ctx *ctx, +struct task_struct *task, +bool cancel_all) +{ +struct hlist_node *tmp; +struct io_kiocb *req; +bool ret = false; + +mutex_lock (&ctx->uring_lock); +hlist_for_each_entry_safe (req, tmp, &ctx->cancelable_uring_cmd, + hash_node) { +struct io_uring_cmd *cmd =io_kiocb_to_cmd (req, + struct io_uring_cmd); + +if (!cancel_all && req->task != task) +continue ; +Quoted 8 lines
include/linux/io_uring.h
Quoted 3 lines
include/linux/io_uring.h +3
-0+/* safe to call ->cancel_fn() since cmd isn't done yet */ +if (cmd->flags &IORING_URING_CMD_CANCELABLE ) { + cmd->cancel_fn (cmd,0 , task);I find it weird to pass task here. Also, it seems you use it only to sanity check it is the same as
ubq->ubq_daemon. Can you just recover it from cmd_to_io_kiocb(cmd)->task? it should be guaranteed to be the same as task by the check immediately before.'task' parameter is very important for ublk use case, in future I plan to support multiple tasks per
queue(io_uring_ctx)for ublk queue for relaxing the current (more stict) limit of single task/context for single queue. So when one task is exiting, ublk driver will just need to cancel commands queued from this task, not necessarily to cancel the whole queue/device.
Quoted 2 lines
Also cmd_to_io_kiocb(cmd)->task shouldn't work since
io_kiocbcan be thought as being not exported to driver strictly speaking.
new API of io_uring_cmd_get_task() can be added which should be just used for handling CANCEL_CMD if we take ->uring_cmd().
Thanks,
Ming