mm/ksw: Introduce KStackWatch debugging tool
Subsystems (10)
v8 cover 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
PATCH v8 1/27 x86/hw_breakpoint: Unify breakpoint install/uninstall
Consolidate 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 | 141 +++++++++++++++------------ 2 files changed, 84 insertions(+), 63 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 +78 -63
181 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 2/27 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.
--- arch/x86/include/asm/hw_breakpoint.h | 2 ++ arch/x86/kernel/hw_breakpoint.c | 9 +++++++++ 2 files changed, 11 insertions(+)
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 +9 -0
@@ -99,6 +99,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 ;
@@ -187,6 +191,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.43.0
PATCH v8 3/27 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.
--- arch/Kconfig | 10 ++++++++++ arch/x86/Kconfig | 1 + include/linux/hw_breakpoint.h | 6 ++++++ kernel/events/hw_breakpoint.c | 37 +++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+)
arch/Kconfig +10 -0
@@ -456,6 +456,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
arch/x86/Kconfig +1 -0
@@ -245,6 +245,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
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 -ENOSYS ; } +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 +37 -0
@@ -887,6 +887,43 @@ void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cpu_events) }EXPORT_SYMBOL_GPL (unregister_wide_hw_breakpoint); +/** +* modify_wide_hw_breakpoint_local - update breakpoint config for local CPU +* @bp: the hwbp perf event for this CPU +* @attr: the new attribute for @bp +* +* This does not release and reserve the slot of a HWBP; it just reuses the +* current slot on local CPU. So the users must update the other CPUs by +* themselves. +* Also, since this does not release/reserve the slot, this can not change the +* type to incompatible type of the HWBP. +* Return err if attr is invalid or the CPU fails to update debug register +* for new @attr. +*/ +#ifdef CONFIG_HAVE_REINSTALL_HW_BREAKPOINT +int modify_wide_hw_breakpoint_local (struct perf_event *bp, +struct perf_event_attr *attr) +{ +int ret; + +if (find_slot_idx (bp->attr.bp_type) !=find_slot_idx (attr->bp_type)) +return -EINVAL ; + + ret =hw_breakpoint_arch_parse (bp, attr,counter_arch_bp (bp)); +if (ret) +return ret; + +return arch_reinstall_hw_breakpoint (bp); +} +#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); +/** * hw_breakpoint_is_used - check if breakpoints are currently used *
Signature · 1 line
2.43.0
PATCH v8 4/27 mm/ksw: add build system support
In reply to Jinchao WangAdd Kconfig and Makefile infrastructure.
The implementation is located under mm/kstackwatch/.
--- include/linux/kstackwatch.h | 5 +++++ mm/Kconfig | 1 + mm/Makefile | 1 + mm/kstackwatch/Kconfig | 14 ++++++++++++++ mm/kstackwatch/Makefile | 2 ++ mm/kstackwatch/kernel.c | 23 +++++++++++++++++++++++ mm/kstackwatch/stack.c | 1 + mm/kstackwatch/watch.c | 1 + 8 files changed, 48 insertions(+) create mode 100644 include/linux/kstackwatch.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/watch.c
include/linux/kstackwatch.h new file +5 -0
@@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _KSTACKWATCH_H +#define _KSTACKWATCH_H + +#endif /* _KSTACKWATCH_H */
mm/Kconfig +1 -0
@@ -1373,5 +1373,6 @@ config FIND_NORMAL_PAGE def_bool n source "mm/damon/Kconfig" +source "mm/kstackwatch/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_KSTACKWATCH) += kstackwatch/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_FAIL_PAGE_ALLOC) += fail_page_alloc.o obj-$(CONFIG_MEMTEST) += memtest.o
mm/kstackwatch/Kconfig new file +14 -0
@@ -0,0 +1,14 @@ +config KSTACKWATCH + bool "Kernel Stack Watch" + depends on HAVE_HW_BREAKPOINT && KPROBES && FPROBE && STACKTRACE + help + A lightweight real-time debugging tool to detect stack corruption + and abnormal stack usage patterns in the kernel. It monitors stack + boundaries and detects overwrites in real time using hardware + breakpoints and probe-based instrumentation. + + This feature is intended for kernel developers or advanced users + diagnosing rare stack overflow or memory corruption bugs. It may + introduce minor overhead during runtime monitoring. + + If unsure, say N.
mm/kstackwatch/Makefile new file +2 -0
@@ -0,0 +1,2 @@ +obj-$(CONFIG_KSTACKWATCH) += kstackwatch.o +kstackwatch-y := kernel.o stack.o watch.o
mm/kstackwatch/kernel.c new file +23 -0
@@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt + +#include <linux/module.h> + +static int __init kstackwatch_init (void ) +{ +pr_info ("module loaded\n" ); +return 0 ; +} + +static void __exit kstackwatch_exit(void) +{ +pr_info ("module unloaded\n" ); +} + +module_init (kstackwatch_init); +module_exit (kstackwatch_exit); + +MODULE_AUTHOR ("Jinchao Wang" ); +MODULE_DESCRIPTION ("Kernel Stack Watch" ); +MODULE_LICENSE ("GPL" ); +
mm/kstackwatch/stack.c new file +1 -0
@@ -0,0 +1 @@ +// SPDX-License-Identifier: GPL-2.0
mm/kstackwatch/watch.c new file +1 -0
@@ -0,0 +1 @@ +// SPDX-License-Identifier: GPL-2.0
Signature · 1 line
2.43.0
PATCH v8 5/27 mm/ksw: add ksw_config struct and parser
In reply to Jinchao WangAdd struct ksw_config and ksw_parse_config() to parse user string.
--- include/linux/kstackwatch.h | 33 +++++++++++ mm/kstackwatch/kernel.c | 114 ++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+)
include/linux/kstackwatch.h +33 -0
@@ -2,4 +2,37 @@#ifndef _KSTACKWATCH_H#define _KSTACKWATCH_H +#include <linux/types.h> + +#define MAX_CONFIG_STR_LEN 128 + +struct ksw_config { +char *func_name; +u16 depth; + +/* +* watched variable info: +* - func_offset : instruction offset in the function, typically the +* assignment of the watched variable, where ksw +* registers a kprobe post-handler. +* - sp_offset : offset from stack pointer at func_offset. Usually 0. +* - watch_len : size of the watched variable (1, 2, 4, or 8 bytes). +*/ +u16 func_offset; +u16 sp_offset; +u16 watch_len; + +/* max number of hwbps that can be used */ +u16 max_watch; + +/* search canary as watch target automatically */ +u16 auto_canary; + +/* panic on watchpoint hit */ +u16 panic_hit; + +/* save to show */ +char *user_input; +}; +#endif /* _KSTACKWATCH_H */
mm/kstackwatch/kernel.c +114 -0
130 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 6/27 mm/ksw: add singleton debugfs interface
In reply to Jinchao WangProvide the debugfs config file to read or update the configuration. Only a single process can open this file at a time, enforced using atomic config_file_busy, to prevent concurrent access.
ksw_get_config() exposes the configuration pointer as const.
--- include/linux/kstackwatch.h | 3 ++ mm/kstackwatch/kernel.c | 103 ++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-)
include/linux/kstackwatch.h +3 -0
@@ -35,4 +35,7 @@ struct ksw_config { char *user_input; }; +// singleton, only modified in kernel.c +const struct ksw_config *ksw_get_config (void ); +#endif /* _KSTACKWATCH_H */
mm/kstackwatch/kernel.c +100 -3
138 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 7/27 mm/ksw: add HWBP pre-allocation
In reply to Jinchao WangPre-allocate per-CPU hardware breakpoints at init with a place holder address, which will be retargeted dynamically in kprobe handler. This avoids allocation in atomic context.
At most max_watch breakpoints are allocated (0 means no limit).
--- include/linux/kstackwatch.h | 13 ++++++ mm/kstackwatch/watch.c | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+)
include/linux/kstackwatch.h +13 -0
@@ -2,6 +2,9 @@#ifndef _KSTACKWATCH_H#define _KSTACKWATCH_H +#include <linux/llist.h> +#include <linux/percpu.h> +#include <linux/perf_event.h> #include <linux/types.h> #define MAX_CONFIG_STR_LEN 128
@@ -38,4 +41,14 @@ struct ksw_config {// singleton, only modified in kernel.c const struct ksw_config *ksw_get_config (void ); +/* watch management */ +struct ksw_watchpoint { +struct perf_event *__percpu *event; +struct perf_event_attr attr; +struct llist_node node;// for atomic watch_on and off +struct list_head list;// for cpu online and offline +}; +int ksw_watch_init (void ); +void ksw_watch_exit (void ); +#endif /* _KSTACKWATCH_H */
mm/kstackwatch/watch.c +93 -0
94 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 8/27 mm/ksw: Add atomic watchpoint management api
In reply to Jinchao WangAdd three functions for atomic lifecycle management of watchpoints:
- ksw_watch_get(): Acquires a watchpoint from a llist.
- ksw_watch_on(): Enables the watchpoint on all online CPUs.
- ksw_watch_off(): Disables the watchpoint and returns it to the llist.
For cross-CPU synchronization, updates are propagated using direct modification on the local CPU and asynchronous IPIs for remote CPUs.
--- include/linux/kstackwatch.h | 4 ++ mm/kstackwatch/watch.c | 85 ++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-)
include/linux/kstackwatch.h +4 -0
@@ -44,11 +44,15 @@ const struct ksw_config *ksw_get_config(void);/* watch management */ struct ksw_watchpoint {struct perf_event *__percpu *event; +call_single_data_t __percpu *csd;struct perf_event_attr attr;struct llist_node node;// for atomic watch_on and off struct list_head list;// for cpu online and offline };int ksw_watch_init (void );void ksw_watch_exit (void ); +int ksw_watch_get (struct ksw_watchpoint **out_wp); +int ksw_watch_on (struct ksw_watchpoint *wp,ulong watch_addr,u16 watch_len); +int ksw_watch_off (struct ksw_watchpoint *wp);#endif /* _KSTACKWATCH_H */
mm/kstackwatch/watch.c +84 -1
120 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 9/27 mm/ksw: ignore false positives from exit trampolines
In reply to Jinchao WangBecause trampolines run after the watched function returns but before the exit_handler is called, and in the original stack frame, so the trampoline code may overwrite the watched stack address.
These false positives should be ignored. is_ftrace_trampoline() does not cover all trampolines, so add a local check to handle the remaining cases.
--- mm/kstackwatch/watch.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
mm/kstackwatch/watch.c +38 -0
@@ -2,6 +2,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/cpuhotplug.h> +#include <linux/ftrace.h> #include <linux/hw_breakpoint.h> #include <linux/irqflags.h> #include <linux/kstackwatch.h>
@@ -14,10 +15,46 @@ static DEFINE_MUTEX(all_wp_mutex);static ulong holder; +#define TRAMPOLINE_NAME "return_to_handler" +#define TRAMPOLINE_DEPTH 16 + +/* Resolved once, then reused */ +static unsigned long tramp_start, tramp_end; + +static void ksw_watch_resolve_trampoline (void ) +{ +unsigned long sz, off; + +if (likely (tramp_start && tramp_end)) +return ; + + tramp_start =kallsyms_lookup_name (TRAMPOLINE_NAME ); +if (tramp_start &&kallsyms_lookup_size_offset (tramp_start, &sz, &off)) + tramp_end = tramp_start + sz; +} + +static bool ksw_watch_in_trampoline (unsigned long ip) +{ +if (tramp_start && tramp_end && ip >= tramp_start && ip < tramp_end) +return true; +return false; +}static void ksw_watch_handler (struct perf_event *bp,struct perf_sample_data *data,struct pt_regs *regs) { +unsigned long entries[TRAMPOLINE_DEPTH ]; +int i, nr =0 ; + + nr =stack_trace_save_regs (regs, entries,TRAMPOLINE_DEPTH ,0 ); +for (i =0 ; i < nr; i++) { +//ignore trampoline +if (is_ftrace_trampoline (entries[i])) +return ; +if (ksw_watch_in_trampoline (entries[i])) +return ; + } +pr_err ("========== KStackWatch: Caught stack corruption =======\n" );pr_err ("config %s\n" ,ksw_get_config ()->user_input);dump_stack ();
@@ -164,6 +201,7 @@ int ksw_watch_init(void) {int ret; +ksw_watch_resolve_trampoline (); ret =ksw_watch_alloc ();if (ret <=0 )return -EBUSY ;
Signature · 1 line
2.43.0
PATCH v8 10/27 mm/ksw: support CPU hotplug
In reply to Jinchao WangRegister CPU online/offline callbacks via cpuhp_setup_state_nocalls() so stack watches are installed/removed dynamically as CPUs come online or go offline.
When a new CPU comes online, register a hardware breakpoint for the holder, avoiding races with watch_on()/watch_off() that may run on another CPU. The watch address will be updated the next time watch_on() is called.
--- mm/kstackwatch/watch.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+)
mm/kstackwatch/watch.c +52 -0
@@ -85,6 +85,48 @@ static void ksw_watch_on_local_cpu(void *info)WARN (ret,"fail to reinstall HWBP on CPU%d ret %d" , cpu, ret); } +static int ksw_watch_cpu_online (unsigned int cpu) +{ +struct perf_event_attr attr; +struct ksw_watchpoint *wp; +call_single_data_t *csd; +struct perf_event *bp; + +mutex_lock (&all_wp_mutex); +list_for_each_entry (wp, &all_wp_list, list) { + attr = wp->attr; + attr.bp_addr = (u64)&holder; + bp =perf_event_create_kernel_counter (&attr, cpu,NULL , + ksw_watch_handler, wp); +if (IS_ERR (bp)) { +pr_warn ("%s failed to create watch on CPU %d: %ld\n" , + __func__, cpu,PTR_ERR (bp)); +continue ; + } + +per_cpu (*wp->event, cpu) = bp; + csd =per_cpu_ptr (wp->csd, cpu); +INIT_CSD (csd, ksw_watch_on_local_cpu, wp); + } +mutex_unlock (&all_wp_mutex); +return 0 ; +} + +static int ksw_watch_cpu_offline (unsigned int cpu) +{ +struct ksw_watchpoint *wp; +struct perf_event *bp; + +mutex_lock (&all_wp_mutex); +list_for_each_entry (wp, &all_wp_list, list) { + bp =per_cpu (*wp->event, cpu); +if (bp) +unregister_hw_breakpoint (bp); + } +mutex_unlock (&all_wp_mutex); +return 0 ; +} +static void ksw_watch_update (struct ksw_watchpoint *wp,ulong addr,u16 len) {call_single_data_t *csd;
@@ -206,6 +248,16 @@ int ksw_watch_init(void)if (ret <=0 )return -EBUSY ; + ret =cpuhp_setup_state_nocalls (CPUHP_AP_ONLINE_DYN , +"kstackwatch:online" , + ksw_watch_cpu_online, + ksw_watch_cpu_offline); +if (ret <0 ) { +ksw_watch_free (); +pr_err ("Failed to register CPU hotplug notifier\n" ); +return ret; + } +return 0 ; }
Signature · 1 line
2.43.0
PATCH v8 11/27 sched/ksw: add per-task context
In reply to Jinchao WangIntroduce struct ksw_ctx to enable lockless per-task state tracking. This is required because KStackWatch operates in NMI context (via kprobe handler) where traditional locking is unsafe.
--- include/linux/kstackwatch_types.h | 14 ++++++++++++++ include/linux/sched.h | 5 +++++ 2 files changed, 19 insertions(+) create mode 100644 include/linux/kstackwatch_types.h
include/linux/kstackwatch_types.h new file +14 -0
@@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_KSTACKWATCH_TYPES_H +#define _LINUX_KSTACKWATCH_TYPES_H +#include <linux/types.h> + +struct ksw_watchpoint ; +struct ksw_ctx { +struct ksw_watchpoint *wp; +ulong sp; +u16 depth; +u16 generation; +}; + +#endif /* _LINUX_KSTACKWATCH_TYPES_H */
include/linux/sched.h +5 -0
@@ -22,6 +22,7 @@#include <linux/sem_types.h> #include <linux/shm.h> #include <linux/kmsan_types.h> +#include <linux/kstackwatch_types.h> #include <linux/mutex_types.h> #include <linux/plist_types.h> #include <linux/hrtimer_types.h>
@@ -1487,6 +1488,10 @@ struct task_struct {struct kmsan_ctx kmsan_ctx;#endif +#if IS_ENABLED (CONFIG_KSTACKWATCH ) +struct ksw_ctx ksw_ctx; +#endif +#if IS_ENABLED (CONFIG_KUNIT )struct kunit *kunit_test;#endif
Signature · 1 line
2.43.0
PATCH v8 12/27 mm/ksw: add entry kprobe and exit fprobe management
In reply to Jinchao WangProvide ksw_stack_init() and ksw_stack_exit() to manage entry and exit probes for the target function from ksw_get_config().
--- include/linux/kstackwatch.h | 4 ++ mm/kstackwatch/stack.c | 100 ++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+)
include/linux/kstackwatch.h +4 -0
@@ -41,6 +41,10 @@ struct ksw_config {// singleton, only modified in kernel.c const struct ksw_config *ksw_get_config (void ); +/* stack management */ +int ksw_stack_init (void ); +void ksw_stack_exit (void ); +/* watch management */ struct ksw_watchpoint {struct perf_event *__percpu *event;
mm/kstackwatch/stack.c +100 -0
101 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 13/27 mm/ksw: add per-task ctx tracking
In reply to Jinchao WangEach task tracks its depth, stack pointer, and generation. A watchpoint is enabled only when the configured depth is reached, and disabled on function exit.
The context is reset when probes are disabled, generation changes, or exit depth becomes inconsistent.
Duplicate arming on the same frame is skipped.
--- mm/kstackwatch/stack.c | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+)
mm/kstackwatch/stack.c +67 -0
@@ -11,6 +11,53 @@static struct kprobe entry_probe;static struct fprobe exit_probe; +static bool probe_enable; +static u16 probe_generation; + +static void ksw_reset_ctx (void ) +{ +struct ksw_ctx *ctx = ¤t->ksw_ctx; + +if (ctx->wp) +ksw_watch_off (ctx->wp); + + ctx->wp =NULL ; + ctx->sp =0 ; + ctx->depth =0 ; + ctx->generation =READ_ONCE (probe_generation); +} + +static bool ksw_stack_check_ctx (bool entry) +{ +struct ksw_ctx *ctx = ¤t->ksw_ctx; +u16 cur_enable =READ_ONCE (probe_enable); +u16 cur_generation =READ_ONCE (probe_generation); +u16 cur_depth, target_depth =ksw_get_config ()->depth; + +if (!cur_enable) { +ksw_reset_ctx (); +return false; + } + +if (ctx->generation != cur_generation) +ksw_reset_ctx (); + +if (!entry && !ctx->depth) { +ksw_reset_ctx (); +return false; + } + +if (entry) + cur_depth = ctx->depth++; +else + cur_depth = --ctx->depth; + +if (cur_depth == target_depth) +return true; +else +return false; +} +static int ksw_stack_prepare_watch (struct pt_regs *regs,const struct ksw_config *config,ulong *watch_addr,u16 *watch_len)
@@ -25,10 +72,22 @@ static void ksw_stack_entry_handler(struct kprobe *p, struct pt_regs *regs, unsigned long flags) {struct ksw_ctx *ctx = ¤t->ksw_ctx; +ulong stack_pointer;ulong watch_addr;u16 watch_len;int ret; + stack_pointer =kernel_stack_pointer (regs); + +/* +* triggered more than once, may be in a loop +*/ +if (ctx->wp && ctx->sp == stack_pointer) +return ; + +if (!ksw_stack_check_ctx (true)) +return ; + ret =ksw_watch_get (&ctx->wp);if (ret)return ;
@@ -49,6 +108,7 @@ static void ksw_stack_entry_handler(struct kprobe *p, struct pt_regs *regs,return ; } + ctx->sp = stack_pointer; } static void ksw_stack_exit_handler(struct fprobe *fp, unsigned long ip,
@@ -57,6 +117,8 @@ static void ksw_stack_exit_handler(struct fprobe *fp, unsigned long ip, {struct ksw_ctx *ctx = ¤t->ksw_ctx; +if (!ksw_stack_check_ctx (false)) +return ;if (ctx->wp) {ksw_watch_off (ctx->wp);
@@ -91,11 +153,16 @@ int ksw_stack_init(void)return ret; } +WRITE_ONCE (probe_generation,READ_ONCE (probe_generation) +1 ); +WRITE_ONCE (probe_enable, true); +return 0 ; }void ksw_stack_exit (void ) { +WRITE_ONCE (probe_enable, false); +WRITE_ONCE (probe_generation,READ_ONCE (probe_generation) +1 );unregister_fprobe (&exit_probe);unregister_kprobe (&entry_probe); }
Signature · 1 line
2.43.0
PATCH v8 14/27 mm/ksw: resolve stack watch addr and len
In reply to Jinchao WangAdd helpers to find the stack canary or a local variable addr and len for the probed function based on ksw_get_config(). For canary search, limits search to a fixed number of steps to avoid scanning the entire stack. Validates that the computed address and length are within the kernel stack.
--- mm/kstackwatch/stack.c | 80 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-)
mm/kstackwatch/stack.c +77 -3
@@ -8,6 +8,7 @@#include <linux/kstackwatch_types.h> #include <linux/printk.h> +#define MAX_CANARY_SEARCH_STEPS 128static struct kprobe entry_probe;static struct fprobe exit_probe;
@@ -58,13 +59,86 @@ static bool ksw_stack_check_ctx(bool entry)return false; } +static unsigned long ksw_find_stack_canary_addr (struct pt_regs *regs) +{ +unsigned long *stack_ptr, *stack_end, *stack_base; +unsigned long expected_canary; +unsigned int i; + + stack_ptr = (unsigned long *)kernel_stack_pointer (regs); + + stack_base = (unsigned long *)(current->stack); + +// TODO: limit it to the current frame + stack_end = (unsigned long *)((char *)current->stack +THREAD_SIZE ); + + expected_canary = current->stack_canary; + +if (stack_ptr < stack_base || stack_ptr >= stack_end) { +pr_err ("Stack pointer 0x%lx out of bounds [0x%lx, 0x%lx)\n" , + (unsigned long )stack_ptr, (unsigned long )stack_base, + (unsigned long )stack_end); +return 0 ; + } + +for (i =0 ; i <MAX_CANARY_SEARCH_STEPS ; i++) { +if (&stack_ptr[i] >= stack_end) +break ; + +if (stack_ptr[i] == expected_canary) { +pr_debug ("canary found i:%d 0x%lx\n" , i, + (unsigned long )&stack_ptr[i]); +return (unsigned long )&stack_ptr[i]; + } + } + +pr_debug ("canary not found in first %d steps\n" , +MAX_CANARY_SEARCH_STEPS ); +return 0 ; +} + +static int ksw_stack_validate_addr (unsigned long addr,size_t size) +{ +unsigned long stack_start, stack_end; + +if (!addr || !size) +return -EINVAL ; + + stack_start = (unsigned long )current->stack; + stack_end = stack_start +THREAD_SIZE ; + +if (addr < stack_start || (addr + size) > stack_end) +return -ERANGE ; + +return 0 ; +} +static int ksw_stack_prepare_watch (struct pt_regs *regs,const struct ksw_config *config,ulong *watch_addr,u16 *watch_len) { -/* implement logic will be added in following patches */ - *watch_addr =0 ; - *watch_len =0 ; +ulong addr; +u16 len; + +if (ksw_get_config ()->auto_canary) { + addr =ksw_find_stack_canary_addr (regs); +if (!addr) +return -EINVAL ; + len =sizeof (ulong); + }else { + addr =kernel_stack_pointer (regs) +ksw_get_config ()->sp_offset; + len =ksw_get_config ()->watch_len; +if (!len) + len =sizeof (ulong); + } + +if (ksw_stack_validate_addr (addr, len)) { +pr_err ("invalid stack addr:0x%lx len :%u\n" , addr, len); +return -EINVAL ; + } + + *watch_addr = addr; + *watch_len = len;return 0 ; }
Signature · 1 line
2.43.0
PATCH v8 15/27 mm/ksw: limit canary search to current stack frame
In reply to Jinchao WangUse the compiler-provided frame pointer when CONFIG_FRAME_POINTER is enabled to restrict the stack canary search range to the current function frame. This prevents scanning beyond valid stack bounds and improves reliability across architectures.
Also add explicit handling for missing CONFIG_STACKPROTECTOR and make the failure message more visible.
--- mm/kstackwatch/stack.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
mm/kstackwatch/stack.c +21 -8
@@ -64,15 +64,32 @@ static unsigned long ksw_find_stack_canary_addr(struct pt_regs *regs)unsigned long *stack_ptr, *stack_end, *stack_base;unsigned long expected_canary;unsigned int i; +#ifdef CONFIG_FRAME_POINTER +unsigned long *fp =NULL ; +#endif stack_ptr = (unsigned long *)kernel_stack_pointer (regs); - stack_base = (unsigned long *)(current->stack); -// TODO: limit it to the current frame stack_end = (unsigned long *)((char *)current->stack +THREAD_SIZE ); +#ifdef CONFIG_FRAME_POINTER +/* +* Use the compiler-provided frame pointer. +* Limit the search to the current frame +* Works on any arch that keeps FP when CONFIG_FRAME_POINTER=y. +*/ + fp =__builtin_frame_address (0 ); +if (fp > stack_ptr && fp < stack_end) + stack_end = fp; +#endif + +#ifdef CONFIG_STACKPROTECTOR expected_canary = current->stack_canary; +#else +pr_err ("no canary without CONFIG_STACKPROTECTOR\n" ); +return 0 ; +#endif if (stack_ptr < stack_base || stack_ptr >= stack_end) { pr_err("Stack pointer 0x%lx out of bounds [0x%lx, 0x%lx)\n",
@@ -85,15 +102,11 @@ static unsigned long ksw_find_stack_canary_addr(struct pt_regs *regs)if (&stack_ptr[i] >= stack_end)break ; -if (stack_ptr[i] == expected_canary) { -pr_debug ("canary found i:%d 0x%lx\n" , i, - (unsigned long )&stack_ptr[i]); +if (stack_ptr[i] == expected_canary)return (unsigned long )&stack_ptr[i]; - } } -pr_debug ("canary not found in first %d steps\n" , -MAX_CANARY_SEARCH_STEPS ); +pr_err ("canary not found in first %d steps\n" ,MAX_CANARY_SEARCH_STEPS );return 0 ; }
Signature · 1 line
2.43.0
PATCH v8 16/27 mm/ksw: manage probe and HWBP lifecycle via procfs
In reply to Jinchao WangAllow dynamic enabling/disabling of KStackWatch through user input of proc. With this patch, the entire system becomes functional.
--- mm/kstackwatch/kernel.c | 60 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-)
mm/kstackwatch/kernel.c +58 -2
@@ -14,6 +14,43 @@ static struct ksw_config *ksw_config;static struct dentry *dbgfs_config;static struct dentry *dbgfs_dir; +static bool watching_active; + +static int ksw_start_watching (void ) +{ +int ret; + +/* +* Watch init will preallocate the HWBP, +* so it must happen before stack init +*/ + ret =ksw_watch_init (); +if (ret) { +pr_err ("ksw_watch_init ret: %d\n" , ret); +return ret; + } + + ret =ksw_stack_init (); +if (ret) { +pr_err ("ksw_stack_init ret: %d\n" , ret); +ksw_watch_exit (); +return ret; + } + watching_active = true; + +pr_info ("start watching: %s\n" , ksw_config->user_input); +return 0 ; +} + +static void ksw_stop_watching (void ) +{ +ksw_stack_exit (); +ksw_watch_exit (); + watching_active = false; + +pr_info ("stop watching: %s\n" , ksw_config->user_input); +} +struct param_map {const char *name;/* long name */ const char *short_name;/* short name (2 letters) */
@@ -119,8 +156,18 @@ static int ksw_parse_config(char *buf, struct ksw_config *config)static ssize_t ksw_dbgfs_read (struct file *file,char __user *buf,size_t count,loff_t *ppos) { -return simple_read_from_buffer (buf, count, ppos, ksw_config->user_input, - ksw_config->user_input ?strlen (ksw_config->user_input) :0 ); +const char *out; +size_t len; + +if (watching_active && ksw_config->user_input) { + out = ksw_config->user_input; + len =strlen (out); + }else { + out ="not watching\n" ; + len =strlen (out); + } + +return simple_read_from_buffer (buf, count, ppos, out, len); } static ssize_t ksw_dbgfs_write(struct file *file, const char __user *buffer,
@@ -135,6 +182,9 @@ static ssize_t ksw_dbgfs_write(struct file *file, const char __user *buffer,if (copy_from_user (input, buffer, count))return -EFAULT ; +if (watching_active) +ksw_stop_watching (); + input[count] ='\0' ;strim (input);
@@ -149,6 +199,12 @@ static ssize_t ksw_dbgfs_write(struct file *file, const char __user *buffer,return ret; } + ret =ksw_start_watching (); +if (ret) { +pr_err ("Failed to start watching with %d\n" , ret); +return ret; + } +return count; }
Signature · 1 line
2.43.0
PATCH v8 17/27 mm/ksw: add KSTACKWATCH_PROFILING to measure probe cost
In reply to Jinchao WangCONFIG_KSTACKWATCH_PROFILING enables runtime measurement of KStackWatch probe latencies. When profiling is enabled, KStackWatch collects entry/exit latencies in its probe callbacks. When KStackWatch is disabled by clearing its config file, the previously collected statistics are printed.
--- mm/kstackwatch/Kconfig | 10 +++ mm/kstackwatch/stack.c | 185 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 183 insertions(+), 12 deletions(-)
mm/kstackwatch/Kconfig +10 -0
@@ -12,3 +12,13 @@ config KSTACKWATCH introduce minor overhead during runtime monitoring. If unsure, say N. + +config KSTACKWATCH_PROFILING + bool "KStackWatch profiling" + depends on KSTACKWATCH + help + Measure probe latency and overhead in KStackWatch. It records + entry/exit probe times (ns and cycles) and shows statistics when + stopping. Useful for performance tuning, not for production use. + + If unsure, say N.
mm/kstackwatch/stack.c +173 -12
254 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 18/27 arm64/hw_breakpoint: Add arch_reinstall_hw_breakpoint
In reply to Jinchao WangAdd arch_reinstall_hw_breakpoint() to restore a hardware breakpoint in an atomic context. Unlike the full uninstall and reallocation path, this lightweight function re-establishes an existing breakpoint efficiently and safely.
This aligns ARM64 with x86 support for atomic breakpoint reinstalls.
--- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/hw_breakpoint.h | 1 + arch/arm64/kernel/hw_breakpoint.c | 5 +++++ 3 files changed, 7 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -232,6 +232,7 @@ config ARM64 select HAVE_HARDLOCKUP_DETECTOR_PERF if PERF_EVENTS && \ HW_PERF_EVENTS && HAVE_PERF_EVENTS_NMI select HAVE_HW_BREAKPOINT if PERF_EVENTS + select HAVE_REINSTALL_HW_BREAKPOINT if PERF_EVENTS select HAVE_IOREMAP_PROT select HAVE_IRQ_TIME_ACCOUNTING select HAVE_LIVEPATCH
arch/arm64/include/asm/hw_breakpoint.h +1 -0
@@ -119,6 +119,7 @@ extern int hw_breakpoint_exceptions_notify(struct notifier_block *unused, unsigned long val, void *data);extern int arch_install_hw_breakpoint (struct perf_event *bp); +extern int arch_reinstall_hw_breakpoint (struct perf_event *bp);extern void arch_uninstall_hw_breakpoint (struct perf_event *bp);extern void hw_breakpoint_pmu_read (struct perf_event *bp);extern int hw_breakpoint_slots (int type);
arch/arm64/kernel/hw_breakpoint.c +5 -0
@@ -292,6 +292,11 @@ int arch_install_hw_breakpoint(struct perf_event *bp)return hw_breakpoint_control (bp,HW_BREAKPOINT_INSTALL ); } +int arch_reinstall_hw_breakpoint (struct perf_event *bp) +{ +return hw_breakpoint_control (bp,HW_BREAKPOINT_RESTORE ); +} +void arch_uninstall_hw_breakpoint (struct perf_event *bp) {hw_breakpoint_control (bp,HW_BREAKPOINT_UNINSTALL );
Signature · 1 line
2.43.0
PATCH v8 19/27 arm64/hwbp/ksw: integrate KStackWatch handler support
In reply to Jinchao WangAdd support for identifying KStackWatch watchpoints in the ARM64 hardware breakpoint handler. When a watchpoint belongs to KStackWatch, the handler bypasses single-step re-arming to allow proper recovery.
Introduce is_ksw_watch_handler() to detect KStackWatch-managed breakpoints and use it in watchpoint_report() under CONFIG_KSTACKWATCH.
--- arch/arm64/kernel/hw_breakpoint.c | 7 +++++++ include/linux/kstackwatch.h | 2 ++ mm/kstackwatch/watch.c | 8 ++++++++ 3 files changed, 17 insertions(+)
arch/arm64/kernel/hw_breakpoint.c +7 -0
@@ -14,6 +14,9 @@#include <linux/errno.h> #include <linux/hw_breakpoint.h> #include <linux/kprobes.h> +#ifdef CONFIG_KSTACKWATCH +#include <linux/kstackwatch.h> +#endif #include <linux/perf_event.h> #include <linux/ptrace.h> #include <linux/smp.h>
@@ -738,6 +741,10 @@ static int watchpoint_report(struct perf_event *wp, unsigned long addr, struct pt_regs *regs) {int step =is_default_overflow_handler (wp); +#ifdef CONFIG_KSTACKWATCH +if (is_ksw_watch_handler (wp)) + step =1 ; +#endif struct arch_hw_breakpoint *info =counter_arch_bp (wp); info->trigger = addr;
include/linux/kstackwatch.h +2 -0
@@ -53,6 +53,8 @@ struct ksw_watchpoint {struct llist_node node;// for atomic watch_on and off struct list_head list;// for cpu online and offline }; + +bool is_ksw_watch_handler (struct perf_event *event);int ksw_watch_init (void );void ksw_watch_exit (void );int ksw_watch_get (struct ksw_watchpoint **out_wp);
mm/kstackwatch/watch.c +8 -0
@@ -64,6 +64,14 @@ static void ksw_watch_handler(struct perf_event *bp,panic ("Stack corruption detected" ); } +bool is_ksw_watch_handler (struct perf_event *event) +{ +perf_overflow_handler_t overflow_handler = event->overflow_handler; + +if (unlikely (overflow_handler == ksw_watch_handler)) +return true; +return false; +}static void ksw_watch_on_local_cpu (void *info) {struct ksw_watchpoint *wp = info;
Signature · 1 line
2.43.0
PATCH v8 20/27 mm/ksw: add self-debug helpers
In reply to Jinchao WangProvide two debug helpers:
- ksw_watch_show(): print the current watch target address and length.
- ksw_watch_fire(): intentionally trigger the watchpoint immediately by writing to the watched address, useful for testing HWBP behavior.
--- include/linux/kstackwatch.h | 2 ++ mm/kstackwatch/watch.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+)
include/linux/kstackwatch.h +2 -0
@@ -60,5 +60,7 @@ void ksw_watch_exit(void);int ksw_watch_get (struct ksw_watchpoint **out_wp);int ksw_watch_on (struct ksw_watchpoint *wp,ulong watch_addr,u16 watch_len);int ksw_watch_off (struct ksw_watchpoint *wp); +void ksw_watch_show (void ); +void ksw_watch_fire (void );#endif /* _KSTACKWATCH_H */
mm/kstackwatch/watch.c +34 -0
@@ -273,3 +273,37 @@ void ksw_watch_exit(void) {ksw_watch_free (); } + +/* self debug function */ +void ksw_watch_show (void ) +{ +struct ksw_watchpoint *wp = current->ksw_ctx.wp; + +if (!wp) { +pr_info ("nothing to show\n" ); +return ; + } + +pr_info ("watch target bp_addr: 0x%llx len:%llu\n" , wp->attr.bp_addr, + wp->attr.bp_len); +} +EXPORT_SYMBOL_GPL (ksw_watch_show); + +/* self debug function */ +void ksw_watch_fire (void ) +{ +struct ksw_watchpoint *wp; +char *ptr; + + wp = current->ksw_ctx.wp; + +if (!wp) { +pr_info ("nothing to fire\n" ); +return ; + } + + ptr = (char *)wp->attr.bp_addr; +pr_warn ("watch triggered immediately\n" ); + *ptr =0x42 ;// This should trigger immediately for any bp_len +} +EXPORT_SYMBOL_GPL (ksw_watch_fire);
Signature · 1 line
2.43.0
PATCH v8 21/27 mm/ksw: add test module
In reply to Jinchao WangAdd a standalone test module for KStackWatch to validate functionality in controlled scenarios.
The module exposes a simple interface via debugfs (/sys/kernel/debug/kstackwatch/test), allowing specific test cases to be triggered with commands such as:
echo test0 > /sys/kernel/debug/kstackwatch/test
To ensure predictable behavior during testing, the module is built with optimizations disabled.
show addr of buf and watch_addr of test case
--- include/linux/kstackwatch.h | 2 + mm/kstackwatch/Kconfig | 10 +++ mm/kstackwatch/Makefile | 6 ++ mm/kstackwatch/kernel.c | 5 ++ mm/kstackwatch/test.c | 121 ++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 mm/kstackwatch/test.c
include/linux/kstackwatch.h +2 -0
@@ -40,6 +40,8 @@ struct ksw_config {// singleton, only modified in kernel.c const struct ksw_config *ksw_get_config (void ); +struct dentry *ksw_get_dbgdir (void ); +/* stack management */ int ksw_stack_init (void );
mm/kstackwatch/Kconfig +10 -0
@@ -22,3 +22,13 @@ config KSTACKWATCH_PROFILING stopping. Useful for performance tuning, not for production use. If unsure, say N. + +config KSTACKWATCH_TEST + tristate "KStackWatch Test Module" + depends on KSTACKWATCH + help + This module provides controlled stack corruption scenarios to verify + the functionality of KStackWatch. It is useful for development and + validation of KStackWatch mechanism. + + If unsure, say N.
mm/kstackwatch/Makefile +6 -0
@@ -1,2 +1,8 @@ obj-$(CONFIG_KSTACKWATCH) += kstackwatch.o kstackwatch-y := kernel.o stack.o watch.o + +obj-$(CONFIG_KSTACKWATCH_TEST) += kstackwatch_test.o +kstackwatch_test-y := test.o +CFLAGS_test.o := -fno-inline \ + -fno-optimize-sibling-calls \ + -fno-pic -fno-pie -O0 -Og
mm/kstackwatch/kernel.c +5 -0
@@ -235,6 +235,11 @@ const struct ksw_config *ksw_get_config(void)return ksw_config; } +struct dentry *ksw_get_dbgdir (void ) +{ +return dbgfs_dir; +} +static int __init kstackwatch_init (void ) {int ret =0 ;
mm/kstackwatch/test.c new file +121 -0
121 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 22/27 mm/ksw: add stack overflow test
In reply to Jinchao WangExtend the test module with a new test case (test1) that intentionally overflows a local u64 buffer to corrupt the stack canary.
show addr of buf and watch_addr of test case
--- mm/kstackwatch/test.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
mm/kstackwatch/test.c +21 -1
@@ -32,6 +32,22 @@ static void test_watch_fire(void)pr_info ("exit of %s\n" , __func__); } +static void test_canary_overflow (void ) +{ +u64 buffer[BUFFER_SIZE ]; + +pr_info ("entry of %s\n" , __func__); +ksw_watch_show (); +pr_info ("buf: 0x%px\n" , buffer); + +/* intentionally overflow */ +for (int i =BUFFER_SIZE ; i <BUFFER_SIZE +10 ; i++) + buffer[i] =0xdeadbeefdeadbeef ; +barrier_data (buffer); + +pr_info ("exit of %s\n" , __func__); +} +static ssize_t test_dbgfs_write (struct file *file,const char __user *buffer,size_t count,loff_t *pos) {
@@ -54,6 +70,9 @@ static ssize_t test_dbgfs_write(struct file *file, const char __user *buffer,case 0 :test_watch_fire ();break ; +case 1 : +test_canary_overflow (); +break ;default :pr_err ("Unknown test number %d\n" , test_num);return -EINVAL ;
@@ -74,7 +93,8 @@ static ssize_t test_dbgfs_read(struct file *file, char __user *buffer,"============ usage ===============\n" "Usage:\n" "echo test{i} > /sys/kernel/debug/kstackwatch/test\n" -" test0 - test watch fire\n" ; +" test0 - test watch fire\n" +" test1 - test canary overflow\n" ;return simple_read_from_buffer (buffer, count, ppos, usage,strlen (usage));
Signature · 1 line
2.43.0
PATCH v8 23/27 mm/ksw: add recursive depth test
In reply to Jinchao WangIntroduce a test that performs stack writes in recursive calls to exercise stack watch at a specific recursion depth.
--- mm/kstackwatch/test.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-)
mm/kstackwatch/test.c +21 -1
@@ -17,6 +17,7 @@static struct dentry *test_file;#define BUFFER_SIZE 32 +#define MAX_DEPTH 6static void test_watch_fire (void ) {
@@ -48,6 +49,21 @@ static void test_canary_overflow(void)pr_info ("exit of %s\n" , __func__); } +static void test_recursive_depth (int depth) +{ +u64 buffer[BUFFER_SIZE ]; + +pr_info ("entry of %s depth:%d\n" , __func__, depth); + +if (depth <MAX_DEPTH ) +test_recursive_depth (depth +1 ); + + buffer[0 ] = depth; +barrier_data (buffer); + +pr_info ("exit of %s depth:%d\n" , __func__, depth); +} +static ssize_t test_dbgfs_write (struct file *file,const char __user *buffer,size_t count,loff_t *pos) {
@@ -73,6 +89,9 @@ static ssize_t test_dbgfs_write(struct file *file, const char __user *buffer,case 1 :test_canary_overflow ();break ; +case 2 : +test_recursive_depth (0 ); +break ;default :pr_err ("Unknown test number %d\n" , test_num);return -EINVAL ;
@@ -94,7 +113,8 @@ static ssize_t test_dbgfs_read(struct file *file, char __user *buffer,"Usage:\n" "echo test{i} > /sys/kernel/debug/kstackwatch/test\n" " test0 - test watch fire\n" -" test1 - test canary overflow\n" ; +" test1 - test canary overflow\n" +" test2 - test recursive func\n" ;return simple_read_from_buffer (buffer, count, ppos, usage,strlen (usage));
Signature · 1 line
2.43.0
PATCH v8 24/27 mm/ksw: add multi-thread corruption test cases
In reply to Jinchao WangThese tests share a common structure and are grouped together.
- buggy():
exposes the stack address to corrupting(); may omit waiting
- corrupting():
reads the exposed pointer and modifies memory;
if buggy() omits waiting, victim()'s buffer is corrupted
- victim():
initializes a local buffer and later verifies it; reports an error if the buffer was unexpectedly modified
buggy() and victim() run in worker() thread, with similar stack frame sizes to simplify testing. By adjusting fence_size in corrupting(), the test can trigger either silent corruption or overflow across threads.
- Test 3: one worker, 20 loops, silent corruption
- Test 4: 20 workers, one loop each, silent corruption
- Test 5: one worker, one loop, overflow corruption
Test 4 also exercises multiple watchpoint instances.
mm/ksw: add KSTACKWATCH_PROFILING to measure probe cost
Introduce CONFIG_KSTACKWATCH_PROFILING to enable optional runtime profiling in KStackWatch. When enabled, it records entry and exit probe latencies (in nanoseconds and CPU cycles) and reports averaged statistics at module exit.
--- mm/kstackwatch/test.c | 186 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-)
mm/kstackwatch/test.c +185 -1
@@ -19,6 +19,20 @@ static struct dentry *test_file;#define BUFFER_SIZE 32#define MAX_DEPTH 6 +struct work_node { +ulong *ptr; +u64 start_ns; +struct completion done; +struct list_head list; +}; + +static DECLARE_COMPLETION (work_res); +static DEFINE_MUTEX (work_mutex); +static LIST_HEAD (work_list); + +static int global_fence_size; +static int global_loop_count; +static void test_watch_fire (void ) {u64 buffer[BUFFER_SIZE ] = {0 };
@@ -64,6 +78,164 @@ static void test_recursive_depth(int depth)pr_info ("exit of %s depth:%d\n" , __func__, depth); } +static struct work_node *test_mthread_buggy (int thread_id,int seq_id) +{ +ulong buf[BUFFER_SIZE ]; +struct work_node *node; +bool trigger; + + node =kmalloc (sizeof (*node),GFP_KERNEL ); +if (!node) +return NULL ; + +init_completion (&node->done); + node->ptr = buf; + node->start_ns =ktime_get_ns (); +mutex_lock (&work_mutex); +list_add (&node->list, &work_list); +mutex_unlock (&work_mutex); +complete (&work_res); + + trigger = (get_random_u32 () %100 ) <10 ; +if (trigger) +return node;/* let the caller handle cleanup */ + +wait_for_completion (&node->done); +kfree (node); +return NULL ; +} + +#define CORRUPTING_MINIOR_WAIT_NS (100000) +#define VICTIM_MINIOR_WAIT_NS (300000) + +static inline void silent_wait_us (u64 start_ns,u64 min_wait_us) +{ +u64 diff_ns, remain_us; + + diff_ns =ktime_get_ns () - start_ns; +if (diff_ns < min_wait_us *1000ULL ) { + remain_us = min_wait_us - (diff_ns >>10 ); +usleep_range (remain_us, remain_us +200 ); + } +} + +static void test_mthread_victim (int thread_id,int seq_id,u64 start_ns) +{ +ulong buf[BUFFER_SIZE ]; + +for (int j =0 ; j <BUFFER_SIZE ; j++) + buf[j] =0xdeadbeef + seq_id; +if (start_ns) +silent_wait_us (start_ns,VICTIM_MINIOR_WAIT_NS ); + +for (int j =0 ; j <BUFFER_SIZE ; j++) { +if (buf[j] != (0xdeadbeef + seq_id)) { +pr_warn ("victim[%d][%d]: unhappy buf[%d]=0x%lx\n" , + thread_id, seq_id, j, buf[j]); +return ; + } + } + +pr_info ("victim[%d][%d]: happy\n" , thread_id, seq_id); +} + +static int test_mthread_corrupting (void *data) +{ +struct work_node *node; +int fence_size; + +while (!kthread_should_stop ()) { +if (!wait_for_completion_timeout (&work_res,HZ )) +continue ; +while (true) { +mutex_lock (&work_mutex); + node =list_first_entry_or_null (&work_list, + struct work_node, list); +if (node) +list_del (&node->list); +mutex_unlock (&work_mutex); + +if (!node) +break ;/* no more nodes, exit inner loop */ +silent_wait_us (node->start_ns, +CORRUPTING_MINIOR_WAIT_NS ); + + fence_size =READ_ONCE (global_fence_size); +for (int i = fence_size; i <BUFFER_SIZE - fence_size; + i++) + node->ptr[i] =0xabcdabcd ; + +complete (&node->done); + } + } + +return 0 ; +} + +static int test_mthread_worker (void *data) +{ +int thread_id = (long )data; +int loop_count; +struct work_node *node; + + loop_count =READ_ONCE (global_loop_count); + +for (int i =0 ; i < loop_count; i++) { + node =test_mthread_buggy (thread_id, i); + +if (node) +test_mthread_victim (thread_id, i, node->start_ns); +else +test_mthread_victim (thread_id, i,0 ); +if (node) { +wait_for_completion (&node->done); +kfree (node); + } + } +return 0 ; +} + +static void test_mthread_case (int num_workers,int loop_count,int fence_size) +{ +static struct task_struct *corrupting; +static struct task_struct **workers; + +WRITE_ONCE (global_loop_count, loop_count); +WRITE_ONCE (global_fence_size, fence_size); + +init_completion (&work_res); + workers =kmalloc_array (num_workers,sizeof (void *),GFP_KERNEL ); +memset (workers,0 ,sizeof (struct task_struct *) * num_workers); + + corrupting =kthread_run (test_mthread_corrupting,NULL ,"corrupting" ); +if (IS_ERR (corrupting)) { +pr_err ("failed to create corrupting thread\n" ); +return ; + } + +for (ulong i =0 ; i < num_workers; i++) { + workers[i] =kthread_run (test_mthread_worker, (void *)i, +"worker_%ld" , i); +if (IS_ERR (workers[i])) { +pr_err ("failto create worker thread %ld" , i); + workers[i] =NULL ; + } + } + +for (ulong i =0 ; i < num_workers; i++) { +if (workers[i] && workers[i]->__state !=TASK_DEAD ) { +usleep_range (1000 ,2000 ); + i--; + } + } +kfree (workers); + +if (corrupting && !IS_ERR (corrupting)) { +kthread_stop (corrupting); + corrupting =NULL ; + } +} +static ssize_t test_dbgfs_write (struct file *file,const char __user *buffer,size_t count,loff_t *pos) {
@@ -92,6 +264,15 @@ static ssize_t test_dbgfs_write(struct file *file, const char __user *buffer,case 2 :test_recursive_depth (0 );break ; +case 3 : +test_mthread_case (1 ,20 ,BUFFER_SIZE /4 ); +break ; +case 4 : +test_mthread_case (200 ,1 ,BUFFER_SIZE /4 ); +break ; +case 5 : +test_mthread_case (1 ,1 ,-3 ); +break ;default :pr_err ("Unknown test number %d\n" , test_num);return -EINVAL ;
@@ -114,7 +295,10 @@ static ssize_t test_dbgfs_read(struct file *file, char __user *buffer,"echo test{i} > /sys/kernel/debug/kstackwatch/test\n" " test0 - test watch fire\n" " test1 - test canary overflow\n" -" test2 - test recursive func\n" ; +" test2 - test recursive func\n" +" test3 - test silent corruption\n" +" test4 - test multiple silent corruption\n" +" test5 - test prologue corruption\n" ;return simple_read_from_buffer (buffer, count, ppos, usage,strlen (usage));
Signature · 1 line
2.43.0
PATCH v8 25/27 tools/ksw: add arch-specific test script
In reply to Jinchao WangAdd a shell script under tools/kstackwatch to run self-tests such as canary overflow and recursive depth. The script supports both x86_64 and arm64, selecting parameters automatically based on uname -m.
--- tools/kstackwatch/kstackwatch_test.sh | 85 +++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 tools/kstackwatch/kstackwatch_test.sh
tools/kstackwatch/kstackwatch_test.sh new file +85 -0
@@ -0,0 +1,85 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +echo "IMPORTANT: Before running, make sure you have updated the config values!" + +usage() { + echo "Usage: $0 [0-5]" + echo " 0 - test watch fire" + echo " 1 - test canary overflow" + echo " 2 - test recursive depth" + echo " 3 - test silent corruption" + echo " 4 - test multi-threaded silent corruption" + echo " 5 - test multi-threaded overflow" +} + +run_test_x86_64() { + local test_num=$1 + case "$test_num" in + 0) echo fn=test_watch_fire fo=0x29 ac=1 >/sys/kernel/debug/kstackwatch/config + echo test0 > /sys/kernel/debug/kstackwatch/test + ;; + 1) echo fn=test_canary_overflow fo=0x14 >/sys/kernel/debug/kstackwatch/config + echo test1 >/sys/kernel/debug/kstackwatch/test + ;; + 2) echo fn=test_recursive_depth fo=0x2f dp=3 wl=8 so=0 >/sys/kernel/debug/kstackwatch/config + echo test2 >/sys/kernel/debug/kstackwatch/test + ;; + 3) echo fn=test_mthread_victim fo=0x4c so=64 wl=8 >/sys/kernel/debug/kstackwatch/config + echo test3 >/sys/kernel/debug/kstackwatch/test + ;; + 4) echo fn=test_mthread_victim fo=0x4c so=64 wl=8 >/sys/kernel/debug/kstackwatch/config + echo test4 >/sys/kernel/debug/kstackwatch/test + ;; + 5) echo fn=test_mthread_buggy fo=0x16 so=0x100 wl=8 >/sys/kernel/debug/kstackwatch/config + echo test5 >/sys/kernel/debug/kstackwatch/test + ;; + *) usage + exit 1 ;; + esac + # Reset watch after test + echo >/sys/kernel/debug/kstackwatch/config +} + +run_test_arm64() { + local test_num=$1 + case "$test_num" in + 0) echo fn=test_watch_fire fo=0x50 ac=1 >/sys/kernel/debug/kstackwatch/config + echo test0 > /sys/kernel/debug/kstackwatch/test + ;; + 1) echo fn=test_canary_overflow fo=0x20 so=264 >/sys/kernel/debug/kstackwatch/config + echo test1 >/sys/kernel/debug/kstackwatch/test + ;; + 2) echo fn=test_recursive_depth fo=0x34 dp=3 wl=8 so=8 >/sys/kernel/debug/kstackwatch/config + echo test2 >/sys/kernel/debug/kstackwatch/test + ;; + 3) echo fn=test_mthread_victim fo=0x6c so=0x48 wl=8 >/sys/kernel/debug/kstackwatch/config + echo test3 >/sys/kernel/debug/kstackwatch/test + ;; + 4) echo fn=test_mthread_victim fo=0x6c so=0x48 wl=8 >/sys/kernel/debug/kstackwatch/config + echo test4 >/sys/kernel/debug/kstackwatch/test + ;; + 5) echo fn=test_mthread_buggy fo=0x20 so=264 >/sys/kernel/debug/kstackwatch/config + echo test5 >/sys/kernel/debug/kstackwatch/test + ;; + *) usage + exit 1 ;; + esac + # Reset watch after test + echo >/sys/kernel/debug/kstackwatch/config +} + +# Check root and module +[ "$EUID" -ne 0 ] && echo "Run as root" && exit 1 +for f in /sys/kernel/debug/kstackwatch/config /sys/kernel/debug/kstackwatch/test; do + [ ! -f "$f" ] && echo "$f not found" && exit 1 +done + +# Run +[ -z "$1" ] && { usage; exit 0; } + +arch=$(uname -m) +case "$arch" in + x86_64|aarch64) run_test_${arch} "$1" ;; + *) echo "Unsupported architecture: $arch" && exit 1 ;; +esac
Signature · 1 line
2.43.0
PATCH v8 26/27 docs: add KStackWatch document
In reply to Jinchao WangAdd documentation for KStackWatch under Documentation/.
It provides an overview, main features, usage details, configuration parameters, and example scenarios with test cases. The document also explains how to locate function offsets and interpret logs.
--- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kstackwatch.rst | 377 ++++++++++++++++++++++++ 2 files changed, 378 insertions(+) create mode 100644 Documentation/dev-tools/kstackwatch.rst
Documentation/dev-tools/index.rst +1 -0
@@ -32,6 +32,7 @@ Documentation/process/debugging/index.rst lkmm/index kfence kselftest + kstackwatch kunit/index ktap checkuapi
Documentation/dev-tools/kstackwatch.rst new file +377 -0
377 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v8 27/27 MAINTAINERS: add entry for KStackWatch
In reply to Jinchao WangAdd a maintainer entry for Kernel Stack Watch.
--- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+)
MAINTAINERS +9 -0
@@ -13615,6 +13615,15 @@ F: Documentation/filesystems/smb/ksmbd.rst F: fs/smb/common/ F: fs/smb/server/ +KERNEL STACK WATCH +M: Jinchao Wang <[email protected]> +S: Maintained +F: Documentation/dev-tools/kstackwatch.rst +F: include/linux/kstackwatch.h +F: include/linux/kstackwatch_types.h +F: mm/kstackwatch/ +F: tools/kstackwatch/ + KERNEL UNIT TESTING FRAMEWORK (KUnit) M: Brendan Higgins <[email protected]> M: David Gow <[email protected]>
Signature · 1 line
2.43.0
Quoted 5 lines · Jinchao Wang
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.
Well, this demonstrates the dangers of keeping this problem siloed within your own exfat group. The fix made in 1bb7ff4204b6 is wrong! It was fixed properly in 7375f22495e7 which lists its Fixes: as Linux-2.6.12-rc2, but that's simply the beginning of git history. It's actually been there since v2.4.6.4 where it's documented as simply:
- some subtle fs/buffer.c race conditions (Andrew Morton, me)
As far as I can tell the changes made in 1bb7ff4204b6 should be reverted.
Quoted 3 lines
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.
So why is the solution here not simply to fix KASAN instead of this giant patch series?
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Jinchao Wang
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.
Well, this demonstrates the dangers of keeping this problem siloed within your own exfat group. The fix made in
1bb7ff4204b6is wrong! It was fixed properly in7375f22495e7which lists its Fixes: as Linux-2.6.12-rc2, but that's simply the beginning of git history. It's actually been there since v2.4.6.4 where it's documented as simply:
Quoted 4 lines
- some subtle
fs/buffer.crace conditions (Andrew Morton, me)As far as I can tell the changes made in
1bb7ff4204b6should be reverted.
Thank you for the correction and the detailed history. I wasn't aware this dated back to v2.4.6.4. I'm not part of the exfat group; I simply encountered a bug that 1bb7ff4204b6 happened to resolve in my scenario. The timeline actually illustrates the exact problem KStackWatch addresses:
a bug introduced in 2001, partially addressed in 2025, then properly fixed months later. The 24-year gap suggests these silent stack corruptions are extremely difficult to locate.
Quoted 7 lines
Quoted 4 lines
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.So why is the solution here not simply to fix KASAN instead of this giant patch series?
KASAN caught 7375f22495e7 because put_bh() accessed bh->b_count after wait_on_buffer() of another thread returned—the stack was invalid. In 1bb7ff4204b6 and my case, corruption occurred before the victim function of another thread returned. The stack remained valid to KASAN, so no warning triggered. This is timing-dependent, not a KASAN deficiency.
Making KASAN treat parts of active stack frame as invalid would be complex and add significant overhead, likely worsening the reproduction prevention issue. KASAN's overhead already prevented reproduction in my environment.
KStackWatch takes a different approach: it watches stack frame regardless of whether KASAN considers them valid or invalid, with much less overhead thereby preserving reproduction scenarios.
The value proposition:
Finding where corruption occurs is the bottleneck. Once located, subsystem experts can analyze the root cause. Without that location, even experts are stuck.
If KStackWatch had existed earlier, this 24-year-old bug might have been found sooner when someone hit a similar corruption. The same applies to other stack corruption bugs.
I'd appreciate your thoughts on whether this addresses your concerns.
Best regards,
Jinchao
[dropping all the individual email addresses; leaving only the mailing lists]
Quoted 19 lines · Jinchao Wang
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Jinchao Wang
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.
Well, this demonstrates the dangers of keeping this problem siloed within your own exfat group. The fix made in
1bb7ff4204b6is wrong! It was fixed properly in7375f22495e7which lists its Fixes: as Linux-2.6.12-rc2, but that's simply the beginning of git history. It's actually been there since v2.4.6.4 where it's documented as simply:Quoted 4 lines
- some subtle
fs/buffer.crace conditions (Andrew Morton, me)As far as I can tell the changes made in
1bb7ff4204b6should be reverted.
Quoted 7 lines
Thank you for the correction and the detailed history. I wasn't aware this dated back to v2.4.6.4. I'm not part of the exfat group; I simply encountered a bug that
1bb7ff4204b6happened to resolve in my scenario. The timeline actually illustrates the exact problem KStackWatch addresses:
a bug introduced in 2001, partially addressed in 2025, then properly fixed months later. The 24-year gap suggests these silent stack corruptions are extremely difficult to locate.
I think that's a misdiagnosis caused by not understanding the limited circumstances in which the problem occurs. To hit this problem, you have to have a buffer_head allocated on the stack. That doesn't happen in many places:
fs/buffer.c: struct buffer_head tmp = {
fs/direct-io.c: struct buffer_head map_bh = { 0, };
fs/ext2/super.c: struct buffer_head tmp_bh;
fs/ext2/super.c: struct buffer_head tmp_bh;
fs/ext4/mballoc-test.c: struct buffer_head bitmap_bh;
fs/ext4/mballoc-test.c: struct buffer_head gd_bh;
fs/gfs2/bmap.c: struct buffer_head bh;
fs/gfs2/bmap.c: struct buffer_head bh;
fs/isofs/inode.c: struct buffer_head dummy;
fs/jfs/super.c: struct buffer_head tmp_bh;
fs/jfs/super.c: struct buffer_head tmp_bh;
fs/mpage.c: struct buffer_head map_bh;
fs/mpage.c: struct buffer_head map_bh;It's far more common for buffer_heads to be allocated from slab and attached to folios. The other necessary condition to hit this problem is that get_block() has to actually read the data from disk. That's not normal either! Most filesystems just fill in the metadata about the block and defer the actual read to when the data is wanted. That's the high-performance way to do it.
So our opportunity to catch this bug was highly limited by the fact that we just don't run the codepaths that would allow it to trigger.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
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.
Quoted 8 lines
Quoted 2 lines
So why is the solution here not simply to fix KASAN instead of this giant patch series?
KASAN caught
7375f22495e7becauseput_bh()accessedbh->b_countafterwait_on_buffer()of another thread returned—the stack was invalid. In1bb7ff4204b6and my case, corruption occurred before the victim function of another thread returned. The stack remained valid to KASAN, so no warning triggered. This is timing-dependent, not a KASAN deficiency.
I agree that it's a narrow race window, but nevertheless KASAN did catch it with ntfs and not with exfat. The KASAN documentation states that it can catch this kind of bug:
Generic KASAN supports finding bugs in all of slab, page_alloc, vmap, vmalloc, stack, and global memory.
Software Tag-Based KASAN supports slab, page_alloc, vmalloc, and stack memory.
Hardware Tag-Based KASAN supports slab, page_alloc, and non-executable vmalloc memory.
(hm, were you using hwkasan instead of swkasan, and that's why you couldn't see it?)
Quoted 80 lines · Matthew Wilcox
[dropping all the individual email addresses; leaving only the mailing lists]
Quoted 19 lines · Jinchao Wang
Quoted 13 lines · Matthew Wilcox
Quoted 5 lines · Jinchao Wang
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.
Well, this demonstrates the dangers of keeping this problem siloed within your own exfat group. The fix made in
1bb7ff4204b6is wrong! It was fixed properly in7375f22495e7which lists its Fixes: as Linux-2.6.12-rc2, but that's simply the beginning of git history. It's actually been there since v2.4.6.4 where it's documented as simply:Quoted 4 lines
- some subtle
fs/buffer.crace conditions (Andrew Morton, me)As far as I can tell the changes made in
1bb7ff4204b6should be reverted.Quoted 7 lines
Thank you for the correction and the detailed history. I wasn't aware this dated back to v2.4.6.4. I'm not part of the exfat group; I simply encountered a bug that
1bb7ff4204b6happened to resolve in my scenario. The timeline actually illustrates the exact problem KStackWatch addresses:
a bug introduced in 2001, partially addressed in 2025, then properly fixed months later. The 24-year gap suggests these silent stack corruptions are extremely difficult to locate.I think that's a misdiagnosis caused by not understanding the limited circumstances in which the problem occurs. To hit this problem, you have to have a
buffer_headallocated on the stack. That doesn't happen in many places:fs/buffer.c: struct buffer_head tmp = { fs/direct-io.c: struct buffer_head map_bh = { 0, }; fs/ext2/super.c: struct buffer_head tmp_bh; fs/ext2/super.c: struct buffer_head tmp_bh; fs/ext4/mballoc-test.c: struct buffer_head bitmap_bh; fs/ext4/mballoc-test.c: struct buffer_head gd_bh; fs/gfs2/bmap.c: struct buffer_head bh; fs/gfs2/bmap.c: struct buffer_head bh; fs/isofs/inode.c: struct buffer_head dummy; fs/jfs/super.c: struct buffer_head tmp_bh; fs/jfs/super.c: struct buffer_head tmp_bh; fs/mpage.c: struct buffer_head map_bh; fs/mpage.c: struct buffer_head map_bh;It's far more common for
buffer_headsto be allocated from slab and attached to folios. The other necessary condition to hit this problem is thatget_block()has to actually read the data from disk. That's not normal either! Most filesystems just fill in the metadata about the block and defer the actual read to when the data is wanted. That's the high-performance way to do it.So our opportunity to catch this bug was highly limited by the fact that we just don't run the codepaths that would allow it to trigger.
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
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.Quoted 8 lines
Quoted 2 lines
So why is the solution here not simply to fix KASAN instead of this giant patch series?
KASAN caught
7375f22495e7becauseput_bh()accessedbh->b_countafterwait_on_buffer()of another thread returned—the stack was invalid. In1bb7ff4204b6and my case, corruption occurred before the victim function of another thread returned. The stack remained valid to KASAN, so no warning triggered. This is timing-dependent, not a KASAN deficiency.I agree that it's a narrow race window, but nevertheless KASAN did catch it with ntfs and not with exfat. The KASAN documentation states that it can catch this kind of bug:
Generic KASAN supports finding bugs in all of slab,
page_alloc, vmap, vmalloc, stack, and global memory.
Quoted 8 lines
Software Tag-Based KASAN supports slab,
page_alloc, vmalloc, and stack memory.Hardware Tag-Based KASAN supports slab,
page_alloc, and non-executable vmalloc memory.(hm, were you using hwkasan instead of swkasan, and that's why you couldn't see it?)
You're right that these conditions are narrow. However, when these bugs hit, they're severe and extremely difficult to debug. This year alone, this specific buffer_head bug was hit at least twice: 1bb7ff4204b6 and my case. Over 24 years, others likely encountered it but lacked tools to pinpoint the root cause.
I used software KASAN for the exfat case, but the bug didn't reproduce, likely due to timing changes from the overhead. More fundamentally, the corruption was in-bounds within active stack frames, which KASAN cannot detect by design.
Beyond buffer_head, I encountered another stack corruption bug in network drivers this year. Without KStackWatch, I had to manually instrument the code to locate where corruption occurred.
These issues may be more common than they appear. Given Linux's massive user base combined with the kernel's huge codebase and the large volume of driver code, both in-tree and out-of-tree, even narrow conditions will be hit.
Since posting earlier versions, several developers have contacted me about using KStackWatch for their own issues. KStackWatch fills a gap: it can pinpoint in-bounds stack corruption with much lower overhead than KASAN.