Re: [PATCH v7 0/2] md: fix sync_work vs array teardown races
From: yu kuai
Date: Sun Nov 29 2026 - 00:23:20 EST
在 2026/11/26 16:32, Li Youhong 写道:
> 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. 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(-)
Applied
>
> ---
> 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@xxxxxxx/
>
> 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@xxxxxxx/
>
> 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@xxxxxxx
>
> 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
--
Thanks,
Kuai