Skip to content

Commit 145a773

Browse files
author
Peter Zijlstra
committed
x86/entry: Fix #UD vs WARN more
vmlinux.o: warning: objtool: exc_invalid_op()+0x47: call to probe_kernel_read() leaves .noinstr.text section Since we use UD2 as a short-cut for 'CALL __WARN', treat it as such. Have the bare exception handler do the report_bug() thing. Fixes: 15a416e ("x86/entry: Treat BUG/WARN as NMI-like entries") Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: Andy Lutomirski <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent c7aadc0 commit 145a773

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

arch/x86/kernel/traps.c

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,16 @@ static inline void cond_local_irq_disable(struct pt_regs *regs)
8484
local_irq_disable();
8585
}
8686

87-
int is_valid_bugaddr(unsigned long addr)
87+
__always_inline int is_valid_bugaddr(unsigned long addr)
8888
{
89-
unsigned short ud;
90-
9189
if (addr < TASK_SIZE_MAX)
9290
return 0;
9391

94-
if (probe_kernel_address((unsigned short *)addr, ud))
95-
return 0;
96-
97-
return ud == INSN_UD0 || ud == INSN_UD2;
92+
/*
93+
* We got #UD, if the text isn't readable we'd have gotten
94+
* a different exception.
95+
*/
96+
return *(unsigned short *)addr == INSN_UD2;
9897
}
9998

10099
static nokprobe_inline int
@@ -216,40 +215,45 @@ static inline void handle_invalid_op(struct pt_regs *regs)
216215
ILL_ILLOPN, error_get_trap_addr(regs));
217216
}
218217

219-
DEFINE_IDTENTRY_RAW(exc_invalid_op)
218+
static noinstr bool handle_bug(struct pt_regs *regs)
220219
{
221-
bool rcu_exit;
220+
bool handled = false;
221+
222+
if (!is_valid_bugaddr(regs->ip))
223+
return handled;
222224

223225
/*
224-
* Handle BUG/WARN like NMIs instead of like normal idtentries:
225-
* if we bugged/warned in a bad RCU context, for example, the last
226-
* thing we want is to BUG/WARN again in the idtentry code, ad
227-
* infinitum.
226+
* All lies, just get the WARN/BUG out.
228227
*/
229-
if (!user_mode(regs) && is_valid_bugaddr(regs->ip)) {
230-
enum bug_trap_type type;
228+
instrumentation_begin();
229+
/*
230+
* Since we're emulating a CALL with exceptions, restore the interrupt
231+
* state to what it was at the exception site.
232+
*/
233+
if (regs->flags & X86_EFLAGS_IF)
234+
raw_local_irq_enable();
235+
if (report_bug(regs->ip, regs) == BUG_TRAP_TYPE_WARN) {
236+
regs->ip += LEN_UD2;
237+
handled = true;
238+
}
239+
if (regs->flags & X86_EFLAGS_IF)
240+
raw_local_irq_disable();
241+
instrumentation_end();
231242

232-
nmi_enter();
233-
instrumentation_begin();
234-
trace_hardirqs_off_finish();
235-
type = report_bug(regs->ip, regs);
236-
if (regs->flags & X86_EFLAGS_IF)
237-
trace_hardirqs_on_prepare();
238-
instrumentation_end();
239-
nmi_exit();
243+
return handled;
244+
}
240245

241-
if (type == BUG_TRAP_TYPE_WARN) {
242-
/* Skip the ud2. */
243-
regs->ip += LEN_UD2;
244-
return;
245-
}
246+
DEFINE_IDTENTRY_RAW(exc_invalid_op)
247+
{
248+
bool rcu_exit;
246249

247-
/*
248-
* Else, if this was a BUG and report_bug returns or if this
249-
* was just a normal #UD, we want to continue onward and
250-
* crash.
251-
*/
252-
}
250+
/*
251+
* We use UD2 as a short encoding for 'CALL __WARN', as such
252+
* handle it before exception entry to avoid recursive WARN
253+
* in case exception entry is the one triggering WARNs.
254+
*/
255+
if (!user_mode(regs) && handle_bug(regs))
256+
return;
253257

254258
rcu_exit = idtentry_enter_cond_rcu(regs);
255259
instrumentation_begin();

0 commit comments

Comments
 (0)