From: Gabriele Monaco <gmonaco@redhat.com>
To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
Nam Cao <namcao@linutronix.de>
Cc: Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>,
Thomas Weissschuh <thomas.weissschuh@linutronix.de>,
Tomas Glozar <tglozar@redhat.com>, John Kacur <jkacur@redhat.com>,
Wen Yang <wen.yang@linux.dev>
Subject: Re: [PATCH v4 13/17] rv: Add KUnit stub for current
Date: Wed, 22 Jul 2026 13:37:48 +0200 [thread overview]
Message-ID: <2280c5a84ce419bf9f96b79552ad47775dc8997e.camel@redhat.com> (raw)
In-Reply-To: <20260717154638.220789-14-gmonaco@redhat.com>
On Fri, 2026-07-17 at 17:46 +0200, Gabriele Monaco wrote:
> Some monitors do not only rely on tracepoint arguments but also on the
> currently executing task.
> This makes it more challenging to mock events in KUnit.
>
> Define wrapper functions around current, the functionality is stubbed
> only during KUnit, however the additional function call is necessary
> whenever the KUnit tests are built in.
>
> Reviewed-by: Nam Cao <namcao@linutronix.de>
> Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
As reported by sashiko, using KUnit's standard stubbing can be dangerous. In the
rare case of an NMI causing an RV event (e.g. a pagefault) when an unrelated
KUnit test is running, the stub would end up using locks and causing issues.
Additionally, it isn't ideal to downgrade to a function call directly if the
tests are built, which is for instance the default on the stock Fedora kernel
(CONFIG_KUNIT_ALL_TESTS=m).
I'm soon going to send a V5 addressing both: no stub and function call only if
KUnit is really running.
I'll reset the review since the patch essentially changes.
> ---
> include/rv/da_monitor.h | 1 +
> include/rv/kunit.h | 13 +++++++++-
> 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.c | 8 +++++++
> kernel/trace/rv/rv_monitors_test.c | 9 +++++++
> 8 files changed, 47 insertions(+), 14 deletions(-)
>
> diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
> index 773564720ba1..9f7ba443d777 100644
> --- a/include/rv/da_monitor.h
> +++ b/include/rv/da_monitor.h
> @@ -16,6 +16,7 @@
>
> #include <rv/automata.h>
> #include <linux/rv.h>
> +#include <rv/kunit.h>
> #include <linux/stringify.h>
> #include <linux/bug.h>
> #include <linux/sched.h>
> diff --git a/include/rv/kunit.h b/include/rv/kunit.h
> index ff98b5137285..6d16a422a80c 100644
> --- a/include/rv/kunit.h
> +++ b/include/rv/kunit.h
> @@ -2,7 +2,10 @@
> /*
> * Copyright (C) 2026-2029 Red Hat, Inc. Gabriele Monaco <gmonaco@redhat.com>
> *
> - * Declaration of utilities to run KUnit tests.
> + * Declaration of wrappers to allow stubbing core functionality, like
> current,
> + * 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
> @@ -16,6 +19,7 @@
>
> int rv_set_testing(struct kunit_suite *suite);
> void rv_clear_testing(struct kunit_suite *suite);
> +struct task_struct *rv_get_current(void);
>
> #define RV_KUNIT_MAX_MOCK_TASKS 8
>
> @@ -23,6 +27,7 @@ struct rv_kunit_ctx {
> int reactions, expected;
> int mock_task_count;
> struct task_struct *mock_tasks[RV_KUNIT_MAX_MOCK_TASKS];
> + struct task_struct *curr;
> };
>
> #define RV_KUNIT_EXPECT_REACTION(test, ctx) \
> @@ -57,5 +62,11 @@ void prepare_test(struct kunit *test, const struct
> rv_kunit_mon *mon);
> void teardown_test(void *arg);
> struct task_struct *rv_kunit_alloc_mock_task(struct kunit *test);
>
> +#define rv_mock_current(ctx, task) (ctx->curr = task)
> +
> +#else /* !CONFIG_RV_MONITORS_KUNIT_TEST */
> +
> +#define rv_get_current() current
> +
> #endif /* CONFIG_RV_MONITORS_KUNIT_TEST */
> #endif /* _RV_KUNIT_H */
> diff --git a/include/rv/ltl_monitor.h b/include/rv/ltl_monitor.h
> index 56e83edcf0c4..d7dc01db4dd9 100644
> --- a/include/rv/ltl_monitor.h
> +++ b/include/rv/ltl_monitor.h
> @@ -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>
>
> diff --git a/kernel/trace/rv/Kconfig b/kernel/trace/rv/Kconfig
> index 34c1feb35a9b..7bae9723cdbf 100644
> --- a/kernel/trace/rv/Kconfig
> +++ b/kernel/trace/rv/Kconfig
> @@ -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.
> diff --git a/kernel/trace/rv/monitors/pagefault/pagefault.c
> b/kernel/trace/rv/monitors/pagefault/pagefault.c
> index 5e1a2a606783..e52500fd2de0 100644
> --- a/kernel/trace/rv/monitors/pagefault/pagefault.c
> +++ b/kernel/trace/rv/monitors/pagefault/pagefault.c
> @@ -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)
> diff --git a/kernel/trace/rv/monitors/sleep/sleep.c
> b/kernel/trace/rv/monitors/sleep/sleep.c
> index 12328ce663f5..71d2005ce520 100644
> --- a/kernel/trace/rv/monitors/sleep/sleep.c
> +++ b/kernel/trace/rv/monitors/sleep/sleep.c
> @@ -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)
> diff --git a/kernel/trace/rv/rv.c b/kernel/trace/rv/rv.c
> index cfe950fef3b4..edb10812f296 100644
> --- a/kernel/trace/rv/rv.c
> +++ b/kernel/trace/rv/rv.c
> @@ -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
> @@ -893,4 +894,11 @@ void rv_clear_testing(struct kunit_suite *suite)
> mutex_unlock(&rv_interface_lock);
> }
> EXPORT_SYMBOL_IF_KUNIT(rv_clear_testing);
> +
> +struct task_struct *rv_get_current(void)
> +{
> + KUNIT_STATIC_STUB_REDIRECT(rv_get_current);
> + return current;
> +}
> +EXPORT_SYMBOL_GPL(rv_get_current);
> #endif
> diff --git a/kernel/trace/rv/rv_monitors_test.c
> b/kernel/trace/rv/rv_monitors_test.c
> index 2145c85d4c9a..2108973383b2 100644
> --- a/kernel/trace/rv/rv_monitors_test.c
> +++ b/kernel/trace/rv/rv_monitors_test.c
> @@ -106,6 +106,13 @@ struct task_struct *rv_kunit_alloc_mock_task(struct kunit
> *test)
> return tsk;
> }
>
> +static struct task_struct *stub_rv_get_current(void)
> +{
> + if (active_ctx && active_ctx->curr)
> + return active_ctx->curr;
> + return current;
> +}
> +
> static int rv_mon_test_init(struct kunit *test)
> {
> struct rv_kunit_ctx *ctx;
> @@ -115,6 +122,8 @@ static int rv_mon_test_init(struct kunit *test)
>
> test->priv = ctx;
>
> + kunit_activate_static_stub(test, rv_get_current,
> stub_rv_get_current);
> +
> return 0;
> }
>
next prev parent reply other threads:[~2026-07-22 11:37 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-17 15:46 [PATCH v4 00/17] rv: Add selftests to tools and KUnit tests Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 01/17] rv: Use generic rv_this for the rv_monitor variable in LTL Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 02/17] tools/rv: Fix exit status when monitor execution fails Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 03/17] verification/rvgen: Improve rv_dir discovery in RVGenerator Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 04/17] verification/rvgen: Use pathlib instead of os.path Gabriele Monaco
2026-07-20 7:48 ` Nam Cao
2026-07-17 15:46 ` [PATCH v4 05/17] verification/rvgen: Improve consistency in template files Gabriele Monaco
2026-07-20 7:49 ` Nam Cao
2026-07-17 15:46 ` [PATCH v4 06/17] tools/rv: Add selftests Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 07/17] verification/rvgen: Add golden and spec folders for tests Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 08/17] verification/rvgen: Add selftests Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 09/17] verification/rvgen: Add the rvgen kunit subcommand Gabriele Monaco
2026-07-20 10:54 ` Nam Cao
2026-07-17 15:46 ` [PATCH v4 10/17] verification/rvgen: Add selftests for rvgen kunit Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 11/17] rv: Export task monitor slot and react symbols Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 12/17] rv: Add KUnit tests for some DA/HA monitors Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 13/17] rv: Add KUnit stub for current Gabriele Monaco
2026-07-22 11:37 ` Gabriele Monaco [this message]
2026-07-17 15:46 ` [PATCH v4 14/17] rv: Add KUnit tests for some LTL monitors Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 15/17] selftests/verification: Fix wrong errexit assumption Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 16/17] selftests/verification: Rearrange the wwnr_printk test Gabriele Monaco
2026-07-20 11:01 ` Nam Cao
2026-07-21 6:24 ` Gabriele Monaco
2026-07-17 15:46 ` [PATCH v4 17/17] selftests/verification: Add selftests for deadline and stall monitors Gabriele Monaco
2026-07-20 11:03 ` Nam Cao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2280c5a84ce419bf9f96b79552ad47775dc8997e.camel@redhat.com \
--to=gmonaco@redhat.com \
--cc=jkacur@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-trace-kernel@vger.kernel.org \
--cc=mhiramat@kernel.org \
--cc=namcao@linutronix.de \
--cc=rostedt@goodmis.org \
--cc=tglozar@redhat.com \
--cc=thomas.weissschuh@linutronix.de \
--cc=wen.yang@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox