Skip to content

Commit 5ccaeed

Browse files
mrutland-armAlexei Starovoitov
authored andcommitted
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 <[email protected]> Co-developed-by: Maxwell Bland <[email protected]> Signed-off-by: Maxwell Bland <[email protected]> Co-developed-by: Sami Tolvanen <[email protected]> Signed-off-by: Sami Tolvanen <[email protected]> Tested-by: Dao Huang <[email protected]> Acked-by: Will Deacon <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 13cb757 commit 5ccaeed

File tree

3 files changed

+29
-60
lines changed

3 files changed

+29
-60
lines changed

arch/riscv/kernel/cfi.c

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* Copyright (C) 2023 Google LLC
66
*/
7+
#include <linux/cfi_types.h>
78
#include <linux/cfi.h>
89
#include <asm/insn.h>
910

@@ -82,41 +83,11 @@ struct bpf_insn;
8283
/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
8384
extern unsigned int __bpf_prog_runX(const void *ctx,
8485
const struct bpf_insn *insn);
85-
86-
/*
87-
* Force a reference to the external symbol so the compiler generates
88-
* __kcfi_typid.
89-
*/
90-
__ADDRESSABLE(__bpf_prog_runX);
91-
92-
/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
93-
asm (
94-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
95-
" .type cfi_bpf_hash,@object \n"
96-
" .globl cfi_bpf_hash \n"
97-
" .p2align 2, 0x0 \n"
98-
"cfi_bpf_hash: \n"
99-
" .word __kcfi_typeid___bpf_prog_runX \n"
100-
" .size cfi_bpf_hash, 4 \n"
101-
" .popsection \n"
102-
);
86+
DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX);
10387

10488
/* Must match bpf_callback_t */
10589
extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
106-
107-
__ADDRESSABLE(__bpf_callback_fn);
108-
109-
/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
110-
asm (
111-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
112-
" .type cfi_bpf_subprog_hash,@object \n"
113-
" .globl cfi_bpf_subprog_hash \n"
114-
" .p2align 2, 0x0 \n"
115-
"cfi_bpf_subprog_hash: \n"
116-
" .word __kcfi_typeid___bpf_callback_fn \n"
117-
" .size cfi_bpf_subprog_hash, 4 \n"
118-
" .popsection \n"
119-
);
90+
DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn);
12091

12192
u32 cfi_get_func_hash(void *func)
12293
{

arch/x86/kernel/alternative.c

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define pr_fmt(fmt) "SMP alternatives: " fmt
33

44
#include <linux/mmu_context.h>
5+
#include <linux/cfi_types.h>
56
#include <linux/perf_event.h>
67
#include <linux/vmalloc.h>
78
#include <linux/memory.h>
@@ -1189,37 +1190,11 @@ struct bpf_insn;
11891190
/* Must match bpf_func_t / DEFINE_BPF_PROG_RUN() */
11901191
extern unsigned int __bpf_prog_runX(const void *ctx,
11911192
const struct bpf_insn *insn);
1192-
1193-
KCFI_REFERENCE(__bpf_prog_runX);
1194-
1195-
/* u32 __ro_after_init cfi_bpf_hash = __kcfi_typeid___bpf_prog_runX; */
1196-
asm (
1197-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
1198-
" .type cfi_bpf_hash,@object \n"
1199-
" .globl cfi_bpf_hash \n"
1200-
" .p2align 2, 0x0 \n"
1201-
"cfi_bpf_hash: \n"
1202-
" .long __kcfi_typeid___bpf_prog_runX \n"
1203-
" .size cfi_bpf_hash, 4 \n"
1204-
" .popsection \n"
1205-
);
1193+
DEFINE_CFI_TYPE(cfi_bpf_hash, __bpf_prog_runX);
12061194

12071195
/* Must match bpf_callback_t */
12081196
extern u64 __bpf_callback_fn(u64, u64, u64, u64, u64);
1209-
1210-
KCFI_REFERENCE(__bpf_callback_fn);
1211-
1212-
/* u32 __ro_after_init cfi_bpf_subprog_hash = __kcfi_typeid___bpf_callback_fn; */
1213-
asm (
1214-
" .pushsection .data..ro_after_init,\"aw\",@progbits \n"
1215-
" .type cfi_bpf_subprog_hash,@object \n"
1216-
" .globl cfi_bpf_subprog_hash \n"
1217-
" .p2align 2, 0x0 \n"
1218-
"cfi_bpf_subprog_hash: \n"
1219-
" .long __kcfi_typeid___bpf_callback_fn \n"
1220-
" .size cfi_bpf_subprog_hash, 4 \n"
1221-
" .popsection \n"
1222-
);
1197+
DEFINE_CFI_TYPE(cfi_bpf_subprog_hash, __bpf_callback_fn);
12231198

12241199
u32 cfi_get_func_hash(void *func)
12251200
{

include/linux/cfi_types.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,28 @@
4141
SYM_TYPED_START(name, SYM_L_GLOBAL, SYM_A_ALIGN)
4242
#endif
4343

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+
4467
#endif /* __ASSEMBLY__ */
4568
#endif /* _LINUX_CFI_TYPES_H */

0 commit comments

Comments
 (0)