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

Swapnil Sapkal
2026-04-01T06:42:46Z

__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 before starting the workload and replacing
pause() with sigsuspend() to atomically unblock and wait.

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swapnil Sapkal <swapnil.sapkal@amd.com>
---
tools/perf/builtin-lock.c | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-lock.c b/tools/perf/builtin-lock.c
index e8962c985d34..1bada9833c93 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 sigchld_mask, oldmask;
int err = -EINVAL;
struct perf_tool eops;
struct perf_data data = {
@@ -2064,6 +2065,15 @@ static int __cmd_contention(int argc, const char **argv)
signal(SIGCHLD, sighandler);
signal(SIGTERM, sighandler);

+ /*
+ * Block SIGCHLD early so that a short-lived workload
+ * cannot deliver the signal before sigsuspend() is
+ * entered below.
+ */
+ sigemptyset(&sigchld_mask);
+ sigaddset(&sigchld_mask, SIGCHLD);
+ sigprocmask(SIG_BLOCK, &sigchld_mask, &oldmask);
+
con.evlist = evlist__new();
if (con.evlist == NULL) {
err = -ENOMEM;
@@ -2127,8 +2137,14 @@ static int __cmd_contention(int argc, const char **argv)
if (argc)
evlist__start_workload(con.evlist);

- /* wait for signal */
- pause();
+ /*
+ * Use sigsuspend() instead of pause() to avoid a race
+ * where a short-lived workload exits and delivers SIGCHLD
+ * before pause() is entered. sigsuspend() atomically
+ * unblocks SIGCHLD (blocked above) and suspends.
+ */
+ sigsuspend(&oldmask);
+ sigprocmask(SIG_SETMASK, &oldmask, NULL);

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