Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'x86/jumplabel' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip

Pull x86 jumplabel changes from Peter Anvin:
"One more x86 tree for this merge window. This tree improves the
handling of jump labels, so that most of the time we don't have to do
a massive initial patching run.

Furthermore, we will error out of the jump label is not what is
expected, eg if it has been corrupted or tampered with"

* 'x86/jumplabel' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
x86/jump-label: Show where and what was wrong on errors
x86/jump-label: Add safety checks to jump label conversions
x86/jump-label: Do not bother updating nops if they are correct
x86/jump-label: Use best default nops for inital jump label calls

+73 -6
+7 -2
arch/x86/include/asm/jump_label.h
··· 3 3 4 4 #ifdef __KERNEL__ 5 5 6 + #include <linux/stringify.h> 6 7 #include <linux/types.h> 7 8 #include <asm/nops.h> 8 9 #include <asm/asm.h> 9 10 10 11 #define JUMP_LABEL_NOP_SIZE 5 11 12 12 - #define STATIC_KEY_INITIAL_NOP ".byte 0xe9 \n\t .long 0\n\t" 13 + #ifdef CONFIG_X86_64 14 + # define STATIC_KEY_INIT_NOP P6_NOP5_ATOMIC 15 + #else 16 + # define STATIC_KEY_INIT_NOP GENERIC_NOP5_ATOMIC 17 + #endif 13 18 14 19 static __always_inline bool arch_static_branch(struct static_key *key) 15 20 { 16 21 asm goto("1:" 17 - STATIC_KEY_INITIAL_NOP 22 + ".byte " __stringify(STATIC_KEY_INIT_NOP) "\n\t" 18 23 ".pushsection __jump_table, \"aw\" \n\t" 19 24 _ASM_ALIGN "\n\t" 20 25 _ASM_PTR "1b, %l[l_yes], %c0 \n\t"
+66 -4
arch/x86/kernel/jump_label.c
··· 24 24 } __attribute__((packed)); 25 25 }; 26 26 27 + static void bug_at(unsigned char *ip, int line) 28 + { 29 + /* 30 + * The location is not an op that we were expecting. 31 + * Something went wrong. Crash the box, as something could be 32 + * corrupting the kernel. 33 + */ 34 + pr_warning("Unexpected op at %pS [%p] (%02x %02x %02x %02x %02x) %s:%d\n", 35 + ip, ip, ip[0], ip[1], ip[2], ip[3], ip[4], __FILE__, line); 36 + BUG(); 37 + } 38 + 27 39 static void __jump_label_transform(struct jump_entry *entry, 28 40 enum jump_label_type type, 29 - void *(*poker)(void *, const void *, size_t)) 41 + void *(*poker)(void *, const void *, size_t), 42 + int init) 30 43 { 31 44 union jump_code_union code; 45 + const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5]; 32 46 33 47 if (type == JUMP_LABEL_ENABLE) { 48 + /* 49 + * We are enabling this jump label. If it is not a nop 50 + * then something must have gone wrong. 51 + */ 52 + if (unlikely(memcmp((void *)entry->code, ideal_nop, 5) != 0)) 53 + bug_at((void *)entry->code, __LINE__); 54 + 34 55 code.jump = 0xe9; 35 56 code.offset = entry->target - 36 57 (entry->code + JUMP_LABEL_NOP_SIZE); 37 - } else 58 + } else { 59 + /* 60 + * We are disabling this jump label. If it is not what 61 + * we think it is, then something must have gone wrong. 62 + * If this is the first initialization call, then we 63 + * are converting the default nop to the ideal nop. 64 + */ 65 + if (init) { 66 + const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; 67 + if (unlikely(memcmp((void *)entry->code, default_nop, 5) != 0)) 68 + bug_at((void *)entry->code, __LINE__); 69 + } else { 70 + code.jump = 0xe9; 71 + code.offset = entry->target - 72 + (entry->code + JUMP_LABEL_NOP_SIZE); 73 + if (unlikely(memcmp((void *)entry->code, &code, 5) != 0)) 74 + bug_at((void *)entry->code, __LINE__); 75 + } 38 76 memcpy(&code, ideal_nops[NOP_ATOMIC5], JUMP_LABEL_NOP_SIZE); 77 + } 39 78 40 79 /* 41 80 * Make text_poke_bp() a default fallback poker. ··· 96 57 { 97 58 get_online_cpus(); 98 59 mutex_lock(&text_mutex); 99 - __jump_label_transform(entry, type, NULL); 60 + __jump_label_transform(entry, type, NULL, 0); 100 61 mutex_unlock(&text_mutex); 101 62 put_online_cpus(); 102 63 } 103 64 65 + static enum { 66 + JL_STATE_START, 67 + JL_STATE_NO_UPDATE, 68 + JL_STATE_UPDATE, 69 + } jlstate __initdata_or_module = JL_STATE_START; 70 + 104 71 __init_or_module void arch_jump_label_transform_static(struct jump_entry *entry, 105 72 enum jump_label_type type) 106 73 { 107 - __jump_label_transform(entry, type, text_poke_early); 74 + /* 75 + * This function is called at boot up and when modules are 76 + * first loaded. Check if the default nop, the one that is 77 + * inserted at compile time, is the ideal nop. If it is, then 78 + * we do not need to update the nop, and we can leave it as is. 79 + * If it is not, then we need to update the nop to the ideal nop. 80 + */ 81 + if (jlstate == JL_STATE_START) { 82 + const unsigned char default_nop[] = { STATIC_KEY_INIT_NOP }; 83 + const unsigned char *ideal_nop = ideal_nops[NOP_ATOMIC5]; 84 + 85 + if (memcmp(ideal_nop, default_nop, 5) != 0) 86 + jlstate = JL_STATE_UPDATE; 87 + else 88 + jlstate = JL_STATE_NO_UPDATE; 89 + } 90 + if (jlstate == JL_STATE_UPDATE) 91 + __jump_label_transform(entry, type, text_poke_early, 1); 108 92 } 109 93 110 94 #endif