Re: [PATCH v1] pid: annotate data-races around pid_ns->pid_allocated

From: Jiayuan Chen
Date: Fri Apr 25 2025 - 01:38:11 EST


April 24, 2025 at 17:38, "Christian Brauner" <brauner@xxxxxxxxxx> wrote:

>
> On Wed, Apr 23, 2025 at 06:38:18PM +0200, Oleg Nesterov wrote:
>
> >
> > On 04/23, Jiayuan Chen wrote:
> >
> > April 23, 2025 at 21:51, "Oleg Nesterov" <oleg@xxxxxxxxxx> wrote:
> >
> > >
> >
> > > On 04/23, Jiayuan Chen wrote:
> >
> > >
> >
> > > >
> >
> > > > Suppress syzbot reports by annotating these accesses using
> >
> > > >
> >
> > > > READ_ONCE() / WRITE_ONCE().
> >
> > > >
> >
> > >
> >
> > > ...
> >
> > >
> >
> > > >
> >
> > > > --- a/kernel/pid.c
> >
> > > >
> >
> > > > +++ b/kernel/pid.c
> >
> > > >
> >
> > > > @@ -122,7 +122,8 @@ void free_pid(struct pid *pid)
> >
> > > >
> >
> > > > for (i = 0; i <= pid->level; i++) {
> >
> > > >
> >
> > > > struct upid *upid = pid->numbers + i;
> >
> > > >
> >
> > > > struct pid_namespace *ns = upid->ns;
> >
> > > >
> >
> > > > - switch (--ns->pid_allocated) {
> >
> > > >
> >
> > > > + WRITE_ONCE(ns->pid_allocated, READ_ONCE(ns->pid_allocated) - 1);
> >
> > > >
> >
> > > > + switch (READ_ONCE(ns->pid_allocated)) {
> >
> > > >
> >
> > >
> >
> > > I keep forgetting how kcsan works, but we don't need
> >
> > >
> >
> > > READ_ONCE(ns->pid_allocated) under pidmap_lock?
> >
> > >
> >
> > > Same for other functions which read/modify ->pid_allocated with
> >
> > >
> >
> > > this lock held.
> >
> > >
> >
> > > Oleg.
> >
> > >
> >
> > However, not all places that read/write pid_allocated are locked,
> >
> > for example:
> >
> > https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/pid_namespace.c#n271
> >
> > https://web.git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/tree/kernel/fork.c#n2602
> >
> > So, in fact, the pidmap_lock is not effective. And if we were to add locks
> >
> > to all these places, it would be too heavy.
> >
> >
> >
> > It seems you misunderstood me. I didn't argue with the lockless READ_ONCE()s
> >
> > outside of pidmap_lock.
> >
>
> Agreed. We should only add those annotations where they're really
>
> needed (someone once taught me ;).
>

Thank you for your suggestion, it make sense to me.