perf/core: Don't send SIGTRAP after exec removed the event
Subsystems (2)
PATCH v3 1/1 perf/core: Don't send SIGTRAP after exec removed the event
A sigtrap event must also set remove_on_exec, so that its SIGTRAP never reaches a program after exec. But the signal is sent from task work, which only runs on the way back to user space. If the event overflows shortly before execve(), the task work can still be pending when the task enters execve(), and then runs when execve() returns. By then perf_event_exec() has removed the event and the new program has default signal handlers, so the SIGTRAP kills it.
The exec_stress test in the remove_on_exec selftest catches this and fails about half the time in a VM. A process that opens a sigtrap event on itself and then calls execve() is killed by SIGTRAP in 15% to 50% of runs, both on an AMD machine running v7.2 and in a VM, with or without close-on-exec on the event fd.
Commit ffb684f2aa14 ("perf: Fix race between perf_event_exit_task() and perf_pending_task()") made perf_sigtrap() return once perf_event_exit_task() has set ctx->task to TASK_TOMBSTONE. Exec only calls perf_event_exit_task() when the new program is not dumpable, such as a setuid one. An ordinary exec keeps the context and only removes the remove_on_exec events, so the SIGTRAP is still sent.
perf_event_exit_event() sets PERF_EVENT_STATE_EXIT when exec removes the event. If the event fd is close-on-exec and was the last reference to the file, exec also queues the file release as task work. Task work runs newest first, so perf_release() runs before the SIGTRAP work and moves the event on to PERF_EVENT_STATE_DEAD. So don't send the signal when the event state is PERF_EVENT_STATE_EXIT or lower, which means the event has been removed. That also covers PERF_EVENT_STATE_REVOKED, where the PMU is gone.
---
Notes:
Changes in v3:
- Rebased onto v7.3-rc7, which has ffb684f2aa14. That commit replaced the PF_EXITING check that v2 was placed after, so the new check now follows its TASK_TOMBSTONE check.
- Peter asked on v2 if this is the same problem as Luo Gengkun's patch:
it isn't. ffb684f2aa14 covers an exec that calls perf_event_exit_task(), which only happens when the new program isn't dumpable. With it applied, an ordinary exec still kills the new program, see the numbers below. The changelog now says this.
- Read event->state with READ_ONCE(), since it is read without locks. Suggested by Sashiko.
- v2: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Skip the signal for every state at or below PERF_EVENT_STATE_EXIT, not only EXIT. With a close-on-exec event fd the file release runs first and moves the event to PERF_EVENT_STATE_DEAD, so v1 still killed 360 of 2000 children. Reported by Sashiko.
- v1: https://lore.kernel.org/all/[email protected]/
Tested on v7.3-rc7 x86_64 under virtme-ng (KASAN, lockdep, 8 vCPUs on an AMD Zen 3 host):
- selftests/perf_events/remove_on_exec: exec_stress failed in 11 of 20 runs before, all 20 pass after.
- The exec_stress pattern in a loop (50 rounds of 30 inheriting children): 24 children in 19 rounds were killed by SIGTRAP before, none after.
- Each child opens its own sigtrap + remove_on_exec event and execs:
342 of 2000 children were killed before, none after. With
PERF_FLAG_FD_CLOEXEC: 1004 of 2000 before, none after. On the bare-metal host running v7.2.6 the same program kills 297 to 1033 of 2000 children across runs, and 1012 of 2000 with PERF_FLAG_FD_CLOEXEC.
- The same program as an execute-only binary run as nobody, so that exec calls perf_event_exit_task() (the ffb684f2aa14 case): no child killed and no warning, before or after.
- sigtrap_threads, watermark_signal and mmap pass before and after. No new kernel warnings.
v7.2 fails the same way in the VM. I did not test kernels older than v7.2. gcc W=1 and sparse show no new warnings.
kernel/events/core.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/events/core.c +9 -0
@@ -7633,6 +7633,15 @@ static void perf_sigtrap(struct perf_event *event)if (event->ctx->task ==TASK_TOMBSTONE )return ; +/* +* The event was removed after this signal was queued, e.g. by exec() +* (remove_on_exec), which may also have closed its fd (close-on-exec). +* The new program has default signal handlers, so a SIGTRAP would +* kill it. +*/ +if (READ_ONCE (event->state) <=PERF_EVENT_STATE_EXIT ) +return ; +/* * We'd expect this to only occur if the irq_work is delayed and either * ctx->task or current has changed in the meantime. This can be the
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.56.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
A sigtrap event must also set
remove_on_exec, so that its SIGTRAP never reaches a program after exec. But the signal is sent from task work, which only runs on the way back to user space. If the event overflows shortly beforeexecve(), the task work can still be pending when the task entersexecve(), and then runs whenexecve()returns. By thenperf_event_exec()has removed the event and the new program has default signal handlers, so the SIGTRAP kills it.The
exec_stresstest in theremove_on_execselftest catches this and fails about half the time in a VM. A process that opens a sigtrap event on itself and then callsexecve()is killed by SIGTRAP in 15% to 50% of runs, both on an AMD machine running v7.2 and in a VM, with or without close-on-exec on the event fd.
Quoted 7 lines
Commit
ffb684f2aa14("perf: Fix race betweenperf_event_exit_task()and [ ... ]Fixes 97ba62b27867 ("perf: Add support for SIGTRAP on perf events")Assisted-by LLMSigned-off-by Danish Khateeb <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
The following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: 6f083fbccd5f06889bc08e1182bd02c0383b9121 Gitweb: https://git.kernel.org/tip/6f083fbccd5f06889bc08e1182bd02c0383b9121 Author: Danish Khateeb <[email protected]> AuthorDate: Thu, 15 Oct 2026 10:20:13 -05:00 Committer: Ingo Molnar <[email protected]> CommitterDate: Fri, 06 Nov 2026 09:52:11 +01:00
perf/core: Don't send SIGTRAP after exec removed the event
A sigtrap event must also set remove_on_exec, so that its SIGTRAP never reaches a program after exec. But the signal is sent from task work, which only runs on the way back to user space. If the event overflows shortly before execve(), the task work can still be pending when the task enters execve(), and then runs when execve() returns. By then perf_event_exec() has removed the event and the new program has default signal handlers, so the SIGTRAP kills it.
The exec_stress test in the remove_on_exec selftest catches this and fails about half the time in a VM. A process that opens a sigtrap event on itself and then calls execve() is killed by SIGTRAP in 15% to 50% of runs, both on an AMD machine running v7.2 and in a VM, with or without close-on-exec on the event fd.
Commit ffb684f2aa14 ("perf: Fix race between perf_event_exit_task() and perf_pending_task()") made perf_sigtrap() return once perf_event_exit_task() has set ctx->task to TASK_TOMBSTONE. Exec only calls perf_event_exit_task() when the new program is not dumpable, such as a setuid one. An ordinary exec keeps the context and only removes the remove_on_exec events, so the SIGTRAP is still sent.
perf_event_exit_event() sets PERF_EVENT_STATE_EXIT when exec removes the event. If the event fd is close-on-exec and was the last reference to the file, exec also queues the file release as task work. Task work runs newest first, so perf_release() runs before the SIGTRAP work and moves the event on to PERF_EVENT_STATE_DEAD. So don't send the signal when the event state is PERF_EVENT_STATE_EXIT or lower, which means the event has been removed. That also covers PERF_EVENT_STATE_REVOKED, where the PMU is gone.
--- kernel/events/core.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/events/core.c +9 -0
@@ -7647,6 +7647,15 @@ static void perf_sigtrap(struct perf_event *event)return ;/* +* The event was removed after this signal was queued, e.g. by exec() +* (remove_on_exec), which may also have closed its fd (close-on-exec). +* The new program has default signal handlers, so a SIGTRAP would +* kill it. +*/ +if (READ_ONCE (event->state) <=PERF_EVENT_STATE_EXIT ) +return ; + +/* * We'd expect this to only occur if the irq_work is delayed and either * ctx->task or current has changed in the meantime. This can be the * case on architectures that do not implement arch_irq_work_raise().