Skip to contentSkip to conversation

runtime verification monitors for Dovetail

RFC PATCH In review Tobias Schaffner 2 participants 9 messages Nov 27 – Nov 28

Subsystems (5)
Documentation Tracing Scheduler IRQ +1 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover runtime verification monitors for Dovetail

Hi all,

with this RFC I would like to propose using the Linux kernel Runtime Verification (RV) system [1] for runtime verification of Dovetail based cokernels.

Running a system with RV monitors active should ensure that the system behaves as described in the model. As this is checked at runtime we can verify if the system is even behaving as inteded when it is running the real workload.

My ultimate goal is not only to use it for verification but also for wakeup latency tracing and decomposition in EVL by describing the whole wakeup path. I am working on upstreaming dwell time measurements in the RV system in parallel for that [2].

This would allow us not only to get a worst case latency and decomposition for a latency evaluation tool like latmus but to get a worst case latency for any load applied to the system (e.g. the real application of the user).

This patch set proposes three Dovetail monitors bundled in one container:

* offstage checks that Linux leaves a task alone while it runs out-of-band. It
must neither schedule the task nor let it raise a signal on itself.

* oob_trap checks that every out-of-band trap notification is unwound for the
same trap before the task goes out-of-band again.

* oob_signal checks that a task receiving a signal out-of-band is kicked
in-band. Once the cokernel handled the notification, the task must not return to user mode out-of-band without a mayday pending. I think his monitor found a present bug in EVL: since "evl/sched: get rid of this_evl_rq_thread()" evl_kick_thread() skips the mayday for a thread running out-of-band on a remote CPU, so the thread ignores all signals, even SIGKILL.

For the monitors the series adds a few bare tracepoints to Dovetail. They are not exposed in tracefs.

The monitors can be built by setting CONFIG_RV_DOVETAIL, which enables RV, the monitors and the printk reactor. oob_signal is not available on 32-bit arm yet, as the out-of-band syscall fast path there is written in assembly.

To execute them:

  # cd /sys/kernel/tracing/rv
  # echo printk > monitors/dovetail/reactors
  # echo 1 > monitors/dovetail/enable

Looking forward to your feedback, input and ideas on how to use and improve this further.

Changes in v2:
* call the dovetail_leave_inband tracepoint directly from dovetail_leave_inband() and drop its helper

Best,
Tobias

[1] https://docs.kernel.org/trace/rv/runtime-verification.html
[2] https://lore.kernel.org/linux-trace-kernel/[email protected]

Tobias Schaffner (7):
dovetail: factor out the mayday check on return to user mode
dovetail: trace stage switches
rv: add dovetail container and offstage monitor
dovetail: trace out-of-band trap notifications
rv: add oob_trap monitor
dovetail: trace signal notifications and out-of-band returns
rv: add oob_signal monitor

 Documentation/trace/rv/index.rst              |   1 +
 Documentation/trace/rv/monitor_dovetail.rst   | 124 +++++++++++++++
 include/linux/dovetail.h                      |  16 +-
 include/trace/events/dovetail.h               |  40 +++++
 kernel/Kconfig.dovetail                       |  14 ++
 kernel/dovetail.c                             |  30 +++-
 kernel/irq/pipeline.c                         |   4 +-
 kernel/sched/core.c                           |   3 +
 kernel/trace/rv/Kconfig                       |   5 +
 kernel/trace/rv/Makefile                      |   4 +
 kernel/trace/rv/monitors/dovetail/Kconfig     |  13 ++
 kernel/trace/rv/monitors/dovetail/dovetail.c  |  41 +++++
 kernel/trace/rv/monitors/dovetail/dovetail.h  |   3 +
 kernel/trace/rv/monitors/offstage/Kconfig     |  15 ++
 kernel/trace/rv/monitors/offstage/offstage.c  | 114 ++++++++++++++
 kernel/trace/rv/monitors/offstage/offstage.h  |  61 ++++++++
 .../rv/monitors/offstage/offstage_trace.h     |  15 ++
 kernel/trace/rv/monitors/oob_signal/Kconfig   |  16 ++
 .../trace/rv/monitors/oob_signal/oob_signal.c | 114 ++++++++++++++
 .../trace/rv/monitors/oob_signal/oob_signal.h |  74 +++++++++
 .../rv/monitors/oob_signal/oob_signal_trace.h |  15 ++
 kernel/trace/rv/monitors/oob_trap/Kconfig     |  14 ++
 kernel/trace/rv/monitors/oob_trap/oob_trap.c  | 144 ++++++++++++++++++
 kernel/trace/rv/monitors/oob_trap/oob_trap.h  |  61 ++++++++
 .../rv/monitors/oob_trap/oob_trap_trace.h     |  19 +++
 kernel/trace/rv/rv_trace.h                    |   3 +
 tools/verification/models/offstage.dot        |  14 ++
 tools/verification/models/oob_signal.dot      |  23 +++
 tools/verification/models/oob_trap.dot        |  14 ++
 29 files changed, 1006 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/trace/rv/monitor_dovetail.rst
 create mode 100644 include/trace/events/dovetail.h
 create mode 100644 kernel/trace/rv/monitors/dovetail/Kconfig
 create mode 100644 kernel/trace/rv/monitors/dovetail/dovetail.c
 create mode 100644 kernel/trace/rv/monitors/dovetail/dovetail.h
 create mode 100644 kernel/trace/rv/monitors/offstage/Kconfig
 create mode 100644 kernel/trace/rv/monitors/offstage/offstage.c
 create mode 100644 kernel/trace/rv/monitors/offstage/offstage.h
 create mode 100644 kernel/trace/rv/monitors/offstage/offstage_trace.h
 create mode 100644 kernel/trace/rv/monitors/oob_signal/Kconfig
 create mode 100644 kernel/trace/rv/monitors/oob_signal/oob_signal.c
 create mode 100644 kernel/trace/rv/monitors/oob_signal/oob_signal.h
 create mode 100644 kernel/trace/rv/monitors/oob_signal/oob_signal_trace.h
 create mode 100644 kernel/trace/rv/monitors/oob_trap/Kconfig
 create mode 100644 kernel/trace/rv/monitors/oob_trap/oob_trap.c
 create mode 100644 kernel/trace/rv/monitors/oob_trap/oob_trap.h
 create mode 100644 kernel/trace/rv/monitors/oob_trap/oob_trap_trace.h
 create mode 100644 tools/verification/models/offstage.dot
 create mode 100644 tools/verification/models/oob_signal.dot
 create mode 100644 tools/verification/models/oob_trap.dot

