2017-08-03 02:34:52

by Masami Hiramatsu

[permalink] [raw]
Subject: [PATCH -tip v9 0/5] 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(-)

--
Masami Hiramatsu


2017-08-03 02:35:56

by Masami Hiramatsu

[permalink] [raw]
Subject: [PATCH -tip 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.

Signed-off-by: Masami Hiramatsu <[email protected]>
---
arch/h8300/include/asm/traps.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/h8300/include/asm/traps.h b/arch/h8300/include/asm/traps.h
index 15e701130b27..1c5a30ec2df8 100644
--- a/arch/h8300/include/asm/traps.h
+++ b/arch/h8300/include/asm/traps.h
@@ -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 */

2017-08-03 02:37:02

by Masami Hiramatsu

[permalink] [raw]
Subject: [PATCH -tip v9 2/5] xtensa: mark _stext and _end as char-arrays, not single char

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

Signed-off-by: Masami Hiramatsu <[email protected]>
---
arch/xtensa/kernel/setup.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/xtensa/kernel/setup.c b/arch/xtensa/kernel/setup.c
index 33bfa5270d95..08175df7a69e 100644
--- a/arch/xtensa/kernel/setup.c
+++ b/arch/xtensa/kernel/setup.c
@@ -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),

2017-08-03 02:38:07

by Masami Hiramatsu

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

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

Signed-off-by: Masami Hiramatsu <[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(-)

diff --git a/arch/cris/arch-v32/mach-a3/arbiter.c b/arch/cris/arch-v32/mach-a3/arbiter.c
index ab5c421a4de8..735a9b0abdb8 100644
--- a/arch/cris/arch-v32/mach-a3/arbiter.c
+++ b/arch/cris/arch-v32/mach-a3/arbiter.c
@@ -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
diff --git a/arch/cris/arch-v32/mach-fs/arbiter.c b/arch/cris/arch-v32/mach-fs/arbiter.c
index c97f4d8120f9..047c70bdbb23 100644
--- a/arch/cris/arch-v32/mach-fs/arbiter.c
+++ b/arch/cris/arch-v32/mach-fs/arbiter.c
@@ -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
}
diff --git a/arch/cris/kernel/traps.c b/arch/cris/kernel/traps.c
index a01636a12a6e..d98131c45bb5 100644
--- a/arch/cris/kernel/traps.c
+++ b/arch/cris/kernel/traps.c
@@ -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);

2017-08-03 02:39:13

by Masami Hiramatsu

[permalink] [raw]
Subject: [PATCH -tip v9 4/5] irq: Make irqentry text section unconditional

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

Signed-off-by: Masami Hiramatsu <[email protected]>
Suggested-by: Ingo Molnar <[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(-)

diff --git a/arch/arm/include/asm/traps.h b/arch/arm/include/asm/traps.h
index f555bb3664dc..683d9230984a 100644
--- a/arch/arm/include/asm/traps.h
+++ b/arch/arm/include/asm/traps.h
@@ -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)
{
diff --git a/arch/arm64/include/asm/traps.h b/arch/arm64/include/asm/traps.h
index 02e9035b0685..47a9066f7c86 100644
--- a/arch/arm64/include/asm/traps.h
+++ b/arch/arm64/include/asm/traps.h
@@ -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)
{
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index aa58155187c5..5e41396efbbf 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -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
diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
index 7574ef5f16ec..d145a0b1f529 100644
--- a/arch/x86/kernel/unwind_frame.c
+++ b/arch/x86/kernel/unwind_frame.c
@@ -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;
}
diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 532372c6cf15..e5da44eddd2f 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -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[];
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index fffc9bdae025..438b014c192f 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -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)
diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
index a2fddddb0d60..59ba11661b6e 100644
--- a/include/linux/interrupt.h
+++ b/include/linux/interrupt.h
@@ -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

2017-08-03 02:40:21

by Masami Hiramatsu

[permalink] [raw]
Subject: [PATCH -tip v9 5/5] [BUGFIX] 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
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.

Signed-off-by: Masami Hiramatsu <[email protected]>
Reported-by: Francis Deslauriers <[email protected]>
Tested-by: Francis Deslauriers <[email protected]>
---
arch/x86/kernel/kprobes/opt.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index 69ea0bc1cfa3..4f98aad38237 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -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. */

