All of lore.kernel.org
 help / color / mirror / Atom feed
From: Michael Ellerman <mpe@ellerman.id.au>
To: Will Deacon <will.deacon@arm.com>, Peter Zijlstra <peterz@infradead.org>
Cc: Young Xiao <92siuyang@gmail.com>,
	linux@armlinux.org.uk, mark.rutland@arm.com, mingo@redhat.com,
	bp@alien8.de, hpa@zytor.com, x86@kernel.org,
	kan.liang@linux.intel.com, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, ravi.bangoria@linux.vnet.ibm.com
Subject: Re: [PATCH] perf: Fix oops when kthread execs user process
Date: Wed, 29 May 2019 14:21:54 +1000	[thread overview]
Message-ID: <877ea9q49p.fsf@concordia.ellerman.id.au> (raw)
In-Reply-To: <20190528153224.GE20758@fuggles.cambridge.arm.com>

Will Deacon <will.deacon@arm.com> writes:
> On Tue, May 28, 2019 at 04:01:03PM +0200, Peter Zijlstra wrote:
>> On Tue, May 28, 2019 at 08:31:29PM +0800, Young Xiao wrote:
>> > When a kthread calls call_usermodehelper() the steps are:
>> >   1. allocate current->mm
>> >   2. load_elf_binary()
>> >   3. populate current->thread.regs
>> > 
>> > While doing this, interrupts are not disabled. If there is a perf
>> > interrupt in the middle of this process (i.e. step 1 has completed
>> > but not yet reached to step 3) and if perf tries to read userspace
>> > regs, kernel oops.
>
> This seems to be because pt_regs(current) gives NULL for kthreads on Power.

Right, we've done that since roughly forever in copy_thread():

