Skip to contentSkip to conversation

x86,ibt: Use UDB instead of 0xEA

RFC PATCH Superseded Peter Zijlstra 6 participants 16 messages Aug 14, 2025 – Aug 15, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 x86,ibt: Use UDB instead of 0xEA

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

The code boots and passes the LKDTM CFI_FORWARD_PROTO test for various combinations (non exhaustive so far).

---
 arch/x86/include/asm/bug.h    |   9 +-
 arch/x86/include/asm/cfi.h    |   4 +-
 arch/x86/kernel/alternative.c | 193 ++++++++++++++++++++++++------------------
 arch/x86/kernel/cet.c         |   2 +-
 arch/x86/kernel/traps.c       |   8 +-
 arch/x86/lib/bhi.S            |  58 ++++++-------
 arch/x86/lib/retpoline.S      |   4 +-
 arch/x86/net/bpf_jit_comp.c   |   7 +-
 8 files changed, 158 insertions(+), 127 deletions(-)
arch/x86/include/asm/bug.h +7 -2
index e427a3f7b751..70711183a020 100644
@@ -5,14 +5,19 @@
 #include <linux/stringify.h>
 #include <linux/instrumentation.h>
 #include <linux/objtool.h>
+#include <asm/asm.h>
 
 /*
  * Despite that some emulators terminate on UD2, we use it for WARN().
  */
-#define ASM_UD2		".byte 0x0f, 0x0b"
+#define ASM_UD2		_ASM_BYTES(0x0f, 0x0b)
 #define INSN_UD2	0x0b0f
 #define LEN_UD2		2
 
+#define ASM_UDB		_ASM_BYTES(0xd6)
+#define INSN_UDB	0xd6
+#define LEN_UDB		1
+
 /*
  * In clang we have UD1s reporting UBSAN failures on X86, 64 and 32bit.
  */
@@ -26,7 +31,7 @@
 #define BUG_UD2			0xfffe
 #define BUG_UD1			0xfffd
 #define BUG_UD1_UBSAN		0xfffc
-#define BUG_EA			0xffea
+#define BUG_UDB			0xffd6
 #define BUG_LOCK		0xfff0
 
 #ifdef CONFIG_GENERIC_BUG
arch/x86/include/asm/cfi.h +2 -2
index 1751f1eb95ef..e7060328bc28 100644
@@ -71,7 +71,7 @@
  *
  * __cfi_foo:
  *   endbr64
- *   subl 0x12345678, %r10d
+ *   subl 0x12345678, %eax
  *   jz   foo
  *   ud2
  *   nop
@@ -86,7 +86,7 @@
  * indirect caller:
  *   lea foo(%rip), %r11
  *   ...
- *   movl $0x12345678, %r10d
+ *   movl $0x12345678, %eax
  *   subl $16, %r11
  *   nop4
  *   call *%r11
arch/x86/kernel/alternative.c +109 -84

363 lines not on this page · raw message

arch/x86/kernel/cet.c +1 -1
index 99444409c026..59f8bf022ec5 100644
@@ -123,7 +123,7 @@ static void do_kernel_cp_fault(struct pt_regs *regs, unsigned long error_code)
 		return;
 	}
 
