From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 298512E88B5 for ; Fri, 12 Sep 2025 10:12:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757671948; cv=none; b=g0I0BBRaEfQqOXymBCMcye8RXI70ieLzhgXDLke5H2KnycvR2FIkyoXyiSqx71xBneE92C6jZD5/rCx32rPNIdMSqY4NYVk6m8dVU6tT9JkVX1AlX7f2vSW8VnrgjkeQ47brrYSzsZnzOHd7aRzEGW9fU62rEa4XuIvwjTukYcs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757671948; c=relaxed/simple; bh=HtnvcRfhS9CLWmrVuR1TwMxPtWHSQtXzxVQktoPREbE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=szCrHDeZ2DxxrxFENjGPXPkpx+A+Ih01ver3xERcJkd7AxNx23oZ77JTacyOq5nM6KPASHJPbDi6U4k05c1iGFFnbmR6uGZ8C1GoZw/sEiG3vs0+ZQ97Oy2j/toorMIBqx2TsEu2qfYFgYgqS9Tb6VVHdeYNLLEp79KmbudS7qU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ibJLqtEi; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ibJLqtEi" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-329b760080fso1804644a91.1 for ; Fri, 12 Sep 2025 03:12:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1757671946; x=1758276746; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Btw7JeEHjCHi0fmk72XeSdeksiIjhqX1c6woWjS/nBg=; b=ibJLqtEiEdIp2HmGI1RMpWTh/fGrIXB5E8TsZedpqe6A+E/fjyt41Gf16UNEN9Pzhw D1NQez7s8Zp87JDkNhSw1+cb+UQwceM5zJxpbqUo0uwRbE69dgmXNImUYWEnQZnTMex1 YAWJYBWR/ZKEAuzVUnGEsRJtxa29ScisaJHCefUCQK8VY065T3iNu5UkWLpaQb5dC/7Q Y830TmgGUoTcfBY+tQ7A2IAJKFGRlkqX+WaupBdQG/qGHa6nsI79DzCa9POnIESyeJM8 p88EzBan7TktsTdpAEeB0LEBTxNTE8DD15YYxBudV7n6h+pkx0S/u++6HSNM3PBJLxay AEUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1757671946; x=1758276746; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Btw7JeEHjCHi0fmk72XeSdeksiIjhqX1c6woWjS/nBg=; b=uDk6IBGMEZJvKYQgiwDgM8LdNtu44EF6S+snKHAmw11JZ4yW+2y8FuqAHlHd8z0sn2 d/NitNnormYg8TkuZtzUwroFu3LTAmJTOkKi+B8uob4tytLFK5Lm0HoBIWOnU52Y/dDR Vaj9M65NvI2qrkTpsWMk/aYb+7nWd242TlHPVmou+CG4AuZbRf1LkmXqN40NQS9VaGPS bh2z8qJephLaZ1Uewqod4rJNxvmzbJwYhf8bkdoLUypebZb52pMPj4LVf6WnHsjjVOIP mXikGevHqT88MnH87EJvkRp0qj+qz7TM9bvNyieO3Si0KFTAviG1fQyVa8xOCYUOzpiA qQWg== X-Forwarded-Encrypted: i=1; AJvYcCVG2D9uYbn5+0EzBVDAU5LNqJfCGxYfNWJ1LKchFLoJeECCt5jgA/B9xgT3rGzzwflzZ5muDHqoClg=@vger.kernel.org X-Gm-Message-State: AOJu0YzIBTalHe3gHdMizX/9KP6adnRWFK/ghfDzfZ9vWpkRBw4AZj8q sfezdntJ0mQGp/0+T1xzeYRqJJfCT2+LrrBA9zqIQyhnSWXoWHLI+fiN X-Gm-Gg: ASbGncvLUGxc594iagadl0dHSiDn0Jdp5susA+5T4g+xK6XfSIgE6FwHXqKFO+gXyyJ 8vmctnZcyN+oxyNeXR3bTosw0lmFl4AUxEbcYRW9MEhhgkBRP7PKREWWo428rPtAiqVoFhRJzwi QC3Gy+ZTUYqFbK71+NvGvAIUdDXH17M0+cdFD7CDojo4S7YHzgH57zAJRYK1zVC7CLgZAs3vfG6 +eFDCnsQPvjuxlLCRCTW5tuGz6U1Xuj5km0vhNu2XK73YQ6EFZP179zz1+54IpVISckqQA0T+kR 8blS5iTEiY8wBc2KEHp3NdwyW4e1yVRiohsdAkTuFc7G6ZlxOASxmzfP3CAH6BxeYcC761uCFOB lVyTXIXULKJQC6lMZbLimgxdK7s6PakwgK8k6052GpslQTM8YVw== X-Google-Smtp-Source: AGHT+IE+sH0wcZG9HaKcll5QXaOw6VOwT34jJJKFyIXbetNwLJVfpmHy9Irzue+96yqtkUewE0lXYQ== X-Received: by 2002:a17:90b:534e:b0:32b:df0e:928f with SMTP id 98e67ed59e1d1-32de4fb9a28mr2940174a91.37.1757671946347; Fri, 12 Sep 2025 03:12:26 -0700 (PDT) Received: from localhost ([185.49.34.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-77607a48c19sm4972892b3a.36.2025.09.12.03.12.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Sep 2025 03:12:25 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , Peter Zijlstra , Mike Rapoport , Alexander Potapenko , Jonathan Corbet , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Kees Cook , Alice Ryhl , Sami Tolvanen , Miguel Ojeda , Masahiro Yamada , Rong Xu , Naveen N Rao , David Kaplan , Andrii Nakryiko , Jinjie Ruan , Nam Cao , workflows@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-mm@kvack.org, llvm@lists.linux.dev, Andrey Ryabinin , Andrey Konovalov , Dmitry Vyukov , Vincenzo Frascino , kasan-dev@googlegroups.com, "David S. Miller" , Mathieu Desnoyers , linux-trace-kernel@vger.kernel.org Cc: Jinchao Wang Subject: [PATCH v4 06/21] mm/ksw: add singleton /proc/kstackwatch interface Date: Fri, 12 Sep 2025 18:11:16 +0800 Message-ID: <20250912101145.465708-7-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250912101145.465708-1-wangjinchao600@gmail.com> References: <20250912101145.465708-1-wangjinchao600@gmail.com> Precedence: bulk X-Mailing-List: workflows@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Provide the /proc/kstackwatch 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 --- mm/kstackwatch/kernel.c | 75 +++++++++++++++++++++++++++++++++++- mm/kstackwatch/kstackwatch.h | 3 ++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 1502795e02af..8e1dca45003e 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -3,7 +3,10 @@ #include #include +#include +#include #include +#include #include "kstackwatch.h" @@ -12,6 +15,7 @@ MODULE_DESCRIPTION("Kernel Stack Watch"); MODULE_LICENSE("GPL"); static struct ksw_config *ksw_config; +static atomic_t config_file_busy = ATOMIC_INIT(0); /* * Format of the configuration string: @@ -23,7 +27,7 @@ static struct ksw_config *ksw_config; * - local_var_offset : offset from the stack pointer at function+ip_offset * - local_var_len : length of the local variable(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 *func_part, *local_var_part = NULL; char *token; @@ -92,18 +96,87 @@ static int __maybe_unused ksw_parse_config(char *buf, struct ksw_config *config) return -EINVAL; } +static ssize_t kstackwatch_proc_write(struct file *file, + const char __user *buffer, size_t count, + loff_t *pos) +{ + 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 kstackwatch_proc_show(struct seq_file *m, void *v) +{ + seq_printf(m, "%s\n", ksw_config->config_str); + return 0; +} + +static int kstackwatch_proc_open(struct inode *inode, struct file *file) +{ + if (atomic_cmpxchg(&config_file_busy, 0, 1)) + return -EBUSY; + + return single_open(file, kstackwatch_proc_show, NULL); +} + +static int kstackwatch_proc_release(struct inode *inode, struct file *file) +{ + atomic_set(&config_file_busy, 0); + return single_release(inode, file); +} + +static const struct proc_ops kstackwatch_proc_ops = { + .proc_open = kstackwatch_proc_open, + .proc_read = seq_read, + .proc_write = kstackwatch_proc_write, + .proc_lseek = seq_lseek, + .proc_release = kstackwatch_proc_release, +}; + +const struct ksw_config *ksw_get_config(void) +{ + return ksw_config; +} static int __init kstackwatch_init(void) { ksw_config = kzalloc(sizeof(*ksw_config), GFP_KERNEL); if (!ksw_config) return -ENOMEM; + if (!proc_create("kstackwatch", 0600, NULL, &kstackwatch_proc_ops)) { + pr_err("create proc kstackwatch fail"); + kfree(ksw_config); + return -ENOMEM; + } + pr_info("module loaded\n"); return 0; } static void __exit kstackwatch_exit(void) { + remove_proc_entry("kstackwatch", NULL); kfree(ksw_config); pr_info("module unloaded\n"); diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index 7c595c5c24d1..277b192f80fa 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -35,4 +35,7 @@ struct ksw_config { char config_str[MAX_CONFIG_STR_LEN]; }; +// singleton, only modified in kernel.c +const struct ksw_config *ksw_get_config(void); + #endif /* _KSTACKWATCH_H */ -- 2.43.0