* [PATCH v3 0/3] perf: Fix SIGCHLD vs pause() race with short-lived workloads
@ 2026-04-22 5:05 Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 1/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record() Swapnil Sapkal
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Swapnil Sapkal @ 2026-04-22 5:05 UTC (permalink / raw)
To: peterz, mingo, acme, namhyung, irogers, james.clark
Cc: mark.rutland, alexander.shishkin, jolsa, adrian.hunter,
ravi.bangoria, linux-perf-users, linux-kernel, Swapnil Sapkal
Several perf subcommands (sched stats, lock contention) use the pattern
of forking a workload child, calling evlist__start_workload() to uncork
it, and then calling pause() to wait for a signal (typically SIGCHLD
when the child exits, or SIGINT/SIGTERM from the user).
This pattern has a race condition: if the workload is very short-lived,
the child can exit and deliver SIGCHLD in the window between
evlist__start_workload() and pause(). Since pause() only returns when a
signal is received *while the process is suspended*, and SIGCHLD has
already been delivered and handled by the empty sighandler(), pause()
blocks indefinitely.
The fix replaces pause() with a simpler approach:
- When a workload is given (argc), use waitpid() to directly wait for
the child process to exit. This is race-free since waitpid()
collects the child's status regardless of when it exited.
- In system-wide mode (no workload), use 'while (!done) sleep(1)' to
wait for SIGINT/SIGTERM. The signal handler sets a
'volatile sig_atomic_t done' flag, and sleep() is interrupted by
signal delivery so the flag is checked promptly.
Three call sites are affected across two files:
- perf_sched__schedstat_record() in builtin-sched.c
- perf_sched__schedstat_live() in builtin-sched.c
- __cmd_contention() in builtin-lock.c
The two pause() sites in builtin-kwork.c are NOT affected because they
do not register SIGCHLD or fork workload children; they only wait for
user-initiated SIGINT/SIGTERM.
Changes since v2:
- Replaced sigsuspend()/sigprocmask() approach with the much simpler
waitpid() + while(!done) sleep(1) pattern as suggested by
Namhyung. This eliminates all signal mask manipulation while
still being race-free.
Changes since v1:
- Moved sigprocmask() to after evlist__prepare_workload() so the
forked child does not inherit a blocked SIGCHLD mask, which would
break workloads relying on SIGCHLD (Sashiko review)
- Block SIGINT and SIGTERM in addition to SIGCHLD to prevent an
early Ctrl+C during setup from being consumed before sigsuspend().
- Error paths before sigprocmask no longer need mask restoration
since the mask is not yet modified at that point.
(Sashiko review)
Swapnil Sapkal (3):
perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record()
perf sched stats: Fix SIGCHLD vs pause() race in schedstat_live()
perf lock contention: Fix SIGCHLD vs pause() race in __cmd_contention()
tools/perf/builtin-lock.c | 17 +++++++++++++----
tools/perf/builtin-sched.c | 28 ++++++++++++++++++++--------
2 files changed, 33 insertions(+), 12 deletions(-)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 1/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record()
2026-04-22 5:05 [PATCH v3 0/3] perf: Fix SIGCHLD vs pause() race with short-lived workloads Swapnil Sapkal
@ 2026-04-22 5:05 ` Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 2/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_live() Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 3/3] perf lock contention: Fix SIGCHLD vs pause() race in __cmd_contention() Swapnil Sapkal
2 siblings, 0 replies; 4+ messages in thread
From: Swapnil Sapkal @ 2026-04-22 5:05 UTC (permalink / raw)
To: peterz, mingo, acme, namhyung, irogers, james.clark
Cc: mark.rutland, alexander.shishkin, jolsa, adrian.hunter,
ravi.bangoria, linux-perf-users, linux-kernel, Swapnil Sapkal
If the profiled workload exits very quickly, SIGCHLD can be delivered
and consumed by the empty signal handler before the process enters
pause(), causing an indefinite hang.
Fix this with a simpler approach:
- When a workload is given, use waitpid() to directly wait for the
child to exit. This is race-free since waitpid() will collect the
child regardless of when it exited.
- In system-wide mode (no workload), use 'while (!done) sleep(1)'
to wait for SIGINT/SIGTERM. The signal handler now sets a
'volatile sig_atomic_t done' flag, and sleep() is interrupted
by signal delivery, so the flag is checked promptly.
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-sched.c | 16 ++++++++++++----
1 file changed, 12 insertions(+), 4 deletions(-)
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 3f509cfdd58c..cfd93bf11c2e 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -36,6 +36,7 @@
#include <linux/zalloc.h>
#include <sys/prctl.h>
#include <sys/resource.h>
+#include <sys/wait.h>
#include <inttypes.h>
#include <errno.h>
@@ -3757,8 +3758,11 @@ static int process_synthesized_schedstat_event(const struct perf_tool *tool,
return 0;
}
+static volatile sig_atomic_t done;
+
static void sighandler(int sig __maybe_unused)
{
+ done = 1;
}
static int enable_sched_schedstats(int *reset)
@@ -3899,11 +3903,15 @@ static int perf_sched__schedstat_record(struct perf_sched *sched,
if (err < 0)
goto out;
- if (argc)
- evlist__start_workload(evlist);
+ done = 0;
- /* wait for signal */
- pause();
+ if (argc) {
+ evlist__start_workload(evlist);
+ waitpid(evlist->workload.pid, NULL, 0);
+ } else {
+ while (!done)
+ sleep(1);
+ }
if (reset) {
err = disable_sched_schedstat();
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 2/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_live()
2026-04-22 5:05 [PATCH v3 0/3] perf: Fix SIGCHLD vs pause() race with short-lived workloads Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 1/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record() Swapnil Sapkal
@ 2026-04-22 5:05 ` Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 3/3] perf lock contention: Fix SIGCHLD vs pause() race in __cmd_contention() Swapnil Sapkal
2 siblings, 0 replies; 4+ messages in thread
From: Swapnil Sapkal @ 2026-04-22 5:05 UTC (permalink / raw)
To: peterz, mingo, acme, namhyung, irogers, james.clark
Cc: mark.rutland, alexander.shishkin, jolsa, adrian.hunter,
ravi.bangoria, linux-perf-users, linux-kernel, Swapnil Sapkal
perf_sched__schedstat_live() has the same lost-wakeup race as
perf_sched__schedstat_record(): a short-lived workload's 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-sched.c | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index cfd93bf11c2e..36fbc20dbebd 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -4695,11 +4695,15 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,
if (err < 0)
goto out;
- if (argc)
- evlist__start_workload(evlist);
+ done = 0;
- /* wait for signal */
- pause();
+ if (argc) {
+ evlist__start_workload(evlist);
+ waitpid(evlist->workload.pid, NULL, 0);
+ } else {
+ while (!done)
+ sleep(1);
+ }
if (reset) {
err = disable_sched_schedstat();
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3 3/3] perf lock contention: Fix SIGCHLD vs pause() race in __cmd_contention()
2026-04-22 5:05 [PATCH v3 0/3] perf: Fix SIGCHLD vs pause() race with short-lived workloads Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 1/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record() Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 2/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_live() Swapnil Sapkal
@ 2026-04-22 5:05 ` Swapnil Sapkal
2 siblings, 0 replies; 4+ messages in thread
From: Swapnil Sapkal @ 2026-04-22 5:05 UTC (permalink / raw)
To: peterz, mingo, acme, namhyung, irogers, james.clark
Cc: mark.rutland, alexander.shishkin, jolsa, adrian.hunter,
ravi.bangoria, linux-perf-users, linux-kernel, Swapnil Sapkal
__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
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-04-22 5:07 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-22 5:05 [PATCH v3 0/3] perf: Fix SIGCHLD vs pause() race with short-lived workloads Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 1/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record() Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 2/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_live() Swapnil Sapkal
2026-04-22 5:05 ` [PATCH v3 3/3] perf lock contention: Fix SIGCHLD vs pause() race in __cmd_contention() Swapnil Sapkal
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®