perf, signal x86: Fix breakpoint events overflow handling
Subsystems (2)
cover perf, signal x86: Fix breakpoint events overflow handling
hi,
while porting Vince's overflow tests I found perf event breakpoint overflow does not work properly.
I found the x86 RF EFLAG bit not being set when returning from debug exception after triggering signal handler. Which is exactly what you get when you set perf breakpoint overflow SIGIO handler.
Patches 1 and 2 are trying to fix that and make attached tests pass. But it's very likely I'm breaking something else by these changes, so please consider those as RFC patches.. pure suggestions to make my point ;-)
Patch 3 fixes the period handling for breakpoint events.
Patches 4 and 5 provide automated perf tests for testing all 3 kernel changes.
Also available at:git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/linux.git
perf/RF1
thanks for comments,
jirka
---
Jiri Olsa (5):
signal x86: Propage RF EFLAGS bit throught the signal restore call signal x86: Clear RF EFLAGS bit for signal handler
perf: Fix hw breakpoints overflow period sampling
perf tests: Test breakpoint overflow signal handler perf tests: Test breakpoint overflow signal handler counts
arch/x86/ia32/ia32_signal.c | 2 - arch/x86/include/asm/sighandling.h | 4 +- arch/x86/kernel/signal.c | 13 ++++--- include/linux/perf_event.h | 2 + kernel/events/core.c | 2 +- kernel/events/hw_breakpoint.c | 5 +++ tools/perf/Makefile | 2 + tools/perf/tests/bp_signal.c | 187 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/bp_signal_overflow.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 8 ++++ tools/perf/tests/tests.h | 2 + 11 files changed, 342 insertions(+), 11 deletions(-) create mode 100644 tools/perf/tests/bp_signal.c create mode 100644 tools/perf/tests/bp_signal_overflow.c
PATCH 1/5 signal x86: Propage RF EFLAGS bit throught the signal restore call
Adding RF EFLAGS bit to be restored on return from signal from the original register context before the signal was entered.
This will prevent the RF flag to disappear when returning from exception due to the signal handler being executed.
--- arch/x86/ia32/ia32_signal.c | 2 -- arch/x86/include/asm/sighandling.h | 4 ++-- arch/x86/kernel/signal.c | 6 ------ 3 files changed, 2 insertions(+), 10 deletions(-)
arch/x86/ia32/ia32_signal.c +0 -2
@@ -34,8 +34,6 @@#include <asm/sys_ia32.h> #include <asm/smap.h> -#define FIX_EFLAGS __FIX_EFLAGS -int copy_siginfo_to_user32 (compat_siginfo_t __user *to,siginfo_t *from) {int err =0 ;
arch/x86/include/asm/sighandling.h +2 -2
@@ -7,10 +7,10 @@#include <asm/processor-flags.h> -#define __FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \ +#define FIX_EFLAGS (X86_EFLAGS_AC | X86_EFLAGS_OF | \ X86_EFLAGS_DF | X86_EFLAGS_TF | X86_EFLAGS_SF | \ X86_EFLAGS_ZF | X86_EFLAGS_AF | X86_EFLAGS_PF | \ - X86_EFLAGS_CF) + X86_EFLAGS_CF | X86_EFLAGS_RF)void signal_fault (struct pt_regs *regs,void __user *frame,char *where);
arch/x86/kernel/signal.c +0 -6
@@ -43,12 +43,6 @@#include <asm/sigframe.h> -#ifdef CONFIG_X86_32 -# define FIX_EFLAGS (__FIX_EFLAGS | X86_EFLAGS_RF) -#else -# define FIX_EFLAGS __FIX_EFLAGS -#endif -#define COPY (x) do { \ get_user_ex(regs->x, &sc->x); \ } while (0)
Signature · 1 line
1.7.11.7
PATCH 2/5 signal x86: Clear RF EFLAGS bit for signal handler
In reply to Jiri OlsaClearing RF EFLAGS bit for signal handler. The reason is, that this flag is set by debug exception code to prevent the recursive exception entry.
Leaving it set for signal handler might prevent debug exception of the signal handler itself.
--- arch/x86/kernel/signal.c | 7 +++++++ 1 file changed, 7 insertions(+)
arch/x86/kernel/signal.c +7 -0
@@ -726,6 +726,13 @@ handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, regs->flags &= ~X86_EFLAGS_DF ;/* +* Clear RF when entering the signal handler, because +* it might disable possible debug exception from the +* signal handler. +*/ + regs->flags &= ~X86_EFLAGS_RF ; + +/* * Clear TF when entering the signal handler, but * notify any tracer that was single-stepping it. * The tracer may want to single-step inside the
Signature · 1 line
1.7.11.7
H. Peter Anvin
reviewerRe: [PATCH 2/5] signal x86: Clear RF EFLAGS bit for signal handler
Quoted 16 lines · Jiri Olsa
Clearing RF EFLAGS bit for signal handler. The reason is, that this flag is set by debug exception code to prevent the recursive exception entry.
Leaving it set for signal handler might prevent debug exception of the signal handler itself.
arch/x86/kernel/signal.c +0
-0@@ -726,6 +726,13 @@ handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, regs->flags &= ~X86_EFLAGS_DF ;/*
arch/x86/kernel/signal.c handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, · line 738
Quoted 10 lines
arch/x86/kernel/signal.c +7
-0+* Clear RF when entering the signal handler, because +* it might disable possible debug exception from the +* signal handler. +*/ + regs->flags &= ~X86_EFLAGS_RF ; + +/* * Clear TF when entering the signal handler, but * notify any tracer that was single-stepping it. * The tracer may want to single-step inside the
Makes sense. However, can you combine all the flags-clearing into one statement while you are at it?
-hpa
Re: [PATCH 2/5] signal x86: Clear RF EFLAGS bit for signal handler
Quoted 15 lines · H. Peter Anvin
Quoted 14 lines · Jiri Olsa
Clearing RF EFLAGS bit for signal handler. The reason is, that this flag is set by debug exception code to prevent the recursive exception entry.
Leaving it set for signal handler might prevent debug exception of the signal handler itself.
arch/x86/kernel/signal.c +0
-0@@ -726,6 +726,13 @@ handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, regs->flags &= ~X86_EFLAGS_DF ;
Quoted 15 lines
arch/x86/kernel/signal.c handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, · line 738
Quoted 12 lines
arch/x86/kernel/signal.c +7
-0/* +* Clear RF when entering the signal handler, because +* it might disable possible debug exception from the +* signal handler. +*/ + regs->flags &= ~X86_EFLAGS_RF ; + +/* * Clear TF when entering the signal handler, but * notify any tracer that was single-stepping it. * The tracer may want to single-step inside the Makes sense. However, can you combine all the flags-clearing into one statement while you are at it?
sure, will send v2
thanks,
jirka
Re: [PATCH 2/5] signal x86: Clear RF EFLAGS bit for signal handler
Quoted 16 lines · Jiri Olsa
Quoted 15 lines · H. Peter Anvin
Quoted 14 lines · Jiri Olsa
Clearing RF EFLAGS bit for signal handler. The reason is, that this flag is set by debug exception code to prevent the recursive exception entry.
Leaving it set for signal handler might prevent debug exception of the signal handler itself.
arch/x86/kernel/signal.c +0
-0@@ -726,6 +726,13 @@ handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, regs->flags &= ~X86_EFLAGS_DF ;
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/signal.c +6
-0/* +* Clear RF when entering the signal handler, because +* it might disable possible debug exception from the +* signal handler. +*/ + regs->flags &= ~X86_EFLAGS_RF ; +Quoted 8 lines
arch/x86/kernel/signal.c handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, · line 738
Quoted 5 lines
arch/x86/kernel/signal.c +1
-0+/* * Clear TF when entering the signal handler, but * notify any tracer that was single-stepping it. * The tracer may want to single-step inside the Makes sense. However, can you combine all the flags-clearing into one statement while you are at it?
Is there any other reason for this besides having just signal instruction doing this update?
It looks like gcc is smart enough to do that anyway:
...
andq $0xfffffffffffffaff,(%rdi)
...
I need to send V2 anyway, since the patchset no longer applies into latest tip.
thanks,
jirka
Re: [PATCH 2/5] signal x86: Clear RF EFLAGS bit for signal handler
Quoted 33 lines · Jiri Olsa
Quoted 16 lines · Jiri Olsa
Quoted 15 lines · H. Peter Anvin
Quoted 14 lines · Jiri Olsa
Clearing RF EFLAGS bit for signal handler. The reason is, that this flag is set by debug exception code to prevent the recursive exception entry.
Leaving it set for signal handler might prevent debug exception of the signal handler itself.
arch/x86/kernel/signal.c +0
-0@@ -726,6 +726,13 @@ handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, regs->flags &= ~X86_EFLAGS_DF ;Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
arch/x86/kernel/signal.c +6
-0/* +* Clear RF when entering the signal handler, because +* it might disable possible debug exception from the +* signal handler. +*/ + regs->flags &= ~X86_EFLAGS_RF ; +Quoted 8 lines
arch/x86/kernel/signal.c handle_signal(unsigned long sig, siginfo_t *info, struct k_sigaction *ka, · line 738
Quoted 5 lines
arch/x86/kernel/signal.c +1
-0+/* * Clear TF when entering the signal handler, but * notify any tracer that was single-stepping it. * The tracer may want to single-step inside the Makes sense. However, can you combine all the flags-clearing into one statement while you are at it?
Quoted 1 line
Is there any other reason for this besides having just signal
s/signal/single/ ;-)
jirka
Quoted 14 lines
instruction doing this update?
It looks like gcc is smart enough to do that anyway:
...
andq $0xfffffffffffffaff,(%rdi)
...I need to send V2 anyway, since the patchset no longer applies into latest tip.
thanks,
jirka
PATCH 3/5 perf: Fix hw breakpoints overflow period sampling
In reply to Jiri OlsaThe hw breakpoint pmu 'add' function is missing the period_left update needed for SW events.
The perf HW breakpoint events use SW events framework for to process the overflow, so it needs to be properly initialized during PMU 'add' method.
--- include/linux/perf_event.h | 2 ++ kernel/events/core.c | 2 +- kernel/events/hw_breakpoint.c | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-)
include/linux/perf_event.h +2 -0
@@ -754,6 +754,7 @@ extern unsigned int perf_output_skip(struct perf_output_handle *handle, unsigned int len);extern int perf_swevent_get_recursion_context (void );extern void perf_swevent_put_recursion_context (int rctx); +extern u64 perf_swevent_set_period (struct perf_event *event);extern void perf_event_enable (struct perf_event *event);extern void perf_event_disable (struct perf_event *event);extern int __perf_event_disable (void *info);
@@ -793,6 +794,7 @@ static inline void perf_event_fork(struct task_struct *tsk) { }static inline void perf_event_init (void ) { }static inline int perf_swevent_get_recursion_context (void ) {return -1 ; }static inline void perf_swevent_put_recursion_context (int rctx) { } +static inline u64 perf_swevent_set_period (struct perf_event *event) {return 0 ; }static inline void perf_event_enable (struct perf_event *event) { }static inline void perf_event_disable (struct perf_event *event) { }static inline int __perf_event_disable (void *info) {return -1 ; }
kernel/events/core.c +1 -1
@@ -4957,7 +4957,7 @@ static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);* sign as trigger. */ -static u64 perf_swevent_set_period (struct perf_event *event) +u64 perf_swevent_set_period (struct perf_event *event) {struct hw_perf_event *hwc = &event->hw;u64 period = hwc->last_period;
kernel/events/hw_breakpoint.c +5 -0
@@ -612,6 +612,11 @@ static int hw_breakpoint_add(struct perf_event *bp, int flags)if (!(flags &PERF_EF_START )) bp->hw.state =PERF_HES_STOPPED ; +if (is_sampling_event (bp)) { + bp->hw.last_period = bp->hw.sample_period; +perf_swevent_set_period (bp); + } +return arch_install_hw_breakpoint (bp); }
Signature · 1 line
1.7.11.7
PATCH 4/5 perf tests: Test breakpoint overflow signal handler
In reply to Jiri OlsaAdding automated test for breakpoint event signal handler checking if it's executed properly.
The test is related to the proper handling of the RF EFLAGS bit on x86_64, but it's generic for all archs.
First we check the signal handler is properly called and that the following debug exception return to user space wouldn't trigger recursive breakpoint.
This is related to x86_64 RF EFLAGS bit being managed in a wrong way.
Second we check that we can set breakpoint in signal handler, which is not possible on x86_64 if the signal handler is executed with RF EFLAG set.
This test is inpired by overflow tests done by Vince Weaver.
--- tools/perf/Makefile | 1 + tools/perf/tests/bp_signal.c | 187 ++++++++++++++++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 4 + tools/perf/tests/tests.h | 1 + 4 files changed, 193 insertions(+) create mode 100644 tools/perf/tests/bp_signal.c
tools/perf/Makefile +1 -0
@@ -497,6 +497,7 @@ LIB_OBJS += $(OUTPUT)tests/evsel-tp-sched.o LIB_OBJS += $(OUTPUT)tests/pmu.o LIB_OBJS += $(OUTPUT)tests/hists_link.o LIB_OBJS += $(OUTPUT)tests/python-use.o +LIB_OBJS += $(OUTPUT)tests/bp_signal.o BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o BUILTIN_OBJS += $(OUTPUT)builtin-bench.o
tools/perf/tests/bp_signal.c new file +187 -0
187 lines not on this page · raw message
tools/perf/tests/builtin-test.c +4 -0
@@ -78,6 +78,10 @@ static struct test { .func = test__python_use, }, { + .desc = "Test breakpoint overflow signal handler", + .func = test__bp_signal, + }, + { .func = NULL, }, };
tools/perf/tests/tests.h +1 -0
@@ -23,5 +23,6 @@ int test__dso_data(void);int test__parse_events (void );int test__hists_link (void );int test__python_use (void ); +int test__bp_signal (void );#endif /* TESTS_H */
Signature · 1 line
1.7.11.7
PATCH 5/5 perf tests: Test breakpoint overflow signal handler counts
In reply to Jiri OlsaAdding automated test to check the exact number of breakpoint event overflows and counts.
This test was originally done by Vince Weaver forperf_event_tests.
--- tools/perf/Makefile | 1 + tools/perf/tests/bp_signal_overflow.c | 126 ++++++++++++++++++++++++++++++++++ tools/perf/tests/builtin-test.c | 4 ++ tools/perf/tests/tests.h | 1 + 4 files changed, 132 insertions(+) create mode 100644 tools/perf/tests/bp_signal_overflow.c
tools/perf/Makefile +1 -0
@@ -498,6 +498,7 @@ LIB_OBJS += $(OUTPUT)tests/pmu.o LIB_OBJS += $(OUTPUT)tests/hists_link.o LIB_OBJS += $(OUTPUT)tests/python-use.o LIB_OBJS += $(OUTPUT)tests/bp_signal.o +LIB_OBJS += $(OUTPUT)tests/bp_signal_overflow.o BUILTIN_OBJS += $(OUTPUT)builtin-annotate.o BUILTIN_OBJS += $(OUTPUT)builtin-bench.o
tools/perf/tests/bp_signal_overflow.c new file +126 -0
126 lines not on this page · raw message
tools/perf/tests/builtin-test.c +4 -0
@@ -82,6 +82,10 @@ static struct test { .func = test__bp_signal, }, { + .desc = "Test breakpoint overflow sampling", + .func = test__bp_signal_overflow, + }, + { .func = NULL, }, };
tools/perf/tests/tests.h +1 -0
@@ -24,5 +24,6 @@ int test__parse_events(void);int test__hists_link (void );int test__python_use (void );int test__bp_signal (void ); +int test__bp_signal_overflow (void );#endif /* TESTS_H */
Signature · 1 line
1.7.11.7