perf: Make SIGTRAP and __perf_pending_irq() work on RT.
Subsystems (2)
v5 cover perf: Make SIGTRAP and __perf_pending_irq() work on RT.
Hi,
Arnaldo reported that "perf test sigtrap" fails on PREEMPT_RT. Sending the signal gets delayed until event_sched_out() which then uses task_work_add() for its delivery. This breaks on PREEMPT_RT because the signal is delivered with disabled preemption.
While looking at this, I also stumbled upon __perf_pending_irq() which requires disabled interrupts but this is not the case on PREEMPT_RT.
This series aim to address both issues while not introducing a new issue at the same time ;)
Any testing is appreciated.
v4…v5: https://lore.kernel.org/all/[email protected]/
- Add TWA_NMI_CURRENT as notify mode for task_work_add() and use it. PeterZ pointed out that the current version is not NMI safe.
v3…v4: https://lore.kernel.org/all/[email protected]/
- Rebased on top of Frederic's series
(https://lore.kernel.org/all/[email protected])
- Frederick pointed out that perf_pending_task() needs to perf_swevent_get_recursion_context() in order not to recurse if something within perf_swevent_.*_recursion_context() triggers a software event. To address this, the counters have been moved to the task_struct (#3 + #4) and preemt_disable() has been replaced with a RCU-read lock (#5).
- The remaning logic same that means the event is pushed to task-work instead of delivering from IRQ-work. The series was tested with remove_on_exec as suggested by Marco Elver: On PREEMPT_RT a single invocation passes, 100 parallel invocations report (for some) unexpected SIGTRAPs and timeouts. This also observed on !RT (without the series) with a higher task-count.
v2…v3: https://lore.kernel.org/all/[email protected]/
- Marco suggested to add a few comments
- Added a comment to __perf_event_overflow() to explain why irq_work is raised in the in_nmi() case.
- Added a comment to perf_event_exit_event() to explain why the pending event is deleted.
v1…v2: https://lore.kernel.org/all/[email protected]/
- Marco pointed me to the testsuite that showed two problems:
- Delayed task_work from NMI / missing events.
Fixed by triggering dummy irq_work to enforce an interrupt for the exit-to-userland path which checks task_work
- Increased ref-count on clean up/ during exec.
Mostly addressed by the former change. There is still a window if the NMI occurs during execve(). This is addressed by removing the task_work before free_event().
The testsuite (remove_on_exec) fails sometimes if the event/ SIGTRAP is sent before the sighandler is installed.
Sebastian
PATCH v5 1/7 perf: Move irq_work_queue() where the event is prepared.
Only if perf_event::pending_sigtrap is zero, the irq_work accounted by increminging perf_event::nr_pending. The member perf_event::pending_addr might be overwritten by a subsequent event if the signal was not yet delivered and is expected. The irq_work will not be enqeueued again because it has a check to be only enqueued once.
Move irq_work_queue() to where the counter is incremented and perf_event::pending_sigtrap is set to make it more obvious that the irq_work is scheduled once.
--- kernel/events/core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/events/core.c +5 -5
@@ -9738,6 +9738,11 @@ static int __perf_event_overflow(struct perf_event *event,if (!event->pending_sigtrap) { event->pending_sigtrap = pending_id;local_inc (&event->ctx->nr_pending); + + event->pending_addr =0 ; +if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR )) + event->pending_addr = data->addr; +irq_work_queue (&event->pending_irq); }else if (event->attr.exclude_kernel && valid_sample) {/* * Should not be able to return to user space without
@@ -9753,11 +9758,6 @@ static int __perf_event_overflow(struct perf_event *event,*/ WARN_ON_ONCE (event->pending_sigtrap != pending_id); } - - event->pending_addr =0 ; -if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR )) - event->pending_addr = data->addr; -irq_work_queue (&event->pending_irq); }READ_ONCE (event->overflow_handler)(event, data, regs);
Signature · 1 line
2.45.2
[tip: perf/core] perf: Move irq_work_queue() where the event is prepared.
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 058244c683111d44a5de16ac74f19a1754dd7a0c Gitweb: https://git.kernel.org/tip/058244c683111d44a5de16ac74f19a1754dd7a0c Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 04 Jul 2024 19:03:35 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 09 Jul 2024 13:26:34 +02:00
perf: Move irq_work_queue() where the event is prepared.
Only if perf_event::pending_sigtrap is zero, the irq_work accounted by increminging perf_event::nr_pending. The member perf_event::pending_addr might be overwritten by a subsequent event if the signal was not yet delivered and is expected. The irq_work will not be enqeueued again because it has a check to be only enqueued once.
Move irq_work_queue() to where the counter is incremented and perf_event::pending_sigtrap is set to make it more obvious that the irq_work is scheduled once.
--- kernel/events/core.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/events/core.c +5 -5
@@ -9727,6 +9727,11 @@ static int __perf_event_overflow(struct perf_event *event,if (!event->pending_sigtrap) { event->pending_sigtrap = pending_id;local_inc (&event->ctx->nr_pending); + + event->pending_addr =0 ; +if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR )) + event->pending_addr = data->addr; +irq_work_queue (&event->pending_irq); }else if (event->attr.exclude_kernel && valid_sample) {/* * Should not be able to return to user space without
@@ -9742,11 +9747,6 @@ static int __perf_event_overflow(struct perf_event *event,*/ WARN_ON_ONCE (event->pending_sigtrap != pending_id); } - - event->pending_addr =0 ; -if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR )) - event->pending_addr = data->addr; -irq_work_queue (&event->pending_irq); }READ_ONCE (event->overflow_handler)(event, data, regs);
PATCH v5 2/7 task_work: Add TWA_NMI_CURRENT as an additional notify mode.
In reply to Sebastian Andrzej SiewiorAdding task_work from NMI context requires the following:
- The kasan_record_aux_stack() is not NMU safe and must be avoided.
- Using TWA_RESUME is NMI safe. If the NMI occurs while the CPU is in userland then it will continue in userland and not invoke the `work' callback.
Add TWA_NMI_CURRENT as an additional notify mode. In this mode skip kasan and use irq_work in hardirq-mode to for needed interrupt. Set TIF_NOTIFY_RESUME within the irq_work callback due to k[ac]san instrumentation in test_and_set_bit() which does not look NMI safe in case of a report.
--- include/linux/task_work.h | 1 + kernel/task_work.c | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-)
include/linux/task_work.h +1 -0
@@ -18,6 +18,7 @@ enum task_work_notify_mode {TWA_RESUME ,TWA_SIGNAL ,TWA_SIGNAL_NO_IPI , + TWA_NMI_CURRENT, };static inline bool task_work_pending (struct task_struct *task)
kernel/task_work.c +22 -3
@@ -1,10 +1,19 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/irq_work.h> #include <linux/spinlock.h> #include <linux/task_work.h> #include <linux/resume_user_mode.h> +#include <trace/events/ipi.h> static struct callback_head work_exited;/* all we need is ->next == NULL */ +static void task_work_set_notify_irq (struct irq_work *entry) +{ +test_and_set_tsk_thread_flag (current,TIF_NOTIFY_RESUME ); +} +static DEFINE_PER_CPU(struct irq_work, irq_work_NMI_resume) = +IRQ_WORK_INIT_HARD (task_work_set_notify_irq); +/** * task_work_add - ask the @task to execute @work->func() * @task: the task which should run the callback
@@ -12,7 +21,7 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */* @notify: how to notify the targeted task * * Queue @work for task_work_run() below and notify the @task if @notify -* is @TWA_RESUME, @TWA_SIGNAL, or @TWA_SIGNAL_NO_IPI. +* is @TWA_RESUME, @TWA_SIGNAL, @TWA_SIGNAL_NO_IPI or @TWA_NMI_CURRENT. * * @TWA_SIGNAL works like signals, in that the it will interrupt the targeted * task and run the task_work, regardless of whether the task is currently
@@ -24,6 +33,8 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */* kernel anyway. * @TWA_RESUME work is run only when the task exits the kernel and returns to * user mode, or before entering guest mode. +* @TWA_NMI_CURRENT works like @TWA_RESUME, except it can only be used for the +* current @task and if the current context is NMI. * * Fails if the @task is exiting/exited and thus it can't process this @work. * Otherwise @work->func() will be called when the @task goes through one of
@@ -44,8 +55,13 @@ int task_work_add(struct task_struct *task, struct callback_head *work, {struct callback_head *head; -/* record the work call stack in order to print it in KASAN reports */ -kasan_record_aux_stack (work); +if (notify ==TWA_NMI_CURRENT ) { +if (WARN_ON_ONCE (task != current)) +return -EINVAL ; + }else { +/* record the work call stack in order to print it in KASAN reports */ +kasan_record_aux_stack (work); + } head =READ_ONCE (task->task_works); do {
@@ -66,6 +82,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,case TWA_SIGNAL_NO_IPI :__set_notify_signal (task);break ; +case TWA_NMI_CURRENT : +irq_work_queue (this_cpu_ptr (&irq_work_NMI_resume)); +break ;default :WARN_ON_ONCE (1 );break ;
Signature · 1 line
2.45.2
Re: [PATCH v5 2/7] task_work: Add TWA_NMI_CURRENT as an additional notify mode.
kernel/task_work.c line 6
Quoted 11 lines · On 2024-07-04 19:03:36 [+0200], To [email protected] wrote:
kernel/task_work.c +2
-0@@ -1,10 +1,19 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/irq_work.h> #include <linux/spinlock.h> #include <linux/task_work.h> #include <linux/resume_user_mode.h> +#include <trace/events/ipi.h>
Just noticed that this trace include does not belong here. It is a leftover from an earlier iteration where I intended to use smp_send_reschedule() instead of irq_work…
Should I repost the series, or just this one?
Sebastian
Peter Zijlstra
maintainerRe: [PATCH v5 2/7] task_work: Add TWA_NMI_CURRENT as an additional notify mode.
Quoted 17 lines · Sebastian Andrzej Siewior
kernel/task_work.c line 6
Quoted 11 lines · On 2024-07-04 19:03:36 [+0200], To [email protected] wrote:
kernel/task_work.c +2
-0@@ -1,10 +1,19 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/irq_work.h> #include <linux/spinlock.h> #include <linux/task_work.h> #include <linux/resume_user_mode.h> +#include <trace/events/ipi.h> Just noticed that this trace include does not belong here. It is a leftover from an earlier iteration where I intended to use
smp_send_reschedule()instead of irq_work…
Quoted 1 line
Should I repost the series, or just this one?
I'll zap it. no worries.
[tip: perf/core] task_work: Add TWA_NMI_CURRENT as an additional notify mode.
In reply to Sebastian Andrzej SiewiorThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 466e4d801cd438a1ab2c8a2cce1bef6b65c31bbb Gitweb: https://git.kernel.org/tip/466e4d801cd438a1ab2c8a2cce1bef6b65c31bbb Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 04 Jul 2024 19:03:36 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 09 Jul 2024 13:26:34 +02:00
task_work: Add TWA_NMI_CURRENT as an additional notify mode.
Adding task_work from NMI context requires the following:
- The kasan_record_aux_stack() is not NMU safe and must be avoided.
- Using TWA_RESUME is NMI safe. If the NMI occurs while the CPU is in userland then it will continue in userland and not invoke the `work' callback.
Add TWA_NMI_CURRENT as an additional notify mode. In this mode skip kasan and use irq_work in hardirq-mode to for needed interrupt. Set TIF_NOTIFY_RESUME within the irq_work callback due to k[ac]san instrumentation in test_and_set_bit() which does not look NMI safe in case of a report.
--- include/linux/task_work.h | 1 + kernel/task_work.c | 24 +++++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-)
include/linux/task_work.h +1 -0
@@ -18,6 +18,7 @@ enum task_work_notify_mode {TWA_RESUME ,TWA_SIGNAL ,TWA_SIGNAL_NO_IPI , + TWA_NMI_CURRENT, };static inline bool task_work_pending (struct task_struct *task)
kernel/task_work.c +21 -3
@@ -1,10 +1,18 @@// SPDX-License-Identifier: GPL-2.0 +#include <linux/irq_work.h> #include <linux/spinlock.h> #include <linux/task_work.h> #include <linux/resume_user_mode.h> static struct callback_head work_exited;/* all we need is ->next == NULL */ +static void task_work_set_notify_irq (struct irq_work *entry) +{ +test_and_set_tsk_thread_flag (current,TIF_NOTIFY_RESUME ); +} +static DEFINE_PER_CPU(struct irq_work, irq_work_NMI_resume) = +IRQ_WORK_INIT_HARD (task_work_set_notify_irq); +/** * task_work_add - ask the @task to execute @work->func() * @task: the task which should run the callback
@@ -12,7 +20,7 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */* @notify: how to notify the targeted task * * Queue @work for task_work_run() below and notify the @task if @notify -* is @TWA_RESUME, @TWA_SIGNAL, or @TWA_SIGNAL_NO_IPI. +* is @TWA_RESUME, @TWA_SIGNAL, @TWA_SIGNAL_NO_IPI or @TWA_NMI_CURRENT. * * @TWA_SIGNAL works like signals, in that the it will interrupt the targeted * task and run the task_work, regardless of whether the task is currently
@@ -24,6 +32,8 @@ static struct callback_head work_exited; /* all we need is ->next == NULL */* kernel anyway. * @TWA_RESUME work is run only when the task exits the kernel and returns to * user mode, or before entering guest mode. +* @TWA_NMI_CURRENT works like @TWA_RESUME, except it can only be used for the +* current @task and if the current context is NMI. * * Fails if the @task is exiting/exited and thus it can't process this @work. * Otherwise @work->func() will be called when the @task goes through one of
@@ -44,8 +54,13 @@ int task_work_add(struct task_struct *task, struct callback_head *work, {struct callback_head *head; -/* record the work call stack in order to print it in KASAN reports */ -kasan_record_aux_stack (work); +if (notify ==TWA_NMI_CURRENT ) { +if (WARN_ON_ONCE (task != current)) +return -EINVAL ; + }else { +/* record the work call stack in order to print it in KASAN reports */ +kasan_record_aux_stack (work); + } head =READ_ONCE (task->task_works); do {
@@ -66,6 +81,9 @@ int task_work_add(struct task_struct *task, struct callback_head *work,case TWA_SIGNAL_NO_IPI :__set_notify_signal (task);break ; +case TWA_NMI_CURRENT : +irq_work_queue (this_cpu_ptr (&irq_work_NMI_resume)); +break ;default :WARN_ON_ONCE (1 );break ;
PATCH v5 3/7 perf: Enqueue SIGTRAP always via task_work.
In reply to Sebastian Andrzej SiewiorA 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.
--- include/linux/perf_event.h | 3 +-- kernel/events/core.c | 31 ++++++++++--------------------- 2 files changed, 11 insertions(+), 23 deletions(-)
include/linux/perf_event.h +1 -2
@@ -781,7 +781,6 @@ struct perf_event {unsigned int pending_wakeup;unsigned int pending_kill;unsigned int pending_disable; -unsigned int pending_sigtrap;unsigned long pending_addr;/* SIGTRAP */ struct irq_work pending_irq;struct callback_head pending_task;
@@ -963,7 +962,7 @@ struct perf_event_context {struct rcu_head rcu_head;/* -* Sum (event->pending_sigtrap + event->pending_work) +* Sum (event->pending_work + event->pending_work) * * The SIGTRAP is targeted at ctx->task, as such it won't do changing * that until the signal is delivered.
kernel/events/core.c +10 -21
@@ -2283,17 +2283,6 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx) state =PERF_EVENT_STATE_OFF ; } -if (event->pending_sigtrap) { - event->pending_sigtrap =0 ; -if (state !=PERF_EVENT_STATE_OFF && - !event->pending_work && - !task_work_add (current, &event->pending_task,TWA_RESUME )) { - event->pending_work =1 ; - }else { -local_dec (&event->ctx->nr_pending); - } - } -perf_event_set_state (event, state);if (!is_software_event (event))
@@ -6787,11 +6776,6 @@ static void __perf_pending_irq(struct perf_event *event)* Yay, we hit home and are in the context of the event. */ if (cpu ==smp_processor_id ()) { -if (event->pending_sigtrap) { - event->pending_sigtrap =0 ; -perf_sigtrap (event); -local_dec (&event->ctx->nr_pending); - }if (event->pending_disable) { event->pending_disable =0 ;perf_event_disable_local (event);
@@ -9732,21 +9716,26 @@ static int __perf_event_overflow(struct perf_event *event,*/ bool valid_sample =sample_is_allowed (event, regs);unsigned int pending_id =1 ; +enum task_work_notify_mode notify_mode;if (regs) pending_id =hash32_ptr ((void *)instruction_pointer (regs)) ?:1 ; -if (!event->pending_sigtrap) { - event->pending_sigtrap = pending_id; + + notify_mode =in_nmi () ?TWA_NMI_CURRENT :TWA_RESUME ; + +if (!event->pending_work && + !task_work_add (current, &event->pending_task, notify_mode)) { + event->pending_work = pending_id;local_inc (&event->ctx->nr_pending); event->pending_addr =0 ;if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR )) event->pending_addr = data->addr; -irq_work_queue (&event->pending_irq); + }else if (event->attr.exclude_kernel && valid_sample) {/* * Should not be able to return to user space without -* consuming pending_sigtrap; with exceptions: +* consuming pending_work; with exceptions: * * 1. Where !exclude_kernel, events can overflow again * in the kernel without returning to user space.
@@ -9756,7 +9745,7 @@ static int __perf_event_overflow(struct perf_event *event,* To approximate progress (with false negatives), * check 32-bit hash of the current IP. */ -WARN_ON_ONCE (event->pending_sigtrap != pending_id); +WARN_ON_ONCE (event->pending_work != pending_id); } }
Signature · 1 line
2.45.2
[tip: perf/core] perf: Enqueue SIGTRAP always via task_work.
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: c5d93d23a26012a98b77f9e354ab9b3afd420059 Gitweb: https://git.kernel.org/tip/c5d93d23a26012a98b77f9e354ab9b3afd420059 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 04 Jul 2024 19:03:37 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 09 Jul 2024 13:26:35 +02:00
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.
--- include/linux/perf_event.h | 3 +-- kernel/events/core.c | 31 ++++++++++--------------------- 2 files changed, 11 insertions(+), 23 deletions(-)
include/linux/perf_event.h +1 -2
@@ -781,7 +781,6 @@ struct perf_event {unsigned int pending_wakeup;unsigned int pending_kill;unsigned int pending_disable; -unsigned int pending_sigtrap;unsigned long pending_addr;/* SIGTRAP */ struct irq_work pending_irq;struct callback_head pending_task;
@@ -963,7 +962,7 @@ struct perf_event_context {struct rcu_head rcu_head;/* -* Sum (event->pending_sigtrap + event->pending_work) +* Sum (event->pending_work + event->pending_work) * * The SIGTRAP is targeted at ctx->task, as such it won't do changing * that until the signal is delivered.
kernel/events/core.c +10 -21
@@ -2283,17 +2283,6 @@ event_sched_out(struct perf_event *event, struct perf_event_context *ctx) state =PERF_EVENT_STATE_OFF ; } -if (event->pending_sigtrap) { - event->pending_sigtrap =0 ; -if (state !=PERF_EVENT_STATE_OFF && - !event->pending_work && - !task_work_add (current, &event->pending_task,TWA_RESUME )) { - event->pending_work =1 ; - }else { -local_dec (&event->ctx->nr_pending); - } - } -perf_event_set_state (event, state);if (!is_software_event (event))
@@ -6776,11 +6765,6 @@ static void __perf_pending_irq(struct perf_event *event)* Yay, we hit home and are in the context of the event. */ if (cpu ==smp_processor_id ()) { -if (event->pending_sigtrap) { - event->pending_sigtrap =0 ; -perf_sigtrap (event); -local_dec (&event->ctx->nr_pending); - }if (event->pending_disable) { event->pending_disable =0 ;perf_event_disable_local (event);
@@ -9721,21 +9705,26 @@ static int __perf_event_overflow(struct perf_event *event,*/ bool valid_sample =sample_is_allowed (event, regs);unsigned int pending_id =1 ; +enum task_work_notify_mode notify_mode;if (regs) pending_id =hash32_ptr ((void *)instruction_pointer (regs)) ?:1 ; -if (!event->pending_sigtrap) { - event->pending_sigtrap = pending_id; + + notify_mode =in_nmi () ?TWA_NMI_CURRENT :TWA_RESUME ; + +if (!event->pending_work && + !task_work_add (current, &event->pending_task, notify_mode)) { + event->pending_work = pending_id;local_inc (&event->ctx->nr_pending); event->pending_addr =0 ;if (valid_sample && (data->sample_flags &PERF_SAMPLE_ADDR )) event->pending_addr = data->addr; -irq_work_queue (&event->pending_irq); + }else if (event->attr.exclude_kernel && valid_sample) {/* * Should not be able to return to user space without -* consuming pending_sigtrap; with exceptions: +* consuming pending_work; with exceptions: * * 1. Where !exclude_kernel, events can overflow again * in the kernel without returning to user space.
@@ -9745,7 +9734,7 @@ static int __perf_event_overflow(struct perf_event *event,* To approximate progress (with false negatives), * check 32-bit hash of the current IP. */ -WARN_ON_ONCE (event->pending_sigtrap != pending_id); +WARN_ON_ONCE (event->pending_work != pending_id); } }
PATCH v5 4/7 perf: Shrink the size of the recursion counter.
In reply to Sebastian Andrzej SiewiorThere are four recursion counter, one for each context. The type of the counter is int' but the counter is used as bool' since it is only incremented if zero.
The main goal here is to shrink the whole struct into 32bit int which can later be added task_struct into an existing hole.
Reduce the type of the recursion counter to an unsigned char, keep the increment/ decrement operation.
--- kernel/events/callchain.c | 2 +- kernel/events/core.c | 2 +- kernel/events/internal.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
kernel/events/callchain.c +1 -1
@@ -29,7 +29,7 @@ static inline size_t perf_callchain_entry__sizeof(void) sysctl_perf_event_max_contexts_per_stack)); } -static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]); +static DEFINE_PER_CPU(u8, callchain_recursion[PERF_NR_CONTEXTS]);static atomic_t nr_callchain_events;static DEFINE_MUTEX (callchain_mutex);static struct callchain_cpus_entries *callchain_cpus_entries;
kernel/events/core.c +1 -1
@@ -9776,7 +9776,7 @@ struct swevent_htable {int hlist_refcount;/* Recursion avoidance in each contexts */ -int recursion[PERF_NR_CONTEXTS ]; +u8 recursion[PERF_NR_CONTEXTS ]; }; static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
kernel/events/internal.h +2 -2
@@ -208,7 +208,7 @@ arch_perf_out_copy_user(void *dst, const void *src, unsigned long n) DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user) -static inline int get_recursion_context (int *recursion) +static inline int get_recursion_context (u8 *recursion) {unsigned char rctx =interrupt_context_level ();
@@ -221,7 +221,7 @@ static inline int get_recursion_context(int *recursion)return rctx; } -static inline void put_recursion_context (int *recursion,int rctx) +static inline void put_recursion_context (u8 *recursion,int rctx) {barrier (); recursion[rctx]--;
Signature · 1 line
2.45.2
[tip: perf/core] perf: Shrink the size of the recursion counter.
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 5af42f928f3ac555c228740fb4a92d05b19fdd49 Gitweb: https://git.kernel.org/tip/5af42f928f3ac555c228740fb4a92d05b19fdd49 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 04 Jul 2024 19:03:38 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 09 Jul 2024 13:26:35 +02:00
perf: Shrink the size of the recursion counter.
There are four recursion counter, one for each context. The type of the counter is int' but the counter is used as bool' since it is only incremented if zero.
The main goal here is to shrink the whole struct into 32bit int which can later be added task_struct into an existing hole.
Reduce the type of the recursion counter to an unsigned char, keep the increment/ decrement operation.
--- kernel/events/callchain.c | 2 +- kernel/events/core.c | 2 +- kernel/events/internal.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-)
kernel/events/callchain.c +1 -1
@@ -29,7 +29,7 @@ static inline size_t perf_callchain_entry__sizeof(void) sysctl_perf_event_max_contexts_per_stack)); } -static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]); +static DEFINE_PER_CPU(u8, callchain_recursion[PERF_NR_CONTEXTS]);static atomic_t nr_callchain_events;static DEFINE_MUTEX (callchain_mutex);static struct callchain_cpus_entries *callchain_cpus_entries;
kernel/events/core.c +1 -1
@@ -9765,7 +9765,7 @@ struct swevent_htable {int hlist_refcount;/* Recursion avoidance in each contexts */ -int recursion[PERF_NR_CONTEXTS ]; +u8 recursion[PERF_NR_CONTEXTS ]; }; static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
kernel/events/internal.h +2 -2
@@ -208,7 +208,7 @@ arch_perf_out_copy_user(void *dst, const void *src, unsigned long n) DEFINE_OUTPUT_COPY(__output_copy_user, arch_perf_out_copy_user) -static inline int get_recursion_context (int *recursion) +static inline int get_recursion_context (u8 *recursion) {unsigned char rctx =interrupt_context_level ();
@@ -221,7 +221,7 @@ static inline int get_recursion_context(int *recursion)return rctx; } -static inline void put_recursion_context (int *recursion,int rctx) +static inline void put_recursion_context (u8 *recursion,int rctx) {barrier (); recursion[rctx]--;
PATCH v5 5/7 perf: Move swevent_htable::recursion into task_struct.
In reply to Sebastian Andrzej SiewiorThe swevent_htable::recursion counter is used to avoid creating an swevent while an event is processed to avoid recursion. The counter is per-CPU and preemption must be disabled to have a stable counter. perf_pending_task() disables preemption to access the counter and then signal. This is problematic on PREEMPT_RT because sending a signal uses a spinlock_t which must not be acquired in atomic on PREEMPT_RT because it becomes a sleeping lock.
The atomic context can be avoided by moving the counter into the task_struct. There is a 4 byte hole between futex_state (usually always on) and the following perf pointer (perf_event_ctxp). After the recursion lost some weight it fits perfectly.
Move swevent_htable::recursion into task_struct.
--- include/linux/perf_event.h | 6 ------ include/linux/sched.h | 7 +++++++ kernel/events/core.c | 13 +++---------- kernel/events/internal.h | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-)
include/linux/perf_event.h +0 -6
@@ -970,12 +970,6 @@ struct perf_event_context {local_t nr_pending; }; -/* -* Number of contexts where an event can trigger: -* task, softirq, hardirq, nmi. -*/ -#define PERF_NR_CONTEXTS 4 -struct perf_cpu_pmu_context {struct perf_event_pmu_context epc;struct perf_event_pmu_context *task_epc;
include/linux/sched.h +7 -0
@@ -734,6 +734,12 @@ enum perf_event_task_context { perf_nr_task_contexts, }; +/* +* Number of contexts where an event can trigger: +* task, softirq, hardirq, nmi. +*/ +#define PERF_NR_CONTEXTS 4 +struct wake_q_node {struct wake_q_node *next; };
@@ -1256,6 +1262,7 @@ struct task_struct {unsigned int futex_state;#endif #ifdef CONFIG_PERF_EVENTS +u8 perf_recursion[PERF_NR_CONTEXTS ];struct perf_event_context *perf_event_ctxp;struct mutex perf_event_mutex;struct list_head perf_event_list;
kernel/events/core.c +3 -10
@@ -9774,11 +9774,7 @@ struct swevent_htable {struct swevent_hlist *swevent_hlist;struct mutex hlist_mutex;int hlist_refcount; - -/* Recursion avoidance in each contexts */ -u8 recursion[PERF_NR_CONTEXTS ]; }; - static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);/*
@@ -9976,17 +9972,13 @@ DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);int perf_swevent_get_recursion_context (void ) { -struct swevent_htable *swhash =this_cpu_ptr (&swevent_htable); - -return get_recursion_context (swhash->recursion); +return get_recursion_context (current->perf_recursion); }EXPORT_SYMBOL_GPL (perf_swevent_get_recursion_context);void perf_swevent_put_recursion_context (int rctx) { -struct swevent_htable *swhash =this_cpu_ptr (&swevent_htable); - -put_recursion_context (swhash->recursion, rctx); +put_recursion_context (current->perf_recursion, rctx); }void ___perf_sw_event (u32 event_id,u64 nr,struct pt_regs *regs,u64 addr)
@@ -13653,6 +13645,7 @@ int perf_event_init_task(struct task_struct *child, u64 clone_flags) {int ret; +memset (child->perf_recursion,0 ,sizeof (child->perf_recursion)); child->perf_event_ctxp =NULL ;mutex_init (&child->perf_event_mutex);INIT_LIST_HEAD (&child->perf_event_list);
kernel/events/internal.h +1 -1
@@ -221,7 +221,7 @@ static inline int get_recursion_context(u8 *recursion)return rctx; } -static inline void put_recursion_context (u8 *recursion,int rctx) +static inline void put_recursion_context (u8 *recursion,unsigned char rctx) {barrier (); recursion[rctx]--;
Signature · 1 line
2.45.2
[tip: perf/core] perf: Move swevent_htable::recursion into task_struct.
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 0d40a6d83e3e6751f1107ba33587262d937c969f Gitweb: https://git.kernel.org/tip/0d40a6d83e3e6751f1107ba33587262d937c969f Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 04 Jul 2024 19:03:39 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 09 Jul 2024 13:26:36 +02:00
perf: Move swevent_htable::recursion into task_struct.
The swevent_htable::recursion counter is used to avoid creating an swevent while an event is processed to avoid recursion. The counter is per-CPU and preemption must be disabled to have a stable counter. perf_pending_task() disables preemption to access the counter and then signal. This is problematic on PREEMPT_RT because sending a signal uses a spinlock_t which must not be acquired in atomic on PREEMPT_RT because it becomes a sleeping lock.
The atomic context can be avoided by moving the counter into the task_struct. There is a 4 byte hole between futex_state (usually always on) and the following perf pointer (perf_event_ctxp). After the recursion lost some weight it fits perfectly.
Move swevent_htable::recursion into task_struct.
--- include/linux/perf_event.h | 6 ------ include/linux/sched.h | 7 +++++++ kernel/events/core.c | 13 +++---------- kernel/events/internal.h | 2 +- 4 files changed, 11 insertions(+), 17 deletions(-)
include/linux/perf_event.h +0 -6
@@ -970,12 +970,6 @@ struct perf_event_context {local_t nr_pending; }; -/* -* Number of contexts where an event can trigger: -* task, softirq, hardirq, nmi. -*/ -#define PERF_NR_CONTEXTS 4 -struct perf_cpu_pmu_context {struct perf_event_pmu_context epc;struct perf_event_pmu_context *task_epc;
include/linux/sched.h +7 -0
@@ -734,6 +734,12 @@ enum perf_event_task_context { perf_nr_task_contexts, }; +/* +* Number of contexts where an event can trigger: +* task, softirq, hardirq, nmi. +*/ +#define PERF_NR_CONTEXTS 4 +struct wake_q_node {struct wake_q_node *next; };
@@ -1256,6 +1262,7 @@ struct task_struct {unsigned int futex_state;#endif #ifdef CONFIG_PERF_EVENTS +u8 perf_recursion[PERF_NR_CONTEXTS ];struct perf_event_context *perf_event_ctxp;struct mutex perf_event_mutex;struct list_head perf_event_list;
kernel/events/core.c +3 -10
@@ -9763,11 +9763,7 @@ struct swevent_htable {struct swevent_hlist *swevent_hlist;struct mutex hlist_mutex;int hlist_refcount; - -/* Recursion avoidance in each contexts */ -u8 recursion[PERF_NR_CONTEXTS ]; }; - static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);/*
@@ -9965,17 +9961,13 @@ DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);int perf_swevent_get_recursion_context (void ) { -struct swevent_htable *swhash =this_cpu_ptr (&swevent_htable); - -return get_recursion_context (swhash->recursion); +return get_recursion_context (current->perf_recursion); }EXPORT_SYMBOL_GPL (perf_swevent_get_recursion_context);void perf_swevent_put_recursion_context (int rctx) { -struct swevent_htable *swhash =this_cpu_ptr (&swevent_htable); - -put_recursion_context (swhash->recursion, rctx); +put_recursion_context (current->perf_recursion, rctx); }void ___perf_sw_event (u32 event_id,u64 nr,struct pt_regs *regs,u64 addr)
@@ -13642,6 +13634,7 @@ int perf_event_init_task(struct task_struct *child, u64 clone_flags) {int ret; +memset (child->perf_recursion,0 ,sizeof (child->perf_recursion)); child->perf_event_ctxp =NULL ;mutex_init (&child->perf_event_mutex);INIT_LIST_HEAD (&child->perf_event_list);
kernel/events/internal.h +1 -1
@@ -221,7 +221,7 @@ static inline int get_recursion_context(u8 *recursion)return rctx; } -static inline void put_recursion_context (u8 *recursion,int rctx) +static inline void put_recursion_context (u8 *recursion,unsigned char rctx) {barrier (); recursion[rctx]--;
PATCH v5 6/7 perf: Don't disable preemption in perf_pending_task().
In reply to Sebastian Andrzej Siewiorperf_pending_task() is invoked in task context and disables preemption because perf_swevent_get_recursion_context() used to access per-CPU variables. The other reason is to create a RCU read section while accessing the perf_event.
The recursion counter is no longer a per-CPU accounter so disabling preemption is no longer required. The RCU section is needed and must be created explicit.
Replace the preemption-disable section with a explicit RCU-read section.
--- kernel/events/core.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/events/core.c +5 -6
@@ -5208,10 +5208,9 @@ static void perf_pending_task_sync(struct perf_event *event) }/* -* All accesses related to the event are within the same -* non-preemptible section in perf_pending_task(). The RCU -* grace period before the event is freed will make sure all -* those accesses are complete by then. +* All accesses related to the event are within the same RCU section in +* perf_pending_task(). The RCU grace period before the event is freed +* will make sure all those accesses are complete by then. */ rcuwait_wait_event (&event->pending_work_wait, !event->pending_work,TASK_UNINTERRUPTIBLE ); }
@@ -6842,7 +6841,7 @@ static void perf_pending_task(struct callback_head *head)* critical section as the ->pending_work reset. See comment in * perf_pending_task_sync(). */ -preempt_disable_notrace (); +rcu_read_lock ();/* * If we 'fail' here, that's OK, it means recursion is already disabled * and we won't recurse 'further'.
@@ -6855,10 +6854,10 @@ static void perf_pending_task(struct callback_head *head)local_dec (&event->ctx->nr_pending);rcuwait_wake_up (&event->pending_work_wait); } +rcu_read_unlock ();if (rctx >=0 )perf_swevent_put_recursion_context (rctx); -preempt_enable_notrace (); }#ifdef CONFIG_GUEST_PERF_EVENTS
Signature · 1 line
2.45.2
[tip: perf/core] perf: Don't disable preemption in perf_pending_task().
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 16b9569df9d2ab07eeee075cb7895e9d3e08e8f0 Gitweb: https://git.kernel.org/tip/16b9569df9d2ab07eeee075cb7895e9d3e08e8f0 Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 04 Jul 2024 19:03:40 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 09 Jul 2024 13:26:36 +02:00
perf: Don't disable preemption in perf_pending_task().
perf_pending_task() is invoked in task context and disables preemption because perf_swevent_get_recursion_context() used to access per-CPU variables. The other reason is to create a RCU read section while accessing the perf_event.
The recursion counter is no longer a per-CPU accounter so disabling preemption is no longer required. The RCU section is needed and must be created explicit.
Replace the preemption-disable section with a explicit RCU-read section.
--- kernel/events/core.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/events/core.c +5 -6
@@ -5208,10 +5208,9 @@ static void perf_pending_task_sync(struct perf_event *event) }/* -* All accesses related to the event are within the same -* non-preemptible section in perf_pending_task(). The RCU -* grace period before the event is freed will make sure all -* those accesses are complete by then. +* All accesses related to the event are within the same RCU section in +* perf_pending_task(). The RCU grace period before the event is freed +* will make sure all those accesses are complete by then. */ rcuwait_wait_event (&event->pending_work_wait, !event->pending_work,TASK_UNINTERRUPTIBLE ); }
@@ -6831,7 +6830,7 @@ static void perf_pending_task(struct callback_head *head)* critical section as the ->pending_work reset. See comment in * perf_pending_task_sync(). */ -preempt_disable_notrace (); +rcu_read_lock ();/* * If we 'fail' here, that's OK, it means recursion is already disabled * and we won't recurse 'further'.
@@ -6844,10 +6843,10 @@ static void perf_pending_task(struct callback_head *head)local_dec (&event->ctx->nr_pending);rcuwait_wake_up (&event->pending_work_wait); } +rcu_read_unlock ();if (rctx >=0 )perf_swevent_put_recursion_context (rctx); -preempt_enable_notrace (); }#ifdef CONFIG_GUEST_PERF_EVENTS
PATCH v5 7/7 perf: Split __perf_pending_irq() out of perf_pending_irq()
In reply to Sebastian Andrzej Siewiorperf_pending_irq() invokes perf_event_wakeup() and __perf_pending_irq(). The former is in charge of waking any tasks which waits to be woken up while the latter disables perf-events.
The irq_work perf_pending_irq(), while this an irq_work, the callback is invoked in thread context on PREEMPT_RT. This is needed because all the waking functions (wake_up_all(), kill_fasync()) acquire sleep locks which must not be used with disabled interrupts.
Disabling events, as done by __perf_pending_irq(), expects a hardirq context and disabled interrupts. This requirement is not fulfilled on PREEMPT_RT.
Split functionality based on perf_event::pending_disable into irq_work named `pending_disable_irq' and invoke it in hardirq context on PREEMPT_RT. Rename the split out callback to perf_pending_disable().
--- include/linux/perf_event.h | 1 + kernel/events/core.c | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-)
include/linux/perf_event.h +1 -0
@@ -783,6 +783,7 @@ struct perf_event {unsigned int pending_disable;unsigned long pending_addr;/* SIGTRAP */ struct irq_work pending_irq; +struct irq_work pending_disable_irq;struct callback_head pending_task;unsigned int pending_work;struct rcuwait pending_work_wait;
kernel/events/core.c +22 -7
@@ -2451,7 +2451,7 @@ static void __perf_event_disable(struct perf_event *event,* hold the top-level event's child_mutex, so any descendant that * goes to exit will block in perf_event_exit_event(). * -* When called from perf_pending_irq it's OK because event->ctx +* When called from perf_pending_disable it's OK because event->ctx * is the current context on this CPU and preemption is disabled, * hence we can't get into perf_event_task_sched_out for this context. */
@@ -2491,7 +2491,7 @@ EXPORT_SYMBOL_GPL(perf_event_disable);void perf_event_disable_inatomic (struct perf_event *event) { event->pending_disable =1 ; -irq_work_queue (&event->pending_irq); +irq_work_queue (&event->pending_disable_irq); }#define MAX_INTERRUPTS (~0ULL)
@@ -5218,6 +5218,7 @@ static void perf_pending_task_sync(struct perf_event *event)static void _free_event (struct perf_event *event) {irq_work_sync (&event->pending_irq); +irq_work_sync (&event->pending_disable_irq);perf_pending_task_sync (event);unaccount_event (event);
@@ -6760,7 +6761,7 @@ static void perf_sigtrap(struct perf_event *event)/* * Deliver the pending work in-event-context or follow the context. */ -static void __perf_pending_irq (struct perf_event *event) +static void __perf_pending_disable (struct perf_event *event) {int cpu =READ_ONCE (event->oncpu);
@@ -6798,11 +6799,26 @@ static void __perf_pending_irq(struct perf_event *event)* irq_work_queue(); // FAILS * * irq_work_run() -* perf_pending_irq() +* perf_pending_disable() * * But the event runs on CPU-B and wants disabling there. */ -irq_work_queue_on (&event->pending_irq, cpu); +irq_work_queue_on (&event->pending_disable_irq, cpu); +} + +static void perf_pending_disable (struct irq_work *entry) +{ + struct perf_event *event = container_of(entry, struct perf_event, pending_disable_irq); +int rctx; + +/* +* If we 'fail' here, that's OK, it means recursion is already disabled +* and we won't recurse 'further'. +*/ + rctx =perf_swevent_get_recursion_context (); +__perf_pending_disable (event); +if (rctx >=0 ) +perf_swevent_put_recursion_context (rctx); }static void perf_pending_irq (struct irq_work *entry)
@@ -6825,8 +6841,6 @@ static void perf_pending_irq(struct irq_work *entry)perf_event_wakeup (event); } -__perf_pending_irq (event); -if (rctx >=0 )perf_swevent_put_recursion_context (rctx); }
@@ -11967,6 +11981,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,init_waitqueue_head (&event->waitq);init_irq_work (&event->pending_irq, perf_pending_irq); + event->pending_disable_irq =IRQ_WORK_INIT_HARD (perf_pending_disable);init_task_work (&event->pending_task, perf_pending_task);rcuwait_init (&event->pending_work_wait);
Signature · 1 line
2.45.2
[tip: perf/core] perf: Split __perf_pending_irq() out of perf_pending_irq()
The following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: 2b84def990d388bed4afe4f21ae383a01991046c Gitweb: https://git.kernel.org/tip/2b84def990d388bed4afe4f21ae383a01991046c Author: Sebastian Andrzej Siewior <[email protected]> AuthorDate: Thu, 04 Jul 2024 19:03:41 +02:00 Committer: Peter Zijlstra <[email protected]> CommitterDate: Tue, 09 Jul 2024 13:26:37 +02:00
perf: Split __perf_pending_irq() out of perf_pending_irq()
perf_pending_irq() invokes perf_event_wakeup() and __perf_pending_irq(). The former is in charge of waking any tasks which waits to be woken up while the latter disables perf-events.
The irq_work perf_pending_irq(), while this an irq_work, the callback is invoked in thread context on PREEMPT_RT. This is needed because all the waking functions (wake_up_all(), kill_fasync()) acquire sleep locks which must not be used with disabled interrupts.
Disabling events, as done by __perf_pending_irq(), expects a hardirq context and disabled interrupts. This requirement is not fulfilled on PREEMPT_RT.
Split functionality based on perf_event::pending_disable into irq_work named `pending_disable_irq' and invoke it in hardirq context on PREEMPT_RT. Rename the split out callback to perf_pending_disable().
--- include/linux/perf_event.h | 1 + kernel/events/core.c | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-)
include/linux/perf_event.h +1 -0
@@ -783,6 +783,7 @@ struct perf_event {unsigned int pending_disable;unsigned long pending_addr;/* SIGTRAP */ struct irq_work pending_irq; +struct irq_work pending_disable_irq;struct callback_head pending_task;unsigned int pending_work;struct rcuwait pending_work_wait;
kernel/events/core.c +22 -7
@@ -2451,7 +2451,7 @@ static void __perf_event_disable(struct perf_event *event,* hold the top-level event's child_mutex, so any descendant that * goes to exit will block in perf_event_exit_event(). * -* When called from perf_pending_irq it's OK because event->ctx +* When called from perf_pending_disable it's OK because event->ctx * is the current context on this CPU and preemption is disabled, * hence we can't get into perf_event_task_sched_out for this context. */
@@ -2491,7 +2491,7 @@ EXPORT_SYMBOL_GPL(perf_event_disable);void perf_event_disable_inatomic (struct perf_event *event) { event->pending_disable =1 ; -irq_work_queue (&event->pending_irq); +irq_work_queue (&event->pending_disable_irq); }#define MAX_INTERRUPTS (~0ULL)
@@ -5218,6 +5218,7 @@ static void perf_pending_task_sync(struct perf_event *event)static void _free_event (struct perf_event *event) {irq_work_sync (&event->pending_irq); +irq_work_sync (&event->pending_disable_irq);perf_pending_task_sync (event);unaccount_event (event);
@@ -6749,7 +6750,7 @@ static void perf_sigtrap(struct perf_event *event)/* * Deliver the pending work in-event-context or follow the context. */ -static void __perf_pending_irq (struct perf_event *event) +static void __perf_pending_disable (struct perf_event *event) {int cpu =READ_ONCE (event->oncpu);
@@ -6787,11 +6788,26 @@ static void __perf_pending_irq(struct perf_event *event)* irq_work_queue(); // FAILS * * irq_work_run() -* perf_pending_irq() +* perf_pending_disable() * * But the event runs on CPU-B and wants disabling there. */ -irq_work_queue_on (&event->pending_irq, cpu); +irq_work_queue_on (&event->pending_disable_irq, cpu); +} + +static void perf_pending_disable (struct irq_work *entry) +{ + struct perf_event *event = container_of(entry, struct perf_event, pending_disable_irq); +int rctx; + +/* +* If we 'fail' here, that's OK, it means recursion is already disabled +* and we won't recurse 'further'. +*/ + rctx =perf_swevent_get_recursion_context (); +__perf_pending_disable (event); +if (rctx >=0 ) +perf_swevent_put_recursion_context (rctx); }static void perf_pending_irq (struct irq_work *entry)
@@ -6814,8 +6830,6 @@ static void perf_pending_irq(struct irq_work *entry)perf_event_wakeup (event); } -__perf_pending_irq (event); -if (rctx >=0 )perf_swevent_put_recursion_context (rctx); }
@@ -11956,6 +11970,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,init_waitqueue_head (&event->waitq);init_irq_work (&event->pending_irq, perf_pending_irq); + event->pending_disable_irq =IRQ_WORK_INIT_HARD (perf_pending_disable);init_task_work (&event->pending_task, perf_pending_task);rcuwait_init (&event->pending_work_wait);
Arnaldo Carvalho de Melo
maintainerQuoted 7 lines · Sebastian Andrzej Siewior
Hi,
Arnaldo reported that "perf test sigtrap" fails on
PREEMPT_RT. Sending the signal gets delayed untilevent_sched_out()which then usestask_work_add()for its delivery. This breaks onPREEMPT_RTbecause the signal is delivered with disabled preemption.
Quoted 6 lines
While looking at this, I also stumbled upon
__perf_pending_irq()which requires disabled interrupts but this is not the case onPREEMPT_RT.This series aim to address both issues while not introducing a new issue at the same time ;)
Any testing is appreciated.
Were should I apply this patch? The v4 series was applied to linux-rt-devel/linux-6.10.y-rt IIRC
- Arnaldo
Quoted 41 lines
v4…v5: https://lore.kernel.org/all/[email protected]/
- AddTWA_NMI_CURRENTas notify mode fortask_work_add()and use it. PeterZ pointed out that the current version is not NMI safe.v3…v4: https://lore.kernel.org/all/[email protected]/
- Rebased on top of Frederic's series(https://lore.kernel.org/all/[email protected])- Frederick pointed out that
perf_pending_task()needs toperf_swevent_get_recursion_context()in order not to recurse if something within perf_swevent_.*_recursion_context() triggers a software event. To address this, the counters have been moved to thetask_struct(#3 + #4) andpreemt_disable()has been replaced with a RCU-read lock (#5).
- The remaning logic same that means the event is pushed to task-work instead of delivering from IRQ-work. The series was tested withremove_on_execas suggested by Marco Elver: OnPREEMPT_RTa single invocation passes, 100 parallel invocations report (for some) unexpected SIGTRAPs and timeouts. This also observed on !RT (without the series) with a higher task-count.v2…v3: https://lore.kernel.org/all/[email protected]/
- Marco suggested to add a few comments
- Added a comment to__perf_event_overflow()to explain whyirq_workis raised in thein_nmi()case.
- Added a comment toperf_event_exit_event()to explain why the pending event is deleted.v1…v2: https://lore.kernel.org/all/[email protected]/
- Marco pointed me to the testsuite that showed two problems:
- Delayedtask_workfrom NMI / missing events.
Fixed by triggering dummyirq_workto enforce an interrupt for the exit-to-userland path which checkstask_work
- Increased ref-count on clean up/ during exec.
Mostly addressed by the former change. There is still a window if the NMI occurs duringexecve(). This is addressed by removing thetask_workbeforefree_event().
The testsuite (remove_on_exec) fails sometimes if the event/ SIGTRAP is sent before the sighandler is installed.Sebastian
Arnaldo Carvalho de Melo
maintainerQuoted 11 lines · Arnaldo Carvalho de Melo
Quoted 10 lines · Sebastian Andrzej Siewior
Hi,
Arnaldo reported that "perf test sigtrap" fails on
PREEMPT_RT. Sending the signal gets delayed untilevent_sched_out()which then usestask_work_add()for its delivery. This breaks onPREEMPT_RTbecause the signal is delivered with disabled preemption.While looking at this, I also stumbled upon
__perf_pending_irq()which requires disabled interrupts but this is not the case onPREEMPT_RT.
Quoted 6 lines
Quoted 3 lines
This series aim to address both issues while not introducing a new issue at the same time ;)
Any testing is appreciated.Were should I apply this patch? The v4 series was applied to linux-rt-devel/linux-6.10.y-rt IIRC
Looking at linux-rt-devel/linux-6.10.y-rt I see:
commit ca8b27c51f0962f8fb59e5acb23e0af791fb5c04
Author: Sebastian Andrzej Siewior <[email protected]> Date: Tue Jun 25 10:56:32 2024 +0200
perf: Update the perf series
This is an all-in-one patch integrating the following changes:
- Merging Frederick's "Fix leaked sigtrap events" series as of v4 which is a dependency.
- Update the "perf test sigtrap" fixup to v4 as posted.
But I think that is v4, right?
- Arnaldo
Quoted 42 lines
Quoted 42 lines
v4…v5: https://lore.kernel.org/all/[email protected]/
- AddTWA_NMI_CURRENTas notify mode fortask_work_add()and use it. PeterZ pointed out that the current version is not NMI safe.v3…v4: https://lore.kernel.org/all/[email protected]/
- Rebased on top of Frederic's series(https://lore.kernel.org/all/[email protected])- Frederick pointed out that
perf_pending_task()needs toperf_swevent_get_recursion_context()in order not to recurse if something within perf_swevent_.*_recursion_context() triggers a software event. To address this, the counters have been moved to thetask_struct(#3 + #4) andpreemt_disable()has been replaced with a RCU-read lock (#5).
- The remaning logic same that means the event is pushed to task-work instead of delivering from IRQ-work. The series was tested withremove_on_execas suggested by Marco Elver: OnPREEMPT_RTa single invocation passes, 100 parallel invocations report (for some) unexpected SIGTRAPs and timeouts. This also observed on !RT (without the series) with a higher task-count.v2…v3: https://lore.kernel.org/all/[email protected]/
- Marco suggested to add a few comments
- Added a comment to__perf_event_overflow()to explain whyirq_workis raised in thein_nmi()case.
- Added a comment toperf_event_exit_event()to explain why the pending event is deleted.v1…v2: https://lore.kernel.org/all/[email protected]/
- Marco pointed me to the testsuite that showed two problems:
- Delayedtask_workfrom NMI / missing events.
Fixed by triggering dummyirq_workto enforce an interrupt for the exit-to-userland path which checkstask_work
- Increased ref-count on clean up/ during exec.
Mostly addressed by the former change. There is still a window if the NMI occurs duringexecve(). This is addressed by removing thetask_workbeforefree_event().
The testsuite (remove_on_exec) fails sometimes if the event/ SIGTRAP is sent before the sighandler is installed.Sebastian
Quoted 2 lines · Arnaldo Carvalho de Melo
Were should I apply this patch? The v4 series was applied to linux-rt-devel/linux-6.10.y-rt IIRC
Revert the v4 series, apply this one. Wait a few hours and there should be a new RT release with this done ;)
Quoted 1 line
- Arnaldo
Sebastian
Peter Zijlstra
maintainerThanks, I'll go queue it for perf/core once the robot gets back to me on the current pile.
Quoted 4 lines · Peter Zijlstra
Thanks, I'll go queue it for perf/core once the robot gets back to me on the current pile.
Thank you.
Sebastian