From: Wen Yang <wen.yang@linux.dev>
To: Gabriele Monaco <gmonaco@redhat.com>,
linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org,
Steven Rostedt <rostedt@goodmis.org>,
Masami Hiramatsu <mhiramat@kernel.org>
Cc: Nam Cao <namcao@linutronix.de>,
Thomas Weissschuh <thomas.weissschuh@linutronix.de>,
Tomas Glozar <tglozar@redhat.com>, John Kacur <jkacur@redhat.com>
Subject: Re: [PATCH v5 13/17] rv: Add KUnit mock for current
Date: Sun, 2 Aug 2026 12:55:52 +0800 [thread overview]
Message-ID: <e0fe1e85-c885-4ce6-b250-1b2028112f26@linux.dev> (raw)
In-Reply-To: <0CC2C4B0-6E6A-4199-A7FA-EEDEB28866B6@redhat.com>
On 7/30/26 13:13, Gabriele Monaco wrote:
>
>
> Il 29 luglio 2026 18:17:31 UTC, Wen Yang <wen.yang@linux.dev> ha scritto:
>
>>> +#define rv_get_current() (unlikely(kunit_get_current_test()) ? rv_get_mock_current() : current)
>
> ...
>
>>> +/*
>>> + * rv_get_mock_current() is called only if we are running from a KUnit test.
>>> + * This can occur from a legitimate RV test or any unrelated test running when
>>> + * a real RV monitor is active and triggering events.
>>> + * We assume the former case is the only one where mock_current is not NULL and
>>> + * can occur only sequentially (KUnit doesn't run tests in parallel).
>>> + * We cannot rely on the test's context because there is no way to safely
>>> + * understand from which test we are running and KUnit utilities require
>>> + * locking, which is unsafe from NMI or scheduling context.
>>> + * Note that it is not possible for a real RV monitor to run when the RV KUnit
>>> + * tests are running (see rv_set_testing()).
>>> + */
>>> +static struct task_struct *mock_current;
>>> +
>>> +void rv_mock_current(struct task_struct *tsk)
>>> +{
>>> + mock_current = tsk;
>>> +}
>>> +EXPORT_SYMBOL_IF_KUNIT(rv_mock_current);
>>> +
>>> +struct task_struct *rv_get_mock_current(void)
>>> +{
>>> + return mock_current ?: current;
>>> +}
>>> +EXPORT_SYMBOL_GPL(rv_get_mock_current);
>>> #endif
>>
>> rv_mock_current() uses EXPORT_SYMBOL_IF_KUNIT, but rv_get_mock_current() uses EXPORT_SYMBOL_GPL. Both are defined inside the same CONFIG_RV_MONITORS_KUNIT_TEST block, so rv_get_mock_current should use EXPORT_SYMBOL_IF_KUNIT as well, otherwise it leaks a test-only symbol into production builds.
>>
>> With that fixed:
>> Reviewed-by: Wen Yang <wen.yang@linux.dev>
>
> Thanks for the review.
> This was intentional however: rv_get_current() can be called by any monitor, those don't have to be KUnit.
> Since rv_get_current() is a macro also calling rv_get_mock_current() we need to be able to link that too.
>
> The idea is that a "real" (non-kunit) monitor handler could be run when interrupting a KUnit test (not an RV one, we make sure of that). In that case we do call rv_get_mock_current() and return current after the function call.
>
> rv_mock_current() CANNOT be called outside of the RV KUnit test cases, it uses a global variable (for problems I tried to explain in the comment), so should be exported only to KUnit and called directly from the test case.
>
> Does it make sense to you?
>
Thanks for the explanation.
That does make sense.
One small thought though -- the kernel already provides
KUNIT_STATIC_STUB_REDIRECT as the idiomatic way to handle this kind of
pattern. It allows a production function to be transparently redirected
to a stub during KUnit tests, with zero overhead when no test is running
(since it uses the same static_branch_unlikely(&kunit_running) path as
kunit_get_current_test()).
This might be a cleaner fit here, since it avoids exporting
rv_mock_current() beyond KUnit and keeps the redirection logic internal
to the test harness.
Just wanted to throw that out there -- what do you think?
diff --git a/kernel/trace/rv/rv_monitors_test.c
b/kernel/trace/rv/rv_monitors_test.c
index 8026176eee90..f4e3e2e4a6d4 100644
--- a/kernel/trace/rv/rv_monitors_test.c
+++ b/kernel/trace/rv/rv_monitors_test.c
...
+struct task_struct *rv_current(void)
+{
+ KUNIT_STATIC_STUB_REDIRECT(rv_current);
+ return current;
+}
+EXPORT_SYMBOL_GPL(rv_current);
...
diff --git a/kernel/trace/rv/rv_monitors_test.c
b/kernel/trace/rv/rv_monitors_test.c
index 8026176eee90..f4e3e2e4a6d4 100644
--- a/kernel/trace/rv/rv_monitors_test.c
+++ b/kernel/trace/rv/rv_monitors_test.c
...
+static struct task_struct *mock_current_task;
+
+static struct task_struct *rv_mock_current_fn(void)
+{
+ return mock_current_task ?: current;
+}
+
+void rv_mock_current(struct kunit *test, struct task_struct *tsk)
+{
+ mock_current_task = tsk;
+ if (tsk)
+ kunit_activate_static_stub(test, rv_current,
rv_mock_current_fn);
+ else
+ kunit_deactivate_static_stub(test, rv_current);
+}
+EXPORT_SYMBOL_IF_KUNIT(rv_mock_current);
diff --git a/kernel/trace/rv/monitors/pagefault/pagefault_kunit.c
b/kernel/trace/rv/monitors/pagefault/pagefault_kunit.c
index unchanged..unchanged 100644
--- a/kernel/trace/rv/monitors/pagefault/pagefault_kunit.c
+++ b/kernel/trace/rv/monitors/pagefault/pagefault_kunit.c
@@ -nn,7 +nn,7 @@ static void rv_test_pagefault(struct kunit *test)
rv_pagefault_ops.handle_task_newtask(NULL, target, 0);
- rv_mock_current(target);
+ rv_mock_current(test, target);
--
Best wishes,
Wen
next prev parent reply other threads:[~2026-08-02 4:56 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-23 7:45 [PATCH v5 00/17] rv: Add selftests to tools and KUnit tests Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 01/17] rv: Use generic rv_this for the rv_monitor variable in LTL Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 02/17] tools/rv: Fix exit status when monitor execution fails Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 03/17] verification/rvgen: Improve rv_dir discovery in RVGenerator Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 04/17] verification/rvgen: Use pathlib instead of os.path Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 05/17] verification/rvgen: Improve consistency in template files Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 06/17] tools/rv: Add selftests Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 07/17] verification/rvgen: Add golden and spec folders for tests Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 08/17] verification/rvgen: Add selftests Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 09/17] verification/rvgen: Add the rvgen kunit subcommand Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 10/17] verification/rvgen: Add selftests for rvgen kunit Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 11/17] rv: Export task monitor slot and react symbols Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 12/17] rv: Add KUnit tests for some DA/HA monitors Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 13/17] rv: Add KUnit mock for current Gabriele Monaco
2026-07-29 18:17 ` Wen Yang
2026-07-30 5:13 ` Gabriele Monaco
2026-08-02 4:55 ` Wen Yang [this message]
2026-08-02 5:26 ` Wen Yang
2026-07-31 10:59 ` Nam Cao
2026-07-23 7:45 ` [PATCH v5 14/17] rv: Add KUnit tests for some LTL monitors Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 15/17] selftests/verification: Fix wrong errexit assumption Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 16/17] selftests/verification: Rearrange the wwnr_printk test Gabriele Monaco
2026-07-23 7:45 ` [PATCH v5 17/17] selftests/verification: Add selftests for deadline and stall monitors Gabriele Monaco
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=e0fe1e85-c885-4ce6-b250-1b2028112f26@linux.dev \
--to=wen.yang@linux.dev \
--cc=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 \
/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
Powered by JetHome