* [PATCH v7 0/2] md: fix sync_work vs array teardown races
@ 2026-11-26 8:32 Li Youhong
2026-11-26 8:32 ` [PATCH v7 1/2] md: use work_busy() to flush sync_work in stop_sync_thread() Li Youhong
2026-11-26 8:32 ` [PATCH v7 2/2] md: fix sync_work vs array teardown races Li Youhong
0 siblings, 2 replies; 4+ messages in thread
From: Li Youhong @ 2026-11-26 8:32 UTC (permalink / raw)
To: song, yukuai, agk, snitzer, mpatocka, bmarzins
Cc: magiclinan, xiao, linux-raid, dm-devel, linux-kernel,
syzbot+a53c2918175b36cfd601, liyouhong
From: Li Youhong <liyouhong@kylinos.cn>
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. md_stop() needs the same wait, plus a few related holes
around work_busy(), MD_RECOVERY_FROZEN on the read-only path, and
event_work on the raid_ctr() error path.
This series splits the previous combined fix so the work_pending ->
work_busy change can land on its own.
Li Youhong (2):
md: use work_busy() to flush sync_work in stop_sync_thread()
md: fix sync_work vs array teardown races
drivers/md/dm-raid.c | 7 +++++++
drivers/md/md.c | 39 +++++++++++++++++++++++++++++----------
2 files changed, 36 insertions(+), 10 deletions(-)
---
v7:
- Resend v6; the previous v6 mails were not posted as one continuous
dependent series. Please ignore that v6 and use this one instead.
- No code change since v6.
- v6: https://lore.kernel.org/linux-raid/20261126080821.144057-1-dayou5941@163.com/
v6:
- Split into two patches per review:
1/2: only replace work_pending() with work_busy() when flushing
sync_work in stop_sync_thread(); Fixes: f52f5c71f3d4.
2/2: remaining changes (md_stop wait, idle early-return gated by
work_busy(), INTR only when RUNNING, RO FROZEN check,
flush event_work on raid_ctr() error path).
- Keep if (work_busy()) flush_work() in 2/2; do not turn it into an
unconditional flush (that would make 1/2 pointless).
- v5: https://lore.kernel.org/linux-raid/20261112013523.120428-1-dayou5941@163.com/
v5:
- Restore idle early-return in stop_sync_thread() gated by work_busy(),
so locked callers keep reconfig_mutex and do not race with do_md_run().
- Flush event_work before raid_set_free() on the raid_ctr error path
(md_stop() may drop the mutex and let md_reap queue it).
- v4: https://lore.kernel.org/linux-raid/20261111015027.4029184-1-dayou5941@163.com
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@163.com
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@163.com
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@163.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v7 1/2] md: use work_busy() to flush sync_work in stop_sync_thread()
2026-11-26 8:32 [PATCH v7 0/2] md: fix sync_work vs array teardown races Li Youhong
@ 2026-11-26 8:32 ` Li Youhong
2026-11-26 8:32 ` [PATCH v7 2/2] md: fix sync_work vs array teardown races Li Youhong
1 sibling, 0 replies; 4+ messages in thread
From: Li Youhong @ 2026-11-26 8:32 UTC (permalink / raw)
To: song, yukuai, agk, snitzer, mpatocka, bmarzins
Cc: magiclinan, xiao, linux-raid, dm-devel, linux-kernel,
syzbot+a53c2918175b36cfd601, liyouhong, stable
From: Li Youhong <liyouhong@kylinos.cn>
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@vger.kernel.org
Signed-off-by: Li Youhong <liyouhong@kylinos.cn>
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,
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH v7 2/2] md: fix sync_work vs array teardown races
2026-11-26 8:32 [PATCH v7 0/2] md: fix sync_work vs array teardown races Li Youhong
2026-11-26 8:32 ` [PATCH v7 1/2] md: use work_busy() to flush sync_work in stop_sync_thread() Li Youhong
@ 2026-11-26 8:32 ` Li Youhong
2026-11-26 8:49 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Li Youhong @ 2026-11-26 8:32 UTC (permalink / raw)
To: song, yukuai, agk, snitzer, mpatocka, bmarzins
Cc: magiclinan, xiao, linux-raid, dm-devel, linux-kernel,
syzbot+a53c2918175b36cfd601, liyouhong, stable
From: Li Youhong <liyouhong@kylinos.cn>
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().
Calling stop_sync_thread() from md_stop() can drop reconfig_mutex,
which introduces two follow-on issues:
- When nothing is in flight, keep reconfig_mutex if the caller held
it (gate the idle early-return with work_busy()). Otherwise clear
of an inactive array can race with do_md_run().
- While the mutex is dropped, mddev->thread may run
md_reap_sync_thread() and queue event_work. Flush event_work on
the raid_ctr() error path before raid_set_free().
Separately, the read-only path in md_check_recovery() queued
sync_work without honouring MD_RECOVERY_FROZEN, unlike the
read-write path. Check FROZEN there so freeze/stop cannot race
with a new queue.
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@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=a53c2918175b36cfd601
Cc: stable@vger.kernel.org
Signed-off-by: Li Youhong <liyouhong@kylinos.cn>
diff --git a/drivers/md/dm-raid.c b/drivers/md/dm-raid.c
index ac019a0f5a92..f82c09d858de 100644
--- a/drivers/md/dm-raid.c
+++ b/drivers/md/dm-raid.c
@@ -3319,6 +3319,13 @@ static int raid_ctr(struct dm_target *ti, unsigned int argc, char **argv)
md_stop(&rs->md);
mddev_unlock(&rs->md);
bad:
+ /*
+ * md_stop() may drop reconfig_mutex so mddev->thread can run
+ * md_reap_sync_thread() and queue event_work; wait for it before
+ * freeing raid_set. Skip if INIT_WORK has not run yet.
+ */
+ if (rs->md.event_work.func)
+ flush_work(&rs->md.event_work);
raid_set_free(rs);
return r;
diff --git a/drivers/md/md.c b/drivers/md/md.c
index 1974943bf746..2d87b3297da4 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -5199,7 +5199,14 @@ 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 nothing is in flight, keep reconfig_mutex when locked so
+ * callers such as do_md_stop() cannot race with do_md_run().
+ * work_busy() is required: md_start_sync() may clear RUNNING and
+ * still use mddev after unlocking.
+ */
+ if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
+ !work_busy(&mddev->sync_work)) {
if (!locked)
mddev_unlock(mddev);
return;
@@ -5207,12 +5214,14 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
mddev_unlock(mddev);
- 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);
+ 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);
+ }
if (work_busy(&mddev->sync_work))
flush_work(&mddev->sync_work);
@@ -7130,6 +7139,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
*/
@@ -10520,9 +10537,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);
}
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH v7 2/2] md: fix sync_work vs array teardown races
2026-11-26 8:32 ` [PATCH v7 2/2] md: fix sync_work vs array teardown races Li Youhong
@ 2026-11-26 8:49 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-26 8:49 UTC (permalink / raw)
To: Li Youhong, song, yukuai, agk, snitzer, mpatocka, bmarzins
Cc: magiclinan, xiao, linux-raid, dm-devel,
syzbot+a53c2918175b36cfd601, liyouhong
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] md: stop_sync_thread() hangs on new sync when md_start_sync()
aborts
--
commit ad365d153f005559a18f08a3280bb7f5a9671a2f
Author: Li Youhong <liyouhong@kylinos.cn>
md: fix sync_work vs array teardown races
This commit stops any in-flight sync_work and sync_thread during md_stop(),
flushes event_work on the raid_ctr() error path, gates the stop_sync_thread()
early return on work_busy(&mddev->sync_work), and honors MD_RECOVERY_FROZEN
in the read-only path of md_check_recovery().
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index bb5ea106cc254..2ac93a836fb25 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -5210,7 +5210,14 @@ 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 nothing is in flight, keep reconfig_mutex when locked so
> + * callers such as do_md_stop() cannot race with do_md_run().
> + * work_busy() is required: md_start_sync() may clear RUNNING and
> + * still use mddev after unlocking.
> + */
> + if (!test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) &&
> + !work_busy(&mddev->sync_work)) {
> if (!locked)
> mddev_unlock(mddev);
> return;
> @@ -5218,12 +5225,14 @@ static void stop_sync_thread(struct mddev *mddev, bool locked)
>
> mddev_unlock(mddev);
>
> - 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);
> + 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);
> + }
> if (work_busy(&mddev->sync_work))
> flush_work(&mddev->sync_work);
>
[Severity: Medium]
Can stop_sync_thread() end up waiting for the entire duration of a newly
started sync thread when an in-flight sync_work exits via not_running?
Unlike md_reap_sync_thread(), the not_running path in md_start_sync() clears
MD_RECOVERY_RUNNING and unlocks reconfig_mutex without incrementing
mddev->sync_seq:
drivers/md/md.c:md_start_sync() {
...
not_running:
clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
clear_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
clear_bit(MD_RECOVERY_REQUESTED, &mddev->recovery);
clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
clear_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
mddev_unlock(mddev);
wake_up(&resync_wait);
}
When a caller such as raid_message() -> md_idle_sync_thread() clears
MD_RECOVERY_FROZEN and enters stop_sync_thread() while sync_work is still
busy on the not_running path, stop_sync_thread() drops reconfig_mutex and
flushes sync_work, allowing mddev->thread to start a fresh sync before
wait_event() runs:
CPU0 (raid_message) CPU1 (sync_work) CPU2 (mddev->thread)
md_idle_sync_thread()
clear_bit(FROZEN)
stop_sync_thread()
sync_seq =
atomic_read(sync_seq)
/* work_busy() is true */
mddev_unlock()
flush_work(sync_work)
md_start_sync()
goto not_running;
clear_bit(RUNNING)
/* sync_seq unchanged */
mddev_unlock()
md_check_recovery()
set_bit(RUNNING)
clear_bit(INTR)
queue_work(sync_work)
wait_event(resync_wait,
!test_bit(RUNNING) ||
(!test_bit(FROZEN) &&
sync_seq !=
atomic_read(sync_seq)))
<-- blocks until the new sync finishes
Because MD_RECOVERY_RUNNING is set again, MD_RECOVERY_INTR was cleared by
md_check_recovery(), and sync_seq never changed, stop_sync_thread() blocks
in wait_event() until the new sync completes instead of returning once the
aborted sync_work has finished.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261126083210.210866-1-dayou5941@163.com?part=2
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-26 8:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 8:32 [PATCH v7 0/2] md: fix sync_work vs array teardown races Li Youhong
2026-11-26 8:32 ` [PATCH v7 1/2] md: use work_busy() to flush sync_work in stop_sync_thread() Li Youhong
2026-11-26 8:32 ` [PATCH v7 2/2] md: fix sync_work vs array teardown races Li Youhong
2026-11-26 8:49 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox