Skip to contentSkip to conversation

md: do not _put wrong device

PATCH Superseded Mariusz Tkaczyk 3 participants 3 messages Sep 11, 2023 – Sep 12, 2023 MD RAID

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 md: do not _put wrong device

Put the device which has been _get with previous md_seq_next call. Add guard for improper mddev_put usage(). It shouldn't be called if there are less than 1 for mddev->active.

I didn't convert atomic_t to refcount_t because refcount warns when 0 is achieved which is likely to happen for mddev->active.

It fixes [1], I'm unable to reproduce this issue now.

[1]: https://bugzilla.kernel.org/show_bug.cgi?id=217798

---

There is md_seq cleanup proposed by Yu, this one should be merged first, because it is low risk fix for particular regression.

This is not complete fix for the problem, we need to prevent new open in the middle of removal, I will propose patchset separately.

 drivers/md/md.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/md/md.c +7 -2
index 0fe7ab6e8ab9..ffae02406175 100644
@@ -618,6 +618,12 @@ static void mddev_delayed_delete(struct work_struct *ws);
 
 void mddev_put(struct mddev *mddev)
 {
+	/* Guard for ambiguous put. */
+	if (unlikely(atomic_read(&mddev->active) < 1)) {
+		pr_warn("%s: active refcount is lower than 1\n", mdname(mddev));
+		return;
+	}
+
 	if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
 		return;
 	if (!mddev->raid_disks && list_empty(&mddev->disks) &&
@@ -8250,8 +8256,7 @@ static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 		next_mddev = list_entry(tmp, struct mddev, all_mddevs);
 		if (mddev_get(next_mddev))
 			break;
-		mddev = next_mddev;
-		tmp = mddev->all_mddevs.next;
+		tmp = next_mddev->all_mddevs.next;
 	}
 	spin_unlock(&all_mddevs_lock);
 
Signature · 1 line
2.26.2
maintainer
#1 lore raw
Quoted 5 lines · Mariusz Tkaczyk

Put the device which has been _get with previous md_seq_next call. Add guard for improper mddev_put usage(). It shouldn't be called if there are less than 1 for mddev->active.

Quoted 7 lines

I didn't convert atomic_t to refcount_t because refcount warns when 0 is achieved which is likely to happen for mddev->active.

It fixes [1], I'm unable to reproduce this issue now.

[1]: https://bugzilla.kernel.org/show_bug.cgi?id=217798

This patch somehow didn't make it through lore. Please repost and CC folks so we can test it thoroughly. Also, please add more information about the failure for future reference.

Thanks,
Song

Quoted 41 lines

---

There is md_seq cleanup proposed by Yu, this one should be merged first, because it is low risk fix for particular regression.

This is not complete fix for the problem, we need to prevent new open in the middle of removal, I will propose patchset separately.

 drivers/md/md.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/md/md.c +7 -3
index 0fe7ab6e8ab9..ffae02406175 100644
@@ -618,6 +618,12 @@ static void mddev_delayed_delete(struct work_struct *ws);
 
 void mddev_put(struct mddev *mddev)
 {
+       /* Guard for ambiguous put. */
+       if (unlikely(atomic_read(&mddev->active) < 1)) {
+               pr_warn("%s: active refcount is lower than 1\n", mdname(mddev));
+               return;
+       }
+
        if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
                return;
        if (!mddev->raid_disks && list_empty(&mddev->disks) &&
@@ -8250,8 +8256,7 @@ static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
                next_mddev = list_entry(tmp, struct mddev, all_mddevs);
                if (mddev_get(next_mddev))
                        break;
-               mddev = next_mddev;
-               tmp = mddev->all_mddevs.next;
+               tmp = next_mddev->all_mddevs.next;
        }
        spin_unlock(&all_mddevs_lock);
 
--

2.26.2

#2 lore raw
In reply to Mariusz Tkaczyk

Hi,

Quoted 9 lines · Mariusz Tkaczyk

Put the device which has been _get with previous md_seq_next call. Add guard for improper mddev_put usage(). It shouldn't be called if there are less than 1 for mddev->active.

I didn't convert atomic_t to refcount_t because refcount warns when 0 is achieved which is likely to happen for mddev->active.

It fixes [1], I'm unable to reproduce this issue now.

Please add a fix tag:

Fixes 12a6caf27324 ("md: only delete entries from all_mddevs when the disk is freed")
Quoted 32 lines

[1]: https://bugzilla.kernel.org/show_bug.cgi?id=217798

---

There is md_seq cleanup proposed by Yu, this one should be merged first, because it is low risk fix for particular regression.

This is not complete fix for the problem, we need to prevent new open in the middle of removal, I will propose patchset separately.

  drivers/md/md.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/md/md.c +6 -0
index 0fe7ab6e8ab9..ffae02406175 100644
@@ -618,6 +618,12 @@ static void mddev_delayed_delete(struct work_struct *ws);
 
  void mddev_put(struct mddev *mddev)
  {
+	/* Guard for ambiguous put. */
+	if (unlikely(atomic_read(&mddev->active) < 1)) {
+		pr_warn("%s: active refcount is lower than 1\n", mdname(mddev));
+		return;
+	}
+
  	if (!atomic_dec_and_lock(&mddev->active, &all_mddevs_lock))
  		return;
  	if (!mddev->raid_disks && list_empty(&mddev->disks) &&

drivers/md/md.c static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos) · line 8262

Quoted 9 lines
drivers/md/md.c +1 -2
@@ -8250,8 +8256,7 @@ static void *md_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  		next_mddev = list_entry(tmp, struct mddev, all_mddevs);
  		if (mddev_get(next_mddev))
  			break;
-		mddev = next_mddev;
-		tmp = mddev->all_mddevs.next;
+		tmp = next_mddev->all_mddevs.next;
  	}
  	spin_unlock(&all_mddevs_lock);
 

And looks like commit 12a6caf27324 wants to mddev_put(to_put), and with your change, 'to_put' is not needed as well, can you also remove the local variable 'to_put'?

Thanks,
Kuai