mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Zhou, Yun" <yun.zhou@windriver.com>
To: Yu Kuai <yukuai1@huaweicloud.com>, Paul Menzel <pmenzel@molgen.mpg.de>
Cc: agk@redhat.com, snitzer@kernel.org, mpatocka@redhat.com,
	song@kernel.org, dm-devel@lists.linux.dev,
	linux-kernel@vger.kernel.org, linux-raid@vger.kernel.org,
	"yukuai (C)" <yukuai3@huawei.com>
Subject: Re: [PATCH] md: fix rcu protection in md_wakeup_thread
Date: Wed, 15 Oct 2025 15:31:47 +0800	[thread overview]
Message-ID: <040131e6-08f7-4f64-a317-deaa21e1a5e1@windriver.com> (raw)
In-Reply-To: <d2574af5-9410-d296-ef74-97f3e43dc1cc@huaweicloud.com>

Hi Kuai,

On 10/15/25 14:58, Yu Kuai wrote:
> CAUTION: This email comes from a non Wind River email account!
> Do not click links or open attachments unless you recognize the sender 
> and know the content is safe.
>
> Hi,
>
> 在 2025/10/15 14:35, Paul Menzel 写道:
>> Dear Yun,
>>
>>
>> Thank you for your patch.
>>
>> Am 15.10.25 um 07:59 schrieb Yun Zhou:
>>> We attempted to use RCU to protect the pointer 'thread', but directly
>>> passed the value when calling md_wakeup_thread(). This means that the
>>> RCU pointer has been acquired before rcu_read_lock(), which renders
>>> rcu_read_lock() ineffective and could lead to a use-after-free.
>>
>> Could you elaborate a little more – especially as nobody has noticed
>> this so far since v6.5-rc1?
>>
>
> This looks correct, memory dereference should be protected by rcu, while
> in fact this is done before function parameter passing.
>
> However, in most places, a null check should be enough because the md
> thread can't be unregistered concurrently, that's probably no one ever
> meet the problem.
>
> However, for the modification, I'll prefer a new marcon like following,
> so that you don't need to update all the callers:
>
This method only changes a little code. But it holds the lock before the 
function jumps, which seems to violate the principle of minimizing the 
critical area.
> Thanks,
> Kuai
>
> diff --git a/drivers/md/md.c b/drivers/md/md.c
> index 1de550108756..d48ee1b50d27 100644
> --- a/drivers/md/md.c
> +++ b/drivers/md/md.c
> @@ -8384,11 +8384,10 @@ static void md_wakeup_thread_directly(struct
> md_thread __rcu *thread)
>         rcu_read_unlock();
>  }
>
> -void md_wakeup_thread(struct md_thread __rcu *thread)
> +void __md_wakeup_thread(struct md_thread __rcu *thread)
>  {
>         struct md_thread *t;
>
> -       rcu_read_lock();
>         t = rcu_dereference(thread);
>         if (t) {
>                 pr_debug("md: waking up MD thread %s.\n", t->tsk->comm);
> @@ -8396,9 +8395,8 @@ void md_wakeup_thread(struct md_thread __rcu 
> *thread)
>                 if (wq_has_sleeper(&t->wqueue))
>                         wake_up(&t->wqueue);
>         }
> -       rcu_read_unlock();
>  }
> -EXPORT_SYMBOL(md_wakeup_thread);
> +EXPORT_SYMBOL(__md_wakeup_thread);
>
>  struct md_thread *md_register_thread(void (*run) (struct md_thread *),
>                 struct mddev *mddev, const char *name)
> diff --git a/drivers/md/md.h b/drivers/md/md.h
> index 1979c2d4fe89..9ec62afc2a7d 100644
> --- a/drivers/md/md.h
> +++ b/drivers/md/md.h
> @@ -882,6 +882,12 @@ struct md_io_clone {
>
>  #define THREAD_WAKEUP  0
>
> +#define md_wakeup_thread(thread) do {                          \
> +       rcu_read_lock();                                        \
> +       __md_wakeup_thread(thread);                             \
> +       rcu_read_unlock();                                      \
> +} while (0)
> +
>  static inline void safe_put_page(struct page *p)
>  {
>         if (p) put_page(p);
> @@ -895,7 +901,7 @@ extern struct md_thread *md_register_thread(
>         struct mddev *mddev,
>         const char *name);
>  extern void md_unregister_thread(struct mddev *mddev, struct md_thread
> __rcu **threadp);
> -extern void md_wakeup_thread(struct md_thread __rcu *thread);
> +extern void __md_wakeup_thread(struct md_thread __rcu *thread);
>  extern void md_check_recovery(struct mddev *mddev);
>  extern void md_reap_sync_thread(struct mddev *mddev);
>  extern enum sync_action md_sync_action(struct mddev *mddev);
>
Regards,
Yun

  reply	other threads:[~2025-10-15  7:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-15  5:59 Yun Zhou
2025-10-15  6:35 ` Paul Menzel
2025-10-15  6:58   ` Yu Kuai
2025-10-15  7:31     ` Zhou, Yun [this message]
2025-10-15  7:57       ` Yu Kuai
2025-10-15  8:19         ` Zhou, Yun

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=040131e6-08f7-4f64-a317-deaa21e1a5e1@windriver.com \
    --to=yun.zhou@windriver.com \
    --cc=agk@redhat.com \
    --cc=dm-devel@lists.linux.dev \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=mpatocka@redhat.com \
    --cc=pmenzel@molgen.mpg.de \
    --cc=snitzer@kernel.org \
    --cc=song@kernel.org \
    --cc=yukuai1@huaweicloud.com \
    --cc=yukuai3@huawei.com \
    /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

all inboxes | Powered by JetHome®