Skip to content

Commit 432db83

Browse files
committed
md: add legacy_async_del_gendisk mode
JIRA: https://issues.redhat.com/browse/RHEL-123668 commit 9e59d60 ("md: call del_gendisk in control path") changes the async way to sync way of calling del_gendisk. But it breaks mdadm --assemble command. The assemble command runs like this: 1. create the array 2. stop the array 3. access the sysfs files after stopping The sync way calls del_gendisk in step 2, so all sysfs files are removed. Now to avoid breaking mdadm assemble command, this patch adds the parameter legacy_async_del_gendisk that can be used to choose which way. The default is async way. In future, we plan to change default to sync way in kernel 7.0. Then users need to upgrade to mdadm 4.5+ which removes step 2. Fixes: 9e59d60 ("md: call del_gendisk in control path") Reported-by: Mikulas Patocka <mpatocka@redhat.com> Closes: https://lore.kernel.org/linux-raid/CAMw=ZnQ=ET2St-+hnhsuq34rRPnebqcXqP1QqaHW5Bh4aaaZ4g@mail.gmail.com/T/#t Suggested-and-reviewed-by: Yu Kuai <yukuai3@huawei.com> Signed-off-by: Xiao Ni <xni@redhat.com> Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de> Link: https://lore.kernel.org/linux-raid/20250813032929.54978-1-xni@redhat.com Signed-off-by: Yu Kuai <yukuai3@huawei.com> (cherry picked from commit 25db5f2) Signed-off-by: Nigel Croxon <ncroxon@redhat.com>
1 parent 0276108 commit 432db83

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

drivers/md/md.c

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ static int start_readonly;
339339
* so all the races disappear.
340340
*/
341341
static bool create_on_open = true;
342+
static bool legacy_async_del_gendisk = true;
342343

343344
/*
344345
* We have a system wide 'event count' that is incremented
@@ -877,15 +878,18 @@ void mddev_unlock(struct mddev *mddev)
877878
export_rdev(rdev, mddev);
878879
}
879880

880-
/* Call del_gendisk after release reconfig_mutex to avoid
881-
* deadlock (e.g. call del_gendisk under the lock and an
882-
* access to sysfs files waits the lock)
883-
* And MD_DELETED is only used for md raid which is set in
884-
* do_md_stop. dm raid only uses md_stop to stop. So dm raid
885-
* doesn't need to check MD_DELETED when getting reconfig lock
886-
*/
887-
if (test_bit(MD_DELETED, &mddev->flags))
888-
del_gendisk(mddev->gendisk);
881+
if (!legacy_async_del_gendisk) {
882+
/*
883+
* Call del_gendisk after release reconfig_mutex to avoid
884+
* deadlock (e.g. call del_gendisk under the lock and an
885+
* access to sysfs files waits the lock)
886+
* And MD_DELETED is only used for md raid which is set in
887+
* do_md_stop. dm raid only uses md_stop to stop. So dm raid
888+
* doesn't need to check MD_DELETED when getting reconfig lock
889+
*/
890+
if (test_bit(MD_DELETED, &mddev->flags))
891+
del_gendisk(mddev->gendisk);
892+
}
889893
}
890894
EXPORT_SYMBOL_GPL(mddev_unlock);
891895

@@ -5877,6 +5881,13 @@ static void md_kobj_release(struct kobject *ko)
58775881
{
58785882
struct mddev *mddev = container_of(ko, struct mddev, kobj);
58795883

5884+
if (legacy_async_del_gendisk) {
5885+
if (mddev->sysfs_state)
5886+
sysfs_put(mddev->sysfs_state);
5887+
if (mddev->sysfs_level)
5888+
sysfs_put(mddev->sysfs_level);
5889+
del_gendisk(mddev->gendisk);
5890+
}
58805891
put_disk(mddev->gendisk);
58815892
}
58825893

@@ -6085,6 +6096,9 @@ static int md_alloc_and_put(dev_t dev, char *name)
60856096
{
60866097
struct mddev *mddev = md_alloc(dev, name);
60876098

6099+
if (legacy_async_del_gendisk)
6100+
pr_warn("md: async del_gendisk mode will be removed in future, please upgrade to mdadm-4.5+\n");
6101+
60886102
if (IS_ERR(mddev))
60896103
return PTR_ERR(mddev);
60906104
mddev_put(mddev);
@@ -6498,10 +6512,22 @@ static void md_clean(struct mddev *mddev)
64986512
mddev->persistent = 0;
64996513
mddev->level = LEVEL_NONE;
65006514
mddev->clevel[0] = 0;
6501-
/* if UNTIL_STOP is set, it's cleared here */
6502-
mddev->hold_active = 0;
6503-
/* Don't clear MD_CLOSING, or mddev can be opened again. */
6504-
mddev->flags &= BIT_ULL_MASK(MD_CLOSING);
6515+
6516+
/*
6517+
* For legacy_async_del_gendisk mode, it can stop the array in the
6518+
* middle of assembling it, then it still can access the array. So
6519+
* it needs to clear MD_CLOSING. If not legacy_async_del_gendisk,
6520+
* it can't open the array again after stopping it. So it doesn't
6521+
* clear MD_CLOSING.
6522+
*/
6523+
if (legacy_async_del_gendisk && mddev->hold_active) {
6524+
clear_bit(MD_CLOSING, &mddev->flags);
6525+
} else {
6526+
/* if UNTIL_STOP is set, it's cleared here */
6527+
mddev->hold_active = 0;
6528+
/* Don't clear MD_CLOSING, or mddev can be opened again. */
6529+
mddev->flags &= BIT_ULL_MASK(MD_CLOSING);
6530+
}
65056531
mddev->sb_flags = 0;
65066532
mddev->ro = MD_RDWR;
65076533
mddev->metadata_type[0] = 0;
@@ -6728,7 +6754,8 @@ static int do_md_stop(struct mddev *mddev, int mode)
67286754

67296755
export_array(mddev);
67306756
md_clean(mddev);
6731-
set_bit(MD_DELETED, &mddev->flags);
6757+
if (!legacy_async_del_gendisk)
6758+
set_bit(MD_DELETED, &mddev->flags);
67326759
}
67336760
md_new_event();
67346761
sysfs_notify_dirent_safe(mddev->sysfs_state);
@@ -10466,6 +10493,7 @@ module_param_call(start_ro, set_ro, get_ro, NULL, S_IRUSR|S_IWUSR);
1046610493
module_param(start_dirty_degraded, int, S_IRUGO|S_IWUSR);
1046710494
module_param_call(new_array, add_named_array, NULL, NULL, S_IWUSR);
1046810495
module_param(create_on_open, bool, S_IRUSR|S_IWUSR);
10496+
module_param(legacy_async_del_gendisk, bool, 0600);
1046910497

1047010498
MODULE_LICENSE("GPL");
1047110499
MODULE_DESCRIPTION("MD RAID framework");

0 commit comments

Comments
 (0)