Skip to content

Commit 021d1d0

Browse files
author
Avenger-285714
authored
Merge pull request #307 from sterling-teng/linux-6.6.y
LoongArch: Add kernel livepatching support
2 parents bcde9be + 9911a88 commit 021d1d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2019
-239
lines changed

arch/loongarch/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,22 @@ config LOONGARCH
133133
select HAVE_KPROBES_ON_FTRACE
134134
select HAVE_KRETPROBES
135135
select HAVE_KVM
136+
select HAVE_LIVEPATCH
136137
select HAVE_MOD_ARCH_SPECIFIC
137138
select HAVE_NMI
139+
select HAVE_OBJTOOL if AS_HAS_EXPLICIT_RELOCS
138140
select HAVE_PCI
139141
select HAVE_PERF_EVENTS
140142
select HAVE_PERF_REGS
141143
select HAVE_PERF_USER_STACK_DUMP
142144
select HAVE_REGS_AND_STACK_ACCESS_API
145+
select HAVE_RELIABLE_STACKTRACE if UNWINDER_ORC
143146
select HAVE_RETHOOK
144147
select HAVE_RSEQ
145148
select HAVE_SAMPLE_FTRACE_DIRECT
146149
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI
147150
select HAVE_SETUP_PER_CPU_AREA if NUMA
151+
select HAVE_STACK_VALIDATION if HAVE_OBJTOOL
148152
select HAVE_STACKPROTECTOR
149153
select HAVE_SYSCALL_TRACEPOINTS
150154
select HAVE_TIF_NOHZ
@@ -619,6 +623,8 @@ config RANDOMIZE_BASE_MAX_OFFSET
619623

620624
This is limited by the size of the lower address memory, 256MB.
621625

626+
source "kernel/livepatch/Kconfig"
627+
622628
endmenu
623629

624630
config ARCH_SELECT_MEMORY_MODEL

arch/loongarch/Kconfig.debug

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ config UNWINDER_PROLOGUE
2626
Some of the addresses it reports may be incorrect (but better than the
2727
Guess unwinder).
2828

29+
config UNWINDER_ORC
30+
bool "ORC unwinder"
31+
select OBJTOOL
32+
help
33+
This option enables the ORC (Oops Rewind Capability) unwinder for
34+
unwinding kernel stack traces. It uses a custom data format which is
35+
a simplified version of the DWARF Call Frame Information standard.
36+
37+
Enabling this option will increase the kernel's runtime memory usage
38+
by roughly 2-4MB, depending on your kernel config.
39+
2940
endchoice

arch/loongarch/Makefile

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ endif
2525
32bit-emul = elf32loongarch
2626
64bit-emul = elf64loongarch
2727

28+
ifdef CONFIG_UNWINDER_ORC
29+
orc_hash_h := arch/$(SRCARCH)/include/generated/asm/orc_hash.h
30+
orc_hash_sh := $(srctree)/scripts/orc_hash.sh
31+
targets += $(orc_hash_h)
32+
quiet_cmd_orc_hash = GEN $@
33+
cmd_orc_hash = mkdir -p $(dir $@); \
34+
$(CONFIG_SHELL) $(orc_hash_sh) < $< > $@
35+
$(orc_hash_h): $(srctree)/arch/loongarch/include/asm/orc_types.h $(orc_hash_sh) FORCE
36+
$(call if_changed,orc_hash)
37+
archprepare: $(orc_hash_h)
38+
endif
39+
2840
ifdef CONFIG_DYNAMIC_FTRACE
2941
KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY
3042
CC_FLAGS_FTRACE := -fpatchable-function-entry=2
@@ -68,8 +80,6 @@ LDFLAGS_vmlinux += -static -n -nostdlib
6880
ifdef CONFIG_AS_HAS_EXPLICIT_RELOCS
6981
cflags-y += $(call cc-option,-mexplicit-relocs)
7082
KBUILD_CFLAGS_KERNEL += $(call cc-option,-mdirect-extern-access)
71-
KBUILD_AFLAGS_MODULE += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
72-
KBUILD_CFLAGS_MODULE += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
7383
else
7484
cflags-y += $(call cc-option,-mno-explicit-relocs)
7585
KBUILD_AFLAGS_KERNEL += -Wa,-mla-global-with-pcrel
@@ -78,6 +88,15 @@ KBUILD_AFLAGS_MODULE += -Wa,-mla-global-with-abs
7888
KBUILD_CFLAGS_MODULE += -fplt -Wa,-mla-global-with-abs,-mla-local-with-abs
7989
endif
8090

91+
KBUILD_AFLAGS += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
92+
KBUILD_CFLAGS += $(call cc-option,-mno-relax) $(call cc-option,-Wa$(comma)-mno-relax)
93+
KBUILD_AFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub)
94+
KBUILD_CFLAGS += $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa$(comma)-mthin-add-sub)
95+
96+
ifdef CONFIG_OBJTOOL
97+
KBUILD_CFLAGS += -fno-jump-tables
98+
endif
99+
81100
ifeq ($(CONFIG_RELOCATABLE),y)
82101
KBUILD_CFLAGS_KERNEL += -fPIE
83102
LDFLAGS_vmlinux += -static -pie --no-dynamic-linker -z notext $(call ld-option, --apply-dynamic-relocs)

arch/loongarch/include/asm/Kbuild

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
2+
generated-y += orc_hash.h
3+
24
generic-y += dma-contiguous.h
35
generic-y += mcs_spinlock.h
46
generic-y += parport.h

arch/loongarch/include/asm/bug.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
do { \
4545
instrumentation_begin(); \
4646
__BUG_FLAGS(BUGFLAG_WARNING|(flags)); \
47+
annotate_reachable(); \
4748
instrumentation_end(); \
4849
} while (0)
4950

arch/loongarch/include/asm/exception.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <asm/ptrace.h>
77
#include <linux/kprobes.h>
88

9+
extern void *exception_table[];
10+
911
void show_registers(struct pt_regs *regs);
1012

1113
asmlinkage void cache_parity_error(void);

arch/loongarch/include/asm/ftrace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ftrace_regs_get_instruction_pointer(struct ftrace_regs *fregs)
6363
static __always_inline void
6464
ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, unsigned long ip)
6565
{
66-
regs_set_return_value(&fregs->regs, ip);
66+
instruction_pointer_set(&fregs->regs, ip);
6767
}
6868

6969
#define ftrace_regs_get_argument(fregs, n) \

arch/loongarch/include/asm/module.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define _ASM_MODULE_H
77

88
#include <asm/inst.h>
9+
#include <asm/orc_types.h>
910
#include <asm-generic/module.h>
1011

1112
#define RELA_STACK_DEPTH 16
@@ -21,6 +22,12 @@ struct mod_arch_specific {
2122
struct mod_section plt;
2223
struct mod_section plt_idx;
2324

25+
#ifdef CONFIG_UNWINDER_ORC
26+
unsigned int num_orcs;
27+
int *orc_unwind_ip;
28+
struct orc_entry *orc_unwind;
29+
#endif
30+
2431
/* For CONFIG_DYNAMIC_FTRACE */
2532
struct plt_entry *ftrace_trampolines;
2633
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
3+
#ifndef _ORC_HEADER_H
4+
#define _ORC_HEADER_H
5+
6+
#include <linux/types.h>
7+
#include <linux/compiler.h>
8+
#include <asm/orc_hash.h>
9+
10+
/*
11+
* The header is currently a 20-byte hash of the ORC entry definition; see
12+
* scripts/orc_hash.sh.
13+
*/
14+
#define ORC_HEADER \
15+
__used __section(".orc_header") __aligned(4) \
16+
static const u8 orc_header[] = { ORC_HASH }
17+
18+
#endif /* _ORC_HEADER_H */
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
#ifndef _ORC_LOOKUP_H
3+
#define _ORC_LOOKUP_H
4+
5+
/*
6+
* This is a lookup table for speeding up access to the .orc_unwind table.
7+
* Given an input address offset, the corresponding lookup table entry
8+
* specifies a subset of the .orc_unwind table to search.
9+
*
10+
* Each block represents the end of the previous range and the start of the
11+
* next range. An extra block is added to give the last range an end.
12+
*
13+
* The block size should be a power of 2 to avoid a costly 'div' instruction.
14+
*
15+
* A block size of 256 was chosen because it roughly doubles unwinder
16+
* performance while only adding ~5% to the ORC data footprint.
17+
*/
18+
#define LOOKUP_BLOCK_ORDER 8
19+
#define LOOKUP_BLOCK_SIZE (1 << LOOKUP_BLOCK_ORDER)
20+
21+
#ifndef LINKER_SCRIPT
22+
23+
extern unsigned int orc_lookup[];
24+
extern unsigned int orc_lookup_end[];
25+
26+
#define LOOKUP_START_IP (unsigned long)_stext
27+
#define LOOKUP_STOP_IP (unsigned long)_etext
28+
29+
#endif /* LINKER_SCRIPT */
30+
31+
#endif /* _ORC_LOOKUP_H */

0 commit comments

Comments
 (0)