Skip to content

Commit 4e321b7

Browse files
committed
Merge tag 'noinstr-lds-2020-05-19' into core/kprobes
Get the noinstr section and markers to base the kprobe changes on.
2 parents d85eaa9 + 6553896 commit 4e321b7

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
@@ -90,6 +90,7 @@ SECTIONS
9090
#ifdef CONFIG_PPC64
9191
*(.tramp.ftrace.text);
9292
#endif
93+
NOINSTR_TEXT
9394
SCHED_TEXT
9495
CPUIDLE_TEXT
9596
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
@@ -540,6 +540,15 @@
540540
. = ALIGN((align)); \
541541
__end_rodata = .;
542542

543+
/*
544+
* Non-instrumentable text section
545+
*/
546+
#define NOINSTR_TEXT \
547+
ALIGN_FUNCTION(); \
548+
__noinstr_text_start = .; \
549+
*(.noinstr.text) \
550+
__noinstr_text_end = .;
551+
543552
/*
544553
* .text section. Map to function alignment to avoid address changes
545554
* during second ld run in second ld pass when generating System.map
@@ -551,6 +560,7 @@
551560
#define TEXT_TEXT \
552561
ALIGN_FUNCTION(); \
553562
*(.text.hot TEXT_MAIN .text.fixup .text.unlikely) \
563+
NOINSTR_TEXT \
554564
*(.text..refcount) \
555565
*(.ref.text) \
556566
MEM_KEEP(init.text*) \

include/linux/compiler.h

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

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

177+
#ifndef instrumentation_begin
178+
#define instrumentation_begin() do { } while(0)
179+
#define instrumentation_end() do { } while(0)
180+
#endif
181+
129182
#ifndef ASM_UNREACHABLE
130183
# define ASM_UNREACHABLE
131184
#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
@@ -948,7 +948,7 @@ static void check_section(const char *modname, struct elf_info *elf,
948948

949949
#define DATA_SECTIONS ".data", ".data.rel"
950950
#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
951-
".kprobes.text", ".cpuidle.text"
951+
".kprobes.text", ".cpuidle.text", ".noinstr.text"
952952
#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
953953
".fixup", ".entry.text", ".exception.text", ".text.*", \
954954
".coldtext"

0 commit comments

Comments
 (0)