Skip to contentSkip to conversation

kprobes related bugfix

PATCH In review Masami Hiramatsu 3 participants 14 messages Aug 3, 2017 – Aug 18, 2017

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v9 cover kprobes related bugfix

Hi,

Here are the 9th version of the series to fix kprobes not to optimize on the functions in irqentry text ([5/5]). To check the irqentry text always, [4/5] makes irqentry text section unconditional.

This involves some other cleanups on h8300, xtensa, and cris ports, because those sources declare section start/end symbol as single characters, but generic sections.h declares it as character arrays. So it leads build errors if we apply [4/5].

Changes in v9
- Add cris cleanup patch (3/5).

BTW, in this series I just give a minimum fix for arch dependent code, since this is a series to fix a bug in x86 optprobe. However, IMHO, we should revisit these "local _stext/_etext references" and replace it with just including asm/sections.h. And also, I would like to expose is_kernel_text() as a generic routine for arch dependent code so that they can use it.

Thank you,

---

Masami Hiramatsu (5):
h8300: mark _stext and _etext as char-arrays, not single char
xtensa: mark _stext and _end as char-arrays, not single char
cris: mark _stext and _end as char-arrays, not single char
irq: Make irqentry text section unconditional
[BUGFIX] kprobes/x86: Do not jump-optimize kprobes on irq entry code

 arch/arm/include/asm/traps.h         |    7 -------
 arch/arm64/include/asm/traps.h       |    7 -------
 arch/cris/arch-v32/mach-a3/arbiter.c |    4 ++--
 arch/cris/arch-v32/mach-fs/arbiter.c |    4 ++--
 arch/cris/kernel/traps.c             |    6 +++---
 arch/h8300/include/asm/traps.h       |    6 +++---
 arch/x86/entry/entry_64.S            |    9 ++-------
 arch/x86/kernel/kprobes/opt.c        |    9 ++++++---
 arch/x86/kernel/unwind_frame.c       |    2 --
 arch/xtensa/kernel/setup.c           |    6 +++---
 include/asm-generic/sections.h       |    4 ++++
 include/asm-generic/vmlinux.lds.h    |    8 --------
 include/linux/interrupt.h            |   14 +-------------
 13 files changed, 26 insertions(+), 60 deletions(-)
#1 lore raw

PATCH v9 1/5 h8300: mark _stext and _etext as char-arrays, not single char

Mark _stext and _etext as character arrays instead of single character, as same as include/asm-generic/sections.h does.

---
 arch/h8300/include/asm/traps.h |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/h8300/include/asm/traps.h +3 -3
index 15e701130b27..1c5a30ec2df8 100644
@@ -33,9 +33,9 @@ extern unsigned long *_interrupt_redirect_table;
 #define TRAP2_VEC 10
 #define TRAP3_VEC 11
 
-extern char _start, _etext;
+extern char _start[], _etext[];
 #define check_kernel_text(addr) \
-	((addr >= (unsigned long)(&_start)) && \
-	 (addr <  (unsigned long)(&_etext)) && !(addr & 1))
+	((addr >= (unsigned long)(_start)) && \
+	 (addr <  (unsigned long)(_etext)) && !(addr & 1))
 
 #endif /* _H8300_TRAPS_H */
#3 lore raw

PATCH v9 2/5 xtensa: mark _stext and _end as char-arrays, not single char

In reply to Masami Hiramatsu

Mark _stext and _end as character arrays instead of single character, as same as include/asm-generic/sections.h does.

---
 arch/xtensa/kernel/setup.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/xtensa/kernel/setup.c +3 -3
index 33bfa5270d95..08175df7a69e 100644
@@ -273,8 +273,8 @@ void __init init_arch(bp_tag_t *bp_start)
  * Initialize system. Setup memory and reserve regions.
  */
 
-extern char _end;
-extern char _stext;
+extern char _end[];
+extern char _stext[];
 extern char _WindowVectors_text_start;
 extern char _WindowVectors_text_end;
 extern char _DebugInterruptVector_literal_start;
@@ -333,7 +333,7 @@ void __init setup_arch(char **cmdline_p)
 	}
 #endif
 
-	mem_reserve(__pa(&_stext), __pa(&_end));
+	mem_reserve(__pa(_stext), __pa(_end));
 
 #ifdef CONFIG_VECTORS_OFFSET
 	mem_reserve(__pa(&_WindowVectors_text_start),
#5 lore raw

PATCH v9 3/5 cris: mark _stext and _end as char-arrays, not single char

In reply to Masami Hiramatsu

Mark _stext and _end as character arrays instead of single character, as same as include/asm-generic/sections.h does.

---
 arch/cris/arch-v32/mach-a3/arbiter.c |    4 ++--
 arch/cris/arch-v32/mach-fs/arbiter.c |    4 ++--
 arch/cris/kernel/traps.c             |    6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/cris/arch-v32/mach-a3/arbiter.c +2 -2
index ab5c421a4de8..735a9b0abdb8 100644
@@ -227,7 +227,7 @@ static void crisv32_arbiter_config(int arbiter, int region, int unused_slots)
 	}
 }
 
