ublk: scan partition in async way
Subsystems (3)
v2 cover ublk: scan partition in async way
Hi Guys,
The 1st patch scans partition in async way, so IO hang during partition scan can be covered by current error handling code.
The 2nd patch adds one test for verifying if hang from scanning partition can be avoided.
The last patch fixes one selftest/ublk rebuild depending issue.
V2:
- fix one comment typo in 1/3(Caleb Sander Mateos)
- simplify test code: 2/3
- fix LOCAL_HDRS usage in patch 3/3
Ming Lei (3):
ublk: scan partition in async way selftests/ublk: add test for async partition scan selftests/ublk: fix Makefile to rebuild on header changes
drivers/block/ublk_drv.c | 35 +++++++++- tools/testing/selftests/ublk/Makefile | 5 +- tools/testing/selftests/ublk/test_common.sh | 16 +++-- .../testing/selftests/ublk/test_generic_15.sh | 68 +++++++++++++++++++ 4 files changed, 115 insertions(+), 9 deletions(-) create mode 100755 tools/testing/selftests/ublk/test_generic_15.sh
Signature · 1 line
2.47.0
PATCH v2 1/3 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
--- drivers/block/ublk_drv.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-)
drivers/block/ublk_drv.c +32 -3
@@ -237,6 +237,7 @@ struct ublk_device {bool canceling;pid_t ublksrv_tgid;struct delayed_work exit_work; +struct work_struct partition_scan_work;struct ublk_queue *queues[]; };
@@ -254,6 +255,20 @@ static inline struct request *__ublk_check_and_get_req(struct ublk_device *ub, u16 q_id, u16 tag, struct ublk_io *io, size_t offset);static inline unsigned int ublk_req_build_flags (struct request *req); +static void ublk_partition_scan_work (struct work_struct *work) +{ +struct ublk_device *ub = + container_of(work, struct ublk_device, partition_scan_work); + +if (WARN_ON_ONCE (!test_and_clear_bit (GD_SUPPRESS_PART_SCAN , + &ub->ub_disk->state))) +return ; + +mutex_lock (&ub->ub_disk->open_mutex); +bdev_disk_changed (ub->ub_disk, false); +mutex_unlock (&ub->ub_disk->open_mutex); +} +static inline struct ublksrv_io_desc *ublk_get_iod (const struct ublk_queue *ubq,unsigned tag) {
@@ -2026,6 +2041,7 @@ static void ublk_stop_dev(struct ublk_device *ub)mutex_lock (&ub->mutex);ublk_stop_dev_unlocked (ub);mutex_unlock (&ub->mutex); +flush_work (&ub->partition_scan_work);ublk_cancel_dev (ub); }
@@ -2954,9 +2970,17 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub,ublk_apply_params (ub); -/* don't probe partitions if any daemon task is un-trusted */ -if (ub->unprivileged_daemons) -set_bit (GD_SUPPRESS_PART_SCAN , &disk->state); +/* +* Suppress partition scan to avoid potential IO hang. +* +* If ublk server error occurs during partition scan, the IO may +* wait while holding ub->mutex, which can deadlock with other +* operations that need the mutex. Defer partition scan to async +* work. +* For unprivileged daemons, keep GD_SUPPRESS_PART_SCAN set +* permanently. +*/ +set_bit (GD_SUPPRESS_PART_SCAN , &disk->state);ublk_get_device (ub); ub->dev_info.state =UBLK_S_DEV_LIVE ;
@@ -2973,6 +2997,10 @@ static int ublk_ctrl_start_dev(struct ublk_device *ub,set_bit (UB_STATE_USED , &ub->state); +/* Schedule async partition scan for trusted daemons */ +if (!ub->unprivileged_daemons) +schedule_work (&ub->partition_scan_work); + out_put_cdev:if (ret) {ublk_detach_disk (ub);
@@ -3138,6 +3166,7 @@ static int ublk_ctrl_add_dev(const struct ublksrv_ctrl_cmd *header)mutex_init (&ub->mutex);spin_lock_init (&ub->lock);mutex_init (&ub->cancel_mutex); +INIT_WORK (&ub->partition_scan_work, ublk_partition_scan_work); ret =ublk_alloc_dev_number (ub, header->dev_id);if (ret <0 )
Signature · 1 line
2.47.0
PATCH v2 2/3 selftests/ublk: add test for async partition scan
In reply to Ming LeiAdd test_generic_15.sh to verify that async partition scan prevents IO hang when reading partition tables.
The test creates ublk devices with fault_inject target and very large delay (60s) to simulate blocked partition table reads, then kills the daemon to verify proper state transitions without hanging:
1. Without recovery support:
- Create device with fault_inject and 60s delay
- Kill daemon while partition scan may be blocked
- Verify device transitions to DEAD state
2. With recovery support (-r 1):
- Create device with fault_inject, 60s delay, and recovery
- Kill daemon while partition scan may be blocked
- Verify device transitions to QUIESCED state
Before the async partition scan fix, killing the daemon during partition scan would cause deadlock as partition scan held ub->mutex while waiting for IO. With the async fix, partition scan happens in a work function and flush_work() ensures proper synchronization.
Add _add_ublk_dev_no_settle() helper function to skip udevadm settle, which would otherwise hang waiting for partition scan events to complete when partition table read is delayed.
--- tools/testing/selftests/ublk/Makefile | 1 + tools/testing/selftests/ublk/test_common.sh | 16 +++-- .../testing/selftests/ublk/test_generic_15.sh | 68 +++++++++++++++++++ 3 files changed, 81 insertions(+), 4 deletions(-) create mode 100755 tools/testing/selftests/ublk/test_generic_15.sh
tools/testing/selftests/ublk/Makefile +1 -0
@@ -22,6 +22,7 @@ TEST_PROGS += test_generic_11.sh TEST_PROGS += test_generic_12.sh TEST_PROGS += test_generic_13.sh TEST_PROGS += test_generic_14.sh +TEST_PROGS += test_generic_15.sh TEST_PROGS += test_null_01.sh TEST_PROGS += test_null_02.sh
tools/testing/selftests/ublk/test_common.sh +12 -4
@@ -178,8 +178,9 @@ _have_feature() _create_ublk_dev() { local dev_id; local cmd=$1 + local settle=$2 - shift 1 + shift 2 if [ ! -c /dev/ublk-control ]; then return ${UBLK_SKIP_CODE}
@@ -194,7 +195,10 @@ _create_ublk_dev() { echo "fail to add ublk dev $*" return 255 fi - udevadm settle + + if [ "$settle" = "yes" ]; then + udevadm settle + fi if [[ "$dev_id" =~ ^[0-9]+$ ]]; then echo "${dev_id}"
@@ -204,14 +208,18 @@ _create_ublk_dev() { } _add_ublk_dev() { - _create_ublk_dev "add" "$@" + _create_ublk_dev "add" "yes" "$@" +} + +_add_ublk_dev_no_settle() { + _create_ublk_dev "add" "no" "$@" } _recover_ublk_dev() { local dev_id local state - dev_id=$(_create_ublk_dev "recover" "$@") + dev_id=$(_create_ublk_dev "recover" "yes" "$@") for ((j=0;j<20;j++)); do state=$(_get_ublk_dev_state "${dev_id}") [ "$state" == "LIVE" ] && break
tools/testing/selftests/ublk/test_generic_15.sh new file +68 -0
68 lines not on this page · raw message
Signature · 1 line
2.47.0
PATCH v2 3/3 selftests/ublk: fix Makefile to rebuild on header changes
In reply to Ming LeiAdd header dependencies to kublk build rule so that changes to kublk.h, ublk_dep.h, or utils.h trigger a rebuild.
--- tools/testing/selftests/ublk/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/ublk/Makefile +2 -2
@@ -51,10 +51,10 @@ TEST_PROGS += test_stress_07.sh TEST_GEN_PROGS_EXTENDED = kublk +LOCAL_HDRS += $(wildcard *.h) include ../lib.mk -$(TEST_GEN_PROGS_EXTENDED): kublk.c null.c file_backed.c common.c stripe.c \ - fault_inject.c +$(TEST_GEN_PROGS_EXTENDED): $(wildcard *.c) check: shellcheck -x -f gcc *.sh
Signature · 1 line
2.47.0
Quoted 9 lines · Ming Lei
The 1st patch scans partition in async way, so IO hang during partition scan can be covered by current error handling code.
The 2nd patch adds one test for verifying if hang from scanning partition can be avoided.
The last patch fixes one selftest/ublk rebuild depending issue.
[...]
Applied, thanks!
[1/3] ublk: scan partition in async way
commit: 7fc4da6a304bdcd3de14fc946dc2c19437a9cc5a
[2/3] selftests/ublk: add test for async partition scan
commit: 60cf863720308ab89ce2fdafea7fcb2cefd9c144
[3/3] selftests/ublk: fix Makefile to rebuild on header changes
commit: a2ce133969175d36d708b7c76536b375d0522e53Best regards,
Signature · 1 line
Jens Axboe