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.

Signed-off-by: Gabriele Monaco <[email protected]>
---
 include/rv/instrumentation.h       |  5 +++++
 include/rv/kunit.h                 |  2 ++
 kernel/trace/rv/rv_monitors_test.c | 10 ++++++++++
 3 files changed, 17 insertions(+)

diff --git a/include/rv/instrumentation.h b/include/rv/instrumentation.h
index d4e7a02ede1a..761f8f147dac 100644
--- a/include/rv/instrumentation.h
+++ b/include/rv/instrumentation.h
@@ -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)
diff --git a/include/rv/kunit.h b/include/rv/kunit.h
index 3ef83d337880..9daaebacfc7e 100644
--- a/include/rv/kunit.h
+++ b/include/rv/kunit.h
@@ -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 */
diff --git a/kernel/trace/rv/rv_monitors_test.c 
b/kernel/trace/rv/rv_monitors_test.c
index 3dbe562f00c1..01cbee9ac6c0 100644
--- a/kernel/trace/rv/rv_monitors_test.c
+++ b/kernel/trace/rv/rv_monitors_test.c
@@ -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);
 }
 
-- 
2.54.0


Reply via email to