From nobody Thu Oct 2 02:12:37 2025 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (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 8EE443002A9 for ; Wed, 24 Sep 2025 11:51:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758714719; cv=none; b=lL2rPj0HB0lhT41NdoQoQKYSkqp28rriFvw0avlBzX2Qz/08zuTaG9MTodoczq67JGdkGCTpyfkOn81L6Fyrhu66xtoWw2m8Cwl/tUwlg1FJPfZUFA60QUd/MCdQOCLo2kznj9BjEwr/2PFWpr7Am5S7ntg6mDBqaf4jYMAr4sw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758714719; c=relaxed/simple; bh=Feu6QMw0Ju5Y4dTxQ53cERWbV/8rWQzq92gPB2aNeXU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lmIpdYj96FafAmZiMww3Gwa+f39TK/PbTs1BDihbzXcgEdYq77kpMnsj+4Sy6NSxtJytY614oOO82A9qCxwWqkSQ3LqR5kYYNiKFM3AwfT3wR9tXhtpPZ8Bd2YXY2qeMoz7oByzqnfGc42I8lrH1ZJsfXmK431ZRisab6KVN2pQ= 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=JjLf52oy; arc=none smtp.client-ip=209.85.215.173 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="JjLf52oy" Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-b555b0fb839so1986366a12.3 for ; Wed, 24 Sep 2025 04:51:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758714713; x=1759319513; 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=eGY4NXn9SYrwp2MIROIAsCXd8mHWC2w3iRZMJXUxzyw=; b=JjLf52oyNMy0PVbma/IMjkr/0CHn7hlxemIPz9rqtb4L/YuHnVvE6f78/+NOhTVvrf 0AKM/ZzvrWvndcO6DYZKA5QLoZ7vfHBDRJSNJaEy4lVxVIIPVNMN0iK+b6UQPZMzTx3Z GRII2WCQCl8wiqTTpiNnNzt0PIDKl9+akciy/wLWKoNzBmhiwPMAXfQ9XKoxRVms2e01 YVM1LqipjyLfZHoxLhtPEuRWYwSk0kFiRgHnTsjS65Gj1omUAk8v9Xckc6RJ4dMJ2cJv VMYhwQzmnwH1Fllg5ohscne3ZMiNAxEEu+Qu10wEFPtaP6V3ED8qBIxB9x3IT+wPMYAR Ev9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758714713; x=1759319513; 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=eGY4NXn9SYrwp2MIROIAsCXd8mHWC2w3iRZMJXUxzyw=; b=pSX79EH109msTIvi6DvEGdM+deNn1pYsGB9WB7wT1wZ5feJ9WKfxCpzSbmqerzveCG gqmWPn26X5b9EiMSD+/gEC5sjqMwEzL/EZOKKoB94RoVdmPSgbolK0KlMFYU7hVTvh8H YPgiwfFiHre3vxC+uUkhGsXBl7WbFO0ETpTd4ibEvAjWcrwIE9507JL1Nt8y2J9BguER Yuuj4wp+Oryud79uIJb/lKYtxKgUVxOy942eTBhwtFMkh535Ho/KtiOjdackSnsiGvmj cJOti0rdCdS7PwSdoC253I7hovgWhn5Ldckrn5JkI6Q8kC4gXvsz8cAS1pF/l6vk2oJ1 w0nQ== X-Forwarded-Encrypted: i=1; AJvYcCUFHM2Tr4HQcbf/SEkCfABQaO+4pbPQIlER8SLAobeJElhPjxjlZuG0NUwhv98nyykHGSgni4G7mFkjOAY=@vger.kernel.org X-Gm-Message-State: AOJu0YzBMpOyZJgpcLOPOfZCV6ZkYbJEPx165tQd2f6UpxU7wPusJ/sk dXRlTIvcx01jMZT7XuQnXHBJ9PrNyTlxWIOZ5B97FivcD+qUL0v40a9a X-Gm-Gg: ASbGncss+Wy2LDSCVCrH00GqqD//RyNnvKGeMO5LeAI+Nea1hGmJBjaX4MymEsBxQca EYQ1/rRQBEKmIZoKwSuIIhRWd9ek/kcwpOP40iBK/y2MEL0aMgjj27HJJOkUVxo1ZcXD/g+nLpO U2ZDbTRit4fmY/CDiXq3TCgqQsKAHGqiZcX/nxwqpVqLIEPB46eErhCy1LKoHKvz7iZKP1jkcLE 2/HG6+fW85s6Ej/x1/bMbOM6mpv/1DuWLgqO5rha9yrGW+hAWUG3mr1BfyQUIQ0DFdmKYeND8/y l4Kobw1yjIxcR/rVR0EkPboxqrCPfarIRdy0E/Qm8ZVv+TsBDshpLAGbc5ynWDkCr4svfEBWp2i i5C9xcMstH4gH7UGIEE2JMk0jKt84+YQ0Nin3 X-Google-Smtp-Source: AGHT+IFMNctGW9Lc2RuLDimMt82FyjBUd6I48axMdmkX53huq+gu8b3I4mIDqyHLSeX7qGzL0isVyA== X-Received: by 2002:a17:902:e885:b0:25e:5d83:2ddd with SMTP id d9443c01a7336-27cc678336emr76685615ad.45.1758714712744; Wed, 24 Sep 2025 04:51:52 -0700 (PDT) Received: from localhost ([23.142.224.65]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-27ec0a7f443sm19635565ad.105.2025.09.24.04.51.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Sep 2025 04:51:52 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , Peter Zijlstra , Mike Rapoport , Alexander Potapenko , Randy Dunlap , 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 v5 05/23] mm/ksw: add ksw_config struct and parser Date: Wed, 24 Sep 2025 19:50:48 +0800 Message-ID: <20250924115124.194940-6-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250924115124.194940-1-wangjinchao600@gmail.com> References: <20250924115124.194940-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" Add struct ksw_config and ksw_parse_config() to parse user string. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kernel.c | 112 +++++++++++++++++++++++++++++++++++ mm/kstackwatch/kstackwatch.h | 27 +++++++++ 2 files changed, 139 insertions(+) diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 78f1d019225f..3b7009033dd4 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -1,16 +1,128 @@ // SPDX-License-Identifier: GPL-2.0 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include #include +#include + +#include "kstackwatch.h" + +static struct ksw_config *ksw_config; + +struct param_map { + const char *name; /* long name */ + const char *short_name; /* short name (2 letters) */ + size_t offset; /* offsetof(struct ksw_config, field) */ + bool is_string; /* true for string */ +}; + +/* macro generates both long and short name automatically */ +#define PMAP(field, short, is_str) \ + { #field, #short, offsetof(struct ksw_config, field), is_str } + +static const struct param_map ksw_params[] =3D { + PMAP(func_name, fn, true), + PMAP(func_offset, fo, false), + PMAP(depth, dp, false), + PMAP(max_watch, mw, false), + PMAP(sp_offset, so, false), + PMAP(watch_len, wl, false), +}; + +static int ksw_parse_param(struct ksw_config *config, const char *key, + const char *val) +{ + const struct param_map *pm =3D NULL; + int ret; + + for (int i =3D 0; i < ARRAY_SIZE(ksw_params); i++) { + if (strcmp(key, ksw_params[i].name) =3D=3D 0 || + strcmp(key, ksw_params[i].short_name) =3D=3D 0) { + pm =3D &ksw_params[i]; + break; + } + } + + if (!pm) + return -EINVAL; + + if (pm->is_string) { + char **dst =3D (char **)((char *)config + pm->offset); + *dst =3D kstrdup(val, GFP_KERNEL); + if (!*dst) + return -ENOMEM; + } else { + ret =3D kstrtou16(val, 0, (u16 *)((char *)config + pm->offset)); + if (ret) + return ret; + } + + return 0; +} + +/* + * Configuration string format: + * param_name=3D [param_name=3D ...] + * + * Required parameters: + * - func_name |fn (str) : target function name + * - func_offset|fo (u16) : instruction pointer offset + * + * Optional parameters: + * - depth |dp (u16) : recursion depth + * - max_watch |mw (u16) : maximum number of watchpoints + * - 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) +{ + char *part, *key, *val; + int ret; + + kfree(config->func_name); + kfree(config->user_input); + memset(ksw_config, 0, sizeof(*ksw_config)); + + buf =3D strim(buf); + config->user_input =3D kstrdup(buf, GFP_KERNEL); + if (!config->user_input) + return -ENOMEM; + + while ((part =3D strsep(&buf, " \t\n")) !=3D NULL) { + if (*part =3D=3D '\0') + continue; + + key =3D strsep(&part, "=3D"); + val =3D part; + if (!key || !val) + continue; + ret =3D ksw_parse_param(config, key, val); + if (ret) + pr_warn("unsupported param %s=3D%s", key, val); + } + + if (!config->func_name || !config->func_offset) { + pr_err("Missing required parameters: function or func_offset\n"); + return -EINVAL; + } + + return 0; +} =20 static int __init kstackwatch_init(void) { + ksw_config =3D kzalloc(sizeof(*ksw_config), GFP_KERNEL); + if (!ksw_config) + return -ENOMEM; + pr_info("module loaded\n"); return 0; } =20 static void __exit kstackwatch_exit(void) { + kfree(ksw_config); + pr_info("module unloaded\n"); } =20 diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index 0273ef478a26..a7bad207f863 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -2,4 +2,31 @@ #ifndef _KSTACKWATCH_H #define _KSTACKWATCH_H =20 +#include + +#define MAX_CONFIG_STR_LEN 128 + +struct ksw_config { + char *func_name; + u16 depth; + + /* + * watched variable info: + * - func_offset : instruction offset in the function, typically the + * assignment of the watched variable, where ksw + * registers a kprobe post-handler. + * - sp_offset : offset from stack pointer at func_offset. Usually 0. + * - watch_len : size of the watched variable (1, 2, 4, or 8 bytes). + */ + u16 func_offset; + u16 sp_offset; + u16 watch_len; + + /* max number of hwbps that can be used */ + u16 max_watch; + + /* save to show */ + char *user_input; +}; + #endif /* _KSTACKWATCH_H */ --=20 2.43.0