Skip to contentSkip to conversation

Add selftests to tools and KUnit tests

PATCH Superseded Gabriele Monaco 2 participants 33 messages May 14 – May 28

Subsystems (2)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Gabriele Monaco

author maintainer
#0 lore raw

v2 cover Add selftests to tools and KUnit tests

This series adds support to the make check target in the rv userspace tool and the rvgen script, this allows to quickly validate its functionality. The selftest framework is inspired by the one used in RTLA.

A few bugs in both tools were also discovered and are fixed as part of this series.

Additionally it adds unit tests for models. This is achieved by running the handlers functions directly within KUnit, emulating all modules paths as if real kernel events fired.

Unit tests emulate a series of events that are expected to trigger violations and checks that a reaction occurred, stub structs and functions are used so the kernel is not affected by the test.

Differences since RFC [1]:
* Fix issue with LTL generator printing literals as uppercase
* Add missing state label in selftest dot spec
* Fail selftest if pid was required but not found (harness error)
* Remove useless static keywords in KUnit tests
* Assert after kunit_kzalloc()
* Use RV_KUNIT_EXPECT_REACTION_HERE to avoid false positives
* Prevent running RV monitors and events together with KUnit
* Rearrange KUnit testing headers
* Expect no reaction at the end of KUnit test cases
* Fix broken nomiss test and allocation

[1] - https://lore.kernel.org/lkml/[email protected]

To: [email protected]
To: [email protected]
Cc: Steven Rostedt <[email protected]>
Cc: Nam Cao <[email protected]>
Cc: Thomas Weissschuh <[email protected]>
Cc: Tomas Glozar <[email protected]>
Cc: John Kacur <[email protected]>
Cc: Wen Yang <[email protected]>

Gabriele Monaco (14):
tools/rv: Fix substring match bug in monitor name search tools/rv: Fix substring match when listing container monitors tools/rv: Fix exit status when monitor execution fails tools/rv: Fix cleanup after failed trace setup tools/rv: Add selftests
verification/rvgen: Fix options shared among commands verification/rvgen: Fix ltl2k writing True as a literal verification/rvgen: Add golden and spec folders for tests verification/rvgen: Add selftests
rv: Add KUnit stub to rv_react() and rv_*_task_monitor_slot()
rv: Add KUnit tests for some DA/HA monitors
rv: Add KUnit stubs for current and smp_processor_id()
rv: Prevent unintentional tracepoints during KUnit tests
rv: Add KUnit tests for some LTL monitors

 include/rv/da_monitor.h                       |  38 +++
 include/rv/instrumentation.h                  |   5 +
 include/rv/kunit.h                            |  59 +++++
 include/rv/ltl_monitor.h                      |  38 +++
 kernel/trace/rv/Kconfig                       |  14 +
 kernel/trace/rv/Makefile                      |   1 +
 kernel/trace/rv/monitors/nomiss/nomiss.c      |  44 ++++
 kernel/trace/rv/monitors/opid/opid.c          |  26 ++
 .../trace/rv/monitors/pagefault/pagefault.c   |  27 +-
 kernel/trace/rv/monitors/sco/sco.c            |  24 ++
 kernel/trace/rv/monitors/sleep/sleep.c        |  76 +++++-
 kernel/trace/rv/monitors/sssw/sssw.c          |  29 ++
 kernel/trace/rv/monitors/sts/sts.c            |  35 +++
 kernel/trace/rv/rv.c                          |   5 +
 kernel/trace/rv/rv_monitors_test.c            | 166 ++++++++++++
 kernel/trace/rv/rv_reactors.c                 |   7 +
 tools/verification/rv/Makefile                |   5 +-
 tools/verification/rv/src/in_kernel.c         |  58 ++--
 tools/verification/rv/src/rv.c                |   2 +-
 tools/verification/rv/tests/rv_list.t         |  48 ++++
 tools/verification/rv/tests/rv_mon.t          |  95 +++++++
 tools/verification/rvgen/Makefile             |   4 +
 tools/verification/rvgen/__main__.py          |  10 +-
 tools/verification/rvgen/rvgen/ltl2ba.py      |   9 +-
 .../rvgen/tests/golden/da_global/Kconfig      |   9 +
 .../rvgen/tests/golden/da_global/da_global.c  |  95 +++++++
 .../rvgen/tests/golden/da_global/da_global.h  |  47 ++++
 .../tests/golden/da_global/da_global_trace.h  |  15 ++
 .../tests/golden/da_perobj_parent/Kconfig     |  11 +
 .../da_perobj_parent/da_perobj_parent.c       | 110 ++++++++
 .../da_perobj_parent/da_perobj_parent.h       |  64 +++++
 .../da_perobj_parent/da_perobj_parent_trace.h |  15 ++
 .../tests/golden/da_pertask_desc/Kconfig      |   9 +
 .../golden/da_pertask_desc/da_pertask_desc.c  | 105 ++++++++
 .../golden/da_pertask_desc/da_pertask_desc.h  |  64 +++++
 .../da_pertask_desc/da_pertask_desc_trace.h   |  15 ++
 .../rvgen/tests/golden/ha_percpu/Kconfig      |   9 +
 .../rvgen/tests/golden/ha_percpu/ha_percpu.c  | 244 +++++++++++++++++
 .../rvgen/tests/golden/ha_percpu/ha_percpu.h  |  72 +++++
 .../tests/golden/ha_percpu/ha_percpu_trace.h  |  19 ++
 .../rvgen/tests/golden/ltl_pertask/Kconfig    |   9 +
 .../tests/golden/ltl_pertask/ltl_pertask.c    | 107 ++++++++
 .../tests/golden/ltl_pertask/ltl_pertask.h    | 108 ++++++++
 .../golden/ltl_pertask/ltl_pertask_trace.h    |  14 +
 .../rvgen/tests/golden/test_container/Kconfig |   5 +
 .../golden/test_container/test_container.c    |  35 +++
 .../golden/test_container/test_container.h    |   3 +
 .../rvgen/tests/golden/test_da/Kconfig        |   9 +
 .../rvgen/tests/golden/test_da/test_da.c      |  95 +++++++
 .../rvgen/tests/golden/test_da/test_da.h      |  47 ++++
 .../tests/golden/test_da/test_da_trace.h      |  15 ++
 .../rvgen/tests/golden/test_ha/Kconfig        |   9 +
 .../rvgen/tests/golden/test_ha/test_ha.c      | 247 ++++++++++++++++++
 .../rvgen/tests/golden/test_ha/test_ha.h      |  72 +++++
 .../tests/golden/test_ha/test_ha_trace.h      |  19 ++
 .../rvgen/tests/golden/test_ltl/Kconfig       |  11 +
 .../rvgen/tests/golden/test_ltl/test_ltl.c    | 108 ++++++++
 .../rvgen/tests/golden/test_ltl/test_ltl.h    | 108 ++++++++
 .../tests/golden/test_ltl/test_ltl_trace.h    |  14 +
 .../rvgen/tests/rvgen_container.t             |  20 ++
 .../verification/rvgen/tests/rvgen_monitor.t  |  87 ++++++
 .../rvgen/tests/specs/test_da.dot             |  16 ++
 .../rvgen/tests/specs/test_da2.dot            |  19 ++
 .../rvgen/tests/specs/test_ha.dot             |  27 ++
 .../rvgen/tests/specs/test_invalid.dot        |   8 +
 .../rvgen/tests/specs/test_invalid.ltl        |   1 +
 .../rvgen/tests/specs/test_invalid_ha.dot     |  16 ++
 .../rvgen/tests/specs/test_ltl.ltl            |   1 +
 tools/verification/tests/engine.sh            | 166 ++++++++++++
 69 files changed, 3075 insertions(+), 49 deletions(-)
 create mode 100644 include/rv/kunit.h
 create mode 100644 kernel/trace/rv/rv_monitors_test.c
 create mode 100644 tools/verification/rv/tests/rv_list.t
 create mode 100644 tools/verification/rv/tests/rv_mon.t
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/da_global.c
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/da_global.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/da_global_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent.c
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc.c
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.c
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.h
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask.c
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask.h
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_container/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_container/test_container.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_container/test_container.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/test_da.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/test_da.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/test_da_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/test_ha.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/test_ha.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/test_ha_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/test_ltl.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/test_ltl.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/test_ltl_trace.h
 create mode 100644 tools/verification/rvgen/tests/rvgen_container.t
 create mode 100644 tools/verification/rvgen/tests/rvgen_monitor.t
 create mode 100644 tools/verification/rvgen/tests/specs/test_da.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_da2.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_ha.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_invalid.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_invalid.ltl
 create mode 100644 tools/verification/rvgen/tests/specs/test_invalid_ha.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_ltl.ltl
 create mode 100644 tools/verification/tests/engine.sh

base-commit: e1914add2799225a87502051415fc5c32aeb02ae

Signature · 1 line
2.54.0
author maintainer
#1 lore raw

PATCH v2 1/14 tools/rv: Fix substring match bug in monitor name search

__ikm_find_monitor_name() relies on strstr() to find a monitor by name, which fails if the target monitor is a substring of a previously listed monitor.

Fix it by tokenizing the available_monitors file and matching full tokens instead.

---
 tools/verification/rv/src/in_kernel.c | 48 ++++++++++++++-------------
 1 file changed, 25 insertions(+), 23 deletions(-)
