From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.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 D10563FBEBB for ; Fri, 17 Jul 2026 13:04:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784293489; cv=none; b=p1UKyFDCfUfwbnBTbHzE5gVZUqHkvRuqDcPnWpusUyHSoRSKREDhXBsOyGcGeIkdM8LL/TjjpkcrwCdzSPfLMQrFCH8S6A+Zo35yYRXrpgfJgMcAi7GHM1fdZZequy8eiQZOH0aJR1as85f0e4ZqX+GqNglXNsTzlxJZ0YVtva0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784293489; c=relaxed/simple; bh=EtJoFWpzZWBdawFoDueuFU6iEwMn2zfmcjtMLM64xIU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ojvFemzP/ytp3nZ7vtEsz3VN6wzWRkJ7GyOoo7GJqtYPjFeLF1ELZtcbN4c9QzRURyHD8L4KzqbFRK3gicpu/ExlfpAdBxNWRyifPPugFLZPIlJEty8uACbcDPoUhJaBLRoFJ7oAinQaO7xiByo/iXlYcuP6xgw8ULa78NYLPso= 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=HQBPPhBU; arc=none smtp.client-ip=209.85.214.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="HQBPPhBU" Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-2caed617615so88129955ad.3 for ; Fri, 17 Jul 2026 06:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1784293487; x=1784898287; 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=4DcODrlK3jRROVEKQp+XA+e79BwmfTHaetARzpTbTQI=; b=HQBPPhBUqwGuBpyqLiWxUXiGJvihqyWKxg6MunhyX4Bg96StAMvwhCahapEjSE/DKO zaN6/yuYruwN23I49T5/cyA89FHiba/mfAJ899negpZZZzwmg8ZR+5ldWa3jH1G23Cvh 7N5ayIxfHwlZmcq3YRqh2Wi/awuqXZMUPXa+y0+5/YzqGxe/LprpxCM5MuVFazlMBFPm 5Hz8PWKa1NkaXwsEA7AW/M+g3Jin2+gPx+Om7vswC4kKGOoq1zjqzKQEVtVn5QrOmwXx aVTqXi8GuT20G46uYtrqgV9owAENAnDWL3sEU6VBBq5BAhRxMz2Wg+XLx1STNAHUjOVi 8AIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784293487; x=1784898287; 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=4DcODrlK3jRROVEKQp+XA+e79BwmfTHaetARzpTbTQI=; b=MjywYuTLiKhzz9oM4ZG0L708Khzw0eUvoizZu1/mjJSCXcpmHeaJV6ab5LchVvZCYy g5FNg7R5YEyfoQ9P4v40+tSB1rX9Rm6ganDZ/UHE9g+r2r5/ZN3W8N2uteZaVKMTkBqC ofixMcFkoLospbsvzE3pIafLCAU1E8cIaaQ/WM8rHOGf8A4pnGASeheiSGChgnWPnBdU BmtS/8y9EHdSxklGCcSfRZsdF2Bw9sx9rQlRrYSKQON617LcOLwz7XhMrXx4Xh8nGxpL nzBfx35GCFh8RWwq4AUPK0vZUQlimIRS7Peng/ugKEcWNXuFwy6vGttsGKuguo6G77tY 5lqQ== X-Forwarded-Encrypted: i=1; AHgh+Rpz1RLXGJ1WKNf+6cEY7asIC/27zen+ogS1Pmkewe47tLIZbpYt7GjVbyeRz7YCH4rVJLCyUOBgLM1wnfck6tG4g7E=@vger.kernel.org X-Gm-Message-State: AOJu0YxwWqis/emi0AcbqrfXJOVRAHm1XHH9QwHEM+Rug5lfiacbmk37 9oGN68DMroI8GSbM6QPX1nmBaHdtqjGz5r9EyeuqgEmbDWFghhOTqxtCt1kmnNU4UKk4Nw== X-Gm-Gg: AfdE7cn3DrmgVOQFsE4UYTNxz+vCHzhyvxSPicD8IQe4Uu9qbDJ1qTKBjT37d/G9NTo q5FEwJOMuWGOKArVVet5LxUZAzc80bWDI+WdhIRwlcryb8Ir7LL+YTQNCQccCJpe/ylfaZ++yaB P0a0GMp4O1Vi01S6OA4OKX0CgFgpA3+DXwmViJv4QnGSvn6Jh+t8vykND8IuXRRzhlQBVfO3HUx rXxH+4YVZw2Yqf3wuRW/0+mJBn5aUHe4qvyFD6j2sdjw4e5cnkHhMkOHKJisthOV12V1SCScf4w /u2tktICvfg+eVRo/8uOIblFlpLONJ7o0HVvr5QHLMphG1adqLKzTg4SabCZ5TMnaoXivOzIaK4 GT/OGt5D/buUG9lBGnbD0s65mZuja30Rf+8tqn713VyCeZfU9BgjKQEFo+GsHLupLYjZsj1B9P0 03uw== X-Received: by 2002:a17:903:32c3:b0:2cc:670d:9b2f with SMTP id d9443c01a7336-2cf349f4041mr28069715ad.33.1784293487195; Fri, 17 Jul 2026 06:04:47 -0700 (PDT) Received: from localhost ([144.24.58.22]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-13ce2cbb7c5sm5234901c88.10.2026.07.17.06.04.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jul 2026 06:04:45 -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 , Alan Stern , Randy Dunlap , Alexander Potapenko , Marco Elver , Mike Rapoport , 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 v2 06/13] mm/kwatch: add lockless per-task context pool Date: Fri, 17 Jul 2026 09:04:30 -0400 Message-ID: <20260717130430.1902527-1-wangjinchao600@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260717125023.1895892-1-wangjinchao600@gmail.com> References: <20260717125023.1895892-1-wangjinchao600@gmail.com> Precedence: bulk X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A task that enters the watched function needs somewhere to keep its window state (nesting depth, owned watchpoint, config epoch). The lookup runs in kprobe and NMI-like contexts, so it must not allocate or take locks. Use a preallocated open-addressing array hashed by task_struct pointer. Slots are claimed with cmpxchg() and released with smp_store_release(); lookup is a read-only probe sequence. The pool size (max_concurrency) bounds how many tasks can be inside watch windows concurrently; excess tasks are simply not tracked. Signed-off-by: Jinchao Wang --- mm/kwatch/Makefile | 2 +- mm/kwatch/task_ctx.c | 125 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 mm/kwatch/task_ctx.c diff --git a/mm/kwatch/Makefile b/mm/kwatch/Makefile index 69c21ae62123..cc6574df0d68 100644 --- a/mm/kwatch/Makefile +++ b/mm/kwatch/Makefile @@ -1,3 +1,3 @@ obj-$(CONFIG_KWATCH) += kwatch.o -kwatch-y := deref.o +kwatch-y := deref.o task_ctx.o diff --git a/mm/kwatch/task_ctx.c b/mm/kwatch/task_ctx.c new file mode 100644 index 000000000000..64383a4429e7 --- /dev/null +++ b/mm/kwatch/task_ctx.c @@ -0,0 +1,125 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include "kwatch.h" + +static u16 kwatch_ctx_pool_size; +static u16 kwatch_ctx_pool_mask; + +static struct kwatch_tsk_ctx *kwatch_ctx_pool; + +/* Pool size is a u16 and indexes a power-of-two hash table, so bound the + * request away from both the roundup_pow_of_two() u16 overflow (>32768 wraps + * to 0) and the degenerate size-1 case (ilog2(1) == 0 breaks hash_ptr()). + */ +#define KWATCH_CTX_POOL_MIN 256 +#define KWATCH_CTX_POOL_MAX 32768 + +int kwatch_tsk_ctx_prealloc(u16 max_concurrency) +{ + if (max_concurrency < KWATCH_CTX_POOL_MIN) + max_concurrency = KWATCH_CTX_POOL_MIN; + else if (max_concurrency > KWATCH_CTX_POOL_MAX) + max_concurrency = KWATCH_CTX_POOL_MAX; + + /* + * Set the size/mask only when actually allocating, so they can never + * drift out of sync with the live pool if prealloc is ever called + * again without a matching free. + */ + if (unlikely(!kwatch_ctx_pool)) { + kwatch_ctx_pool_size = roundup_pow_of_two(max_concurrency); + kwatch_ctx_pool_mask = kwatch_ctx_pool_size - 1; + + kwatch_ctx_pool = kcalloc(kwatch_ctx_pool_size, + sizeof(struct kwatch_tsk_ctx), + GFP_KERNEL); + if (!kwatch_ctx_pool) + return -ENOMEM; + } + return 0; +} + +struct kwatch_tsk_ctx *kwatch_tsk_ctx_get(bool can_alloc) +{ + int start_idx, i, idx; + struct task_struct *t; + + if (unlikely(!kwatch_ctx_pool)) + return NULL; + + start_idx = hash_ptr(current, ilog2(kwatch_ctx_pool_size)); + + for (i = 0; i < kwatch_ctx_pool_size; i++) { + idx = (start_idx + i) & kwatch_ctx_pool_mask; + t = READ_ONCE(kwatch_ctx_pool[idx].task); + if (t == current) + return &kwatch_ctx_pool[idx]; + } + + if (!can_alloc) + return NULL; + + for (i = 0; i < kwatch_ctx_pool_size; i++) { + idx = (start_idx + i) & kwatch_ctx_pool_mask; + t = READ_ONCE(kwatch_ctx_pool[idx].task); + if (!t) { + if (!cmpxchg(&kwatch_ctx_pool[idx].task, NULL, current)) + return &kwatch_ctx_pool[idx]; + } + } + + return NULL; +} + +void kwatch_tsk_ctx_reset(struct kwatch_tsk_ctx *ctx, u32 new_epoch) +{ + struct kwatch_watchpoint *wp = xchg(&ctx->wp, NULL); + + if (wp) + kwatch_hwbp_put(wp); + ctx->depth = 0; + ctx->epoch = new_epoch; +} + +/* Release a slot we hold a pointer to: disarm its wp and free the slot. */ +void kwatch_tsk_ctx_release(struct kwatch_tsk_ctx *ctx) +{ + kwatch_tsk_ctx_reset(ctx, 0); + + /* Pairs with READ_ONCE() in kwatch_tsk_ctx_get() */ + smp_store_release(&ctx->task, NULL); +} + +void kwatch_tsk_ctx_put(void) +{ + struct kwatch_tsk_ctx *ctx = kwatch_tsk_ctx_get(false); + + if (unlikely(!ctx)) + return; + + kwatch_tsk_ctx_release(ctx); +} + +void kwatch_tsk_ctx_release_wps(void) +{ + int i; + + if (!kwatch_ctx_pool) + return; + + for (i = 0; i < kwatch_ctx_pool_size; i++) { + struct kwatch_watchpoint *wp = xchg(&kwatch_ctx_pool[i].wp, + NULL); + if (wp) + kwatch_hwbp_put(wp); + } +} + +void kwatch_tsk_ctx_free(void) +{ + kfree(kwatch_ctx_pool); + kwatch_ctx_pool = NULL; +} -- 2.53.0