From nobody Thu Oct 2 02:12:36 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (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 31045305E1B for ; Wed, 24 Sep 2025 11:52:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758714744; cv=none; b=Xd7JgyHobhDv5s1iLylXS+4evPNBMzl6D3rwSOWoeijG76erKL91Vv4IXY2+rsorkji4uiNFS1kbrsIrsOEEXkjO7KUoa51IozRd6CAPIwb7pn6alPk9f2/qCxUoUyCP+EciohJDoSWhsckpXgr42Cr7iUdrmgW6/LVPKKRN41I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758714744; c=relaxed/simple; bh=Sl2YI7Td/C9YkkJpnWkOlkyn4hvpDBvdNr6f7stAoSw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D37Ijw4Fy71Hj6JzhDscM5Uk/sPp+wPDK3ZfmBOQVpj++ekOZhljHbKVqe3dsfdHCp0IKD3clhJrV1qaVqqzbkZIOF+y4Cd/5Rp1XphWpIzMF8uey+UL6zTxxV5u8EXEGqaw+OmXA7WtVer/w6qns156SttIuBTkx19f9NT1RuY= 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=QI3SFqRg; arc=none smtp.client-ip=209.85.210.179 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="QI3SFqRg" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-77f67ba775aso1026952b3a.3 for ; Wed, 24 Sep 2025 04:52:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758714742; x=1759319542; 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=sX4DcHmKN6k6jLli4BcDyPNAsEAGb3ORwL+dRbW7euA=; b=QI3SFqRg0/keF/536rklQG51Kdy42hQLSADPTsDYI6WM6YwX74p3ulcWiaL5+EgEm0 XXf23ffITvdqQUVPGmU8s5dazbY/RMZ5i7gb+IOZJ4ctbSQoUa5Iz4XPvk1rmEPFrrTZ LyZaNXwhtKc/dSU6qcH2FiQR/JjN6akeohEVXjLnThpNDx9CU/IrWWnegK0t+zhdcdsd 82zl9KIOhe35HtabEGLg7B7hhRWr2p2lMB1H9rad6WzzNdDxzxOmwjOgd4Vas3xxmsmQ M+BSg4HM3QcwmeOp34rZjdHF8x83bQG3/qghWWh6eGBPZqeeit2VtWTjHMjnwuvoeEju HFBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758714742; x=1759319542; 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=sX4DcHmKN6k6jLli4BcDyPNAsEAGb3ORwL+dRbW7euA=; b=okmovEf5f+3qNKO9BRD5nZQVK3shkNu7Ec9GvDnH2DOZwYPMqcQk8pGaJY5ZAtlXwK 4a4Mw1RRewW6YuKxeuTH16J3cxsaYToOlaQTjzNdRUg0gJqaypMiayEFNG+kfOcHYGOw DR6bMVYkKApViSMHIZQm7mw3iSWalSuwr+DhUJXYRgJ6bR/CZhkiL7b+zZsVHvtpb0ox rnXt5f75mu1QEu3hHFfMA6qEodkwHHurmyhfR5yhF5C1jxAf8VEcTz4xb1bZtidE8PTG W3+BP62ALjpXPZvVQ6HAUBc01RPz/UPOhNmkbH8zWvmldrKT0S14xNDIx59fNIM5eA7F 9gEg== X-Forwarded-Encrypted: i=1; AJvYcCVsXzYHJEJocq/WD8Wq8lKQqMQMjCxeialv6HyxxKexqtJV9dS1RGsAEuyWNFr3MWF8vftDWqyiUlndwmk=@vger.kernel.org X-Gm-Message-State: AOJu0Ywo1a5WvEsSO9Ww0R7xjkwhjoHyPpl9ixbKDKbEuLy4JZ7h7Dmt g5dKKxpOxVrE1fOpzqCCWvrK0k6XsSK7YxOKtBa+kxEhq95iEUYTzOi4 X-Gm-Gg: ASbGnctX38Dffc7docdWYTr4T/XZCdH38eCtwdNgWzg5e4rWK2BfATs2jxx2REM+sNy KGO65hy+7vRaMxCzeOx9KMwngvBXffBdXi8nweXptbFlLzJ7M6D/wgf0UeZSX5nFWRW/iDcVwS5 Oq2/+BPFegU9tKC3t/Kcl+ZS4eEy9rbXZ94kr+4r1u3X2NvMNZDa+XUIbCAneDg0bOeLFMsPqYm WCoh3DuHe5rYOYLVBbkoXZ5bBvfR/l/B8Qu/PMqZGwWwV1uwHr4vvm9NXJg5l/54PULzp4swF/I Go1gziDes6SJxJAyRa09SglS/Xq3jIomVMWcxHhidRGQAf40B1ZKDnbXLxmXWCdlY7Pkk/Ip1Es 9P6wPyZilw49gV4na0dZuMr57hw== X-Google-Smtp-Source: AGHT+IHM4OGfhR5oYYdpAC3Dd6EW1w7h66PDSvRIMWNc5YTjulef4dYL+e/NY4vhVJcodihdgSSIbA== X-Received: by 2002:a05:6a20:e292:b0:247:65a0:822 with SMTP id adf61e73a8af0-2cff1c61c0amr8760157637.40.1758714742579; Wed, 24 Sep 2025 04:52:22 -0700 (PDT) Received: from localhost ([23.142.224.65]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b551518480asm14947563a12.28.2025.09.24.04.52.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Sep 2025 04:52:21 -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 13/23] mm/ksw: add per-task ctx tracking Date: Wed, 24 Sep 2025 19:50:56 +0800 Message-ID: <20250924115124.194940-14-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" Each task tracks its depth, stack pointer, and generation. A watchpoint is enabled only when the configured depth is reached, and disabled on function exit. The context is reset when probes are disabled, generation changes, or exit depth becomes inconsistent. Duplicate arming on the same frame is skipped. Signed-off-by: Jinchao Wang --- mm/kstackwatch/stack.c | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index 9f59f41d954c..e596ef97222d 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -12,6 +12,53 @@ static struct kprobe entry_probe; static struct fprobe exit_probe; =20 +static bool probe_enable; +static u16 probe_generation; + +static void ksw_reset_ctx(void) +{ + struct ksw_ctx *ctx =3D ¤t->ksw_ctx; + + if (ctx->wp) + ksw_watch_off(ctx->wp); + + ctx->wp =3D NULL; + ctx->sp =3D 0; + ctx->depth =3D 0; + ctx->generation =3D READ_ONCE(probe_generation); +} + +static bool ksw_stack_check_ctx(bool entry) +{ + struct ksw_ctx *ctx =3D ¤t->ksw_ctx; + u16 cur_enable =3D READ_ONCE(probe_enable); + u16 cur_generation =3D READ_ONCE(probe_generation); + u16 cur_depth, target_depth =3D ksw_get_config()->depth; + + if (!cur_enable) { + ksw_reset_ctx(); + return false; + } + + if (ctx->generation !=3D cur_generation) + ksw_reset_ctx(); + + if (!entry && !ctx->depth) { + ksw_reset_ctx(); + return false; + } + + if (entry) + cur_depth =3D ctx->depth++; + else + cur_depth =3D --ctx->depth; + + if (cur_depth =3D=3D target_depth) + return true; + else + return false; +} + static int ksw_stack_prepare_watch(struct pt_regs *regs, const struct ksw_config *config, ulong *watch_addr, u16 *watch_len) @@ -26,10 +73,22 @@ static void ksw_stack_entry_handler(struct kprobe *p, s= truct pt_regs *regs, unsigned long flags) { struct ksw_ctx *ctx =3D ¤t->ksw_ctx; + ulong stack_pointer; ulong watch_addr; u16 watch_len; int ret; =20 + stack_pointer =3D kernel_stack_pointer(regs); + + /* + * triggered more than once, may be in a loop + */ + if (ctx->wp && ctx->sp =3D=3D stack_pointer) + return; + + if (!ksw_stack_check_ctx(true)) + return; + ret =3D ksw_watch_get(&ctx->wp); if (ret) return; @@ -50,6 +109,7 @@ static void ksw_stack_entry_handler(struct kprobe *p, st= ruct pt_regs *regs, return; } =20 + ctx->sp =3D stack_pointer; } =20 static void ksw_stack_exit_handler(struct fprobe *fp, unsigned long ip, @@ -58,6 +118,8 @@ static void ksw_stack_exit_handler(struct fprobe *fp, un= signed long ip, { struct ksw_ctx *ctx =3D ¤t->ksw_ctx; =20 + if (!ksw_stack_check_ctx(false)) + return; =20 if (ctx->wp) { ksw_watch_off(ctx->wp); @@ -92,11 +154,16 @@ int ksw_stack_init(void) return ret; } =20 + WRITE_ONCE(probe_generation, READ_ONCE(probe_generation) + 1); + WRITE_ONCE(probe_enable, true); + return 0; } =20 void ksw_stack_exit(void) { + WRITE_ONCE(probe_enable, false); + WRITE_ONCE(probe_generation, READ_ONCE(probe_generation) + 1); unregister_fprobe(&exit_probe); unregister_kprobe(&entry_probe); } --=20 2.43.0