Re: [PATCH 0/6] md: don't wait for q->limits_lock while md holds back I/O
From: Nilay Shroff <hidden>
Date: 2026-09-08 11:16:23
Also in:
linux-block
On 9/7/26 7:09 PM, Jack Wang wrote:
From: Jack Wang<jinpu.wang@cloud.ionos.com> Writing to a queue limits attribute of an md array while a spare is being re-added deadlocks the array. I reported this earlier here: https://lore.kernel.org/linux-raid/CAMGffE=heGA3y8FjQ0Sm1jj-kd-=H9Y54WozKASSEZhc9UNKjA@mail.gmail.com/ (local) Four tasks, one array: udev-worker queue_attr_store() holds q->limits_lock, waits in blk_mq_freeze_queue() for q_usage_counter to drain fio holds a q_usage_counter reference, parked in md_handle_request()'s is_suspended() loop mdadm suspended the array, waits for reconfig_mutex md_start_sync holds reconfig_mutex, waits for q->limits_lock The last leg is mddev_stack_new_rdev() from ->hot_add_disk(). Since commit c99f66e4084a ("block: fix queue freeze vs limits lock order in sysfs store methods") the sysfs store holds q->limits_lock across the freeze, so md must not block on that lock while it is holding back the I/O the freeze waits for. That is the same hazard mddev_suspend() already documents for reconfig_mutex. The rule this series applies is that q->limits_lock nests outside both reconfig_mutex and the suspend. Where md cannot arrange that, because it is called with reconfig_mutex already held or from the sync thread, it takes the update with a trylock and does without one on a contended pass. Patches 1, 2 and 4 are plumbing with no functional change. Patches 3 and 5 convert the two callers that cannot own an update. Patch 6 does the hoists, all in one patch because a mix of the two lock orders is an ABBA. Two callers still take q->limits_lock inside reconfig_mutex, both with the array suspended, and patch 6 says why: ->start_reshape() from action_store(), which suspends before flushing sync_work, and raid*_run() -> queue_limits_set() from level_store(), which already hangs on its own because it freezes the queue while suspended. Both need more restructuring than belongs here. The patches are based on v7.3-rc2. Tested there with a raid1 of two ram devices, fio in flight and a loop writing queue/max_sectors_kb: 20 fail/remove/add cycles complete, where the same test wedges the array before the series. Every patch builds on its own. A reshape and a level change are not covered by that test.
Overall, I think the direction looks good. With this series, we now have the
locking order where q->limits_lock is acquired before the suspend and
reconfig_mutex.
However, when I ran these changes through blktests, I hit the lockdep splat[1],
which exposes an ABBA dependency between disk->open_mutex and q->limits_lock.
Looking at the existing dependency chain, disk->open_mutex is expected to
be acquired before q->limits_lock. However, with this change, md_ioctl()
acquires q->limits_lock first and subsequently reaches md_import_device(),
which acquires disk->open_mutex. This reverses the existing lock ordering
and introduces the ABBA dependency, so I think this needs to be addressed.
Please find the lockdep splat below for reference:
[1]
======================================================
WARNING: possible circular locking dependency detected
7.0.0+ #33 Not tainted
------------------------------------------------------
mdadm/15956 is trying to acquire lock:
c000000144f14358 (&disk->open_mutex){+.+.}-{4:4}, at: bdev_open+0x94/0x504
but task is already holding lock:
c000000122a13370 (&mddev->reconfig_mutex){+.+.}-{4:4}, at: md_ioctl+0xb18/0x1f44
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #2 (&mddev->reconfig_mutex){+.+.}-{4:4}:
lock_acquire+0x16c/0x498
__mutex_lock+0xc8/0xf1c
md_ioctl+0xb18/0x1f44
blkdev_ioctl+0x5e8/0x1950
sys_ioctl+0x494/0x11c0
system_call_exception+0x138/0x380
system_call_vectored_common+0x15c/0x2ec
-> #1 (&q->limits_lock){+.+.}-{4:4}:
lock_acquire+0x16c/0x498
__mutex_lock+0xc8/0xf1c
sd_revalidate_disk+0xe4/0x2d74 [sd_mod]
sd_open+0x1a8/0x21c [sd_mod]
blkdev_get_whole+0x54/0x124
bdev_open+0x3cc/0x504
bdev_file_open_by_dev+0x114/0x1b4
disk_scan_partitions+0xc4/0x1d8
add_disk_fwnode+0x1ec/0x1f0
sd_probe+0x3a0/0x644 [sd_mod]
scsi_bus_probe+0x38/0x5c
really_probe+0x100/0x550
__driver_probe_device+0xb4/0x224
driver_probe_device+0x50/0x128
__driver_attach_async_helper+0x78/0x14c
async_run_entry_fn+0x5c/0x1f8
process_one_work+0x2c0/0x8d0
worker_thread+0x218/0x444
kthread+0x16c/0x1a0
start_kernel_thread+0x14/0x18
-> #0 (&disk->open_mutex){+.+.}-{4:4}:
check_prev_add+0x170/0x1248
__lock_acquire+0x17d0/0x2144
lock_acquire+0x16c/0x498
__mutex_lock+0xc8/0xf1c
bdev_open+0x94/0x504
bdev_file_open_by_dev+0x114/0x1b4
md_import_device+0x15c/0x2c8
md_add_new_disk+0x130/0x810
md_ioctl+0x1840/0x1f44
blkdev_ioctl+0x5e8/0x1950
sys_ioctl+0x494/0x11c0
system_call_exception+0x138/0x380
system_call_vectored_common+0x15c/0x2ec
other info that might help us debug this:
Chain exists of:
&disk->open_mutex --> &q->limits_lock --> &mddev->reconfig_mutex
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&mddev->reconfig_mutex);
lock(&q->limits_lock);
lock(&mddev->reconfig_mutex);
lock(&disk->open_mutex);
*** DEADLOCK ***
2 locks held by mdadm/15956:
#0: c0000001629d6a18 (&q->limits_lock){+.+.}-{4:4}, at: md_ioctl+0xa74/0x1f44
#1: c000000122a13370 (&mddev->reconfig_mutex){+.+.}-{4:4}, at: md_ioctl+0xb18/0x1f44
stack backtrace:
CPU: 3 UID: 0 PID: 15956 Comm: mdadm Kdump: loaded Not tainted 7.0.0+ #33 PREEMPTLAZY
Hardware name: IBM,9105-22A Power11 (architected) 0x820200 0xf000007 of:IBM,FW1120.00 (RB1120_179) hv:phyp pSeries
Call Trace:
[c00000015919f1e0] [c000000001303538] dump_stack_lvl+0xe8/0x150 (unreliable)
[c00000015919f210] [c0000000002f204c] print_circular_bug+0x44c/0x608
[c00000015919f2c0] [c0000000002f23d4] check_noncircular+0x1cc/0x1ec
[c00000015919f390] [c0000000002f3f9c] check_prev_add+0x170/0x1248
[c00000015919f450] [c0000000002f876c] __lock_acquire+0x17d0/0x2144
[c00000015919f580] [c0000000002f9f4c] lock_acquire+0x16c/0x498
[c00000015919f680] [c000000001371fcc] __mutex_lock+0xc8/0xf1c
[c00000015919f7d0] [c0000000009de664] bdev_open+0x94/0x504
[c00000015919f850] [c0000000009debe8] bdev_file_open_by_dev+0x114/0x1b4
[c00000015919f8a0] [c000000000e539a8] md_import_device+0x15c/0x2c8
[c00000015919f930] [c000000000e62718] md_add_new_disk+0x130/0x810
[c00000015919f9c0] [c000000000e6a3a0] md_ioctl+0x1840/0x1f44
[c00000015919fc30] [c000000000a185e4] blkdev_ioctl+0x5e8/0x1950
[c00000015919fd10] [c000000000823274] sys_ioctl+0x494/0x11c0
[c00000015919fe10] [c0000000000318d8] system_call_exception+0x138/0x380
[c00000015919fe50] [c00000000000cedc] system_call_vectored_common+0x15c/0x2ec