Skip to content

Commit a8ce5f5

Browse files
Ming Leiaxboe
authored andcommitted
ublk_drv: cancel device even though disk isn't up
Each ublk queue is started before adding disk, we have to cancel queues in ublk_stop_dev() so that ubq daemon can be exited, otherwise DEL_DEV command may hang forever. Also avoid to cancel queues two times by checking if queue is ready, otherwise use-after-free on io_uring may be triggered because ublk_stop_dev is called by ublk_remove() too. Fixes: 71f28f3 ("ublk_drv: add io_uring based userspace block driver") Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Ming Lei <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent e97424f commit a8ce5f5

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

drivers/block/ublk_drv.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -788,16 +788,27 @@ static void ublk_daemon_monitor_work(struct work_struct *work)
788788
UBLK_DAEMON_MONITOR_PERIOD);
789789
}
790790

791+
static inline bool ublk_queue_ready(struct ublk_queue *ubq)
792+
{
793+
return ubq->nr_io_ready == ubq->q_depth;
794+
}
795+
791796
static void ublk_cancel_queue(struct ublk_queue *ubq)
792797
{
793798
int i;
794799

800+
if (!ublk_queue_ready(ubq))
801+
return;
802+
795803
for (i = 0; i < ubq->q_depth; i++) {
796804
struct ublk_io *io = &ubq->ios[i];
797805

798806
if (io->flags & UBLK_IO_FLAG_ACTIVE)
799807
io_uring_cmd_done(io->cmd, UBLK_IO_RES_ABORT, 0);
800808
}
809+
810+
/* all io commands are canceled */
811+
ubq->nr_io_ready = 0;
801812
}
802813

803814
/* Cancel all pending commands, must be called after del_gendisk() returns */
@@ -818,19 +829,14 @@ static void ublk_stop_dev(struct ublk_device *ub)
818829
del_gendisk(ub->ub_disk);
819830
ub->dev_info.state = UBLK_S_DEV_DEAD;
820831
ub->dev_info.ublksrv_pid = -1;
821-
ublk_cancel_dev(ub);
822832
put_disk(ub->ub_disk);
823833
ub->ub_disk = NULL;
824834
unlock:
835+
ublk_cancel_dev(ub);
825836
mutex_unlock(&ub->mutex);
826837
cancel_delayed_work_sync(&ub->monitor_work);
827838
}
828839

829-
static inline bool ublk_queue_ready(struct ublk_queue *ubq)
830-
{
831-
return ubq->nr_io_ready == ubq->q_depth;
832-
}
833-
834840
/* device can only be started after all IOs are ready */
835841
static void ublk_mark_io_ready(struct ublk_device *ub, struct ublk_queue *ubq)
836842
{

0 commit comments

Comments
 (0)