linux/kernel/trace/rv/monitors/sts/sts_kunit.c
Gabriele Monaco 8da2a88383 rv: Add KUnit tests for some DA/HA monitors
Validate the functionality of DA monitors by injecting events in a
controlled environment (KUnit) and expecting reactions.

Events handlers are exported directly from the monitor source files
without using system events and with dummy arguments (e.g. no real
tasks). If the provided sequence of events incurs a violation, the test
expects the stub version of rv_react() to be called.

This testing method can validate the entire monitor implementation since
it sits between the monitor and the system (in place of the
tracepoints). All sorts of system and timing events can be emulated
without affecting the running kernel.

Handlers and monitor functions are exported as part of a struct to
simplify the process of running KUnit tests from kernel modules.

Reviewed-by: Nam Cao <namcao@linutronix.de>
Link: https://lore.kernel.org/r/20260723074534.43521-13-gmonaco@redhat.com
Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
2026-07-31 16:27:45 +02:00

40 lines
1.2 KiB
C

// SPDX-License-Identifier: GPL-2.0
#include <linux/kernel.h>
#include <linux/rv.h>
#include <rv/kunit.h>
#include <trace/events/sched.h>
#include "sts_kunit.h"
#if IS_REACHABLE(CONFIG_RV_MON_STS)
static void rv_test_sts(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_sts_ops.mon);
/* Per-CPU monitor, make sure we don't change CPU mid-test */
guard(migrate)();
/* Switch without disabling interrupts */
rv_sts_ops.handle_schedule_exit(NULL, false);
rv_sts_ops.handle_schedule_entry(NULL, false);
RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
rv_sts_ops.handle_sched_switch(NULL, 0, target, other, TASK_RUNNING);
rv_sts_ops.handle_schedule_exit(NULL, false);
/* Schedule from interrupt context */
rv_sts_ops.handle_schedule_entry(NULL, false);
rv_sts_ops.handle_irq_disable(NULL, 0, 0);
rv_sts_ops.handle_irq_entry(NULL, 0, NULL);
RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
rv_sts_ops.handle_sched_switch(NULL, 0, target, other, TASK_RUNNING);
rv_sts_ops.handle_irq_enable(NULL, 0, 0);
}
#else
#define rv_test_sts rv_test_stub
#endif