[PATCH v2] md: flush running sync_work in stop_sync_thread()
From: Thanh Nguyen
Date: Tue Dec 01 2026 - 23:13:47 EST
In md_start_sync() on the not_running path, MD_RECOVERY_RUNNING is
cleared before calling mddev_unlock(mddev). After mddev_unlock()
releases reconfig_mutex, both mddev_unlock() and the remainder of
md_start_sync() continue dereferencing mddev (e.g., mddev->thread,
mddev->sb_wait, __mddev_resume(), and mddev->sysfs_action).
Meanwhile, stop_sync_thread() fails to wait for a running
mddev->sync_work in two ways:
1. If MD_RECOVERY_RUNNING is already cleared, stop_sync_thread() returns
immediately without flushing mddev->sync_work.
2. Even when MD_RECOVERY_RUNNING is still set, it checks
work_pending(&mddev->sync_work), which returns false once a worker
has dequeued and started executing md_start_sync().
Furthermore, while stop_sync_thread() drops reconfig_mutex to flush
mddev->sync_work, md_check_recovery() on a read-only array can re-queue
mddev->sync_work before reconfig_mutex is re-acquired.
Use work_busy(&mddev->sync_work) instead of work_pending(), flush
mddev->sync_work even when MD_RECOVERY_RUNNING is already cleared, and
re-check work_busy(&mddev->sync_work) and MD_RECOVERY_RUNNING (when
MD_RECOVERY_FROZEN is set) after re-acquiring reconfig_mutex, looping
back to retry if a new sync_work was queued while unlocked.
Fixes: db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
Reported-by: syzbot+58beac388fb46ea52592@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=58beac388fb46ea52592
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Thanh Nguyen <thanhnguyentuan2007@xxxxxxxxx>
---
Changes since v1:
- Re-check work_busy(&mddev->sync_work) and MD_RECOVERY_RUNNING (when
MD_RECOVERY_FROZEN is set) after re-acquiring reconfig_mutex and loop
back to retry if mddev->sync_work was re-queued while unlocked.
drivers/md/md.c | 22 +++++++++++++++++-----
1 file changed, 17 insertions(+), 5 deletions(-)
diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a..a05dbb0d2 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -5199,10 +5199,16 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
{
int sync_seq = atomic_read(&mddev->sync_seq);
+retry:
if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) {
- if (!locked)
- mddev_unlock(mddev);
- return;
+ if (!work_busy(&mddev->sync_work)) {
+ if (!locked)
+ mddev_unlock(mddev);
+ return;
+ }
+ mddev_unlock(mddev);
+ flush_work(&mddev->sync_work);
+ goto relock;
}
mddev_unlock(mddev);
@@ -5213,7 +5219,7 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
* never happen
*/
md_wakeup_thread_directly(&mddev->sync_thread);
- if (work_pending(&mddev->sync_work))
+ if (work_busy(&mddev->sync_work))
flush_work(&mddev->sync_work);
wait_event(resync_wait,
@@ -5221,8 +5227,14 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
(!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery) &&
sync_seq != atomic_read(&mddev->sync_seq)));
- if (locked)
+relock:
+ if (locked) {
mddev_lock_nointr(mddev);
+ if (work_busy(&mddev->sync_work) ||
+ (test_bit(MD_RECOVERY_FROZEN, &mddev->recovery) &&
+ test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)))
+ goto retry;
+ }
}
void md_idle_sync_thread(struct mddev *mddev)
--
2.56.1