mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Hui Su <sh_def@163.com>
To: elver@google.com, dvyukov@google.com
Cc: kasan-dev@googlegroups.com, corbet@lwn.net,
	skhan@linuxfoundation.org, rdunlap@infradead.org,
	workflows@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [RFC PATCH 1/4] kcsan: add targeted watchpoint matching
Date: Wed, 14 Oct 2026 18:52:35 +0900	[thread overview]
Message-ID: <20261014095238.2132580-2-sh_def@163.com> (raw)

Add target state and a static key to KCSAN. When a target is set, only
non-atomic instrumented accesses containing that address are eligible for
watchpoint setup. Other accesses do not consume the skip interval. Keep
report filtering independent from target matching.

Signed-off-by: Hui Su <sh_def@163.com>
---
 kernel/kcsan/core.c  | 37 +++++++++++++++++++++++++++++++++++++
 kernel/kcsan/kcsan.h | 10 ++++++++++
 2 files changed, 47 insertions(+)

diff --git a/kernel/kcsan/core.c b/kernel/kcsan/core.c
index 2db82661cd60..2a58a3a53adb 100644
--- a/kernel/kcsan/core.c
+++ b/kernel/kcsan/core.c
@@ -12,10 +12,12 @@
 #include <linux/delay.h>
 #include <linux/export.h>
 #include <linux/init.h>
+#include <linux/jump_label.h>
 #include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/minmax.h>
 #include <linux/moduleparam.h>
+#include <linux/mutex.h>
 #include <linux/percpu.h>
 #include <linux/preempt.h>
 #include <linux/sched.h>
@@ -108,9 +110,40 @@ static atomic_long_t watchpoints[CONFIG_KCSAN_NUM_WATCHPOINTS + NUM_SLOTS-1];
  */
 static DEFINE_PER_CPU(long, kcsan_skip);
 
+static DEFINE_STATIC_KEY_FALSE(kcsan_target_key);
+static DEFINE_MUTEX(kcsan_target_lock);
+static unsigned long kcsan_target_addr;
+
 /* For kcsan_prandom_u32_max(). */
 static DEFINE_PER_CPU(u32, kcsan_rand_state);
 
+void kcsan_target_set(unsigned long addr)
+{
+	unsigned long old;
+
+	mutex_lock(&kcsan_target_lock);
+	old = READ_ONCE(kcsan_target_addr);
+
+	if (!old && addr) {
+		/* Publish the address before enabling the fast-path branch. */
+		WRITE_ONCE(kcsan_target_addr, addr);
+		static_branch_enable(&kcsan_target_key);
+	} else if (old && !addr) {
+		/* Disable the fast-path branch before invalidating the target. */
+		static_branch_disable(&kcsan_target_key);
+		WRITE_ONCE(kcsan_target_addr, 0);
+	} else if (old && addr) {
+		WRITE_ONCE(kcsan_target_addr, addr);
+	}
+
+	mutex_unlock(&kcsan_target_lock);
+}
+
+unsigned long kcsan_target_get(void)
+{
+	return READ_ONCE(kcsan_target_addr);
+}
+
 static __always_inline atomic_long_t *find_watchpoint(unsigned long addr,
 						      size_t size,
 						      bool expect_write,
@@ -277,6 +310,10 @@ should_watch(struct kcsan_ctx *ctx, const volatile void *ptr, size_t size, int t
 	if (is_atomic(ctx, ptr, size, type))
 		return false;
 
+	if (static_branch_unlikely(&kcsan_target_key))
+		return kcsan_target_matches(READ_ONCE(kcsan_target_addr),
+					    (unsigned long)ptr, size);
+
 	if (this_cpu_dec_return(kcsan_skip) >= 0)
 		return false;
 
diff --git a/kernel/kcsan/kcsan.h b/kernel/kcsan/kcsan.h
index ae33c2a7f07e..2a36813c3a76 100644
--- a/kernel/kcsan/kcsan.h
+++ b/kernel/kcsan/kcsan.h
@@ -25,6 +25,16 @@ extern unsigned int kcsan_udelay_interrupt;
  */
 extern bool kcsan_enabled;
 
+void kcsan_target_set(unsigned long addr);
+unsigned long kcsan_target_get(void);
+
+static __always_inline bool
+kcsan_target_matches(unsigned long target, unsigned long addr,
+		     size_t size)
+{
+	return size && target >= addr && target - addr < size;
+}
+
 /*
  * Save/restore IRQ flags state trace dirtied by KCSAN.
  */
-- 
2.55.0


             reply	other threads:[~2026-10-14  9:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-14  9:52 Hui Su [this message]
2026-10-15 14:33 ` Marco Elver
2026-10-15 15:27   ` Hui Su

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261014095238.2132580-2-sh_def@163.com \
    --to=sh_def@163.com \
    --cc=corbet@lwn.net \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=skhan@linuxfoundation.org \
    --cc=workflows@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®