ARM, ARM64, LONGARCH, XTENSA: Delay HW BP notification to task_work()
Subsystems (8)
Sebastian Andrzej Siewior
maintainerPATCH v3 1/1 ARM, ARM64, LONGARCH, XTENSA: Delay HW BP notification to task_work()
Waiman, Luis, Ada reported that HW breakpoints on ARM64 trigger "sleeping while atomic" warnings on PREEMPT_RT. The hardware event is delivered with disabled interrupts and perf intrastrucure expects disabled interrupts while the overflow callback is invoked.
The callback then sends a SIGTRAP signal for which it acquires sighand_struct::siglock, a spinlock_t which becomes a sleeping lock and must not be acquired in atomic context.
Delay the event callback until the return to userland.
Add perf_arch_hwbp_notify(), a generic perf callback which delayes the actual callback invocation to task_work_add() callback. This callback invokes the architecture defines callback arch_hwbp_send_sig(). This requires struct callback_head and the functions require ARCH_NEED_PERF_HW_NOTIF to be defined.
This was reported against ARM64. ARM, LongARCH and Xtensa follow the same pattern are also converted. Xtensa is the only not supporting PREEMPT_RT but now we have all architectures using the same pattern.
---
v2…v3: https://lore.kernel.org/all/[email protected]/
- Add Xtensa for completion
- sashiko complains and wants TWA_SIGNAL instead TWA_RESUME. His argument is that a syscall will trap via get_user() and loop forever instead making progress. This is wrong IMHO. ARM64 will single step over the watchpoint and continue execution. The only downside is that userland will get notified after the syscall completed. So my theory. Using TWA_SIGNAL is worse: Assume we have a watchpoint on UADDR and are in a futex() syscall. The get_user() invocation will trigger the exception, the debug handler will step over and queue a signal. The futex code will notice this and return ERESTARTNOINTR. A signal will be sent, the syscall restarts, traps onto UADDR again, the loop continues. But this should be case now, too…
Now that I look into arch_build_bp_info() and do actual testing I must say arm64 does not support mixed breakpoints. This means there is no breakpoint in kernel on a userland address. \o/
v1…v2: https://lore.kernel.org/all/[email protected]/
- sashiko complained that a memory breakpoint might trigger several times before a signal is sent if the syscall touches the memory (via get_user()) more than once before returning back. This would lead to list corruption in task_work_add(). To handle this, there is now a variable which is set via xchg before task_work_add() and cleared after the signal has been sent.
arch/arm/include/asm/hw_breakpoint.h | 1 + arch/arm/kernel/ptrace.c | 6 ++--- arch/arm64/include/asm/hw_breakpoint.h | 1 + arch/arm64/kernel/ptrace.c | 6 ++--- arch/loongarch/include/asm/hw_breakpoint.h | 1 + arch/loongarch/kernel/ptrace.c | 6 ++--- arch/xtensa/include/asm/hw_breakpoint.h | 1 + arch/xtensa/kernel/ptrace.c | 6 ++--- include/linux/hw_breakpoint.h | 3 +++ include/linux/perf_event.h | 4 ++++ kernel/events/core.c | 26 ++++++++++++++++++++++ 11 files changed, 45 insertions(+), 16 deletions(-)
arch/arm/include/asm/hw_breakpoint.h +1 -0
@@ -7,6 +7,7 @@struct task_struct ; #ifdef CONFIG_HAVE_HW_BREAKPOINT +#define ARCH_NEED_PERF_HW_NOTIF struct arch_hw_breakpoint_ctrl { u32 __reserved : 9,
arch/arm/kernel/ptrace.c +2 -4
@@ -347,9 +347,7 @@ static long ptrace_hbp_idx_to_num(int idx)/* * Handle hitting a HW-breakpoint. */ -static void ptrace_hbptriggered (struct perf_event *bp, -struct perf_sample_data *data, -struct pt_regs *regs) +void arch_hwbp_send_sig (struct perf_event *bp) {struct arch_hw_breakpoint *bkpt =counter_arch_bp (bp);long num;
@@ -424,7 +422,7 @@ static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type) attr.bp_type = type; attr.disabled =1 ; -return register_user_hw_breakpoint (&attr, ptrace_hbptriggered,NULL , +return register_user_hw_breakpoint (&attr, perf_arch_hwbp_notify,NULL , tsk); }
arch/arm64/include/asm/hw_breakpoint.h +1 -0
@@ -124,6 +124,7 @@ extern void hw_breakpoint_pmu_read(struct perf_event *bp);extern int hw_breakpoint_slots (int type);#ifdef CONFIG_HAVE_HW_BREAKPOINT +#define ARCH_NEED_PERF_HW_NOTIF extern void hw_breakpoint_thread_switch (struct task_struct *next);extern void ptrace_hw_copy_thread (struct task_struct *task);#else
arch/arm64/kernel/ptrace.c +2 -4
@@ -168,9 +168,7 @@ void ptrace_disable(struct task_struct *child)/* * Handle hitting a HW-breakpoint. */ -static void ptrace_hbptriggered (struct perf_event *bp, -struct perf_sample_data *data, -struct pt_regs *regs) +void arch_hwbp_send_sig (struct perf_event *bp) {struct arch_hw_breakpoint *bkpt =counter_arch_bp (bp);const char *desc ="Hardware breakpoint trap (ptrace)" ;
@@ -312,7 +310,7 @@ static struct perf_event *ptrace_hbp_create(unsigned int note_type, attr.bp_type = type; attr.disabled =1 ; - bp =register_user_hw_breakpoint (&attr, ptrace_hbptriggered,NULL , tsk); + bp =register_user_hw_breakpoint (&attr, perf_arch_hwbp_notify,NULL , tsk);if (IS_ERR (bp))return bp;
arch/loongarch/include/asm/hw_breakpoint.h +1 -0
@@ -120,6 +120,7 @@ void breakpoint_handler(struct pt_regs *regs);void watchpoint_handler (struct pt_regs *regs);#ifdef CONFIG_HAVE_HW_BREAKPOINT +#define ARCH_NEED_PERF_HW_NOTIF extern void ptrace_hw_copy_thread (struct task_struct *task);extern void hw_breakpoint_thread_switch (struct task_struct *next);#else
arch/loongarch/kernel/ptrace.c +2 -4
@@ -384,9 +384,7 @@ static int lbt_set(struct task_struct *target,/* * Handle hitting a HW-breakpoint. */ -static void ptrace_hbptriggered (struct perf_event *bp, -struct perf_sample_data *data, -struct pt_regs *regs) +void arch_hwbp_send_sig (struct perf_event *bp) {int i;struct arch_hw_breakpoint *bkpt =counter_arch_bp (bp);
@@ -479,7 +477,7 @@ static struct perf_event *ptrace_hbp_create(unsigned int note_type, attr.bp_type = type; attr.disabled =1 ; - bp =register_user_hw_breakpoint (&attr, ptrace_hbptriggered,NULL , tsk); + bp =register_user_hw_breakpoint (&attr, perf_arch_hwbp_notify,NULL , tsk);if (IS_ERR (bp))return bp;
arch/xtensa/include/asm/hw_breakpoint.h +1 -0
@@ -12,6 +12,7 @@#define __ASM_XTENSA_HW_BREAKPOINT_H#ifdef CONFIG_HAVE_HW_BREAKPOINT +#define ARCH_NEED_PERF_HW_NOTIF #include <linux/kdebug.h> #include <linux/types.h>
arch/xtensa/kernel/ptrace.c +2 -4
@@ -361,9 +361,7 @@ static int ptrace_pokeusr(struct task_struct *child, long regno, long val) }#ifdef CONFIG_HAVE_HW_BREAKPOINT -static void ptrace_hbptriggered (struct perf_event *bp, -struct perf_sample_data *data, -struct pt_regs *regs) +void arch_hwbp_send_sig (struct perf_event *bp) {int i;struct arch_hw_breakpoint *bkpt =counter_arch_bp (bp);
@@ -395,7 +393,7 @@ static struct perf_event *ptrace_hbp_create(struct task_struct *tsk, int type) attr.bp_type = type; attr.disabled =1 ; -return register_user_hw_breakpoint (&attr, ptrace_hbptriggered,NULL , +return register_user_hw_breakpoint (&attr, perf_arch_hwbp_notify,NULL , tsk); }
include/linux/hw_breakpoint.h +3 -0
@@ -85,6 +85,9 @@ extern int register_perf_hw_breakpoint(struct perf_event *bp);extern void unregister_hw_breakpoint (struct perf_event *bp);extern void unregister_wide_hw_breakpoint (struct perf_event *__percpu *cpu_events);extern bool hw_breakpoint_is_used (void ); +extern void arch_hwbp_send_sig (struct perf_event *bp); +extern void perf_arch_hwbp_notify (struct perf_event *bp,struct perf_sample_data *data, +struct pt_regs *regs);extern int dbg_reserve_bp_slot (struct perf_event *bp);extern int dbg_release_bp_slot (struct perf_event *bp);
include/linux/perf_event.h +4 -0
@@ -215,6 +215,10 @@ struct hw_perf_event {/* Last sync'ed generation of filters */ unsigned long addr_filters_gen; +#ifdef ARCH_NEED_PERF_HW_NOTIF +struct callback_head arch_hw_notif; +int arch_hw_notif_busy; +#endif /* * hw_perf_event::state flags; used to track the PERF_EF_* state.
kernel/events/core.c +26 -0
@@ -13381,6 +13381,28 @@ static void account_event(struct perf_event *event)account_pmu_sb_event (event); } +#ifdef ARCH_NEED_PERF_HW_NOTIF +static void perf_arch_hwbp_send_sig (struct callback_head *head) +{ +struct perf_event *bp; + + bp = container_of(head, struct perf_event, hw.arch_hw_notif); +arch_hwbp_send_sig (bp); +xchg_relaxed (&bp->hw.arch_hw_notif_busy,0 ); +put_event (bp); +} + +void perf_arch_hwbp_notify (struct perf_event *bp,struct perf_sample_data *data, +struct pt_regs *regs) +{ +if (WARN_ON_ONCE (!atomic_long_inc_not_zero (&bp->refcount))) +return ; +if (xchg_relaxed (&bp->hw.arch_hw_notif_busy,1 ) || +WARN_ON_ONCE (task_work_add (current, &bp->hw.arch_hw_notif,TWA_RESUME ))) +put_event (bp); +} +#endif +/* * Allocate and initialize an event structure */
@@ -13486,6 +13508,10 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, }if (overflow_handler) { +#ifdef ARCH_NEED_PERF_HW_NOTIF +if (overflow_handler == perf_arch_hwbp_notify) +init_task_work (&event->hw.arch_hw_notif, perf_arch_hwbp_send_sig); +#endif event->overflow_handler = overflow_handler; event->overflow_handler_context = context; }else if (is_write_backward (event)){
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] events: delayed or lost SIGTRAP for hardware watchpoints hit in blocking syscalls --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] events: delayed or lost SIGTRAP for hardware watchpoints hit in blocking syscalls --
commit f836aefea872e6050b4535461bc8040c46ae9a47
Author: Sebastian Andrzej Siewior <[email protected]>
ARM, ARM64, LONGARCH, XTENSA: Delay HW BP notification to task_work()
This commit addresses sleeping while atomic warnings on PREEMPT_RT caused by hardware breakpoints on ARM64. It delays the hardware event callback execution until the return to userland by queueing it via task_work_add(). The same pattern is also adopted for ARM, LongARCH, and Xtensa.
Quoted 4 lines
kernel/events/core.c +0
-0
[ ... ]
Quoted 12 lines
kernel/events/core.c +8
-0@@ -13374,6 +13374,28 @@ static void account_event(struct perf_event *event)account_pmu_sb_event (event); } +#ifdef ARCH_NEED_PERF_HW_NOTIF +static void perf_arch_hwbp_send_sig (struct callback_head *head) +{ +struct perf_event *bp; + + bp = container_of(head, struct perf_event, hw.arch_hw_notif); +arch_hwbp_send_sig (bp); +xchg_relaxed (&bp->hw.arch_hw_notif_busy,0 );
kernel/events/core.c static void account_event(struct perf_event *event) · line 13394
Quoted 10 lines
kernel/events/core.c +10
-0+put_event (bp); +} + +void perf_arch_hwbp_notify (struct perf_event *bp,struct perf_sample_data *data, +struct pt_regs *regs) +{ +if (WARN_ON_ONCE (!atomic_long_inc_not_zero (&bp->refcount))) +return ; +if (xchg_relaxed (&bp->hw.arch_hw_notif_busy,1 ) || +WARN_ON_ONCE (task_work_add (current, &bp->hw.arch_hw_notif,TWA_RESUME )))
[Severity: Medium]
Could queuing the task work with TWA_RESUME instead of TWA_SIGNAL cause delayed or lost SIGTRAP deliveries for hardware watchpoints?
If a tracee hits a hardware watchpoint while in a blocking syscall (for example, when the syscall accesses memory via get_user or put_user), using TWA_RESUME will only set TIF_NOTIFY_RESUME.
Because it does not set TIF_SIGPENDING or TIF_NOTIFY_SIGNAL, the syscall will not be interrupted. Does this allow the tracee to hang indefinitely in the blocking syscall instead of aborting the syscall to deliver the synchronous SIGTRAP?
Quoted 4 lines
kernel/events/core.c +4
-0+ put_event(bp); +} +#endif +
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Sebastian,
Sorry for the long wait, finally taking a look at this ! (+Linus Walleij for the arch/arm/ side)
Quoted 5 lines · Sebastian Andrzej Siewior
Waiman, Luis, Ada reported that HW breakpoints on ARM64 trigger "sleeping while atomic" warnings on
PREEMPT_RT. The hardware event is delivered with disabled interrupts and perf intrastrucure expects disabled interrupts while the overflow callback is invoked.
Quoted 8 lines
The callback then sends a SIGTRAP signal for which it acquires
sighand_struct::siglock, aspinlock_twhich becomes a sleeping lock and must not be acquired in atomic context.Delay the event callback until the return to userland.
Addperf_arch_hwbp_notify(), a generic perf callback which delayes the actual callback invocation totask_work_add()callback. This callback invokes the architecture defines callbackarch_hwbp_send_sig().
Typo : [...] architecture defined [...]
Quoted 13 lines
This requires struct
callback_headand the functions requireARCH_NEED_PERF_HW_NOTIFto be defined.This was reported against ARM64. ARM, LongARCH and Xtensa follow the same pattern are also converted. Xtensa is the only not supporting
PREEMPT_RTbut now we have all architectures using the same pattern.Reported-by Luis Claudio R. Goncalves <[email protected]>Reported-by Waiman Long <[email protected]>Signed-off-by Sebastian Andrzej Siewior <[email protected]>---
Quoted 16 lines
v2…v3: https://lore.kernel.org/all/[email protected]/
- Add Xtensa for completion
- sashiko complains and wantsTWA_SIGNALinsteadTWA_RESUME. His argument is that a syscall will trap viaget_user()and loop forever instead making progress. This is wrong IMHO. ARM64 will single step over the watchpoint and continue execution. The only downside is that userland will get notified after the syscall completed. So my theory. UsingTWA_SIGNALis worse: Assume we have a watchpoint on UADDR and are in afutex()syscall. Theget_user()invocation will trigger the exception, the debug handler will step over and queue a signal. The futex code will notice this and return ERESTARTNOINTR. A signal will be sent, the syscall restarts, traps onto UADDR again, the loop continues. But this should be case now, too…
Now that I look intoarch_build_bp_info()and do actual testing I must say arm64 does not support mixed breakpoints. This means there is no breakpoint in kernel on a userland address. \o/
For the record, the comment on task_work_add() reads :
Quoted 6 lines
@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 running in the kernel or userspace.
[...]
@TWA_RESUME work is run only when the task exits the kernel and returns to user mode, or before entering guest mode.
At least on arm64, we are explicitly not preemptible while handling hardware breakpoint/watchpoint exceptions. (See debug_exception_enter() in arch/arm64/kernel/entry-common.c). So TWA_RESUME is definitely the behaviour we want in my opinion.
Quoted 58 lines
v1…v2: https://lore.kernel.org/all/[email protected]/
- sashiko complained that a memory breakpoint might trigger several times before a signal is sent if the syscall touches the memory (viaget_user()) more than once before returning back. This would lead to list corruption intask_work_add(). To handle this, there is now a variable which is set via xchg beforetask_work_add()and cleared after the signal has been sent.arch/arm/include/asm/hw_breakpoint.h | 1 + arch/arm/kernel/ptrace.c | 6 ++--- arch/arm64/include/asm/hw_breakpoint.h | 1 + arch/arm64/kernel/ptrace.c | 6 ++--- arch/loongarch/include/asm/hw_breakpoint.h | 1 + arch/loongarch/kernel/ptrace.c | 6 ++--- arch/xtensa/include/asm/hw_breakpoint.h | 1 + arch/xtensa/kernel/ptrace.c | 6 ++--- include/linux/hw_breakpoint.h | 3 +++ include/linux/perf_event.h | 4 ++++ kernel/events/core.c | 26 ++++++++++++++++++++++ 11 files changed, 45 insertions(+), 16 deletions(-)[...]
include/linux/perf_event.h +4
-0@@ -215,6 +215,10 @@ struct hw_perf_event {/* Last sync'ed generation of filters */ unsigned long addr_filters_gen; +#ifdef ARCH_NEED_PERF_HW_NOTIF +struct callback_head arch_hw_notif; +int arch_hw_notif_busy; +#endif /* * hw_perf_event::state flags; used to track the PERF_EF_* state. kernel/events/core.c +11
-0@@ -13381,6 +13381,28 @@ static void account_event(struct perf_event *event)account_pmu_sb_event (event); } +#ifdef ARCH_NEED_PERF_HW_NOTIF +static void perf_arch_hwbp_send_sig (struct callback_head *head) +{ +struct perf_event *bp; + + bp = container_of(head, struct perf_event, hw.arch_hw_notif); +arch_hwbp_send_sig (bp); +xchg_relaxed (&bp->hw.arch_hw_notif_busy,0 ); +put_event (bp); +} +
kernel/events/core.c static void account_event(struct perf_event *event) · line 13404
Quoted 10 lines
kernel/events/core.c +10
-0+void perf_arch_hwbp_notify (struct perf_event *bp,struct perf_sample_data *data, +struct pt_regs *regs) +{ +if (WARN_ON_ONCE (!atomic_long_inc_not_zero (&bp->refcount))) +return ; +if (xchg_relaxed (&bp->hw.arch_hw_notif_busy,1 ) || +WARN_ON_ONCE (task_work_add (current, &bp->hw.arch_hw_notif,TWA_RESUME ))) +put_event (bp); +} +#endif
I find the function names a bit counter-intuitive, compared to the other arch-specific perf functions.
Given the name perf_arch_..., I would have expected them to be defined in arch code, rather than in the generic perf code.
From what I can see, usually perf functions calling an arch-specific function lack the _arch_ infix of their arch_ counterpart.
I do not know very well what we expect in perf, so I might be off-base, but would calling them perf_hwbp_send_sig() and perf_hwbp_notify() make sense ?
Otherwise, it looks good to me on the arm64 side !
I had a look on the arm side as well, given the debug handling architecture is similar, and I think it is OK on there as well, though I wouldn't mind a more experienced arm review :)
I also tested the patch with pNMI and CONFIG_PREEMPT_RT on arm64 : I can confirm that the atomic sleep warning is gone and everything works as expected !
Thanks a lot for looking into this, combined with[0] the hardware debug handling should be much cleaner ! :)
Kind regards,
Ada
Sebastian Andrzej Siewior
maintainerQuoted 1 line · Ada Couprie Diaz
Hi Sebastian,
Hi Ada,
Quoted 10 lines
I find the function names a bit counter-intuitive, compared to the other arch-specific perf functions.
Given the nameperf_arch_..., I would have expected them to be defined in arch code, rather than in the generic perf code.
From what I can see, usually perf functions calling an arch-specific function lack the_arch_infix of theirarch_counterpart.I do not know very well what we expect in perf, so I might be off-base, but would calling them
perf_hwbp_send_sig()andperf_hwbp_notify()make sense ?
Thanks for looking at this. I renamed them as:
- perf_arch_hwbp_notify() -> perf_hwbp_notify() (generic)
- arch_hwbp_send_sig() -> perf_arch_hwbp_send_sig() (arch specific)
- perf_arch_hwbp_send_sig() -> perf_hwbp_send_sig() (generic)
Quoted 7 lines
Otherwise, it looks good to me on the arm64 side !
I had a look on the arm side as well, given the debug handling architecture is similar, and I think it is OK on there as well, though I wouldn't mind a more experienced arm review :)Reviewed-by Ada Couprie Diaz <[email protected]>
Quoted 8 lines
I also tested the patch with pNMI and
CONFIG_PREEMPT_RTon arm64 : I can confirm that the atomic sleep warning is gone and everything works as expected !Thanks a lot for looking into this, combined with[0] the hardware debug handling should be much cleaner ! :)
great.
Quoted 2 lines
Kind regards,
Ada
Sebastian
Hi Sebastian,
Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 1 line · Ada Couprie Diaz
Hi Sebastian,
Hi Ada,
Quoted 10 lines
I find the function names a bit counter-intuitive, compared to the other arch-specific perf functions.
Given the nameperf_arch_..., I would have expected them to be defined in arch code, rather than in the generic perf code.
From what I can see, usually perf functions calling an arch-specific
function lack the_arch_infix of theirarch_counterpart.I do not know very well what we expect in perf, so I might be off-base, but would calling them
perf_hwbp_send_sig()andperf_hwbp_notify()make sense ?
Quoted 8 lines
Thanks for looking at this. I renamed them as:
-perf_arch_hwbp_notify()->perf_hwbp_notify()(generic)
-arch_hwbp_send_sig()->perf_arch_hwbp_send_sig()(arch specific)
-perf_arch_hwbp_send_sig()->perf_hwbp_send_sig()(generic)[...]
Sebastian
That sounds good to me, thanks !
Kind regards
Ada