[PATCH 09/10] rv: Add KUnit test for throttle monitor
From: Gabriele Monaco
Date: Tue Sep 29 2026 - 08:56:05 EST
Add a test case for the throttle monitor to validate reactions occur
when a task:
* gets throttled on time but switched back in without replenish
* runs longer than its runtime
Signed-off-by: Gabriele Monaco <gmonaco@xxxxxxxxxx>
---
kernel/trace/rv/monitors/throttle/throttle.c | 17 +++++++
.../rv/monitors/throttle/throttle_kunit.c | 44 +++++++++++++++++++
.../rv/monitors/throttle/throttle_kunit.h | 34 ++++++++++++++
kernel/trace/rv/rv_monitors_test.c | 2 +
4 files changed, 97 insertions(+)
create mode 100644 kernel/trace/rv/monitors/throttle/throttle_kunit.c
create mode 100644 kernel/trace/rv/monitors/throttle/throttle_kunit.h
diff --git a/kernel/trace/rv/monitors/throttle/throttle.c b/kernel/trace/rv/monitors/throttle/throttle.c
index b22a2abe59fe..2b181c46b7b6 100644
--- a/kernel/trace/rv/monitors/throttle/throttle.c
+++ b/kernel/trace/rv/monitors/throttle/throttle.c
@@ -283,3 +283,20 @@ module_exit(unregister_throttle);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gabriele Monaco <gmonaco@xxxxxxxxxx>");
MODULE_DESCRIPTION("throttle: throttle dl entities when they use up their runtime.");
+
+#if IS_ENABLED(CONFIG_RV_MONITORS_KUNIT_TEST)
+#include <kunit/visibility.h>
+#include "throttle_kunit.h"
+
+const struct rv_throttle_ops rv_throttle_ops = {
+ .mon = RV_MON_OPS_INIT(),
+ .handle_dl_replenish = handle_dl_replenish,
+ .handle_dl_throttle = handle_dl_throttle,
+ .handle_sched_enqueue = handle_sched_enqueue,
+ .handle_sched_switch = handle_sched_switch,
+ .handle_sys_enter = handle_sys_enter,
+ .handle_newtask = handle_newtask,
+ .handle_dl_server_stop = handle_dl_server_stop,
+};
+EXPORT_SYMBOL_IF_KUNIT(rv_throttle_ops);
+#endif
diff --git a/kernel/trace/rv/monitors/throttle/throttle_kunit.c b/kernel/trace/rv/monitors/throttle/throttle_kunit.c
new file mode 100644
index 000000000000..8194095dbab8
--- /dev/null
+++ b/kernel/trace/rv/monitors/throttle/throttle_kunit.c
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/kernel.h>
+#include <linux/rv.h>
+#include <rv/kunit.h>
+#include <trace/events/sched.h>
+#include "throttle_kunit.h"
+
+#if IS_REACHABLE(CONFIG_RV_MON_THROTTLE)
+
+static void rv_test_throttle(struct kunit *test)
+{
+ struct task_struct *target = rv_kunit_alloc_mock_task(test);
+ struct task_struct *other = rv_kunit_alloc_mock_task(test);
+ struct rv_kunit_ctx *ctx = test->priv;
+
+ prepare_test(test, &rv_throttle_ops.mon);
+
+ target->pid = 99;
+ target->policy = SCHED_DEADLINE;
+ target->dl.runtime = 10000;
+ target->dl.deadline = 20000;
+
+ rv_throttle_ops.handle_newtask(NULL, target, 0);
+
+ /* Task gets throttled on time but switched back in without replenish */
+ rv_throttle_ops.handle_sched_switch(NULL, 0, other, target, TASK_RUNNING);
+ rv_throttle_ops.handle_dl_replenish(NULL, &target->dl, 0, DL_TASK);
+ udelay(9);
+ rv_throttle_ops.handle_dl_throttle(NULL, &target->dl, 0, DL_TASK);
+ RV_KUNIT_EXPECT_NO_REACTION(test, ctx);
+ rv_throttle_ops.handle_sched_switch(NULL, 0, other, target, TASK_RUNNING);
+ RV_KUNIT_EXPECT_REACTION(test, ctx);
+
+ /* Task runs longer than runtime */
+ rv_throttle_ops.handle_sched_switch(NULL, 0, other, target, TASK_RUNNING);
+ rv_throttle_ops.handle_dl_replenish(NULL, &target->dl, 0, DL_TASK);
+ udelay(10 + TICK_USEC);
+ rv_throttle_ops.handle_dl_throttle(NULL, &target->dl, 0, DL_TASK);
+ RV_KUNIT_EXPECT_REACTION(test, ctx);
+}
+
+#else
+#define rv_test_throttle rv_test_stub
+#endif
diff --git a/kernel/trace/rv/monitors/throttle/throttle_kunit.h b/kernel/trace/rv/monitors/throttle/throttle_kunit.h
new file mode 100644
index 000000000000..d730497deca4
--- /dev/null
+++ b/kernel/trace/rv/monitors/throttle/throttle_kunit.h
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Automatically generated by rvgen kunit.
+ * May need manual intervention for function prototypes that couldn't be
+ * found (e.g. are in another file) or variables to be exported.
+ */
+
+#ifndef __THROTTLE_KUNIT_H
+#define __THROTTLE_KUNIT_H
+
+#if IS_ENABLED(CONFIG_RV_MONITORS_KUNIT_TEST)
+
+#include <linux/rv.h>
+#include <rv/kunit.h>
+
+extern const struct rv_throttle_ops {
+ struct rv_kunit_mon mon;
+ void (*handle_dl_replenish)(void *data, struct sched_dl_entity *dl_se,
+ int cpu, u8 type);
+ void (*handle_dl_throttle)(void *data, struct sched_dl_entity *dl_se,
+ int cpu, u8 type);
+ void (*handle_sched_enqueue)(void *data, struct task_struct *tsk, int cpu);
+ void (*handle_sched_switch)(void *data, bool preempt,
+ struct task_struct *prev,
+ struct task_struct *next,
+ unsigned int prev_state);
+ void (*handle_sys_enter)(void *data, struct pt_regs *regs, long id);
+ void (*handle_newtask)(void *data, struct task_struct *task, u64 flags);
+ void (*handle_dl_server_stop)(void *data, struct sched_dl_entity *dl_se,
+ int cpu, u8 type);
+} rv_throttle_ops;
+#endif
+
+#endif /* __THROTTLE_KUNIT_H */
diff --git a/kernel/trace/rv/rv_monitors_test.c b/kernel/trace/rv/rv_monitors_test.c
index 3ad11195e664..13af7e26205c 100644
--- a/kernel/trace/rv/rv_monitors_test.c
+++ b/kernel/trace/rv/rv_monitors_test.c
@@ -151,6 +151,7 @@ static void rv_test_dummy(struct kunit *test)
#include "monitors/sts/sts_kunit.c"
#include "monitors/opid/opid_kunit.c"
#include "monitors/nomiss/nomiss_kunit.c"
+#include "monitors/throttle/throttle_kunit.c"
#include "monitors/pagefault/pagefault_kunit.c"
#include "monitors/sleep/sleep_kunit.c"
@@ -161,6 +162,7 @@ static struct kunit_case rv_mon_test_cases[] = {
KUNIT_CASE(rv_test_sts),
KUNIT_CASE(rv_test_opid),
KUNIT_CASE(rv_test_nomiss),
+ KUNIT_CASE(rv_test_throttle),
KUNIT_CASE(rv_test_pagefault),
KUNIT_CASE(rv_test_sleep),
{}
--
2.55.0