io_uring/ublk: exit notifier support
Subsystems (3)
cover io_uring/ublk: exit notifier support
Hello,
In do_exit(), io_uring needs to wait pending requests.
ublk is one uring_cmd driver, and its usage is a bit special by submitting command for waiting incoming block IO request in advance, so if there isn't any IO request coming, the command can't be completed. So far ublk driver has to bind its queue with one ublk daemon server, meantime starts one monitor work to check if this daemon is live periodically. This way requires ublk queue to be bound one single daemon pthread, and not flexible, meantime the monitor work is run in 3rd context, and the implementation is a bit tricky.
The 1st 3 patches adds io_uring task exit notifier, and the other patches converts ublk into this exit notifier, and the implementation becomes more robust & readable, meantime it becomes easier to relax the ublk queue/daemon limit in future, such as not require to bind ublk queue with single daemon.
Ming Lei (10):
io_uring: allocate ctx id and build map between id and ctx
io_uring: pass io_uring_ctx->id to uring_cmd
io_uring: support io_uring notifier for uring_cmd
ublk: don't get ublk device reference in ublk_abort_queue()
ublk: make sure ublk uring cmd handling is done in submitter task
context
ublk: make sure that uring cmd aiming at same queue won't cross
io_uring contexts
ublk: rename mm_lock as lock
ublk: quiesce request queue when aborting queue
ublk: replace monitor work with uring_cmd exit notifier
ublk: simplify aborting request
drivers/block/ublk_drv.c | 216 +++++++++++++++++++-------------- include/linux/io_uring.h | 27 ++++- include/linux/io_uring_types.h | 3 + io_uring/io_uring.c | 57 +++++++++ io_uring/io_uring.h | 4 + io_uring/uring_cmd.c | 13 ++ 6 files changed, 230 insertions(+), 90 deletions(-)
Signature · 1 line
2.40.1
PATCH 1/10 io_uring: allocate ctx id and build map between id and ctx
Prepare for supporting to notify uring_cmd driver when ctx/io_uring_task is going away.
Notifier callback will be registered by driver to get notified, so that driver can cancel in-flight command which may depend on the io task.
For driver to check if the ctx is matched with uring_cmd, allocate/provide ctx id to the callback, so we can avoid to expose the whole ctx instance.
The global xarray of ctx_ids is added for holding the mapping and allocating unique id for each ctx.
--- include/linux/io_uring.h | 2 ++ include/linux/io_uring_types.h | 3 +++ io_uring/io_uring.c | 9 +++++++++ 3 files changed, 14 insertions(+)
include/linux/io_uring.h +2 -0
@@ -41,6 +41,8 @@ static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)return sqe->cmd; } +#define IO_URING_INVALID_CTX_ID UINT_MAX +#if defined(CONFIG_IO_URING )int io_uring_cmd_import_fixed (u64 ubuf,unsigned long len,int rw,struct iov_iter *iter,void *ioucmd);
include/linux/io_uring_types.h +3 -0
@@ -215,6 +215,9 @@ struct io_ring_ctx {struct percpu_ref refs;enum task_work_notify_mode notify_method; + +/* for uring cmd driver to retrieve context */ +unsigned int id; }____cacheline_aligned_in_smp ;/* submission data */
io_uring/io_uring.c +9 -0
@@ -175,6 +175,9 @@ static struct ctl_table kernel_io_uring_disabled_table[] = { };#endif +/* mapping between io_ring_ctx instance and its ctx_id */ +static DEFINE_XARRAY_FLAGS(ctx_ids, XA_FLAGS_ALLOC); +struct sock *io_uring_get_socket (struct file *file) {#if defined(CONFIG_UNIX )
@@ -303,6 +306,10 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)xa_init (&ctx->io_bl_xa); + ctx->id =IO_URING_INVALID_CTX_ID ; +if (xa_alloc (&ctx_ids, &ctx->id, ctx, xa_limit_31b,GFP_KERNEL )) + goto err; +/* * Use 5 bits less than the max cq entries, that should give us around * 32 entries per hash list if totally full and uniformly spread, but
@@ -356,6 +363,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p)kfree (ctx->cancel_table_locked.hbs);kfree (ctx->io_bl);xa_destroy (&ctx->io_bl_xa); +xa_erase (&ctx_ids, ctx->id);kfree (ctx);return NULL ; }
@@ -2929,6 +2937,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)kfree (ctx->cancel_table_locked.hbs);kfree (ctx->io_bl);xa_destroy (&ctx->io_bl_xa); +xa_erase (&ctx_ids, ctx->id);kfree (ctx); }
Signature · 1 line
2.40.1
PATCH 2/10 io_uring: pass io_uring_ctx->id to uring_cmd
In reply to Ming LeiPass io_uring_ctx-id to uring_cmd driver, and prepare for supporting io_uring ctx or task exit notifier.
--- include/linux/io_uring.h | 6 +++++- io_uring/uring_cmd.c | 1 + 2 files changed, 6 insertions(+), 1 deletion(-)
include/linux/io_uring.h +5 -1
@@ -33,7 +33,11 @@ struct io_uring_cmd { };u32 cmd_op;u32 flags; -u8 pdu[32 ];/* available inline for free use */ +union { +/* driver needs to save ctx_id */ +u32 ctx_id; +u8 pdu[32 ];/* available inline for free use */ + }; };static inline const void *io_uring_sqe_cmd (const struct io_uring_sqe *sqe)
io_uring/uring_cmd.c +1 -0
@@ -105,6 +105,7 @@ int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) req->imu = ctx->user_bufs[index];io_req_set_rsrc_node (req, ctx,0 ); } + ioucmd->ctx_id = req->ctx->id; ioucmd->sqe = sqe; ioucmd->cmd_op =READ_ONCE (sqe->cmd_op);return 0 ;
Signature · 1 line
2.40.1
PATCH 3/10 io_uring: support io_uring notifier for uring_cmd
In reply to Ming LeiNotifier callback is registered by driver to get notified, so far only for uring_cmd based driver.
With this notifier, driver can cancel in-flight command when ctx is being released or io task is exiting.
The main use case is ublk(or probably fuse with uring cmd support) in which uring command may never complete, so driver has to cancel this command when io task is exiting or ctx is releasing, otherwise __io_uring_cancel() may wait forever because of inflight commands.
--- include/linux/io_uring.h | 19 ++++++++++++++++ io_uring/io_uring.c | 48 ++++++++++++++++++++++++++++++++++++++++ io_uring/io_uring.h | 4 ++++ io_uring/uring_cmd.c | 12 ++++++++++ 4 files changed, 83 insertions(+)
include/linux/io_uring.h +19 -0
@@ -47,7 +47,19 @@ static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)#define IO_URING_INVALID_CTX_ID UINT_MAX +enum io_uring_notifier { +IO_URING_NOTIFIER_CTX_EXIT , +IO_URING_NOTIFIER_IO_TASK_EXIT , +}; + +struct io_uring_notifier_data { +unsigned int ctx_id; +const struct task_struct *task; +}; +#if defined(CONFIG_IO_URING ) +int io_uring_cmd_register_notifier (struct notifier_block *nb); +void io_uring_cmd_unregister_notifier (struct notifier_block *nb);int io_uring_cmd_import_fixed (u64 ubuf,unsigned long len,int rw,struct iov_iter *iter,void *ioucmd); void io_uring_cmd_done(struct io_uring_cmd *cmd, ssize_t ret, ssize_t res2,
@@ -89,6 +101,13 @@ static inline void io_uring_free(struct task_struct *tsk) }int io_uring_cmd_sock (struct io_uring_cmd *cmd,unsigned int issue_flags);#else +static inline int io_uring_cmd_register_notifier (struct notifier_block *nb) +{ +return 0 ; +} +static inline void io_uring_cmd_unregister_notifier (struct notifier_block *nb) +{ +}static inline int io_uring_cmd_import_fixed (u64 ubuf,unsigned long len,int rw,struct iov_iter *iter,void *ioucmd) {
io_uring/io_uring.c +48 -0
90 lines not on this page · raw message
io_uring/io_uring.h +4 -0
@@ -38,6 +38,10 @@ enum { IOU_STOP_MULTISHOT = -ECANCELED, }; +struct io_ring_ctx *io_uring_id_to_ctx (unsigned int id); +int io_uring_register_notifier (struct notifier_block *nb); +void io_uring_unregister_notifier (struct notifier_block *nb); +bool io_cqe_cache_refill (struct io_ring_ctx *ctx,bool overflow);void io_req_cqe_overflow (struct io_kiocb *req);int io_run_task_work_sig (struct io_ring_ctx *ctx);
io_uring/uring_cmd.c +12 -0
@@ -192,3 +192,15 @@ int io_uring_cmd_sock(struct io_uring_cmd *cmd, unsigned int issue_flags) } }EXPORT_SYMBOL_GPL (io_uring_cmd_sock); + +int io_uring_cmd_register_notifier (struct notifier_block *nb) +{ +return io_uring_register_notifier (nb); +} +EXPORT_SYMBOL_GPL (io_uring_cmd_register_notifier); + +void io_uring_cmd_unregister_notifier (struct notifier_block *nb) +{ +io_uring_unregister_notifier (nb); +} +EXPORT_SYMBOL_GPL (io_uring_cmd_unregister_notifier);
Signature · 1 line
2.40.1
PATCH 4/10 ublk: don't get ublk device reference in ublk_abort_queue()
In reply to Ming Leiublk_abort_queue() is called in ublk_daemon_monitor_work(), in which it is guaranteed that ublk device is live, so no need to get the device reference.
--- drivers/block/ublk_drv.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/block/ublk_drv.c +0 -4
@@ -1419,9 +1419,6 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq) {int i; -if (!ublk_get_device (ub)) -return ; -for (i =0 ; i < ubq->q_depth; i++) {struct ublk_io *io = &ubq->ios[i];
@@ -1437,7 +1434,6 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)__ublk_fail_req (ubq, io, rq); } } -ublk_put_device (ub); }static void ublk_daemon_monitor_work (struct work_struct *work)
Signature · 1 line
2.40.1
PATCH 5/10 ublk: make sure ublk uring cmd handling is done in submitter task context
In reply to Ming LeiIn well-done ublk server implementation, ublk io command won't be linked into any link chain. Meantime we are always handled in no-wait style, so basically ublk uring cmd handling is always done in submitter task context.
However, the server may set IOSQE_ASYNC, or io command is linked to one chain mistakenly, then we may still run into io-wq context and ctx->uring_lock isn't held.
So in case of IO_URING_F_UNLOCKED, schedule this command by io_uring_cmd_complete_in_task to run it in submitter task. Then ublk_ch_uring_cmd_local() is always called with context uring_lock held, and we needn't to worry about sync among submission code path.
--- drivers/block/ublk_drv.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
drivers/block/ublk_drv.c +21 -1
@@ -1810,7 +1810,8 @@ static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub,return NULL ; } -static int ublk_ch_uring_cmd (struct io_uring_cmd *cmd,unsigned int issue_flags) +static inline int ublk_ch_uring_cmd_local (struct io_uring_cmd *cmd, +unsigned int issue_flags) {/* * Not necessary for async retry, but let's keep it simple and always
@@ -1824,9 +1825,28 @@ static int ublk_ch_uring_cmd(struct io_uring_cmd *cmd, unsigned int issue_flags) .addr = READ_ONCE(ub_src->addr) }; +WARN_ON_ONCE (issue_flags &IO_URING_F_UNLOCKED ); +return __ublk_ch_uring_cmd (cmd, issue_flags, &ub_cmd); } +static void ublk_ch_uring_cmd_cb (struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +ublk_ch_uring_cmd_local (cmd, issue_flags); +} + +static int ublk_ch_uring_cmd (struct io_uring_cmd *cmd,unsigned int issue_flags) +{ +/* well-implemented server won't run into unlocked */ +if (unlikely (issue_flags &IO_URING_F_UNLOCKED )) { +io_uring_cmd_complete_in_task (cmd, ublk_ch_uring_cmd_cb); +return -EIOCBQUEUED ; + } + +return ublk_ch_uring_cmd_local (cmd, issue_flags); +} +static inline bool ublk_check_ubuf_dir (const struct request *req,int ubuf_dir) {
Signature · 1 line
2.40.1
PATCH 6/10 ublk: make sure that uring cmd aiming at same queue won't cross io_uring contexts
In reply to Ming LeiMake sure that all commands aiming at same ublk queue are from same io_uring context. This way is one very reasonable requirement, and not see any reason userspace may send uring cmd to same queue by multiple io_uring contexts.
--- drivers/block/ublk_drv.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-)
drivers/block/ublk_drv.c +16 -2
@@ -131,6 +131,7 @@ struct ublk_queue {unsigned long flags;struct task_struct *ubq_daemon;char *io_cmd_buf; +unsigned int ctx_id;struct llist_head io_cmds;
@@ -1410,6 +1411,11 @@ static void ublk_commit_completion(struct ublk_device *ub,ublk_put_req_ref (ubq, req); } +static inline bool ublk_ctx_id_is_valid (unsigned int ctx_id) +{ +return ctx_id !=IO_URING_INVALID_CTX_ID ; +} +/* * When ->ubq_daemon is exiting, either new request is ended immediately, * or any queued io command is drained, so it is safe to abort queue
@@ -1609,11 +1615,13 @@ static void ublk_stop_dev(struct ublk_device *ub) }/* device can only be started after all IOs are ready */ -static void ublk_mark_io_ready (struct ublk_device *ub,struct ublk_queue *ubq) +static void ublk_mark_io_ready (struct ublk_device *ub,struct ublk_queue *ubq, +unsigned int ctx_id) {mutex_lock (&ub->mutex); ubq->nr_io_ready++;if (ublk_queue_ready (ubq)) { + ubq->ctx_id = ctx_id; ubq->ubq_daemon = current;get_task_struct (ubq->ubq_daemon); ub->nr_queues_ready++;
@@ -1682,6 +1690,9 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,if (ubq->ubq_daemon && ubq->ubq_daemon != current) goto out; +if (ublk_ctx_id_is_valid (ubq->ctx_id) && cmd->ctx_id != ubq->ctx_id) + goto out; +if (tag >= ubq->q_depth) goto out;
@@ -1734,7 +1745,7 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, }ublk_fill_io_cmd (io, cmd, ub_cmd->addr); -ublk_mark_io_ready (ub, ubq); +ublk_mark_io_ready (ub, ubq, cmd->ctx_id);break ;case UBLK_IO_COMMIT_AND_FETCH_REQ : req =blk_mq_tag_to_rq (ub->tag_set.tags[ub_cmd->q_id], tag);
@@ -1989,6 +2000,7 @@ static int ublk_init_queue(struct ublk_device *ub, int q_id) ubq->io_cmd_buf = ptr; ubq->dev = ub; + ubq->ctx_id =IO_URING_INVALID_CTX_ID ;return 0 ; }
@@ -2593,6 +2605,8 @@ static void ublk_queue_reinit(struct ublk_device *ub, struct ublk_queue *ubq) ubq->ubq_daemon =NULL ; ubq->timeout = false; + ubq->ctx_id =IO_URING_INVALID_CTX_ID ; +for (i =0 ; i < ubq->q_depth; i++) {struct ublk_io *io = &ubq->ios[i];
Signature · 1 line
2.40.1
PATCH 7/10 ublk: rename mm_lock as lock
In reply to Ming LeiRename the mm_lock field of ublk_device as lock, so that this lock can be reused for protecting access of ub->ub_disk, which will be used for simplifying ublk_abort_queue() by quiesce queue in next patch.
--- drivers/block/ublk_drv.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/block/ublk_drv.c +4 -4
@@ -167,7 +167,7 @@ struct ublk_device {struct mutex mutex; -spinlock_t mm_lock; +spinlock_t lock;struct mm_struct *mm;struct ublk_params params;
@@ -1358,12 +1358,12 @@ static int ublk_ch_mmap(struct file *filp, struct vm_area_struct *vma)unsigned long pfn, end, phys_off = vma->vm_pgoff <<PAGE_SHIFT ;int q_id, ret =0 ; -spin_lock (&ub->mm_lock); +spin_lock (&ub->lock);if (!ub->mm) ub->mm = current->mm;if (current->mm != ub->mm) ret = -EINVAL ; -spin_unlock (&ub->mm_lock); +spin_unlock (&ub->lock);if (ret)return ret;
@@ -2348,7 +2348,7 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)if (!ub) goto out_unlock;mutex_init (&ub->mutex); -spin_lock_init (&ub->mm_lock); +spin_lock_init (&ub->lock);INIT_WORK (&ub->quiesce_work, ublk_quiesce_work_fn);INIT_WORK (&ub->stop_work, ublk_stop_work_fn);INIT_DELAYED_WORK (&ub->monitor_work, ublk_daemon_monitor_work);
Signature · 1 line
2.40.1
PATCH 8/10 ublk: quiesce request queue when aborting queue
In reply to Ming LeiSo far aborting queue ends request when the ubq daemon is exiting, and it can be run concurrently with ublk_queue_rq(), this way is fragile and we depend on the tricky usage of UBLK_IO_FLAG_ABORTED for avoiding such race.
Quiesce queue when aborting queue, and the two code paths can be run completely exclusively, then it becomes easier to add new ublk feature, such as relaxing single same task limit for each queue.
--- drivers/block/ublk_drv.c | 43 ++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-)
drivers/block/ublk_drv.c +37 -6
@@ -1446,21 +1446,45 @@ static void ublk_daemon_monitor_work(struct work_struct *work) {struct ublk_device *ub = container_of(work, struct ublk_device, monitor_work.work); +struct gendisk *disk;int i;for (i =0 ; i < ub->dev_info.nr_hw_queues; i++) {struct ublk_queue *ubq =ublk_get_queue (ub, i); -if (ubq_daemon_is_dying (ubq)) { -if (ublk_queue_can_use_recovery (ubq)) -schedule_work (&ub->quiesce_work); -else -schedule_work (&ub->stop_work); +if (ubq_daemon_is_dying (ubq)) + goto found; + } +return ; + +found: +spin_lock (&ub->lock); + disk = ub->ub_disk; +if (disk) +get_device (disk_to_dev (disk)); +spin_unlock (&ub->lock); + +/* Our disk has been dead */ +if (!disk) +return ; + +/* Now we are serialized with ublk_queue_rq() */ +blk_mq_quiesce_queue (disk->queue); +for (i =0 ; i < ub->dev_info.nr_hw_queues; i++) { +struct ublk_queue *ubq =ublk_get_queue (ub, i); +if (ubq_daemon_is_dying (ubq)) {/* abort queue is for making forward progress */ ublk_abort_queue (ub, ubq); } } +blk_mq_unquiesce_queue (disk->queue); +put_device (disk_to_dev (disk)); + +if (ublk_can_use_recovery (ub)) +schedule_work (&ub->quiesce_work); +else +schedule_work (&ub->stop_work);/* * We can't schedule monitor work after ub's state is not UBLK_S_DEV_LIVE.
@@ -1595,6 +1619,8 @@ static void ublk_unquiesce_dev(struct ublk_device *ub)static void ublk_stop_dev (struct ublk_device *ub) { +struct gendisk *disk; +mutex_lock (&ub->mutex);if (ub->dev_info.state ==UBLK_S_DEV_DEAD ) goto unlock;
@@ -1604,10 +1630,15 @@ static void ublk_stop_dev(struct ublk_device *ub)ublk_unquiesce_dev (ub); }del_gendisk (ub->ub_disk); + +/* Sync with ublk_abort_queue() by holding the lock */ +spin_lock (&ub->lock); + disk = ub->ub_disk; ub->dev_info.state =UBLK_S_DEV_DEAD ; ub->dev_info.ublksrv_pid =-1 ; -put_disk (ub->ub_disk); ub->ub_disk =NULL ; +spin_unlock (&ub->lock); +put_disk (disk); unlock:ublk_cancel_dev (ub);mutex_unlock (&ub->mutex);
Signature · 1 line
2.40.1
PATCH 9/10 ublk: replace monitor work with uring_cmd exit notifier
In reply to Ming LeiMonitor work actually introduces one extra context for handling abort, this way is easy to cause race, and also introduce extra delay when handling aborting.
Now uring_cmd introduces exit notifier, so use it instead:
1) this notifier callback is either run from the uring cmd submission task context or called after the io_uring context is exit, so the callback is run exclusively with ublk_ch_uring_cmd() and __ublk_rq_task_work().
2) the previous patch freezes request queue when calling ublk_abort_queue(), which is now completely exclusive with ublk_queue_rq() and ublk_ch_uring_cmd()/__ublk_rq_task_work().
This way simplifies aborting queue, and is helpful for adding new feature, such as, relax the limit of using single task for handling one queue.
--- drivers/block/ublk_drv.c | 89 +++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 47 deletions(-)
drivers/block/ublk_drv.c +42 -47
@@ -144,8 +144,6 @@ struct ublk_queue {struct ublk_io ios[]; }; -#define UBLK_DAEMON_MONITOR_PERIOD (5 * HZ) -struct ublk_device {struct gendisk *ub_disk;
@@ -176,11 +174,7 @@ struct ublk_device {unsigned int nr_queues_ready;unsigned int nr_privileged_daemon; -/* -* Our ubq->daemon may be killed without any notification, so -* monitor each queue's daemon periodically -*/ -struct delayed_work monitor_work; +struct notifier_block notif;struct work_struct quiesce_work;struct work_struct stop_work; };
@@ -194,7 +188,6 @@ struct ublk_params_header {static inline unsigned int ublk_req_build_flags (struct request *req);static inline struct ublksrv_io_desc *ublk_get_iod (struct ublk_queue *ubq,int tag); -static inline bool ublk_dev_is_user_copy (const struct ublk_device *ub) {return ub->dev_info.flags &UBLK_F_USER_COPY ;
@@ -1119,8 +1112,6 @@ static inline void __ublk_abort_rq(struct ublk_queue *ubq,blk_mq_requeue_request (rq, false); elseblk_mq_end_request (rq ,BLK_STS_IOERR ); - -mod_delayed_work (system_wq, &ubq->dev->monitor_work,0 ); } static inline void __ublk_rq_task_work(struct request *req,
@@ -1241,12 +1232,12 @@ static void ublk_queue_cmd(struct ublk_queue *ubq, struct request *rq) io = &ubq->ios[rq->tag];/* * If the check pass, we know that this is a re-issued request aborted -* previously in monitor_work because the ubq_daemon(cmd's task) is +* previously in exit notifier because the ubq_daemon(cmd's task) is * PF_EXITING. We cannot call io_uring_cmd_complete_in_task() anymore * because this ioucmd's io_uring context may be freed now if no inflight * ioucmd exists. Otherwise we may cause null-deref in ctx->fallback_work. * -* Note: monitor_work sets UBLK_IO_FLAG_ABORTED and ends this request(releasing +* Note: exit notifier sets UBLK_IO_FLAG_ABORTED and ends this request(releasing * the tag). Then the request is re-started(allocating the tag) and we are here. * Since releasing/allocating a tag implies smp_mb(), finding UBLK_IO_FLAG_ABORTED * guarantees that here is a re-issued request aborted previously.
@@ -1334,9 +1325,17 @@ static int ublk_ch_open(struct inode *inode, struct file *filp) {struct ublk_device *ub =container_of (inode->i_cdev, struct ublk_device, cdev); +int ret;if (test_and_set_bit (UB_STATE_OPEN , &ub->state))return -EBUSY ; + + ret =io_uring_cmd_register_notifier (&ub->notif); +if (ret) { +clear_bit (UB_STATE_OPEN , &ub->state); +return ret; + } + filp->private_data = ub;return 0 ; }
@@ -1346,6 +1345,8 @@ static int ublk_ch_release(struct inode *inode, struct file *filp)struct ublk_device *ub = filp->private_data;clear_bit (UB_STATE_OPEN , &ub->state); +io_uring_cmd_unregister_notifier (&ub->notif); +return 0 ; }
@@ -1417,9 +1418,9 @@ static inline bool ublk_ctx_id_is_valid(unsigned int ctx_id) }/* -* When ->ubq_daemon is exiting, either new request is ended immediately, -* or any queued io command is drained, so it is safe to abort queue -* lockless +* Called from ubq_daemon context via the notifier, meantime quiesce ublk +* blk-mq queue, so we are called exclusively with blk-mq and ubq_daemon +* context, so everything is serialized. */ static void ublk_abort_queue (struct ublk_device *ub,struct ublk_queue *ubq) {
@@ -1442,20 +1443,34 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq) } } -static void ublk_daemon_monitor_work (struct work_struct *work) +static inline bool ubq_daemon_is_exiting (const struct ublk_queue *ubq, +const struct io_uring_notifier_data *data) { -struct ublk_device *ub = - container_of(work, struct ublk_device, monitor_work.work); +return data->ctx_id == ubq->ctx_id && (!data->task || + data->task == ubq->ubq_daemon); +} + +static int ublk_notifier_cb (struct notifier_block *nb, +unsigned long event,void *val) +{ + struct ublk_device *ub = container_of(nb, struct ublk_device, notif); +struct io_uring_notifier_data *data = val;struct gendisk *disk;int i; +pr_devel ("%s: event %lu ctx_id %u task %p\n" , __func__, event, + data->ctx_id, data->task); + +if (!ublk_ctx_id_is_valid (data->ctx_id)) +return 0 ; +for (i =0 ; i < ub->dev_info.nr_hw_queues; i++) {struct ublk_queue *ubq =ublk_get_queue (ub, i); -if (ubq_daemon_is_dying (ubq)) +if (ubq_daemon_is_exiting (ubq, data)) goto found; } -return ; +return 0 ; found:spin_lock (&ub->lock);
@@ -1466,14 +1481,14 @@ static void ublk_daemon_monitor_work(struct work_struct *work)/* Our disk has been dead */ if (!disk) -return ; +return 0 ;/* Now we are serialized with ublk_queue_rq() */ blk_mq_quiesce_queue (disk->queue);for (i =0 ; i < ub->dev_info.nr_hw_queues; i++) {struct ublk_queue *ubq =ublk_get_queue (ub, i); -if (ubq_daemon_is_dying (ubq)) { +if (ubq_daemon_is_exiting (ubq, data)) {/* abort queue is for making forward progress */ ublk_abort_queue (ub, ubq); }
@@ -1485,17 +1500,7 @@ static void ublk_daemon_monitor_work(struct work_struct *work)schedule_work (&ub->quiesce_work); else schedule_work(&ub->stop_work); - -/* -* We can't schedule monitor work after ub's state is not UBLK_S_DEV_LIVE. -* after ublk_remove() or __ublk_quiesce_dev() is started. -* -* No need ub->mutex, monitor work are canceled after state is marked -* as not LIVE, so new state is observed reliably. -*/ -if (ub->dev_info.state ==UBLK_S_DEV_LIVE ) -schedule_delayed_work (&ub->monitor_work, -UBLK_DAEMON_MONITOR_PERIOD ); +return 0 ; }static inline bool ublk_queue_ready (struct ublk_queue *ubq)
@@ -1512,6 +1517,9 @@ static void ublk_cancel_queue(struct ublk_queue *ubq) {int i; +if (ublk_ctx_id_is_valid (ubq->ctx_id)) + ubq->ctx_id =IO_URING_INVALID_CTX_ID ; +if (!ublk_queue_ready (ubq))return ;
@@ -1572,15 +1580,6 @@ static void __ublk_quiesce_dev(struct ublk_device *ub)ublk_wait_tagset_rqs_idle (ub); ub->dev_info.state =UBLK_S_DEV_QUIESCED ;ublk_cancel_dev (ub); -/* we are going to release task_struct of ubq_daemon and resets -* ->ubq_daemon to NULL. So in monitor_work, check on ubq_daemon causes UAF. -* Besides, monitor_work is not necessary in QUIESCED state since we have -* already scheduled quiesce_work and quiesced all ubqs. -* -* Do not let monitor_work schedule itself if state it QUIESCED. And we cancel -* it here and re-schedule it in END_USER_RECOVERY to avoid UAF. -*/ -cancel_delayed_work_sync (&ub->monitor_work); }static void ublk_quiesce_work_fn (struct work_struct *work)
@@ -1642,7 +1641,6 @@ static void ublk_stop_dev(struct ublk_device *ub) unlock:ublk_cancel_dev (ub);mutex_unlock (&ub->mutex); -cancel_delayed_work_sync (&ub->monitor_work); }/* device can only be started after all IOs are ready */
@@ -2210,8 +2208,6 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub, struct io_uring_cmd *cmd)if (wait_for_completion_interruptible (&ub->completion) !=0 )return -EINTR ; -schedule_delayed_work (&ub->monitor_work,UBLK_DAEMON_MONITOR_PERIOD ); -mutex_lock (&ub->mutex);if (ub->dev_info.state ==UBLK_S_DEV_LIVE ||test_bit (UB_STATE_USED , &ub->state)) {
@@ -2382,7 +2378,7 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd)spin_lock_init (&ub->lock);INIT_WORK (&ub->quiesce_work, ublk_quiesce_work_fn);INIT_WORK (&ub->stop_work, ublk_stop_work_fn); -INIT_DELAYED_WORK (&ub->monitor_work, ublk_daemon_monitor_work); + ub->notif.notifier_call = ublk_notifier_cb; ret =ublk_alloc_dev_number (ub, header->dev_id);if (ret <0 )
@@ -2722,7 +2718,6 @@ static int ublk_ctrl_end_recovery(struct ublk_device *ub, __func__, header->dev_id);blk_mq_kick_requeue_list (ub->ub_disk->queue); ub->dev_info.state =UBLK_S_DEV_LIVE ; -schedule_delayed_work (&ub->monitor_work,UBLK_DAEMON_MONITOR_PERIOD ); ret =0 ; out_unlock:mutex_unlock (&ub->mutex);
Signature · 1 line
2.40.1
PATCH 10/10 ublk: simplify aborting request
In reply to Ming LeiNow ublk_abort_queue() is run exclusively with ublk_queue_rq() and the ubq_daemon task, so simplify aborting request:
- set UBLK_IO_FLAG_ABORTED in ublk_abort_queue()
- abort request in ublk_queue_rq() if UBLK_IO_FLAG_ABORTED is observed, and no need to check ubq_daemon any more
--- drivers/block/ublk_drv.c | 46 ++++++++++++---------------------------- 1 file changed, 14 insertions(+), 32 deletions(-)
drivers/block/ublk_drv.c +14 -32
@@ -1077,13 +1077,10 @@ static void __ublk_fail_req(struct ublk_queue *ubq, struct ublk_io *io, {WARN_ON_ONCE (io->flags &UBLK_IO_FLAG_ACTIVE ); -if (!(io->flags &UBLK_IO_FLAG_ABORTED )) { - io->flags |=UBLK_IO_FLAG_ABORTED ; -if (ublk_queue_can_use_recovery_reissue (ubq)) -blk_mq_requeue_request (req, false); -else -ublk_put_req_ref (ubq, req); - } +if (ublk_queue_can_use_recovery_reissue (ubq)) +blk_mq_requeue_request (req, false); +else +ublk_put_req_ref (ubq, req); } static void ubq_complete_io_cmd(struct ublk_io *io, int res,
@@ -1221,30 +1218,12 @@ static void ublk_rq_task_work_cb(struct io_uring_cmd *cmd, unsigned issue_flags)ublk_forward_io_cmds (ubq, issue_flags); } -static void ublk_queue_cmd (struct ublk_queue *ubq,struct request *rq) +static inline void ublk_queue_cmd (struct ublk_queue *ubq,struct request *rq) {struct ublk_rq_data *data =blk_mq_rq_to_pdu (rq); -struct ublk_io *io; - -if (!llist_add (&data->node, &ubq->io_cmds)) -return ; - io = &ubq->ios[rq->tag]; -/* -* If the check pass, we know that this is a re-issued request aborted -* previously in exit notifier because the ubq_daemon(cmd's task) is -* PF_EXITING. We cannot call io_uring_cmd_complete_in_task() anymore -* because this ioucmd's io_uring context may be freed now if no inflight -* ioucmd exists. Otherwise we may cause null-deref in ctx->fallback_work. -* -* Note: exit notifier sets UBLK_IO_FLAG_ABORTED and ends this request(releasing -* the tag). Then the request is re-started(allocating the tag) and we are here. -* Since releasing/allocating a tag implies smp_mb(), finding UBLK_IO_FLAG_ABORTED -* guarantees that here is a re-issued request aborted previously. -*/ -if (unlikely (io->flags &UBLK_IO_FLAG_ABORTED )) { -ublk_abort_io_cmds (ubq); - }else { +if (llist_add (&data->node, &ubq->io_cmds)) { +struct ublk_io *io = &ubq->ios[rq->tag];struct io_uring_cmd *cmd = io->cmd;struct ublk_uring_cmd_pdu *pdu =ublk_get_uring_cmd_pdu (cmd);
@@ -1274,6 +1253,7 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx, {struct ublk_queue *ubq = hctx->driver_data;struct request *rq = bd->rq; +struct ublk_io *io = &ubq->ios[rq->tag];blk_status_t res;/* fill iod to slot in io cmd buffer */
@@ -1293,13 +1273,12 @@ static blk_status_t ublk_queue_rq(struct blk_mq_hw_ctx *hctx,if (ublk_queue_can_use_recovery (ubq) &&unlikely (ubq->force_abort))return BLK_STS_IOERR ; -blk_mq_start_request (bd->rq); - -if (unlikely (ubq_daemon_is_dying (ubq))) { +if (unlikely (io->flags &UBLK_IO_FLAG_ABORTED )) {__ublk_abort_rq (ubq, rq);return BLK_STS_OK ; } +blk_mq_start_request (bd->rq);ublk_queue_cmd (ubq, rq);return BLK_STS_OK ;
@@ -1429,6 +1408,9 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)for (i =0 ; i < ubq->q_depth; i++) {struct ublk_io *io = &ubq->ios[i]; +if (!(io->flags &UBLK_IO_FLAG_ABORTED )) + io->flags |=UBLK_IO_FLAG_ABORTED ; +if (!(io->flags &UBLK_IO_FLAG_ACTIVE )) {struct request *rq;
@@ -1437,7 +1419,7 @@ static void ublk_abort_queue(struct ublk_device *ub, struct ublk_queue *ubq)* will do it */ rq =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], i); -if (rq) +if (rq &&blk_mq_request_started (rq))__ublk_fail_req (ubq, io, rq); } }
Signature · 1 line
2.40.1
Jens Axboe
maintainerQuoted 13 lines · Ming Lei
Hello,
In
do_exit(),io_uringneeds to wait pending requests.ublk is one
uring_cmddriver, and its usage is a bit special by submitting command for waiting incoming block IO request in advance, so if there isn't any IO request coming, the command can't be completed. So far ublk driver has to bind its queue with one ublk daemon server, meantime starts one monitor work to check if this daemon is live periodically. This way requires ublk queue to be bound one single daemon pthread, and not flexible, meantime the monitor work is run in 3rd context, and the implementation is a bit tricky.
Quoted 5 lines
The 1st 3 patches adds
io_uringtask exit notifier, and the other patches converts ublk into this exit notifier, and the implementation becomes more robust & readable, meantime it becomes easier to relax the ublk queue/daemon limit in future, such as not require to bind ublk queue with single daemon.
The normal approach for this is to ensure that each request is cancelable, which we need for other things too (like actual cancel support). Why can't we just do the same for ublk?
Signature · 1 line
Jens Axboe
Quoted 20 lines · Jens Axboe
Quoted 13 lines · Ming Lei
Hello,
In
do_exit(),io_uringneeds to wait pending requests.ublk is one
uring_cmddriver, and its usage is a bit special by submitting command for waiting incoming block IO request in advance, so if there isn't any IO request coming, the command can't be completed. So far ublk driver has to bind its queue with one ublk daemon server, meantime starts one monitor work to check if this daemon is live periodically. This way requires ublk queue to be bound one single daemon pthread, and not flexible, meantime the monitor work is run in 3rd context, and the implementation is a bit tricky.Quoted 5 lines
The 1st 3 patches adds
io_uringtask exit notifier, and the other patches converts ublk into this exit notifier, and the implementation becomes more robust & readable, meantime it becomes easier to relax the ublk queue/daemon limit in future, such as not require to bind ublk queue with single daemon.
Quoted 3 lines
The normal approach for this is to ensure that each request is cancelable, which we need for other things too (like actual cancel support) Why can't we just do the same for ublk?
I guess you meant IORING_OP_ASYNC_CANCEL, which needs userspace to submit this command, but here the userspace(ublk server) may be just panic or killed, and there isn't chance to send IORING_OP_ASYNC_CANCEL.
And driver doesn't have any knowledge if the io_uring ctx or io task is exiting, so can't complete issued commands, then hang in io_uring_cancel_generic() when the io task/ctx is exiting.
Thanks,
Ming
Jens Axboe
maintainerQuoted 21 lines · Ming Lei
Quoted 20 lines · Jens Axboe
Quoted 18 lines · Ming Lei
Hello,
In
do_exit(),io_uringneeds to wait pending requests.ublk is one
uring_cmddriver, and its usage is a bit special by submitting command for waiting incoming block IO request in advance, so if there isn't any IO request coming, the command can't be completed. So far ublk driver has to bind its queue with one ublk daemon server, meantime starts one monitor work to check if this daemon is live periodically. This way requires ublk queue to be bound one single daemon pthread, and not flexible, meantime the monitor work is run in 3rd context, and the implementation is a bit tricky.The 1st 3 patches adds
io_uringtask exit notifier, and the other patches converts ublk into this exit notifier, and the implementation becomes more robust & readable, meantime it becomes easier to relax the ublk queue/daemon limit in future, such as not require to bind ublk queue with single daemon.
Quoted 7 lines
Quoted 3 lines
The normal approach for this is to ensure that each request is cancelable, which we need for other things too (like actual cancel support) Why can't we just do the same for ublk?
I guess you meant
IORING_OP_ASYNC_CANCEL, which needs userspace to submit this command, but here the userspace(ublk server) may be just panic or killed, and there isn't chance to sendIORING_OP_ASYNC_CANCEL.
Either that, or cancel done because of task exit.
Quoted 3 lines
And driver doesn't have any knowledge if the
io_uringctx or io task is exiting, so can't complete issued commands, then hang inio_uring_cancel_generic()when the io task/ctx is exiting.
If you hooked into the normal cancel paths, you very much would get notified when the task is exiting. That's how the other cancelations work, eg if a task has pending poll requests and exits, they get canceled and reaped.
Signature · 1 line
Jens Axboe
Quoted 32 lines · Jens Axboe
Quoted 21 lines · Ming Lei
Quoted 20 lines · Jens Axboe
Quoted 18 lines · Ming Lei
Hello,
In
do_exit(),io_uringneeds to wait pending requests.ublk is one
uring_cmddriver, and its usage is a bit special by submitting command for waiting incoming block IO request in advance, so if there isn't any IO request coming, the command can't be completed. So far ublk driver has to bind its queue with one ublk daemon server, meantime starts one monitor work to check if this daemon is live periodically. This way requires ublk queue to be bound one single daemon pthread, and not flexible, meantime the monitor work is run in 3rd context, and the implementation is a bit tricky.The 1st 3 patches adds
io_uringtask exit notifier, and the other patches converts ublk into this exit notifier, and the implementation becomes more robust & readable, meantime it becomes easier to relax the ublk queue/daemon limit in future, such as not require to bind ublk queue with single daemon.Quoted 7 lines
Quoted 3 lines
The normal approach for this is to ensure that each request is cancelable, which we need for other things too (like actual cancel support) Why can't we just do the same for ublk?
I guess you meant
IORING_OP_ASYNC_CANCEL, which needs userspace to submit this command, but here the userspace(ublk server) may be just panic or killed, and there isn't chance to sendIORING_OP_ASYNC_CANCEL.Either that, or cancel done because of task exit.
Quoted 8 lines
Quoted 3 lines
And driver doesn't have any knowledge if the
io_uringctx or io task is exiting, so can't complete issued commands, then hang inio_uring_cancel_generic()when the io task/ctx is exiting.If you hooked into the normal cancel paths, you very much would get notified when the task is exiting. That's how the other cancelations work, eg if a task has pending poll requests and exits, they get canceled and reaped.
Ok, got the idea, thanks for the point!
Turns out it is cancelable uring_cmd, and I will try to work towards this direction, and has got something in mind about the implementation.
Thanks,
Ming
Jens Axboe
maintainerQuoted 42 lines · Ming Lei
Quoted 32 lines · Jens Axboe
Quoted 21 lines · Ming Lei
Quoted 20 lines · Jens Axboe
Quoted 18 lines · Ming Lei
Hello,
In
do_exit(),io_uringneeds to wait pending requests.ublk is one
uring_cmddriver, and its usage is a bit special by submitting command for waiting incoming block IO request in advance, so if there isn't any IO request coming, the command can't be completed. So far ublk driver has to bind its queue with one ublk daemon server, meantime starts one monitor work to check if this daemon is live periodically. This way requires ublk queue to be bound one single daemon pthread, and not flexible, meantime the monitor work is run in 3rd context, and the implementation is a bit tricky.The 1st 3 patches adds
io_uringtask exit notifier, and the other patches converts ublk into this exit notifier, and the implementation becomes more robust & readable, meantime it becomes easier to relax the ublk queue/daemon limit in future, such as not require to bind ublk queue with single daemon.Quoted 7 lines
Quoted 3 lines
The normal approach for this is to ensure that each request is cancelable, which we need for other things too (like actual cancel support) Why can't we just do the same for ublk?
I guess you meant
IORING_OP_ASYNC_CANCEL, which needs userspace to submit this command, but here the userspace(ublk server) may be just panic or killed, and there isn't chance to sendIORING_OP_ASYNC_CANCEL.Either that, or cancel done because of task exit.
Quoted 8 lines
Quoted 3 lines
And driver doesn't have any knowledge if the
io_uringctx or io task is exiting, so can't complete issued commands, then hang inio_uring_cancel_generic()when the io task/ctx is exiting.If you hooked into the normal cancel paths, you very much would get notified when the task is exiting. That's how the other cancelations work, eg if a task has pending poll requests and exits, they get canceled and reaped.
Quoted 4 lines
Ok, got the idea, thanks for the point!
Turns out it is cancelable
uring_cmd, and I will try to work towards this direction, and has got something in mind about the implementation.
Perfect, thanks Ming!
Signature · 1 line
Jens Axboe