Re: hit a KASan bug related to Perf during stress test

From: Oleg Nesterov
Date: Tue Oct 25 2016 - 10:43:05 EST


On 10/25, Peter Zijlstra wrote:
>
> On Mon, Oct 24, 2016 at 05:39:08PM +0200, Oleg Nesterov wrote:
> > On 10/24, Peter Zijlstra wrote:
> > >
> > > --- a/kernel/events/core.c
> > > +++ b/kernel/events/core.c
> > > @@ -1257,7 +1257,14 @@ static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
> > > if (event->parent)
> > > event = event->parent;
> > >
> > > - return task_tgid_nr_ns(p, event->ns);
> > > + /*
> > > + * It is possible the task already got unhashed, in which case we
> > > + * cannot determine the current->group_leader/real_parent.
> > > + *
> > > + * Also, report -1 to indicate unhashed, so as not to confused with
> > > + * 0 for the idle task.
> > > + */
> > > + return pid_alive(p) ? task_tgid_nr_ns(p, event->ns) : ~0;
> > > }
> >
> > Yes, but this _looks_ racy unless p == current. I mean, pid_alive() makes
> > task_tgid_nr_ns() safe, but task_tgid_nr_ns() still can return zero _if_
> > it can race with the exiting task.
>
> So what serialization would close that race? __task_pid_nr_ns() only
> seems to use RCU nothing more.

I do not see how can we close this race, we obviously do not want to use
any locking.

That is why I tried to suggest

nr = __task_pid_nr_ns(p, type, event->ns);
if (!nr && !is_idle_task(p))
nr = -1;
return nr;

but this will report -1 if p runs in another namespace, so perhaps we
can do

nr = __task_pid_nr_ns(p, type, event->ns);
if (!nr && p->exit_state)
// it has already called exit_notify
nr = -1;
return nr;

Oleg.