From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 1B4CF2E2EF9 for ; Thu, 9 Oct 2025 10:57:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760007453; cv=none; b=bDgTB1+Pfr/MZG4JRJFVENOnAJIUWWJi4dTPkAOVji/laYvHxnW822TXi3iZ3sBNppdh1kK5KcKApLsRnsGe6jhVCVjrVmTaWVyGZG0F1nsHTvEUacN6kPU1ZDrlLV9udaT4anmb1l41NtylKKQr+zChX1yhR1iJlUO2nOjTDl8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760007453; c=relaxed/simple; bh=NKGG2Deka0iYHCcjeICKwSp0xNn5UzCtr+YC+OdIklo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Aw2QSjuFdSH4GqWOJZhCwRJtQdEE8zIbzfc6rBy6BIbxKUMWqMSAvP06HsFYf/KZJazr5IDHQfrZYRiZKzxJdD7WfScASeI1VTDcEneEo77r8LtkumQvwt1Q4Lmqmns27YZT6kIoEXmzPBW1WfPHyBa0dxrukhjD8m3Be79CYns= 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=h+08Gy0t; arc=none smtp.client-ip=209.85.210.179 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="h+08Gy0t" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-78125ed4052so1005163b3a.0 for ; Thu, 09 Oct 2025 03:57:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1760007451; x=1760612251; darn=lists.linux.dev; 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=tH9NPPXYzjzWmBeV5TNMSRB6I94mkI1pmHDAIM87ayY=; b=h+08Gy0t7nBJKMalmSqCpMuoST5DPITTPWW90JAtaFfwAbkz1fUIZWHx/zBvyX0hP/ /4U09YTxv7p0d+L+OfvX+Z1Hki7WYLWDSq85XpTyVjTF9JtIH1TNx+172X+sUC0N2Z2I Bio5c5newnOO9fPkgDleb4WErUPYOlSVED0Hm7g651kyNDKXT6xhS5AjRmfepPfyCJ92 qe1+An3v4Y2ZOdsaQ6rrTQLvuSyUzWLIoVfabxFufoGmfP1cEbPKznE/QwmpxxNTYi1V BVI9djctuHU1TJli3xSi3NH+E1SsTcJLHmSkx3nf1K8Tt5qcQzb3YPpqAsozeBfziHNY PD3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760007451; x=1760612251; 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=tH9NPPXYzjzWmBeV5TNMSRB6I94mkI1pmHDAIM87ayY=; b=GX+2LsaGqJcKD9ngOoTNAVCvnchS5APrdF5oje+qdDWWa0vGIGXH/Z0TAIdJOwuhFb V2rAckwG3zEK2hW5wFVV40hHdvyc+6D+3wJkTg+T+VwCN8IRta56EGtH60o9xRKKYmKy yL6emErai8uRf/7OLR09YPjMry4VIILTS0nTLEa7y8bIBPZjF0DWiRS9LaB2muuGOPLE wp/4a4pbaOl6EJDbvdgr/EB/KGvX4I6Di0gPu5f75UtBwfMvbuFzEKyMJnBMxsYO5W+Z ppuNsieV8cLZUj8lyMgJUoTqTFQJkoXuRTZ2sHVaiFIvzbhB1GIeuUeshjzBq5ttfYTQ BJ2w== X-Forwarded-Encrypted: i=1; AJvYcCVblkPycBlTzdyXKiTZrtZnX846RsiuwGY01PhpLHM0Czqthfp4SoKCtp59jPs5YCrPtULX@lists.linux.dev X-Gm-Message-State: AOJu0YygQRbzwqBN4pPP7/67E6dqkc5vn+WLAsicdGx9RcjiZl/vif11 BOJybT/7h1OO+KPH21NYdHznhTlLzr4Y1I4BnIias25dwP1XIwj74kTc X-Gm-Gg: ASbGncthxgTvag5jJoP8wE8Gq0OvD2OIoA3jaN2H2IhpB9MIE8Txhig31J9KR/A9upu CW5BPRUWTA9KDaGeicCwqRmRGQSV5VkBAdrbZV57BmjYiAPnKrBEHJgF6OrDQWVxl8uG01ywlaQ 4RY3wvcI+aU0DDdCzj2rlFjG2/y6WRuhoFPRTNLsI9do4i8+HksWB548Mw5nfVyyX12uIGodOWa T5WcOUxo6Paw5DkLPaLCrxyL9DBldtFigI1FLgp8l8f4BwiXsff02yR7dBpvk/BsZSAtb/jE5sh LK7QSZqClNEMqvKg/hYNzV9g4ZC8fPmqM05WII4SJ7Xvn9C3GjelrYi7ReC0o62YQvR/Vi+VceE hVQ93EvxOAjB42wlBaEviGlOQiCMdZFYFSosXJ435d1Ez/Z4OUV6tfHAY/fb7 X-Google-Smtp-Source: AGHT+IFHk/BtBSKimaKQm5uYl91w8zgkVCHa9z7abhTtJzmBXj1LTCWyaE902ZK//m0NEMBKQHd4Bw== X-Received: by 2002:a05:6a00:4fd0:b0:783:7de9:d3ca with SMTP id d2e1a72fcca58-79387c1a74bmr8499688b3a.31.1760007451210; Thu, 09 Oct 2025 03:57:31 -0700 (PDT) Received: from localhost ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-794d4b3d866sm2531315b3a.11.2025.10.09.03.57.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Oct 2025 03:57:30 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , Peter Zijlstra , Mike Rapoport , Alexander Potapenko , Randy Dunlap , Marco Elver , 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 v7 06/23] mm/ksw: add singleton debugfs interface Date: Thu, 9 Oct 2025 18:55:42 +0800 Message-ID: <20251009105650.168917-7-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251009105650.168917-1-wangjinchao600@gmail.com> References: <20251009105650.168917-1-wangjinchao600@gmail.com> Precedence: bulk X-Mailing-List: llvm@lists.linux.dev 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 --- mm/kstackwatch/kernel.c | 104 +++++++++++++++++++++++++++++++++-- mm/kstackwatch/kstackwatch.h | 3 + 2 files changed, 103 insertions(+), 4 deletions(-) diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 3b7009033dd4..898ebb2966fe 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 "kstackwatch.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 */ @@ -74,7 +79,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; @@ -109,20 +114,111 @@ 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"); } diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index a7bad207f863..983125d5cf18 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -29,4 +29,7 @@ struct ksw_config { char *user_input; }; +// singleton, only modified in kernel.c +const struct ksw_config *ksw_get_config(void); + #endif /* _KSTACKWATCH_H */ -- 2.43.0