lkml.org 
[lkml]   [2020]   [Jun]   [11]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    From
    Subject[PATCH] x86/entry: Treat BUG/WARN as NMI-like entries
    Date
    If we BUG or WARN in a funny RCU context, we cleverly optimize the
    BUG/WARN using the ud2 hack, which takes us through the
    idtentry_enter...() paths, which might helpfully WARN that the RCU
    context is invalid, which results in infinite recursion.

    Split the BUG/WARN handling into an nmi_enter()/nmi_exit() path in
    exc_invalid_op() to increase the chance that we survive the
    experience.

    Signed-off-by: Andy Lutomirski <luto@kernel.org>
    ---

    This is not as well tested as I would like, but it does cause the splat
    I'm chasing to display a nice warning instead of causing an undebuggable
    stack overflow.

    (It would have been debuggable on x86_64, but it's a 32-bit splat, and
    x86_32 doesn't have ORC.)

    arch/x86/kernel/traps.c | 61 +++++++++++++++++++++++------------------
    arch/x86/mm/extable.c | 15 ++++++++--
    2 files changed, 48 insertions(+), 28 deletions(-)

    diff --git a/arch/x86/kernel/traps.c b/arch/x86/kernel/traps.c
    index cb8c3d26cdf5..6340b12a6616 100644
    --- a/arch/x86/kernel/traps.c
    +++ b/arch/x86/kernel/traps.c
    @@ -98,24 +98,6 @@ int is_valid_bugaddr(unsigned long addr)
    return ud == INSN_UD0 || ud == INSN_UD2;
    }

    -int fixup_bug(struct pt_regs *regs, int trapnr)
    -{
    - if (trapnr != X86_TRAP_UD)
    - return 0;
    -
    - switch (report_bug(regs->ip, regs)) {
    - case BUG_TRAP_TYPE_NONE:
    - case BUG_TRAP_TYPE_BUG:
    - break;
    -
    - case BUG_TRAP_TYPE_WARN:
    - regs->ip += LEN_UD2;
    - return 1;
    - }
    -
    - return 0;
    -}
    -
    static nokprobe_inline int
    do_trap_no_signal(struct task_struct *tsk, int trapnr, const char *str,
    struct pt_regs *regs, long error_code)
    @@ -191,13 +173,6 @@ static void do_error_trap(struct pt_regs *regs, long error_code, char *str,
    {
    RCU_LOCKDEP_WARN(!rcu_is_watching(), "entry code didn't wake RCU");

    - /*
    - * WARN*()s end up here; fix them up before we call the
    - * notifier chain.
    - */
    - if (!user_mode(regs) && fixup_bug(regs, trapnr))
    - return;
    -
    if (notify_die(DIE_TRAP, str, regs, error_code, trapnr, signr) !=
    NOTIFY_STOP) {
    cond_local_irq_enable(regs);
    @@ -242,9 +217,43 @@ static inline void handle_invalid_op(struct pt_regs *regs)
    ILL_ILLOPN, error_get_trap_addr(regs));
    }

    -DEFINE_IDTENTRY(exc_invalid_op)
    +DEFINE_IDTENTRY_RAW(exc_invalid_op)
    {
    + bool rcu_exit;
    +
    + /*
    + * Handle BUG/WARN like NMIs instead of like normal idtentries:
    + * if we bugged/warned in a bad RCU context, for example, the last
    + * thing we want is to BUG/WARN again in the idtentry code, ad
    + * infinitum.
    + */
    + if (!user_mode(regs) && is_valid_bugaddr(regs->ip)) {
    + enum bug_trap_type type;
    +
    + nmi_enter();
    + instrumentation_begin();
    + type = report_bug(regs->ip, regs);
    + instrumentation_end();
    + nmi_exit();
    +
    + if (type == BUG_TRAP_TYPE_WARN) {
    + /* Skip the ud2. */
    + regs->ip += LEN_UD2;
    + return;
    + }
    +
    + /*
    + * Else, if this was a BUG and report_bug returns or if this
    + * was just a normal #UD, we want to continue onward and
    + * crash.
    + */
    + }
    +
    + rcu_exit = idtentry_enter_cond_rcu(regs);
    + instrumentation_begin();
    handle_invalid_op(regs);
    + instrumentation_end();
    + idtentry_exit_cond_rcu(regs, rcu_exit);
    }

    DEFINE_IDTENTRY(exc_coproc_segment_overrun)
    diff --git a/arch/x86/mm/extable.c b/arch/x86/mm/extable.c
    index b991aa4bdfae..1d6cb07f4f86 100644
    --- a/arch/x86/mm/extable.c
    +++ b/arch/x86/mm/extable.c
    @@ -204,8 +204,19 @@ void __init early_fixup_exception(struct pt_regs *regs, int trapnr)
    if (fixup_exception(regs, trapnr, regs->orig_ax, 0))
    return;

    - if (fixup_bug(regs, trapnr))
    - return;
    + if (trapnr == X86_TRAP_UD) {
    + if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) {
    + /* Skip the ud2. */
    + regs->ip += LEN_UD2;
    + return;
    + }
    +
    + /*
    + * If this was a BUG and report_bug returns or if this
    + * was just a normal #UD, we want to continue onward and
    + * crash.
    + */
    + }

    fail:
    early_printk("PANIC: early exception 0x%02x IP %lx:%lx error %lx cr2 0x%lx\n",
    --
    2.25.4
    \
     
     \ /
      Last update: 2020-06-12 05:27    [W:3.126 / U:0.016 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site