block, md: suspend md around queue limits updates
Subsystems (2)
v4 cover block, md: suspend md around queue limits updates
From: Jack Wang <[email protected]>
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:
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
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. A bio parked in md_handle_request() while the array is suspended still holds a q_usage_counter reference, so that freeze never completes.
v3 froze the queue in mddev_suspend(). Review showed this deadlocks against raid1d/raid10d resubmitting a split bio and against raid456 I/O waiting on an interrupted reshape. Following Yu Kuai's suggestion, v4 lets a bio based disk suspend itself instead of freezing its queue:
- Patch 1 adds optional ->suspend() and ->resume() to block_device_operations. queue_attr_store() and the integrity flag_store() call ->suspend() before taking q->limits_lock, commit the limits without a freeze, then call ->resume().
- Patch 2 implements them for md with mddev_suspend() and mddev_resume().
The order is suspend, then q->limits_lock, as in mddev_update_io_opt(). A suspend already held by another task only nests.
Known limitation: md_submit_bio() reads the limits in bio_split_to_limits() before taking active_io, so a bio already there when the suspend completes can see a mix of old and new limits. mddev_update_io_opt() has the same window.
Christoph, does a ->suspend()/->resume() pair in block_device_operations fit what you had in mind?
v3: https://lore.kernel.org/linux-raid/[email protected]/
v2: https://lore.kernel.org/linux-raid/[email protected]/
v1: https://lore.kernel.org/linux-raid/[email protected]/T/#t
Changes since v3:
- Drop the freeze in mddev_suspend(); add ->suspend()/->resume() to block_device_operations, as suggested by Yu Kuai.
- Also cover the integrity flag_store() path.
- Rebased on v7.4-rc1.
Testing: the reproducer below, 20 fail/remove/add cycles with fio in flight and a loop writing queue/max_sectors_kb, on a 6.12 based kernel with lockdep enabled. No lockdep reports, no hung tasks. That kernel had the queue_attr_store() change only; the flag_store() change and the rebase to v7.4-rc1 are build tested only.
A blktests case for this deadlock was sent separately:
https://lore.kernel.org/all/[email protected]/
The reproducer:
mdadm -C /dev/md111 --force -e 1.2 --assume-clean -l 1 \
--bitmap=internal -n 2 /dev/ram0 /dev/ram1
fio --direct=1 --rw=randrw --ioengine=libaio --iodepth=32 --numjobs=4 \
--time_based=1 --runtime=180 --filename=/dev/md111 --name=repro &while :; do
echo 128 > /sys/block/md111/queue/max_sectors_kb 2>/dev/null
done &
for i in $(seq 20); do
mdadm /dev/md111 --fail /dev/ram0
mdadm /dev/md111 --remove /dev/ram0
mdadm /dev/md111 --add /dev/ram0
mdadm --wait /dev/md111
doneJack Wang (2):
block: suspend bio based disks around queue limits updates
md: suspend the array instead of freezing the queue for limits updates
block/blk-integrity.c | 10 ++++++++-- block/blk-sysfs.c | 19 +++++++++++++------ block/blk.h | 20 ++++++++++++++++++++ drivers/md/md.c | 12 ++++++++++++ include/linux/blkdev.h | 7 +++++++ 5 files changed, 60 insertions(+), 8 deletions(-)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
PATCH v4 1/2 block: suspend bio based disks around queue limits updates
queue_attr_store() freezes the queue under q->limits_lock. For md this can
deadlock: a bio parked while the array is suspended still holds a q_usage_counter reference, so the freeze waits for it, while the suspender waits for reconfig_mutex held by md_start_sync(), which waits for q->limits_lock.
Add optional ->suspend() and ->resume() block_device_operations. When present, queue_attr_store() and the integrity flag_store() suspend the disk before taking q->limits_lock and commit the limits without freezing the queue. Other disks behave as before.
--- block/blk-integrity.c | 10 ++++++++-- block/blk-sysfs.c | 19 +++++++++++++------ block/blk.h | 20 ++++++++++++++++++++ include/linux/blkdev.h | 7 +++++++ 4 files changed, 48 insertions(+), 8 deletions(-)
block/blk-integrity.c +8 -2
@@ -226,7 +226,8 @@ EXPORT_SYMBOL_GPL(blk_integrity_profile_name);static ssize_t flag_store (struct device *dev,const char *page,size_t count,unsigned char flag) { -struct request_queue *q =dev_to_disk (dev)->queue; +struct gendisk *disk =dev_to_disk (dev); +struct request_queue *q = disk->queue;struct queue_limits lim;unsigned long val;int err;
@@ -235,6 +236,10 @@ static ssize_t flag_store(struct device *dev, const char *page, size_t count,if (err)return err; + err =blk_disk_suspend (disk); +if (err) +return err; +/* note that the flags are inverted vs the values in the sysfs files */ lim =queue_limits_start_update (q);if (val)
@@ -242,7 +247,8 @@ static ssize_t flag_store(struct device *dev, const char *page, size_t count,else lim.integrity.flags |= flag; - err =queue_limits_commit_update_frozen (q, &lim); + err =blk_disk_commit_limits (disk, &lim); +blk_disk_resume (disk);if (err)return err;return count;
block/blk-sysfs.c +13 -6
@@ -887,20 +887,27 @@ queue_attr_store(struct kobject *kobj, struct attribute *attr,return -EIO ;if (entry->store_limit) { +struct queue_limits lim;ssize_t res; -struct queue_limits lim =queue_limits_start_update (q); + res =blk_disk_suspend (disk); +if (res) +return res; + + lim =queue_limits_start_update (q); res = entry->store_limit (disk, page, length, &lim);if (res <0 ) {queue_limits_cancel_update (q); -return res; + goto out_resume; } - res =queue_limits_commit_update_frozen (q, &lim); -if (res) -return res; -return length; + res =blk_disk_commit_limits (disk, &lim); +if (!res) + res = length; +out_resume: +blk_disk_resume (disk); +return res; }return entry->store (disk, page, length);
block/blk.h +20 -0
@@ -661,6 +661,26 @@ void drop_partition(struct block_device *part);void bdev_set_nr_sectors (struct block_device *bdev,sector_t sectors); +/* Suspend before queue_limits_start_update(), never under q->limits_lock. */ +static inline int blk_disk_suspend (struct gendisk *disk) +{ +return disk->fops->suspend ? disk->fops->suspend (disk) :0 ; +} + +static inline void blk_disk_resume (struct gendisk *disk) +{ +if (disk->fops->suspend) + disk->fops->resume (disk); +} + +static inline int blk_disk_commit_limits (struct gendisk *disk, +struct queue_limits *lim) +{ +if (disk->fops->suspend) +return queue_limits_commit_update (disk->queue, lim); +return queue_limits_commit_update_frozen (disk->queue, lim); +} +struct gendisk *__alloc_disk_node (struct request_queue *q,int node_id,struct lock_class_key *lkclass);struct request_queue *blk_alloc_queue (struct queue_limits *lim,int node_id);
include/linux/blkdev.h +7 -0
@@ -1601,6 +1601,13 @@ struct block_device_operations { enum blk_unique_id id_type);int (*init_dma_buf_io_ctx)(struct block_device *,struct dma_buf_io_ctx *); +/* +* Optional, for bio based drivers that cannot be frozen under +* q->limits_lock: stop I/O around a sysfs limits update. Called +* before q->limits_lock is taken, with no queue frozen. +*/ +int (*suspend)(struct gendisk *disk); +void (*resume)(struct gendisk *disk);struct module *owner;const struct pr_ops *pr_ops;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 1/2] block: suspend bio based disks around queue limits updates
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] block: missing __releases() annotation on blk_disk_commit_limits() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] block: missing __releases() annotation on blk_disk_commit_limits()
--
commit 191cb84f3b159c80a0741f7887d437ec63eb1187
Author: Jack Wang <[email protected]>
block: suspend bio based disks around queue limits updates
This commit adds optional ->suspend() and ->resume() block_device_operations callbacks and uses them in queue_attr_store() and flag_store() to suspend the disk before taking q->limits_lock and commit queue limits without freezing the queue.
Quoted 17 lines
block/blk.h +9
-0@@ -661,6 +661,26 @@ void drop_partition(struct block_device *part);void bdev_set_nr_sectors (struct block_device *bdev,sector_t sectors); +/* Suspend before queue_limits_start_update(), never under q->limits_lock. */ +static inline int blk_disk_suspend (struct gendisk *disk) +{ +return disk->fops->suspend ? disk->fops->suspend (disk) :0 ; +} + +static inline void blk_disk_resume (struct gendisk *disk) +{ +if (disk->fops->suspend)
block/blk.h void drop_partition(struct block_device *part); · line 682
Quoted 10 lines
block/blk.h +10
-0+ disk->fops->resume (disk); +} + +static inline int blk_disk_commit_limits (struct gendisk *disk, +struct queue_limits *lim) +{ +if (disk->fops->suspend) +return queue_limits_commit_update (disk->queue, lim); +return queue_limits_commit_update_frozen (disk->queue, lim); +}
[Severity: Low]
Should blk_disk_commit_limits() have a __releases(&disk->queue->limits_lock) annotation?
block/Makefile enables CONTEXT_ANALYSIS := y, and both queue_limits_commit_update() and queue_limits_commit_update_frozen() are annotated with __releases(&q->limits_lock) to pair with queue_limits_start_update(). Without the annotation on blk_disk_commit_limits(), building with Clang and CONFIG_WARN_CONTEXT_ANALYSIS=y triggers -Wthread-safety warnings in blk_disk_commit_limits() and its callers flag_store() and queue_attr_store().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
kernel test robot
build botRe: [PATCH v4 1/2] block: suspend bio based disks around queue limits updates
In reply to Jack Wangkernel test robot noticed the following build warnings:
Hi Jack,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4]
url: https://github.com/intel-lab-lkp/linux/commits/Jack-Wang/block-suspend-bio-based-disks-around-queue-limits-updates/20261112-144659 base: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 patch link: https://lore.kernel.org/r/20261112134700.372702-2-jinpu.wang%40cloud.ionos.com patch subject: [PATCH v4 1/2] block: suspend bio based disks around queue limits updates config: arm-randconfig-1002-20261112 (https://download.01.org/0day-ci/archive/20261113/[email protected]/config) compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 1b1fed44d3dcca649a9877915d57c3357a98124e) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261113/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from block/blk-map.c:12:
>> block/blk.h:680:10: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]
680 | return queue_limits_commit_update(disk->queue, lim);
| ^
block/blk.h:681:9: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]
681 | return queue_limits_commit_update_frozen(disk->queue, lim);
| ^
2 warnings generated.
--
In file included from block/blk-sysfs.c:14:
>> block/blk.h:680:10: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]
680 | return queue_limits_commit_update(disk->queue, lim);
| ^
block/blk.h:681:9: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]
681 | return queue_limits_commit_update_frozen(disk->queue, lim);
| ^
>> block/blk-sysfs.c:909:3: warning: mutex '(kobj - #undefined)->queue->limits_lock' is not held on every path through here [-Wthread-safety-analysis]
909 | blk_disk_resume(disk);
| ^
block/blk-sysfs.c:897:9: note: mutex acquired here
897 | lim = queue_limits_start_update(q);
| ^3 warnings generated.
vim +680 block/blk.h
675
676 static inline int blk_disk_commit_limits(struct gendisk *disk,
677 struct queue_limits *lim)
678 {
679 if (disk->fops->suspend)
> 680 return queue_limits_commit_update(disk->queue, lim);
681 return queue_limits_commit_update_frozen(disk->queue, lim);
682 }
683
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH v4 1/2] block: suspend bio based disks around queue limits updates
Hi, all
I will fix the __releases(&disk->queue->limits_lock) annotation in next version, will wait a bit if any other comments.
Thanks!
Quoted 62 lines · kernel test robot
Hi Jack,
kernel test robot noticed the following build warnings:
[auto build test WARNING on
6628e7d4c6b25c8d18f1345e3a20d21829aae7e4]url: https://github.com/intel-lab-lkp/linux/commits/Jack-Wang/block-suspend-bio-based-disks-around-queue-limits-updates/20261112-144659 base: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 patch link: https://lore.kernel.org/r/20261112134700.372702-2-jinpu.wang%40cloud.ionos.com patch subject: [PATCH v4 1/2] block: suspend bio based disks around queue limits updates config: arm-randconfig-1002-20261112 (https://download.01.org/0day-ci/archive/20261113/[email protected]/config) compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 1b1fed44d3dcca649a9877915d57c3357a98124e) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261113/[email protected]/reproduce)If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/All warnings (new ones prefixed by >>):
In file included from
block/blk-map.c:12:Quoted 1 line
Quoted 1 line
block/blk.h:680:10: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]680 | return queue_limits_commit_update(disk->queue, lim); | ^ block/blk.h:681:9: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis] 681 | return queue_limits_commit_update_frozen(disk->queue, lim); | ^2 warnings generated.
--
In file included fromblock/blk-sysfs.c:14:Quoted 1 line
Quoted 1 line
block/blk.h:680:10: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]680 | return queue_limits_commit_update(disk->queue, lim); | ^ block/blk.h:681:9: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis] 681 | return queue_limits_commit_update_frozen(disk->queue, lim); | ^Quoted 1 line
Quoted 1 line
block/blk-sysfs.c:909:3: warning: mutex '(kobj - #undefined)->queue->limits_lock' is not held on every path through here [-Wthread-safety-analysis]909 | blk_disk_resume(disk); | ^ block/blk-sysfs.c:897:9: note: mutex acquired here 897 | lim = queue_limits_start_update(q); | ^3 warnings generated.
vim +680
block/blk.h675 676 static inline int blk_disk_commit_limits(struct gendisk *disk, 677 struct queue_limits *lim) 678 { 679 if (disk->fops->suspend)Quoted 1 line
680 return
queue_limits_commit_update(disk->queue, lim);681 return queue_limits_commit_update_frozen(disk->queue, lim); 682 } 683--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v4 1/2] block: suspend bio based disks around queue limits updates
In reply to Jack Wangkernel test robot noticed the following build warnings:
Hi Jack,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4]
url: https://github.com/intel-lab-lkp/linux/commits/Jack-Wang/block-suspend-bio-based-disks-around-queue-limits-updates/20261112-144659 base: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4 patch link: https://lore.kernel.org/r/20261112134700.372702-2-jinpu.wang%40cloud.ionos.com patch subject: [PATCH v4 1/2] block: suspend bio based disks around queue limits updates config: loongarch-defconfig (https://download.01.org/0day-ci/archive/20261113/[email protected]/config) compiler: clang version 24.0.0git (https://github.com/llvm/llvm-project 1b1fed44d3dcca649a9877915d57c3357a98124e) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261113/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from block/blk-integrity.c:18:
block/blk.h:680:10: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]
680 | return queue_limits_commit_update(disk->queue, lim);
| ^
block/blk.h:681:9: warning: releasing mutex 'disk->queue->limits_lock' that was not held [-Wthread-safety-analysis]
681 | return queue_limits_commit_update_frozen(disk->queue, lim);
| ^
>> block/blk-integrity.c:255:1: warning: mutex '(dev - #undefined)->bd_disk->queue->limits_lock' is not held on every path through here [-Wthread-safety-analysis]
255 | }
| ^
block/blk-integrity.c:244:8: note: mutex acquired here
244 | lim = queue_limits_start_update(q);
| ^3 warnings generated.
vim +255 block/blk-integrity.c
e9f5f44ad372533 Christoph Hellwig 2024-06-13 225
b83bd486b43d2b7 Kanchan Joshi 2024-06-17 226 static ssize_t flag_store(struct device *dev, const char *page, size_t count,
b83bd486b43d2b7 Kanchan Joshi 2024-06-17 227 unsigned char flag)
1366251a794b149 Christoph Hellwig 2024-06-13 228 {
9a7e3243fd42730 Jack Wang 2026-11-12 229 struct gendisk *disk = dev_to_disk(dev);
9a7e3243fd42730 Jack Wang 2026-11-12 230 struct request_queue *q = disk->queue;
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 231 struct queue_limits lim;
1d59857ed2ec4d5 Christoph Hellwig 2024-06-13 232 unsigned long val;
1d59857ed2ec4d5 Christoph Hellwig 2024-06-13 233 int err;
1d59857ed2ec4d5 Christoph Hellwig 2024-06-13 234
1d59857ed2ec4d5 Christoph Hellwig 2024-06-13 235 err = kstrtoul(page, 10, &val);
1d59857ed2ec4d5 Christoph Hellwig 2024-06-13 236 if (err)
1d59857ed2ec4d5 Christoph Hellwig 2024-06-13 237 return err;
1366251a794b149 Christoph Hellwig 2024-06-13 238
9a7e3243fd42730 Jack Wang 2026-11-12 239 err = blk_disk_suspend(disk);
9a7e3243fd42730 Jack Wang 2026-11-12 240 if (err)
9a7e3243fd42730 Jack Wang 2026-11-12 241 return err;
9a7e3243fd42730 Jack Wang 2026-11-12 242
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 243 /* note that the flags are inverted vs the values in the sysfs files */
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 244 lim = queue_limits_start_update(q);
1366251a794b149 Christoph Hellwig 2024-06-13 245 if (val)
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 246 lim.integrity.flags &= ~flag;
9f4aa46f2a74010 Christoph Hellwig 2024-06-13 247 else
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 248 lim.integrity.flags |= flag;
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 249
9a7e3243fd42730 Jack Wang 2026-11-12 250 err = blk_disk_commit_limits(disk, &lim);
9a7e3243fd42730 Jack Wang 2026-11-12 251 blk_disk_resume(disk);
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 252 if (err)
c6e56cf6b2e79a4 Christoph Hellwig 2024-06-13 253 return err;
1366251a794b149 Christoph Hellwig 2024-06-13 254 return count;1366251a794b149 Christoph Hellwig 2024-06-13 @255 } 1366251a794b149 Christoph Hellwig 2024-06-13 256
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
PATCH v4 2/2 md: suspend the array instead of freezing the queue for limits updates
In reply to Jack WangProvide ->suspend() and ->resume() for md_fops using mddev_suspend() and mddev_resume(), so sysfs limits updates no longer freeze the queue.
mddev_suspend() only waits for active_io, but md_submit_bio() reads the limits in bio_split_to_limits() before taking it. A bio already there may see a mix of old and new limits, as with mddev_update_io_opt().
--- drivers/md/md.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
drivers/md/md.c +12 -0
@@ -8669,6 +8669,16 @@ static unsigned int md_check_events(struct gendisk *disk, unsigned int clearing)return ret; } +static int md_suspend_disk (struct gendisk *disk) +{ +return mddev_suspend (disk->private_data, true); +} + +static void md_resume_disk (struct gendisk *disk) +{ +mddev_resume (disk->private_data); +} +static void md_free_disk (struct gendisk *disk) {struct mddev *mddev = disk->private_data;
@@ -8689,6 +8699,8 @@ const struct block_device_operations md_fops = .getgeo = md_getgeo, .check_events = md_check_events, .set_read_only = md_set_read_only, + .suspend = md_suspend_disk, + .resume = md_resume_disk, .free_disk = md_free_disk, };
Signature · 1 line
2.43.0