[PATCH v5 3/3] riscv: kprobes: add KUnit test for LR/SC sequence rejection

Vladimir Kondratiev vladimir.kondratiev at mobileye.com
Wed Nov 25 08:12:19 PST 2026


Extend the kprobes KUnit test with a function containing an LR/SC
sequence and assert that register_kprobe() accepts a probe placed
right before or right after the sequence, and rejects a probe
placed on the LR, on an instruction inside the sequence, or on
the SC.

Add never-executed functions that imitates:
- valid short LR/SC loop
- valid maximum size LR/SC loop
- CAS like valid LR/SC with fail and success exit paths
- LR without SC mimicking __cmpwait where LR is followed by
  the conditional branch
- literals inside code, with value interpreted as SC

Suggested-by: Xiaofeng Yuan <yuanxiaofeng at eswincomputing.com>
Signed-off-by: Vladimir Kondratiev <vladimir.kondratiev at mobileye.com>
---
 arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S | 148 +++++++++++++++++++++
 arch/riscv/kernel/tests/kprobes/test-kprobes.c     |  32 +++++
 arch/riscv/kernel/tests/kprobes/test-kprobes.h     |   6 +
 3 files changed, 186 insertions(+)

diff --git a/arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S b/arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S
index 15e50934bbd7..f09b098bf771 100644
--- a/arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S
+++ b/arch/riscv/kernel/tests/kprobes/test-kprobes-asm.S
@@ -198,6 +198,118 @@ SYM_FUNC_END(test_kprobes_c_jal)
 
 #endif /* CONFIG_RISCV_ISA_C */
 
+SYM_FUNC_START(test_kprobes_lrsc)
+test_kprobes_lrsc_addr0:
+	nop
+	/* valid short LR/SC loop */
+1:
+test_kprobes_lrsc_addr1: /* expect: rejected */
+	lr.w t0, 0(a0)
+test_kprobes_lrsc_addr2: /* expect: rejected */
+	addi t0, t0, 1
+test_kprobes_lrsc_addr3: /* expect: rejected */
+	sc.w t0, t0, 0(a0)
+test_kprobes_lrsc_addr4:
+	bnez t0, 1b
+
+	/* part of __cmpwait - not a valid LR/SC sequence - no SC */
+test_kprobes_lrsc_addr5:
+	lr.w	t0, 0(a0)
+test_kprobes_lrsc_addr6:
+	and	t0, t0, a1
+	xor	t0, t0, a2
+	bnez	t0, 1f
+test_kprobes_lrsc_addr7:
+	nop /* shoult be wrs.nto on platforms with zawrs */
+1:
+	nop
+	ret
+
+	/* valid LR/SC loop with maximum number of instructions */
+1:
+test_kprobes_lrsc_addr8: /* expect: rejected */
+	lr.w t0, 0(a0)
+test_kprobes_lrsc_addr9: /* expect: rejected */
+	.rept 13
+	nop
+	.endr
+	sc.w t0, t0, 0(a0)
+	bnez t0, 1b
+	ret
+SYM_FUNC_END(test_kprobes_lrsc)
+
+/*
+ * CAS with both success and fail exit paths using
+ * instructions not acceptable inside LR/SC loop. Probe
+ * rejected for "cas*" labels inside LR/SC and accepted for
+ * "after_cas*" locations
+ */
+SYM_FUNC_START(test_kprobes_cas)
+1:
+cas0: /* expect: rejected */
+	lr.w   t0, (a0)
+cas1: /* expect: rejected */
+	bne    t0, a1, 2f
+cas2: /* expect: rejected */
+	sc.w   t0, a2, (a0)
+after_cas0:
+	bnez   t0, 1b
+	li     a0, 0
+	ret
+2:
+after_cas1:
+	lw     a0, (a0)
+	ret
+SYM_FUNC_END(test_kprobes_cas)
+
+/*
+ * valid constrained LR/SC sequence can contain a backward branch on a failure path.
+ *
+ * Consider this CAS example, where the failure path placed before the LR/SC sequence
+ */
+
+SYM_FUNC_START(test_kprobes_inv_cas)
+before_invcas0:
+	j 1f
+2:
+	li a0, -1
+before_invcas1:
+	ret
+1:
+invcas0: /* expect: rejected */
+	lr.w   t0, (a0)
+invcas1: /* expect: rejected */
+	bne    t0, a1, 2b
+invcas2: /* expect: rejected */
+	sc.w   t0, a2, (a0)
+after_invcas0:
+	bnez   t0, 1b
+	li     a0, 0
+after_invcas1:
+	ret
+SYM_FUNC_END(test_kprobes_inv_cas)
+
+
+/*
+ * Literals inside code, with value interpreted as CS
+ * shall not confuse the parser
+ * All locations acceptable
+ */
+SYM_FUNC_START(test_kprobes_literal)
+lit0:
+	nop
+lit1:
+	j 1f
+	.word 0x18c7b62f /* sc.d    a2,a2,(a5) */
+1:
+lit2:
+	jal x0, 1f /* force 32-bit jump instruction */
+	.word 0x18c7b62f /* sc.d    a2,a2,(a5) */
+1:
+lit3:
+	ret;
+SYM_FUNC_END(test_kprobes_literal)
+
 .section .rodata
 SYM_DATA_START(test_kprobes_addresses)
 	RISCV_PTR test_kprobes_add_addr1
