Connect the watch and stack functions to complete
the kstackwatch initialization sequence with proper ordering and
error handling.

This patch integrates the previously implemented components:

Initialization sequence:
1. ksw_watch_init() - Pre-allocate HWBP on all CPUs (must be first)
2. ksw_stack_init() - Register kprobes for function entry/exit
3. Set watching_active flag to enable operation

The ordering is critical because:
- HWBP must be pre-allocated before kprobes are registered
- Kprobe handlers depend on pre-existing HWBP infrastructure

Cleanup sequence:
1. ksw_stack_exit() - Unregister kprobes (stops new activations)
2. ksw_watch_exit() - Release pre-allocated HWBP resources
3. Clear watching_active flag

This completes the functional kstackwatch implementation, enabling
real-time stack corruption detection through the proc interface
with automatic HWBP management.

Signed-off-by: Jinchao Wang <wangjinchao...@gmail.com>
---
 mm/kstackwatch/kernel.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c
index 726cf3f25888..b6366808e891 100644
--- a/mm/kstackwatch/kernel.c
+++ b/mm/kstackwatch/kernel.c
@@ -26,11 +26,29 @@ MODULE_PARM_DESC(panic_on_catch,
 
 static int start_watching(void)
 {
+       int ret;
+
        if (strlen(ksw_config->function) == 0) {
                pr_err("KSW: No target function specified\n");
                return -EINVAL;
        }
 
+       /*
+        * watch init will prealloc HWBP
+        * so it must be before stack init
+        */
+       ret = ksw_watch_init(ksw_config);
+       if (ret) {
+               pr_err("KSW: ksw_watch_init ret: %d\n", ret);
+               return ret;
+       }
+
+       ret = ksw_stack_init(ksw_config);
+       if (ret) {
+               pr_err("KSW: ksw_stack_init ret: %d\n", ret);
+               ksw_watch_exit();
+               return ret;
+       }
        watching_active = true;
 
        pr_info("KSW: start watching %s\n", ksw_config->config_str);
@@ -39,6 +57,8 @@ static int start_watching(void)
 
 static void stop_watching(void)
 {
+       ksw_stack_exit();
+       ksw_watch_exit();
        watching_active = false;
 
        pr_info("KSW: stop watching %s\n", ksw_config->config_str);
-- 
2.43.0


Reply via email to