From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) (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 BC6012FC011 for ; Mon, 10 Nov 2025 16:37:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.178 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792628; cv=none; b=Jh7G2HdlCRMrR/bXkH9XGgItdktlsgNXdb7i2Cw/2Yg30Y1F+kVCfLzHLOshwoko/Y63Hk8pZKZC/plDQgqogB6RlbUBPZka8ea3hMLQ3jRhhOkjehLjw+ZLGRJYAf7cxyeMmxHsuaDwfUcqwLhBmVgzLKksRvUSqRvWLgxUebo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792628; c=relaxed/simple; bh=J0fK6zHQukT5AAwp1WNQGcCty1gFWyGS7LkumBAy8ig=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qxq2eRTwUPWLJxtwGFVfz53G1QO8O7as6QjZpzbc4UlH2kC0gqeHAkBF3fkQpOAeYMU39CHzeiIDFyT8njokMdwikW/adS2xViVd1F0r00bauWqrfk+PoCV/pNgBW2Z5hZDd+9sp09t/oPpLpL6j6dDo0IaBMC8ViYfbZzVln5U= 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.214.178 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-pl1-f178.google.com with SMTP id d9443c01a7336-2958db8ae4fso26965005ad.2 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=PTjZ6lulxnLSOVnj3FzLO33HbuLElgJrf4EcV66rBROQDEbSYHFp+qROJyyOX6vDjm EVyR4YyYWZPqCEgab1IS+IHIimvGL713XTJ+MMpr+GAIQa8hVs/FGOEenZXbYlOA72D/ 8gqtTqEt8raJxI04qe80dRbnD9HcFni8YESo6Fq+jXPbmyl06sMg4QaEJM6U90moc7WP cKvNfDWhmJq1fUvkjhuaVeUJheHASV8lvats2SHE1S6Z9rzg9v7jkqOd6je7lrllPcS7 oTJMDBRdrvNiqbq5lwNv0SnJUbtT3iM36voZNwADR5MpwLUibolvsSd/uMBB8wMf087W AhKg== X-Forwarded-Encrypted: i=1; AJvYcCWXPYiDYBUutzR8t7+JdedaR8pV6x/AbdOwwvagLj312VoFfHTBW3Uwfhz4EKVPgUQATgST3L2x7y8=@vger.kernel.org X-Gm-Message-State: AOJu0YwSyn6wOgD5fjYzAC500TO2rE3MLkUSnoZQjcxrKeRqd1zCIGO8 ygPdkahIGk/JLW746FlZND6Pzy2L8AtmBq/HcZe1sP9cNWZwH+Nr61X/ X-Gm-Gg: ASbGncs1fhC5tDjAq6MrZExbQWecqjWZXT4sXfzVbFhn2O3PxQWTAy79u/NP+Yv0jHu UkHuC9kG4W6mM038OuQQKTrYFDUxzOgqWzehFbS/V0lmq++cQafG2CGFfiazv8djpx1XGmz2xQF nuHnpzWfFLis4ZaKVsejtlXeA+ACWGiWkVvdscnuvCXFYF+aH4iYwSfvkumCBc7SsEBx5YGuBM0 Ai2cTpiTGOe7UiEa11/JIbHGi5H1NK7+o6DvujaCU/YFTtpq1Y+cTBxGAut6pe7cG0FNkveW29u Y/9ous9BvgXW8BWjXajrCzlkpkZIoHxPMDQCkj34iV+EQ8yrcq8rXFSacMIPrgj5GY1mNDEfG6p NGBe/iRyjsHcCRxP7xfshnwesnHy0AFkeOjpOyzxH2JsHQqJUEN5wqVAwvIUPaabSpWHXFPc1gb Tg9mDCSFarpaiZX/AGIefU/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-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 +#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 +#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[] = { + 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 = NULL; + int ret; + + for (int i = 0; i < ARRAY_SIZE(ksw_params); i++) { + if (strcmp(key, ksw_params[i].name) == 0 || + strcmp(key, ksw_params[i].short_name) == 0) { + pm = &ksw_params[i]; + break; + } + } + + if (!pm) + return -EINVAL; + + if (pm->is_string) { + char **dst = (char **)((char *)config + pm->offset); + *dst = kstrdup(val, GFP_KERNEL); + if (!*dst) + return -ENOMEM; + } else { + ret = kstrtou16(val, 0, (u16 *)((char *)config + pm->offset)); + if (ret) + return ret; + } + + return 0; +} + +/* + * Configuration string format: + * param_name= [param_name= ...] + * + * 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 *config) +{ + char *part, *key, *val; + int ret; + + kfree(config->func_name); + kfree(config->user_input); + memset(ksw_config, 0, sizeof(*ksw_config)); + + buf = strim(buf); + config->user_input = kstrdup(buf, GFP_KERNEL); + if (!config->user_input) + return -ENOMEM; + + while ((part = strsep(&buf, " \t\n")) != NULL) { + if (*part == '\0') + continue; + + key = strsep(&part, "="); + val = part; + if (!key || !val) + continue; + ret = ksw_parse_param(config, key, val); + if (ret) + pr_warn("unsupported param %s=%s", key, val); + } + + if (!config->func_name) { + pr_err("Missing required parameters: function or func_offset\n"); + return -EINVAL; + } + + return 0; +} static int __init kstackwatch_init(void) { + ksw_config = kzalloc(sizeof(*ksw_config), GFP_KERNEL); + if (!ksw_config) + return -ENOMEM; + pr_info("module loaded\n"); return 0; } static void __exit kstackwatch_exit(void) { + kfree(ksw_config); + pr_info("module unloaded\n"); } -- 2.43.0