Subject: [tip:perf/core] 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.

Signed-off-by: Masami Hiramatsu <[email protected]>
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]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Link: http://lkml.kernel.org/r/150172769415.27216.12021110228384155707.stgit@devbox
Signed-off-by: Ingo Molnar <[email protected]>
---
arch/h8300/include/asm/traps.h | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/h8300/include/asm/traps.h b/arch/h8300/include/asm/traps.h
index 15e70113..1c5a30e 100644
--- a/arch/h8300/include/asm/traps.h
+++ b/arch/h8300/include/asm/traps.h
@@ -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 */

Subject: [tip:perf/core] 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.

Signed-off-by: Masami Hiramatsu <[email protected]>
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]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Link: http://lkml.kernel.org/r/150172775958.27216.12951305461398200544.stgit@devbox
Signed-off-by: Ingo Molnar <[email protected]>
---
arch/xtensa/kernel/setup.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/xtensa/kernel/setup.c b/arch/xtensa/kernel/setup.c
index 33bfa52..08175df 100644
--- a/arch/xtensa/kernel/setup.c
+++ b/arch/xtensa/kernel/setup.c
@@ -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),

Subject: [tip:perf/core] 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.

Signed-off-by: Masami Hiramatsu <[email protected]>
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]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Link: http://lkml.kernel.org/r/150172782555.27216.2805751327900543374.stgit@devbox
Signed-off-by: Ingo Molnar <[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(-)

diff --git a/arch/cris/arch-v32/mach-a3/arbiter.c b/arch/cris/arch-v32/mach-a3/arbiter.c
index ab5c421..735a9b0 100644
--- a/arch/cris/arch-v32/mach-a3/arbiter.c
+++ b/arch/cris/arch-v32/mach-a3/arbiter.c
@@ -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
diff --git a/arch/cris/arch-v32/mach-fs/arbiter.c b/arch/cris/arch-v32/mach-fs/arbiter.c
index c97f4d8..047c70b 100644
--- a/arch/cris/arch-v32/mach-fs/arbiter.c
+++ b/arch/cris/arch-v32/mach-fs/arbiter.c
@@ -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
}
diff --git a/arch/cris/kernel/traps.c b/arch/cris/kernel/traps.c
index a01636a..d98131c 100644
--- a/arch/cris/kernel/traps.c
+++ b/arch/cris/kernel/traps.c
@@ -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);

Subject: [tip:perf/core] 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.

Suggested-by: Ingo Molnar <[email protected]>
Signed-off-by: Masami Hiramatsu <[email protected]>
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]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Link: http://lkml.kernel.org/r/150172789110.27216.3955739126693102122.stgit@devbox
Signed-off-by: Ingo Molnar <[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(-)

diff --git a/arch/arm/include/asm/traps.h b/arch/arm/include/asm/traps.h
index f555bb3..683d923 100644
--- a/arch/arm/include/asm/traps.h
+++ b/arch/arm/include/asm/traps.h
@@ -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)
{
diff --git a/arch/arm64/include/asm/traps.h b/arch/arm64/include/asm/traps.h
index 02e9035..47a9066 100644
--- a/arch/arm64/include/asm/traps.h
+++ b/arch/arm64/include/asm/traps.h
@@ -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)
{
diff --git a/arch/x86/entry/entry_64.S b/arch/x86/entry/entry_64.S
index d271fb7..3e3da29 100644
--- a/arch/x86/entry/entry_64.S
+++ b/arch/x86/entry/entry_64.S
@@ -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
diff --git a/arch/x86/kernel/unwind_frame.c b/arch/x86/kernel/unwind_frame.c
index b9389d7..c29e5bc 100644
--- a/arch/x86/kernel/unwind_frame.c
+++ b/arch/x86/kernel/unwind_frame.c
@@ -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;
}
diff --git a/include/asm-generic/sections.h b/include/asm-generic/sections.h
index 532372c..e5da44e 100644
--- a/include/asm-generic/sections.h
+++ b/include/asm-generic/sections.h
@@ -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[];
diff --git a/include/asm-generic/vmlinux.lds.h b/include/asm-generic/vmlinux.lds.h
index da0be9a..62e2395 100644
--- a/include/asm-generic/vmlinux.lds.h
+++ b/include/asm-generic/vmlinux.lds.h
@@ -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)
diff --git a/include/linux/interrupt.h b/include/linux/interrupt.h
index a2fdddd..59ba116 100644
--- a/include/linux/interrupt.h
+++ b/include/linux/interrupt.h
@@ -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

