From nobody Thu Oct 2 02:15:04 2025 Received: from mail-pj1-f49.google.com (mail-pj1-f49.google.com [209.85.216.49]) (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 6C57123E23C for ; Tue, 30 Sep 2025 02:44:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759200294; cv=none; b=jw1f05wNFNIGDcC31t/mnEqWi/+ZB011WimFYs3AOxzEWkOR9VP/+GOYO6UR+Xq5REw/fZUtNQT1iuL+8TEqUbtOT+dMP/zO3/SLHYBcfxV2hj1E/6tXwCUo/S2ImYStNYYtR6Td7XsvV/3HdKbghNzTAzMNHP5W6V3iuV/0T3A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759200294; c=relaxed/simple; bh=NKGG2Deka0iYHCcjeICKwSp0xNn5UzCtr+YC+OdIklo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CQh3cZbf5YiJa/9uv5gyLJNETG1EkmU/ANhDdiP30BCqwjU6V7DxlX++2fl/OHIYrGm3+RFtVOyh/HQBoTxQbmyAJ0C9Jc10265jg3JMfncOAziZhgyZIAqQl9gaChjqVv2a8SSi7mF9j8RdRnY4vP4ehcnb6IvEtKO6r29iREQ= 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.49 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-f49.google.com with SMTP id 98e67ed59e1d1-329a41dc2ebso4463180a91.3 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=RLlPSOeCPJPLBvCx9TYDFEgvAiusmk6LtO1qVARcGGOd+Ztf7/CNmWhuqzvX4bc8/c TUmlEjLg73lRMB2A9dXfHeA6UYYCD133N6puuhwjnUTZ/LTm8zatV+a4GdqeCgbxGD0v P6oxlgltrbvmtUUztUJx3NSVE2PBKP4RX2EWauZiXqVebwvwfHzHJOUZEvAlv/ZyrBmZ frCb//8+jUBf3L2Ti7KjeSYDXGp4gVzEHF4f3TXmmOSH6lZWtZrkS2RCsVoG+7uabuHG t4o84eUwPPyFr3gZxNnZnKa0uqF3YIdUMzUVkoEzdDO866WQeZHSN3SPFDw2IhxpzX+u Y/tA== X-Forwarded-Encrypted: i=1; AJvYcCVY8odYz2dbZwLFHBBNBPqoyQ3wcEfmsG4g5qyQmGRAli/RjgfVwm1GZgp82dAdT5opxXOPHV6jrhiXYRU=@vger.kernel.org X-Gm-Message-State: AOJu0YxX2yn1CMs67cdOmXaJ0A79JgQZ+MSCz2Gs82/zLvrAD93K7PwJ ID1r3H06aHvJ/y9m3zYf1od88hm456qQIgcPAGWf/d2Z92Mxadq65pc0 X-Gm-Gg: ASbGncvLBYfeonyjlSOLltjqnLlG57qYG4S9/l/sXO9UpPrWrJrughuXIZVhw51lb51 npSkavwW4amocgsEcUpEsGnH4eU422X4qZ8juTkYvKHg6cAsNBuDubvj+XCNbaHKdMVNC0H7l4+ qoKNn3Gtj/ihDCq53hPbs7WC6+CkvcHeL8tXAQPUDigQbUUX2TbqEZZcJOBNifLtG/Ndo1yMcPZ wlekaEapKNH/ZmOOPiU/xDGlKCJLQsFVC4EAB37Ee6AEi3DM6B3beSlRreWpKERUU3qsbJHDD4Y HwAIVler0EAZ5VpvL3Bqci8aV1ZbZgM9V1fGycEEHzKOoqZTxb8KTVasJxXsLtQm1bB+/8io7NX 1f+fUu8yQInDH7AaY3nGqMqNDx6way1ozpgqvU68qhlE0qsH4cSzcardfWTXq0ZL3krYF6bpN7F I+ 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: 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 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 =20 +#include #include #include #include +#include =20 #include "kstackwatch.h" =20 +static atomic_t dbgfs_config_busy =3D ATOMIC_INIT(0); static struct ksw_config *ksw_config; +static struct dentry *dbgfs_config; +static struct dentry *dbgfs_dir; =20 struct param_map { const char *name; /* long name */ @@ -74,7 +79,7 @@ static int ksw_parse_param(struct ksw_config *config, con= st 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 *c= onfig) +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; } =20 +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 *buffe= r, + size_t count, loff_t *ppos) +{ + 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 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 =3D { + .owner =3D THIS_MODULE, + .open =3D ksw_dbgfs_open, + .read =3D ksw_dbgfs_read, + .write =3D ksw_dbgfs_write, + .release =3D ksw_dbgfs_release, + .llseek =3D default_llseek, +}; + +const struct ksw_config *ksw_get_config(void) +{ + return ksw_config; +} + static int __init kstackwatch_init(void) { + int ret =3D 0; + ksw_config =3D kzalloc(sizeof(*ksw_config), GFP_KERNEL); - if (!ksw_config) - return -ENOMEM; + if (!ksw_config) { + ret =3D -ENOMEM; + goto err_alloc; + } + + dbgfs_dir =3D debugfs_create_dir("kstackwatch", NULL); + if (!dbgfs_dir) { + ret =3D -ENOMEM; + goto err_dir; + } + + dbgfs_config =3D debugfs_create_file("config", 0600, dbgfs_dir, NULL, + &kstackwatch_fops); + if (!dbgfs_config) { + ret =3D -ENOMEM; + goto err_file; + } =20 pr_info("module loaded\n"); return 0; + +err_file: + debugfs_remove_recursive(dbgfs_dir); + dbgfs_dir =3D NULL; +err_dir: + kfree(ksw_config); + ksw_config =3D NULL; +err_alloc: + return ret; } =20 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"); } =20 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; }; =20 +// singleton, only modified in kernel.c +const struct ksw_config *ksw_get_config(void); + #endif /* _KSTACKWATCH_H */ --=20 2.43.0