From: Christoph Hellwig <hch@lst.de> Date: 2021-09-01 11:39:11
Hi Song,
the first patch in this series fixed the reported lock order reversal
in md_alloc. The rest sort out the error handling in that function,
starting with the patch from Luis to handle add_disk errors, which
would otherwise conflict with the first patch.
Note that I have had a hard time verifying this all works fine as the
testsuite in mdadm already keeps failing a lot for me with the baseline
kernel. Some of thos reproducibly and others randomly. Is there a
good document somehow describing what to expect from the mdadm test
suite?
Diffstat:
drivers/md/md.c | 56 +++++++++++++++++++++++++++++---------------------------
1 file changed, 29 insertions(+), 27 deletions(-)
From: Christoph Hellwig <hch@lst.de> Date: 2021-09-01 11:40:11
Commit b0140891a8cea3 ("md: Fix race when creating a new md device.")
not only moved assigning mddev->gendisk before calling add_disk, which
fixes the races described in the commit log, but also added a
mddev->open_mutex critical section over add_disk and creation of the
md kobj. Adding a kobject after add_disk is racy vs deleting the gendisk
right after adding it, but md already prevents against that by holding
a mddev->active reference.
On the other hand taking this lock added a lock order reversal with what
is not disk->open_mutex (used to be bdev->bd_mutex when the commit was
added) for partition devices, which need that lock for the internal open
for the partition scan, and a recent commit also takes it for
non-partitioned devices, leading to further lockdep splatter.
Fixes: b0140891a8ce ("md: Fix race when creating a new md device.")
Fixes: d62633873590 ("block: support delayed holder registration")
Reported-by: syzbot+fadc0aaf497e6a493b9f@syzkaller.appspotmail.com
Signed-off-by: Christoph Hellwig <hch@lst.de>
Tested-by: syzbot+fadc0aaf497e6a493b9f@syzkaller.appspotmail.com
---
drivers/md/md.c | 5 -----
1 file changed, 5 deletions(-)
@@ -5700,10 +5700,6 @@ static int md_alloc(dev_t dev, char *name)disk->flags|=GENHD_FL_EXT_DEVT;disk->events|=DISK_EVENT_MEDIA_CHANGE;mddev->gendisk=disk;-/* As soon as we call add_disk(), another thread could get-*throughtomd_open,somakesureitdoesn'tgettoofar-*/-mutex_lock(&mddev->open_mutex);add_disk(disk);error=kobject_add(&mddev->kobj,&disk_to_dev(disk)->kobj,"%s","md");
From: Christoph Hellwig <hch@lst.de> Date: 2021-09-01 11:41:15
From: Luis Chamberlain <mcgrof@kernel.org>
We never checked for errors on add_disk() as this function
returned void. Now that this is fixed, use the shiny new
error handling.
We just do the unwinding of what was not done before, and are
sure to unlock prior to bailing.
Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
drivers/md/md.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
From: Christoph Hellwig <hch@lst.de> Date: 2021-09-01 11:42:12
Replace the deprecated default_attrs with the default_groups mechanism,
and add the always visible bitmap group to the groups created add
kobject_add time.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
drivers/md/md.c | 16 +++++++++++-----
1 file changed, 11 insertions(+), 5 deletions(-)
From: Christoph Hellwig <hch@lst.de> Date: 2021-09-01 11:43:29
disks_mutex is intended to serialize md_alloc. Extended it to also cover
the kobject_uevent call and getting the sysfs dirent to help reducing
error handling complexity.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
drivers/md/md.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
From: Christoph Hellwig <hch@lst.de> Date: 2021-09-01 11:44:28
Add proper error handling to delete the gendisk when failing to add
the md kobject and clean up the error unwinding in general.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
drivers/md/md.c | 37 +++++++++++++++++--------------------
1 file changed, 17 insertions(+), 20 deletions(-)
@@ -5672,7 +5672,7 @@ static int md_alloc(dev_t dev, char *name)strcmp(mddev2->gendisk->disk_name,name)==0){spin_unlock(&all_mddevs_lock);error=-EEXIST;-gotoabort;+gotoout_unlock_disks_mutex;}spin_unlock(&all_mddevs_lock);}
@@ -5685,7 +5685,7 @@ static int md_alloc(dev_t dev, char *name)error=-ENOMEM;disk=blk_alloc_disk(NUMA_NO_NODE);if(!disk)-gotoabort;+gotoout_unlock_disks_mutex;disk->major=MAJOR(mddev->unit);disk->first_minor=unit<<shift;
@@ -5710,26 +5710,23 @@ static int md_alloc(dev_t dev, char *name)disk->events|=DISK_EVENT_MEDIA_CHANGE;mddev->gendisk=disk;error=add_disk(disk);-if(error){-blk_cleanup_disk(disk);-gotoabort;-}+if(error)+gotoout_cleanup_disk;error=kobject_add(&mddev->kobj,&disk_to_dev(disk)->kobj,"%s","md");-if(error){-/* This isn't possible, but as kobject_init_and_add is marked-*__must_check,wemustdosomethingwiththeresult-*/-pr_debug("md: cannot register %s/md - name in use\n",-disk->disk_name);-error=0;-}-abort:-if(!error&&mddev->kobj.sd){-kobject_uevent(&mddev->kobj,KOBJ_ADD);-mddev->sysfs_state=sysfs_get_dirent_safe(mddev->kobj.sd,"array_state");-mddev->sysfs_level=sysfs_get_dirent_safe(mddev->kobj.sd,"level");-}+if(error)+gotoout_del_gendisk;++kobject_uevent(&mddev->kobj,KOBJ_ADD);+mddev->sysfs_state=sysfs_get_dirent_safe(mddev->kobj.sd,"array_state");+mddev->sysfs_level=sysfs_get_dirent_safe(mddev->kobj.sd,"level");+gotoout_unlock_disks_mutex;++out_del_gendisk:+del_gendisk(disk);+out_cleanup_disk:+blk_cleanup_disk(disk);+out_unlock_disks_mutex:mutex_unlock(&disks_mutex);mddev_put(mddev);returnerror;
From: Song Liu <song@kernel.org> Date: 2021-09-02 05:06:54
Hi Christoph,
On Wed, Sep 1, 2021 at 4:39 AM Christoph Hellwig [off-list ref] wrote:
Hi Song,
the first patch in this series fixed the reported lock order reversal
in md_alloc. The rest sort out the error handling in that function,
starting with the patch from Luis to handle add_disk errors, which
would otherwise conflict with the first patch.
Thanks for the fixes! I have noticed the issue, but haven't got a good
solution for it. I will test and apply the set (maybe after the labor day
long weekend).
Note that I have had a hard time verifying this all works fine as the
testsuite in mdadm already keeps failing a lot for me with the baseline
kernel. Some of thos reproducibly and others randomly. Is there a
good document somehow describing what to expect from the mdadm test
suite?
Unfortunately, mdadm test needs quite some work to be really useful again.
I will work with Jes and other folks to come up with a plan for this.
Thanks again,
Song
Commit b0140891a8cea3 ("md: Fix race when creating a new md device.")
not only moved assigning mddev->gendisk before calling add_disk, which
fixes the races described in the commit log, but also added a
mddev->open_mutex critical section over add_disk and creation of the
md kobj. Adding a kobject after add_disk is racy vs deleting the gendisk
right after adding it, but md already prevents against that by holding
a mddev->active reference.
Assuming you mean md_open calls mddev_find -> mddev_get
-> atomic_inc(&mddev->active), but the path had already existed
before b0140891a8c, and md_alloc also called mddev_find at
that time, not sure how it prevents the race though I probably missed
something. Cc Neil.
On the other hand taking this lock added a lock order reversal with what
is not disk->open_mutex (used to be bdev->bd_mutex when the commit was
added) for partition devices, which need that lock for the internal open
for the partition scan, and a recent commit also takes it for
non-partitioned devices, leading to further lockdep splatter.
Fixes: b0140891a8ce ("md: Fix race when creating a new md device.")
Fixes: d62633873590 ("block: support delayed holder registration")
IIUC, the issue appeared after d6263387359 (which was for dm issue),
perhaps stable maintainer should not apply this to any stable kernel
if it only includes b0140891a8ce.
Thanks,
Guoqing
Commit b0140891a8cea3 ("md: Fix race when creating a new md device.")
not only moved assigning mddev->gendisk before calling add_disk, which
fixes the races described in the commit log, but also added a
mddev->open_mutex critical section over add_disk and creation of the
md kobj. Adding a kobject after add_disk is racy vs deleting the gendisk
right after adding it, but md already prevents against that by holding
a mddev->active reference.
Assuming you mean md_open calls mddev_find -> mddev_get
-> atomic_inc(&mddev->active), but the path had already existed
before b0140891a8c, and md_alloc also called mddev_find at
that time, not sure how it prevents the race though I probably missed
something. Cc Neil.
It is a long time since I've looked much at this code, but I *think*
that the reason calling kobject_add() after add_disk() is safe is
because that gendisk is *only* deleted by md_free() which happens when
that kobject is 'put' - in mddev_delayed_delete(). That only gets
called when mddev->active is zero (from mddev_put().
So as Christoph says, the fact that md_alloc() hold a count on
mddev->active (until it calls mddeV_put() at the very end) makes this
safe.
As for the ->open_mutex locking - I have a vague memory that it was to
protect something that might happen *after* md_open() from running
before md_alloc() had completed.
add_disk() itself adds sufficient exclusion via disk->open_mutex, so it
could only be the kobject/sysfs manipulations.
All I can think if is that add_disk() would send an event to udev and udev
might do something before the 'md' directory had been created.
But the udev rules seem safe against that. In particular they check if
md/array_state exists before checking if it is clear or inactive.
I did struggle with some races like that, but if that was the problem,
I'm surprised that I didn't explain it in the commit message.
So in short: I cannot see why I added that locking, and I cannot see
that removing it would break anything.
Reviewed-by: NeilBrown <redacted>
NeilBrown
quoted
On the other hand taking this lock added a lock order reversal with what
is not disk->open_mutex (used to be bdev->bd_mutex when the commit was
added) for partition devices, which need that lock for the internal open
for the partition scan, and a recent commit also takes it for
non-partitioned devices, leading to further lockdep splatter.
Fixes: b0140891a8ce ("md: Fix race when creating a new md device.")
Fixes: d62633873590 ("block: support delayed holder registration")
IIUC, the issue appeared after d6263387359 (which was for dm issue),
perhaps stable maintainer should not apply this to any stable kernel
if it only includes b0140891a8ce.
Thanks,
Guoqing
From: Song Liu <song@kernel.org> Date: 2021-09-09 06:14:26
On Wed, Sep 1, 2021 at 4:39 AM Christoph Hellwig [off-list ref] wrote:
Hi Song,
the first patch in this series fixed the reported lock order reversal
in md_alloc. The rest sort out the error handling in that function,
starting with the patch from Luis to handle add_disk errors, which
would otherwise conflict with the first patch.
Note that I have had a hard time verifying this all works fine as the
testsuite in mdadm already keeps failing a lot for me with the baseline
kernel. Some of thos reproducibly and others randomly. Is there a
good document somehow describing what to expect from the mdadm test
suite?
Diffstat:
drivers/md/md.c | 56 +++++++++++++++++++++++++++++---------------------------
1 file changed, 29 insertions(+), 27 deletions(-)