-	pr_err("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs));
+	early_printk("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs));
 	if (!ibt_fatal) {
 		printk(KERN_DEFAULT CUT_HERE);
 		__warn(__FILE__, __LINE__, (void *)regs->ip, TAINT_WARN, regs, NULL);
arch/x86/kernel/traps.c +4 -4
index 36354b470590..6b22611e69cc 100644
@@ -97,7 +97,7 @@ __always_inline int is_valid_bugaddr(unsigned long addr)
  * Check for UD1 or UD2, accounting for Address Size Override Prefixes.
  * If it's a UD1, further decode to determine its use:
  *
- * FineIBT:      ea                      (bad)
+ * FineIBT:      d6                      udb
  * FineIBT:      f0 75 f9                lock jne . - 6
  * UBSan{0}:     67 0f b9 00             ud1    (%eax),%eax
  * UBSan{10}:    67 0f b9 40 10          ud1    0x10(%eax),%eax
@@ -130,9 +130,9 @@ __always_inline int decode_bug(unsigned long addr, s32 *imm, int *len)
 		WARN_ON_ONCE(!lock);
 		return BUG_LOCK;
 
-	case 0xea:
+	case 0xd6:
 		*len = addr - start;
-		return BUG_EA;
+		return BUG_UDB;
 
 	case OPCODE_ESCAPE:
 		break;
@@ -341,7 +341,7 @@ static noinstr bool handle_bug(struct pt_regs *regs)
 		}
 		fallthrough;
 
-	case BUG_EA:
+	case BUG_UDB:
 	case BUG_LOCK:
 		if (handle_cfi_failure(regs) == BUG_TRAP_TYPE_WARN) {
 			handled = true;
arch/x86/lib/bhi.S +29 -29

106 lines not on this page · raw message

arch/x86/lib/retpoline.S +2 -2
index d78d769a02bd..24b7aca454ec 100644
@@ -373,10 +373,10 @@ SYM_FUNC_END(call_depth_return_thunk)
 .macro ITS_THUNK reg
 
 /*
- * If CFI paranoid is used then the ITS thunk starts with opcodes (0xea; jne 1b)
+ * If CFI paranoid is used then the ITS thunk starts with opcodes (1: udb; jne 1b)
  * that complete the fineibt_paranoid caller sequence.
  */
-1:	.byte 0xea
+1:	ASM_UDB
 SYM_INNER_LABEL(__x86_indirect_paranoid_thunk_\reg, SYM_L_GLOBAL)
 	UNWIND_HINT_UNDEFINED
 	ANNOTATE_NOENDBR
arch/x86/net/bpf_jit_comp.c +4 -3
index 7e3fca164620..f54353ef3e51 100644
@@ -419,12 +419,13 @@ static void emit_fineibt(u8 **pprog, u8 *ip, u32 hash, int arity)
 	u8 *prog = *pprog;
 
 	EMIT_ENDBR();
-	EMIT3_off32(0x41, 0x81, 0xea, hash);		/* subl $hash, %r10d	*/
+	EMIT1_off32(0x2d, hash);			/* subl $hash, %eax	*/
 	if (cfi_bhi) {
+		EMIT2(0x2e, 0x2e);			/* cs cs */
 		emit_call(&prog, __bhi_args[arity], ip + 11);
 	} else {
-		EMIT2(0x75, 0xf9);			/* jne.d8 .-7		*/
-		EMIT3(0x0f, 0x1f, 0x00);		/* nop3			*/
+		EMIT5(0x67, 0x0f, 0x1f, 0x40, 0xd6);	/* nopl -42(%eax)	*/
+		EMIT2(0x75, 0xfd);			/* jne.d8 .-3		*/
 	}
 	EMIT_ENDBR_POISON();
 
#1 lore raw

arch/x86/kernel/cet.c static void do_kernel_cp_fault(struct pt_regs *regs, unsigned long error_code) · line 129

Quoted 13 lines · Peter Zijlstra
arch/x86/kernel/cet.c +1 -1
index 99444409c026..59f8bf022ec5 100644
@@ -123,7 +123,7 @@ static void do_kernel_cp_fault(struct pt_regs *regs, unsigned long error_code)
 		return;
 	}
 
-	pr_err("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs));
+	early_printk("Missing ENDBR: %pS\n", (void *)instruction_pointer(regs));
 	if (!ibt_fatal) {
 		printk(KERN_DEFAULT CUT_HERE);
 		__warn(__FILE__, __LINE__, (void *)regs->ip, TAINT_WARN, regs, NULL);

Obviously this is debugging remnant and will go away.

#2 lore raw
In reply to Peter Zijlstra
Quoted 14 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Quoted 7 lines

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

The code boots and passes the LKDTM CFI_FORWARD_PROTO test for various combinations (non exhaustive so far).

Must be perfect :P on my hardware:

cfi=auto (i.e. cfi=fineibt,paranoid):

```
[  +0.000861] SMP alternatives: Using paranoid FineIBT CFI
...
[Aug14 11:33] lkdtm: Performing direct entry CFI_FORWARD_PROTO
[  +0.000005] lkdtm: Calling matched prototype ...
[  +0.000000] lkdtm: Calling mismatched prototype ...
[  +0.000014] CFI failure at lkdtm_indirect_call+0x1c/0x30 [lkdtm] (target: lkdtm_increment_int+0x0/0x30 [lkdtm]; expected type: 0x4d3fe584)
[  +0.000027] Oops: invalid opcode: 0000 [#1] SMP NOPTI
[  +0.000004] CPU: 0 UID: 0 PID: 3232 Comm: cat Not tainted 6.17.0-rc1-debug-next-20250814-02528-g3aebcf0edb14 #1 PREEMPT(full)  724b1db7f580ddb34e14b43820ff16d9fd75d36c
[  +0.000004] Hardware name: AZW MINI S/MINI S, BIOS ADLNV106 05/12/2024
[  +0.000002] RIP: 0010:lkdtm_indirect_call+0x1c/0x30 [lkdtm]
[  +0.000008] Code: 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 49 89 fb 48 c7 c7 30 49 fd c1 b8 84 e5 3f 4d 41 3b 43 f5 2e 4d 8d 5b <f0> 75 fd 41 ff e3 90 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 f3 0f
[  +0.000002] RSP: 0018:ffffceb847387d38 EFLAGS: 00010216
[  +0.000002] RAX: 000000004d3fe584 RBX: 0000000000000001 RCX: 332c9ba94c7b7c00
[  +0.000002] RDX: 0000000000000002 RSI: 00000000ffffefff RDI: ffffffffc1fd4930
[  +0.000002] RBP: 0000000000000002 R08: 0000000000000fff R09: ffffffffb025b2a0
[  +0.000001] R10: 0000000000002ffd R11: ffffffffc218e0d0 R12: 0000000000000000
[  +0.000002] R13: 0000000000000006 R14: ffff8bc3c3d3b000 R15: ffffffffc1fd47c0
[  +0.000001] FS:  00007f025f833740(0000) GS:ffff8bc6febb8000(0000) knlGS:0000000000000000
[  +0.000002] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  +0.000001] CR2: 00007f025f5bf000 CR3: 000000017036a002 CR4: 0000000000f72ef0
[  +0.000002] PKRU: 55555554
[  +0.000001] Call Trace:
[  +0.000002]  <TASK>
[  +0.000002]  lkdtm_CFI_FORWARD_PROTO+0x39/0x60 [lkdtm 57ec71482c5cb110c2f23373ae7338671243d293]
[  +0.000008]  lkdtm_do_action+0x26/0x40 [lkdtm 57ec71482c5cb110c2f23373ae7338671243d293]
[  +0.000009]  direct_entry+0x147/0x160 [lkdtm 57ec71482c5cb110c2f23373ae7338671243d293]
[  +0.000009]  full_proxy_write+0x9e/0x140
[  +0.000005]  vfs_write+0x11b/0x3f0
[  +0.000004]  ksys_write+0x79/0x100
[  +0.000002]  do_syscall_64+0x85/0x920
[  +0.000003]  ? do_user_addr_fault+0x271/0x680
[  +0.000003]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  +0.000002] RIP: 0033:0x7f025f6931ce
[  +0.000036] Code: 4d 89 d8 e8 64 be 00 00 4c 8b 5d f8 41 8b 93 08 03 00 00 59 5e 48 83 f8 fc 74 11 c9 c3 0f 1f 80 00 00 00 00 48 8b 45 10 0f 05 <c9> c3 83 e2 39 83 fa 08 75 e7 e8 13 ff ff ff 0f 1f 00 f3 0f 1e fa
[  +0.000001] RSP: 002b:00007ffd001e8930 EFLAGS: 00000202 ORIG_RAX: 0000000000000001
[  +0.000003] RAX: ffffffffffffffda RBX: 0000000000000012 RCX: 00007f025f6931ce
[  +0.000001] RDX: 0000000000000012 RSI: 00007f025f5bf000 RDI: 0000000000000001
[  +0.000001] RBP: 00007ffd001e8940 R08: 0000000000000000 R09: 0000000000000000
[  +0.000002] R10: 0000000000000000 R11: 0000000000000202 R12: 00007f025f5bf000
[  +0.000001] R13: 0000000000000012 R14: 0000000000000000 R15: 0000000000000000
[  +0.000002]  </TASK>
[  +0.000001] Modules linked in: lkdtm ...
[  +0.000047]  hid_logitech_dj hid_razer nvme nvme_core nvme_keyring spi_intel_pci nvme_auth spi_intel
[  +0.000006] ---[ end trace 0000000000000000 ]---
[  +0.000002] RIP: 0010:lkdtm_indirect_call+0x1c/0x30 [lkdtm]
[  +0.000008] Code: 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 49 89 fb 48 c7 c7 30 49 fd c1 b8 84 e5 3f 4d 41 3b 43 f5 2e 4d 8d 5b <f0> 75 fd 41 ff e3 90 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 f3 0f
[  +0.000002] RSP: 0018:ffffceb847387d38 EFLAGS: 00010216
[  +0.000002] RAX: 000000004d3fe584 RBX: 0000000000000001 RCX: 332c9ba94c7b7c00
[  +0.000001] RDX: 0000000000000002 RSI: 00000000ffffefff RDI: ffffffffc1fd4930
[  +0.000001] RBP: 0000000000000002 R08: 0000000000000fff R09: ffffffffb025b2a0
[  +0.000001] R10: 0000000000002ffd R11: ffffffffc218e0d0 R12: 0000000000000000
[  +0.000001] R13: 0000000000000006 R14: ffff8bc3c3d3b000 R15: ffffffffc1fd47c0
[  +0.000001] FS:  00007f025f833740(0000) GS:ffff8bc6febb8000(0000) knlGS:0000000000000000
[  +0.000002] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  +0.000001] CR2: 00007f025f5bf000 CR3: 000000017036a002 CR4: 0000000000f72ef0
[  +0.000001] PKRU: 55555554
```

cfi=fineibt,paranoid,bhi

```
[Aug14 12:27] Linux version 6.17.0-rc1-debug-next-20250814-02528-g3aebcf0edb14 (nathan@ax162) (ClangBuiltLinux clang version 22.0.0git (https://github.com/llvm/llvm-project.git 9f96e3f80f5d25e50ca8423d4eb9063989b85f0b), ClangBuiltLinux LLD 22.0.0 (https://github.com/llvm/llvm-project.git 9f96e3f80f5d25e50ca8423d4eb9063989b85f0b)) #1 SMP PREEMPT_DYNAMIC Thu Aug 14 11:11:44 MST 2025
...
[  +0.000860] SMP alternatives: Using paranoid FineIBT+BHI CFI
...
[  +1.406475] lkdtm: Performing direct entry CFI_FORWARD_PROTO
[  +0.000005] lkdtm: Calling matched prototype ...
[  +0.000001] lkdtm: Calling mismatched prototype ...
[  +0.000012] CFI failure at lkdtm_indirect_call+0x1c/0x30 [lkdtm] (target: lkdtm_increment_int+0x0/0x30 [lkdtm]; expected type: 0x0b71a5dc)
[  +0.000027] Oops: invalid opcode: 0000 [#1] SMP NOPTI
[  +0.000004] CPU: 3 UID: 0 PID: 2100 Comm: cat Tainted: G        W           6.17.0-rc1-debug-next-20250814-02528-g3aebcf0edb14 #1 PREEMPT(full)  724b1db7f580ddb34e14b43820ff16d9fd75d36c
[  +0.000004] Tainted: [W]=WARN
[  +0.000001] Hardware name: AZW MINI S/MINI S, BIOS ADLNV106 05/12/2024
[  +0.000002] RIP: 0010:lkdtm_indirect_call+0x1c/0x30 [lkdtm]
[  +0.000008] Code: 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 49 89 fb 48 c7 c7 30 89 46 c2 b8 dc a5 71 0b 41 3b 43 f5 2e 4d 8d 5b <f0> 75 fd 41 ff e3 90 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 f3 0f
[  +0.000002] RSP: 0018:ffffcf77c4507c68 EFLAGS: 00010283
[  +0.000002] RAX: 000000000b71a5dc RBX: 0000000000000001 RCX: f242256082c3a100
[  +0.000002] RDX: 0000000000000002 RSI: 00000000ffffefff RDI: ffffffffc2468930
[  +0.000002] RBP: 0000000000000002 R08: 0000000000000fff R09: ffffffff9a85b2a0
[  +0.000001] R10: 0000000000002ffd R11: ffffffffc19e40d0 R12: 0000000000000000
[  +0.000001] R13: 0000000000000006 R14: ffff8ed41e1e6000 R15: ffffffffc24687c0
[  +0.000002] FS:  00007fda4f785740(0000) GS:ffff8ed7d4738000(0000) knlGS:0000000000000000
[  +0.000002] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  +0.000001] CR2: 00007fda4f744000 CR3: 000000016856f005 CR4: 0000000000f72ef0
[  +0.000002] PKRU: 55555554
[  +0.000001] Call Trace:
[  +0.000003]  <TASK>
[  +0.000001]  lkdtm_CFI_FORWARD_PROTO+0x39/0x60 [lkdtm 57ec71482c5cb110c2f23373ae7338671243d293]
[  +0.000009]  lkdtm_do_action+0x26/0x40 [lkdtm 57ec71482c5cb110c2f23373ae7338671243d293]
[  +0.000008]  direct_entry+0x147/0x160 [lkdtm 57ec71482c5cb110c2f23373ae7338671243d293]
[  +0.000009]  full_proxy_write+0x9e/0x140
[  +0.000004]  vfs_write+0x11b/0x3f0
[  +0.000004]  ? __lruvec_stat_mod_folio+0x7c/0xc0
[  +0.000002]  ? set_ptes+0x12/0xa0
[  +0.000008]  ? do_pte_missing+0xb8c/0xf00
[  +0.000002]  ksys_write+0x79/0x100
[  +0.000003]  do_syscall_64+0x85/0x920
[  +0.000004]  ? do_user_addr_fault+0x271/0x680
[  +0.000002]  entry_SYSCALL_64_after_hwframe+0x76/0x7e
[  +0.000002] RIP: 0033:0x7fda4f4931ce
[  +0.000027] Code: 4d 89 d8 e8 64 be 00 00 4c 8b 5d f8 41 8b 93 08 03 00 00 59 5e 48 83 f8 fc 74 11 c9 c3 0f 1f 80 00 00 00 00 48 8b 45 10 0f 05 <c9> c3 83 e2 39 83 fa 08 75 e7 e8 13 ff ff ff 0f 1f 00 f3 0f 1e fa
[  +0.000001] RSP: 002b:00007fffe1b7c780 EFLAGS: 00000202 ORIG_RAX: 0000000000000001
[  +0.000002] RAX: ffffffffffffffda RBX: 0000000000000012 RCX: 00007fda4f4931ce
[  +0.000002] RDX: 0000000000000012 RSI: 00007fda4f744000 RDI: 0000000000000001
[  +0.000001] RBP: 00007fffe1b7c790 R08: 0000000000000000 R09: 0000000000000000
[  +0.000001] R10: 0000000000000000 R11: 0000000000000202 R12: 00007fda4f744000
[  +0.000001] R13: 0000000000000012 R14: 0000000000000000 R15: 0000000000000000
[  +0.000002]  </TASK>
[  +0.000000] Modules linked in: lkdtm ...
[  +0.000044]  hid_logitech_dj hid_razer nvme nvme_core nvme_keyring spi_intel_pci nvme_auth spi_intel
[  +0.000016] ---[ end trace 0000000000000000 ]---
[  +0.000002] RIP: 0010:lkdtm_indirect_call+0x1c/0x30 [lkdtm]
[  +0.000009] Code: 90 90 90 90 90 90 90 90 90 90 90 90 90 90 0f 1f 44 00 00 49 89 fb 48 c7 c7 30 89 46 c2 b8 dc a5 71 0b 41 3b 43 f5 2e 4d 8d 5b <f0> 75 fd 41 ff e3 90 66 66 66 66 2e 0f 1f 84 00 00 00 00 00 f3 0f
[  +0.000001] RSP: 0018:ffffcf77c4507c68 EFLAGS: 00010283
[  +0.000002] RAX: 000000000b71a5dc RBX: 0000000000000001 RCX: f242256082c3a100
[  +0.000001] RDX: 0000000000000002 RSI: 00000000ffffefff RDI: ffffffffc2468930
[  +0.000001] RBP: 0000000000000002 R08: 0000000000000fff R09: ffffffff9a85b2a0
[  +0.000001] R10: 0000000000002ffd R11: ffffffffc19e40d0 R12: 0000000000000000
[  +0.000001] R13: 0000000000000006 R14: ffff8ed41e1e6000 R15: ffffffffc24687c0
[  +0.000001] FS:  00007fda4f785740(0000) GS:ffff8ed7d4738000(0000) knlGS:0000000000000000
[  +0.000002] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[  +0.000001] CR2: 00007fda4f744000 CR3: 000000016856f005 CR4: 0000000000f72ef0
[  +0.000001] PKRU: 55555554
```

I also tested

cfi=fineibt,bhi
cfi=fineibt,bhi,warn
cfi=fineibt
cfi=kcfi

which showed no additional errors.

Cheers,
Nathan

#3 lore raw
In reply to Peter Zijlstra
Quoted 19 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

Quoted 4 lines

The code boots and passes the LKDTM CFI_FORWARD_PROTO test for various combinations (non exhaustive so far).

Looks good to me (and using %eax will save one byte per call site as well), but as per our IRC discussion, *my understanding* is that the best possible performance (least branch predictor impact across implementations) is to use a forward branch with a 2E prefix (jcc,pn in GAS syntax) rather than a reverse branch, if space allows.

-hpa

#4 lore raw
Quoted 23 lines · H. Peter Anvin
Quoted 22 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

The code boots and passes the LKDTM CFI_FORWARD_PROTO test for various combinations (non exhaustive so far).

Quoted 7 lines
Quoted 1 line

Looks good to me (and using %eax will save one byte per call site as well), but as per our IRC discussion, *my understanding* is that the best possible performance (least branch predictor impact across implementations) is to use a forward branch with a 2E prefix (jcc,pn in GAS syntax) rather than a reverse branch, if space allows.

Oh right. I did see that comment on IRC and them promptly forgot about it again :/ I'll have a poke. Scott, do you agree? You being responsible for the backward jump and such.

#5 lore raw
Quoted 32 lines · Peter Zijlstra
Quoted 23 lines · H. Peter Anvin
Quoted 22 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

The code boots and passes the LKDTM CFI_FORWARD_PROTO test for various combinations (non exhaustive so far).

Quoted 7 lines
Quoted 1 line

Looks good to me (and using %eax will save one byte per call site as well), but as per our IRC discussion, *my understanding* is that the best possible performance (least branch predictor impact across implementations) is to use a forward branch with a 2E prefix (jcc,pn in GAS syntax) rather than a reverse branch, if space allows.

Quoted 3 lines

Oh right. I did see that comment on IRC and them promptly forgot about it again :/ I'll have a poke. Scott, do you agree? You being responsible for the backward jump and such.

On top of the other, to show the delta.

If we want a fwd branch, we can stick the D6 in the endbr poison nop. That frees up more bytes again, but also that matches what I already did for the bhi1 case, so less special cases is more better.

I've had to use cs prefixed jcc.d32, because our older toolchains don't like the ,pn notation.

---
 arch/x86/include/asm/cfi.h    |  5 ++---
 arch/x86/include/asm/ibt.h    | 10 +++------
 arch/x86/kernel/alternative.c | 50 +++++++++++++++++++++----------------------
 arch/x86/net/bpf_jit_comp.c   |  3 +--
 4 files changed, 30 insertions(+), 38 deletions(-)
arch/x86/include/asm/cfi.h +2 -3
index 9256908b5134..3fcfdd996962 100644
@@ -72,10 +72,9 @@
  * __cfi_foo:
  *   endbr64
  *   subl 0x12345678, %eax
- *   nopl -42(%eax)
- *   jne  __cfi_foo+13
+ *   jne.32,pn foo+3
  * foo:
- *   osp nop3			# was endbr64
+ *   nopl -42(%rax)		# was endbr64
  *   ... code here ...
  *   ret
  *
arch/x86/include/asm/ibt.h +3 -7
index 28d845257303..5e45d6424722 100644
@@ -59,10 +59,10 @@ static __always_inline __attribute_const__ u32 gen_endbr(void)
 static __always_inline __attribute_const__ u32 gen_endbr_poison(void)
 {
 	/*
-	 * 4 byte NOP that isn't NOP4 (in fact it is OSP NOP3), such that it
-	 * will be unique to (former) ENDBR sites.
+	 * 4 byte NOP that isn't NOP4, such that it will be unique to (former)
+	 * ENDBR sites. Additionally it carries UDB as immediate.
 	 */
-	return 0x001f0f66; /* osp nopl (%rax) */
+	return 0xd6401f0f; /* nopl -42(%rax) */
 }
 
 static inline bool __is_endbr(u32 val)
@@ -70,10 +70,6 @@ static inline bool __is_endbr(u32 val)
 	if (val == gen_endbr_poison())
 		return true;
 
-	/* See cfi_fineibt_bhi_preamble() */
-	if (IS_ENABLED(CONFIG_FINEIBT_BHI) && val == 0x001f0ff5)
-		return true;
-
 	val &= ~0x01000000U; /* ENDBR32 -> ENDBR64 */
 	return val == gen_endbr();
 }
arch/x86/kernel/alternative.c +24 -26

141 lines not on this page · raw message

arch/x86/net/bpf_jit_comp.c +1 -2
index f54353ef3e51..5178ef1aa5c7 100644
@@ -424,8 +424,7 @@ static void emit_fineibt(u8 **pprog, u8 *ip, u32 hash, int arity)
 		EMIT2(0x2e, 0x2e);			/* cs cs */
 		emit_call(&prog, __bhi_args[arity], ip + 11);
 	} else {
-		EMIT5(0x67, 0x0f, 0x1f, 0x40, 0xd6);	/* nopl -42(%eax)	*/
-		EMIT2(0x75, 0xfd);			/* jne.d8 .-3		*/
+		EMIT3_off32(0x2e, 0x0f, 0x85, 3);	/* jne.d32,pn 3		*/
 	}
 	EMIT_ENDBR_POISON();
 
#6 lore raw
Quoted 37 lines · Peter Zijlstra
Quoted 32 lines · Peter Zijlstra
Quoted 23 lines · H. Peter Anvin
Quoted 22 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

The code boots and passes the LKDTM CFI_FORWARD_PROTO test for various combinations (non exhaustive so far).

Quoted 7 lines
Quoted 1 line

Looks good to me (and using %eax will save one byte per call site as well), but as per our IRC discussion, *my understanding* is that the best possible performance (least branch predictor impact across implementations) is to use a forward branch with a 2E prefix (jcc,pn in GAS syntax) rather than a reverse branch, if space allows.

Quoted 3 lines

Oh right. I did see that comment on IRC and them promptly forgot about it again :/ I'll have a poke. Scott, do you agree? You being responsible for the backward jump and such.

Quoted 8 lines

On top of the other, to show the delta.

If we want a fwd branch, we can stick the D6 in the endbr poison nop. That frees up more bytes again, but also that matches what I already did for the bhi1 case, so less special cases is more better.

I've had to use cs prefixed jcc.d32, because our older toolchains don't like the ,pn notation.

And then I forgot to move that cs prefix around in the bhi1 case... fixed that.

#7 lore raw
Quoted 37 lines · Peter Zijlstra
Quoted 36 lines · Peter Zijlstra
Quoted 25 lines · Peter Zijlstra
Quoted 24 lines · H. Peter Anvin
Quoted 23 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

The code boots and passes the LKDTM CFI_FORWARD_PROTO test for various combinations (non exhaustive so far).

Quoted 8 lines
Quoted 5 lines

Looks good to me (and using %eax will save one byte per call site as well), but as per our IRC discussion, *my understanding* is that the best possible performance (least branch predictor impact across implementations) is to use a forward branch with a 2E prefix (jcc,pn in GAS syntax) rather than a reverse branch, if space allows.

Oh right. I did see that comment on IRC and them promptly forgot about it again :/ I'll have a poke. Scott, do you agree? You being responsible for the backward jump and such.

On top of the other, to show the delta.

Quoted 8 lines
Quoted 6 lines

If we want a fwd branch, we can stick the D6 in the endbr poison nop. That frees up more bytes again, but also that matches what I already did for the bhi1 case, so less special cases is more better.

I've had to use cs prefixed jcc.d32, because our older toolchains don't like the ,pn notation.

And then I forgot to move that cs prefix around in the bhi1 case... fixed that.

Dare I ask what ,pn notation is?  It's not only the older toolchains that don't know it :)

~Andrew

#8 lore raw
Quoted 7 lines · Andrew Cooper
Quoted 4 lines
Quoted 2 lines

I've had to use cs prefixed jcc.d32, because our older toolchains don't like the ,pn notation.

And then I forgot to move that cs prefix around in the bhi1 case... fixed that.

Dare I ask what ,pn notation is?  It's not only the older toolchains that don't know it :)

