[PATCH v4 2/3] perf sched stats: Fix SIGCHLD vs pause() race in schedstat_live()

From: Swapnil Sapkal

Date: Wed May 20 2026 - 06:29:19 EST


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: replace pause() with a loop checking the 'done'
flag and using waitpid(WNOHANG) for the workload case.

Suggested-by: Namhyung Kim <namhyung@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swapnil Sapkal <swapnil.sapkal@xxxxxxx>
---
tools/perf/builtin-sched.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c
index 7da71c372e25..9e8941ae1b3d 100644
--- a/tools/perf/builtin-sched.c
+++ b/tools/perf/builtin-sched.c
@@ -4755,6 +4755,7 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,
int reset = 0;
int err = 0;

+ done = 0;
signal(SIGINT, sighandler);
signal(SIGCHLD, sighandler);
signal(SIGTERM, sighandler);
@@ -4800,8 +4801,11 @@ static int perf_sched__schedstat_live(struct perf_sched *sched,
if (argc)
evlist__start_workload(evlist);

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

if (reset) {
err = disable_sched_schedstat();
--
2.43.0