base-commit: a3ad77d7ba3eac8a73f64026272747c01dd84fc6

Signature · 1 line
2.53.0
#1 lore raw

RFC PATCH v2 1/7 dovetail: factor out the mayday check on return to user mode

Dovetail checks for a pending mayday at three places before returning to user mode out-of-band. Move the check into dovetail_oob_exit_to_user(). On the IRQ path it now runs only when returning out-of-band, as an in-band task has no mayday pending.

---
 include/linux/dovetail.h |  2 ++
 kernel/dovetail.c        | 14 +++++++++-----
 kernel/irq/pipeline.c    |  4 ++--
 3 files changed, 13 insertions(+), 7 deletions(-)
include/linux/dovetail.h +2 -0
index 1140c5b7c99f..b41ea0f8fc4e 100644
@@ -198,6 +198,8 @@ struct oob_mm_state *dovetail_mm_state(void)
 
 void dovetail_call_mayday(struct pt_regs *regs);
 
+void dovetail_oob_exit_to_user(struct pt_regs *regs);
+
 static inline void dovetail_send_mayday(struct task_struct *castaway)
 {
 	struct thread_info *ti = task_thread_info(castaway);
kernel/dovetail.c +9 -5
index d2ea78913fb6..da4016697b67 100644
@@ -99,6 +99,13 @@ void dovetail_call_mayday(struct pt_regs *regs)
 	hard_local_irq_restore(flags);
 }
 
