[PATCH v7 1/2] md: use work_busy() to flush sync_work in stop_sync_thread()

From: Li Youhong

Date: Thu Nov 26 2026 - 03:33:21 EST


From: Li Youhong <liyouhong@xxxxxxxxxx>

stop_sync_thread() only flushes sync_work when work_pending() is true.
Once the work is running, PENDING is cleared, so an in-flight
md_start_sync() can be missed and teardown can race with it.

Use work_busy() instead, so both queued and executing sync_work are
flushed.

Fixes: db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Li Youhong <liyouhong@xxxxxxxxxx>

diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a802..1974943bf746 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -5213,7 +5213,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,