-extern char _stext, _etext;
+extern char _stext[], _etext[];
 
 static void crisv32_arbiter_init(void)
 {
@@ -265,7 +265,7 @@ static void crisv32_arbiter_init(void)
 
 #ifndef CONFIG_ETRAX_KGDB
 	/* Global watch for writes to kernel text segment. */
-	crisv32_arbiter_watch(virt_to_phys(&_stext), &_etext - &_stext,
+	crisv32_arbiter_watch(virt_to_phys(_stext), _etext - _stext,
 		MARB_CLIENTS(arbiter_all_clients, arbiter_bar_all_clients),
 			      arbiter_all_write, NULL);
 #endif
arch/cris/arch-v32/mach-fs/arbiter.c +2 -2
index c97f4d8120f9..047c70bdbb23 100644
@@ -158,7 +158,7 @@ static void crisv32_arbiter_config(int region, int unused_slots)
 	}
 }
 
-extern char _stext, _etext;
+extern char _stext[], _etext[];
 
 static void crisv32_arbiter_init(void)
 {
@@ -190,7 +190,7 @@ static void crisv32_arbiter_init(void)
 
 #ifndef CONFIG_ETRAX_KGDB
 	/* Global watch for writes to kernel text segment. */
-	crisv32_arbiter_watch(virt_to_phys(&_stext), &_etext - &_stext,
+	crisv32_arbiter_watch(virt_to_phys(_stext), _etext - _stext,
 			      arbiter_all_clients, arbiter_all_write, NULL);
 #endif
 }
arch/cris/kernel/traps.c +3 -3
index a01636a12a6e..d98131c45bb5 100644
@@ -42,7 +42,7 @@ void (*nmi_handler)(struct pt_regs *);
 void show_trace(unsigned long *stack)
 {
 	unsigned long addr, module_start, module_end;
-	extern char _stext, _etext;
+	extern char _stext[], _etext[];
 	int i;
 
 	pr_err("\nCall Trace: ");
@@ -69,8 +69,8 @@ void show_trace(unsigned long *stack)
 		 * down the cause of the crash will be able to figure
 		 * out the call path that was taken.
 		 */
-		if (((addr >= (unsigned long)&_stext) &&
-		     (addr <= (unsigned long)&_etext)) ||
+		if (((addr >= (unsigned long)_stext) &&
+		     (addr <= (unsigned long)_etext)) ||
 		    ((addr >= module_start) && (addr <= module_end))) {
 #ifdef CONFIG_KALLSYMS
 			print_ip_sym(addr);
#10 lore raw

PATCH v9 4/5 irq: Make irqentry text section unconditional

In reply to Masami Hiramatsu

Generate irqentry and softirqentry text sections without any configs. This will add above extra sections, but no performace impact.

---
 arch/arm/include/asm/traps.h      |    7 -------
 arch/arm64/include/asm/traps.h    |    7 -------
 arch/x86/entry/entry_64.S         |    9 ++-------
 arch/x86/kernel/unwind_frame.c    |    2 --
 include/asm-generic/sections.h    |    4 ++++
 include/asm-generic/vmlinux.lds.h |    8 --------
 include/linux/interrupt.h         |   14 +-------------
 7 files changed, 7 insertions(+), 44 deletions(-)
arch/arm/include/asm/traps.h +0 -7
index f555bb3664dc..683d9230984a 100644
@@ -18,7 +18,6 @@ struct undef_hook {
 void register_undef_hook(struct undef_hook *hook);
 void unregister_undef_hook(struct undef_hook *hook);
 
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
 static inline int __in_irqentry_text(unsigned long ptr)
 {
 	extern char __irqentry_text_start[];
@@ -27,12 +26,6 @@ static inline int __in_irqentry_text(unsigned long ptr)
 	return ptr >= (unsigned long)&__irqentry_text_start &&
 	       ptr < (unsigned long)&__irqentry_text_end;
 }
-#else
-static inline int __in_irqentry_text(unsigned long ptr)
-{
-	return 0;
-}
-#endif
 
 static inline int in_exception_text(unsigned long ptr)
 {
arch/arm64/include/asm/traps.h +0 -7
index 02e9035b0685..47a9066f7c86 100644
@@ -37,18 +37,11 @@ void unregister_undef_hook(struct undef_hook *hook);
 
 void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr);
 
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
 static inline int __in_irqentry_text(unsigned long ptr)
 {
 	return ptr >= (unsigned long)&__irqentry_text_start &&
 	       ptr < (unsigned long)&__irqentry_text_end;
 }
-#else
-static inline int __in_irqentry_text(unsigned long ptr)
-{
-	return 0;
-}
-#endif
 
 static inline int in_exception_text(unsigned long ptr)
 {
arch/x86/entry/entry_64.S +2 -7
index aa58155187c5..5e41396efbbf 100644
@@ -766,13 +766,8 @@ apicinterrupt3 \num trace(\sym) smp_trace(\sym)
 #endif
 
 /* Make sure APIC interrupt handlers end up in the irqentry section: */
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
-# define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
-# define POP_SECTION_IRQENTRY	.popsection
-#else
-# define PUSH_SECTION_IRQENTRY
-# define POP_SECTION_IRQENTRY
-#endif
+#define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
+#define POP_SECTION_IRQENTRY	.popsection
 
 .macro apicinterrupt num sym do_sym
 PUSH_SECTION_IRQENTRY
arch/x86/kernel/unwind_frame.c +0 -2
index 7574ef5f16ec..d145a0b1f529 100644
@@ -84,10 +84,8 @@ static bool in_entry_code(unsigned long ip)
 	if (addr >= __entry_text_start && addr < __entry_text_end)
 		return true;
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 	if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
 		return true;
-#endif
 
 	return false;
 }
include/asm-generic/sections.h +4 -0
index 532372c6cf15..e5da44eddd2f 100644
@@ -27,6 +27,8 @@
  *	__kprobes_text_start, __kprobes_text_end
  *	__entry_text_start, __entry_text_end
  *	__ctors_start, __ctors_end
+ *	__irqentry_text_start, __irqentry_text_end
+ *	__softirqentry_text_start, __softirqentry_text_end
  */
 extern char _text[], _stext[], _etext[];
 extern char _data[], _sdata[], _edata[];
@@ -39,6 +41,8 @@ extern char __per_cpu_load[], __per_cpu_start[], __per_cpu_end[];
 extern char __kprobes_text_start[], __kprobes_text_end[];
 extern char __entry_text_start[], __entry_text_end[];
 extern char __start_rodata[], __end_rodata[];
+extern char __irqentry_text_start[], __irqentry_text_end[];
+extern char __softirqentry_text_start[], __softirqentry_text_end[];
 
 /* Start and end of .ctors section - used for constructor calls. */
 extern char __ctors_start[], __ctors_end[];
include/asm-generic/vmlinux.lds.h +0 -8
index fffc9bdae025..438b014c192f 100644
@@ -483,25 +483,17 @@
 		*(.entry.text)						\
 		VMLINUX_SYMBOL(__entry_text_end) = .;
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 #define IRQENTRY_TEXT							\
 		ALIGN_FUNCTION();					\
 		VMLINUX_SYMBOL(__irqentry_text_start) = .;		\
 		*(.irqentry.text)					\
 		VMLINUX_SYMBOL(__irqentry_text_end) = .;
-#else
-#define IRQENTRY_TEXT
-#endif
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 #define SOFTIRQENTRY_TEXT						\
 		ALIGN_FUNCTION();					\
 		VMLINUX_SYMBOL(__softirqentry_text_start) = .;		\
 		*(.softirqentry.text)					\
 		VMLINUX_SYMBOL(__softirqentry_text_end) = .;
-#else
-#define SOFTIRQENTRY_TEXT
-#endif
 
 /* Section used for early init (in .S files) */
 #define HEAD_TEXT  *(.head.text)
include/linux/interrupt.h +1 -13
index a2fddddb0d60..59ba11661b6e 100644
@@ -18,6 +18,7 @@
 #include <linux/atomic.h>
 #include <asm/ptrace.h>
 #include <asm/irq.h>
+#include <asm/sections.h>
 
 /*
  * These correspond to the IORESOURCE_IRQ_* defines in
@@ -726,7 +727,6 @@ extern int early_irq_init(void);
 extern int arch_probe_nr_irqs(void);
 extern int arch_early_irq_init(void);
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 /*
  * We want to know which function is an entrypoint of a hardirq or a softirq.
  */
@@ -734,16 +734,4 @@ extern int arch_early_irq_init(void);
 #define __softirq_entry  \
 	__attribute__((__section__(".softirqentry.text")))
 
-/* Limits of hardirq entrypoints */
-extern char __irqentry_text_start[];
-extern char __irqentry_text_end[];
-/* Limits of softirq entrypoints */
-extern char __softirqentry_text_start[];
-extern char __softirqentry_text_end[];
-
-#else
-#define __irq_entry
-#define __softirq_entry
-#endif
-
 #endif
#12 lore raw

PATCH v9 5/5 kprobes/x86: Do not jump-optimize kprobes on irq entry code

In reply to Masami Hiramatsu

Since the kernel segment registers are not prepared at the entry of irq-entry code, if a kprobe on such code is jump-optimized, accessing per-cpu variables may cause kernel panic.
However, if the kprobe is not optimized, it kicks int3 exception and set segment registers correctly.

This checks probe-address and if it is in irq-entry code, it prohibits optimizing such kprobes. This means we can continuously probing such interrupt handlers by kprobes but it is not optimized anymore.

---
 arch/x86/kernel/kprobes/opt.c |    9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kernel/kprobes/opt.c +6 -3
index 69ea0bc1cfa3..4f98aad38237 100644
@@ -39,6 +39,7 @@
 #include <asm/insn.h>
 #include <asm/debugreg.h>
 #include <asm/set_memory.h>
+#include <asm/sections.h>
 
 #include "common.h"
 
@@ -251,10 +252,12 @@ static int can_optimize(unsigned long paddr)
 
 	/*
 	 * Do not optimize in the entry code due to the unstable
-	 * stack handling.
+	 * stack handling and registers setup.
 	 */
-	if ((paddr >= (unsigned long)__entry_text_start) &&
-	    (paddr <  (unsigned long)__entry_text_end))
+	if (((paddr >= (unsigned long)__entry_text_start) &&
+	     (paddr <  (unsigned long)__entry_text_end)) ||
+	    ((paddr >= (unsigned long)__irqentry_text_start) &&
+	     (paddr <  (unsigned long)__irqentry_text_end)))
 		return 0;
 
 	/* Check there is enough space for a relative jump. */
#4 lore raw

[tip:perf/core] xtensa: Mark _stext and _end as char-arrays, not single char variables

In reply to Masami Hiramatsu
xtensa: Mark _stext and _end as char-arrays, not single char variables
Commit-ID:  1824436262b2f43a46051a4958e2dd58a9d9aadf
Gitweb:     http://git.kernel.org/tip/1824436262b2f43a46051a4958e2dd58a9d9aadf
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 3 Aug 2017 11:36:09 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 10 Aug 2017 16:28:52 +0200

xtensa: Mark _stext and _end as char-arrays, not single char variables

Mark _stext and _end as character arrays instead of single character variables, like include/asm-generic/sections.h does.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Anil S Keshavamurthy <[email protected]>
Cc Chris Zankel <[email protected]>
Cc David S . Miller <[email protected]>
Cc Francis Deslauriers <[email protected]>
Cc Jesper Nilsson <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Max Filippov <[email protected]>
Cc Mikael Starvik <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Yoshinori Sato <[email protected]>
---
 arch/xtensa/kernel/setup.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/xtensa/kernel/setup.c +3 -3
index 33bfa52..08175df 100644
@@ -273,8 +273,8 @@ void __init init_arch(bp_tag_t *bp_start)
  * Initialize system. Setup memory and reserve regions.
  */
 
-extern char _end;
-extern char _stext;
+extern char _end[];
+extern char _stext[];
 extern char _WindowVectors_text_start;
 extern char _WindowVectors_text_end;
 extern char _DebugInterruptVector_literal_start;
@@ -333,7 +333,7 @@ void __init setup_arch(char **cmdline_p)
 	}
 #endif
 
-	mem_reserve(__pa(&_stext), __pa(&_end));
+	mem_reserve(__pa(_stext), __pa(_end));
 
 #ifdef CONFIG_VECTORS_OFFSET
 	mem_reserve(__pa(&_WindowVectors_text_start),
#2 lore raw

[tip:perf/core] h8300: Mark _stext and _etext as char-arrays, not single char variables

In reply to Masami Hiramatsu
h8300: Mark _stext and _etext as char-arrays, not single char variables
Commit-ID:  b4464bf977004832f63f31c015751c049bc47dde
Gitweb:     http://git.kernel.org/tip/b4464bf977004832f63f31c015751c049bc47dde
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 3 Aug 2017 11:35:04 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 10 Aug 2017 16:28:52 +0200

h8300: Mark _stext and _etext as char-arrays, not single char variables

Mark _stext and _etext as character arrays instead of single character variables, like include/asm-generic/sections.h does.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Anil S Keshavamurthy <[email protected]>
Cc Chris Zankel <[email protected]>
Cc David S . Miller <[email protected]>
Cc Francis Deslauriers <[email protected]>
Cc Jesper Nilsson <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Max Filippov <[email protected]>
Cc Mikael Starvik <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Yoshinori Sato <[email protected]>
---
 arch/h8300/include/asm/traps.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/h8300/include/asm/traps.h +3 -3
index 15e70113..1c5a30e 100644
@@ -33,9 +33,9 @@ extern unsigned long *_interrupt_redirect_table;
 #define TRAP2_VEC 10
 #define TRAP3_VEC 11
 
-extern char _start, _etext;
+extern char _start[], _etext[];
 #define check_kernel_text(addr) \
-	((addr >= (unsigned long)(&_start)) && \
-	 (addr <  (unsigned long)(&_etext)) && !(addr & 1))
+	((addr >= (unsigned long)(_start)) && \
+	 (addr <  (unsigned long)(_etext)) && !(addr & 1))
 
 #endif /* _H8300_TRAPS_H */
#6 lore raw

[tip:perf/core] cris: Mark _stext and _end as char-arrays, not single char variables

In reply to Masami Hiramatsu
cris: Mark _stext and _end as char-arrays, not single char variables
Commit-ID:  c2579fee22483b0f156099abd9996d900634562c
Gitweb:     http://git.kernel.org/tip/c2579fee22483b0f156099abd9996d900634562c
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 3 Aug 2017 11:37:15 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 10 Aug 2017 16:28:53 +0200

cris: Mark _stext and _end as char-arrays, not single char variables

Mark _stext and _end as character arrays instead of single character variable, like include/asm-generic/sections.h does.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Anil S Keshavamurthy <[email protected]>
Cc Chris Zankel <[email protected]>
Cc David S . Miller <[email protected]>
Cc Francis Deslauriers <[email protected]>
Cc Jesper Nilsson <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Max Filippov <[email protected]>
Cc Mikael Starvik <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Yoshinori Sato <[email protected]>
---
 arch/cris/arch-v32/mach-a3/arbiter.c | 4 ++--
 arch/cris/arch-v32/mach-fs/arbiter.c | 4 ++--
 arch/cris/kernel/traps.c             | 6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/cris/arch-v32/mach-a3/arbiter.c +2 -2
index ab5c421..735a9b0 100644
@@ -227,7 +227,7 @@ static void crisv32_arbiter_config(int arbiter, int region, int unused_slots)
 	}
 }
 
-extern char _stext, _etext;
+extern char _stext[], _etext[];
 
 static void crisv32_arbiter_init(void)
 {
@@ -265,7 +265,7 @@ static void crisv32_arbiter_init(void)
 
 #ifndef CONFIG_ETRAX_KGDB
 	/* Global watch for writes to kernel text segment. */
-	crisv32_arbiter_watch(virt_to_phys(&_stext), &_etext - &_stext,
+	crisv32_arbiter_watch(virt_to_phys(_stext), _etext - _stext,
 		MARB_CLIENTS(arbiter_all_clients, arbiter_bar_all_clients),
 			      arbiter_all_write, NULL);
 #endif
arch/cris/arch-v32/mach-fs/arbiter.c +2 -2
index c97f4d8..047c70b 100644
@@ -158,7 +158,7 @@ static void crisv32_arbiter_config(int region, int unused_slots)
 	}
 }
 
-extern char _stext, _etext;
+extern char _stext[], _etext[];
 
 static void crisv32_arbiter_init(void)
 {
@@ -190,7 +190,7 @@ static void crisv32_arbiter_init(void)
 
 #ifndef CONFIG_ETRAX_KGDB
 	/* Global watch for writes to kernel text segment. */
-	crisv32_arbiter_watch(virt_to_phys(&_stext), &_etext - &_stext,
+	crisv32_arbiter_watch(virt_to_phys(_stext), _etext - _stext,
 			      arbiter_all_clients, arbiter_all_write, NULL);
 #endif
 }
arch/cris/kernel/traps.c +3 -3
index a01636a..d98131c 100644
@@ -42,7 +42,7 @@ void (*nmi_handler)(struct pt_regs *);
 void show_trace(unsigned long *stack)
 {
 	unsigned long addr, module_start, module_end;
-	extern char _stext, _etext;
+	extern char _stext[], _etext[];
 	int i;
 
 	pr_err("\nCall Trace: ");
@@ -69,8 +69,8 @@ void show_trace(unsigned long *stack)
 		 * down the cause of the crash will be able to figure
 		 * out the call path that was taken.
 		 */
-		if (((addr >= (unsigned long)&_stext) &&
-		     (addr <= (unsigned long)&_etext)) ||
+		if (((addr >= (unsigned long)_stext) &&
+		     (addr <= (unsigned long)_etext)) ||
 		    ((addr >= module_start) && (addr <= module_end))) {
 #ifdef CONFIG_KALLSYMS
 			print_ip_sym(addr);
#11 lore raw

[tip:perf/core] irq: Make the irqentry text section unconditional

In reply to Masami Hiramatsu
irq: Make the irqentry text section unconditional
Commit-ID:  229a71860547ec856b156179a9c6bef2de426f66
Gitweb:     http://git.kernel.org/tip/229a71860547ec856b156179a9c6bef2de426f66
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 3 Aug 2017 11:38:21 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 10 Aug 2017 16:28:53 +0200

irq: Make the irqentry text section unconditional

Generate irqentry and softirqentry text sections without any Kconfig dependencies. This will add extra sections, but there should be no performace impact.

Cc Ananth N Mavinakayanahalli <[email protected]>
Cc Anil S Keshavamurthy <[email protected]>
Cc Chris Zankel <[email protected]>
Cc David S . Miller <[email protected]>
Cc Francis Deslauriers <[email protected]>
Cc Jesper Nilsson <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Max Filippov <[email protected]>
Cc Mikael Starvik <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Yoshinori Sato <[email protected]>
---
 arch/arm/include/asm/traps.h      |  7 -------
 arch/arm64/include/asm/traps.h    |  7 -------
 arch/x86/entry/entry_64.S         |  9 ++-------
 arch/x86/kernel/unwind_frame.c    |  2 --
 include/asm-generic/sections.h    |  4 ++++
 include/asm-generic/vmlinux.lds.h |  8 --------
 include/linux/interrupt.h         | 14 +-------------
 7 files changed, 7 insertions(+), 44 deletions(-)
arch/arm/include/asm/traps.h +0 -7
index f555bb3..683d923 100644
@@ -18,7 +18,6 @@ struct undef_hook {
 void register_undef_hook(struct undef_hook *hook);
 void unregister_undef_hook(struct undef_hook *hook);
 
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
 static inline int __in_irqentry_text(unsigned long ptr)
 {
 	extern char __irqentry_text_start[];
@@ -27,12 +26,6 @@ static inline int __in_irqentry_text(unsigned long ptr)
 	return ptr >= (unsigned long)&__irqentry_text_start &&
 	       ptr < (unsigned long)&__irqentry_text_end;
 }
-#else
-static inline int __in_irqentry_text(unsigned long ptr)
-{
-	return 0;
-}
-#endif
 
 static inline int in_exception_text(unsigned long ptr)
 {
arch/arm64/include/asm/traps.h +0 -7
index 02e9035..47a9066 100644
@@ -37,18 +37,11 @@ void unregister_undef_hook(struct undef_hook *hook);
 
 void arm64_notify_segfault(struct pt_regs *regs, unsigned long addr);
 
-#ifdef CONFIG_FUNCTION_GRAPH_TRACER
 static inline int __in_irqentry_text(unsigned long ptr)
 {
 	return ptr >= (unsigned long)&__irqentry_text_start &&
 	       ptr < (unsigned long)&__irqentry_text_end;
 }
-#else
-static inline int __in_irqentry_text(unsigned long ptr)
-{
-	return 0;
-}
-#endif
 
 static inline int in_exception_text(unsigned long ptr)
 {
arch/x86/entry/entry_64.S +2 -7
index d271fb7..3e3da29 100644
@@ -675,13 +675,8 @@ apicinterrupt3 \num trace(\sym) smp_trace(\sym)
 #endif
 
 /* Make sure APIC interrupt handlers end up in the irqentry section: */
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
-# define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
-# define POP_SECTION_IRQENTRY	.popsection
-#else
-# define PUSH_SECTION_IRQENTRY
-# define POP_SECTION_IRQENTRY
-#endif
+#define PUSH_SECTION_IRQENTRY	.pushsection .irqentry.text, "ax"
+#define POP_SECTION_IRQENTRY	.popsection
 
 .macro apicinterrupt num sym do_sym
 PUSH_SECTION_IRQENTRY
arch/x86/kernel/unwind_frame.c +0 -2
index b9389d7..c29e5bc 100644
@@ -91,10 +91,8 @@ static bool in_entry_code(unsigned long ip)
 	if (addr >= __entry_text_start && addr < __entry_text_end)
 		return true;
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 	if (addr >= __irqentry_text_start && addr < __irqentry_text_end)
 		return true;
-#endif
 
 	return false;
 }
include/asm-generic/sections.h +4 -0
index 532372c..e5da44e 100644
@@ -27,6 +27,8 @@
  *	__kprobes_text_start, __kprobes_text_end
  *	__entry_text_start, __entry_text_end
  *	__ctors_start, __ctors_end
+ *	__irqentry_text_start, __irqentry_text_end
+ *	__softirqentry_text_start, __softirqentry_text_end
  */
 extern char _text[], _stext[], _etext[];
 extern char _data[], _sdata[], _edata[];
@@ -39,6 +41,8 @@ extern char __per_cpu_load[], __per_cpu_start[], __per_cpu_end[];
 extern char __kprobes_text_start[], __kprobes_text_end[];
 extern char __entry_text_start[], __entry_text_end[];
 extern char __start_rodata[], __end_rodata[];
+extern char __irqentry_text_start[], __irqentry_text_end[];
+extern char __softirqentry_text_start[], __softirqentry_text_end[];
 
 /* Start and end of .ctors section - used for constructor calls. */
 extern char __ctors_start[], __ctors_end[];
include/asm-generic/vmlinux.lds.h +0 -8
index da0be9a..62e2395 100644
@@ -483,25 +483,17 @@
 		*(.entry.text)						\
 		VMLINUX_SYMBOL(__entry_text_end) = .;
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 #define IRQENTRY_TEXT							\
 		ALIGN_FUNCTION();					\
 		VMLINUX_SYMBOL(__irqentry_text_start) = .;		\
 		*(.irqentry.text)					\
 		VMLINUX_SYMBOL(__irqentry_text_end) = .;
-#else
-#define IRQENTRY_TEXT
-#endif
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 #define SOFTIRQENTRY_TEXT						\
 		ALIGN_FUNCTION();					\
 		VMLINUX_SYMBOL(__softirqentry_text_start) = .;		\
 		*(.softirqentry.text)					\
 		VMLINUX_SYMBOL(__softirqentry_text_end) = .;
-#else
-#define SOFTIRQENTRY_TEXT
-#endif
 
 /* Section used for early init (in .S files) */
 #define HEAD_TEXT  *(.head.text)
include/linux/interrupt.h +1 -13
index a2fdddd..59ba116 100644
@@ -18,6 +18,7 @@
 #include <linux/atomic.h>
 #include <asm/ptrace.h>
 #include <asm/irq.h>
+#include <asm/sections.h>
 
 /*
  * These correspond to the IORESOURCE_IRQ_* defines in
@@ -726,7 +727,6 @@ extern int early_irq_init(void);
 extern int arch_probe_nr_irqs(void);
 extern int arch_early_irq_init(void);
 
-#if defined(CONFIG_FUNCTION_GRAPH_TRACER) || defined(CONFIG_KASAN)
 /*
  * We want to know which function is an entrypoint of a hardirq or a softirq.
  */
@@ -734,16 +734,4 @@ extern int arch_early_irq_init(void);
 #define __softirq_entry  \
 	__attribute__((__section__(".softirqentry.text")))
 
-/* Limits of hardirq entrypoints */
-extern char __irqentry_text_start[];
-extern char __irqentry_text_end[];
-/* Limits of softirq entrypoints */
-extern char __softirqentry_text_start[];
-extern char __softirqentry_text_end[];
-
-#else
-#define __irq_entry
-#define __softirq_entry
-#endif
-
 #endif
#13 lore raw

[tip:perf/core] kprobes/x86: Do not jump-optimize kprobes on irq entry code

In reply to Masami Hiramatsu
kprobes/x86: Do not jump-optimize kprobes on irq entry code
Commit-ID:  d9f5f32a7d17f4906a21ad59589853639a1328a0
Gitweb:     http://git.kernel.org/tip/d9f5f32a7d17f4906a21ad59589853639a1328a0
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Thu, 3 Aug 2017 11:39:26 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Thu, 10 Aug 2017 16:28:53 +0200

kprobes/x86: Do not jump-optimize kprobes on irq entry code

Since the kernel segment registers are not prepared at the entry of irq-entry code, if a kprobe on such code is jump-optimized, accessing per-CPU variables may cause a kernel panic.

However, if the kprobe is not optimized, it triggers an int3 exception and sets segment registers correctly.

With this patch we check the probe-address and if it is in the irq-entry code, it prohibits optimizing such kprobes.

This means we can continue probing such interrupt handlers by kprobes but it is not optimized anymore.

---
 arch/x86/kernel/kprobes/opt.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
arch/x86/kernel/kprobes/opt.c +6 -3
index 69ea0bc..4f98aad 100644
@@ -39,6 +39,7 @@
 #include <asm/insn.h>
 #include <asm/debugreg.h>
 #include <asm/set_memory.h>
+#include <asm/sections.h>
 
 #include "common.h"
 
@@ -251,10 +252,12 @@ static int can_optimize(unsigned long paddr)
 
 	/*
 	 * Do not optimize in the entry code due to the unstable
-	 * stack handling.
+	 * stack handling and registers setup.
 	 */
-	if ((paddr >= (unsigned long)__entry_text_start) &&
-	    (paddr <  (unsigned long)__entry_text_end))
+	if (((paddr >= (unsigned long)__entry_text_start) &&
+	     (paddr <  (unsigned long)__entry_text_end)) ||
+	    ((paddr >= (unsigned long)__irqentry_text_start) &&
+	     (paddr <  (unsigned long)__irqentry_text_end)))
 		return 0;
 
 	/* Check there is enough space for a relative jump. */
#7 lore raw

Re: [PATCH -tip v9 3/5] cris: mark _stext and _end as char-arrays, not single char

In reply to Masami Hiramatsu
Quoted 14 lines · On Thu, 3 Aug 2017 11:37:15 +0900 Masami Hiramatsu <[email protected]> wrote:
arch/cris/kernel/traps.c +1 -1
index a01636a12a6e..d98131c45bb5 100644
@@ -42,7 +42,7 @@ void (*nmi_handler)(struct pt_regs *);
 void show_trace(unsigned long *stack)
 {
 	unsigned long addr, module_start, module_end;
-	extern char _stext, _etext;
+	extern char _stext[], _etext[];
 	int i;
 
 	pr_err("\nCall Trace: ");

arch/cris/kernel/traps.c void show_trace(unsigned long *stack) · line 74

Quoted 8 lines
arch/cris/kernel/traps.c +2 -2
@@ -69,8 +69,8 @@ void show_trace(unsigned long *stack)
 		 * down the cause of the crash will be able to figure
 		 * out the call path that was taken.
 		 */
-		if (((addr >= (unsigned long)&_stext) &&
-		     (addr <= (unsigned long)&_etext)) ||
+		if (((addr >= (unsigned long)_stext) &&
+		     (addr <= (unsigned long)_etext)) ||
 		    ((addr >= module_start) && (addr <= module_end))) {

BTW, I would like to ask cris people, this seems to check over 1 byte, since _etext and module_end will be placed at the next byte of the real address area.
As same as other arch, Should it be

(addr < (unsigned long)_etext)

or are there any other reason?

Thank you,

Quoted 3 lines
arch/cris/kernel/traps.c +0 -0
 #ifdef CONFIG_KALLSYMS
 			print_ip_sym(addr);
Signature · 1 line
Masami Hiramatsu <[email protected]>
Acked-by on v9 3/5
#9 lore raw

Re: [PATCH -tip v9 3/5] cris: mark _stext and _end as char-arrays, not single char

In reply to Masami Hiramatsu
Quoted 2 lines · Masami Hiramatsu

Mark _stext and _end as character arrays instead of single character, as same as include/asm-generic/sections.h does.

Looks good.

Quoted 65 lines
---
 arch/cris/arch-v32/mach-a3/arbiter.c |    4 ++--
 arch/cris/arch-v32/mach-fs/arbiter.c |    4 ++--
 arch/cris/kernel/traps.c             |    6 +++---
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/cris/arch-v32/mach-a3/arbiter.c +2 -2
index ab5c421a4de8..735a9b0abdb8 100644
@@ -227,7 +227,7 @@ static void crisv32_arbiter_config(int arbiter, int region, int unused_slots)
 	}
 }
 
-extern char _stext, _etext;
+extern char _stext[], _etext[];
 
 static void crisv32_arbiter_init(void)
 {
@@ -265,7 +265,7 @@ static void crisv32_arbiter_init(void)
 
 #ifndef CONFIG_ETRAX_KGDB
 	/* Global watch for writes to kernel text segment. */
-	crisv32_arbiter_watch(virt_to_phys(&_stext), &_etext - &_stext,
+	crisv32_arbiter_watch(virt_to_phys(_stext), _etext - _stext,
 		MARB_CLIENTS(arbiter_all_clients, arbiter_bar_all_clients),
 			      arbiter_all_write, NULL);
 #endif
arch/cris/arch-v32/mach-fs/arbiter.c +2 -2
index c97f4d8120f9..047c70bdbb23 100644
@@ -158,7 +158,7 @@ static void crisv32_arbiter_config(int region, int unused_slots)
 	}
 }
 
-extern char _stext, _etext;
+extern char _stext[], _etext[];
 
 static void crisv32_arbiter_init(void)
 {
@@ -190,7 +190,7 @@ static void crisv32_arbiter_init(void)
 
 #ifndef CONFIG_ETRAX_KGDB
 	/* Global watch for writes to kernel text segment. */
-	crisv32_arbiter_watch(virt_to_phys(&_stext), &_etext - &_stext,
+	crisv32_arbiter_watch(virt_to_phys(_stext), _etext - _stext,
 			      arbiter_all_clients, arbiter_all_write, NULL);
 #endif
 }
arch/cris/kernel/traps.c +1 -1
index a01636a12a6e..d98131c45bb5 100644
@@ -42,7 +42,7 @@ void (*nmi_handler)(struct pt_regs *);
 void show_trace(unsigned long *stack)
 {
 	unsigned long addr, module_start, module_end;
-	extern char _stext, _etext;
+	extern char _stext[], _etext[];
 	int i;
 
 	pr_err("\nCall Trace: ");

arch/cris/kernel/traps.c void show_trace(unsigned long *stack) · line 76

Quoted 10 lines
arch/cris/kernel/traps.c +2 -2
@@ -69,8 +69,8 @@ void show_trace(unsigned long *stack)
 		 * down the cause of the crash will be able to figure
 		 * out the call path that was taken.
 		 */
-		if (((addr >= (unsigned long)&_stext) &&
-		     (addr <= (unsigned long)&_etext)) ||
+		if (((addr >= (unsigned long)_stext) &&
+		     (addr <= (unsigned long)_etext)) ||
 		    ((addr >= module_start) && (addr <= module_end))) {
 #ifdef CONFIG_KALLSYMS
 			print_ip_sym(addr);

/^JN - Jesper Nilsson

Signature · 1 line
               Jesper Nilsson -- [email protected]
#8 lore raw

Re: [PATCH -tip v9 3/5] cris: mark _stext and _end as char-arrays, not single char

In reply to Masami Hiramatsu
Quoted 31 lines · Masami Hiramatsu
Quoted 14 lines · On Thu, 3 Aug 2017 11:37:15 +0900 Masami Hiramatsu <[email protected]> wrote:
arch/cris/kernel/traps.c +1 -1
index a01636a12a6e..d98131c45bb5 100644
@@ -42,7 +42,7 @@ void (*nmi_handler)(struct pt_regs *);
 void show_trace(unsigned long *stack)
 {
 	unsigned long addr, module_start, module_end;
-	extern char _stext, _etext;
+	extern char _stext[], _etext[];
 	int i;
 
 	pr_err("\nCall Trace: ");

arch/cris/kernel/traps.c void show_trace(unsigned long *stack) · line 74

Quoted 8 lines
arch/cris/kernel/traps.c +2 -2
@@ -69,8 +69,8 @@ void show_trace(unsigned long *stack)
 		 * down the cause of the crash will be able to figure
 		 * out the call path that was taken.
 		 */
-		if (((addr >= (unsigned long)&_stext) &&
-		     (addr <= (unsigned long)&_etext)) ||
+		if (((addr >= (unsigned long)_stext) &&
+		     (addr <= (unsigned long)_etext)) ||
 		    ((addr >= module_start) && (addr <= module_end))) {

BTW, I would like to ask cris people, this seems to check over 1 byte, since _etext and module_end will be placed at the next byte of the real address area.
As same as other arch, Should it be

Quoted 3 lines

(addr < (unsigned long)_etext)

or are there any other reason?

As far as I can tell, this is non-intentional.

Quoted 3 lines

Thank you,

Masami Hiramatsu <[email protected]>

/^JN - Jesper Nilsson

Signature · 1 line
               Jesper Nilsson -- [email protected]