[RFC PATCH 3/4] kcsan: add targeted watchpoint selftest

From: Hui Su

Date: Wed Oct 14 2026 - 05:54:01 EST


Test targeted address matching for exact and containing access ranges,
adjacent ranges, zero-sized accesses, and boundary addresses at ULONG_MAX.
These cases cover subtraction-based matching without runtime debugfs setup.

Signed-off-by: Hui Su <sh_def@xxxxxxx>
---
kernel/kcsan/selftest.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)

diff --git a/kernel/kcsan/selftest.c b/kernel/kcsan/selftest.c
index 84a1200271af..8da1ba2ba25a 100644
--- a/kernel/kcsan/selftest.c
+++ b/kernel/kcsan/selftest.c
@@ -12,6 +12,7 @@
#include <linux/init.h>
#include <linux/kcsan-checks.h>
#include <linux/kernel.h>
+#include <linux/limits.h>
#include <linux/printk.h>
#include <linux/random.h>
#include <linux/sched.h>
@@ -101,6 +102,33 @@ static bool __init test_matching_access(void)
return true;
}

+/* Test whether an access contains the selected target byte. */
+static bool __init test_target_matching(void)
+{
+ const unsigned long target = 0x1004;
+
+ if (WARN_ON(!kcsan_target_matches(target, 0x1004, 1)))
+ return false;
+ if (WARN_ON(!kcsan_target_matches(target, 0x1000, 8)))
+ return false;
+ if (WARN_ON(kcsan_target_matches(target, 0x1000, 4)))
+ return false;
+ if (WARN_ON(kcsan_target_matches(target, 0x1005, 1)))
+ return false;
+ if (WARN_ON(kcsan_target_matches(target, 0x1004, 0)))
+ return false;
+ if (WARN_ON(!kcsan_target_matches(ULONG_MAX, ULONG_MAX, 1)))
+ return false;
+ if (WARN_ON(!kcsan_target_matches(ULONG_MAX, ULONG_MAX - 3, 4)))
+ return false;
+ if (WARN_ON(kcsan_target_matches(ULONG_MAX, ULONG_MAX - 1, 1)))
+ return false;
+ if (WARN_ON(kcsan_target_matches(0, ULONG_MAX - 1, 4)))
+ return false;
+
+ return true;
+}
+
/*
* Correct memory barrier instrumentation is critical to avoiding false
* positives: simple test to check at boot certain barriers are always properly
@@ -251,6 +279,7 @@ static int __init kcsan_selftest(void)

RUN_TEST(test_encode_decode);
RUN_TEST(test_matching_access);
+ RUN_TEST(test_target_matching);
RUN_TEST(test_barrier);

pr_info("selftest: %d/%d tests passed\n", passed, total);
--
2.55.0