public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@redhat.com>
To: Andy Lutomirski <luto@amacapital.net>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@kernel.org>, "H . Peter Anvin" <hpa@zytor.com>,
	X86 ML <x86@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Brian Gerst <brgerst@gmail.com>,
	Kees Cook <keescook@chromium.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Frederic Weisbecker <fweisbec@gmail.com>,
	Byungchul Park <byungchul.park@lge.com>,
	Nilay Vaish <nilayvaish@gmail.com>
Subject: Re: [PATCH v3 41/51] x86/entry/unwind: create stack frames for saved interrupt registers
Date: Mon, 15 Aug 2016 11:33:59 -0500	[thread overview]
Message-ID: <20160815163359.dciqlp3224othrzi@treble> (raw)
In-Reply-To: <CALCETrX1jB+2dYjMNV0=xraGLH83wuL7=895-uxv2JZWD5JY7w@mail.gmail.com>

On Sun, Aug 14, 2016 at 01:10:42AM -0700, Andy Lutomirski wrote:
> On Fri, Aug 12, 2016 at 7:29 AM, Josh Poimboeuf <jpoimboe@redhat.com> wrote:
> > With frame pointers, when a task is interrupted, its stack is no longer
> > completely reliable because the function could have been interrupted
> > before it had a chance to save the previous frame pointer on the stack.
> > So the caller of the interrupted function could get skipped by a stack
> > trace.
> >
> > This is problematic for live patching, which needs to know whether a
> > stack trace of a sleeping task can be relied upon.  There's currently no
> > way to detect if a sleeping task was interrupted by a page fault
> > exception or preemption before it went to sleep.
> >
> > Another issue is that when dumping the stack of an interrupted task, the
> > unwinder has no way of knowing where the saved pt_regs registers are, so
> > it can't print them.
> >
> > This solves those issues by encoding the pt_regs pointer in the frame
> > pointer on entry from an interrupt or an exception.
> >
> > This patch also updates the unwinder to be able to decode it, because
> > otherwise the unwinder would be broken by this change.
> >
> > Note that this causes a change in the behavior of the unwinder: each
> > instance of a pt_regs on the stack is now considered a "frame".  So
> > callers of unwind_get_return_address() will now get an occasional
> > 'regs->ip' address that would have previously been skipped over.
> 
> Acked-by: Andy Lutomirski <luto@kernel.org>
> 
> with minor optional nitpicks below.
> 
> >
> > Suggested-by: Andy Lutomirski <luto@amacapital.net>
> > Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
> > ---
> >  arch/x86/entry/calling.h       | 21 +++++++++++
> >  arch/x86/entry/entry_32.S      | 40 ++++++++++++++++++---
> >  arch/x86/entry/entry_64.S      | 10 ++++--
> >  arch/x86/include/asm/unwind.h  | 18 ++++++++--
> >  arch/x86/kernel/unwind_frame.c | 82 +++++++++++++++++++++++++++++++++++++-----
> >  5 files changed, 153 insertions(+), 18 deletions(-)
> >
> > diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h
> > index 9a9e588..ab799a3 100644
> > --- a/arch/x86/entry/calling.h
> > +++ b/arch/x86/entry/calling.h
> > @@ -201,6 +201,27 @@ For 32-bit we have the following conventions - kernel is built with
> >         .byte 0xf1
> >         .endm
> >
> > +       /*
> > +        * This is a sneaky trick to help the unwinder find pt_regs on the
> > +        * stack.  The frame pointer is replaced with an encoded pointer to
> > +        * pt_regs.  The encoding is just a clearing of the highest-order bit,
> > +        * which makes it an invalid address and is also a signal to the
> > +        * unwinder that it's a pt_regs pointer in disguise.
> > +        *
> > +        * NOTE: This macro must be used *after* SAVE_EXTRA_REGS because it
> > +        * corrupts the original rbp.
> > +        */
> > +.macro ENCODE_FRAME_POINTER ptregs_offset=0
> > +#ifdef CONFIG_FRAME_POINTER
> > +       .if \ptregs_offset
> > +               leaq \ptregs_offset(%rsp), %rbp
> > +       .else
> > +               mov %rsp, %rbp
> > +       .endif
> > +       btr $63, %rbp
> > +#endif
> > +.endm
> > +
> >  #endif /* CONFIG_X86_64 */
> >
> >  /*
> > diff --git a/arch/x86/entry/entry_32.S b/arch/x86/entry/entry_32.S
> > index 4396278..4006fa3 100644
> > --- a/arch/x86/entry/entry_32.S
> > +++ b/arch/x86/entry/entry_32.S
> > @@ -174,6 +174,23 @@
> >         SET_KERNEL_GS %edx
> >  .endm
> >
> > +/*
> > + * This is a sneaky trick to help the unwinder find pt_regs on the
> > + * stack.  The frame pointer is replaced with an encoded pointer to
> > + * pt_regs.  The encoding is just a clearing of the highest-order bit,
> > + * which makes it an invalid address and is also a signal to the
> > + * unwinder that it's a pt_regs pointer in disguise.
> > + *
> > + * NOTE: This macro must be used *after* SAVE_ALL because it corrupts the
> > + * original rbp.
> > + */
> > +.macro ENCODE_FRAME_POINTER
> > +#ifdef CONFIG_FRAME_POINTER
> > +       mov %esp, %ebp
> > +       btr $31, %ebp
> > +#endif
> > +.endm
> > +
> >  .macro RESTORE_INT_REGS
> >         popl    %ebx
> >         popl    %ecx
> > @@ -205,10 +222,16 @@
> >  .endm
> >
> >  ENTRY(ret_from_fork)
> > +       call    1f
> 
> pushl $ret_from_fork is the same length and slightly less strange.
> OTOH it forces a relocation, and this function doesn't return, so
> there shouldn't be any performance issue, so this may save a byte or
> two in the compressed image.
> 
> > +1:     push    $0
> 
> This could maybe use a comment.

Oops.  This ret_from_fork bit was meant for a separate patch.

I think the problem with "pushl $ret_from_fork" is that
ret_from_fork+0x0 is not a valid call return address.
printk_stack_address() will show it as the end of the previous function
in the file.

Anyway, this definitely needs a comment and should be split out to a
separate patch.

-- 
Josh

  reply	other threads:[~2016-08-15 16:34 UTC|newest]

Thread overview: 99+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-12 14:28 [PATCH v3 00/51] x86/dumpstack: rewrite x86 stack dump code Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 01/51] x86/dumpstack: remove show_trace() Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 02/51] x86/asm/head: remove unused init_rsp variable extern Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 03/51] x86/asm/head: rename 'stack_start' -> 'initial_stack' Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 04/51] x86/asm/head: use a common function for starting CPUs Josh Poimboeuf
2016-08-12 22:08   ` Nilay Vaish
2016-08-12 14:28 ` [PATCH v3 05/51] x86/dumpstack: make printk_stack_address() more generally useful Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 06/51] x86/dumpstack: add IRQ_USABLE_STACK_SIZE define Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 07/51] x86/dumpstack: remove extra brackets around "<EOE>" Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 08/51] x86/dumpstack: fix irq stack bounds calculation in show_stack_log_lvl() Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 09/51] x86/dumpstack: fix x86_32 kernel_stack_pointer() previous stack access Josh Poimboeuf
2016-08-14  7:26   ` Andy Lutomirski
2016-08-14 12:55     ` Brian Gerst
2016-08-14 13:42       ` Andy Lutomirski
2016-08-15 15:05     ` Josh Poimboeuf
2016-08-15 17:22       ` Josh Poimboeuf
2016-08-15 20:04         ` Andy Lutomirski
2016-08-12 14:28 ` [PATCH v3 10/51] x86/dumpstack: add get_stack_pointer() and get_frame_pointer() Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 11/51] x86/dumpstack: remove unnecessary stack pointer arguments Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 12/51] x86: move _stext marker to before head code Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 13/51] x86/asm/head: remove useless zeroed word Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 14/51] x86/asm/head: put real return address on idle task stack Josh Poimboeuf
2016-08-14  7:29   ` Andy Lutomirski
2016-08-17 20:30   ` Nilay Vaish
2016-08-17 21:10     ` Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 15/51] x86/asm/head: standardize the end of the stack for idle tasks Josh Poimboeuf
2016-08-14  7:30   ` Andy Lutomirski
2016-08-12 14:28 ` [PATCH v3 16/51] x86/32: put real return address on stack in entry code Josh Poimboeuf
2016-08-14  7:31   ` Andy Lutomirski
2016-08-15 15:09     ` Josh Poimboeuf
2016-08-15 18:04       ` H. Peter Anvin
2016-08-15 18:25         ` Josh Poimboeuf
2016-08-15 19:22           ` H. Peter Anvin
2016-08-15 20:06             ` Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 17/51] x86/smp: fix initial idle stack location on 32-bit Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 18/51] x86/entry/head/32: use local labels Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 19/51] x86/entry/32: rename 'error_code' to 'common_exception' Josh Poimboeuf
2016-08-14  7:40   ` Andy Lutomirski
2016-08-15 15:30     ` Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 20/51] perf/x86: check perf_callchain_store() error Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 21/51] oprofile/x86: add regs->ip to oprofile trace Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 22/51] proc: fix return address printk conversion specifer in /proc/<pid>/stack Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 23/51] ftrace: remove CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST from config Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 24/51] ftrace: only allocate the ret_stack 'fp' field when needed Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 25/51] ftrace: add return address pointer to ftrace_ret_stack Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 26/51] ftrace: add ftrace_graph_ret_addr() stack unwinding helpers Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 27/51] x86/dumpstack/ftrace: convert dump_trace() callbacks to use ftrace_graph_ret_addr() Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 28/51] ftrace/x86: implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTR Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 29/51] x86/dumpstack/ftrace: mark function graph handler function as unreliable Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 30/51] x86/dumpstack/ftrace: don't print unreliable addresses in print_context_stack_bp() Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 31/51] x86/dumpstack: allow preemption in show_stack_log_lvl() and dump_trace() Josh Poimboeuf
2016-08-14  7:45   ` Andy Lutomirski
2016-08-15 15:32     ` Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 32/51] x86/dumpstack: simplify in_exception_stack() Josh Poimboeuf
2016-08-14  7:48   ` Andy Lutomirski
2016-08-15 15:34     ` Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 33/51] x86/dumpstack: add get_stack_info() interface Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 34/51] x86/dumpstack: add recursion checking for all stacks Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 35/51] x86/unwind: add new unwind interface and implementations Josh Poimboeuf
2016-08-15 21:43   ` Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 36/51] perf/x86: convert perf_callchain_kernel() to use the new unwinder Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 37/51] x86/stacktrace: convert save_stack_trace_*() " Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 38/51] oprofile/x86: convert x86_backtrace() " Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 39/51] x86/dumpstack: convert show_trace_log_lvl() " Josh Poimboeuf
2016-08-14  8:13   ` Andy Lutomirski
2016-08-15 16:44     ` Josh Poimboeuf
2016-08-12 14:28 ` [PATCH v3 40/51] x86/dumpstack: remove dump_trace() and related callbacks Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 41/51] x86/entry/unwind: create stack frames for saved interrupt registers Josh Poimboeuf
2016-08-14  8:10   ` Andy Lutomirski
2016-08-15 16:33     ` Josh Poimboeuf [this message]
2016-08-12 14:29 ` [PATCH v3 42/51] x86/unwind: create stack frames for saved syscall registers Josh Poimboeuf
2016-08-14  8:23   ` Andy Lutomirski
2016-08-15 16:52     ` Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 43/51] x86/dumpstack: print stack identifier on its own line Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 44/51] x86/dumpstack: print any pt_regs found on the stack Josh Poimboeuf
2016-08-14  8:16   ` Andy Lutomirski
2016-08-12 14:29 ` [PATCH v3 45/51] x86: remove 64-byte gap at end of irq stack Josh Poimboeuf
2016-08-14  7:52   ` Andy Lutomirski
2016-08-14 12:50     ` Brian Gerst
2016-08-15 17:00       ` Josh Poimboeuf
2016-08-15 15:42     ` Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 46/51] x86/unwind: warn on kernel stack corruption Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 47/51] x86/unwind: warn on bad stack return address Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 48/51] x86/unwind: warn if stack grows up Josh Poimboeuf
2016-08-14  7:56   ` Andy Lutomirski
2016-08-15 16:25     ` Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 49/51] x86/dumpstack: warn on stack recursion Josh Poimboeuf
2016-08-12 14:29 ` [PATCH v3 50/51] x86/mm: move arch_within_stack_frames() to usercopy.c Josh Poimboeuf
2016-08-12 17:36   ` Kees Cook
2016-08-12 19:12     ` Josh Poimboeuf
2016-08-12 20:06       ` Kees Cook
2016-08-12 20:36         ` Josh Poimboeuf
2016-08-12 20:44           ` Kees Cook
2016-08-12 14:29 ` [PATCH v3 51/51] x86/mm: convert arch_within_stack_frames() to use the new unwinder Josh Poimboeuf
2016-08-12 15:17   ` Josh Poimboeuf
2016-08-12 17:38     ` Kees Cook
2016-08-12 19:15       ` Josh Poimboeuf
2016-08-12 20:41   ` Josh Poimboeuf
2016-08-12 20:47     ` Kees Cook

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=20160815163359.dciqlp3224othrzi@treble \
    --to=jpoimboe@redhat.com \
    --cc=brgerst@gmail.com \
    --cc=byungchul.park@lge.com \
    --cc=fweisbec@gmail.com \
    --cc=hpa@zytor.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luto@amacapital.net \
    --cc=mingo@kernel.org \
    --cc=nilayvaish@gmail.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox