Skip to content

Commit a971cfb

Browse files
KAGA-KOKOksacilotto
authored andcommitted
vmlinux.lds.h: Create section for protection against instrumentation
BugLink: https://bugs.launchpad.net/bugs/1918167 [ Upstream commit 6553896 ] Some code pathes, especially the low level entry code, must be protected against instrumentation for various reasons: - Low level entry code can be a fragile beast, especially on x86. - With NO_HZ_FULL RCU state needs to be established before using it. Having a dedicated section for such code allows to validate with tooling that no unsafe functions are invoked. Add the .noinstr.text section and the noinstr attribute to mark functions. noinstr implies notrace. Kprobes will gain a section check later. Provide also a set of markers: instrumentation_begin()/end() These are used to mark code inside a noinstr function which calls into regular instrumentable text section as safe. The instrumentation markers are only active when CONFIG_DEBUG_ENTRY is enabled as the end marker emits a NOP to prevent the compiler from merging the annotation points. This means the objtool verification requires a kernel compiled with this option. Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Alexandre Chartre <[email protected]> Acked-by: Peter Zijlstra <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Kamal Mostafa <[email protected]> Signed-off-by: Kelsey Skunberg <[email protected]>
1 parent d92da7d commit a971cfb

File tree

6 files changed

+72
-1
lines changed

6 files changed

+72
-1
lines changed

arch/powerpc/kernel/vmlinux.lds.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ SECTIONS
102102
#ifdef CONFIG_PPC64
103103
*(.tramp.ftrace.text);
104104
#endif
105+
NOINSTR_TEXT
105106
SCHED_TEXT
106107
CPUIDLE_TEXT
107108
LOCK_TEXT

include/asm-generic/sections.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ extern char __ctors_start[], __ctors_end[];
5353
/* Start and end of .opd section - used for function descriptors. */
5454
extern char __start_opd[], __end_opd[];
5555

56+
/* Start and end of instrumentation protected text section */
57+
extern char __noinstr_text_start[], __noinstr_text_end[];
58+
5659
extern __visible const void __nosave_begin, __nosave_end;
5760

5861
/* Function descriptor handling (if any). Override in asm/sections.h */

include/asm-generic/vmlinux.lds.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,15 @@
510510
#define RODATA RO_DATA_SECTION(4096)
511511
#define RO_DATA(align) RO_DATA_SECTION(align)
512512

513+
/*
514+
* Non-instrumentable text section
515+
*/
516+
#define NOINSTR_TEXT \
517+
ALIGN_FUNCTION(); \
518+
__noinstr_text_start = .; \
519+
*(.noinstr.text) \
520+
__noinstr_text_end = .;
521+
513522
/*
514523
* .text section. Map to function alignment to avoid address changes
515524
* during second ld run in second ld pass when generating System.map
@@ -524,6 +533,7 @@
524533
*(TEXT_MAIN .text.fixup) \
525534
*(.text.unlikely .text.unlikely.*) \
526535
*(.text.unknown .text.unknown.*) \
536+
NOINSTR_TEXT \
527537
*(.text..refcount) \
528538
*(.ref.text) \
529539
MEM_KEEP(init.text*) \

include/linux/compiler.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,65 @@ void ftrace_likely_update(struct ftrace_likely_data *f, int val,
134134
/* Annotate a C jump table to allow objtool to follow the code flow */
135135
#define __annotate_jump_table __section(.rodata..c_jump_table)
136136

137+
#ifdef CONFIG_DEBUG_ENTRY
138+
/* Begin/end of an instrumentation safe region */
139+
#define instrumentation_begin() ({ \
140+
asm volatile("%c0:\n\t" \
141+
".pushsection .discard.instr_begin\n\t" \
142+
".long %c0b - .\n\t" \
143+
".popsection\n\t" : : "i" (__COUNTER__)); \
144+
})
145+
146+
/*
147+
* Because instrumentation_{begin,end}() can nest, objtool validation considers
148+
* _begin() a +1 and _end() a -1 and computes a sum over the instructions.
149+
* When the value is greater than 0, we consider instrumentation allowed.
150+
*
151+
* There is a problem with code like:
152+
*
153+
* noinstr void foo()
154+
* {
155+
* instrumentation_begin();
156+
* ...
157+
* if (cond) {
158+
* instrumentation_begin();
159+
* ...
160+
* instrumentation_end();
161+
* }
162+
* bar();
163+
* instrumentation_end();
164+
* }
165+
*
166+
* If instrumentation_end() would be an empty label, like all the other
167+
* annotations, the inner _end(), which is at the end of a conditional block,
168+
* would land on the instruction after the block.
169+
*
170+
* If we then consider the sum of the !cond path, we'll see that the call to
171+
* bar() is with a 0-value, even though, we meant it to happen with a positive
172+
* value.
173+
*
174+
* To avoid this, have _end() be a NOP instruction, this ensures it will be
175+
* part of the condition block and does not escape.
176+
*/
177+
#define instrumentation_end() ({ \
178+
asm volatile("%c0: nop\n\t" \
179+
".pushsection .discard.instr_end\n\t" \
180+
".long %c0b - .\n\t" \
181+
".popsection\n\t" : : "i" (__COUNTER__)); \
182+
})
183+
#endif /* CONFIG_DEBUG_ENTRY */
184+
137185
#else
138186
#define annotate_reachable()
139187
#define annotate_unreachable()
140188
#define __annotate_jump_table
141189
#endif
142190

191+
#ifndef instrumentation_begin
192+
#define instrumentation_begin() do { } while(0)
193+
#define instrumentation_end() do { } while(0)
194+
#endif
195+
143196
#ifndef ASM_UNREACHABLE
144197
# define ASM_UNREACHABLE
145198
#endif

include/linux/compiler_types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ struct ftrace_likely_data {
118118
#define notrace __attribute__((__no_instrument_function__))
119119
#endif
120120

121+
/* Section for code which can't be instrumented at all */
122+
#define noinstr \
123+
noinline notrace __attribute((__section__(".noinstr.text")))
124+
121125
/*
122126
* it doesn't make sense on ARM (currently the only user of __naked)
123127
* to trace naked functions because then mcount is called without

scripts/mod/modpost.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -960,7 +960,7 @@ static void check_section(const char *modname, struct elf_info *elf,
960960

961961
#define DATA_SECTIONS ".data", ".data.rel"
962962
#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
963-
".kprobes.text", ".cpuidle.text"
963+
".kprobes.text", ".cpuidle.text", ".noinstr.text"
964964
#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
965965
".fixup", ".entry.text", ".exception.text", ".text.*", \
966966
".coldtext"

0 commit comments

Comments
 (0)