Skip to content

Commit 860238a

Browse files
author
Peter Zijlstra
committed
x86_64/bug: Inline the UD1
(Ab)use the static_call infrastructure to convert all: call __WARN_trap instances into the desired: ud1 (%edx), %rdi eliminating the CALL/RET, but more importantly, fixing the fact that all WARNs will have: RIP: 0010:__WARN_trap+0 Basically, by making it a static_call trampoline call, objtool will collect the callsites, and then the inline rewrite will hit the special case and replace the code with the magic instruction. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://patch.msgid.link/20251110115758.456717741@infradead.org
1 parent 11bb494 commit 860238a

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

arch/x86/include/asm/bug.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ do { \
140140
#ifdef HAVE_ARCH_BUG_FORMAT_ARGS
141141

142142
#ifndef __ASSEMBLY__
143+
#include <linux/static_call_types.h>
144+
DECLARE_STATIC_CALL(WARN_trap, __WARN_trap);
145+
143146
struct pt_regs;
144147
struct sysv_va_list { /* from AMD64 System V ABI */
145148
unsigned int gp_offset;
@@ -171,7 +174,7 @@ extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
171174
#define __WARN_print_arg(flags, format, arg...) \
172175
do { \
173176
int __flags = (flags) | BUGFLAG_WARNING | BUGFLAG_ARGS ; \
174-
__WARN_trap(__WARN_bug_entry(__flags, format), ## arg); \
177+
static_call_mod(WARN_trap)(__WARN_bug_entry(__flags, format), ## arg); \
175178
asm (""); /* inhibit tail-call optimization */ \
176179
} while (0)
177180

arch/x86/kernel/static_call.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ static const u8 xor5rax[] = { 0x2e, 0x2e, 0x2e, 0x31, 0xc0 };
2626

2727
static const u8 retinsn[] = { RET_INSN_OPCODE, 0xcc, 0xcc, 0xcc, 0xcc };
2828

29+
/*
30+
* ud1 (%edx),%rdi -- see __WARN_trap() / decode_bug()
31+
*/
32+
static const u8 warninsn[] = { 0x67, 0x48, 0x0f, 0xb9, 0x3a };
33+
2934
static u8 __is_Jcc(u8 *insn) /* Jcc.d32 */
3035
{
3136
u8 ret = 0;
@@ -69,7 +74,10 @@ static void __ref __static_call_transform(void *insn, enum insn_type type,
6974
emulate = code;
7075
code = &xor5rax;
7176
}
72-
77+
if (func == &__WARN_trap) {
78+
emulate = code;
79+
code = &warninsn;
80+
}
7381
break;
7482

7583
case NOP:
@@ -128,7 +136,8 @@ static void __static_call_validate(u8 *insn, bool tail, bool tramp)
128136
} else {
129137
if (opcode == CALL_INSN_OPCODE ||
130138
!memcmp(insn, x86_nops[5], 5) ||
131-
!memcmp(insn, xor5rax, 5))
139+
!memcmp(insn, xor5rax, 5) ||
140+
!memcmp(insn, warninsn, 5))
132141
return;
133142
}
134143

arch/x86/kernel/traps.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <linux/kexec.h>
3232
#include <linux/sched.h>
3333
#include <linux/sched/task_stack.h>
34+
#include <linux/static_call.h>
3435
#include <linux/timer.h>
3536
#include <linux/init.h>
3637
#include <linux/bug.h>
@@ -215,6 +216,9 @@ static inline unsigned long pt_regs_val(struct pt_regs *regs, int nr)
215216
}
216217

217218
#ifdef HAVE_ARCH_BUG_FORMAT_ARGS
219+
DEFINE_STATIC_CALL(WARN_trap, __WARN_trap);
220+
EXPORT_STATIC_CALL_TRAMP(WARN_trap);
221+
218222
/*
219223
* Create a va_list from an exception context.
220224
*/

0 commit comments

Comments
 (0)