Skip to content

Commit c5d93d2

Browse files
Sebastian Andrzej SiewiorPeter Zijlstra
authored andcommitted
perf: Enqueue SIGTRAP always via task_work.
A signal is delivered by raising irq_work() which works from any context including NMI. irq_work() can be delayed if the architecture does not provide an interrupt vector. In order not to lose a signal, the signal is injected via task_work during event_sched_out(). Instead going via irq_work, the signal could be added directly via task_work. The signal is sent to current and can be enqueued on its return path to userland. Queue signal via task_work and consider possible NMI context. Remove perf_event::pending_sigtrap and and use perf_event::pending_work instead. Reported-by: Arnaldo Carvalho de Melo <[email protected]> Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Marco Elver <[email protected]> Tested-by: Arnaldo Carvalho de Melo <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 466e4d8 commit c5d93d2

File tree

2 files changed

+11
-23
lines changed

2 files changed

+11
-23
lines changed

include/linux/perf_event.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,6 @@ struct perf_event {
781781
unsigned int pending_wakeup;
782782
unsigned int pending_kill;
783783
unsigned int pending_disable;
784-
unsigned int pending_sigtrap;
785784
unsigned long pending_addr; /* SIGTRAP */
786785
struct irq_work pending_irq;
787786
struct callback_head pending_task;
@@ -963,7 +962,7 @@ struct perf_event_context {
963962
struct rcu_head rcu_head;
964963

965964
/*
966-
* Sum (event->pending_sigtrap + event->pending_work)
965+
* Sum (event->pending_work + event->pending_work)
967966
*
968967
* The SIGTRAP is targeted at ctx->task, as such it won't do changing
969968
* that until the signal is delivered.

kernel/events/core.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,17 +2283,6 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx)
22832283
state = PERF_EVENT_STATE_OFF;
22842284
}
22852285

2286-
if (event->pending_sigtrap) {
2287-
event->pending_sigtrap = 0;
2288-
if (state != PERF_EVENT_STATE_OFF &&
2289-
!event->pending_work &&
2290-
!task_work_add(current, &event->pending_task, TWA_RESUME)) {
2291-
event->pending_work = 1;
2292-
} else {
2293-
local_dec(&event->ctx->nr_pending);
2294-
}
2295-
}
2296-
22972286
perf_event_set_state(event, state);
22982287

22992288
if (!is_software_event(event))
@@ -6776,11 +6765,6 @@ static void __perf_pending_irq(struct perf_event *event)
67766765
* Yay, we hit home and are in the context of the event.
67776766
*/
67786767
if (cpu == smp_processor_id()) {
6779-
if (event->pending_sigtrap) {
6780-
event->pending_sigtrap = 0;
6781-
perf_sigtrap(event);
6782-
local_dec(&event->ctx->nr_pending);
6783-
}
67846768
if (event->pending_disable) {
67856769
event->pending_disable = 0;
67866770
perf_event_disable_local(event);
@@ -9721,21 +9705,26 @@ static int __perf_event_overflow(struct perf_event *event,
97219705
*/
97229706
bool valid_sample = sample_is_allowed(event, regs);
97239707
unsigned int pending_id = 1;
9708+
enum task_work_notify_mode notify_mode;
97249709

97259710
if (regs)
97269711
pending_id = hash32_ptr((void *)instruction_pointer(regs)) ?: 1;
9727-
if (!event->pending_sigtrap) {
9728-
event->pending_sigtrap = pending_id;
9712+
9713+
notify_mode = in_nmi() ? TWA_NMI_CURRENT : TWA_RESUME;
9714+
9715+
if (!event->pending_work &&
9716+
!task_work_add(current, &event->pending_task, notify_mode)) {
9717+
event->pending_work = pending_id;
97299718
local_inc(&event->ctx->nr_pending);
97309719

97319720
event->pending_addr = 0;
97329721
if (valid_sample && (data->sample_flags & PERF_SAMPLE_ADDR))
97339722
event->pending_addr = data->addr;
9734-
irq_work_queue(&event->pending_irq);
9723+
97359724
} else if (event->attr.exclude_kernel && valid_sample) {
97369725
/*
97379726
* Should not be able to return to user space without
9738-
* consuming pending_sigtrap; with exceptions:
9727+
* consuming pending_work; with exceptions:
97399728
*
97409729
* 1. Where !exclude_kernel, events can overflow again
97419730
* in the kernel without returning to user space.
@@ -9745,7 +9734,7 @@ static int __perf_event_overflow(struct perf_event *event,
97459734
* To approximate progress (with false negatives),
97469735
* check 32-bit hash of the current IP.
97479736
*/
9748-
WARN_ON_ONCE(event->pending_sigtrap != pending_id);
9737+
WARN_ON_ONCE(event->pending_work != pending_id);
97499738
}
97509739
}
97519740

0 commit comments

Comments
 (0)