[PATCH v1] LoongArch: Only send SIGTRAP for ptrace in do_watch()

From: Tiezhu Yang

Date: Thu Dec 03 2026 - 22:55:40 EST


A previous fix introduced attr.sigtrap checks in breakpoint_handler()
and watchpoint_handler(). At that time, the aim was to avoid forcing
SIGTRAP when attr.sigtrap is 0, but this caused do_watch() to force
SIGTRAP when attr.sigtrap is 1.

Given that the generic perf subsystem already handles attr.sigtrap via
perf_bp_event() and send_sig_perf(), the extra SIGTRAP in do_watch()
breaks perf_event semantics, delivering SI_KERNEL instead of TRAP_PERF,
causing BPF selftests "perf_skip" to fail.

Fix this by removing the redundant attr.sigtrap checks, and only force
SIGTRAP if current->ptrace is set in do_watch().

Before:
$ sudo ./test_progs -t perf_skip
...
handle_sigtrap:FAIL:si_code unexpected si_code: actual 128 != expected 6
...
Summary: 0/0 PASSED, 0 SKIPPED, 1/0 FAILED

After:
$ sudo ./test_progs -t perf_skip
...
Summary: 1/0 PASSED, 0 SKIPPED, 0/0 FAILED

Cc: stable@xxxxxxxxxxxxxxx
Fixes: f087903018e3 ("LoongArch: Only send SIGTRAP signal if necessary in do_watch()")
Signed-off-by: Tiezhu Yang <yangtiezhu@xxxxxxxxxxx>
---
arch/loongarch/include/asm/hw_breakpoint.h | 4 ++--
arch/loongarch/kernel/hw_breakpoint.c | 18 ++----------------
arch/loongarch/kernel/traps.c | 8 +++-----
3 files changed, 7 insertions(+), 23 deletions(-)

diff --git a/arch/loongarch/include/asm/hw_breakpoint.h b/arch/loongarch/include/asm/hw_breakpoint.h
index f4478936787d..c8a9aa264080 100644
--- a/arch/loongarch/include/asm/hw_breakpoint.h
+++ b/arch/loongarch/include/asm/hw_breakpoint.h
@@ -116,8 +116,8 @@ extern void arch_uninstall_hw_breakpoint(struct perf_event *bp);
extern int hw_breakpoint_slots(int type);
extern void hw_breakpoint_pmu_read(struct perf_event *bp);

-bool breakpoint_handler(struct pt_regs *regs);
-bool watchpoint_handler(struct pt_regs *regs);
+void breakpoint_handler(struct pt_regs *regs);
+void watchpoint_handler(struct pt_regs *regs);

#ifdef CONFIG_HAVE_HW_BREAKPOINT
extern void ptrace_hw_copy_thread(struct task_struct *task);
diff --git a/arch/loongarch/kernel/hw_breakpoint.c b/arch/loongarch/kernel/hw_breakpoint.c
index df149ff06dd5..ad677c985a38 100644
--- a/arch/loongarch/kernel/hw_breakpoint.c
+++ b/arch/loongarch/kernel/hw_breakpoint.c
@@ -485,11 +485,10 @@ NOKPROBE_SYMBOL(update_bp_registers);
/*
* Debug exception handlers.
*/
-bool breakpoint_handler(struct pt_regs *regs)
+void breakpoint_handler(struct pt_regs *regs)
{
int i;
unsigned int clear_mask = 0;
- bool need_sigtrap = false;
struct perf_event *bp, **slots;

slots = this_cpu_ptr(bp_on_reg);
@@ -499,27 +498,20 @@ bool breakpoint_handler(struct pt_regs *regs)
bp = slots[i];
if (bp == NULL)
continue;
-
perf_bp_event(bp, regs);
- if (bp->attr.sigtrap)
- need_sigtrap = true;
-
clear_mask |= (0x1 << i);
}
}

if (clear_mask)
csr_write32(clear_mask | CSR_FWPS_SKIP, LOONGARCH_CSR_FWPS);
-
- return need_sigtrap;
}
NOKPROBE_SYMBOL(breakpoint_handler);

-bool watchpoint_handler(struct pt_regs *regs)
+void watchpoint_handler(struct pt_regs *regs)
{
int i;
unsigned int clear_mask = 0;
- bool need_sigtrap = false;
struct perf_event *wp, **slots;

slots = this_cpu_ptr(wp_on_reg);
@@ -529,19 +521,13 @@ bool watchpoint_handler(struct pt_regs *regs)
wp = slots[i];
if (wp == NULL)
continue;
-
perf_bp_event(wp, regs);
- if (wp->attr.sigtrap)
- need_sigtrap = true;
-
clear_mask |= (0x1 << i);
}
}

if (clear_mask)
csr_write32(clear_mask | CSR_MWPS_SKIP, LOONGARCH_CSR_MWPS);
-
- return need_sigtrap;
}
NOKPROBE_SYMBOL(watchpoint_handler);

diff --git a/arch/loongarch/kernel/traps.c b/arch/loongarch/kernel/traps.c
index 790db6c1926e..bef02ca90429 100644
--- a/arch/loongarch/kernel/traps.c
+++ b/arch/loongarch/kernel/traps.c
@@ -821,8 +821,6 @@ asmlinkage void noinstr do_watch(struct pt_regs *regs)
#ifndef CONFIG_HAVE_HW_BREAKPOINT
pr_warn("Hardware watch point handler not implemented!\n");
#else
- bool need_sigtrap = !!current->ptrace;
-
if (kgdb_breakpoint_handler(regs))
goto out;

@@ -859,11 +857,11 @@ asmlinkage void noinstr do_watch(struct pt_regs *regs)
}
}
} else {
- need_sigtrap |= breakpoint_handler(regs);
- need_sigtrap |= watchpoint_handler(regs);
+ breakpoint_handler(regs);
+ watchpoint_handler(regs);
}

- if (need_sigtrap)
+ if (current->ptrace)
force_sig(SIGTRAP);
out:
#endif
--
2.42.0