Thread (21 messages) read the whole thread 21 messages, 3 authors, 1d ago

Re: [PATCH v5 13/17] rv: Add KUnit mock for current

From: Gabriele Monaco <gmonaco@redhat.com>
Date: 2026-07-30 05:13:30
Also in: lkml


Il 29 luglio 2026 18:17:31 UTC, Wen Yang [off-list ref] ha scritto:
quoted
+#define rv_get_current() (unlikely(kunit_get_current_test()) ? rv_get_mock_current() : current)
 
...
quoted
+/*
+ * 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 <redacted>
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,
Gabriele
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help