tools/verification/rv/src/in_kernel.c +25 -23
index 4bb746ea6e17..a200b63f4509 100644
@@ -58,38 +58,40 @@ static int __ikm_read_enable(char *monitor_name)
  */
 static int __ikm_find_monitor_name(char *monitor_name, char *out_name)
 {
-	char *available_monitors, container[MAX_DA_NAME_LEN+1], *cursor, *end;
-	int retval = 1;
+	char *available_monitors, *cursor, *line;
+	int len = strlen(monitor_name);
+	int found = 0;
 
 	available_monitors = tracefs_instance_file_read(NULL, "rv/available_monitors", NULL);
 	if (!available_monitors)
 		return -1;
 
-	cursor = strstr(available_monitors, monitor_name);
-	if (!cursor) {
-		retval = 0;
-		goto out_free;
-	}
+	config_is_container = 0;
+	cursor = available_monitors;
+	while ((line = strsep(&cursor, "\n"))) {
+		char *colon = strchr(line, ':');
 
-	for (; cursor > available_monitors; cursor--)
-		if (*(cursor-1) == '\n')
-			break;
-	end = strstr(cursor, "\n");
-	memcpy(out_name, cursor, end-cursor);
-	out_name[end-cursor] = '\0';
-
-	cursor = strstr(out_name, ":");
-	if (cursor)
-		*cursor = '/';
-	else {
-		sprintf(container, "%s:", monitor_name);
-		if (strstr(available_monitors, container))
-			config_is_container = 1;
+		if (strcmp(line, monitor_name) && (!colon || strcmp(colon + 1, monitor_name)))
+			continue;
+
+		strncpy(out_name, line, 2 * MAX_DA_NAME_LEN);
+		out_name[2 * MAX_DA_NAME_LEN - 1] = '\0';
+
+		if (colon) {
+			out_name[colon - line] = '/';
+		} else {
+			/* If there are children, they are on the next line. */
+			line = strsep(&cursor, "\n");
+			if (line && !strncmp(line, monitor_name, len) && line[len] == ':')
+				config_is_container = 1;
+		}
+
+		found = 1;
+		break;
 	}
 
-out_free:
 	free(available_monitors);
-	return retval;
+	return found;
 }
 
 /*
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH v2 01/14] tools/rv: Fix substring match bug in monitor name search

Quoted 8 lines · Gabriele Monaco

static int __ikm_find_monitor_name(char *monitor_name, char *out_name) {

tools/verification/rv/src/in_kernel.c +3 -2
-	char *available_monitors, container[MAX_DA_NAME_LEN+1], *cursor, *end;
-	int retval = 1;
+	char *available_monitors, *cursor, *line;
+	int len = strlen(monitor_name);
+	int found = 0;
 

tools/verification/rv/src/in_kernel.c static int __ikm_read_enable(char *monitor_name) · line 69

Quoted 10 lines
tools/verification/rv/src/in_kernel.c +1 -5
 	available_monitors = tracefs_instance_file_read(NULL, "rv/available_monitors", NULL);
 	if (!available_monitors)
 		return -1;
 
-	cursor = strstr(available_monitors, monitor_name);
-	if (!cursor) {
-		retval = 0;
-		goto out_free;
-	}
+	config_is_container = 0;

Isn't config_is_container unused?

Perhaps it is used in a follow-up patch? Let me keep reading..

Nam

Reviewed-by
#3 lore raw

Re: [PATCH v2 01/14] tools/rv: Fix substring match bug in monitor name search

Quoted 22 lines · Nam Cao
Quoted 8 lines · Gabriele Monaco

static int __ikm_find_monitor_name(char *monitor_name, char *out_name) {

tools/verification/rv/src/in_kernel.c +3 -2
-	char *available_monitors, container[MAX_DA_NAME_LEN+1], *cursor, *end;
-	int retval = 1;
+	char *available_monitors, *cursor, *line;
+	int len = strlen(monitor_name);
+	int found = 0;
 

tools/verification/rv/src/in_kernel.c static int __ikm_read_enable(char *monitor_name) · line 69

Quoted 10 lines
tools/verification/rv/src/in_kernel.c +1 -5
 	available_monitors = tracefs_instance_file_read(NULL, "rv/available_monitors", NULL);
 	if (!available_monitors)
 		return -1;
 
-	cursor = strstr(available_monitors, monitor_name);
-	if (!cursor) {
-		retval = 0;
-		goto out_free;
-	}
+	config_is_container = 0;

Isn't config_is_container unused?

Quoted 1 line

Perhaps it is used in a follow-up patch? Let me keep reading..

Never mind, I'm stupid.

author maintainer
#4 lore raw

PATCH v2 2/14 tools/rv: Fix substring match when listing container monitors

In reply to Gabriele Monaco

When listing monitors within a specific container (rv list <container>), the tool incorrectly matched monitors if the requested container name was only a prefix of the actual container (e.g., 'rv list sche' would incorrectly list monitors from 'sched:').

Fix this by ensuring the container name is an exact match and is immediately followed by the ':' separator.

---
 tools/verification/rv/src/in_kernel.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/verification/rv/src/in_kernel.c +6 -2
index a200b63f4509..95adae321c11 100644
@@ -193,8 +193,12 @@ static int ikm_fill_monitor_definition(char *name, struct monitor *ikm, char *co
 	nested_name = strstr(name, ":");
 	if (nested_name) {
 		/* it belongs in container if it starts with "container:" */
-		if (container && strstr(name, container) != name)
-			return 1;
+		if (container) {
+			int len = strlen(container);
+
+			if (strncmp(name, container, len) || name[len] != ':')
+				return 1;
+		}
 		*nested_name = '/';
 		++nested_name;
 		ikm->nested = 1;
Signature · 1 line
2.54.0
Reviewed-by on v2 2/14
#5 lore raw

Re: [PATCH v2 02/14] tools/rv: Fix substring match when listing container monitors

Quoted 9 lines · Gabriele Monaco

When listing monitors within a specific container (rv list <container>), the tool incorrectly matched monitors if the requested container name was only a prefix of the actual container (e.g., 'rv list sche' would incorrectly list monitors from 'sched:').

Fix this by ensuring the container name is an exact match and is immediately followed by the ':' separator.

author maintainer
#6 lore raw

PATCH v2 3/14 tools/rv: Fix exit status when monitor execution fails

In reply to Gabriele Monaco

When running "rv mon" on a monitor that is already enabled, the tool fails to start but incorrectly exits with a success status (0).

Fix the exit condition to ensure it returns a failure code on any execution error.

---
 tools/verification/rv/src/rv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/verification/rv/src/rv.c +1 -1
index b8fe24a87d97..6d65f4037581 100644
@@ -127,7 +127,7 @@ static void rv_mon(int argc, char **argv)
 
 	if (!run)
 		err_msg("rv: monitor %s does not exist\n", monitor_name);
-	exit(!run);
+	exit(run <= 0);
 }
 
 static void usage(int exit_val, const char *fmt, ...)
Signature · 1 line
2.54.0
Reviewed-by on v2 3/14
#7 lore raw

Re: [PATCH v2 03/14] tools/rv: Fix exit status when monitor execution fails

tools/verification/rv/src/rv.c static void rv_mon(int argc, char **argv) · line 130

Quoted 1 line · Gabriele Monaco
tools/verification/rv/src/rv.c +1 -0
+	exit(run <= 0);

Probably better to stick to the C standard:

exit(run > 0 ? EXIT_SUCCESS : EXIT_FAILURE)

but whatever.

author maintainer
#8 lore raw

Re: [PATCH v2 03/14] tools/rv: Fix exit status when monitor execution fails

Quoted 10 lines · Nam Cao

tools/verification/rv/src/rv.c static void rv_mon(int argc, char **argv) · line 130

Quoted 1 line · Gabriele Monaco
tools/verification/rv/src/rv.c +1 -0
+	exit(run <= 0);

Probably better to stick to the C standard:

    exit(run > 0 ? EXIT_SUCCESS : EXIT_FAILURE)

but whatever.

Good point, that's far more readable, will apply and perhaps use those macros also for other 0/1 in the file.

Thanks,
Gabriele

author maintainer
#9 lore raw

PATCH v2 4/14 tools/rv: Fix cleanup after failed trace setup

In reply to Gabriele Monaco

Currently if ikm_setup_trace_instance() fails, the tool returns without any cleanup, if rv was called with both -t and -r, this means the reactor is not going to be cleared.

Jump to the cleanup label to restore the reactor if necessary.

---
 tools/verification/rv/src/in_kernel.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/verification/rv/src/in_kernel.c +1 -1
index 95adae321c11..131a6787d639 100644
@@ -809,7 +809,7 @@ int ikm_run_monitor(char *monitor_name, int argc, char **argv)
 	if (config_trace) {
 		inst = ikm_setup_trace_instance(nested_name);
 		if (!inst)
-			return -1;
+			goto out_free_instance;
 	}
 
 	retval = ikm_enable(full_name);
Signature · 1 line
2.54.0
Reviewed-by on v2 4/14
#10 lore raw

Re: [PATCH v2 04/14] tools/rv: Fix cleanup after failed trace setup

Quoted 7 lines · Gabriele Monaco

Currently if ikm_setup_trace_instance() fails, the tool returns without any cleanup, if rv was called with both -t and -r, this means the reactor is not going to be cleared.

Jump to the cleanup label to restore the reactor if necessary.

author maintainer
#11 lore raw

PATCH v2 5/14 tools/rv: Add selftests

In reply to Gabriele Monaco

The rv tool needs automated testing to catch regressions and verify correct functionality across different usage scenarios.

Add selftests that validate monitor listing (including containers and nested monitors), monitor execution with different configurations (reactors, verbose output, tracing), and trace output format for both per-task and per-cpu monitors. Error handling paths are also tested. Tests use a shared engine for common patterns.

---
 tools/verification/rv/Makefile        |   5 +-
 tools/verification/rv/tests/rv_list.t |  48 ++++++++++
 tools/verification/rv/tests/rv_mon.t  |  95 ++++++++++++++++++
 tools/verification/tests/engine.sh    | 132 ++++++++++++++++++++++++++
 4 files changed, 279 insertions(+), 1 deletion(-)
 create mode 100644 tools/verification/rv/tests/rv_list.t
 create mode 100644 tools/verification/rv/tests/rv_mon.t
 create mode 100644 tools/verification/tests/engine.sh
tools/verification/rv/Makefile +4 -1
index 5b898360ba48..8ae5fc0d1d17 100644
@@ -78,4 +78,7 @@ clean: doc_clean fixdep-clean
 	$(Q)rm -f rv rv-static fixdep FEATURE-DUMP rv-*
 	$(Q)rm -rf feature
 
-.PHONY: FORCE clean
+check: $(RV)
+	RV=$(RV) prove -o --directives -f tests/
+
+.PHONY: FORCE clean check
tools/verification/rv/tests/rv_list.t new file +48 -0

48 lines not on this page · raw message

tools/verification/rv/tests/rv_mon.t new file +95 -0

95 lines not on this page · raw message

tools/verification/tests/engine.sh new file +132 -0

132 lines not on this page · raw message

Signature · 1 line
2.54.0
Acked-by on v2 5/14
#12 lore raw

Re: [PATCH v2 05/14] tools/rv: Add selftests

Quoted 10 lines · Gabriele Monaco

The rv tool needs automated testing to catch regressions and verify correct functionality across different usage scenarios.

Add selftests that validate monitor listing (including containers and nested monitors), monitor execution with different configurations (reactors, verbose output, tracing), and trace output format for both per-task and per-cpu monitors. Error handling paths are also tested. Tests use a shared engine for common patterns.

