From: Swapnil Sapkal Date: Wed, 20 May 2026 10:20:15 +0000 (+0000) Subject: perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9416008e5462cf7a1da62bbaca915f483cea8b77;p=thirdparty%2Fkernel%2Fstable.git perf sched stats: Fix SIGCHLD vs pause() race in schedstat_record() 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: - The signal handler now sets a 'volatile sig_atomic_t done' flag. Reset 'done' before registering signal handlers so that an early signal during setup is not discarded by a later reset. - Replace pause() with a loop that checks 'done' and uses waitpid(WNOHANG) to detect child exit without blocking. This handles both workload mode (child exits) and system-wide mode (user sends SIGINT/SIGTERM). Using WNOHANG avoids the SA_RESTART problem where a blocking waitpid() would auto-restart and ignore the done flag if the child doesn't exit on signal. Suggested-by: Namhyung Kim Assisted-by: Claude:claude-opus-4.6 Signed-off-by: Swapnil Sapkal Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Ian Rogers Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Mark Rutland Cc: Peter Zijlstra Cc: Ravi Bangoria Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 9d73c7043182..4f54a08e6672 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -3746,8 +3747,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) @@ -3807,6 +3811,7 @@ static int perf_sched__schedstat_record(struct perf_sched *sched, .mode = PERF_DATA_MODE_WRITE, }; + done = 0; signal(SIGINT, sighandler); signal(SIGCHLD, sighandler); signal(SIGTERM, sighandler); @@ -3891,8 +3896,11 @@ static int perf_sched__schedstat_record(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();