+/* Returning to user mode on the out-of-band stage. */
+void dovetail_oob_exit_to_user(struct pt_regs *regs)
+{
+	if (test_thread_flag(TIF_MAYDAY))
+		dovetail_call_mayday(regs);
+}
+
 void inband_retuser_notify(void)
 {
 	clear_thread_flag(TIF_RETUSER);
@@ -137,7 +144,6 @@ bool in_oob_syscall(struct pt_regs *regs)
 
 int __pipeline_syscall(struct pt_regs *regs)
 {
-	struct thread_info *ti = current_thread_info();
 	struct irq_stage *caller_stage, *target_stage;
 	struct irq_stage_data *p, *this_context;
 	unsigned long flags;
@@ -189,8 +195,7 @@ int __pipeline_syscall(struct pt_regs *regs)
 		if (stage_irqs_pending(p))
 			sync_current_irq_stage();
 	} else {
-		if (test_ti_thread_flag(ti, TIF_MAYDAY))
-			call_mayday(ti, regs);
+		dovetail_oob_exit_to_user(regs);
 	}
 
 	hard_local_irq_restore(flags);
@@ -244,8 +249,7 @@ int pipeline_syscall(unsigned int nr, struct pt_regs *regs)
 		local_flags = READ_ONCE(ti_local_flags(ti));
 		if (likely(ret)) {
 			if (local_flags & _TLF_OOB) {
-				if (test_ti_thread_flag(ti, TIF_MAYDAY))
-					dovetail_call_mayday(regs);
+				dovetail_oob_exit_to_user(regs);
 				return 1; /* don't pass down, no tail work. */
 			} else {
 				WARN_ON_ONCE(dovetail_debug() && irqs_disabled());
kernel/irq/pipeline.c +2 -2
index 88911489aeb6..f9c9c13d7137 100644
@@ -1244,8 +1244,8 @@ int handle_irq_pipelined_finish(struct irq_stage_data *prevd,
 	 * Sending MAYDAY is in essence a rare case, so prefer test
 	 * then maybe clear over test_and_clear.
 	 */
-	if (user_mode(regs) && test_thread_flag(TIF_MAYDAY))
-		dovetail_call_mayday(regs);
+	if (user_mode(regs) && running_oob())
+		dovetail_oob_exit_to_user(regs);
 #endif
 
 	return running_inband() && !irqs_disabled();
Signature · 1 line
2.53.0
#3 lore raw

RFC PATCH v2 2/7 dovetail: trace stage switches

In reply to Tobias Schaffner

Add tracepoints for a task leaving the in-band and the out-of-band stage, for use by the Dovetail RV monitors.

---
 include/linux/dovetail.h        |  6 ++++++
 include/trace/events/dovetail.h | 23 +++++++++++++++++++++++
 kernel/dovetail.c               |  8 ++++++++
 kernel/sched/core.c             |  3 +++
 4 files changed, 40 insertions(+)
 create mode 100644 include/trace/events/dovetail.h
include/linux/dovetail.h +6 -0
index b41ea0f8fc4e..3c079e5bf373 100644
@@ -18,6 +18,7 @@ struct files_struct;
 #include <linux/mm.h>
 #include <linux/thread_info.h>
 #include <linux/irqstage.h>
+#include <linux/tracepoint-defs.h>
 #include <uapi/asm-generic/dovetail.h>
 #include <asm/dovetail.h>
 
@@ -163,8 +164,13 @@ void dovetail_stop_altsched(void);
 
 __must_check int dovetail_leave_inband(void);
 
+DECLARE_TRACEPOINT(dovetail_leave_oob_tp);
+void __trace_dovetail_leave_oob(void);
+
 static inline void dovetail_leave_oob(void)
 {
+	if (tracepoint_enabled(dovetail_leave_oob_tp))
+		__trace_dovetail_leave_oob();
 	clear_thread_local_flags(_TLF_OOB|_TLF_OFFSTAGE);
 	clear_thread_flag(TIF_MAYDAY);
 }
include/trace/events/dovetail.h new file +23 -0
new file mode 100644
index 000000000000..785a653252d3
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#undef TRACE_SYSTEM
+#define TRACE_SYSTEM dovetail
+
+#if !defined(_TRACE_DOVETAIL_H) || defined(TRACE_HEADER_MULTI_READ)
+#define _TRACE_DOVETAIL_H
+
+#include <linux/tracepoint.h>
+
+struct task_struct;
+
+DECLARE_TRACE(dovetail_leave_inband,
+	TP_PROTO(struct task_struct *p),
+	TP_ARGS(p));
+
+DECLARE_TRACE(dovetail_leave_oob,
+	TP_PROTO(struct task_struct *p),
+	TP_ARGS(p));
+
+#endif /* _TRACE_DOVETAIL_H */
+
+/* This part must be outside protection */
+#include <trace/define_trace.h>
kernel/dovetail.c +8 -0
index da4016697b67..b2ed57829a64 100644
@@ -12,6 +12,14 @@
 #include <asm/syscall.h>
 #include <uapi/asm-generic/dovetail.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/dovetail.h>
+
+void __trace_dovetail_leave_oob(void)
+{
+	trace_dovetail_leave_oob_tp(current);
+}
+
 static bool dovetail_enabled;
 
 void __weak arch_inband_task_init(struct task_struct *p)
kernel/sched/core.c +3 -0
index 3ec77a682c4a..aa742a4a9464 100644
@@ -88,6 +88,8 @@
 #include <trace/events/ipi.h>
 #undef CREATE_TRACE_POINTS
 
+#include <trace/events/dovetail.h>
+
 #include "sched.h"
 #include "stats.h"
 
@@ -10689,6 +10691,7 @@ int dovetail_leave_inband(void)
 	 */
 	if (likely(__schedule(SM_NONE))) {
 		arch_dovetail_switch_finish(false);
+		trace_dovetail_leave_inband_tp(p);
 		return 0;
 	}
 
Signature · 1 line
2.53.0
#4 lore raw

RFC PATCH v2 3/7 add dovetail container and offstage monitor

In reply to Tobias Schaffner

Add a container for monitors verifying the interaction between Dovetail and the companion core, and CONFIG_RV_DOVETAIL to enable it with RV.

Add the offstage monitor checking that Linux neither schedules a task nor lets it signal itself while it runs out-of-band.

---
 Documentation/trace/rv/index.rst              |   1 +
 Documentation/trace/rv/monitor_dovetail.rst   |  64 ++++++++++
 kernel/Kconfig.dovetail                       |  14 +++
 kernel/trace/rv/Kconfig                       |   3 +
 kernel/trace/rv/Makefile                      |   2 +
 kernel/trace/rv/monitors/dovetail/Kconfig     |  13 ++
 kernel/trace/rv/monitors/dovetail/dovetail.c  |  41 +++++++
 kernel/trace/rv/monitors/dovetail/dovetail.h  |   3 +
 kernel/trace/rv/monitors/offstage/Kconfig     |  15 +++
 kernel/trace/rv/monitors/offstage/offstage.c  | 114 ++++++++++++++++++
 kernel/trace/rv/monitors/offstage/offstage.h  |  61 ++++++++++
 .../rv/monitors/offstage/offstage_trace.h     |  15 +++
 kernel/trace/rv/rv_trace.h                    |   1 +
 tools/verification/models/offstage.dot        |  14 +++
 14 files changed, 361 insertions(+)
 create mode 100644 Documentation/trace/rv/monitor_dovetail.rst
 create mode 100644 kernel/trace/rv/monitors/dovetail/Kconfig
 create mode 100644 kernel/trace/rv/monitors/dovetail/dovetail.c
 create mode 100644 kernel/trace/rv/monitors/dovetail/dovetail.h
 create mode 100644 kernel/trace/rv/monitors/offstage/Kconfig
 create mode 100644 kernel/trace/rv/monitors/offstage/offstage.c
 create mode 100644 kernel/trace/rv/monitors/offstage/offstage.h
 create mode 100644 kernel/trace/rv/monitors/offstage/offstage_trace.h
 create mode 100644 tools/verification/models/offstage.dot
Documentation/trace/rv/index.rst +1 -0
index 29769f06bb0f..8e2b4a270670 100644
@@ -14,6 +14,7 @@ Runtime Verification
    da_monitor_instrumentation.rst
    monitor_wip.rst
    monitor_wwnr.rst
+   monitor_dovetail.rst
    monitor_sched.rst
    monitor_rtapp.rst
    monitor_stall.rst
Documentation/trace/rv/monitor_dovetail.rst new file +64 -0

64 lines not on this page · raw message

kernel/Kconfig.dovetail +14 -0
index 29c46e06e435..26478d9112d9 100644
@@ -18,6 +18,20 @@ config DOVETAIL
 	  running a high-priority software core on the out-of-band
 	  pipeline stage (aka "dual kernel" configuration).
 
+config RV_DOVETAIL
+	bool "Runtime verification of the Dovetail interface"
+	depends on DOVETAIL
+	select RV
+	select RV_REACTORS
+	select RV_REACT_PRINTK
+	select RV_MON_DOVETAIL
+	help
+	  Enable the runtime verification monitors checking the contract
+	  between Dovetail and the companion core, with the printk reactor.
+
+	  For further information, see:
+	    Documentation/trace/rv/monitor_dovetail.rst
+
 config DOVETAIL_LEGACY_SYSCALL_RANGE
        depends on DOVETAIL
        def_bool y
kernel/trace/rv/Kconfig +3 -0
index 3884b14df375..3f43cdfcbb32 100644
@@ -54,6 +54,7 @@ config RV_PER_TASK_MONITORS
 	int "Maximum number of per-task monitor"
 	depends on RV
 	range 1 8
+	default 3 if RV_DOVETAIL
 	default 2
 	help
 	  This option configures the maximum number of per-task RV monitors that can run
@@ -83,6 +84,8 @@ source "kernel/trace/rv/monitors/deadline/Kconfig"
 source "kernel/trace/rv/monitors/nomiss/Kconfig"
 # Add new deadline monitors here
 
+source "kernel/trace/rv/monitors/dovetail/Kconfig"
+source "kernel/trace/rv/monitors/offstage/Kconfig"
 # Add new monitors here
 
 config RV_REACTORS
kernel/trace/rv/Makefile +2 -0
index 94498da35b37..a684b0c9af2f 100644
@@ -20,6 +20,8 @@ obj-$(CONFIG_RV_MON_OPID) += monitors/opid/opid.o
 obj-$(CONFIG_RV_MON_STALL) += monitors/stall/stall.o
 obj-$(CONFIG_RV_MON_DEADLINE) += monitors/deadline/deadline.o
 obj-$(CONFIG_RV_MON_NOMISS) += monitors/nomiss/nomiss.o
+obj-$(CONFIG_RV_MON_DOVETAIL) += monitors/dovetail/dovetail.o
+obj-$(CONFIG_RV_MON_OFFSTAGE) += monitors/offstage/offstage.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/dovetail/Kconfig new file +13 -0
new file mode 100644
index 000000000000..59faf165d0ec
@@ -0,0 +1,13 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_DOVETAIL
+	depends on RV
+	depends on DOVETAIL
+	bool "dovetail monitor"
+	help
+	  Collection of per-task monitors checking that Dovetail and its
+	  companion core keep their contract: stage switches, out-of-band
+	  traps and signals. Enable this to enable all of them.
+
+	  For further information, see:
+	    Documentation/trace/rv/monitor_dovetail.rst
kernel/trace/rv/monitors/dovetail/dovetail.c new file +41 -0

41 lines not on this page · raw message

kernel/trace/rv/monitors/dovetail/dovetail.h new file +3 -0
new file mode 100644
index 000000000000..279806cd8e00
@@ -0,0 +1,3 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+extern struct rv_monitor rv_dovetail;
kernel/trace/rv/monitors/offstage/Kconfig new file +15 -0
new file mode 100644
index 000000000000..2b9c6869b315
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_OFFSTAGE
+	depends on RV
+	depends on RV_MON_DOVETAIL
+	default y
+	select DA_MON_EVENTS_ID
+	bool "offstage monitor"
+	help
+	  Per-task monitor following which stage a dovetailed task runs on,
+	  in-band or out-of-band. It reports a task that Linux schedules, or
+	  that raises a signal on itself, while Dovetail has it out-of-band.
+
+	  For further information, see:
+	    Documentation/trace/rv/monitor_dovetail.rst
kernel/trace/rv/monitors/offstage/offstage.c new file +114 -0

114 lines not on this page · raw message

kernel/trace/rv/monitors/offstage/offstage.h new file +61 -0

61 lines not on this page · raw message

kernel/trace/rv/monitors/offstage/offstage_trace.h new file +15 -0
new file mode 100644
index 000000000000..275ba7c78914
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_OFFSTAGE
+DEFINE_EVENT(event_da_monitor_id, event_offstage,
+	     TP_PROTO(int id, char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(id, state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor_id, error_offstage,
+	     TP_PROTO(int id, char *state, char *event),
+	     TP_ARGS(id, state, event));
+#endif /* CONFIG_RV_MON_OFFSTAGE */
kernel/trace/rv/rv_trace.h +1 -0
index 9622c269789c..9bda8ef3b96f 100644
@@ -156,6 +156,7 @@ DECLARE_EVENT_CLASS(error_da_monitor_id,
 #include <monitors/snroc/snroc_trace.h>
 #include <monitors/nrp/nrp_trace.h>
 #include <monitors/sssw/sssw_trace.h>
+#include <monitors/offstage/offstage_trace.h>
 // Add new monitors based on CONFIG_DA_MON_EVENTS_ID here
 
 #ifdef CONFIG_HA_MON_EVENTS_ID
tools/verification/models/offstage.dot new file +14 -0
new file mode 100644
index 000000000000..79f36efd430f
@@ -0,0 +1,14 @@
+digraph state_automaton {
+	{node [shape = plaintext, style=invis, label=""] "__init_inband"};
+	{node [shape = ellipse] "inband"};
+	{node [shape = plaintext] "oob"};
+	"__init_inband" -> "inband";
+	"inband" [label = "inband", color = green3];
+	"inband" -> "inband" [ label = "sched_in\nsignal_self" ];
+	"inband" -> "oob" [ label = "leave_inband" ];
+	"oob" -> "inband" [ label = "leave_oob" ];
+	{ rank = min ;
+		"__init_inband";
+		"inband";
+	}
+}
Signature · 1 line
2.53.0
#6 lore raw

RFC PATCH v2 5/7 add oob_trap monitor

In reply to Tobias Schaffner

A trap handler missing oob_trap_unwind() leaves _TLF_OOBTRAP set and the companion core is no longer notified about traps of that task.

Add a monitor checking that every out-of-band trap is unwound for the same trap, by number and frame, before the task resumes out-of-band.

---
 Documentation/trace/rv/monitor_dovetail.rst   |  26 ++++
 kernel/trace/rv/Kconfig                       |   1 +
 kernel/trace/rv/Makefile                      |   1 +
 kernel/trace/rv/monitors/oob_trap/Kconfig     |  14 ++
 kernel/trace/rv/monitors/oob_trap/oob_trap.c  | 144 ++++++++++++++++++
 kernel/trace/rv/monitors/oob_trap/oob_trap.h  |  61 ++++++++
 .../rv/monitors/oob_trap/oob_trap_trace.h     |  19 +++
 kernel/trace/rv/rv_trace.h                    |   1 +
 tools/verification/models/oob_trap.dot        |  14 ++
 9 files changed, 281 insertions(+)
 create mode 100644 kernel/trace/rv/monitors/oob_trap/Kconfig
 create mode 100644 kernel/trace/rv/monitors/oob_trap/oob_trap.c
 create mode 100644 kernel/trace/rv/monitors/oob_trap/oob_trap.h
 create mode 100644 kernel/trace/rv/monitors/oob_trap/oob_trap_trace.h
 create mode 100644 tools/verification/models/oob_trap.dot
Documentation/trace/rv/monitor_dovetail.rst +26 -0
index e46cd0ab0b8d..e660b5c9a1ea 100644
@@ -27,6 +27,8 @@ dovetail_leave_inband_tp    oob     after a successful switch; oob stage
                                     stalled, hard IRQs off, the core may hold
                                     its runqueue lock
 dovetail_leave_oob_tp       oob     before the switch to in-band, in the core
+dovetail_trap_notify_tp     oob     hard IRQs off, before the core is notified
+dovetail_trap_unwind_tp     either  hard IRQs off, before the core is notified
 ==========================  ======  ============================================
 
 Monitor offstage
@@ -48,6 +50,29 @@ the companion core demoted the task would::
 ``sched_in`` is a ``sched_switch`` to the task, ``signal_self`` a
 ``signal_generate`` by the task for itself.
 
+Monitor oob_trap
+++++++++++++++++
+
+Every out-of-band trap notification is unwound for the same trap before the
+task goes out-of-band again. A handler missing the unwind leaves
+``_TLF_OOBTRAP`` set, and the companion core is no longer told about the
+traps of that task::
+
+                  trap_notify; reset(same_trap)
+          +-------+   ------------------------>   +---------+
+          | idle  |                               | trapped |
+          +-------+   <------------------------   +---------+
+           |     ^    trap_unwind; same_trap == 1
+           +-----+
+         leave_inband
+
+A trap is identified by its number and register frame, so an unwind with the
+wrong number, and the unwind of a trap nested in the in-band handling of
+another one, are reported too. A leak is only seen once the task goes
+out-of-band again. Traps taken in NMI context are ignored. The model uses no
+clock, so the hybrid automaton core arms no timer and is safe on the
+out-of-band stage.
+
 Usage
 -----
 
@@ -59,6 +84,7 @@ Usage
   # cat enabled_monitors
   dovetail
   dovetail:offstage
+  dovetail:oob_trap
 
 Violations are reported by the selected reactor and as ``error_*`` events in
 ``events/rv``. The models are in ``tools/verification/models/``.
kernel/trace/rv/Kconfig +1 -0
index 3f43cdfcbb32..d7207d7e9892 100644
@@ -86,6 +86,7 @@ source "kernel/trace/rv/monitors/nomiss/Kconfig"
 
 source "kernel/trace/rv/monitors/dovetail/Kconfig"
 source "kernel/trace/rv/monitors/offstage/Kconfig"
+source "kernel/trace/rv/monitors/oob_trap/Kconfig"
 # Add new monitors here
 
 config RV_REACTORS
kernel/trace/rv/Makefile +1 -0
index a684b0c9af2f..c5848be60f5b 100644
@@ -22,6 +22,7 @@ obj-$(CONFIG_RV_MON_DEADLINE) += monitors/deadline/deadline.o
 obj-$(CONFIG_RV_MON_NOMISS) += monitors/nomiss/nomiss.o
 obj-$(CONFIG_RV_MON_DOVETAIL) += monitors/dovetail/dovetail.o
 obj-$(CONFIG_RV_MON_OFFSTAGE) += monitors/offstage/offstage.o
+obj-$(CONFIG_RV_MON_OOB_TRAP) += monitors/oob_trap/oob_trap.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/oob_trap/Kconfig new file +14 -0
new file mode 100644
index 000000000000..7a1709d3a8f3
@@ -0,0 +1,14 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_OOB_TRAP
+	depends on RV
+	depends on RV_MON_DOVETAIL
+	default y
+	select HA_MON_EVENTS_ID
+	bool "oob_trap monitor"
+	help
+	  Per-task monitor checking that every out-of-band trap notification
+	  is unwound for the same trap before the task goes out-of-band again.
+
+	  For further information, see:
+	    Documentation/trace/rv/monitor_dovetail.rst
kernel/trace/rv/monitors/oob_trap/oob_trap.c new file +144 -0

144 lines not on this page · raw message

kernel/trace/rv/monitors/oob_trap/oob_trap.h new file +61 -0

61 lines not on this page · raw message

kernel/trace/rv/monitors/oob_trap/oob_trap_trace.h new file +19 -0
new file mode 100644
index 000000000000..fcb2f5bc6965
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_OOB_TRAP
+DEFINE_EVENT(event_da_monitor_id, event_oob_trap,
+	     TP_PROTO(int id, char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(id, state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor_id, error_oob_trap,
+	     TP_PROTO(int id, char *state, char *event),
+	     TP_ARGS(id, state, event));
+
+DEFINE_EVENT(error_env_da_monitor_id, error_env_oob_trap,
+	     TP_PROTO(int id, char *state, char *event, char *env),
+	     TP_ARGS(id, state, event, env));
+#endif /* CONFIG_RV_MON_OOB_TRAP */
kernel/trace/rv/rv_trace.h +1 -0
index 9bda8ef3b96f..bcd7ee424e89 100644
@@ -190,6 +190,7 @@ DECLARE_EVENT_CLASS(error_env_da_monitor_id,
 
 #include <monitors/stall/stall_trace.h>
 #include <monitors/nomiss/nomiss_trace.h>
+#include <monitors/oob_trap/oob_trap_trace.h>
 // Add new monitors based on CONFIG_HA_MON_EVENTS_ID here
 
 #endif
tools/verification/models/oob_trap.dot new file +14 -0
new file mode 100644
index 000000000000..97ef5c3b9431
@@ -0,0 +1,14 @@
+digraph state_automaton {
+	{node [shape = plaintext, style=invis, label=""] "__init_idle"};
+	{node [shape = ellipse] "idle"};
+	{node [shape = plaintext] "trapped"};
+	"__init_idle" -> "idle";
+	"idle" [label = "idle", color = green3];
+	"idle" -> "trapped" [ label = "trap_notify;reset(same_trap)" ];
+	"trapped" -> "idle" [ label = "trap_unwind;same_trap == 1" ];
+	"idle" -> "idle" [ label = "leave_inband" ];
+	{ rank = min ;
+		"__init_idle";
+		"idle";
+	}
+}
Signature · 1 line
2.53.0
#7 lore raw

RFC PATCH v2 6/7 dovetail: trace signal notifications and out-of-band returns

In reply to Tobias Schaffner

Add a tracepoint for the signal notification to the companion core, fired once the core handled it, and one in dovetail_oob_exit_to_user(), for use by the oob_signal RV monitor.

---
 include/linux/dovetail.h        | 8 +++++++-
 include/trace/events/dovetail.h | 8 ++++++++
 kernel/dovetail.c               | 6 ++++++
 3 files changed, 21 insertions(+), 1 deletion(-)
include/linux/dovetail.h +7 -1
index 3c079e5bf373..6e1333956a3c 100644
@@ -85,10 +85,16 @@ void inband_event_notify(enum inband_event_type,
 
 void inband_clock_was_set(void);
 
+DECLARE_TRACEPOINT(dovetail_signal_notify_tp);
+void __trace_dovetail_signal_notify(struct task_struct *p);
+
 static inline void inband_signal_notify(struct task_struct *p)
 {
-	if (test_ti_local_flags(task_thread_info(p), _TLF_DOVETAIL))
+	if (test_ti_local_flags(task_thread_info(p), _TLF_DOVETAIL)) {
 		inband_event_notify(INBAND_TASK_SIGNAL, p);
+		if (tracepoint_enabled(dovetail_signal_notify_tp))
+			__trace_dovetail_signal_notify(p);
+	}
 }
 
 static inline void inband_migration_notify(struct task_struct *p, int cpu)
include/trace/events/dovetail.h +8 -0
index 86d6405e84f5..6ac5a7fed420 100644
@@ -26,6 +26,14 @@ DECLARE_TRACE(dovetail_trap_unwind,
 	TP_PROTO(unsigned int trapnr, struct pt_regs *regs),
 	TP_ARGS(trapnr, regs));
 
+DECLARE_TRACE(dovetail_oob_return,
+	TP_PROTO(struct task_struct *p),
+	TP_ARGS(p));
+
+DECLARE_TRACE(dovetail_signal_notify,
+	TP_PROTO(struct task_struct *p),
+	TP_ARGS(p));
+
 #endif /* _TRACE_DOVETAIL_H */
 
 /* This part must be outside protection */
kernel/dovetail.c +6 -0
index 0f7dae3beeda..93fd35828ca5 100644
@@ -20,6 +20,11 @@ void __trace_dovetail_leave_oob(void)
 	trace_dovetail_leave_oob_tp(current);
 }
 
+void __trace_dovetail_signal_notify(struct task_struct *p)
+{
+	trace_dovetail_signal_notify_tp(p);
+}
+
 static bool dovetail_enabled;
 
 void __weak arch_inband_task_init(struct task_struct *p)
@@ -110,6 +115,7 @@ void dovetail_call_mayday(struct pt_regs *regs)
 /* Returning to user mode on the out-of-band stage. */
 void dovetail_oob_exit_to_user(struct pt_regs *regs)
 {
+	trace_dovetail_oob_return_tp(current);
 	if (test_thread_flag(TIF_MAYDAY))
 		dovetail_call_mayday(regs);
 }
Signature · 1 line
2.53.0
#8 lore raw

RFC PATCH v2 7/7 add oob_signal monitor

In reply to Tobias Schaffner

Signals are delivered in-band only, so the companion core has to kick a task receiving a signal out-of-band, usually with a mayday.

Add a monitor checking that such a task does not return to user mode out-of-band without a mayday pending.

---
 Documentation/trace/rv/monitor_dovetail.rst   |  34 ++++++
 kernel/trace/rv/Kconfig                       |   1 +
 kernel/trace/rv/Makefile                      |   1 +
 kernel/trace/rv/monitors/oob_signal/Kconfig   |  16 +++
 .../trace/rv/monitors/oob_signal/oob_signal.c | 114 ++++++++++++++++++
 .../trace/rv/monitors/oob_signal/oob_signal.h |  74 ++++++++++++
 .../rv/monitors/oob_signal/oob_signal_trace.h |  15 +++
 kernel/trace/rv/rv_trace.h                    |   1 +
 tools/verification/models/oob_signal.dot      |  23 ++++
 9 files changed, 279 insertions(+)
 create mode 100644 kernel/trace/rv/monitors/oob_signal/Kconfig
 create mode 100644 kernel/trace/rv/monitors/oob_signal/oob_signal.c
 create mode 100644 kernel/trace/rv/monitors/oob_signal/oob_signal.h
 create mode 100644 kernel/trace/rv/monitors/oob_signal/oob_signal_trace.h
 create mode 100644 tools/verification/models/oob_signal.dot
Documentation/trace/rv/monitor_dovetail.rst +34 -0
index e660b5c9a1ea..de9f6f05db24 100644
@@ -17,6 +17,10 @@ are per-task monitors fed by Dovetail and generic tracepoints only, meant for
 validation: while enabled, they add work to out-of-band hot paths.
 ``CONFIG_RV_DOVETAIL`` enables them with RV and the printk reactor.
 
+Each monitor takes one of ``CONFIG_RV_PER_TASK_MONITORS`` slots while
+enabled, so the container needs at least three, the default with
+``CONFIG_RV_DOVETAIL``.
+
 The Dovetail tracepoints fire in the context given here, so their probes must
 be safe there:
 
@@ -29,6 +33,11 @@ dovetail_leave_inband_tp    oob     after a successful switch; oob stage
 dovetail_leave_oob_tp       oob     before the switch to in-band, in the core
 dovetail_trap_notify_tp     oob     hard IRQs off, before the core is notified
 dovetail_trap_unwind_tp     either  hard IRQs off, before the core is notified
+dovetail_oob_return_tp      oob     on return to user mode from an interrupt
+                                    or an oob syscall; hard IRQs on at the oob
+                                    syscall return
+dovetail_signal_notify_tp   inband  after the core handled the notification,
+                                    siglock held
 ==========================  ======  ============================================
 
 Monitor offstage
@@ -73,6 +82,30 @@ out-of-band again. Traps taken in NMI context are ignored. The model uses no
 clock, so the hybrid automaton core arms no timer and is safe on the
 out-of-band stage.
 
+Monitor oob_signal
+++++++++++++++++++
+
+Signals are delivered in-band, so the companion core has to kick a task
+signaled out-of-band, with a mayday if the task may return to user mode
+there. Once the core handled the notification, the task must not return to
+user mode out-of-band without a mayday pending. A task reaching the
+out-of-band stage with a signal pending is held to the same rule::
+
+    state     leave_   leave_  oob_    oob_return_  signal
+              inband   oob     return  mayday
+    inband    oob      -       -       -            inband
+    oob       -        inband  oob     oob          signaled
+    signaled  -        inband  -       signaled     signaled
+
+The monitor takes no lock. The core sets ``TIF_MAYDAY`` in its
+``INBAND_TASK_SIGNAL`` handler, before the notification moves the task to
+signaled, and a return reads ``TIF_MAYDAY`` only once it sees signaled. As the
+notifier holds ``siglock``, the task cannot consume the signal and come back
+out-of-band before the notification is seen. A return from a trap the core
+does not demote, or after a RETUSER switch, is not seen. The monitor is not
+available on 32-bit arm, whose out-of-band syscall fast path is not
+instrumented.
+
 Usage
 -----
 
@@ -85,6 +118,7 @@ Usage
   dovetail
   dovetail:offstage
   dovetail:oob_trap
+  dovetail:oob_signal
 
 Violations are reported by the selected reactor and as ``error_*`` events in
 ``events/rv``. The models are in ``tools/verification/models/``.
kernel/trace/rv/Kconfig +1 -0
index d7207d7e9892..cf1c81e3df3f 100644
@@ -87,6 +87,7 @@ source "kernel/trace/rv/monitors/nomiss/Kconfig"
 source "kernel/trace/rv/monitors/dovetail/Kconfig"
 source "kernel/trace/rv/monitors/offstage/Kconfig"
 source "kernel/trace/rv/monitors/oob_trap/Kconfig"
+source "kernel/trace/rv/monitors/oob_signal/Kconfig"
 # Add new monitors here
 
 config RV_REACTORS
kernel/trace/rv/Makefile +1 -0
index c5848be60f5b..46bc8bf33e33 100644
@@ -23,6 +23,7 @@ obj-$(CONFIG_RV_MON_NOMISS) += monitors/nomiss/nomiss.o
 obj-$(CONFIG_RV_MON_DOVETAIL) += monitors/dovetail/dovetail.o
 obj-$(CONFIG_RV_MON_OFFSTAGE) += monitors/offstage/offstage.o
 obj-$(CONFIG_RV_MON_OOB_TRAP) += monitors/oob_trap/oob_trap.o
+obj-$(CONFIG_RV_MON_OOB_SIGNAL) += monitors/oob_signal/oob_signal.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/oob_signal/Kconfig new file +16 -0
new file mode 100644
index 000000000000..82d3ca1dbed7
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_OOB_SIGNAL
+	depends on RV
+	depends on RV_MON_DOVETAIL
+	depends on !ARM
+	default y
+	select DA_MON_EVENTS_ID
+	bool "oob_signal monitor"
+	help
+	  Per-task monitor checking that a dovetailed task which receives a
+	  signal while out-of-band does not return to user mode out-of-band
+	  without a mayday pending, since signals are delivered in-band only.
+
+	  For further information, see:
+	    Documentation/trace/rv/monitor_dovetail.rst
kernel/trace/rv/monitors/oob_signal/oob_signal.c new file +114 -0

114 lines not on this page · raw message

kernel/trace/rv/monitors/oob_signal/oob_signal.h new file +74 -0

74 lines not on this page · raw message

kernel/trace/rv/monitors/oob_signal/oob_signal_trace.h new file +15 -0
new file mode 100644
index 000000000000..63842f32effb
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_OOB_SIGNAL
+DEFINE_EVENT(event_da_monitor_id, event_oob_signal,
+	     TP_PROTO(int id, char *state, char *event, char *next_state, bool final_state),
+	     TP_ARGS(id, state, event, next_state, final_state));
+
+DEFINE_EVENT(error_da_monitor_id, error_oob_signal,
+	     TP_PROTO(int id, char *state, char *event),
+	     TP_ARGS(id, state, event));
+#endif /* CONFIG_RV_MON_OOB_SIGNAL */
kernel/trace/rv/rv_trace.h +1 -0
index bcd7ee424e89..e590b753beab 100644
@@ -157,6 +157,7 @@ DECLARE_EVENT_CLASS(error_da_monitor_id,
 #include <monitors/nrp/nrp_trace.h>
 #include <monitors/sssw/sssw_trace.h>
 #include <monitors/offstage/offstage_trace.h>
+#include <monitors/oob_signal/oob_signal_trace.h>
 // Add new monitors based on CONFIG_DA_MON_EVENTS_ID here
 
 #ifdef CONFIG_HA_MON_EVENTS_ID
tools/verification/models/oob_signal.dot new file +23 -0
new file mode 100644
index 000000000000..1a9e9b5c9e17
@@ -0,0 +1,23 @@
+digraph state_automaton {
+	center = true;
+	size = "7,11";
+	{node [shape = plaintext, style=invis, label=""] "__init_inband"};
+	{node [shape = doublecircle] "inband"};
+	{node [shape = circle] "oob"};
+	{node [shape = circle] "signaled"};
+	"__init_inband" -> "inband";
+	"inband" [label = "inband", color = green3];
+	"oob" [label = "oob"];
+	"signaled" [label = "signaled"];
+	"inband" -> "inband" [ label = "signal" ];
+	"inband" -> "oob" [ label = "leave_inband" ];
+	"oob" -> "inband" [ label = "leave_oob" ];
+	"oob" -> "oob" [ label = "oob_return\noob_return_mayday" ];
+	"oob" -> "signaled" [ label = "signal" ];
+	"signaled" -> "inband" [ label = "leave_oob" ];
+	"signaled" -> "signaled" [ label = "signal\noob_return_mayday" ];
+	{ rank = min ;
+		"__init_inband";
+		"inband";
+	}
+}
Signature · 1 line
2.53.0
#5 lore raw

RFC PATCH v2 4/7 dovetail: trace out-of-band trap notifications

In reply to Tobias Schaffner

Add tracepoints to oob_trap_notify() and oob_trap_unwind(), passing the trap number and its register frame, for use by the oob_trap RV monitor.

---
 include/trace/events/dovetail.h | 9 +++++++++
 kernel/dovetail.c               | 2 ++
 2 files changed, 11 insertions(+)
include/trace/events/dovetail.h +9 -0
index 785a653252d3..86d6405e84f5 100644
@@ -7,6 +7,7 @@
 
 #include <linux/tracepoint.h>
 
+struct pt_regs;
 struct task_struct;
 
 DECLARE_TRACE(dovetail_leave_inband,
@@ -17,6 +18,14 @@ DECLARE_TRACE(dovetail_leave_oob,
 	TP_PROTO(struct task_struct *p),
 	TP_ARGS(p));
 
+DECLARE_TRACE(dovetail_trap_notify,
+	TP_PROTO(unsigned int trapnr, struct pt_regs *regs),
+	TP_ARGS(trapnr, regs));
+
+DECLARE_TRACE(dovetail_trap_unwind,
+	TP_PROTO(unsigned int trapnr, struct pt_regs *regs),
+	TP_ARGS(trapnr, regs));
+
 #endif /* _TRACE_DOVETAIL_H */
 
 /* This part must be outside protection */
kernel/dovetail.c +2 -0
index b2ed57829a64..0f7dae3beeda 100644
@@ -299,6 +299,7 @@ noinstr void __oob_trap_notify(unsigned int exception,
 		set_thread_local_flags(_TLF_OOBTRAP);
 		flags = hard_local_irq_save();
 		instrumentation_begin();
+		trace_dovetail_trap_notify_tp(exception, regs);
 		handle_oob_trap_entry(exception, regs);
 		instrumentation_end();
 		hard_local_irq_restore(flags);
@@ -320,6 +321,7 @@ noinstr void __oob_trap_unwind(unsigned int exception, struct pt_regs *regs)
 	hard_local_irq_disable();
 	clear_thread_local_flags(_TLF_OOBTRAP);
 	instrumentation_begin();
+	trace_dovetail_trap_unwind_tp(exception, regs);
 	handle_oob_trap_exit(exception, regs);
 	instrumentation_end();
 }
Signature · 1 line
2.53.0
#2 lore raw

Re: [RFC PATCH dovetail v2 1/7] dovetail: factor out the mayday check on return to user mode

In reply to Tobias Schaffner
Quoted 40 lines · Tobias Schaffner

Dovetail checks for a pending mayday at three places before returning to user mode out-of-band. Move the check into dovetail_oob_exit_to_user(). On the IRQ path it now runs only when returning out-of-band, as an in-band task has no mayday pending.

---
 include/linux/dovetail.h |  2 ++
 kernel/dovetail.c        | 14 +++++++++-----
 kernel/irq/pipeline.c    |  4 ++--
 3 files changed, 13 insertions(+), 7 deletions(-)
include/linux/dovetail.h +2 -0
index 1140c5b7c99f..b41ea0f8fc4e 100644
@@ -198,6 +198,8 @@ struct oob_mm_state *dovetail_mm_state(void)
 
 void dovetail_call_mayday(struct pt_regs *regs);
 
+void dovetail_oob_exit_to_user(struct pt_regs *regs);
+
 static inline void dovetail_send_mayday(struct task_struct *castaway)
 {
 	struct thread_info *ti = task_thread_info(castaway);
kernel/dovetail.c +7 -0
index d2ea78913fb6..da4016697b67 100644
@@ -99,6 +99,13 @@ void dovetail_call_mayday(struct pt_regs *regs)
 	hard_local_irq_restore(flags);
 }
 
+/* Returning to user mode on the out-of-band stage. */
+void dovetail_oob_exit_to_user(struct pt_regs *regs)
+{
+	if (test_thread_flag(TIF_MAYDAY))
+		dovetail_call_mayday(regs);
+}
+

[snip]

Quoted 5 lines
kernel/irq/pipeline.c +0 -0
index 88911489aeb6..f9c9c13d7137 100644

kernel/irq/pipeline.c int handle_irq_pipelined_finish(struct irq_stage_data *prevd, · line 1251

Quoted 10 lines
kernel/irq/pipeline.c +2 -2
@@ -1244,8 +1244,8 @@ int handle_irq_pipelined_finish(struct irq_stage_data *prevd,
 	 * Sending MAYDAY is in essence a rare case, so prefer test
 	 * then maybe clear over test_and_clear.
 	 */
-	if (user_mode(regs) && test_thread_flag(TIF_MAYDAY))
-		dovetail_call_mayday(regs);
+	if (user_mode(regs) && running_oob())
+		dovetail_oob_exit_to_user(regs);
 #endif
 
 	return running_inband() && !irqs_disabled();

The code dovetail_oob_exit_to_user() runs is now in a different compilation unit, so I would inline this helper because we can't count on LTO to fold it into the interrupt hot path.

Signature · 1 line
Philippe.