]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
x86_64/bug: Inline the UD1
authorPeter Zijlstra <peterz@infradead.org>
Sat, 7 Jun 2025 08:06:40 +0000 (10:06 +0200)
committerPeter Zijlstra <peterz@infradead.org>
Mon, 24 Nov 2025 19:23:25 +0000 (20:23 +0100)
(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
arch/x86/include/asm/bug.h
arch/x86/kernel/static_call.c
arch/x86/kernel/traps.c

index 87199e6633f9835b7303a8b5532b39b942d3f1dc..d0a96ff5c02c0baa80ac92f53dfc5c891c592b3b 100644 (file)
@@ -140,6 +140,9 @@ do {                                                                        \
 #ifdef HAVE_ARCH_BUG_FORMAT_ARGS
 
 #ifndef __ASSEMBLY__
+#include <linux/static_call_types.h>
+DECLARE_STATIC_CALL(WARN_trap, __WARN_trap);
+
 struct pt_regs;
 struct sysv_va_list { /* from AMD64 System V ABI */
        unsigned int gp_offset;
@@ -171,7 +174,7 @@ extern void *__warn_args(struct arch_va_list *args, struct pt_regs *regs);
 #define __WARN_print_arg(flags, format, arg...)                                \
 do {                                                                   \
        int __flags = (flags) | BUGFLAG_WARNING | BUGFLAG_ARGS ;        \
-       __WARN_trap(__WARN_bug_entry(__flags, format), ## arg);         \
+       static_call_mod(WARN_trap)(__WARN_bug_entry(__flags, format), ## arg); \
        asm (""); /* inhibit tail-call optimization */                  \
 } while (0)
 
index 378c388d1b3105f8ec49c19387a3d2b36fcda2d1..2892cdb145638ae75dc96bcc824b62959c7c1ec5 100644 (file)
@@ -26,6 +26,11 @@ static const u8 xor5rax[] = { 0x2e, 0x2e, 0x2e, 0x31, 0xc0 };
 
 static const u8 retinsn[] = { RET_INSN_OPCODE, 0xcc, 0xcc, 0xcc, 0xcc };
 
+/*
+ * ud1    (%edx),%rdi -- see __WARN_trap() / decode_bug()
+ */
+static const u8 warninsn[] = { 0x67, 0x48, 0x0f, 0xb9, 0x3a };
+
 static u8 __is_Jcc(u8 *insn) /* Jcc.d32 */
 {
        u8 ret = 0;
@@ -69,7 +74,10 @@ static void __ref __static_call_transform(void *insn, enum insn_type type,
                        emulate = code;
                        code = &xor5rax;
                }
-
+               if (func == &__WARN_trap) {
+                       emulate = code;
+                       code = &warninsn;
+               }
                break;
 
        case NOP:
@@ -128,7 +136,8 @@ static void __static_call_validate(u8 *insn, bool tail, bool tramp)
        } else {
                if (opcode == CALL_INSN_OPCODE ||
                    !memcmp(insn, x86_nops[5], 5) ||
-                   !memcmp(insn, xor5rax, 5))
+                   !memcmp(insn, xor5rax, 5) ||
+                   !memcmp(insn, warninsn, 5))
                        return;
        }
 
index fbbe3ab145a71952817ecc624f89984393e76ea0..cb324cc1fd99c292aebcdd683187456738f8dea7 100644 (file)
@@ -31,6 +31,7 @@
 #include <linux/kexec.h>
 #include <linux/sched.h>
 #include <linux/sched/task_stack.h>
+#include <linux/static_call.h>
 #include <linux/timer.h>
 #include <linux/init.h>
 #include <linux/bug.h>
@@ -215,6 +216,9 @@ static inline unsigned long pt_regs_val(struct pt_regs *regs, int nr)
 }
 
 #ifdef HAVE_ARCH_BUG_FORMAT_ARGS
+DEFINE_STATIC_CALL(WARN_trap, __WARN_trap);
+EXPORT_STATIC_CALL_TRAMP(WARN_trap);
+
 /*
  * Create a va_list from an exception context.
  */