[PATCH v3 3/3] perf lock contention: Fix SIGCHLD vs pause() race in __cmd_contention()

Swapnil Sapkal
2026-04-22T05:07:16Z

__cmd_contention() suffers from the same lost-wakeup race as the perf
sched stats paths: SIGCHLD can be consumed by the signal handler
before pause() is entered, hanging the process.

Apply the same fix: use waitpid() for workload mode and
'while (!done) sleep(1)' for system-wide mode.

Suggested-by: Namhyung Kim <namhyung@kernel.org>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
---
tools/perf/builtin-lock.c | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c
index e8962c985d34..3c165d632941 100644
--- a/tools/perf/builtin-lock.c
+++ b/tools/perf/builtin-lock.c
@@ -31,6 +31,7 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/prctl.h>
+#include <sys/wait.h>
#include <semaphore.h>
#include <math.h>
#include <limits.h>
@@ -1921,8 +1922,11 @@ static int __cmd_report(bool display_info)
return err;
}

+static volatile sig_atomic_t done;
+
static void sighandler(int sig __maybe_unused)
{
+ done = 1;
}

static int check_lock_contention_options(const struct option *options,
@@ -2124,11 +2128,16 @@ static int __cmd_contention(int argc, const char **argv)

if (use_bpf) {
lock_contention_start();
- if (argc)
- evlist__start_workload(con.evlist);

- /* wait for signal */
- pause();
+ done = 0;
+
+ if (argc) {
+ evlist__start_workload(con.evlist);
+ waitpid(con.evlist->workload.pid, NULL, 0);
+ } else {
+ while (!done)
+ sleep(1);
+ }

lock_contention_stop();
lock_contention_read(&con);
--
2.43.0