From nobody Fri Oct 3 16:47:15 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 19AA12D6E59; Thu, 28 Aug 2025 07:34:12 +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=1756366455; cv=none; b=rrHw0Cz+gQWRUi2y1KmwCYmU37NTihTBqB48f2YLnDegqx8zUAxWcfndOSgzFh4/39frwt1Z/fO4adQ9immx0ZPNJtdJZwO6vJ9Jmi4iLUlz4S+dXeeP5Wl/IJgg+FfyTOvWJzPDi/zzDnCxlpEDjNGArcukT6/OQPVRHFbHPPY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366455; c=relaxed/simple; bh=tBX0hO0TjzB27+2RjkvnkUfPVj0tB9UkFzjloHpm/as=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Nlpg/PkGvkQ8/v5l4fTrfuJp7o78ZowDBOeNsgwN+nNdjbMCkIM4+zZkZkNmBNM0MS09OUNXNn12tAF9VNbsfWdAc3QwHYM+38NWEN4lxVb2w6KFBAOkW3JDxD2py0gIFgJFQjXn3G5z1vdWLMfhm+HnB2cPeUlq1YOaYBsvrxM= 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=jCIc+nIx; 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="jCIc+nIx" Received: by mail-pf1-f177.google.com with SMTP id d2e1a72fcca58-7721b8214d4so245498b3a.2; Thu, 28 Aug 2025 00:34:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366452; x=1756971252; 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=jCIc+nIxa/rDXdjBbrPa+HJceSgm/8kH3z0RtuRzyzcaOGhOzpsRpzkvngKYtJrASW +Iiqh+B4GB0vb1QiD3sO6b30mp1Zlqe5EMzoNXqHLHRYvUeiy/tqLQDRl5FKn+zfAwCb zNzg7cmp0oveoEMlqD579/2BdjlWk7E8walrpWG6LWRzrNG/0fxnIGIXZm0lBDkfGHh4 PZ42AFeqLfH7lZaffRzp9wmCiSEaEHzYyvU4KpGvtcuAx01Oq74Vk675hA6CI9jiJYbC gLlP+4GuxAL6BLlCWeAl2xmNjBRgnEIzIqhjgLojTpLAF7V5+DTc/xtJy90Of1FrDVKA Z3RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366452; x=1756971252; 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=HU0e7qWope8TxCyMBqbjXo+ptJxpOQjdrJfZMAqAM9vByLKrdG6TeZxRyFQZjQoEQM TnMYqs4AwHRPciCGYSXrVuTqbRvbjyiquwf//1asGdtc/2WgyAC8h5aNUaY+Q2RT4o51 cA/8nmYJlP0EMo94GdzzCHU1WSXk17VcqEZw9jvsNwX3fA9yp/GQ/VveAr9UbZ9rCFw8 9lrHu8Sg25vqgponQdKa2OQKWiwH/pP/1BT2HRfolW3NsjjxHpGEkZo27qR4++9/Mlte /r/Leyh7pLJSEad+XoxdNyhb2gN1m3+ym2cnxZGD9PfQQF1xVPMIZQqxCGZnZajo1QSy UxFw== X-Forwarded-Encrypted: i=1; AJvYcCVQz46gqNnCMl7e9OUYympQZrhGR2FIT71J60UlgocaY3DDH26LPZpL6E5kBl2a71N/krtgWnvUaXricAUQdDIWRDk=@vger.kernel.org X-Gm-Message-State: AOJu0YynUONJopEi3cqAYPhi7imioiRqfSt94H4NkVQy0T29gPT3gjMk yx1KJm70+lTRoNswJ7jjpSny84fxfPZLoM9iNyp6VuFRIx2Yp62voz8f X-Gm-Gg: ASbGncvP5matwlzlRwYXpm+wrMkUTdS4l5RY4ZD41XGFY0HQgH2vG/pMTy7UQoMg+Qw lqHHD2o9vQb/43mUq/kyZz7YN0wMdoMpuOPgQ92/wZCDCO8S95/wvmoMQC/scAk5XXMqfOWy5/M znW7AQc3VXE06uc9hXtbB7E6d7TRCa5XaS1BPDMYPVZAuHcHzEx0v9VhxCjwI0bgEAWZthACNMf v96VYHyqnnsrj9r2pxqTGnthQwI9itSE9Q9LbEV12V3g+RKJ7gglNt54gCTC3aFDbO3FefvqOgk 8uJZ3b5bsorDcOF3EAFVVsPxkybkzHoxzJFcJeCe8s9xlVZQ5wTDB7XIbsET1D5lHz9fGsY2sDM 6MXsW11Nig6jzRiRV/LqmG2q5+DKW0qYhi4ClikEWGZN2JT+P/7CjraR5LWul X-Google-Smtp-Source: AGHT+IFW8MIhp5lAtNEt+uw+DAmzNYmf1p0SiEn72vtOqZ4l+f/YrYQksgLtRZc7uoqAe8mYBAPXtg== X-Received: by 2002:a05:6a20:914d:b0:243:a52d:7a70 with SMTP id adf61e73a8af0-243a52d7b0dmr6549481637.3.1756366452260; Thu, 28 Aug 2025 00:34:12 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:11 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 01/17] mm/ksw: add build system support Date: Thu, 28 Aug 2025 15:32:34 +0800 Message-ID: <20250828073311.1116593-2-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 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 94DB02D7D30; Thu, 28 Aug 2025 07:34:16 +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=1756366459; cv=none; b=E8rJGXky/Ft7fTnWJdhZYl8JU6IZqSy7O+t7dZH6WWlFbsq3CFy72KMUM1r1aiUlJgojjSZ8Te3/XEWDxKaPSQI/WR4KbIi0azDQfvbzepk/baTz4yz6kHk2H+Dgrp5QBaFg6GUG0wUIeB+xtTLm7J16LWnXwyN5+5wQGMcPjtE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366459; c=relaxed/simple; bh=f7oIGMu82h0EUyi+ciPNEzqBC4KP/xyBf4hESY7E3jw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HPGWMIlbprC02J0myLtuzmPgzFLF/oITXsRWDestFwthWvLqc4BNWox1akiG21kt8XlogaWNBuH8KqVeUcKSV4VfG5wWmeuWbypymFzVdXmxdScDFfHGqIPUxgP5Wan/xFBpza2z4wzMsU0kk8A9tBu/9T6oiTD+IL94dTrVOLo= 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=Wj0Gd1vx; 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="Wj0Gd1vx" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-7704799d798so528669b3a.3; Thu, 28 Aug 2025 00:34:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366456; x=1756971256; 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=Wj0Gd1vx2mpEZ2lwmS69oiOPZuYscVCVAmSb+VcqFqo4tpXRHjDgSEDplCItoWqSqm haBJg005jaV5flcpnQP+K/mT5mDG0tmnmjZSW7qtbWuiUi/W7tKePDy62JwIByM01OP+ +IXU7kC15qR/aC49g1ZW+Ff4TfhVzcCaWRvAodoFPzMNtJwXgg9dHpRESY/GFvq4g3iC aOSlRFV16nuYZXLq9bUpNxcAhhup9pEuPghfhcD7aEJO3MzCOCcY+W2FXjLnc3O9NkE2 xZhCbQ/kd0Tfb1RfCWe5FDRxiQ5WeAWq0uirHVIuB8iIrwScdsqealZa1w+L/7ElRKzE 9ZPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366456; x=1756971256; 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=KD3gfkPZbvx6BcqZvtF6Caxha/FLi6Zk6a7EhhH86qRwe0lUuW1Y2JKP2e5dm3tfaL uBQbx+lRjZATYKMnNXkzEV2FYJ/92fD2D8C7R6h7Nco42FLjrCgu+le8BJO0y/S1vMoT QBSckCjE2jH4TmZYvZls3cq81X4a/lkuc0Z58+xczqOAHbeACvRzpeYMI9eLt3jSW/iy ob3oxtiDDelnwL7dqoMIQ9DdIl1qP+anOyYoKfUZsSxti5AfpsgPr6NXAyo768DnZQjM rZ7KRS2zcdM9SajtLa+Z0Xev9yBz16a5lK0JTmzsRwkXkTrUwoyz9X/zSu5JWdDql047 nyqw== X-Forwarded-Encrypted: i=1; AJvYcCV22WUbw8qJlPafwe7XUpc5VlUKhHuwhRaZW2KJeTAyK5lumncVVXNlRAvyzm5NDqAbGqq8Iw3k9aToVtXFWI3O1Eg=@vger.kernel.org X-Gm-Message-State: AOJu0Yx13C8u6//l6wXEDbmyj37dS9T5JPONZj6Ie5sZLA2gPvbanVxe dfpeLyq8lkaZLshYbIZMS+VlYwd07b2gVY+XoCgVbsjP+NX8Ud9ESpc6 X-Gm-Gg: ASbGncto7Hh9Sw3iFbB2DsmaITEwVe3FV/X8hs8TqYbi6l2XNQ01d32n3PNRb84lNZK JrX5qBoPECB7CUF6mgADb8cUOS8IbpNtk94TlfZrYWKV7arkwOV9T619mxTHLdvMqEB6mwiyKoQ JczmEoEEicntR7AF2lzM9fv7jVArS8i1Sw8Hge79wZnvpplTJTA2HIn+TyoOxyP0kjPsy8R23Ve vhDdDIhjnr44eZ1MqBSafC2xgyOcTq4wNux7f60BXqcHKUIL9X+kNCqQKp85/iHT20hai4+qo3m lMWkVlHEnwADJxbG5J2oDTiwNDElVryziT+LID6SEE72O8VsFowbxEJpLl+mumO16Ar0BArDRZi fJ1cbblaVSkl+DPrjceCswS9W8kjy2PKcQd+G3LjarRyAhICs36XWIaKs0Sc5 X-Google-Smtp-Source: AGHT+IFHzBxQakuN1JXsJ42RsWWWZRkns+BK6Z0QAL9Y0L/arSVa3U2kDuosHVpFKM1rO0HaGrnViA== X-Received: by 2002:a05:6a21:999f:b0:240:116b:cc41 with SMTP id adf61e73a8af0-24340b2757bmr32060790637.16.1756366455717; Thu, 28 Aug 2025 00:34:15 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:15 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 02/17] mm/ksw: add ksw_config struct and parser Date: Thu, 28 Aug 2025 15:32:35 +0800 Message-ID: <20250828073311.1116593-3-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 2025 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 ED1E92D9798; Thu, 28 Aug 2025 07:34:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366462; cv=none; b=bj/xICt19aflrCiss6f6WO3yCBP2NnocjwGF7/E0hYjCqlDpZaixrWQbs8u/FqBc9209g4OCO5lWbcxVmmfUs5jzIzvGuVd7Zk96yTzgoBBwDCtLNGPznU894LPBz7sdDJU1yOxfg1yeoAWaep9HimxqzSxyVQWJZcJqDWqZWnI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366462; c=relaxed/simple; bh=riKBu2I/0IniTUQEc51qBXh7Lfn5NQ94RTpD6hcQAWo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KXcGNjGfApvHxTfScfY6uqZ3WJQpsr0hP2Hj+JjlcQokNtMIEHqdY0UUGBJ1KRFJZuOZDSbT7MAB7T5yrcvwweq163BAH8fiCNqFmcuG3gvJcnl0I1/2EzVY+Wk7E82MJRQONl3Yt42iqym7w2xdTH+ZYgsCoxgS5O9k/0fHauI= 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=jISD1V9J; arc=none smtp.client-ip=209.85.210.180 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="jISD1V9J" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-771facea122so333827b3a.1; Thu, 28 Aug 2025 00:34:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366459; x=1756971259; 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=jISD1V9JYehdFQkQuMPjgovqzaTXgYhdKgq7zOU9UkBEpOK2a+XFHOSecS2UWZGW6i keoPRn/0Vz4ZMWVDApgU6F96OSfDks69EHVhEjEIZq6RTzjJm/LC9A2sep21TdLN9jzx K1HM80So8KWxZ2g8d7SzQay+yZWZ/FF+eD/jC33uvQ0LCcT8pbeWtibhQYOf8QYx/6aS a+d9YnR1HT0OtyGDYN08RuCn3mRqMcL7Tw3X+fygYJ3eslxMnVex+Og8wtB7fHF8zpwI gwidR3WkTTGxGws5FIc10mNMN/QpOAGQoVvgmrP8WlMJtCY5C30t42o9Fm24zXOEeCZj Tdbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366459; x=1756971259; 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=POLlL/+wofc8HPbyvHxwtC8CGwrVGHNx9VRnYZGdCimYrV2oQULbxA5hV6wJah+lk+ AeH+C7Z4X9qKa0a5g6VB3f04625gH2LOT/IzsgvvIAVCJOX5H4nV+4lmVF8v/U5vq77l BCSK6wpgGeaHj2uL+I5b2N8Vvpj0ies/X0aIHGztcLbngk0ZeAd2J1ctMGzy6DeFNluA l6X3FqP8j5+LFmPSHMu6byR4b/Wokn6Jw5S/KyRuInPfx4sRM1PoEtNu1R7qlC4Tvhfn Ca3lE5f1DMprgxCs3pD1cZBGly3sAPk9g3u5PRTa/UzjwZBiM3Bs6NL1PoLrR1/yy115 5VWA== X-Forwarded-Encrypted: i=1; AJvYcCUTGNXSDx0pvpne8iQgdoyNG5UIdL3sYIdDf7wRSpRJ5+QbnXAksu51XOlqGinh4ifYa4Tdj7zatZ+bGHMf4kbbybo=@vger.kernel.org X-Gm-Message-State: AOJu0YzRKj1KCVtT3f0nh/2qgk4J3nHjfhF0icPXUPEdwb8mqj5Uwyll kAgrmC4OfNhHIQMGmq9+0pHm8ser6IBI3UsMPT3WUV8w536MKH5VQIsW X-Gm-Gg: ASbGncsX275DMe9Gy58pEh15rBw6Nuzhiu+65pI6bja4Ur7D6kRTsMRV/Jxb4e37Vdf EyOB9/2IWxoeNUs2x+xTiIPUoaBsAEH+aYKRWqi+7eOGEGse4YmfLqDZTtdf8KoUYW+yv1r8sZj 3fiqU+Yrlf0aXQgFwRCEjbAmEVfiV12lZQlLn18MQXFl1vCFKK3qdPmBQ1Pd5go95T1v9YVz7ef 6r+FrBKBVR7jQgThu8KR9krLyM/c21pg+UT4oFFEd/1h1UjGdb/eISFlFmYgxcCdxLmBYo61Ila wa19s7wI05qtsdCDczRXUp7NulyuAn6hXfasM4nlST82eD3J1gCNvcZYXwFcUMvP9D9yZzhOE94 8Ekj4UQ/tpwAsOhmO8b2tpcE/tiBR+5+bxB2VxPYCtWGZSdJ7duTUJgISZKvi X-Google-Smtp-Source: AGHT+IFYhlbFCeQPuAaCUEgKiUrGtQ+7l2i/ih6peCZQtuclDB1MWqRTOOZ5F985pI8fDDhetrxXsg== X-Received: by 2002:a05:6a20:6a1c:b0:243:9587:a743 with SMTP id adf61e73a8af0-2439587a912mr9614332637.23.1756366459180; Thu, 28 Aug 2025 00:34:19 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:18 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 03/17] mm/ksw: add /proc/kstackwatch interface Date: Thu, 28 Aug 2025 15:32:36 +0800 Message-ID: <20250828073311.1116593-4-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 2025 Received: from mail-pj1-f53.google.com (mail-pj1-f53.google.com [209.85.216.53]) (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 A53CD2DF6E6; Thu, 28 Aug 2025 07:34:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366465; cv=none; b=FseGJDC+TKOwLRGsdoH7qqk8qBTfb8Dm0eFj21mNLy05Dvs6RMmBZ3AEBQGaujL/uTbr3jteeFbGUY4JyjTrJu9MhAcu+gfz23SBj1+SAzHq07tMhgkZbyg0B8asOqaZ6JKsR3AONy+4vQ5GnLZ1dbnZqcnD6kykYuRgprZ/dAE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366465; c=relaxed/simple; bh=rufIEWmrAKLQ+mfcV5W+4Uljip8z5lF1CAVZ2QbsVZM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WlQx+RaSgsAmPB9+xmy/b3Xm2m/wzoCHFMi/HneKjNqHFyW0zZGE6Hr2SGIX1dKyRHuERmZmrWpAzFZO8GL1dVw57sUXycGC2922iN1dzqbuxAXaED4CDSO24UnITT+3HmANRngYKP1lwUWNElBotcj2w+RlWlVbM/8HY6G8t78= 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=hIT7mrfu; arc=none smtp.client-ip=209.85.216.53 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="hIT7mrfu" Received: by mail-pj1-f53.google.com with SMTP id 98e67ed59e1d1-324e6daaa39so674718a91.0; Thu, 28 Aug 2025 00:34:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366463; x=1756971263; 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=hIT7mrfuPxaCiMuyeLR+JgUncHBIBjmoy9XGk4+qZbp+EAeWmR1Z9alOUQLf4VWPOM PClQRGUPBaTNx/uQ2Nv63/gzPbY4d8YKCkfltMotppKIfl29ZPC1+VSGrEHFgROkQpsr c+mBMZjQ6QmnOaF6CFJAJ33I5yaf5Si0rUyNaj1/gBUK0cngLq7g9tg7GGuf9h/HlHR2 xqhqKocSyqcQsiiLl0etO7Teadkwqibk7EjPA5jzWop2tlCiiMKSXcv0fIcP2SibPceu JDutNJLi+JmLuRMHiAjeVRToWIolIPQK54tFfGaPRtWn8Mi0wk10LX+NPvHeafj/8CaZ tE9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366463; x=1756971263; 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=N0sLLyoEXP5qLuu9L9tUAzSHKQjj90R249S9XqwsPv9TTva/c6bdCBSZ82teTKfbpa 1Nekp+WkC56VaqpBd+QmTv9KJlA7yJ+vut1Jl4edT2JHooggIIa4VKrGm5BYORz7eqA9 5oIcXZM8eDLCtJubFxWjXeGEbuqLO23DiOIAbI/riEM8hPeFh/hLRMzDP0qPLfM8WrIu GviTHSUNmyeQCyyDpp9LiHYOgn2u4NhvyLL9atqyZh7uZFhiYVy31HrsbNXFL0gNCr2m peXVEehthgGrw7pRBeMLtMuULAeymOpO1FFit7FZfsyDO1daRzcWcbstCsy07WJ0tFEZ d6Xg== X-Forwarded-Encrypted: i=1; AJvYcCV1GIFdp2yGsqTG1/J04+jsRnAC1cbOybrQqevqplwiSKWVgnrVNiNu2fWuV4WBVnGKM+S9UI0r8KqT6l9qdb6o+po=@vger.kernel.org X-Gm-Message-State: AOJu0YyTRHYbPIo6LWdXOUp6C+Va6hb/tKwiLnESJ/Z+E6uhS3SJjg8G MQBEsYJAyvN7phbVEa/pHZxti2MNXkTiFA6PBhxJH8bOD4MFnpGV+G77 X-Gm-Gg: ASbGnctFIXJiPQii1eQXLey7UDTrLonAG2S6OD6FhPfszyxz0sAhgkytMyGEmxnwXfb c/w49r0tOzHMlsltWnvvCV1EHcTEvdLcwpSn3H3a7bmhJOd9SBSAKerBbyMhVrmIFY9Oss3UAB/ o6NFoLa8U4FJK7oMP770UX5ipEx9bCG0YqFMSfFi73fbD/0Hcx1Dt7JJB9DLuhggt5OzcmSDHlt 4z4SMkwJP5Mi6qCfGQVwbeGL/Az4eIruwKHV2t49SRv6qor2YBEIH+/SPGrBByDJXsUvdFM6yeB pBUMgJ4pIduk8J5JL0jrjrSr5vrdUwex+MCKnXeJDGUlCrhBKUe17+7gjWadjpQNJAS2ATJ+t+S kiDlQMjtkp/sYPKjLTCG5/aHAWO/dBsBk8Zsd37CjTodAVA77tw== X-Google-Smtp-Source: AGHT+IF3oK1wtDDUKHh7prZzFv/h2WgsQ8uT2JvqKbar3UHoHg6u2L/SgTz5aOR5xgZdGT+bcHmpXA== X-Received: by 2002:a17:90b:5823:b0:327:50b2:8007 with SMTP id 98e67ed59e1d1-32750b28118mr9705895a91.32.1756366462755; Thu, 28 Aug 2025 00:34:22 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:22 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 04/17] mm/ksw: add HWBP pre-allocation support Date: Thu, 28 Aug 2025 15:32:37 +0800 Message-ID: <20250828073311.1116593-5-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 2025 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (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 498D92E040F; Thu, 28 Aug 2025 07:34:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366470; cv=none; b=g67h/2qi5HUX5rAdySNbCcLypOQ+vQkiHBZN+FhAaUJ72w2l6p56MH/d9Ytw+4jMVKg0LCed+Qt7ZGLVhrZQr+3BXQfhlBKvZsehU/WNSYbI/aXzIj1EYkOiPyWzTQfgq+ehqQ0b3OQOLJ9oArduB5O7wrB/nGFilar0ajL9mpo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366470; c=relaxed/simple; bh=4PtaQ74SJJPVLMYDACBxzq/zXR+p3d86HHwkF6eNxxw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MD5Sz5nirKZqWfK/IkelA4TPmsYmfyr07EN3+CH0MIbfCMRiFqXyhea2IjPfEti0Eaw8b6iSwtDz3HkR4hmei/3GqyrdVn11w7hQSXtU9ipskELOGy57xq7m7tSzFXFLQaj48JEywYARUr15q7OSK/oDel8uFDhtMTXtLrZa/tI= 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=bUQwbD9l; arc=none smtp.client-ip=209.85.214.180 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="bUQwbD9l" Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-248dc002bbaso3272205ad.1; Thu, 28 Aug 2025 00:34:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366466; x=1756971266; 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=bUQwbD9lMuoyP2w30lTwK0hBzlrtkuyJnseJ7gaycI3XfV44uhJZuQ3pJcz7Bd0oAI NYs5YebXCbnC4Q/0k5p64oUNz9NXDX3p/DSEA2/kCbWyijVqbzlHefOQiDeQhiH35O6f Xo2hHU5nv2NGTw1ohUPsJ13Zt3mKR//CicRaatSNcQlok1dy1fiu0cLkvV5ExF8pfFVw nSYvY/9oYik/KUu0zqwF+ewqKr27Z8c1dUaYyASBUiXmCp9sjqvmXwMeDI1vFOd8VlPz ulwCM4C3LisO/NxBTYF78A4Y6s+BJKo8J3VK1gvsXuUGse4WBsUUgXRRD0+Uh5QAG52q ifLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366466; x=1756971266; 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=d9jiIqc5fkUd3is5SPWkT2Qb+wG55XSnqN+Ou3yHqQu4+XYnd9eXddp3b4fX0yVyhz hY3ZqzrGgjHsJwWlKs2DNWpwPKakq/hOURyzDHbhlaBhghy1sUdISH+dsFB3DTiVxYdJ jUbyEIcFZ0tdZVR+KK6Ykfl2W6umbQy1GlpFVd9VQlPLsRSSWn4KECsAyTPcg1emOVTJ 72KoRbHrwjzq8f0MDu8fBUAW7AWSOaxll/Kck8cxCRhMXDEaZCQw2UxXXKGbe3C6lhL5 GDzQwoolEXDdh0WR2nnFEpCPBzmUwAeHxGlH5qubvVFSwDUvBlxHBdeTTd3nSS1loTtA Y1IA== X-Forwarded-Encrypted: i=1; AJvYcCXZX8TukVfVVTFGr/NNDlSFvzG7oxAVmH0iNFD1arpq1YHpTcZoTB4RgeXdKuA3iEdx2gIjeoUwiVPzcw5mn9PBorY=@vger.kernel.org X-Gm-Message-State: AOJu0YxlADKaQi3nU/UwAXqZB2X6u97zl5biLkAxNhTvmgSQhsRD1p+b qayLlML+arNYo5rg0On7pEZGrp9HNXz95oLkEQFudmD/mal/c01N6fKm X-Gm-Gg: ASbGnctZphdlgdRS7s6z+bhHq9JMFjCFxhXZaugkb/WhYtj1KOQU6P/hWt0mEm+teM9 MGxE4t2WjyxO0NqKs1e2Yv3btOGTITX+XLNWw8BlkU1iN90veFjrWZrYP8/lDh5d9RpUNDC3NUA fiaWeCEnIXY+V9OCYlRXciLZeStatOqBQSZWR42wgCKKxAZk7u6z8S3yUiYRHhAazcb+YmjJcGq eKGi9TTaOLsXrGeePGKnqrpT+UOoRyNBK0Y3cYktdoynpaISyIi+wdYcBz+KzDmmOCBgkApkHQm vEScOrwZb8Lsz3g9bMqSx/MJG+5rEogRQSBKBONj5roBDQ44UO+ZqFYUyKafd3v/+2gdJ4p3Kct fHCb+jO5cHyNU75lbd6tYiJw5WdpIr4Hv+igaR64wMARws0PD1kBie6phjDt+brTk1/xyQZ0= X-Google-Smtp-Source: AGHT+IE4aRaufJVGsSk9bLtr/W5Lqko9zfUO1kQyEX8W4RgjXYJGEEk+9pgg5qU+rFjRTJfPqxplDQ== X-Received: by 2002:a17:902:d4d2:b0:248:9964:3796 with SMTP id d9443c01a7336-24899643b24mr70784475ad.43.1756366466373; Thu, 28 Aug 2025 00:34:26 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:26 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 05/17] x86/HWBP: introduce arch_reinstall_hw_breakpoint() for atomic context Date: Thu, 28 Aug 2025 15:32:38 +0800 Message-ID: <20250828073311.1116593-6-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 2025 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6385A2E1C6B; Thu, 28 Aug 2025 07:34:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366472; cv=none; b=pLtyTcczhVwG0bp3aVx85PQnj09sS0gUAW0+VWzciGPjVk4HWcRDYR+7WzG8IBhZjuQAXacx18Lff7VGmbvdYW1FOQgn5DAhumT6P5Llg96dp5/MjuJhLQVnrvU4acUtBTV6J465wYWzGScCQ4pTAQ0OtUxhUlv0AxFl/cyfOUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366472; c=relaxed/simple; bh=G+A4eJnc065fB8b3BnxhobbmRRvCRJMPpENJLlV5vgc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l3LSGroShYhiajuLph8CLq+7jKKL9iIR0nGwTgvUcDTsN2XfFpaVgBNFGsyJz6TTOuN/yGDlj5llT+g2ttE81+pRWp9xf84+/+elumNx8lUP3yBzVO7D/vsub8FYpSYRAYC2h3pD7kIY7iDRPuY/ej6QF6MN01zjpfkLqhzsp+Y= 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=ewrAO3YU; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ewrAO3YU" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-771ff6f117aso636896b3a.2; Thu, 28 Aug 2025 00:34:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366470; x=1756971270; 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=nhYtCOUm6bvq4qZIRBPhvFj01jBGWvLGzlXSp/AiWds=; b=ewrAO3YU0v8IP0rz0CD67lcAjOg8lsRaNg2HjP/1DJaldEc/942JSsHHSfTa8EEyLN ZfkHIEAK4thnlsGGorjOTgPGwpekJYBcI4HSiM6wpNC2eoF/OZBdJeVO9KHk6B+LHKP2 v5qBN113TX/eCC/G6/sNefoxWzBBhJMCH8t4DG0wzcv2F4nnTSpnNvIYQcg2RHzbK6L0 A6IdxpM9Y3Fjaljg11Foh8KQMv8r6aP7kMIwaGw36v1EfofUjA3fvQ64qCYvlq80An9r wQiX6i98FP8bYYKTgIHIrgyG84qsMPSIxo7Trx3uqk1iWdeZewTUvPyCyL7b79/wlR2p /vJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366470; x=1756971270; 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=nhYtCOUm6bvq4qZIRBPhvFj01jBGWvLGzlXSp/AiWds=; b=PyvLRKb8rJ6HSmphRYz6Yb82pfaY2d2gOudny6xZijgnus0Nfm/GrqFhlfA0tjaFJ5 XPuv7AvpTbc55yvjmArI1Q21mjHBwpZA9Kk33U29me169/HDLzjScVLBv9QYafh02luT gCMyw99+GMgvznO/Yf6efws+swS4zLvw28Rim0wvNpDXhuF1GB+6Uf9AwvIoFmtDKnAc 1HvKcfAWcp/DGw1VR6/0zPS51214woShmFBMuxoPtp1mRjpzCYEce4WGvFJ0s68KNcfd d1yDZEkNfEAETGPJGffHu26b/rCDAEPpMYLycya6piKR192KMjd8GHUEJDW5HU5Ibvaz s2bA== X-Forwarded-Encrypted: i=1; AJvYcCWKAw1aACgYei+dUbA1mgLev742CatGdDkYi1C0dfaYdCejlHDQ2uWSQYKQDN5a+EHY43nwFxxLjGFqnDms0lQgZkg=@vger.kernel.org X-Gm-Message-State: AOJu0YxvcEpR/IAy4m5M7esh8R5bhhidJI7XAJAFBc4NBxFq70ryjzLL YKbYJW5t0hKmqrUiBwBYJPwvCFpXLstCzlAoZlxg7r5hahqUJpFrLeqI X-Gm-Gg: ASbGncvk8PTlm/oexWpqqJIO7pBwtpsDlwwGjx6HToVYvjZpFJI0VvYRU4eOJgsaa3F uuDoFdCQ2TqOeEfZs5/Ub4qqJyniMbU36F0uFcCPaMccK1w088SX/mFkwVHPbYdaM4s8FvSOx1H yrs0/7vkfKGq0VDEdbuylUQJCUavkdaRatz1mbOf61hdDkT5i1qmJ0m62AJEBJV7iMMtf9cCIqB uQJqeSmj4zYbTAtDwcVR4wQbPCaCXjQ43LbodSxCLab+1MC/ly3It0KSftfIDffLgBbAiVIw+8C 1WhbkukZ8L025BRH6AW1BHOzIHIGTC9ZY8Ha20mXSmqo+lFTuPkvMXBw2DmhXtitrJ1lrmCMmL9 vAnxSLQVn1mr79dg7AP28lINfqy1q6VDxNZNsvTiBMdEZcFaIcIMJ2ZCNSpUi X-Google-Smtp-Source: AGHT+IGixiCEjVcYHcne/aHNIr1QDzxbqM5oGrgRGi2xpynpf2i3z8+aAxOQ92IXbm+/nHFvDKB7rA== X-Received: by 2002:a05:6a21:33a8:b0:243:b0fb:7086 with SMTP id adf61e73a8af0-243b0fb711fmr1251757637.4.1756366470529; Thu, 28 Aug 2025 00:34:30 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:30 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 06/17] mm/ksw: add atomic watch on/off operations Date: Thu, 28 Aug 2025 15:32:39 +0800 Message-ID: <20250828073311.1116593-7-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 arch_reinstall_hw_breakpoint(). 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 | 99 ++++++++++++++++++++++++++++++++++++ 2 files changed, 101 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..284facaac8fc 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,93 @@ 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 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; + bp->attr.bp_addr =3D watch_info->addr; + bp->attr.bp_len =3D watch_info->len; + ret =3D hw_breakpoint_arch_parse(bp, &bp->attr, counter_arch_bp(bp)); + if (ret) { + pr_err("KSW: failed to validate HWBP for CPU %d ret %d\n", cpu, + ret); + return; + } + ret =3D arch_reinstall_hw_breakpoint(bp); + 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 16:47:15 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 325D32E7BC2; Thu, 28 Aug 2025 07:34:34 +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=1756366476; cv=none; b=kSTIJFv5gA25NnNSQR+ORWfRx1F3SiqkF1m4rAr6biAcxo9+nWmST8dmY4WvuNbw3LLmcDfelNKTVrFxNQnoNyFsXgv2T5Lqe7VG49dc4j93ewIt350cqv8mIs1WWDV94GsD/Pyzxt5tGzyAhg1FyywcLcFi/G4f/G2CrIeWJv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366476; c=relaxed/simple; bh=QgMzLX+EujUl5nFUa6zgdRyiCNDRhy+hzjs8trJUMFo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Lu3Pp9NGCHnYTg5MgDQr4drg+T9Wmcsd9NYnLr2CHd8eyqinruBkH6uDGR9QULVspK2W30ncsopq2KVUME4BpT/xj28oZapWWkb6/T+E/xlBX3S0eQAgdi+AqrPrKfAy6xuuzKVmWNyl2hxgODBrI3P/782sdZ9JfkCg+FijHss= 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=UuezaxE+; 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="UuezaxE+" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7720f231174so595454b3a.1; Thu, 28 Aug 2025 00:34:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366474; x=1756971274; 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=UuezaxE+XgzYLLWHAz1C2s/ScMf/KgV5gqpYUs1j8umvFPgUaeBeJyVmdPI9jppkn/ PLdBW25I4Bi0Ivc95r+r+n6PMeTx3dYLL3a5xVoXCglRySdzzCTyjUvovBeQ9LS3juoi 877tBBmZP+j6LFAKRwqtcWmUFjYlDufDTC7RKvn+gNtcC7eWniBhfKrbTqLOmINBQDH1 N18rrFL17Nv7kbIucCEdslrQLfyK3Otx3gkIwOHhy5NEbTFob3Sjib2ea9WtxL0IvMPC qW0lVODFECW/Hp+zq+X8B9A7VdjJ2dbnlw5Q6RLb1WAGUwSmQyqQzIhz5Q9Zc45zZULy U2rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366474; x=1756971274; 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=ctGV5CgYAqURRC98GQ6CeGnew2afLTd4DyArBYoQvFn2wjRf8q0RciGNtJ6TgK9u1+ PYH0fYtKJjgtY0BNFCcAvGBlgjXZo3UV83HTcu6uVdD+EyaKjC7gahfUs1es+7UJMB+p o3ga4JmYeb1mGXs7D8jElZrTcSOEpuhIMJm2CcEu7AVlD8OvSS5uczDXG/xa6ZctRIlN 1RlmlTF55hMnDQhEi5xNqOlH8rkC6T3XLL4XnQrA6xF8n7KLBoyvKkhNbkBA/CGFcSh0 CuC9hmoRxi7R9diqI04OvU/4fLbWS3R4xK2ltNHB7569RWzRUjL5Hjt1gfw4q64SXX4k VTDg== X-Forwarded-Encrypted: i=1; AJvYcCX7SsKPuU/zJxye88Y2TcRQDLbKpCTDHTheq0Ug01glAoJ7v3as4lZSnOP8iQWJmx1XYIWIx5t6todPmOKm+GBKqX0=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7/OJGk8k/HlwRTavLUPULcNm2+i6BIr5XGV7Dxz1tx9kCXV3f Pt0+Wok73l6PXspzuU/MnOLJOU0s8FVK/xDn8BMr1DPd6CNOUr/P10Ti X-Gm-Gg: ASbGnct67ag45goxYgztAbinQtji37ktY8RIrEUIkw0Lv0I2sW7jbTGltTyEt6ncldy HUS3puRA7hf7zuWRyJx+BFzNHp3r6WWepDVTAT9i1B7uSnOhvOq+qnn5aZfF69DzrCnw3Bh2FWY uUqi+kK70ajDIueyzjSJ3rf3HQIrRkHzJWbUzgaGSpy9UnI9XH7fMrz0Lhna824axFNjRRs9KeR fr1j7KZe6tfKrqk4GXx0gc6vRVXvtHqhYUhp22cs2pqv1Tuq4vJRFe5JIyiXYX2GVnKJxue/Oqw yU/LL8K9ko/Q1krqYWBDbkYE7cin2oP4WYOrCESUTIAL7ozwPYNTO0YNRr7HVDLF//xHBHDmhnh KCBBshvQpYhAbQX7DiPQ+SCaluJsOsMs6phKiwRyswyz4VwYQL4fAd+w+LKP1 X-Google-Smtp-Source: AGHT+IG2cMUWd4+kNO3PHhwHAxn0aEjXgn4rf+81c8SxWqiq8TRWIkN8Ewenr6E1OgwngH659LqMOg== X-Received: by 2002:a05:6a20:7f96:b0:243:78a:828d with SMTP id adf61e73a8af0-24340e2a75cmr34553679637.52.1756366474244; Thu, 28 Aug 2025 00:34:34 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:33 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 07/17] mm/ksw: add stack probe support Date: Thu, 28 Aug 2025 15:32:40 +0800 Message-ID: <20250828073311.1116593-8-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 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 785832E975A; Thu, 28 Aug 2025 07:34:38 +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=1756366479; cv=none; b=M7rXeYOkw2GIxe68MAYPnW4m78KMgVonkL7CDqsLHTWW95WEX5cxb2wjtTHSv132jwF29K/sde2P4fSbh7woOWkOydwGdzPMxnQjE5Cznm2+Xp5A3I50YXyt7jon+G0d+pp3I5pyJ0ErYqVxe426k+SeyMksrQr9OLKpAxXgxbI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366479; c=relaxed/simple; bh=GWYsCj2bqT7yEVuQqPqCBzjmTA08Q3+v4IQiO0uA9LY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G3Fq6XDpLmNgUn85gfvKRf/2wjYEgDLjSmGmRAer0yA72z+vIRflg6ejNpEOlLjsE5OgzdLlgbnZ2sKqmDRTI7REtrFUkChaR8Cij6qlDmo8fYj1xnm4g21CGUySJYnhWWa9Lsa19+KYqh+u3/DgIdB1Mnra5OAgPWhm5PDemZk= 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=UNXQsxP+; 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="UNXQsxP+" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7720f231174so595483b3a.1; Thu, 28 Aug 2025 00:34:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366478; x=1756971278; 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=b3AulfcHa1eTu5KICr74URcMQphKQ/vLHucAGhDPpIY=; b=UNXQsxP+EXVARALlAHiv0Ji4EyaheeafIKJ7D1z+Ao/C373LW0TKBOkcWqocyUucud fNwlO6neP0SBQyL87Cv/+16Mn7xf5W60SZtkGEBoHufXKv/1Isl2fDpUveVJ+TeL9KU7 M7UkcH0i2IfX11eA/f+4m9lfmRKdiZbH9y0HpFaL6ZV24rxX0KQ8a+dZThsFpYUbt813 06ZF2oHMSbG34qaxVpqfxCr/Yc68hL4l3i/mSy62lMo/30jm8quJDK0poKH5+f5mhKZV Jbjb6pkdnviTN7SvslVoVA8+ZhJOdGtpzovE1RBV1BaPhnF0MQqpu/F7XhLCC5obNQ22 sCXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366478; x=1756971278; 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=b3AulfcHa1eTu5KICr74URcMQphKQ/vLHucAGhDPpIY=; b=KUgMhOvTI8rF76/mgpqk4sWt2YOeJt5HwF6bVUOFSTSi8azmm8Aa9heoh4w2s4VpSY 8XiZPq12FnbbIprW5zig6rX6DttoOf2dG8oN2qAPPQGM4vE07k0Vl7JCaXohjhFNU3oI bExyyHPigs9e9Q4wTLsb0TVEOVGNPANAOCVjVP8LG+BPyULwAr/R/RBKYgxOA9dcGlBW YghdgiTBMCXIPm3rIV17G9TBV7CRJaQMIiHpIfYDMtDVyOyN2I93avOamgP4RIYRWlHe HsRu+zK675dZKw/vHSRYGZNQ/JrJlFJ08W29KxXyfNMjWwnH+prPXrDfdU2XqGgIAL+J daGw== X-Forwarded-Encrypted: i=1; AJvYcCVWlKXqX4fI43mRZpeZGKvxgv7ENQ2Y7qxZEOUUpL9usNx/lJilyTnVO9vwRsf5NFVAavaF5tkbJ59jxtsD7eSrZ1A=@vger.kernel.org X-Gm-Message-State: AOJu0YyL05sV/oIk8kM0DshPJjlgbOH78QQE6E6/QbIXBw6qekCQtrrj +H0NYT6+PDJAUyanVQwHctCcWfrvSaXO5wQ++rRDUrcC1/CWlV9Ep7ai4iKzoATG3rQ= X-Gm-Gg: ASbGncsqcMbb2FLtVRboKNLwrVw/cbkha/U5Uw8e3exCsmRekkaAVrR7sDMw+JP9Yie yAgrZxPxOTJ5wPNESkhgOR6TNkMDkebYWF2Tl5qCcPRYa+PyLBdfrGG5K7kCzRU4DpLba5OYNP8 0m3Qr7spASEli4igt51IoJspIu0dwy+6YA8BF/+PnYNJF4ZUygvtD850C3nF5hwXVlOfrS5nZN8 bGwWGG2/y5waSwLNpLKtocH1kWsv1OrKHAhmt5KLewZxo1P7+XXHIX8T0wCtmlfvdLkkXdYCbo2 Fv/gmrJQ21Q/1rKaMFM8YzBzjqHw8CyHAs2tP8mcgFZK/F2XQYVRM0Rr/w61cwtV2+prRUKGlTk Udvs3JrooMCFi93e4UpSt5moHW+BCKT1fUCB6NMxeUZQA2UFJ41Ty1F1EVbAi X-Google-Smtp-Source: AGHT+IEQPKTDJOGVO2EbTeY/cLoR+hJ9sc+9WFZR9p9/1pqb0apVThZUN4kWnczmM0yNi/O2dtI1UA== X-Received: by 2002:a05:6a20:748d:b0:243:b144:ad9d with SMTP id adf61e73a8af0-243b144b0b5mr1218934637.6.1756366477661; Thu, 28 Aug 2025 00:34:37 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:37 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 08/17] mm/ksw: implement stack canary and local var resolution logic Date: Thu, 28 Aug 2025 15:32:41 +0800 Message-ID: <20250828073311.1116593-9-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 | 102 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 99 insertions(+), 3 deletions(-) diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c index 3b72177315cc..1d9814a58fde 100644 --- a/mm/kstackwatch/stack.c +++ b/mm/kstackwatch/stack.c @@ -1,22 +1,118 @@ // 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; + + if (!regs || !regs->sp) + return 0; + + stack_ptr =3D (unsigned long *)regs->sp; + 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 regs->sp; + 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 16:47:15 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 03F6A2D7384; Thu, 28 Aug 2025 07:34:41 +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=1756366483; cv=none; b=uAodioZa0V6jpT1x7Um9qT7TEU8upjfo8+A+Vzcnxh0mMxrfygItYekJzuqSMDE4J1Ck5T4v9lVHkx4w9B68bo2AVV6Kgy02L+NW2HIWw8mXCyVxqsRbBA5kKlxetCh4/0xr0/XYDymRgIfbxRgfcrD5LE3Fh16R0VjnLJs0eXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366483; c=relaxed/simple; bh=Ovdo5hgFo2bBbWAKUVD3LPmdNb2YG1mi6w9uAqUbgnA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pLy0setwk3/3v1Lo2lrnsCXC6MbTKRuhtIWo3VGBdSjBIu/JwE9YsNj/H0UEu8nEHph9hPQZi7iatXhDda0OGy54Pedj+zOC5kBFP3Vw/AKtEAWpk76il35Mdb9oxl7QPr7lt+AidNI6b0WtJ+7FzAFc2fDZNzW5WQWP9wj1xvY= 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=gUZeC6ox; 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="gUZeC6ox" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-7720c7cbcabso486643b3a.3; Thu, 28 Aug 2025 00:34:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366481; x=1756971281; 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=dROQY+DQQxgfw8aR1H7lwjWnAl7BOjoXCnJTNltx2NI=; b=gUZeC6oxQfLGCOjH7ZS7ifuU/ILZEkAKaxcQGSChUUxSsxNB0RUPjT7S969FAdn4WJ 3ZdpuxblkAT10Qu74N1h1WL2DfyJM683rPdt+6PMLVabBn6vhgge6e8brAf0nPdkx0am 5pYwblMtKPsoAxMB40vWD4kg81wsWPIIU4lNrIzOthToiv91R8K7IdcJaE6WHrZ5mBpm Ch0cZduLU0cQI5A9GF+XEXOJ/BSuOB59H82+rPajouaFmGj8qFZMmmWmfRhQ64PaOt6b X+w3yIa8GWFNY23toL6a3u+/fZkE7w9QDHd8tMEtZSM10omEuYKMqi+ohZq/Ge93M5uK fJCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366481; x=1756971281; 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=dROQY+DQQxgfw8aR1H7lwjWnAl7BOjoXCnJTNltx2NI=; b=IAGQsq84vrz02lrcJp3/5Du0PtGh9jA2DXgctc2xHf7o4AMivfI5Pt4NnRMLvjskiy BfOMhJEWF1DdIFYef9SSGKv2K4Rs/bSG0h7CocHpGms1ip3R/tWmzxVAWK7y7tiY+90k H37evNCFrAz1Jo00MY9jw1QPbOg9ycpwpSfWWk2jj13gMyG3+7P8TjWQ5kfuTPtlCmGX cGrWWFZSklxBe5W0Gb9JpefpNr+KTy4EsV02J9sfIK8VQMny5qGIVdjHBAHU4x5grYNR OfytMhNNc9LqAbaeee3f/a0ML6qq7L3aucPE31O+BjOdGgpDxvRzav/MWlkrkdPIkLLq jYsg== X-Forwarded-Encrypted: i=1; AJvYcCUEbZgMfYbKzfZPWYz85zHT8fhmIELMyZI6J7T6RmjCg6SZ8vE0sriMbUIOYd5MVLOddrLXMyvWNFN9ET3ZGgmDFTc=@vger.kernel.org X-Gm-Message-State: AOJu0YycljG6zDZZPmMzMHbqs9faIk+U1FkX8kFj3m7R7SSrvyc4Sc8T wWslrvfo9u1DQKXQtkQKD7zW8Wd1HbBB6XNmPdCvKUnNOC+uAMEcoiCW X-Gm-Gg: ASbGncsHuX42XOjZtLBNN/UTW44hYofL2DQosoj/Q3sZ22I9j78o+ZFkaE21Gd+6tDq zps9gj13Zf6h8reV5EYuYgm4Tzv9s+2Z1Xs30VkOVf+/o+SlD1qwoPYsM6oD/feipNkwddWlVil LqfsQmXbl2Iw9aeKzlewD36MVXK/d6rryEEBHMxw9QTM+tRAnJwdPG+DdB/uoPwh3p31cQbNGsP 5a9fD79KJLGzJ/+etN/Hj/9XcP2uM6tI8uwuON6kDvkEMviNbpPBdWfGuf80EZfr2rLitm3TanR YJ+90ZsxiQqocUB7wrX2KxVy3V+7B61RQ3akVV7iVwJjVFChKL109/JnXxSjJA2ndqg5Y5bSl2R bAE14/A2es4mQYyvXJAU1lPLnm/kX/Os9vFGhl8XbF4ZIvd8n+w== X-Google-Smtp-Source: AGHT+IHOzcFsWLv4OBgcDeQy4TpdPl7zO4VoKoUPzbnA5fEUxzCOhCFvpJStWdJxL53EzHY8tsoBbQ== X-Received: by 2002:a05:6a20:9145:b0:243:9845:4137 with SMTP id adf61e73a8af0-243984542bemr10262081637.26.1756366481086; Thu, 28 Aug 2025 00:34:41 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:40 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 09/17] mm/ksw: add per-task recursion depth tracking Date: Thu, 28 Aug 2025 15:32:42 +0800 Message-ID: <20250828073311.1116593-10-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 1d9814a58fde..fe4cc1352cb1 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) { @@ -122,10 +199,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) { @@ -135,8 +223,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; } } @@ -145,6 +233,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 @@ -153,6 +252,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 16:47:15 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 1101A2EB868; Thu, 28 Aug 2025 07:34:44 +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=1756366486; cv=none; b=ETya2YpVn2QeYws71wUH/G1y6Af5mrpwgCHWt2Z60bW6A8hfWK0d1ETCki4EzX2A4fiG+RWzf5LHKIjlMZaobpayKSbIpwxM/DaQWpGCrRnWwps/kWYjY3QA6cX5SUSiDrkBdHFG9K20RcIWiBTxG6tEzvXxbOkTS5kjfRiCL/4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366486; c=relaxed/simple; bh=IGZGRl7SPv8QYEkfF81MfJQSk3f6SOpoIfAg9jVan/I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gh87/DlJzAe2mgYDChOFgRQEK0LDSFU/ORMFBTKi9BkMR+fi4ldw0gimvIgjxuOQv0BtkR1jQnEl5XuKp2N6xjCLyWCz9gCmeeo92+fLVqdCF6fXk61p4g3hJ9+n2BIG8FVFKil+XzhhO81PhKlkXwSa1CSXrOjegXGGzGwAkY0= 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=fdL8wPJT; 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="fdL8wPJT" Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-771ff6f117aso637087b3a.2; Thu, 28 Aug 2025 00:34:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366484; x=1756971284; 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=fdL8wPJTl5JRKHXPHhjVsl5c/kjtXn93/eaqygafEK9V9kGf419qUXQ68hjmAVzGC1 kKXNGF6JviYgIXrv7t6qmUxXNKPor4LN6AK0q343w7bepZ6CzJP29DW3j9nslkpvZDT0 xFJC1LyIGakKsZTWD4SRxocCbmsE2+UH2p2u8c+vTjaqZ7/YanLNr/dcMoEkATvj2HuC 1LZGCP0GxZ1YCNMqUc5xP+uOEqyxoYKRlGAQD7cQVYVhSP6XjeYd43FMx61af0kdL3ZZ t2N2GI7gnypSAmGawcQL7OSIr2fSGy4ZAI2i6ohKmIDzfUwcS77ligbgFv0yw6YHkRxP JWHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366484; x=1756971284; 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=Gnn1qVU9amm6JR6NZvWMw3A3GG/9M2wTdMslK8gbJdUzfLyAFUJRlPqbWsl72FX4MS e2UVmFpUE67G1GwkdWu/5Ocy3dC/kwc4RLdc/EHaZDkADuTskvF57/6VQbZWWjK2H7ll 4BTsqzmaxvY6uzinkKBILiN4duwuNGFIXHR4yrLfPM6eLe8QVE1GNH3AyRkGRN3iOTvz o8NWvtlFzs6TUaysFIJork3wiJPT7qVb2+Ih3yRJ4eok8rz8Ck44oUP4hrP5154Z3vn6 hfvC36g2KsupOE9HfVDb/ca8/sgC1gr3rQymMkzIClZMKrI8y57tOfhsHoAInQ3N2r9o UNHg== X-Forwarded-Encrypted: i=1; AJvYcCV26C3+mhlOVrLYZ58HNfbBPqvZvrqGIFXEyNJxIUa3nRjPFisD2qH6vqc/WryooHPXdQ1FyjmvhMgnpgQzqan+fqQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyxlAONpCrIXqZAmXBZevVBcQ5ZmlN8nC+TOUPtAm2ICZM70cm6 bw4131OY8cHV6nnUF1adHUt37IkbjUHwAnkAuNhbJgqjuzJKPRizgCYc X-Gm-Gg: ASbGncuk6ewd8Qqy1iu9qCL0r9weWfI9gtiKorL61zOOHyLzK9R6+wOdUs0IFJsPppP X6732YWZ0l7k0E0BxfdX3+iKkq++bW9kFXUJYZol/23dGwZovfogJUTkWHPAE12YUbFUgd79MlY Degi6brGcbOo4Hgd214Tzr3by1NegVZYIFZ80BFyA/+r1kYoKvzjuNwdjn6ZOhRbHT9JpGYGIUp E/WZSmhhisOQOn6VdwxnVwahxm5dpqzYxpAR3dv2IPu540KKSF1p6cawuaKLPNrJ9xIg5ssFsc0 KKnmQvW2oFSpgoC6hP7zFCKYisFgL4e1EbhGkxcNtGeOZLFrWtPLKFqGrWmpTZModueuuNWBUHF 67WpT+KB82Q1lX/GyMzuMTYlNMOVnTFC/55+jkWP9Y1jsfvSLV1at8YT3B37mBCleyVRHXfw= X-Google-Smtp-Source: AGHT+IEIHHNWrId/X4KeEFqzkhy9anSPZ3dizoDTWPZdjGsO5evyyrSwN6uApRA0NfOM0BM6Js4xvg== X-Received: by 2002:a05:6a20:9147:b0:23f:31ab:371b with SMTP id adf61e73a8af0-24340b4623bmr30386114637.17.1756366484347; Thu, 28 Aug 2025 00:34:44 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:44 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 10/17] mm/ksw: coordinate watch and stack for full functionality Date: Thu, 28 Aug 2025 15:32:43 +0800 Message-ID: <20250828073311.1116593-11-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 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 4FD362F0C5D; Thu, 28 Aug 2025 07:34:48 +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=1756366489; cv=none; b=OBhEV4w+/fdxmp29+9tMzzHbR4J3Z9QUit/sQQ+UEn3NeXwU9QUKmmYeSBUjA+RihHTeMIsEpxRlx32Uh4RCpttmyZt+6v3kr1RxPlCkxvnEye+INo7Fk7Lp4zQR13ikBzmxyw16ejgn7Rk9pKqsFkTmL1Fbxz5vhnZaH8kek94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366489; c=relaxed/simple; bh=QKroQKgE786tSVKjng78IPN4xWkjXaZ/DFWdSoJQlsQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ENCcWNZqp1Pihqv7kKRoOZkMaH+3l30OkiwZCH4k0fATsdRez64sO2GTFl2MUtZ9JjjT0H4KhIwBiwdssz8rvYR1cIhoWwbcuS4vKGjDHoOr6IuNRx9m2cMlgIlnOfBD/n34+KNoEN+tpxiLXZTzPpTVD0PfMEu/xpAxc8zOBo0= 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=lWQQU0XV; 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="lWQQU0XV" Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-770530175a6so425430b3a.3; Thu, 28 Aug 2025 00:34:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366488; x=1756971288; 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=/HWJW+tuPYyKxysGDOHcH2OoItBTHApdsbmGOaraLm0=; b=lWQQU0XV3Y1DTeR9zvp0cR5y8aD3C0/wX9mviP9LXh0wefLJY+fQo+T235TvrXKM4U KhpB358zSqycw9C1tOqZFHP7RSW0kMF4PLh3U3RB06CuBDIAGcutu0QmSwroup7omQk4 NalSAhykAWcG0/TwVZ0hNPwkfkTsvbNqVUa0EO62fEaEHStibLkVi66SyIRRjepXZSkf t1i6CmT0ebHCvHYr2gJ8Tdu61fzhHh+zzwbtL3yW42svP5VfnsDB0zOMu0tLOXavTGSE peH5BHtfobhNQ1Z+sFjvOmcZbGcv2/zdD0HY/xtVJcbOWh4hpBfnZSzbqFVqdjdnzqDR LaHQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366488; x=1756971288; 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=/HWJW+tuPYyKxysGDOHcH2OoItBTHApdsbmGOaraLm0=; b=YF9E0q2iKU+fr4MHVzVJ1ks+cs2QnWHvlCpfaEOucsskdlPNsUZ8WEN690aYElxf7w bKR5QLX/0V/wHVjfHZM+j6TRz5N5Sk2ZsPa2tIy+BWtYul662pfhcRgz7DyG6pg9DT74 4Pqxq9C+cYYC1sqauTdOSNzB7RpYfCZ4/JM6xG/oGHFa8MTba/sv07nSTlfCCJ8QRqc7 GUYYQCHVOwC2xOWTVnpJTebOz5TFxRqLn6mjOUqp/QEHgtw8Oy8nfbvuo19/bCqbXrf2 Qw0diq+5JipBBm8aqjI8h6Bped7r5sSLFaAj9TXzkCIXBSiv+vr8/YEoOqU0SqFv8pLw atuQ== X-Forwarded-Encrypted: i=1; AJvYcCVr3QcfOes2CdBctz17U23zutHd2cQz3ymSYaUxS5qrRpw8K7vmSVyjoMu/UpwABE6d8VrDgxWD1/OMQGt+jhbe4xA=@vger.kernel.org X-Gm-Message-State: AOJu0YxL01fGFs3M0lkzX7/bSEfwO843uYJacugaQImTIBqPfffgCBj7 G05gJ/F5XlCVPvOHCEY1HlxAy9XZhmbAcT2v+cc4BAwgcfcY+5V/Y9Hx X-Gm-Gg: ASbGncvq+R7cKC8wO5Mi58CaGbeOcz0zzQjCn9nz9GYrO1eRmcthln57+pMUUs0n1OY 4MiT8sE0i3VMHu/HKhljCA8JD9eRbf/tJ89dskFlQMKeAJmkq7rxw7Z6G7bzPGkx2Vs151uS1sl n8XEzhMFVRMcM6pN4EfUH7HevHWAgItNybSVXYaVvykIYWu3nk0jjZulObAuNz3Hpr77kcN22up GdTZ8ZN0iXoa5iu0RslHU8OwgHhTBopgqjQp6rfWCoonGAwke0IRVML57yrKn7oWBANPWs++Ocr uCCV8Ba+cNRgCcrK4s/YDMeoqtr7dmsgJb+roQhVTeFt+JAV/TOxhgHTsaMW3e5fZMgxC5PfLjj 5oJ32ujcipsSseq1kH2u7m7X1adoVQ8Yw0XRajYe7dNpgBbeyM7lFByzIl6ue X-Google-Smtp-Source: AGHT+IHMS2NPWoxRpnmY3flLVzQUfw/v5SMsqEM3beAW5o+sLJcBBLfDbzGyGozdOk7sQZB2BLTNOg== X-Received: by 2002:a05:6a20:3ca8:b0:240:750:58f with SMTP id adf61e73a8af0-24340d91edcmr29392145637.30.1756366487573; Thu, 28 Aug 2025 00:34:47 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:47 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 11/17] mm/ksw: add self-debug functions for kstackwatch watch Date: Thu, 28 Aug 2025 15:32:44 +0800 Message-ID: <20250828073311.1116593-12-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 284facaac8fc..158843876958 100644 --- a/mm/kstackwatch/watch.c +++ b/mm/kstackwatch/watch.c @@ -118,7 +118,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); } } @@ -160,3 +159,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 16:47:15 2025 Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9C9C62D94A3; Thu, 28 Aug 2025 07:34:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.174 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366493; cv=none; b=WcfJdwp8LM21VJjRZ4VYVD8wncT692Sr2AMuvVtHZRS2z681OqgWZevNNYMu4+xiqNFM9N14lY7wMNQmET8vS6jEQFiNOfPU7Rys6CilWKJZAQHZRoxksVgMp8Cm4erEo7sQOaitU5QSa+uQGsaa34r+06TG2K0x+4nPDHISWdc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366493; c=relaxed/simple; bh=qkcftcj0fqlwmdDkYmum8il+ZDjAfjmbv7Do9ex3a28=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Nco1ZqHZ4VmCraZ2idEttQrlSEY3FDa7tNiuCnu+JHV/hZzx01aryn45XIOIlDW7CK0KAbna2X8YClz/O9esST90lkT2z3BuOylY4dHox9wiTzCLmFXfA3Pwc8NGkDtxb2kKu1BiUd9XLJelVKUU+YUw+k1FrEX2scerAtjIXjk= 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=LADLh5tF; arc=none smtp.client-ip=209.85.210.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="LADLh5tF" Received: by mail-pf1-f174.google.com with SMTP id d2e1a72fcca58-771f3f89952so538725b3a.0; Thu, 28 Aug 2025 00:34:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366491; x=1756971291; 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=LADLh5tFElhD48ZtlyyGy+OGC5wE7WvrsRF/H8ceHcI+MbEfEdJKVnSR6ZkQDM09qc iG5RK+1ri+9F1tk3fN/BwwY4lrXedhCNX5gBHx3kbjsc5AX96fSgtAXv0ox/RsfcKZ/R 2/6/zK7dQI8DyKNOafpRIkvvvzNH08K7IRpEt8eV2IdOf4R9ADfkoYEwiRz3KnWviHZE b33Y3A972+Rroy34vV2XJzxwaVyr0opUkAtIO0icDnah/urdHsptM7ze0l0WAWI7haFr dqtBb5u4PT992SwYrc9lMLsjl6dNDPGPqRkq5XXAX1/rqbO/Hhuk5WjXLIb87eP8EmMf 0rjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366491; x=1756971291; 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=ew2uolHx7XMeyUJpqywbpSe2OJ/BQkEKsKyTn6LEnv2JmjR95ftOj9MZN9IKjr6QYE PN32e09g+98vBVUVyPMo8JaD+tC7Soo/fNzau/Zwb1F6m0BBoflzrw7XjLmcGTgxYMJi Q2lgdS+6JGr2/rYET1j34BWvn5j2i45+dYpH6UrZBOBW+vUspo7/ehIaPBXUqGVwEm4X 2+0ZWVRs4SZy2h0PvT7VHb3KkaWJKXmhxlIgrfO3kiDT7jBNW6LFcLU1iIFuX6ZmiFxG kSCDSTIhZ/iuFIhwHMrsBFr7YCS0cdOvwX/3ljNbTRpcq2Kdl39MLSqpRZih8Y0CCBDm jNRw== X-Forwarded-Encrypted: i=1; AJvYcCUbz+7VcySY59gIJD2bfuBYD7JD83ZLL+PIYFmO6Zos3FVvYESimHpoixrASbC7FoBFAzpRhZDBNZfpJC/s7g5k7Es=@vger.kernel.org X-Gm-Message-State: AOJu0YyNt2eDdVA+jVqFzT0aaGG7ab98Oqx3gY6XZEizpSJ0B5ZPVnzJ mIpCcoYwSPE0+IsYRsZMJ3VMsA6f6R1OAEj3uTS6/pJ9gG24ttluc6WS X-Gm-Gg: ASbGnctrDXl7gQB5SB4Rf66SkJi0QP7N7OLav0hqJwUPQ6fuZyleUm44CgaNkq388XO iTIQsmqJm+rlKbSWgus5ZxMByJ5fkDR5rssK0E4pdGGLmrnbQSjJtVPU9axqLyq4uIoh2TuDHb0 rhCdi9I04FwGNpe/IJle2ZZxoIuFNWcSkJjUNP5MbdbBOU3cgCQgZQU7cHZKRGfFUWO8MaQDnL2 V9PfOiFXLgv+qw9VHzIRMgPDvc1w7Pgq54T/M7PvyHiMYWqf9TgwoJgl3ehuC/qohZFd36l6pch GIFY6+MPqOTm8/BsDfmsdbaorGg5Rq4fWj/FZG8pWhRllNDzyAZWYFtDq2FSnf8hgbcennTNukG lcpfWh/9iowEVA9z8KbLdnrUIPqukmEO5hQt4x4Qjx8OSmOzDRPMpxbCF0FSERxtvFpvuv40= X-Google-Smtp-Source: AGHT+IFVYiXk5nYF/khbmrTEYSILlLTUA9ybwIqxEMQ2lhYq37r8kAhdhR5nVQny3eugczv3xuUyrQ== X-Received: by 2002:a05:6a20:a10b:b0:243:50c7:24e5 with SMTP id adf61e73a8af0-2438fad31b4mr15697853637.7.1756366490914; Thu, 28 Aug 2025 00:34:50 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:50 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 12/17] mm/ksw: add test module Date: Thu, 28 Aug 2025 15:32:45 +0800 Message-ID: <20250828073311.1116593-13-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 2025 Received: from mail-pj1-f42.google.com (mail-pj1-f42.google.com [209.85.216.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1A9E02D7D59; Thu, 28 Aug 2025 07:34:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366496; cv=none; b=RhT3CYUQU7hTTqdmmFEv6riN8kYaCovCFRIC17ptBy0Mz0wwCbI4255QSlI4wUpIgVKA3bjAemVwyY/s/k/cf2wRYkVJ9vNP9G4ceM0bILafaqyAbSkciGgC3lBJljUPSNRFvWUrXvAnhdzdoIDFBBoxo39ncYvCXtQ+7bBRiG0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366496; c=relaxed/simple; bh=6rw+m4VKvEY+PNH3LtpXPUlmxSfDZ834B7ADRGFPwkE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XhmJ01NW1GWG9HNMf0W3E8JW3+6y3tRka6ytWPPQQFrxHamujwr3zbqkzvZ8XEUrYCqzn4H6nvcMSxzieehZyvbgrwT8Kn98OycK4z8LNSW9aZH654LMbG7IdIO+qZ31/pgGtWRjtQM+HPjvOzLiNTzwNZcyYw6nc6nuLfHOUNU= 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=M8OOP7e3; arc=none smtp.client-ip=209.85.216.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="M8OOP7e3" Received: by mail-pj1-f42.google.com with SMTP id 98e67ed59e1d1-32753ef4e33so524786a91.0; Thu, 28 Aug 2025 00:34:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366494; x=1756971294; 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=M8OOP7e3CaYEnkS8hKzzOszRiqHHYpcvFIJUs7RcQw518QRzBLpE+FYk3o5LEP2wxR dCf/h98IEQNavFaKuLmjQV8CwX8ckJu40WCi4M2dyBc5jDA434TTimtml7Po4NGKgAVf S1/R7q15T8fumOQ/gTXgHkXVmHkFDY01nISxjOa61fZG0zgWVRO8JIaGFFrT29VprVLb AsoPoX95wxkXb0XcgzHOeoSxwIHaQCW0WOrWNnyM9G5EIuXc1021e7+bYVkiXZBxAdYc tKiQj6TtiSIzqaZTjHbt8vt1eq7dZ9VGHllvqUzla87/Z7ek4Wt/mT6Q/1z/O8RY+cgK gOrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366494; x=1756971294; 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=hAJua/XkRTkeSVnZk13jVD9CpkQNOGUnb15BcrhehdV0u+echpd2HTUFXYMYAJFQt2 D/QMbFzHQPyxOEkBcSXjXfFBcgOeluMI8Fj7TP/BGNyVD03FLldmc/vlSYejEdHxvwr2 wGXzh5WRXsga2uchZ46+rHJdWV1Or3oUXW70KQ6PUXup9Fn50j80wc6cJje9y3aihmvV JzPpSzMpllD+G8Oi10ZwBSjkQsiaTPBrStuXxY5zL5DfoJ7tN5O1RkiXWY+4/PE/fze2 /8Fn0OYqCDEtGOE13ZPpASuqsAQehshGo3OsUGaX9MfsLrRsY7joq+Q/C014z1WdB4mX DwoA== X-Forwarded-Encrypted: i=1; AJvYcCV1APDv3pKPU4j4zHw/I/uLgMVBwVycGdVSZP9BDHaaWqwcOh1b8plJSp974Pk1qmmD4hIqfOeMZvZGbwYyFm3aEPw=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+mZBOpTGBXSuVNxygZ9552LKGWZNtLOgmM0tihg2yy2X0pBXC hUXU9TexDAcLCsnEFPUJRdZ4Q2MjySbWoRcvtmpP1R509m7dJqoLErVA X-Gm-Gg: ASbGncu5CtCKJY0/bbyTouDPjOrdovtaSORe0waqF2yNBJJ6tiRkTYVnlEQFPX6Ef1A k6/UXyn3Bi7L2UrNsaKbt9iky8S1Cnop6rVCPlqVngwpxjhTWvCRrogVsS7MmKBf6GggcBJuUgL 7orJcE6GW3236zq/+7qQUdxRHb/zot0KW+cj8iZFl6o+gizaNNvfJpJyYc03+0OEHk9HxiT/ltU riBLuZ+lmwzw1Xs5XI9HX9VTqAvtIM8D8uF7T+ACG46E8DT1j6MDqIu66Pc130OfAXaA6NHzkAc PKy0zPrHQ9jcZd1dETGfi7ZWod3OoI4DNGoakO3jETpiAtnelZm3PwrOFJEoz+v2FZnUx7MUnFY NF6LlfRRdEijYjnB9vbt4ObhXLtDcw0X/Vu56lpsN3uJU1BPm41nbieUaZbxGA2fubllD7T4= X-Google-Smtp-Source: AGHT+IHB1bGrZQkRWWOeTcaD9yBKG+Jt7AaKjMTGJ6v8jmWN0++u4eqV5qBUc+XfK8W7V9oke139Hg== X-Received: by 2002:a17:90a:fc46:b0:327:5461:51ce with SMTP id 98e67ed59e1d1-32754615342mr10004212a91.35.1756366494136; Thu, 28 Aug 2025 00:34:54 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:53 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 13/17] mm/ksw: add stack overflow test Date: Thu, 28 Aug 2025 15:32:46 +0800 Message-ID: <20250828073311.1116593-14-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 2025 Received: from mail-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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 410072F39A3; Thu, 28 Aug 2025 07:34:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366499; cv=none; b=h9QDPqmjpGp7r6PiDPCuOLKWr4siGc7p6bOZYKY7y4wVGfEmcLLkSHBUPedm/CWB6oPGRsXUzK2jhmPeHglcxwZ+6bZKGcEWUR3L4EvmeyiPEBO0O7jZJhB8+UkOR8ac6InNee8qIu9R8epGBJuyx5zizbifo3VnnUYqcVq9Irg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366499; 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=UJ73jBy6uiIeV7AMvoqzxt5AATDUGbq0Xf2LmPjlhgQGNsyHF01s7Gt4aSzSaxyxjzaNA0ADZSJluU33RdeGp+UcQEO14Yz7SvQE4V3vNMqqtpdKLNa+b8Hxv5AudFLxqZZbEt4M85GoykLnHj7FYcQwx+4H8TCU3NTCBoiDomM= 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=GJ7SUa40; arc=none smtp.client-ip=209.85.210.180 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="GJ7SUa40" Received: by mail-pf1-f180.google.com with SMTP id d2e1a72fcca58-7704f3c46ceso563776b3a.2; Thu, 28 Aug 2025 00:34:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366497; x=1756971297; 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=GJ7SUa40KafgGjuf1ElpkcXGYWS/31QvxRdywg5ZfBRB6PjzRME/I7kPqJ42PLbG7a m4us4nTVeA/6I8nhrOoSEPD24qVXyOGV7PH6AwHBYZWgYu2yyYufjl4nY3SDpgFns3w5 4JmpZUZXxv99Hx4iym/txk2wEx4lgv79OdQNyYekWZ3VEbjDGYSWdUsSzoNdqEUY3YyO e3mMdk/Upi6dnu0HMn6ZomPFlUrBEKHCgH9SHw6WRAUhjs38HJpVZnJ0m442k0nvM7ND Eg8sC5JLje331TBD7xOCOMG/ez9YMGbT1Ug17WMD8E5lYMy8f+9qOME29R8FfVcBc0R0 N8Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366497; x=1756971297; 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=SGbVhyz0wMpDBtbgHie+5lIEiJlKi4k/I90V3EwTkEeFf9NadKVok7xSP0DvWaRExm A++y1FrHCgfvHqkDRQa7RLlSYLEqY+bj3YdLK311BXx7cnNwAl1AAVv1oazld1px9sWx LGg+Wdbtpnolm9QZiIlOt7PC5HuY6scOfwxlxBSvVHpjfx/4dL/SgjnmD2QVyBmDtFYc A+9cO7GwtZZtcMS0SUpGewsqmvj5UY2/KSELMKZPPra+KbYBw9gnikCv6XVphltnTkuo 3lVz1cGisIk3tbGIZ6cdsNibeDhdPoJnpdAne9KLeOepK2rV8mRk8ofepllzRYAIjQtz 1U1A== X-Forwarded-Encrypted: i=1; AJvYcCVotZ3n3tn+Z7AyH7QcNcIX6RdUElvSf70b0fJLGYYo+j93mh6GUlFKwtO3+hX7VHK1qtvOn2nT1YthbyLgryBzRpQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yzu2sVxsPu19cBACQ5TByG/kZ61ZnDFrZSCIF15snNa4FdamgQE qyX5kyrkZDRfgM7QvgYstvS1EZN5SR9Y0GU3/AlGHVYPbYHFEjGV/+HM21QmL8TwTXs= X-Gm-Gg: ASbGncvGzRPvzaTQjtQUtCBXdVYmyuJ+oReYB+XZPJk9K0fnSaV4qA7j52cuYXSe8rs y62TjAvVlIbnfMX/7QJV/nk/BeQGByiqb/OFAVU1XHUs6sdRLB3npJkeC8Hf1XEbh2DXl074dW5 cFaG48kH3/cfQsGkwg++f+aLbgTFxc96FaFOl5a4M4KOulUNw7cp/B6OxwG9VAk9xxSiOt6qe6+ bKujx6pmn6fgcLa9X+nUAvg/wtoWXYZdhXxxBZVxC6AA5bsGGZdC4A8KUGeydWS4QGNF/d1uZ4g YPTlaRqAuhizfOf/xkPeDCoeM8w2mC3rWx+yDiiB4+uUz7VsbBx7ohl1P1rMrwu8jOWhzBzZk6k sdJdpe5dYO8D0q7uR+nmWxlVXcB28NQgTsamuHfy6YfdpkQnfxw== X-Google-Smtp-Source: AGHT+IHD57dw5PWoE8qv8A9jbGNzXzct5VOkOCkS7MNXKbnk/+b/dQdygfPv1AzYB+6KyyshnI7WQg== X-Received: by 2002:a05:6a21:6d84:b0:243:971d:cd84 with SMTP id adf61e73a8af0-243971dd02bmr9544389637.22.1756366497452; Thu, 28 Aug 2025 00:34:57 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:34:57 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 14/17] mm/ksw: add simplified silent corruption test Date: Thu, 28 Aug 2025 15:32:47 +0800 Message-ID: <20250828073311.1116593-15-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 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 76A472DECA1; Thu, 28 Aug 2025 07:35:01 +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=1756366502; cv=none; b=ttx1+ICrAjCfY1JZ4WsuFVetfXkLHO7muiNu5MLO58OR+C2NGur4nL6FmlN9jQS+J5/T3QYxK+HFwHuv7f8uKv/XHGuL9wjFa9ri0/yIQ4uBheZa/0ZgEJt/JAjEAH11cLNn5yPSrjxCCuxFG2mbS1uIF4Bg+VTy5kBvP/mkcak= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366502; c=relaxed/simple; bh=ivSu/IMaZGfW21FS3ZEvzo2NiJNn9PUIMXyecx0W7Ag=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L8BNSAZm16uhgTmisYzx2adXjdoSocnEBlOZiylXxmwn4dLsruz+ImW8p1FBZFuth3GTL45edbYGwF+13Mx+vEuObNgA21yZC38TYpKW+KIwHMLJOrsjT2ILuoyua2ZuuSL09mI2OAsexYJHJupAKqN6idT8HnbFBSBl1a6gg04= 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=SC7kjKQp; 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="SC7kjKQp" Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-76e6cbb991aso632475b3a.1; Thu, 28 Aug 2025 00:35:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366501; x=1756971301; 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=SC7kjKQpI6Tc+KOWaooHsnrKpEUHsjOaFLlC6AOMlNwg1ANXmC67tVktmXdz90SDsQ 8ySftmNZ406m4RLJfHN62LeAxGlDiaFovan5D3v2AN0J4KPA1pzfGKuBVFSlEJs84sI0 LNjhrit3oNoMrm4cBZUBvyw+yb1f/KRFwGf9kl4UgOJlXXvvKvLr+V4/zVv3wr5oH/pE 9SDMA2ObmztgpdElkr6XCx+jd7gWFkP1rdOHr8wZUxmOvMskzD74/KNuJ7tHofh72RjR ikaEEne8PtGA26jquAEgRK/yxet/XdHpmxkWWTLkQn3i0j5CYAdHVGsvUGjzUdV+rEHX 4XPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366501; x=1756971301; 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=r71I3SfHkPgVE2/mPC8Y5yKF0t6NUNst6ixs2BzdJtt5o8qGWt0HGh9HbjuSy5/1OG /f4aTlXTrsNHHFyL1bdg9jaO5MKoXcQR5kWgMzVpXJftXsfavvofFKmGVIUfzZCqnH+t kezgW9AGsCLI5kpE5q/TaVRmeyjJA6JFvAIsEk6ygNgs6PrjZjUBwQjkhzgxUBM9HPR4 koadFlMflA3vqShBuvRX0iMe/BL+Wig5eYWeDcBSej7y2JEkcaWn+joqGzdRGVA3tYgf ugk2/6PHdVz5YtW95d/xEl8+ofzj50H5SDn+pFN9OldFs113m5ExhODQQV/tIdYGeW0x kIeQ== X-Forwarded-Encrypted: i=1; AJvYcCXawYh601CpWbSvt7qhV3RPJYAfvFZpeNAE1Z2Okt2J5Pn0AaKHZyTwUlxBs8EfF3VdoBTfXymEjlwm04+cwLeSGac=@vger.kernel.org X-Gm-Message-State: AOJu0YzyBRY5tOxX5wBUVSiI++OOvIqKZb380N0TJsbEgWQgQVn1RJRA pNn+xnr/yERAIFhgKiifZaRLm00phfB9bLEjoOqDho830qmYRYyRVYIh X-Gm-Gg: ASbGncv1LzmO/BHeCR0ivABxVzPI433xOSIzIuOybMdgyZ2l9WfuwrBwwSjdHSIUlrP NnnEJ6sS2sfZjQTaQY4KIhTAlX4+Y97y0o7lT4Tq8oFwW1rA8CGMLFI3UQvRtUl9NHHHV2iDKmn JE1qVit1HTosoyvsieMFjG5KYZs/6EazhIzvNYS7Vj7ojE+tNPBMzGCoxGqCc6zaDzG0prx4atV O71MTIsOhzIcafcqALlu1t+iKDTwJ4mzk4uxda7RJRcj3j8/Hl85lDOTSHA0xQaOaxqlq/T8UAm jjMII17+LrHmyD5lPGhXw0j/4qvWIy7B6mYP5dZBBXrTQ/8e1YJ1rLVma1BxrO+EzcK04RkfHuq FR3BpRWNftk9LlCESU3JbtBh2QIp/ZccoT+82CMWbzvv7iPTph6W/68GkWXZK X-Google-Smtp-Source: AGHT+IFY7Sr3NFmqU4r7l704msEMWDuzdTM26kIlnGaBdOScCFq81peAWPHhGn4VtotCUhBmhFVWWA== X-Received: by 2002:a05:6a20:2585:b0:240:ed9:dd0a with SMTP id adf61e73a8af0-24340d11bf2mr32380410637.35.1756366500817; Thu, 28 Aug 2025 00:35:00 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.34.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:35:00 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 15/17] mm/ksw: add recursive corruption test Date: Thu, 28 Aug 2025 15:32:48 +0800 Message-ID: <20250828073311.1116593-16-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 16:47:15 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 297192FB98A; Thu, 28 Aug 2025 07:35:04 +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=1756366506; cv=none; b=nj89SAHmduPLgarOihxGPn9TyzKF/y/uKeewfnqxjACbKISBms8NO0BVR+rL9gLmjAxsPLkQ/qg5XFad1VwUIbg1v69T3DqmG0VGGp24siIwNEPVdhk/c4BHOzB6N5iVlybs2yTPcT2mmc3lqmdKlRSr3/zlRMXIm37rOIWH0T0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366506; c=relaxed/simple; bh=JoL7MR2CzdQS6BPrywO8jB1PEyibulfdpOOvNvnO3eU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IyX4Opi+HZRx4DTKW4YdXIdeuojqpTXjAEkeXwcEe450JmMbuTqAF9g3WlKYqbBIXptrlTwfHTNbQv4wKoDy6DoMcq/e1ZL/NHV4swq/K4iwW/ii9u9413uOYMIZYGRMZmiR7gn0bOOGympi1YxnOo2FsVI6DHwxzy6hVWZIEck= 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=GbAztueP; 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="GbAztueP" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-770593ba164so579115b3a.3; Thu, 28 Aug 2025 00:35:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366504; x=1756971304; 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=FwaUG2mqmSuw60xQO2PjnUcliyWKWlyIr1nprl+PudA=; b=GbAztuePbTcK9XFKDctFVvUGngyFRyBQ8oxYdZk8TgmcjjxqtJ7uAqd4aHoOUtZFga 0wjSVs7fCpzGuCSVe8KZNhIKzVyjyoW2pHFti1QdFZ5R1yk4/5Vdm8C/JPT8+/s4Uh0p bzvbxJlr4R7acFLFOiOTBM65IZqf2RndedWNueDC5BHCzczUtdzidnduAVTLvKlwJUqh FcnjUK6VHlN+A92Nh5jYNspQlmLcAe71IF+8ABHj5gP5caRlOCgwePFOHG+M7dx1BjFB 6AFXipS7AAaOs5s+vo2HSf3tyZ0o6Hm/8eHnj4hI7jzXy3lS5zaZaAaYbv4ju+U8emDd +F2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366504; x=1756971304; 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=FwaUG2mqmSuw60xQO2PjnUcliyWKWlyIr1nprl+PudA=; b=R6u2LEYO2lHHpEuG2UioZ00VvLb7k3FEqgNEv0Z1OUBiXjOkz55MT6ToZH+3T0YYaG MRScYxdMP/LIvvtr50UIGnEEYzczXp5h6YF4lQ1Lp+K9uJ+lJv5wVJqCaCnYc6h5qLXR FJIz1Q0Zn+eg5qNiAfTN7aTXuy13kCMj0zD+D25G/UYOQ0zFLWyzeI15fh2s08Yj9LCZ nRha/lxfAcA56lLDipd4+yKeajROI4j2sGsdRTsjeP9TnSxTmEeqaPDMMOZuyY0lEz6X nBHo0Q7PpjGtoWwA0Z/e62RuqfTln/GcsE0zMUWAV2GVh7Egizv3jOxKsG0rP5VUhkgh OpMg== X-Forwarded-Encrypted: i=1; AJvYcCUvQrxIkbjYi1++Hglyl6uEvAxY0fmc6LyGTLFwXGWSY3C/8cn3uiez3YObogSYDkc8w1GcZZGtqS8pQaPlYly9NiY=@vger.kernel.org X-Gm-Message-State: AOJu0Yzi9NcH9s5srg+TO7TKTxhrR0vfltJqUHtYIooOlKQJ7yVoJ+ng QVmdKwSdUioBDp8ZuTJfWphi7DD/10hGUv2ZavwKd5whFiKuOeB0UICQtRBgUBc7W3w= X-Gm-Gg: ASbGnctGKI4MLTJrtcbIsBp7KkF68H4j60DRcyl72enynHCLooHH8dpMh0fKVAuews4 PrnJlberwbUTGuv8DlVZr6fZsxfzEfRNkwsHW458js30kfWSZs7mkORAkl6t225m2p6xo1HbGuW LMLhceJCywWXww05IkG0eyLtxo6jZI4oSDzH2hctWhK840kzRKcKDhemkuhRq//pw1LDJzeSfAS U6jIZ8siT+28IWOsVmTLRSAVmRDhxeki9+FlrQ1SGLHZe4BC968BlaDSIRaG+X+hwmfEKO/vXJW BykXx2od4OemNBFePZMbf6nXKBcDnwz7QMaTRIXHu8n5vl7Ev0no5B8TKElxx8ombUVbMZTGLZb 5VIkSL0w3zrenqUMYxthO9lYZcdps9HR3gQZ9bwFNqW1oYTmZVg== X-Google-Smtp-Source: AGHT+IE7yqNI1wNfU3kqRgvdUB03Q+7fqxylivWelN8xwvP2fUBVMLhtkCKDyOOS3PYiCl6wvaTFUg== X-Received: by 2002:a05:6a20:4325:b0:243:755:58b5 with SMTP id adf61e73a8af0-24340d920b2mr32588008637.54.1756366504311; Thu, 28 Aug 2025 00:35:04 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.35.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:35:03 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 16/17] tools/kstackwatch: add interactive test script for KStackWatch Date: Thu, 28 Aug 2025 15:32:49 +0800 Message-ID: <20250828073311.1116593-17-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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 100644 tools/kstackwatch/kstackwatch_test.sh diff --git a/tools/kstackwatch/kstackwatch_test.sh b/tools/kstackwatch/ksta= ckwatch_test.sh new file mode 100644 index 000000000000..af0fbdb3a975 --- /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 - Silient 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) Silient 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: Silient Corruption --- +test2() { + echo "=3D=3D=3D Running Test Case 2: Silient 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 16:47:15 2025 Received: from mail-pf1-f175.google.com (mail-pf1-f175.google.com [209.85.210.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1582A2FD1C2; Thu, 28 Aug 2025 07:35:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366510; cv=none; b=FTiQ/zIoxbFDMeD/5rgw8yO/AkQYOCKog6E3dDczuih30Vcxk0t37Oeo19XDUQBko8K9uGHCTzzVZMmDAZZmolbnXRCPDMgnwO+kZyz5BzKoTFt715EL6zjmA4CV79xKH9Zo1KuixiJhzJEAyQ+sN7qEVsfz+7+ZvAhY4Dj7Ido= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756366510; c=relaxed/simple; bh=8M2/0UwLcXRxMD4ixlU21GMSrgoCNNEd15QxD/byhss=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rCqoC+f/n3c8jb7TDrHyyfiNSYZAo+x/DyoSrtwhGATIlBCGnkugwMp/QxQEKMQ22/WijPCzwfdul5vwMsqCIYkT4ccuXwDD/8Yba6mUHRW00fTOeI1UuEgXj6ldWLPZlZtLE/6oX86WfVkeD02N6KOW8B006R8Pj+V9UkUn1ps= 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=Cox9wtDn; arc=none smtp.client-ip=209.85.210.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Cox9wtDn" Received: by mail-pf1-f175.google.com with SMTP id d2e1a72fcca58-771e1e64fbbso786682b3a.3; Thu, 28 Aug 2025 00:35:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1756366508; x=1756971308; 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=Cox9wtDne8+LjAmL8OUbPqLfknAWhTGmLu1WLIGOBCOiPt/GJputAsHABFQCwg6Rct 1gJFAk7Qt78msPe5Hq45CfE7yEOPtVIFsm73y72Bt22p0+reQjoCl/gL0kDyA++5UE3w 8e3p+mjIcFqhWMcsUYil1kgEt0/5NnaIouo9qFQh/uXLgrL3LBP3aVTuv8nxamZbZ+0J ZnYQ/7cWVXXn04O7jTrHPeRL4KcNcHL6BG+gHLu7Jjky5NadPGlSRp9r4GDvMoGgBoOd QtmePwMUpb9qGLOWuAC/thOPV0kDk7qj8Ifh0H0Oi/cHESBzsZd/45SrPOexWVxrtvdv PtDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756366508; x=1756971308; 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=JDgB3K2p/JDM/OajNttEi65mzeHrQGKajJno1pJJAggvTJCI/7tcAcMmBy3kE46jIu WshBSH9VEU5APhHmT6VoLWAULShLHZjKMlLxmCcLpfNBJrQPdHUJlw8poFPd5NtRgiTM iqvYcsSeZxF1d8c1z0fZ/BGCvYXGTF1OK9GVNZM9ffbpWX+XQeCGiAGtjhaur7IPKiRT BWEYyVv/nY70Xhj4mETXGm+Vfxm5nyD3OLG0zbdLM0PwqD5b7e83o5kZOL4/L3lxa8bm 7v7g4OrsEwnlmnw6H7PM13xjSX2A6t4wRKeaSWp7Tunm1dMxKAlb00OpON9qsZM4X1md fRXQ== X-Forwarded-Encrypted: i=1; AJvYcCXCU1W+oisOIAHqtJxGKXH6zDemS/Npm8mlOMOXgzKzDTxt2aAtvJsyTrdiqCr8CT3xWclvWW4c2Z37mv96s3iESqo=@vger.kernel.org X-Gm-Message-State: AOJu0Yzl1OdzSqS51ObPJ8vveq3G0zHEWQcH5VigZe1Srta/kbj8Ii0T UuWLeMzQTEJV9WcmZzL2YrqyNaFTHNDk/12QGjdVV4woP2wI5LYfrQme X-Gm-Gg: ASbGncuo3bNeTR+Hxwt2D6SpY/W8M7MuQsB1W/T+X9ZaLZe4LFS5lMubr6nxPbbkvA7 fIKvsdSxRV/iYF81glo9euJu04vDPkPnHMURFHHyb3yECVi6QjDWgOqorhLWKP9KIKdvEkfmcS7 qYqXbZ7oteqqKJPj/TxEQYh8PDs/wMe8Z75w9Mhe3vM3Yt4lw02JfVkv527yg35VL/mDbdZVKLD quykz/mfLrzJDVk+sfeXYhrheDaqTjDwH6mNx+fLFqYhg27MaRrExqO3iftgfTsFW1rTUnYItA/ 0/SQKkzvvOPlKEN4v37eKD+Kb/619My7v3sur7BFw1aSz1lT3vwD0uT4R6X+KdAYjPcK/aMxCLm 9f7BccvKqozWNlaKGFrf7mFbI4yzPt8TP2lF5ozqUVXiJUUFIvSD9QCa+yCB1 X-Google-Smtp-Source: AGHT+IG3E7HsLBsyZBtUPuuN6RttHjWI+4mOI8ICICciNWM3PhjKuCGhtkvM8/9T1iyfrPsEw+5ApQ== X-Received: by 2002:a05:6a20:7291:b0:243:b144:adaa with SMTP id adf61e73a8af0-243b144b09emr1456023637.54.1756366508342; Thu, 28 Aug 2025 00:35:08 -0700 (PDT) Received: from localhost.localdomain ([103.88.46.62]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-b49cb8afb7bsm13182613a12.16.2025.08.28.00.35.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Aug 2025 00:35:07 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Masami Hiramatsu , "Naveen N . Rao" , linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Jinchao Wang Subject: [PATCH 17/17] MAINTAINERS: add entry for KStackWatch (Kernel Stack Watch) Date: Thu, 28 Aug 2025 15:32:50 +0800 Message-ID: <20250828073311.1116593-18-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com> References: <20250828073311.1116593-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