Skip to content

mm/ksw: Introduce KStackWatch debugging tool

Jinchao Wang linux-arm-kernel linux-doc linux-mm linux-perf-users linux-trace-kernel lkml llvm workflows 8 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v8 Latest27 patches 4 repliesIn review
  2. v723 patches 3 repliesSuperseded
  3. v623 patches 4 repliesSuperseded
Earlier versions · 5
VersionPostedPatchesRepliesStatus
v523 patches6 repliesSuperseded
v421 patches6 repliesSuperseded
v319 patches5 repliesSuperseded
v218 patches15 repliesSuperseded
v117 patches0 repliesSuperseded

Changes in v8 from the cover letter

* Add arm64 support
* Implement hwbp_reinstall() for arm64.
* Use single-step mode as default in ksw_watch_handler().
* Add latency measurements for probe handlers.
* Update configuration options
* Introduce explicit auto_canary parameter.
* Default watch_len to sizeof(unsigned long) when zero.
* Replace panic_on_catch with panic_hit ksw_config option.
* Enable KStackWatch in non-debug builds.
* Limit canary search range to the current stack frame when possible.
* Add automatic architecture detection for test parameters.
* Move kstackwatch.h to include/linux/.
* Relocate Kconfig fragments to the kstackwatch/ directory.

Patches in v8 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/27x86/hw_breakpoint: Unify breakpoint install/uninstallarch/x86/include/asm/hw_breakpoint.h arch/x86/kernel/hw_breakpoint.c+84 -630 replies
2/27x86/hw_breakpoint: Add arch_reinstall_hw_breakpointarch/x86/include/asm/hw_breakpoint.h arch/x86/kernel/hw_breakpoint.c+11 -00 replies
3/27HWBP: Add modify_wide_hw_breakpoint_local() APIarch/Kconfig arch/x86/Kconfig include/linux/hw_breakpoint.h +1 more+54 -00 replies
4/27mm/ksw: add build system supportinclude/linux/kstackwatch.h mm/Kconfig mm/Makefile +5 more+48 -00 replies
5/27mm/ksw: add ksw_config struct and parserinclude/linux/kstackwatch.h mm/kstackwatch/kernel.c+147 -00 replies
6/27mm/ksw: add singleton debugfs interfaceinclude/linux/kstackwatch.h mm/kstackwatch/kernel.c+103 -30 replies
7/27mm/ksw: add HWBP pre-allocationinclude/linux/kstackwatch.h mm/kstackwatch/watch.c+106 -00 replies
8/27mm/ksw: Add atomic watchpoint management apiinclude/linux/kstackwatch.h mm/kstackwatch/watch.c+88 -10 replies
9/27mm/ksw: ignore false positives from exit trampolinesmm/kstackwatch/watch.c+38 -00 replies
10/27mm/ksw: support CPU hotplugmm/kstackwatch/watch.c+52 -00 replies
11/27sched/ksw: add per-task contextinclude/linux/kstackwatch_types.h include/linux/sched.h+19 -00 replies
12/27mm/ksw: add entry kprobe and exit fprobe managementinclude/linux/kstackwatch.h mm/kstackwatch/stack.c+104 -00 replies
13/27mm/ksw: add per-task ctx trackingmm/kstackwatch/stack.c+67 -00 replies
14/27mm/ksw: resolve stack watch addr and lenmm/kstackwatch/stack.c+77 -30 replies
15/27mm/ksw: limit canary search to current stack framemm/kstackwatch/stack.c+21 -80 replies
16/27mm/ksw: manage probe and HWBP lifecycle via procfsmm/kstackwatch/kernel.c+58 -20 replies
17/27mm/ksw: add KSTACKWATCH_PROFILING to measure probe costmm/kstackwatch/Kconfig mm/kstackwatch/stack.c+183 -120 replies
18/27arm64/hw_breakpoint: Add arch_reinstall_hw_breakpointarch/arm64/Kconfig arch/arm64/include/asm/hw_breakpoint.h arch/arm64/kernel/hw_breakpoint.c+7 -00 replies
19/27arm64/hwbp/ksw: integrate KStackWatch handler supportarch/arm64/kernel/hw_breakpoint.c include/linux/kstackwatch.h mm/kstackwatch/watch.c+17 -00 replies
20/27mm/ksw: add self-debug helpersinclude/linux/kstackwatch.h mm/kstackwatch/watch.c+36 -00 replies
21/27mm/ksw: add test moduleinclude/linux/kstackwatch.h mm/kstackwatch/Kconfig mm/kstackwatch/Makefile +2 more+144 -00 replies
22/27mm/ksw: add stack overflow testmm/kstackwatch/test.c+21 -10 replies
23/27mm/ksw: add recursive depth testmm/kstackwatch/test.c+21 -10 replies
24/27mm/ksw: add multi-thread corruption test casesmm/kstackwatch/test.c+185 -10 replies
25/27tools/ksw: add arch-specific test scripttools/kstackwatch/kstackwatch_test.sh+85 -00 replies
26/27docs: add KStackWatch documentDocumentation/dev-tools/index.rst Documentation/dev-tools/kstackwatch.rst+378 -00 replies
27/27MAINTAINERS: add entry for KStackWatchMAINTAINERS+9 -00 replies