Some fancy notation for the CS/DS branch hints.

CS Jcc, decodes to Jcc,pn for non-taken
DS Jcc, decodes to Jcc,pt for taken

#9 lore raw
Quoted 4 lines · Peter Zijlstra
Quoted 2 lines · Andrew Cooper
Quoted 2 lines
Quoted 2 lines

I've had to use cs prefixed jcc.d32, because our older toolchains don't like the ,pn notation.

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

And then I forgot to move that cs prefix around in the bhi1 case... fixed that.

Dare I ask what ,pn notation is?  It's not only the older toolchains that don't know it :)

Some fancy notation for the CS/DS branch hints.

CS Jcc, decodes to Jcc,pn for non-taken
DS Jcc, decodes to Jcc,pt for taken

Ah, thanks.  I was looking at the hex in one of the comments and still couldn't figure it out.

So with this notation, we also have the dual meaning of ,pt between the P4 and LNC.  At least the encoding is the same.

~Andrew

#10 lore raw
Quoted 10 lines · Andrew Cooper
Quoted 3 lines

CS Jcc, decodes to Jcc,pn for non-taken
DS Jcc, decodes to Jcc,pt for taken

Ah, thanks.  I was looking at the hex in one of the comments and still couldn't figure it out.

So with this notation, we also have the dual meaning of ,pt between the P4 and LNC.  At least the encoding is the same.

