From: Wen Yang <[email protected]>

rv_react() overrides the lockdep wait type to LD_WAIT_FREE to enforce
that reactor callbacks take no locks. But callbacks run in the context
of the triggering tracepoint, which can be preemptible task context on
any kernel. A timer interrupt firing during the callback makes the
interrupt-exit path schedule and take rq->__lock (LD_WAIT_SPIN) while
the LD_WAIT_FREE override is still held, producing a spurious
"Invalid wait context" warning:

    [ BUG: Invalid wait context ]
    context-{5:5}
    1 lock held by kunit_try_catch/209:
     #0: (rv_react_map-wait-type-override){+.+.}-{1:1}
    kunit_try_catch/209 is trying to lock:
    ffff8a743ed3e8a0 (&rq->__lock){-...}-{2:2}

Use LD_WAIT_SPIN instead of LD_WAIT_FREE, which causes false-positive
warnings in preemptible contexts due to scheduler preemption taking
rq->__lock. Add documentation to runtime-verification.rst.

Fixes: 69d8895cb9a9 ("rv: Add explicit lockdep context for reactors")
Reviewed-by: Gabriele Monaco <[email protected]>
Signed-off-by: Wen Yang <[email protected]>
Cc: Thomas Weißschuh <[email protected]>
---
 Documentation/trace/rv/monitor_synthesis.rst | 20 ++++++++++++++++++++
 kernel/trace/rv/rv_reactors.c                |  6 +++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/Documentation/trace/rv/monitor_synthesis.rst 
b/Documentation/trace/rv/monitor_synthesis.rst
index 2c1b5a0ae154..aab4b0342d5f 100644
--- a/Documentation/trace/rv/monitor_synthesis.rst
+++ b/Documentation/trace/rv/monitor_synthesis.rst
@@ -365,6 +365,26 @@ but higher overhead. The timer wheel (``HA_TIMER_WHEEL``) 
is a good alternative
 for monitors with several instances (e.g. per-task) that achieves lower
 overhead with increased latency, yet without compromising precision.
 
+Reactors
+--------
+
+A reactor is a callback triggered by a monitor when a violation is
+detected. Reactors are registered via ``/sys/kernel/tracing/rv/reactors/``
+and enabled per monitor.
+
+Reactor Locking Rules
++++++++++++++++++++++
+
+A reactor callback may be invoked from various contexts (process,
+softirq, hardirq, NMI) depending on the tracepoint to which its
+monitor is attached.
+
+Lockdep uses a fixed wait type: ``LD_WAIT_SPIN``. This allows
+``raw_spinlock_t`` but disallows sleepable locks. ``LD_WAIT_FREE`` is
+not viable in preemptible contexts because scheduler preemption takes
+``rq->__lock`` (``LD_WAIT_SPIN``), which would cause false-positive
+warnings.
+
 Final remarks
 -------------
 
diff --git a/kernel/trace/rv/rv_reactors.c b/kernel/trace/rv/rv_reactors.c
index 2f5fc8d18dea..afc97d097109 100644
--- a/kernel/trace/rv/rv_reactors.c
+++ b/kernel/trace/rv/rv_reactors.c
@@ -465,7 +465,11 @@ int init_rv_reactors(struct dentry *root_dir)
 
 void rv_react(struct rv_monitor *monitor, const char *msg, ...)
 {
-       static DEFINE_WAIT_OVERRIDE_MAP(rv_react_map, LD_WAIT_FREE);
+       /*
+        * Use LD_WAIT_SPIN uniformly for deterministic lockdep checking.
+        * See Documentation/trace/rv/runtime-verification.rst.
+        */
+       static DEFINE_WAIT_OVERRIDE_MAP(rv_react_map, LD_WAIT_SPIN);
        va_list args;
 
        if (!rv_reacting_on() || !monitor->react)
-- 
2.25.1


Reply via email to