Linux RAID subsystem development
 help / color / mirror / Atom feed
From: Thanh Nguyen <thanhnguyxntester@gmail.com>
To: Song Liu <song@kernel.org>, Yu Kuai <yukuai@fygo.io>
Cc: Li Nan <magiclinan@didiglobal.com>, Xiao Ni <xiao@kernel.org>,
	linux-raid@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org,
	Thanh Nguyen <thanhnguyxntester@gmail.com>,
	syzbot+58beac388fb46ea52592@syzkaller.appspotmail.com
Subject: [PATCH] md: flush running sync_work in stop_sync_thread()
Date: Tue,  1 Dec 2026 03:41:49 -0500	[thread overview]
Message-ID: <20261201084150.1120-1-thanhnguyxntester@gmail.com> (raw)

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


             reply	other threads:[~2026-12-01  8:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  8:41 Thanh Nguyen [this message]
2026-12-01  8:58 ` [PATCH] md: flush running sync_work in stop_sync_thread() sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201084150.1120-1-thanhnguyxntester@gmail.com \
    --to=thanhnguyxntester@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=magiclinan@didiglobal.com \
    --cc=song@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=syzbot+58beac388fb46ea52592@syzkaller.appspotmail.com \
    --cc=xiao@kernel.org \
    --cc=yukuai@fygo.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox