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

Configure Feed

Select the types of activity you want to include in your feed.

cfi: add C CFI type macro

Currently x86 and riscv open-code 4 instances of the same logic to
define a u32 variable with the KCFI typeid of a given function.

Replace the duplicate logic with a common macro.

Signed-off-by: Mark Rutland <mark.rutland@arm.com>
Co-developed-by: Maxwell Bland <mbland@motorola.com>
Signed-off-by: Maxwell Bland <mbland@motorola.com>
Co-developed-by: Sami Tolvanen <samitolvanen@google.com>
Signed-off-by: Sami Tolvanen <samitolvanen@google.com>
Tested-by: Dao Huang <huangdao1@oppo.com>
Acked-by: Will Deacon <will@kernel.org>
Link: https://lore.kernel.org/r/20250801001004.1859976-6-samitolvanen@google.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Mark Rutland and committed by
Alexei Starovoitov
5ccaeedb 13cb7573

+29 -60
+3 -32
arch/riscv/kernel/cfi.c
··· 4 4 * 5 5 * Copyright (C) 2023 Google LLC 6 6 */ 7 + #include <linux/cfi_types.h> 7 8 #include <linux/cfi.h> 8 9 #include <asm/insn.h> 9 10 ··· 83 82 /* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */ 84 83 extern unsigned int __bpf_prog_runX(const void *ctx, 85 84 const struct bpf_insn *insn); 86 - 87 - /* 88 - * Force a reference to the external symbol so the compiler generates 89 - * __kcfi_typid. 90 - */ 91 - __ADDRESSABLE(__bpf_prog_runX); 92 - 93 - /* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */ 94 - asm ( 95 - " .pushsection .data..ro_after_init,\"aw\",@progbits \n" 96 - " .type cfi_bpf_hash,@object \n" 97 - " .globl cfi_bpf_hash \n" 98 - " .p2align 2, 0x0 \n" 99 - "cfi_bpf_hash: \n" 100 - " .word __kcfi_typeid___bpf_prog_runX \n" 101 - " .size cfi_bpf_hash, 4 \n" 102 - " .popsection \n" 103 - ); 85 + DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); 104 86 105 87 /* Must match bpf_callback_t */ 106 88 extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64); 107 - 108 - __ADDRESSABLE(__bpf_callback_fn); 109 - 110 - /* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */ 111 - asm ( 112 - " .pushsection .data..ro_after_init,\"aw\",@progbits \n" 113 - " .type cfi_bpf_subprog_hash,@object \n" 114 - " .globl cfi_bpf_subprog_hash \n" 115 - " .p2align 2, 0x0 \n" 116 - "cfi_bpf_subprog_hash: \n" 117 - " .word __kcfi_typeid___bpf_callback_fn \n" 118 - " .size cfi_bpf_subprog_hash, 4 \n" 119 - " .popsection \n" 120 - ); 89 + DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn); 121 90 122 91 u32 cfi_get_func_hash(void *func) 123 92 {
+3 -28
arch/x86/kernel/alternative.c
··· 2 2 #define pr_fmt(fmt) "SMP alternatives: " fmt 3 3 4 4 #include <linux/mmu_context.h> 5 + #include <linux/cfi_types.h> 5 6 #include <linux/perf_event.h> 6 7 #include <linux/vmalloc.h> 7 8 #include <linux/memory.h> ··· 1190 1189 /* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */ 1191 1190 extern unsigned int __bpf_prog_runX(const void *ctx, 1192 1191 const struct bpf_insn *insn); 1193 - 1194 - KCFI_REFERENCE(__bpf_prog_runX); 1195 - 1196 - /* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */ 1197 - asm ( 1198 - " .pushsection .data..ro_after_init,\"aw\",@progbits \n" 1199 - " .type cfi_bpf_hash,@object \n" 1200 - " .globl cfi_bpf_hash \n" 1201 - " .p2align 2, 0x0 \n" 1202 - "cfi_bpf_hash: \n" 1203 - " .long __kcfi_typeid___bpf_prog_runX \n" 1204 - " .size cfi_bpf_hash, 4 \n" 1205 - " .popsection \n" 1206 - ); 1192 + DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX); 1207 1193 1208 1194 /* Must match bpf_callback_t */ 1209 1195 extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64); 1210 - 1211 - KCFI_REFERENCE(__bpf_callback_fn); 1212 - 1213 - /* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */ 1214 - asm ( 1215 - " .pushsection .data..ro_after_init,\"aw\",@progbits \n" 1216 - " .type cfi_bpf_subprog_hash,@object \n" 1217 - " .globl cfi_bpf_subprog_hash \n" 1218 - " .p2align 2, 0x0 \n" 1219 - "cfi_bpf_subprog_hash: \n" 1220 - " .long __kcfi_typeid___bpf_callback_fn \n" 1221 - " .size cfi_bpf_subprog_hash, 4 \n" 1222 - " .popsection \n" 1223 - ); 1196 + DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn); 1224 1197 1225 1198 u32 cfi_get_func_hash(void *func) 1226 1199 {
+23
include/linux/cfi_types.h
··· 41 41 SYM_TYPED_START(name, SYM_L_GLOBAL, SYM_A_ALIGN) 42 42 #endif 43 43 44 + #else /* __ASSEMBLY__ */ 45 + 46 + #ifdef CONFIG_CFI_CLANG 47 + #define DEFINE_CFI_TYPE(name, func) \ 48 + /* \ 49 + * Force a reference to the function so the compiler generates \ 50 + * __kcfi_typeid_<func>. \ 51 + */ \ 52 + __ADDRESSABLE(func); \ 53 + /* u32 name __ro_after_init = __kcfi_typeid_<func> */ \ 54 + extern u32 name; \ 55 + asm ( \ 56 + " .pushsection .data..ro_after_init,\"aw\",\%progbits \n" \ 57 + " .type " #name ",\%object \n" \ 58 + " .globl " #name " \n" \ 59 + " .p2align 2, 0x0 \n" \ 60 + #name ": \n" \ 61 + " .4byte __kcfi_typeid_" #func " \n" \ 62 + " .size " #name ", 4 \n" \ 63 + " .popsection \n" \ 64 + ); 65 + #endif 66 + 44 67 #endif /* __ASSEMBLY__ */ 45 68 #endif /* _LINUX_CFI_TYPES_H */