Reviews trailers per patch

2 of 27 patches have a Reviewed-by or Acked-by.

PatchMasami Hiramatsu (Google)
1/27R-b
2/27R-b
3/27
4/27
5/27
6/27
7/27
8/27
9/27
10/27
11/27
12/27
13/27
14/27
15/27
16/27
17/27
18/27
19/27
20/27
21/27
22/27
23/27
24/27
25/27
26/27
27/27

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v8 thread

Cover letter [PATCH v8 00/27] mm/ksw: Introduce KStackWatch debugging tool

Earlier this year, I debugged a stack corruption panic that revealed the limitations of existing debugging tools. The bug persisted for 739 days before being fixed (CVE-2025-22036), and my reproduction scenario differed from the CVE report—highlighting how unpredictably these bugs manifest.

The panic call trace:

<4>[89318.486564]  <TASK>
<4>[89318.486570]  dump_stack_lvl+0x48/0x70
<4>[89318.486580]  dump_stack+0x10/0x20
<4>[89318.486586]  panic+0x345/0x3a0
<4>[89318.486596]  ? __blk_flush_plug+0x121/0x130
<4>[89318.486603]  __stack_chk_fail+0x14/0x20
<4>[89318.486612]  __blk_flush_plug+0x121/0x130
...27 other frames omitted
<4>[89318.486824]  ksys_read+0x6b/0xf0
<4>[89318.486829]  __x64_sys_read+0x19/0x30
<4>[89318.486834]  x64_sys_call+0x1ada/0x25c0
<4>[89318.486840]  do_syscall_64+0x7f/0x180
<4>[89318.486847]  ? exc_page_fault+0x94/0x1b0
<4>[89318.486855]  entry_SYSCALL_64_after_hwframe+0x73/0x7b
<4>[89318.486866]  </TASK>

Initially, I enabled KASAN, but the bug did not reproduce. Reviewing the code in __blk_flush_plug(), I found it difficult to trace all logic paths due to indirect function calls through function pointers.

I added canary-locating code to obtain the canary address and value, then inserted extensive debugging code to track canary modifications. I observed the canary being corrupted between two unrelated assignments, indicating corruption by another thread—a silent stack corruption bug.

I then added hardware breakpoint (hwbp) code, but still failed to catch the corruption. After adding PID filters, function parameter filters, and depth filters, I discovered the corruption occurred in end_buffer_read_sync() via atomic_dec(&bh->b_count), where bh->b_count overlapped with __blk_flush_plug()'s canary address. Tracing the bh lifecycle revealed the root cause in exfat_get_block()—a function not even present in the panic call trace.