What "dual meaning?"

-hpa

#11 lore raw
Quoted 10 lines · H. Peter Anvin
Quoted 8 lines · Andrew Cooper
Quoted 2 lines

CS Jcc, decodes to Jcc,pn for non-taken
DS Jcc, decodes to Jcc,pt for taken

Ah, thanks.  I was looking at the hex in one of the comments and still couldn't figure it out.

So with this notation, we also have the dual meaning of ,pt between the P4 and LNC.  At least the encoding is the same.

What "dual meaning?"

Well, it has different semantics now it's been reintroduced in LNC. (Only has any effect on a prediction miss, and causes a proactive decode resteer).

Sure, it's "just perf" so can be argued as "compatible behaviour", but people caring about the ,pt / ,pn properties do need to know the different.

~Andrew

#12 lore raw
Quoted 12 lines · Andrew Cooper
Quoted 10 lines · H. Peter Anvin
Quoted 8 lines · Andrew Cooper
Quoted 2 lines

CS Jcc, decodes to Jcc,pn for non-taken
DS Jcc, decodes to Jcc,pt for taken

Ah, thanks.  I was looking at the hex in one of the comments and still couldn't figure it out.

So with this notation, we also have the dual meaning of ,pt between the P4 and LNC.  At least the encoding is the same.

