perf test task-exit: Fix POLLHUP busy-loop race waiting for SIGCHLD
Ian Rogers
reviewerPATCH 1/1 perf test task-exit: Fix POLLHUP busy-loop race waiting for SIGCHLD
In the kernel's do_exit(), perf_event_exit_task() runs before exit_mm() and exit_notify() (which delivers SIGCHLD to the parent). Once perf_event_exit_task() completes, PERF_RECORD_EXIT is written to the ring buffer and POLLHUP is set on the perf event file descriptor.
In test__task_exit(), once POLLHUP is set on the fd, evlist__poll() returns immediately on every call without blocking. If the child is still in exit_mm()..exit_notify() on another CPU when the parent reads PERF_RECORD_EXIT, exited is still 0 and the retry loop spins through all 1000 non-blocking poll() calls in less than a millisecond before SIGCHLD is delivered.
Sleep briefly with usleep(1000) on subsequent retries (which also wakes early on EINTR when SIGCHLD arrives), mark exited as volatile sig_atomic_t, and reset exited and nr_exit on test entry.
--- tools/perf/tests/task-exit.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/tests/task-exit.c +7 -2
@@ -9,13 +9,14 @@#include <errno.h> #include <signal.h> +#include <unistd.h> #include <linux/string.h> #include <perf/cpumap.h> #include <perf/evlist.h> #include <perf/mmap.h> -static int exited; -static int nr_exit; +static volatile sig_atomic_t exited; +static volatile int nr_exit;static void sig_handler (int sig__maybe_unused ) {
@@ -55,6 +56,8 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _struct mmap *md;int retry_count =0 ; + exited =0 ; + nr_exit =0 ;signal (SIGCHLD , sig_handler); evlist =evlist__new_dummy ();
@@ -135,6 +138,8 @@ static int test__task_exit(struct test_suite *test __maybe_unused, int subtest _ err =-1 ; goto out_put_evlist; } +if (retry_count >1 ) +usleep (1000 ); goto retry; }
Signature · 1 line
2.56.0.307.g5fe7bd680c-goog
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 12 lines
In the kernel's
do_exit(),perf_event_exit_task()runs beforeexit_mm()andexit_notify()(which delivers SIGCHLD to the parent). Onceperf_event_exit_task()completes,PERF_RECORD_EXITis written to the ring buffer and POLLHUP is set on the perf event file descriptor.In
test__task_exit(), once POLLHUP is set on the fd,evlist__poll()returns immediately on every call without blocking. If the child is still in exit_mm()..exit_notify() on another CPU when the parent readsPERF_RECORD_EXIT, exited is still 0 and the retry loop spins through all 1000 non-blockingpoll()calls in less than a millisecond before SIGCHLD is delivered.
Quoted 6 lines
Sleep briefly with usleep(1000) on subsequent retries (which also wakes early on EINTR when SIGCHLD arrives), mark exited as volatile
sig_atomic_t, and reset exited andnr_exiton test entry.Fixes 791ce9c48c79 ("perf test: Avoid infinite loop for task exit case")Signed-off-by Ian Rogers <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 5 lines · Ian Rogers
In the kernel's
do_exit(),perf_event_exit_task()runs beforeexit_mm()andexit_notify()(which delivers SIGCHLD to the parent). Onceperf_event_exit_task()completes,PERF_RECORD_EXITis written to the ring buffer and POLLHUP is set on the perf event file descriptor.
Quoted 8 lines
In
test__task_exit(), once POLLHUP is set on the fd,evlist__poll()returns immediately on every call without blocking. If the child is still in exit_mm()..exit_notify() on another CPU when the parent readsPERF_RECORD_EXIT, exited is still 0 and the retry loop spins through all 1000 non-blockingpoll()calls in less than a millisecond before SIGCHLD is delivered.[...]
Applied to perf-tools-next, thanks!
Best regards
Namhyung