linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Jeongjun Park <aha310510@gmail.com>
To: Dennis Zhou <dennis@kernel.org>
Cc: "Christoph Lameter (Ampere)" <cl@gentwo.org>,
	tj@kernel.org, akpm@linux-foundation.org, vbabka@suse.cz,
	 rientjes@google.com, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org,
	 syzbot+e5bd32b79413e86f389e@syzkaller.appspotmail.com
Subject: Re: [PATCH] mm/percpu: prevent concurrency problem for pcpu_nr_populated read with spin lock
Date: Thu, 3 Jul 2025 15:09:20 +0900	[thread overview]
Message-ID: <CAO9qdTEwPVqKMOJxoGqgWjHsqEZU+xy7afwZdg=wDi4h+jF63w@mail.gmail.com> (raw)
In-Reply-To: <aGYaXcB1CaA3BKEa@snowbird>

Hello,

Dennis Zhou <dennis@kernel.org> wrote:
>
> Hello,
>
> On Thu, Jul 03, 2025 at 01:45:36PM +0900, Jeongjun Park wrote:
> > Christoph Lameter (Ampere) <cl@gentwo.org> wrote:
> > >
> > > On Wed, 2 Jul 2025, Jeongjun Park wrote:
> > >
> > > > diff --git a/mm/percpu.c b/mm/percpu.c
> > > > index b35494c8ede2..0f98b857fb36 100644
> > > > --- a/mm/percpu.c
> > > > +++ b/mm/percpu.c
> > > > @@ -3355,7 +3355,13 @@ void __init setup_per_cpu_areas(void)
> > > >   */
> > > >  unsigned long pcpu_nr_pages(void)
> > > >  {
> > > > -     return pcpu_nr_populated * pcpu_nr_units;
> > > > +     unsigned long flags, ret;
> > > > +
> > > > +     spin_lock_irqsave(&pcpu_lock, flags);
> > > > +     ret = pcpu_nr_populated * pcpu_nr_units;
> > > > +     spin_unlock_irqrestore(&pcpu_lock, flags);
> > >
> > >
> > > Ummm.. What? You are protecting a single read with a spinlock? There needs
> > > to be some updating of data somewhere for this to make sense.
> > >
> > >
> > > Unless a different critical section protected by the lock sets the value
> > > intermittendly to something you are not allowed to see before a final
> > > store of a valid value. But that would be unusual.
> > >
> > > This is an academic exercise or did you really see a problem?
> > >
> > > What is racing?
> > >
> > >
> >
> > This patch is by no means an academic exercise.
> >
> > As written in the reported tag, This race has actually been reported
> > in syzbot [1].
> >
> > [1]: https://syzkaller.appspot.com/bug?extid=e5bd32b79413e86f389e
> >
>
> A report by syzbot doesn't mean it is a real problem. A production
> problem or broken test case is much more urgent.
>
> > pcpu_nr_populated is currently being write in pcpu_chunk_populated()
> > and pcpu_chunk_depopulated(), and since this two functions perform
> > pcpu_nr_populated write under the protection of pcpu_lock, there is no
> > race for write/write.
> >
> > However, since pcpu_nr_pages(), which performs a read operation on
> > pcpu_nr_populated, is not protected by pcpu_lock, races between read/write
> > can easily occur.
> >
> > Therefore, I think it is appropriate to protect it through pcpu_lock
> > according to the comment written in the definition of pcpu_nr_populated.
> >
>
> You're right that this is a race condition, but this was an intention
> choice done because the value read here is only being used to pass
> information to userspace for /proc/meminfo. As Christoph mentioned, the
> caller of pcpu_nr_pages() will never see an invalid value nor does it
> really matter either.
>
> The pcpu_lock is core to the percpu allocator and isn't something we
> would want to blindly expose either.
>
> The appropriate solution here is what Shakeel proposed to just mark the
> access as a data_race().
>
> Thanks,
> Dennis

If this data race was intentional, it makes sense why it was written
this way. I'll send v2 patch with the fix Shakeel proposed.
--
Regards,

Jeongjun Park


  reply	other threads:[~2025-07-03  6:09 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-02  8:27 Jeongjun Park
2025-07-02 15:55 ` Christoph Lameter (Ampere)
2025-07-03  4:45   ` Jeongjun Park
2025-07-03  5:51     ` Dennis Zhou
2025-07-03  6:09       ` Jeongjun Park [this message]
2025-07-03 16:39       ` Tejun Heo
2025-07-02 17:03 ` Shakeel Butt
2025-07-03  5:19   ` Jeongjun Park
2025-07-03  5:57     ` Dennis Zhou

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='CAO9qdTEwPVqKMOJxoGqgWjHsqEZU+xy7afwZdg=wDi4h+jF63w@mail.gmail.com' \
    --to=aha310510@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=dennis@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=rientjes@google.com \
    --cc=syzbot+e5bd32b79413e86f389e@syzkaller.appspotmail.com \
    --cc=tj@kernel.org \
    --cc=vbabka@suse.cz \
    /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