From nobody Fri Oct 3 07:40:19 2025 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 3DF1022301; Thu, 4 Sep 2025 00:21:57 +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=1756945318; cv=none; b=tbyg4luVSUg45sQ3VXJHP1X/0Twr/DnW10KwmZlqbvaTp4r0nUjA+USP2MgAqYvkGvz6hAd/quSviiq3seRZbZlvNptS5Rkji6pjPP0w5vtooxcvTFseYCiHqWeul4pCaNLbv5PkfvGKatEmrBkClvTX7bwKsxmfAcC+WBh5JYM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945318; c=relaxed/simple; bh=tBX0hO0TjzB27+2RjkvnkUfPVj0tB9UkFzjloHpm/as=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BdIkyg+0GGErlFiosNC33p46nt6GXgw5DOBG+pKqGsBlXaIGAeDUctV6SLGAvzva3Cqv7sYrB2X+AK/56jF2KdAJgb5yCpcheDEVFPH4h1A9HZZOQ9PJEDVJkTuPjCDRxrmRib9PXndAAgs2a/72u60r6f6WYpPga98inMMB7Hw= 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=hk4MM2RV; 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="hk4MM2RV" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-77251d7cca6so416868b3a.3; Wed, 03 Sep 2025 17:21:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945316; x=1757550116; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=lhhrrRkb4F3PyX047v9pUxx2LHKmYVjN4mot0t9leaw=; b=hk4MM2RVduke4sUg80O/IvpaaumOREqRvweF8EpXL8ZnzF/4GAkq5VcdNa8MiYvlOx CdexzJsfy5Fpj7bNSVVj1UM1vpKFj4Ttnsc7RuqkLxZN6t2QTsvJ3sEJ+JM9p06u6UUa pVqUa8t06PkTq6dWsIaCa/hYDHWaetWdCrIvzZtmONJjW+WM7i76+yFTuQ2tMk3D1kmm apZkNn6V+yuX0aEQKwYjTFJOvFT0wqSA1Ll3skwvhzH/fWnbolBdCFJHcrqlAQJz3Idz ZUUh4OTFQz7xg3HAXddQ62NQFGxJSWOthMLkN6zSGQETSIODmOVeDoRxDG10IsDEKEnK Q9Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945316; x=1757550116; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lhhrrRkb4F3PyX047v9pUxx2LHKmYVjN4mot0t9leaw=; b=oqbLFZo8sFCNPPrlu6Lpiy59Dh7kIY7z0ODs5GElUB+zUqhEjJR8HpLDEaBZUmUmnI Ps+JXcysKmbwcmsWTPy+bRSVgdc42g4sDj9q4ZrGJ1pdQ9r92DbFn7nMExmM5VLEJcaH 2CdHxppSZRZ9c3nkfyxADJU8v9s2oMUMwIQE/5a21/UuAewm6O6RBmeoyALHhV2giFQT QEZcKi5Gs4WPLHl6ES+obv6Q1QTOgQRa4WGQVsTv/bIQwFZ6dLLqdDT2jSrBy92w9EHh vt3e/hGfhZj9PkWo6U4Q1xrXQPBcvPtAg7tpmq0AQDFbdjfA5KiWfqnhwq2NsE3nsvjO dO1g== X-Forwarded-Encrypted: i=1; AJvYcCWkKHH0k+KRuPLEwmV6cIXvkTfUa3bS5Z61n2o43g2dvjDUl6FtbZujIPPdZqprWwYvONDSetLiN5Ti7YahLsb5JquN@vger.kernel.org, AJvYcCXBVTkY2HAs9wXvXxuDb1yG9QwdAoU5dNTld8ICHsjO47D/VRmbuYQtZG+Zwwd1OGgDKytbH3XoXdZdaFzK6coo@vger.kernel.org X-Gm-Message-State: AOJu0YwpcMn2SLUHaWFm7IEnYWznsoYchix0caEaNtf+mml5kzFhRWxO WFqbWW6LPR1v9auataIAXnLfOwZDgjqekcQ9lPdk6qZt9myOWD3i0qWu X-Gm-Gg: ASbGncvUyFlzu1zWkzCuGBLfZJ+fpW2mJpN1xyRZwKK8yTdaKPL3DAVQumFRJf1wz6y 0MbGqSs72B49oc0QZvym/EFys90igqY0IDHSddHL6lVPdTyQjh1fTCwCqQL8V5IQpm2TFQFBJvN AX8QzsLUT48mwzJh036tVg10rBiNwPTyKYIx+7HpgEzRotXlHRNjqi46lsm0vLTzM6WlCQjG3A2 b14uVWjjunFdcWNwEc8ZEwAV+qpg1VzquEdcyfUzrLAjYEpvtQUc/0L7N8xSbL+HXdQgX0EVd/s 4564+2A+T2OVm3YMWJap+TkvRONg45K2eN0owDyK0WLQv4AxoHSREh+UZGLzd6TVGR8bG8CngvJ 5WgKjwNcKr/Sux8tgJclz47HGhEliIOT+C5OtZocowknud7l57rSIIuXo3U8aacA= X-Google-Smtp-Source: AGHT+IGv0Jx86DoXMDOrUyjPpfPEAsH8ifMU/xMBIpbPH5AuwQ76YE98CDe3gaBbQQj+tqsd+VcU6g== X-Received: by 2002:a05:6a00:2306:b0:76b:fd26:162f with SMTP id d2e1a72fcca58-7723e367f77mr18894886b3a.20.1756945316384; Wed, 03 Sep 2025 17:21:56 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.21.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:21:55 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 01/18] mm/ksw: add build system support Date: Thu, 4 Sep 2025 08:20:58 +0800 Message-ID: <20250904002126.1514566-2-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 for KStackWatch, a real-time tool for debugging kernel stack corruption. The implementation is located under `mm/kstackwatch/`. Signed-off-by: Jinchao Wang --- mm/Kconfig.debug | 10 ++++++++++ mm/Makefile | 1 + mm/kstackwatch/Makefile | 2 ++ mm/kstackwatch/kernel.c | 24 ++++++++++++++++++++++++ mm/kstackwatch/kstackwatch.h | 5 +++++ mm/kstackwatch/stack.c | 1 + mm/kstackwatch/watch.c | 1 + 7 files changed, 44 insertions(+) create mode 100644 mm/kstackwatch/Makefile create mode 100644 mm/kstackwatch/kernel.c create mode 100644 mm/kstackwatch/kstackwatch.h create mode 100644 mm/kstackwatch/stack.c create mode 100644 mm/kstackwatch/watch.c diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index 32b65073d0cc..c2cc8c7b595f 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -309,3 +309,13 @@ config PER_VMA_LOCK_STATS overhead in the page fault path. =20 If in doubt, say N. + +config KSTACK_WATCH + tristate "Kernel Stack Watch" + depends on STACKPROTECTOR && HAVE_HW_BREAKPOINT && KPROBES && FPROBE + help + A lightweight real-time debugging tool to detect stack corruption. + It can watch either the canary or local variable and tracks + the recursive depth of the monitored function. + + If unsure, say N. diff --git a/mm/Makefile b/mm/Makefile index ef54aa615d9d..665c9f2bf987 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_KSTACK_WATCH) +=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/Makefile b/mm/kstackwatch/Makefile new file mode 100644 index 000000000000..84a46cb9a766 --- /dev/null +++ b/mm/kstackwatch/Makefile @@ -0,0 +1,2 @@ +obj-$(CONFIG_KSTACK_WATCH) +=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..93379a0a0f7e --- /dev/null +++ b/mm/kstackwatch/kernel.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include + +MODULE_AUTHOR("Jinchao Wang"); +MODULE_DESCRIPTION("Kernel Stack Watch"); +MODULE_LICENSE("GPL"); + +static int __init kstackwatch_init(void) +{ + pr_info("KSW: module loaded\n"); + pr_info("KSW: usage:\n"); + pr_info("KSW: echo 'function+ip_offset[+depth] [local_var_offset:local_va= r_len]' > /proc/kstackwatch\n"); + + return 0; +} + +static void __exit kstackwatch_exit(void) +{ + pr_info("KSW: Module unloaded\n"); +} + +module_init(kstackwatch_init); +module_exit(kstackwatch_exit); diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h new file mode 100644 index 000000000000..0273ef478a26 --- /dev/null +++ b/mm/kstackwatch/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/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 Fri Oct 3 07:40:19 2025 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 043BC15A85A; Thu, 4 Sep 2025 00:22:05 +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=1756945327; cv=none; b=Q+atom1wMuivk5+flywvTAMPXZZUv2cHEY8BKLzPRTa/MLLZydzL40eUpVh6aLq29Y2/Oschw/celRdB5U8F2V5BI+DWjBfAGM9DT3fNOf1o1LtZAX3X7YsJm5LKQvEjqYct0b/Sk4GAqqiuYBu2eOR4dyyQecfS2sp0mzXcQLw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945327; c=relaxed/simple; bh=f7oIGMu82h0EUyi+ciPNEzqBC4KP/xyBf4hESY7E3jw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nLFiTgjOGMs3ex3cFXwha5CM2ArRVFSEafPgJyCfHpOYvxVcVvaJva5g8igX06Snxvt3tjnDNM/P4JwEvP4tr9uBqZ6jInl66K6wji76Fs+s8E5vQrraL39x4bboMjS+dklBY7mkMZ+gG4iIXw3eYKK29GCf6NIKLuQXOMuhRY4= 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=XKnRodn3; 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="XKnRodn3" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7728815e639so322502b3a.1; Wed, 03 Sep 2025 17:22:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945325; x=1757550125; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oy0C0swglM9nhMs2jJeQr2J+ATypwXUQHvq+zmKtZ8o=; b=XKnRodn3Inx8Xhhnph1zuOWsE/yd7U6UBOdI/lGMmLtfRH7erYgLHeeGNQgd5DGG1L fgBjdDVzL/9I3NzFVFTpkNEhWFHof2kz53YelkFgcx+AZ8BuIzs9xe6zxJ076CxvUYUV 0VolvCZtvNS7TspdbDF2rYF4ukO1moT1knoKkupCKjYlX1YVTXshvs5ejSoBz9CE0mDp BkvT+w22Qa3PbBT2m0WhOUCc5OeWu5fIFDdtlscHRaj/KDH3WoEydJRRT9o3IK3An6uC nzZrNai4gV5ZludOPOAhIAJondmZ30n8w8JXg4nQ3fqQQHd4BxrVJoVsF86h7of/jinV rTlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945325; x=1757550125; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oy0C0swglM9nhMs2jJeQr2J+ATypwXUQHvq+zmKtZ8o=; b=cwpWnchepOYTYoh6P6cPPv68XBo/NWdxQyZhIoSAJvsz8vSCFD1AqOK8UQ0n87NW9K fFYcyWv40YRC+0NxQSEU5M03KCZxAHX4wSnM12jxHzePhzHZ+c0av4yVL0snmHYSKezX nJAPh3UfQb5Lsq0A011eCCS5dGUSjyzICCkZFrauIpYz3XaB01d5JwbuZ0GZiwr0WJ1e ABlIpqlXS1Tsinlslg4LDPa9jk3ikBqy0GnHAebF8m1/AWi6isVfm8u2LhsXPajlL75K woZapPEtBzjs68fosRitKbRsIVeVzTySMPqrC83IRsLEql0mDuOSQM5xomlXTArqAyue jo/Q== X-Forwarded-Encrypted: i=1; AJvYcCVgG3amTAGU9x1d7CyUTitGqDplIsJxRU6QvNYvnbL028JsXM/YhXltkqfKLaWGhqgeRbxtHVZJTDCDXl1CYlWDwld9@vger.kernel.org, AJvYcCWuOuTqaEbTCf8ttZRQjW/my2zApS98IyaGyKJLoiKamSIFxd/tDmi4xuQtHGpCrqwhAWeBNajKUKykBRDui3S8@vger.kernel.org X-Gm-Message-State: AOJu0Ywd7CajxHjqT6JmnW7V22iMgchyrRpJcQsbnQGEKCfB8/fHfIZe wHsRUzOJhEj1o9ZLt/wuDwo6VBCsgWYlcyclCbLWib0Dv0TfqBbTOvNx X-Gm-Gg: ASbGncswigQvUh8mLuP+Ox0LsfOEDY2sVTFr2l4T2E3JpvZ8lk2bduZ32GzG+kiiwIg WT42x2kq4X5TKUhxfiWDc5PfUFFpxX/MlVUBGTuFaT02LCOn3c9cQWkALiTZL067GYn7kudMAKA 2QTrJuUHNWHqs5CZizMNWOKZqKvi0yNPx009FOXgQYB+dcpR1lE4UkYEZleoNVs1bHvQTk3GZHq HiYAXt0vzavWVy8CfAlySCj4DskbAVIWUhGYFBvBEiqBHTc6CSfhbHT37bS8lQElDw1/B7UeG4V UMn6sPvcpnVaxlOvp76hNPIwzs9dDXcN1cJx0hvEpSnsIDOwq8BZNbcpLv+qRxSioV7jTpkeK1d olqxg/jGU/gSwkyM4WJ7GwAVW71dQBNAz9ZAWhXtJI1ZDwOrYpo6km1msw/1bgxk= X-Google-Smtp-Source: AGHT+IFybxLzi3RhG8G6E0XW25a7NBfoJQUABi3RBwpHwqaPA2H33RcEs3ZAByeGLnEmBrGiX7iCMw== X-Received: by 2002:a05:6a20:4c8f:b0:244:58:c147 with SMTP id adf61e73a8af0-2440058c3f7mr9480142637.21.1756945325307; Wed, 03 Sep 2025 17:22:05 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.21.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:22:04 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 02/18] mm/ksw: add ksw_config struct and parser Date: Thu, 4 Sep 2025 08:20:59 +0800 Message-ID: <20250904002126.1514566-3-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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. Update `Makefile` to pass compilation. Signed-off-by: Jinchao Wang --- mm/kstackwatch/Makefile | 2 ++ mm/kstackwatch/kernel.c | 70 +++++++++++++++++++++++++++++++++++- mm/kstackwatch/kstackwatch.h | 34 ++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/Makefile b/mm/kstackwatch/Makefile index 84a46cb9a766..d422f0e114dd 100644 --- a/mm/kstackwatch/Makefile +++ b/mm/kstackwatch/Makefile @@ -1,2 +1,4 @@ obj-$(CONFIG_KSTACK_WATCH) +=3D kstackwatch.o kstackwatch-y :=3D kernel.o stack.o watch.o + +CFLAGS_kernel.o :=3D -Wno-error=3Dunused-function diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 93379a0a0f7e..4a6dc49449fe 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -1,11 +1,79 @@ // SPDX-License-Identifier: GPL-2.0 - +#include #include +#include + +#include "kstackwatch.h" =20 MODULE_AUTHOR("Jinchao Wang"); MODULE_DESCRIPTION("Kernel Stack Watch"); MODULE_LICENSE("GPL"); =20 +/* + * Format of the configuration string: + * function+ip_offset[+depth] [local_var_offset:local_var_len] + * + * - function : name of the target function + * - ip_offset : instruction pointer offset within the function + * - depth : recursion depth to watch + * - local_var_offset : offset from the stack pointer at function+ip_offset + * - local_var_len : length of the local variable + */ +static int ksw_parse_config(char *buf, struct ksw_config *config) +{ + char *func_part, *local_var_part =3D NULL; + char *token; + + /* Set the watch type to the default canary-based monitoring */ + config->type =3D WATCH_CANARY; + + func_part =3D strim(buf); + strscpy(config->config_str, func_part, MAX_CONFIG_STR_LEN); + + local_var_part =3D strchr(func_part, ' '); + if (local_var_part) { + *local_var_part =3D '\0'; // Terminate the function part + local_var_part =3D strim(local_var_part + 1); + } + + /* 1. Parse the function part: function+ip_offset[+depth] */ + token =3D strsep(&func_part, "+"); + if (!token) + return -EINVAL; + + strscpy(config->function, token, MAX_FUNC_NAME_LEN - 1); + + token =3D strsep(&func_part, "+"); + if (!token || kstrtou16(token, 0, &config->ip_offset)) { + pr_err("KSW: failed to parse instruction offset\n"); + return -EINVAL; + } + + token =3D strsep(&func_part, "+"); + if (token && kstrtou16(token, 0, &config->depth)) { + pr_err("KSW: failed to parse depth\n"); + return -EINVAL; + } + if (!local_var_part || !(*local_var_part)) + return 0; + + /* 2. Parse the optional local var: offset:len */ + config->type =3D WATCH_LOCAL_VAR; + token =3D strsep(&local_var_part, ":"); + if (!token || kstrtou16(token, 0, &config->local_var_offset)) { + pr_err("KSW: failed to parse stack variable offset\n"); + return -EINVAL; + } + + if (!local_var_part || + kstrtou16(local_var_part, 0, &config->local_var_len)) { + pr_err("KSW: failed to parse stack variable length\n"); + return -EINVAL; + } + + return 0; +} + static int __init kstackwatch_init(void) { pr_info("KSW: module loaded\n"); diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index 0273ef478a26..b5f1835586c1 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -2,4 +2,38 @@ #ifndef _KSTACKWATCH_H #define _KSTACKWATCH_H =20 +#include + +#define MAX_FUNC_NAME_LEN 64 +#define MAX_CONFIG_STR_LEN 128 +#define MAX_FRAME_SEARCH 128 + +enum watch_type { + WATCH_CANARY =3D 0, + WATCH_LOCAL_VAR, +}; + +struct ksw_config { + /* function part */ + char function[MAX_FUNC_NAME_LEN]; + u16 ip_offset; + u16 depth; + + /* local var, useless for canary watch */ + /* offset from rsp at function+ip_offset */ + u16 local_var_offset; + + /* + * local var size (1,2,4,8 bytes) + * it will be the watching len + */ + u16 local_var_len; + + /* easy for understand*/ + enum watch_type type; + + /* save to show */ + char config_str[MAX_CONFIG_STR_LEN]; +}; + #endif /* _KSTACKWATCH_H */ --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 EF0CD1E572F; Thu, 4 Sep 2025 00:22:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945339; cv=none; b=S48TgCAErWtfawDxaQLj9/epKwURjWWX/pOvl2Mc1ejm/jRDT72epe7K0m0V1wT26pe1XDp1fPaIF8WIRgJz+tOVJ8G+ZqB6rlX6ddEQpHSfDWwyAdXSGjcO4ls7gN2JBJrqgoSS5lS5woV/1tnM0KXNn0sybyeWPn10ypKvt9k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945339; c=relaxed/simple; bh=riKBu2I/0IniTUQEc51qBXh7Lfn5NQ94RTpD6hcQAWo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PiyFTfkv+WZ7j91ybQ/PouHGs++StGODxKtDgq2AmKEYkdT3u+d8GVwzO0ESzCMaTF7CI8Yhhl5DxHdbFd5Gw1TJQ9LqJEn/h4N5fWwK9hKuEgvca70CTw6/Uon5hBJEyB/VdF5lg4dQwLBjNra0nQz0gDBN8KjdUGZWf0wQ7gE= 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=fLcVEcb4; arc=none smtp.client-ip=209.85.210.172 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="fLcVEcb4" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-77238cb3cbbso541029b3a.0; Wed, 03 Sep 2025 17:22:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945335; x=1757550135; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PVjzrm6vJvhOYeF4QMjv0eGIgiL9m2bJu49WtrTbuMs=; b=fLcVEcb4hmRqAYinYJs00rm4cHOjbmjQHg3XOpEP6YqDNNn68irnZAlkopy/NRat4i YiIRwOvzbWjyVcSo3aQSpjNWUveOoQXTnbXhKHjCMzSqOgENKss3l15bt2VGBFtiDiiI 7R40czyEnIrE64HHLaSS7NKG4P86N7eZq+YV5+TnVMOB/X+4OyKe2mMQmwHwY5MGVND4 NwAGhJFUVnUxAs8b5Q5fFLQ4E75/+qf9IKaC9XDWoG241A2HiQG4SIj0Y38rBiTB9Woo zdwaSHzRjGfkZI5M21WWDJjwbsS89e242Hw5g8mmOrx9dykXnsPGCJYnkABn5nVX/FVm ikmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945335; x=1757550135; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PVjzrm6vJvhOYeF4QMjv0eGIgiL9m2bJu49WtrTbuMs=; b=kZJFpNLLtU0dmZ5gAXC1Qhl4bITbXnCx/AcO1B4o0I9zrjONwug+S01C4JYO+/jIJs SJuFm9RMNbLPSB4kFtyDSq+fxGytlQ9LLq6br/5X/iS1E115IWosQAbxY7YqiPHIejFk pNOkCn5UfJGzZTQKIv3LZ4TF9AVBuVSaSTVcYBwclfHgcBrb8eLTHgFdDJXrc/p4vOOY 2Ia2ZUUG2E1ssnu5DPwzcLocohXwwlOgwAZUZVwLRbRnVBTjCM4vpNrBgnvMi3x4u+VK 06Suw8OLBGSUCd0hFu7kaRcu/soPtAzGpS0F43QUt5J9kiiuZI9RkABHPFQkLiC05aLn 4yyQ== X-Forwarded-Encrypted: i=1; AJvYcCWSu2hIiut4MevFShFxMjjmsLK1yPsMU32plPZUYOs/5pEX8icncryhqt9DVJcpQ6ryl7xGpHNE0pJlBTeyjPUa@vger.kernel.org, AJvYcCWi7HL1rf26SGrOtK4mDoWZFHBDKNJLHVqhiVKZCdC4rdBsryl08mog9vpbHI7J0tGCBaSJwNypn1xZkAAltW80Q9+m@vger.kernel.org X-Gm-Message-State: AOJu0YwHFjhNnwUbkcVZ4v0MB5LLr2/m2RFvAZw1ekkYA8OIsz4NSsrN yWr+HGvz+D0FghAwkXCLDIkLDl5KG3Jsfe0J4Nk1jeqzfeT5jXEnidUz X-Gm-Gg: ASbGnctPUykwC3zfwTDXuPt025zcMe2akrU7WJnIs6CxCjeGpLv60b+07ZjZ75XegUs 6c/1koSd3svJGIksAS1il7BSd7STVzG9ODg/4oX9KKNqMcRdq8RHIODbUXMPUnBNe6Q98we8Mml NSyB5XM6F3LD88InF+yaqdD1HnCYS1ucqLtdF2sw16tmvFqxqXOIJAoX68g8qdWyp/bfaMXdC3L V8AH0zUDRKczVB40RU5Jef6sM+JA2m/NwDnkSg97qn7a8A0OaLWk43B70p1f9lA0GHmyfMZ+pTM lCT9nh2/8WKZG+GB2z2N6zIBeh2TOeNUPnKtQRVkl11mLeYI0Usi0AtTkQG6hNZ1HotDC5tym1C oXzH9vGU4c90sDr/8tx1ETpVkmOVvZaBuUAZRd0xA6sl9VaIsppZU X-Google-Smtp-Source: AGHT+IHi/o/IAm0vynFn2e/IsWAicKmyi78tP6aMpCX3iR0UzR1wOUQxX5ciC3ig3wFrcV6Iy1bIqQ== X-Received: by 2002:a05:6a20:7484:b0:243:9312:4fb4 with SMTP id adf61e73a8af0-243d6f86b99mr26405251637.52.1756945334920; Wed, 03 Sep 2025 17:22:14 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.22.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:22:14 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 03/18] mm/ksw: add /proc/kstackwatch interface Date: Thu, 4 Sep 2025 08:21:00 +0800 Message-ID: <20250904002126.1514566-4-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 /proc/kstackwatch file to read or update the KSW configuration. Writing a valid config string starts watching; empty input stops watching. Invalid input stops watching and reports an error. Allocate a ksw_config struct on module init and free it on exit. Manage watching state with ksw_start_watching() and ksw_stop_watching(). Signed-off-by: Jinchao Wang --- mm/kstackwatch/kernel.c | 140 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 4a6dc49449fe..95ade95abde1 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -1,7 +1,10 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include +#include #include +#include =20 #include "kstackwatch.h" =20 @@ -9,6 +12,29 @@ MODULE_AUTHOR("Jinchao Wang"); MODULE_DESCRIPTION("Kernel Stack Watch"); MODULE_LICENSE("GPL"); =20 +struct ksw_config *ksw_config; +bool watching_active; + +bool panic_on_catch; +module_param(panic_on_catch, bool, 0644); +MODULE_PARM_DESC(panic_on_catch, + "Trigger a kernel panic immediately on corruption catch"); + +static int ksw_start_watching(void) +{ + watching_active =3D true; + + pr_info("KSW: start watching %s\n", ksw_config->config_str); + return 0; +} + +static void ksw_stop_watching(void) +{ + watching_active =3D false; + + pr_info("KSW: stop watching %s\n", ksw_config->config_str); +} + /* * Format of the configuration string: * function+ip_offset[+depth] [local_var_offset:local_var_len] @@ -74,8 +100,114 @@ static int ksw_parse_config(char *buf, struct ksw_conf= ig *config) return 0; } =20 +/** + * kstackwatch_proc_write - Handle writes to the /proc/kstackwatch file + * @file: file struct for the proc entry + * @buffer: user-space buffer containing the command string + * @count: the number of bytes from the user-space buffer + * @pos: file offset + * + * This function processes user input to control the kernel stack watcher + * Before attempting to process any new configuration. It handles three + * cases based on the input string, In all three cases, the system will + * clear its state first, with subsequent actions determined by the input: + * + * 1. An empty or whitespace-only string + * Return a success code + * + * 2. An invalid configuration string + * Return a error code + * + * 3. A valid configuration string + * Start a new stack watch, return a success code + * + * Return: The number of bytes successfully processed on success, + * or a negative error code on failure. + */ +static ssize_t kstackwatch_proc_write(struct file *file, + const char __user *buffer, size_t count, + loff_t *pos) +{ + 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; + + if (watching_active) + ksw_stop_watching(); + memset(ksw_config, 0, sizeof(*ksw_config)); + + input[count] =3D '\0'; + strim(input); + + /* case 1 */ + if (!strlen(input)) { + pr_info("KSW: config cleared\n"); + return count; + } + + ret =3D ksw_parse_config(input, ksw_config); + if (ret) { + /* case 2 */ + pr_err("KSW: Failed to parse config %d\n", ret); + return ret; + } + + /* case 3 */ + ret =3D ksw_start_watching(); + if (ret) { + pr_err("KSW: Failed to start watching with %d\n", ret); + return ret; + } + + return count; +} + +static int kstackwatch_proc_show(struct seq_file *m, void *v) +{ + if (watching_active) { + seq_printf(m, "%s\n", ksw_config->config_str); + } else { + seq_puts(m, "not watching\n"); + seq_puts(m, "\nusage:\n"); + seq_puts( + m, + " echo 'function+ip_offset[+depth] [local_var_offset:local_var_len]' > = /proc/kstackwatch\n"); + seq_puts(m, " Watch the canary without the local var part."); + } + + return 0; +} + +static int kstackwatch_proc_open(struct inode *inode, struct file *file) +{ + return single_open(file, kstackwatch_proc_show, NULL); +} + +static const struct proc_ops kstackwatch_proc_ops =3D { + .proc_open =3D kstackwatch_proc_open, + .proc_read =3D seq_read, + .proc_write =3D kstackwatch_proc_write, + .proc_lseek =3D seq_lseek, + .proc_release =3D single_release, +}; + static int __init kstackwatch_init(void) { + ksw_config =3D kmalloc(sizeof(*ksw_config), GFP_KERNEL); + if (!ksw_config) + return -ENOMEM; + + /* Create proc interface */ + if (!proc_create("kstackwatch", 0644, NULL, &kstackwatch_proc_ops)) { + pr_err("KSW: create proc kstackwatch fail"); + return -ENOMEM; + } + pr_info("KSW: module loaded\n"); pr_info("KSW: usage:\n"); pr_info("KSW: echo 'function+ip_offset[+depth] [local_var_offset:local_va= r_len]' > /proc/kstackwatch\n"); @@ -85,6 +217,14 @@ static int __init kstackwatch_init(void) =20 static void __exit kstackwatch_exit(void) { + /* Cleanup active watching */ + if (watching_active) + ksw_stop_watching(); + + /* Remove proc interface */ + remove_proc_entry("kstackwatch", NULL); + kfree(ksw_config); + pr_info("KSW: Module unloaded\n"); } =20 --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.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 DE7591F463E; Thu, 4 Sep 2025 00:22:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945346; cv=none; b=XJSuWUgsyyzbIbaoO/ctamBPK6LttzP8xPEQTQo9kTJa3sj42nVpnOgZkEO3m51LjnnTHYVg5Pt8+tMZDvtpvzkwSN+tyKyHt+jNc3Nx5I6CvN0wvwUFgKo3fCDARSWAl0LRwAOf9xL6bOVC55SiPL2g6GGiQVMH5MDCIUWpnqk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945346; c=relaxed/simple; bh=rufIEWmrAKLQ+mfcV5W+4Uljip8z5lF1CAVZ2QbsVZM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L97Tz/g9drV9s4Sb+2+3cBPk9i4HrDCGhMr3AQjHwcrl54YK00BrgIBKG2uyESobApM31R004jUSvHj40bdtZn89Vpti8E5I9S4NLSKsXoXAk/OdpmQAbJBTs9q/s8J+qo4oohWz32C9eDkqg4vPMMH2TKqq6zFpAHsVYVePuQA= 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=Qfn2DBT7; arc=none smtp.client-ip=209.85.210.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="Qfn2DBT7" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-772679eb358so457913b3a.1; Wed, 03 Sep 2025 17:22:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945344; x=1757550144; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=06EnX4xNHqiA4W3dBLh6+8AEySVO628YRMoxqtZgpzs=; b=Qfn2DBT7tGKU/jMAOByFs5RbHc+5ZuO+R3AAz+o6SHB13T89X8M+FKEoZtUFeOzp1/ lwhI6+7q4d7hrSmAdQiebpYipXwQySd0Ed7h/8SlB26Qp3ZH/4K9/f1qCRasatzDZLfi DT7sws1cF8q5zftglMTt9U/5n9eu1K7NExcA2Q/7HvehV1oUOF9R3UYELk34Ybc5ZtnB sI9pWYXe7lgkBOSDIOrFHbqR97BMJDruaXv8Be74K0nmzoaAklFV0ds1zouVby6IX73O yFvOcHrl1jIPyyH8j34lBPxKCeGdXzn54Kj7HwiRjHtBGKOTL/XjxNhSUdbiYsTG/YuQ k6GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945344; x=1757550144; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=06EnX4xNHqiA4W3dBLh6+8AEySVO628YRMoxqtZgpzs=; b=kd76rJNwSk6aK6w8/WnTO0iPK3TfU/aQWeot/HDqkxAp8k61sLiiOWhcvgVTGgEQD2 SE668FSgYFVvrlelGXAI6qekP+yC3zA1iYvLSgGPDH0ktDWWqU7rguK8NfWLW6BDDdMM NVVu/B8b+sjcFzpoNGiy4zbyZtkqbe7P/amIJltOdhCeNJlfQKdWVFfpsMduvheFb3jF CEN+1RByftZqTjsCQx3OpllDdw0UYOkioQ0vucwyfzHjyNvtkJAzmwg0qEGLq2P8E79N eEXkqzop76DvfvVTImc92BqJOZnVaisjgetSCynpNGL7mSbQNhNdiLcr1yEaRKx8CAfj Htug== X-Forwarded-Encrypted: i=1; AJvYcCWbkDCVsDpo3ixEEZdVjDktqQWAAlYFg2B0CKxm2GHQVO+GiP5/jNsCnpwY+fcffqdBnbxsTHeNP7COrfDK2lQB@vger.kernel.org, AJvYcCX3bLEUuKREjXNUKIfbMiBHl1ppsN/MD6e0ghH6aLO7at4DEQ6fqQ68elPjmBMxZQ9zaQvj5G0/4572UPrI3hMbT+7f@vger.kernel.org X-Gm-Message-State: AOJu0Yxfix3Q8ivdCUGlzHZeup2RATmK3yBAbM+CJgfiDKqLQ+YFKoD/ QOmLZcOMY0cyQQ3NlJyXr6jIJEWy0TJ9QBv1/RvSzI4TERJ3GeT/kT2u X-Gm-Gg: ASbGncu7G27gz5awP1zPUi5YVwwm0L4igp7gR9n4g1pkqLEeFhaBDcE6BNIQFTB0Icl TngOAH0YkWTgdAMkTZrm6xqt+u+f8ix2xE94ZinTEsRlfjgL1h1dD56t2QHMkRJQQSs0gd10Olp deSrFOOhVO64zA/iX8lq/Lb26s0PcbCDzREItLqjx8p0/YHF/06eMHzW+JeIGMzs/0o0Hoz4RxB Or85kfiqQczNezyDbWHxpi7Yqip4jbkSMtP2UL92QTp1yjunhYeIZdgSVLnBciL5lkVgtWjR9SZ t02jQPLgt2wbw17WEb4wzpa4xHO6eIIC0MZZV1OqTw5pWwT9CaM54Sr0nTBBHuRisNLYvtROH40 33APKsZgilEHmuh0Jv/wbTrz4hiKB67c/CzLkKClWcktt1J8A63FMzjMn5drqReo= X-Google-Smtp-Source: AGHT+IEA5bFflP+ewlk1s4zRmV7WjxdLsMy01rCTDV7VoKPhFZJggfHFuKygnu201Hh4P+tgmpwDhg== X-Received: by 2002:a05:6a00:b84:b0:772:1a18:b8d0 with SMTP id d2e1a72fcca58-7723e3d1d5bmr16156720b3a.30.1756945344084; Wed, 03 Sep 2025 17:22:24 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.22.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:22:23 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 04/18] mm/ksw: add HWBP pre-allocation support Date: Thu, 4 Sep 2025 08:21:01 +0800 Message-ID: <20250904002126.1514566-5-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 dummy address, which will be retargeted dynamically in kprobe handler. This avoids allocation in atomic contexts. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kstackwatch.h | 6 ++++ mm/kstackwatch/watch.c | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index b5f1835586c1..2318779bde70 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -36,4 +36,10 @@ struct ksw_config { char config_str[MAX_CONFIG_STR_LEN]; }; =20 +extern bool panic_on_catch; + +/* watch management */ +int ksw_watch_init(struct ksw_config *config); +void ksw_watch_exit(void); + #endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index cec594032515..e7ed88700b49 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -1 +1,63 @@ // SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kstackwatch.h" + +#define MAX_STACK_ENTRIES 64 + +struct perf_event *__percpu *watch_events; +struct ksw_config *watch_config; + +static unsigned long long watch_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("KSW: config %s\n", watch_config->config_str); + show_regs(regs); + 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\n"); + + if (panic_on_catch) + panic("KSW: Stack corruption detected"); +} + +int ksw_watch_init(struct ksw_config *config) +{ + struct perf_event_attr attr; + + hw_breakpoint_init(&attr); + attr.bp_addr =3D (unsigned long)&watch_holder; + attr.bp_len =3D HW_BREAKPOINT_LEN_8; + attr.bp_type =3D HW_BREAKPOINT_W; + watch_events =3D + register_wide_hw_breakpoint(&attr, ksw_watch_handler, NULL); + if (IS_ERR((void *)watch_events)) { + int ret =3D PTR_ERR((void *)watch_events); + + pr_err("KSW: failed to register wide hw breakpoint: %d\n", ret); + return ret; + } + + watch_config =3D config; + pr_info("KSW: watch inited\n"); + return 0; +} + +void ksw_watch_exit(void) +{ + unregister_wide_hw_breakpoint(watch_events); + watch_events =3D NULL; + + pr_info("KSW: watch exited\n"); +} --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.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 E6E4A1F463E; Thu, 4 Sep 2025 00:22:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945355; cv=none; b=gM/YH3OJwxI3/Ns5zq3+R9oWt/f5RL91HFSVJSiqhFOZG/cIK3g+m5tvG/Cq61h/BZ4DnixKcG+4+2kch0z0N8tmOqIjFwWz/ZKisj82ebxv8Sygm2Hyiy0tP/cuYgiOeayek6LdAhnDPaTeKVEhzllrPARl+Tdrkez/MNghuq8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945355; c=relaxed/simple; bh=4PtaQ74SJJPVLMYDACBxzq/zXR+p3d86HHwkF6eNxxw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F/wuLWUM9C/cjMf2013FSGBQHLfbsEEDKJrel/bw5ze+A5mkRWMyyYfLerp0cL/Xn5FXyAS1cxRv5xVhGlPdDbFDK6BotKuxLp6MLIv6h0CWRyTiCGYEc/fygkYaYcgy1ua2AcerkMt2x8vgIEReAxyBgrM+73EAPpErIjmZkRo= 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=CdC6EDhV; arc=none smtp.client-ip=209.85.210.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="CdC6EDhV" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7723bf02181so393299b3a.1; Wed, 03 Sep 2025 17:22:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945353; x=1757550153; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BQYOiNSD0BVFYt8LJn6Fepybq7p8FLHlj9qtf7JwQoI=; b=CdC6EDhVaq8dKEnHrDsIaN5HLCEXFcNu+/mlDdxX+oSuuUQLvJtIJuQei/erOwbhK5 2vJOR6wk72/0HDLjYp2+ePrLXhHMVfIkU7pU/2tmZcb+JwkBifxV7Em8v4i1CPARGQKG vi7cv+psHTEzFnVWMFp+5DFnhWEUiWCIuGvcsH3FmaLusoPtW6bnR980BmhhyUJH23Lh D6wSxM/3u+VMMa9bVAqfx9v0W1zLMcKawAIDVIfNFFeGQqqhdQAdXsO2KSBS/n4ROSiT fYTcQ8lyVRBRTmDJqLGzIwbo6s0hBKiuzXXdWRBBorLAGxeaMVMHTWm3FL6WGreAzfQl NksA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945353; x=1757550153; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BQYOiNSD0BVFYt8LJn6Fepybq7p8FLHlj9qtf7JwQoI=; b=SuF4qzJsr7r+65fCWlRTu/ePnHL7kVnNW0XZ5k4Mm/S1isiolp+VcUXdwga8MYVuZo aCFmtUZHhwHf+nw8UyM3dtovAsA082sSTeVeqnfHBIj+uF30i9TYeGqcTtNCTuxqpJRn d2RstZ+/7KQV56EiT9HoSUpLAPbqSovfegYPt+PY6r/4tkzlOesxrv5R0/xUL1QoYclO HtbiD9pky3PPHK39FmXFkpif2hQMfDI9xptzWSphhlNOjg2Fu90V/Opsx2SaHBB62yxm ykqU6bEuQNEdPljV46OPLBeSiwIjStFJvIxOYbVbUFgoqhiCqDMkaE4oJ6dFJKnULzXo I/Mw== X-Forwarded-Encrypted: i=1; AJvYcCV2W39VzQCfFOIWuwDgrsLlwh2tKXgT0rAfUfsowxoD5gZgKhNGWWGWIRLn5ema0jaC81epys7iJdrcjn/Jr+vv@vger.kernel.org, AJvYcCWyJSa45qTnOn0kVW1pRs+DKbLDDmNn20ywp9HkLAxlS5wSkkTC33d0wAWlnkpPDljsxfQO0psuvN/cKehLHO0gkun1@vger.kernel.org X-Gm-Message-State: AOJu0Yw65vsrDidC4aoVV4dIXAgUcI2/oykDqXYKam65AHq9cTN389rf IHY8LtW64M9jM+4Va+GEL33JEIjp0blo9T8QKFsqVADhbK/x5A3WW6HS X-Gm-Gg: ASbGncvd2sU7g7Y+RL/5LOEi2dqIqcbyDcCN2MiVg/kRkuRXy8p0KykbsT0PHUZnvlT AdL8CiyyEFAp0sFowkcdSbp7xWmRGYXm6HrFsvxRALYJUWj8kh9o8mpq3cVoJxchQVZb4z963J4 4ydnHiQSo4S7RWqUnL8s9fVqhLKBu2HxtRuhUPjeiigkJbNMH2nOJWPE0IL7uNLErNPbrsezUhS 69dhd6IDw4XVepJuEz9sonkM/etgFJqP1nTJWX368MMqzEoLKDpXpgvEuJa96qfWLxR5Zp7/cLy lzo/9a5qfZq+I+Ja5DFzNZivXJozugnLbUMow0aepJWEX7AyjJnyDqZs3nStzvsAUe4jA6//OBi NTlJisXawCgjmVkJ+zXOGqiRus0J3bWDRPSvaVyDJMalz6vYT8Cp41p4EX1U8FI4= X-Google-Smtp-Source: AGHT+IFnjpaYI5g8ja51Lvpi7nBlJrRrUarHPHHsCIaBV04TNusHvcfyUMtMGsdyArLqGzZZyKE3fA== X-Received: by 2002:a05:6a20:918a:b0:240:1132:6f0 with SMTP id adf61e73a8af0-243d6f3801dmr25287454637.43.1756945353145; Wed, 03 Sep 2025 17:22:33 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.22.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:22:32 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 05/18] x86/hw_breakpoint: introduce arch_reinstall_hw_breakpoint() for atomic context Date: Thu, 4 Sep 2025 08:21:02 +0800 Message-ID: <20250904002126.1514566-6-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 arch_reinstall_hw_breakpoint() to update hardware breakpoint parameters (address, length, type) without freeing and reallocating the debug register slot. This allows atomic updates in contexts where memory allocation is not permitted, such as kprobe handlers. Signed-off-by: Jinchao Wang --- arch/x86/include/asm/hw_breakpoint.h | 1 + arch/x86/kernel/hw_breakpoint.c | 50 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/arch/x86/include/asm/hw_breakpoint.h b/arch/x86/include/asm/hw= _breakpoint.h index 0bc931cd0698..bb7c70ad22fe 100644 --- a/arch/x86/include/asm/hw_breakpoint.h +++ b/arch/x86/include/asm/hw_breakpoint.h @@ -59,6 +59,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 b01644c949b2..89135229ed21 100644 --- a/arch/x86/kernel/hw_breakpoint.c +++ b/arch/x86/kernel/hw_breakpoint.c @@ -132,6 +132,56 @@ int arch_install_hw_breakpoint(struct perf_event *bp) return 0; } =20 +/* + * Reinstall a hardware breakpoint on the current CPU. + * + * This function is used to re-establish a perf counter hardware breakpoin= t. + * It finds the debug address register slot previously allocated for the + * breakpoint and re-enables it by writing the address to the debug regist= er + * and setting the corresponding bits in the debug control register (DR7). + * + * It is expected that the breakpoint's event context lock is already held + * and interrupts are disabled, ensuring atomicity and safety from other + * event handlers. + */ +int arch_reinstall_hw_breakpoint(struct perf_event *bp) +{ + struct arch_hw_breakpoint *info =3D counter_arch_bp(bp); + unsigned long *dr7; + int i; + + lockdep_assert_irqs_disabled(); + + 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) + break; + } + + if (WARN_ONCE(i =3D=3D HBP_NUM, "Can't find a matching breakpoint slot")) + return -EINVAL; + + set_debugreg(info->address, i); + __this_cpu_write(cpu_debugreg[i], info->address); + + dr7 =3D this_cpu_ptr(&cpu_dr7); + *dr7 |=3D encode_dr7(i, info->len, info->type); + + /* + * 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. + */ + barrier(); + + set_debugreg(*dr7, 7); + if (info->mask) + amd_set_dr_addr_mask(info->mask, i); + + return 0; +} +EXPORT_SYMBOL_GPL(arch_reinstall_hw_breakpoint); + /* * Uninstall the breakpoint contained in the given counter. * --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 20BB01A9FBD; Thu, 4 Sep 2025 00:22:42 +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=1756945364; cv=none; b=Fz6okgyhHHcOKKYv+AOaZ8Kh6W1QAcCTTeEWAC6lZMZFleFfwVCg44o9i1femvsmAcUpOnxX5AT/ie10CaQgSOtc2tNE+f5Oj8STe20k4U3m086i8Ud2ZSx/Rj8GOx9EA1gCiTf5keKCor5wesQrlOMAk9VzGS4HGP/dRkvqCFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945364; c=relaxed/simple; bh=3124CTpWX2NmJyzDqaFaXtKC+brHOzNaUuRC4z7F+n8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R2TnMi8sofcIR0CRpq8hFYJ0qdL8U/TNce9rbU+Nl80XCBERxGG/G6aLx5lrQuzwRafKV9gV3imezH1ip/YhTzdXT5taXLUUYVXiZg0iIgFJ6ZS27gAJVBZH8yWIFuPbxeeE+AfFqlBze5qEfa6kQ4QtS7WMn7xQDpLkG2pweOI= 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=C4j3V4VV; 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="C4j3V4VV" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7723cf6e4b6so412832b3a.3; Wed, 03 Sep 2025 17:22:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945362; x=1757550162; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=U2npOZMsLAhal6oQPysy/cOAT94aswbe5KhIEvIg4+s=; b=C4j3V4VVyI8nYcTazdtI6abdS9eZe1ME8HGGbTVhiQFdlgqbusno0pWmxPdyajjyFZ uhhy7v+hcXtOfDgWt5DMrfbZJIiCnE/N5hq7yy1BMo9uucml+cLmgy+WXB0h+V27/KQ6 OviTF25WyU76drp/so/BlntGrUH8HJHNfbHeblFBR+AYmn6SShaZNneTnch4DZdiMy4w G9FtJzYmVP+i/pcX753oHE5p97SAoameQmjb9Aenmu6JIQ/X0DfvjTsuYuPh/KKvhXaH diDQ9rrjyivjnMZtD9v4GbjW4HKU9fkKUJl1JOUwwfIfpUSryGUXqp252tk46KL2S94F kvCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945362; x=1757550162; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U2npOZMsLAhal6oQPysy/cOAT94aswbe5KhIEvIg4+s=; b=DGZecTYmJxyFgV9MucaXel4LQRLSwPAVIfcOZIKM1FathckiQ1DbfCjREER7WIC+xv jzIe/lt1KPGeyGtizdo9HNiM0NR9BSzJ7/xb6qo63OWCREUCSC4N7/LorE3jKM6zugV8 QL7wAGoAhd6EusnyTGCqVFVK6RFN1fSDQ5q1qCnoGmASMplyK563ABd1ObLTOtEuNwOj RNe/f4juCkbPKCT2CewFtI2D5ryCG//ZR1Jshc2YWv457CVtF6bgngLS5uqkwJ2WHyHw fU2nhTBN8lyPB9+6V2zuX7LXndbeAqpTDBUkvD1EHIVefYly7Lua1VCELVxYISrty/g+ X9rA== X-Forwarded-Encrypted: i=1; AJvYcCU56nLJV530VHt3zcVBmKvamvVmJHsnpgGAbF1B7g5FHR9E46cF0cI1aPM8LvdH31lL6LJMlNZiGQP0VqG+VBi8@vger.kernel.org, AJvYcCXuLDwFTy3js84v1Ia+5DoGUBaAOTsOEPk8RtRVG9BCMuaPNBc8vU1QXgY2dpSvXWNAJKJTSsxA5yAkW1cB6UO6W3PT@vger.kernel.org X-Gm-Message-State: AOJu0YzSkOSeEF1evNKjmERPMHpt3oXOqSRQmSRQxv3/Iu3STkcDIfrW NeueYW9963FFyglUHO7rY4pOWeGiIisThPH5UdpaMxxA3bfqpPCcsw/Y X-Gm-Gg: ASbGncs0cNO2+by5OiuVXIL8AbDuRrOXmaiKB3+yOr2ahnwc9GzOgzzrGU+Zxps5uRh uq9mGHFkHXR8Ot9Uc2HTxqED94X5tGNgZMb4WF7RrS7qr85owH4GYPq2nrh9/gDf8RPnJlzYFrP WFTImLMjCPRAIAvxN52InFRXc0ZMeJtAxpp/FbgQQJqlXZ32zz31IRjY3K9kZjUuIB8OJJoEtni qs1DbwN+KLXXNR5EfPEvA/bjaMD3/+rFbTG1wRcBBCO2zP9VvjiQujsblopRHnGJyKmulqiUWrb lEYw40cSKSgUlSZ2c+7GH+vhRJMq0Grkt4rYAyeVPTI1YXVAKGADhZd6L0BHVHH6hQOlFrM+2QS sOlJFSXpoWFtU24WyMhBBDvz/LFajpH63RqUQF8icMHSheK7DcafBYcJg+RC+XDI= X-Google-Smtp-Source: AGHT+IFse51pKuiC/VP4GKbWvaDWcMuhZ9D45Q5y3eF7fSZGFZeaEsbe60FdXrOe2iwXwBkbBGQYEw== X-Received: by 2002:a05:6a00:2e86:b0:772:306d:a1ed with SMTP id d2e1a72fcca58-7723e3dd39emr25528212b3a.32.1756945362315; Wed, 03 Sep 2025 17:22:42 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.22.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:22:41 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 06/18] perf/hw_breakpoint: add arch-independent hw_breakpoint_modify_local() Date: Thu, 4 Sep 2025 08:21:03 +0800 Message-ID: <20250904002126.1514566-7-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 hw_breakpoint_modify_local() as a generic helper to modify an existing hardware breakpoint. The function invokes hw_breakpoint_arch_parse() and delegates the reinstall step to the architecture via arch_reinstall_hw_breakpoint(). A weak default implementation of arch_reinstall_hw_breakpoint() is provided, returning -EOPNOTSUPP on architectures without support. This makes the interface arch-independent while allowing x86 (and others) to provide their own implementation. Signed-off-by: Jinchao Wang --- include/linux/hw_breakpoint.h | 1 + kernel/events/hw_breakpoint.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/linux/hw_breakpoint.h b/include/linux/hw_breakpoint.h index db199d653dd1..9453b5bdb443 100644 --- a/include/linux/hw_breakpoint.h +++ b/include/linux/hw_breakpoint.h @@ -67,6 +67,7 @@ extern int modify_user_hw_breakpoint_check(struct perf_event *bp, struct perf_event_a= ttr *attr, bool check); =20 +int hw_breakpoint_modify_local(struct perf_event *bp, struct perf_event_at= tr *attr); /* * Kernel breakpoints are not associated with any particular thread. */ diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c index 8ec2cb688903..ff428739f71e 100644 --- a/kernel/events/hw_breakpoint.c +++ b/kernel/events/hw_breakpoint.c @@ -983,6 +983,24 @@ static void hw_breakpoint_del(struct perf_event *bp, i= nt flags) arch_uninstall_hw_breakpoint(bp); } =20 +int hw_breakpoint_modify_local(struct perf_event *bp, struct perf_event_at= tr *attr) +{ + int err; + + err =3D hw_breakpoint_arch_parse(bp, attr, counter_arch_bp(bp)); + if (err) + return err; + + return arch_reinstall_hw_breakpoint(bp); +} +EXPORT_SYMBOL(hw_breakpoint_modify_local); + +/* weak fallback for arches without support */ +__weak int arch_reinstall_hw_breakpoint(struct perf_event *bp) +{ + return -EOPNOTSUPP; +} + static void hw_breakpoint_start(struct perf_event *bp, int flags) { bp->hw.state =3D 0; --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 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 EC7C51B0437; Thu, 4 Sep 2025 00:22:51 +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=1756945373; cv=none; b=gUlKT2JMFooLeP2CRvUnfLII4ySIilH6Usc5vKD6DHlrl8uj4ztlRv+MDWGl32UlLminP32I8LjCE8Xt24mo9SeqCEu7uLpc7G8F6zAVryZgPpisgh0ONJdIWGTfH9ai6+pfYH6zeMD4Ozmwy66XiRxBbuiGxwo6kJTxdHlstFk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945373; c=relaxed/simple; bh=O+PIwdZUlhV7lswiRN6FobRcgHyGWoHJZ0R7vjxdAx8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r8Ve9ZmneqZpRFl7uX6/czQolTU0lHmbqlSf8GgCIaeFoCZ8tCeBvMsHo2vSkIrC+nX8NZlcBoIsADlkvyOTfwjLd+Evj8LuAxc/AFoc7K/QdHZa4yOGjqmAzpZpGJlzf5aIKwZQgSIN8rWK73uMb64g3rR8lmbWuod+c1mDfd8= 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=NfU5BWzv; 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="NfU5BWzv" Received: by mail-pf1-f170.google.com with SMTP id d2e1a72fcca58-77269d19280so447658b3a.3; Wed, 03 Sep 2025 17:22:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945371; x=1757550171; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BTZRFEDtbchH98AsRwMn3UAjgd97CVQoF2+IU9lNOKA=; b=NfU5BWzvP4hH/KIpSKs3EKhou+1TjjjraMbe8wUdRZj5jkkJ75BcDPOciyJOeUY1MM 8/0BC3Sv8pO50DVQJarIZGzGiKPSljLGWXR6jluZKdAMTw8YRmBYXsO3iJGo2GGaX80p BJsGnv7AIypN/T5u+8PpMqxv4PdpnuVnPizywuIqdU0mFH3tVsVAt3n1g0WSADfqMDbJ 3e4DFSTykO20jucjBLinayOzwTAujp/FbrExcfSo2rnWORF2o0xhb0ydjlYkKsY9d5UJ itXPSp1SQNJaJ7oUyC4flGZNuT6yYbzn+p+/fCjtxoi6PAa1OIgR5t+S+0pwVXVAxMmw MO0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945371; x=1757550171; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BTZRFEDtbchH98AsRwMn3UAjgd97CVQoF2+IU9lNOKA=; b=EapLN8QrFxZamQvRo0WIY+YYFlVOgbk68a+WLxz6NmVPTanFsGz+R63PZ2y5MalQRi fMETKhvty3V6va9FvHylw5xLWnF75nVHdjg7KqHyQ8GKCLe3Jh1MaBPqxyT+YvIj6Vfh to7Vkdpz0LyzmlRgDxeiv0TAsA2C9W70ld7vrhNrg9ZGYf+riWlu05MdMSQ29fH8mvfI bXYXk5867EPsmL9OMRjl8S44G/u1O/fe8EDb4C9wPUNQkW3nFva59I53aB+SsY5KNQRA f35hJdxAP0rfgsWyStS6RrlRKrIYcYePbIErR3wlOr6iN8ijNRhFrNzahiFBie9p6kOx Qfng== X-Forwarded-Encrypted: i=1; AJvYcCUKE/O4X0KrGDa0NHpv8ySuftd+qTYjL19bIFWoxR7OYpzDJaMa63TxSoua+s6L2NE2XCAqAV9kL9FVlQIst1Ig/PTV@vger.kernel.org, AJvYcCUW23h8dOGwtbLGn6ixHafiV6O2eQSoeFmz/ITHvZeGaldFXJZmJUO5cRjDUJu4dwe/ojUC9Fe3mP/Irc3NqPF2@vger.kernel.org X-Gm-Message-State: AOJu0YyWvjfuns4mMFTQ6HQgRDe6+atlzRxKlh7P6G0eISrB3FOOUwWm y1QbqN51S62JMqapCkJOFLrnd3K3Abwa8vNFEIIeic7LYy3Uk+Zdrrt6 X-Gm-Gg: ASbGncu/EH2m/GdoTHIjuFkbx19l7/rG64x/sccQZkrchxUUoUz8TvnCkTsC2GFqZgN 5Nn5fi0/0clZOJS4dtEeaKnPWThAyW3mhTOnQrYGpg3hxKk7e7Yojbt1LJTaLZmTwvB21YDx6FK SyND3wxwDa4i/UCuqW+HX3vuvjV/kJo5+My0alXz4pFxIC/5r/Y0tCF3blzUxlvony0/89nlT1O 4CcdfXs7ffy23aAnY7jYzzmjq1j/XMBiheDihG6r1Vr2LwcuR5+ICMkMwl3vk6TbZ2DhmvZmHgG YwHeQdnhlg1rH7UvTffpQyOLe/05BnJmWFZGOvNmVc/Dv0MKea0yGRJ2ZN7uqy63HsPq1FUAYWe vf6+F2sdDLDAN1T/rtuiqquz7QKcg+fd9SyqunokofoaUeIJaf2Y2/+B8z+aP8hw= X-Google-Smtp-Source: AGHT+IGWU73oT/VaQcAznSm7YzjNOZbgfBD0gmg8RYjxK7cLGZgpVNX1W3T0EI71ND9cLoNXP4e0sA== X-Received: by 2002:a05:6a00:4f81:b0:772:60b5:957b with SMTP id d2e1a72fcca58-77260b598a8mr16120493b3a.32.1756945371106; Wed, 03 Sep 2025 17:22:51 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.22.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:22:50 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 07/18] mm/ksw: add atomic watch on/off operations Date: Thu, 4 Sep 2025 08:21:04 +0800 Message-ID: <20250904002126.1514566-8-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 to atomically turn the hardware watch on and off without allocation overhead. The watch is pre-allocated and later retargeted using hw_breakpoint_modify_= local(). The current CPU is updated directly, while other CPUs are updated asynchronously via smp_call_function_single_async(). This allows KStackWatch to switch the watch in kprobe/fprobe handlers. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kstackwatch.h | 2 + mm/kstackwatch/watch.c | 97 ++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index 2318779bde70..13ef8c79f855 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -41,5 +41,7 @@ extern bool panic_on_catch; /* watch management */ int ksw_watch_init(struct ksw_config *config); void ksw_watch_exit(void); +int ksw_watch_on(u64 watch_addr, u64 watch_len); +void ksw_watch_off(void); =20 #endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c index e7ed88700b49..c563f8d17829 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -16,9 +16,21 @@ =20 struct perf_event *__percpu *watch_events; struct ksw_config *watch_config; +static DEFINE_SPINLOCK(watch_lock); =20 static unsigned long long watch_holder; =20 +static struct watch_info { + u64 addr; + u64 len; +} watch_info; + +static void ksw_watch_on_local_cpu(void *info); + +static DEFINE_PER_CPU(call_single_data_t, + watch_csd) =3D CSD_INIT(ksw_watch_on_local_cpu, + &watch_info); + static void ksw_watch_handler(struct perf_event *bp, struct perf_sample_data *data, struct pt_regs *regs) @@ -32,6 +44,91 @@ static void ksw_watch_handler(struct perf_event *bp, panic("KSW: Stack corruption detected"); } =20 +/* + * set up watchon current CPU + * addr and len updated by ksw_watch_on() already + */ +static void ksw_watch_on_local_cpu(void *data) +{ + struct perf_event *bp; + struct perf_event_attr attr; + struct watch_info *watch_info =3D data; + + int cpu =3D smp_processor_id(); + int ret; + + bp =3D *per_cpu_ptr(watch_events, cpu); + if (!bp) + return; + attr.bp_addr =3D watch_info->addr; + attr.bp_len =3D watch_info->len; + attr.bp_type =3D bp->attr.bp_type; + + ret =3D hw_breakpoint_modify_local(bp, &attr); + if (ret) { + pr_err("KSW: failed to reinstall HWBP on CPU %d ret %d\n", cpu, + ret); + return; + } + + if (bp->attr.bp_addr =3D=3D (unsigned long)&watch_holder) { + pr_debug("KSW: watch off CPU %d\n", cpu); + } else { + pr_debug("KSW: watch on CPU %d at 0x%px (len %llu)\n", cpu, + (void *)bp->attr.bp_addr, bp->attr.bp_len); + } +} + +int ksw_watch_on(u64 watch_addr, u64 watch_len) +{ + unsigned long flags; + int cpu; + + if (!watch_addr) { + pr_err("KSW: watch with invalid address\n"); + return -EINVAL; + } + + spin_lock_irqsave(&watch_lock, flags); + + /* + * check if already watched + */ + if (watch_info.addr !=3D 0 && // not uninit + watch_info.addr !=3D (unsigned long)&watch_holder && // installed + watch_addr !=3D (unsigned long)&watch_holder) { //not restore + spin_unlock_irqrestore(&watch_lock, flags); + return -EBUSY; + } + + watch_info.addr =3D watch_addr; + watch_info.len =3D watch_len; + + spin_unlock_irqrestore(&watch_lock, flags); + + if (watch_addr =3D=3D (unsigned long)&watch_holder) + pr_debug("KSW: watch off starting\n"); + else + pr_debug("KSW: watch on starting\n"); + + for_each_online_cpu(cpu) { + if (cpu =3D=3D raw_smp_processor_id()) { + ksw_watch_on_local_cpu(&watch_info); + } else { + call_single_data_t *csd =3D &per_cpu(watch_csd, cpu); + + smp_call_function_single_async(cpu, csd); + } + } + + return 0; +} + +void ksw_watch_off(void) +{ + ksw_watch_on((unsigned long)&watch_holder, sizeof(watch_holder)); +} + int ksw_watch_init(struct ksw_config *config) { struct perf_event_attr attr; --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B579F1D63C2; Thu, 4 Sep 2025 00:23:01 +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=1756945383; cv=none; b=TyT0FSMMLDndpoqyUUwROVlrm8TlPjAgG1+C5erkFtG2IM115AHMIOMyr7u+TgC4IvJxfBI/j4Zbz2l2JkxnYC5XzptvzuZSnrOMdJpsDQShYW5XqXaKmjjAeEA52Kp1PWy0ep9aC95PBf7RB/UymgaC+hfPS3t9N/fThmRWM0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945383; c=relaxed/simple; bh=QgMzLX+EujUl5nFUa6zgdRyiCNDRhy+hzjs8trJUMFo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cE9NAVtUEhbusnf1hp0/pnmMaTDRHCJTbjBBws0cnCMjlKiHvHtcDDdVVd/UemeiGnHNMz8yNsVIVZ9N3+W2FAOOv0sz47EOd2vkS5E/Z0GwVJHAaHSdJn4u97ZpA5AbuoGRgxjZJ7Vrrv13q5cXnMc/+f+VGEvfcJ8ZlD3kEaI= 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=O2Zcl6y9; 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="O2Zcl6y9" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-7722f2f2aa4so649376b3a.1; Wed, 03 Sep 2025 17:23:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945381; x=1757550181; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bG7HgZbRDL9zQbYB/rpSIbCyKAIzwo1Rr8phODu9KXc=; b=O2Zcl6y9Ohn2KJmw6NddIUaHknyUN4skT6O/5mn91EDaztAslUpR0BhiMJ2LQIlt6U JOs5cNVsKdCljQ67BT9RIBmgrDq85oft6LZ3kVP9nsxKQ6OiI74ouQwi7hSFPXrnkGF0 MHnF85LIxRFGbnQXOyRNI1Swl6w3st3m37n5d9dUVpGwbbhebaT2J0UdCBxyJOgXTMGE cHJkBjpAXM1S77i5JIXyQN7ZTcLH1KTUcPBM9rAclphKx03foJbNf1qkRLJoWC0GfKE9 YrJCJf4LnDcT3L5lzy0nQ0CDcskYYE1mKXypK++UBO8jyqZrjI1/2UisT2GpNsKCiTR4 mPcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945381; x=1757550181; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bG7HgZbRDL9zQbYB/rpSIbCyKAIzwo1Rr8phODu9KXc=; b=VNXl8wNefo8EKJRXqU/7uZz0U4UwUpqYmZKtKD0TCn70djTaZjZzTyWIL0rah69SvF i1YRlVGyupZXEng9D1ULnADCY04y69DQoARI0RMmuSrJpcri7NqNEn9qARM0e1i/yhO1 tiVvDhyu2x2Oxl/2xYddwFJAidavyCFGAjbaq2K+r64Vm6DkOwvyVVWflr05u+Muy3DJ QSAr9piSLk0l3BGp86g4KWYNiBJ5Xl5HPdOTQv22uw73WlE8NFW/Tbe90//9UgwgSRuw LQh1GL5zF16BpkLA9o9OAV29jeRFEdezZM3pA18ksLtRl74/RRrcA2vLy7t4c5HyPTQ4 82qg== X-Forwarded-Encrypted: i=1; AJvYcCVs94TP5HfFMqioveDylYxWEitNZHXlXJuq/7Ah6PLtaaMv24s2kobXUrrhuQcaf3I839nwjUBe3Lt3dBCUcpt8k7xp@vger.kernel.org, AJvYcCW7Zr58o+wZoSrWfjrwMOH6dsky0W64fn70dD/dCm9Vf6072SZhl1lvdJeftofHuJBVBQ9DV9effyuIMqm0V+Mx@vger.kernel.org X-Gm-Message-State: AOJu0YzP27TWf59NTWULp4dREDZMo2PcB3i9vtvGdKuVRRGY9m1qofRe pqm4rS/5WqDvsTN2rADCzIFBbq9LH9J/y2sclqUbidw64skiN0U27mP5 X-Gm-Gg: ASbGncvw+ypS+52MX4WT57XV6NHTHLdbszNzKNQ4Jnho5o3IOleJZIMDJXc0BQ56gIU X3spHEvJGSwO6hPq/Xt8q3h0i7IQvH9HqDVdDsmxaJSzX+kuRUT1dK5N/ZJxiSQ60dH2V5TvOmE QOmp205M04D2nuHiifPA6dmMGoExzanZdP+NKr7i3ixUuOHO6hu41LQmo2qvmhriM5i2G/ay9x2 tq/Px25lezbxUg6qTCr8ShoaTue1OgtL8Cyz76+rpTRX1tv+JfbghmRMLwA6g3tInnh6BKjq9SF pJE/aH6ilFNeF1JurtdO34eKfV1clsoCqGbo0y5t8Q+iPXLk4vJM7BC1zjPTky8J3EjLjE3JVfc HkrXLLaQb7vd4hmfBlJDJf0O4vdRZHNaf/nieMlpgp0GhVytP7niRvbtSYYOAN40= X-Google-Smtp-Source: AGHT+IF8vif2SMyS7b0Y14u6occzXKi/v4yy8ICXoht40V0het8ir2qpLWqnkYrrTJFAlnnbmVHg9Q== X-Received: by 2002:a05:6a20:3d87:b0:247:65a0:822 with SMTP id adf61e73a8af0-24765a008c8mr6727769637.40.1756945380982; Wed, 03 Sep 2025 17:23:00 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.22.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:23:00 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 08/18] mm/ksw: add stack probe support Date: Thu, 4 Sep 2025 08:21:05 +0800 Message-ID: <20250904002126.1514566-9-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 stack management for KStackWatch using kprobes and fprobes to enable dynamic watch switching: - Entry: prepare target address/length and enable watch - Exit: disable watch Signed-off-by: Jinchao Wang --- mm/kstackwatch/kstackwatch.h | 4 ++ mm/kstackwatch/stack.c | 91 ++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index 13ef8c79f855..bc8664af4fa6 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -38,6 +38,10 @@ struct ksw_config { =20 extern bool panic_on_catch; =20 +/* stack management */ +int ksw_stack_init(struct ksw_config *config); +void ksw_stack_exit(void); + /* watch management */ int ksw_watch_init(struct ksw_config *config); void ksw_watch_exit(void); diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index cec594032515..3b72177315cc 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -1 +1,92 @@ // SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include + +#include "kstackwatch.h" + +struct ksw_config *probe_config; + +/* prepare watch_addr and watch_len for watch */ +static int ksw_stack_prepare_watch(struct pt_regs *regs, + struct ksw_config *config, u64 *watch_addr, + u64 *watch_len) +{ + /* TODO: implement logic */ + *watch_addr =3D 0; + *watch_len =3D 0; + return 0; +} + +static struct kprobe entry_probe; +static struct fprobe exit_probe_fprobe; + +static void ksw_stack_entry_handler(struct kprobe *p, struct pt_regs *regs, + unsigned long flags) +{ + int ret; + u64 watch_addr; + u64 watch_len; + + ret =3D ksw_stack_prepare_watch(regs, probe_config, &watch_addr, + &watch_len); + if (ret) { + pr_err("KSW: failed to prepare watch target: %d\n", ret); + return; + } + + ret =3D ksw_watch_on(watch_addr, watch_len); + if (ret) { + pr_err("KSW: failed to watch on addr:0x%llx len:%llx %d\n", + 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) +{ + ksw_watch_off(); +} + +int ksw_stack_init(struct ksw_config *config) +{ + int ret; + char *symbuf =3D NULL; + + /* Setup entry probe */ + memset(&entry_probe, 0, sizeof(entry_probe)); + entry_probe.symbol_name =3D config->function; + entry_probe.offset =3D config->ip_offset; + entry_probe.post_handler =3D ksw_stack_entry_handler; + probe_config =3D config; + ret =3D register_kprobe(&entry_probe); + if (ret < 0) { + pr_err("KSW: Failed to register kprobe ret %d\n", ret); + return ret; + } + + /* Setup exit probe */ + memset(&exit_probe_fprobe, 0, sizeof(exit_probe_fprobe)); + exit_probe_fprobe.exit_handler =3D ksw_stack_exit_handler; + symbuf =3D probe_config->function; + + ret =3D register_fprobe_syms(&exit_probe_fprobe, (const char **)&symbuf, + 1); + if (ret < 0) { + pr_err("KSW: register_fprobe_syms fail %d\n", ret); + unregister_kprobe(&entry_probe); + return ret; + } + + return 0; +} + +void ksw_stack_exit(void) +{ + unregister_fprobe(&exit_probe_fprobe); + unregister_kprobe(&entry_probe); +} --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 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 89B0A1DDC1D; Thu, 4 Sep 2025 00:23:10 +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=1756945392; cv=none; b=kKFnuHp+zx74WyGfl7wTZF799JlN3tm0iQBhvP37OiUY+WIe9c0aImO+g7ghUPNefAVvSP6geACbSYHrONznxKzIVblfUFb4syTJmZg/g+G9x1kL+0t+ViIDIqm4QzMRRMVs4G4gk+usogVY/UNyMP7YGtK19MJyVak21ynrr7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945392; c=relaxed/simple; bh=CBD5TyJIG48zunzijV26LZ21NLQJuMz4rIJCSEL3zs0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=URn19UmLVOoeGSkKR6R4ASqNiWKl3ErlavLfodjZD+WwiSVEHXnV0dYgKnUvshQ91r/ANDMj4D5JPMV6lPIrlZM9Q1dqvTspUAgJKdgWMZcyk/Ka16ze8i8VDGqirBqzOe5drqHiqOnaAwsRQkFRYgfFSP5RdUNGW5gDjaVL0EU= 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=KXThaLu5; 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="KXThaLu5" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-76e4fc419a9so540682b3a.0; Wed, 03 Sep 2025 17:23:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945390; x=1757550190; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eZFeltWH3JIFgXl4IiDP3gNU26ULKF6mUGhK5pmKxrA=; b=KXThaLu5PQ1QRAbehqvPYIm2+EaeyOggCXGVOIk+u0cdCmG9D6PasfoxLgIiIGHlXd ooMwCjXZ+7P1efI6DcSr0aMuQhc4+WAKRAYMtqUYMCK39MtJ0NAjnPbucxGE3vQLDQv7 5DO+cW0jKpE6+qpyZau6s4+xoIcRa4GidV1QNw6PSiK/3mBWDP8KeYmajjxlJtoPz0id i2NTQNdKthZTTOPA/kSKatA1gA5uN1HQ3g1u04DN+gZaTiofi8wJ3EPlnytPiruABUu+ iBr1kUTncYWrO8H9E0wS4ffM7YFw1GxfLn3T6pnQTw2234ssAFqqFUm6PgHo4ho+rF4M +dwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945390; x=1757550190; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eZFeltWH3JIFgXl4IiDP3gNU26ULKF6mUGhK5pmKxrA=; b=AbOYEulfYOQ+RamgE1o7ZKJV4Jh0Gtvu+KO9eQswUH5joXhXNGaTey3M2D8EzW7OiY tFiUwXT8arYKbVj8Pn3g0OrbkeCrJjM20vqHwwZ/NPlBVKIeB5jGLPeiTsvx7/BmAPp6 8upcf030SiL8hctrqwZDG02ZMzBpbn5xBwyAcV/Vx9TGtaIfGcjoAlYqT6BCyFoNcFf7 k8hxrUQiQxiOQKdq22KhmonKJJ37EDPVkp7rRmNE7jWtdPTY16VHcrko3tHbNKsWw0LF /EGQyiftzwHM5iJUjLiiMIwFNFXawflNcy+k6rmqrlN9FLyoBw0E46hd8tZE3izlAeHd 5ICQ== X-Forwarded-Encrypted: i=1; AJvYcCUXmOkPN+lJ26dkO2oiVcmUuiPOVXE7M5Oe39WmDEYgp2Kh96Uko9KBK3lkSC6S3W/mmbZF89XryUzCT8zT6tkUmjOR@vger.kernel.org, AJvYcCVF+lsJ11adNjnaK4xxrNUYwUIPEt+5jOqX1GHKvNHnqXcodNifjPRr+Y+5rEgSrEYCPS1lU/XA9VdketBtqEsi@vger.kernel.org X-Gm-Message-State: AOJu0YztThtnrbg0hQhfZEI7dtlZMOtNKkVbsdfOYsei678WqeWAjDpt dYjFijQrkNbWThPEBiWzj3OYzaI1m4yaoP/YTl7Q2HFmuZWa3WKjnJ/c X-Gm-Gg: ASbGncuX2xO1dR47CyzfjP0+0Nx8KQWKtQQh2GO6xSvektMYuSortYfu/02lZsljNKy ou1l8VubKHevUmf3PaQbaCZOL7IuDjHxX2c1JKIdNwQlk/6ebi/JRZgvlfSsVAD0uZixuNcjWta g8S0+ojsmhbXDva7DL3dcSuEraaMC0wIAv9TII+4CHK4KVnMCSM/dialpdMuDFtvlYuqLYvbe4B DfI94GIaM/1kCYqVpnQ5F+xb3DbQ8YlKQO87BH0w3Tv1eLi9A+weW1sBA+BDB4Ak5RYa/nkets+ N9Jo0LCxDPuOuduNtpqjjN/fzJsFG951N8qPmaaYgVXCETcbMrLwu4R/uKEsgWATB4fh/7MFsYG vkdFod2m0AT3YDW7J8Ma6nsYUq0u9+Gu18me3eRijJVkFv02NNlaliL7NeMD5pvjpQCMXZrekiA == X-Google-Smtp-Source: AGHT+IFgl+sw+tmEYUJ4upz5Un2DxoHCv2E7JtarNm7ur8WtEKTrx96zo+yDn11a5xFBTb7wAEqpBg== X-Received: by 2002:a05:6a00:180a:b0:772:ce8:d894 with SMTP id d2e1a72fcca58-7723e3e3aeamr22624402b3a.29.1756945389846; Wed, 03 Sep 2025 17:23:09 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:23:09 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 09/18] mm/ksw: implement stack canary and local var resolution logic Date: Thu, 4 Sep 2025 08:21:06 +0800 Message-ID: <20250904002126.1514566-10-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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" Implement logic to resolve stack watch target for kstackwatch: - Locate the stack canary within the current frame - Resolve local variable offsets relative to the stack pointer - Validate addresses against current task's stack bounds This logic prepares watch addr and len for use in kprobe/fprobe handlers, enabling dynamic stack monitoring. Signed-off-by: Jinchao Wang --- mm/kstackwatch/stack.c | 99 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index 3b72177315cc..00cb38085a9f 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -1,22 +1,115 @@ // SPDX-License-Identifier: GPL-2.0 =20 #include +#include #include +#include #include #include +#include =20 #include "kstackwatch.h" =20 struct ksw_config *probe_config; =20 +/* Find canary address in current stack frame */ +static unsigned long ksw_stack_find_canary(struct pt_regs *regs) +{ + unsigned long *stack_ptr, *stack_end; + unsigned long expected_canary; + unsigned int i; + + stack_ptr =3D (unsigned long *)kernel_stack_pointer(regs); + stack_end =3D + (unsigned long *)current->stack + THREAD_SIZE / sizeof(long); + expected_canary =3D current->stack_canary; + + for (i =3D 0; i < MAX_FRAME_SEARCH && &stack_ptr[i] < stack_end; i++) { + if (stack_ptr[i] =3D=3D expected_canary) { + pr_info("KSW: canary found i:%d 0x%px\n", i, + &stack_ptr[i]); + return (unsigned long)&stack_ptr[i]; + } + } + + return 0; +} + +/* Resolve stack offset to actual address */ +static unsigned long ksw_stack_resolve_offset(struct pt_regs *regs, + s64 local_var_offset) +{ + unsigned long stack_base; + unsigned long target_addr; + + if (!regs) + return 0; + + /* Use stack pointer as base for offset calculation */ + stack_base =3D kernel_stack_pointer(regs); + target_addr =3D stack_base + local_var_offset; + + pr_debug("KSW: stack resolve offset target: 0x%lx\n", target_addr); + + return target_addr; +} + +/* Validate that address is within current stack bounds */ +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) { + pr_warn("KSW: address 0x%lx (size %zu) outside stack bounds [0x%lx-0x%lx= ]\n", + addr, size, stack_start, stack_end); + return -ERANGE; + } + + return 0; +} + /* prepare watch_addr and watch_len for watch */ static int ksw_stack_prepare_watch(struct pt_regs *regs, struct ksw_config *config, u64 *watch_addr, u64 *watch_len) { - /* TODO: implement logic */ - *watch_addr =3D 0; - *watch_len =3D 0; + u64 addr; + u64 len; + + /* Resolve addresses for all active watches */ + switch (config->type) { + case WATCH_CANARY: + addr =3D ksw_stack_find_canary(regs); + len =3D 8; + break; + + case WATCH_LOCAL_VAR: + addr =3D ksw_stack_resolve_offset(regs, config->local_var_offset); + if (!addr) { + pr_err("KSW: invalid stack var offset %u\n", + config->local_var_offset); + return -EINVAL; + } + if (ksw_stack_validate_addr(addr, config->local_var_len)) { + pr_err("KSW: invalid stack var len %u\n", + config->local_var_len); + } + len =3D config->local_var_len; + break; + + default: + pr_warn("KSW: Unknown watch type %d\n", config->type); + return -EINVAL; + } + + *watch_addr =3D addr; + *watch_len =3D len; return 0; } =20 --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 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 759511DF723; Thu, 4 Sep 2025 00:23:19 +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=1756945401; cv=none; b=uE1EbT+N7SUVZFJwo10ykPVMVNcV1ReGTOEtUztZ1TUyppJt89l96cDZAv6PBmfcTSwzAwyDaf3y/mZrzpjla/sOf0SByml2FG9Nvvc5eEyqE11juT1q+6sZRO3uw1FcYQayFFAnYz0H1IVPKlCJaKfiDF8FEg3bhj/6kwVhBLM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945401; c=relaxed/simple; bh=U7CutZhDSDeFyrxaFAIBsWLB2f2n69YdYMG7rkCCFZg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nI+WRyTHcLg2ZVPey9ugX8Nacm9mum3gljoJBHNohufGaBYdxAlxs8RBLiXo54n7b4qkXQTB9EUy6AbFBJJ2Ybf3QWDCsYLqK5SrHmM5DxZWLHo4EUI5L4pgCGqmyYT4ex42xKrYhHgfXm1EKzBdz4di859KmJHgEEeWBZZRxxE= 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=CTx3t0Hi; 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="CTx3t0Hi" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-772481b2329so570227b3a.2; Wed, 03 Sep 2025 17:23:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945399; x=1757550199; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=18zaW3moNW/J2OB76TZ6MBmnN/MPkuC7bfXPlyWG+/g=; b=CTx3t0HiVxEY9Gt7Tm3NdKOobOYNjqf9+Mr48lRdisFhPl8n8R4iCmwmDmArcssj66 eMtCaI6BnT1qS7DvxZcKSTSAC1Mjg/EYi9wPAhQimgwnKzOWtSxJNAqi9rIK3GrUIaKb 6roB4sjl/KsNhdJCfi7u5xRoxuHzNMsFUaApmC/KDPur8J1buxVfjaH7YX0xfR2fiE6c jspd+CH+QEiQ+TyNJxEJgHhvVgai/PyQ2chZIwdfjoTYFRldA7IXPDo8CwgYNB/yxTcM PzH46EnhIlTNu7YBPi0aXiGXpG9KOdYB+0Sub/qVwIctIXgt9ydtEAZjtQ7Zi92qSLpM vApg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945399; x=1757550199; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=18zaW3moNW/J2OB76TZ6MBmnN/MPkuC7bfXPlyWG+/g=; b=O3t14s1cR3r7sfqmiuE8Ob3+hjY8Yt9pys/SMf3x7Xh100cWdW/C3DBeTTR7VyqY8M pj7GpiTJwlBjpK1tMw8mzB9dPaxZWcXQTtHNvp4xYO4UKK/FWz8355oFDmb1NhY9hG3e B11atQfCQ/OS1XXQeYZZ/VYVyvm2bSxJ0bVQG1ugoN37mFi36TBqa/wFb/47EoqrW6BA ioloYT32YWFlTbKWOimHv3hzNSk2qs6IN74Zo+8tcCDngllCvuNL5qPfXMfseo/KhxgA y/BAUj0slGm+1R5odB1rdTcDSWtZPXSIXAKxvicsHWE5zCSUAoQjGd1Md4ZUghnr9qSO njgw== X-Forwarded-Encrypted: i=1; AJvYcCWC3vzosVw/A8eWhCwkuVtXCV9sofI8hjuIIktS5D32+u92+ZjB11JSUIAPgog9JhcRMsvB1z3wBwbzEFDFFopuClUj@vger.kernel.org, AJvYcCWcFm5+0QX8P/x1mDkUMg3IyDkRXVVoIVUFNrxigK1snSvOZ8aO1NBx0Rdi/vOAKqLO0/LxaoXKIxKmO5vFx4TP@vger.kernel.org X-Gm-Message-State: AOJu0YweMP4VrCrcWhNPT3YwhjQYZbbaSs1Hbp3Gra31NX0/3TPLpwOq KZ/Ii4iv7X2M9eEKgF2LrH1Uh84J07eKihwrVE5Ujw8RWKB9zi/oimihugfVD0RYGbxUkw== X-Gm-Gg: ASbGncsQcziw0U7y4VRu07VkLhm8p3WqJop6GC22EGtFIIFSd6IHl1MGlBBF6u5QvN/ beaYDpas8w0Ro2Mb4gSwX/KMcB/6JUJJ0g9fJ8eA0BRyX5pokQsBUQ5MI6EZQSoDpR4FlKWQHYX Dt6NSa/E1DEpv00vmvMpzVCYFv40dAJOGy1Y0qM8DEqmK+OJAVc/Xys7ykyZNgR5QiI1tVCUf4s r5NKj7fEMD4UQq3MOJVLxNw06CsuSExYVF8q6sMPJFZKKjOukV8Ann5UvTpwc6ENiNTnMOLQMw0 R1DmKHYZvoKAM8eyaKSKf66PXo750pQJaiCCAq10+xeptKQClE+an82GRd5gpH2/SLrczCGlXtZ rp1SsgS6qX9YLt/DzI4jV0lPjEC4YxKA62NRQNbyr5WyRS9ur5F16 X-Google-Smtp-Source: AGHT+IFn+y2P+lkSjCxmHbNPVMFNCQGc4aVUADK0EWUrb+8UNHFLYez+kqXUbc2XvO+u3VVteAjfqA== X-Received: by 2002:a05:6a00:3492:b0:772:5899:eae8 with SMTP id d2e1a72fcca58-772589a2e50mr16425370b3a.27.1756945398629; Wed, 03 Sep 2025 17:23:18 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.23.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:23:18 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 10/18] mm/ksw: add per-task recursion depth tracking Date: Thu, 4 Sep 2025 08:21:07 +0800 Message-ID: <20250904002126.1514566-11-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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" Implement depth tracking for KStackWatch to support stack-level filtering. Each task's recursive entry depth is stored in a global hash table keyed by pid: - get_recursive_depth()/set_recursive_depth() manage per-task depth - reset_recursive_depth() clears all tracked entries - entry/exit handlers increment or decrement depth and skip if the current depth does not match the configured depth. This works even across task scheduling or in interrupt context, since depth is tracked per-task, ensuring KStackWatch can selectively monitor a specific recursion level without redundant triggers. Signed-off-by: Jinchao Wang --- mm/kstackwatch/stack.c | 105 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 2 deletions(-) diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index 00cb38085a9f..8758b8b94f7f 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -1,6 +1,8 @@ // SPDX-License-Identifier: GPL-2.0 =20 #include +#include +#include #include #include #include @@ -12,6 +14,81 @@ =20 struct ksw_config *probe_config; =20 +#define DEPTH_HASH_BITS 8 +#define DEPTH_HASH_SIZE BIT(DEPTH_HASH_BITS) + +struct depth_entry { + pid_t pid; + int depth; /* starts from 0 */ + struct hlist_node node; +}; + +static DEFINE_HASHTABLE(depth_hash, DEPTH_HASH_BITS); +static DEFINE_SPINLOCK(depth_hash_lock); + +static int get_recursive_depth(void) +{ + struct depth_entry *entry; + pid_t pid =3D current->pid; + int depth =3D 0; + + spin_lock(&depth_hash_lock); + hash_for_each_possible(depth_hash, entry, node, + hash_32(pid, DEPTH_HASH_BITS)) { + if (entry->pid =3D=3D pid) { + depth =3D entry->depth; + break; + } + } + spin_unlock(&depth_hash_lock); + return depth; +} + +static void set_recursive_depth(int depth) +{ + struct depth_entry *entry; + pid_t pid =3D current->pid; + bool found =3D false; + + spin_lock(&depth_hash_lock); + hash_for_each_possible(depth_hash, entry, node, + hash_32(pid, DEPTH_HASH_BITS)) { + if (entry->pid =3D=3D pid) { + entry->depth =3D depth; + found =3D true; + break; + } + } + + if (!found && depth > 0) { + entry =3D kmalloc(sizeof(*entry), GFP_ATOMIC); + if (entry) { + entry->pid =3D pid; + entry->depth =3D depth; + hash_add(depth_hash, &entry->node, + hash_32(pid, DEPTH_HASH_BITS)); + } + } else if (found && depth =3D=3D 0) { + hash_del(&entry->node); + kfree(entry); + } + spin_unlock(&depth_hash_lock); +} + +static void reset_recursive_depth(void) +{ + struct depth_entry *entry; + struct hlist_node *tmp; + int bkt; + + spin_lock(&depth_hash_lock); + hash_for_each_safe(depth_hash, bkt, tmp, entry, node) { + hash_del(&entry->node); + kfree(entry); + } + spin_unlock(&depth_hash_lock); +} + /* Find canary address in current stack frame */ static unsigned long ksw_stack_find_canary(struct pt_regs *regs) { @@ -119,10 +196,21 @@ static struct fprobe exit_probe_fprobe; static void ksw_stack_entry_handler(struct kprobe *p, struct pt_regs *regs, unsigned long flags) { + int cur_depth; int ret; u64 watch_addr; u64 watch_len; =20 + cur_depth =3D get_recursive_depth(); + set_recursive_depth(cur_depth + 1); + + /* depth start from 0 */ + if (cur_depth !=3D probe_config->depth) { + pr_info("KSW: config_depth:%u cur_depth:%d entry skipping\n", + probe_config->depth, cur_depth); + return; + } + ret =3D ksw_stack_prepare_watch(regs, probe_config, &watch_addr, &watch_len); if (ret) { @@ -132,8 +220,8 @@ static void ksw_stack_entry_handler(struct kprobe *p, s= truct pt_regs *regs, =20 ret =3D ksw_watch_on(watch_addr, watch_len); if (ret) { - pr_err("KSW: failed to watch on addr:0x%llx len:%llx %d\n", - watch_addr, watch_len, ret); + pr_err("KSW: failed to watch on depth:%d addr:0x%llx len:%llx %d\n", + cur_depth, watch_addr, watch_len, ret); return; } } @@ -142,6 +230,17 @@ static void ksw_stack_exit_handler(struct fprobe *fp, = unsigned long ip, unsigned long ret_ip, struct ftrace_regs *regs, void *data) { + int cur_depth; + + cur_depth =3D get_recursive_depth() - 1; + set_recursive_depth(cur_depth); + + if (cur_depth !=3D probe_config->depth) { + pr_info("KSW: config_depth:%u cur_depth:%d exit skipping\n", + probe_config->depth, cur_depth); + return; + } + ksw_watch_off(); } =20 @@ -150,6 +249,8 @@ int ksw_stack_init(struct ksw_config *config) int ret; char *symbuf =3D NULL; =20 + reset_recursive_depth(); + /* Setup entry probe */ memset(&entry_probe, 0, sizeof(entry_probe)); entry_probe.symbol_name =3D config->function; --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.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 CE6651E2614; Thu, 4 Sep 2025 00:23:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945410; cv=none; b=jMI1dhUHAFRJ8vgWEgUDgGI/HXO3yJa1i8JdM0iM+s7cn1eWjeaKyDb31k88TaQTIAZEZmxDuYQVH0zgY0zC3ogmmPCeU8BokPZ6dFmtHH+ZjbOfksTRd5IfrwV06/IbckER8XI8aDLP/jZy+0G8XY0DZBy85f1PmJ5M8XI1fZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945410; c=relaxed/simple; bh=IGZGRl7SPv8QYEkfF81MfJQSk3f6SOpoIfAg9jVan/I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=cmZZWzMLYJd+4gyKI7wKJXEsLaoc2vMM6NXypMJKwiZ1HZAFlT/D6+k/SsRiO/nzlHmMJU1pXUIeXHUAMH2fnH7NzeqK95elgxz1JENHE5bqxDJVdvwTLXH3r+aTwzeN2IQi1qPiDKYHQkhZKcx8g5KRVkQrekmFWWs2yRG8vGg= 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=IVgZJlqA; arc=none smtp.client-ip=209.85.210.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="IVgZJlqA" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-76e4f2e4c40so408335b3a.2; Wed, 03 Sep 2025 17:23:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945408; x=1757550208; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=y7709ieFUlS+2MrC1zNfTfKDbThJkcQv3sQ/PEEU3R8=; b=IVgZJlqAKY5X+VEgxEIMICMjvAxkG10uzdijXFUPdCGT/JQQF5OxYyOXbqGGBAgYyi 4ICdEe0+QGksRpsLXqvxooeDPZAFBoJtoDx8bkeovx8/UsttDQiRncNGAhiPCnNJAMVt Kdy/7R29yYWhFA2DYsMvYOvqIfg+zhT8yJsGtOGS8PRXInv+i5TDRyQHuw8/SifCFK+W MclI2bL01D4MeCO27QikT1DAuhMU3sfkp+hM9vA0w5lgt6ZvSMoea+Wisqn/z0zLoLvV G77JqYQ0K2I29XkMbhdnO2ZyW6197Lpf0Nb0eGKRB1/aLlWWIe5JkTPfN8MUqr0/5gFX DSIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945408; x=1757550208; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=y7709ieFUlS+2MrC1zNfTfKDbThJkcQv3sQ/PEEU3R8=; b=qY0jbsy7xXVnKKng2T8beVFiPmQXecrC/B+NaNmhFFSXW3JPLway0SDYttKlZynP2W ZfJtZfaK9hJ95HZ5DEE8dDgqulsQ2vpH7LbMvFrhAlCNmMqSTgK5ZRNIro8Ajs/OkxP3 VD1Rsy8UkFrfZnddxtvTeE7oDMOr0WVpoTla60dMPPYwWvP2nnF3K4DTwvDnPTr12tUf uDd+b1ARj6CigZCtK0V+9k40MO74xq4rQaOr/UsJ6sq6rTy6UptMKz9ZtKHuWWQ0tbdp LKsLwe4V8PHFBHKGy9YlrRguukKkGax9U3tlAAZlAtDns8lIJGHZ1TssFT2a55cM1fuq yO6Q== X-Forwarded-Encrypted: i=1; AJvYcCUpYyqOro/axT0oY3ei61JUx9pkAPVfimW7tqSyn1urBEK8iNCZT+ktJKxiD33966mgrIh2Vdofo0dHey57Yf/Y3kwc@vger.kernel.org, AJvYcCVU+Sd/VZVh/eE+CNcW4cfgJ0qb4G96AQkeH0oJtZUPOTCAFGM0UaKQNxwEUDfNf6lschEd/RAOclQn8KbdVl6z@vger.kernel.org X-Gm-Message-State: AOJu0YwJGwQlhflyZHD+fT7W3lur92+ETUWdTNm8/XY8rS9qs8Tb4XQ7 YeymTcPVOOn6HUzgI2cmoVd6wn97Zj2CuSLibRjZhoz/n/0LGrf2bypm X-Gm-Gg: ASbGncvXjfCeJ68LWCsRNZ+c1sW2+xh17YqWFgPxNmnJZ0biWSfx4IebUfiGWCt2txs S91/DMjOT6Jld4oOyebq9Lbmh8t1UKCXUiuseuizdwQrvfVMQNrPjN/NPSpM5YPS0Dr6SY/hNt1 yScdzZAWYWiDwlbYtdRIdKMfXCWSwT9qVIwGPs7M/uPkB7c3crTng+gfSYGsxKzLGv47VS+V1/0 CIFWj9NfAfpB18n7AXPL1W3G00CZzbkuLdKXKl/nVakJkrtaR9mZeZicwM1SdR1tBNaltvGrwMy NOWRy4VYs6bPFowvmq6vKi0G7DsgzsXWvw1xvVKOnhZvUb5cXHmwemM94ca9jHGAziTEPl3vfGo OXA4x+S3HXte4BPY8fkkNHenYGlIQJ/CyNKZXvV4xTBnuVhMfHclQ X-Google-Smtp-Source: AGHT+IHhppPhCunvFzVZDYZ67D6HMJaD+bYACrgmc8Txd1zbRbMjBjJcCjKS/dJR/OMmvBRDlXJzhw== X-Received: by 2002:a05:6a21:6daa:b0:243:d5ab:d1fc with SMTP id adf61e73a8af0-243d6dd7a45mr24329846637.7.1756945407888; Wed, 03 Sep 2025 17:23:27 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.23.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:23:27 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 11/18] mm/ksw: coordinate watch and stack for full functionality Date: Thu, 4 Sep 2025 08:21:08 +0800 Message-ID: <20250904002126.1514566-12-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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" This patch connects the watch and stack so that all components function together. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kernel.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 95ade95abde1..4c5fbcaddab0 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -1,10 +1,13 @@ // SPDX-License-Identifier: GPL-2.0 +#include +#include #include #include #include #include #include #include +#include =20 #include "kstackwatch.h" =20 @@ -22,6 +25,29 @@ MODULE_PARM_DESC(panic_on_catch, =20 static int ksw_start_watching(void) { + int ret; + + if (strlen(ksw_config->function) =3D=3D 0) { + pr_err("KSW: no target function specified\n"); + return -EINVAL; + } + + /* + * Watch init will preallocate the HWBP, + * so it must happen before stack init + */ + ret =3D ksw_watch_init(ksw_config); + if (ret) { + pr_err("KSW: ksw_watch_init ret: %d\n", ret); + return ret; + } + + ret =3D ksw_stack_init(ksw_config); + if (ret) { + pr_err("KSW: ksw_stack_init_fprobe ret: %d\n", ret); + ksw_watch_exit(); + return ret; + } watching_active =3D true; =20 pr_info("KSW: start watching %s\n", ksw_config->config_str); @@ -30,6 +56,8 @@ static int ksw_start_watching(void) =20 static void ksw_stop_watching(void) { + ksw_stack_exit(); + ksw_watch_exit(); watching_active =3D false; =20 pr_info("KSW: stop watching %s\n", ksw_config->config_str); --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 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 A0BBD1DF97C; Thu, 4 Sep 2025 00:23:37 +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=1756945419; cv=none; b=fJvvrf6mDp2M0g2yS+ff1bektCUuYzJVJUb8bmCdTBtqrJSbDEA7tTFMuMBFPmg+iKHjGONEGlQDip4GqvdLkkSusqCrQY1K064HCWsqkQFw3F1mFwvhYbF3Z0QuWTzUVGlh8jzGP2vvr24tVSQx6ycAQxJzKJYncqxuwiiXyzw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945419; c=relaxed/simple; bh=+F7WhvUuFEr17da3jUnHwsnHX9itQiAKBlhr+pK9kw0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iFH9CIQ/5ngNZipj2kZaZpsbEygW+izG6ltVf9U5ahXXfdNmKh/IPj4vLFuXC3tf4KlDroJQKaEHQoqbMK5eH75Y6X8wjJrtDXjsRxx6fJw/h1+1eMVWHRPpScWQ2/fjL2SQlu2Fq3P+33FL2rvcmetrN3aN8wJGAWQ2kngp4UU= 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=Xu7q3r+D; 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="Xu7q3r+D" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-772481b2329so570610b3a.2; Wed, 03 Sep 2025 17:23:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945417; x=1757550217; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zy5btr4Cxic2TrxrfGpIdem3AHKjFkBdPXJVoZYatJg=; b=Xu7q3r+DqDFVtblArzSJqPGrtFPdF6DHiwEIPRUOCyzxl9BhC0R8UXgkefjULD/67k 35kRGeotBg4xdoF4iSohvpLpgGfwMrNGaq3ILQeQWdWCPnyYK0BhV8HAftTgGfx+oENs OKzXg8X5tencd2/O2VVC7YlgpHOXhZ8gNDcIJaIBPR8kh1xaWHl2rU0vt3C0ufJ32xT9 kEMzgkzBFefC2XX5ZHt3Wl7yqzaOILBhExsYokyaRD/kATHjMhI7E3azJKpa2DJnmczq 2pGu++9wJT/+gKJNFk3ezZxGCfm6znYg9H+EuEVIemSz395dSCgH+14W9WCRzg6mcEEh D6mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945417; x=1757550217; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=zy5btr4Cxic2TrxrfGpIdem3AHKjFkBdPXJVoZYatJg=; b=XnKyPKprSib2hlPq7LnBbsxs7h2by7U3ODKe8WkxmXTKTq3tPGb6Z0Mh4DfntvyXZg SF6yrGS8RR1MDsU9OpxXdhTbRiy+FHFUlKHIeJSsMNGlnrNrvEaHwXUnmUpAOpLcO2/Y xBLSEtvFZHO0KXXr6jFzPExELxbO2bYLe/9hOCPF4wZBHWvlitcIBlCfPXFP0fqVs1AK v5KjerW6a+B5i4v2BXvsKT4J9lJB+E41cF9W9SSCZcasONeXo4n/sHQ8ZZU6/6W6u3gP XuR1spKir5iz5IImSb4lJwau0QI4BouwxpIUIns/51rMJ++8K8/Bok1MFr6kBLaM6mn0 Yt4Q== X-Forwarded-Encrypted: i=1; AJvYcCWEJilMzta/GxPaTmpgwM4ijkw2GtaTAP+Zb+vfWh+NSSBjeEDfWcXKTdxKMKHeOIXjT21XFqrWpzbxBYWMYJcZ/3yA@vger.kernel.org, AJvYcCWdtk/JVY3I3VmSSr2YymILTGRb+GkpIyl/9QeUkAGNP+1mfGjmEYXB1XhAFIXypuSYnu/9IGKCknJ+YMuqxptd@vger.kernel.org X-Gm-Message-State: AOJu0Yz6hgZtlneLKN61j2+XD0CrVVu9Thpl3fH4cXZWKHkeFlvZ3DK6 MbRl/02qU3zDorqZaTqj/IcAseXkQ45uerS1K43kfJAi0SI66Meg9lY9 X-Gm-Gg: ASbGncvm9Kzfgx0S3ZtwJs13FwqByle7ylzvZDqv13x5CQ6cq5xbWfhu0Bilrp2HAd1 tVK1WSZQkbQra2399mL0B1SK8qcSVqo0KSQWLctOBJYeusVDxB+dqFc0qrYk6fToi4mJxLvw+NE EWVaVSZRQeSlly7wEx3bDA/l2z6nS7cAGe79Hqvd+n8X07SwLN4r0kXtT4zWu+C0aUFofxwELu9 y59v+gUmNXf7Wi7v4Tt2i5Jdob+qqp+vOYIQpP80MqLWIPlNLmjWBKfqtxA/8MjLyq1vy64ngWt n+YDU2deC12xOkoDPvsKw8/ckNduV3Xm4c+oG5u9uT44LRbZL25tjhKtUC+CZrBH6N+VBmO47nR xX2ls0HhSEATEXvANYoZOD5gBfLtbzRMszju90XHzcQQrVxEOPeiRTq7m2BOVpmg= X-Google-Smtp-Source: AGHT+IFj/QUfdNr54DaXrxUfOwjpV2lFcaLFWb1JY2iCOe+1SSplj0UymNXEqzVyRY1fytwFbBj4MQ== X-Received: by 2002:a05:6a20:6a22:b0:243:7617:7fbb with SMTP id adf61e73a8af0-243d6f3941fmr24535766637.43.1756945416828; Wed, 03 Sep 2025 17:23:36 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.23.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:23:36 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 12/18] mm/ksw: add self-debug functions for kstackwatch watch Date: Thu, 4 Sep 2025 08:21:09 +0800 Message-ID: <20250904002126.1514566-13-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 ksw_watch_show() and ksw_watch_fire() for runtime debugging of kstackwatch's watch mechanism: - ksw_watch_show(): prints the currently armed watch address and length - ksw_watch_fire(): forcibly triggers the watch by writing to the watched address These functions help validate the dynamic watch behavior and facilitate testing without requiring real events. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kstackwatch.h | 2 ++ mm/kstackwatch/watch.c | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index bc8664af4fa6..d1bb5ae75aae 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -47,5 +47,7 @@ int ksw_watch_init(struct ksw_config *config); void ksw_watch_exit(void); int ksw_watch_on(u64 watch_addr, u64 watch_len); void ksw_watch_off(void); +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 c563f8d17829..8f3f6cb9f17a 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -116,7 +116,6 @@ int ksw_watch_on(u64 watch_addr, u64 watch_len) ksw_watch_on_local_cpu(&watch_info); } else { call_single_data_t *csd =3D &per_cpu(watch_csd, cpu); - smp_call_function_single_async(cpu, csd); } } @@ -158,3 +157,19 @@ void ksw_watch_exit(void) =20 pr_info("KSW: watch exited\n"); } + +/* self debug function */ +void ksw_watch_show(void) +{ + pr_info("KSW: watch target bp_addr: 0x%llx len:%llu\n", + watch_info.addr, watch_info.len); +} + +/* self debug function */ +void ksw_watch_fire(void) +{ + char *ptr =3D (char *)watch_info.addr; + + pr_warn("KSW: watch triggered immediately\n"); + *ptr =3D 0x42; // This should trigger immediately +} --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f177.google.com (mail-pf1-f177.google.com [209.85.210.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 C01141C5485; Thu, 4 Sep 2025 00:23:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945428; cv=none; b=tS2COpICteMrdpqm4khMEH/psnOe8217nKNakpXa7lZrPhEgjWs6scaz4egA/UX0LSWLH+ev2Zyge5gT0HwVRK2aVicMw59ehaucitZKbQcvOXOInWuu8502zkBdlX+KG6/lX9Gfxeq8WhUotA5XxGpFtx3ms6lXudbjVU9ZJm8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945428; c=relaxed/simple; bh=qkcftcj0fqlwmdDkYmum8il+ZDjAfjmbv7Do9ex3a28=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=N9qtyFpgrTkWzvzJzZ6uUIg+XrNBBbzcRHx9iTyhWkAMU3rl5jp6ECXjc8mjDevLeG9zOLp5bjMoDQFQGoc9S3/OH23PeoFr/WhFDgJU47aCG3P0Zxz4r/eF52Azkz0FpCNX44+dt5QF97NkJHGF2fKJbAZj110mTR1h7bd6SSo= 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=D3PPhdrO; arc=none smtp.client-ip=209.85.210.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="D3PPhdrO" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-77269d19280so448210b3a.3; Wed, 03 Sep 2025 17:23:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945426; x=1757550226; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HEG4bH5cgwUC7a1orTI7hmUJnz956GpzDiyDWs45yMQ=; b=D3PPhdrOpbTz8iap4nBY6XtGhmMUgBnDgezx5H/fbpbPt+CTAQyzJuDYhN0UbnqNOL PCZhGsX3SrpQojEyv63hf58UO67y96GhZ2hUAEuawbiAA0/dzghIYdhL4J2e3NwhteW8 5HYt/VZ9Ib6ghCn/wx/E3m+9Xb4RJqr+5B/XWv09ZWiwJcdiJ6+aTWWhDVz7cb86VKYl y79fI8lH7v6wWMuaBSjEZ+Abr4pCgfCgFsr3/fJaBWemud/0VcIRFGqdnB1RTCon+r/y EqweQSM6jvrmmwL06FmJdRKFkQ+FFUL1Ff033fRuFFs8dKFSagpj1JQe2O79XHvS6aAd nV2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945426; x=1757550226; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HEG4bH5cgwUC7a1orTI7hmUJnz956GpzDiyDWs45yMQ=; b=DFyV5qZPpDxZUq1uHBbvPudei0gocGyZTGRadO3LGa7lQQjDWE31B5gugKkMp8e+w6 i1lSFzz4zVWsmIzbu1swffjFlRhzKcV7GIdARpLyIzwEN5s6nfxKgG3qxTdauOA1o4XX HU4AUy+KcgMDx0Dw4/342/I/lrV+T00DNEKKaE1Bg+m2uWNu53SvU+42VvCgTWj4Ua+q 5DvxHqCtTwEm+GaugODtI99xpo9Gncbarr8tFM8UVRqgJnOMhgEmZMqJjVxiyl66kW6q OwdTgcVUZ41JDxL3sxF3cXXSAdC9LhBWe+p68Ga/KvVta6+pIT/hbV8+/QZDZVQElOn5 JiAw== X-Forwarded-Encrypted: i=1; AJvYcCUNJbsPLvjcA+Jj6Ycr08CSDG/KfRLHZQ4R+dbuqPl5AREZBvVDWD5z8XnlJPx45UAlnugiTYC3Aa5pw8s6h0nx@vger.kernel.org, AJvYcCWk3X0DXZLwdo/0rYv7A4BVUhTH5Zy+m+lTFOWoEDCheKwBhDGXGW2fpbhvnG8wct5HWaa9xH62slRW4c4rNbmECtz6@vger.kernel.org X-Gm-Message-State: AOJu0YxO/YrieKm6u90QL/qmOPSLeDNksOGUeN3/fhf9xAhwTSJ6PMbJ qYTVUN1TT1tzfId1eIEmLF8JmHpmcwM2DNA0rNfDgHic8lsd4K7wnX/v X-Gm-Gg: ASbGncuMKKPMMf1cD/eLpbhnIX6+N/yiymhUznk/WA0PnY+aVe37etamOktbwwdeeA2 x6URsQ39WwTakpC35wGz3G53aq/hIGpm9YTTsF8UlWzeS4BWpmGRrrEgElzqED8APaFvyR9r0f3 MN4XX6U4ZcteNt4DRE9cKkIMxyoWKpC9xh16RaVoS7V+pOrWvIPF0CpEsgTHBxG1KV1cDmMFp0E zCj/To6xrZrRA7ud06RXzptUyEQM5VBU+r1K0M7lhpTRRgVg2+nJM7AfCny9k6HphgkzQCI/YXM Y2m8nHFt1uAR4DBSRCo0LFQ24CzNhAgMsmesfAJGK7zo57HQXqMa4qlsD2LhDfb3/iCctj4qU9s 0na7eaMtTV9++rwuVP7PPEjtTDprktcD7WvFZQMhMj/QDhPOPpHRX0ieNYuTNvwr8bhg1cXz6UA == X-Google-Smtp-Source: AGHT+IEr/C81GB/BuP4Yk9gK7yApaaYJOMu3ISoyiOcC/P42G0UqOztnrGwzfHBNa2zOn5PIjEynzg== X-Received: by 2002:a05:6a20:9187:b0:24a:b2eb:6676 with SMTP id adf61e73a8af0-24ab2eb699emr1717825637.17.1756945425818; Wed, 03 Sep 2025 17:23:45 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:23:45 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 13/18] mm/ksw: add test module Date: Thu, 4 Sep 2025 08:21:10 +0800 Message-ID: <20250904002126.1514566-14-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 separate test module to validate KStackWatch functionality in controlled scenarios, such as stack canary writes and simulated corruption. The module provides a proc interface (/proc/kstackwatch_test) that allows triggering specific test cases via simple commands: - test0: directly corrupt the canary to verify watch/fire behavior This helps development and validation of KStackWatch without affecting normal kernel operation. Test module is built with optimizations disabled to ensure predictable behavior. Signed-off-by: Jinchao Wang --- mm/Kconfig.debug | 10 +++ mm/kstackwatch/Makefile | 6 +- mm/kstackwatch/kstackwatch_test.c | 118 ++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 mm/kstackwatch/kstackwatch_test.c diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index c2cc8c7b595f..7fdf7b03d458 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -319,3 +319,13 @@ config KSTACK_WATCH the recursive depth of the monitored function. =20 If unsure, say N. + +config KSTACK_WATCH_TEST + tristate "KStackWatch Test Module" + depends on KSTACK_WATCH + help + This module provides controlled stack exhaustion and overflow scenarios + to verify the functionality of KStackWatch. It is particularly useful + for development and validation of the KStachWatch mechanism. + + If unsure, say N. diff --git a/mm/kstackwatch/Makefile b/mm/kstackwatch/Makefile index d422f0e114dd..c04c0780da01 100644 --- a/mm/kstackwatch/Makefile +++ b/mm/kstackwatch/Makefile @@ -1,4 +1,8 @@ obj-$(CONFIG_KSTACK_WATCH) +=3D kstackwatch.o kstackwatch-y :=3D kernel.o stack.o watch.o =20 -CFLAGS_kernel.o :=3D -Wno-error=3Dunused-function +obj-$(CONFIG_KSTACK_WATCH_TEST) +=3D kstackwatch_test.o +CFLAGS_kstackwatch_test.o :=3D -fno-ipa-sra -fno-inline \ + -fno-optimize-sibling-calls \ + -fno-section-anchors \ + -fno-pic -fno-pie -O0 -Og diff --git a/mm/kstackwatch/kstackwatch_test.c b/mm/kstackwatch/kstackwatch= _test.c new file mode 100644 index 000000000000..bba2ab8530ed --- /dev/null +++ b/mm/kstackwatch/kstackwatch_test.c @@ -0,0 +1,118 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kstackwatch.h" + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Jinchao Wang"); +MODULE_DESCRIPTION("Simple KStackWatch Test Module"); + +static struct proc_dir_entry *test_proc; +#define BUFFER_SIZE 4 +#define MAX_DEPTH 4 + +/* + * Test Case 0: Write to the canary position directly (Canary Test) + * use a u64 buffer array to ensure the canary will be placed + * corrupt the stack canary using the debug function + */ +static void canary_test_write(void) +{ + u64 buffer[BUFFER_SIZE]; + + pr_info("KSW: test: starting %s with u64 write\n", __func__); + ksw_watch_show(); + ksw_watch_fire(); + + buffer[0] =3D 0; + + /* make sure the compiler do not drop assign action */ + barrier_data(buffer); + pr_info("KSW: test: canary write test completed\n"); +} + +static ssize_t test_proc_write(struct file *file, const char __user *buffe= r, + 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("KSW: test: received command: %s\n", cmd); + + if (sscanf(cmd, "test%d", &test_num) =3D=3D 1) { + switch (test_num) { + case 0: + pr_info("KSW: test: triggering canary write test\n"); + canary_test_write(); + break; + + default: + pr_err("KSW: test: Unknown test number %d\n", test_num); + return -EINVAL; + } + } else { + pr_err("KSW: test: invalid command format. Use 'test1', 'test2', or 'tes= t3'.\n"); + return -EINVAL; + } + + return count; +} + +static ssize_t test_proc_read(struct file *file, char __user *buffer, + size_t count, loff_t *pos) +{ + static const char usage[] =3D + "KStackWatch Simplified Test Module\n" + "=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\n" + "Usage:\n" + " echo 'test0' > /proc/kstackwatch_test - canary write test\n"; + + return simple_read_from_buffer(buffer, count, pos, usage, + strlen(usage)); +} + +static const struct proc_ops test_proc_ops =3D { + .proc_read =3D test_proc_read, + .proc_write =3D test_proc_write, +}; + +static int __init kstackwatch_test_init(void) +{ + test_proc =3D proc_create("kstackwatch_test", 0644, NULL, &test_proc_ops); + if (!test_proc) { + pr_err("KSW: test: Failed to create proc entry\n"); + return -ENOMEM; + } + pr_info("KSW: test: Module loaded, use 'cat /proc/kstackwatch_test' for u= sage\n"); + return 0; +} + +static void __exit kstackwatch_test_exit(void) +{ + if (test_proc) + remove_proc_entry("kstackwatch_test", NULL); + pr_info("KSW: test: Module unloaded\n"); +} + +module_init(kstackwatch_test_init); +module_exit(kstackwatch_test_exit); --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 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 1C49D23ABB3; Thu, 4 Sep 2025 00:23:54 +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=1756945436; cv=none; b=sjGuM1oQQZQFgQPf0r0KcqednZOWh8SsRDwSKCfHpVDrvV4/SvdDDi7ivo0rjF0B+Y9hqcgaoZOMpkPTibR+D3SjcH1GR6S6+D2Fqp6LzvUdViF+KatH1/EUPeGtT2TKALCRLjcmCH4Py/KIOr/UGuctrMXUvrPexzchXWqo7T8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945436; c=relaxed/simple; bh=6rw+m4VKvEY+PNH3LtpXPUlmxSfDZ834B7ADRGFPwkE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hKiGNcWq82kcOlUvMlGgdizDjIMRgCiRoo/EgRgev6OCwhRA749A4bbhbU/vtmfCYrMM+g0OHjjKJFpPcEMWaSUPEs0grSh+FMHKSP07hiQGsHmkfVrJrVTvawVXx0/kIEfcXZLv/I0zbpClNuYp61ODQ6Y5pfu0ftOSm36bvnc= 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=jNddLgAM; 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="jNddLgAM" Received: by mail-pf1-f182.google.com with SMTP id d2e1a72fcca58-772301f8ae2so429143b3a.0; Wed, 03 Sep 2025 17:23:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945434; x=1757550234; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Or2e3wHrlUvOPFgjfY3GC0NcILvcAZ+AmyV41U5R5a8=; b=jNddLgAMkQZecu8JgrjseupFX3pyoNTJYWsuwLSa+LXSbOPuXKbg1ODRDuVgEsS0iW vepmWL7rJpWFOsT7Y2Tl6RcuCQX30Kc9O5QiahAMeKmbYQoFd1bhOjelwegSO4hD86M3 +nz1oo/q/uFZm9RtOCg34Jrz+Zyi7qg0yc8BpBszuKcIJAKnieLEQP2pp1KrnyLkBxmt O+hBuqKLLlQL7EpErsh/k/YOADh7apn/3FQ6F8lUxPItfx+x0EmuiPw9pwWCkiSFJjkC Nnyc9ATBEFeWf+0xmou4FXbA90BxeXo9ehBecEGrTBHuvxJuIS69vyADqpQLt5GJUW9O wFbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945434; x=1757550234; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Or2e3wHrlUvOPFgjfY3GC0NcILvcAZ+AmyV41U5R5a8=; b=IzlQIWNs56SmVGmsCrmkIkOnwt4MCXWPV1xWCCZEI+Ye8jZUjeCTgvLF7LBPb036f/ UzufvYbtQ195DQLjn9u6v1yUD5Jb5muPxztl51p8zxt+8tAVazN8KRnwMMpBK0UrVue+ 0Nhj0YLJeA8cEa1MT0sniUqRK+neJ4QEGFbg8hfO67CYsY58AFh56K9uExjgOeIAXmaR hnCkVqShpA/a1AReWPT4uqMBPoGGZWwgeKzT0eyEL/a3URxTQ6rdBswGp6SZjQuXo/ts P1WlscLuf1429il/SF1ObFySSXI4qjNrAtiWkPh8GItjF09SRJYcSUKFhOQvMojM+Eqj URVw== X-Forwarded-Encrypted: i=1; AJvYcCVEkUzfl6096x9M+6b6fCUlYfz/d6wdFstaAOmBdUaYXWPk9qNsY4bZgtcGJUpFeN0wj0rTwgjcrlW5+gZ6/k1b@vger.kernel.org, AJvYcCWBvqGLJBR28W3wIcaplYq1SW+Fg/vSVWUuI8KVIzkGysTDqvhTEoZN47Upvq0Sym3bPprgsjJQR28/3CNZGDEtu37C@vger.kernel.org X-Gm-Message-State: AOJu0YzayQEVf8PWcnRKxyg/YkuCzMzkENDExjrTWGTDyWdn+WeEWI+6 lhuGohpdHvR00g+KwBPM1ZUnn8NHHXV2iKCc84NQccPnwdAyzWX4nuDi X-Gm-Gg: ASbGncuRfz83GOMGzc1poTmDXZw8dExztZ83DqCch/3F9SraWIkEpyQOag1DLd7zNiA 101ptLJO3CSrvz1GM+62dnmj8XAt3UGiQc6NnwKxfWL2H1o9fnyc6FiAoXMPrQSOVphfxJJU4mI pGF2Ru3/M3twM0oIMAZw/NaskZNFqB+68m/GeFZOLKLifYYJwrQGuLN5bOIPyTzEMdAwovYTMm4 c1L1jle4OeUhvwIZMPSs3cO3ckZRkjUboUog0tt9B/xURZFgm1z/L2Vmf+GGf/fMv+sd+8Um07h SEE19curPsihH7JZG5hry7HvduYhUe0WVREGvc5MX/W/DdvVbXPLZi0uU2MfQ+eA4WKiZ2tCbRq XQx1M2PvDeHh2/oP8dyEosToA/Xh+Ig5K1v1RjoW2sQ8kN5qGKmXhtC6DSN9raE0= X-Google-Smtp-Source: AGHT+IEFRgUm8G3fOt9Njei098V+G0dLsLeU/egZbWUWIOvW90ec1jG3Rb7Noxb0HUZlTjRFfu3Imw== X-Received: by 2002:a05:6a00:1399:b0:771:e2f7:5a12 with SMTP id d2e1a72fcca58-7723e1f0883mr21918504b3a.6.1756945434094; Wed, 03 Sep 2025 17:23:54 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.23.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:23:53 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 14/18] mm/ksw: add stack overflow test Date: Thu, 4 Sep 2025 08:21:11 +0800 Message-ID: <20250904002126.1514566-15-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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. This helps validate KStackWatch's detection of stack corruption under overflow conditions. The proc interface is updated to document the new test: - test1: stack canary overflow test Signed-off-by: Jinchao Wang --- mm/kstackwatch/kstackwatch_test.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/mm/kstackwatch/kstackwatch_test.c b/mm/kstackwatch/kstackwatch= _test.c index bba2ab8530ed..138163472b03 100644 --- a/mm/kstackwatch/kstackwatch_test.c +++ b/mm/kstackwatch/kstackwatch_test.c @@ -42,6 +42,27 @@ static void canary_test_write(void) pr_info("KSW: test: canary write test completed\n"); } =20 +/* + * Test Case 1: Stack Overflow (Canary Test) + * This function uses a u64 buffer 64-bit write + * to corrupt the stack canary with a single operation + */ +static void canary_test_overflow(void) +{ + u64 buffer[BUFFER_SIZE]; + + pr_info("KSW: test: starting %s with u64 write\n", __func__); + pr_info("KSW: test: buffer 0x%px\n", buffer); + + /* intentionally overflow the u64 buffer. */ + buffer[BUFFER_SIZE] =3D 0xdeadbeefdeadbeef; + + /* make sure the compiler do not drop assign action */ + barrier_data(buffer); + + pr_info("KSW: test: canary overflow test completed\n"); +} + static ssize_t test_proc_write(struct file *file, const char __user *buffe= r, size_t count, loff_t *pos) { @@ -65,7 +86,10 @@ static ssize_t test_proc_write(struct file *file, const = char __user *buffer, pr_info("KSW: test: triggering canary write test\n"); canary_test_write(); break; - + case 1: + pr_info("KSW: test: triggering canary overflow test\n"); + canary_test_overflow(); + break; default: pr_err("KSW: test: Unknown test number %d\n", test_num); return -EINVAL; @@ -85,7 +109,8 @@ static ssize_t test_proc_read(struct file *file, char __= user *buffer, "KStackWatch Simplified Test Module\n" "=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\n" "Usage:\n" - " echo 'test0' > /proc/kstackwatch_test - canary write test\n"; + " echo 'test0' > /proc/kstackwatch_test - Canary write test\n" + " echo 'test1' > /proc/kstackwatch_test - Canary overflow test\n"; =20 return simple_read_from_buffer(buffer, count, pos, usage, strlen(usage)); --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 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 1F1EA199E94; Thu, 4 Sep 2025 00:24:04 +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=1756945445; cv=none; b=A6t9qnV/2sOM9NOCHLDeMEiQheI3D20Mlqt8VTlUPt9kjeWNmCBmkKjpSLTGRHSTO5S65XrTnRL6JEeQwXPFPkbhtaQQR2vyigIe8R0hr8PX4/TavAU4qrIvb0fUxaZej/rkDLzVBzj9XX2PHEFichn1mbCdpPCRCpv2WRhij/A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945445; c=relaxed/simple; bh=LZfImMzMcwQbupG4M6+Sd95ddnMNs/MgYxCY/DAN4QM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Bg+bvWHZubKkF0QxnmOd39hff0CwW2rYa2daJRNLTqlyFMgKhwCX7Hnu+aLOxx7XLhSyplAnqPTkDOVX+1r3QRSyrPloQM/pVrmEb99rQSV+YhZOQBPe6LiHZwIyFk0bywVVU/fm7vhDGo2/qYoWy7eHvvhZFbSAWZglnP6hQ5E= 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=V0+wcD3z; 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="V0+wcD3z" Received: by mail-pj1-f52.google.com with SMTP id 98e67ed59e1d1-32b863ed6b6so334358a91.2; Wed, 03 Sep 2025 17:24:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945443; x=1757550243; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MrzeI3EYnXpSYBJKftFAUOvxevvy4jLNuncba+XGBDk=; b=V0+wcD3zAY0DWXW0Ob86jXpmPq0Yz6BmXET9ipNLk9fy20hS1GtA4pXlNs1c+A8Bdq QBC4Qz2r/3XmqHSNdNEo09ruunOInYlYtOV99IgEfZXfgqQNn3tU5dJvDVolw9vg8VrE 0QFtq00kGxlWAlQ0fv8sXA95WcVGwOvVyxNrwhBTQ3xVHMwdK73Aw7ceYdbLAWR6Uj3G IvT+Y+DTGZDibqQXuQHD04Nvp4AAcaCiAtj8Dd9bTiBo1nMwFt1KW7FoVV+tVFYJU9uY GXA/AWzkNFfnhP0nsiyW3rcnYXOczkm0IXmHcobfTq/4iCtvdq15bsf2dzwn5eNLRq1Q ct5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945443; x=1757550243; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MrzeI3EYnXpSYBJKftFAUOvxevvy4jLNuncba+XGBDk=; b=InDczYQeVeVJcGTDTuxgnTeA/ZJxX9mzlNE5liBv+jKq6IQe+hUkB5ECugkmE4uGSp jnd4itueGzY2pPD6TraFQOxilBW/z4E2VTUkd1HxBWThelLBGIOn+x6QbGRrEcWULjxb lEy7PUyTx4uH4JLSaTI0uCQmQuK5y/fXEoCyREhXNh8QB8S9e43SpAL/zUFs3dtG86dz eq+cG5fD1kYm1fzop3GMhmf8ZVtvEvqBGZRsZZg56GjSWj2BrQ79XGCkJY6KqdRVmvKm +hNJCcb+ww3eyLVrwRRIjo92DInDdX8InxJ5YoqinqoZlAwI/KS8OFhwDhNqrg6YDDqU SZDA== X-Forwarded-Encrypted: i=1; AJvYcCXR9v4grtH++yGkcsYAMfyQCDBWvQ6JTwLr6ciGI8tfKJtUl++7Yg+Q/jx4UkDd3egWZ82ntbVvVOZMqk9sKEEW@vger.kernel.org, AJvYcCXg2T6F2F3Mv70aN2eU4/nGutI/VxklTJ0cZFzu20V7nvVyFX9OEGzoMqc16yjW0gNd3aSro6hNb0RTLikOarVXQuiu@vger.kernel.org X-Gm-Message-State: AOJu0Yz9c8pG+hR4f3zHlje6DAKGO37tADoeZGTQF25wXpibDVUlsgGt 5EC1wEdNdsJKG1wSwXqEtfSm1M6VQj7C9vpvMOVDUJb1EQKI9tTNMoR1 X-Gm-Gg: ASbGncswQB8JdRHJQdKZ6jX9nBYeBRT7yHKk1OmKVpkZH4BCOiw006nYxCx1BmyBBwv gIBgz9HC6GJUnGS5KoO3xibmS2Anx7MhOW2bDds0Z8LBtumqDnByaRyFpxB8a8VrpzAC6rAXqk3 CX2crbr2LmQBFBmbTo6hJ1uEBYMscVYQoR7q+x1LcHm5avSIUCv1W91dtGARdUWBv9HcCKgSqbM 8q6SFlt1Rlj4LkCjUwL7l2QHNmfLZUVK0x6VA+Y01SET9qK2aHImpORzzrK/6qhtR0pOwvy0G32 ilJ+clTOZpxwxDOLn+ppg2sxhLwPrsew3xNO9uOdLVJFbIJ9sTA5piWNZWkaA2uM9f9uPFfZ9gc N5flkVuABe5WTeZBxw+NxP2UfoEVDTv9owJX3o30/NGA1sSM896BOGsbvBe3GIFM= X-Google-Smtp-Source: AGHT+IF59l6Y6il6sWe+ojfk5O2VEV4/iuJ8NUJrR5PDNiGZHhFNdzPEip0wnBYNg8k9w5p76AV22A== X-Received: by 2002:a17:90b:2e04:b0:32b:5195:d124 with SMTP id 98e67ed59e1d1-32b5195d364mr5800597a91.11.1756945443320; Wed, 03 Sep 2025 17:24:03 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.23.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:24:02 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 15/18] mm/ksw: add simplified silent corruption test Date: Thu, 4 Sep 2025 08:21:12 +0800 Message-ID: <20250904002126.1514566-16-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 Introduce a lightweight test case simulating =E2=80=9Csilent=E2=80=9D stack= corruption where hapless() is unaware its local variable may have been modified. This test is much simpler than real production scenarios but demonstrates the core logic. Test logic: - buggy(): exposes a local variable via a global pointer without resetting it, creating a dangling reference. - unwitting(): a background kernel thread accesses the global pointer and modifies the pointed memory. - hapless(): operates on its local variable, unaware it may be modified. This controlled, minimal scenario provides a simple way to validate KStackWatch=E2=80=99s detection of unintended stack modifications or silient corruption. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kstackwatch_test.c | 90 ++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/kstackwatch_test.c b/mm/kstackwatch/kstackwatch= _test.c index 138163472b03..1f0d616db7c5 100644 --- a/mm/kstackwatch/kstackwatch_test.c +++ b/mm/kstackwatch/kstackwatch_test.c @@ -22,6 +22,9 @@ static struct proc_dir_entry *test_proc; #define BUFFER_SIZE 4 #define MAX_DEPTH 4 =20 +/* global variables for silient corruption test */ +static u64 *g_corrupt_ptr; + /* * Test Case 0: Write to the canary position directly (Canary Test) * use a u64 buffer array to ensure the canary will be placed @@ -63,6 +66,86 @@ static void canary_test_overflow(void) pr_info("KSW: test: canary overflow test completed\n"); } =20 +static void do_something(int min_ms, int max_ms) +{ + u32 rand; + + get_random_bytes(&rand, sizeof(rand)); + rand =3D min_ms + rand % (max_ms - min_ms + 1); + msleep(rand); +} + +static void silent_corruption_buggy(int i) +{ + u64 local_var; + + pr_info("KSW: test: starting %s\n", __func__); + + pr_info("KSW: test: %s %d local_var addr: 0x%px\n", __func__, i, + &local_var); + WRITE_ONCE(g_corrupt_ptr, &local_var); + + //buggy: return without reset g_corrupt_ptr +} + +static int silent_corruption_unwitting(void *data) +{ + pr_debug("KSW: test: starting %s\n", __func__); + u64 *local_ptr; + + do { + local_ptr =3D READ_ONCE(g_corrupt_ptr); + do_something(0, 300); + } while (!local_ptr); + + local_ptr[0] =3D 0; + + return 0; +} + +static void silent_corruption_hapless(int i) +{ + u64 local_var; + + pr_debug("KSW: test: starting %s %d\n", __func__, i); + get_random_bytes(&local_var, sizeof(local_var)); + local_var =3D 0xff0000 + local_var % 0xffff; + pr_debug("KSW: test: %s local_var addr: 0x%px\n", __func__, &local_var); + + do_something(50, 150); + if (local_var >=3D 0xff0000) + pr_info("KSW: test: %s %d happy with 0x%llx", __func__, i, + local_var); + else + pr_info("KSW: test: %s %d unhappy with 0x%llx", __func__, i, + local_var); +} + +/* + * Test Case 2: Silient Corruption + * buggy() does not protect its local var correctly + * unwitting() simply does its intended work + * hapless() is unaware know what happened + */ +static void silent_corruption_test(void) +{ + struct task_struct *unwitting; + + pr_info("KSW: test: starting %s\n", __func__); + WRITE_ONCE(g_corrupt_ptr, NULL); + + unwitting =3D kthread_run(silent_corruption_unwitting, NULL, + "unwitting"); + if (IS_ERR(unwitting)) { + pr_err("KSW: test: failed to create thread2\n"); + return; + } + + silent_corruption_buggy(0); + for (int i =3D 0; i < 10; i++) + silent_corruption_hapless(i); +} + static ssize_t test_proc_write(struct file *file, const char __user *buffe= r, size_t count, loff_t *pos) { @@ -90,6 +173,10 @@ static ssize_t test_proc_write(struct file *file, const= char __user *buffer, pr_info("KSW: test: triggering canary overflow test\n"); canary_test_overflow(); break; + case 2: + pr_info("KSW: test: triggering silent corruption test\n"); + silent_corruption_test(); + break; default: pr_err("KSW: test: Unknown test number %d\n", test_num); return -EINVAL; @@ -110,7 +197,8 @@ static ssize_t test_proc_read(struct file *file, char _= _user *buffer, "=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\n" "Usage:\n" " echo 'test0' > /proc/kstackwatch_test - Canary write test\n" - " echo 'test1' > /proc/kstackwatch_test - Canary overflow test\n"; + " echo 'test1' > /proc/kstackwatch_test - Canary overflow test\n" + " echo 'test2' > /proc/kstackwatch_test - Silent corruption test\n"; =20 return simple_read_from_buffer(buffer, count, pos, usage, strlen(usage)); --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 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 92E9478F20; Thu, 4 Sep 2025 00:24:13 +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=1756945455; cv=none; b=KOKhAOwEhI+rGzAgh5PLvTVY2f3GfrTQ+a3JASiWHKeBjR25YrBmpWNlq4rqDqbos2wY7lu4egFrnZtkg4U0DA1zTwKlQgTK63wCNmB/dJX1xanrGOoxT+MdMJg/7FzRlSWH3F5I7ANrGxxYXSg8qgGi370+7m8b9yn5JImT8s8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945455; c=relaxed/simple; bh=ivSu/IMaZGfW21FS3ZEvzo2NiJNn9PUIMXyecx0W7Ag=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZcwD+Ide2KucNSX4lLuq5MVLARqtXkYi5mPt0IS1Jt+dHDDLmROWf0fA+evphQd0CbCpX6aYcF1CfKf0uYc0BXg8uRpCpVLWtNXUEnIZJ0ZrIb/zfM+ZVT/BIdxoAORUMaRV+PRTiUMMNsqfoHFOvKonLFJaiQScfuydccLh1yA= 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=MoFE9I55; 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="MoFE9I55" Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-b4f9d61e7deso264130a12.2; Wed, 03 Sep 2025 17:24:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945453; x=1757550253; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KOiu/GBUWD+aWp2OIEycImwBUVzT2dU285cFy6GfNhs=; b=MoFE9I55HBvet4wMYS9nvP5jXQqcgS+UYW9myNrwefxQhCA7o+VhyMp9kCYdGRPrqZ X+lctpbHq3g8bGMm7s42xy2Dm/CVfR0d2EFysM01caP8OJRG7fYQZBMZyYUV7QUOmlWs hWRwIqqfQNdl1IiXa4+FNUKjliSHOsqBwTyWA3JS22SZahYx4IJgBikszXIkPglSk5WC SWU4gKdrjvlqiHI6wAaaTunfScM9p+7vAUnPtt9rBGZGoKOIGqM2gfkLCimXOyUoP5Mj OkoBF2FrmFkCoA3KB/Mx5Wk6IwRKfQKGFMyECrPG3LU1BkyB/oDPfbzstn5I7ek6Zoid 43Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945453; x=1757550253; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KOiu/GBUWD+aWp2OIEycImwBUVzT2dU285cFy6GfNhs=; b=Tl+1c7CHuefiARghrd5R5Jo9HweOtQvZl49slHmErqm3c1bK5L0yPArkkLmanGPuLk t6/KwZX0mliktPsIqN8vjkNtE844rGG+1HsmvUZEug7oPXiaEeKDJr0Lg2OuwS0wMUB3 mVUtTbHs9nbGbwaDwbrB0KZpjKv2YMGLkxQ6xzCOd1PZgKx7tGUT36dgnTUzbrWzqCs/ 3LoepfFOEdQN5TQ/ORdylFjlNtLtrUytOsYZvOTWHpITU6Qyx4rSVc0io9y0B5pMQAUA 4o7lOAwnD+e5KlpamC/pf+wJt05Pxq3lF6mS9UTfaOTan3Bo83QrQLzvIRGN5Y82FnL4 9P4A== X-Forwarded-Encrypted: i=1; AJvYcCUmlPs41aINGIqDa+vLgU2kOu76ORE2tnOInKerWOEN/Gz9zXoz9kGZd39gnMRBCG28UINZzRyx77bEEfZ7rlHU2gV/@vger.kernel.org, AJvYcCVo8eeenXSEenQXtx97kEwsavxhv6+Y4Xu6pIY0v0Uy9ZIH9iCvFkQv8NruRX+peGCCY9Upc/xzSr5ldb2rBVOJ@vger.kernel.org X-Gm-Message-State: AOJu0YwPD0UCmlIf7x+cjiNwARvsJlrVk3rgY8rm5BGo/Ker7l5kmCMC aTA24rnx5POYl0Znzi3Z73o49gNDDKi+3blPWHmXW/p4JJesL4KK7muR X-Gm-Gg: ASbGncvGTSONIWwv7v/ePG/iER6j1q6gNB+UIv13Ko9HeAra6oB5XjAL6vG31eW27xd h9pgltEtTyymHzYa9HEl9MHj2Rrtg2HpUVB8LJ1SVsN2ctv3ZilwfixEY3CJZAku3KV8ZW1eUjq OxRn8cpMt9P9Eci+oW/7T2rsYFUOihR0w7mcYO/dfsIBwRKwDXj+bfQg3P4SvjqWx8OW0DGcxMO GklQ+JlK9HD2tIs+kS5U2dqP/bfR4BixBr14LDWnQrgPM1x8Dv7u+UnDT3rQBQnKEJBSsDBGjbq bbqO9w1IqE2Mk5X65pA884oJI6BtH2MXpbWXOQ6mhPyN0iMEq+7O0b3WSL8NZRHfPWQ0ETm35AO Vq4ufZgokCVr0nKv2X78b6lOvCEFiAtogHEQZ9m8oySvtXXqu1yo7YZm949g3yTg= X-Google-Smtp-Source: AGHT+IGlCKz3wi42oRDwUIR9PQQ4OHO3kKDWN9VDgQVuoZDIBj9URRG5PxCTyk/oUZhJZ+LJU2AQvg== X-Received: by 2002:a17:902:c40c:b0:24c:b2a4:7089 with SMTP id d9443c01a7336-24cb2a4744fmr36622505ad.31.1756945452858; Wed, 03 Sep 2025 17:24:12 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.24.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:24:12 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 16/18] mm/ksw: add recursive corruption test Date: Thu, 4 Sep 2025 08:21:13 +0800 Message-ID: <20250904002126.1514566-17-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 case simulating stack corruption across recursive calls. This scenario writes to a local buffer at every recursion depth up to a configured maximum, allowing validation that KStackWatch can detect corruption in nested stack frames. Signed-off-by: Jinchao Wang --- mm/kstackwatch/kstackwatch_test.c | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/kstackwatch_test.c b/mm/kstackwatch/kstackwatch= _test.c index 1f0d616db7c5..cb216b6ee5d7 100644 --- a/mm/kstackwatch/kstackwatch_test.c +++ b/mm/kstackwatch/kstackwatch_test.c @@ -146,6 +146,30 @@ static void silent_corruption_test(void) silent_corruption_hapless(i); } =20 +/* + * Test Case 3: Recursive Call Corruption + * Check whether KStackWatch can handle corruption in a recursive call + * Write the local variable at every depth + * Configure /proc/kstackwatch to specify the corruption depth + * Verify that the watch is triggered + */ +static void recursive_corruption_test(int depth) +{ + u64 buffer[BUFFER_SIZE]; + + pr_info("KSW: test: recursive call at depth %d\n", depth); + pr_info("KSW: test: buffer 0x%px\n", buffer); + if (depth <=3D MAX_DEPTH) + recursive_corruption_test(depth + 1); + + buffer[0] =3D depth; + + /* make sure the compiler do not drop assign action */ + barrier_data(buffer); + + pr_info("KSW: test: returning from depth %d\n", depth); +} + static ssize_t test_proc_write(struct file *file, const char __user *buffe= r, size_t count, loff_t *pos) { @@ -177,6 +201,11 @@ static ssize_t test_proc_write(struct file *file, cons= t char __user *buffer, pr_info("KSW: test: triggering silent corruption test\n"); silent_corruption_test(); break; + case 3: + pr_info("KSW: test: triggering recursive corruption test\n"); + /* depth start with 0 */ + recursive_corruption_test(0); + break; default: pr_err("KSW: test: Unknown test number %d\n", test_num); return -EINVAL; @@ -198,7 +227,8 @@ static ssize_t test_proc_read(struct file *file, char _= _user *buffer, "Usage:\n" " echo 'test0' > /proc/kstackwatch_test - Canary write test\n" " echo 'test1' > /proc/kstackwatch_test - Canary overflow test\n" - " echo 'test2' > /proc/kstackwatch_test - Silent corruption test\n"; + " echo 'test2' > /proc/kstackwatch_test - Silent corruption test\n" + " echo 'test3' > /proc/kstackwatch_test - Recursive corruption test\n"; =20 return simple_read_from_buffer(buffer, count, pos, usage, strlen(usage)); --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) (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 0984820458A; Thu, 4 Sep 2025 00:24:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945465; cv=none; b=ty2//Yg17t6LMWJiEB7rVmDNdQOO/HMNtS3VHySemyMUtFuSzeNWGF/VrwnAlg9erNx9TYYC2Koc5GRQpPtBmiESBsezxoiD2UdUDUSInnTGwGuZntA6WAMI1FtUOe6FideX+SHhPl0d+U4hoHC8nhjCvx1SPZVtBWT7DXTH5HU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945465; c=relaxed/simple; bh=3tN/HRC+L41rVh+xg7/C6oFcNmhKFhjVH37L+PVpcpg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ed+OH+S4fmFnT9ub/eGPf8Rp/woxbZ2QnZ5Ix3tkwqiHgCpuE10/0RtQoREGV72rK1qzqw1XsDV+SLJ1PsIhdtyr0VOmLHQBpgqaV2QOlmXETdv1x86eh4KQe4PlRgSRySID+GdoJ+8Wxy8+BNWv+sVj+B901TsbwShK4s6diXQ= 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=dSQminmy; arc=none smtp.client-ip=209.85.216.43 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="dSQminmy" Received: by mail-pj1-f43.google.com with SMTP id 98e67ed59e1d1-327771edfbbso374929a91.0; Wed, 03 Sep 2025 17:24:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945462; x=1757550262; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=incKrp2UiMRL1bfFYHRaGnhm/dsia+B968fzxhF99tc=; b=dSQminmyNQQG72ol2z4mYrvANJ4OS3X1te2mK8oVpnWgtfPiPBfDUc4J2ua6BiW+ku LVic4Vh/Kh1CztcLa5VXHh9pTYyMMc9HfI9jPOIk3XnpkHvBexuou7kY8k057yMt4kys /X/zuYWOuZEu3tKwKZB6SfCZBpu606XC7qgrJN9ENdGfmLgAknYYD8hZ94cT5K1e+gsa Zg7e007oBPLSDxHdTPq0xIioO9q1b+GqsC3Nl4WGs7v/lmG+Pigc1T6SOmeOeASHaJOP YykHYB31p0YH66vOVwPhT1Acz34DY5vdJzGa8Ujlrr2WeqfU7HWH4bLerXWq3FzK81ou bkNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945462; x=1757550262; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=incKrp2UiMRL1bfFYHRaGnhm/dsia+B968fzxhF99tc=; b=xCy+rV6b9EXrkuUDd88CN9XZhb4h1uGKgcMLZF+xMHbKgSZNOKD7/6tlrjHphSxHjM jB3KhwyjRUhra465QYOzQG5Ssj6pGacp68CMJMSTep1l3DdvXbMgpTHUDoMtHOX+JLL1 M2fRovp8Pdn8iD43e+kEhOfd05f++Js1YH1gj4MJUv4KmXws9vAYrTxKx+3uvPAkZ37Z OzA1MBDr5eUs/m2S0n5W9cfOIyEy2H0PSxiE4Ru4NkV2VbssAt0hGUVDcqWUdDVdVXFu lZKyQUSWuTMib13WOKWQ67dx7wdQLn6YF+R8hzPh7vPUa0obiHy3dtfhYnfZRGAsf7kI yPWQ== X-Forwarded-Encrypted: i=1; AJvYcCU+2xtbHynuC01Ar2B1uPdUZNbKsQ1ZrssKL3GLSZ/8AiPC/25JmZ4GLpYtOz/l0ANdbgUGCm3+T8U7LoMcTMkFzh+4@vger.kernel.org, AJvYcCVip/VGTYkQhqInlLJ4s/N9MA3tutmzZ8/vdth7N0my/xzSzTpY5kYQ4/XaQrN4C8bHnXNl+PgIrkxlR2+aA4St@vger.kernel.org X-Gm-Message-State: AOJu0YzHEh0QFkp+Xh4vIeTRITIgKJ3vIVg4E0HM6UTaALeo6PO/osGZ tT46cEolISvtyWhCdassR0jVxTYH4/qHDkQ1gCfIXzxW5uX332WE9uZT X-Gm-Gg: ASbGnctif32wEadIhIfT2WiWkVFo8urN9oj/EbCpdjhjJUbi/6kNBlTY18L8msj0leH tCflui7XBhGpJszffXTTPZBL/tUjwPwU972YBujcYqnM4pzLW+m7gFXqond9AwgKEl82EvOQP/0 fDAbZHXTO/CibM9GmF5hP032Up+60uxWBO9zbfZvSUYg+gO6f966m/nSn73gPfshji4feE2gHQc kBzQllX+TQ2+npvNOifdep0Y/8PEc9jxFpBlMMQMVjrFnrXdpvvHIDK9VXQBIpuyRw0d1raCUr6 egw7mDLUJBhV1CtDIGsTp5OIz0TGlQ7Lococ0X0XT/uVQUcZoaPVpvxf4X/gCSO87QBGZvZ9HCb GN/1hjhQzyLJzpGN2i4cWEGrQIUUkCxHZg5oODl1HJ/szjRSSD+qK X-Google-Smtp-Source: AGHT+IEWdRv7pWRXFkXZkWNxZkP18IvQb4vEl1O/0LufV3mWJr4v4vPlNk8x7dizzsCLeMOYBwD13g== X-Received: by 2002:a17:90b:3c8d:b0:327:ea30:4 with SMTP id 98e67ed59e1d1-328156e0093mr20349272a91.30.1756945462223; Wed, 03 Sep 2025 17:24:22 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.24.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:24:21 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 17/18] tools/kstackwatch: add interactive test script for KStackWatch Date: Thu, 4 Sep 2025 08:21:14 +0800 Message-ID: <20250904002126.1514566-18-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 kstackwatch_test.sh, a shell script to facilitate running various test scenarios interactively or via command-line arguments. Signed-off-by: Jinchao Wang --- tools/kstackwatch/kstackwatch_test.sh | 118 ++++++++++++++++++++++++++ 1 file changed, 118 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..fcdb5ac9b607 --- /dev/null +++ b/tools/kstackwatch/kstackwatch_test.sh @@ -0,0 +1,118 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +# --- Usage function --- +usage() { + echo "=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" + echo " KStackWatch Test Script Usage" + echo "=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" + echo "" + echo "IMPORTANT: Before running, make sure you have updated the offset va= lues!" + echo "" + echo "To find your offsets, use objdump:" + echo " objdump -S --disassemble=3Dcanary_test_write vmlinux" + echo "" + echo "Then search for your function names to find the instruction address= es." + echo "- Instruction offset: address relative to function's start" + echo "- Stack var offset: distance from stack base (%rbp) to the variable" + echo "" + echo "Usage: $0 [test_case_number]" + echo "" + echo "Available test cases:" + echo " 0 - Canary Write Test" + echo " 1 - Canary Overflow Test" + echo " 2 - Silent Corruption Test" + echo " 3 - Recursive Corruption Test" + echo "" + echo "=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" + echo "" +} + +# --- Interactive menu --- +show_menu() { + echo "Select a test case to run:" + echo " 0) Canary Write Test" + echo " 1) Canary Overflow Test" + echo " 2) Silent Corruption Test" + echo " 3) Recursive Corruption Test" + echo " q) Quit" + echo "" + echo "WARNING: Each test may cause system crash/hang!" + echo "" + read -p "Enter your choice [0-3/q]: " choice + echo "" + + case "$choice" in + 0) test0 ;; + 1) test1 ;; + 2) test2 ;; + 3) test3 ;; + q | Q) + echo "Exiting..." + exit 0 + ;; + *) + echo "Invalid choice. Please try again." + echo "" + show_menu + ;; + esac +} + +# --- Test Case 0: Canary Write --- +test0() { + echo "=3D=3D=3D Running Test Case 0: Canary Write =3D=3D=3D" + # function+instruction_off[+depth] [local_var_offset:local_var_len] + echo "canary_test_write+0x12" >/proc/kstackwatch + echo "test0" >/proc/kstackwatch_test + echo >/proc/kstackwatch +} + +# --- Test Case 1: Canary Overflow --- +test1() { + echo "=3D=3D=3D Running Test Case 1: Canary Overflow =3D=3D=3D" + # function+instruction_off[+depth] [local_var_offset:local_var_len] + echo "canary_test_overflow+0x12" >/proc/kstackwatch + echo "test1" >/proc/kstackwatch_test + echo >/proc/kstackwatch + +} + +# --- Test Case 2: Silent Corruption --- +test2() { + echo "=3D=3D=3D Running Test Case 2: Silent Corruption =3D=3D=3D" + # function+instruction_off[+depth] [local_var_offset:local_var_len] + echo "silent_corruption_hapless+0x7f 0:8" >/proc/kstackwatch + echo "test2" >/proc/kstackwatch_test + echo >/proc/kstackwatch +} + +# --- Test Case 3: Recursive Corruption --- +test3() { + echo "=3D=3D=3D Running Test Case 3: Recursive Corruption =3D=3D=3D" + # function+instruction_off[+depth] [local_var_offset:local_var_len] + echo "recursive_corruption_test+0x1b+3 0:8" >/proc/kstackwatch + echo "test3" >/proc/kstackwatch_test + echo >/proc/kstackwatch +} + +# --- Main --- +if [ -z "$1" ]; then + usage + echo "" + show_menu +else + case "$1" in + 0) test0 ;; + 1) test1 ;; + 2) test2 ;; + 3) test3 ;; + help | --help | -h) usage ;; + *) + echo "Error: Invalid argument '$1'" + echo "" + usage + exit 1 + ;; + esac +fi --=20 2.43.0 From nobody Fri Oct 3 07:40:19 2025 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 848B2207DE2; Thu, 4 Sep 2025 00:24:32 +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=1756945475; cv=none; b=T7IsGDSyiF4TljWVd2h/IeD2VNf+AZSvtfG4aFBmHKeP72H8fZCPkXts3NzaTYOnMz/yzsFEom6DMzKXgMa0J+x848VqTiRPp9R9MXMVntTKTlMH9+gdytb7WelWI/z9yQPoP9LnxkBvOOkwvkpKO4H+pm+Wviv/Ga4B8+kR78E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756945475; c=relaxed/simple; bh=8M2/0UwLcXRxMD4ixlU21GMSrgoCNNEd15QxD/byhss=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rC/i1babZbtvjMVTaF7KzuheUEcs2zA1wUlf6c0nsGLgwB8vvW22tOcAIT3IQzDWCminMgeyekWqlCA5Sw93lkM93qpvps0uLIcnRb+Bno9IbZs8PrE9Dao2J29D6VBcqpOUboDZ1gaqj6plOHWTEa2KuyTNHJ52QYQGGlLw/F8= 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=RiJWxl+O; 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="RiJWxl+O" Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-76e4fc419a9so541362b3a.0; Wed, 03 Sep 2025 17:24:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756945472; x=1757550272; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=eP+OdhpAXeM3MeCoK1wrQ3GVDNoiOJzpSPwPKz/ThLg=; b=RiJWxl+OOBsOILiw8DNDisjKZCt+86CoJrAvJnZP8YYmysGAhsWzWfaOHUJcVk7w6l h9ym9e8zIFp42HO1vZyEZxLV13IFxA/1Mw5mS3e3MDsPfpVjQBH9+UeqpePyKk/XtFpY 9YQmLefde5N8/9uEL3x0zIGRn+ZrnrJi3B5QGDj8w8WJPY0i4iTxBI7PSO8CXaxelGZN tjE18IeSO3L6S8yKznzmrSMg8E9tXDjNbsyoD0tCZL4Mrk/4CqCuP3j9HJprB2EzPGIW ThOMRn5KbNmENoTTykSrN6q7uzp8lJBwfzeQc12+C38uJtFQjKUb3t/gAs8u7Mez1t2j fD7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756945472; x=1757550272; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eP+OdhpAXeM3MeCoK1wrQ3GVDNoiOJzpSPwPKz/ThLg=; b=UKc0b2IlrA5+r/kR+P11LjJVy4zf+gKrxW81C3xukD0Iaa2INNyrHQqrByIgCjQwAR KMSBrqFPqmdtV47d9iKevXYjOOva/e/yrephcAnOtb/jnJg8QQ9KRtspxn9JoFxfH3NQ 8OyXBKFKgcJGUkACvKhmNzBfXzMUrRl5teC9mraqapk2ncTjKM9qy6Em2n6tfJfe4Fuw GwLt0pU0iAc2KCv27sl5b87gUKJHFfvvDU2paJhtP35kVdVFw73KiVhizehj8CJ52Icr +2pP38FT3hRdu92vWG5aIKzzy03nb2UVH5fwlKI6ED6MNanbLHmcgCnQ+CIxFeC7Nnih MTvA== X-Forwarded-Encrypted: i=1; AJvYcCUKjwEYmSEIv0uar7zOr1tRmU9GBvT8aULhn6ajWiHg9mPtFkDLPhFRy/ezufqvrHZXmrLKEk1Rgge+vhC5XC5z@vger.kernel.org, AJvYcCVYgqShgLe2Qzp2eQoT6Ht5Nw6H7hvoNEds+qPDcCO4AYQd/nVoEz2aQugMDF9EA1GWj2FCupkou5JC5s0kJaqZSN+o@vger.kernel.org X-Gm-Message-State: AOJu0YxUkBDHTQ5J1ofJL6ujNzmI4UgyvtP5lTG8CTDLEfZOOEsGT1Sx mfG/aqS/FndiIQvhSZ5u59jMVufeYDhOXR0y9oSlxCm9llevmRtsuMGt X-Gm-Gg: ASbGncvognJMV0wnKpQQtc/VkdHAsnPCp6IdAw3xJ6ZSHg0y1Qs6jp+9j9wQp6JuHcj wO4KZLx6ZYF9q0fY/KnogKaeNv+mG2KlC6Y+fCPLAqAnAhL+4F5dDE3vfYBn9VXXeTwKgF1Xzy4 kbGfawZlwQwtFUypzS2IeM/GY/+C0ZolsTi65Mqs+17IP8TW92c2JxvFDuOKOuiEFWW0xlV/qV6 41sZoC2Rac3q0upGPc+HyH2wDkx/RpcTm3g8Dhxszn0m4CqsZS+r4hYyV5MfKvbBLVpMlyHWb/5 0+DVnDkBNyYpmW/sOAnX67QFURSpukZCpUB3Zmiu8sCWcqZ8AYDuss7wZiEBoyiFntkzMw4MJDq YrqgxnwkJqLeu87yotymeSoE5Toa88riEes63K2UQxrbP5apTwECNU69klgsWZj4= X-Google-Smtp-Source: AGHT+IFdiMNyAnu9UryWCSK1mcKi/kj3qV4ntRIkK3d5kpgJtmLY+vw0EOiis+3DxW1OxC/tcEOAxA== X-Received: by 2002:a05:6a00:114e:b0:76b:ef44:1fa4 with SMTP id d2e1a72fcca58-7723e27d90fmr23784888b3a.14.1756945471815; Wed, 03 Sep 2025 17:24:31 -0700 (PDT) Received: from localhost.localdomain ([45.142.165.62]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7722a4bd2aesm17579551b3a.48.2025.09.03.17.24.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Sep 2025 17:24:31 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Peter Zijlstra , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , "Liang, Kan" , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-perf-users@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH v2 18/18] MAINTAINERS: add entry for KStackWatch (Kernel Stack Watch) Date: Thu, 4 Sep 2025 08:21:15 +0800 Message-ID: <20250904002126.1514566-19-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250904002126.1514566-1-wangjinchao600@gmail.com> References: <20250904002126.1514566-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 Jinchao Wang as the maintainer for KStackWatch, including its kernel module and test script. Signed-off-by: Jinchao Wang --- MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index fed6cd812d79..097a0462e604 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13354,6 +13354,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/g= it/shuah/linux-kselftest.git F: Documentation/dev-tools/kselftest* F: tools/testing/selftests/ =20 +KERNEL STACK WATCH +M: Jinchao Wang +S: Maintained +F: mm/kstackwatch/ +F: tools/kstackwatch/ + KERNEL SMB3 SERVER (KSMBD) M: Namjae Jeon M: Namjae Jeon --=20 2.43.0