What "dual meaning?"

Quoted 8 lines

Well, it has different semantics now it's been reintroduced in LNC. (Only has any effect on a prediction miss, and causes a proactive decode resteer).

Sure, it's "just perf" so can be argued as "compatible behaviour", but people caring about the ,pt / ,pn properties do need to know the different.

~Andrew

The architectural semantics are "hint to the microarchitecture that this branch is strongly biased in one specific direction", and that has been true since the P4.

There are no other semantic guarantees, including, of course, that the hardware will do anything at all with it.

Performance tuning at the uarch level changes over time and will continue to do so, so if you don't distinguish between architectural behavior and performance you have to define pretty much every instruction as semantically different in each generation.

#13 lore raw
In reply to Peter Zijlstra
Quoted 12 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Quoted 7 lines

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

rax is used to pass tail_call count.
See diagram in commit log:
https://lore.kernel.org/all/[email protected]/
Before that commit rax was used differently.
Bottom line rax was used for a long time to support bpf_tail_calls. I'm traveling atm. So cc-ing folks for follow ups.

#14 lore raw
Quoted 21 lines · Alexei Starovoitov
Quoted 12 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Quoted 7 lines

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

Quoted 6 lines

rax is used to pass tail_call count.
See diagram in commit log:
https://lore.kernel.org/all/[email protected]/
Before that commit rax was used differently.
Bottom line rax was used for a long time to support bpf_tail_calls. I'm traveling atm. So cc-ing folks for follow ups.

IIRC the bpf2bpf tailcall doesn't use CFI at the moment. But let me double check.

So emit_cfi() is called at the very start of emit_prologue() and __arch_prepare_bpf_trampoline() in the BPF_TRAMP_F_INDIRECT case.

Now, emit_prologue() starts with the CFI bits, but the tailcall lands at X86_TAIL_CALL_OFFSET, at which spot we only have EMIT_ENDBR(), nothing else. So RAX should be unaffected at that point.

So, AFAICT, we're good on that point. It is just the C level indirect function call ABI that is affected, BPF internal conventions are unaffected.

#15 lore raw
Quoted 35 lines · Peter Zijlstra
Quoted 21 lines · Alexei Starovoitov
Quoted 12 lines · Peter Zijlstra

Hi!

A while ago FineIBT started using the instruction 0xEA to generate #UD. All existing parts will generate #UD in 64bit mode on that instruction.

However; Intel/AMD have not blessed using this instruction, it is on their 'reserved' list for future use.

Peter Anvin worked the committees and got use of 0xD6 blessed, and it will be called UDB (per the next SDM or so).

Quoted 7 lines

Reworking the FineIBT code to use UDB wasn't entirely trivial, and I've had to switch the hash register to EAX in order to free up some bytes.

Per the x86_64 ABI, EAX is used to pass the number of vector registers for varargs -- something that should not happen in the kernel. More so, we build with -mskip-rax-setup, which should leave EAX completely unused in the calling convention.

Quoted 6 lines

rax is used to pass tail_call count.
See diagram in commit log:
https://lore.kernel.org/all/[email protected]/
Before that commit rax was used differently.
Bottom line rax was used for a long time to support bpf_tail_calls. I'm traveling atm. So cc-ing folks for follow ups.

IIRC the bpf2bpf tailcall doesn't use CFI at the moment. But let me double check.

So emit_cfi() is called at the very start of emit_prologue() and __arch_prepare_bpf_trampoline() in the BPF_TRAMP_F_INDIRECT case.

Quoted 8 lines

Now, emit_prologue() starts with the CFI bits, but the tailcall lands at X86_TAIL_CALL_OFFSET, at which spot we only have EMIT_ENDBR(), nothing else. So RAX should be unaffected at that point.

So, AFAICT, we're good on that point. It is just the C level indirect function call ABI that is affected, BPF internal conventions are unaffected.

RAX is used for propagating tail_call_cnt_ptr from caller to callee for bpf2bpf+tailcall on x86_64.

Before the aforementioned commit, RAX is used for propagating tail_call_cnt from caller to callee for the case.

Thanks,
Leon