int copy_thread(unsigned long clone_flags, unsigned long usp,
		unsigned long kthread_arg, struct task_struct *p)
{
	...
	/* Copy registers */
	sp -= sizeof(struct pt_regs);
	childregs = (struct pt_regs *) sp;
	if (unlikely(p->flags & PF_KTHREAD)) {
		/* kernel thread */
		memset(childregs, 0, sizeof(struct pt_regs));
		childregs->gpr[1] = sp + sizeof(struct pt_regs);
                ...
		p->thread.regs = NULL;	/* no user register state */

See commit from 2002:
  https://github.com/mpe/linux-fullhistory/commit/c0a96c0918d21d8a99270e94d9c4a4a322d04581#diff-edb76bfcc84905163f34d24d2aad3f3aR187

> From the initial report [1], it doesn't look like the mm isn't initialised,
> but rather than we're dereferencing a NULL pt_regs pointer somehow for the
> current task (see previous comment). I don't see how that can happen on
> arm64, given that we put the pt_regs on the kernel stack which is allocated
> during fork.

We have the regs on the stack too (see above), but we're explicitly
NULL'ing the link from task->thread.

Looks like on arm64 and x86 there is no link from task->thread, instead
you get from task to pt_regs via task_stack_page().

That actually seems potentially fishy given the comment on
task_stack_page() about the stack going away for exiting tasks. We
should probably be NULL'ing the regs pointer in free_thread_stack() or
similar. Though that race mustn't be happening because other arches
would see it.

Or are we just wrong and kthreads should have non-NULL regs? I can't
find another arch that does the same as us.

cheers

WARNING: multiple messages have this Message-ID (diff)
From: Michael Ellerman <mpe@ellerman.id.au>
To: Will Deacon <will.deacon@arm.com>, Peter Zijlstra <peterz@infradead.org>
Cc: mark.rutland@arm.com, Young Xiao <92siuyang@gmail.com>,
	x86@kernel.org, linux@armlinux.org.uk,
	linux-kernel@vger.kernel.org, mingo@redhat.com, bp@alien8.de,
	hpa@zytor.com, ravi.bangoria@linux.vnet.ibm.com,
	linux-arm-kernel@lists.infradead.org, kan.liang@linux.intel.com
Subject: Re: [PATCH] perf: Fix oops when kthread execs user process
Date: Wed, 29 May 2019 14:21:54 +1000	[thread overview]
Message-ID: <877ea9q49p.fsf@concordia.ellerman.id.au> (raw)
In-Reply-To: <20190528153224.GE20758@fuggles.cambridge.arm.com>

Will Deacon <will.deacon@arm.com> writes:
> On Tue, May 28, 2019 at 04:01:03PM +0200, Peter Zijlstra wrote:
>> On Tue, May 28, 2019 at 08:31:29PM +0800, Young Xiao wrote:
>> > When a kthread calls call_usermodehelper() the steps are:
>> >   1. allocate current->mm
>> >   2. load_elf_binary()
>> >   3. populate current->thread.regs
>> > 
>> > While doing this, interrupts are not disabled. If there is a perf
>> > interrupt in the middle of this process (i.e. step 1 has completed
>> > but not yet reached to step 3) and if perf tries to read userspace
>> > regs, kernel oops.
>
> This seems to be because pt_regs(current) gives NULL for kthreads on Power.

Right, we've done that since roughly forever in copy_thread():

int copy_thread(unsigned long clone_flags, unsigned long usp,
		unsigned long kthread_arg, struct task_struct *p)
{
	...
	/* Copy registers */
	sp -= sizeof(struct pt_regs);
	childregs = (struct pt_regs *) sp;
	if (unlikely(p->flags & PF_KTHREAD)) {
		/* kernel thread */
		memset(childregs, 0, sizeof(struct pt_regs));
		childregs->gpr[1] = sp + sizeof(struct pt_regs);
                ...
		p->thread.regs = NULL;	/* no user register state */

See commit from 2002:
  https://github.com/mpe/linux-fullhistory/commit/c0a96c0918d21d8a99270e94d9c4a4a322d04581#diff-edb76bfcc84905163f34d24d2aad3f3aR187

> From the initial report [1], it doesn't look like the mm isn't initialised,
> but rather than we're dereferencing a NULL pt_regs pointer somehow for the
> current task (see previous comment). I don't see how that can happen on
> arm64, given that we put the pt_regs on the kernel stack which is allocated
> during fork.

We have the regs on the stack too (see above), but we're explicitly
NULL'ing the link from task->thread.

Looks like on arm64 and x86 there is no link from task->thread, instead
you get from task to pt_regs via task_stack_page().

That actually seems potentially fishy given the comment on
task_stack_page() about the stack going away for exiting tasks. We
should probably be NULL'ing the regs pointer in free_thread_stack() or
similar. Though that race mustn't be happening because other arches
would see it.

Or are we just wrong and kthreads should have non-NULL regs? I can't
find another arch that does the same as us.

cheers

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2019-05-29  4:22 UTC|newest]

Thread overview: 60+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-28 12:31 [PATCH] perf: Fix oops when kthread execs user process Young Xiao
2019-05-28 12:31 ` Young Xiao
2019-05-28 12:41 ` Russell King - ARM Linux admin
2019-05-28 12:41   ` Russell King - ARM Linux admin
2019-05-28 14:01 ` Peter Zijlstra
2019-05-28 14:01   ` Peter Zijlstra
2019-05-28 15:32   ` Will Deacon
2019-05-28 15:32     ` Will Deacon
2019-05-28 16:12     ` Mark Rutland
2019-05-28 16:12       ` Mark Rutland
2019-05-28 17:32     ` Peter Zijlstra
2019-05-28 17:32       ` Peter Zijlstra
2019-05-29  9:17       ` Will Deacon
2019-05-29  9:17         ` Will Deacon
2019-05-29 10:10         ` Peter Zijlstra
2019-05-29 10:10           ` Peter Zijlstra
2019-05-29 10:20           ` Will Deacon
2019-05-29 10:20             ` Will Deacon
2019-05-29 12:55             ` Peter Zijlstra
2019-05-29 12:55               ` Peter Zijlstra
2019-05-29 13:05               ` Will Deacon
2019-05-29 13:05                 ` Will Deacon
2019-05-29 13:25                 ` Peter Zijlstra
2019-05-29 13:25                   ` Peter Zijlstra
2019-05-29 14:35                   ` Will Deacon
2019-05-29 14:35                     ` Will Deacon
2019-05-29 16:19                     ` Peter Zijlstra
2019-05-29 16:19                       ` Peter Zijlstra
2019-05-29 16:24                       ` Mark Rutland
2019-05-29 16:24                         ` Mark Rutland
2019-05-29 16:38                         ` Mark Rutland
2019-05-29 16:38                           ` Mark Rutland
2019-05-29 17:03                           ` Peter Zijlstra
2019-05-29 17:03                             ` Peter Zijlstra
2019-05-30 10:35                             ` Mark Rutland
2019-05-30 10:35                               ` Mark Rutland
2019-05-29 16:25                       ` Will Deacon
2019-05-29 16:25                         ` Will Deacon
2019-05-29 16:44                         ` Peter Zijlstra
2019-05-29 16:44                           ` Peter Zijlstra
2019-05-30  7:28                           ` Will Deacon
2019-05-30  7:28                             ` Will Deacon
2019-05-30  8:38               ` Ravi Bangoria
2019-05-30  8:38                 ` Ravi Bangoria
2019-05-30 10:27                 ` Ravi Bangoria
2019-05-30 10:27                   ` Ravi Bangoria
2019-05-31 15:37                   ` Will Deacon
2019-05-31 15:37                     ` Will Deacon
2019-06-03 11:23                     ` Will Deacon
2019-06-03 11:23                       ` Will Deacon
2019-06-03 11:48                     ` Peter Zijlstra
2019-06-03 11:48                       ` Peter Zijlstra
2019-06-03 13:30                     ` Michael Ellerman
2019-06-03 13:30                       ` Michael Ellerman
2019-05-29 10:11       ` Mark Rutland
2019-05-29 10:11         ` Mark Rutland
2019-05-29  4:21     ` Michael Ellerman [this message]
2019-05-29  4:21       ` Michael Ellerman
2019-05-29  1:44   ` Michael Ellerman
2019-05-29  1:44     ` Michael Ellerman

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=877ea9q49p.fsf@concordia.ellerman.id.au \
    --to=mpe@ellerman.id.au \
    --cc=92siuyang@gmail.com \
    --cc=bp@alien8.de \
    --cc=hpa@zytor.com \
    --cc=kan.liang@linux.intel.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=ravi.bangoria@linux.vnet.ibm.com \
    --cc=will.deacon@arm.com \
    --cc=x86@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.