From nobody Sun Feb 8 09:33:07 2026 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 929B1285042 for ; Mon, 10 Nov 2025 16:36:46 +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=1762792608; cv=none; b=Og6kJZll/2WKJSHjHcweSzzaghQJp9RU/sLUlHa6hWSzO8gZE8/tXU9wuyHz6wKxehpNsm6Azry/XsCinpfHUdDEFfoSdlX+hbmsNvLnt67RjZRK9bBVAC195F62lsJF32aB7NtFTy2PW2by4GOT7q3n4IFcaEJ3KxZSATqH6m4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792608; c=relaxed/simple; bh=X7rbGcP88vwI8LXFWSyD4f0KAHbEZ/JYDGwMupozUWE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iUZTvsCBFHvEPW1HqV347/v0blOg77wy4Kl5bV+0Fcm/tuVsZezEJw2S8Mt9aKJfBj06EKs5UW2naiIsSXYaxQ+uKsWaqjovCVoMGCQbaZs6HGD6ZACWuoBRDVCKfIuzXBdBakB8/QnRCTBtQvMOzTtPQmPrLocMr6AVmcauH/s= 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=O4IkfT6u; 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="O4IkfT6u" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7a59ec9bef4so3560240b3a.2 for ; Mon, 10 Nov 2025 08:36:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792606; x=1763397406; 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=kk8QwiK6ueKhbPm+iybiDp/pMjWX1cNh1XEGnvIxiQI=; b=O4IkfT6u/ukH6KY8i6cSPTxVyrPjStJDSJYFsn+7f+ITXG5nsl2z3GwjKocKsn67sm NkDsphgSWCykGdv+aMDXgGig9hyoxiPUpMq8fM1dpEX86w6unXctSLzCOYlhH/KOdiDA vpkzLR/pS6sp4ua/Qw3c9l2kiY+okH/nK4/Vm9UuF4C9EcxAfjkndh4IIl17mRd27tGu x6MtH9MnFWKUGPOIYQlao7jm0+P/99JSAg59aMNtLHQEpVLpVsiTWthjYhUY5qryWMh7 N5hF4x3ShcJ9ip2RtzRqlaJckb1XVUk+e334Or4I59kphY2N6n2rkwK67M90Bw/PLuX1 zO2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792606; x=1763397406; 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=kk8QwiK6ueKhbPm+iybiDp/pMjWX1cNh1XEGnvIxiQI=; b=HDiTn5AIvlI8Wdqdk5nkevtZeknas0xMO5nDeYYgnr0Tt5zSWTgmKBWf4HsHtLIGVJ Uclz0MB/KQAEY+zJ/ODGkbP39vXx/ynREUeA8fYI7gKnr+Zm+Ndxja79AP32wG5vNMic QrUV6Y9eyu4vYKuANmOQQIIGBhkLWDx7260N1b71nL9NogjgP62B4uadZTEybw30aCwv oqkyj2I3EsMG65hqb3wyykfv7qWHC30Z7QHmljiyMQrna4pfePCym4rs189bJ3xxYYwg YR212DY9YGfDX36XZVATrhfjBE5Zxq+StDeEVKKFNRYr5H0s7FWImVgIkK7HfIFgig5R ntHg== X-Forwarded-Encrypted: i=1; AJvYcCXD82EKtRFPEx15Enr0/wtf+y9rMMlSF2zxOyanGMyffbB6OXbZfJMKgFfXeJ1f5OKzNYLHZBX34hZWVjw=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/xRuzEZPn2tarZs+h3dnhDPnN0Y4tUOp8cUp0NQZanOTVp12a RPVL7uAeTRsKWre3fZknley3n6RwamWiCUc2aWlzi9FHprTKBds+3lCP X-Gm-Gg: ASbGnctV3sTzzWBq8G3jyRVQNIgpS/DlgYJKSH8DaJ64noYSV0xrJ55ulOqYnnueum9 BanNJiUoX73HP1w629D1RKFY08NtoGxFXUUdm0z0BQ2GYh5Hi1XXgx+e7CPX4E4iVdyjPNud90z 3/jydQxwGTHeASQzyLkxqPrcGpfLqNZFd24PLptAWAbc1Qzt10bd9Fun0UoZPI8gLWuH1BtvHKw nY7Pkwach5yxtRzKyL6/QjzsCBxQta3zu+4I1CCQnZcDTmJyIWvWcmpH5OhEV68rMamDF3GCYfU D7ottaq+v6vD9QhWPQtrCiQt1lYakaVi82CPVXGywOKxe92OtMPZ9zCAxIE8S2J3bkRF0AoAPow g+MQ+VTswsjAZQrcQJVeeh5hoaG8RMyX/H5xf+GZbwEop70dhLn/Avgpke5/7vuODWDUsjyNd86 QMlkh6gxS+B5U= X-Google-Smtp-Source: AGHT+IH1f+r1UrU5gIs1h7VugOnmhHceXLBGuSuO4vWp4ORB3huGtoQpeyNaf5f88WRoFNvTTkca/Q== X-Received: by 2002:a05:6a20:6a06:b0:334:a901:c052 with SMTP id adf61e73a8af0-3539e635fd9mr11647011637.0.1762792605556; Mon, 10 Nov 2025 08:36:45 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ba902c9d0d4sm12765118a12.36.2025.11.10.08.36.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:36:45 -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 01/27] x86/hw_breakpoint: Unify breakpoint install/uninstall Date: Tue, 11 Nov 2025 00:35:56 +0800 Message-ID: <20251110163634.3686676-2-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" Consolidate breakpoint management to reduce code duplication. The diffstat was misleading, so the stripped code size is compared instead. After refactoring, it is reduced from 11976 bytes to 11448 bytes on my x86_64 system built with clang. This also makes it easier to introduce arch_reinstall_hw_breakpoint(). In addition, including linux/types.h to fix a missing build dependency. Signed-off-by: Jinchao Wang Reviewed-by: Masami Hiramatsu (Google) --- arch/x86/include/asm/hw_breakpoint.h | 6 ++ arch/x86/kernel/hw_breakpoint.c | 141 +++++++++++++++------------ 2 files changed, 84 insertions(+), 63 deletions(-) diff --git a/arch/x86/include/asm/hw_breakpoint.h b/arch/x86/include/asm/hw= _breakpoint.h index 0bc931cd0698..aa6adac6c3a2 100644 --- a/arch/x86/include/asm/hw_breakpoint.h +++ b/arch/x86/include/asm/hw_breakpoint.h @@ -5,6 +5,7 @@ #include =20 #define __ARCH_HW_BREAKPOINT_H +#include =20 /* * The name should probably be something dealt in @@ -18,6 +19,11 @@ struct arch_hw_breakpoint { u8 type; }; =20 +enum bp_slot_action { + BP_SLOT_ACTION_INSTALL, + BP_SLOT_ACTION_UNINSTALL, +}; + #include #include #include diff --git a/arch/x86/kernel/hw_breakpoint.c b/arch/x86/kernel/hw_breakpoin= t.c index b01644c949b2..3658ace4bd8d 100644 --- a/arch/x86/kernel/hw_breakpoint.c +++ b/arch/x86/kernel/hw_breakpoint.c @@ -48,7 +48,6 @@ static DEFINE_PER_CPU(unsigned long, cpu_debugreg[HBP_NUM= ]); */ static DEFINE_PER_CPU(struct perf_event *, bp_per_reg[HBP_NUM]); =20 - static inline unsigned long __encode_dr7(int drnum, unsigned int len, unsigned int type) { @@ -85,96 +84,112 @@ int decode_dr7(unsigned long dr7, int bpnum, unsigned = *len, unsigned *type) } =20 /* - * Install a perf counter breakpoint. - * - * We seek a free debug address register and use it for this - * breakpoint. Eventually we enable it in the debug control register. - * - * Atomic: we hold the counter->ctx->lock and we only handle variables - * and registers local to this cpu. + * We seek a slot and change it or keep it based on the action. + * Returns slot number on success, negative error on failure. + * Must be called with IRQs disabled. */ -int arch_install_hw_breakpoint(struct perf_event *bp) +static int manage_bp_slot(struct perf_event *bp, enum bp_slot_action actio= n) { - struct arch_hw_breakpoint *info =3D counter_arch_bp(bp); - unsigned long *dr7; - int i; - - lockdep_assert_irqs_disabled(); + struct perf_event *old_bp; + struct perf_event *new_bp; + int slot; + + switch (action) { + case BP_SLOT_ACTION_INSTALL: + old_bp =3D NULL; + new_bp =3D bp; + break; + case BP_SLOT_ACTION_UNINSTALL: + old_bp =3D bp; + new_bp =3D NULL; + break; + default: + return -EINVAL; + } =20 - for (i =3D 0; i < HBP_NUM; i++) { - struct perf_event **slot =3D this_cpu_ptr(&bp_per_reg[i]); + for (slot =3D 0; slot < HBP_NUM; slot++) { + struct perf_event **curr =3D this_cpu_ptr(&bp_per_reg[slot]); =20 - if (!*slot) { - *slot =3D bp; - break; + if (*curr =3D=3D old_bp) { + *curr =3D new_bp; + return slot; } } =20 - if (WARN_ONCE(i =3D=3D HBP_NUM, "Can't find any breakpoint slot")) - return -EBUSY; + if (old_bp) { + WARN_ONCE(1, "Can't find matching breakpoint slot"); + return -EINVAL; + } + + WARN_ONCE(1, "No free breakpoint slots"); + return -EBUSY; +} + +static void setup_hwbp(struct arch_hw_breakpoint *info, int slot, bool ena= ble) +{ + unsigned long dr7; =20 - set_debugreg(info->address, i); - __this_cpu_write(cpu_debugreg[i], info->address); + set_debugreg(info->address, slot); + __this_cpu_write(cpu_debugreg[slot], info->address); =20 - dr7 =3D this_cpu_ptr(&cpu_dr7); - *dr7 |=3D encode_dr7(i, info->len, info->type); + dr7 =3D this_cpu_read(cpu_dr7); + if (enable) + dr7 |=3D encode_dr7(slot, info->len, info->type); + else + dr7 &=3D ~__encode_dr7(slot, info->len, info->type); =20 /* - * Ensure we first write cpu_dr7 before we set the DR7 register. - * This ensures an NMI never see cpu_dr7 0 when DR7 is not. + * Enabling: + * Ensure we first write cpu_dr7 before we set the DR7 register. + * This ensures an NMI never see cpu_dr7 0 when DR7 is not. */ + if (enable) + this_cpu_write(cpu_dr7, dr7); + barrier(); =20 - set_debugreg(*dr7, 7); + set_debugreg(dr7, 7); + if (info->mask) - amd_set_dr_addr_mask(info->mask, i); + amd_set_dr_addr_mask(enable ? info->mask : 0, slot); =20 - return 0; + /* + * Disabling: + * Ensure the write to cpu_dr7 is after we've set the DR7 register. + * This ensures an NMI never see cpu_dr7 0 when DR7 is not. + */ + if (!enable) + this_cpu_write(cpu_dr7, dr7); } =20 /* - * Uninstall the breakpoint contained in the given counter. - * - * First we search the debug address register it uses and then we disable - * it. - * - * Atomic: we hold the counter->ctx->lock and we only handle variables - * and registers local to this cpu. + * find suitable breakpoint slot and set it up based on the action */ -void arch_uninstall_hw_breakpoint(struct perf_event *bp) +static int arch_manage_bp(struct perf_event *bp, enum bp_slot_action actio= n) { - struct arch_hw_breakpoint *info =3D counter_arch_bp(bp); - unsigned long dr7; - int i; + struct arch_hw_breakpoint *info; + int slot; =20 lockdep_assert_irqs_disabled(); =20 - for (i =3D 0; i < HBP_NUM; i++) { - struct perf_event **slot =3D this_cpu_ptr(&bp_per_reg[i]); - - if (*slot =3D=3D bp) { - *slot =3D NULL; - break; - } - } - - if (WARN_ONCE(i =3D=3D HBP_NUM, "Can't find any breakpoint slot")) - return; + slot =3D manage_bp_slot(bp, action); + if (slot < 0) + return slot; =20 - dr7 =3D this_cpu_read(cpu_dr7); - dr7 &=3D ~__encode_dr7(i, info->len, info->type); + info =3D counter_arch_bp(bp); + setup_hwbp(info, slot, action !=3D BP_SLOT_ACTION_UNINSTALL); =20 - set_debugreg(dr7, 7); - if (info->mask) - amd_set_dr_addr_mask(0, i); + return 0; +} =20 - /* - * Ensure the write to cpu_dr7 is after we've set the DR7 register. - * This ensures an NMI never see cpu_dr7 0 when DR7 is not. - */ - barrier(); +int arch_install_hw_breakpoint(struct perf_event *bp) +{ + return arch_manage_bp(bp, BP_SLOT_ACTION_INSTALL); +} =20 - this_cpu_write(cpu_dr7, dr7); +void arch_uninstall_hw_breakpoint(struct perf_event *bp) +{ + arch_manage_bp(bp, BP_SLOT_ACTION_UNINSTALL); } =20 static int arch_bp_generic_len(int x86_len) --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 852AD28727E for ; Mon, 10 Nov 2025 16:36:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792614; cv=none; b=OFp8C+OsUO+ISRCcx8EhRYGqPvQUuEl2+kpCicq6yWYd1MjMJSEzasEOaKrWiVBrTNBcrX7gDrjJaM846zo7zzuQcU7EgArrgPnCGe67v6gcYiM9QmI3CI7gU6dyMn9r5l4JPSWX2Z0w0w40I7GNZxTTHTTQ6mZOlXryalI1v4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792614; c=relaxed/simple; bh=ZEq5LCeeZxdZtY+a+kvZMIoWs2Mzj+sOmq7f1LtT1gE=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YhpPwrW0J4xrnSRZfXico9lZiY/fhqrP+MLM5+cbcPCD7ElxWSW3buawTG/wpPp/vSQ+Zoy5HnkEl7/+qsoy1yZGmomCZu9TxQ5zZDnjK5i3CnEqk8q0Zvmq00YnDRgcBOFi2IC/t8A0PjYlDq8v7sPONUntRX83agMFH3wvKAE= 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=jRabdu46; arc=none smtp.client-ip=209.85.214.175 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="jRabdu46" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-295247a814bso41646505ad.0 for ; Mon, 10 Nov 2025 08:36:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792610; x=1763397410; 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=bd/jIJYyk1G6SYf0mNQVLBMDX4oiGoErfqkyMXn4+LQ=; b=jRabdu46VsVuOHR63vEIRW4bFJWrUyUPWRGPfiFdsRr56n1/hyTUrG+utaSuwDnNSg dh8h3wgEkVWPMh82JArPRV9IvCttNB7kRZBpzMV2PdugyV/V6dipUVX1gjBuYk/Ehskx GJvmuwlqS0+G5X1krRIgPjL9F5ksQm+BwQEsVL8p/G8GFnO3s9K+xBMLg6UfzhNBpNym eNednzosnU7icEqs6IJhLw111u/Vnwe13TwOm5jbP01keq8CL4muYrVp1NNo8Wn3k1Q6 2zn4b6FDMVmhayhu9RwNoNLkHQ4BdO8w55/KHEVA+xFAnCcg9JkgIwnnp2nIkQAQNF7K frIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792610; x=1763397410; 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=bd/jIJYyk1G6SYf0mNQVLBMDX4oiGoErfqkyMXn4+LQ=; b=CSbHzGdyPPhrOORegwVKINZIDLkkkOmhbwVaSDbUleTelH66YlcMsKq6qu+lv1FV6H OkmbrtDVHaRUFPi2KE/pqa/lxCtlGLYVAa6RS90KWWDJqebpZ/s4n5yM8xdtOb03BhOE HLz38ev68lmqg5VtycsJeoWUx9grPkGn33kS8PinGKsPLrH9AFsxvkIF6E8pxP5aVWGY UV7lBKyXR6qkxlhG5TIUUZRosYx2jIoImZna39JnEz9Y82GLZT249wWrqPZpd5UWVsQg D3+RFdfyKXQqiY3b+ExhqVFxTm/CQpYfucNPfE5eVLxUG5mlVqd9GaJIn6bnnXlopW5q kGJg== X-Forwarded-Encrypted: i=1; AJvYcCVWgZWTYwYru5ZWLxuPNGrLmrGSIjAVUg9+R6hgkr5xzXtfQMHTPUetrbGsYHfPmBV2HlGtmLuThoxWrUQ=@vger.kernel.org X-Gm-Message-State: AOJu0YwUNDHkC5fwkXhN45lB4PdzAYMWX8Aw4v+TRpr0mm+BHRcavPZ3 xPwxaQtS+vbRPZadstSITwBJq+Nn39DHSmilfRPsIBdiqL52osqIng5Q X-Gm-Gg: ASbGncuqx//y2KX0DHi22q5l5X5C2pQ60XB29lSNT3z+g+4pkonESKVHOmrDT0KVpae cGUcoLwLOr3tEgGWkKc5IRSJKAFELD3kTd1I1W9fkj91tXh2If8A0qOFS74a9at8Q2tfYrJbAAV rfh32QYCwXwLQ1mTuGpfPvRBXxIRCJheNWl7k92Q0Uf3MUTxg7MD6rfNvv+O0DgXR8C3ep7oAev CkXArX/ZDsVvfM/ub7WsUoFOeDtNJTa+4h3osznxdUbRiY7bJwCOnJBPY+Pr+k19/cMracKawCO ThTz3Q/JXtlV3d+VsA0okCtxXsN4LM7s3K0m9acPYl24Rns85fZw8gLfR7gR5/NTUlC+RYFNx5I UCzDSc9TQBgHA7hjyqNM+BQGFQpCa1PXc49V4OAs+FPWz4x4wKn0bmD79Z2lDfHCl89jR5oJDjE ZyU6EUnIK42LjI5q7hIs192g== X-Google-Smtp-Source: AGHT+IHC7aPoZ6nrAIOpkNf/Po8HO2SSr/NWVgq/h5VzJS+qKaFGcmt1davBTXDotW4c33fFW6QGWA== X-Received: by 2002:a17:903:2f86:b0:295:1626:6be5 with SMTP id d9443c01a7336-297e57090dcmr104835365ad.44.1762792609610; Mon, 10 Nov 2025 08:36:49 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29650c5d0c1sm150508695ad.27.2025.11.10.08.36.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:36:49 -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 02/27] x86/hw_breakpoint: Add arch_reinstall_hw_breakpoint Date: Tue, 11 Nov 2025 00:35:57 +0800 Message-ID: <20251110163634.3686676-3-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" The new arch_reinstall_hw_breakpoint() function can be used in an atomic context, unlike the more expensive free and re-allocation path. This allows callers to efficiently re-establish an existing breakpoint. Signed-off-by: Jinchao Wang Reviewed-by: Masami Hiramatsu (Google) --- arch/x86/include/asm/hw_breakpoint.h | 2 ++ arch/x86/kernel/hw_breakpoint.c | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/arch/x86/include/asm/hw_breakpoint.h b/arch/x86/include/asm/hw= _breakpoint.h index aa6adac6c3a2..c22cc4e87fc5 100644 --- a/arch/x86/include/asm/hw_breakpoint.h +++ b/arch/x86/include/asm/hw_breakpoint.h @@ -21,6 +21,7 @@ struct arch_hw_breakpoint { =20 enum bp_slot_action { BP_SLOT_ACTION_INSTALL, + BP_SLOT_ACTION_REINSTALL, BP_SLOT_ACTION_UNINSTALL, }; =20 @@ -65,6 +66,7 @@ extern int hw_breakpoint_exceptions_notify(struct notifie= r_block *unused, =20 =20 int arch_install_hw_breakpoint(struct perf_event *bp); +int arch_reinstall_hw_breakpoint(struct perf_event *bp); void arch_uninstall_hw_breakpoint(struct perf_event *bp); void hw_breakpoint_pmu_read(struct perf_event *bp); void hw_breakpoint_pmu_unthrottle(struct perf_event *bp); diff --git a/arch/x86/kernel/hw_breakpoint.c b/arch/x86/kernel/hw_breakpoin= t.c index 3658ace4bd8d..29c9369264d4 100644 --- a/arch/x86/kernel/hw_breakpoint.c +++ b/arch/x86/kernel/hw_breakpoint.c @@ -99,6 +99,10 @@ static int manage_bp_slot(struct perf_event *bp, enum bp= _slot_action action) old_bp =3D NULL; new_bp =3D bp; break; + case BP_SLOT_ACTION_REINSTALL: + old_bp =3D bp; + new_bp =3D bp; + break; case BP_SLOT_ACTION_UNINSTALL: old_bp =3D bp; new_bp =3D NULL; @@ -187,6 +191,11 @@ int arch_install_hw_breakpoint(struct perf_event *bp) return arch_manage_bp(bp, BP_SLOT_ACTION_INSTALL); } =20 +int arch_reinstall_hw_breakpoint(struct perf_event *bp) +{ + return arch_manage_bp(bp, BP_SLOT_ACTION_REINSTALL); +} + void arch_uninstall_hw_breakpoint(struct perf_event *bp) { arch_manage_bp(bp, BP_SLOT_ACTION_UNINSTALL); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 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 C97E6283FC5 for ; Mon, 10 Nov 2025 16:36:54 +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=1762792618; cv=none; b=mAka5qIeIoJjEkj2aQVn3Xr/6aNlSxSdRMt/vlZlDPjkw9YFrfEmsewCoz22miTQmFfOPrllwalVTtOICQcz2AQGVeDancS7Zrj7fp+5yTCxSaqTBNAwYmVPJuY8j+jxkFiznnPl+kAs+pF7DpR/0oHdg7LlEmW3xO2t7XeA/dM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792618; c=relaxed/simple; bh=IHekeM/5ALv1Tfapztub9UTNCtQ4Mse7xBlGJh7bRJo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OGmHeG8douNiakqiQfjEiCGlsRfBoTMUJryxjstiaP5z+aftC0ZlkVtXGsqQzGqwJwp2cokKhCA4AagpFhGzesUcGmPypATcYBOCqbj9Xzt61NpO8fbUCRREkVp2XcLkro6V7lY8ClNP6Kv5FZRB3bOBgkQfkNNu723Bi95TEbg= 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=PmiUEwuq; 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="PmiUEwuq" Received: by mail-pj1-f47.google.com with SMTP id 98e67ed59e1d1-34374febdefso2239157a91.0 for ; Mon, 10 Nov 2025 08:36:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792614; x=1763397414; 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=Rf1O2IW8PmKXDZ3O4r/3b4myYk+kPZmKXtIohIyK/BM=; b=PmiUEwuqVYNOPV1B/GqHXOCsHOZk4+3HYqthCi8Z/MSqaSSztFyWWBVT0W5FPxkn4u W9XI/RVzqHGje3SXicSWwF2QRyrJ2X6u3ICNiG/5pXSQl9dtDo8guxo/+jP3vwISz5cQ 3p69yZsiOkpzr3TSxNg3rYE+iVm/JfRHE55BP3VdhSeCbGA/qGq7utygAblqppU3Jpk4 z2nwLKrl1xL1iX2Gz0iep2/Q0p57hMF1iwd3WG1X8FOzDQY//VLIzcI6D6RUPJoylfrZ mDAn6LXIJkADfsBJmYG2GGp/LutdytrwcCYKPB7V/3oT6XC1rA33/GUgujksjststUZ+ lLNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792614; x=1763397414; 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=Rf1O2IW8PmKXDZ3O4r/3b4myYk+kPZmKXtIohIyK/BM=; b=riW0kM8Lq5NOr53USppbxGcBd5loJ2c2Prw26X0fenloxqiOt9LWsXuRcK1CLODaKB jY6cE7s8xxwbFLFplt1wSw4nmNx8MB28LcnHEZf+PDyCNPXXAmFfBn/br7j84uWDeuIk EbQyBIDw4U3TEUsl2DHp1VwZY1kHln7fM9fgPM9qpoPRHxcsOiYTA5zDBlh7FQnO39Il dkBPNvm0R13dlJcT7722S4P+NIKxlN+Ak7KoWAzcWZTAVMVEBp29PvdT/dT7pnld3eSi 0P9va0p/GSAZYuHAS8201RP1HKImc+b6WMKuwLc/Law+gCfbiPFHXeRqCsZyFzDloB6i aZ9A== X-Forwarded-Encrypted: i=1; AJvYcCVqb+GdduHWL8koPv/ia8h4JZVoD8EiiXnpiMkWmJk9vmXtHSqqvIMJBNjPkDCFkvW1EPJ9PPThmlMi9+Y=@vger.kernel.org X-Gm-Message-State: AOJu0Yzb0cKqUcmqWwy753nFZ1yRDEAQyW0s0OWrV93mH2h7vrFpQaOX 0mtj51XDDo2dZXanK6E2e8Wmvtx9sHg6VeKkJLIdsFjoMSu+npQxzaio X-Gm-Gg: ASbGnctK+aeIz2uuTyx25OAD06258JcaUEZprYr26lQvHtE80hrO0VmzGVL/OrhKXLu jORVWoX/0L8LwjkioUrtwL3ZHZwCW5L5Akwrso4AdEujO5Y3sbSca+O/ZtnwboPb+PlDSmFPozH dtx0xdvtotGMF8zPVifaw8yeNXgkxHc/XtpgWctcCcZRHC/a+koG4SZNFwOp+N39k2U2taw+4ew T0TIzR/hy4GP7cMGHImxW4AflWZJPJVHI3XHVmzvIVkhEef8+L6towsOYcIyAVN3LxWSdX1UVyj Ll6XHmOMvHkG87uEVwA/fvXMUOhf6dJhvUXj3tDEBjwL+f8rduZWcjnIIneI1OdmNogXGTlu2Vb xrLp/7YDuZFutAbSBEqB40vqi4OSZ0Q88x0zQKSiEOUF1ISWG+y+V1WCUScZwkykue5zsUeW3hg EyuVHxSpEe8so= X-Google-Smtp-Source: AGHT+IF/jQs1f7B6cG5SH+bBA2T5zaJBaOf+IxgFwWIZkh8f5KWpsuxpiPdi/QInw6w6XvvtLJa9gQ== X-Received: by 2002:a17:90a:bf06:b0:343:a631:28b1 with SMTP id 98e67ed59e1d1-343a6312b7emr3517293a91.16.1762792613858; Mon, 10 Nov 2025 08:36:53 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-341d0adc41dsm5956374a91.1.2025.11.10.08.36.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:36:53 -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 03/27] HWBP: Add modify_wide_hw_breakpoint_local() API Date: Tue, 11 Nov 2025 00:35:58 +0800 Message-ID: <20251110163634.3686676-4-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" From: "Masami Hiramatsu (Google)" Add modify_wide_hw_breakpoint_local() arch-wide interface which allows hwbp users to update watch address on-line. This is available if the arch supports CONFIG_HAVE_REINSTALL_HW_BREAKPOINT. Note that this allows to change the type only for compatible types, because it does not release and reserve the hwbp slot based on type. For instance, you can not change HW_BREAKPOINT_W to HW_BREAKPOINT_X. Signed-off-by: Masami Hiramatsu (Google) --- arch/Kconfig | 10 ++++++++++ arch/x86/Kconfig | 1 + include/linux/hw_breakpoint.h | 6 ++++++ kernel/events/hw_breakpoint.c | 37 +++++++++++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) diff --git a/arch/Kconfig b/arch/Kconfig index 61130b88964b..c45fe5366125 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -456,6 +456,16 @@ config HAVE_MIXED_BREAKPOINTS_REGS Select this option if your arch implements breakpoints under the latter fashion. =20 +config HAVE_REINSTALL_HW_BREAKPOINT + bool + depends on HAVE_HW_BREAKPOINT + help + Depending on the arch implementation of hardware breakpoints, + some of them are able to update the breakpoint configuration + without release and reserve the hardware breakpoint register. + What configuration is able to update depends on hardware and + software implementation. + config HAVE_USER_RETURN_NOTIFIER bool =20 diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index fa3b616af03a..4d2ef8a45681 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -245,6 +245,7 @@ config X86 select HAVE_FUNCTION_TRACER select HAVE_GCC_PLUGINS select HAVE_HW_BREAKPOINT + select HAVE_REINSTALL_HW_BREAKPOINT select HAVE_IOREMAP_PROT select HAVE_IRQ_EXIT_ON_IRQ_STACK if X86_64 select HAVE_IRQ_TIME_ACCOUNTING diff --git a/include/linux/hw_breakpoint.h b/include/linux/hw_breakpoint.h index db199d653dd1..ea373f2587f8 100644 --- a/include/linux/hw_breakpoint.h +++ b/include/linux/hw_breakpoint.h @@ -81,6 +81,9 @@ register_wide_hw_breakpoint(struct perf_event_attr *attr, perf_overflow_handler_t triggered, void *context); =20 +extern int modify_wide_hw_breakpoint_local(struct perf_event *bp, + struct perf_event_attr *attr); + extern int register_perf_hw_breakpoint(struct perf_event *bp); extern void unregister_hw_breakpoint(struct perf_event *bp); extern void unregister_wide_hw_breakpoint(struct perf_event * __percpu *cp= u_events); @@ -124,6 +127,9 @@ register_wide_hw_breakpoint(struct perf_event_attr *att= r, perf_overflow_handler_t triggered, void *context) { return NULL; } static inline int +modify_wide_hw_breakpoint_local(struct perf_event *bp, + struct perf_event_attr *attr) { return -ENOSYS; } +static inline int register_perf_hw_breakpoint(struct perf_event *bp) { return -ENOSYS; } static inline void unregister_hw_breakpoint(struct perf_event *bp) { } static inline void diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c index 8ec2cb688903..5ee1522a99c9 100644 --- a/kernel/events/hw_breakpoint.c +++ b/kernel/events/hw_breakpoint.c @@ -887,6 +887,43 @@ void unregister_wide_hw_breakpoint(struct perf_event *= __percpu *cpu_events) } EXPORT_SYMBOL_GPL(unregister_wide_hw_breakpoint); =20 +/** + * modify_wide_hw_breakpoint_local - update breakpoint config for local CPU + * @bp: the hwbp perf event for this CPU + * @attr: the new attribute for @bp + * + * This does not release and reserve the slot of a HWBP; it just reuses the + * current slot on local CPU. So the users must update the other CPUs by + * themselves. + * Also, since this does not release/reserve the slot, this can not change= the + * type to incompatible type of the HWBP. + * Return err if attr is invalid or the CPU fails to update debug register + * for new @attr. + */ +#ifdef CONFIG_HAVE_REINSTALL_HW_BREAKPOINT +int modify_wide_hw_breakpoint_local(struct perf_event *bp, + struct perf_event_attr *attr) +{ + int ret; + + if (find_slot_idx(bp->attr.bp_type) !=3D find_slot_idx(attr->bp_type)) + return -EINVAL; + + ret =3D hw_breakpoint_arch_parse(bp, attr, counter_arch_bp(bp)); + if (ret) + return ret; + + return arch_reinstall_hw_breakpoint(bp); +} +#else +int modify_wide_hw_breakpoint_local(struct perf_event *bp, + struct perf_event_attr *attr) +{ + return -EOPNOTSUPP; +} +#endif +EXPORT_SYMBOL_GPL(modify_wide_hw_breakpoint_local); + /** * hw_breakpoint_is_used - check if breakpoints are currently used * --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (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 819892F693A for ; Mon, 10 Nov 2025 16:37:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792624; cv=none; b=LhB7ma2ORQ7F6DpJ2e2NxztXLo2sdJ9On5ck4xqlZpwSMYskzhLdRb9WqwgspXR74SfVUr9H2nBAWzQvmN2OvxtFobHFu9X3dlC/vyWQuFxfRRPZsErnKg7AXh8ogrQ84zWr74h/CivmcnsJBOm/ltjcoWXkZsAtZMkHteskTvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792624; c=relaxed/simple; bh=PyR/1C/G7IpUGl4mLUfLzY/550ANKp9vcF1hQ7PDNTc=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iM0OE2c999kCo4WRhEJiS6/RNoSfNuSeL029c8wNfJ2eBLYXUI5aCZFQhdE7Cj+I5TixMcAJeUqnkaK0GI+o6JacylhfCDOh0DF/L/xF9KzWC6XLfnumMzqpY+yHpTVnmRPBQSMdUvq8o9/dkkfqqTSgostLqo/FL2sCQskgCfA= 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=cJ7QzKd4; arc=none smtp.client-ip=209.85.210.174 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="cJ7QzKd4" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-7833765433cso3654296b3a.0 for ; Mon, 10 Nov 2025 08:37:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792619; x=1763397419; 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=3XURKXnAmZnyauprzc95LAi6JcC8IVZIZCwVkARWD9E=; b=cJ7QzKd4JpyYdNppBQQ/Kfo20vq4KOUASijWOu5m3zhCssi7oiv05+C+KpocyAj3kj DT2MsdJTrf+iMWfcyDVIt4KgaLX566n0d3AZwko9CK3/IIxZ+PNgOw+jrvBBIu/dhhn7 FVJULocjMpt2tbiFVaZOsE3jHwzUYHYtO8GamBsfbj0n8fPmly/nyCDWM8k/4EtWvi8z tfztH5SWLsI/pBvb5z6Lw/uO013/7SdWvWbjVPJ7z/15bV4s55zgOYwxQgQ2bR3H2+Eq Q14BS03/i/8J4e+QweERGzkXfOJ1b/CtslKoAP3HBJRKgFnSXbHVl71Q0x0qecldIq2B 0n4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792619; x=1763397419; 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=3XURKXnAmZnyauprzc95LAi6JcC8IVZIZCwVkARWD9E=; b=Cn/vVFNiWQ8L7rVFe4qTGHQRCIrJijDrX4AVsN22dqKjmqskW2LF+NjpIlhm3jnKNd h8J+ftg+FG8NvCRAwHnlKfD3FRj52oYj2Uel6STYmYU1MLZrhSei+coJz9hmmlWLeTr3 BAA3zq+Xf39Oefa6SJUvVAsFm9SEL2xAyBKGhZikxrIC1hTasieUNxLwtlQuIbcHrkZk Ylwl+e+J94TzXnkh4p66KdB7ygaXu4DPsfn5xMyOyPEK7eNC+iDGn29Uf+51HiuXRjj1 3L+hEKopgNfArQdXY/BeMe3cxN1Y9Ghl+mVG465tOL8A2bcSkp9/O08V0dfPTdYbx5Jr j5Uw== X-Forwarded-Encrypted: i=1; AJvYcCXlP80cANKBy/hhU7rvhGHejGc9N824FYmt0EZoDtI2cqdazDPTLoAvTtN0cdEsUO/5PS/5mS/Db6J38TI=@vger.kernel.org X-Gm-Message-State: AOJu0YynKKGqz5fTVqhyCsYVNUp180cJqJniWLyTIL/uHT0DXsgVpSV1 u6j0Gaq3sCUnpInYN9rNiw0qM77tMhwujm2mIJGx8G/YACAfZUWTJnTf X-Gm-Gg: ASbGncsS0tvJjfbNh33qb3IUEET09GR8FJZcKPhpjRRICAo5bVSrXeGZ4gjF3Wnc8vs xfSguywcH7baIDjRCRL4Y8vW/5AaesxQXfPQfgRJwAnbQuCQ+OBCQCh1KIaNakMefC4Z04zUrHF I6XYRA7Ll9UYhQwHjJEe/AfxCQM1e7XW3YKy9L4tiw9KS9bYAIdO77o093PM80y9C/JrgDRBJfj McToR734YiwuJ55vehqFX2ESRNJ2U/3kEoOghx+ir+Yl3F6Dvcgs/S/T7Q12LYZUMNgcQkBdMzR wM6YzO8r7V4Ihz34AC4/YUTB4QthWZ0t5WCvf8rKSuC6daRDjS+sZLWw9xE+VEga1TFLBVEqYwK fsEOBE48YrhDoFf63A3Iqgo3DbNEcQc0gAKmIHFPeGQAwF4MBzdS8kWIUJ/m8L0AFRsRSuQwRbc E1pUjytQOGmkisRCRwvNU1VA== X-Google-Smtp-Source: AGHT+IE4ra3aOU6AC3VY9Gg6aAiRBbZVp1TeJobqAmTS20aSxV2gTSQJIElDd/SxyV53Mq4DKRR9AA== X-Received: by 2002:a05:6a21:681:b0:334:97a6:17f2 with SMTP id adf61e73a8af0-353a18b7968mr11417885637.14.1762792619356; Mon, 10 Nov 2025 08:36:59 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ba900eab130sm13106983a12.25.2025.11.10.08.36.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:36:58 -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 04/27] mm/ksw: add build system support Date: Tue, 11 Nov 2025 00:35:59 +0800 Message-ID: <20251110163634.3686676-5-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 Kconfig and Makefile infrastructure. The implementation is located under `mm/kstackwatch/`. Signed-off-by: Jinchao Wang --- include/linux/kstackwatch.h | 5 +++++ mm/Kconfig | 1 + mm/Makefile | 1 + mm/kstackwatch/Kconfig | 14 ++++++++++++++ mm/kstackwatch/Makefile | 2 ++ mm/kstackwatch/kernel.c | 23 +++++++++++++++++++++++ mm/kstackwatch/stack.c | 1 + mm/kstackwatch/watch.c | 1 + 8 files changed, 48 insertions(+) create mode 100644 include/linux/kstackwatch.h create mode 100644 mm/kstackwatch/Kconfig create mode 100644 mm/kstackwatch/Makefile create mode 100644 mm/kstackwatch/kernel.c create mode 100644 mm/kstackwatch/stack.c create mode 100644 mm/kstackwatch/watch.c diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h new file mode 100644 index 000000000000..0273ef478a26 --- /dev/null +++ b/include/linux/kstackwatch.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _KSTACKWATCH_H +#define _KSTACKWATCH_H + +#endif /* _KSTACKWATCH_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 0e26f4fc8717..61d4e6edadf2 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1373,5 +1373,6 @@ config FIND_NORMAL_PAGE def_bool n =20 source "mm/damon/Kconfig" +source "mm/kstackwatch/Kconfig" =20 endmenu diff --git a/mm/Makefile b/mm/Makefile index 21abb3353550..efc101816f00 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -92,6 +92,7 @@ obj-$(CONFIG_PAGE_POISONING) +=3D page_poison.o obj-$(CONFIG_KASAN) +=3D kasan/ obj-$(CONFIG_KFENCE) +=3D kfence/ obj-$(CONFIG_KMSAN) +=3D kmsan/ +obj-$(CONFIG_KSTACKWATCH) +=3D kstackwatch/ obj-$(CONFIG_FAILSLAB) +=3D failslab.o obj-$(CONFIG_FAIL_PAGE_ALLOC) +=3D fail_page_alloc.o obj-$(CONFIG_MEMTEST) +=3D memtest.o diff --git a/mm/kstackwatch/Kconfig b/mm/kstackwatch/Kconfig new file mode 100644 index 000000000000..496caf264f35 --- /dev/null +++ b/mm/kstackwatch/Kconfig @@ -0,0 +1,14 @@ +config KSTACKWATCH + bool "Kernel Stack Watch" + depends on HAVE_HW_BREAKPOINT && KPROBES && FPROBE && STACKTRACE + help + A lightweight real-time debugging tool to detect stack corruption + and abnormal stack usage patterns in the kernel. It monitors stack + boundaries and detects overwrites in real time using hardware + breakpoints and probe-based instrumentation. + + This feature is intended for kernel developers or advanced users + diagnosing rare stack overflow or memory corruption bugs. It may + introduce minor overhead during runtime monitoring. + + If unsure, say N. diff --git a/mm/kstackwatch/Makefile b/mm/kstackwatch/Makefile new file mode 100644 index 000000000000..c99c621eac02 --- /dev/null +++ b/mm/kstackwatch/Makefile @@ -0,0 +1,2 @@ +obj-$(CONFIG_KSTACKWATCH) +=3D kstackwatch.o +kstackwatch-y :=3D kernel.o stack.o watch.o diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c new file mode 100644 index 000000000000..78f1d019225f --- /dev/null +++ b/mm/kstackwatch/kernel.c @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include + +static int __init kstackwatch_init(void) +{ + pr_info("module loaded\n"); + return 0; +} + +static void __exit kstackwatch_exit(void) +{ + pr_info("module unloaded\n"); +} + +module_init(kstackwatch_init); +module_exit(kstackwatch_exit); + +MODULE_AUTHOR("Jinchao Wang"); +MODULE_DESCRIPTION("Kernel Stack Watch"); +MODULE_LICENSE("GPL"); + diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c new file mode 100644 index 000000000000..cec594032515 --- /dev/null +++ b/mm/kstackwatch/stack.c @@ -0,0 +1 @@ +// SPDX-License-Identifier: GPL-2.0 diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c new file mode 100644 index 000000000000..cec594032515 --- /dev/null +++ b/mm/kstackwatch/watch.c @@ -0,0 +1 @@ +// SPDX-License-Identifier: GPL-2.0 --=20 2.43.0 From nobody Sun Feb 8 09:33:07 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 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pj1-f52.google.com (mail-pj1-f52.google.com [209.85.216.52]) (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 A20CA2F693A for ; Mon, 10 Nov 2025 16:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.52 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792631; cv=none; b=geNNOaUC8dwzeIKkrekJg0j/WWUfMX0jUXZUY/AxQUlqkgfL0O7BlXeTY95j8Y4ldTLy8TyjQrTPpMGBhgW/RJBZYH6EwFXw4/A/KX+/ieqSvBFY6HS+A7yrRPNeIFWbwR2CGSXKvju/tSF4yuKTF9aGXV7HAdZN+9qxhIdRqHM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792631; c=relaxed/simple; bh=Ima2e4/C5WqdeZyYCnDIwbDgz9ZYOLESM4kickHutQ8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PW8p0pyz/tATG7iDSOi+jTmZHkGTRLJ3Ilwxd/mpAmP6rjmdDs428E+bJ1GS24nHhHdiEFNxFB2PW6x/2rr3PmLZ6jbJjo90T4khorvqanHwVlprL/Drtk/weCRKwKIY7Iu4LMGVRNa2S91f2SJQm35Kbv3gi7Abn50lNiCyMHI= 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=PSIZCYhG; arc=none smtp.client-ip=209.85.216.52 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="PSIZCYhG" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-3436a97f092so2884607a91.3 for ; Mon, 10 Nov 2025 08:37:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792629; x=1763397429; 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=RDpE0CvbPiS95CD/e7bDhFl1FHfdcNhWeVLe6GjR39g=; b=PSIZCYhGt4Dd7vdyEl0mOkvONblClWeuS4QZ5ZGZ0MedDHaXvNpz8ADNwK6qenO/nl Pb/UnAJOqDscQSNuGq5DvTveWqzQNJRjZR74EfcpeSq51CYCe8HZQf1eSXkClT3lN4v5 jDA4Uf1eMXEwoFGNz7xw5vivscquUJFUyP3J6VHHZKqUBpHtJy+DPu7PXVAYSUuoVyJP aB4usMrzfDrouMV5Cvutjl1EAzaR4YDI8xNeDhDMscnv5m1YQVAjiAs0jvblEGrrVfpv i1/D/oTtCvkXRE+Waa0EpWEcbdCvHLnzoFCbgrWsj1I31g3LKULWa5rQBUPlBE/yXd+A DXAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792629; x=1763397429; 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=RDpE0CvbPiS95CD/e7bDhFl1FHfdcNhWeVLe6GjR39g=; b=bo0vs3iGzZII8qpJ1g3xcWgKZ1jPPwLcM4Q7Tljxa4rNj8N4D1mhMuWqhfRVLItoGd 3qxrN1RM/6RIUagqdDuDX8UKoEGslttUukB6PGM+/fpfn9VYHWtGPx3kEemp7vorNPfE VD5Krvziqc9ldNe3Mc4IFauCb1+M17QYxD2Vc5Cy0KioXs8uo/KkFAz1s4djbZ6bBqK7 SmR3ELhrm8OEbzlwZF5eqEjhZPW1XAj1+wUUZo2/z2sEjY5Lk2GRbpbcuZifDF333A9s 70eD4hWoocDvTcZMy0HJgKNmto8XhDVnp6Q6eOwfiYCvX/OHsjfq+zmMFbnIDGVWc4FG MV3Q== X-Forwarded-Encrypted: i=1; AJvYcCUBzRdIuvHGKjWHAuhKLV+oeFGifySF4ndfJt8R9YQ7bziL2xuJrkZw43DwADnKl/c0qrDUCVKbhMId+IA=@vger.kernel.org X-Gm-Message-State: AOJu0YxHwjQfIbcMVMud7GVhjO03ULlbvwTojnTXM7aal+14xQZgmygb gssINWI1MLot7Wi8KzOLlLZcmh0TH5tMKu9r3rZ3RfmjgsMqdWdTkS5L X-Gm-Gg: ASbGnctaqDaHeWk/+VpVpxu0hvOjePrDousiM/NCVROeo/cAyc93jQqJwOuGzyc3qMS x4Pe7ujlKFZ+hLt2fggIY6k5yuICJmgZz/79BquFdkYm6gdlMdrCyAfYkuRHdJY043Y6k8uLKsu 7vp52gi+ImUiHuIsbRHd6F2FISQwTsBSF0dPRHi2jSrKa7ER91NnZgUCRkEHp/aPiiDMr2ke1Rg yzot0yliQ6icJm1U8zbECmi/2SBOfv5CB+ylwd6IIZIOdQsoCEQ1q5YDSXOiFLxaWTquiQTX3Wn K3wOJBpxC6URdVVHC8cONHAuBGXLl1bHdv95lpwFdGLxmTc+9CJeABQRVEHGo240cdDOK8mKnY4 DkkAAJaMNl8TdLAZ3u/XSg4SHEKzOh2BEB0KHqKhLOidWhSjrLDNkw6uJ5f1TxgP+e/GOZdLBGE dM1aZK5YbW0V7HTTSYpDunTA== X-Google-Smtp-Source: AGHT+IHLWcFC7W3aH7MtEyBpRAAfyoGWF1TXnIybIjXPJD9gYvIPg0ozGaDjCXeoqvDbHEVRJoG18g== X-Received: by 2002:a17:902:d481:b0:295:9b73:b15c with SMTP id d9443c01a7336-297e56f9c21mr125602655ad.42.1762792628683; Mon, 10 Nov 2025 08:37:08 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29650c5ce87sm150073335ad.29.2025.11.10.08.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:08 -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 06/27] mm/ksw: add singleton debugfs interface Date: Tue, 11 Nov 2025 00:36:01 +0800 Message-ID: <20251110163634.3686676-7-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" Provide the debugfs config file to read or update the configuration. Only a single process can open this file at a time, enforced using atomic config_file_busy, to prevent concurrent access. ksw_get_config() exposes the configuration pointer as const. Signed-off-by: Jinchao Wang --- include/linux/kstackwatch.h | 3 ++ mm/kstackwatch/kernel.c | 103 ++++++++++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index dd00c4c8922e..ada5ac64190c 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -35,4 +35,7 @@ struct ksw_config { char *user_input; }; =20 +// singleton, only modified in kernel.c +const struct ksw_config *ksw_get_config(void); + #endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 50104e78cf3d..87fef139f494 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -1,13 +1,18 @@ // SPDX-License-Identifier: GPL-2.0 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 +#include #include #include #include #include #include +#include =20 +static atomic_t dbgfs_config_busy =3D ATOMIC_INIT(0); static struct ksw_config *ksw_config; +static struct dentry *dbgfs_config; +static struct dentry *dbgfs_dir; =20 struct param_map { const char *name; /* long name */ @@ -76,7 +81,7 @@ static int ksw_parse_param(struct ksw_config *config, con= st char *key, * - 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) +static int ksw_parse_config(char *buf, struct ksw_config *config) { char *part, *key, *val; int ret; @@ -111,18 +116,110 @@ static int __maybe_unused ksw_parse_config(char *buf= , struct ksw_config *config) return 0; } =20 +static ssize_t ksw_dbgfs_read(struct file *file, char __user *buf, size_t = count, + loff_t *ppos) +{ + return simple_read_from_buffer(buf, count, ppos, ksw_config->user_input, + ksw_config->user_input ? strlen(ksw_config->user_input) : 0); +} + +static ssize_t ksw_dbgfs_write(struct file *file, const char __user *buffe= r, + size_t count, loff_t *ppos) +{ + char input[MAX_CONFIG_STR_LEN]; + int ret; + + if (count =3D=3D 0 || count >=3D sizeof(input)) + return -EINVAL; + + if (copy_from_user(input, buffer, count)) + return -EFAULT; + + input[count] =3D '\0'; + strim(input); + + if (!strlen(input)) { + pr_info("config cleared\n"); + return count; + } + + ret =3D ksw_parse_config(input, ksw_config); + if (ret) { + pr_err("Failed to parse config %d\n", ret); + return ret; + } + + return count; +} + +static int ksw_dbgfs_open(struct inode *inode, struct file *file) +{ + if (atomic_cmpxchg(&dbgfs_config_busy, 0, 1)) + return -EBUSY; + return 0; +} + +static int ksw_dbgfs_release(struct inode *inode, struct file *file) +{ + atomic_set(&dbgfs_config_busy, 0); + return 0; +} + +static const struct file_operations kstackwatch_fops =3D { + .owner =3D THIS_MODULE, + .open =3D ksw_dbgfs_open, + .read =3D ksw_dbgfs_read, + .write =3D ksw_dbgfs_write, + .release =3D ksw_dbgfs_release, + .llseek =3D default_llseek, +}; + +const struct ksw_config *ksw_get_config(void) +{ + return ksw_config; +} + static int __init kstackwatch_init(void) { + int ret =3D 0; + ksw_config =3D kzalloc(sizeof(*ksw_config), GFP_KERNEL); - if (!ksw_config) - return -ENOMEM; + if (!ksw_config) { + ret =3D -ENOMEM; + goto err_alloc; + } + + dbgfs_dir =3D debugfs_create_dir("kstackwatch", NULL); + if (!dbgfs_dir) { + ret =3D -ENOMEM; + goto err_dir; + } + + dbgfs_config =3D debugfs_create_file("config", 0600, dbgfs_dir, NULL, + &kstackwatch_fops); + if (!dbgfs_config) { + ret =3D -ENOMEM; + goto err_file; + } =20 pr_info("module loaded\n"); return 0; + +err_file: + debugfs_remove_recursive(dbgfs_dir); + dbgfs_dir =3D NULL; +err_dir: + kfree(ksw_config); + ksw_config =3D NULL; +err_alloc: + return ret; } =20 static void __exit kstackwatch_exit(void) { + debugfs_remove_recursive(dbgfs_dir); + kfree(ksw_config->func_name); + kfree(ksw_config->user_input); kfree(ksw_config); =20 pr_info("module unloaded\n"); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 BB1513126C7 for ; Mon, 10 Nov 2025 16:37:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792636; cv=none; b=Rfmh4w7w+u+JfjOUa8DVVCLrsYEebs8r7JVjQC+3Jwgbw7EzRYFwV9W30WnPtvyICnoRH16pYy6SHclZMGcnj0Eob0w3OiacNzEXcMXKB2LYQT+UhK1CggAH7A/yAgi2eNKL1U8l2st0t+GqyIme/WqUf8Sc4E3Dra5+JsVNBfY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792636; c=relaxed/simple; bh=tFikxSFFwCv1IrH9L1W4062VVacCAi+rgy+45fumM8s=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Q0WTHCwHg29dGmXMhK2N+kL1V16FCJtw+5EC2pOpv+mx4D0jkIOnbT8o2KFqsrkMNVWPxRptBzveG2raiti1/KZu2/N9SZm2/hoOz7AN6wDff+mVJLf8wuLUXX/3FljlesvVeICQpbV21ifbFCRlGh+kK6zsUzJCAG9FjZNRj/Q= 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=JWRYCoeV; arc=none smtp.client-ip=209.85.210.171 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="JWRYCoeV" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7a9fb6fccabso2598460b3a.0 for ; Mon, 10 Nov 2025 08:37:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792633; x=1763397433; 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=OfQWKEWM8M6Sp/kqVVWTo1Q3rJtTZ9aAiY0lp7l1vgw=; b=JWRYCoeV4eIdOwpESd27XIKf+8iT9XrLIIU2jDVzQjePsiyeqGtpVSajomSGvRHYPr 73xZxrSgsxBIyk8T3RKT2tyUAQFRmn6O7j9G+5TXjJays7s51C5ZhLH2+1ELSnl3FVWx /j/8evvaTnz0ACzy2wBZgwWfMO1O00/UK1m6W4AxHzFzVzwZSFXDMJuQMPgmst2hvNyy YhPPih+OU+A1ZISMUp/YuElrzAzRqBsWJt/GwYFmNitde1cXClk1CVh3wSpkx4TF7f9z jeGYHw+nFoS7uWrJ/w0hmM0zCbf86hhoPxyXFBVxDzqrlL17t6qXgSMYm7isveahbFkq g7Kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792633; x=1763397433; 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=OfQWKEWM8M6Sp/kqVVWTo1Q3rJtTZ9aAiY0lp7l1vgw=; b=jmNTHjjm/k9dc0qVtDFXNAIAs9rEWW3chKTIUTQ2q+bdGwVb+FjSqKyNASvw3yzYux Stn7VMBnTuMXGKg+a9KR3DUg3oRq5b6TFxFxkHQtqhLUp6IdD+A6n9fUXLYqWgQFD4eC 8jfXAsk1Xdz73HTFq07BbGr9TS2X/c8waP63nAv5aULxqFQY30Zw/pGobwFRoXIOOs10 IHjl9YAa05Wp6/tWTIRci0vlXHLZsAhmyrrP31jEm3xcCRDjWQVanXec7d28sSkRdf2b LHVI0BDHg7PkJOOS8WlG4GsUmPYUmBaYiB/uf1Yc9YJbpyjLuh54DK7ipsdE/Hlg7WAA Karg== X-Forwarded-Encrypted: i=1; AJvYcCXuGa0G+hv5TLWYl93eNzrI9GvZCKuz2IPRiLcQeYoqHTxgT6puiu2svdpA5onl9yGPKO05/k7nkrnyNAw=@vger.kernel.org X-Gm-Message-State: AOJu0YzFo5HWhP/oe7H+ZQBpW8vSDqo0wKpZSJ7/xQB4qmky/u808Xnn LGNJcrk69mFYUwf5l1Isa8uEAW6afSQFveZ4/QkHWmwhu+Fh1JSaGm3g X-Gm-Gg: ASbGncteI9rJxoflBcTRSj/7FGr6QGgXJgAxs8eUcX+foDP6rcXMUT64CaUpsJSsdxs UZLLwjBdf1E+M0fJ0Xbi1+pmGypvK4n8CfG/t3rJAVhIokZmRatJ6AW82lgOh9Yx/hDmN4KqGcX ph6rVS5iI0za4AVHFkVQH8rTWNx6vN1wXVLfCHRFzcCeUHlwK1fOf8izj4r/sQr2qP8PeUEExho EnzEYrGH4mcCHSYMjdsUrUh+xqhxw0Aw17kI5yni2ujrTGXNV3KnU8KvWQClwn9Kb4tyEUkDKFr pfsqqay4lqgbGFoNFAIque/QE9kJzicYhQ27TFXZSgn0XxPnRMvZk96K5JgIcPHObFU2BY8rLKf 8369nCyyTgSI3fpEjup7k2tQ8gD45blfntnLtRKjNfAVGViUvQer91K8Goz4V2lagfOCR0eO8CA VObij/bVeGXO+4cJactm0jXA== X-Google-Smtp-Source: AGHT+IHERHOBNDq56+F7J5rW3t9aw2ypcpu6OwOvG+DJGpa6Vz1ez0X0Vnm2nUUH/tERSMimjoOmZw== X-Received: by 2002:a05:6a20:5483:b0:342:873d:7e62 with SMTP id adf61e73a8af0-353a2d42046mr10346355637.29.1762792632699; Mon, 10 Nov 2025 08:37:12 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b0cc17784bsm12559855b3a.47.2025.11.10.08.37.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:12 -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 07/27] mm/ksw: add HWBP pre-allocation Date: Tue, 11 Nov 2025 00:36:02 +0800 Message-ID: <20251110163634.3686676-8-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" Pre-allocate per-CPU hardware breakpoints at init with a place holder address, which will be retargeted dynamically in kprobe handler. This avoids allocation in atomic context. At most max_watch breakpoints are allocated (0 means no limit). Signed-off-by: Jinchao Wang --- include/linux/kstackwatch.h | 13 ++++++ mm/kstackwatch/watch.c | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index ada5ac64190c..eb9f2b4f2109 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -2,6 +2,9 @@ #ifndef _KSTACKWATCH_H #define _KSTACKWATCH_H =20 +#include +#include +#include #include =20 #define MAX_CONFIG_STR_LEN 128 @@ -38,4 +41,14 @@ struct ksw_config { // singleton, only modified in kernel.c const struct ksw_config *ksw_get_config(void); =20 +/* watch management */ +struct ksw_watchpoint { + struct perf_event *__percpu *event; + struct perf_event_attr attr; + struct llist_node node; // for atomic watch_on and off + struct list_head list; // for cpu online and offline +}; +int ksw_watch_init(void); +void ksw_watch_exit(void); + #endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index cec594032515..4947eac32c61 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -1 +1,94 @@ // SPDX-License-Identifier: GPL-2.0 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include + +static LLIST_HEAD(free_wp_list); +static LIST_HEAD(all_wp_list); +static DEFINE_MUTEX(all_wp_mutex); + +static ulong holder; + +static void ksw_watch_handler(struct perf_event *bp, + struct perf_sample_data *data, + struct pt_regs *regs) +{ + pr_err("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D KStackWatch: Caught stack corrupti= on =3D=3D=3D=3D=3D=3D=3D\n"); + pr_err("config %s\n", ksw_get_config()->user_input); + dump_stack(); + pr_err("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D KStackW= atch End =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); + + if (ksw_get_config()->panic_hit) + panic("Stack corruption detected"); +} + +static int ksw_watch_alloc(void) +{ + int max_watch =3D ksw_get_config()->max_watch; + struct ksw_watchpoint *wp; + int success =3D 0; + int ret; + + init_llist_head(&free_wp_list); + + //max_watch=3D0 means at most + while (!max_watch || success < max_watch) { + wp =3D kzalloc(sizeof(*wp), GFP_KERNEL); + if (!wp) + return success > 0 ? success : -EINVAL; + + hw_breakpoint_init(&wp->attr); + wp->attr.bp_addr =3D (ulong)&holder; + wp->attr.bp_len =3D sizeof(ulong); + wp->attr.bp_type =3D HW_BREAKPOINT_W; + wp->event =3D register_wide_hw_breakpoint(&wp->attr, + ksw_watch_handler, wp); + if (IS_ERR((void *)wp->event)) { + ret =3D PTR_ERR((void *)wp->event); + kfree(wp); + return success > 0 ? success : ret; + } + llist_add(&wp->node, &free_wp_list); + mutex_lock(&all_wp_mutex); + list_add(&wp->list, &all_wp_list); + mutex_unlock(&all_wp_mutex); + success++; + } + + return success; +} + +static void ksw_watch_free(void) +{ + struct ksw_watchpoint *wp, *tmp; + + mutex_lock(&all_wp_mutex); + list_for_each_entry_safe(wp, tmp, &all_wp_list, list) { + list_del(&wp->list); + unregister_wide_hw_breakpoint(wp->event); + kfree(wp); + } + mutex_unlock(&all_wp_mutex); +} + +int ksw_watch_init(void) +{ + int ret; + + ret =3D ksw_watch_alloc(); + if (ret <=3D 0) + return -EBUSY; + + + return 0; +} + +void ksw_watch_exit(void) +{ + ksw_watch_free(); +} --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 E21DF28314E for ; Mon, 10 Nov 2025 16:37:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792642; cv=none; b=DvEPAp33WXmwHBNoqlkVS5oQN3bwGIxp27S7yrf/FLk4yUD8wdCZE8AK1lPOrwpVMkn72DL56xQslySM56bQNDHBN3+o4o31Amf8cj7XRvoOXI3H1EB7PIQSrdFX5QqGpu43FQgXTbvhUa2RS4MDt+Bfx+blLMTc5xcfqIRYt1w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792642; c=relaxed/simple; bh=JF6+F5+AYlPr3XZARG3RggWEKB5pA9Os5BWchj7XHxM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eeBNaXCkUYBfsNCzjZIcrF91+KD6UX3i1s+OoweB/w8qGwOExE4pl14H6lrf748hwM5BqTdQ42x/LLdEHZP5V37buNsM1tV7pg8XmJHT+YtC8QwBPXN0BS7pO2wVw9Mltp3M4gyRZwo7u7vsD2pkjUHerGYmBImioLRbSh+EtMc= 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=k0wF0nWt; arc=none smtp.client-ip=209.85.214.169 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="k0wF0nWt" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-29558061c68so39113545ad.0 for ; Mon, 10 Nov 2025 08:37:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792637; x=1763397437; 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=Xci1h+5LLX8h0ueKKgEGcPTmlGP88Cc5ZUWnC1Z7B10=; b=k0wF0nWtM//Sd97/ySB2HxCf4+leQEnK3IH6FZZ+gLR7io5I3SZF4VgPexqCNW4DG6 dxTnzIQQzD90r2k4IHjzPeFk+iPmKwkJkTefqJ+eFU07kF24Ybkv1HycBwYx+U5IjPUg cOnq4KQTms5RX0TbgKmXf2hcRWw6TkhUofLLIW0WO++Vp48kI9A6YJ4nnYvaOEml/llu QVrfeExL0j2s1h8BXZs9jW3MQqfq9yYhGYOVkWYI54J/KROsvea1N0f4KDF+jsAQSwML LFvOhzZ1EMToks84N1QMUpj5a9+DXD3LqgQJ+ChgGalO1yrO9Rz1ZejO1LaRJlxMVkUD gA5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792637; x=1763397437; 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=Xci1h+5LLX8h0ueKKgEGcPTmlGP88Cc5ZUWnC1Z7B10=; b=W0qv6B6Yw7hajvwzMQ7cqTMRuGDJsaxb1+KT74fxGuPE5rf79O7URkaFXN2AxLJyCX YudGNRioKYMqDeLi+U3yVA46DqcTl0IC/8hBVjxl4NMLs7Yof/UzUDuoZRiumV/OFOV1 w4d5gCExFz0ZRoYSDrz1/yQOTx5iJVI3dDgAGVgLei4/k0ox2KF7qFpLvzJLnLNaKDFd o52xTAP5t0wgWtoDza5jbwuPKDIuH9RKizgWJXlQ/dlByALo2Wh5Uy6xdhMNTMIhkkpx GZNfMmn+VRbmP+wone9iUHtC8308P1Y3ZM1MSDA700O4cr4r214PVvwPFDF/tODtq4dm Wpvg== X-Forwarded-Encrypted: i=1; AJvYcCXkXLgzhxaTfKwaiMKBpdljt+KiIENonFHvdGteBlk8SMOwcWrFNXFr+m+zbbQCQ0jHBrhkMGqGWDCHAiE=@vger.kernel.org X-Gm-Message-State: AOJu0Yyr8H/aruI+xtW2L5riv6YbFSydYa2kz1DKs86gMR6l77JkUMJg A6d9z7/SRj+cUyghiisszOemowL4DG0jxfXH9Oz8cZvMii6rMZKDtTfZ X-Gm-Gg: ASbGncsUA0d1UoR2Gz/iluAcUREwVWLGWQ5qkU1qgXoSZ+lg9lvqk05MhbJc0gjXpoL XmmF+qQbcBvYGDQKTWJUzDi5dYD3HQgmHnFzHH3GW8HUxjJ5ry33YhVFywkO4EvhneOu0hCVy/F AN1dGorb756aG95Y4q+eFMR3XFNgq/8N+st65ZWYLb3WaOrdPCTolqfQUGqCi+hMEL2iSjVsBL8 IiRrNrqNq1qHJDbDM6WV+Rbbr/ztvPG81YiiJsViGnPgElnx/r9GwYHeaDtUb1AE+fQxdi0OtQZ GNonjBeqHA9H1NAlMiN3bzf5BhykK4YFKX+l4e0TkyTEnP6iJukAKbeW0BFdND8sJaxyVHotRBq 2+7kpS4tNOxgfHyIskq+XXl5KdqnOFfPmqv7dKZ6AxqdLbqLZ6Dp5AadL/gBtoXuQbbsqUn+YQO roB+oo5pVdwFpffcWEvTXkuzQc0eQqycAi X-Google-Smtp-Source: AGHT+IEGWB34N8M9pAG46FEM6eUFu44tr8SQ44nAFlldnOd0ujaAQ7y38dVr1Lx0wQIkS529Z8I+TQ== X-Received: by 2002:a17:903:1a44:b0:298:68e:4057 with SMTP id d9443c01a7336-298068e42a0mr86330195ad.59.1762792636805; Mon, 10 Nov 2025 08:37:16 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ba901e312a2sm13029584a12.31.2025.11.10.08.37.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:16 -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 08/27] mm/ksw: Add atomic watchpoint management api Date: Tue, 11 Nov 2025 00:36:03 +0800 Message-ID: <20251110163634.3686676-9-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 three functions for atomic lifecycle management of watchpoints: - ksw_watch_get(): Acquires a watchpoint from a llist. - ksw_watch_on(): Enables the watchpoint on all online CPUs. - ksw_watch_off(): Disables the watchpoint and returns it to the llist. For cross-CPU synchronization, updates are propagated using direct modification on the local CPU and asynchronous IPIs for remote CPUs. Signed-off-by: Jinchao Wang --- include/linux/kstackwatch.h | 4 ++ mm/kstackwatch/watch.c | 85 ++++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index eb9f2b4f2109..d7ea89c8c6af 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -44,11 +44,15 @@ const struct ksw_config *ksw_get_config(void); /* watch management */ struct ksw_watchpoint { struct perf_event *__percpu *event; + call_single_data_t __percpu *csd; struct perf_event_attr attr; struct llist_node node; // for atomic watch_on and off struct list_head list; // for cpu online and offline }; int ksw_watch_init(void); void ksw_watch_exit(void); +int ksw_watch_get(struct ksw_watchpoint **out_wp); +int ksw_watch_on(struct ksw_watchpoint *wp, ulong watch_addr, u16 watch_le= n); +int ksw_watch_off(struct ksw_watchpoint *wp); =20 #endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index 4947eac32c61..3817a172dc25 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -27,11 +27,83 @@ static void ksw_watch_handler(struct perf_event *bp, panic("Stack corruption detected"); } =20 +static void ksw_watch_on_local_cpu(void *info) +{ + struct ksw_watchpoint *wp =3D info; + struct perf_event *bp; + ulong flags; + int cpu; + int ret; + + local_irq_save(flags); + cpu =3D raw_smp_processor_id(); + bp =3D per_cpu(*wp->event, cpu); + if (!bp) { + local_irq_restore(flags); + return; + } + + ret =3D modify_wide_hw_breakpoint_local(bp, &wp->attr); + local_irq_restore(flags); + WARN(ret, "fail to reinstall HWBP on CPU%d ret %d", cpu, ret); +} + +static void ksw_watch_update(struct ksw_watchpoint *wp, ulong addr, u16 le= n) +{ + call_single_data_t *csd; + int cur_cpu; + int cpu; + + wp->attr.bp_addr =3D addr; + wp->attr.bp_len =3D len; + + cur_cpu =3D raw_smp_processor_id(); + for_each_online_cpu(cpu) { + /* remote cpu first */ + if (cpu =3D=3D cur_cpu) + continue; + csd =3D per_cpu_ptr(wp->csd, cpu); + smp_call_function_single_async(cpu, csd); + } + ksw_watch_on_local_cpu(wp); +} + +int ksw_watch_get(struct ksw_watchpoint **out_wp) +{ + struct ksw_watchpoint *wp; + struct llist_node *node; + + node =3D llist_del_first(&free_wp_list); + if (!node) + return -EBUSY; + + wp =3D llist_entry(node, struct ksw_watchpoint, node); + WARN_ON_ONCE(wp->attr.bp_addr !=3D (u64)&holder); + + *out_wp =3D wp; + return 0; +} +int ksw_watch_on(struct ksw_watchpoint *wp, ulong watch_addr, u16 watch_le= n) +{ + ksw_watch_update(wp, watch_addr, watch_len); + return 0; +} + +int ksw_watch_off(struct ksw_watchpoint *wp) +{ + WARN_ON_ONCE(wp->attr.bp_addr =3D=3D (u64)&holder); + ksw_watch_update(wp, (ulong)&holder, sizeof(ulong)); + llist_add(&wp->node, &free_wp_list); + return 0; +} + static int ksw_watch_alloc(void) { int max_watch =3D ksw_get_config()->max_watch; struct ksw_watchpoint *wp; + call_single_data_t *csd; int success =3D 0; + int cpu; int ret; =20 init_llist_head(&free_wp_list); @@ -41,6 +113,16 @@ static int ksw_watch_alloc(void) wp =3D kzalloc(sizeof(*wp), GFP_KERNEL); if (!wp) return success > 0 ? success : -EINVAL; + wp->csd =3D alloc_percpu(call_single_data_t); + if (!wp->csd) { + kfree(wp); + return success > 0 ? success : -EINVAL; + } + + for_each_possible_cpu(cpu) { + csd =3D per_cpu_ptr(wp->csd, cpu); + INIT_CSD(csd, ksw_watch_on_local_cpu, wp); + } =20 hw_breakpoint_init(&wp->attr); wp->attr.bp_addr =3D (ulong)&holder; @@ -50,6 +132,7 @@ static int ksw_watch_alloc(void) ksw_watch_handler, wp); if (IS_ERR((void *)wp->event)) { ret =3D PTR_ERR((void *)wp->event); + free_percpu(wp->csd); kfree(wp); return success > 0 ? success : ret; } @@ -71,6 +154,7 @@ static void ksw_watch_free(void) list_for_each_entry_safe(wp, tmp, &all_wp_list, list) { list_del(&wp->list); unregister_wide_hw_breakpoint(wp->event); + free_percpu(wp->csd); kfree(wp); } mutex_unlock(&all_wp_mutex); @@ -84,7 +168,6 @@ int ksw_watch_init(void) if (ret <=3D 0) return -EBUSY; =20 - return 0; } =20 --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) (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 4BB2C3148C4 for ; Mon, 10 Nov 2025 16:37:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792651; cv=none; b=ElGdKsaG8DGWi2Q4eYSZ9aEh2yBLt9A3pwIqZaMcmMJWPNUxtzzXpup52v9IdJWfwtZpzrpJooZLEujJWf0j9EqowU3Yp/ToI8FLysX8cqdnwinInTQsLPwypdMCokjASsdGkLCuY0ApVP18jzDAap7cxFjrJoh3c5CSOcoiijM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792651; c=relaxed/simple; bh=g1wEhkOGJQ86eJMHw7XszrU/CWcE9VZPoVm5H+XJaoM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k42+ejQw+4vl3OBI5ZEk9egUNC+Pi3CQh8sZe2YLcojYXLPIRxhh+5ihav0WtwNfzO0da9uCuC6G2r3ro3k6ZCvWdybydZJ6HtW6rrqpdp7z7CBUfLuDQTNxisImtlRw4RYub0rGLBGTq7OSM7KlSCwYSUDjy1Q8waSHg6WgLUk= 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=V/ccWJ/u; arc=none smtp.client-ip=209.85.214.174 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="V/ccWJ/u" Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-298144fb9bcso11585705ad.0 for ; Mon, 10 Nov 2025 08:37:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792642; x=1763397442; 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=pLWfzYX4fFMPPLWMDyPb3KZEfyFxyFdWrCERfTSFQ90=; b=V/ccWJ/utRobCre/Wz8e8tqbqycDisjP2q87TSxtuMthhKPQTlu72MM666VB7dt95R DWQdUBORZ7JcxOtEySRkd4jisgm9Ju6tflUABM5MjOr3ed8oO7kOqRn3fAO4WltThERS ta1qbnUkWkgpoyTSGTSInWj1vh6xSOx3dwX9s4fmAgF9cKcbf3bCKGbM5h3cXNiLQhVi 385RRvWom1Mv9QZIpo5HqR7bJYazQyMIzPa4hFWU6Sd0F5O+9rbvTGHnsxVUDGKUpaWI rZM8jnRZpLRPlgMkXWYN8sJyA2O4G1QYyn1EAjbfBIoJrYLSGj2FkgjtLf8p80VhE71p UA8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792642; x=1763397442; 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=pLWfzYX4fFMPPLWMDyPb3KZEfyFxyFdWrCERfTSFQ90=; b=lcBsv4ZEUWiHRhfY/HUHnqx00+I/AzYRT2HHyj+I0tcsJIzuBN9ys2ydCMhlkPUGK7 hs/7Nxla6lXI8Mzw71y88BZE8JYjxSeNu/ylxl92FC0CSD7sp5SHbLiWVwIU+aPPqNfA DN95wOYpdQzD/PximlsxEX5kBQy3weLu7JEvhb1Ux7WeH6k2nzEszhU6JSuij86bEkg4 eRL1tNSn+VNIejwwyx2kjXEdpQhQ0xW051pj7jW0J+h0fSxGmzVRgtKpIZSXBNiRZCeH zIJr8YRwXZ15qHCEb01/iyygyiFa1TQ02BT37lAHLjDTzAh/u+5I22ceaUIYV8EIWWcc mWFQ== X-Forwarded-Encrypted: i=1; AJvYcCWySJLW7ufATzUWgPH6ZT8joNTd290RL0f+O3gMLphVtkn/BmtuP/xdCFtflQj8ugBqsxtksvqNSj/++D8=@vger.kernel.org X-Gm-Message-State: AOJu0YzGFEr/pG8FbPJ7/M/iTIYHRuUjoETokcJWUUXVEDMmZWWSIhWT Ie5ebTblYfK71iSjyzvCAsIU503EFKLdaVHlHiVEMIFImo57cD33VK6j X-Gm-Gg: ASbGncvPZ7l8rTqFnw25++6vDMX11Th126SOTOVw5ElBMOq5Q1xKLf0sqEnqgPwAnrV JqRct2w2izceGrXnJ1SmSaon+CZSDg42WO5NGhOpdeeHMNLIyaBuRrOCNOOzwhMGUBSR/d4OA9h aF8ap3LFcdNxUPPCGUE+5oX7arhH+00RYBuR8k8M7eg7WmcgVK90udLLnGSVJI/AiehvGGU5EHD 4v9ct89GovOAOz84kCOR1uQ7L65w8LxRLt4a0ONgV99081uV1YooENJMw//moy4tuPHZxgXBtwb ZKmPXz35IcAsQ8VJdfQnCrRrixzZvbo7fHfxLxAO92ijUcUJ0QsiuLP0ZrbuV8AtGzGDBUeqvvO CdADgFMfsjgWu80yRG3IRbi2kYPzDYTXKojjk2vMLtk26pZQhGwJbod+ecVbU/MwHbHF+XsbuH8 uF45DYvlYaDmg= X-Google-Smtp-Source: AGHT+IG/cFQI/lmXu5Y2kAApFRNeOGqv88LbladeQ9d14Tfk1RPsTiArc8xlLuvBlu4N/APLeeqQog== X-Received: by 2002:a17:902:f791:b0:297:e267:c4c1 with SMTP id d9443c01a7336-297e5718125mr109122315ad.55.1762792641884; Mon, 10 Nov 2025 08:37:21 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29651c740b5sm154589925ad.70.2025.11.10.08.37.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:21 -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 09/27] mm/ksw: ignore false positives from exit trampolines Date: Tue, 11 Nov 2025 00:36:04 +0800 Message-ID: <20251110163634.3686676-10-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" Because trampolines run after the watched function returns but before the exit_handler is called, and in the original stack frame, so the trampoline code may overwrite the watched stack address. These false positives should be ignored. is_ftrace_trampoline() does not cover all trampolines, so add a local check to handle the remaining cases. Signed-off-by: Jinchao Wang --- mm/kstackwatch/watch.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index 3817a172dc25..f922b4164be5 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -2,6 +2,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt =20 #include +#include #include #include #include @@ -14,10 +15,46 @@ static DEFINE_MUTEX(all_wp_mutex); =20 static ulong holder; =20 +#define TRAMPOLINE_NAME "return_to_handler" +#define TRAMPOLINE_DEPTH 16 + +/* Resolved once, then reused */ +static unsigned long tramp_start, tramp_end; + +static void ksw_watch_resolve_trampoline(void) +{ + unsigned long sz, off; + + if (likely(tramp_start && tramp_end)) + return; + + tramp_start =3D kallsyms_lookup_name(TRAMPOLINE_NAME); + if (tramp_start && kallsyms_lookup_size_offset(tramp_start, &sz, &off)) + tramp_end =3D tramp_start + sz; +} + +static bool ksw_watch_in_trampoline(unsigned long ip) +{ + if (tramp_start && tramp_end && ip >=3D tramp_start && ip < tramp_end) + return true; + return false; +} static void ksw_watch_handler(struct perf_event *bp, struct perf_sample_data *data, struct pt_regs *regs) { + unsigned long entries[TRAMPOLINE_DEPTH]; + int i, nr =3D 0; + + nr =3D stack_trace_save_regs(regs, entries, TRAMPOLINE_DEPTH, 0); + for (i =3D 0; i < nr; i++) { + //ignore trampoline + if (is_ftrace_trampoline(entries[i])) + return; + if (ksw_watch_in_trampoline(entries[i])) + return; + } + pr_err("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D KStackWatch: Caught stack corrupti= on =3D=3D=3D=3D=3D=3D=3D\n"); pr_err("config %s\n", ksw_get_config()->user_input); dump_stack(); @@ -164,6 +201,7 @@ int ksw_watch_init(void) { int ret; =20 + ksw_watch_resolve_trampoline(); ret =3D ksw_watch_alloc(); if (ret <=3D 0) return -EBUSY; --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.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 CF7EF2857CF for ; Mon, 10 Nov 2025 16:37:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792652; cv=none; b=GGj02VwqZ8cL5RPx9ZmJxCA/JH6tssrZty+d0KKhgWN23+rIjmxYCfVy2+cxZhzDueA2Vsaj4z5jzaqXJwAYRPZHQz0OGJw6zkDo31XOgzANCmcjIowt6S9o2Ki0oy+xklm8yVguL8YU/QZQWxdqaI8FSOBrbdoMpu6GqUIaENU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792652; c=relaxed/simple; bh=HNu7Dcd5vvNfh2oB/9//HT00PJ8dhROTfKt08npi0Xo=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xb2tpc13KPztmOfK747RTtdStbXOEePslwmAraCSiyBBMa6X7JHm5JfIxJaYqyQPFmLevbQGRmMVmzmjMbCReSDGQ1k3nliDewezEWsOuDhBr/Z1fmQwFn9BQrlfao+Z4A6ZYmf+rU6B/NfdKrKApQktcoWKkcYtEOBNX+fEAPs= 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=BkxQVfEk; arc=none smtp.client-ip=209.85.214.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="BkxQVfEk" Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-298250d7769so6500145ad.0 for ; Mon, 10 Nov 2025 08:37:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792646; x=1763397446; 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=NvOciuVeFDrZI28wwbDFkVg+RFkidCZi4q1TH1/mApw=; b=BkxQVfEkEBOYEpCeUBV842In3TXrZSeDm6WTzj4dnhlJnM/V9G58u2nYPa/Knffz1U 6Sy/ftr0I4ErZmavti+TsQOhwPBv2DFSW37bpJoP53In1JSYftTGImWiVtiBsQ2ORN47 MCtLtxT1bidHRtJveRiETAElwfMJt+wiOLmV8za6EJ4sxmeGzyPTx42LXlH/oVmWpdpu 8Q8ZKqD2Cf5esbgYH3GHQRNwJviYlHsamyt0H/EcrG1ewtc0sTUyHpgeo53KexFSmBxC D2AnvxOe9DAEcB0yGOEe4o7YPl0fkn5C0HAJC9CRr+lr/zjPEjRd7v23NsEMoKX3nHrH wP0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792646; x=1763397446; 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=NvOciuVeFDrZI28wwbDFkVg+RFkidCZi4q1TH1/mApw=; b=BMCv9MbQOhsJFtqklcPM5FaeMWty00zKXj5AkcgGt4keMTAG1urdjUjE7VBf0A1Dvq P6CDk03CPJN5ozSzKhFRznFmKwqQot58YFYMqjqfg+m4WpUb0WablNDDBlJQJNzxnyTp mKD3vYvlg0Ka7UG0Oq0BSUBo6zW+IWzgNbWwhPOLNTZdSM8iPRhZscvC/h4tBaAqSz91 vDDB0UJfOFdcfbu0e6lYqMyI/WJpNty3qhTE8gQCib+rmrzPW5P84FI72M9ryhPunVcX C47Scwsy08svt3tR4wrgIRA9gdxqY2Pd6KFxJBGIKvHsvn6Xcnc0mnf0o/EM57LipDtt lzrQ== X-Forwarded-Encrypted: i=1; AJvYcCXkLBW4zgK0PpfHEhKWQIldjAgiK0L+jsfQufkjfTGnMw/VBsdYFYXXaZyDlbHeTBYjbgLd4fjc+1T0ArE=@vger.kernel.org X-Gm-Message-State: AOJu0YzqJEBB/8YsKyKMSEsrS9+L7IZiz8tME5rkwuh9DPl4SRZ0uKRJ cyIkvQEPDBggCiZn3Ji8ke68or9BKAvg0asuuJa5TDpiyOlQHmk6rsxP X-Gm-Gg: ASbGncvRJQBo1sk1k23Pz6pYZ313TcQiJBtw5wDB2vE6y6/qpuWS2PNP+z/l5FmgnOJ 3iXwlUKge04q7zAVOfNdrPFlONlQQHYgZ0OrhVmtkf+493hDyNFpyRU8ZnDANFXPD3qXXE18MYu dTJTNbEQ1KeAmVrf8zd68NUvqm09yq/0/hgi/IKD4NHPORaUwB5Egcv0OvhBrK0eJcCmetn79wA n8ZhiXUD0I68TFbdUBhjF2ikVoqs3yk2I/L7FoLIWLUBaCfigo3fXHDiRIOV2A+PHjgMFgvIEPw BcsNHLA8GOPfjk7ywIRKcFYP9shbeRrbCNtK+FYODJrHSKznFQtuu4bSgN5SKpJrS4o1pUgVfVD 5iFshvn/yAL4B2WMUk5/RISFw+tbw78kNW6O5/Sa/ru3IRjPJkkjwblnNWZ7UJp3J9XHLesV5ts uZGcxqfelGd7s= X-Google-Smtp-Source: AGHT+IHgMULgC+DBrIvta4Gt30Ay5gyo7asjfOY4+q2Dbpcc5OVehm1NQ3FiY7ELdv1UD5aLJGKDyg== X-Received: by 2002:a17:902:da4b:b0:27e:f018:d2fb with SMTP id d9443c01a7336-297e540dbf8mr108586355ad.6.1762792645676; Mon, 10 Nov 2025 08:37:25 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29650c5ce87sm150078775ad.29.2025.11.10.08.37.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:25 -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 10/27] mm/ksw: support CPU hotplug Date: Tue, 11 Nov 2025 00:36:05 +0800 Message-ID: <20251110163634.3686676-11-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" Register CPU online/offline callbacks via cpuhp_setup_state_nocalls() so stack watches are installed/removed dynamically as CPUs come online or go offline. When a new CPU comes online, register a hardware breakpoint for the holder, avoiding races with watch_on()/watch_off() that may run on another CPU. The watch address will be updated the next time watch_on() is called. Signed-off-by: Jinchao Wang --- mm/kstackwatch/watch.c | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index f922b4164be5..99184f63d7e3 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -85,6 +85,48 @@ static void ksw_watch_on_local_cpu(void *info) WARN(ret, "fail to reinstall HWBP on CPU%d ret %d", cpu, ret); } =20 +static int ksw_watch_cpu_online(unsigned int cpu) +{ + struct perf_event_attr attr; + struct ksw_watchpoint *wp; + call_single_data_t *csd; + struct perf_event *bp; + + mutex_lock(&all_wp_mutex); + list_for_each_entry(wp, &all_wp_list, list) { + attr =3D wp->attr; + attr.bp_addr =3D (u64)&holder; + bp =3D perf_event_create_kernel_counter(&attr, cpu, NULL, + ksw_watch_handler, wp); + if (IS_ERR(bp)) { + pr_warn("%s failed to create watch on CPU %d: %ld\n", + __func__, cpu, PTR_ERR(bp)); + continue; + } + + per_cpu(*wp->event, cpu) =3D bp; + csd =3D per_cpu_ptr(wp->csd, cpu); + INIT_CSD(csd, ksw_watch_on_local_cpu, wp); + } + mutex_unlock(&all_wp_mutex); + return 0; +} + +static int ksw_watch_cpu_offline(unsigned int cpu) +{ + struct ksw_watchpoint *wp; + struct perf_event *bp; + + mutex_lock(&all_wp_mutex); + list_for_each_entry(wp, &all_wp_list, list) { + bp =3D per_cpu(*wp->event, cpu); + if (bp) + unregister_hw_breakpoint(bp); + } + mutex_unlock(&all_wp_mutex); + return 0; +} + static void ksw_watch_update(struct ksw_watchpoint *wp, ulong addr, u16 le= n) { call_single_data_t *csd; @@ -206,6 +248,16 @@ int ksw_watch_init(void) if (ret <=3D 0) return -EBUSY; =20 + ret =3D cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, + "kstackwatch:online", + ksw_watch_cpu_online, + ksw_watch_cpu_offline); + if (ret < 0) { + ksw_watch_free(); + pr_err("Failed to register CPU hotplug notifier\n"); + return ret; + } + return 0; } =20 --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 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 9737F314B73 for ; Mon, 10 Nov 2025 16:37:31 +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=1762792653; cv=none; b=f0yYogIJlyMunS7ckV9HgVnwyxJPgCZo9UDUHKzimlJpiJ1dqwSNiT1xKt7wkBfIn1USw7MT/bDBvpRHUjFMAgF1nDf2u6IBJwF23DyDt30Jkmf+X/1ZuBz/rkMXYnFQJ/76sSU5PecXT7wqxJc5kdkAmJbDVxCychh8tZN5Gbk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792653; c=relaxed/simple; bh=/b/zOktEwqAjI5HJj6uEpLQ11BHnYeMx0oURGuRUTjs=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lqw9AD93iKlFSlKuqZvz2OMZZ1LkrnUltSo6SEGjih9X7PS92T7T5Rf2mOEhMhBUJ3T/Sx63zODPwnzSLkDaUQwW74tox4FyAxreore4JGljHa0C803lUyw7vLyANOwFyri8gxLHT4u//KL6bdfUdUTa9jpxpHTSOp/+jLjcNSo= 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=Y1SLqfyD; 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="Y1SLqfyD" Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-297dd95ffe4so22999665ad.3 for ; Mon, 10 Nov 2025 08:37:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792650; x=1763397450; 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=zmTHCnPEb0OVb2q4c4fHcn++Nk34I0DPY6dUiFoe4Xs=; b=Y1SLqfyDfR8ppi0EnhB5ihICf/DW8etj06fLjMr71E7TRvRl2e2hEpaly2M5h5KEYb +30JYjqd6vMFwoX6PLbUgi4uiVEBLehaoK++cGI0AF+7ptsbbCN5DIL2wRTIyfY31xj8 zRSi1+Zw5Rg/Jc9DW6cXZOe0qOjpVijNAKDgElrCirdv4w28YiVkB+H/8x2eh6nHc1Br +1MIm7SvPnJTrTFjwzp/FUERRF6HopdhqqRcrMDxJa9X0WGxd1HdcyBXo8s6YgrWMVMB a6e0K2286aeD036eGmVxme/1fILqXivvLNDXy/BoZxfaiPntVpPoeVD3MQotC1wXu9E/ t5Cg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792650; x=1763397450; 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=zmTHCnPEb0OVb2q4c4fHcn++Nk34I0DPY6dUiFoe4Xs=; b=mUTxroZDA7F4fg4+2fXBUKCfG2NIiSZpICYoZ+RgeTXorST222H/jB6iIUUOxSJCyD BPVg1bc9w4bror9u/5ahMtAqHC3yCV74wMAqf4izuiZhSrh4IcTVctkACdQ06bCLdATr EdHmrDuqlT9aD77EnIUmaFnvSlGhVJv2a1v5s/AEnG+wvA4GI3gbaazJy/YE5t2+cXBF gF2Z0x2BNywUxY0uF83bWTH9iZbea42T91vYP/+KqgYar16GAkERYTb+AhwEQD8ijBY3 5vtJIz/S2dXdVZVrBsM3atnyrKw/EspBDgNu4+o8xsPoiiK3K+mXfV+h8JmZDCqxfPgM 3S9g== X-Forwarded-Encrypted: i=1; AJvYcCVwz2cr1bVmztuzr0SAOgHwo3WdFvvWoLLKkIiy9zUIuM87RRsHRIwY3/gN10IZOImFI1mHgXHG/JIsd78=@vger.kernel.org X-Gm-Message-State: AOJu0YzMBQNiWdzx9bx7T3MTYuBWDcV+mUQ4GvDjIOs/ZwuXT/3lfA3j Tsj8Yd347fpYbXl1qPC9Sq7dcm5FuNQmlKYrPPeQSxVrK0YeOF9e8TYi X-Gm-Gg: ASbGnctoj1z0augSS1RnTVqDmTN8XglU1TKc+a0AxpGbwS5gP2sRja2U0hX7Y5tTaWM gcNwZMW/D528Fv6D6SbzNnHgA04AwQyf9nFyvvbPo1DKJWzYFyX8odpyryr2JDge0jG9md6oe0N 4uUBojkAs1bGS4/OYmutmdn9bftO9FzWoRO5Ml9Mz5rNTEiCw1qOBTqXkk5fwu5LGpZXMJ6bAgE Anl3Eqrd0iUR1M0OihLJiewGlaoWFRJq+52VRqb8F/Qw8eT27NXpJ2RlFMxDkg1gzba4X/71vlB p4uhd1fiYtwifAvM1caLu8uRwpwKpC17KDtplmad+UYB0Mb+KCH6twHh+TbtmNK9zZETTO1ZOXA T2466zTmw8DhRAvW1/tSsxSxhAd5809zpsoi1Sepfn0OQcQgqAY+/6yI+Me46M6VO0zlmQwHaUH d2MDCL39YTgQYuEHFIl4/3EQ== X-Google-Smtp-Source: AGHT+IFq15Zr3Z90Q0MMFQV+lAVCnrHviaSI2/PKIFU3mj11pJDvw+OMCcvtXWZk8em43woe/8IelQ== X-Received: by 2002:a17:903:944:b0:296:3f23:b910 with SMTP id d9443c01a7336-297e5619f92mr100007215ad.9.1762792650184; Mon, 10 Nov 2025 08:37:30 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2965096b801sm154506025ad.7.2025.11.10.08.37.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:29 -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 11/27] sched/ksw: add per-task context Date: Tue, 11 Nov 2025 00:36:06 +0800 Message-ID: <20251110163634.3686676-12-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" Introduce struct ksw_ctx to enable lockless per-task state tracking. This is required because KStackWatch operates in NMI context (via kprobe handler) where traditional locking is unsafe. Signed-off-by: Jinchao Wang --- include/linux/kstackwatch_types.h | 14 ++++++++++++++ include/linux/sched.h | 5 +++++ 2 files changed, 19 insertions(+) create mode 100644 include/linux/kstackwatch_types.h diff --git a/include/linux/kstackwatch_types.h b/include/linux/kstackwatch_= types.h new file mode 100644 index 000000000000..8c4e9b0f0c6a --- /dev/null +++ b/include/linux/kstackwatch_types.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_KSTACKWATCH_TYPES_H +#define _LINUX_KSTACKWATCH_TYPES_H +#include + +struct ksw_watchpoint; +struct ksw_ctx { + struct ksw_watchpoint *wp; + ulong sp; + u16 depth; + u16 generation; +}; + +#endif /* _LINUX_KSTACKWATCH_TYPES_H */ diff --git a/include/linux/sched.h b/include/linux/sched.h index b469878de25c..db49325428b3 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -1487,6 +1488,10 @@ struct task_struct { struct kmsan_ctx kmsan_ctx; #endif =20 +#if IS_ENABLED(CONFIG_KSTACKWATCH) + struct ksw_ctx ksw_ctx; +#endif + #if IS_ENABLED(CONFIG_KUNIT) struct kunit *kunit_test; #endif --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) (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 D8A6E3164C3 for ; Mon, 10 Nov 2025 16:37:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792658; cv=none; b=T0Rpc9E+Mul2VxMmATv4pCqG7VvFXDBQQP4JF77bjiytgnVR0swP/d+HUu1TCfqpGU4bRxM8NJ07f8yy59sBLmfRGpKVfswgNYWZSdU3CDwpHpeFLShoUFk19NUIk2N1cXGenu6oWY+5GnBXdlxJP0lnMgyTDZhDRzNoI8Dugik= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792658; c=relaxed/simple; bh=gl+G5Qso+k3I+qIwzV1n+u7DG36AFw0uDF3MMxGh5rI=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kkQ8e28REp0P43I8atfPyVmfnJrWpegC/oWCDFeL8JuD8k/vHipaaB3lafxIHa8U1Kn6hjBPgmNU31u3qvO0wxvD+RONWCMDjpKa27ZBYheJcMMLp5u0X+JGQk05ZtE+fxq6EhR+pqqE2ouZV/TSWNrkUw91BDlVep4NWizNTCY= 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=Pzy7lD+N; arc=none smtp.client-ip=209.85.210.181 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="Pzy7lD+N" Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-7af6a6f20easo2583825b3a.0 for ; Mon, 10 Nov 2025 08:37:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792655; x=1763397455; 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=j+iVGEpI5Tg4S20J7P4CCPxNAttk2LGvwxP9Ue3oAtA=; b=Pzy7lD+NquyHSGa1JX0CzL2mRM2AEQB34Tqfh9dMTEn2PagUByYT09lY0RVBdjmJZr /h4IpIzUSDe9HpDDD+wF7lY8ec/fhrerEyNWJZ2P3uo5e3VtOcB0cMXQ44Xi2lu0/thr iDillqqNE3vTv6lyo/uwRwePuCbkzadF7CUB1R/1R/QARlQMDJ+wn4ypzTI3NL+pMsm1 qLyZLDIGhmNOzkK0AY1Q7IsP0gOf8e2eg6VhQUsJsJk8UODn+YqHK/RSmv3Gwvf3prUK nGi0lbLCiSr7sr2NAL5bUvlGocJS934tDASJ/tRgQ+AzWTh7zyvvCQw46G2SL+q8UuJG OMmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792655; x=1763397455; 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=j+iVGEpI5Tg4S20J7P4CCPxNAttk2LGvwxP9Ue3oAtA=; b=P1BUcTK5qOwe83weYTMyUsoA0tdCxzKIxHW6IKlHzAN1NbBSI2c9W6FCi2kiGZ6/nS uHOMMnnuK9AYlZahDy88f1kbhxYhz+WTu9YcoMw/Mp9+nqXjU2E3+ERgzDYtl5MG9kcu jcUzBQCLxixeyIXseDTybGHYBOYOHLdUWOXGZ73S0Mr1FhLBQrbv4hVGdcDsSamn8oUz /0fdOLkJaqwRNiGfWNk+6VH53h8I9HDwe+ODYtsijb8bmynQsRtOifmPhHIpWaZ82lPf 8rv+WLZT8n2Y32soDGJmqEK/Tp5c0jBjoZ99Zv/i88ZUPzqxN5DCRVwq7avJczi8j4jY k2mA== X-Forwarded-Encrypted: i=1; AJvYcCX7P5uYl6huzstTs1QD/DtognD1q31egjKyukFArgXu2sofupElQFV/XOz5UzmDuQt2suXRVIaGOZZN3Sc=@vger.kernel.org X-Gm-Message-State: AOJu0Yy8XdhZ1X8PXwzbJlATb3A6AnoX9K5ynyojGiQx4ATeWcGnw9QB JxfVdKbasALETvFyWMC83pnBjAwy/gcpCCbNYVu6t24VzpEuTqHd8j/2 X-Gm-Gg: ASbGncuZCSK8VFCS/60q/hy02FJNPqo+Mb5khE1hxM0a3EEPJ4qfZYFOBufs0px2pmY acdysKhaFBShEDoluQZArlxXk60zitOuVzNsjL7fX1GJwovbJ+LQMFq09BP0cxGVAHiQ3rlEmmP adOoWJK57msvXF0881k003w5kQSPi9I5aIfSvLF1pTP59Yd3ZaF1rFUcpzMB9qqjsWM2C4MLpBt evuHAWNMmuC4e4Bm2qEfnT4FfJNQe79D2GULPo1W94Q0oUddxywl73EV9gDLxsmkh5LcSXVX2Us /SAXHiEN3aviTb9p/xiZIAOHjyPIQwlRRZ3wZ6537jM98vYVt3Jja174S32uZzBjkmIj1I/+jLA cyscR6nuXLusU71PTcbmqe4FGwoHxAnKy5GDwObOyuRDbYlX2oO+ri1DGjari9B0RAu7WNa3lea yJEWTaS7SDlGc= X-Google-Smtp-Source: AGHT+IGtiWcQfJK4Ygp04b6O0toULhQ9ivsVDr7hyiasnMUnwDIgBXPDlxiRxSR9lhapVrMC+QPG1w== X-Received: by 2002:a05:6a20:72a0:b0:342:378e:44af with SMTP id adf61e73a8af0-353a39633e5mr12501946637.41.1762792654931; Mon, 10 Nov 2025 08:37:34 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b0ccc59de7sm12250750b3a.65.2025.11.10.08.37.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:34 -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 12/27] mm/ksw: add entry kprobe and exit fprobe management Date: Tue, 11 Nov 2025 00:36:07 +0800 Message-ID: <20251110163634.3686676-13-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" Provide ksw_stack_init() and ksw_stack_exit() to manage entry and exit probes for the target function from ksw_get_config(). Signed-off-by: Jinchao Wang --- include/linux/kstackwatch.h | 4 ++ mm/kstackwatch/stack.c | 100 ++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index d7ea89c8c6af..afedd9823de9 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -41,6 +41,10 @@ struct ksw_config { // singleton, only modified in kernel.c const struct ksw_config *ksw_get_config(void); =20 +/* stack management */ +int ksw_stack_init(void); +void ksw_stack_exit(void); + /* watch management */ struct ksw_watchpoint { struct perf_event *__percpu *event; diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index cec594032515..3aa02f8370af 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -1 +1,101 @@ // SPDX-License-Identifier: GPL-2.0 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include + +static struct kprobe entry_probe; +static struct fprobe exit_probe; + +static int ksw_stack_prepare_watch(struct pt_regs *regs, + const struct ksw_config *config, + ulong *watch_addr, u16 *watch_len) +{ + /* implement logic will be added in following patches */ + *watch_addr =3D 0; + *watch_len =3D 0; + return 0; +} + +static void ksw_stack_entry_handler(struct kprobe *p, struct pt_regs *regs, + unsigned long flags) +{ + struct ksw_ctx *ctx =3D ¤t->ksw_ctx; + ulong watch_addr; + u16 watch_len; + int ret; + + ret =3D ksw_watch_get(&ctx->wp); + if (ret) + return; + + ret =3D ksw_stack_prepare_watch(regs, ksw_get_config(), &watch_addr, + &watch_len); + if (ret) { + ksw_watch_off(ctx->wp); + ctx->wp =3D NULL; + pr_err("failed to prepare watch target: %d\n", ret); + return; + } + + ret =3D ksw_watch_on(ctx->wp, watch_addr, watch_len); + if (ret) { + pr_err("failed to watch on depth:%d addr:0x%lx len:%u %d\n", + ksw_get_config()->depth, watch_addr, watch_len, ret); + return; + } + +} + +static void ksw_stack_exit_handler(struct fprobe *fp, unsigned long ip, + unsigned long ret_ip, + struct ftrace_regs *regs, void *data) +{ + struct ksw_ctx *ctx =3D ¤t->ksw_ctx; + + + if (ctx->wp) { + ksw_watch_off(ctx->wp); + ctx->wp =3D NULL; + ctx->sp =3D 0; + } +} + +int ksw_stack_init(void) +{ + int ret; + char *symbuf =3D NULL; + + memset(&entry_probe, 0, sizeof(entry_probe)); + entry_probe.symbol_name =3D ksw_get_config()->func_name; + entry_probe.offset =3D ksw_get_config()->func_offset; + entry_probe.post_handler =3D ksw_stack_entry_handler; + ret =3D register_kprobe(&entry_probe); + if (ret) { + pr_err("failed to register kprobe ret %d\n", ret); + return ret; + } + + memset(&exit_probe, 0, sizeof(exit_probe)); + exit_probe.exit_handler =3D ksw_stack_exit_handler; + symbuf =3D (char *)ksw_get_config()->func_name; + + ret =3D register_fprobe_syms(&exit_probe, (const char **)&symbuf, 1); + if (ret < 0) { + pr_err("failed to register fprobe ret %d\n", ret); + unregister_kprobe(&entry_probe); + return ret; + } + + return 0; +} + +void ksw_stack_exit(void) +{ + unregister_fprobe(&exit_probe); + unregister_kprobe(&entry_probe); +} --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pf1-f182.google.com (mail-pf1-f182.google.com [209.85.210.182]) (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 F237531A07F for ; Mon, 10 Nov 2025 16:37:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792661; cv=none; b=tkpWgPm+dfS5rTM7kaFqSBe7pKpSo6x4X2vJ0MvobAxL7WYxw2gjie4k0o/45u9WvO/w44Xsz9BWhrsP0f+6Hj6VwSlvZVMcuui0W5FPnQliwQXz/rIeT8Kgr3EwTrV/XeJrqYbitrXia5VoCuTIMG23wnN3u/OiZPJhGtHkrRo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792661; c=relaxed/simple; bh=xxEQ3A3gLYi43VcM4txNyA5nH16DvxbSfWTaDMVtF+U=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bR4Kj7f0QQtGMGEnhWqhFMDdCxvkXxbmHxOUOtK/oArvAcFng+/LTa+vhXaLWY3glsX+xdteU/nbgp4TtH/Z+c2H7nqbI2pcS1MKqxr1T9t3ce53RheiEbKIFbI2UZuWEJjpPoCnVzv1jBauXkiwXnTawRY5pGHzMGzdm4nqSgE= 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=KuPlXl+q; arc=none smtp.client-ip=209.85.210.182 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="KuPlXl+q" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-7ade456b6abso2702139b3a.3 for ; Mon, 10 Nov 2025 08:37:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792659; x=1763397459; 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=qY+qINpaOz0cBrftmlOPWeh/tDbDV+8hjCudsDUPv3w=; b=KuPlXl+qZQSyNBuOAXuQO4DRHm7J492d13z6AiLgqUOlc8hHq489IyM+p6zboLWI1s oj0ve+yu+Sj8ohA+TjPxqmmpS3+9eSIzzgNFqAvL72AMmOcz8GVyl1jf1tKn0HD4zOtc Z9cjjKHdMHPQoFSxGeapBSH5iEetnGTdYVk8njOc34pX5TaZ56Q9egMCaJ4IhZSbbucT MCneCHQstUg6l03SD3K50IkpgDNDbDhpRRq3MXqBfCq2fFxQn0+TXk5bca3vo6p66TpD h5CDZrrAGmnmWUUe1OQPx6eJLLkzMpA/F2kAUls2mIkKLkNeao2GGUbr2KlUDlOM8X4L Y+TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792659; x=1763397459; 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=qY+qINpaOz0cBrftmlOPWeh/tDbDV+8hjCudsDUPv3w=; b=YOCMSDLUHgUQmJP00dMgM4w2U3ThZHVIqJA3MRMTT5XIWwHvMrZUReXYGWTBoD5IJs +ZgX4s+pZon9Rdt1+MfDqz1aM3663k8jjAKNb1EzqIs878r0je5CRAVBd46L/P6C+aj8 n1mSeAMFOJHmV0dGDBVmc6xAw11ogUPRebF3VKPGQBJadHxKRZ/laEkQpgye4x/mrvCi DOy1hSmb+S0iXNngRw4dmxAv7mRRfENAQqMjeczWT8kHvmGGbQVy0E5deOCX/ZdFz4cx N4PrvnxNChizPKj2VMpsH39xy8tZSbuV+SQkaV3yfNmABMTTSuVf8P+lOOVJOygsTDXT t5AQ== X-Forwarded-Encrypted: i=1; AJvYcCWlQEdICTdypTLHM0e+rOv1mx3VjBTLEMijyLAE3H6JeuCrlJcysgroOYEXUJHs0U2xDMoXrFAosxzmFOY=@vger.kernel.org X-Gm-Message-State: AOJu0YyozVZpv97SEBSGvM+vijZfqgo3UOnnp5dCBeGUFW/JRdZQp6im huALYRQuKA8TUok/k+osxltVgqHyFcBYHmt+FkDs8aGWv9e+WHAdbDcS X-Gm-Gg: ASbGncuetZNkLVW5A4BOC5sRbg2SGMrd1WHRqKk/CKQl27Wke5zbc044wN+++zyVXxK wH16vwvY+5PCI9ygwuDiATHAI3FOOak+406Gl3ssKlFgeYFMiQSGnuHO76v0A3maniJ/wYctg7g 0MDUIdjnqMh5/vf4YDM8UFv/NC2NHiXR75wPdQ+KBzMDzL3gIJ/lKdP1OI89Aw/rG11zNV6mNSn TlYDkquJCwTqzYYPWneP3d9MSPmBwbFT/jdLcx897Jy7Yp9/KBUZzDqm8AC453x4cvGhoCqb8pH k4LkoYkRVza7I2WWxap3MPqJyxLoID0ygsYuXfnxFTqrt+SUGq1yneKeOoIlqBq3X5K2ky2sUTG 4lvgRYWISjaqMI1OgyyQ6EJxfyBIy5l3/k/vjM1Y2fHsHiey5EdJ7TZjXREcATybOvVDphPRuI0 aCELEyWbCRApm6cefpu3FMCg== X-Google-Smtp-Source: AGHT+IH2fkExOiw2xx23499QoJR+0QVGEltJ2Kq069nEQ8OMBF3QJ74WPA0vkc5LrwSnkN3SgQQOIA== X-Received: by 2002:a05:6a21:6d97:b0:334:8d0b:6640 with SMTP id adf61e73a8af0-353a13aaf66mr12767757637.8.1762792659125; Mon, 10 Nov 2025 08:37:39 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-ba8f8e750c1sm13193534a12.1.2025.11.10.08.37.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:38 -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 13/27] mm/ksw: add per-task ctx tracking Date: Tue, 11 Nov 2025 00:36:08 +0800 Message-ID: <20251110163634.3686676-14-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" 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 3aa02f8370af..96014eb4cb12 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -11,6 +11,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) @@ -25,10 +72,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; @@ -49,6 +108,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, @@ -57,6 +117,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); @@ -91,11 +153,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 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pf1-f170.google.com (mail-pf1-f170.google.com [209.85.210.170]) (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 6669A31A81B for ; Mon, 10 Nov 2025 16:37:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792666; cv=none; b=drFd8o/Nc8t+WH7Hy/ljtBEAcmpxpLYPkRTd4t5fsEwHnd/hcJSZp3ZIeI2QRBrUhEyQYVu7Hnhs9jxMOpi27Nd0eWDVrf+Myv5MKsgqK86OE6p6vewv4CWABWENv96UxMyCu46L77Fvc9+y73syAraaBOyGnyl3pooPqUSGIs8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792666; c=relaxed/simple; bh=ugzTOOBc4YO6IdubWCkDyybIdRjQGKqV6BTx+jx7Hdw=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZtXQVICRkykSMAeqRvrsmAK0rIKWr+TTwtJQ9dwUBs07/zLI8R+fKynxXtUbYc59Zz+OvQ84ge5yAgpL9ruaqQLEi/qE93qYVg9iRXH76wJ8YVud/zZfz2QoXaCyF6xSs1aRLM/ymSwp5V9RGi957vGyTzUTUSxq65Wk82MA5pI= 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=gJ82IX1t; arc=none smtp.client-ip=209.85.210.170 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="gJ82IX1t" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-7a9fb6fccabso2599048b3a.0 for ; Mon, 10 Nov 2025 08:37:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792664; x=1763397464; 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=rUIy2ZawuG8l7hvlTJp6NnNoOG+7tynb2rchqUDRw1k=; b=gJ82IX1tjcB06wfqQsfkyNftKgQlSgYhYuSO08Ub+ZaJ8BYx6k0vxZ1XYkn/OWSuKE QAvA2HFaPCC6T6DzdlndgKs5GmjfJ3f958JFg1Ak2v7ATYfBjFDj1N0ocBv2qTAxz23w uX+Rwc8icuVN6BFkgDgX2XWDEncAVX1FSYZka86B8YrxPdrFYHiZ80T5mJwU/uHkzHDK EuVszyNk/Us3LaJUNIze/NLneLhwmjmVVONjKoAWxmPq23LUIJ1k4LL/DW+s7P007U0s jjpydf8NXCVQR2ZlVphNlhCbcMctGtpVzQ0XlL+Lk8uz6++XZM5LVbOQfD7tJpNsORZn aXZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792664; x=1763397464; 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=rUIy2ZawuG8l7hvlTJp6NnNoOG+7tynb2rchqUDRw1k=; b=malH0nMOUMBZgnvYbeF7iZU6EZP1bIGr/YSSPHx9gQCJoZorEDCZZUorFL0SJv5HrR TozeiTD2dQe7d8UN9IfLNaPW/bnmmfocKW2Rb1VaF8fqx9Af9qfa8URTQg8WiizBfuJV eUprXowu89iXCQSlozEdIIbi3WiBt5AR/1MI6JDEdYyELIRhy7x2XvkHxz5EWNfEKGNl B5tZxdVIhF7M8nmo46kJU0pEaU9XGoE6/STr5wpyMQJ1xk7sXnGD7yUpM/vGKX/kMK4m 2iV1hxqixk8bqMtcB9uZTBZIAmsU6/v0yjR7shimr7tUxUzE4XlnNi57BS/Rc39N4b0K pdhg== X-Forwarded-Encrypted: i=1; AJvYcCVWzDpgb5PUk4ZeQXV2KvAxJ2k7Z02MZU13sZsWP82oIYerd94i0UmH5Esaezp6REVqSGx9MLlO2fk/KOw=@vger.kernel.org X-Gm-Message-State: AOJu0YxD/zDgfFe6WWPt4+FCxgaAIFONm4efdxWd3U/9kvcdMMpPXhxT s88YunIVqhmUIMXQ8x7Q037CklEMXgOr++CTyLZ4+cTusYKjKkuzpdos X-Gm-Gg: ASbGnctbjVPqPisRELP/2Q9YsTqZTkkNGn43QBfEspDNzA5bYMB17uac21Xe7tPpgxC 3tWm6U+OugdZoIdMDRd3URashqeScpJjgEQpYNwzpHOrcPCtiY3ktq8HcQzNfm6lmPjhAQvx7pj 7QprrEozMsFZhgxPns2hBXBmojkIJ4QH/YXfn+By2g/3rtXojqRcMQVLvZpqI9N8wXY9Iqr/HnG L0QlQ8HEWG/zLNuKO47JptK2yoq0OXwfWKpwwrUSTVrbDiO2RUECXLwF9P5kRqHZD2kqhbOD5O+ T1HqRu5QO+6I0FXghQvaWdhNYZPQha6wHYdhfRY6JJgH74J+TTDSNyALvEVgSf4YQGaK+AKRIoN zyWA0LwfGVT2ry6aL+SipvaiLQE65PkdNPqysN/UmKrKewxTzfYbId/g89bWCad12LE+vi3TGcU rHNZDz/LGDvF0= X-Google-Smtp-Source: AGHT+IHmtKjxaoNsV7D/kMioLqLEx1DalHNLT8KZpVzjD66EjiPrOQ3mNXTEP3A6f7b/Rdv9Vcu6sQ== X-Received: by 2002:a05:6a00:b81:b0:7a9:e786:bdaf with SMTP id d2e1a72fcca58-7b225c9b43amr11411331b3a.14.1762792663661; Mon, 10 Nov 2025 08:37:43 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b0c9635007sm12550190b3a.2.2025.11.10.08.37.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:43 -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 14/27] mm/ksw: resolve stack watch addr and len Date: Tue, 11 Nov 2025 00:36:09 +0800 Message-ID: <20251110163634.3686676-15-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 helpers to find the stack canary or a local variable addr and len for the probed function based on ksw_get_config(). For canary search, limits search to a fixed number of steps to avoid scanning the entire stack. Validates that the computed address and length are within the kernel stack. Signed-off-by: Jinchao Wang --- mm/kstackwatch/stack.c | 80 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 3 deletions(-) diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index 96014eb4cb12..60371b292915 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -8,6 +8,7 @@ #include #include =20 +#define MAX_CANARY_SEARCH_STEPS 128 static struct kprobe entry_probe; static struct fprobe exit_probe; =20 @@ -58,13 +59,86 @@ static bool ksw_stack_check_ctx(bool entry) return false; } =20 +static unsigned long ksw_find_stack_canary_addr(struct pt_regs *regs) +{ + unsigned long *stack_ptr, *stack_end, *stack_base; + unsigned long expected_canary; + unsigned int i; + + stack_ptr =3D (unsigned long *)kernel_stack_pointer(regs); + + stack_base =3D (unsigned long *)(current->stack); + + // TODO: limit it to the current frame + stack_end =3D (unsigned long *)((char *)current->stack + THREAD_SIZE); + + expected_canary =3D current->stack_canary; + + if (stack_ptr < stack_base || stack_ptr >=3D stack_end) { + pr_err("Stack pointer 0x%lx out of bounds [0x%lx, 0x%lx)\n", + (unsigned long)stack_ptr, (unsigned long)stack_base, + (unsigned long)stack_end); + return 0; + } + + for (i =3D 0; i < MAX_CANARY_SEARCH_STEPS; i++) { + if (&stack_ptr[i] >=3D stack_end) + break; + + if (stack_ptr[i] =3D=3D expected_canary) { + pr_debug("canary found i:%d 0x%lx\n", i, + (unsigned long)&stack_ptr[i]); + return (unsigned long)&stack_ptr[i]; + } + } + + pr_debug("canary not found in first %d steps\n", + MAX_CANARY_SEARCH_STEPS); + return 0; +} + +static int ksw_stack_validate_addr(unsigned long addr, size_t size) +{ + unsigned long stack_start, stack_end; + + if (!addr || !size) + return -EINVAL; + + stack_start =3D (unsigned long)current->stack; + stack_end =3D stack_start + THREAD_SIZE; + + if (addr < stack_start || (addr + size) > stack_end) + return -ERANGE; + + return 0; +} + static int ksw_stack_prepare_watch(struct pt_regs *regs, const struct ksw_config *config, ulong *watch_addr, u16 *watch_len) { - /* implement logic will be added in following patches */ - *watch_addr =3D 0; - *watch_len =3D 0; + ulong addr; + u16 len; + + if (ksw_get_config()->auto_canary) { + addr =3D ksw_find_stack_canary_addr(regs); + if (!addr) + return -EINVAL; + len =3D sizeof(ulong); + } else { + addr =3D kernel_stack_pointer(regs) + ksw_get_config()->sp_offset; + len =3D ksw_get_config()->watch_len; + if (!len) + len =3D sizeof(ulong); + } + + if (ksw_stack_validate_addr(addr, len)) { + pr_err("invalid stack addr:0x%lx len :%u\n", addr, len); + return -EINVAL; + } + + *watch_addr =3D addr; + *watch_len =3D len; return 0; } =20 --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 80B3431B13F for ; Mon, 10 Nov 2025 16:37:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792671; cv=none; b=ZEvTMJvcVricb6cEcrHkQhZuVgha7hK7uLuJkj4jpSRgP7lbNJIpFRdvL+736bq7LVodROZpMNEByp+8OHQ8MuELsZNsAKrxu7NkG/2d3IShRTNkwEv6l7eYDK9QO9JunTqktCCN4rYegcv5Fcn0mHXdiBc/5wmGaPZQNAFXvFM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792671; c=relaxed/simple; bh=e4vkEHA+9PTiYMh5yLQWM+MeyKZuslfqcgr629D3+7E=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RfKuYofmMcRoqBtbjBC4boID2hoD5yZJ54J7prrT4TWZru9/xpw1aEE/a0YaqiGcxtE/oV1EnrZRtlgTzATScl9OJMM3W8Ds6EhaKeMsFTYJqlilUaCSXn7BgrwwyaH1Zu4e26olb319EX0AqoDiBZD8Nm4gpc5qOJsa3e5s704= 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=OQBR1353; arc=none smtp.client-ip=209.85.214.182 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="OQBR1353" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-29516a36affso30297095ad.3 for ; Mon, 10 Nov 2025 08:37:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792668; x=1763397468; 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=mbgbln0r/0DljbZ66YwwMMl7cTrgB9t9J30U0WpMJ+k=; b=OQBR135369VT4PBc2hT+xdd5VzRxTuB3C1HjCn9+F1vDopvc539ePQpm6Ee2GA+EqD lfx6/UfDSuphvjq+xMy7mV4jp4iNppQvvP7+ysUMaqPz5bjGbFvRqP6+zOPenEBsfyaQ oWpmrc/pgw2jbDKu5FqjNAYvNNG1Otf2niawwM25tVE8PeEZJ0Wdor6Yh8ialpn4SWYo 3qpm8WtvKl6LiY70DZ6jIrbPPlgPa2eBOE+vWhG1lXysEREIC8SC2A69IcKRasMH7J0/ i6Im5iL2Kh828eRW2hS74RRA3Xq4J+oC9wBBEFWVQwXvxzuc57ta5ISEyQwq0b/KSOWg LTJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792668; x=1763397468; 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=mbgbln0r/0DljbZ66YwwMMl7cTrgB9t9J30U0WpMJ+k=; b=vZNHusPTiGzUZAE0EBEe9epmsaLSlX/BOtBHMgXGQueQ5WQ9YuO3slQUPd+Jvv1TAs qgZrf4EnHVPpX/2+Nx+ZUvxSrest/p2QBgvnVuhC4OMikKNB6umEJ2fsRgNRDYdPnp8v AW/sYfy3pbBQweYDC+CkmHaGxouLM5vzLZqlBOHuC3zy8aQAq7z+puxCjELephIbKBCQ cp8pA2rfVtUgauQ25r1Gf2XqZB7sVIWc5v6MYUU+e95INLOHamNzVuFy5Q6QoGw1reR6 W3W29+13y7xxsKMn40QXBrR8556NeHbrsaxjziUrroqV7cS8g14fZ5s+tx5N5TZbNuhG DC0g== X-Forwarded-Encrypted: i=1; AJvYcCUslD5mhONHNAc2d9ltDPcCRoTW4sHK9/ReUvTz9yHyx4pyyDLTmd6GTmZQ8hYd9MUDRt2mjapmWr5wa5o=@vger.kernel.org X-Gm-Message-State: AOJu0YwXI7TVvUeNV773dRRGmC4kqOlrw80ndkFGIdV9EWdX9lrkqjiP 7C4jPqtcti0jIj9p8tfSxK84+pIpfp4xqk3K41zLOmi7/vhwUQ7lwRWV X-Gm-Gg: ASbGncu8Z4eZ9daxTHYjfWXHLmz+xDX3zCMl159YPD72WZAdF9EUoKDigbeys3csk10 fFxA+WKhMAIzuL6kPVapDbW1Mh/YxOeRmwQsR0ceClSAlHE7G+WM8USCta/DFDVDnv0V4dVg7bw iW/JcfTp94zMDACQuT8X4PiCc3gZHk+RJvyBpxdRVh7QGig9lpGN/rgXVZ3yXBDPR3zaIP/mGH3 He48Z3vw0fRkCzda13CMSga3A4hs20kpjIAXEzm4/5j3R0WQHJAtCtJM3kKvcBR7KjhJBQfCpe5 Je9GmDrN5VJ+qcIo18GG09NekOyRGq0+BxkGOqaNAzZXyjMOYAsV4Ggvc31SnqOf89+APqpA44L ApEoQo704c+YZBaalrrMvnWtJ2P0e4LkhhVR3Z5PERvTANcp7/t1g6Ih7Hcp9KaeaJA6i4rStkj 2cjpl7pwN0YW4= X-Google-Smtp-Source: AGHT+IF9D2s5oh2IOSIoqnbGH6eyisWVL5PhHGPCW6i2QHK95VMIMn4wu9hvntS0Aig4CKNxr1HiEw== X-Received: by 2002:a17:902:ebc1:b0:294:cc8d:c0c2 with SMTP id d9443c01a7336-297e5663a67mr107303455ad.27.1762792667923; Mon, 10 Nov 2025 08:37:47 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29651c92cddsm154610615ad.83.2025.11.10.08.37.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:47 -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 15/27] mm/ksw: limit canary search to current stack frame Date: Tue, 11 Nov 2025 00:36:10 +0800 Message-ID: <20251110163634.3686676-16-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" Use the compiler-provided frame pointer when CONFIG_FRAME_POINTER is enabled to restrict the stack canary search range to the current function frame. This prevents scanning beyond valid stack bounds and improves reliability across architectures. Also add explicit handling for missing CONFIG_STACKPROTECTOR and make the failure message more visible. Signed-off-by: Jinchao Wang --- mm/kstackwatch/stack.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index 60371b292915..3455d1e70db9 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -64,15 +64,32 @@ static unsigned long ksw_find_stack_canary_addr(struct = pt_regs *regs) unsigned long *stack_ptr, *stack_end, *stack_base; unsigned long expected_canary; unsigned int i; +#ifdef CONFIG_FRAME_POINTER + unsigned long *fp =3D NULL; +#endif =20 stack_ptr =3D (unsigned long *)kernel_stack_pointer(regs); - stack_base =3D (unsigned long *)(current->stack); =20 - // TODO: limit it to the current frame stack_end =3D (unsigned long *)((char *)current->stack + THREAD_SIZE); +#ifdef CONFIG_FRAME_POINTER + /* + * Use the compiler-provided frame pointer. + * Limit the search to the current frame + * Works on any arch that keeps FP when CONFIG_FRAME_POINTER=3Dy. + */ + fp =3D __builtin_frame_address(0); =20 + if (fp > stack_ptr && fp < stack_end) + stack_end =3D fp; +#endif + +#ifdef CONFIG_STACKPROTECTOR expected_canary =3D current->stack_canary; +#else + pr_err("no canary without CONFIG_STACKPROTECTOR\n"); + return 0; +#endif =20 if (stack_ptr < stack_base || stack_ptr >=3D stack_end) { pr_err("Stack pointer 0x%lx out of bounds [0x%lx, 0x%lx)\n", @@ -85,15 +102,11 @@ static unsigned long ksw_find_stack_canary_addr(struct= pt_regs *regs) if (&stack_ptr[i] >=3D stack_end) break; =20 - if (stack_ptr[i] =3D=3D expected_canary) { - pr_debug("canary found i:%d 0x%lx\n", i, - (unsigned long)&stack_ptr[i]); + if (stack_ptr[i] =3D=3D expected_canary) return (unsigned long)&stack_ptr[i]; - } } =20 - pr_debug("canary not found in first %d steps\n", - MAX_CANARY_SEARCH_STEPS); + pr_err("canary not found in first %d steps\n", MAX_CANARY_SEARCH_STEPS); return 0; } =20 --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (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 5B80031B839 for ; Mon, 10 Nov 2025 16:37:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792675; cv=none; b=WkGsrIxkoobbdDmiMHOs6Vfci/PCbOzpMoZVO1oX01B8K4qdnObgUV6k7pEo4loCvljGMKct+6mjMas5GPSCh5+IHxl8MZh+6iaAZFndMGRpA+0jn2MoWjLTXOqBGoIwFJWA/FOTU8ynBMmUHDgtc+Z1PnWbO8qLn+oSmnTpWKk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792675; c=relaxed/simple; bh=wQ1McUxBvOQybWZtkihRRIOMW2MyuqP74EGicbjl+yY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=t/ybs9snnIJN3kVh1XbR4nPeXsjK9JiQC4mfAgwz9XMEE4ezzg9RAcmC/AQfg0wi/BCRHldU5gGGej2elHKP9JlPuNEdcLU3V/yuQmmImTBW+H4lE8rajVq8Hu/HYEpH6dLxpTOFaO5Gabxyow00N0ChRYghr3fOhgH9GJqf7r4= 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=akYXr6hT; arc=none smtp.client-ip=209.85.216.42 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="akYXr6hT" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-3436a97f092so2885552a91.3 for ; Mon, 10 Nov 2025 08:37:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792673; x=1763397473; 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=Z475VFvVTEIt4JwZ3YGODDVB/J4Mi7oiMRjRIDfUacs=; b=akYXr6hTTmKYO6bD/KyxeV16JXOYZWQ8DwCYDA0t0jVp3SHPE27q3tvfZErkdMW+lP 2RSaYGsAM7BXt3NSMeS5cAQRjuQpwMA1lYXTG8f3TGbIxwGNhq9vEREUVVFEbCM5/x2m jE+91qFsxS+7OvZ2hTW0lIbLdO1VAcYE0ZVtpQTqaHlTLykJpl/uu5XWg/0LVEeSXk2+ X7l3snS2OX6ralC067tjX44SbtnYJ77M2S5ykFuDV4JPGuxZ7YXw+WH+AbMxw6YSuoN0 O5NWHDIWyPS2lY5FBZ+Wy1RAEhJBG69v0RXKnAHaaSLBwzfwkMXLuJFtazFI5aE/WXfR wuiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792673; x=1763397473; 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=Z475VFvVTEIt4JwZ3YGODDVB/J4Mi7oiMRjRIDfUacs=; b=uILUCt6SUXBtGl3ujFtGQA8ivK2sEJNLSuwOJINcD1mZHb44qimxspe8huzG3/eLWL Swe4sQM4wzoNazAm5A1fpaxSLFCgPELRavoYexIlSlSMa6O6yt5ld+356nyBNnLuQqDz DBOjywmfejaobh1wN7AeEeLcqaCeIMTKqku0lIuVZ3RnanrZ6qHuOGmtWEP1bu9ngXF3 vOlYLVHqy7qYZVyJ/zE+V8Obj3ZMTc0tKRt2IwH0M0pX/Xb5xbwOTpReiVESYfn46QFr HiaB3Zn8L01tmkQS44ZL+EC2a8mpq3VilXKRnL1xfaIZHjERGV1RyFCKX/Sn+fd7y5mr 8LhQ== X-Forwarded-Encrypted: i=1; AJvYcCVJR91Ob+O7dWfaceyalSyBII5axPl1ATO2CaKDh4P0fuefnkK2Z2/G7Ndo8nYIYYMWkPJ2MqCg11erMMA=@vger.kernel.org X-Gm-Message-State: AOJu0YwhYrTZYn1wEohDwRZCzZWUaijshn9Qprn1/AVG+eq0OP4w+wQI cVM80pcM2CCsBOGgfFNW/8Tx7AeZyCrULkp7hl8hfYjJFFKzHiHQpukF X-Gm-Gg: ASbGnct93oFiT3D3zQ585v0Txwpc3LHA3XhkspQ7MNhkCBLP+XJf6adQRh8YuuhyDM4 vSaV3V3fC+OXG2VUXHWbv20q44pARJF7M8PAjF2M5w8/jl/kyXX9vFUdo1e3vO+NraPC2GOXaVP 90PGiArEKi82XyTLnECLf5RLM+zBtc08U4c5neAi7qD4t6Oq2eSNF2uNSM5QRj+TC7NLIO6xKMm aYcWU6+v4c5gdwc8UUL+yCvUqM584GXV/7RhTwCFHpsdmpTHmoZB3b/ps4ScCQ35kXLojMxEqGt m5UJJ8xU8zym4HjmcNU4A0bePxCb4uGubIkmNbrRl1+urTdZSmxNkw6duAPb8Kuhhxrl2a0jsv3 b0lRlOSeal1nXzNSBGlr8Tfh5DUNfoQ5pQ9ibxCYH1sV5WQoox+WA8IvsbDHpWMMjDRz35GPh4w uQR2v2vIkZeio= X-Google-Smtp-Source: AGHT+IFMkp6mzspl/QeVRsUZzdmIZ1SaB55q2G1mEh95QyGe2qx8u+PVoYkCuCMgJ33eu6H6IlXxoQ== X-Received: by 2002:a17:90b:2d0d:b0:341:d326:7354 with SMTP id 98e67ed59e1d1-3436cbda0e8mr11097915a91.37.1762792672604; Mon, 10 Nov 2025 08:37:52 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-341a68ad143sm18023453a91.3.2025.11.10.08.37.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:51 -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 16/27] mm/ksw: manage probe and HWBP lifecycle via procfs Date: Tue, 11 Nov 2025 00:36:11 +0800 Message-ID: <20251110163634.3686676-17-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" Allow dynamic enabling/disabling of KStackWatch through user input of proc. With this patch, the entire system becomes functional. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kernel.c | 60 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 87fef139f494..a0e676e60692 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -14,6 +14,43 @@ static struct ksw_config *ksw_config; static struct dentry *dbgfs_config; static struct dentry *dbgfs_dir; =20 +static bool watching_active; + +static int ksw_start_watching(void) +{ + int ret; + + /* + * Watch init will preallocate the HWBP, + * so it must happen before stack init + */ + ret =3D ksw_watch_init(); + if (ret) { + pr_err("ksw_watch_init ret: %d\n", ret); + return ret; + } + + ret =3D ksw_stack_init(); + if (ret) { + pr_err("ksw_stack_init ret: %d\n", ret); + ksw_watch_exit(); + return ret; + } + watching_active =3D true; + + pr_info("start watching: %s\n", ksw_config->user_input); + return 0; +} + +static void ksw_stop_watching(void) +{ + ksw_stack_exit(); + ksw_watch_exit(); + watching_active =3D false; + + pr_info("stop watching: %s\n", ksw_config->user_input); +} + struct param_map { const char *name; /* long name */ const char *short_name; /* short name (2 letters) */ @@ -119,8 +156,18 @@ static int ksw_parse_config(char *buf, struct ksw_conf= ig *config) static ssize_t ksw_dbgfs_read(struct file *file, char __user *buf, size_t = count, loff_t *ppos) { - return simple_read_from_buffer(buf, count, ppos, ksw_config->user_input, - ksw_config->user_input ? strlen(ksw_config->user_input) : 0); + const char *out; + size_t len; + + if (watching_active && ksw_config->user_input) { + out =3D ksw_config->user_input; + len =3D strlen(out); + } else { + out =3D "not watching\n"; + len =3D strlen(out); + } + + return simple_read_from_buffer(buf, count, ppos, out, len); } =20 static ssize_t ksw_dbgfs_write(struct file *file, const char __user *buffe= r, @@ -135,6 +182,9 @@ static ssize_t ksw_dbgfs_write(struct file *file, const= char __user *buffer, if (copy_from_user(input, buffer, count)) return -EFAULT; =20 + if (watching_active) + ksw_stop_watching(); + input[count] =3D '\0'; strim(input); =20 @@ -149,6 +199,12 @@ static ssize_t ksw_dbgfs_write(struct file *file, cons= t char __user *buffer, return ret; } =20 + ret =3D ksw_start_watching(); + if (ret) { + pr_err("Failed to start watching with %d\n", ret); + return ret; + } + return count; } =20 --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 B122F31CA75 for ; Mon, 10 Nov 2025 16:37:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792680; cv=none; b=EKZm5LgWci54uerJldL1o0zqDNRZObEx5Px7iSW7Y/MSs1E5xm2SUM0ScML20rQcrQQY3Pw4uZ/LGDPjtYoMEG9GF46zvBgDImxdECF/3lWvVDIUv7mZFIVxUuNroRRJjUTMICKHAp6Ly1S074Last7IiHTlwMxYBBorAyDgZss= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792680; c=relaxed/simple; bh=u5rYMDHG15I/cQVc25duuIIeZNrf7utvrw96eXmgPSk=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kMfBzw0VDe3hC/QgMRd9NjNNI3Al2E7mSdtC2hztVR6C7V5S23yTxOze4/hv5SgorpdlEJCZ2BuoQSnLfCd3uF2eKx53R9LzlQ3mGHTEmn+JmDwzWDoiZRW81xYTzZ6XyyVNNT84r8CJojlklzbraJbMvOjvrGT2gRN+nee9TzQ= 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=jgN9V9tb; arc=none smtp.client-ip=209.85.214.177 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="jgN9V9tb" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-29808a9a96aso13788535ad.1 for ; Mon, 10 Nov 2025 08:37:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792678; x=1763397478; 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=IoYSCz9IZK+WTJVuSJ5kdqWuXYPyBcWSfVrsNGbuyIk=; b=jgN9V9tb1u2tX7EcLEJaOx8wVnAL8EdgrCnutPFBySuBe8TpcwF87l5bJQ05kv654B cRBwwUb7OcQ7aIzhsfrlsliFBLo+mwAO5bOIlAw7pABQSI22M5im1O7UsvfKKD3WPfIJ 59xFuyiMRLSjUHiX/zWvMP2hKOzl+W18Lnibp77oIjYHMuf3ocBNME5HXl/QBIR8TG38 z5fbBv+NGAXzYegr+TiiKlsjv9X0bCCDte2sTBJoL0gQAW8uoroPTAyGJq+iDottAPrO tUBL6mSsNOp2nUNd7s6m6GAnbNxJn8IRlo3LS6O248uqL8YUtliE1kDCunYS4RDkHcgr 1YwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792678; x=1763397478; 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=IoYSCz9IZK+WTJVuSJ5kdqWuXYPyBcWSfVrsNGbuyIk=; b=ETEQoCmLPZroAR0ejOiN5xbAryaSCe5fKqjUMiJWbEgsmy1pv/nn/iAwlIsUQjCpaH TRs9KzDMWcB16SDKfHHJ+UEr2uaTqQVMv7vcvF7AMaY+nl22BTN5+PUMqdpFrzqtF6sj YwBxLTJ6gticAi1Ay33IA6z0vSU0jmzRcwu0IU8H04z9ED7vGMTmfNxJTGHlzMGKXgp8 zAuk0iFsFBb0vf1bIuu0m14ROaGUYjnjqcHocsHX2gZYue4r4g6Ej76o811ZFVtqZ3BS +DdfEQ1pZ7szxFXFukj8kTlqRCnWTAVofs9yzNH0EUHm46YCYUf+EXnbzaNN3GKNEtwb kjyg== X-Forwarded-Encrypted: i=1; AJvYcCVnnlrSZzeiItVhjzjE6PKsZJNtNjLIIp0Dz+zBgYrDJcJ63/zcVuJ6QMxlBh2PbLvaHR7xRnWfQgtaVFU=@vger.kernel.org X-Gm-Message-State: AOJu0YxJhi64dsFQFgnGV8tnlEIjny/3gqE/y3StzrnAf8o1AmzQPZUo 9TgzvgXjE6/bTpfB1ag0E8q2Zivq+JlsG+eZuGGS68WnqulQW1b1biYw X-Gm-Gg: ASbGncvcnJJhVlu0sPo7v4YHQVANKdnHSciQWfhGeMjmTVsKYzoDabSuOwFwsBs3Zbn z4YFaaSq82vRRXupnV4xsTi841lF4IY9nqElWddLotYyTH9p1IZmmR1dU47iW/m+9xc3niARI7b jXj88SdGt4NamRWjUTeKrZeNSIQgUbbIVjYaumqYjmUF9izI5ZTWgKmPsc1LPNwIZvbZEcUQTgY S43PntCy1a4aHFVW8LJa4nzmP/P+DnFiv52HegGP0h7+beM833ML/QT/UOzBd4vFnubzSfc3t3d avO6vGvDay9LPyHh9MO2OWzoL3ivKv4jHVpchSHU3/KzqyGjivfxmlTlbPnDfs3ydYRWZDIuSml jcymhlGoRc5276xlDGU/ptHDYvqyZ0PVs1XLX4dv25gdRuE9RkS4DEWUzk/211VZe/MxTyaXYRl CK3Q36Yeh9WH4= X-Google-Smtp-Source: AGHT+IGNkULY4KbMreL+r3C6zKYa6LAZ6HejhCC3QQjkk4WrB7CidoZ7O068kW/SdXYru/ivG/AiRg== X-Received: by 2002:a17:902:e80e:b0:271:479d:3de2 with SMTP id d9443c01a7336-297e5624c9fmr126791755ad.13.1762792677804; Mon, 10 Nov 2025 08:37:57 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29651ccec88sm150510135ad.107.2025.11.10.08.37.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:37:57 -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 17/27] mm/ksw: add KSTACKWATCH_PROFILING to measure probe cost Date: Tue, 11 Nov 2025 00:36:12 +0800 Message-ID: <20251110163634.3686676-18-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" CONFIG_KSTACKWATCH_PROFILING enables runtime measurement of KStackWatch probe latencies. When profiling is enabled, KStackWatch collects entry/exit latencies in its probe callbacks. When KStackWatch is disabled by clearing its config file, the previously collected statistics are printed. Signed-off-by: Jinchao Wang --- mm/kstackwatch/Kconfig | 10 +++ mm/kstackwatch/stack.c | 185 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 183 insertions(+), 12 deletions(-) diff --git a/mm/kstackwatch/Kconfig b/mm/kstackwatch/Kconfig index 496caf264f35..3c9385a15c33 100644 --- a/mm/kstackwatch/Kconfig +++ b/mm/kstackwatch/Kconfig @@ -12,3 +12,13 @@ config KSTACKWATCH introduce minor overhead during runtime monitoring. =20 If unsure, say N. + +config KSTACKWATCH_PROFILING + bool "KStackWatch profiling" + depends on KSTACKWATCH + help + Measure probe latency and overhead in KStackWatch. It records + entry/exit probe times (ns and cycles) and shows statistics when + stopping. Useful for performance tuning, not for production use. + + If unsure, say N. diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index 3455d1e70db9..72ae2d3adeec 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -6,7 +6,10 @@ #include #include #include +#include +#include #include +#include =20 #define MAX_CANARY_SEARCH_STEPS 128 static struct kprobe entry_probe; @@ -15,6 +18,120 @@ static struct fprobe exit_probe; static bool probe_enable; static u16 probe_generation; =20 +#ifdef CONFIG_KSTACKWATCH_PROFILING +struct measure_data { + u64 total_entry_with_watch_ns; + u64 total_entry_with_watch_cycles; + u64 total_entry_without_watch_ns; + u64 total_entry_without_watch_cycles; + u64 total_exit_with_watch_ns; + u64 total_exit_with_watch_cycles; + u64 total_exit_without_watch_ns; + u64 total_exit_without_watch_cycles; + u64 entry_with_watch_count; + u64 entry_without_watch_count; + u64 exit_with_watch_count; + u64 exit_without_watch_count; +}; + +static DEFINE_PER_CPU(struct measure_data, measure_stats); + +struct measure_ctx { + u64 ns_start; + u64 cycles_start; +}; + +static __always_inline void measure_start(struct measure_ctx *ctx) +{ + ctx->ns_start =3D ktime_get_ns(); + ctx->cycles_start =3D get_cycles(); +} + +static __always_inline void measure_end(struct measure_ctx *ctx, u64 *tota= l_ns, + u64 *total_cycles, u64 *count) +{ + u64 ns_end =3D ktime_get_ns(); + u64 c_end =3D get_cycles(); + + *total_ns +=3D ns_end - ctx->ns_start; + *total_cycles +=3D c_end - ctx->cycles_start; + (*count)++; +} + +static void show_measure_stats(void) +{ + int cpu; + struct measure_data sum =3D {}; + + for_each_possible_cpu(cpu) { + struct measure_data *md =3D per_cpu_ptr(&measure_stats, cpu); + + sum.total_entry_with_watch_ns +=3D md->total_entry_with_watch_ns; + sum.total_entry_with_watch_cycles +=3D + md->total_entry_with_watch_cycles; + sum.total_entry_without_watch_ns +=3D + md->total_entry_without_watch_ns; + sum.total_entry_without_watch_cycles +=3D + md->total_entry_without_watch_cycles; + + sum.total_exit_with_watch_ns +=3D md->total_exit_with_watch_ns; + sum.total_exit_with_watch_cycles +=3D + md->total_exit_with_watch_cycles; + sum.total_exit_without_watch_ns +=3D + md->total_exit_without_watch_ns; + sum.total_exit_without_watch_cycles +=3D + md->total_exit_without_watch_cycles; + + sum.entry_with_watch_count +=3D md->entry_with_watch_count; + sum.entry_without_watch_count +=3D md->entry_without_watch_count; + sum.exit_with_watch_count +=3D md->exit_with_watch_count; + sum.exit_without_watch_count +=3D md->exit_without_watch_count; + } + +#define AVG(ns, cnt) ((cnt) ? ((ns) / (cnt)) : 0ULL) + + pr_info("entry (with watch): %llu ns, %llu cycles (%llu samples)\n", + AVG(sum.total_entry_with_watch_ns, sum.entry_with_watch_count), + AVG(sum.total_entry_with_watch_cycles, + sum.entry_with_watch_count), + sum.entry_with_watch_count); + + pr_info("entry (without watch): %llu ns, %llu cycles (%llu samples)\n", + AVG(sum.total_entry_without_watch_ns, + sum.entry_without_watch_count), + AVG(sum.total_entry_without_watch_cycles, + sum.entry_without_watch_count), + sum.entry_without_watch_count); + + pr_info("exit (with watch): %llu ns, %llu cycles (%llu samples)\n", + AVG(sum.total_exit_with_watch_ns, sum.exit_with_watch_count), + AVG(sum.total_exit_with_watch_cycles, + sum.exit_with_watch_count), + sum.exit_with_watch_count); + + pr_info("exit (without watch): %llu ns, %llu cycles (%llu samples)\n", + AVG(sum.total_exit_without_watch_ns, + sum.exit_without_watch_count), + AVG(sum.total_exit_without_watch_cycles, + sum.exit_without_watch_count), + sum.exit_without_watch_count); +} + +static void reset_measure_stats(void) +{ + int cpu; + + for_each_possible_cpu(cpu) { + struct measure_data *md =3D per_cpu_ptr(&measure_stats, cpu); + + memset(md, 0, sizeof(*md)); + } + + pr_info("measure stats reset.\n"); +} + +#endif + static void ksw_reset_ctx(void) { struct ksw_ctx *ctx =3D ¤t->ksw_ctx; @@ -159,25 +276,28 @@ static void ksw_stack_entry_handler(struct kprobe *p,= struct pt_regs *regs, unsigned long flags) { struct ksw_ctx *ctx =3D ¤t->ksw_ctx; - ulong stack_pointer; - ulong watch_addr; + ulong stack_pointer, watch_addr; u16 watch_len; int ret; +#ifdef CONFIG_KSTACKWATCH_PROFILING + struct measure_ctx m; + struct measure_data *md =3D this_cpu_ptr(&measure_stats); + bool watched =3D false; + + measure_start(&m); +#endif =20 stack_pointer =3D kernel_stack_pointer(regs); =20 - /* - * triggered more than once, may be in a loop - */ if (ctx->wp && ctx->sp =3D=3D stack_pointer) - return; + goto out; =20 if (!ksw_stack_check_ctx(true)) - return; + goto out; =20 ret =3D ksw_watch_get(&ctx->wp); if (ret) - return; + goto out; =20 ret =3D ksw_stack_prepare_watch(regs, ksw_get_config(), &watch_addr, &watch_len); @@ -185,17 +305,32 @@ static void ksw_stack_entry_handler(struct kprobe *p,= struct pt_regs *regs, ksw_watch_off(ctx->wp); ctx->wp =3D NULL; pr_err("failed to prepare watch target: %d\n", ret); - return; + goto out; } =20 ret =3D ksw_watch_on(ctx->wp, watch_addr, watch_len); if (ret) { pr_err("failed to watch on depth:%d addr:0x%lx len:%u %d\n", ksw_get_config()->depth, watch_addr, watch_len, ret); - return; + goto out; } =20 ctx->sp =3D stack_pointer; +#ifdef CONFIG_KSTACKWATCH_PROFILING + watched =3D true; +#endif + +out: +#ifdef CONFIG_KSTACKWATCH_PROFILING + if (watched) + measure_end(&m, &md->total_entry_with_watch_ns, + &md->total_entry_with_watch_cycles, + &md->entry_with_watch_count); + else + measure_end(&m, &md->total_entry_without_watch_ns, + &md->total_entry_without_watch_cycles, + &md->entry_without_watch_count); +#endif } =20 static void ksw_stack_exit_handler(struct fprobe *fp, unsigned long ip, @@ -203,15 +338,36 @@ static void ksw_stack_exit_handler(struct fprobe *fp,= unsigned long ip, struct ftrace_regs *regs, void *data) { struct ksw_ctx *ctx =3D ¤t->ksw_ctx; +#ifdef CONFIG_KSTACKWATCH_PROFILING + struct measure_ctx m; + struct measure_data *md =3D this_cpu_ptr(&measure_stats); + bool watched =3D false; =20 + measure_start(&m); +#endif if (!ksw_stack_check_ctx(false)) - return; + goto out; =20 if (ctx->wp) { ksw_watch_off(ctx->wp); ctx->wp =3D NULL; ctx->sp =3D 0; +#ifdef CONFIG_KSTACKWATCH_PROFILING + watched =3D true; +#endif } + +out: +#ifdef CONFIG_KSTACKWATCH_PROFILING + if (watched) + measure_end(&m, &md->total_exit_with_watch_ns, + &md->total_exit_with_watch_cycles, + &md->exit_with_watch_count); + else + measure_end(&m, &md->total_exit_without_watch_ns, + &md->total_exit_without_watch_cycles, + &md->exit_without_watch_count); +#endif } =20 int ksw_stack_init(void) @@ -239,7 +395,9 @@ int ksw_stack_init(void) unregister_kprobe(&entry_probe); return ret; } - +#ifdef CONFIG_KSTACKWATCH_PROFILING + reset_measure_stats(); +#endif WRITE_ONCE(probe_generation, READ_ONCE(probe_generation) + 1); WRITE_ONCE(probe_enable, true); =20 @@ -252,4 +410,7 @@ void ksw_stack_exit(void) WRITE_ONCE(probe_generation, READ_ONCE(probe_generation) + 1); unregister_fprobe(&exit_probe); unregister_kprobe(&entry_probe); +#ifdef CONFIG_KSTACKWATCH_PROFILING + show_measure_stats(); +#endif } --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f175.google.com (mail-pl1-f175.google.com [209.85.214.175]) (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 59F3C31D387 for ; Mon, 10 Nov 2025 16:38:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792684; cv=none; b=BGPxQ9MpxWyAWf+ArpMc5Z3GY1ow5jnp2lbi89/BQbec12DPPBThjG2FCnb+DxaPj82r2SzKdPfaib4DVfBkXK4smdsQ0Ozfq0/llVMvAAvnl06LS6ogEy3gStB8vfs8ii5G809yk2EHu/3Y8GpomYLaIhE6VVmQk5z+Zmixuoo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792684; c=relaxed/simple; bh=0ll1UY3KeG8rCRKq1wHdxRSPsRNrYLdVRTpppx6seVA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QKE74WQ/Aj6fMPpXzWKwqwMQAKb3BnDbnPuVYwbVEZ/q7HDIE8hwWqRAK/w8Ib/W7q/col5vn3rUaJo1xMOsH7QznaxqjnENkLGAqdP+BGmw0RNR7UCrCvsrqrlgxDaYcOTCLOUS4Fgz1ied2ZNhKJomRjfg0xIti668hJE6Gn0= 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=A/IW2xSR; arc=none smtp.client-ip=209.85.214.175 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="A/IW2xSR" Received: by mail-pl1-f175.google.com with SMTP id d9443c01a7336-298144fb9bcso11593425ad.0 for ; Mon, 10 Nov 2025 08:38:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792682; x=1763397482; 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=UVmErpKw40BYN09ToGb11RVmfMPSvvKFwxlIzoWfVWE=; b=A/IW2xSRULTfD2CUE6PiV3hO/VJS3mgcS4krXuQCnIyGCJ1hsaNXfw75NJeZvsGY0p gpj5JfYOCPp2KQZBAfgvxZoPBm8VSJBfW+uFcJBSohskV+NEKXIfY9oxP2/25hc4Hz2q ZZVgAZh7IdIWZdqm05FfXllJ9Ty1LFXIo6W7sAQvhi1C730hMh8XcZqBgP5wuUp8wDfE xuaWS/LSjLBZBX74X9tsbEJphqFqNEsgiR8m/tpdjTivhR9IrN7n3yCbKnXro8kb65hr aJMocx5EipF2Bp0jgC/rC2G2BbUQdE7Lf7WrvzEyKFpuRnMAVbQnXXoZ89jol3oIohAt W80w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792682; x=1763397482; 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=UVmErpKw40BYN09ToGb11RVmfMPSvvKFwxlIzoWfVWE=; b=lHJwp0QySLggoKm9OcqW8j+4DUA3Mlop63HPNxlo+QqwR7aPr5O74kRiaUyRBuR3FH DflGMjMNMb7eNz4i8EYQUFaTixEk0Y5IhGpox3fjVsQTc9sgQ4P/B5VuWDjyzvuPkDId 79v6Nkd9yZ1UTONEjb5QeHsYW36Xhs7eWwv+OfuXkOXxGldmJu7uyMU/gHxgkFvlmhSw 84RyyKYGqJsR7eRI2v+Nm4LXyvrlqBILi6PdJaG2D6GbkxQ2MTQaXe2BKHFWsxAW1C9B JW46eSi046oyOb4+M0g0l+szfjYnf1oEY4oQ6N874rIqhFwM1ZIHbfGgtA1fPvpR6y0b yIvw== X-Forwarded-Encrypted: i=1; AJvYcCUK9/ujFIxrvTkcApCa8g+o9hYSTOV4UDlGZmjSgnhPOmSQ71EcwkUwqJzG0OKU63ZWWEJnTIJF456NYPE=@vger.kernel.org X-Gm-Message-State: AOJu0Yzii4fD45KxfWsOhxiE4ktm/CUd+2ylqwNdRRxLDRnRrMC919tR Xw5QchXzJeY9sB3GNQXGrdK2ksFQiV89BQ9uwW+JJVkh2JtdQ0sZI8MC X-Gm-Gg: ASbGncvUs8OwR9Uy3vHS7hsQ1BXMO97ZDrpsqs63RLXvfARt0tWooCI8yPVtxNWFygr Hh/rbY2qzHhTCFMGO9BcdYGy9rWb9mDz6En47wHPtgjJDPyj4gPPEfCYQ7pYNFYnsILDfq9MJhA WJLYiJvWltpnSld4LiT+dQZaxlpQruTtvYWESuGaAgzJ664Z2ta8iZhMkXaJaQ+fRtdFPYKglWx epcRguI95UeX5dkUDZBoI4XD1aX0PmI32j6x3NyQ8ZrHDyVFTxYNE/S6v5EkHUo0PXW6tbFyO49 Ws6wTWpAn+p6kecNa5MtYs22y3125jzqUQSPLiUEULpVXUb/owQiOG8XDZyKMbnxnqjabgiC8eu I5MowKD0nkgEisTUzEX5bthko5UjyezDJw5WaKCdH+jtxQkNPBq96feW1Znr1qhXJPX+5MMBNty zWPAQ3BCIS7ALftDMpCioReygmeWFhCPS1 X-Google-Smtp-Source: AGHT+IF4UnHu4G9Q8eIDvf2W+ctzcOIuGaDk8UK6CDWVqUsSsFW88QwW7PGjK89tTce0NP7TNVfhnw== X-Received: by 2002:a17:902:da4b:b0:297:dfae:1524 with SMTP id d9443c01a7336-297e56473f1mr131374835ad.16.1762792681587; Mon, 10 Nov 2025 08:38:01 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29651c8ef47sm153049485ad.74.2025.11.10.08.38.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:01 -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 18/27] arm64/hw_breakpoint: Add arch_reinstall_hw_breakpoint Date: Tue, 11 Nov 2025 00:36:13 +0800 Message-ID: <20251110163634.3686676-19-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 arch_reinstall_hw_breakpoint() to restore a hardware breakpoint in an atomic context. Unlike the full uninstall and reallocation path, this lightweight function re-establishes an existing breakpoint efficiently and safely. This aligns ARM64 with x86 support for atomic breakpoint reinstalls. --- arch/arm64/Kconfig | 1 + arch/arm64/include/asm/hw_breakpoint.h | 1 + arch/arm64/kernel/hw_breakpoint.c | 5 +++++ 3 files changed, 7 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 6663ffd23f25..fa35dfa2f5cc 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -232,6 +232,7 @@ config ARM64 select HAVE_HARDLOCKUP_DETECTOR_PERF if PERF_EVENTS && \ HW_PERF_EVENTS && HAVE_PERF_EVENTS_NMI select HAVE_HW_BREAKPOINT if PERF_EVENTS + select HAVE_REINSTALL_HW_BREAKPOINT if PERF_EVENTS select HAVE_IOREMAP_PROT select HAVE_IRQ_TIME_ACCOUNTING select HAVE_LIVEPATCH diff --git a/arch/arm64/include/asm/hw_breakpoint.h b/arch/arm64/include/as= m/hw_breakpoint.h index bd81cf17744a..6c98bbbc6aa6 100644 --- a/arch/arm64/include/asm/hw_breakpoint.h +++ b/arch/arm64/include/asm/hw_breakpoint.h @@ -119,6 +119,7 @@ extern int hw_breakpoint_exceptions_notify(struct notif= ier_block *unused, unsigned long val, void *data); =20 extern int arch_install_hw_breakpoint(struct perf_event *bp); +extern int arch_reinstall_hw_breakpoint(struct perf_event *bp); extern void arch_uninstall_hw_breakpoint(struct perf_event *bp); extern void hw_breakpoint_pmu_read(struct perf_event *bp); extern int hw_breakpoint_slots(int type); diff --git a/arch/arm64/kernel/hw_breakpoint.c b/arch/arm64/kernel/hw_break= point.c index ab76b36dce82..bd7d23d7893d 100644 --- a/arch/arm64/kernel/hw_breakpoint.c +++ b/arch/arm64/kernel/hw_breakpoint.c @@ -292,6 +292,11 @@ int arch_install_hw_breakpoint(struct perf_event *bp) return hw_breakpoint_control(bp, HW_BREAKPOINT_INSTALL); } =20 +int arch_reinstall_hw_breakpoint(struct perf_event *bp) +{ + return hw_breakpoint_control(bp, HW_BREAKPOINT_RESTORE); +} + void arch_uninstall_hw_breakpoint(struct perf_event *bp) { hw_breakpoint_control(bp, HW_BREAKPOINT_UNINSTALL); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 1B28A31CA75 for ; Mon, 10 Nov 2025 16:38:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792689; cv=none; b=sdvC5qCiRpf5ezEC0WukUHoySzopCab6UKQJLys2zY5ZhpngNwGoSeRlbv+BZ7RP4ml99wO0D90O1kG1+LK/7Bz5JPA3YkYkBWUxc2YR+M2raQjzEPx+ZyAnuBQUJnJ8mfbBzChZIQ89iaaHWI9TPwFw3Q/qT7eq8j1joW06iKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792689; c=relaxed/simple; bh=1lAxfch01axZkI2fEWCFxH87S1am8PsNEQTHP7qVP20=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u/IBZxuMqQZ2a6695IMpf6xxAG84KrR+yBZTwB9jhAkFquF7wd9EcdAPTya93aDafu9HEbs/ron3/bJKomch0oBqi7hshCC3BBO9Z5pIfFTe2CafCW5Xo1VXW+fHo+RvHgYY56F+zKZ3ITm5yabNUihJ9W85fNp3zCaYPup3m2I= 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=lJyRIHn3; arc=none smtp.client-ip=209.85.214.169 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="lJyRIHn3" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-2953e415b27so28605405ad.2 for ; Mon, 10 Nov 2025 08:38:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792686; x=1763397486; 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=BbxG26SeCS0bD0vh784LPkUdC/dr0BSaPzCrIbaXc1o=; b=lJyRIHn3jHnIFsSz81LTqQF+BQ/DRtX5lPvH8wnaX+FErVPurZEtbJArUMQgRX98K9 xJs+XkPUer114NQ8MVRDwtz0slfnjhoRezORCc/FlRlxzl5NpIH+g5J8FbFvukPYGbje vA2oS6OiWfPXD8xQxx6xGF7JE0s7/PG+0j3+PZU1NgPCMzWOdAUetawh7mDptVZVmLx/ MWqUKSloDOxDwz1SBMU/X+vPygHGWgXMBP7KbcFk87oA46hgGiu2LoIXj62IKalfU4kQ yzmMQwlnO4Ni2DuRHv79epNtKwGyfmbOWzwoyOf+okvH5EXRmsipTEM9SEgu960gudqH aG0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792686; x=1763397486; 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=BbxG26SeCS0bD0vh784LPkUdC/dr0BSaPzCrIbaXc1o=; b=s3ZdOW5d17hQrl29pXIac/EnTnduCiE7mTogrmSGGEVG+tvHB5B59QZlhH1KL1boRH PsIqFKtrNsGzRqaYym2ERwsciPaWXO6GlRsFGYaXkA24EI4wE1NoKD9yIiGgUkOAo/vt o0USegBSFQJlF9LHcBXGIsVgtlVOlsw4NF/te7yYPWpO4yYGzedkah89ztRkLIXyAbBu teDP9I2TeJddclYG1VL6BzlBq7LgHQMJdcAKYsEaS2M13x0bG3+dN1FZMVK7S83ruUKR Dz4PnUFRyLWOD7SdKMhIJGcayGLU2lgKiMC39DgECHPkdkuqMSPZYdQKMo6vRbIKtmi7 RgfQ== X-Forwarded-Encrypted: i=1; AJvYcCUOHzmRAwQZkC5LPNYFyg2aeYftuQK0yywLsJcL/i87f4jxw1ydjYc158aSz75JRa75lhjwuLvx1OdFadg=@vger.kernel.org X-Gm-Message-State: AOJu0YyYkgtwjF2lCOR5JDXc31i3Lj5h0k6Sc0z+ChKXh412kypnJzeE VJFPk5jKBh08amUM0hl9LdgmH1BAXuPso5B9hxSDMFAZiABlmSrHvKfc X-Gm-Gg: ASbGncsw7BZihIUHN3VRg4UjSBKTvugEB9FP9Oo24d4B20b1l6HhDAfYZ2Z9G/8mAZK DfvO4xIEJCKeXxQeuflux+fSxpZnbwVuBKuI6xBAiwGIk2r9rq3Tly8YugAbIBhysWiPIVcDBZ1 SxrPJvZ//kGagCBkLEAtttr58nVol9bwKbL44P6Y53GN0ApGWf8+EXT4H7QlTprjINIQ6DCw/ni PmwuEZqTSxoC/L48LOJcrpIX7RDknkbwDqK0icLlwru8BSRlnL2vQ232LIb62dESpWQPHCa73Cj evkYp4d+sY+3bQ23rybps6sgrrs/rXN5oBQqwSo2jA+5YEbb0HqNb8hISHt7nK8qqgkHnAEw9a3 30YIWqcBeG5JQ8cw+q/wXX+3FXscyete7jErD4WUpejPkiYUlbpTz1f2ubXOhx9qkkB/+1HQhYn enaspXbh08Gck= X-Google-Smtp-Source: AGHT+IFGVJoow1/cESxoE1b0elhSpiXpfiC9HmcofENWA1KFmQBzVRQaX5Wyx8+4PBNdomOGrCwPww== X-Received: by 2002:a17:902:ce10:b0:295:596f:8507 with SMTP id d9443c01a7336-297e4bfd91dmr122302855ad.0.1762792686313; Mon, 10 Nov 2025 08:38:06 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29651c7445dsm150531925ad.62.2025.11.10.08.38.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:05 -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 19/27] arm64/hwbp/ksw: integrate KStackWatch handler support Date: Tue, 11 Nov 2025 00:36:14 +0800 Message-ID: <20251110163634.3686676-20-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 support for identifying KStackWatch watchpoints in the ARM64 hardware breakpoint handler. When a watchpoint belongs to KStackWatch, the handler bypasses single-step re-arming to allow proper recovery. Introduce is_ksw_watch_handler() to detect KStackWatch-managed breakpoints and use it in watchpoint_report() under CONFIG_KSTACKWATCH. Signed-off-by: Jinchao Wang --- arch/arm64/kernel/hw_breakpoint.c | 7 +++++++ include/linux/kstackwatch.h | 2 ++ mm/kstackwatch/watch.c | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/arch/arm64/kernel/hw_breakpoint.c b/arch/arm64/kernel/hw_break= point.c index bd7d23d7893d..7abcd988c5c2 100644 --- a/arch/arm64/kernel/hw_breakpoint.c +++ b/arch/arm64/kernel/hw_breakpoint.c @@ -14,6 +14,9 @@ #include #include #include +#ifdef CONFIG_KSTACKWATCH +#include +#endif #include #include #include @@ -738,6 +741,10 @@ static int watchpoint_report(struct perf_event *wp, un= signed long addr, struct pt_regs *regs) { int step =3D is_default_overflow_handler(wp); +#ifdef CONFIG_KSTACKWATCH + if (is_ksw_watch_handler(wp)) + step =3D 1; +#endif struct arch_hw_breakpoint *info =3D counter_arch_bp(wp); =20 info->trigger =3D addr; diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index afedd9823de9..ce3882acc5dc 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -53,6 +53,8 @@ struct ksw_watchpoint { struct llist_node node; // for atomic watch_on and off struct list_head list; // for cpu online and offline }; + +bool is_ksw_watch_handler(struct perf_event *event); int ksw_watch_init(void); void ksw_watch_exit(void); int ksw_watch_get(struct ksw_watchpoint **out_wp); diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index 99184f63d7e3..c2aa912bf4c4 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -64,6 +64,14 @@ static void ksw_watch_handler(struct perf_event *bp, panic("Stack corruption detected"); } =20 +bool is_ksw_watch_handler(struct perf_event *event) +{ + perf_overflow_handler_t overflow_handler =3D event->overflow_handler; + + if (unlikely(overflow_handler =3D=3D ksw_watch_handler)) + return true; + return false; +} static void ksw_watch_on_local_cpu(void *info) { struct ksw_watchpoint *wp =3D info; --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 99295320A2E for ; Mon, 10 Nov 2025 16:38:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792693; cv=none; b=FE2ZQWRZAW9iFNXUqbImBvSw5qKkrMHjSiHR0xSFnsUaQ1VW1GU1taem10QAjD8DUIDCNNUK8aLkBEmbx9a0e3m5H9ApdBYo+0mJW2t3hzP4fX7LSNa3ukHqMZaM/UqBntWqpvv5oyIR6R7ufvHCVLLiRG2ts2vNBhlqYPyKQtA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792693; c=relaxed/simple; bh=hgEfzLMYGp8CwN/e9f7FjAIj6pt9xNamhCZCRYBRqQA=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tNgxfi00T9+XstsJRPDDQRPpbqsgxfQctnTCh4MbyrfbpGNbEgJNXHmRSNtvcTKa6cqgLYB7zMiNcaD6yrZ01Z+QJ7NU7LVD5W+ScUxz1xFaxt0IKLgCg3eCBZT4QYDDrUZPVmW5f3nGlqIyGyHLi1Qfuts5/+hXvV89eBrhPwo= 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=WTRPvRp+; arc=none smtp.client-ip=209.85.214.182 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="WTRPvRp+" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-297ef378069so18864245ad.3 for ; Mon, 10 Nov 2025 08:38:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792691; x=1763397491; 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=tjEoXPNXhfVJUSFrtIZUZPOp73U+hEWiDpkQ2qC/SvQ=; b=WTRPvRp+nyHQ+BzVfs502SSQi3Rbgab5rfHcbvwAuza0fLAUFJ8Z/QoeFAF2sDrrFm 9F2wZt/veLF0y+UnYcODjEBb9317RYByPySO6C4BKYlzlZxrUviSr4aQmxmTQMtysysZ yv5P96wGDrV4nTpCBKHuEVgWW1pnViQYZntJRcqnSz76rZAR9UbdYcIe/wtm/0qrlCIT B7WBYgY3KL9KbV88KUSfErHzpesQNOLB3zZ/lQN0uXS6BO/Rl6j55CwQjEDZT4i/od2L 2cN0m1TyPHY4jSPSS9ABnj12kQXOADGSq6xFBWwvRiz/RT3NVKMtqYqd1+amA1KZz7W5 1YBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792691; x=1763397491; 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=tjEoXPNXhfVJUSFrtIZUZPOp73U+hEWiDpkQ2qC/SvQ=; b=Z+RuWWA5H1FCDrDHPTHmckSjLREepYHl835VnML5ANOV5g49Ya139RjLPn56OVLqeG yqtwQfpuPewZmqCxktC2ooqMc1/S93rM6wi7lLsUEItSdSkEAiCcj8yCW4CBqWu9MktP AKwLi9ikvPtgbtk/p0RzuKTYwJXXLc7vo6fEQIpIUKPXT68H1NSBa8SZPhQt+0o1sY/M At5vFU6glF1ztDVQ/kkcF5VUMeEPb/OJyI6vgtF/MVNE+eoNy8j5mIbIPXzmOuNioSFU IZFbuYkfPM19RsGL4KIuHamuZlo3Lb62Aw+HqgTkM04ZtlB5oAHtgb4rqUMJ1Kornahs mgWA== X-Forwarded-Encrypted: i=1; AJvYcCU3vi5lShZuOZVeyDQz8MFQOH0uj66cafIAEn29c8qaeVts52In5D3efZ9QtkwBrKPzeZfoHd5x7vsUBFI=@vger.kernel.org X-Gm-Message-State: AOJu0YxNeyOD96lcIxujLXdZo1IbWi9TBjL5jD0A4JED4SJkRxhpVqV8 QWffehR292lUCKpA3PPC6L6opp7FO782KdtUGJIXbIcwBF+GbUj8lukl X-Gm-Gg: ASbGncsWBRYCKyqDZvxmsxj72TmrQz+goe5+zTA9VZs+aGLbE9lt5X9wEMaYnzwgB2D blqy4dCoVl7df0HPDKIDagpMIKNZNVawNY1029f4D3kXNvzL8vFkrTyUU8QiHq2qVlL2ME0BJQh cWJPVqrgAMApOBXpdecPHDp+j+QXwiXEczOtfeJ9zSaTydgGuBgb9MH9ubhQCrOp7X4N+OXVXoC KRc91NeVd6MkNda45X3Fm/aRt6arhn8fthPSzC2gthhCUDGKC3/P2cbC8MgbS1qAxx03Ptnpy4o z+gUuIeH0BrDrEDv7cb6/yF4gL1ZUl5ZcE/P/Aars/l358KtCB8LJM1ImHBYJHW/Ic+b/xaTlSX Vmgz4Sno5Yc5EYNaUmPph4AdjOzZ2Zd/HFeH2iZTR31kv1EAL4rGX+8LVbEWO2ndR7Rqh7krppw a1dCkwuogqQS8= X-Google-Smtp-Source: AGHT+IGgGEjFRVaat56XCDxopiL8+SM4HXr1TFRgWsca/knmSVG340IyJkWxuGKU2hwaDE2EKORtXQ== X-Received: by 2002:a17:903:244a:b0:25c:8745:4a58 with SMTP id d9443c01a7336-297e53f933cmr112300665ad.3.1762792690825; Mon, 10 Nov 2025 08:38:10 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-341d0aee149sm8572006a91.1.2025.11.10.08.38.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:10 -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 20/27] mm/ksw: add self-debug helpers Date: Tue, 11 Nov 2025 00:36:15 +0800 Message-ID: <20251110163634.3686676-21-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" Provide two debug helpers: - ksw_watch_show(): print the current watch target address and length. - ksw_watch_fire(): intentionally trigger the watchpoint immediately by writing to the watched address, useful for testing HWBP behavior. Signed-off-by: Jinchao Wang --- include/linux/kstackwatch.h | 2 ++ mm/kstackwatch/watch.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index ce3882acc5dc..6daded932ba6 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -60,5 +60,7 @@ void ksw_watch_exit(void); int ksw_watch_get(struct ksw_watchpoint **out_wp); int ksw_watch_on(struct ksw_watchpoint *wp, ulong watch_addr, u16 watch_le= n); int ksw_watch_off(struct ksw_watchpoint *wp); +void ksw_watch_show(void); +void ksw_watch_fire(void); =20 #endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index c2aa912bf4c4..a298c31848a2 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -273,3 +273,37 @@ void ksw_watch_exit(void) { ksw_watch_free(); } + +/* self debug function */ +void ksw_watch_show(void) +{ + struct ksw_watchpoint *wp =3D current->ksw_ctx.wp; + + if (!wp) { + pr_info("nothing to show\n"); + return; + } + + pr_info("watch target bp_addr: 0x%llx len:%llu\n", wp->attr.bp_addr, + wp->attr.bp_len); +} +EXPORT_SYMBOL_GPL(ksw_watch_show); + +/* self debug function */ +void ksw_watch_fire(void) +{ + struct ksw_watchpoint *wp; + char *ptr; + + wp =3D current->ksw_ctx.wp; + + if (!wp) { + pr_info("nothing to fire\n"); + return; + } + + ptr =3D (char *)wp->attr.bp_addr; + pr_warn("watch triggered immediately\n"); + *ptr =3D 0x42; // This should trigger immediately for any bp_len +} +EXPORT_SYMBOL_GPL(ksw_watch_fire); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 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 875F5322DBB for ; Mon, 10 Nov 2025 16:38:15 +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=1762792698; cv=none; b=GslP8sqJOieRr4CS9awYpJRt3Iv9gd/yFIOIdjJw3Zb7UJ58pkTpbtOAyOiQX/8o2lbfbSGXWvccKiBGT+3eS/kTe9kZQaULrTI5MwBeM8dICVPcAYBNSkp4nxc6RBlILJkZlneRPeuC8nsbKrYQCj1v/iZRDXO5wR8N2uShBlA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792698; c=relaxed/simple; bh=5B8MAc8YcV00iMizH11Y40qRDHrUmpg3vQMS9397iuY=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mB7AL3nV1Vaw5i1nydKgXRjXWbPgclnlSlLsNOx3fQvbJFk9EZs+RDacm///wB4uDgAWOqAq3ZFgq6nM8fzKAM+ZS8KwusqKplnIhy2G8FtikYeRRa98xfTHMF1YwV2xyxzVSlNIIzCNGwtEw+XvTfa2OZMGDl31WvVT0HUiKSA= 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=dhE5kNYp; 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="dhE5kNYp" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7aca3e4f575so2638457b3a.2 for ; Mon, 10 Nov 2025 08:38:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792695; x=1763397495; 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=LZgTPWUg6fgnNgIWmgc5f2sdICO+ed5Q8W3qiS9yQCY=; b=dhE5kNYpjcgzW4+jT3t75DgtPHRNDulghdJNU4syvwxVqwcN5GZ5MnQml+QTDXEN32 OQ6/yX3unKQE8qHk1cK/56FZkvcAR7pcXu82CvV7D8PhM+wWIJmUCF1SuwBsNTgrDYLW ER0FyTXwlKXKNSg77MybeBE1lZ3mQvGGuwy0LEqx2WJk8jmAH7+nt+GfjXfu2Bc22tv3 tSY8rwid2YVNpR1m7qazEf0MkWOOtAuhhkcs8v41ZxK15fRbcJMQKln6pfdYP+5JlB9V Q/pVSABTvhFQTOpEtG2+jfzlSWDrO6e9rkReJRARHKiqUSsJ5/GOamshvWlzgOtY+jCw 05iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792695; x=1763397495; 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=LZgTPWUg6fgnNgIWmgc5f2sdICO+ed5Q8W3qiS9yQCY=; b=k6pZNKQl5udjmP95bPLyEemr0WvWzARG+lgpZBnhIW3I0cHjsLquY+wsNRINxKN9DV x49Ip56GVr/9eTeYT8ZRapiDX5Tv/R9wuevMM8eBUOdyOx3kpzlKVl7vnSSuwzSz8M2G gfP6bTo5KWWikD0tFEcWipjE1wrcgVtr7TxiKioM0FjVCgzujDZbm7KeISh4ifjokvPi SjCtaF2iMQUEStYD5Cak7Ypn6MEuEh6ht+ka24TfZ4WstqII5tep4YwEzhdIDRlmLRhU pf4BKMQZKeCca44c148J9WLOScYZi7Gb13//nT/PiJRxbkWE6G65Kn6JYYtRDlsPMh3z XGXQ== X-Forwarded-Encrypted: i=1; AJvYcCVDpoyRkt87cUD/vzvVMCmzKRdqC9fxz536ijXgWKnrvtvUQDR6FJfGAZVBPwNS3MbSUVSRk2hSKgaAwaI=@vger.kernel.org X-Gm-Message-State: AOJu0YySyQbIZicAX2qUQy0r3b+tiGddI6Drh1XTDR+w6YXNkU/wZ3qR HBDY+l5c6B+XZJLN9Z6Tf49iQMIQ3vajPPYBotIp88HoZRRlaXr1uC9m X-Gm-Gg: ASbGncs4ZaQux+6CxSzhx7wpBQB/anhyLP5kK7RzUirKSQuPj2+f4lTSfpkOB431lkZ UW6ZRPy9tI/IZylkkRbWafHFDU1imyW/N8eyJfuKj6LtjTDfUG3HAlEFIN+1mmxm9EusvNTcuuQ IKCStBJKVricLEOfarDCjUmofyZtLhLaxGlmcIikBkaj92Tm844D0YAmK2OUn+p2MiWxKIEno+n d9pnKr6XnK1gMLt17aLw7RIpDHPtKz3dXEH0WpjrUxWwA/1ufynQ+pbL+TjHmEtqu3yk5NVqffS 4ubS16+qSglNQ03bFGJIlNe/PILRn2i1YkfPY18iWWh5C/aNY1h0fgbxf8QxnLVAAKsNfvc9nCU uZxVs0SlAaRRjWSf8K2399yJSSuZkgjZMk2DklTFxC35fwwxeZpCjMv9+6biiU2E1/tvzrDLvgA s0ChpmSyFvyzoCAuPsvT7Kyw== X-Google-Smtp-Source: AGHT+IEM5Su6Hnp4ru+i7ZTcc23vkwmNQiSNdXgOr/mmO6C717GoWpmDkvA/Iw9C6gak0AA7kGqiVQ== X-Received: by 2002:a17:903:244a:b0:295:59ef:809e with SMTP id d9443c01a7336-297e564e380mr115227515ad.24.1762792694655; Mon, 10 Nov 2025 08:38:14 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b0c953d0a6sm12288816b3a.12.2025.11.10.08.38.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:14 -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 21/27] mm/ksw: add test module Date: Tue, 11 Nov 2025 00:36:16 +0800 Message-ID: <20251110163634.3686676-22-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 a standalone test module for KStackWatch to validate functionality in controlled scenarios. The module exposes a simple interface via debugfs (/sys/kernel/debug/kstackwatch/test), allowing specific test cases to be triggered with commands such as: echo test0 > /sys/kernel/debug/kstackwatch/test To ensure predictable behavior during testing, the module is built with optimizations disabled. Signed-off-by: Jinchao Wang show addr of buf and watch_addr of test case --- include/linux/kstackwatch.h | 2 + mm/kstackwatch/Kconfig | 10 +++ mm/kstackwatch/Makefile | 6 ++ mm/kstackwatch/kernel.c | 5 ++ mm/kstackwatch/test.c | 121 ++++++++++++++++++++++++++++++++++++ 5 files changed, 144 insertions(+) create mode 100644 mm/kstackwatch/test.c diff --git a/include/linux/kstackwatch.h b/include/linux/kstackwatch.h index 6daded932ba6..7711efe85240 100644 --- a/include/linux/kstackwatch.h +++ b/include/linux/kstackwatch.h @@ -40,6 +40,8 @@ struct ksw_config { =20 // singleton, only modified in kernel.c const struct ksw_config *ksw_get_config(void); +struct dentry *ksw_get_dbgdir(void); + =20 /* stack management */ int ksw_stack_init(void); diff --git a/mm/kstackwatch/Kconfig b/mm/kstackwatch/Kconfig index 3c9385a15c33..343b492ddbd3 100644 --- a/mm/kstackwatch/Kconfig +++ b/mm/kstackwatch/Kconfig @@ -22,3 +22,13 @@ config KSTACKWATCH_PROFILING stopping. Useful for performance tuning, not for production use. =20 If unsure, say N. + +config KSTACKWATCH_TEST + tristate "KStackWatch Test Module" + depends on KSTACKWATCH + help + This module provides controlled stack corruption scenarios to verify + the functionality of KStackWatch. It is useful for development and + validation of KStackWatch mechanism. + + If unsure, say N. diff --git a/mm/kstackwatch/Makefile b/mm/kstackwatch/Makefile index c99c621eac02..a2c7cd647f69 100644 --- a/mm/kstackwatch/Makefile +++ b/mm/kstackwatch/Makefile @@ -1,2 +1,8 @@ obj-$(CONFIG_KSTACKWATCH) +=3D kstackwatch.o kstackwatch-y :=3D kernel.o stack.o watch.o + +obj-$(CONFIG_KSTACKWATCH_TEST) +=3D kstackwatch_test.o +kstackwatch_test-y :=3D test.o +CFLAGS_test.o :=3D -fno-inline \ + -fno-optimize-sibling-calls \ + -fno-pic -fno-pie -O0 -Og diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index a0e676e60692..b25cf6830b15 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -235,6 +235,11 @@ const struct ksw_config *ksw_get_config(void) return ksw_config; } =20 +struct dentry *ksw_get_dbgdir(void) +{ + return dbgfs_dir; +} + static int __init kstackwatch_init(void) { int ret =3D 0; diff --git a/mm/kstackwatch/test.c b/mm/kstackwatch/test.c new file mode 100644 index 000000000000..2969564b1a00 --- /dev/null +++ b/mm/kstackwatch/test.c @@ -0,0 +1,121 @@ +// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static struct dentry *test_file; + +#define BUFFER_SIZE 32 + +static void test_watch_fire(void) +{ + u64 buffer[BUFFER_SIZE] =3D { 0 }; + + pr_info("entry of %s\n", __func__); + ksw_watch_show(); + pr_info("buf: 0x%px\n", buffer); + + ksw_watch_fire(); + + barrier_data(buffer); + pr_info("exit of %s\n", __func__); +} + +static ssize_t test_dbgfs_write(struct file *file, const char __user *buff= er, + size_t count, loff_t *pos) +{ + char cmd[256]; + int test_num; + + if (count >=3D sizeof(cmd)) + return -EINVAL; + + if (copy_from_user(cmd, buffer, count)) + return -EFAULT; + + cmd[count] =3D '\0'; + strim(cmd); + + pr_info("received command: %s\n", cmd); + + if (sscanf(cmd, "test%d", &test_num) =3D=3D 1) { + switch (test_num) { + case 0: + test_watch_fire(); + break; + default: + pr_err("Unknown test number %d\n", test_num); + return -EINVAL; + } + } else { + pr_err("invalid command format. Use 'testN'.\n"); + return -EINVAL; + } + + return count; +} + +static ssize_t test_dbgfs_read(struct file *file, char __user *buffer, + size_t count, loff_t *ppos) +{ + static const char usage[] =3D + "KStackWatch Simplified Test Module\n" + "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D usage =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D\n" + "Usage:\n" + "echo test{i} > /sys/kernel/debug/kstackwatch/test\n" + " test0 - test watch fire\n"; + + return simple_read_from_buffer(buffer, count, ppos, usage, + strlen(usage)); +} + +static const struct file_operations test_dbgfs_fops =3D { + .owner =3D THIS_MODULE, + .read =3D test_dbgfs_read, + .write =3D test_dbgfs_write, + .llseek =3D noop_llseek, +}; + +static int __init kstackwatch_test_init(void) +{ + struct dentry *ksw_dir =3D ksw_get_dbgdir(); + + if (!ksw_dir) { + pr_err("kstackwatch must be loaded first\n"); + return -ENODEV; + } + + test_file =3D debugfs_create_file("test", 0600, ksw_dir, NULL, + &test_dbgfs_fops); + if (!test_file) { + pr_err("Failed to create debugfs test file\n"); + return -ENOMEM; + } + + pr_info("module loaded\n"); + return 0; +} + +static void __exit kstackwatch_test_exit(void) +{ + debugfs_remove(test_file); + pr_info("module unloaded\n"); +} + +module_init(kstackwatch_test_init); +module_exit(kstackwatch_test_exit); + +MODULE_AUTHOR("Jinchao Wang"); +MODULE_DESCRIPTION("KStackWatch Test Module"); +MODULE_LICENSE("GPL"); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (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 5B6B0325483 for ; Mon, 10 Nov 2025 16:38:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792702; cv=none; b=H9RZtyWGPEARdPAexSu5Eupl6460gD2f52q5s8CrLPcld7JVId2cU9HptFUC7rO1yqUmBN7fA7R6PeqpfddTrSDMbcOMFotuHJfBneHRohyt7XnMKk8li+ReHP12VygIPZSdQQ2urvv/n2MZIJJ17zdeYE5gLcC3iBYawQ6UWpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792702; c=relaxed/simple; bh=aUATb6PPAs1EeoevQLD9YYPgevbnELTf3A5rXZMQrkU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U5CJ2vkRlLoCOymGvrAuO9ANWwd059RI63gxc7dMS+DuaW9JYuybjLpix4DEIoiDT3O/ImENZskL1GhuFQl0ypzOsC+ammtH1+csh7Rp+4nBrZncjwCfmAmmLS9GkI7217q7t+5Pb/JV3C9/oJCLzJlhiArY5FWq+46ITFaK4as= 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=bWETdonx; arc=none smtp.client-ip=209.85.210.175 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="bWETdonx" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-7b4933bc4bbso1075612b3a.1 for ; Mon, 10 Nov 2025 08:38:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792700; x=1763397500; 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=Tu+j4Ql3fqr3I/4VuukNZy9fUe1HehKP0k0yDQPRfsM=; b=bWETdonxaESSX3yVyFi8vU/Fw76eFPXcTvtrRMeUaxBE1HZjiK7XEKFWIUFEdfNGVK JsZWR5Hnxy+O9hrTWPFPIbkUbhKLNlFwuKozm8/7uIToMqSt0E7ldaaZxR20FMioicAS JTa5e0jRudrFr4kQC507KBM0qHdndWnEtUMJo1JhL4KMg9sLFkxj0ugg8HuGWf4NuowH 20Av4ImijkHrNEE6exTJxczraU0DcyY9yqPVCqx4AU3lROeDQ/vswv+M34lRpEvrLqT3 9EVBz6X/UouARAIpa9IPj5XH+lcsMmEGPZ4tk1Z+vhay/Iy9ETcEAkCmWFe2Zy14O67D ObsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792700; x=1763397500; 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=Tu+j4Ql3fqr3I/4VuukNZy9fUe1HehKP0k0yDQPRfsM=; b=fXWasrBkC6h4LzPfKPMoCFCnBcWa84f0WXJx5kfBRAVQewPOkYhbp8LVCRQaCebEPp el6pEAUlgnpa7q9FxfgchgqM6xMQ54CKPYYP4TELgU4lbPXmoScfgHEllZ0snsaHtVVP Ewsj660EJyyOCQiBfd6MJEFZZDBxg216Xgr9fanGDfSBi6nE0JMGmZAZB/+XYi7tgXca sBPCQUPug/6NLCMVxrYW/EoFOXmJUdiCUOsFK0WbP/4rxOtUg/SQbzBM/IroHnhJ7dfY EL3+8RDfMzGPwGMg+hUsmYuz28OCHwt9S4Cr2S6AQ/qQkeWHuPa868RvaCCkbCMkabbq LO1Q== X-Forwarded-Encrypted: i=1; AJvYcCUrZZCMI7C/fmuGXr1jF9o/4I1axqq7mTfN0ifEWW5LAv0LfR89LuFz2V1WjcYOKMOrEMrvifuxcC6cZbQ=@vger.kernel.org X-Gm-Message-State: AOJu0YytK7/+xmiO/VrFG6Lg/hqwMRPRuX0W7Avpqn6paVvYpQfS9QUg mdGbzvL2zf5xPKKpkUva/04CfRSJRpLCoP/z1AKyI4ZNNt/iGfCdOl/k X-Gm-Gg: ASbGnctFGYYPQQlVjxP1EwdUGghcjgtpgM/m+IM0Ny5tkvAWGrXHswkMjiEhYSEolg4 Yly/PQDaFVrvWtEUF4TmqWz+lQAIqui+BHJyzXj/n86x9cX08AdQ63RAZUSPhGz/Kf8U8QsFDQW n/IYnMIu7C8w/wbXhpdV/MR21Wpg9Tjp36lSbLXO9DQyDClNZLkKc2iOmZYAaK/Ee0udhFbj72L xTJzbIjUO7ITb03fLVXgrqL1K3QyrZFf+tC+uVGW0pbH/r9lHPuGV6BuOxiRuY9c71hjgnMRGDY 8VfUB1eBn26PWnzUCtbe4pH8zRaiGK60jW11ffsySTChH6rimjFXi0SLtO5cDbM1EqDEwUexO7G 9BsImBHJShnJtHwi05kgLgcqsLuG6EmVfpfwpjJjw7ZnfRBueg0IpHCM9CpPW1EsGX6DwfzL2hw Qa78w62XPfZ39Qf0BfP58pfA== X-Google-Smtp-Source: AGHT+IGLmwDIGeljEZq6Ek3cMIcpWBuZ761zCA9XCLHpvBlYPJFK2olRS54fuUcN9DoThdB4AXP5Bg== X-Received: by 2002:a05:6a00:9510:b0:7aa:d1d4:bb7b with SMTP id d2e1a72fcca58-7b21a285a43mr11992599b3a.16.1762792699318; Mon, 10 Nov 2025 08:38:19 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b0ccb5c823sm12295899b3a.62.2025.11.10.08.38.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:18 -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 22/27] mm/ksw: add stack overflow test Date: Tue, 11 Nov 2025 00:36:17 +0800 Message-ID: <20251110163634.3686676-23-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" Extend the test module with a new test case (test1) that intentionally overflows a local u64 buffer to corrupt the stack canary. Signed-off-by: Jinchao Wang show addr of buf and watch_addr of test case --- mm/kstackwatch/test.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/test.c b/mm/kstackwatch/test.c index 2969564b1a00..b3f363d9e1e8 100644 --- a/mm/kstackwatch/test.c +++ b/mm/kstackwatch/test.c @@ -32,6 +32,22 @@ static void test_watch_fire(void) pr_info("exit of %s\n", __func__); } =20 +static void test_canary_overflow(void) +{ + u64 buffer[BUFFER_SIZE]; + + pr_info("entry of %s\n", __func__); + ksw_watch_show(); + pr_info("buf: 0x%px\n", buffer); + + /* intentionally overflow */ + for (int i =3D BUFFER_SIZE; i < BUFFER_SIZE + 10; i++) + buffer[i] =3D 0xdeadbeefdeadbeef; + barrier_data(buffer); + + pr_info("exit of %s\n", __func__); +} + static ssize_t test_dbgfs_write(struct file *file, const char __user *buff= er, size_t count, loff_t *pos) { @@ -54,6 +70,9 @@ static ssize_t test_dbgfs_write(struct file *file, const = char __user *buffer, case 0: test_watch_fire(); break; + case 1: + test_canary_overflow(); + break; default: pr_err("Unknown test number %d\n", test_num); return -EINVAL; @@ -74,7 +93,8 @@ static ssize_t test_dbgfs_read(struct file *file, char __= user *buffer, "=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D usage =3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D\n" "Usage:\n" "echo test{i} > /sys/kernel/debug/kstackwatch/test\n" - " test0 - test watch fire\n"; + " test0 - test watch fire\n" + " test1 - test canary overflow\n"; =20 return simple_read_from_buffer(buffer, count, ppos, usage, strlen(usage)); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (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 11E0E326D6C for ; Mon, 10 Nov 2025 16:38:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.171 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792707; cv=none; b=KEqDq8A+mtx9AY8EA/oCNu++sbWEASrSPQ2OhIUWuhLtPM9MpwwcCN8yKQRJduMUetz+j+Jv7ROTOC6B/Q6AlPdmC0FiTNZMukXLlp8SZY2xj+YeRzIr/bU0lTSyruLtVdZRnK/nNHtb3CFRyWnekqe48HgfPaZ2q9p5RawcLNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792707; c=relaxed/simple; bh=ARZpIKOlBzKXjre3Q9k7I/0rPX2+0FVqcnLvQ0SL+xQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=szGioHh417LaD0TWFT8qgEFvZ1+KSfUWT0nXZtLyU5TSALi0FGXyzT6RM2xstq5iaEuh+U5qT8feU1easXzUMkYDO6rCFkpUmXpz/BTn6fEqoAKbT9mEDigKJecolLcaC8Sg+QTgPoe8AVwF/aUo8bHqsAlMj1OK1SbjESPWxJk= 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=WMhFpo0+; arc=none smtp.client-ip=209.85.210.171 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="WMhFpo0+" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7afc154e411so1890855b3a.1 for ; Mon, 10 Nov 2025 08:38:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792705; x=1763397505; 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=OV6BMzpvIqPwW8tqyoqDeqegoFqswySAQs38qdvsWjQ=; b=WMhFpo0+fc8I2XzRVcLvXDPxY/9K/seUdY+dwPTE0soi/EN2G99WavgUEwqrtWzsQh H16pZs+dvOpD57zlEbs6T9nDQT51Yh0it26FDyGgkRHXtBPqtu6m5WarXEM8E3FyA/f1 ehejOCnc+YIWV+k1QUgPfNASuMDAeL5IGzc717BoKU910zVkXgacjmhI88IcnoPGotjE IY1OWi3zKqgZw+wRXSHc1ord3WryIVf/7PtG0Hj8Ma2oHzZlcQmKKYMcekmMxROd2hK9 lVnkHdNghicMP9pKYSQ/R9d04FQlvEbqV28hsNO2NYuNiziX0ONaSc6BqTkH822yqtXY 0YfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792705; x=1763397505; 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=OV6BMzpvIqPwW8tqyoqDeqegoFqswySAQs38qdvsWjQ=; b=vFldZLSBmKrxG+1ccqkakvmjBUu+EonyHZr7Sxdye+JWDWrKmvrWMNBaGNg4Si2Qd7 LXbcz6VkPPPPr161HYRW67vkFPV+nGfA+mVcSVANLQZszWV/HHp7lzH0NAVHTkBNFyso 92RlTVd3nAgvxLHiGU7eg4Lr2L1OIWisk8nMI/8nqZ2MOIWkifeswNp5XV/WEUCSQbeB 8J8P2IzPd3eaFxCdF8D2Oz/pB4o68Zbi8V6R2117mnjw0y9t+13xGkX3M5uN3TuVGq/M XNrYMNGSGBNGz1qafxftoAHLqXvuiQ3fbeZaeHiNcbGFG+Y4cBM6FPHA5Tnbc+iEbM0x 1pUQ== X-Forwarded-Encrypted: i=1; AJvYcCUKhLZzLKQ8l7HpLpGUr3Uhw8Z7u9FWL4nZCjc0PGc0MWTjn1pRAfPYNT/YpLYytA52mDU3BYVPW02XkAQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzMZ0w2S5SBqkwkYoi8A1BJBDaxvJIpLQDHSBukI/r6YgEFQeOx pOJuGCSngX6/g8HwnImhgr3y7/a9iEDKLPcYs8Q0th/MtoEJm0ylcaP+ X-Gm-Gg: ASbGnctVkotMFM5XfhLgpREVAzgVjSi+WYfeV9UggudR0QgaE1wlIR6mqaM3HyJkhTN EKPHISE4b3+Q2ihfUWYnvoZPmjnPNt9iC1BfuOwANQVsqrQ3GGkg7mJ+oD4JTEDVSDuB+2uJrGV GJGbZBUoJcj8Aim0PRftM7mxmfKL+lK3Acq4WBEaKNaet6D/fahTXvO6fFN9K5lACUr4wIWNqrZ X2dCYV/nsncYHvPpyOcLs/xF3GEtRAXRvwSojjyAiJHr6XsWwgpkY6XKnSSc151dqBmkDsgQ+RE EDsk+VPJTfU0S05jXRgx9yQuzIj8Z/LOQRLcH4SrI4kE2Z3OhBNWpQG2Nol3Te0NiQJmdNTjVUX o6g234c4KaCsNIwCsEVasWXnoRXKMalMa54EBMnU+Th+dV/R5hKGldqfl46imiOLHlBOqIpbmNm MLrZthayP8djnIXNxiNwc+eA== X-Google-Smtp-Source: AGHT+IFPqCK5s7mZ0bMPwZdYahUYIC+qd++YwSpdh6nmmJ2pTVRZoEYmMMiGsvaFI6blh1/WMmJ4hA== X-Received: by 2002:a05:6a00:883:b0:772:4319:e7ed with SMTP id d2e1a72fcca58-7b2277caeecmr11401444b3a.29.1762792705147; Mon, 10 Nov 2025 08:38:25 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b0ccb5a31esm12324547b3a.63.2025.11.10.08.38.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:24 -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 23/27] mm/ksw: add recursive depth test Date: Tue, 11 Nov 2025 00:36:18 +0800 Message-ID: <20251110163634.3686676-24-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" Introduce a test that performs stack writes in recursive calls to exercise stack watch at a specific recursion depth. Signed-off-by: Jinchao Wang --- mm/kstackwatch/test.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/test.c b/mm/kstackwatch/test.c index b3f363d9e1e8..1d196f72faba 100644 --- a/mm/kstackwatch/test.c +++ b/mm/kstackwatch/test.c @@ -17,6 +17,7 @@ static struct dentry *test_file; =20 #define BUFFER_SIZE 32 +#define MAX_DEPTH 6 =20 static void test_watch_fire(void) { @@ -48,6 +49,21 @@ static void test_canary_overflow(void) pr_info("exit of %s\n", __func__); } =20 +static void test_recursive_depth(int depth) +{ + u64 buffer[BUFFER_SIZE]; + + pr_info("entry of %s depth:%d\n", __func__, depth); + + if (depth < MAX_DEPTH) + test_recursive_depth(depth + 1); + + buffer[0] =3D depth; + barrier_data(buffer); + + pr_info("exit of %s depth:%d\n", __func__, depth); +} + static ssize_t test_dbgfs_write(struct file *file, const char __user *buff= er, size_t count, loff_t *pos) { @@ -73,6 +89,9 @@ static ssize_t test_dbgfs_write(struct file *file, const = char __user *buffer, case 1: test_canary_overflow(); break; + case 2: + test_recursive_depth(0); + break; default: pr_err("Unknown test number %d\n", test_num); return -EINVAL; @@ -94,7 +113,8 @@ static ssize_t test_dbgfs_read(struct file *file, char _= _user *buffer, "Usage:\n" "echo test{i} > /sys/kernel/debug/kstackwatch/test\n" " test0 - test watch fire\n" - " test1 - test canary overflow\n"; + " test1 - test canary overflow\n" + " test2 - test recursive func\n"; =20 return simple_read_from_buffer(buffer, count, ppos, usage, strlen(usage)); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 7B041327208 for ; Mon, 10 Nov 2025 16:38:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792712; cv=none; b=vAGJTrIGuHOmEU21M+qGhlnC6Wg5swq1DkfVyuNSTqch66BZFQ2CCf3psMDa4SBD5yAlmV4CDAiEQ+5fPMo7qC6huEcegu4Xlh0bo9mynr6NVA3+FYxp2Q4EuZ9aY65rtQqiIhYyDx6/sawpndSk1EyHnrilNEMb6VF5Wu5oMl4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792712; c=relaxed/simple; bh=NfGEbJbI2JFov2jJzKiHrfTCu+bputoxbrgn4T4KCZQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nlO3f+4XxVhJavOzq7GiZavQYNhPXngAC5Z0XWIf3TB0A5OtqQOlTfCUzDxrDMf0ANYnLNvdV27K0FxYzeuWxoV+D5ony8rsncxGMPusFqSKNglnMuVCtvtRkTSM648TX12dDECrJWwmLVnqOdrceyBi7G5bzjph/0K02vvwZ/s= 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=L+NsIW10; arc=none smtp.client-ip=209.85.214.182 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="L+NsIW10" Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-297d4a56f97so23190275ad.1 for ; Mon, 10 Nov 2025 08:38:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792709; x=1763397509; 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=/i9Y6D84AcwkcQizXylWnD304yGUpX7AGwmA82BxIzc=; b=L+NsIW10VFItrQxtlYCR/vbCh0Y0OxL7z3gkwLWeaUmZZiV6RvmEP6YN35GUxlfrIS yYfRW5ijSNQh8ck8gIwlF0OSMW2W8A8Fl15Jke4tfDPWTN3aozRNUW/xy82m5Rtpn7v0 msORlPpMNYhRU9QVx/HTdqGeeh7/KQKbKaMpy68JUS2VYw0AfbouawfEGjDdHvoNAXYN w59al5JF34eGNCpt485S7m5zOVytE/PTO1P5rL1SvDmAorw5ZpKwHkJbWyzu/501VV0Y +axIHfUBz4zDbPxk2409I9llMjJx8VGPWC4TBcJrixGdp2WmDKnoNTargfAanc553d9E LToQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792709; x=1763397509; 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=/i9Y6D84AcwkcQizXylWnD304yGUpX7AGwmA82BxIzc=; b=UT5ghlScUu0Gsrz+VeP3B7nyeNnauo8xz7MH7iliSLbX3QBzBgm9sJnqpFre//pZS0 JFHQKG5nyJmotsA0rDEIUp7JUUX6ujLgMUIn+mvw3VzqMedl7wOqCrfoc4nNxSZCBMgD InJIj7PAhEPRFg+JkzdK5uQlGrcUBGFx1nqJbmMdCQDgYae3sjLqWfkpWhAB+7PBHNK4 FvzH+colGYFixUM+ESHRQxVxmFGs691ux27MuDB+I3E7bJpm/J2QJdkw4oRa49c+foUL +H5hMJ3kRgcpaPowQPzSPqzBh/BUWxGliVtvlcz/b4xp8wPDOCFUaf9wdxYKWH7anck6 Y2Dg== X-Forwarded-Encrypted: i=1; AJvYcCXOeXX6BQun68lDjZHa1tq5eByH9yklbsiOThpN5V0+a0CJddkxcBObyRHK6dNPsJbACvQkuag0iOKuJjk=@vger.kernel.org X-Gm-Message-State: AOJu0YyupOcFg5H3g7DvH1S3gQIcE6jYBwDf9uVCb3Q8gwMDZHB2LNfj WAFfXuP7bWmET/7Zh3rz2bBpEKfwQ7C6B6CfePArzV4Mwdt1kqzsqz6U X-Gm-Gg: ASbGncviqZgwqcli8edK18DBDUhqo402QUOVxGDA4USFc8NcgDwkLSst5I/ZcfrZVkT tXKhF0ZN0aE5wjuUl93F7UlvYkTVKnUuDSUj+Om+cJukLgLGiSrtp01zuHJSLpMir0Sb8r02UJl 8WsaERoC+68rzWWOZzK4RGLkkOPfKR8TkruUQF5f2QS9AtDCfa2HvFs9EpwUstGrTD+FEmpl+hZ r1xJvmu1wfBP2JNsESM2zZrQ+KFps+rnS7dhvjKOoNZzTsX9jlzu+nuX80ITBpCNtyCab98eQL3 KtfO9LbwOwA8Bl1xe4bJl4K1p2kimpFSUUkWOA1MwW0QzxezD1JrYMNJeaohKN1yvkjEgQg5bpI dHKsDlNDwvnHtfbtL1u3ZviNmOC3nYhEEiuAXfDw8M6EkyWRkBnh5KJ3jWgBQYqOZ6PkX0ECPIo EmvlB0eyFfqtdG4aMN+nJv7w== X-Google-Smtp-Source: AGHT+IGGLWsP+rKIukfYRL1IHqALzr1SjL54Nbxq/1zFc3pjrCTHbWmU7///o0j0mRvG3+5kbWDS4Q== X-Received: by 2002:a17:902:da4b:b0:295:512f:5060 with SMTP id d9443c01a7336-297e540dc24mr116769525ad.7.1762792709436; Mon, 10 Nov 2025 08:38:29 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29651ccec04sm151070875ad.102.2025.11.10.08.38.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:28 -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 24/27] mm/ksw: add multi-thread corruption test cases Date: Tue, 11 Nov 2025 00:36:19 +0800 Message-ID: <20251110163634.3686676-25-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" These tests share a common structure and are grouped together. - buggy(): exposes the stack address to corrupting(); may omit waiting - corrupting(): reads the exposed pointer and modifies memory; if buggy() omits waiting, victim()'s buffer is corrupted - victim(): initializes a local buffer and later verifies it; reports an error if the buffer was unexpectedly modified buggy() and victim() run in worker() thread, with similar stack frame sizes to simplify testing. By adjusting fence_size in corrupting(), the test can trigger either silent corruption or overflow across threads. - Test 3: one worker, 20 loops, silent corruption - Test 4: 20 workers, one loop each, silent corruption - Test 5: one worker, one loop, overflow corruption Test 4 also exercises multiple watchpoint instances. Signed-off-by: Jinchao Wang mm/ksw: add KSTACKWATCH_PROFILING to measure probe cost Introduce CONFIG_KSTACKWATCH_PROFILING to enable optional runtime profiling in KStackWatch. When enabled, it records entry and exit probe latencies (in nanoseconds and CPU cycles) and reports averaged statistics at module exit. Signed-off-by: Jinchao Wang --- mm/kstackwatch/test.c | 186 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 185 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/test.c b/mm/kstackwatch/test.c index 1d196f72faba..4bd0e5026fd9 100644 --- a/mm/kstackwatch/test.c +++ b/mm/kstackwatch/test.c @@ -19,6 +19,20 @@ static struct dentry *test_file; #define BUFFER_SIZE 32 #define MAX_DEPTH 6 =20 +struct work_node { + ulong *ptr; + u64 start_ns; + struct completion done; + struct list_head list; +}; + +static DECLARE_COMPLETION(work_res); +static DEFINE_MUTEX(work_mutex); +static LIST_HEAD(work_list); + +static int global_fence_size; +static int global_loop_count; + static void test_watch_fire(void) { u64 buffer[BUFFER_SIZE] =3D { 0 }; @@ -64,6 +78,164 @@ static void test_recursive_depth(int depth) pr_info("exit of %s depth:%d\n", __func__, depth); } =20 +static struct work_node *test_mthread_buggy(int thread_id, int seq_id) +{ + ulong buf[BUFFER_SIZE]; + struct work_node *node; + bool trigger; + + node =3D kmalloc(sizeof(*node), GFP_KERNEL); + if (!node) + return NULL; + + init_completion(&node->done); + node->ptr =3D buf; + node->start_ns =3D ktime_get_ns(); + mutex_lock(&work_mutex); + list_add(&node->list, &work_list); + mutex_unlock(&work_mutex); + complete(&work_res); + + trigger =3D (get_random_u32() % 100) < 10; + if (trigger) + return node; /* let the caller handle cleanup */ + + wait_for_completion(&node->done); + kfree(node); + return NULL; +} + +#define CORRUPTING_MINIOR_WAIT_NS (100000) +#define VICTIM_MINIOR_WAIT_NS (300000) + +static inline void silent_wait_us(u64 start_ns, u64 min_wait_us) +{ + u64 diff_ns, remain_us; + + diff_ns =3D ktime_get_ns() - start_ns; + if (diff_ns < min_wait_us * 1000ULL) { + remain_us =3D min_wait_us - (diff_ns >> 10); + usleep_range(remain_us, remain_us + 200); + } +} + +static void test_mthread_victim(int thread_id, int seq_id, u64 start_ns) +{ + ulong buf[BUFFER_SIZE]; + + for (int j =3D 0; j < BUFFER_SIZE; j++) + buf[j] =3D 0xdeadbeef + seq_id; + if (start_ns) + silent_wait_us(start_ns, VICTIM_MINIOR_WAIT_NS); + + for (int j =3D 0; j < BUFFER_SIZE; j++) { + if (buf[j] !=3D (0xdeadbeef + seq_id)) { + pr_warn("victim[%d][%d]: unhappy buf[%d]=3D0x%lx\n", + thread_id, seq_id, j, buf[j]); + return; + } + } + + pr_info("victim[%d][%d]: happy\n", thread_id, seq_id); +} + +static int test_mthread_corrupting(void *data) +{ + struct work_node *node; + int fence_size; + + while (!kthread_should_stop()) { + if (!wait_for_completion_timeout(&work_res, HZ)) + continue; + while (true) { + mutex_lock(&work_mutex); + node =3D list_first_entry_or_null(&work_list, + struct work_node, list); + if (node) + list_del(&node->list); + mutex_unlock(&work_mutex); + + if (!node) + break; /* no more nodes, exit inner loop */ + silent_wait_us(node->start_ns, + CORRUPTING_MINIOR_WAIT_NS); + + fence_size =3D READ_ONCE(global_fence_size); + for (int i =3D fence_size; i < BUFFER_SIZE - fence_size; + i++) + node->ptr[i] =3D 0xabcdabcd; + + complete(&node->done); + } + } + + return 0; +} + +static int test_mthread_worker(void *data) +{ + int thread_id =3D (long)data; + int loop_count; + struct work_node *node; + + loop_count =3D READ_ONCE(global_loop_count); + + for (int i =3D 0; i < loop_count; i++) { + node =3D test_mthread_buggy(thread_id, i); + + if (node) + test_mthread_victim(thread_id, i, node->start_ns); + else + test_mthread_victim(thread_id, i, 0); + if (node) { + wait_for_completion(&node->done); + kfree(node); + } + } + return 0; +} + +static void test_mthread_case(int num_workers, int loop_count, int fence_s= ize) +{ + static struct task_struct *corrupting; + static struct task_struct **workers; + + WRITE_ONCE(global_loop_count, loop_count); + WRITE_ONCE(global_fence_size, fence_size); + + init_completion(&work_res); + workers =3D kmalloc_array(num_workers, sizeof(void *), GFP_KERNEL); + memset(workers, 0, sizeof(struct task_struct *) * num_workers); + + corrupting =3D kthread_run(test_mthread_corrupting, NULL, "corrupting"); + if (IS_ERR(corrupting)) { + pr_err("failed to create corrupting thread\n"); + return; + } + + for (ulong i =3D 0; i < num_workers; i++) { + workers[i] =3D kthread_run(test_mthread_worker, (void *)i, + "worker_%ld", i); + if (IS_ERR(workers[i])) { + pr_err("failto create worker thread %ld", i); + workers[i] =3D NULL; + } + } + + for (ulong i =3D 0; i < num_workers; i++) { + if (workers[i] && workers[i]->__state !=3D TASK_DEAD) { + usleep_range(1000, 2000); + i--; + } + } + kfree(workers); + + if (corrupting && !IS_ERR(corrupting)) { + kthread_stop(corrupting); + corrupting =3D NULL; + } +} + static ssize_t test_dbgfs_write(struct file *file, const char __user *buff= er, size_t count, loff_t *pos) { @@ -92,6 +264,15 @@ static ssize_t test_dbgfs_write(struct file *file, cons= t char __user *buffer, case 2: test_recursive_depth(0); break; + case 3: + test_mthread_case(1, 20, BUFFER_SIZE / 4); + break; + case 4: + test_mthread_case(200, 1, BUFFER_SIZE / 4); + break; + case 5: + test_mthread_case(1, 1, -3); + break; default: pr_err("Unknown test number %d\n", test_num); return -EINVAL; @@ -114,7 +295,10 @@ static ssize_t test_dbgfs_read(struct file *file, char= __user *buffer, "echo test{i} > /sys/kernel/debug/kstackwatch/test\n" " test0 - test watch fire\n" " test1 - test canary overflow\n" - " test2 - test recursive func\n"; + " test2 - test recursive func\n" + " test3 - test silent corruption\n" + " test4 - test multiple silent corruption\n" + " test5 - test prologue corruption\n"; =20 return simple_read_from_buffer(buffer, count, ppos, usage, strlen(usage)); --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) (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 1F026328B44 for ; Mon, 10 Nov 2025 16:38:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.169 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792717; cv=none; b=JGl95VzY1hRRFtDoUfIEjYEgrrHcTsaeI8b68VNeM4MgQJixIIMXQ0zgmscL3YFgtyejU6JwXpgZlDaKYSp0oEAlRKPGB2UkBFuBkTu+IX2yX1ZjEpthhS/syULK+o2S0Hk/aN2xjUc8HZinoGI49ucJ3NTm06Uq8jgj9fgHuyE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792717; c=relaxed/simple; bh=7XtXQ97oCD17IPK7+NK1Mw0M3hjSZXH8IzRrWjOBBJQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=tZN+u9MiAAy1xD8uXmcrmsn2e/VBZX/xdyYvntLFs0+o7tAvXdelT74RKzzx4tKyLfplaYocbBaMmGEIfCTMCNPlCT+lqvuqE+80XBaqlVc309Tn3SBeNMiv5Np7ujK+glBPJvlEjc3UhGl1xQzOvj8fFEJ/jxm+78hYM+YDUuM= 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=MtfYJ9ms; arc=none smtp.client-ip=209.85.214.169 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="MtfYJ9ms" Received: by mail-pl1-f169.google.com with SMTP id d9443c01a7336-295548467c7so34500035ad.2 for ; Mon, 10 Nov 2025 08:38:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792714; x=1763397514; 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=Y3GxOA64nlDPTJib953n4vjoIghnHFGUI3myXADNt+s=; b=MtfYJ9msGxlKiKt2vwibjYrQn9Q7Iem3xQxfw94Y0bjSQQZ2Tpw5CtlvU3vBcRGcDz I+V8r12hwuTjXcrfUHsvDA+j6oUMIcenhVGuzgG7sNYKdMMpMiC8dn9pBqP+NTzxW2cD tuOcqo+wHJlruQ8duXqcZRMidpt1Iy47PSWDEsHUne/izqMhkWNpxv6HrucgufSSApSn 2tuJo2mhUIWceRaWxWTQ175ix0wFd+3gy70Y7Ztz0aMqkupWRn7I7e3kag5ftI/v/hkU wVxuy8Gp6dtov2GwQ1q4ovvEYAwcemIGcFjq/CMkusCJM7sZmJeYstkefeJIpbflcYJL +ERg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792714; x=1763397514; 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=Y3GxOA64nlDPTJib953n4vjoIghnHFGUI3myXADNt+s=; b=UtlXi4y58j9zwP+Osg8HPIueQ/HgpGvtvbw3uoz6kN5F6tLSEY1pO+R68BKuszWDzV c2/i6ECfXlt5QZ4EFt5E+O6oXA8f75pgBIZF4nJMkYz0fTtqmEzo4VD3l8r9b50oCrda 9i6Z9vLYRPELiOJyX+mSHFrU+a8MyOsvNd7bi3PGEknM8rIPw4SUozOcj8qNZEwR4L3/ eGEhg3zT+IJE0YhtRZvAqfsXQMmxQT8ictlKxRJvWoLrXbxo1kDUWBmodkn6EfeI10+n RnVBTguDORo5wvUXEdGpVasXceAURtS3TFdeJYg39/ybIeOiiM33nun4hTXnG+2Lse4z roKA== X-Forwarded-Encrypted: i=1; AJvYcCX1017LQUlujCw4isQAWx5vhIYKcBTpMz46JVqfdbGcQqf8Dw6RONF5Krs/qoV4jM9ruQwEd9K2BPw+eXI=@vger.kernel.org X-Gm-Message-State: AOJu0YzLzfXt811VjzKKP8LjZlcy4jHlsBjo9d+vWL+HorSXJct+t+Iz EQ9dSCFheKkoNUpgidUi9dvVcWjj18mn61wQqf330ePT8z2OKVMXOoH2 X-Gm-Gg: ASbGnctGfua+tLLTffJJZyM66NJNHls2/PIsqAh6AV/C7jI4QLQ9YWOw8WyxXFN9x4Z lhOJixC6hb4sylgQTn4PkSoycEbhcxnZPkrhRWQD3G8T7w8vnizQ4LjwNyqRiVDpCKYo1NMzhrm Hr9p83+7cgFHQesK+91OMaGPAwQjBfxMUDwEBMGxhWsWogsZs51OdBayJZxVriW9TuZjKK22ilp iExL44fBcoJAP/ZY1tBVVAtQnwXSpVQ0EpjvmnN/bFOo3Wd1c5sBb1llEfMcfgq/Qe6h+H05yEv OjdXfOzH/aVfA/ftP8ymyGPrIks9/p8J49Nz4lsrULiuJIuN0adWtcru3blFlQZDhzsZnkzt9KC 4p/+2AABpFiEHzoaHEXRPrPdXnYFPoDdhNcjehCc0B3Tx3YSj2lP9pQCHSDsbljgiV/b+I43dcb wvBv+rbIWk6QG6vJACW65K2w== X-Google-Smtp-Source: AGHT+IHJX8e0VatXnyxoY2Gj4pP23au6Z6EmKSnmcplOP/Vs7kpe6TtUClUvAMyMHtho7bQMYRxkQg== X-Received: by 2002:a17:902:da4b:b0:295:fc0:5a32 with SMTP id d9443c01a7336-297e53e7aa7mr114885545ad.3.1762792714416; Mon, 10 Nov 2025 08:38:34 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2980c51b8c9sm52389295ad.47.2025.11.10.08.38.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:33 -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 25/27] tools/ksw: add arch-specific test script Date: Tue, 11 Nov 2025 00:36:20 +0800 Message-ID: <20251110163634.3686676-26-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 a shell script under tools/kstackwatch to run self-tests such as canary overflow and recursive depth. The script supports both x86_64 and arm64, selecting parameters automatically based on uname -m. Signed-off-by: Jinchao Wang --- tools/kstackwatch/kstackwatch_test.sh | 85 +++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 tools/kstackwatch/kstackwatch_test.sh diff --git a/tools/kstackwatch/kstackwatch_test.sh b/tools/kstackwatch/ksta= ckwatch_test.sh new file mode 100755 index 000000000000..6e83397d3213 --- /dev/null +++ b/tools/kstackwatch/kstackwatch_test.sh @@ -0,0 +1,85 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +echo "IMPORTANT: Before running, make sure you have updated the config val= ues!" + +usage() { + echo "Usage: $0 [0-5]" + echo " 0 - test watch fire" + echo " 1 - test canary overflow" + echo " 2 - test recursive depth" + echo " 3 - test silent corruption" + echo " 4 - test multi-threaded silent corruption" + echo " 5 - test multi-threaded overflow" +} + +run_test_x86_64() { + local test_num=3D$1 + case "$test_num" in + 0) echo fn=3Dtest_watch_fire fo=3D0x29 ac=3D1 >/sys/kernel/debug/kstackwa= tch/config + echo test0 > /sys/kernel/debug/kstackwatch/test + ;; + 1) echo fn=3Dtest_canary_overflow fo=3D0x14 >/sys/kernel/debug/kstackwatc= h/config + echo test1 >/sys/kernel/debug/kstackwatch/test + ;; + 2) echo fn=3Dtest_recursive_depth fo=3D0x2f dp=3D3 wl=3D8 so=3D0 >/sys/ke= rnel/debug/kstackwatch/config + echo test2 >/sys/kernel/debug/kstackwatch/test + ;; + 3) echo fn=3Dtest_mthread_victim fo=3D0x4c so=3D64 wl=3D8 >/sys/kernel/de= bug/kstackwatch/config + echo test3 >/sys/kernel/debug/kstackwatch/test + ;; + 4) echo fn=3Dtest_mthread_victim fo=3D0x4c so=3D64 wl=3D8 >/sys/kernel/de= bug/kstackwatch/config + echo test4 >/sys/kernel/debug/kstackwatch/test + ;; + 5) echo fn=3Dtest_mthread_buggy fo=3D0x16 so=3D0x100 wl=3D8 >/sys/kernel/= debug/kstackwatch/config + echo test5 >/sys/kernel/debug/kstackwatch/test + ;; + *) usage + exit 1 ;; + esac + # Reset watch after test + echo >/sys/kernel/debug/kstackwatch/config +} + +run_test_arm64() { + local test_num=3D$1 + case "$test_num" in + 0) echo fn=3Dtest_watch_fire fo=3D0x50 ac=3D1 >/sys/kernel/debug/kstackwa= tch/config + echo test0 > /sys/kernel/debug/kstackwatch/test + ;; + 1) echo fn=3Dtest_canary_overflow fo=3D0x20 so=3D264 >/sys/kernel/debug/k= stackwatch/config + echo test1 >/sys/kernel/debug/kstackwatch/test + ;; + 2) echo fn=3Dtest_recursive_depth fo=3D0x34 dp=3D3 wl=3D8 so=3D8 >/sys/ke= rnel/debug/kstackwatch/config + echo test2 >/sys/kernel/debug/kstackwatch/test + ;; + 3) echo fn=3Dtest_mthread_victim fo=3D0x6c so=3D0x48 wl=3D8 >/sys/kernel/= debug/kstackwatch/config + echo test3 >/sys/kernel/debug/kstackwatch/test + ;; + 4) echo fn=3Dtest_mthread_victim fo=3D0x6c so=3D0x48 wl=3D8 >/sys/kernel/= debug/kstackwatch/config + echo test4 >/sys/kernel/debug/kstackwatch/test + ;; + 5) echo fn=3Dtest_mthread_buggy fo=3D0x20 so=3D264 >/sys/kernel/debug/kst= ackwatch/config + echo test5 >/sys/kernel/debug/kstackwatch/test + ;; + *) usage + exit 1 ;; + esac + # Reset watch after test + echo >/sys/kernel/debug/kstackwatch/config +} + +# Check root and module +[ "$EUID" -ne 0 ] && echo "Run as root" && exit 1 +for f in /sys/kernel/debug/kstackwatch/config /sys/kernel/debug/kstackwatc= h/test; do + [ ! -f "$f" ] && echo "$f not found" && exit 1 +done + +# Run +[ -z "$1" ] && { usage; exit 0; } + +arch=3D$(uname -m) +case "$arch" in + x86_64|aarch64) run_test_${arch} "$1" ;; + *) echo "Unsupported architecture: $arch" && exit 1 ;; +esac --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (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 3CF85329361 for ; Mon, 10 Nov 2025 16:38:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792721; cv=none; b=V+Mht/bWw+9A6ROEAFGxVZsixCRQWSIX65XxnJp2rsg/0nBdmxYMbqZ252cVmqVgkSKPg4OmdfbyX2G6+6uO07zK3qfmWz9Oe0USHRvDKncQDsUMCem+ouFjlK51BvgwVKnkBuIqc4HaLUxOAEuke1VXy5ci5kksZvdi5T4o/Lw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792721; c=relaxed/simple; bh=VDsZenUzfnE+W8QkR96oGT8so3wbc7gpK/KHvs4jUIM=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=hj/7gN2mwwJoDHsY0007m4BGzlVuJrNCCnuf0g9VO/DG7YaLzjPZZMi00vXAEgZjb6BOEQDaJ+r7Z0n+5m/etWSjyZFtQTq5NAKRPFM2EZWm9BUPvElfS3S+p4SyFvmY9L8IyGvAh3htAuQiSGZjcl9WQW5yBuLIOmh1CRNu2pw= 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=baFq+VWt; arc=none smtp.client-ip=209.85.215.176 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="baFq+VWt" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-b98a619f020so2853300a12.2 for ; Mon, 10 Nov 2025 08:38:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792718; x=1763397518; 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=1OccL0Sq4R/RA62B7XB61l1HOYHt90e9E66Mfl8uInY=; b=baFq+VWt0QzdbLhjDWftOXoIiJQ9KySs/iIb+ZkmSofkOAKwv3nB74M82lQ8pXbPGb 36yATHxiixrSJJY4i5FTdAr7a6xJxAChhgc36ESDDNm8xlBkIBofSgyc6C8KaEu6wh3c Vq5pQv/jdJ9RpUGpbW10RI/RdRmduIooHXfXLhwNDlz/c21lt6LGRT/Sw+ArywO0rEdA tnQDQoj2GTwaZr9NYXvRpz5g07N/7F9fr8E7r4nXyE7PpryI4hc6s0ZnczjpsgyCQ9f+ 07z1y/kvSpaz1NvgwOiHMpkASVk281dSBw5TGk/4pTp8v7b9BLi6p/AEwa2LCHzCRnuM 4YlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792718; x=1763397518; 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=1OccL0Sq4R/RA62B7XB61l1HOYHt90e9E66Mfl8uInY=; b=IfwZMXaMeZvMFUj56BHMJOJ5UeLL8mZztmKjgXa+m/24Rm/Rso4R4k+Je9a/2bg5DV eTlDiEMh9bO6usocfEPS8xFHYW2V38KwLRa3iYHiZUAtNRuHOJ2wCW2hczzLQy04ztk3 M2jpAZXpKulp0opEDHqJmLZGy5F1NSiNVMt0Cp0J/bKW3x6zHeZAF4TXbl1kmBDm0fTi N2GVhp25HAt0tvxVcDvGPKrT0gHKiVxNdF8PkpCZh3LVfcJ2T03o73/M4UYPn8Q84aFa nboB+BDqW27hQVUVvpfKru9jIBtCOxw0/VTCgLp4ch/4T87PsTnxp8tjC48G7d81kmyL DuBg== X-Forwarded-Encrypted: i=1; AJvYcCXv0GgXIM/2QPh/pmjaiEYA99Hj+OhD+sSE11szOGAl7kv+ocC6wGCw/7zVtzHcSD4LE8BOtTqA3m4zA3E=@vger.kernel.org X-Gm-Message-State: AOJu0YxMDWRn6DPhOPju4Qt7evmxXqQWSiMxeySTk4FgIH0abKsd3xtR /LHq2vmdJc5rYwBfiINeH2qT//58DOM7PMe7AAZrpfGD1dx5420c7RTN X-Gm-Gg: ASbGncuJX5Cs/t/Cm+lLV+al9uNWhvRDpWngmo7nKkgZD/wzxp2GoAaKLDpUx+q7PGj lQFJSeFXcbbx37x2poUrvuiPlMaNxQD/9Dd7vgySR9tlGqM+euuA6u5EwLB84yvosGqG+gpXNff 3is/vxc1nOEyfeXuNRywunwi/iaQ/PoLs3ZbD7kzr5TL5YNan0F0EpbnuVt12WuB6BYMEiiT3/X WAISU8FpVnSfTSlHOEOq+s9AUbXKBQuqzzTr/m9mFDAm1qQa0/cv/HcxTA77fUCeEIM87RU6075 xAXxx+6HQDwvtA1QX2IHXSel7yncHdcjXs0ICKwolfZbKUgp0atpaqnJ0+Mlrq6Z11rix7dlLVw qvxG/v8ScrB07HZjXX0XV9SEr9YugSyfTbV2VSIUjxyufOvjFeIZa4Lr6G4N5RN8w4l2EHj5tW0 pXsf7CEyp/1eA= X-Google-Smtp-Source: AGHT+IExIQBwxVCTQRLrox7JMc34uETHKbl/Z36VlnkqJ0Z8ANoAeswbWItT/aZ29+cnLxMDYj5a3A== X-Received: by 2002:a17:902:cec7:b0:295:9db1:ff32 with SMTP id d9443c01a7336-297e56dc7b2mr114908455ad.48.1762792718180; Mon, 10 Nov 2025 08:38:38 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-29650c5e5bdsm150563255ad.39.2025.11.10.08.38.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:37 -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 26/27] docs: add KStackWatch document Date: Tue, 11 Nov 2025 00:36:21 +0800 Message-ID: <20251110163634.3686676-27-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Add documentation for KStackWatch under Documentation/. It provides an overview, main features, usage details, configuration parameters, and example scenarios with test cases. The document also explains how to locate function offsets and interpret logs. Signed-off-by: Jinchao Wang --- Documentation/dev-tools/index.rst | 1 + Documentation/dev-tools/kstackwatch.rst | 377 ++++++++++++++++++++++++ 2 files changed, 378 insertions(+) create mode 100644 Documentation/dev-tools/kstackwatch.rst diff --git a/Documentation/dev-tools/index.rst b/Documentation/dev-tools/in= dex.rst index 4b8425e348ab..272ae9b76863 100644 --- a/Documentation/dev-tools/index.rst +++ b/Documentation/dev-tools/index.rst @@ -32,6 +32,7 @@ Documentation/process/debugging/index.rst lkmm/index kfence kselftest + kstackwatch kunit/index ktap checkuapi diff --git a/Documentation/dev-tools/kstackwatch.rst b/Documentation/dev-to= ols/kstackwatch.rst new file mode 100644 index 000000000000..9b710b90e512 --- /dev/null +++ b/Documentation/dev-tools/kstackwatch.rst @@ -0,0 +1,377 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D +Kernel Stack Watch (KStackWatch) +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D + +Overview +=3D=3D=3D=3D=3D=3D=3D=3D + +KStackWatch is a lightweight debugging tool designed to detect kernel stack +corruption in real time. It installs a hardware breakpoint (watchpoint) at= a +function's specified offset using *kprobe.post_handler* and removes it in +*fprobe.exit_handler*. This covers the full execution window and reports +corruption immediately with time, location, and call stack. + +Main features: + +* Immediate and precise stack corruption detection +* Support for multiple concurrent watchpoints with configurable limits +* Lockless design, usable in any context +* Depth filter for recursive calls +* Low overhead of memory and CPU +* Flexible debugfs configuration with key=3Dval syntax +* Architecture support: x86_64 and arm64 +* Auto-canary detection to simplify configuration + +Performance Impact +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Runtime overhead was measured on Intel Core Ultra 5 125H @ 3 GHz running +kernel 6.17, using test4: + ++------------------------+-------------+---------+ +| Type | Time (ns) | Cycles | ++=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D=3D+ +| entry with watch | 10892 | 32620 | ++------------------------+-------------+---------+ +| entry without watch | 159 | 466 | ++------------------------+-------------+---------+ +| exit with watch | 12541 | 37556 | ++------------------------+-------------+---------+ +| exit without watch | 124 | 369 | ++------------------------+-------------+---------+ + +From a broader perspective, the overall comparison is as follows: + ++----------------------------+----------------------+---------------------= ----+ +| Mode | CPU Overhead (add) | Memory Overhead (add= ) | ++=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D+ +| Compiled but not enabled | None | ~20 B per task = | ++----------------------------+----------------------+---------------------= ----+ +| Enabled, no function hit | None | ~few hundred B = | ++----------------------------+----------------------+---------------------= ----+ +| Func hit, HWBP not toggled | ~140 ns per call | None = | ++----------------------------+----------------------+---------------------= ----+ +| Func hit, HWBP toggled | ~11=E2=80=9312 =C2=B5s per call | None = | ++----------------------------+----------------------+---------------------= ----+ + +The overhead is minimal, making KStackWatch suitable for production +environments where stack corruption is suspected but kernel rebuilds are n= ot +feasible. + +Kconfig Options +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +The following configuration options control KStackWatch builds: + +- CONFIG_KSTACKWATCH + + Builds the kernel with KStackWatch enabled. + +- CONFIG_KSTACKWATCH_PROFILING + + Measures probe runtime overhead for performance analysis and tuning. + +- CONFIG_KSTACKWATCH_TEST + + Builds a test module to validate KStackWatch functionality. + +Usage +=3D=3D=3D=3D=3D + +KStackWatch provides optional configurations for different use cases. +CONFIG_KSTACKWATCH enables real-time stack corruption detection using hard= ware breakpoints and probes. +CONFIG_KSTACKWATCH_PROFILING allows measurement of probe latency and overh= ead for performance analysis. +CONFIG_KSTACKWATCH_TEST builds a test module for validating KStackWatch fu= nctionality under controlled conditions. + +KStackWatch is configured through */sys/kernel/debug/kstackwatch/config* u= sing a +key=3Dvalue format. Both long and short forms are supported. Writing an em= pty +string disables the watch. + +.. code-block:: bash + + # long form + echo func_name=3D? func_offset=3D? ... > /sys/kernel/debug/kstackwatch/co= nfig + + # short form + echo fn=3D? fo=3D? ... > /sys/kernel/debug/kstackwatch/config + + # disable + echo > /sys/kernel/debug/kstackwatch/config + +The func_name and the func_offset where the watchpoint should be placed mu= st be +known. This information can be obtained from *objdump* or other tools. + +Required parameters +-------------------- + ++--------------+--------+-----------------------------------------+ +| Parameter | Short | Description | ++=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D+=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+ +| func_name | fn | Name of the target function | ++--------------+--------+-----------------------------------------+ +| func_offset | fo | Instruction pointer offset | ++--------------+--------+-----------------------------------------+ + +Optional parameters +-------------------- + +Default 0 and can be omitted. +Both decimal and hexadecimal are supported. + ++--------------+--------+------------------------------------------------+ +| Parameter | Short | Description | ++=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+=3D=3D=3D=3D=3D=3D=3D=3D+=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D+ +| auto_canary | ac | Automatically calculated canary sp_offset | ++--------------+--------+------------------------------------------------+ +| depth | dp | Recursion depth filter | ++--------------+--------+------------------------------------------------+ +| | | Maximum number of concurrent watchpoints | +| max_watch | mw | (default 0, capped by available hardware | +| | | breakpoints) | ++--------------+--------+------------------------------------------------+ +| panic_hit | ph | Panic system on watchpoint hit (default 0) | ++--------------+--------+------------------------------------------------+ +| sp_offset | so | Watching addr offset from stack pointer | ++--------------+--------+------------------------------------------------+ +| watch_len | wl | Watch length in bytes (1, 2, 4, 8 onX86_64) | ++--------------+--------+------------------------------------------------+ + + +Workflow Example +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Silent corruption +----------------- + +Consider *test3* in *kstackwatch_test.sh*. Run it directly: + +.. code-block:: bash + + echo test3 >/sys/kernel/debug/kstackwatch/test + +Sometimes, *test_mthread_victim()* may report as unhappy: + +.. code-block:: bash + + [ 7.807082] kstackwatch_test: victim[0][11]: unhappy buf[8]=3D0xabcdab= cd + +Its source code is: + +.. code-block:: c + + static void test_mthread_victim(int thread_id, int seq_id, u64 start_ns) + { + ulong buf[BUFFER_SIZE]; + + for (int j =3D 0; j < BUFFER_SIZE; j++) + buf[j] =3D 0xdeadbeef + seq_id; + + if (start_ns) + silent_wait_us(start_ns, VICTIM_MINIOR_WAIT_NS); + + for (int j =3D 0; j < BUFFER_SIZE; j++) { + if (buf[j] !=3D (0xdeadbeef + seq_id)) { + pr_warn("victim[%d][%d]: unhappy buf[%d]=3D0x%lx\n", + thread_id, seq_id, j, buf[j]); + return; + } + } + + pr_info("victim[%d][%d]: happy\n", thread_id, seq_id); + } + +From the source code, the report indicates buf[8] was unexpectedly modifie= d, +a case of silent corruption. + +Configuration +------------- + +Since buf[8] is the corrupted variable, the following configuration shows +how to use KStackWatch to detect its corruption. + +func_name +~~~~~~~~~~~ + +As seen, buf[8] is initialized and modified in *test_mthread_victim*\(\) , +which sets *func_name*. + +func_offset & sp_offset +~~~~~~~~~~~~~~~~~~~~~~~~~ +The watchpoint should be set after the assignment and as close as +possible, which sets *func_offset*. + +The watchpoint should be set to watch buf[8], which sets *sp_offset*. + +Use the objdump output to disassemble the function: + +.. code-block:: bash + + objdump -S --disassemble=3Dtest_mthread_victim vmlinux + +A shortened output is: + +.. code-block:: text + + static void test_mthread_victim(int thread_id, int seq_id, u64 start_ns) + { + ffffffff815cb4e0: e8 5b 9b ca ff call ffffffff81275040 <= __fentry__> + ffffffff815cb4e5: 55 push %rbp + ffffffff815cb4e6: 53 push %rbx + ffffffff815cb4e7: 48 81 ec 08 01 00 00 sub $0x108,%rsp + ffffffff815cb4ee: 89 fd mov %edi,%ebp + ffffffff815cb4f0: 89 f3 mov %esi,%ebx + ffffffff815cb4f2: 49 89 d0 mov %rdx,%r8 + ffffffff815cb4f5: 65 48 8b 05 0b cb 80 mov %gs:0x280cb0b(%rip= ),%rax # ffffffff83dd8008 <__stack_chk_guard> + ffffffff815cb4fc: 02 + ffffffff815cb4fd: 48 89 84 24 00 01 00 mov %rax,0x100(%rsp) + ffffffff815cb504: 00 + ffffffff815cb505: 31 c0 xor %eax,%eax + ulong buf[BUFFER_SIZE]; + ffffffff815cb507: 48 89 e2 mov %rsp,%rdx + ffffffff815cb50a: b9 20 00 00 00 mov $0x20,%ecx + ffffffff815cb50f: 48 89 d7 mov %rdx,%rdi + ffffffff815cb512: f3 48 ab rep stos %rax,%es:(%rdi) + + for (int j =3D 0; j < BUFFER_SIZE; j++) + ffffffff815cb515: eb 10 jmp ffffffff815cb527 <= test_mthread_victim+0x47> + buf[j] =3D 0xdeadbeef + seq_id; + ffffffff815cb517: 8d 93 ef be ad de lea -0x21524111(%rbx),= %edx + ffffffff815cb51d: 48 63 c8 movslq %eax,%rcx + ffffffff815cb520: 48 89 14 cc mov %rdx,(%rsp,%rcx,8) + ffffffff815cb524: 83 c0 01 add $0x1,%eax + ffffffff815cb527: 83 f8 1f cmp $0x1f,%eax + ffffffff815cb52a: 7e eb jle ffffffff815cb517 <= test_mthread_victim+0x37> + if (start_ns) + ffffffff815cb52c: 4d 85 c0 test %r8,%r8 + ffffffff815cb52f: 75 21 jne ffffffff815cb552 <= test_mthread_victim+0x72> + silent_wait_us(start_ns, VICTIM_MINIOR_WAIT_NS); + ... + ffffffff815cb571: 48 8b 84 24 00 01 00 mov 0x100(%rsp),%rax + ffffffff815cb579: 65 48 2b 05 87 ca 80 sub %gs:0x280ca87(%rip= ),%rax # ffffffff83dd8008 <__stack_chk_guard> + ... + ffffffff815cb5a1: eb ce jmp ffffffff815cb571 <= test_mthread_victim+0x91> + } + ffffffff815cb5a3: e8 d8 86 f1 00 call ffffffff824e3c80 <= __stack_chk_fail> + + +func_offset +^^^^^^^^^^^ + +The function begins at ffffffff815cb4e0. The *buf* array is initialized in= a loop. +The instruction storing values into the array is at ffffffff815cb520, and = the +first instruction after the loop is at ffffffff815cb52c. + +Because KStackWatch uses *kprobe.post_handler*, the watchpoint can be +set right after ffffffff815cb520. However, this will cause false positive +because the watchpoint is active before buf[8] is assigned. + +An alternative is to place the watchpoint at ffffffff815cb52c, right +after the loop. This avoids false positives but leaves a small window +for false negatives. + +In this document, ffffffff815cb52c is chosen for cleaner logs. If false +negatives are suspected, repeat the test to catch the corruption. + +The required offset is calculated from the function start: + +*func_offset* is 0x4c (ffffffff815cb52c - ffffffff815cb4e0). + +sp_offset +^^^^^^^^^^^ + +From the disassembly, the buf array is at the top of the stack, +meaning buf =3D=3D rsp. Therefore, buf[8] sits at rsp + 8 * sizeof(ulong) = =3D +rsp + 64. Thus, *sp_offset* is 64. + +Other parameters +~~~~~~~~~~~~~~~~~~ + +* *depth* is 0, as test_mthread_victim is not recursive +* *max_watch* is 0 to use all available hwbps +* *watch_len* is 8, the size of a ulong on x86_64 + +Parameters with a value of 0 can be omitted as defaults. + +Configure the watch: + +.. code-block:: bash + + echo "fn=3Dtest_mthread_victim fo=3D0x4c so=3D64 wl=3D8" > /sys/kernel/de= bug/kstackwatch/config + +Now rerun the test: + +.. code-block:: bash + + echo test3 >/sys/kernel/debug/kstackwatch/test + +The dmesg log shows: + +.. code-block:: text + + [ 7.607074] kstackwatch: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D KStackWatch: C= aught stack corruption =3D=3D=3D=3D=3D=3D=3D + [ 7.607077] kstackwatch: config fn=3Dtest_mthread_victim fo=3D0x4c so= =3D64 wl=3D8 + [ 7.607080] CPU: 2 UID: 0 PID: 347 Comm: corrupting Not tainted 6.17.0= -rc7-00022-g90270f3db80a-dirty #509 PREEMPT(voluntary) + [ 7.607083] Call Trace: + [ 7.607084] <#DB> + [ 7.607085] dump_stack_lvl+0x66/0xa0 + [ 7.607091] ksw_watch_handler.part.0+0x2b/0x60 + [ 7.607094] ksw_watch_handler+0xba/0xd0 + [ 7.607095] ? test_mthread_corrupting+0x48/0xd0 + [ 7.607097] ? kthread+0x10d/0x210 + [ 7.607099] ? ret_from_fork+0x187/0x1e0 + [ 7.607102] ? ret_from_fork_asm+0x1a/0x30 + [ 7.607105] __perf_event_overflow+0x154/0x570 + [ 7.607108] perf_bp_event+0xb4/0xc0 + [ 7.607112] ? look_up_lock_class+0x59/0x150 + [ 7.607115] hw_breakpoint_exceptions_notify+0xf7/0x110 + [ 7.607117] notifier_call_chain+0x44/0x110 + [ 7.607119] atomic_notifier_call_chain+0x5f/0x110 + [ 7.607121] notify_die+0x4c/0xb0 + [ 7.607123] exc_debug_kernel+0xaf/0x170 + [ 7.607126] asm_exc_debug+0x1e/0x40 + [ 7.607127] RIP: 0010:test_mthread_corrupting+0x48/0xd0 + [ 7.607129] Code: c7 80 0a 24 83 e8 48 f1 f1 00 48 85 c0 74 dd eb 30 b= b 00 00 00 00 eb 59 48 63 c2 48 c1 e0 03 48 03 03 be cd ab cd ab 48 89 30 <= 83> c2 01 b8 20 00 00 00 29 c8 39 d0 7f e0 48 8d 7b 10 e8 d1 86 d4 + [ 7.607130] RSP: 0018:ffffc90000acfee0 EFLAGS: 00000286 + [ 7.607132] RAX: ffffc90000a13de8 RBX: ffff888102d57580 RCX: 000000000= 0000008 + [ 7.607132] RDX: 0000000000000008 RSI: 00000000abcdabcd RDI: ffffc9000= 0acfe00 + [ 7.607133] RBP: ffff8881085bc800 R08: 0000000000000001 R09: 000000000= 0000000 + [ 7.607133] R10: 0000000000000001 R11: 0000000000000000 R12: ffff88810= 5398000 + [ 7.607134] R13: ffff8881085bc800 R14: ffffffff815cb660 R15: 000000000= 0000000 + [ 7.607134] ? __pfx_test_mthread_corrupting+0x10/0x10 + [ 7.607137] + [ 7.607138] + [ 7.607138] kthread+0x10d/0x210 + [ 7.607140] ? __pfx_kthread+0x10/0x10 + [ 7.607141] ret_from_fork+0x187/0x1e0 + [ 7.607143] ? __pfx_kthread+0x10/0x10 + [ 7.607144] ret_from_fork_asm+0x1a/0x30 + [ 7.607147] + [ 7.607147] kstackwatch: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D KStackWatch End =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D + [ 7.807082] kstackwatch_test: victim[0][11]: unhappy buf[8]=3D0xabcdab= cd + +The line ``RIP: 0010:test_mthread_corrupting+0x48/0xd0`` shows the exact +location where the corruption occurred. Now that the ``corrupting()`` func= tion has +been identified, it is straightforward to trace back to ``buggy()`` and fi= x the bug. + + +More usage examples and corruption scenarios are provided in +``kstackwatch_test.sh`` and ``mm/kstackwatch/test.c``. + +Limitations +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +* Limited by available hardware breakpoints +* Only one function can be watched at a time +* Canary search limited to 128 * sizeof(ulong) from the current stack + pointer. This is sufficient for most cases, but has three limitations: + + - If the stack frame is larger, the search may fail. + - If the function does not have a canary, the search may fail. + - If stack memory occasionally contains the same value as the canary, + it may be incorrectly matched. + + In these cases, the user can provide the canary location using + ``sp_offset``, or treat any memory in the function prologue + as the canary. --=20 2.43.0 From nobody Sun Feb 8 09:33:07 2026 Received: from mail-pj1-f51.google.com (mail-pj1-f51.google.com [209.85.216.51]) (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 731BD32936C for ; Mon, 10 Nov 2025 16:38:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792725; cv=none; b=A+4NYevjgbOMd9GA/6bByX44UFi9s11ObSUUs2Y1TOEBzMzuX9l0geEt4huxHRzN+91PuljSemn/AEHsTAeatTmcYMvqxS5OSkgfU5CDs03JIfK+NxskRVn4ThqtcFTbrbdcB1dKz/o6PbBo1hOe0NeOOk3fROPNXJi6/blm7SI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762792725; c=relaxed/simple; bh=a5MAKHVYkyHwEXpHGaXuZpG+FT9ZL54Tfmjo5gUuPh8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=W/jij/2rBw/IqoPdLNH8VcrfNiymfU8DO2LTSCMTscMDArfn+MlPb3bjCyyWEe4EOiUhSlYdDn0TqauYZFQ9tDbT8fY5VMDhDdhqVQIwJVuSWIrCKdHsV9Ip4afKpv/ABNIlZqwscyJYyYnq4A3YFSsln/1O2IWfpdQjxLqKafA= 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=Oy3MEheO; arc=none smtp.client-ip=209.85.216.51 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="Oy3MEheO" Received: by mail-pj1-f51.google.com with SMTP id 98e67ed59e1d1-3436cbb723fso1825756a91.2 for ; Mon, 10 Nov 2025 08:38:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1762792723; x=1763397523; 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=3UfdW5vQJBbw1wKbWsFdhhHcBKlPZrTKHywEKgKhNPc=; b=Oy3MEheO9RTVhtTIaZWJXn3lRH+6NC7xhwUKY1ik8GI7W8XfFF8fak/KmZVLqju6jd SVqxWM+rUUXOZlL72NaiJf7/W8KapXUzrCXzA8abISx2fIg1DWVGRXNUwOE4eluoeps7 wfLdbRMtJ8DeAK6IRiJ608OZm/ssAjBv0qrh68iiPs8v0Wv8QrRuunMNC74jtZIrtFEx 4JATCNcjHzcoaBFI2l50da8J6nHfplay/nygY+tOADL/20jRfx0RjnIq/1aqLAdIz7g2 bTas8FhLA/N1wm5RQFhCt5LteVdTAC7h4hpIQ9fCDg/YWwPIYoe4uS6EchdhkWpM1nOY nlvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1762792723; x=1763397523; 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=3UfdW5vQJBbw1wKbWsFdhhHcBKlPZrTKHywEKgKhNPc=; b=nT+1mdY01foUL6HxaoHvlGIB4XohC4ah78OS5BZzRqM2kYHxizwjJQLhABp6MAgQ+/ XhwF5LmlY7DcZ5knzhF+Nsg86Qrc5IwSsXfXHC6TvaNiNnPia5WjFB3zbFcBBKQdx/jS Q+kPftMbnfoIdvD/9k5IE6/GcrlZjaFJaQzj1Awl7s0Oiwzu9/JRsQMSMYbeMBWJ23Ir WmMkCTJB88LnfZhgAb2SZUwqXm6fbskuycbaQgwbjsB+KlQxLNMb840nV9E5B7o82auF /uSfq7Yv81AEOK+TvAzLbEDk4xnYk5ZdmyKwHAmu8F+WXwJl8Noof2HqEYaE/gFbghN/ 9eIA== X-Forwarded-Encrypted: i=1; AJvYcCUaP8Aa3vp0uT5Ps0IfMbXA9DE0L/rchtO5tLSISazEgsRJwO7HmrMD+KZQIcJcO6wUfTGfZNzA0MG2/CU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz4XQZr8ppOCVlU5mYV+pdLVSfCf03LTIkaXXp781mZ9wQYLrf3 njee5bdmVZZDudqMVm9H60gouZ54GEiamXVR6NQJHNaDoEaFeqngLkoX X-Gm-Gg: ASbGncszJQ2iGUccGS3VcCXgDstuNhemkO/And7yexUXnQLN4EI4Nzt+B7tOsLislpu s5O4oUDfD4zKRDDYeohTmUsrSzywrgzZarxeKdwhgltFEhRyVDFOb6YgB/kBK25iuP9S+kBcGcz 6JpND8R7dY0MRhV4gan4FZAQgXPix5MNZCnmk7D97VDYYTqC9MOYBHVFu1YYaN+OfWSoyejNYJa ZqLN32BJbWhOiOjcOGhW1u0jDaFTwMYzUm4i4IMYGEHFtmQbXSJxratlAufdITVcxlBVrGJ+Ctw /ZC7cZUju++gGFvsU/bP8M5tBg6Y1X7ZfZuQAQD4CBnu3d9In0NadU0gwYXZdpuUYQitB2TL1e1 8HY7vbPT3SZMo8j/5I48pl3vdoic9eEdu7BS9nitg1WlCCCO1M+pSjIlEgMdjljiuWVDxwshrnR jOaLUTmGlAdPv7IOTndGa/Fg== X-Google-Smtp-Source: AGHT+IFEhL+ivOL3mJYmk2GKHVq/7v95YCugColCvVCwbEtQDJoAIyDrMPXTzpn3mUDrpnLozW3jrA== X-Received: by 2002:a17:90b:42:b0:340:7b2e:64cc with SMTP id 98e67ed59e1d1-3436cb91daamr9486479a91.15.1762792722638; Mon, 10 Nov 2025 08:38:42 -0800 (PST) Received: from localhost ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7b0c963613esm12684061b3a.1.2025.11.10.08.38.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Nov 2025 08:38:42 -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 27/27] MAINTAINERS: add entry for KStackWatch Date: Tue, 11 Nov 2025 00:36:22 +0800 Message-ID: <20251110163634.3686676-28-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 a maintainer entry for Kernel Stack Watch. Signed-off-by: Jinchao Wang --- MAINTAINERS | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index ddecf1ef3bed..9757775de515 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13615,6 +13615,15 @@ F: Documentation/filesystems/smb/ksmbd.rst F: fs/smb/common/ F: fs/smb/server/ =20 +KERNEL STACK WATCH +M: Jinchao Wang +S: Maintained +F: Documentation/dev-tools/kstackwatch.rst +F: include/linux/kstackwatch.h +F: include/linux/kstackwatch_types.h +F: mm/kstackwatch/ +F: tools/kstackwatch/ + KERNEL UNIT TESTING FRAMEWORK (KUnit) M: Brendan Higgins M: David Gow --=20 2.43.0