From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.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 D23E72DEA8C for ; Tue, 14 Jul 2026 18:33:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784053990; cv=none; b=QU4s2yXeIij5oM8lvE567NXlTwWlSyYrAGcBrRTu7dUU0UJdlWe1bS1cssF33Hg8iNSxeDHM2la9LcJWi6/iDelWe8TCjxIlzlffr3fMDxkhgpNGbScmRXY33tUfssTHR6HTjumUr+uNVhqr+gIvPQ5j07klalUDCDkoV3sKAhg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784053990; c=relaxed/simple; bh=HtjHQF0w4jDf5jPZ2VynT9uUeex8aV3wyoJGoZkJo4Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ttbgoo6Ni2uNslWFZU65XJD2081m+LHqI3141qMyjY/zHN4N72rbeueIVvtrEdpKJrZezS0DVh7cnZnsCTsn15UJ0/gS3mDxCVnoldNnb9bExFcRvVoUSnkFBC848/9H0h3mK1monQ0YycMMmoDfnyJJSwRQj9WRNjiz1li91OE= 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=dETxNDqx; arc=none smtp.client-ip=209.85.222.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="dETxNDqx" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-92e50a650a0so240807685a.1 for ; Tue, 14 Jul 2026 11:33:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784053988; x=1784658788; 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:content-type; bh=88qx9YixiLkFD/vh8cYDKX7z/SdNlgRZ6Zz58PjlN0k=; b=dETxNDqxbHw0FOIZOXFfiIEdLJG9fczIlEfMsIYl4L11w87iwra4wFviXb+E5+9Tb6 Ay4Qj/8qa30SiDz0s9pi9SOfYl/QYzSrTJlEkaDYBNkGBa9XIoRxZ9RpgHGbXxXrwYDV cKKYgySsHnJM9cVT+7YpN/3EBL8LvrK6gwUSb/EWSAIHYjvdb0o41Vd/Jh6RZT6WHQVk 85ffKaWkfPT8SfsV+JLcGK4/KO+PemSss7PTRJM9hY0GTf7oCqkF9DBzVBGcYn/jq29/ mujz6yjLQup/HeovE77FFsUV2U7roGkdXhrJveLQjS0EHxcAmo87Bmn0y7nazv+rL1eu IDPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784053988; x=1784658788; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=88qx9YixiLkFD/vh8cYDKX7z/SdNlgRZ6Zz58PjlN0k=; b=g3vHv1RMsOr1Cktu9ipQWjWyR28miLHDoafh42jinBqDsU9YAxzILlp1W6ehortJYJ wFer18kzYTOjxlmA9Pk3uWwZ39SZJAsa7kMhtwY4P8DaCbLLemzKZKECf9tjrJjhPTB4 PboFmQOpeGlgb19Ksb30nT+Bo0RqWxP9DUkzsn9CmBAeiAHCkD1BA5lINC9pGkNIvPGM MFZFvDd8AE4m+QNnh/bNxZTl9sdiPoYKwmLgjOndfY5ICxKidqwfw0NJP8TP1/TjUW5x s5uDO6q1UzgpX9dsO016Kz8Dp4Y8P/B+jzek4Yp6fZRAKYbuaSQmmV3i718OZHn6Ji73 pbFA== X-Forwarded-Encrypted: i=1; AHgh+Rq0Y+WdeXSxX507r+ktMP2r/kcAqytyrTxdVBfFhaYGAug1HHLcAbfPUWvFk5djbDxsj0edjw6aq1w=@vger.kernel.org X-Gm-Message-State: AOJu0YwFlfYSUcGq3A+uwf9xQAiE/ysBVKkYUEoWx9qGvM3xFPC9cCEM gDH/fLBHdEqD+ZYKnI417JjmMGNbwtHLV5NNh05JMH88NPzNwMpixPsN X-Gm-Gg: AfdE7cmiiLHdCTeDJpV6Jqa/QbH57ZwLZ2alxFUF7V/pSvuM4qwFsBrGc0mVodQFy2V u9drpPmftSH72ndN3K5zbVb/dulA4mYnuch8zfvGQRTuh7pE1q2FK1FlTpYrvnBo5EH9OM5uBAH pJMufYVa4JWS1Zgveb1UfxC3bz3AqA1wWoDimZVzIWZHzvuIoFRkTGSaLII1VQkLuPL+/4+CUi9 /AgfW1Ipo8BTRlVvOG0k+K2nqWYaEv5bCMOiwCf3wVTF537emOr3ng26xqGspgA1W71txt9uK4n vajp8DZIDc9rq841D26eQKHHEMIsK/aAvhUagYK8xBAj71v07fBiKc4/IuU6Mcvv3B5zl8KlHiF 5b02GlVS/MeK1CWV2GTX6Tsv9nvUvl1rHi2zen4xJRXEK1Jv3gaak/G4GRGSC7yfy2rsESblhJv teS1YjTL6oGezGlOW/9RKwBSKkhCLBr6hUc0VqypeUL1p5gXqOEA3ojwgFblLs6w== X-Received: by 2002:a05:620a:2623:b0:92e:7ba3:740d with SMTP id af79cd13be357-93086c1dfb1mr389163985a.68.1784053987420; Tue, 14 Jul 2026 11:33:07 -0700 (PDT) Received: from localhost ([48.45.163.146]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92ee5d6c28bsm1566578185a.46.2026.07.14.11.33.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Jul 2026 11:33:06 -0700 (PDT) From: Jinchao Wang To: Andrew Morton , Peter Zijlstra , Thomas Gleixner , Steven Rostedt , Masami Hiramatsu Cc: Ingo Molnar , Borislav Petkov , Dave Hansen , "H . Peter Anvin" , x86@kernel.org, Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Mathieu Desnoyers , David Hildenbrand , Jonathan Corbet , Matthew Wilcox , linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-doc@vger.kernel.org, Jinchao Wang Subject: [RFC PATCH 10/13] mm/kwatch: add anchor thread for global watchpoints Date: Wed, 15 Jul 2026 02:32:53 +0800 Message-ID: <20260714183253.12848-1-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260714182243.10687-1-wangjinchao600@gmail.com> References: <20260714182243.10687-1-wangjinchao600@gmail.com> Precedence: bulk X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Global variables have no function whose execution can bound the watch window. Provide one: a kernel thread sleeps for the configured duration inside a dedicated noinline function, kwatch_global_anchor(), and the probe runtime hooks that function like any other target. When the duration expires the thread schedules a work item that tears the session down; the expired flag is cleared under the control-plane mutex so a stale work item from a previous session cannot stop a new one. Signed-off-by: Jinchao Wang --- mm/kwatch/Makefile | 2 +- mm/kwatch/anchor.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 mm/kwatch/anchor.c diff --git a/mm/kwatch/Makefile b/mm/kwatch/Makefile index f04673cc5b1c..b196c794619a 100644 --- a/mm/kwatch/Makefile +++ b/mm/kwatch/Makefile @@ -1,3 +1,3 @@ obj-$(CONFIG_KWATCH) += kwatch.o -kwatch-y := deref.o task_ctx.o hwbp.o probe.o +kwatch-y := deref.o task_ctx.o hwbp.o probe.o anchor.o diff --git a/mm/kwatch/anchor.c b/mm/kwatch/anchor.c new file mode 100644 index 000000000000..11da6aff9413 --- /dev/null +++ b/mm/kwatch/anchor.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include + +#include "kwatch.h" + +static DECLARE_WAIT_QUEUE_HEAD(kwatch_anchor_wq); +static struct task_struct *kwatch_anchor_tsk; +static bool kwatch_anchor_expired; + +bool kwatch_anchor_has_expired(void) +{ + return READ_ONCE(kwatch_anchor_expired); +} + +void kwatch_anchor_clear_expired(void) +{ + WRITE_ONCE(kwatch_anchor_expired, false); +} + +static void kwatch_auto_stop_handler(struct work_struct *work) +{ + kwatch_auto_stop(); +} + +static DECLARE_WORK(kwatch_auto_stop_work, kwatch_auto_stop_handler); + +noinline void kwatch_global_anchor(unsigned long duration_sec) +{ + wait_event_timeout(kwatch_anchor_wq, kthread_should_stop(), + duration_sec * HZ); +} + +static int kwatch_anchor_thread_fn(void *data) +{ + unsigned long duration = (unsigned long)data; + + kwatch_global_anchor(duration); + + if (!kthread_should_stop()) { + /* mark before scheduling; cleared under the control mutex */ + WRITE_ONCE(kwatch_anchor_expired, true); + schedule_work(&kwatch_auto_stop_work); + } + + while (!kthread_should_stop()) + schedule_timeout_uninterruptible(HZ); + + return 0; +} + +int kwatch_anchor_start(u16 duration) +{ + kwatch_anchor_tsk = kthread_run(kwatch_anchor_thread_fn, + (void *)(unsigned long)duration, + "kwatch_anchor"); + if (IS_ERR(kwatch_anchor_tsk)) { + int ret = PTR_ERR(kwatch_anchor_tsk); + + kwatch_anchor_tsk = NULL; + return ret; + } + return 0; +} + +void kwatch_anchor_stop(void) +{ + if (kwatch_anchor_tsk) { + kthread_stop(kwatch_anchor_tsk); + kwatch_anchor_tsk = NULL; + } +} + +void kwatch_anchor_cancel_work(void) +{ + cancel_work_sync(&kwatch_auto_stop_work); +} -- 2.53.0