Linux RAID subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Thanh Nguyen" <thanhnguyxntester@gmail.com>
Cc: linux-raid@vger.kernel.org, yukuai@fygo.io
Subject: Re: [PATCH] md: flush running sync_work in stop_sync_thread()
Date: Tue, 01 Dec 2026 08:58:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-242899@kernel.org> (raw)
In-Reply-To: <20261201084150.1120-1-thanhnguyxntester@gmail.com>

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

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

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

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=sashiko-outbox-242899@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thanhnguyxntester@gmail.com \
    --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