From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (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 4E9DC3090F1 for ; Mon, 10 Nov 2025 16:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792631; cv=none; b=gYc91bdB9FNloS7fWj/eRlrwpU6Kb0VNhkTKfKiGqUuKly5318lzAqV8I6lS6WeFQ/BNy3ZHHm+JW+CacNEbWehhA6dy0J7fxM4DlgXiubJpMPhaSaIxsaMxiWh6ajVwcbg99mAe/peAiYMx1uhHAjpOseQYmCz1CG8yZHXiySM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792631; c=relaxed/simple; bh=Ima2e4/C5WqdeZyYCnDIwbDgz9ZYOLESM4kickHutQ8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PW8p0pyz/tATG7iDSOi+jTmZHkGTRLJ3Ilwxd/mpAmP6rjmdDs428E+bJ1GS24nHhHdiEFNxFB2PW6x/2rr3PmLZ6jbJjo90T4khorvqanHwVlprL/Drtk/weCRKwKIY7Iu4LMGVRNa2S91f2SJQm35Kbv3gi7Abn50lNiCyMHI= 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=PSIZCYhG; arc=none smtp.client-ip=209.85.214.181 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="PSIZCYhG" Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-298145fe27eso15208505ad.1 for ; Mon, 10 Nov 2025 08:37:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792629; x=1763397429; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=RDpE0CvbPiS95CD/e7bDhFl1FHfdcNhWeVLe6GjR39g=; b=PSIZCYhGt4Dd7vdyEl0mOkvONblClWeuS4QZ5ZGZ0MedDHaXvNpz8ADNwK6qenO/nl Pb/UnAJOqDscQSNuGq5DvTveWqzQNJRjZR74EfcpeSq51CYCe8HZQf1eSXkClT3lN4v5 jDA4Uf1eMXEwoFGNz7xw5vivscquUJFUyP3J6VHHZKqUBpHtJy+DPu7PXVAYSUuoVyJP aB4usMrzfDrouMV5Cvutjl1EAzaR4YDI8xNeDhDMscnv5m1YQVAjiAs0jvblEGrrVfpv i1/D/oTtCvkXRE+Waa0EpWEcbdCvHLnzoFCbgrWsj1I31g3LKULWa5rQBUPlBE/yXd+A DXAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792629; x=1763397429; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=RDpE0CvbPiS95CD/e7bDhFl1FHfdcNhWeVLe6GjR39g=; b=pVu54gIJZBzPizWrlv/76q/DiB7ho3DbX6mauv9YE5jSTL3+j2N//I0O+Gber8hOYI 6TOA7s1LPDKDs7UwJr3KGQGYdlq4ZlbQCvNlletcmYHYfqsU51Y/XLKXM4STjkcbqA6A b5Vk7nmyptWL5A9xkRtAbO1ZEDnzlx4jPfgOudvCaJjMwNUNJi6JAqW8EBQE0xL8xfvU jqZzYN0T8or/vMuTEec3JOfFMnKoAsGQPHUx8d2D2V3MOtUMI2kmbgqwIt7cd4Hr89u+ l3X7KpJ+akytxuveFmSIY8Dy/1iul4SLphxVj2Gs5HkMJC+3GrmV/ez91UO6ZZ9crDbk xY9w== X-Forwarded-Encrypted: i=1; AJvYcCW+AL07W/R5r3thZJdCtWE6hX/jCFPtmcaNeSrzu1WvGSm6DZgQ7fmmgAS/WuDoO12pMHnBLFTBr+4=@vger.kernel.org X-Gm-Message-State: AOJu0YywFgd4rD3khrA2R0FdTd85TF97qfg16trhWz8hYFQj7sLeVL0w YxhP8+J//Za1qAWdhkGOyC8y3L41EZUpShLugfEYh+ABNT0Cue02gVtv X-Gm-Gg: ASbGnctUuKmeYh4DjxFm93Tdjcfu8A2/JLAg2L2j7zkvQbfKMJeehquAmnHXkD9GE1s tyIqXgGBNbyWZargQr+q6KyBpa1YVnIpvVQkzWYghi3GRhMYzMr0ng8h42NxHjHVl7YBofozldo Wb1FQs2N35/jRzOi0SZagr6mV7wfpeDOr+9r16mCIW7MimZZv8aBTUapCykFIQ2URSl+fT6+hLr SMiD7Z0ktmVOGEqP+2AZdP0yJcb2v5WrJ9XCBWvKnX8UvBbbre1zQOdyM7dPk9lZjT/Sj6QoGDa YHuS8gzIqvQuWDKLZv0Y3Mf6msGeXgahAMAos0Nbu/HLOJF0+mT31JIPA0an5JuekqGSbmH4TPq +OXyJk8JYMnyDMQG/qMlglLKsfX1sKYGTBe2FezRpSidjfZWqGDQ02Or+PnRGoq3rAySG+9ocjU VxKnrS9OHft2JVPuWAb1V8Hg== X-Google-Smtp-Source: AGHT+IHLWcFC7W3aH7MtEyBpRAAfyoGWF1TXnIybIjXPJD9gYvIPg0ozGaDjCXeoqvDbHEVRJoG18g== X-Received: by 2002:a17:902:d481:b0:295:9b73:b15c with SMTP id d9443c01a7336-297e56f9c21mr125602655ad.42.1762792628683; Mon, 10 Nov 2025 08:37:08 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29650c5ce87sm150073335ad.29.2025.11.10.08.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:08 -0800 (PST) From: Jinchao Wang To: Andrew Morton , "Masami Hiramatsu (Google)" , Peter Zijlstra , Randy Dunlap , Marco Elver , Mike Rapoport , Alexander Potapenko , Adrian Hunter , Alexander Shishkin , Alice Ryhl , Andrey Konovalov , Andrey Ryabinin , Andrii Nakryiko , Ard Biesheuvel , Arnaldo Carvalho de Melo , Ben Segall , Bill Wendling , Borislav Petkov , Catalin Marinas , Dave Hansen , David Hildenbrand , David Kaplan , "David S. Miller" , Dietmar Eggemann , Dmitry Vyukov , "H. Peter Anvin" , Ian Rogers , Ingo Molnar , James Clark , Jinchao Wang , Jinjie Ruan , Jiri Olsa , Jonathan Corbet , Juri Lelli , Justin Stitt , kasan-dev@googlegroups.com, Kees Cook , "Liam R. Howlett" , "Liang Kan" , Linus Walleij , linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-perf-users@vger.kernel.org, linux-trace-kernel@vger.kernel.org, llvm@lists.linux.dev, Lorenzo Stoakes , Mark Rutland , Masahiro Yamada , Mathieu Desnoyers , Mel Gorman , Michal Hocko , Miguel Ojeda , Nam Cao , Namhyung Kim , Nathan Chancellor , Naveen N Rao , Nick Desaulniers , Rong Xu , Sami Tolvanen , Steven Rostedt , Suren Baghdasaryan , Thomas Gleixner , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Valentin Schneider , Vincent Guittot , Vincenzo Frascino , Vlastimil Babka , Will Deacon , workflows@vger.kernel.org, x86@kernel.org Subject: [PATCH v8 06/27] mm/ksw: add singleton debugfs interface Date: Tue, 11 Nov 2025 00:36:01 +0800 Message-ID: <20251110163634.3686676-7-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251110163634.3686676-1-wangjinchao600@gmail.com> References: <20251110163634.3686676-1-wangjinchao600@gmail.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 #include #include #include #include #include +#include +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