Subject: [tip:perf/core] 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.

Reported-by: Francis Deslauriers <[email protected]>
Tested-by: Francis Deslauriers <[email protected]>
Signed-off-by: Masami Hiramatsu <[email protected]>
Cc: Ananth N Mavinakayanahalli <[email protected]>
Cc: Anil S Keshavamurthy <[email protected]>
Cc: Chris Zankel <[email protected]>
Cc: David S . Miller <[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]>
Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
Link: http://lkml.kernel.org/r/150172795654.27216.9824039077047777477.stgit@devbox
Signed-off-by: Ingo Molnar <[email protected]>
---
arch/x86/kernel/kprobes/opt.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kernel/kprobes/opt.c b/arch/x86/kernel/kprobes/opt.c
index 69ea0bc..4f98aad 100644
--- a/arch/x86/kernel/kprobes/opt.c
+++ b/arch/x86/kernel/kprobes/opt.c
@@ -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. */

2017-08-16 11:01:47

by Masami Hiramatsu

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

On Thu, 3 Aug 2017 11:37:15 +0900
Masami Hiramatsu <[email protected]> wrote:

> diff --git a/arch/cris/kernel/traps.c b/arch/cris/kernel/traps.c
> index a01636a12a6e..d98131c45bb5 100644
> --- a/arch/cris/kernel/traps.c
> +++ b/arch/cris/kernel/traps.c
> @@ -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))) {

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,


> #ifdef CONFIG_KALLSYMS
> print_ip_sym(addr);
>


--
Masami Hiramatsu <[email protected]>

2017-08-18 18:38:33

by Jesper Nilsson

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


On Thu, Aug 03, 2017 at 11:37:15AM +0900, Masami Hiramatsu wrote:
> Mark _stext and _end as character arrays instead of single
> character, as same as include/asm-generic/sections.h does.

Looks good.

Acked-by: Jesper Nilsson <[email protected]>

> Signed-off-by: Masami Hiramatsu <[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(-)
>
> diff --git a/arch/cris/arch-v32/mach-a3/arbiter.c b/arch/cris/arch-v32/mach-a3/arbiter.c
> index ab5c421a4de8..735a9b0abdb8 100644
> --- a/arch/cris/arch-v32/mach-a3/arbiter.c
> +++ b/arch/cris/arch-v32/mach-a3/arbiter.c
> @@ -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
> diff --git a/arch/cris/arch-v32/mach-fs/arbiter.c b/arch/cris/arch-v32/mach-fs/arbiter.c
> index c97f4d8120f9..047c70bdbb23 100644
> --- a/arch/cris/arch-v32/mach-fs/arbiter.c
> +++ b/arch/cris/arch-v32/mach-fs/arbiter.c
> @@ -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
> }
> diff --git a/arch/cris/kernel/traps.c b/arch/cris/kernel/traps.c
> index a01636a12a6e..d98131c45bb5 100644
> --- a/arch/cris/kernel/traps.c
> +++ b/arch/cris/kernel/traps.c
> @@ -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);

/^JN - Jesper Nilsson
--
Jesper Nilsson -- [email protected]

2017-08-18 18:38:40

by Jesper Nilsson

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

On Wed, Aug 16, 2017 at 08:01:33PM +0900, Masami Hiramatsu wrote:
> On Thu, 3 Aug 2017 11:37:15 +0900
> Masami Hiramatsu <[email protected]> wrote:
>
> > diff --git a/arch/cris/kernel/traps.c b/arch/cris/kernel/traps.c
> > index a01636a12a6e..d98131c45bb5 100644
> > --- a/arch/cris/kernel/traps.c
> > +++ b/arch/cris/kernel/traps.c
> > @@ -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))) {
>
> 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?

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

> Thank you,

> Masami Hiramatsu <[email protected]>

/^JN - Jesper Nilsson
--
Jesper Nilsson -- [email protected]