Skip to contentSkip to conversation

perf test task-exit: Fix POLLHUP busy-loop race waiting for SIGCHLD

PATCH Applied Ian Rogers 3 participants 3 messages Oct 25 – Nov 15 perf events

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 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.

Fixes 791ce9c48c79 ("perf test: Avoid infinite loop for task exit case")
---
 tools/perf/tests/task-exit.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/tests/task-exit.c +7 -2
index 9fac426ac2f9..1a9de2d13497 100644
@@ -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
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines

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.

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 and nr_exit on test entry.

Fixes 791ce9c48c79 ("perf test: Avoid infinite loop for task exit case")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#2 lore raw
In reply to Ian Rogers
Quoted 5 lines · Ian Rogers

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.

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 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.

[...]

Applied to perf-tools-next, thanks!

Best regards
Namhyung