Skip to content

Commit 7fc4da6

Browse files
Ming Leiaxboe
authored andcommitted
ublk: scan partition in async way
Implement async partition scan to avoid IO hang when reading partition tables. Similar to nvme_partition_scan_work(), partition scanning is deferred to a work queue to prevent deadlocks. When partition scan happens synchronously during add_disk(), IO errors can cause the partition scan to wait while holding ub->mutex, which can deadlock with other operations that need the mutex. Changes: - Add partition_scan_work to ublk_device structure - Implement ublk_partition_scan_work() to perform async scan - Always suppress sync partition scan during add_disk() - Schedule async work after add_disk() for trusted daemons - Add flush_work() in ublk_stop_dev() before grabbing ub->mutex Reviewed-by: Caleb Sander Mateos <csander@purestorage.com> Reported-by: Yoav Cohen <yoav@nvidia.com> Closes: https://lore.kernel.org/linux-block/DM4PR12MB63280C5637917C071C2F0D65A9A8A@DM4PR12MB6328.namprd12.prod.outlook.com/ Fixes: 71f28f3 ("ublk_drv: add io_uring based userspace block driver") Signed-off-by: Ming Lei <ming.lei@redhat.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 04bdb1a commit 7fc4da6

1 file changed

Lines changed: 32 additions & 3 deletions

File tree

drivers/block/ublk_drv.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ struct ublk_device {
237237
bool canceling;
238238
pid_t ublksrv_tgid;
239239
struct delayed_work exit_work;
240+
struct work_struct partition_scan_work;
240241

241242
struct ublk_queue *queues[];
242243
};
@@ -254,6 +255,20 @@ static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub,
254255
u16 q_id, u16 tag, struct ublk_io *io, size_t offset);
255256
static inline unsigned int ublk_req_build_flags(struct request *req);
256257

258+
static void ublk_partition_scan_work(struct work_struct *work)
259+
{
260+
struct ublk_device *ub =
261+
container_of(work, struct ublk_device, partition_scan_work);
262+
263+
if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN,
264+
&ub->ub_disk->state)))
265+
return;
266+
267+
mutex_lock(&ub->ub_disk->open_mutex);
268+
bdev_disk_changed(ub->ub_disk, false);
269+
mutex_unlock(&ub->ub_disk->open_mutex);
270+
}
271+
257272
static inline struct ublksrv_io_desc *
258273
ublk_get_iod(const struct ublk_queue *ubq, unsigned tag)
259274
{
@@ -2026,6 +2041,7 @@ static void ublk_stop_dev(struct ublk_device *ub)
20262041
mutex_lock(&ub->mutex);
20272042
ublk_stop_dev_unlocked(ub);
20282043
mutex_unlock(&ub->mutex);
2044+
flush_work(&ub->partition_scan_work);
20292045
ublk_cancel_dev(ub);
20302046
}
20312047

@@ -2954,9 +2970,17 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub,
29542970

29552971
ublk_apply_params(ub);
29562972

2957-
/* don't probe partitions if any daemon task is un-trusted */
2958-
if (ub->unprivileged_daemons)
2959-
set_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
2973+
/*
2974+
* Suppress partition scan to avoid potential IO hang.
2975+
*
2976+
* If ublk server error occurs during partition scan, the IO may
2977+
* wait while holding ub->mutex, which can deadlock with other
2978+
* operations that need the mutex. Defer partition scan to async
2979+
* work.
2980+
* For unprivileged daemons, keep GD_SUPPRESS_PART_SCAN set
2981+
* permanently.
2982+
*/
2983+
set_bit(GD_SUPPRESS_PART_SCAN, &disk->state);
29602984

29612985
ublk_get_device(ub);
29622986
ub->dev_info.state = UBLK_S_DEV_LIVE;
@@ -2973,6 +2997,10 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub,
29732997

29742998
set_bit(UB_STATE_USED, &ub->state);
29752999

3000+
/* Schedule async partition scan for trusted daemons */
3001+
if (!ub->unprivileged_daemons)
3002+
schedule_work(&ub->partition_scan_work);
3003+
29763004
out_put_cdev:
29773005
if (ret) {
29783006
ublk_detach_disk(ub);
@@ -3138,6 +3166,7 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)
31383166
mutex_init(&ub->mutex);
31393167
spin_lock_init(&ub->lock);
31403168
mutex_init(&ub->cancel_mutex);
3169+
INIT_WORK(&ub->partition_scan_work, ublk_partition_scan_work);
31413170

31423171
ret = ublk_alloc_dev_number(ub, header->dev_id);
31433172
if (ret < 0)

0 commit comments

Comments
 (0)