perf test: Add basic stress test for sigtrap handling
PATCH 1/1 perf test: Add basic stress test for sigtrap handling
Add basic stress test for sigtrap handling as a perf tool built-in test. This allows sanity checking the basic sigtrap functionality from within the perf tool.
--- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/sigtrap.c | 154 ++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 157 insertions(+) create mode 100644 tools/perf/tests/sigtrap.c
tools/perf/tests/Build +1 -0
@@ -65,6 +65,7 @@ perf-y += pe-file-parsing.o perf-y += expand-cgroup.o perf-y += perf-time-to-tsc.o perf-y += dlfilter-test.o +perf-y += sigtrap.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/builtin-test.c +1 -0
@@ -107,6 +107,7 @@ static struct test_suite *generic_tests[] = { &suite__expand_cgroup_events, &suite__perf_time_to_tsc, &suite__dlfilter, + &suite__sigtrap, NULL, };
tools/perf/tests/sigtrap.c new file +154 -0
154 lines not on this page · raw message
tools/perf/tests/tests.h +1 -0
@@ -146,6 +146,7 @@ DECLARE_SUITE(pe_file_parsing);DECLARE_SUITE (expand_cgroup_events);DECLARE_SUITE (perf_time_to_tsc);DECLARE_SUITE (dlfilter); +DECLARE_SUITE (sigtrap);/* * PowerPC and S390 do not support creation of instruction breakpoints using the
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
Arnaldo Carvalho de Melo
maintainerEm Mon, Nov 15, 2021 at 12:28:23PM +0100, Marco Elver escreveu:
Quoted 3 lines
Add basic stress test for sigtrap handling as a perf tool built-in test. This allows sanity checking the basic sigtrap functionality from within the perf tool.
Works as root:
[root@five ~]# perf test sigtrap
73: Sigtrap : Ok
[root@five ~]
Not for !root:
⬢[acme@toolbox perf]$ perf test sigtrap
73: Sigtrap : FAILED!
⬢[acme@toolbox perf]$ perf test -v sigtrap
Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc
73: Sigtrap :
--- start ---
test child forked, pid 3812428
FAILED sys_perf_event_open()
test child finished with -1
---- end ----
Sigtrap: FAILED!
⬢[acme@toolbox perf]$
I'll add the following patch on top of it, with it I get:
⬢[acme@toolbox perf]$ perf test sigtrap
73: Sigtrap : FAILED!
⬢[acme@toolbox perf]$ perf test -v sigtrap
Couldn't bump rlimit(MEMLOCK), failures may take place when creating BPF maps, etc
73: Sigtrap :
--- start ---
test child forked, pid 3816772
FAILED sys_perf_event_open(): Permission denied
test child finished with -1
---- end ----
Sigtrap: FAILED!
⬢[acme@toolbox perf]$
tools/perf/tests/sigtrap.c +6 -3
@@ -5,9 +5,11 @@* Copyright (C) 2021, Google LLC. */ +#include <errno.h> #include <stdint.h> #include <stdlib.h> #include <linux/hw_breakpoint.h> +#include <linux/string.h> #include <pthread.h> #include <signal.h> #include <sys/ioctl.h>
@@ -115,6 +117,7 @@ static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __mstruct sigaction oldact;pthread_t threads[NUM_THREADS ];pthread_barrier_t barrier; +char sbuf[STRERR_BUFSIZE ];int i, fd, ret =TEST_FAIL ;pthread_barrier_init (&barrier,NULL ,NUM_THREADS +1 );
@@ -123,19 +126,19 @@ static int test__sigtrap(struct test_suite *test __maybe_unused, int subtest __m action.sa_sigaction = sigtrap_handler;sigemptyset (&action.sa_mask);if (sigaction (SIGTRAP , &action, &oldact)) { -pr_debug ("FAILED sigaction()\n" ); +pr_debug ("FAILED sigaction(): %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); goto out; } fd =sys_perf_event_open (&attr,0 ,-1 ,-1 ,perf_event_open_cloexec_flag ());if (fd <0 ) { -pr_debug ("FAILED sys_perf_event_open()\n" ); +pr_debug ("FAILED sys_perf_event_open(): %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); goto out_restore_sigaction; }for (i =0 ; i <NUM_THREADS ; i++) {if (pthread_create (&threads[i],NULL , test_thread, &barrier)) { -pr_debug ("FAILED pthread_create()" ); +pr_debug ("FAILED pthread_create(): %s\n" ,str_error_r (errno, sbuf,sizeof (sbuf))); goto out_close_perf_event; } }
Quoted 207 lines
Signed-off-by Marco Elver <[email protected]>--- tools/perf/tests/Build | 1 + tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/sigtrap.c | 154 ++++++++++++++++++++++++++++++++ tools/perf/tests/tests.h | 1 + 4 files changed, 157 insertions(+) create mode 100644 tools/perf/tests/sigtrap.ctools/perf/tests/Build +1
-0@@ -65,6 +65,7 @@ perf-y += pe-file-parsing.o perf-y += expand-cgroup.o perf-y += perf-time-to-tsc.o perf-y += dlfilter-test.o +perf-y += sigtrap.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)tools/perf/tests/builtin-test.c +1
-0@@ -107,6 +107,7 @@ static struct test_suite *generic_tests[] = { &suite__expand_cgroup_events, &suite__perf_time_to_tsc, &suite__dlfilter, + &suite__sigtrap, NULL, };tools/perf/tests/sigtrap.c new file +154
-0@@ -0,0 +1,154 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Basic test for sigtrap support. +* +* Copyright (C) 2021, Google LLC. +*/ + +#include <stdint.h> +#include <stdlib.h> +#include <linux/hw_breakpoint.h> +#include <pthread.h> +#include <signal.h> +#include <sys/ioctl.h> +#include <sys/syscall.h> +#include <unistd.h> + +#include "cloexec.h" +#include "debug.h" +#include "event.h" +#include "tests.h" +#include "../perf-sys.h" + +#define NUM_THREADS 5 + +static struct { +int tids_want_signal;/* Which threads still want a signal. */ +int signal_count;/* Sanity check number of signals received. */ +volatile int iterate_on;/* Variable to set breakpoint on. */ +siginfo_t first_siginfo;/* First observed siginfo_t. */ +} ctx; + +#define TEST_SIG_DATA (~(unsigned long)(&ctx.iterate_on)) + +static struct perf_event_attr make_event_attr (void ) +{ +struct perf_event_attr attr = { + .type =PERF_TYPE_BREAKPOINT , + .size =sizeof (attr), + .sample_period =1 , + .disabled =1 , + .bp_addr = (unsigned long )&ctx.iterate_on, + .bp_type =HW_BREAKPOINT_RW , + .bp_len =HW_BREAKPOINT_LEN_1 , + .inherit =1 ,/* Children inherit events ... */ + .inherit_thread =1 ,/* ... but only cloned with CLONE_THREAD. */ + .remove_on_exec =1 ,/* Required by sigtrap. */ + .sigtrap =1 ,/* Request synchronous SIGTRAP on event. */ + .sig_data =TEST_SIG_DATA , + }; +return attr; +} + +static void +sigtrap_handler (int signum__maybe_unused ,siginfo_t *info,void *ucontext__maybe_unused ) +{ +if (!__atomic_fetch_add (&ctx.signal_count,1 , __ATOMIC_RELAXED)) + ctx.first_siginfo = *info; +__atomic_fetch_sub (&ctx.tids_want_signal,syscall (SYS_gettid), __ATOMIC_RELAXED); +} + +static void *test_thread (void *arg) +{ +pthread_barrier_t *barrier = (pthread_barrier_t *)arg; +pid_t tid =syscall (SYS_gettid); +int i; + +pthread_barrier_wait (barrier); + +__atomic_fetch_add (&ctx.tids_want_signal, tid, __ATOMIC_RELAXED); +for (i =0 ; i < ctx.iterate_on -1 ; i++) +__atomic_fetch_add (&ctx.tids_want_signal, tid, __ATOMIC_RELAXED); + +return NULL ; +} + +static int run_test_threads (pthread_t *threads,pthread_barrier_t *barrier) +{ +int i; + +pthread_barrier_wait (barrier); +for (i =0 ; i <NUM_THREADS ; i++) +TEST_ASSERT_EQUAL ("pthread_join() failed" ,pthread_join (threads[i],NULL ),0 ); + +return TEST_OK ; +} + +static int run_stress_test (int fd,pthread_t *threads,pthread_barrier_t *barrier) +{ +int ret; + + ctx.iterate_on =3000 ; + +TEST_ASSERT_EQUAL ("misfired signal?" , ctx.signal_count,0 ); +TEST_ASSERT_EQUAL ("enable failed" ,ioctl (fd,PERF_EVENT_IOC_ENABLE ,0 ),0 ); + ret =run_test_threads (threads, barrier); +TEST_ASSERT_EQUAL ("disable failed" ,ioctl (fd,PERF_EVENT_IOC_DISABLE ,0 ),0 ); + +TEST_ASSERT_EQUAL ("unexpected sigtraps" , ctx.signal_count,NUM_THREADS * ctx.iterate_on); +TEST_ASSERT_EQUAL ("missing signals or incorrectly delivered" , ctx.tids_want_signal,0 ); +TEST_ASSERT_VAL ("unexpected si_addr" , ctx.first_siginfo.si_addr == &ctx.iterate_on); +#if 0 /* FIXME: enable when libc's signal.h has si_perf_{type,data} */ +TEST_ASSERT_EQUAL ("unexpected si_perf_type" , ctx.first_siginfo.si_perf_type, +PERF_TYPE_BREAKPOINT ); +TEST_ASSERT_EQUAL ("unexpected si_perf_data" , ctx.first_siginfo.si_perf_data, +TEST_SIG_DATA ); +#endif + +return ret; +} + +static int test__sigtrap (struct test_suite *test__maybe_unused ,int subtest__maybe_unused ) +{ +struct perf_event_attr attr =make_event_attr (); +struct sigaction action = {}; +struct sigaction oldact; +pthread_t threads[NUM_THREADS ]; +pthread_barrier_t barrier; +int i, fd, ret =TEST_FAIL ; + +pthread_barrier_init (&barrier,NULL ,NUM_THREADS +1 ); + + action.sa_flags =SA_SIGINFO |SA_NODEFER ; + action.sa_sigaction = sigtrap_handler; +sigemptyset (&action.sa_mask); +if (sigaction (SIGTRAP , &action, &oldact)) { +pr_debug ("FAILED sigaction()\n" ); + goto out; + } + + fd =sys_perf_event_open (&attr,0 ,-1 ,-1 ,perf_event_open_cloexec_flag ()); +if (fd <0 ) { +pr_debug ("FAILED sys_perf_event_open()\n" ); + goto out_restore_sigaction; + } + +for (i =0 ; i <NUM_THREADS ; i++) { +if (pthread_create (&threads[i],NULL , test_thread, &barrier)) { +pr_debug ("FAILED pthread_create()" ); + goto out_close_perf_event; + } + } + + ret =run_stress_test (fd, threads, &barrier); + +out_close_perf_event: +close (fd); +out_restore_sigaction: +sigaction (SIGTRAP , &oldact,NULL ); +out: +pthread_barrier_destroy (&barrier); +return ret; +} + +DEFINE_SUITE ("Sigtrap" , sigtrap);tools/perf/tests/tests.h +1
-0@@ -146,6 +146,7 @@ DECLARE_SUITE(pe_file_parsing);DECLARE_SUITE (expand_cgroup_events);DECLARE_SUITE (perf_time_to_tsc);DECLARE_SUITE (dlfilter); +DECLARE_SUITE (sigtrap);/* * PowerPC and S390 do not support creation of instruction breakpoints using the Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
Signature · 1 line
- Arnaldo
Quoted 12 lines · Arnaldo Carvalho de Melo
Em Mon, Nov 15, 2021 at 12:28:23PM +0100, Marco Elver escreveu:
Quoted 3 lines
Add basic stress test for sigtrap handling as a perf tool built-in test. This allows sanity checking the basic sigtrap functionality from within the perf tool.
Works as root:
[root@five ~]# perf test sigtrap
73: Sigtrap : Ok
[root@five ~]Not for !root:
[...]
Quoted 4 lines
FAILED
sys_perf_event_open(): Permission denied
test child finished with -1
---- end ----
Sigtrap: FAILED!
Ah, that shouldn't be the case. It's missing exclude_kernel/hv, and this test should work just fine as non-root. Please squash the below as well. Let me know if you'd like a v2.
Ack for your change printing errors as well.
Thanks,
-- Marco
------ >8 ------
From: Marco Elver <[email protected]>
Date: Tue, 16 Nov 2021 14:52:18 +0100
Subject: [PATCH] fixup! perf test: Add basic stress test for sigtrap handling
Exclude kernel/hypervisor so the test can run as non-root.
--- tools/perf/tests/sigtrap.c | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/tests/sigtrap.c +2 -0
@@ -46,6 +46,8 @@ static struct perf_event_attr make_event_attr(void) .remove_on_exec = 1, /* Required by sigtrap. */ .sigtrap = 1, /* Request synchronous SIGTRAP on event. */ .sig_data = TEST_SIG_DATA, + .exclude_kernel = 1, + .exclude_hv = 1, };return attr; }
Signature · 1 line
2.34.0.rc1.387.gb447b232ab-goog
Quoted 14 lines · Marco Elver
Quoted 12 lines · Arnaldo Carvalho de Melo
Em Mon, Nov 15, 2021 at 12:28:23PM +0100, Marco Elver escreveu:
Quoted 3 lines
Add basic stress test for sigtrap handling as a perf tool built-in test. This allows sanity checking the basic sigtrap functionality from within the perf tool.
Works as root:
[root@five ~]# perf test sigtrap
73: Sigtrap : Ok
[root@five ~]Not for !root:
[...]
Quoted 8 lines
Quoted 4 lines
FAILED
sys_perf_event_open(): Permission denied
test child finished with -1
---- end ----
Sigtrap: FAILED!Ah, that shouldn't be the case. It's missing exclude_kernel/hv, and this test should work just fine as non-root. Please squash the below as well. Let me know if you'd like a v2.
I'll squash
Quoted 32 lines
Ack for your change printing errors as well.
Thanks,
-- Marco------ >8 ------
From: Marco Elver <[email protected]>
Date: Tue, 16 Nov 2021 14:52:18 +0100
Subject: [PATCH] fixup! perf test: Add basic stress test for sigtrap handlingExclude kernel/hypervisor so the test can run as non-root.
Signed-off-by Marco Elver <[email protected]>---
tools/perf/tests/sigtrap.c| 2 ++1 file changed, 2 insertions(+)tools/perf/tests/sigtrap.c +2
-0@@ -46,6 +46,8 @@ static struct perf_event_attr make_event_attr(void) .remove_on_exec = 1, /* Required by sigtrap. */ .sigtrap = 1, /* Request synchronous SIGTRAP on event. */ .sig_data = TEST_SIG_DATA, + .exclude_kernel = 1, + .exclude_hv = 1, };return attr; }
Arnaldo Carvalho de Melo
maintainerEm Tue, Nov 16, 2021 at 02:57:33PM +0100, Marco Elver escreveu:
Quoted 19 lines · Arnaldo Carvalho de Melo
Quoted 12 lines
Em Mon, Nov 15, 2021 at 12:28:23PM +0100, Marco Elver escreveu:
Quoted 3 lines
Add basic stress test for sigtrap handling as a perf tool built-in test. This allows sanity checking the basic sigtrap functionality from within the perf tool.
Works as root:
[root@five ~]# perf test sigtrap
73: Sigtrap : Ok
[root@five ~]Not for !root:
[...]
Quoted 4 lines
FAILED
sys_perf_event_open(): Permission denied
test child finished with -1
---- end ----
Sigtrap: FAILED!
Quoted 5 lines
Ah, that shouldn't be the case. It's missing exclude_kernel/hv, and this test should work just fine as non-root. Please squash the below as well. Let me know if you'd like a v2.
Ack for your change printing errors as well.
Squashed, thanks!
- Arnaldo