From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 7227123E35F for ; Tue, 30 Sep 2025 02:44:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759200293; cv=none; b=Rt8B38OABhQiu37ddTVw0V4RyCfcW2XbLd/t3HIh+fbLA3FcRLTWDJHuJsfS3iF/klK1qyJRBXzsHsQU59NmvokvR2GcfRESlfwdKKSfE+zERjaY7P2zq5joUgeNzoQPGd0LWQwzkZdcf39ZA18jKAjXzW5ZQJ2p3EwP35/1BCo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759200293; c=relaxed/simple; bh=NKGG2Deka0iYHCcjeICKwSp0xNn5UzCtr+YC+OdIklo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g4RCFU7pBbYotJKkWca45Zzk6c1Gd4+01qQVRMP5RSgZyrBV3lyS5Ukw9vL+s1NmIzhNEVZsEcBytmM4tHIlXdAH+o6pQZpMZx/+DqR34TtxDAwbosCOkZL77BqnwZ8iPXUUzU3m+Z0xrOoDnnRjPvIas0ivEoOkcnYEkb3QnA4= 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=iJU7h/hq; arc=none smtp.client-ip=209.85.216.51 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="iJU7h/hq" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3324fdfd54cso5658227a91.0 for ; Mon, 29 Sep 2025 19:44:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759200291; x=1759805091; 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=tH9NPPXYzjzWmBeV5TNMSRB6I94mkI1pmHDAIM87ayY=; b=iJU7h/hq+0Zl+S5CJiDaa4ggLdpamAziSTzoQj64IOgBovCo+YDbRLFBTugApzi8Dk hkCRsIT8iVELG8JajFpmPm/Nce+mjSnr0ZOZSN09wSO1T/BDahZ3q+C1yx4OsFU8ivs6 8QxReTYLt950fQKnijDmHD5zK51G20XPoQ9aWfk0Ott1XSwI4u7D1h4JEEJwLi9e1bK1 ZIHTssvnxpDrxbOu/16G+y+uQHneJyTluqq99xjbiQm5PwNcGF5IF/XiXIdgxeQhIgQa vYRLg8bnzzIQqWezNxOsY2SjzrSZ9k0hugo5XnyUB0fEA5d86nk37HACXYKF/kyC09ie tj9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759200291; x=1759805091; 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=hRxfY0k/POU/jnGLxURgSRpS3hg02GPiK1b+X8Chb4HaSWAt9YICxgK6/bmbSuTozM fmqHN1NRFoJRcB1gX5RNEZ4+mFA4KKv9nfHnzfXZeRroQjD+XAqa59ABXavY0lUVLoqr LoXj+esMp08spvtyG7/M6OFlhxslD8/OpOW2I8BIngPRfFlH5nc/fMymVhLHVjQvOKPU c2rR2YAxw8HWVQChTlCST9SZNaXSEfIx9rv0TKFk5scMyOZB2jU4f1zzlPQOCnBipkOJ pQs2vgI0PQ/vD8TJJZTz9hIvS4tbXTp3IiBTA6HTg1HP9w+W/kEIEPWaHccsefxu3dFE nZ0Q== X-Forwarded-Encrypted: i=1; AJvYcCUmjWc3Tzp0eAQPUoYeYSDT18V4A56qb5ka5agcaae5oql2mex+shO0TJEdW1G0PNoT0jv4YdK9zYg=@vger.kernel.org X-Gm-Message-State: AOJu0YySEG3qqZMZhf3NnWx6MEYdBtemRT88b+GEN96t//sNCq4Ak+Ve 3eI5t8ZoZmLO8F93P3lPIlxxFtnlkok+Kk4iZItlp3A35+dMcd4V/epA X-Gm-Gg: ASbGnctFLynUoAEYkUfQomooNfzRLHYNLgcS7fSYJwzx7yPK1lsdvZrHt5fQvJELBI/ qFcQdUplFnJPThGqz1i+KGhmy1p+I2jqapW2qDKV0nBKNdKMj1AB21RKChyFrTh38DPQDUFokmw SK5OUiwVAsSiif81DqWBIsbUs4VQuHRlOTzxPwLV5WPesMSp/N8gd1rdi7fte6k1zLTvUgPF6ka Y76SmvZzWqY69UxjwcZJobBzE2enJU3k2qD+0DaKHdaPwfS2r3m05ezO6I6wdEpcPfwYUmPi2og UpIAp5Kodyt5JQ7EaC0H9lzgNWgOEFGjYMSicYlP7piMdwDcBgysttKVBnnED7TvC3fFzGDEkV8 2aPWli5U/G+3ux8yCsPuLVPVaBFWLxSRBtsGerVj3SmN2P5VZgyz2089mj4aLigMozGiyiHpdv5 wT X-Google-Smtp-Source: AGHT+IFVE9O//P1+ssBUOG8eBG1xptc20Z1rkKT0r8Peul+CYAeELZZLuPAbA8MMh6puLAlFfjJIyA== X-Received: by 2002:a17:90b:3ece:b0:32b:6145:fa63 with SMTP id 98e67ed59e1d1-3342a216f73mr19097573a91.4.1759200290701; Mon, 29 Sep 2025 19:44:50 -0700 (PDT) Received: from localhost ([45.142.167.196]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3341be38382sm18526671a91.25.2025.09.29.19.44.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Sep 2025 19:44:50 -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 v6 06/23] mm/ksw: add singleton debugfs interface Date: Tue, 30 Sep 2025 10:43:27 +0800 Message-ID: <20250930024402.1043776-7-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250930024402.1043776-1-wangjinchao600@gmail.com> References: <20250930024402.1043776-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 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