[RFC PATCH 12/12] rv: Add KUnit tests for some LTL monitors

Gabriele Monaco
2026-04-27T15:13:02Z

Validate the functionality of LTL monitors by injecting events in a
controlled environment (KUnit) and expecting reactions, just like it is
done in DA monitors.

Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
---
include/rv/ltl_monitor.h | 31 ++++++++++++++
.../trace/rv/monitors/pagefault/pagefault.c | 24 +++++++++++
kernel/trace/rv/monitors/sleep/sleep.c | 42 +++++++++++++++++++
kernel/trace/rv/rv_monitors_test.c | 2 +
kernel/trace/rv/rv_monitors_test.h | 18 ++++++++
5 files changed, 117 insertions(+)

diff --git a/include/rv/ltl_monitor.h b/include/rv/ltl_monitor.h
index 0f2c3820b9b8..49e680b769f6 100644
--- a/include/rv/ltl_monitor.h
+++ b/include/rv/ltl_monitor.h
@@ -172,3 +172,34 @@ static void __maybe_unused ltl_atom_pulse(struct task_struct *task, enum ltl_ato
ltl_atom_set(mon, atom, !value);
ltl_validate(task, mon);
}
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+#include <kunit/test.h>
+
+/*
+ * rv_prepare_test - Disable the monitor for a kunit test
+ */
+static inline void ltl_teardown_test(void *arg)
+{
+ struct rv_monitor *rv_this = arg;
+
+ rv_this->enabled = 0;
+ ltl_monitor_destroy();
+}
+
+/*
+ * rv_prepare_test - Enable the monitor for a kunit test
+ *
+ * Do the bare minimum to set up the monitor, make sure it is not active and
+ * real tracepoint handlers are NOT attached.
+ */
+static inline void ltl_prepare_test(struct kunit *test, struct rv_monitor *rv_this)
+{
+ KUNIT_ASSERT_FALSE(test, rv_this->enabled);
+ ltl_monitor_init();
+ rv_this->enabled = 1;
+
+ KUNIT_ASSERT_EQ(test, 0,
+ kunit_add_action_or_reset(test, ltl_teardown_test, rv_this));
+}
+#endif /* CONFIG_RV_MONITORS_KUNIT_TEST */
diff --git a/kernel/trace/rv/monitors/pagefault/pagefault.c b/kernel/trace/rv/monitors/pagefault/pagefault.c
index 56abe5079676..1849d7b81545 100644
--- a/kernel/trace/rv/monitors/pagefault/pagefault.c
+++ b/kernel/trace/rv/monitors/pagefault/pagefault.c
@@ -86,3 +86,27 @@ module_exit(unregister_pagefault);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Nam Cao <namcao@linutronix.de>");
MODULE_DESCRIPTION("pagefault: Monitor that RT tasks do not raise page faults");
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+#include "rv_monitors_test.h"
+
+void rv_test_pagefault(struct kunit *test)
+{
+ static struct task_struct *target;
+ struct rv_kunit_ctx *ctx = test->priv;
+
+ ltl_prepare_test(test, &rv_pagefault);
+ target = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+ target->policy = SCHED_FIFO;
+ target->prio = MAX_RT_PRIO - 1;
+ handle_task_newtask(NULL, target, 0);
+
+ ltl_attempt_start(target, ltl_get_monitor(target));
+
+ /* RT task has a page fault */
+ rv_mock_current(ctx, target);
+ handle_page_fault(NULL, 0, NULL, 0);
+ RV_KUNIT_EXPECT_REACTION(test, ctx);
+}
+EXPORT_SYMBOL_GPL(rv_test_pagefault);
+#endif
diff --git a/kernel/trace/rv/monitors/sleep/sleep.c b/kernel/trace/rv/monitors/sleep/sleep.c
index 7c16967add70..9388ffb55aa9 100644
--- a/kernel/trace/rv/monitors/sleep/sleep.c
+++ b/kernel/trace/rv/monitors/sleep/sleep.c
@@ -247,3 +247,45 @@ module_exit(unregister_sleep);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Nam Cao <namcao@linutronix.de>");
MODULE_DESCRIPTION("sleep: Monitor that RT tasks do not undesirably sleep");
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+#include "rv_monitors_test.h"
+
+void rv_test_sleep(struct kunit *test)
+{
+ static struct task_struct *target, *other;
+ struct rv_kunit_ctx *ctx = test->priv;
+ unsigned long args[6];
+ struct pt_regs regs;
+
+ ltl_prepare_test(test, &rv_sleep);
+ target = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+ target->policy = SCHED_FIFO;
+ target->prio = MAX_RT_PRIO - 2;
+ other = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+ other->policy = SCHED_FIFO;
+ other->prio = MAX_RT_PRIO - 1;
+ handle_task_newtask(NULL, target, 0);
+
+ /* RT task sleeps on a non RT-friendly nanosleep */
+ rv_mock_current(ctx, target);
+ args[0] = CLOCK_REALTIME;
+ syscall_set_arguments(target, &regs, args);
+ handle_sys_enter(NULL, &regs, __NR_clock_nanosleep);
+ handle_sys_exit(NULL, NULL, 0);
+ handle_sched_set_state(NULL, target, TASK_INTERRUPTIBLE);
+ RV_KUNIT_EXPECT_REACTION(test, ctx);
+
+ /* RT task woken up by lower priority task */
+ args[1] = FUTEX_WAIT;
+ syscall_set_arguments(target, &regs, args);
+ rv_mock_current(ctx, target);
+ handle_sys_enter(NULL, &regs, __NR_futex);
+ handle_sched_set_state(NULL, target, TASK_INTERRUPTIBLE);
+ rv_mock_current(ctx, other);
+ handle_sched_waking(NULL, target);
+ handle_sched_wakeup(NULL, target);
+ RV_KUNIT_EXPECT_REACTION(test, ctx);
+}
+EXPORT_SYMBOL_GPL(rv_test_sleep);
+#endif
diff --git a/kernel/trace/rv/rv_monitors_test.c b/kernel/trace/rv/rv_monitors_test.c
index a57a40932d34..78af77310d56 100644
--- a/kernel/trace/rv/rv_monitors_test.c
+++ b/kernel/trace/rv/rv_monitors_test.c
@@ -81,6 +81,8 @@ static struct kunit_case rv_trigger_test_cases[] = {
KUNIT_CASE(rv_test_sts),
KUNIT_CASE(rv_test_opid),
KUNIT_CASE(rv_test_nomiss),
+ KUNIT_CASE(rv_test_pagefault),
+ KUNIT_CASE(rv_test_sleep),
{}
};

diff --git a/kernel/trace/rv/rv_monitors_test.h b/kernel/trace/rv/rv_monitors_test.h
index 3015943c5dda..c745edc85991 100644
--- a/kernel/trace/rv/rv_monitors_test.h
+++ b/kernel/trace/rv/rv_monitors_test.h
@@ -70,3 +70,21 @@ static inline void rv_test_nomiss(struct kunit *test)
kunit_skip(test, "Monitor not enabled\n");
}
#endif
+
+#ifdef CONFIG_RV_MON_PAGEFAULT
+extern void rv_test_pagefault(struct kunit *test);
+#else
+static inline void rv_test_pagefault(struct kunit *test)
+{
+ kunit_skip(test, "Monitor not enabled\n");
+}
+#endif
+
+#ifdef CONFIG_RV_MON_SLEEP
+extern void rv_test_sleep(struct kunit *test);
+#else
+static inline void rv_test_sleep(struct kunit *test)
+{
+ kunit_skip(test, "Monitor not enabled\n");
+}
+#endif
--
2.53.0