This bug was later assigned CVE-2025-22036
(https://lore.kernel.org/all/2025041658-CVE-2025-22036-6469@gregkh/).
The vulnerability was introduced in commit 11a347fb6cef (March 13, 2023) and fixed in commit 1bb7ff4204b6 (March 21, 2025)—persisting for 739 days. Notably, my reproduction scenario differed significantly from that described in the CVE report, highlighting how these bugs manifest unpredictably across different workloads.

This experience revealed how notoriously difficult stack corruption bugs are to debug: KASAN cannot reproduce them, call traces are misleading, and the actual culprit often lies outside the visible call chain. Manual instrumentation with hardware breakpoints and filters was effective but extremely time-consuming.

This motivated KStackWatch: automating the debugging workflow I manually performed, making hardware breakpoint-based stack monitoring readily available to all kernel developers facing similar issues.

KStackWatch is a lightweight debugging tool to detect kernel stack corruption in real time. It installs a hardware breakpoint (watchpoint) at a function's specified offset using kprobe.post_handler and removes it in fprobe.exit_handler. This covers the full execution window and reports corruption immediately with time, location, and a call stack.

Beyond automating proven debugging workflows, KStackWatch incorporates robust engineering to handle complex scenarios like context switches, recursion, and concurrent execution, making it suitable for broad debugging use cases.

## Key Features

* Immediate and precise stack corruption detection
* Support for multiple concurrent watchpoints with configurable limits
* Lockless design, usable in any context
* Depth filter for recursive calls
* Low overhead of memory and CPU
* Flexible debugfs configuration with key=val syntax
* Architecture support: x86_64 and arm64
* Auto-canary detection to simplify configuration

## Architecture Support

KStackWatch currently supports x86_64 and arm64. The design is architecture-agnostic, requiring only:
* Hardware breakpoint modification in atomic context

Arm64 support required only ~20 lines of code(patch 18,19). Future ports to other architectures (e.g., riscv) should be straightforward for developers familiar with their hardware breakpoint implementations.

## Performance Impact

Runtime overhead was measured on Intel Core Ultra 5 125H @ 3 GHz running kernel 6.17, using test4 from patch 24:

Type | Time (ns) | Cycles
-----------------------------------------------
entry with watch | 10892 | 32620
entry without watch | 159 | 466
exit with watch | 12541 | 37556
exit without watch | 124 | 369

Comparation with other scenarios:

Mode | CPU Overhead (add) | Memory Overhead (add)
----------------------------+----------------------+-------------------------

Compiled but not enabled    |  None                |  ~20 B per task
Enabled, no function hit    |  None                |  ~few hundred B
Func hit, HWBP not toggled  |  ~140 ns per call    |  None
Func hit, HWBP toggled      |  ~11–12 µs per call  |  None

The overhead is minimal, making KStackWatch suitable for production environments where stack corruption is suspected but kernel rebuilds are not feasible.

## Validation

To validate the approach, this series includes a self-contained test module and a companion shell script. The module provides several test cases covering scenarios such as canary overflow, recursive depth tracking, multi-threaded silent corruption, retaddr overwriten. A detailed workflow example and usage guide are provided in the documentation (patch 26).

While KStackWatch itself is a new tool and has not yet discovered production bugs, it automates the exact methodology that I used to manually uncover CVE-2025-22036. The tool is designed to make this powerful debugging technique readily available to kernel developers, enabling them to efficiently detect and diagnose similar stack corruption issues in the future.

---
Patches 1–3 of this series are also used in the wprobe work proposed by Masami Hiramatsu, so there may be some overlap between our patches. Patch 3 comes directly from Masami Hiramatsu (thanks).
---

Changelog:
v8:
* Add arm64 support
* Implement hwbp_reinstall() for arm64.
* Use single-step mode as default in ksw_watch_handler().
* Add latency measurements for probe handlers.
* Update configuration options
* Introduce explicit auto_canary parameter.
* Default watch_len to sizeof(unsigned long) when zero.
* Replace panic_on_catch with panic_hit ksw_config option.
* Enable KStackWatch in non-debug builds.
* Limit canary search range to the current stack frame when possible.
* Add automatic architecture detection for test parameters.
* Move kstackwatch.h to include/linux/.
* Relocate Kconfig fragments to the kstackwatch/ directory.

v7:
https://lore.kernel.org/all/[email protected]/
* Fix maintainer entry to alphabetical position

v6:
https://lore.kernel.org/all/[email protected]/
* Replace procfs with debugfs interface
* Fix typos

v5:
https://lore.kernel.org/all/[email protected]/
* Support key=value input format
* Support multiple watchpoints
* Support watching instruction inside loop
* Support recursion depth tracking with generation
* Ignore triggers from fprobe trampoline
* Split watch_on into watch_get and watch_on to fail fast
* Handle ksw_stack_prepare_watch error
* Rewrite silent corruption test
* Add multiple watchpoints test
* Add an example in documentation

v4:
https://lore.kernel.org/all/[email protected]/
* Solve the lockdep issues with:
* per-task KStackWatch context to track depth
* atomic flag to protect watched_addr
* Use refactored version of arch_reinstall_hw_breakpoint

v3:
https://lore.kernel.org/all/[email protected]/
* Use modify_wide_hw_breakpoint_local() (from Masami)
* Add atomic flag to restrict /proc/kstackwatch to a single opener
* Protect stack probe with an atomic PID flag
* Handle CPU hotplug for watchpoints
* Add preempt_disable/enable in ksw_watch_on_local_cpu()
* Introduce const struct ksw_config *ksw_get_config(void) and use it
* Switch to global watch_attr, remove struct watch_info
* Validate local_var_len in parser()
* Handle case when canary is not found
* Use dump_stack() instead of show_regs() to allow module build
* Reduce logging and comments
* Format logs with KBUILD_MODNAME
* Remove unused headers
* Add new document

v2:
https://lore.kernel.org/all/[email protected]/
* Make hardware breakpoint and stack operations
architecture-independent.

v1:
https://lore.kernel.org/all/[email protected]/
* Replaced kretprobe with fprobe for function exit hooking, as suggested by Masami Hiramatsu
* Introduced per-task depth logic to track recursion across scheduling
* Removed the use of workqueue for a more efficient corruption check
* Reordered patches for better logical flow
* Simplified and improved commit messages throughout the series
* Removed initial archcheck which should be improved later
* Replaced the multiple-thread test with silent corruption test
* Split self-tests into a separate patch to improve clarity.
* Added a new entry for KStackWatch to the MAINTAINERS file.
---

Jinchao Wang (26):
x86/hw_breakpoint: Unify breakpoint install/uninstall x86/hw_breakpoint: Add arch_reinstall_hw_breakpoint mm/ksw: add build system support
mm/ksw: add ksw_config struct and parser mm/ksw: add singleton debugfs interface mm/ksw: add HWBP pre-allocation
mm/ksw: Add atomic watchpoint management api mm/ksw: ignore false positives from exit trampolines mm/ksw: support CPU hotplug
sched/ksw: add per-task context
mm/ksw: add entry kprobe and exit fprobe management mm/ksw: add per-task ctx tracking
mm/ksw: resolve stack watch addr and len mm/ksw: limit canary search to current stack frame mm/ksw: manage probe and HWBP lifecycle via procfs mm/ksw: add KSTACKWATCH_PROFILING to measure probe cost arm64/hw_breakpoint: Add arch_reinstall_hw_breakpoint arm64/hwbp/ksw: integrate KStackWatch handler support mm/ksw: add self-debug helpers
mm/ksw: add test module
mm/ksw: add stack overflow test
mm/ksw: add recursive depth test
mm/ksw: add multi-thread corruption test cases tools/ksw: add arch-specific test script
docs: add KStackWatch document
MAINTAINERS: add entry for KStackWatch

Masami Hiramatsu (Google) (1):
HWBP: Add modify_wide_hw_breakpoint_local() API

 Documentation/dev-tools/index.rst       |   1 +
 Documentation/dev-tools/kstackwatch.rst | 377 +++++++++++++++++++++
 MAINTAINERS                             |   9 +
 arch/Kconfig                            |  10 +
 arch/arm64/Kconfig                      |   1 +
 arch/arm64/include/asm/hw_breakpoint.h  |   1 +
 arch/arm64/kernel/hw_breakpoint.c       |  12 +
 arch/x86/Kconfig                        |   1 +
 arch/x86/include/asm/hw_breakpoint.h    |   8 +
 arch/x86/kernel/hw_breakpoint.c         | 148 +++++----
 include/linux/hw_breakpoint.h           |   6 +
 include/linux/kstackwatch.h             |  68 ++++
 include/linux/kstackwatch_types.h       |  14 +
 include/linux/sched.h                   |   5 +
 kernel/events/hw_breakpoint.c           |  37 +++
 mm/Kconfig                              |   1 +
 mm/Makefile                             |   1 +
 mm/kstackwatch/Kconfig                  |  34 ++
 mm/kstackwatch/Makefile                 |   8 +
 mm/kstackwatch/kernel.c                 | 295 +++++++++++++++++
 mm/kstackwatch/stack.c                  | 416 ++++++++++++++++++++++++
 mm/kstackwatch/test.c                   | 345 ++++++++++++++++++++
 mm/kstackwatch/watch.c                  | 309 ++++++++++++++++++
 tools/kstackwatch/kstackwatch_test.sh   |  85 +++++
 24 files changed, 2130 insertions(+), 62 deletions(-)
 create mode 100644 Documentation/dev-tools/kstackwatch.rst
 create mode 100644 include/linux/kstackwatch.h
 create mode 100644 include/linux/kstackwatch_types.h
 create mode 100644 mm/kstackwatch/Kconfig
 create mode 100644 mm/kstackwatch/Makefile
 create mode 100644 mm/kstackwatch/kernel.c
 create mode 100644 mm/kstackwatch/stack.c
 create mode 100644 mm/kstackwatch/test.c
 create mode 100644 mm/kstackwatch/watch.c
 create mode 100755 tools/kstackwatch/kstackwatch_test.sh

-*
2.43.0