From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.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 E87F13D813C for ; Tue, 14 Jul 2026 18:33:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784053990; cv=none; b=HWfhJKlYlp4S5ygN6O/R6uJ7yFuA/RjM2l7wXr0jaQjiLAP8j89H1UTe2ogCu5bIsmlT4AjlnsyA3+Os3XIDQhUrFIG5q+p2PsligTdv18ng3C1VcLOu4OH3TJ/UIrfgnuGpGc3xfW3X9JDcecrohlT/uS1UNugRYLZ3o9/sysU= 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.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="dETxNDqx" Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-92e5c92c389so247197985a.3 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=o6i8GHGXFQpOmGSWgRrfoBw3ZwUp2Uc6jPDH4VQQEnAJPdoF3TzlCXTaRbcFqpmRG7 BlqvVc5IwTrD/fGYiYReL46ch5fYzY18Gq5MnkEsJ86xoNlqqqvcOCB4ajZgRDNLgVjv 8J8t7CVM/fS/HvrCB+cEtHyeQD26aRTy7hE8a9TQc5+5FrbWT7XXrB0Qy8KfvOwOtgaB Tbp5P+R0S3OHTX6GSLbl1G5dVdD2TLhqiHdR8U+0mbvW/p0nXjlAaIEoOhDTD5+shdg4 GejiZ5dKltGjuSuDeoyzT+bgCjmUlJyjKuuhWIjkHjQJYMJC5zqpmrFfSP0G/24cO7eh w3GQ== X-Forwarded-Encrypted: i=1; AHgh+RosjqptFmSvwQOZ/GrKEduF/2nrLJ6MbJTFFp8svmpA/1GcOjtHilbSCPOiGKyLuwR9u8tWTx0eFWopVVI=@vger.kernel.org X-Gm-Message-State: AOJu0Yxstabjsy1Qn3kqCFRE5UhNCU33lgYoKXCRLV3+de90eKGM6ZBI ElOZqCCHEJOOi5np09VGhxzwNhmZyu6cK85cDkjNV2J5fObkmJAIjW/i X-Gm-Gg: AfdE7ckXEMIv5vNf0iflh9/PrMLhhT7/CkoXDgFtWdiKL0nW1mVOz9Bv2yrBoKGmUhM CjGBf1GjTwd7fvWWY2VkwfhSVvftDzfYDc4PU9xo5ec6i4k7ajk1Xy5lSkHX4BtfSZCgrWPebrX r41H4HsdlUWQZ7obk3S/hGhNm8laIwRVLk8PQNuOTBwIjYh+p3Ncv5IuMXs/ysjvJk/MZsxRXSz O+rY6L4BXgp72+z2vqxSyq5MfH95iVM9KcINXiJpwbWgjCuNibQ4dcmi+iY7qC6Lwll2E742iiY I+WYVPx199XVcbOCOqPyltRF4rOpAdo3I10mr1i3y7SFatwo4pJre10Q6VgoQjas83/S8Iown3s t85sHknUgkvE/LXDfOZca4LZKsnp/eHe9F4V9uxRtuiAlJBc1Ki0XulLdxfHGgQpwFfE5EC4bZb 8eoZn5SiJaGc1CbLmqZIDpQqwW2H7uYTemdGAmPiOXTyv/JIuxqA5nXkRrgfQdlg== 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-kernel@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