Re: [PATCH] perf: Fix oops when kthread execs user process

From: Peter Zijlstra
Date: Wed May 29 2019 - 06:14:21 EST


On Wed, May 29, 2019 at 10:17:33AM +0100, Will Deacon wrote:
> On Tue, May 28, 2019 at 07:32:28PM +0200, Peter Zijlstra wrote:

> > 'funny' thing that, perf_sample_regs_user() seems to assume that
> > anything with current->mm is in fact a user task, and that assumption is
> > just plain wrong, consider use_mm().
>
> Right, I suppose that was attempting to handle interrupt skid from the PMU
> overflow?

Nah, just a broken test to determine if there is userspace at all. It is
mostly right, just not completely :-)

> > So I'm thinking the right thing to do here is something like the below;
> > umh should get PF_KTHREAD cleared when it passes exec(). And this should
> > also fix the power splat I'm thinking.
> >
> > ---
> >
> > diff --git a/kernel/events/core.c b/kernel/events/core.c
> > index abbd4b3b96c2..9929404b6eb9 100644
> > --- a/kernel/events/core.c
> > +++ b/kernel/events/core.c
> > @@ -5923,7 +5923,7 @@ static void perf_sample_regs_user(struct perf_regs *regs_user,
> > if (user_mode(regs)) {
> > regs_user->abi = perf_reg_abi(current);
> > regs_user->regs = regs;
> > - } else if (current->mm) {
> > + } else if (!(current->flags & PF_KTHREAD) && current->mm) {
> > perf_get_regs_user(regs_user, regs, regs_user_copy);
>
> Makes sense, but under which circumstances would we have a NULL mm here?

Dunno; I'm paranoid, and also:

mm/memcontrol.c: if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
mm/vmacache.c: return current->mm == mm && !(current->flags & PF_KTHREAD);