Re: [PATCH] mm/percpu: prevent concurrency problem for pcpu_nr_populated read with spin lock

From: Tejun Heo
Date: Thu Jul 03 2025 - 12:39:54 EST


On Wed, Jul 02, 2025 at 10:51:25PM -0700, Dennis Zhou wrote:
> > 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.

This isn't an actual race condition. The value can be read atomically and an
unprotected read can't lead to a result which wouldn't be possible when
reading under the lock. ie. Whether the lock is added or not, the end result
doesn't change. It's just that syzbot can't tell the difference.

Thanks.

--
tejun