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

From: Swapnil Sapkal

Date: Thu Apr 09 2026 - 12:29:02 EST


__cmd_contention() in builtin-lock.c has the same signal race condition
as the perf sched stats code paths. When running with a short-lived
workload via 'perf lock contention -- <cmd>', the child can exit and
deliver SIGCHLD before pause() is entered, causing an indefinite hang.

Fix this by blocking SIGCHLD, SIGINT and SIGTERM after the workload is
forked but before it is started, then replacing pause() with
sigsuspend() to atomically unblock and wait. Blocking is done after
evlist__prepare_workload() so the child does not inherit a modified
signal mask that could break workloads relying on SIGCHLD delivery.

Reviewed-by: James Clark <james.clark@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swapnil Sapkal <swapnil.sapkal@xxxxxxx>
---
tools/perf/builtin-lock.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c
index e8962c985d34..2da689de134a 100644
--- a/tools/perf/builtin-lock.c
+++ b/tools/perf/builtin-lock.c
@@ -1991,6 +1991,7 @@ static int check_lock_contention_options(const struct option *options,

static int __cmd_contention(int argc, const char **argv)
{
+ sigset_t sig_mask, oldmask;
int err = -EINVAL;
struct perf_tool eops;
struct perf_data data = {
@@ -2123,12 +2124,25 @@ static int __cmd_contention(int argc, const char **argv)
}

if (use_bpf) {
+ /*
+ * Block all handled signals after evlist__prepare_workload()
+ * so the forked child does not inherit a modified signal
+ * mask, then use sigsuspend() to atomically unblock and
+ * wait. This prevents a race where a short-lived workload
+ * exits and delivers SIGCHLD before we are ready to wait.
+ */
+ sigemptyset(&sig_mask);
+ sigaddset(&sig_mask, SIGCHLD);
+ sigaddset(&sig_mask, SIGINT);
+ sigaddset(&sig_mask, SIGTERM);
+ sigprocmask(SIG_BLOCK, &sig_mask, &oldmask);
+
lock_contention_start();
if (argc)
evlist__start_workload(con.evlist);

- /* wait for signal */
- pause();
+ sigsuspend(&oldmask);
+ sigprocmask(SIG_SETMASK, &oldmask, NULL);

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