While running the new 'perf bench tests' suite introduced in the series
"perf tests: Add coverage for the untested perf subcommands", a SIGTRAP
core dump was observed on powerpc (ppc64le) when running:

    # ./perf bench breakpoint enable -p 1 -a 1
    # Running 'breakpoint/enable' benchmark:
    Trace/breakpoint trap (core dumped)

In bench_breakpoint_enable(), 'done' (flag passed to active_thread)
and 'watched' (the target of the hardware read/write watchpoint) are both
allocated as adjacent local stack variables:

        &watched = stack_base + 3   (1 byte)
        &done    = stack_base + 4   (4 bytes)

On powerpc, hardware watchpoint match ranges are always 8-byte doubleword
aligned (HW_BREAKPOINT_SIZE = 8). As a result, 'watched' and 'done' fall
within the exact same 8-byte aligned doubleword window
[stack_base ... stack_base + 7].

When active_thread actively spins loading 'done', every atomic load
instruction hits the hardware watchpoint set on 'watched', triggering
an unhandled SIGTRAP and terminating the benchmark.

Fix this by dynamically allocating 'watched' on the heap so it does
not share an aligned doubleword with stack variables.

Fixes: 68a6772f11dbb ("perf bench: Add breakpoint benchmarks")
Signed-off-by: Athira Rajeev <[email protected]>
---
 tools/perf/bench/breakpoint.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/tools/perf/bench/breakpoint.c b/tools/perf/bench/breakpoint.c
index 1b7cd4481bd2..1f52775621b0 100644
--- a/tools/perf/bench/breakpoint.c
+++ b/tools/perf/bench/breakpoint.c
@@ -198,7 +198,7 @@ static const char * const enable_usage[] = {
 int bench_breakpoint_enable(int argc, const char **argv)
 {
        unsigned int i, nthreads, result_usec, done = 0;
-       char watched;
+       char *watched;
        int fd;
        pthread_t *threads;
        struct timeval start, stop, diff;
@@ -207,9 +207,14 @@ int bench_breakpoint_enable(int argc, const char **argv)
                usage_with_options(enable_usage, enable_options);
                exit(EXIT_FAILURE);
        }
-       fd = breakpoint_setup(&watched);
+       watched = malloc(sizeof(*watched));
+       if (!watched)
+               exit((perror("malloc"), EXIT_FAILURE));
+
+       fd = breakpoint_setup(watched);
 
        if (fd < 0) {
+               free(watched);
                if (fd == -ENODEV) {
                        printf("Skipping perf bench breakpoint enable: No 
hardware support\n");
                        return 0;
@@ -242,6 +247,7 @@ int bench_breakpoint_enable(int argc, const char **argv)
                pthread_join(threads[i], NULL);
        free(threads);
        close(fd);
+       free(watched);
        switch (bench_format) {
        case BENCH_FORMAT_DEFAULT:
                printf("# Enabled/disabled breakpoint %d time with %d passive 
and %d active threads\n",
-- 
2.52.0


Reply via email to