* [PATCH] md: flush running sync_work in stop_sync_thread()
@ 2026-12-01 8:41 Thanh Nguyen
2026-12-01 8:58 ` sashiko-bot
2026-12-02 4:13 ` [PATCH v2] " Thanh Nguyen
0 siblings, 2 replies; 4+ messages in thread
From: Thanh Nguyen @ 2026-12-01 8:41 UTC (permalink / raw)
To: Song Liu, Yu Kuai
Cc: Li Nan, Xiao Ni, linux-raid, linux-kernel, stable, Thanh Nguyen,
syzbot+58beac388fb46ea52592
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().
As a result, do_md_stop() can finish stopping and cleaning the array and
allow mddev_put() -> mddev_delayed_delete() -> mddev_free() to kfree()
mddev while md_start_sync() is still executing inside mddev_unlock(),
causing a slab-use-after-free.
Use work_busy(&mddev->sync_work) instead of work_pending(), and also
flush mddev->sync_work when MD_RECOVERY_RUNNING is already cleared so
stop_sync_thread() does not return while md_start_sync() is still
running.
Fixes: db5e653d7c9f ("md: delay choosing sync action to md_start_sync()")
Reported-by: syzbot+58beac388fb46ea52592@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=58beac388fb46ea52592
Cc: stable@vger.kernel.org
Signed-off-by: Thanh Nguyen <thanhnguyxntester@gmail.com>
---
drivers/md/md.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/md/md.c b/drivers/md/md.c
index 03e35386a..81c6318d1 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -5200,8 +5200,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 (!locked)
+ if (work_busy(&mddev->sync_work)) {
mddev_unlock(mddev);
+ flush_work(&mddev->sync_work);
+ if (locked)
+ mddev_lock_nointr(mddev);
+ } else if (!locked) {
+ mddev_unlock(mddev);
+ }
return;
}
@@ -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,
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] md: flush running sync_work in stop_sync_thread()
2026-12-01 8:41 [PATCH] md: flush running sync_work in stop_sync_thread() Thanh Nguyen
@ 2026-12-01 8:58 ` sashiko-bot
2026-12-02 4:13 ` [PATCH v2] " Thanh Nguyen
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-01 8:58 UTC (permalink / raw)
To: Thanh Nguyen; +Cc: linux-raid, yukuai
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] md: unflushed re-queued sync_work after re-locking in
stop_sync_thread()
--
commit f4396ad9817951219df85157244681d0984e96d8
Author: Thanh Nguyen <thanhnguyxntester@gmail.com>
md: flush running sync_work in stop_sync_thread()
This commit updates stop_sync_thread() to check work_busy() instead of
work_pending() and to flush mddev->sync_work even when MD_RECOVERY_RUNNING
is already cleared.
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..a1a4f4f76d349 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -5211,8 +5211,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 (!locked)
> + if (work_busy(&mddev->sync_work)) {
> mddev_unlock(mddev);
> + flush_work(&mddev->sync_work);
> + if (locked)
> + mddev_lock_nointr(mddev);
> + } else if (!locked) {
> + mddev_unlock(mddev);
> + }
> return;
> }
>
> @@ -5224,7 +5230,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,
[Severity: Medium]
Can mddev->sync_work be queued again after flush_work() returns and before
stop_sync_thread() re-acquires reconfig_mutex?
Both branches drop reconfig_mutex to flush mddev->sync_work (and in the
second branch, flush_work() runs before wait_event(resync_wait, ...)) and
do not re-check work_busy(&mddev->sync_work) after mddev_lock_nointr(mddev).
On a read-only or read-auto array, md_check_recovery() does not check
MD_RECOVERY_FROZEN before queuing sync_work when spares need change:
drivers/md/md.c:md_check_recovery() {
...
if (!md_is_rdwr(mddev) &&
!test_bit(MD_RECOVERY_NEEDED, &mddev->recovery))
goto not_running;
if (!md_is_rdwr(mddev)) {
if (md_spares_need_change(mddev)) {
set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
queue_work(md_misc_wq, &mddev->sync_work);
}
clear_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
goto not_running;
}
...
}
This allows a newly queued md_start_sync() to hit the same use-after-free
window during do_md_stop():
CPU0 (do_md_stop) CPU1 (md_thread / kworker)
stop_sync_thread(mddev, true)
mddev_unlock(mddev)
flush_work(&mddev->sync_work)
md_check_recovery()
set_bit(MD_RECOVERY_RUNNING, ...)
queue_work(..., &mddev->sync_work)
md_start_sync()
mddev_lock_nointr(mddev)
goto not_running
clear_bit(MD_RECOVERY_RUNNING, ...)
mddev_unlock(mddev)
mutex_unlock(&mddev->reconfig_mutex)
mddev_lock_nointr(mddev)
return
/* MD_RECOVERY_RUNNING is clear */
mddev_unlock(mddev)
mddev_put(mddev)
... -> mddev_free()
md_wakeup_thread(mddev->thread)
<-- use-after-free
Should stop_sync_thread() re-check work_busy(&mddev->sync_work) and
MD_RECOVERY_RUNNING after re-acquiring reconfig_mutex and loop back if a
new sync_work was started while unlocked, similar to the retry loop in
action_store()?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261201084150.1120-1-thanhnguyxntester@gmail.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2] md: flush running sync_work in stop_sync_thread()
2026-12-01 8:41 [PATCH] md: flush running sync_work in stop_sync_thread() Thanh Nguyen
2026-12-01 8:58 ` sashiko-bot
@ 2026-12-02 4:13 ` Thanh Nguyen
2026-12-02 4:33 ` sashiko-bot
1 sibling, 1 reply; 4+ messages in thread
From: Thanh Nguyen @ 2026-12-02 4:13 UTC (permalink / raw)
To: Song Liu, Yu Kuai
Cc: Li Nan, Xiao Ni, linux-raid, linux-kernel, stable,
syzbot+58beac388fb46ea52592
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@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=58beac388fb46ea52592
Cc: stable@vger.kernel.org
Signed-off-by: Thanh Nguyen <thanhnguyentuan2007@gmail.com>
---
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v2] md: flush running sync_work in stop_sync_thread()
2026-12-02 4:13 ` [PATCH v2] " Thanh Nguyen
@ 2026-12-02 4:33 ` sashiko-bot
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-02 4:33 UTC (permalink / raw)
To: Thanh Nguyen; +Cc: linux-raid, yukuai
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] md: stop_sync_thread() retry interrupts new sync in
md_idle_sync_thread()
--
commit 71abb1be4745952743263f8a77a5e5ef3f06af23
Author: Thanh Nguyen <thanhnguyentuan2007@gmail.com>
md: flush running sync_work in stop_sync_thread()
This commit updates stop_sync_thread() to check work_busy() instead of
work_pending(), flush mddev->sync_work even when MD_RECOVERY_RUNNING is
already cleared, and retry after re-acquiring reconfig_mutex if a new
sync_work was queued while unlocked.
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 47da9edc1c606..6be40840b8499 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -5210,10 +5210,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);
> @@ -5224,7 +5230,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,
> @@ -5232,8 +5238,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)
[Severity: Medium]
When called from md_idle_sync_thread(), can the unconditional
work_busy(&mddev->sync_work) check at relock cause stop_sync_thread() to
abort the newly started sync cycle and return while it is still running?
md_idle_sync_thread() clears MD_RECOVERY_FROZEN to stop the current sync
thread while allowing a subsequent sync operation to start. Once the first
sync thread is reaped, mddev->thread can queue mddev->sync_work for the
next sync cycle before stop_sync_thread() re-acquires reconfig_mutex:
CPU0 (md_idle_sync_thread) CPU1 (mddev->thread)
stop_sync_thread(mddev, true)
sync_seq = atomic_read(...)
mddev_unlock(mddev)
set_bit(MD_RECOVERY_INTR, ...)
wait_event(resync_wait, ...)
md_check_recovery()
md_reap_sync_thread()
atomic_inc(&mddev->sync_seq)
clear_bit(MD_RECOVERY_RUNNING, ...)
set_bit(MD_RECOVERY_NEEDED, ...)
wake_up(&resync_wait)
mddev_unlock(mddev)
md_check_recovery()
set_bit(MD_RECOVERY_RUNNING, ...)
clear_bit(MD_RECOVERY_INTR, ...)
queue_work(..., &mddev->sync_work)
mddev_lock_nointr(mddev)
work_busy(&mddev->sync_work)
goto retry
mddev_unlock(mddev)
set_bit(MD_RECOVERY_INTR, ...) <-- aborts the new sync cycle
flush_work(&mddev->sync_work)
wait_event(resync_wait, ...) <-- sync_seq != mddev->sync_seq is
already true; returns immediately
Because sync_seq is sampled before the retry label and never refreshed,
wait_event() does not wait for the newly interrupted sync thread to exit,
leaving MD_RECOVERY_RUNNING set when md_idle_sync_thread() returns (causing
callers such as dm-raid's raid_message() to fail with -EBUSY).
Should the retry at relock skip work_busy(&mddev->sync_work) when
!test_bit(MD_RECOVERY_FROZEN, &mddev->recovery) and sync_seq has already
advanced?
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202041327.616-1-thanhnguyentuan2007@gmail.com?part=1
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-02 4:33 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01 8:41 [PATCH] md: flush running sync_work in stop_sync_thread() Thanh Nguyen
2026-12-01 8:58 ` sashiko-bot
2026-12-02 4:13 ` [PATCH v2] " Thanh Nguyen
2026-12-02 4:33 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox