From nobody Mon Feb 9 04:14:25 2026 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.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 DE9382FD678 for ; Mon, 10 Nov 2025 16:37:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.173 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792627; cv=none; b=jR9Ovokp1Hkg6muAbDfROypRz8ZWBb1DW5uNFmVQwieblPc8pD4Tp//Hux7AWQp0yueimI/cFtlXyqmSr/7AGyV9Q9MS7Sv6Dyf8Dzt4hk8O6OhwPwObX9XOXMlHbajIFhdS1tcQ735zQvHkGvQqlR/ADD+eBuh55wiZ4qGS6Rw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792627; c=relaxed/simple; bh=J0fK6zHQukT5AAwp1WNQGcCty1gFWyGS7LkumBAy8ig=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z/H1eBRd6sadNYqwKfHkbEKSURx3QqSKTmcu2ONNKJK1j0uhdVTIUq14zTOqffhpyCBBl98blpBAQ7xyrHV7tocvlCBDpvlxtSlXYPzWHp9XwSNplZgMoW6BoTRER03yxEErlbkajm0f9YdXo9VEQeZ3S7mRpqKJjbgYZNdxwW4= 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=XGyi+xcx; arc=none smtp.client-ip=209.85.210.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="XGyi+xcx" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7aad4823079so2901789b3a.0 for ; Mon, 10 Nov 2025 08:37:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792624; x=1763397424; 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=ouGNoGCEWNf78k//tY5pOloDk6SH5B0un9nqZ5MFRzI=; b=XGyi+xcxLZak97Bc7irfqdBIjXPUti3QhMP1P/HrDY1YyjQknLDM/qeJsSmwSalA8m 7kcurpTZpo0Lnnib56QmC9Pe0ed6b5JO+3XcPBlbk8olz8XfHUOC8N4KHhUWbJVMt4ZP tk6xe9xaEH8Lc0f2sDkSN5dvNxdrTvOVOXE13oLZWB62CANuv9dxqPTYMIXC51B3GFpY 5nFX6quPVJ3pN3yVadtnORz5yJdOeaD/aUt8a0NSQvgcO//wytHpdQz03zhvOG6eFtkL E3ZMDAjkEb7maebXG55EbZwgT/oCQ/nytLl4efb7lDjcZPhCcsijtWh900xGmWxTTTzw 4nNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792624; x=1763397424; 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=ouGNoGCEWNf78k//tY5pOloDk6SH5B0un9nqZ5MFRzI=; b=dXKuFJe20dof3oI5kzodNTMeDoL+dp3B3hiu39Mz05MNCs5u7/S6lKP9qz0OHXWizb nvoXHHQ58Ip5DuRcAjr5PFVcheB2lk+4ykXxkrMtg1WOgj2Um5ZM8Xepk105SHzM7yoi J96AQxuJ3PAv7QCrlw3umUVgcQGSfYVlSAb+UyXq8tHaHUs5x1dqCGL/KLBynj4xB+4M e/qywqPQoDiSKYRCp5q8gE0vWw5Nr0eQRmoQgQekeG86qy03nTussUS/ymD5easC7NrN QD7XwmdnW6PDraVvdAOIY/oHOrV+T8Mdx85ed3+L+08iW3BU6XYBY6Pl1WoAKCbScniN vT6g== X-Forwarded-Encrypted: i=1; AJvYcCUu3mZdiX9WxpbrXIUTpFPxMaju3Y4IKC4Ejyc9G/mEd0rclIvS1xX2gfirTcAHrsEDysb0jMGsp19XVtQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzlrCSDrN8hr6g0khtnyTNWKkNMjWbkxnBYd8ElSViqAm9RAoHC WFaOeufrk5PDVS6A9QgxBDJvIKZjBv3yuI6ptEn3qf4d8FA0GKlzTOhP X-Gm-Gg: ASbGncumT/pcHmMtUkxfohbAon3Rg5TBL+GAk5canAHKvthjLx6GdnztCDs1KhVn9yZ S/zZm3SxUdks6L+I1d/0Lqe7zg0tKsBnI0l3wJ8rHqp5cWcRE02xynSunQ5YxrRgET5w5rAK0fb yhC0eNs0pyviJBbGapvqa6CwUzW6U9zo6g0doZDlGUgekXZm7PoDRjjBUZacWLqe5RhcGd3e9uP OrpZjVkZUtMmghqkoBT8/1CQfH3cK6SUx8RQ/FkTj87LkZaDfne8UIP+Gc8HRRfVxx5Lg+Mfj9B WpFC/O2uHt8grU2iiDKdqMHnYoeSZaztzSwCKNhxivBz2SjpJtO5f4wUxH5GmYAE1T7rvTb0SQD ey/Wan94S391GFZ5lVXf8kkWk7niNJ+1+M8tRX22Zj+wjVgtbBENaoYO+TG7E8aQFdor8e0oQSX VGHAUDg+heBJXQv8WSCuk+/Q== X-Google-Smtp-Source: AGHT+IEv35u8inwf//3hNgNcTHKrbkmcMG/RxR47Sb7uiTU4vtvIQ3hgGu7aTHTaitcgX4VFQg+h0Q== X-Received: by 2002:a17:902:ecc6:b0:294:f1fa:9097 with SMTP id d9443c01a7336-297e56d621cmr108081745ad.34.1762792623885; Mon, 10 Nov 2025 08:37:03 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-297df996d31sm88101695ad.13.2025.11.10.08.37.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:03 -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 05/27] mm/ksw: add ksw_config struct and parser Date: Tue, 11 Nov 2025 00:36:00 +0800 Message-ID: <20251110163634.3686676-6-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-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 --- include/linux/kstackwatch.h | 33 +++++++++++ mm/kstackwatch/kernel.c | 114 ++++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index 0273ef478a26..dd00c4c8922e 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -2,4 +2,37 @@ #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; + + /* search canary as watch target automatically */ + u16 auto_canary; + + /* panic on watchpoint hit */ + u16 panic_hit; + + /* save to show */ + char *user_input; +}; + #endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 78f1d019225f..50104e78cf3d 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -1,16 +1,130 @@ // SPDX-License-Identifier: GPL-2.0 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include +#include +#include #include +#include + +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), + PMAP(auto_canary, ac, false), + PMAP(panic_hit, ph, 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) { + 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 --=20 2.43.0