[PATCH v8 06/27] mm/ksw: add singleton debugfs interface

Jinchao Wang
2025-11-10T16:37:09Z

Provide 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.

Signed-off-by: Jinchao Wang <wangjinchao600@gmail.com>
---
include/linux/kstackwatch.h | 3 ++
mm/kstackwatch/kernel.c | 103 ++++++++++++++++++++++++++++++++++--
2 files changed, 103 insertions(+), 3 deletions(-)

diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h
index dd00c4c8922e..ada5ac64190c 100644
--- a/include/linux/kstackwatch.h
+++ b/include/linux/kstackwatch.h
@@ -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 */
diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c
index 50104e78cf3d..87fef139f494 100644
--- a/mm/kstackwatch/kernel.c
+++ b/mm/kstackwatch/kernel.c
@@ -1,13 +1,18 @@
// SPDX-License-Identifier: GPL-2.0
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

+#include <linux/debugfs.h>
#include <linux/kstackwatch.h>
#include <linux/kstrtox.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/string.h>
+#include <linux/uaccess.h>

+static atomic_t dbgfs_config_busy = ATOMIC_INIT(0);
static struct ksw_config *ksw_config;
+static struct dentry *dbgfs_config;
+static struct dentry *dbgfs_dir;

struct param_map {
const char *name; /* long name */
@@ -76,7 +81,7 @@ static int ksw_parse_param(struct ksw_config *config, const char *key,
* - sp_offset |so (u16) : offset from stack pointer at func_offset
* - watch_len |wl (u16) : watch length (1,2,4,8)
*/
-static int __maybe_unused ksw_parse_config(char *buf, struct ksw_config *config)
+static int ksw_parse_config(char *buf, struct ksw_config *config)
{
char *part, *key, *val;
int ret;
@@ -111,18 +116,110 @@ static int __maybe_unused ksw_parse_config(char *buf, struct ksw_config *config)
return 0;
}

+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);
+}
+
+static ssize_t ksw_dbgfs_write(struct file *file, const char __user *buffer,
+ size_t count, loff_t *ppos)
+{
+ char input[MAX_CONFIG_STR_LEN];
+ int ret;
+
+ if (count == 0 || count >= sizeof(input))
+ return -EINVAL;
+
+ if (copy_from_user(input, buffer, count))
+ return -EFAULT;
+
+ input[count] = '\0';
+ strim(input);
+
+ if (!strlen(input)) {
+ pr_info("config cleared\n");
+ return count;
+ }
+
+ ret = ksw_parse_config(input, ksw_config);
+ if (ret) {
+ pr_err("Failed to parse config %d\n", ret);
+ return ret;
+ }
+
+ return count;
+}
+
+static int ksw_dbgfs_open(struct inode *inode, struct file *file)
+{
+ if (atomic_cmpxchg(&dbgfs_config_busy, 0, 1))
+ return -EBUSY;
+ return 0;
+}
+
+static int ksw_dbgfs_release(struct inode *inode, struct file *file)
+{
+ atomic_set(&dbgfs_config_busy, 0);
+ return 0;
+}
+
+static const struct file_operations kstackwatch_fops = {
+ .owner = THIS_MODULE,
+ .open = ksw_dbgfs_open,
+ .read = ksw_dbgfs_read,
+ .write = ksw_dbgfs_write,
+ .release = ksw_dbgfs_release,
+ .llseek = default_llseek,
+};
+
+const struct ksw_config *ksw_get_config(void)
+{
+ return ksw_config;
+}
+
static int __init kstackwatch_init(void)
{
+ int ret = 0;
+
ksw_config = kzalloc(sizeof(*ksw_config), GFP_KERNEL);
- if (!ksw_config)
- return -ENOMEM;
+ if (!ksw_config) {
+ ret = -ENOMEM;
+ goto err_alloc;
+ }
+
+ dbgfs_dir = debugfs_create_dir("kstackwatch", NULL);
+ if (!dbgfs_dir) {
+ ret = -ENOMEM;
+ goto err_dir;
+ }
+
+ dbgfs_config = debugfs_create_file("config", 0600, dbgfs_dir, NULL,
+ &kstackwatch_fops);
+ if (!dbgfs_config) {
+ ret = -ENOMEM;
+ goto err_file;
+ }

pr_info("module loaded\n");
return 0;
+
+err_file:
+ debugfs_remove_recursive(dbgfs_dir);
+ dbgfs_dir = NULL;
+err_dir:
+ kfree(ksw_config);
+ ksw_config = NULL;
+err_alloc:
+ return ret;
}

static void __exit kstackwatch_exit(void)
{
+ debugfs_remove_recursive(dbgfs_dir);
+ kfree(ksw_config->func_name);
+ kfree(ksw_config->user_input);
kfree(ksw_config);

pr_info("module unloaded\n");
--
2.43.0