* [PATCH] perf test: Fix race in "Event re-armed with PERF_EVENT_IOC_REFRESH"
@ 2026-11-23 15:34 James Clark
2026-11-23 15:38 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: James Clark @ 2026-11-23 15:34 UTC (permalink / raw)
To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
Namhyung Kim, Mark Rutland, Alexander Shishkin, Jiri Olsa,
Ian Rogers, Adrian Hunter, Francesco Nigro
Cc: Arnaldo Carvalho de Melo, linux-perf-users, linux-kernel,
James Clark
trigger() is invoked without blocking, but the test expects N events to
always be received for N triggers. After each trigger, the event is
disabled and only re-enabled by the signal handler. If the next trigger
happens before the event is re-enabled, then an event count will be
dropped. This is possible because signals are delivered asynchronously.
Fix it by blocking signals then unblocking while waiting for the event
after each call to trigger().
Blocking results in a 1:1 relation between trigger and signal counts,
even when there are extra page faults, so remove the comment and change
>= to == on the final test assert.
Timeout after 2 seconds because we already know there are broken kernels
for the tracepoint test, and the default 120s timeout would be annoying.
This fixes the following failure seen on Arm64:
$ perf test -vv -r 30000 -S "Event re-armed"
...
---- start ----
test child forked, pid 1087
1000 events, count 999, 999 overflows (999 POLL_HUP), re-arm errno 0
---- end(-1) ----
20.1: Software event : FAILED!
Fixes: 3dabc8ad8ae0 ("perf test: Add test for events re-armed with PERF_EVENT_IOC_REFRESH")
Signed-off-by: James Clark <james.clark@linaro.org>
---
Assisted by Claude Opus-5.5
---
tools/perf/tests/event_refresh.c | 30 +++++++++++++++++++++++++++---
1 file changed, 27 insertions(+), 3 deletions(-)
diff --git a/tools/perf/tests/event_refresh.c b/tools/perf/tests/event_refresh.c
index 726e5de4c795..0750cbb63866 100644
--- a/tools/perf/tests/event_refresh.c
+++ b/tools/perf/tests/event_refresh.c
@@ -6,6 +6,7 @@
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
+#include <poll.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
@@ -44,6 +45,7 @@ static void sig_handler(int signum __maybe_unused, siginfo_t *info,
static int count_overflows(struct perf_event_attr *attr, void (*trigger)(int i))
{
struct sigaction sa, old_sa;
+ sigset_t mask, old_mask, wait_mask;
uint64_t count = 0;
int fd, i, err = TEST_FAIL;
@@ -68,6 +70,13 @@ static int count_overflows(struct perf_event_attr *attr, void (*trigger)(int i))
return TEST_FAIL;
}
+ /* Block signals so we only receive while waiting after each trigger() */
+ sigemptyset(&mask);
+ sigaddset(&mask, REFRESH_SIGNAL);
+ sigprocmask(SIG_BLOCK, &mask, &old_mask);
+ wait_mask = old_mask;
+ sigdelset(&wait_mask, REFRESH_SIGNAL);
+
if (fcntl(fd, F_SETFL, O_RDWR | O_NONBLOCK | O_ASYNC) < 0 ||
fcntl(fd, F_SETSIG, REFRESH_SIGNAL) < 0 ||
fcntl(fd, F_SETOWN, getpid()) < 0) {
@@ -82,20 +91,35 @@ static int count_overflows(struct perf_event_attr *attr, void (*trigger)(int i))
pr_debug("PERF_EVENT_IOC_REFRESH failed: %s\n", strerror(errno));
goto out;
}
- for (i = 0; i < NR_EVENTS; i++)
+ for (i = 0; i < NR_EVENTS; i++) {
+ struct timespec timeout = { .tv_sec = 2 };
+ int before = overflows;
+
trigger(i);
+
+ /* Unblock signal to count and refresh event before triggering again */
+ while (overflows == before) {
+ if (ppoll(NULL, 0, &timeout, &wait_mask) == 0)
+ break;
+ }
+ if (overflows == before) {
+ pr_debug("no overflow signal for trigger %d\n", i);
+ break;
+ }
+ }
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
if (read(fd, &count, sizeof(count)) != sizeof(count))
pr_debug("failed to read the event count\n");
pr_debug("%d events, count %" PRIu64 ", %d overflows (%d POLL_HUP), re-arm errno %d\n",
NR_EVENTS, count, overflows, hups, refresh_errno);
- /* Unrelated page faults can only add overflows, never hide one. */
- if (overflows >= NR_EVENTS)
+
+ if (overflows == NR_EVENTS)
err = TEST_OK;
out:
/* Close first: a late signal must still find our handler. */
close(fd);
+ sigprocmask(SIG_SETMASK, &old_mask, NULL);
sigaction(REFRESH_SIGNAL, &old_sa, NULL);
return err;
}
---
base-commit: eb79d2fbc230d41b009e49f20671303c6efbd946
change-id: 20261123-james-perf-event_refresh-cce5f6bd6f8c
Best regards,
--
James Clark <james.clark@linaro.org>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] perf test: Fix race in "Event re-armed with PERF_EVENT_IOC_REFRESH"
2026-11-23 15:34 [PATCH] perf test: Fix race in "Event re-armed with PERF_EVENT_IOC_REFRESH" James Clark
@ 2026-11-23 15:38 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-23 15:38 UTC (permalink / raw)
To: James Clark; +Cc: linux-perf-users
> trigger() is invoked without blocking, but the test expects N events to
> always be received for N triggers. After each trigger, the event is
> disabled and only re-enabled by the signal handler. If the next trigger
> happens before the event is re-enabled, then an event count will be
> dropped. This is possible because signals are delivered asynchronously.
> Fix it by blocking signals then unblocking while waiting for the event
> after each call to trigger().
>
> Blocking results in a 1:1 relation between trigger and signal counts,
> even when there are extra page faults, so remove the comment and change
> >= to == on the final test assert.
>
> Timeout after 2 seconds because we already know there are broken kernels
> for the tracepoint test, and the default 120s timeout would be annoying.
>
> This fixes the following failure seen on Arm64:
>
> $ perf test -vv -r 30000 -S "Event re-armed"
> ...
>
> ---- start ----
> test child forked, pid 1087
> 1000 events, count 999, 999 overflows (999 POLL_HUP), re-arm errno 0
> ---- end(-1) ----
> 20.1: Software event : FAILED!
>
> Fixes: 3dabc8ad8ae0 ("perf test: Add test for events re-armed with PERF_EVENT_IOC_REFRESH")
> Signed-off-by: James Clark <james.clark@linaro.org>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123-james-perf-event_refresh-v1-1-571efa5395ab@linaro.org?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-23 15:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 15:34 [PATCH] perf test: Fix race in "Event re-armed with PERF_EVENT_IOC_REFRESH" James Clark
2026-11-23 15:38 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox