[PATCH v4] md: fix sync_work vs array teardown races

From: Li Youhong

Date: Tue Nov 10 2026 - 20:51:42 EST


From: Li Youhong <liyouhong@xxxxxxxxxx>

dm-raid tears down arrays via md_stop(), which cleared mddev->pers
without waiting for previously queued sync_work. That work could
then call remove_spares() with pers == NULL, or keep using mddev
after raid_dtr() frees it.

do_md_stop()/md_stop_writes() already call stop_sync_thread() before
teardown. Do the same in md_stop().

Also fix two related holes that made that wait unreliable:

- md_start_sync() may still use mddev after unlocking. Checking
work_pending() in stop_sync_thread() is not enough once the work
is running, so always flush_work() and wait for it to return
before teardown.

- The read-only path in md_check_recovery() queued sync_work without
honouring MD_RECOVERY_FROZEN, unlike the read-write path. Check
FROZEN so freeze/stop cannot race with a new queue while
stop_sync_thread() drops reconfig_mutex.

Fixes: db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
Fixes: 81e2ce1b3d5a ("md: delay remove_and_add_spares() for read only array to md_start_sync()")
Reported-by: syzbot+a53c2918175b36cfd601@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=a53c2918175b36cfd601
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Li Youhong <liyouhong@xxxxxxxxxx>
---
v4:
- Always flush_work(&sync_work) in stop_sync_thread(); do not delay
clearing MD_RECOVERY_RUNNING (that can drop MD_RECOVERY_NEEDED via
md_check_recovery, and does not cover the successful sync_thread
registration path).
- v3: https://lore.kernel.org/linux-raid/20261110063045.912703-1-dayou5941@xxxxxxx
v3:
- Honour MD_RECOVERY_FROZEN before queueing sync_work on the
read-only md_check_recovery() path.
- (superseded) delayed MD_RECOVERY_RUNNING clear on not_running.
- v2: https://lore.kernel.org/linux-raid/20261109023055.2164333-1-dayou5941@xxxxxxx
v2:
- Drop the pers NULL check in md_start_sync().
- Call stop_sync_thread() from md_stop() before clearing pers,
matching do_md_stop()/md_stop_writes().
- v1: https://lore.kernel.org/linux-raid/20261106083543.249267-1-dayou5941@xxxxxxx

drivers/md/md.c | 38 ++++++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 680b34a63cb3..68a72513e884 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -5184,22 +5184,22 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
{
int sync_seq = atomic_read(&mddev->sync_seq);

- if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
- if (!locked)
- mddev_unlock(mddev);
- return;
- }
-
mddev_unlock(mddev);

- set_bit(MD_RECOVERY_INTR, &mddev->recovery);
+ if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
+ set_bit(MD_RECOVERY_INTR, &mddev->recovery);
+ /*
+ * Thread might be blocked waiting for metadata update which
+ * will now never happen
+ */
+ md_wakeup_thread_directly(&mddev->sync_thread);
+ }
/*
- * Thread might be blocked waiting for metadata update which will now
- * never happen
+ * md_start_sync() may still use mddev after unlocking. Always
+ * flush so teardown waits for that work to return; checking
+ * work_pending() is not enough once the work is running.
*/
- md_wakeup_thread_directly(&mddev->sync_thread);
- if (work_pending(&mddev->sync_work))
- flush_work(&mddev->sync_work);
+ flush_work(&mddev->sync_work);

wait_event(resync_wait,
!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) ||
@@ -7101,6 +7101,14 @@ void md_stop(struct mddev *mddev)
{
lockdep_assert_held(&mddev->reconfig_mutex);

+ /*
+ * md_check_recovery() may have queued sync_work; that work accesses
+ * pers. Wait for it here before __md_stop() clears pers, as
+ * do_md_stop()/md_stop_writes() do.
+ */
+ set_bit(MD_RECOVERY_FROZEN, &mddev->recovery);
+ stop_sync_thread(mddev, true);
+
/* stop the array and free an attached data structures.
* This is called from dm-raid
*/
@@ -10494,9 +10502,11 @@ void md_check_recovery(struct mddev *mddev)

/*
* Let md_start_sync() to remove and add rdevs to the
- * array.
+ * array. Honour MD_RECOVERY_FROZEN like the read-write
+ * path so freeze/stop does not race with a new queue.
*/
- if (md_spares_need_change(mddev)) {
+ if (md_spares_need_change(mddev) &&
+ !test_bit(MD_RECOVERY_FROZEN, &mddev->recovery)) {
set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
queue_work(md_misc_wq, &mddev->sync_work);
}
--
2.25.1