@@ -253,3 +365,39 @@ SYM_DATA_START(test_kprobes_functions)
 #endif /* CONFIG_RISCV_ISA_C */
 	RISCV_PTR 0
 SYM_DATA_END(test_kprobes_functions)
+
+.section .rodata
+SYM_DATA_START(test_kprobes_lrsc_allowed_addresses)
+	RISCV_PTR test_kprobes_lrsc_addr0
+	RISCV_PTR test_kprobes_lrsc_addr4
+	RISCV_PTR test_kprobes_lrsc_addr5
+	RISCV_PTR test_kprobes_lrsc_addr6
+	RISCV_PTR test_kprobes_lrsc_addr7
+	RISCV_PTR after_cas0
+	RISCV_PTR after_cas1
+	RISCV_PTR lit0
+	RISCV_PTR lit1
+	RISCV_PTR lit2
+	RISCV_PTR lit3
+	RISCV_PTR before_invcas0
+	RISCV_PTR before_invcas1
+	RISCV_PTR after_invcas0
+	RISCV_PTR after_invcas1
+	RISCV_PTR 0
+SYM_DATA_END(test_kprobes_lrsc_allowed_addresses)
+
+.section .rodata
+SYM_DATA_START(test_kprobes_lrsc_rejected_addresses)
+	RISCV_PTR test_kprobes_lrsc_addr1
+	RISCV_PTR test_kprobes_lrsc_addr2
+	RISCV_PTR test_kprobes_lrsc_addr3
+	RISCV_PTR test_kprobes_lrsc_addr8
+	RISCV_PTR test_kprobes_lrsc_addr9
+	RISCV_PTR cas0
+	RISCV_PTR cas1
+	RISCV_PTR cas2
+	RISCV_PTR invcas0
+	RISCV_PTR invcas1
+	RISCV_PTR invcas2
+	RISCV_PTR 0
+SYM_DATA_END(test_kprobes_lrsc_rejected_addresses)
diff --git a/arch/riscv/kernel/tests/kprobes/test-kprobes.c b/arch/riscv/kernel/tests/kprobes/test-kprobes.c
index 027424a3ff7b..ff9d5bb2b998 100644
--- a/arch/riscv/kernel/tests/kprobes/test-kprobes.c
+++ b/arch/riscv/kernel/tests/kprobes/test-kprobes.c
@@ -43,8 +43,40 @@ static void test_kprobe_riscv(struct kunit *test)
 	kfree(kp);
 }
 
+static void test_kprobe_riscv_lrsc(struct kunit *test)
+{
+	/* probes outside the LR/SC sequence must be accepted */
+	for (int i = 0; test_kprobes_lrsc_allowed_addresses[i]; i++) {
+		struct kprobe kp = {};
+		int ret;
+
+		kp.pre_handler = kprobe_dummy_handler;
+		kp.addr = test_kprobes_lrsc_allowed_addresses[i];
+
+		ret = register_kprobe(&kp);
+		KUNIT_EXPECT_EQ_MSG(test, ret, 0, "location %pS rejected", kp.addr);
+		if (!ret)
+			unregister_kprobe(&kp);
+	}
+
+	/* probes on or inside the LR/SC sequence must be rejected */
+	for (int i = 0; test_kprobes_lrsc_rejected_addresses[i]; i++) {
+		struct kprobe kp = {};
+		int ret;
+
+		kp.pre_handler = kprobe_dummy_handler;
+		kp.addr = test_kprobes_lrsc_rejected_addresses[i];
+
+		ret = register_kprobe(&kp);
+		KUNIT_EXPECT_LT_MSG(test, ret, 0, "location %pS accepted", kp.addr);
+		if (!ret)
+			unregister_kprobe(&kp);
+	}
+}
+
 static struct kunit_case kprobes_testcases[] = {
 	KUNIT_CASE(test_kprobe_riscv),
+	KUNIT_CASE(test_kprobe_riscv_lrsc),
 	{}
 };
 
diff --git a/arch/riscv/kernel/tests/kprobes/test-kprobes.h b/arch/riscv/kernel/tests/kprobes/test-kprobes.h
index 537f44aa9d3f..93393e77a2cf 100644
--- a/arch/riscv/kernel/tests/kprobes/test-kprobes.h
+++ b/arch/riscv/kernel/tests/kprobes/test-kprobes.h
@@ -19,6 +19,12 @@ extern void *test_kprobes_addresses[];
 /* array of functions that return KPROBE_TEST_MAGIC */
 extern long (*test_kprobes_functions[])(void);
 
+/* probe points that must be accepted, terminated by NULL */
+extern void *test_kprobes_lrsc_allowed_addresses[];
+
+/* probe points that must be rejected, terminated by NULL */
+extern void *test_kprobes_lrsc_rejected_addresses[];
+
 #endif /* __ASSEMBLER__ */
 
 #endif /* TEST_KPROBES_H */

-- 
2.43.0




More information about the linux-riscv mailing list