Commit 99892147 authored by Yu Kuai's avatar Yu Kuai Committed by Song Liu

md: fix warning for holder mismatch from export_rdev()

Commit a1d76719 ("md: use mddev->external to select holder in
export_rdev()") fix the problem that 'claim_rdev' is used for
blkdev_get_by_dev() while 'rdev' is used for blkdev_put().

However, if mddev->external is changed from 0 to 1, then 'rdev' is used
for blkdev_get_by_dev() while 'claim_rdev' is used for blkdev_put(). And
this problem can be reporduced reliably by following:

New file: mdadm/tests/23rdev-lifetime

devname=${dev0##*/}
devt=`cat /sys/block/$devname/dev`
pid=""
runtime=2

clean_up_test() {
        pill -9 $pid
        echo clear > /sys/block/md0/md/array_state
}

trap 'clean_up_test' EXIT

add_by_sysfs() {
        while true; do
                echo $devt > /sys/block/md0/md/new_dev
        done
}

remove_by_sysfs(){
        while true; do
                echo remove > /sys/block/md0/md/dev-${devname}/state
        done
}

echo md0 > /sys/module/md_mod/parameters/new_array || die "create md0 failed"

add_by_sysfs &
pid="$pid $!"

remove_by_sysfs &
pid="$pid $!"

sleep $runtime
exit 0

Test cmd:

./test --save-logs --logdir=/tmp/ --keep-going --dev=loop --tests=23rdev-lifetime

Test result:

------------[ cut here ]------------
WARNING: CPU: 0 PID: 960 at block/bdev.c:618 blkdev_put+0x27c/0x330
Modules linked in: multipath md_mod loop
CPU: 0 PID: 960 Comm: test Not tainted 6.5.0-rc2-00121-g01e55c376936-dirty #50
RIP: 0010:blkdev_put+0x27c/0x330
Call Trace:
 <TASK>
 export_rdev.isra.23+0x50/0xa0 [md_mod]
 mddev_unlock+0x19d/0x300 [md_mod]
 rdev_attr_store+0xec/0x190 [md_mod]
 sysfs_kf_write+0x52/0x70
 kernfs_fop_write_iter+0x19a/0x2a0
 vfs_write+0x3b5/0x770
 ksys_write+0x74/0x150
 __x64_sys_write+0x22/0x30
 do_syscall_64+0x40/0x90
 entry_SYSCALL_64_after_hwframe+0x63/0xcd

Fix the problem by recording if 'rdev' is used as holder.

Fixes: a1d76719 ("md: use mddev->external to select holder in export_rdev()")
Signed-off-by: default avatarYu Kuai <yukuai3@huawei.com>
Signed-off-by: default avatarSong Liu <song@kernel.org>
Link: https://lore.kernel.org/r/20230825025532.1523008-3-yukuai1@huaweicloud.com
parent 7deac114
...@@ -2452,7 +2452,8 @@ static void export_rdev(struct md_rdev *rdev, struct mddev *mddev) ...@@ -2452,7 +2452,8 @@ static void export_rdev(struct md_rdev *rdev, struct mddev *mddev)
if (test_bit(AutoDetected, &rdev->flags)) if (test_bit(AutoDetected, &rdev->flags))
md_autodetect_dev(rdev->bdev->bd_dev); md_autodetect_dev(rdev->bdev->bd_dev);
#endif #endif
blkdev_put(rdev->bdev, mddev->external ? &claim_rdev : rdev); blkdev_put(rdev->bdev,
test_bit(Holder, &rdev->flags) ? rdev : &claim_rdev);
rdev->bdev = NULL; rdev->bdev = NULL;
kobject_put(&rdev->kobj); kobject_put(&rdev->kobj);
} }
...@@ -3632,6 +3633,7 @@ EXPORT_SYMBOL_GPL(md_rdev_init); ...@@ -3632,6 +3633,7 @@ EXPORT_SYMBOL_GPL(md_rdev_init);
static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor) static struct md_rdev *md_import_device(dev_t newdev, int super_format, int super_minor)
{ {
struct md_rdev *rdev; struct md_rdev *rdev;
struct md_rdev *holder;
sector_t size; sector_t size;
int err; int err;
...@@ -3646,8 +3648,15 @@ static struct md_rdev *md_import_device(dev_t newdev, int super_format, int supe ...@@ -3646,8 +3648,15 @@ static struct md_rdev *md_import_device(dev_t newdev, int super_format, int supe
if (err) if (err)
goto out_clear_rdev; goto out_clear_rdev;
if (super_format == -2) {
holder = &claim_rdev;
} else {
holder = rdev;
set_bit(Holder, &rdev->flags);
}
rdev->bdev = blkdev_get_by_dev(newdev, BLK_OPEN_READ | BLK_OPEN_WRITE, rdev->bdev = blkdev_get_by_dev(newdev, BLK_OPEN_READ | BLK_OPEN_WRITE,
super_format == -2 ? &claim_rdev : rdev, NULL); holder, NULL);
if (IS_ERR(rdev->bdev)) { if (IS_ERR(rdev->bdev)) {
pr_warn("md: could not open device unknown-block(%u,%u).\n", pr_warn("md: could not open device unknown-block(%u,%u).\n",
MAJOR(newdev), MINOR(newdev)); MAJOR(newdev), MINOR(newdev));
...@@ -3684,7 +3693,7 @@ static struct md_rdev *md_import_device(dev_t newdev, int super_format, int supe ...@@ -3684,7 +3693,7 @@ static struct md_rdev *md_import_device(dev_t newdev, int super_format, int supe
return rdev; return rdev;
out_blkdev_put: out_blkdev_put:
blkdev_put(rdev->bdev, super_format == -2 ? &claim_rdev : rdev); blkdev_put(rdev->bdev, holder);
out_clear_rdev: out_clear_rdev:
md_rdev_clear(rdev); md_rdev_clear(rdev);
out_free_rdev: out_free_rdev:
......
...@@ -211,6 +211,9 @@ enum flag_bits { ...@@ -211,6 +211,9 @@ enum flag_bits {
* check if there is collision between raid1 * check if there is collision between raid1
* serial bios. * serial bios.
*/ */
Holder, /* rdev is used as holder while opening
* underlying disk exclusively.
*/
}; };
static inline int is_badblock(struct md_rdev *rdev, sector_t s, int sectors, static inline int is_badblock(struct md_rdev *rdev, sector_t s, int sectors,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment