Skip to contentSkip to conversation

md: clear MD_CLOSING when array_state_store() bails on interrupted lock

PATCH Superseded Jack Wang 4 participants 7 messages Jul 17 – Nov 6 MD RAID

lore
Share & tools
lore

Jack Wang

author
#0 lore raw

PATCH v2 1/1 md: clear MD_CLOSING when array_state_store() bails on interrupted lock

From: Jack Wang <[email protected]>

Writing "clear", "readonly", "inactive" or "read_auto" to array_state calls mddev_set_closing_and_sync_blockdev(), which sets MD_CLOSING so the array cannot be reopened while it is being torn down. The flag is only cleared again at the tail of array_state_store() (for readonly, read_auto, inactive, or the failed-clear case).

Between setting the flag and reaching that tail there is an early return:

	err = mddev_lock(mddev);
	if (err)
		return err;

mddev_lock() is mutex_lock_interruptible() on reconfig_mutex. If the writing task is signalled while blocked there - easy to hit when the mutex is held for a long time by a running resync/recovery or reshape - it returns -EINTR and array_state_store() returns with MD_CLOSING still set. do_md_stop()/md_set_readonly() never ran, so the array keeps working internally, but every subsequent md_open() now returns -ENODEV for all
callers: the device still shows up in /proc/mdstat and sysfs is fully populated, yet it cannot be opened and cannot be recovered without a reboot (the remaining clear paths either need the device open or re-enter test_and_set_bit(MD_CLOSING) and bail with -EBUSY before the clear).

Route the interrupted-lock exit through a common label so the flag is released. Guard the clear with a set_closing flag that records whether this write actually set MD_CLOSING: when mddev->pers is already NULL (for example a concurrent STOP_ARRAY is mid-teardown) this write skips the mddev_set_closing_and_sync_blockdev() call above, so it must not clear a MD_CLOSING that the other thread owns - doing so could let the array be reopened while it is being destroyed. This also tightens the pre-existing tail clear, which had the same unconditional behaviour.

mddev_unlock() is correctly skipped on the goto path since the lock was never acquired.

Fixes 99b902ac1725 ("md: sync blockdev before stopping raid or setting readonly")
Assisted-by Claude:claude-opus-4-8 [Claude Code]
---
 drivers/md/md.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/md/md.c +14 -3
index d1465bcd86c8..7b7a4d925907 100644
@@ -4649,6 +4649,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 {
 	int err = 0;
 	enum array_state st = match_word(buf, array_states);
+	bool set_closing = false;
 
 	/* No lock dependent actions */
 	switch (st) {
@@ -4668,6 +4669,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 		err = mddev_set_closing_and_sync_blockdev(mddev, 0);
 		if (err)
 			return err;
+		set_closing = true;
 		break;
 	default:
 		break;
@@ -4696,7 +4698,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 	}
 	err = mddev_lock(mddev);
 	if (err)
-		return err;
+		goto out_clear_closing;
 
 	switch (st) {
 	case inactive:
@@ -4769,8 +4771,17 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 	}
 	mddev_unlock(mddev);
 
-	if (st == readonly || st == read_auto || st == inactive ||
-	    (err && st == clear))
+out_clear_closing:
+	/*
+	 * Only clear MD_CLOSING if this write actually set it. Otherwise a
+	 * concurrent teardown (e.g. STOP_ARRAY) may own the flag - this write
+	 * would have skipped setting it above when mddev->pers was already
+	 * NULL - and clearing it here would let the array be reopened while it
+	 * is being destroyed.
+	 */
+	if (set_closing &&
+	    (st == readonly || st == read_auto || st == inactive ||
+	     (err && st == clear)))
 		clear_bit(MD_CLOSING, &mddev->flags);
 
 	return err ?: len;
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Calling restart_array() with the mddev->lock spinlock held can sleep because it clears the disk read-only flag, which triggers a GFP_KERNEL memory allocation for a uevent. --

--- Patch [1]: [PATCHv2] md: clear MD_CLOSING when array_state_store() bails on interrupted lock --- commit 3ae9b81fbeee45b412994652c2c3b3ac1b35d177
Author: Jack Wang <[email protected]>

md: clear MD_CLOSING when array_state_store() bails on interrupted lock

This commit ensures that MD_CLOSING is correctly cleared if mutex_lock_interruptible() fails while writing to array_state. It introduces a common exit label and a boolean flag to track whether the flag was actually set by the current thread.

Quoted 4 lines
drivers/md/md.c +0 -0
index d1465bcd86c81..7b7a4d9259079 100644

[ ... ]

drivers/md/md.c array_state_store(struct mddev *mddev, const char *buf, size_t len) · line 4701

Quoted 6 lines
drivers/md/md.c +1 -1
@@ -4696,7 +4698,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 	}
 	err = mddev_lock(mddev);
 	if (err)
-		return err;
+		goto out_clear_closing;

[Severity: High]
This is a pre-existing issue, but just above this code in array_state_store(), there is a fast path that can sleep while holding a spinlock.

If a privileged user sets the md device to read-only using blockdev --setro /dev/md0 and then writes 'active' to /sys/block/md0/md/array_state, the code acquires the mddev->lock spinlock and calls restart_array():

drivers/md/md.c:array_state_store() {
    ...
    spin_lock(&mddev->lock);
    if (st == active) {
        restart_array(mddev);
    ...
}

restart_array() unconditionally calls set_disk_ro() to ensure the disk is read-write:

drivers/md/md.c:restart_array() {
    ...
    set_disk_ro(disk, 0);
    ...
}

Because the disk was set to read-only, set_disk_ro() clears the flag and triggers a uevent:

block/genhd.c:set_disk_ro() {
    ...
    set_disk_ro_uevent(disk, read_only);
    ...
}

This calls into kobject_uevent_env(), which performs an unconditional GFP_KERNEL allocation:

lib/kobject_uevent.c:kobject_uevent_env() {
    ...
    devpath = kobject_get_path(kobj, GFP_KERNEL);
    ...
}

Can this sequence cause a sleep-in-atomic bug and potentially lead to a deadlock or panic?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw

Hi Team,

Regarding the pre-existing issue reported by the bot, it is not related to this fix and cannot be triggered in normal cases, so I will skip it for now.

Linux RAID folks, do you have a chance to check this patch? It fixes a problem we hit by chance.

Thanks,
Jinpu

Quoted 79 lines · On Fri, Jul 17, 2026 at 7:25 AM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Calling restart_array() with the mddev->lock spinlock held can sleep because it clears the disk read-only flag, which triggers a GFP_KERNEL memory allocation for a uevent. --

--- Patch [1]: [PATCHv2] md: clear MD_CLOSING when array_state_store() bails on interrupted lock --- commit 3ae9b81fbeee45b412994652c2c3b3ac1b35d177
Author: Jack Wang <[email protected]>

md: clear MD_CLOSING when array_state_store() bails on interrupted lock

This commit ensures that MD_CLOSING is correctly cleared if mutex_lock_interruptible() fails while writing to array_state. It introduces a common exit label and a boolean flag to track whether the flag was actually set by the current thread.

Quoted 4 lines
drivers/md/md.c +0 -0
index d1465bcd86c81..7b7a4d9259079 100644

[ ... ]

drivers/md/md.c array_state_store(struct mddev *mddev, const char *buf, size_t len) · line 4701

Quoted 6 lines
drivers/md/md.c +1 -1
@@ -4696,7 +4698,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
      }
      err = mddev_lock(mddev);
      if (err)
-             return err;
+             goto out_clear_closing;

[Severity: High]
This is a pre-existing issue, but just above this code in array_state_store(), there is a fast path that can sleep while holding a spinlock.

If a privileged user sets the md device to read-only using blockdev --setro /dev/md0 and then writes 'active' to /sys/block/md0/md/array_state, the code acquires the mddev->lock spinlock and calls restart_array():

drivers/md/md.c:array_state_store() {
    ...
    spin_lock(&mddev->lock);
    if (st == active) {
        restart_array(mddev);
    ...
}

restart_array() unconditionally calls set_disk_ro() to ensure the disk is read-write:

drivers/md/md.c:restart_array() {
    ...
    set_disk_ro(disk, 0);
    ...
}

Because the disk was set to read-only, set_disk_ro() clears the flag and triggers a uevent:

block/genhd.c:set_disk_ro() {
    ...
    set_disk_ro_uevent(disk, read_only);
    ...
}

This calls into kobject_uevent_env(), which performs an unconditional GFP_KERNEL allocation:

lib/kobject_uevent.c:kobject_uevent_env() {
    ...
    devpath = kobject_get_path(kobj, GFP_KERNEL);
    ...
}

Can this sequence cause a sleep-in-atomic bug and potentially lead to a deadlock or panic?

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1

maintainer
#3 lore raw
In reply to Jack Wang

Hi,

Quoted 65 lines · Jack Wang

From: Jack Wang <[email protected]>

Writing "clear", "readonly", "inactive" or "read_auto" to array_state calls mddev_set_closing_and_sync_blockdev(), which sets MD_CLOSING so the array cannot be reopened while it is being torn down. The flag is only cleared again at the tail of array_state_store() (for readonly, read_auto, inactive, or the failed-clear case).

Between setting the flag and reaching that tail there is an early return:

	err = mddev_lock(mddev);
	if (err)
		return err;

mddev_lock() is mutex_lock_interruptible() on reconfig_mutex. If the writing task is signalled while blocked there - easy to hit when the mutex is held for a long time by a running resync/recovery or reshape - it returns -EINTR and array_state_store() returns with MD_CLOSING still set. do_md_stop()/md_set_readonly() never ran, so the array keeps working internally, but every subsequent md_open() now returns -ENODEV for all
callers: the device still shows up in /proc/mdstat and sysfs is fully populated, yet it cannot be opened and cannot be recovered without a reboot (the remaining clear paths either need the device open or re-enter test_and_set_bit(MD_CLOSING) and bail with -EBUSY before the clear).

Route the interrupted-lock exit through a common label so the flag is released. Guard the clear with a set_closing flag that records whether this write actually set MD_CLOSING: when mddev->pers is already NULL (for example a concurrent STOP_ARRAY is mid-teardown) this write skips the mddev_set_closing_and_sync_blockdev() call above, so it must not clear a MD_CLOSING that the other thread owns - doing so could let the array be reopened while it is being destroyed. This also tightens the pre-existing tail clear, which had the same unconditional behaviour.

mddev_unlock() is correctly skipped on the goto path since the lock was never acquired.

Fixes 99b902ac1725 ("md: sync blockdev before stopping raid or setting readonly")
Assisted-by Claude:claude-opus-4-8 [Claude Code]
---
  drivers/md/md.c | 17 ++++++++++++++---
  1 file changed, 14 insertions(+), 3 deletions(-)
drivers/md/md.c +2 -0
index d1465bcd86c8..7b7a4d925907 100644
@@ -4649,6 +4649,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  {
  	int err = 0;
  	enum array_state st = match_word(buf, array_states);
+	bool set_closing = false;
 
  	/* No lock dependent actions */
  	switch (st) {
@@ -4668,6 +4669,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  		err = mddev_set_closing_and_sync_blockdev(mddev, 0);
  		if (err)
  			return err;
+		set_closing = true;
  		break;
  	default:
  		break;

drivers/md/md.c array_state_store(struct mddev *mddev, const char *buf, size_t len) · line 4701

Quoted 5 lines
drivers/md/md.c +1 -1
@@ -4696,7 +4698,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  	}
  	err = mddev_lock(mddev);
  	if (err)
-		return err;
+		goto out_clear_closing;

Can you just clear MD_CLOSING here and still return err directly? I think this is much simpler.

Quoted 6 lines
drivers/md/md.c +0 -0
  	switch (st) {
  	case inactive:
@@ -4769,8 +4771,17 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  	}
  	mddev_unlock(mddev);

drivers/md/md.c array_state_store(struct mddev *mddev, const char *buf, size_t len) · line 4780

Quoted 10 lines
drivers/md/md.c +7 -2
 
-	if (st == readonly || st == read_auto || st == inactive ||
-	    (err && st == clear))
+out_clear_closing:
+	/*
+	 * Only clear MD_CLOSING if this write actually set it. Otherwise a
+	 * concurrent teardown (e.g. STOP_ARRAY) may own the flag - this write
+	 * would have skipped setting it above when mddev->pers was already
+	 * NULL - and clearing it here would let the array be reopened while it
+	 * is being destroyed.

I don't think the comment is correct. Only one caller can set MD_CLOSING, concurrent teardown can't own the flag since this context already own the flag.

Quoted 7 lines
drivers/md/md.c +4 -0
+	 */
+	if (set_closing &&
+	    (st == readonly || st == read_auto || st == inactive ||
+	     (err && st == clear)))
  		clear_bit(MD_CLOSING, &mddev->flags);
 
  	return err ?: len;
Signature · 2 lines
Thanks,
Kuai
author
#4 lore raw

PATCH v3 1/1 md: clear MD_CLOSING when array_state_store() bails on interrupted lock

From: Jack Wang <[email protected]>

Writing "clear", "readonly", "inactive" or "read_auto" to array_state calls mddev_set_closing_and_sync_blockdev(), which sets MD_CLOSING to keep the array from being reopened while it is torn down. The flag is only cleared again at the tail of array_state_store() (for readonly / read_auto / inactive, or the failed-clear case).

Between setting the flag and reaching that tail there is an early return:

	err = mddev_lock(mddev);
	if (err)
		return err;

mddev_lock() is mutex_lock_interruptible() on reconfig_mutex. If the writing task catches a signal while blocked there, it returns -EINTR and MD_CLOSING is left set on a still-running array. do_md_stop() / md_set_readonly() never ran, so the array keeps working internally, but every subsequent md_open() now returns -ENODEV for all callers - the device shows up in /proc/mdstat and sysfs is fully populated, yet cannot be opened, and cannot be recovered without a reboot (all clear paths either need the device open or re-enter test_and_set_bit(MD_CLOSING) and bail with -EBUSY before the clear).

The window is easy to hit on a busy host: reconfig_mutex is held for long stretches by a running resync/recovery, widening the interruptible wait, and a userspace helper writing to array_state and then being killed or timed out is enough to leak the flag.

Clear the flag directly in the interrupted-lock exit. Guard the clear with a set_closing flag that records whether this write actually set
MD_CLOSING: the set call is skipped when the array is not running read-write (or mddev->pers is NULL), and in that case this write must not clear a MD_CLOSING it never set. This also tightens the pre-existing tail clear, which had the same unconditional behaviour.

Fixes 99b902ac1725 ("md: sync blockdev before stopping raid or setting readonly")
Assisted-by Claude:claude-opus-4-8 [Claude Code]

---
v3: (Kuai) clear MD_CLOSING directly at the mddev_lock() failure and
return, drop the out_clear_closing label; reword the commit message. Keep the set_closing guard: the set call is skipped when the array is not rdwr or mddev->pers is NULL, so this write may not own the flag. Rebased on v7.4-rc1.
v2: guard the clear with set_closing.

 drivers/md/md.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/md/md.c +10 -3
index 03e35386a802..f14a31387990 100644
@@ -4671,6 +4671,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 {
 	int err = 0;
 	enum array_state st = match_word(buf, array_states);
+	bool set_closing = false;
 
 	/* No lock dependent actions */
 	switch (st) {
@@ -4691,6 +4692,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 		err = mddev_set_closing_and_sync_blockdev(mddev, 0);
 		if (err)
 			return err;
+		set_closing = true;
 		break;
 	default:
 		break;
@@ -4718,8 +4720,12 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 		return err ?: len;
 	}
 	err = mddev_lock(mddev);
-	if (err)
+	if (err) {
+		/* Interrupted before any teardown ran, don't leak MD_CLOSING */
+		if (set_closing)
+			clear_bit(MD_CLOSING, &mddev->flags);
 		return err;
+	}
 
 	switch (st) {
 	case inactive:
@@ -4792,8 +4798,9 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
 	}
 	mddev_unlock(mddev);
 
-	if (st == readonly || st == read_auto || st == inactive ||
-	    (err && st == clear))
+	if (set_closing &&
+	    (st == readonly || st == read_auto || st == inactive ||
+	     (err && st == clear)))
 		clear_bit(MD_CLOSING, &mddev->flags);
 
 	return err ?: len;
Signature · 1 line
2.43.0
maintainer
#5 lore raw

Hi,

Quoted 36 lines · Jack Wang

From: Jack Wang <[email protected]>

Writing "clear", "readonly", "inactive" or "read_auto" to array_state calls mddev_set_closing_and_sync_blockdev(), which sets MD_CLOSING to keep the array from being reopened while it is torn down. The flag is only cleared again at the tail of array_state_store() (for readonly / read_auto / inactive, or the failed-clear case).

Between setting the flag and reaching that tail there is an early return:

	err = mddev_lock(mddev);
	if (err)
		return err;

mddev_lock() is mutex_lock_interruptible() on reconfig_mutex. If the writing task catches a signal while blocked there, it returns -EINTR and MD_CLOSING is left set on a still-running array. do_md_stop() / md_set_readonly() never ran, so the array keeps working internally, but every subsequent md_open() now returns -ENODEV for all callers - the device shows up in /proc/mdstat and sysfs is fully populated, yet cannot be opened, and cannot be recovered without a reboot (all clear paths either need the device open or re-enter test_and_set_bit(MD_CLOSING) and bail with -EBUSY before the clear).

The window is easy to hit on a busy host: reconfig_mutex is held for long stretches by a running resync/recovery, widening the interruptible wait, and a userspace helper writing to array_state and then being killed or timed out is enough to leak the flag.

Clear the flag directly in the interrupted-lock exit. Guard the clear with a set_closing flag that records whether this write actually set
MD_CLOSING: the set call is skipped when the array is not running read-write (or mddev->pers is NULL), and in that case this write must not clear a MD_CLOSING it never set. This also tightens the pre-existing tail clear, which had the same unconditional behaviour.

Quoted 2 lines
Fixes 99b902ac1725 ("md: sync blockdev before stopping raid or setting readonly")
Assisted-by Claude:claude-opus-4-8 [Claude Code]

checkpatch reprot error with this tag.

Quoted 47 lines

---
v3: (Kuai) clear MD_CLOSING directly at the mddev_lock() failure and
return, drop the out_clear_closing label; reword the commit message. Keep the set_closing guard: the set call is skipped when the array is not rdwr or mddev->pers is NULL, so this write may not own the flag. Rebased on v7.4-rc1.
v2: guard the clear with set_closing.

  drivers/md/md.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
drivers/md/md.c +7 -1
index 03e35386a802..f14a31387990 100644
@@ -4671,6 +4671,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  {
  	int err = 0;
  	enum array_state st = match_word(buf, array_states);
+	bool set_closing = false;
 
  	/* No lock dependent actions */
  	switch (st) {
@@ -4691,6 +4692,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  		err = mddev_set_closing_and_sync_blockdev(mddev, 0);
  		if (err)
  			return err;
+		set_closing = true;
  		break;
  	default:
  		break;
@@ -4718,8 +4720,12 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  		return err ?: len;
  	}
  	err = mddev_lock(mddev);
-	if (err)
+	if (err) {
+		/* Interrupted before any teardown ran, don't leak MD_CLOSING */
+		if (set_closing)
+			clear_bit(MD_CLOSING, &mddev->flags);
  		return err;
+	}
 
  	switch (st) {
  	case inactive:

drivers/md/md.c array_state_store(struct mddev *mddev, const char *buf, size_t len) · line 4803

Quoted 8 lines
drivers/md/md.c +3 -2
@@ -4792,8 +4798,9 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  	}
  	mddev_unlock(mddev);
 
-	if (st == readonly || st == read_auto || st == inactive ||
-	    (err && st == clear))
+	if (set_closing &&
+	    (st == readonly || st == read_auto || st == inactive ||
+	     (err && st == clear)))

Just keep the checking of set_closing should be enough.

Quoted 3 lines
drivers/md/md.c +0 -0
  		clear_bit(MD_CLOSING, &mddev->flags);
 
  	return err ?: len;
Signature · 2 lines
Thanks,
Kuai
#6 lore raw
Quoted 40 lines · yu kuai

Hi,

Quoted 36 lines · Jack Wang

From: Jack Wang <[email protected]>

Writing "clear", "readonly", "inactive" or "read_auto" to array_state calls mddev_set_closing_and_sync_blockdev(), which sets MD_CLOSING to keep the array from being reopened while it is torn down. The flag is only cleared again at the tail of array_state_store() (for readonly / read_auto / inactive, or the failed-clear case).

Between setting the flag and reaching that tail there is an early return:

      err = mddev_lock(mddev);
      if (err)
              return err;

mddev_lock() is mutex_lock_interruptible() on reconfig_mutex. If the writing task catches a signal while blocked there, it returns -EINTR and MD_CLOSING is left set on a still-running array. do_md_stop() / md_set_readonly() never ran, so the array keeps working internally, but every subsequent md_open() now returns -ENODEV for all callers - the device shows up in /proc/mdstat and sysfs is fully populated, yet cannot be opened, and cannot be recovered without a reboot (all clear paths either need the device open or re-enter test_and_set_bit(MD_CLOSING) and bail with -EBUSY before the clear).

The window is easy to hit on a busy host: reconfig_mutex is held for long stretches by a running resync/recovery, widening the interruptible wait, and a userspace helper writing to array_state and then being killed or timed out is enough to leak the flag.

Clear the flag directly in the interrupted-lock exit. Guard the clear with a set_closing flag that records whether this write actually set
MD_CLOSING: the set call is skipped when the array is not running read-write (or mddev->pers is NULL), and in that case this write must not clear a MD_CLOSING it never set. This also tightens the pre-existing tail clear, which had the same unconditional behaviour.

Quoted 4 lines
Quoted 2 lines
Fixes 99b902ac1725 ("md: sync blockdev before stopping raid or setting readonly")
Assisted-by Claude:claude-opus-4-8 [Claude Code]

checkpatch reprot error with this tag.

ok, will drop it.

Quoted 51 lines
Quoted 51 lines

---
v3: (Kuai) clear MD_CLOSING directly at the mddev_lock() failure and
return, drop the out_clear_closing label; reword the commit message. Keep the set_closing guard: the set call is skipped when the array is not rdwr or mddev->pers is NULL, so this write may not own the flag. Rebased on v7.4-rc1.
v2: guard the clear with set_closing.

  drivers/md/md.c | 13 ++++++++++---
  1 file changed, 10 insertions(+), 3 deletions(-)
drivers/md/md.c +7 -1
index 03e35386a802..f14a31387990 100644
@@ -4671,6 +4671,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
  {
      int err = 0;
      enum array_state st = match_word(buf, array_states);
+     bool set_closing = false;
 
      /* No lock dependent actions */
      switch (st) {
@@ -4691,6 +4692,7 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
              err = mddev_set_closing_and_sync_blockdev(mddev, 0);
              if (err)
                      return err;
+             set_closing = true;
              break;
      default:
              break;
@@ -4718,8 +4720,12 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
              return err ?: len;
      }
      err = mddev_lock(mddev);
-     if (err)
+     if (err) {
+             /* Interrupted before any teardown ran, don't leak MD_CLOSING */
+             if (set_closing)
+                     clear_bit(MD_CLOSING, &mddev->flags);
              return err;
+     }
 
      switch (st) {
      case inactive:
@@ -4792,8 +4798,9 @@ array_state_store(struct mddev *mddev, const char *buf, size_t len)
      }
      mddev_unlock(mddev);
 
Quoted 7 lines

drivers/md/md.c array_state_store(struct mddev *mddev, const char *buf, size_t len) · line 4803

Quoted 5 lines
drivers/md/md.c +3 -2
-     if (st == readonly || st == read_auto || st == inactive ||
-         (err && st == clear))
+     if (set_closing &&
+         (st == readonly || st == read_auto || st == inactive ||
+          (err && st == clear)))

Just keep the checking of set_closing should be enough.

A bare "if (set_closing)" at the tail would also clear MD_CLOSING after a successful "clear". do_md_stop() -> md_clean() keeps it on purpose so the stopped array can't be reopened (except legacy mode with hold_active).

So v4 uses:

        if (set_closing && (st != clear || err))

i.e. always clear for readonly/read_auto/inactive, and for "clear" only if it failed (array still live). Does that look OK to you?

--
Thanks,

Quoted 8 lines

drivers/md/md.c array_state_store(struct mddev *mddev, const char *buf, size_t len) · line 4806

Quoted 4 lines
drivers/md/md.c +0 -0
              clear_bit(MD_CLOSING, &mddev->flags);
 
      return err ?: len;

--
Thanks,
Kuai