mm/kwatch: dynamic hardware watchpoints for hunting memory corruption
Subsystems (7)
v2 cover mm/kwatch: dynamic hardware watchpoints for hunting memory corruption
Motivation
==========
The hardest memory corruption bugs are the silent ones: a rogue writer scribbles over a live object through a stale pointer or a race, and the victim crashes in a code path far away from the culprit. Any single developer hits such a bug rarely, but across the kernel's code base and install base they keep arriving, and each one is disproportionately expensive to localize. The question to answer is "who wrote to this object, and from where?", and it is hard to get at with the existing tools:
- The kernel's own reports - an oops on a clobbered pointer, a BUG_ON, a list-corruption warning - fire at the victim's access, not at the corrupting write.
- KASAN/KFENCE catch memory-safety violations: out-of-bounds accesses and use-after-free. But they have a blind spot: a corrupting write can be fully memory-safe - a *valid* pointer, in bounds, to a live object, written just at the wrong time or to the wrong place - and then they stay silent by design. And even for the bugs they can catch, KASAN's rebuild, overhead and redzones change timing and layout enough that racy corruption often no longer reproduces.
- Hardware watchpoints can catch the writer, but they are scarce (four slots per CPU on x86), they watch a fixed address, and registering/releasing one may sleep, so they cannot be managed from atomic context. Each way of using them also has practical
constraints: the in-kernel API means writing a custom debugging patch for each hunt, kgdb needs a debug console and stops the whole machine at every interaction, and perf drives watchpoints from userspace, so the address must be known before the run.
Design
======
KWatch arms a hardware breakpoint on the exact address a function invocation is operating on, for exactly as long as that invocation runs. Four key designs make this work:
1. Hardware breakpoint pool. All breakpoints are preallocated and parked on a harmless dummy variable. Arming re-points one at the target address, using a small "reinstall" operation added to the hw_breakpoint layer; releasing parks it back on the dummy. The pool is managed locklessly and the arming path calls no sleeping API, so a breakpoint can be armed from whatever context the watched function runs in - real NMI excepted - and a hit can fire and be handled in any context.
2. Function-scoped watch window. A kretprobe pair opens the window at function entry - resolving the target address and arming a breakpoint - and closes it on return, releasing the breakpoint. A depth setting picks which level of a recursion opens the window, and hits are validated against the arming task and depth. The window is also what makes the scarce hardware
affordable: every corruption happens within some execution context, so a breakpoint is armed only while that context runs. Global variables can also be watched without a window, in a time-bounded anchor session.
3. Watch expression engine. At each entry it evaluates the configured expression to resolve that invocation's target address. The base can be a function argument, the stack pointer, a symbol or an absolute address; offsets and pointer dereferences chain on top, so heap fields reachable from an argument, globals and stack slots are all expressible.
4. Painless deployment. KWatch is fully self-contained and can be built as a module, loaded only when a corruption hunt needs it. It is just a debugfs entry until a watch is configured; after that only the watched function pays the kprobe cost and the rest of the system runs at full speed, which keeps KWatch usable on busy, highly concurrent systems.
Together: point KWatch at the suspect function and field with a single debugfs line, reproduce the bug, and the tracepoint reports the writer - the writing instruction and its stack trace.
A real case: dummy_hcd
======================
Gadget requests were completing through a clobbered req->complete. Months of KASAN-enabled syzkaller runs produced only downstream symptoms, with no lead on the root cause. Watching the victim field with KWatch:
func_name=usb_gadget_giveback_request watch_expr=arg2+56 \ watch_len=8
caught the writer in the act:
kwatch_hit: KWatch HIT: time=370.399836 ip=memcpy+0xc/0x30
addr=0xffff888109cf5218
=> usb_ep_queue+0xf1/0x3c0
=> raw_process_ep_io+0x5e4/0xd80
=> raw_ioctl+0x251c/0x41c0
=> __se_sys_ioctl+0xfc/0x170
=> do_syscall_64+0x174/0x580
=> entry_SYSCALL_64_after_hwframe+0x77/0x7f
on the same request that crashed an instant later - the crash RIP was the just-written garbage value. Root cause: dummy_queue()'s single shared fifo_req is struct-copied over while dummy_timer() is mid-giveback. A fix based on this diagnosis has been picked up into the USB tree [1] - KWatch's part was answering who clobbers the pointer, and from where.
Series layout
=============
Patches 1-4: a minimal "reinstall" operation for hw_breakpoint. Re-pointing an already-installed breakpoint from a kprobe handler is not possible with the current API (register/unregister may sleep and rebalances constraints); reinstall lets the arch rewrite a slot it already owns, and modify_wide_hw_breakpoint_local() exposes that for the local CPU - cross-CPU propagation is the caller's job (KWatch uses async IPIs). Patch 4 is Masami Hiramatsu's work, carried verbatim from his current wprobe series.
Patches 5-11: KWatch itself, in mm/kwatch/ (patch 7 exports stack_trace_save_regs() for the modular build).
Patches 12-13: KUnit tests and documentation.
Testing
=======
The dummy_hcd hunt above exercised the function-window path against a live reproducer. Global watching, session auto-stop and the KUnit parser suite were verified end to end under QEMU on x86_64. Both KWATCH=y and KWATCH=m build.
arm64
=====
This RFC deliberately targets x86 only. On arm64 the watchpoint exception fires before the access, so the arch must single-step over hits, and today it only does that for the default overflow handler. Rather than hardcoding a KWatch hook into arm64 core code, I plan a follow-up that adds a generic way for in-kernel breakpoint consumers to request stepping, and arm64 support on top of it (a prototype exists).
Changes in v2
=============
Addressing Steven Rostedt's review and the Sashiko AI review findings on v1 [2]:
Tracepoint (Steven Rostedt's review):
- u64 field first, count last, and the stack trace is a dynamic array sized to the captured depth (also fixes leaking the uninitialized tail of the fixed array).
- div_u64 in TP_printk for 32-bit builds.
Scope:
- Drop access_type: KWatch now always watches for writes. It is a corruption localizer, and write is the one type that matters for that job.
hw_breakpoint prerequisites (patches 2-4):
- Restore the compiler barrier before the cpu_dr7 update on the disable path, lost in the install/uninstall unification.
- Always push the AMD DR address mask, so a reinstall from a masked range breakpoint to an exact one clears the stale mask.
- Patch 4 updated to Masami's latest version (wprobe v8): parse into a temporary arch_hw_breakpoint to keep error paths side-effect free, sync the logical bp->attr fields, -EOPNOTSUPP over -ENOSYS.
Runtime fixes:
- Rate-limit only the cross-CPU arm broadcast, never the local re-point; suppressed broadcasts are counted and reported as arm_ipi_suppressed (was: a rate-limited arm skipped the local CPU too, missing the current window entirely).
- The hit handler reports the per-CPU breakpoint address instead of the shared attribute another CPU may be re-pointing concurrently.
- Release the context slot claimed by an entry that races the register/epoch-publish window (slot leak).
- Clamp the context pool size to [256, 32768]: a u16 request above 32768 wrapped roundup_pow_of_two() to zero.
- nmi_rejected and arm_ipi_suppressed are session-scoped now.
- The anchor thread sleeps in TASK_IDLE so a long session no longer inflates loadavg.
- The debugfs read path takes the control mutex against concurrent writes and auto-stop.
Kconfig / tests / docs:
- depends on KPROBES/KRETPROBES/STACKTRACE instead of select.
- KWATCH_KUNIT_TEST now explicitly depends on KWATCH=y; the parser test gets a width-appropriate literal for 32-bit.
- Document that argN bases are only meaningful at function entry, the IPI rate-limit consequences, and best-effort cleanup when a task dies abnormally inside the watched function.
Relationship to KStackWatch
===========================
KWatch grew out of KStackWatch [3], an earlier tool aimed at stack corruption only, and has been substantially reworked since. The hw_breakpoint prerequisites are carried over from that series.
Major changes since the KStackWatch v8 posting:
- The watch expression engine widens the watchable range from the stack to any address expressible via function arguments, globals or stack addresses plus pointer dereference chains.
- The task_struct and scheduler hooks are gone; KWatch is now fully self-contained, as described above.
- A time-bounded anchor session was added for watching global variables (duration=N, auto-stop on expiry).
- Hits are reported through a tracepoint carrying a stack trace instead of printk: safe in NMI-like contexts, and recoverable after a crash (ftrace_dump_on_oops, kdump, pstore).
- Invocations in real NMI(-like) context are detected and rejected, with a visible nmi_rejected counter.
- arm64 support and the auto-canary, profiling and test-module extras were dropped from this series to keep it reviewable.
Feedback on the design, the implementation or the usage is welcome; if you are staring at a corruption that the existing tools cannot attribute, give KWatch a try, or simply Cc me - I am glad to help.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/all/[email protected]/
Jinchao Wang (12):
arch: add HAVE_REINSTALL_HW_BREAKPOINT x86/hw_breakpoint: Unify breakpoint install/uninstall x86/hw_breakpoint: Add arch_reinstall_hw_breakpoint mm/kwatch: add watch expression parser and dereference engine mm/kwatch: add lockless per-task context pool
stacktrace: export stack_trace_save_regs() mm/kwatch: add hardware breakpoint backend mm/kwatch: add probe lifecycle runtime
mm/kwatch: add anchor thread for global watchpoints mm/kwatch: add debugfs control plane
mm/kwatch: add KUnit tests for the watch expression parser Documentation/dev-tools: document KWatch
Masami Hiramatsu (Google) (1):
HWBP: Add modify_wide_hw_breakpoint_local() API
Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kwatch.rst | 207 ++++++++++++++ MAINTAINERS | 8 + arch/Kconfig | 10 + arch/x86/Kconfig | 1 + arch/x86/include/asm/hw_breakpoint.h | 8 + arch/x86/kernel/hw_breakpoint.c | 163 ++++++----- include/linux/hw_breakpoint.h | 6 + include/trace/events/kwatch.h | 68 +++++ kernel/events/hw_breakpoint.c | 43 +++ kernel/stacktrace.c | 2 + mm/Kconfig | 1 + mm/Makefile | 1 + mm/kwatch/.kunitconfig | 9 + mm/kwatch/Kconfig | 28 ++ mm/kwatch/Makefile | 4 + mm/kwatch/anchor.c | 85 ++++++ mm/kwatch/core.c | 324 ++++++++++++++++++++++ mm/kwatch/deref.c | 174 ++++++++++++ mm/kwatch/deref_test.c | 146 ++++++++++ mm/kwatch/hwbp.c | 388 +++++++++++++++++++++++++++ mm/kwatch/kwatch.h | 101 +++++++ mm/kwatch/probe.c | 275 +++++++++++++++++++ mm/kwatch/task_ctx.c | 125 +++++++++ 24 files changed, 2115 insertions(+), 63 deletions(-) create mode 100644 Documentation/dev-tools/kwatch.rst create mode 100644 include/trace/events/kwatch.h create mode 100644 mm/kwatch/.kunitconfig create mode 100644 mm/kwatch/Kconfig create mode 100644 mm/kwatch/Makefile create mode 100644 mm/kwatch/anchor.c create mode 100644 mm/kwatch/core.c create mode 100644 mm/kwatch/deref.c create mode 100644 mm/kwatch/deref_test.c create mode 100644 mm/kwatch/hwbp.c create mode 100644 mm/kwatch/kwatch.h create mode 100644 mm/kwatch/probe.c create mode 100644 mm/kwatch/task_ctx.c
Signature · 1 line
2.53.0
RFC PATCH v2 1/13 arch: add HAVE_REINSTALL_HW_BREAKPOINT
Some architectures can update the address, length or type of an installed hardware breakpoint in place, without releasing and re-reserving its slot. Add an opt-in Kconfig symbol so generic code can offer such an operation on architectures that implement arch_reinstall_hw_breakpoint().
This is a prerequisite for KWatch, which re-points preallocated per-CPU breakpoints from atomic context, where the register/release path (which may sleep and rebalances slot constraints) cannot be used.
--- arch/Kconfig | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/Kconfig +10 -0
@@ -457,6 +457,16 @@ config HAVE_MIXED_BREAKPOINTS_REGS Select this option if your arch implements breakpoints under the latter fashion. +config HAVE_REINSTALL_HW_BREAKPOINT + bool + depends on HAVE_HW_BREAKPOINT + help + Depending on the arch implementation of hardware breakpoints, + some of them are able to update the breakpoint configuration + without release and reserve the hardware breakpoint register. + What configuration is able to update depends on hardware and + software implementation. + config HAVE_USER_RETURN_NOTIFIER bool
Signature · 1 line
2.53.0
RFC PATCH v2 2/13 x86/hw_breakpoint: Unify breakpoint install/uninstall
In reply to Jinchao WangConsolidate breakpoint management to reduce code duplication. The diffstat was misleading, so the stripped code size is compared instead. After refactoring, it is reduced from 11976 bytes to 11448 bytes on my x86_64 system built with clang.
This also makes it easier to introduce arch_reinstall_hw_breakpoint().
In addition, including linux/types.h to fix a missing build dependency.
--- arch/x86/include/asm/hw_breakpoint.h | 6 ++ arch/x86/kernel/hw_breakpoint.c | 153 ++++++++++++++++----------- 2 files changed, 95 insertions(+), 64 deletions(-)
arch/x86/include/asm/hw_breakpoint.h +6 -0
@@ -5,6 +5,7 @@#include <uapi/asm/hw_breakpoint.h> #define __ARCH_HW_BREAKPOINT_H +#include <linux/types.h> /* * The name should probably be something dealt in
@@ -18,6 +19,11 @@ struct arch_hw_breakpoint {u8 type; }; +enum bp_slot_action { +BP_SLOT_ACTION_INSTALL , +BP_SLOT_ACTION_UNINSTALL , +}; +#include <linux/kdebug.h> #include <linux/percpu.h> #include <linux/list.h>
arch/x86/kernel/hw_breakpoint.c +89 -64
192 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 02/13] x86/hw_breakpoint: Unify breakpoint install/uninstall
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Unnecessary write to debug address registers (DR0-DR3) during breakpoint uninstall causes a…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Unnecessary write to debug address registers (DR0-DR3) during breakpoint uninstall causes a performance regression. --
--- Patch [2]: [RFC PATCH v2 02/13] x86/hw_breakpoint: Unify breakpoint install/uninstall --- commit 5b286a3cc2eb7cc8f15c628fb198117f5bc1729d
Author: Jinchao Wang <[email protected]>
x86/hw_breakpoint: Unify breakpoint install/uninstall
This commit consolidates breakpoint management to reduce code duplication by unifying the install and uninstall paths into common management functions. It also includes linux/types.h to fix a missing build dependency.
Quoted 2 lines
arch/x86/kernel/hw_breakpoint.c +0
-0
[ ... ]
arch/x86/kernel/hw_breakpoint.c int decode_dr7(unsigned long dr7, int bpnum, unsigned *len, unsigned *type) · line 134
Quoted 6 lines
arch/x86/kernel/hw_breakpoint.c +6
-0+static void setup_hwbp (struct arch_hw_breakpoint *info,int slot,bool enable) +{ +unsigned long dr7; + +set_debugreg (info->address, slot); +__this_cpu_write (cpu_debugreg[slot], info->address);
[Severity: Medium]
Does this code introduce a performance regression by unconditionally writing to the hardware debug address registers (DR0-DR3) during uninstallation?
Writing to hardware debug registers is an expensive operation that forces pipeline flushes on bare metal and causes VM-exits in virtualized environments. Looking at the old arch_uninstall_hw_breakpoint(), the uninstall path used to only update DR7 and skipped writing the address register.
Now, when a task with a hardware breakpoint is scheduled out, the perf_event subsystem uninstalls the breakpoint which calls into setup_hwbp() with enable=false. This forces an unconditional set_debugreg() on the address register anyway. Could this write be restricted to only happen when the enable flag is true?
Quoted 6 lines
arch/x86/kernel/hw_breakpoint.c +6
-0+ + dr7 =this_cpu_read (cpu_dr7); +if (enable) + dr7 |=encode_dr7 (slot, info->len, info->type); +else + dr7 &= ~__encode_dr7 (slot, info->len, info->type);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
RFC PATCH v2 3/13 x86/hw_breakpoint: Add arch_reinstall_hw_breakpoint
In reply to Jinchao WangThe new arch_reinstall_hw_breakpoint() function can be used in an atomic context, unlike the more expensive free and re-allocation path. This allows callers to efficiently re-establish an existing breakpoint, and x86 advertises the capability via HAVE_REINSTALL_HW_BREAKPOINT.
Since a REINSTALL may change bp_len, setup_hwbp() must clear the slot's stale len/type and enable bits in DR7 before re-encoding:
OR-merging the new encoding over the old one would keep the CPU watching with the stale width (verified in QEMU by reading DR7 after re-arming watch_len=1 over a len8 breakpoint: 0x999906aa merged without the clearing, 0x199906aa with it).
--- arch/x86/Kconfig | 1 + arch/x86/include/asm/hw_breakpoint.h | 2 ++ arch/x86/kernel/hw_breakpoint.c | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-)
arch/x86/Kconfig +1 -0
@@ -246,6 +246,7 @@ config X86 select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS select HAVE_HW_BREAKPOINT + select HAVE_REINSTALL_HW_BREAKPOINT select HAVE_IOREMAP_PROT select HAVE_IRQ_EXIT_ON_IRQ_STACK if X86_64 select HAVE_IRQ_TIME_ACCOUNTING
arch/x86/include/asm/hw_breakpoint.h +2 -0
@@ -21,6 +21,7 @@ struct arch_hw_breakpoint {enum bp_slot_action {BP_SLOT_ACTION_INSTALL , +BP_SLOT_ACTION_REINSTALL ,BP_SLOT_ACTION_UNINSTALL , };
@@ -65,6 +66,7 @@ extern int hw_breakpoint_exceptions_notify(struct notifier_block *unused,int arch_install_hw_breakpoint (struct perf_event *bp); +int arch_reinstall_hw_breakpoint (struct perf_event *bp);void arch_uninstall_hw_breakpoint (struct perf_event *bp);void hw_breakpoint_pmu_read (struct perf_event *bp);void hw_breakpoint_pmu_unthrottle (struct perf_event *bp);
arch/x86/kernel/hw_breakpoint.c +14 -2
@@ -100,6 +100,10 @@ static int manage_bp_slot(struct perf_event *bp, enum bp_slot_action action) old_bp =NULL ; new_bp = bp;break ; +case BP_SLOT_ACTION_REINSTALL : + old_bp = bp; + new_bp = bp; +break ;case BP_SLOT_ACTION_UNINSTALL : old_bp = bp; new_bp =NULL ;
@@ -134,10 +138,13 @@ static void setup_hwbp(struct arch_hw_breakpoint *info, int slot, bool enable)__this_cpu_write (cpu_debugreg[slot], info->address); dr7 =this_cpu_read (cpu_dr7); +/* +* Clear the slot's stale len/type and enable bits first: a REINSTALL +* with a different bp_len would otherwise OR-merge both encodings. +*/ + dr7 &= ~__encode_dr7 (slot,0xf ,0 );if (enable) dr7 |=encode_dr7 (slot, info->len, info->type); -else - dr7 &= ~__encode_dr7 (slot, info->len, info->type);/* * Enabling:
@@ -198,6 +205,11 @@ int arch_install_hw_breakpoint(struct perf_event *bp)return arch_manage_bp (bp,BP_SLOT_ACTION_INSTALL ); } +int arch_reinstall_hw_breakpoint (struct perf_event *bp) +{ +return arch_manage_bp (bp,BP_SLOT_ACTION_REINSTALL ); +} +void arch_uninstall_hw_breakpoint (struct perf_event *bp) {arch_manage_bp (bp,BP_SLOT_ACTION_UNINSTALL );
Signature · 1 line
2.53.0
RFC PATCH v2 4/13 HWBP: Add modify_wide_hw_breakpoint_local() API
In reply to Jinchao WangFrom: "Masami Hiramatsu (Google)" <[email protected]>
Add modify_wide_hw_breakpoint_local() arch-wide interface which allows hwbp users to update watch address on-line. This is available if the arch supports CONFIG_HAVE_REINSTALL_HW_BREAKPOINT.
Note that this allows to change the type only for compatible types, because it does not release and reserve the hwbp slot based on type. For instance, you can not change HW_BREAKPOINT_W to HW_BREAKPOINT_X.
--- include/linux/hw_breakpoint.h | 6 +++++ kernel/events/hw_breakpoint.c | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+)
include/linux/hw_breakpoint.h +6 -0
@@ -81,6 +81,9 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr,perf_overflow_handler_t triggered, void *context); +extern int modify_wide_hw_breakpoint_local (struct perf_event *bp, +struct perf_event_attr *attr); +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);
@@ -124,6 +127,9 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr,perf_overflow_handler_t triggered, void *context) { return NULL; }static inline int +modify_wide_hw_breakpoint_local (struct perf_event *bp, +struct perf_event_attr *attr) {return -EOPNOTSUPP ; } +static inline int register_perf_hw_breakpoint (struct perf_event *bp) {return -ENOSYS ; }static inline void unregister_hw_breakpoint (struct perf_event *bp) { } static inline void
kernel/events/hw_breakpoint.c +43 -0
49 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 04/13] HWBP: Add modify_wide_hw_breakpoint_local() API
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Non-atomic updates to active hardware breakpoint state can cause torn reads in NMI/#DB handlers.…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Non-atomic updates to active hardware breakpoint state can cause torn reads in NMI/#DB handlers.
- [High] The function alters the event's internal software state before verifying if the hardware update succeeds, leaving the state permanently corrupted if the hardware update fails. --
--- Patch [4]: [RFC PATCH v2 04/13] HWBP: Add modify_wide_hw_breakpoint_local() API --- commit 326c7d6d8fc03bc42d6719968b050227e4e25f31
Author: Masami Hiramatsu (Google) <[email protected]>
HWBP: Add modify_wide_hw_breakpoint_local() API
This commit adds modify_wide_hw_breakpoint_local(), an arch-wide interface allowing hardware breakpoint users to update watch addresses on-line for the local CPU. It updates the configuration by reusing the current hardware slot without releasing and reserving it.
Quoted 4 lines
kernel/events/hw_breakpoint.c +0
-0
[ ... ]
Quoted 7 lines
kernel/events/hw_breakpoint.c +7
-0+int modify_wide_hw_breakpoint_local (struct perf_event *bp, +struct perf_event_attr *attr) +{ +struct arch_hw_breakpoint info; +int ret; + +if (find_slot_idx (bp->attr.bp_type) !=find_slot_idx (attr->bp_type))
kernel/events/hw_breakpoint.c void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events) · line 921
Quoted 10 lines
kernel/events/hw_breakpoint.c +10
-0+return -EINVAL ; + + ret =hw_breakpoint_arch_parse (bp, attr, &info); +if (ret) +return ret; + + *counter_arch_bp (bp) = info; + bp->attr.bp_addr = attr->bp_addr; + bp->attr.bp_type = attr->bp_type; + bp->attr.bp_len = attr->bp_len;
[Severity: High]
Can this cause a torn read if an NMI/#DB exception occurs while we are non-atomically updating the breakpoint state?
If an NMI triggers a #DB exception before arch_reinstall_hw_breakpoint() completes, the hw_breakpoint_handler might read the partially updated bp->hw.info and bp->attr while the hardware DR7 register is still armed with the old configuration.
On architectures like x86, reading a torn bp->hw.info.type might cause the handler to misidentify the breakpoint type and fail to set the RF flag, leading to an infinite #DB loop.
kernel/events/hw_breakpoint.c
Quoted 2 lines
kernel/events/hw_breakpoint.c +2
-0+ +return arch_reinstall_hw_breakpoint (bp);
[Severity: High]
Does this leave the software state permanently corrupted if the hardware update fails?
If the PMU rotates the event out just before this function is called, arch_reinstall_hw_breakpoint() might fail and return an error like -EINVAL. This error is propagated to the caller, so the caller assumes the modification failed.
However, the software state (bp->attr and bp->hw.info) was already overwritten above and is not rolled back. When the PMU later rotates the breakpoint back in, will it silently use this uncommitted new configuration?
Quoted 9 lines
kernel/events/hw_breakpoint.c +9
-0+} +#else +int modify_wide_hw_breakpoint_local (struct perf_event *bp, +struct perf_event_attr *attr) +{ +return -EOPNOTSUPP ; +} +#endif +EXPORT_SYMBOL_GPL (modify_wide_hw_breakpoint_local);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
RFC PATCH v2 5/13 mm/kwatch: add watch expression parser and dereference engine
In reply to Jinchao WangKWatch watches a memory address that is only known once the target function runs, e.g. "argument 1, plus 8, dereferenced once". Add the two halves of that mechanism:
- kwatch_deref_parse() turns a textual watch expression {base}[+-off][->[+-]off]... into a kwatch_config: a base anchor (arg1..arg6, stack, an absolute address or - for built-in KWatch - a symbol name) plus a static offset chain.
- kwatch_deref_resolve() replays the chain at probe time against pt_regs. Every pointer load goes through get_kernel_nofault() and the final address must be a kernel address.
Also add the internal kwatch.h header shared by the rest of the series. Nothing is built yet; the Kconfig entry comes with the control plane.
--- mm/kwatch/Makefile | 3 + mm/kwatch/deref.c | 174 +++++++++++++++++++++++++++++++++++++++++++++ mm/kwatch/kwatch.h | 101 ++++++++++++++++++++++++++ 3 files changed, 278 insertions(+) create mode 100644 mm/kwatch/Makefile create mode 100644 mm/kwatch/deref.c create mode 100644 mm/kwatch/kwatch.h
mm/kwatch/Makefile new file +3 -0
@@ -0,0 +1,3 @@ +obj-$(CONFIG_KWATCH) += kwatch.o + +kwatch-y := deref.o
mm/kwatch/deref.c new file +174 -0
174 lines not on this page · raw message
mm/kwatch/kwatch.h new file +101 -0
101 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 05/13] mm/kwatch: add watch expression parser and dereference engine
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The watch expression parser incorrectly shadows valid 4-character symbol names starting with…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The watch expression parser incorrectly shadows valid 4-character symbol names starting with 'arg' (e.g., 'args', 'argv', 'argc', 'arg0'). --
--- Patch [5]: [RFC PATCH v2 05/13] mm/kwatch: add watch expression parser and dereference engine --- commit f73ceb00166a670b40222eb3b2e46fa6d5931306
Author: Jinchao Wang <[email protected]>
mm/kwatch: add watch expression parser and dereference engine
This commit introduces the watch expression parser and dereference engine for KWatch. It allows parsing textual expressions into configurations and resolving them safely against pt_regs at probe time.
Quoted 5 lines
mm/kwatch/deref.c new file +0
-0
[ ... ]
Quoted 2 lines
mm/kwatch/deref.c new file +2
-0+int kwatch_deref_parse (struct kwatch_config *cfg,const char *watch_expr) +{
[ ... ]
mm/kwatch/deref.c line 106
Quoted 10 lines
mm/kwatch/deref.c new file +10
-0+if (!strcmp (dup_expr,"stack" )) { + cfg->base =KWATCH_BASE_STACK ; + }else if (!strncmp (dup_expr,"arg" ,3 ) &&strlen (dup_expr) ==4 ) { +int arg_num; + +if (kstrtoint (dup_expr +3 ,10 , &arg_num) || arg_num <1 || + arg_num >6 ) { + ret = -EINVAL ; + goto out; + }
[Severity: Medium]
Will this logic incorrectly reject valid 4-character symbol names that start with "arg" (like "args", "argv", or "arg0")?
If a user attempts to watch a global symbol like "argv", this block intercepts it because it starts with "arg" and is 4 characters long. However, the call to kstrtoint() will fail on the "v" character, causing the parser to return -EINVAL immediately instead of falling through to the general symbol lookup block.
Quoted 4 lines
mm/kwatch/deref.c new file +4
-0+ cfg->base =KWATCH_BASE_ARG1 + (arg_num -1 ); + }else if (kstrtoul (dup_expr,0 , &cfg->sym_addr) ==0 ) { + cfg->base =KWATCH_BASE_ABS_ADDR ; + }else {
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
RFC PATCH v2 6/13 mm/kwatch: add lockless per-task context pool
In reply to Jinchao WangA task that enters the watched function needs somewhere to keep its window state (nesting depth, owned watchpoint, config epoch). The lookup runs in kprobe and NMI-like contexts, so it must not allocate or take locks.
Use a preallocated open-addressing array hashed by task_struct pointer. Slots are claimed with cmpxchg() and released with smp_store_release(); lookup is a read-only probe sequence. The pool size (max_concurrency) bounds how many tasks can be inside watch windows concurrently; excess tasks are simply not tracked.
--- mm/kwatch/Makefile | 2 +- mm/kwatch/task_ctx.c | 125 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 mm/kwatch/task_ctx.c
mm/kwatch/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_KWATCH) += kwatch.o -kwatch-y := deref.o +kwatch-y := deref.o task_ctx.o
mm/kwatch/task_ctx.c new file +125 -0
125 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 06/13] mm/kwatch: add lockless per-task context pool
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] Concurrent lockless accesses to the global `kwatch_ctx_pool` lack RCU or READ_ONCE protection…
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] Concurrent lockless accesses to the global kwatch_ctx_pool lack RCU or READ_ONCE protection, leading to Use-After-Free or NULL pointer dereferences.
- [High] NMI reentrancy between slot claim and release causes premature release, double free, and cross-task data corruption.
- [High] Hash table lookup degenerates to an unconditional O(N) full array scan on negative lookups, causing massive NMI latency.
- [High] Context slots permanently leak if a task exits abruptly, causing state corruption and ABA problems when the task_struct is reallocated.
- [High] Missing release/acquire memory barriers when dynamically publishing kwatch_ctx_pool expose lockless readers to uninitialized state and UB.
- [Medium] Large contiguous memory allocation with kcalloc() risks frequent failure on fragmented systems.
--
--- Patch [6]: [RFC PATCH v2 06/13] mm/kwatch: add lockless per-task context pool --- commit 48d8ef7e220842e6317681d38fcc2886436cae7d
Author: Jinchao Wang <[email protected]>
mm/kwatch: add lockless per-task context pool
A task that enters the watched function needs somewhere to keep its window state (nesting depth, owned watchpoint, config epoch). The lookup runs in kprobe and NMI-like contexts, so it must not allocate or take locks.
Quoted 5 lines
mm/kwatch/task_ctx.c new file +0
-0
[ ... ]
Quoted 10 lines
mm/kwatch/task_ctx.c new file +9
-0@@ -0,0 +1,125 @@ +int kwatch_tsk_ctx_prealloc (u16 max_concurrency) +{ +if (max_concurrency <KWATCH_CTX_POOL_MIN ) + max_concurrency =KWATCH_CTX_POOL_MIN ; +else if (max_concurrency >KWATCH_CTX_POOL_MAX ) + max_concurrency =KWATCH_CTX_POOL_MAX ; + +/* +* Set the size/mask only when actually allocating, so they can never
mm/kwatch/task_ctx.c line 19
Quoted 10 lines
mm/kwatch/task_ctx.c new file +10
-0+* drift out of sync with the live pool if prealloc is ever called +* again without a matching free. +*/ +if (unlikely (!kwatch_ctx_pool)) { + kwatch_ctx_pool_size =roundup_pow_of_two (max_concurrency); + kwatch_ctx_pool_mask = kwatch_ctx_pool_size -1 ; + + kwatch_ctx_pool =kcalloc (kwatch_ctx_pool_size, +sizeof (struct kwatch_tsk_ctx ), +GFP_KERNEL );
[Severity: Medium]
Does this allocation risk frequent failure on fragmented systems?
In kwatch_tsk_ctx_prealloc(), this uses kcalloc with GFP_KERNEL to allocate up to 32768 elements of struct kwatch_tsk_ctx. Such large physically contiguous allocations are highly prone to failure when memory is fragmented.
Would using kvcalloc be more appropriate here to allow fallback to non-contiguous virtual memory mappings?
[Severity: High]
Are there missing memory barriers here when publishing kwatch_ctx_pool?
In kwatch_tsk_ctx_prealloc(), kwatch_ctx_pool_size is initialized before dynamically assigning kwatch_ctx_pool. Without smp_store_release, the CPU could reorder these stores.
If a concurrent reader in kwatch_tsk_ctx_get() evaluates !kwatch_ctx_pool without smp_load_acquire, could it observe kwatch_ctx_pool != NULL but read a stale value of 0 for kwatch_ctx_pool_size? This would result in ilog2(0), which returns -1, and cause a bitshift overflow when passed to hash_ptr().
Quoted 8 lines
mm/kwatch/task_ctx.c new file +8
-0+if (!kwatch_ctx_pool) +return -ENOMEM ; + } +return 0 ; +} + +struct kwatch_tsk_ctx *kwatch_tsk_ctx_get (bool can_alloc) +{
mm/kwatch/task_ctx.c line 56
Quoted 10 lines
mm/kwatch/task_ctx.c new file +10
-0+int start_idx, i, idx; +struct task_struct *t; + +if (unlikely (!kwatch_ctx_pool)) +return NULL ; + + start_idx =hash_ptr (current,ilog2 (kwatch_ctx_pool_size)); + +for (i =0 ; i < kwatch_ctx_pool_size; i++) { + idx = (start_idx + i) & kwatch_ctx_pool_mask;
[Severity: High]
Does this loop cause significant NMI latency?
In kwatch_tsk_ctx_get(), because the open-addressing hash table nullifies the task pointer instead of using tombstones for deletions, it cannot stop probing when encountering an empty slot.
On every cache miss, such as when a task first triggers a watchpoint, this loop unconditionally iterates kwatch_ctx_pool_size times to ensure the task is not already present. Is it safe to perform an O(N) scan in NMI context?
mm/kwatch/task_ctx.c line 59
Quoted 3 lines
mm/kwatch/task_ctx.c new file +3
-0+ t =READ_ONCE (kwatch_ctx_pool[idx].task); +if (t == current) +return &kwatch_ctx_pool[idx];
[Severity: High]
Could this falsely match a new task to a dead task's leaked slot?
In kwatch_tsk_ctx_get(), if a task exits abruptly due to a fatal signal and bypasses the return probe, it will fail to call kwatch_tsk_ctx_put(), leaving the slot assigned to the dead task_struct pointer.
When the slab allocator reuses the freed memory for a new task, the new task's current pointer would match this leaked slot. Will the new task erroneously inherit stale state and potentially trigger a use-after-free on the dangling watchpoint?
mm/kwatch/task_ctx.c line 70
Quoted 11 lines
mm/kwatch/task_ctx.c new file +11
-0+ } + +if (!can_alloc) +return NULL ; + +for (i =0 ; i < kwatch_ctx_pool_size; i++) { + idx = (start_idx + i) & kwatch_ctx_pool_mask; + t =READ_ONCE (kwatch_ctx_pool[idx].task); +if (!t) { +if (!cmpxchg (&kwatch_ctx_pool[idx].task,NULL , current)) +return &kwatch_ctx_pool[idx];
[Severity: High]
Can an NMI interrupting this path cause a premature release or double free?
In kwatch_tsk_ctx_get(), this lockless claim publishes ownership before the caller can establish safe nesting state. If an NMI interrupts the task immediately after this claim, enters a watch window, and reclaims the same slot because t == current, it will unconditionally call kwatch_tsk_ctx_put() when exiting the watch window.
This releases the slot by writing NULL to ctx->task. When the interrupted task resumes, it will incorrectly assume ownership of a now-freed slot that might be claimed by another CPU. Is there a way to prevent this reentrancy issue?
Quoted 5 lines
mm/kwatch/task_ctx.c new file +5
-0+ } + } + +return NULL ; +}
[ ... ]
mm/kwatch/task_ctx.c line 124
Quoted 4 lines
mm/kwatch/task_ctx.c new file +4
-0+void kwatch_tsk_ctx_free (void ) +{ +kfree (kwatch_ctx_pool); + kwatch_ctx_pool =NULL ;
[Severity: High]
Does this create a use-after-free regression?
In kwatch_tsk_ctx_free(), kfree is called directly on kwatch_ctx_pool followed by assigning it to NULL, without any RCU synchronization.
If an NMI or task concurrently executes kwatch_tsk_ctx_get(), won't it dereference the freed memory? Additionally, since kwatch_tsk_ctx_get() evaluates kwatch_ctx_pool in loops without READ_ONCE, could the compiler reload the global pointer after it is set to NULL, causing a NULL pointer dereference?
Quoted 1 line
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
RFC PATCH v2 7/13 stacktrace: export stack_trace_save_regs()
In reply to Jinchao WangThe other stack_trace_save_*() flavours are exported, but the regs variant is not, so no module can capture a stack trace for a given pt_regs. KWatch, which may be built as a module, uses it to record who wrote to a watched address from the hardware breakpoint handler. Export it like its siblings.
--- kernel/stacktrace.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/stacktrace.c +2 -0
@@ -175,6 +175,7 @@ unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,arch_stack_walk (consume_entry, &c, current, regs);return c.len; } +EXPORT_SYMBOL_GPL (stack_trace_save_regs);#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE /**
@@ -325,6 +326,7 @@ unsigned int stack_trace_save_regs(struct pt_regs *regs, unsigned long *store,save_stack_trace_regs (regs, &trace);return trace.nr_entries; } +EXPORT_SYMBOL_GPL (stack_trace_save_regs);#ifdef CONFIG_HAVE_RELIABLE_STACKTRACE /**
Signature · 1 line
2.53.0
RFC PATCH v2 8/13 mm/kwatch: add hardware breakpoint backend
In reply to Jinchao WangManage a preallocated pool of wide (per-CPU) perf hardware breakpoints. All breakpoints are registered up front against a dummy address; arming a watchpoint only re-points an already-registered event, so the arm path can run from a kprobe handler.
- kwatch_hwbp_get()/put() claim and release pool entries with per-slot cmpxchg, safe for concurrent consumers on any CPU.
- kwatch_hwbp_arm() updates the local CPU synchronously via modify_wide_hw_breakpoint_local() and broadcasts asynchronous IPIs to the other CPUs. Arm-side IPIs are rate-limited per CPU; disarm IPIs are refcounted so an entry is only recycled once every CPU has dropped it.
- Hits are reported through the kwatch:kwatch_hit tracepoint with a short stack trace: the ftrace ring buffer is usable from NMI-like context and survives a subsequent crash, unlike printk.
- A CPU hotplug callback creates/destroys the per-CPU events as CPUs come and go.
--- include/trace/events/kwatch.h | 68 ++++++ mm/kwatch/Makefile | 2 +- mm/kwatch/hwbp.c | 388 ++++++++++++++++++++++++++++++++++ 3 files changed, 457 insertions(+), 1 deletion(-) create mode 100644 include/trace/events/kwatch.h create mode 100644 mm/kwatch/hwbp.c
include/trace/events/kwatch.h new file +68 -0
@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#undef TRACE_SYSTEM +#define TRACE_SYSTEM kwatch + +#if !defined(_TRACE_KWATCH_H) || defined(TRACE_HEADER_MULTI_READ ) +#define _TRACE_KWATCH_H + +#include <linux/tracepoint.h> +#include <linux/ptrace.h> +#include <linux/math64.h> + +#define KWATCH_STACK_DEPTH 8 + +struct trace_seq ; +const char *kwatch_trace_print_stack (struct trace_seq *p, +const unsigned long *stack, +unsigned int nr); + +TRACE_EVENT (kwatch_hit, + TP_PROTO(unsigned long ip, unsigned long sp, unsigned long addr, + u64 time_ns, + unsigned long *stack_entries, unsigned int stack_nr), +TP_ARGS (ip, sp, addr, time_ns, stack_entries, stack_nr), + +TP_STRUCT__entry ( +/* +* time_ns first: u64 leading the entry avoids a 4-byte hole +* after the unsigned-long fields on 32-bit. stack_nr trails +* the fixed fields for the same reason; the stack is a +* dynamic array sized to what was actually captured, so a +* short trace neither wastes space nor leaks uninitialized +* tail slots. +*/ + __field(u64, time_ns) + __field(unsigned long, ip) + __field(unsigned long, sp) + __field(unsigned long, addr) + __dynamic_array(unsigned long, stack, + min_t(unsigned int, stack_nr, KWATCH_STACK_DEPTH)) + __field(unsigned int, stack_nr) + ), + +TP_fast_assign ( + unsigned long *stack = __get_dynamic_array(stack); + unsigned int i; + + __entry->time_ns = time_ns; + __entry->ip = ip; + __entry->sp = sp; + __entry->addr = addr; + __entry->stack_nr = min_t(unsigned int, stack_nr, + KWATCH_STACK_DEPTH); + for (i = 0; i < __entry->stack_nr; i++) + stack[i] = stack_entries[i]; + ), + +TP_printk ("KWatch HIT: time=%llu.%06u ip=%pS addr=0x%lx%s" , +div_u64 (__entry->time_ns,1000000000ULL ), + (unsigned int )(div_u64 (__entry->time_ns,1000ULL ) %1000000ULL ), + (void *)__entry->ip, __entry->addr, +kwatch_trace_print_stack (p,__get_dynamic_array (stack), + __entry->stack_nr)) +); + +#endif /* _TRACE_KWATCH_H */ + +/* This part must be outside protection */ +#include <trace/define_trace.h>
mm/kwatch/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_KWATCH) += kwatch.o -kwatch-y := deref.o task_ctx.o +kwatch-y := deref.o task_ctx.o hwbp.o
mm/kwatch/hwbp.c new file +388 -0
388 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 08/13] mm/kwatch: add hardware breakpoint backend
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] Modifying the trace_seq buffer directly during TP_printk argument evaluation prepends the stack…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Modifying the trace_seq buffer directly during TP_printk argument evaluation prepends the stack trace and corrupts the trace output.
- [High] Race condition in CPU offline callback leading to Use-After-Free in the IPI handler.
- [High] Concurrent read and write of the shared wp->attr structure across CPUs can result in torn reads and invalid hardware breakpoint configuration.
- [High] Untracked csd_arm IPIs can execute after the watchpoint is freed if the target CPU goes offline.
- [High] The wp->arm_tsk pointer is assigned to current without holding a reference, causing a Use-After-Free if the task exits.
- [High] Missing NULL check on register_wide_hw_breakpoint return value causes a kernel panic if hardware breakpoints are unsupported.
- [Medium] Newly onlined CPUs are unconditionally forced to watch the dummy address, missing hits if the watchpoint is currently active.
- [Medium] Per-CPU throttling of arm IPIs causes unrelated watchpoints to be silently ignored on remote CPUs.
- [Medium] A race gap between register_wide_hw_breakpoint and cpuhp_setup_state_nocalls drops watchpoints on concurrently hotplugged CPUs. --
--- Patch [8]: [RFC PATCH v2 08/13] mm/kwatch: add hardware breakpoint backend --- commit 6c7794f69c285985dbf3f0583ef594237690a973
Author: Jinchao Wang <[email protected]>
mm/kwatch: add hardware breakpoint backend
Manage a preallocated pool of wide (per-CPU) perf hardware breakpoints. All breakpoints are registered up front against a dummy address; arming a watchpoint only re-points an already-registered event, so the arm path can run from a kprobe handler.
Quoted 4 lines
include/trace/events/kwatch.h new file +0
-0
[ ... ]
include/trace/events/kwatch.h line 62
Quoted 6 lines
include/trace/events/kwatch.h new file +6
-0+TP_printk ("KWatch HIT: time=%llu.%06u ip=%pS addr=0x%lx%s" , +div_u64 (__entry->time_ns,1000000000ULL ), + (unsigned int )(div_u64 (__entry->time_ns,1000ULL ) %1000000ULL ), + (void *)__entry->ip, __entry->addr, +kwatch_trace_print_stack (p,__get_dynamic_array (stack), + __entry->stack_nr))
[Severity: High]
Does evaluating kwatch_trace_print_stack() as a TP_printk() argument modify the trace_seq buffer prematurely?
Because C argument evaluation order is unsequenced, calling trace_seq_printf() directly inside kwatch_trace_print_stack() mutates the trace_seq buffer p before or while the main format string is processed.
This causes the output to be written out-of-order or truncated.
Quoted 4 lines
mm/kwatch/hwbp.c new file +0
-0
[ ... ]
mm/kwatch/hwbp.c line 64
Quoted 10 lines
mm/kwatch/hwbp.c new file +10
-0+static void kwatch_hwbp_handler (struct perf_event *bp, +struct perf_sample_data *data, +struct pt_regs *regs) +{ +struct kwatch_watchpoint *wp = bp->overflow_handler_context; +unsigned long stack_entries[KWATCH_STACK_DEPTH ]; +unsigned int stack_nr; + +if (!kwatch_probe_validate_hit (regs, wp->arm_tsk)) +return ;
[Severity: High]
Does this code dereference a freed task_struct if the arming task exits?
When armed, wp->arm_tsk is assigned to current without taking a reference count. If the task exits, a subsequent breakpoint hit will dereference a freed task, leading to a use-after-free and kernel panic.
[ ... ]
mm/kwatch/hwbp.c line 82
Quoted 11 lines
mm/kwatch/hwbp.c new file +11
-0+static void kwatch_hwbp_arm_local (void *info) +{ +struct kwatch_watchpoint *wp = info; +struct perf_event *bp; +unsigned long flags; +int cpu, err; + +local_irq_save (flags); + + cpu =smp_processor_id (); + bp =per_cpu (*wp->event, cpu);
[Severity: High]
Can this dereference a freed perf_event or a NULL pointer?
If a concurrent CPU offline event unregisters and frees the perf_event, the per-CPU pointer might be read here before it is nullified in kwatch_hwbp_cpu_offline().
Additionally, if register_wide_hw_breakpoint() returned NULL during initialization (which IS_ERR_PCPU does not catch), this will attempt to access the NULL pointer.
mm/kwatch/hwbp.c line 95
Quoted 13 lines
mm/kwatch/hwbp.c new file +13
-0+ +if (unlikely (!bp)) + goto out; + +kwatch_probe_mute (true); +barrier (); + +/* +* On success this also updates the per-CPU bp->attr, so the hit +* handler reports what THIS CPU is watching instead of the shared +* wp->attr, which another CPU may be re-pointing. +*/ + err =modify_wide_hw_breakpoint_local (bp, &wp->attr);
[Severity: High]
Can this read a torn or partially updated wp->attr structure?
The shared wp->attr structure is modified by kwatch_hwbp_arm() on another CPU without locking or WRITE_ONCE(). Remote CPUs executing this IPI concurrently can observe torn reads, leading to an invalid hardware breakpoint configuration.
[ ... ]
mm/kwatch/hwbp.c line 135
Quoted 10 lines
mm/kwatch/hwbp.c new file +10
-0+static int kwatch_hwbp_cpu_online (unsigned int cpu) +{ +struct perf_event_attr attr; +struct kwatch_watchpoint *wp; +struct perf_event *bp; + +mutex_lock (&kwatch_all_wp_mutex); +list_for_each_entry (wp, &kwatch_all_wp_list, list) { + attr = wp->attr; + attr.bp_addr = (unsigned long )&kwatch_dummy_holder;
[Severity: Medium]
Will this unconditional override cause a newly onlined CPU to miss active hits?
If a watchpoint is currently armed and active, bringing a CPU online forces the new CPU to watch the dummy address instead of the currently watched address.
[ ... ]
mm/kwatch/hwbp.c line 159
Quoted 11 lines
mm/kwatch/hwbp.c new file +11
-0+static int kwatch_hwbp_cpu_offline (unsigned int cpu) +{ +struct kwatch_watchpoint *wp; +struct perf_event *bp; + +mutex_lock (&kwatch_all_wp_mutex); +list_for_each_entry (wp, &kwatch_all_wp_list, list) { + bp =per_cpu (*wp->event, cpu); +if (bp) { +unregister_hw_breakpoint (bp); +per_cpu (*wp->event, cpu) =NULL ;
[Severity: High]
Does unregistering the breakpoint before setting the pointer to NULL create a race condition?
If an IPI broadcast from kwatch_hwbp_arm() occurs concurrently, it can read the stale pointer in kwatch_hwbp_arm_local() and access the freed memory.
Quoted 5 lines
mm/kwatch/hwbp.c new file +5
-0+ } + } +mutex_unlock (&kwatch_all_wp_mutex); +return 0 ; +}
[ ... ]
mm/kwatch/hwbp.c line 198
Quoted 11 lines
mm/kwatch/hwbp.c new file +11
-0+void kwatch_hwbp_arm (struct kwatch_watchpoint *wp,unsigned long addr,u16 len) +{ + static DEFINE_PER_CPU(u64, last_ipi_time); +int cur_cpu; +call_single_data_t *csd; +int cpu; +bool is_disarm = (addr == (unsigned long )&kwatch_dummy_holder); +bool skip_remote = false; + + wp->attr.bp_addr = addr; + wp->attr.bp_len = len;
[Severity: High]
Can mutating these shared fields concurrently cause torn reads?
Because wp->attr is modified without locking or WRITE_ONCE(), remote CPUs processing the arm IPI via modify_wide_hw_breakpoint_local() might read partially updated states.
mm/kwatch/hwbp.c line 201
Quoted 3 lines
mm/kwatch/hwbp.c new file +3
-0+ +if (!is_disarm) + wp->arm_tsk = current;
[Severity: High]
Can this result in a use-after-free on current?
No reference count is taken when storing the current task pointer. If the task exits, a subsequent breakpoint hit will dereference a freed task.
Quoted 14 lines
mm/kwatch/hwbp.c new file +14
-0+ +/* ensure attr update visible to other cpu before sending IPI */ +smp_wmb (); + +atomic_set (&wp->pending_ipis,1 ); + cur_cpu =get_cpu (); + +/* +* Rate-limit only the cross-CPU broadcast, never the local re-point. +* Arming the current CPU is free and must always reflect this window; +* only the remote IPI fan-out is throttled to keep a hot function from +* storming every CPU. A suppressed broadcast means remote CPUs keep +* watching the previous address for that window (a missed remote-CPU +* writer is possible) - hence the visible counter, and why kwatch
mm/kwatch/hwbp.c line 225
Quoted 10 lines
mm/kwatch/hwbp.c new file +10
-0+* targets low-frequency functions. Disarm is never throttled: the +* slot must always be released. +*/ +if (!is_disarm) { +u64 now =local_clock (); +u64 last =this_cpu_read (last_ipi_time); + +if (now - last <KWATCH_ARM_IPI_MIN_INTERVAL_NS ) { +atomic_long_inc (&kwatch_arm_ipi_suppressed); + skip_remote = true;
[Severity: Medium]
Will this per-CPU throttling drop arm IPIs for entirely independent watchpoints?
Since last_ipi_time is stored per-CPU rather than per-watchpoint, arming two distinct watchpoints in rapid succession from the same CPU will cause the second watchpoint to be silently ignored on remote CPUs.
Quoted 5 lines
mm/kwatch/hwbp.c new file +5
-0+ }else { +this_cpu_write (last_ipi_time, now); + } + } +
mm/kwatch/hwbp.c line 240
Quoted 10 lines
mm/kwatch/hwbp.c new file +10
-0+if (!skip_remote) { +for_each_online_cpu (cpu) { +if (cpu == cur_cpu) +continue ; + +if (is_disarm) +atomic_inc (&wp->pending_ipis); + + csd =per_cpu_ptr (is_disarm ? wp->csd_disarm : wp->csd_arm, + cpu);
[Severity: High]
Can leaving arm IPIs untracked result in a use-after-free?
Because only disarm IPIs are tracked by pending_ipis, an active csd_arm IPI might still be queued on an offline-going CPU when kwatch_hwbp_free() is called and frees the csd_arm structure.
[ ... ]
Quoted 16 lines
mm/kwatch/hwbp.c new file +16
-0+void kwatch_hwbp_free (void ) +{ +struct kwatch_watchpoint *wp, *tmp; + + kwatch_wp_nr =0 ; +kfree (kwatch_wp_slots); + kwatch_wp_slots =NULL ; + +if (kwatch_hwbp_cpuhp_state !=CPUHP_INVALID ) { +cpuhp_remove_state_nocalls (kwatch_hwbp_cpuhp_state); + kwatch_hwbp_cpuhp_state =CPUHP_INVALID ; + } + +mutex_lock (&kwatch_all_wp_mutex); +list_for_each_entry_safe (wp, tmp, &kwatch_all_wp_list, list) { +list_del (&wp->list);
mm/kwatch/hwbp.c line 294
Quoted 10 lines
mm/kwatch/hwbp.c new file +10
-0+ +WRITE_ONCE (wp->teardown, true); +atomic_dec (&wp->refcount); + +/* Wait for all async IPIs to finish */ +while (atomic_read (&wp->refcount) >0 ) +cpu_relax (); + +unregister_wide_hw_breakpoint (wp->event); +free_percpu (wp->csd_arm);
[Severity: High]
Is it safe to free csd_arm here while untracked arm IPIs might still be pending?
Since csd_arm IPIs are not tracked by pending_ipis, a delayed or queued arm IPI on an offline-going CPU could access the freed csd_arm.
[ ... ]
Quoted 5 lines
mm/kwatch/hwbp.c new file +5
-0+int kwatch_hwbp_prealloc (u16 max_watch) +{ +struct kwatch_watchpoint *wp; +int success =0 , cpu; +int ret;
[ ... ]
mm/kwatch/hwbp.c line 341
Quoted 4 lines
mm/kwatch/hwbp.c new file +4
-0+ wp->event =register_wide_hw_breakpoint (&wp->attr, + kwatch_hwbp_handler, + wp); +if (IS_ERR_PCPU (wp->event)) {
[Severity: High]
Can wp->event be NULL if hardware breakpoints are unsupported?
register_wide_hw_breakpoint() can return NULL. The IS_ERR_PCPU() check does not catch a NULL return value, causing a kernel panic when kwatch_hwbp_arm_local() later attempts to dereference the NULL pointer.
[Severity: Medium]
Does registering breakpoints before setting up CPU hotplug callbacks create a race gap?
If a CPU comes online between this register_wide_hw_breakpoint() loop and the cpuhp_setup_state_nocalls() below, the new CPU will miss the watchpoint creation.
[ ... ]
Quoted 11 lines
mm/kwatch/hwbp.c new file +11
-0+ ret =cpuhp_setup_state_nocalls (CPUHP_AP_ONLINE_DYN ,"kwatch:online" , + kwatch_hwbp_cpu_online, + kwatch_hwbp_cpu_offline); +if (ret <0 ) { +kwatch_hwbp_free (); +return ret; + } + + kwatch_hwbp_cpuhp_state = ret; +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
RFC PATCH v2 9/13 mm/kwatch: add probe lifecycle runtime
In reply to Jinchao WangOpen and close the watch window with a kretprobe on the target
function: the entry handler tracks per-task nesting depth and, when the configured depth is reached, resolves the watch expression and arms a watchpoint; the exit handler disarms it. An optional kprobe at func_offset arms mid-function instead of at entry.
Functions running in a real NMI(-like) context are rejected once, at function entry, by comparing the NMI nesting count against the one NMI-like layer that int3-based kprobe delivery itself adds; a companion kprobe with a post_handler pins the probe point so jump optimization cannot change the delivery mechanism after it is sampled. Rejections are counted and exposed to the control plane.
A global epoch versioning scheme invalidates stale per-task state across reconfigurations, and a per-CPU mute flag keeps window management quiet while a CPU rewrites its own debug registers.
--- mm/kwatch/Makefile | 2 +- mm/kwatch/probe.c | 275 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 mm/kwatch/probe.c
mm/kwatch/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_KWATCH) += kwatch.o -kwatch-y := deref.o task_ctx.o hwbp.o +kwatch-y := deref.o task_ctx.o hwbp.o probe.o
mm/kwatch/probe.c new file +275 -0
275 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 09/13] mm/kwatch: add probe lifecycle runtime
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [Critical] Use-After-Free and data race in `kwatch_probe_validate_hit()` during session teardown.…
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [Critical] Use-After-Free and data race in kwatch_probe_validate_hit() during session teardown.
- [High] Mid-function offset kprobes incorrectly inherit NMI context rejection, bypassing safety policies.
- [High] Tasks terminating abnormally leak hardware watchpoints and cause pointer ABA vulnerabilities in the lockless context pool.
- [Medium] Race condition causing false NMI context rejections due to uninitialized delivery flag.
- [Low] Unused macro definition TRAMPOLINE_CHECK_DEPTH.
--
--- Patch [9]: [RFC PATCH v2 09/13] mm/kwatch: add probe lifecycle runtime --- commit 4c4a554ca5428e6d0dbf3c8593ac5647b9e7d585
Author: Jinchao Wang <[email protected]>
Subject: mm/kwatch: add probe lifecycle runtime
This commit introduces the probe lifecycle runtime for the watch window using a kretprobe. It handles function entry and exit, tracks nesting depth to manage hardware watchpoints, and includes logic for NMI rejection and epoch versioning.
Quoted 6 lines
mm/kwatch/probe.c new file +0
-0
mm/kwatch/probe.c line 10
Quoted 10 lines
mm/kwatch/probe.c new file +10
-0@@ -0,0 +1,275 @@ +// SPDX-License-Identifier: GPL-2.0 +#include <linux/atomic.h> +#include <linux/kprobes.h> +#include <linux/kallsyms.h> +#include <linux/percpu.h> +#include <linux/preempt.h> +#include <linux/sched.h> + +#include "kwatch.h" +#define TRAMPOLINE_CHECK_DEPTH 16
[Severity: Low]
Is this macro used anywhere in the codebase?
[ ... ]
mm/kwatch/probe.c line 51
Quoted 11 lines
mm/kwatch/probe.c new file +11
-0+static void kwatch_pin_post_handler (struct kprobe *p,struct pt_regs *regs, +unsigned long flags) +{ +/* a post_handler pins the probepoint: no jump optimization */ +} + +bool kwatch_probe_validate_hit (struct pt_regs *regs, +struct task_struct *arm_tsk) +{ +struct kwatch_tsk_ctx *ctx =kwatch_tsk_ctx_get (false); +const struct kwatch_config *cfg = kwatch_probe_ctx.cfg;
[Severity: Critical]
If a session is stopped, kwatch_probe_stop() resets the epoch but does not synchronously disarm hardware watchpoints for tasks still executing inside the probed function.
Could a leftover watchpoint firing later cause this handler path to locklessly dereference a freed cfg pointer?
Additionally, if a new session starts concurrently, kwatch_probe_start() clears the context via memset:
kwatch_probe_start() {
...
memset(&kwatch_probe_ctx, 0, sizeof(kwatch_probe_ctx));
...
}Might this result in a torn or null pointer dereference here if a hardware watchpoint fires during the memset?
[ ... ]
Quoted 7 lines
mm/kwatch/probe.c new file +7
-0+static int kwatch_activate_handler (struct kprobe *p,struct pt_regs *regs) +{ +struct kwatch_tsk_ctx *ctx =kwatch_tsk_ctx_get (false); +unsigned long watch_addr; +u16 watch_len; + +if (unlikely (!ctx))
mm/kwatch/probe.c line 142
Quoted 10 lines
mm/kwatch/probe.c new file +10
-0+return 0 ; + +if (unlikely (kwatch_probe_is_muted ())) +return 0 ; + +if (unlikely (!kwatch_tsk_ctx_check (KWATCH_PROBE_POSITION_ACTIVE ))) +return 0 ; + +if (ctx->depth != kwatch_probe_ctx.cfg->depth +1 || ctx->wp) +return 0 ;
[Severity: High]
Could an NMI interrupt the probed function after the outer invocation has already incremented ctx->depth, and then recursively call the same probed function?
Since kwatch_lifecycle_entry() deliberately skips the ctx->depth increment for rejected NMIs, would this mid-function offset kprobe falsely pass the check by reading the outer context's depth and incorrectly arm the hardware watchpoint using NMI register state?
[ ... ]
mm/kwatch/probe.c line 166
Quoted 12 lines
mm/kwatch/probe.c new file +12
-0+static int kwatch_lifecycle_entry (struct kretprobe_instance *ri, +struct pt_regs *regs) +{ +/* +* Single policy point: the target function's context is judged once +* here. A rejected invocation never increments depth, so the offset +* kprobe path inherits the verdict through the depth check. +*/ +if (unlikely (kwatch_probed_ctx_in_nmi (kwatch_probe_ctx.rp_via_int3))) { +atomic_long_inc (&kwatch_nmi_rejected); +return 1 ;/* NMI context is unsupported: no window, no return hook */ + }
[Severity: Medium]
Is there a race window here where rp_via_int3 is read before being initialized?
kwatch_probe_start() registers the kretprobe, which enables this function to intercept calls across all CPUs, but it initializes rp_via_int3 slightly after:
kwatch_probe_start() {
...
ret = register_kretprobe(&kwatch_probe_ctx.rp);
if (ret < 0) {
...
}
kwatch_probe_ctx.rp_via_int3 = !kprobe_ftrace(&kwatch_probe_ctx.rp.kp);
...
}If another CPU hits this probe before rp_via_int3 is set, might it evaluate as false and misclassify a normal trap context as an NMI if the architecture uses int3 for delivery?
[ ... ]
mm/kwatch/probe.c line 180
Quoted 4 lines
mm/kwatch/probe.c new file +4
-0+static int kwatch_lifecycle_exit (struct kretprobe_instance *ri, +struct pt_regs *regs) +{ +struct kwatch_tsk_ctx *ctx =kwatch_tsk_ctx_get (false);
[Severity: High]
If a task is killed by a fatal signal (like SIGKILL) or calls do_exit() directly while inside the probed function, it bypasses this kretprobe exit handler.
Does this permanently leak the hardware watchpoint assigned to ctx->wp?
Furthermore, kwatch_tsk_ctx_get() matches raw pointers for the active task:
mm/kwatch/task_ctx.c:kwatch_tsk_ctx_get() {
...
t = READ_ONCE(kwatch_ctx_pool[idx].task);
if (t == current)
return &kwatch_ctx_pool[idx];
...
}If the slab allocator reuses the dead task's task_struct memory address for a new task, will the new task erroneously inherit the corrupted state and leaked watchpoint from the pool?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
RFC PATCH v2 10/13 mm/kwatch: add anchor thread for global watchpoints
In reply to Jinchao WangGlobal variables have no function whose execution can bound the watch window. Provide one: a kernel thread sleeps for the configured duration inside a dedicated noinline function, kwatch_global_anchor(), and the probe runtime hooks that function like any other target.
When the duration expires the thread schedules a work item that tears the session down; the expired flag is cleared under the control-plane mutex so a stale work item from a previous session cannot stop a new one.
--- mm/kwatch/Makefile | 2 +- mm/kwatch/anchor.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 mm/kwatch/anchor.c
mm/kwatch/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_KWATCH) += kwatch.o -kwatch-y := deref.o task_ctx.o hwbp.o probe.o +kwatch-y := deref.o task_ctx.o hwbp.o probe.o anchor.o
mm/kwatch/anchor.c new file +85 -0
85 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 10/13] mm/kwatch: add anchor thread for global watchpoints
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] `kwatch_anchor_start()` overwrites `kwatch_anchor_tsk` without checking if a thread is already…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] kwatch_anchor_start() overwrites kwatch_anchor_tsk without checking if a thread is already running, permanently leaking the previous kernel thread.
- [Low] Undefined reference to kwatch_auto_stop causing a linker error.
--
--- Patch [10]: [RFC PATCH v2 10/13] mm/kwatch: add anchor thread for global watchpoints --- commit a3eb869f890aaa4760c373cf20d5f98126ae7d23
Author: Jinchao Wang <[email protected]>
mm/kwatch: add anchor thread for global watchpoints
This patch adds a kernel thread that sleeps for a configured duration to bound the watch window for global variables. When the duration expires, the thread schedules a work item to tear down the session.
Quoted 5 lines
mm/kwatch/anchor.c new file +0
-0
[ ... ]
mm/kwatch/anchor.c line 29
Quoted 4 lines
mm/kwatch/anchor.c new file +4
-0+static void kwatch_auto_stop_handler (struct work_struct *work) +{ +kwatch_auto_stop (); +}
[Severity: Low]
Does this cause a linker error? kwatch_auto_stop() is called here, but it appears to be undefined in the tree, which would result in a build failure when CONFIG_KWATCH=y is enabled.
[ ... ]
mm/kwatch/anchor.c line 65
Quoted 6 lines
mm/kwatch/anchor.c new file +6
-0+int kwatch_anchor_start (u16 duration) +{ + kwatch_anchor_tsk =kthread_run (kwatch_anchor_thread_fn, + (void *)(unsigned long )duration, +"kwatch_anchor" ); +if (IS_ERR (kwatch_anchor_tsk)) {
[Severity: Medium]
What happens if this function is called while an anchor thread is already running? The global kwatch_anchor_tsk pointer is unconditionally overwritten here without verifying if a previous thread is active or stopping it.
Since the leaked thread loops waiting for kthread_should_stop() on a lost task pointer, could this permanently leak the previous thread's task_struct and stack?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
RFC PATCH v2 11/13 mm/kwatch: add debugfs control plane
In reply to Jinchao WangWire the pieces together behind a single debugfs file, /sys/kernel/debug/kwatch/config. Writing a key=value configuration string stops any active session and starts a new one; reading shows the active configuration and the nmi_rejected counter. An open-count guard keeps the file single-open and a mutex serializes start/stop/auto-stop against each other.
Add the Kconfig entry and hook mm/kwatch into the mm build. KWatch can be built in or as a module; symbol-name watch expressions need the built-in flavour (kallsyms_lookup_name is not exported).
--- MAINTAINERS | 8 ++ mm/Kconfig | 1 + mm/Makefile | 1 + mm/kwatch/Kconfig | 16 +++ mm/kwatch/Makefile | 2 +- mm/kwatch/core.c | 324 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 351 insertions(+), 1 deletion(-) create mode 100644 mm/kwatch/Kconfig create mode 100644 mm/kwatch/core.c
MAINTAINERS +8 -0
@@ -14578,6 +14578,14 @@ S: Supported T: git git://git.kernel.org/pub/scm/virt/kvm/kvm.git F: arch/x86/kvm/xen.* +KWATCH +M: Jinchao Wang <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/dev-tools/kwatch.rst +F: include/trace/events/kwatch.h +F: mm/kwatch/ + L3MDEV M: David Ahern <[email protected]> L: [email protected]
mm/Kconfig +1 -0
@@ -1510,5 +1510,6 @@ config LAZY_MMU_MODE_KUNIT_TEST If unsure, say N. source "mm/damon/Kconfig" +source "mm/kwatch/Kconfig" endmenu
mm/Makefile +1 -0
@@ -92,6 +92,7 @@ obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_KFENCE) += kfence/ obj-$(CONFIG_KMSAN) += kmsan/ +obj-$(CONFIG_KWATCH) += kwatch/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_FAIL_PAGE_ALLOC) += fail_page_alloc.o obj-$(CONFIG_MEMTEST) += memtest.o
mm/kwatch/Kconfig new file +16 -0
@@ -0,0 +1,16 @@ +config KWATCH + tristate "Kernel Watch Framework" + depends on PERF_EVENTS && HAVE_HW_BREAKPOINT && DEBUG_FS + depends on HAVE_REINSTALL_HW_BREAKPOINT + depends on KPROBES && KRETPROBES + depends on STACKTRACE + help + A generalized hardware-assisted memory monitor utility. + It provides a low-overhead, real-time trigger mechanism to monitor + kernel memory safely in atomic contexts using hardware breakpoints. + + KWatch is designed to catch silent memory corruptions, stack + overwrites, and complex Heisenbugs by synchronously trapping the + exact instruction causing the illegal access. + + If unsure, say N.
mm/kwatch/Makefile +1 -1
@@ -1,3 +1,3 @@ obj-$(CONFIG_KWATCH) += kwatch.o -kwatch-y := deref.o task_ctx.o hwbp.o probe.o anchor.o +kwatch-y := core.o deref.o task_ctx.o hwbp.o probe.o anchor.o
mm/kwatch/core.c new file +324 -0
324 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 11/13] mm/kwatch: add debugfs control plane
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Critical] Use-After-Free in kwatch context pool due to delayed kretprobe exit handlers --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Critical] Use-After-Free in kwatch context pool due to delayed kretprobe exit handlers --
--- Patch [11]: [RFC PATCH v2 11/13] mm/kwatch: add debugfs control plane --- commit 7abbeb0e0111172be94e0f025440e5d06358c7a1
Author: Jinchao Wang <[email protected]>
mm/kwatch: add debugfs control plane
Wire the pieces together behind a single debugfs file. Add the Kconfig entry and hook mm/kwatch into the mm build.
Quoted 5 lines
mm/kwatch/core.c new file +0
-0
[ ... ]
Quoted 8 lines
mm/kwatch/core.c new file +7
-0@@ -0,0 +1,324 @@ +static void kwatch_stop_watching (void ) +{ + watching_active = false; + +kwatch_anchor_stop (); +/* after kthread_stop: the dead thread cannot re-mark expiry */ +kwatch_anchor_clear_expired ();
mm/kwatch/core.c line 17
Quoted 10 lines
mm/kwatch/core.c new file +10
-0+ +kwatch_probe_stop (); +synchronize_rcu (); +kwatch_tsk_ctx_release_wps (); +/* +* Waits for disarm IPIs and unregisters breakpoints: no #DB can +* reach the ctx pool once this returns. +*/ +kwatch_hwbp_free (); +kwatch_tsk_ctx_free ();
[Severity: Critical]
Is it possible for a sleeping task to access the context pool after it gets freed here?
If a task enters a watched function that can sleep, it triggers the kretprobe entry handler and goes to sleep. When the user stops watching, this code unregisters the probe and calls synchronize_rcu(). Since the sleeping task is not in an RCU read-side critical section, synchronize_rcu() can return without waiting for the task to wake up.
The kwatch_ctx_pool is then freed. When the sleeping task eventually wakes up and returns from the watched function, its kretprobe exit trampoline executes kwatch_lifecycle_exit(). That exit handler calls kwatch_tsk_ctx_get(), which dereferences the freed pool, resulting in a use-after-free regression.
Quoted 1 line
mm/kwatch/core.c new file +1
-0+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
RFC PATCH v2 12/13 mm/kwatch: add KUnit tests for the watch expression parser
In reply to Jinchao WangCover base anchors (stack, argN, absolute address), positive and negative offsets, dereference chains, and rejection of malformed expressions (missing offsets, bad argument index, junk offsets).
--- mm/kwatch/.kunitconfig | 9 +++ mm/kwatch/Kconfig | 12 ++++ mm/kwatch/Makefile | 1 + mm/kwatch/deref_test.c | 146 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) create mode 100644 mm/kwatch/.kunitconfig create mode 100644 mm/kwatch/deref_test.c
mm/kwatch/.kunitconfig new file +9 -0
@@ -0,0 +1,9 @@ +CONFIG_KUNIT=y +CONFIG_KWATCH=y +CONFIG_KWATCH_KUNIT_TEST=y +CONFIG_PERF_EVENTS=y +CONFIG_HAVE_HW_BREAKPOINT=y +CONFIG_HAVE_REINSTALL_HW_BREAKPOINT=y +CONFIG_KPROBES=y +CONFIG_KRETPROBES=y +CONFIG_PRINTK=y
mm/kwatch/Kconfig +12 -0
@@ -14,3 +14,15 @@ config KWATCH exact instruction causing the illegal access. If unsure, say N. + +config KWATCH_KUNIT_TEST + bool "KUnit tests for KWatch" if !KUNIT_ALL_TESTS + # Built into the kwatch module, so it must be y; a bool cannot be + # enabled when KWATCH is a module (KWATCH=m would force it off). + depends on KWATCH=y && KUNIT + default KUNIT_ALL_TESTS + help + Enable KUnit tests for the KWatch kernel module. + This suite tests the core parsing logic, the pointer-chasing + finite state machine, and edge cases involving complex watchpoint + expressions. If unsure, say N.
mm/kwatch/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-$(CONFIG_KWATCH) += kwatch.o kwatch-y := core.o deref.o task_ctx.o hwbp.o probe.o anchor.o +kwatch-$(CONFIG_KWATCH_KUNIT_TEST) += deref_test.o
mm/kwatch/deref_test.c new file +146 -0
146 lines not on this page · raw message
Signature · 1 line
2.53.0
RFC PATCH v2 13/13 Documentation/dev-tools: document KWatch
In reply to Jinchao WangDescribe what KWatch is for, how it compares with KASAN and KFENCE, the debugfs configuration interface, the watch expression syntax, how to read hits from the trace buffer (including after a crash), and the current limitations.
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kwatch.rst | 207 +++++++++++++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 Documentation/dev-tools/kwatch.rst
Documentation/dev-tools/index.rst +1 -0
@@ -30,6 +30,7 @@ Documentation/process/debugging/index.rst ubsan kmemleak kcsan + kwatch lkmm/index kfence kselftest
Documentation/dev-tools/kwatch.rst new file +207 -0
207 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [RFC PATCH v2 13/13] Documentation/dev-tools: document KWatch
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Race condition during CPU offline leads to spurious WARN_ONCE and potential Use-After-Free.…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Race condition during CPU offline leads to spurious WARN_ONCE and potential Use-After-Free.
- [High] KWatch blindly filters out hits from the armed task's callees and local interrupts.
- [Medium] Missing Kconfig dependency on TRACING/TRACEPOINTS.
--
--- Patch [13]: [RFC PATCH v2 13/13] Documentation/dev-tools: document KWatch --- commit 2276ea12010818a130be33709ca8b2c8928c26a9
Author: Jinchao Wang <[email protected]>
Documentation/dev-tools: document KWatch
Describe what KWatch is for, how it compares with KASAN and KFENCE, the debugfs configuration interface, the watch expression syntax, how to read hits from the trace buffer (including after a crash), and the current limitations.
mm/kwatch/Kconfig line 6
Quoted 12 lines
mm/kwatch/Kconfig new file +6
-0@@ -0,0 +1,28 @@ +config KWATCH + tristate "Kernel Watch Framework" + depends on PERF_EVENTS && HAVE_HW_BREAKPOINT && DEBUG_FS + depends on HAVE_REINSTALL_HW_BREAKPOINT + depends on KPROBES && KRETPROBES + depends on STACKTRACE
[Severity: Medium]
Should this configuration depend on or select TRACING or TRACEPOINTS?
Since KWatch outputs successful memory access hits exclusively through the kwatch_hit tracepoint, building without tracepoint support results in KWatch compiling and consuming hardware debug registers, but silently failing to report any hits when memory corruption occurs.
Quoted 2 lines
mm/kwatch/Kconfig new file +2
-0+ help + A generalized hardware-assisted memory monitor utility.
[ ... ]
Quoted 5 lines
mm/kwatch/hwbp.c new file +0
-0
[ ... ]
mm/kwatch/hwbp.c
Quoted 11 lines
mm/kwatch/hwbp.c new file +11
-0+static int kwatch_hwbp_cpu_offline (unsigned int cpu) +{ +struct kwatch_watchpoint *wp; +struct perf_event *bp; + +mutex_lock (&kwatch_all_wp_mutex); +list_for_each_entry (wp, &kwatch_all_wp_list, list) { + bp =per_cpu (*wp->event, cpu); +if (bp) { +unregister_hw_breakpoint (bp); +per_cpu (*wp->event, cpu) =NULL ;
[Severity: High]
Does this sequence open a race condition during CPU offline?
If an asynchronous IPI from kwatch_hwbp_arm() interrupts the offlining CPU exactly between the unregistration and the pointer being cleared:
unregister_hw_breakpoint(bp); /* IPI arrives here */ per_cpu(*wp->event, cpu) = NULL;
The IPI handler in kwatch_hwbp_arm_local() will read the non-NULL, unregistered breakpoint and pass it to modify_wide_hw_breakpoint_local().
Could this access unlinked and potentially freed memory (if the RCU grace period elapses), corrupting debug registers or triggering a spurious WARN_ONCE?
Quoted 5 lines
mm/kwatch/hwbp.c new file +5
-0+ } + } +mutex_unlock (&kwatch_all_wp_mutex); +return 0 ; +}
[ ... ]
Quoted 5 lines
mm/kwatch/probe.c new file +0
-0
[ ... ]
mm/kwatch/probe.c
Quoted 13 lines
mm/kwatch/probe.c new file +13
-0+bool kwatch_probe_validate_hit (struct pt_regs *regs, +struct task_struct *arm_tsk) +{ +struct kwatch_tsk_ctx *ctx =kwatch_tsk_ctx_get (false); +const struct kwatch_config *cfg = kwatch_probe_ctx.cfg; + +if (unlikely (!ctx || !cfg)) +return true; + +if (arm_tsk != current || ctx->depth != cfg->depth +1 ) +return true; + +return false;
[Severity: High]
Does this logic inadvertently filter out genuine corruptions?
Returning false when arm_tsk == current and ctx->depth == cfg->depth + 1 prevents hits from being logged for the current function. However, since depth is only incremented when recursively entering the watched function, does this also silently ignore memory corruptions caused by the watched function's callees, or by any interrupt handler that preempts the watched task on the same CPU?
Quoted 1 line
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Quoted 1 line · Jinchao Wang
24 files changed, 2115 insertions(+), 63 deletions(-)
Reading this, I wonder how many kernel debugging features we need. I don't even think we have a centralized list of them. They all just live in their own silos.
This one really seems like a super specialized tool. It has to be enabled at compile time and specifically aimed at a specific function.
Maybe this should live off on the side for a while. If folks end up actually needing it, they can point their friendly LLM over to its tree.
Borislav Petkov
maintainerQuoted 11 lines · Dave Hansen
Quoted 1 line · Jinchao Wang
24 files changed, 2115 insertions(+), 63 deletions(-)Reading this, I wonder how many kernel debugging features we need. I don't even think we have a centralized list of them. They all just live in their own silos.
This one really seems like a super specialized tool. It has to be enabled at compile time and specifically aimed at a specific function.
Maybe this should live off on the side for a while. If folks end up actually needing it, they can point their friendly LLM over to its tree.
Right, and in my mbox right under this mail thread there's a
https://lore.kernel.org/all/178429796992.157981.3393977217853767915.stgit@devnote2/
which Masami has been blasting almost every day this week which contains two of the patches from this set here...
Looks to me like folks need to sit down and agree on strategy first.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteMasami Hiramatsu (Google)
maintainerQuoted 13 lines · On Fri, 17 Jul 2026 11:10:04 -0700 Borislav Petkov <[email protected]> wrote:
Quoted 11 lines · Dave Hansen
Quoted 1 line · Jinchao Wang
24 files changed, 2115 insertions(+), 63 deletions(-)Reading this, I wonder how many kernel debugging features we need. I don't even think we have a centralized list of them. They all just live in their own silos.
This one really seems like a super specialized tool. It has to be enabled at compile time and specifically aimed at a specific function.
Maybe this should live off on the side for a while. If folks end up actually needing it, they can point their friendly LLM over to its tree.
Quoted 6 lines
Right, and in my mbox right under this mail thread there's a
https://lore.kernel.org/all/178429796992.157981.3393977217853767915.stgit@devnote2/
which Masami has been blasting almost every day this week which contains two of the patches from this set here...
Sorry about that. I tried to fix the reviewed comment from Sashiko, but I should wait for someone's comment.
Quoted 2 lines
Looks to me like folks need to sit down and agree on strategy first.
Yeah, I need to talk with Jinchao.
Like kprobe, wprobe itself integrates watchpoint functionality into the tracing subsystem. Jinchao, I would like to ask you to redesign the kwatch tool based on fprobe and wprobe events in user space? I think that could reduce redundant efforts on the similar feature.
Using fprobe entry and exit event trigger, we can make a Function- scoped watch window. Also, since fprobe and wprobe are having BTF typecast support, you can access to the members of data structures more naturally.
If you are interested, I would be open to having you handle the development of the necessary features for wprobe.
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 17 lines · Masami Hiramatsu (Google)
Quoted 1 line · On Fri, 17 Jul 2026 11:10:04 -0700 Borislav Petkov <[email protected]> wrote:
Looks to me like folks need to sit down and agree on strategy first.
Yeah, I need to talk with Jinchao.
Like kprobe, wprobe itself integrates watchpoint functionality into the tracing subsystem. Jinchao, I would like to ask you to redesign the kwatch tool based on fprobe and wprobe events in user space? I think that could reduce redundant efforts on the similar feature.
Using fprobe entry and exit event trigger, we can make a Function- scoped watch window. Also, since fprobe and wprobe are having BTF typecast support, you can access to the members of data structures more naturally.
Quoted 5 lines
If you are interested, I would be open to having you handle the development of the necessary features for wprobe.
Thank you,
Thank you, Boris and Masami.
Boris is right that we should agree on the overall strategy before moving either series forward, especially since they share the low-level HWBP work. Masami is also right that fprobe and wprobe provide a more appropriate foundation than introducing KWatch as another standalone debugging tool.
After reconsidering the design, I will stop the standalone KWatch series. Instead, I would like to work with Masami on extending wprobe to support the function-scoped dynamic-watchpoint use cases that motivated KWatch.
Masami, thank you for being open to having me develop the necessary wprobe features. I am interested in taking that direction.
---
Thanks,
Jinchao
Masami Hiramatsu (Google)
maintainerQuoted 23 lines · On Tue, 21 Jul 2026 08:36:01 -0400 Jinchao Wang <[email protected]>…
Quoted 17 lines · Masami Hiramatsu (Google)
Quoted 1 line · On Fri, 17 Jul 2026 11:10:04 -0700 Borislav Petkov <[email protected]> wrote:
Looks to me like folks need to sit down and agree on strategy first.
Yeah, I need to talk with Jinchao.
Like kprobe, wprobe itself integrates watchpoint functionality into the tracing subsystem. Jinchao, I would like to ask you to redesign the kwatch tool based on fprobe and wprobe events in user space? I think that could reduce redundant efforts on the similar feature.
Using fprobe entry and exit event trigger, we can make a Function- scoped watch window. Also, since fprobe and wprobe are having BTF typecast support, you can access to the members of data structures more naturally.
Quoted 5 lines
If you are interested, I would be open to having you handle the development of the necessary features for wprobe.
Thank you,
Quoted 7 lines
Thank you, Boris and Masami.
Boris is right that we should agree on the overall strategy before moving either series forward, especially since they share the low-level HWBP work. Masami is also right that fprobe and wprobe provide a more appropriate foundation than introducing KWatch as another standalone debugging tool.
Agreed. I just sent v10 because v9 lacked base-commit and Sashiko did not reviewed. (so it just add a trigger-side BTF support which was dropped in v9).
Think of v10 as the complete set of ideas we wanted to implement with this wprobe.
Quoted 5 lines
After reconsidering the design, I will stop the standalone KWatch series. Instead, I would like to work with Masami on extending wprobe to support the function-scoped dynamic-watchpoint use cases that motivated KWatch.
Thanks Jinchao, I appreciate to work with you on this project.
Quoted 3 lines
Masami, thank you for being open to having me develop the necessary wprobe features. I am interested in taking that direction.
OK, please share your thoughts on watchpoint usage in the kernel.
Basically, I will fix the points raised in the review, but please let me know if you have any other suggestions.
Thank you,
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
Quoted 11 lines · Dave Hansen
Quoted 1 line · Jinchao Wang
24 files changed, 2115 insertions(+), 63 deletions(-)Reading this, I wonder how many kernel debugging features we need. I don't even think we have a centralized list of them. They all just live in their own silos.
This one really seems like a super specialized tool. It has to be enabled at compile time and specifically aimed at a specific function.
Maybe this should live off on the side for a while. If folks end up actually needing it, they can point their friendly LLM over to its tree.
Thanks, Dave.
I agree that adding another standalone in-kernel debugging facility of this size is not the right direction.
The motivation was practical. I have encountered several silent memory corruption bugs for which I wrote similar one-off instrumentation to identify the writer. KWatch was an attempt to make that technique reusable instead of rebuilding it for each bug, and to reduce the same setup cost for other developers facing similar problems.
This approach proved useful for localizing the dummy_hcd corruption:
https://lore.kernel.org/all/[email protected]/
It identified the copy that overwrote req->complete and recorded the writer's call stack. So I believe the debugging use case is valid, but it does not require a separate KWatch implementation.
As discussed in my reply to Masami and Boris, I will stop pursuing the standalone KWatch series and work with Masami on implementing the necessary scoped watchpoint features in wprobe/tracing instead.
Thanks,
Jinchao
Quoted 6 lines · Jinchao Wang
Motivation
==========The hardest memory corruption bugs are the silent ones: a rogue writer scribbles over a live object through a stale pointer or a race, and
The stale/dangling pointer (use-after-free) case is the main one you're after?
Quoted 7 lines
the victim crashes in a code path far away from the culprit. Any single developer hits such a bug rarely, but across the kernel's code base and install base they keep arriving, and each one is disproportionately expensive to localize. The question to answer is "who wrote to this object, and from where?", and it is hard to get at with the existing tools:
Quoted 6 lines
- The kernel's own reports - an oops on a clobbered pointer, a
BUG_ON, a list-corruption warning - fire at the victim's access, not at the corrupting write.
- KASAN/KFENCE catch memory-safety violations: out-of-bounds accesses and use-after-free. But they have a blind spot: a corrupting write can be fully memory-safe - a *valid* pointer, in
Language-semantically speaking, a use-after-free write to recycled memory (be it in heap or stack) is NOT memory-safe. The detectors may not always catch these (KASAN relies on quarantine for that to increase the chances, but yeah, not guaranteed..).
Quoted 5 lines
bounds, to a live object, written just at the wrong time or to the wrong place - and then they stay silent by design. And even for the bugs they can catch, KASAN's rebuild, overhead and redzones change timing and layout enough that racy corruption often no longer reproduces.
There's also tag-based KASAN (KASAN_SW_TAGS or KASAN_HW_TAGS), of which KASAN_SW_TAGS has an x86 version based on LAM (not yet merged though? see https://lwn.net/ml/all/[email protected]/).
One property of tag-based schemes (and any other lock+key detection scheme) is that upon recycling some memory, the tag (or key) to that location changes, and any previous pointer that still has the old tag will fault. The caveat with current pointer-tagging schemes is that entropy is relatively low (4 bit tags).
Quoted 8 lines · Marco Elver
Quoted 6 lines · Jinchao Wang
Motivation
==========The hardest memory corruption bugs are the silent ones: a rogue writer scribbles over a live object through a stale pointer or a race, and
Thanks, Marco. These are good points. My cover letter did not clearly separate two different classes of bugs.
Quoted 1 line
The stale/dangling pointer (use-after-free) case is the main one you're after?
Not exclusively. KASAN already does a good job as a general use-after-free detector. However, a stale-pointer write after the storage has been recycled, which may no longer be reported by Generic KASAN, is one of the cases that the scoped watchpoint can help localize.
The broader target is silent corruption of a known live victim object, including non-UAF cases such as an in-bounds racing write to an object whose allocation is still live.
Quoted 14 lines
Quoted 7 lines
the victim crashes in a code path far away from the culprit. Any single developer hits such a bug rarely, but across the kernel's code base and install base they keep arriving, and each one is disproportionately expensive to localize. The question to answer is "who wrote to this object, and from where?", and it is hard to get at with the existing tools:
Quoted 6 lines
- The kernel's own reports - an oops on a clobbered pointer, a
BUG_ON, a list-corruption warning - fire at the victim's access, not at the corrupting write.
- KASAN/KFENCE catch memory-safety violations: out-of-bounds accesses and use-after-free. But they have a blind spot: a corrupting write can be fully memory-safe - a *valid* pointer, in
Quoted 5 lines
Language-semantically speaking, a use-after-free write to recycled memory (be it in heap or stack) is NOT memory-safe. The detectors may not always catch these (KASAN relies on quarantine for that to increase the chances, but yeah, not guaranteed..).
You are right that a stale-pointer write to recycled storage remains a use-after-free, so "memory-safe" was the wrong term. What I meant was that such a corrupting write may not be detected by Generic KASAN at the point where it occurs. KWatch was intended to use a hardware watchpoint to catch the write and report the actual writer and its call stack.
Quoted 10 lines
Quoted 5 lines
bounds, to a live object, written just at the wrong time or to the wrong place - and then they stay silent by design. And even for the bugs they can catch, KASAN's rebuild, overhead and redzones change timing and layout enough that racy corruption often no longer reproduces.
There's also tag-based KASAN (
KASAN_SW_TAGSorKASAN_HW_TAGS), of whichKASAN_SW_TAGShas an x86 version based on LAM (not yet merged though? see https://lwn.net/ml/all/[email protected]/).
Quoted 5 lines
One property of tag-based schemes (and any other lock+key detection scheme) is that upon recycling some memory, the tag (or key) to that location changes, and any previous pointer that still has the old tag will fault. The caveat with current pointer-tagging schemes is that entropy is relatively low (4 bit tags).
Thanks for pointing this out. I agree that tag-based KASAN can detect a recycled-storage UAF when the new allocation receives a different tag, so it covers part of the intended use case.
There are still cases it cannot detect, such as an in-bounds corrupting write to the same still-live allocation, or a recycled allocation which reuses the same tag. Software tag-based KASAN also still instruments memory accesses and can perturb a timing-sensitive bug enough that it no longer reproduces; hardware tag checking reduces this overhead but is not available everywhere.
The original goal of KWatch, which I now plan to pursue through wprobe, was to provide a simple and targeted interface to hardware watchpoints. It does not instrument every memory access system-wide, so it can have lower overall overhead and less timing perturbation while identifying the actual writer. I see this as complementary to KASAN rather than a replacement for it.
Thanks,
Jinchao