From nobody Thu Oct 2 02:13:27 2025 Received: from mail-pj1-f47.google.com (mail-pj1-f47.google.com [209.85.216.47]) (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 E6D1321B9E2 for ; Tue, 30 Sep 2025 02:44:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759200287; cv=none; b=UX5BeBS0FWJcpgIfanmJUXCn+tfcvgRK6+nnjvb6hRX7z1pYnk0LfGUkyieNXta8R7n55/BgbhrvUSNASD34mhhLsqltqyJw0Df6mKLrznxS8HVp0P3wUjgjvokbYuFRhLis5t/Yngw7TfLf8KsoMDxgWia/yO/F5GSF3HiecFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1759200287; c=relaxed/simple; bh=Feu6QMw0Ju5Y4dTxQ53cERWbV/8rWQzq92gPB2aNeXU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=f85Q2/5Y45gA9tMK+XSn+S69HLzGXsAonYA/L/299+g35Srgpttez4MT4M9kgk98XIEPge5QVDD7wTR+Tp8b0vT2rTGpSX27nNlbq+Lw6Epkt/t4yOchqwT7tsVItX9B1pkirvCGkjMuHIXdIAIm4ZnG9l68KkqGU0B4fntTeq0= 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=cD9UsyAP; arc=none smtp.client-ip=209.85.216.47 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="cD9UsyAP" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-32df5cae0b1so6379115a91.2 for ; Mon, 29 Sep 2025 19:44:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1759200285; x=1759805085; 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=cD9UsyAP5k2wj4gs9yH53Z5F0evW6nvZeYep6U0kGGBeOq22iMBGQ10fM4HLmh6GoF ibawQ7frk+355kuKbgNVplNh2RBpcejEqPqp8ekEoIAd/nN0lrc8n7zGmNOu1PNXZjSl i+jMW7vO3nhuGEKYovvsy+1U+FVTZv+u3oxyDCVIa/7HagNJvsc63RMlohld5WAoArmv WsgKXvDy2bkVe6dLS1kaRSdoMxVgyyN6+EzILqOlGDHRWQGxakjNwT9P4VnSx5ENfSZL ezdf4YzogQO0TaQ5Ji79Dh7FUajDGZcrv7mkd4HnUmIAz22USLvwRkj2cQKWmYxQirj/ X4vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1759200285; x=1759805085; 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=ta0Amrtu2EJ3srtbR8k5Yez07rWlTexVTbWh3C6pmxkYLjtkjrEr/jqZPVUPptZHaC 7N9mG7oOHcFeF30Fd0pee1ZnwuH9rnwa1RShU+QC4DcKp69wHg/d5j3pIm5GqqGpvEeD 8lReo3+LlZqPvnxFBUMVGVvN0uvUB/ZTm/KLmn+4h/meydjfwbOvEIReEe5gZi55LNHw atpaHIgYzVPtuFDmXnfT5xKxUm93TioaQgeV9XXA2fCCh/8iq/j4HX9yjU1S37nAz/gl lcRvfKW7uXvEi6KtB/aH2RizFZJtJrS5x0QXKnj0hpDNS9f6zloLjccQA/E23TiU5yv6 qE+A== X-Forwarded-Encrypted: i=1; AJvYcCVNdW+OS0L9JBejt2YyInfYC/GuwMLyF1XWbzs253o4+k0Asr7mb/UmbiIj6SeQydKYvom5ioThdhxTTmY=@vger.kernel.org X-Gm-Message-State: AOJu0YxgGNuIW4keQVihF2p4bsV0pLJVDa9rWG+jlkv9l9xem9QtLXGW MzHWXxG8OapkG9rUTjR3MMiA1jWJ7nXYsZq9e2E/rG1nNdHoeHzAWXa1 X-Gm-Gg: ASbGncsZCxeHyfF3MdTK/7kX9ywLubV9k+YKzMszge99xCUdagYIfFlUoNAs/2sOWf6 GTnTTSy8ictDCmpBKJwBxhWIDj1/nXPmXI806d/Fq9tJYOrQ+2+twsls4DHd9ytFu/Dkp5FuOdu taYPpuBpw26pExvaKsJ8jRqsF9hjTBAMNiPS2BNioBcB/TRNLKvcUS7S+lvPwJxPGsZZGOSzzxZ ty0zOsg92LHRcY8AjjsuV38+x6CE+Pewlib8rsqum8WDMRh6cN6+DHTYnr+gmZ6NynNl7U91SSA /w2fu5D+dzbzBkcEBe2mveV8B/7KG1ABcg1QPlwSA/+gq8DYkon0ilxOKM3kGeh0WVvmbHJIw41 veNQbeJulAoIFnrsCB3vgCro7C7CqGVIGc6XDHqCKTpvjvYMt2/u+aNtscLtYuz28zlWGzK0Zzg V+ X-Google-Smtp-Source: AGHT+IFCIxJ+f2ZP7/SRGQLtykJQ9skkyS5Xc4YM8nOS80KRWl9hYAY1JbghIyfFVwcemJPxzbxqqQ== X-Received: by 2002:a17:90b:1347:b0:330:6edd:9cf with SMTP id 98e67ed59e1d1-3342a2b0f1fmr20209339a91.22.1759200285220; Mon, 29 Sep 2025 19:44:45 -0700 (PDT) Received: from localhost ([45.142.167.196]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3341bdac46bsm18593679a91.12.2025.09.29.19.44.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Sep 2025 19:44:44 -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 05/23] mm/ksw: add ksw_config struct and parser Date: Tue, 30 Sep 2025 10:43:26 +0800 Message-ID: <20250930024402.1043776-6-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" 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