I am not good enough at bash script to review this. But test scripts can never hurt, so:

author maintainer
#13 lore raw

PATCH v2 6/14 verification/rvgen: Fix options shared among commands

In reply to Gabriele Monaco

After rvgen was refactored to use subparsers, the common options (-a and -D) were left in the main parser. This meant that they needed to be called /before/ the subcommand and using them without subcommand was allowed. This is not the original intent.

rvgen -D "some description" container -n name

Define the options as parent in the subparsers to allow them to be used from both subcommands together with other options.

rvgen container -n name -D "some description"

---
 tools/verification/rvgen/__main__.py | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
tools/verification/rvgen/__main__.py +6 -4
index 3be7f85fe37b..5c923dc10d0f 100644
@@ -18,14 +18,16 @@ if __name__ == '__main__':
     import sys
 
     parser = argparse.ArgumentParser(description='Generate kernel rv monitor')
-    parser.add_argument("-D", "--description", dest="description", required=False)
-    parser.add_argument("-a", "--auto_patch", dest="auto_patch",
+
+    parent_parser = argparse.ArgumentParser(add_help=False)
+    parent_parser.add_argument("-D", "--description", dest="description", required=False)
+    parent_parser.add_argument("-a", "--auto_patch", dest="auto_patch",
                         action="store_true", required=False,
                         help="Patch the kernel in place")
 
     subparsers = parser.add_subparsers(dest="subcmd", required=True)
 
-    monitor_parser = subparsers.add_parser("monitor")
+    monitor_parser = subparsers.add_parser("monitor", parents=[parent_parser])
     monitor_parser.add_argument('-n', "--model_name", dest="model_name")
     monitor_parser.add_argument("-p", "--parent", dest="parent",
                                 required=False, help="Create a monitor nested to parent")
@@ -36,7 +38,7 @@ if __name__ == '__main__':
     monitor_parser.add_argument('-t', "--monitor_type", dest="monitor_type", required=True,
                                 help=f"Available options: {', '.join(Monitor.monitor_types.keys())}")
 
-    container_parser = subparsers.add_parser("container")
+    container_parser = subparsers.add_parser("container", parents=[parent_parser])
     container_parser.add_argument('-n', "--model_name", dest="model_name", required=True)
 
     params = parser.parse_args()
Signature · 1 line
2.54.0
Reviewed-by on v2 6/14
#14 lore raw

Re: [PATCH v2 06/14] verification/rvgen: Fix options shared among commands

Quoted 5 lines · Gabriele Monaco

After rvgen was refactored to use subparsers, the common options (-a and -D) were left in the main parser. This meant that they needed to be called /before/ the subcommand and using them without subcommand was allowed. This is not the original intent.

Quoted 8 lines

rvgen -D "some description" container -n name

Define the options as parent in the subparsers to allow them to be used from both subcommands together with other options.

rvgen container -n name -D "some description"

I didn't know we can do this.

author maintainer
#15 lore raw

PATCH v2 7/14 verification/rvgen: Fix ltl2k writing True as a literal

In reply to Gabriele Monaco

The rvgen parser for LTL stores literal true values in the python representation (capitalised True), this doesn't build in C. The Literal class should already handle this case but ASTNode skips its strigification method and converts the value (true/false) directly.

Fix by delegating ASTNode stringification to the Literal and Variable classes instead of bypassing them.

Fixes 97ffa4ce6ab32 ("verification/rvgen: Add support for linear temporal logic")
---
 tools/verification/rvgen/rvgen/ltl2ba.py | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
tools/verification/rvgen/rvgen/ltl2ba.py +5 -4
index 7f538598a868..016e7cf93bbb 100644
@@ -122,10 +122,8 @@ class ASTNode:
         return self.op.expand(self, node, node_set)
 
     def __str__(self):
-        if isinstance(self.op, Literal):
-            return str(self.op.value)
-        if isinstance(self.op, Variable):
-            return self.op.name.lower()
+        if isinstance(self.op, (Literal, Variable)):
+            return str(self.op)
         return "val" + str(self.id)
 
     def normalize(self):
@@ -382,6 +380,9 @@ class Variable:
     def __iter__(self):
         yield from ()
 
+    def __str__(self):
+        return self.name.lower()
+
     def negate(self):
         new = ASTNode(self)
         return NotOp(new)
Signature · 1 line
2.54.0
Reviewed-by on v2 7/14
#16 lore raw

Re: [PATCH v2 07/14] verification/rvgen: Fix ltl2k writing True as a literal

Quoted 10 lines · Gabriele Monaco

The rvgen parser for LTL stores literal true values in the python representation (capitalised True), this doesn't build in C. The Literal class should already handle this case but ASTNode skips its strigification method and converts the value (true/false) directly.

Fix by delegating ASTNode stringification to the Literal and Variable classes instead of bypassing them.

Fixes 97ffa4ce6ab32 ("verification/rvgen: Add support for linear temporal logic")
author maintainer
#17 lore raw

PATCH v2 8/14 verification/rvgen: Add golden and spec folders for tests

In reply to Gabriele Monaco

Create reference models specifications and generated files in the golded folder. Those can be used as reference to validate rvgen still generates files as expected in automated tests.

---
 .../rvgen/tests/golden/da_global/Kconfig      |   9 +
 .../rvgen/tests/golden/da_global/da_global.c  |  95 +++++++
 .../rvgen/tests/golden/da_global/da_global.h  |  47 ++++
 .../tests/golden/da_global/da_global_trace.h  |  15 ++
 .../tests/golden/da_perobj_parent/Kconfig     |  11 +
 .../da_perobj_parent/da_perobj_parent.c       | 110 ++++++++
 .../da_perobj_parent/da_perobj_parent.h       |  64 +++++
 .../da_perobj_parent/da_perobj_parent_trace.h |  15 ++
 .../tests/golden/da_pertask_desc/Kconfig      |   9 +
 .../golden/da_pertask_desc/da_pertask_desc.c  | 105 ++++++++
 .../golden/da_pertask_desc/da_pertask_desc.h  |  64 +++++
 .../da_pertask_desc/da_pertask_desc_trace.h   |  15 ++
 .../rvgen/tests/golden/ha_percpu/Kconfig      |   9 +
 .../rvgen/tests/golden/ha_percpu/ha_percpu.c  | 244 +++++++++++++++++
 .../rvgen/tests/golden/ha_percpu/ha_percpu.h  |  72 +++++
 .../tests/golden/ha_percpu/ha_percpu_trace.h  |  19 ++
 .../rvgen/tests/golden/ltl_pertask/Kconfig    |   9 +
 .../tests/golden/ltl_pertask/ltl_pertask.c    | 107 ++++++++
 .../tests/golden/ltl_pertask/ltl_pertask.h    | 108 ++++++++
 .../golden/ltl_pertask/ltl_pertask_trace.h    |  14 +
 .../rvgen/tests/golden/test_container/Kconfig |   5 +
 .../golden/test_container/test_container.c    |  35 +++
 .../golden/test_container/test_container.h    |   3 +
 .../rvgen/tests/golden/test_da/Kconfig        |   9 +
 .../rvgen/tests/golden/test_da/test_da.c      |  95 +++++++
 .../rvgen/tests/golden/test_da/test_da.h      |  47 ++++
 .../tests/golden/test_da/test_da_trace.h      |  15 ++
 .../rvgen/tests/golden/test_ha/Kconfig        |   9 +
 .../rvgen/tests/golden/test_ha/test_ha.c      | 247 ++++++++++++++++++
 .../rvgen/tests/golden/test_ha/test_ha.h      |  72 +++++
 .../tests/golden/test_ha/test_ha_trace.h      |  19 ++
 .../rvgen/tests/golden/test_ltl/Kconfig       |  11 +
 .../rvgen/tests/golden/test_ltl/test_ltl.c    | 108 ++++++++
 .../rvgen/tests/golden/test_ltl/test_ltl.h    | 108 ++++++++
 .../tests/golden/test_ltl/test_ltl_trace.h    |  14 +
 .../rvgen/tests/specs/test_da.dot             |  16 ++
 .../rvgen/tests/specs/test_da2.dot            |  19 ++
 .../rvgen/tests/specs/test_ha.dot             |  27 ++
 .../rvgen/tests/specs/test_invalid.dot        |   8 +
 .../rvgen/tests/specs/test_invalid.ltl        |   1 +
 .../rvgen/tests/specs/test_invalid_ha.dot     |  16 ++
 .../rvgen/tests/specs/test_ltl.ltl            |   1 +
 42 files changed, 2026 insertions(+)
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/da_global.c
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/da_global.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_global/da_global_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent.c
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc.c
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc.h
 create mode 100644 tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.c
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.h
 create mode 100644 tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask.c
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask.h
 create mode 100644 tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_container/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_container/test_container.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_container/test_container.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/test_da.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/test_da.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_da/test_da_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/test_ha.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/test_ha.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ha/test_ha_trace.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/Kconfig
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/test_ltl.c
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/test_ltl.h
 create mode 100644 tools/verification/rvgen/tests/golden/test_ltl/test_ltl_trace.h
 create mode 100644 tools/verification/rvgen/tests/specs/test_da.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_da2.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_ha.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_invalid.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_invalid.ltl
 create mode 100644 tools/verification/rvgen/tests/specs/test_invalid_ha.dot
 create mode 100644 tools/verification/rvgen/tests/specs/test_ltl.ltl
tools/verification/rvgen/tests/golden/da_global/Kconfig new file +9 -0
new file mode 100644
index 000000000000..799fbf11c3ac
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_DA_GLOBAL
+	depends on RV
+	# XXX: add dependencies if there
+	select DA_MON_EVENTS_IMPLICIT
+	bool "da_global monitor"
+	help
+	  auto-generated
tools/verification/rvgen/tests/golden/da_global/da_global.c new file +95 -0

95 lines not on this page · raw message

tools/verification/rvgen/tests/golden/da_global/da_global.h new file +47 -0

47 lines not on this page · raw message

tools/verification/rvgen/tests/golden/da_global/da_global_trace.h new file +15 -0
new file mode 100644
index 000000000000..4d2730b71dd0
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_DA_GLOBAL
+DEFINE_EVENT(event_da_monitor, event_da_global,
+	     TP_PROTO(char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor, error_da_global,
+	     TP_PROTO(char *state, char *event),
+	     TP_ARGS(state, event));
+#endif /* CONFIG_RV_MON_DA_GLOBAL */
tools/verification/rvgen/tests/golden/da_perobj_parent/Kconfig new file +11 -0
new file mode 100644
index 000000000000..249ba3aee8d7
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_DA_PEROBJ_PARENT
+	depends on RV
+	# XXX: add dependencies if there
+	depends on RV_MON_PARENT_MON
+	default y
+	select DA_MON_EVENTS_ID
+	bool "da_perobj_parent monitor"
+	help
+	  auto-generated
tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent.c new file +110 -0

110 lines not on this page · raw message

tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent.h new file +64 -0

64 lines not on this page · raw message

tools/verification/rvgen/tests/golden/da_perobj_parent/da_perobj_parent_trace.h new file +15 -0
new file mode 100644
index 000000000000..59bfca8f73d2
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_DA_PEROBJ_PARENT
+DEFINE_EVENT(event_da_monitor_id, event_da_perobj_parent,
+	     TP_PROTO(int id, char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(id, state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor_id, error_da_perobj_parent,
+	     TP_PROTO(int id, char *state, char *event),
+	     TP_ARGS(id, state, event));
+#endif /* CONFIG_RV_MON_DA_PEROBJ_PARENT */
tools/verification/rvgen/tests/golden/da_pertask_desc/Kconfig new file +9 -0
new file mode 100644
index 000000000000..c6f350179098
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_DA_PERTASK_DESC
+	depends on RV
+	# XXX: add dependencies if there
+	select DA_MON_EVENTS_ID
+	bool "da_pertask_desc monitor"
+	help
+	  Custom description for testing
tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc.c new file +105 -0

105 lines not on this page · raw message

tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc.h new file +64 -0

64 lines not on this page · raw message

tools/verification/rvgen/tests/golden/da_pertask_desc/da_pertask_desc_trace.h new file +15 -0
new file mode 100644
index 000000000000..4e6086c4d86e
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_DA_PERTASK_DESC
+DEFINE_EVENT(event_da_monitor_id, event_da_pertask_desc,
+	     TP_PROTO(int id, char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(id, state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor_id, error_da_pertask_desc,
+	     TP_PROTO(int id, char *state, char *event),
+	     TP_ARGS(id, state, event));
+#endif /* CONFIG_RV_MON_DA_PERTASK_DESC */
tools/verification/rvgen/tests/golden/ha_percpu/Kconfig new file +9 -0
new file mode 100644
index 000000000000..0cc185ccfddf
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_HA_PERCPU
+	depends on RV
+	# XXX: add dependencies if there
+	select HA_MON_EVENTS_IMPLICIT
+	bool "ha_percpu monitor"
+	help
+	  auto-generated
tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.c new file +244 -0

244 lines not on this page · raw message

tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu.h new file +72 -0

72 lines not on this page · raw message

tools/verification/rvgen/tests/golden/ha_percpu/ha_percpu_trace.h new file +19 -0
new file mode 100644
index 000000000000..074ddff6a60d
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_HA_PERCPU
+DEFINE_EVENT(event_da_monitor, event_ha_percpu,
+	     TP_PROTO(char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor, error_ha_percpu,
+	     TP_PROTO(char *state, char *event),
+	     TP_ARGS(state, event));
+
+DEFINE_EVENT(error_env_da_monitor, error_env_ha_percpu,
+	     TP_PROTO(char *state, char *event, char *env),
+	     TP_ARGS(state, event, env));
+#endif /* CONFIG_RV_MON_HA_PERCPU */
tools/verification/rvgen/tests/golden/ltl_pertask/Kconfig new file +9 -0
new file mode 100644
index 000000000000..b37f46670bfd
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_LTL_PERTASK
+	depends on RV
+	# XXX: add dependencies if there
+	select LTL_MON_EVENTS_ID
+	bool "ltl_pertask monitor"
+	help
+	  auto-generated
tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask.c new file +107 -0

107 lines not on this page · raw message

tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask.h new file +108 -0

108 lines not on this page · raw message

tools/verification/rvgen/tests/golden/ltl_pertask/ltl_pertask_trace.h new file +14 -0
new file mode 100644
index 000000000000..ebd53621a5b1
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_LTL_PERTASK
+DEFINE_EVENT(event_ltl_monitor_id, event_ltl_pertask,
+	     TP_PROTO(struct task_struct *task, char *states, char *atoms, char *next),
+	     TP_ARGS(task, states, atoms, next));
+DEFINE_EVENT(error_ltl_monitor_id, error_ltl_pertask,
+	     TP_PROTO(struct task_struct *task),
+	     TP_ARGS(task));
+#endif /* CONFIG_RV_MON_LTL_PERTASK */
tools/verification/rvgen/tests/golden/test_container/Kconfig new file +5 -0
new file mode 100644
index 000000000000..2becb65dddad
@@ -0,0 +1,5 @@
+config RV_MON_TEST_CONTAINER
+	depends on RV
+	bool "test_container monitor"
+	help
+	  Test container for grouping monitors
tools/verification/rvgen/tests/golden/test_container/test_container.c new file +35 -0
new file mode 100644
index 000000000000..984e2eac7196
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/rv.h>
+
+#define MODULE_NAME "test_container"
+
+#include "test_container.h"
+
+struct rv_monitor rv_test_container = {
+	.name = "test_container",
+	.description = "Test container for grouping monitors",
+	.enable = NULL,
+	.disable = NULL,
+	.reset = NULL,
+	.enabled = 0,
+};
+
+static int __init register_test_container(void)
+{
+	return rv_register_monitor(&rv_test_container, NULL);
+}
+
+static void __exit unregister_test_container(void)
+{
+	rv_unregister_monitor(&rv_test_container);
+}
+
+module_init(register_test_container);
+module_exit(unregister_test_container);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("dot2k: auto-generated");
+MODULE_DESCRIPTION("test_container: Test container for grouping monitors");
tools/verification/rvgen/tests/golden/test_container/test_container.h new file +3 -0
new file mode 100644
index 000000000000..83e434432650
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+extern struct rv_monitor rv_test_container;
tools/verification/rvgen/tests/golden/test_da/Kconfig new file +9 -0
new file mode 100644
index 000000000000..0143a148ef34
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_TEST_DA
+	depends on RV
+	# XXX: add dependencies if there
+	select DA_MON_EVENTS_IMPLICIT
+	bool "test_da monitor"
+	help
+	  auto-generated
tools/verification/rvgen/tests/golden/test_da/test_da.c new file +95 -0

95 lines not on this page · raw message

tools/verification/rvgen/tests/golden/test_da/test_da.h new file +47 -0

47 lines not on this page · raw message

tools/verification/rvgen/tests/golden/test_da/test_da_trace.h new file +15 -0
new file mode 100644
index 000000000000..8bd67115d244
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_TEST_DA
+DEFINE_EVENT(event_da_monitor, event_test_da,
+	     TP_PROTO(char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor, error_test_da,
+	     TP_PROTO(char *state, char *event),
+	     TP_ARGS(state, event));
+#endif /* CONFIG_RV_MON_TEST_DA */
tools/verification/rvgen/tests/golden/test_ha/Kconfig new file +9 -0
new file mode 100644
index 000000000000..f4048290c774
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_TEST_HA
+	depends on RV
+	# XXX: add dependencies if there
+	select HA_MON_EVENTS_ID
+	bool "test_ha monitor"
+	help
+	  auto-generated
tools/verification/rvgen/tests/golden/test_ha/test_ha.c new file +247 -0

247 lines not on this page · raw message

tools/verification/rvgen/tests/golden/test_ha/test_ha.h new file +72 -0

72 lines not on this page · raw message

tools/verification/rvgen/tests/golden/test_ha/test_ha_trace.h new file +19 -0
new file mode 100644
index 000000000000..381bafcb3322
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_TEST_HA
+DEFINE_EVENT(event_da_monitor_id, event_test_ha,
+	     TP_PROTO(int id, char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(id, state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor_id, error_test_ha,
+	     TP_PROTO(int id, char *state, char *event),
+	     TP_ARGS(id, state, event));
+
+DEFINE_EVENT(error_env_da_monitor_id, error_env_test_ha,
+	     TP_PROTO(int id, char *state, char *event, char *env),
+	     TP_ARGS(id, state, event, env));
+#endif /* CONFIG_RV_MON_TEST_HA */
tools/verification/rvgen/tests/golden/test_ltl/Kconfig new file +11 -0
new file mode 100644
index 000000000000..e2d0e721f180
@@ -0,0 +1,11 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_TEST_LTL
+	depends on RV
+	# XXX: add dependencies if there
+	depends on RV_MON_LTL_PARENT
+	default y
+	select LTL_MON_EVENTS_ID
+	bool "test_ltl monitor"
+	help
+	  Simple description
tools/verification/rvgen/tests/golden/test_ltl/test_ltl.c new file +108 -0

108 lines not on this page · raw message

tools/verification/rvgen/tests/golden/test_ltl/test_ltl.h new file +108 -0

108 lines not on this page · raw message

tools/verification/rvgen/tests/golden/test_ltl/test_ltl_trace.h new file +14 -0
new file mode 100644
index 000000000000..3571b004c114
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_TEST_LTL
+DEFINE_EVENT(event_ltl_monitor_id, event_test_ltl,
+	     TP_PROTO(struct task_struct *task, char *states, char *atoms, char *next),
+	     TP_ARGS(task, states, atoms, next));
+DEFINE_EVENT(error_ltl_monitor_id, error_test_ltl,
+	     TP_PROTO(struct task_struct *task),
+	     TP_ARGS(task));
+#endif /* CONFIG_RV_MON_TEST_LTL */
tools/verification/rvgen/tests/specs/test_da.dot new file +16 -0
new file mode 100644
index 000000000000..e555c239b221
@@ -0,0 +1,16 @@
+digraph state_automaton {
+	{node [shape = circle] "state_b"};
+	{node [shape = plaintext, style=invis, label=""] "__init_state_a"};
+	{node [shape = doublecircle] "state_a"};
+	{node [shape = circle] "state_a"};
+	"__init_state_a" -> "state_a";
+	"state_a" [label = "state_a"];
+	"state_a" -> "state_a" [ label = "event_2" ];
+	"state_a" -> "state_b" [ label = "event_1" ];
+	"state_b" [label = "state_b"];
+	"state_b" -> "state_a" [ label = "event_2" ];
+	{ rank = min ;
+		"__init_state_a";
+		"state_a";
+	}
+}
tools/verification/rvgen/tests/specs/test_da2.dot new file +19 -0
new file mode 100644
index 000000000000..cdd4192f58ae
@@ -0,0 +1,19 @@
+digraph state_automaton {
+	{node [shape = circle] "state_b"};
+	{node [shape = circle] "state_c"};
+	{node [shape = plaintext, style=invis, label=""] "__init_state_a"};
+	{node [shape = doublecircle] "state_a"};
+	{node [shape = circle] "state_a"};
+	"__init_state_a" -> "state_a";
+	"state_a" [label = "state_a"];
+	"state_a" -> "state_b" [ label = "event_1" ];
+	"state_a" -> "state_c" [ label = "event_2" ];
+	"state_b" [label = "state_b"];
+	"state_b" -> "state_a" [ label = "event_2" ];
+	"state_b" -> "state_c" [ label = "event_3" ];
+	"state_c" [label = "state_c"];
+	{ rank = min ;
+		"__init_state_a";
+		"state_a";
+	}
+}
tools/verification/rvgen/tests/specs/test_ha.dot new file +27 -0
new file mode 100644
index 000000000000..786aa8b22098
@@ -0,0 +1,27 @@
+digraph state_automaton {
+	center = true;
+	size = "7,11";
+	{node [shape = circle] "S1"};
+	{node [shape = plaintext, style=invis, label=""] "__init_S0"};
+	{node [shape = doublecircle] "S0"};
+	{node [shape = circle] "S0"};
+	{node [shape = circle] "S2"};
+	{node [shape = circle] "S3"};
+	"__init_S0" -> "S0";
+	"S0" [label = "S0\nclk < bar_ns()", color = green3];
+	"S1" [label = "S1"];
+	"S2" [label = "S2\nclk < BAR_NS()"];
+	"S3" [label = "S3"];
+	"S1" -> "S0" [ label = "event0;reset(clk)" ];
+	"S0" -> "S1" [ label = "event1;reset(clk)" ];
+	"S0" -> "S0" [ label = "event0;reset(clk)" ];
+	"S1" -> "S2" [ label = "event2;env1 == 0;reset(clk)" ];
+	"S2" -> "S3" [ label = "event2" ];
+	"S2" -> "S2" [ label = "event1;clk < foo_ns" ];
+	"S3" -> "S0" [ label = "event0;clk < FOO_NS && env2 == 0" ];
+	"S3" -> "S1" [ label = "event1;clk < foo_ns && env1 == 1;reset(clk)" ];
+	{ rank = min ;
+		"__init_S0";
+		"S0";
+	}
+}
tools/verification/rvgen/tests/specs/test_invalid.dot new file +8 -0
new file mode 100644
index 000000000000..17c63fc57f17
@@ -0,0 +1,8 @@
+digraph invalid {
+	{node [shape = circle] "init"};
+	{node [shape = circle] "state1"};
+	"init" [label = "init"];
+	"init" -> "state1" [ label = "event_a" ];
+	"state1" [label = "state1"];
+	"state1" -> "init" [ label = "event_b" ];
+}
tools/verification/rvgen/tests/specs/test_invalid.ltl new file +1 -0
new file mode 100644
index 000000000000..cf36307e003c
@@ -0,0 +1 @@
+RULE = A invalid B
tools/verification/rvgen/tests/specs/test_invalid_ha.dot new file +16 -0
new file mode 100644
index 000000000000..06de6aa8709f
@@ -0,0 +1,16 @@
+digraph state_automaton {
+	{node [shape = circle] "state_b"};
+	{node [shape = plaintext, style=invis, label=""] "__init_state_a"};
+	{node [shape = doublecircle] "state_a"};
+	{node [shape = circle] "state_a"};
+	"__init_state_a" -> "state_a";
+	"state_a" [label = "state_a;clk < 1"];
+	"state_a" -> "state_a" [ label = "event_2;reset(clk)" ];
+	"state_a" -> "state_b" [ label = "event_1;wrong_constraint" ];
+	"state_b" [label = "state_b"];
+	"state_b" -> "state_a" [ label = "event_2" ];
+	{ rank = min ;
+		"__init_state_a";
+		"state_a";
+	}
+}
tools/verification/rvgen/tests/specs/test_ltl.ltl new file +1 -0
new file mode 100644
index 000000000000..5ed658abd69c
@@ -0,0 +1 @@
+RULE = always (EVENT_A imply eventually EVENT_B)
Signature · 1 line
2.54.0
Reviewed-by on v2 8/14
#18 lore raw

Re: [PATCH v2 08/14] verification/rvgen: Add golden and spec folders for tests

Quoted 5 lines · Gabriele Monaco

Create reference models specifications and generated files in the golded folder. Those can be used as reference to validate rvgen still generates files as expected in automated tests.

Didn't look at the "golden" files, I presume those are generated.

author maintainer
#19 lore raw

Re: [PATCH v2 08/14] verification/rvgen: Add golden and spec folders for tests

Quoted 10 lines · Nam Cao
Quoted 5 lines · Gabriele Monaco

Create reference models specifications and generated files in the golded folder. Those can be used as reference to validate rvgen still generates files as expected in automated tests.

Didn't look at the "golden" files, I presume those are generated.

Thanks for the review!

Yes the golden are generated, I checked them and had a few AIs run through them and that's how I spotted the True/true issue.
They aren't guaranteed to be spotless but again, some test is better than no test.

Thanks,
Gabriele

author maintainer
#20 lore raw

PATCH v2 9/14 verification/rvgen: Add selftests

In reply to Gabriele Monaco

The rvgen code generator needs validation to ensure it produces correct monitor implementations from input specifications.

Add selftests with golden reference outputs covering all monitor classes (DA, HA, LTL) and types (global, per_cpu, per_task, per_obj), including optional features like descriptions and parent monitors. Container generation and error handling (missing files, invalid specifications, missing arguments) are also validated against expected output.

---
 tools/verification/rvgen/Makefile             |  4 +
 .../rvgen/tests/rvgen_container.t             | 20 +++++
 .../verification/rvgen/tests/rvgen_monitor.t  | 87 +++++++++++++++++++
 tools/verification/tests/engine.sh            | 34 ++++++++
 4 files changed, 145 insertions(+)
 create mode 100644 tools/verification/rvgen/tests/rvgen_container.t
 create mode 100644 tools/verification/rvgen/tests/rvgen_monitor.t
tools/verification/rvgen/Makefile +4 -0
index cfc4056c1e87..2a2b9e64ea42 100644
@@ -13,6 +13,10 @@ all:
 .PHONY: clean
 clean:
 
+.PHONY: check
+check:
+	prove -o --directives -f tests/
+
 .PHONY: install
 install:
 	$(INSTALL) rvgen/automata.py -D -m 644 $(DESTDIR)$(PYLIB)/rvgen/automata.py
tools/verification/rvgen/tests/rvgen_container.t new file +20 -0
new file mode 100644
index 000000000000..fa4fb3db8288
@@ -0,0 +1,20 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+source ../tests/engine.sh
+test_begin
+
+set_timeout 30s
+
+# Help tests
+check "verify container subcommand help" \
+	"$RVGEN container -h" 0 "model_name" "class"
+
+check_and_compare_folder "container with description" \
+	"$RVGEN container -n test_container -D 'Test container for grouping monitors'" \
+	"test_container" "Writing the monitor into the directory test_container"
+
+# Error handling tests
+check "missing required model_name" \
+	"$RVGEN container" 2 "the following arguments are required: -n/--model_name"
+
+test_end
tools/verification/rvgen/tests/rvgen_monitor.t new file +87 -0

87 lines not on this page · raw message

tools/verification/tests/engine.sh +34 -0
index 76cc254ff94c..f86d44460895 100644
@@ -5,6 +5,8 @@ test_begin() {
 	# included correctly.
 	ctr=0
 	[ -z "$RV" ] && RV="../rv/rv"
+	[ -z "$RVGEN" ] && RVGEN="python3 ../rvgen"
+	[ -z "$GOLDEN_DIR" ] && GOLDEN_DIR="tests/golden"
 	[ -n "$TEST_COUNT" ] && echo "1..$TEST_COUNT"
 }
 
@@ -109,6 +111,38 @@ check_if_exists() {
 	fi
 }
 
+check_and_compare_folder() {
+	# Run command, compare generated folder to golden, and cleanup
+	local desc=$1
+	local command=$2
+	local generated_dir=$3
+	local expected_output=$4
+	local unexpected_output=$5
+	local golden_dir="$GOLDEN_DIR/$generated_dir"
+
+	ctr=$((ctr + 1))
+	if [ -n "$TEST_COUNT" ]; then
+		rm -rf "$generated_dir"
+		_check "$desc" "$command" 0 "$expected_output" "$unexpected_output"
+
+		if [ "$fail" -eq 0 ] && [ ! -d "$generated_dir" ]; then
+			failure "# Generated directory not found: $generated_dir"
+		fi
+
+		if [ "$fail" -ne 0 ]; then
+			:
+		elif ! diff -r "$generated_dir" "$golden_dir" &> /dev/null; then
+			failure "# Directories differ:"
+			failbuf+=$(diff -r "$generated_dir" "$golden_dir" 2>&1 | sed 's/^/#   /')
+			failbuf+=$'\n'
+		fi
+
+		report "$1"
+
+		rm -rf "$generated_dir"
+	fi
+}
+
 set_timeout() {
 	TIMEOUT="timeout -v -k 15s $1"
 }
Signature · 1 line
2.54.0
Acked-by on v2 9/14
#21 lore raw

Re: [PATCH v2 09/14] verification/rvgen: Add selftests

Quoted 10 lines · Gabriele Monaco

The rvgen code generator needs validation to ensure it produces correct monitor implementations from input specifications.

Add selftests with golden reference outputs covering all monitor classes (DA, HA, LTL) and types (global, per_cpu, per_task, per_obj), including optional features like descriptions and parent monitors. Container generation and error handling (missing files, invalid specifications, missing arguments) are also validated against expected output.

author maintainer
#22 lore raw

PATCH v2 10/14 Add KUnit stub to rv_react() and rv_*_task_monitor_slot()

In reply to Gabriele Monaco

Add KUNIT_STATIC_STUB_REDIRECT to allow those functions to be stubbed in a KUnit test. This is useful to catch reaction without creating a custom reactor and going through the effort of setting it from a test. rv_{get/put}_task_monitor_slot() rely on a lock, but this isn't necessary during a unit test, so simply skip the calls.

---
 kernel/trace/rv/rv.c          | 5 +++++
 kernel/trace/rv/rv_reactors.c | 7 +++++++
 2 files changed, 12 insertions(+)
kernel/trace/rv/rv.c +5 -0
index ee4e68102f17..f59385a24fa1 100644
@@ -142,6 +142,7 @@
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/slab.h>
+#include <kunit/static_stub.h>
 
 #ifdef CONFIG_RV_MON_EVENTS
 #define CREATE_TRACE_POINTS
@@ -171,6 +172,8 @@ int rv_get_task_monitor_slot(void)
 {
 	int i;
 
+	KUNIT_STATIC_STUB_REDIRECT(rv_get_task_monitor_slot);
+
 	lockdep_assert_held(&rv_interface_lock);
 
 	if (task_monitor_count == CONFIG_RV_PER_TASK_MONITORS)
@@ -192,6 +195,8 @@ int rv_get_task_monitor_slot(void)
 
 void rv_put_task_monitor_slot(int slot)
 {
+	KUNIT_STATIC_STUB_REDIRECT(rv_put_task_monitor_slot, slot);
+
 	lockdep_assert_held(&rv_interface_lock);
 
 	if (slot < 0 || slot >= CONFIG_RV_PER_TASK_MONITORS) {
kernel/trace/rv/rv_reactors.c +7 -0
index 460af07f7aba..3435dcedc7ee 100644
@@ -63,6 +63,7 @@
 
 #include <linux/lockdep.h>
 #include <linux/slab.h>
+#include <kunit/static_stub.h>
 
 #include "rv.h"
 
@@ -468,6 +469,12 @@ void rv_react(struct rv_monitor *monitor, const char *msg, ...)
 	static DEFINE_WAIT_OVERRIDE_MAP(rv_react_map, LD_WAIT_FREE);
 	va_list args;
 
+	__diag_push();
+	__diag_ignore(GCC, all, "-Wsuggest-attribute=format",
+		      "Not a valid __printf() conversion candidate.");
+	KUNIT_STATIC_STUB_REDIRECT(rv_react, monitor, msg);
+	__diag_pop();
+
 	if (!rv_reacting_on() || !monitor->react)
 		return;
 
Signature · 1 line
2.54.0
Reviewed-by on v2 10/14
#23 lore raw

Re: [PATCH v2 10/14] rv: Add KUnit stub to rv_react() and rv_*_task_monitor_slot()

Quoted 7 lines · Gabriele Monaco

Add KUNIT_STATIC_STUB_REDIRECT to allow those functions to be stubbed in a KUnit test. This is useful to catch reaction without creating a custom reactor and going through the effort of setting it from a test. rv_{get/put}_task_monitor_slot() rely on a lock, but this isn't necessary during a unit test, so simply skip the calls.

author maintainer
#24 lore raw

PATCH v2 11/14 Add KUnit tests for some DA/HA monitors

In reply to Gabriele Monaco

Validate the functionality of DA monitors by injecting events in a controlled environment (KUnit) and expecting reactions.

Events handlers are called 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.

---
 include/rv/da_monitor.h                  |  38 +++++++
 include/rv/kunit.h                       |  41 ++++++++
 kernel/trace/rv/Kconfig                  |  11 ++
 kernel/trace/rv/Makefile                 |   1 +
 kernel/trace/rv/monitors/nomiss/nomiss.c |  44 ++++++++
 kernel/trace/rv/monitors/opid/opid.c     |  26 +++++
 kernel/trace/rv/monitors/sco/sco.c       |  24 +++++
 kernel/trace/rv/monitors/sssw/sssw.c     |  29 ++++++
 kernel/trace/rv/monitors/sts/sts.c       |  35 +++++++
 kernel/trace/rv/rv_monitors_test.c       | 126 +++++++++++++++++++++++
 10 files changed, 375 insertions(+)
 create mode 100644 include/rv/kunit.h
 create mode 100644 kernel/trace/rv/rv_monitors_test.c
include/rv/da_monitor.h +38 -0
index 39765ff6f098..d16a55292f3f 100644
@@ -15,6 +15,7 @@
 #define _RV_DA_MONITOR_H
 
 #include <rv/automata.h>
+#include <rv/kunit.h>
 #include <linux/rv.h>
 #include <linux/stringify.h>
 #include <linux/bug.h>
@@ -817,4 +818,41 @@ static inline void da_reset(da_id_type id, monitor_target target)
 }
 #endif /* RV_MON_TYPE */
 
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+
+/*
+ * da_teardown_test - Disable the monitor for a kunit test
+ */
+static inline void da_teardown_test(void *arg)
+{
+	struct rv_monitor *rv_this = arg;
+	struct kunit *test = kunit_get_current_test();
+
+	if (test) {
+		struct rv_kunit_ctx *ctx = test->priv;
+
+		RV_KUNIT_EXPECT_NO_REACTION(test, ctx);
+	}
+
+	rv_this->enabled = 0;
+	da_monitor_destroy();
+}
+
+/*
+ * da_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 da_prepare_test(struct kunit *test, struct rv_monitor *rv_this)
+{
+	KUNIT_ASSERT_FALSE(test, rv_this->enabled);
+	da_monitor_init();
+	rv_this->enabled = 1;
+
+	KUNIT_ASSERT_EQ(test, 0,
+			kunit_add_action_or_reset(test, da_teardown_test, rv_this));
+}
+#endif /* CONFIG_RV_MONITORS_KUNIT_TEST */
+
 #endif
include/rv/kunit.h new file +41 -0

41 lines not on this page · raw message

kernel/trace/rv/Kconfig +11 -0
index 3884b14df375..d7dba4453bd3 100644
@@ -111,3 +111,14 @@ config RV_REACT_PANIC
 	help
 	  Enables the panic reactor. The panic reactor emits a printk()
 	  message if an exception is found and panic()s the system.
+
+config RV_MONITORS_KUNIT_TEST
+	bool "KUnit tests for RV monitors" if !KUNIT_ALL_TESTS
+	depends on KUNIT=y && RV
+	default KUNIT_ALL_TESTS
+	help
+	  Enable KUnit tests for the RV (Runtime Verification) monitors.
+	  These tests verify that monitors correctly detect violations by
+	  triggering fake events and validating the expected reactions.
+
+	  If unsure, say N.
kernel/trace/rv/Makefile +1 -0
index 94498da35b37..a3502b7fe7f2 100644
@@ -24,3 +24,4 @@ obj-$(CONFIG_RV_MON_NOMISS) += monitors/nomiss/nomiss.o
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
 obj-$(CONFIG_RV_REACT_PANIC) += reactor_panic.o
+obj-$(CONFIG_RV_MONITORS_KUNIT_TEST) += rv_monitors_test.o
kernel/trace/rv/monitors/nomiss/nomiss.c +44 -0

47 lines not on this page · raw message

kernel/trace/rv/monitors/opid/opid.c +26 -0
index 4594c7c46601..124dd043999f 100644
@@ -121,3 +121,29 @@ module_exit(unregister_opid);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Gabriele Monaco <[email protected]>");
 MODULE_DESCRIPTION("opid: operations with preemption and irq disabled.");
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+void rv_test_opid(struct kunit *test);
+
+void rv_test_opid(struct kunit *test)
+{
+	struct rv_kunit_ctx *ctx = test->priv;
+
+	da_prepare_test(test, &rv_this);
+
+	/* Ensure we keep the same per-cpu monitor */
+	guard(migrate)();
+	KUNIT_EXPECT_TRUE(test, preemptible());
+
+	/* Wakeup with preemption and interrupts enabled */
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
+		handle_sched_waking(NULL, NULL);
+
+	/* Need resched with interrupts enabled */
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx) {
+		scoped_guard(preempt)
+			handle_sched_need_resched(NULL, NULL, 0, TIF_NEED_RESCHED);
+	}
+}
+EXPORT_SYMBOL_GPL(rv_test_opid);
+#endif
kernel/trace/rv/monitors/sco/sco.c +24 -0
index 5a3bd5e16e62..40eab946574b 100644
@@ -83,3 +83,27 @@ module_exit(unregister_sco);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Gabriele Monaco <[email protected]>");
 MODULE_DESCRIPTION("sco: scheduling context operations.");
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+void rv_test_sco(struct kunit *test);
+
+void rv_test_sco(struct kunit *test)
+{
+	struct task_struct *target;
+	struct rv_kunit_ctx *ctx = test->priv;
+
+	da_prepare_test(test, &rv_this);
+	target = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, target);
+
+	/* Ensure we keep the same per-cpu monitor */
+	guard(migrate)();
+
+	/* Set state while scheduling */
+	handle_sched_set_state(NULL, target, TASK_INTERRUPTIBLE);
+	handle_schedule_entry(NULL, false);
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
+		handle_sched_set_state(NULL, target, TASK_INTERRUPTIBLE);
+}
+EXPORT_SYMBOL_GPL(rv_test_sco);
+#endif
kernel/trace/rv/monitors/sssw/sssw.c +29 -0
index a91321c890cd..6d33b740474c 100644
@@ -112,3 +112,32 @@ module_exit(unregister_sssw);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Gabriele Monaco <[email protected]>");
 MODULE_DESCRIPTION("sssw: set state sleep and wakeup.");
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+void rv_test_sssw(struct kunit *test);
+
+void rv_test_sssw(struct kunit *test)
+{
+	struct task_struct *target, *other;
+	struct rv_kunit_ctx *ctx = test->priv;
+
+	da_prepare_test(test, &rv_this);
+	target = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, target);
+	other = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, other);
+
+	/* Suspend without setting to sleepable */
+	handle_sched_set_state(NULL, target, TASK_RUNNING);
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
+		handle_sched_switch(NULL, 0, target, other, TASK_INTERRUPTIBLE);
+
+	/* Switch in after suspension without wakeup */
+	handle_sched_wakeup(NULL, target);
+	handle_sched_set_state(NULL, target, TASK_INTERRUPTIBLE);
+	handle_sched_switch(NULL, 0, target, other, TASK_INTERRUPTIBLE);
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
+		handle_sched_switch(NULL, 0, other, target, TASK_RUNNING);
+}
+EXPORT_SYMBOL_GPL(rv_test_sssw);
+#endif
kernel/trace/rv/monitors/sts/sts.c +35 -0
index ce031cbf202a..587ec44fb509 100644
@@ -152,3 +152,38 @@ module_exit(unregister_sts);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Gabriele Monaco <[email protected]>");
 MODULE_DESCRIPTION("sts: schedule implies task switch.");
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+void rv_test_sts(struct kunit *test);
+
+void rv_test_sts(struct kunit *test)
+{
+	struct task_struct *target, *other;
+	struct rv_kunit_ctx *ctx = test->priv;
+
+	da_prepare_test(test, &rv_this);
+	target = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, target);
+	other = kunit_kzalloc(test, sizeof(struct task_struct), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, other);
+	/* Per-CPU monitor, make sure we don't change CPU mid-test */
+	guard(migrate)();
+
+	/* Switch without disabling interrupts */
+	handle_schedule_exit(NULL, false);
+	handle_schedule_entry(NULL, false);
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
+		handle_sched_switch(NULL, 0, target, other, TASK_RUNNING);
+
+	handle_schedule_exit(NULL, false);
+
+	/* Schedule from interrupt context */
+	handle_schedule_entry(NULL, false);
+	handle_irq_disable(NULL, 0, 0);
+	handle_irq_entry(NULL, 0, NULL);
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
+		handle_sched_switch(NULL, 0, target, other, TASK_RUNNING);
+	handle_irq_enable(NULL, 0, 0);
+}
+EXPORT_SYMBOL_GPL(rv_test_sts);
+#endif
kernel/trace/rv/rv_monitors_test.c new file +126 -0

126 lines not on this page · raw message

Signature · 1 line
2.54.0
Reviewed-by on v2 11/14
#25 lore raw

Re: [PATCH v2 11/14] rv: Add KUnit tests for some DA/HA monitors

include/rv/kunit.h line 38

Quoted 4 lines · Gabriele Monaco
include/rv/kunit.h new file +4 -0
+#define RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)                             \
+	for (int __done = ({ RV_KUNIT_EXPECT_NO_REACTION(test, ctx); 0; });  \
+	     !__done;                                                        \
+	     __done = ({ RV_KUNIT_EXPECT_REACTION(test, ctx); 1; }))

Cool trick.

kernel/trace/rv/rv_monitors_test.c line 103

Quoted 13 lines
kernel/trace/rv/rv_monitors_test.c new file +13 -0
+static void rv_test_stub(struct kunit *test)
+{
+	kunit_skip(test, "Monitor not enabled\n");
+}
+
+#define DECLARE_RV_TEST(name) \
+	void name(struct kunit *test) __weak __alias(rv_test_stub)
+
+DECLARE_RV_TEST(rv_test_sco);
+DECLARE_RV_TEST(rv_test_sssw);
+DECLARE_RV_TEST(rv_test_sts);
+DECLARE_RV_TEST(rv_test_opid);
+DECLARE_RV_TEST(rv_test_nomiss);

Good to know this one. I think I can make use of this trick for some LTL improvement..

author maintainer
#26 lore raw

PATCH v2 12/14 Add KUnit stubs for current and smp_processor_id()

In reply to Gabriele Monaco

Some monitors do not only rely on tracepoint arguments but also on the currently executing task and current processor.
This makes it more challenging to mock events in KUnit.

Define wrapper functions around current and smp_processor_id(), the functionality is stubbed only during KUnit, however the additional function call is necessary whenever the KUnit tests are built in.

---
 include/rv/kunit.h                            | 18 ++++++++++++-
 include/rv/ltl_monitor.h                      |  1 +
 kernel/trace/rv/Kconfig                       |  3 +++
 .../trace/rv/monitors/pagefault/pagefault.c   |  2 +-
 kernel/trace/rv/monitors/sleep/sleep.c        | 24 ++++++++---------
 kernel/trace/rv/rv_monitors_test.c            | 26 +++++++++++++++++++
 6 files changed, 60 insertions(+), 14 deletions(-)
include/rv/kunit.h +17 -1
index 67f6057bd5b1..3ef83d337880 100644
@@ -2,7 +2,10 @@
 /*
  * Copyright (C) 2026-2029 Red Hat, Inc. Gabriele Monaco <[email protected]>
  *
- * Declaration of utilities to run KUnit tests.
+ * Declaration of wrappers to allow stubbing core functionality, like current
+ * and smp_processor_id(), and other testing utilities.
+ * Necessary only when mocking may be needed. If the RV KUnit test is
+ * enabled, the wrappers incur an additional function call overhead.
  */
 
 #ifndef _RV_KUNIT_H
@@ -14,8 +17,13 @@
 #include <kunit/test-bug.h>
 #include <linux/delay.h>
 
+struct task_struct *rv_get_current(void);
+int rv_current_cpu(void);
+
 struct rv_kunit_ctx {
 	int reactions, expected;
+	int cpu;
+	struct task_struct *curr;
 };
 
 #define RV_KUNIT_EXPECT_REACTION(test, ctx)                             \
@@ -37,5 +45,13 @@ struct rv_kunit_ctx {
 	     !__done;                                                        \
 	     __done = ({ RV_KUNIT_EXPECT_REACTION(test, ctx); 1; }))
 
+#define rv_mock_current(ctx, task) (ctx->curr = task)
+#define rv_mock_cpu(ctx, cpu) (ctx->cpu = cpu)
+
+#else /* !CONFIG_RV_MONITORS_KUNIT_TEST */
+
+#define rv_get_current() current
+#define rv_current_cpu() smp_processor_id()
+
 #endif /* CONFIG_RV_MONITORS_KUNIT_TEST */
 #endif /* _RV_KUNIT_H */
include/rv/ltl_monitor.h +1 -0
index eff60cd61106..35bc870d808a 100644
@@ -9,6 +9,7 @@
 #include <linux/stringify.h>
 #include <linux/seq_buf.h>
 #include <rv/instrumentation.h>
+#include <rv/kunit.h>
 #include <trace/events/task.h>
 #include <trace/events/sched.h>
 
kernel/trace/rv/Kconfig +3 -0
index d7dba4453bd3..702349e1ddd4 100644
@@ -121,4 +121,7 @@ config RV_MONITORS_KUNIT_TEST
 	  These tests verify that monitors correctly detect violations by
 	  triggering fake events and validating the expected reactions.
 
+	  Enabling this may slightly increase overhead of some monitors even
+	  when the KUnit test is not running.
+
 	  If unsure, say N.
kernel/trace/rv/monitors/pagefault/pagefault.c +1 -1
index 9fe6123b2200..56abe5079676 100644
@@ -38,7 +38,7 @@ static void ltl_atoms_init(struct task_struct *task, struct ltl_monitor *mon, bo
 static void handle_page_fault(void *data, unsigned long address, struct pt_regs *regs,
 			      unsigned long error_code)
 {
-	ltl_atom_pulse(current, LTL_PAGEFAULT, true);
+	ltl_atom_pulse(rv_get_current(), LTL_PAGEFAULT, true);
 }
 
 static int enable_pagefault(void)
kernel/trace/rv/monitors/sleep/sleep.c +12 -12
index 8dfe5ec13e19..8b44161d47d3 100644
@@ -102,7 +102,7 @@ static void handle_sched_waking(void *data, struct task_struct *task)
 	if (this_cpu_read(hardirq_context)) {
 		ltl_atom_pulse(task, LTL_WOKEN_BY_HARDIRQ, true);
 	} else if (in_task()) {
-		if (current->prio <= task->prio)
+		if (rv_get_current()->prio <= task->prio)
 			ltl_atom_pulse(task, LTL_WOKEN_BY_EQUAL_OR_HIGHER_PRIO, true);
 	} else if (in_nmi()) {
 		ltl_atom_pulse(task, LTL_WOKEN_BY_NMI, true);
@@ -112,12 +112,12 @@ static void handle_sched_waking(void *data, struct task_struct *task)
 static void handle_contention_begin(void *data, void *lock, unsigned int flags)
 {
 	if (flags & LCB_F_RT)
-		ltl_atom_update(current, LTL_BLOCK_ON_RT_MUTEX, true);
+		ltl_atom_update(rv_get_current(), LTL_BLOCK_ON_RT_MUTEX, true);
 }
 
 static void handle_contention_end(void *data, void *lock, int ret)
 {
-	ltl_atom_update(current, LTL_BLOCK_ON_RT_MUTEX, false);
+	ltl_atom_update(rv_get_current(), LTL_BLOCK_ON_RT_MUTEX, false);
 }
 
 static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
@@ -126,7 +126,7 @@ static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
 	unsigned long args[6];
 	int op, cmd;
 
-	mon = ltl_get_monitor(current);
+	mon = ltl_get_monitor(rv_get_current());
 
 	switch (id) {
 #ifdef __NR_clock_nanosleep
@@ -135,11 +135,11 @@ static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
 #ifdef __NR_clock_nanosleep_time64
 	case __NR_clock_nanosleep_time64:
 #endif
-		syscall_get_arguments(current, regs, args);
+		syscall_get_arguments(rv_get_current(), regs, args);
 		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, args[0] == CLOCK_MONOTONIC);
 		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, args[0] == CLOCK_TAI);
 		ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, args[1] == TIMER_ABSTIME);
-		ltl_atom_update(current, LTL_CLOCK_NANOSLEEP, true);
+		ltl_atom_update(rv_get_current(), LTL_CLOCK_NANOSLEEP, true);
 		break;
 
 #ifdef __NR_futex
@@ -148,25 +148,25 @@ static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
 #ifdef __NR_futex_time64
 	case __NR_futex_time64:
 #endif
-		syscall_get_arguments(current, regs, args);
+		syscall_get_arguments(rv_get_current(), regs, args);
 		op = args[1];
 		cmd = op & FUTEX_CMD_MASK;
 
 		switch (cmd) {
 		case FUTEX_LOCK_PI:
 		case FUTEX_LOCK_PI2:
-			ltl_atom_update(current, LTL_FUTEX_LOCK_PI, true);
+			ltl_atom_update(rv_get_current(), LTL_FUTEX_LOCK_PI, true);
 			break;
 		case FUTEX_WAIT:
 		case FUTEX_WAIT_BITSET:
 		case FUTEX_WAIT_REQUEUE_PI:
-			ltl_atom_update(current, LTL_FUTEX_WAIT, true);
+			ltl_atom_update(rv_get_current(), LTL_FUTEX_WAIT, true);
 			break;
 		}
 		break;
 #ifdef __NR_epoll_wait
 	case __NR_epoll_wait:
-		ltl_atom_update(current, LTL_EPOLL_WAIT, true);
+		ltl_atom_update(rv_get_current(), LTL_EPOLL_WAIT, true);
 		break;
 #endif
 	}
@@ -174,7 +174,7 @@ static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
 
 static void handle_sys_exit(void *data, struct pt_regs *regs, long ret)
 {
-	struct ltl_monitor *mon = ltl_get_monitor(current);
+	struct ltl_monitor *mon = ltl_get_monitor(rv_get_current());
 
 	ltl_atom_set(mon, LTL_FUTEX_LOCK_PI, false);
 	ltl_atom_set(mon, LTL_FUTEX_WAIT, false);
@@ -182,7 +182,7 @@ static void handle_sys_exit(void *data, struct pt_regs *regs, long ret)
 	ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, false);
 	ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, false);
 	ltl_atom_set(mon, LTL_EPOLL_WAIT, false);
-	ltl_atom_update(current, LTL_CLOCK_NANOSLEEP, false);
+	ltl_atom_update(rv_get_current(), LTL_CLOCK_NANOSLEEP, false);
 }
 
 static void handle_kthread_stop(void *data, struct task_struct *task)
kernel/trace/rv/rv_monitors_test.c +26 -0
index 5a12a109c1ed..3dbe562f00c1 100644
@@ -31,6 +31,30 @@ static void stub_rv_put_task_monitor_slot(int slot)
 {
 }
 
+struct task_struct *rv_get_current(void)
+{
+	KUNIT_STATIC_STUB_REDIRECT(rv_get_current);
+	return current;
+}
+int rv_current_cpu(void)
+{
+	KUNIT_STATIC_STUB_REDIRECT(rv_current_cpu);
+	return smp_processor_id();
+}
+
+static struct task_struct *stub_rv_get_current(void)
+{
+	struct rv_kunit_ctx *ctx = kunit_get_current_test()->priv;
+
+	return ctx->curr ?: current;
+}
+static int stub_rv_current_cpu(void)
+{
+	struct rv_kunit_ctx *ctx = kunit_get_current_test()->priv;
+
+	return ctx->cpu;
+}
+
 static int rv_mon_test_init(struct kunit *test)
 {
 	struct rv_kunit_ctx *ctx;
@@ -49,6 +73,8 @@ static int rv_mon_test_init(struct kunit *test)
 				   stub_rv_get_task_monitor_slot);
 	kunit_activate_static_stub(test, rv_put_task_monitor_slot,
 				   stub_rv_put_task_monitor_slot);
+	kunit_activate_static_stub(test, rv_get_current, stub_rv_get_current);
+	kunit_activate_static_stub(test, rv_current_cpu, stub_rv_current_cpu);
 
 	return 0;
 }
Signature · 1 line
2.54.0
Reviewed-by on v2 12/14
#27 lore raw

Re: [PATCH v2 12/14] rv: Add KUnit stubs for current and smp_processor_id()

Quoted 9 lines · Gabriele Monaco

Some monitors do not only rely on tracepoint arguments but also on the currently executing task and current processor.
This makes it more challenging to mock events in KUnit.

Define wrapper functions around current and smp_processor_id(), the functionality is stubbed only during KUnit, however the additional function call is necessary whenever the KUnit tests are built in.

author maintainer
#28 lore raw

PATCH v2 13/14 Prevent unintentional tracepoints during KUnit tests

In reply to Gabriele Monaco

Monitor initialisation also called during KUnit tests may register some tracepoints, this can lead to issues since we don't expect real monitor events running during KUnit tests.

Prevent tracepoint registration if an RV KUnit test is running.

---
 include/rv/instrumentation.h       |  5 +++++
 include/rv/kunit.h                 |  2 ++
 kernel/trace/rv/rv_monitors_test.c | 10 ++++++++++
 3 files changed, 17 insertions(+)
include/rv/instrumentation.h +5 -0
index d4e7a02ede1a..761f8f147dac 100644
@@ -9,12 +9,15 @@
  */
 
 #include <linux/ftrace.h>
+#include <rv/kunit.h>
 
 /*
  * rv_attach_trace_probe - check and attach a handler function to a tracepoint
  */
 #define rv_attach_trace_probe(monitor, tp, rv_handler)					\
 	do {										\
+		if (rv_mon_test_is_running())						\
+			break;								\
 		check_trace_callback_type_##tp(rv_handler);				\
 		WARN_ONCE(register_trace_##tp(rv_handler, NULL),			\
 				"fail attaching " #monitor " " #tp "handler");		\
@@ -25,5 +28,7 @@
  */
 #define rv_detach_trace_probe(monitor, tp, rv_handler)					\
 	do {										\
+		if (rv_mon_test_is_running())						\
+			break;								\
 		unregister_trace_##tp(rv_handler, NULL);				\
 	} while (0)
include/rv/kunit.h +2 -0
index 3ef83d337880..9daaebacfc7e 100644
@@ -19,6 +19,7 @@
 
 struct task_struct *rv_get_current(void);
 int rv_current_cpu(void);
+bool rv_mon_test_is_running(void);
 
 struct rv_kunit_ctx {
 	int reactions, expected;
@@ -52,6 +53,7 @@ struct rv_kunit_ctx {
 
 #define rv_get_current() current
 #define rv_current_cpu() smp_processor_id()
+#define rv_mon_test_is_running() false
 
 #endif /* CONFIG_RV_MONITORS_KUNIT_TEST */
 #endif /* _RV_KUNIT_H */
kernel/trace/rv/rv_monitors_test.c +10 -0
index 3dbe562f00c1..01cbee9ac6c0 100644
@@ -14,6 +14,8 @@
 #include <linux/rv.h>
 #include "rv.h"
 
+static bool rv_mon_test_running;
+
 __printf(2, 3)
 static void stub_rv_react(struct rv_monitor *monitor, const char *msg, ...)
 {
@@ -55,6 +57,11 @@ static int stub_rv_current_cpu(void)
 	return ctx->cpu;
 }
 
+bool rv_mon_test_is_running(void)
+{
+	return rv_mon_test_running;
+}
+
 static int rv_mon_test_init(struct kunit *test)
 {
 	struct rv_kunit_ctx *ctx;
@@ -94,6 +101,8 @@ static int rv_set_testing(struct kunit_suite *suite)
 
 	mutex_lock(&rv_interface_lock);
 
+	rv_mon_test_running = true;
+
 	list_for_each_entry(mon, &rv_monitors_list, list) {
 		if (mon->enabled) {
 			mutex_unlock(&rv_interface_lock);
@@ -111,6 +120,7 @@ static int rv_set_testing(struct kunit_suite *suite)
  */
 static void rv_clear_testing(struct kunit_suite *suite)
 {
+	rv_mon_test_running = false;
 	mutex_unlock(&rv_interface_lock);
 }
 
Signature · 1 line
2.54.0
Reviewed-by on v2 13/14
#29 lore raw

Re: [PATCH v2 13/14] rv: Prevent unintentional tracepoints during KUnit tests

Quoted 7 lines · Gabriele Monaco

Monitor initialisation also called during KUnit tests may register some tracepoints, this can lead to issues since we don't expect real monitor events running during KUnit tests.

Prevent tracepoint registration if an RV KUnit test is running.

author maintainer
#30 lore raw

PATCH v2 14/14 Add KUnit tests for some LTL monitors

In reply to Gabriele Monaco

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.

---
 include/rv/ltl_monitor.h                      | 37 +++++++++++++
 .../trace/rv/monitors/pagefault/pagefault.c   | 25 +++++++++
 kernel/trace/rv/monitors/sleep/sleep.c        | 52 +++++++++++++++++++
 kernel/trace/rv/rv_monitors_test.c            |  4 ++
 4 files changed, 118 insertions(+)
include/rv/ltl_monitor.h +37 -0
index 35bc870d808a..75c9c6230dae 100644
@@ -172,3 +172,40 @@ 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
+
+/*
+ * ltl_teardown_test - Disable the monitor for a kunit test
+ */
+static inline void ltl_teardown_test(void *arg)
+{
+	struct rv_monitor *rv_this = arg;
+	struct kunit *test = kunit_get_current_test();
+
+	if (test) {
+		struct rv_kunit_ctx *ctx = test->priv;
+
+		RV_KUNIT_EXPECT_NO_REACTION(test, ctx);
+	}
+
+	rv_this->enabled = 0;
+	ltl_monitor_destroy();
+}
+
+/*
+ * ltl_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 */
kernel/trace/rv/monitors/pagefault/pagefault.c +25 -0
index 56abe5079676..28c3382eabb0 100644
@@ -86,3 +86,28 @@ module_exit(unregister_pagefault);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Nam Cao <[email protected]>");
 MODULE_DESCRIPTION("pagefault: Monitor that RT tasks do not raise page faults");
+
+#ifdef CONFIG_RV_MONITORS_KUNIT_TEST
+void rv_test_pagefault(struct kunit *test);
+
+void rv_test_pagefault(struct kunit *test)
+{
+	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);
+	KUNIT_ASSERT_NOT_NULL(test, target);
+	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);
+	RV_KUNIT_EXPECT_REACTION_HERE(test, ctx)
+		handle_page_fault(NULL, 0, NULL, 0);
+}
+EXPORT_SYMBOL_GPL(rv_test_pagefault);
+#endif
kernel/trace/rv/monitors/sleep/sleep.c +52 -0

55 lines not on this page · raw message

kernel/trace/rv/rv_monitors_test.c +4 -0
index 01cbee9ac6c0..d3e3aa1ac4ec 100644
@@ -137,6 +137,8 @@ DECLARE_RV_TEST(rv_test_sssw);
 DECLARE_RV_TEST(rv_test_sts);
 DECLARE_RV_TEST(rv_test_opid);
 DECLARE_RV_TEST(rv_test_nomiss);
+DECLARE_RV_TEST(rv_test_pagefault);
+DECLARE_RV_TEST(rv_test_sleep);
 
 static struct kunit_case rv_mon_test_cases[] = {
 	KUNIT_CASE(rv_test_sco),
@@ -144,6 +146,8 @@ 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_pagefault),
+	KUNIT_CASE(rv_test_sleep),
 	{}
 };
 
Signature · 1 line
2.54.0
Reviewed-by on v2 14/14
#31 lore raw

Re: [PATCH v2 14/14] rv: Add KUnit tests for some LTL monitors

Quoted 5 lines · Gabriele Monaco

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.

#32 lore raw
In reply to Gabriele Monaco
Quoted 4 lines · Gabriele Monaco

This series adds support to the make check target in the rv userspace tool and the rvgen script, this allows to quickly validate its functionality. The selftest framework is inspired by the one used in RTLA.

Just a quick word of appreciation for this work. I use this to aid my own development, it helps a lot.

Thanks,
Nam