From nobody Thu Oct 2 20:23:05 2025 Received: from mail-pj1-f45.google.com (mail-pj1-f45.google.com [209.85.216.45]) (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 517292E8B78 for ; Fri, 12 Sep 2025 10:12:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757671950; cv=none; b=c1aCW1cdA3jrjVTvP1pPmsEUnDTjDnWvV1DaTFb7Vf7RKtEk0j4u3N7fvBDEpujNdUuVghHfvVvlAM8DyeJpHmZheYaRcXAv9iSjz1TPhNdtMxyhjxA9ZouIpJHYeelPWZCI7vcysLyLv2Q3noP/XlUMWl+2qxlCgn/EWNuDFVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757671950; c=relaxed/simple; bh=HtnvcRfhS9CLWmrVuR1TwMxPtWHSQtXzxVQktoPREbE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SxM4hHe0tgisUkrVJXq2bPZsrBNmFlnxqm+J4MB5bAxQCrdBvCBndy5kIyaRtoDJEa4BE1I/gVsksVBZjFJ9foGvUTQjbeq7YPcwXkBXMKbhywuHx0Kyf7d06NtAia5YZ+lWvydmLFcFd3IiyCU3HaJQbPuQVn2zx9tZsmxvnTk= 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.45 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-f45.google.com with SMTP id 98e67ed59e1d1-329b760080fso1804645a91.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=Ws12OkdudN4GCKYNFroQ0zwLBlnGfMl1igbhemH6piSUbxQWUjY8ShrGWoQ1xvV7Gg 7WkxfzxSmirkYjRmB72xZCV3hm1/LFyvTPXG9ZaqjU064C1hZuZUdLaCHvlD8LB57Rdy njX4sr/4wJPYv1DveCPC7baPEnkN3tF22Fv1imzYPPGHuC4MGbw1tV0EOV0vjBZVcY/S bwxlVzHJzPzP982phQWULkbSR6KOXtQNMD3MPuFigEXyrJgiqkhOeKVBFpaphkcf4/Un cNZSwrEXmH27sj91ENxZy7+qelRXjpDKLw2QFPzN9RXZmsjUTCgL/9ApHpDZnaX7VUdV aj/w== X-Forwarded-Encrypted: i=1; AJvYcCVDK6eGEl76qxkHwd7UIaOcaX1rfDw5z6laKvar3huS37w0wx1oK4yPli7ewBVJBcEJj4IZ+fE2CZogBog=@vger.kernel.org X-Gm-Message-State: AOJu0Yz8Xq7W6wLBsJFwJdmC+lgzXRpPYusVKQogpBbpnfqAK7PJ6tji y7s+Av8s5fgLPJrv68byBKVWSa8gtbR2JFQv+8hwpHHjyYEls7ieQqOR X-Gm-Gg: ASbGnctx2cGpfQa+iDJmhC5i4BI+zG/vSVC41jS0NhdXsabcsRGPn79qBFbTnYy7BqA TdANfNlS6lyPScDEOo4Mx27sUTuHH5qHCakX8hQcBBTSxzj/CL49781Ab4pidGR5Br8qRi7B2/w IaA6KyU43dVhdJh3Lqv5+zTpV99004mvj78hW+l3C4WVXxLIO/fQxvHBmV8DkKVVtBz2p/ILHLP QZGd8gaLeFXznR8OZjjGNl0vmJWgAUSBmzzMOnzdMx/YTItoXcfaDH/ti9B6qMS9LFfJTIkpFfP 3NWM/9Q6f4WldeuRwJNZlQEYpGy5ZYnSylT8ozsAGUVVh2hvxWT1PqNA3T2Beo305J2n3WaoDRM 35Wt0WN7JdT72PcZ3Hf9pNY4iqKWQ4RckRqNM2/c3gcSm7SgU5g== 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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 @@ =20 #include #include +#include +#include #include +#include =20 #include "kstackwatch.h" =20 @@ -12,6 +15,7 @@ MODULE_DESCRIPTION("Kernel Stack Watch"); MODULE_LICENSE("GPL"); =20 static struct ksw_config *ksw_config; +static atomic_t config_file_busy =3D ATOMIC_INIT(0); =20 /* * 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 *c= onfig) +static int ksw_parse_config(char *buf, struct ksw_config *config) { char *func_part, *local_var_part =3D NULL; char *token; @@ -92,18 +96,87 @@ static int __maybe_unused ksw_parse_config(char *buf, s= truct ksw_config *config) return -EINVAL; } =20 +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 =3D=3D 0 || count >=3D sizeof(input)) + return -EINVAL; + + if (copy_from_user(input, buffer, count)) + return -EFAULT; + + input[count] =3D '\0'; + strim(input); + + if (!strlen(input)) { + pr_info("config cleared\n"); + return count; + } + + ret =3D 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 =3D { + .proc_open =3D kstackwatch_proc_open, + .proc_read =3D seq_read, + .proc_write =3D kstackwatch_proc_write, + .proc_lseek =3D seq_lseek, + .proc_release =3D kstackwatch_proc_release, +}; + +const struct ksw_config *ksw_get_config(void) +{ + return ksw_config; +} static int __init kstackwatch_init(void) { ksw_config =3D kzalloc(sizeof(*ksw_config), GFP_KERNEL); if (!ksw_config) return -ENOMEM; =20 + 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; } =20 static void __exit kstackwatch_exit(void) { + remove_proc_entry("kstackwatch", NULL); kfree(ksw_config); =20 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]; }; =20 +// singleton, only modified in kernel.c +const struct ksw_config *ksw_get_config(void); + #endif /* _KSTACKWATCH_H */ --=20 2.43.0