Skip to contentSkip to conversation

Linear temporal logic monitors for RT application

PATCH Superseded Nam Cao 5 participants 23 messages Mar 11, 2025 – Apr 2, 2025

Subsystems (13)

v1 of 13 Superseded Latest is v13 →

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

cover Linear temporal logic monitors for RT application

Realtime applications often have design flaws causing them to have unexpected latency. For example, the applications may raise page faults, or may be blocked trying to take a mutex without priority inheritance.

This series adds runtime verification monitors to detect these flaws.

This monitors was tried on pipewire and detected some issues. One of them has been reported to pipewire's maintainers and is being fixed:
https://gitlab.freedesktop.org/pipewire/pipewire/-/merge_requests/2285

Patch 1 and patch 2 fix some minor issues with RV.

Patch 3 adds the infrastructure for linear temporal logic monitor.

Patch 4 adds the first monitor: rtapp_block. This monitor detects if a realtime task is undesirable blocked.

Patch 5-8 prepare the pagefault tracepoints, so that patch 9 can add the rtapp_pagefault monitor. This monitor task detects if an RT task raise page fault.

Patch 10 raises the max number of per-task monitor to 2, so that the above monitors can be enabled simultaneously.
---
Cc: Petr Mladek <[email protected]>
Cc: John Ogness <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Waiman Long <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: H. Peter Anvin <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: Catalin Marinas <[email protected]>
Cc: [email protected]
Cc: Paul Walmsley <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: [email protected]
---
Nam Cao (10):
rv: Add #undef TRACE_INCLUDE_FILE
rv: Let the reactors take care of buffers
rv: Add infrastructure for linear temporal logic monitor
rv: Add rtapp_block monitor
x86/tracing: Remove redundant trace_pagefault_key x86/tracing: Move page fault trace points to generic
arm64: mm: Add page fault trace points
riscv: mm: Add page fault trace points
rv: Add rtapp_pagefault monitor
rv: raise the number of per-task monitor to 2

 .../trace/rv/linear_temporal_logic.rst        |  73 +++
 .../trace/rv/monitor_rtapp_block.rst          |  34 ++
 .../trace/rv/monitor_rtapp_pagefault.rst      |  36 ++
 arch/arm64/mm/fault.c                         |   8 +
 arch/riscv/mm/fault.c                         |   8 +
 arch/x86/include/asm/trace/common.h           |  12 -
 arch/x86/include/asm/trace/irq_vectors.h      |   1 -
 arch/x86/kernel/tracepoint.c                  |  21 -
 arch/x86/mm/fault.c                           |   5 +-
 include/linux/printk.h                        |   1 +
 include/linux/rv.h                            |  39 +-
 include/rv/da_monitor.h                       |  61 +-
 .../trace/events}/exceptions.h                |  27 +-
 include/trace/events/lock.h                   |  12 +
 kernel/fork.c                                 |   5 +-
 kernel/locking/rtmutex.c                      |   4 +
 kernel/printk/internal.h                      |   1 -
 kernel/trace/rv/Kconfig                       |  20 +-
 kernel/trace/rv/Makefile                      |   4 +
 kernel/trace/rv/monitors/rtapp_block/ba.c     | 146 +++++
 kernel/trace/rv/monitors/rtapp_block/ba.h     | 166 ++++++
 kernel/trace/rv/monitors/rtapp_block/ltl      |   9 +
 .../rv/monitors/rtapp_block/rtapp_block.c     | 232 ++++++++
 kernel/trace/rv/monitors/rtapp_pagefault/ba.c | 139 +++++
 kernel/trace/rv/monitors/rtapp_pagefault/ba.h | 158 +++++
 kernel/trace/rv/monitors/rtapp_pagefault/ltl  |   1 +
 .../rtapp_pagefault/rtapp_pagefault.c         |  84 +++
 kernel/trace/rv/reactor_panic.c               |   7 +-
 kernel/trace/rv/reactor_printk.c              |   8 +-
 kernel/trace/rv/rv_reactors.c                 |   2 +-
 kernel/trace/rv/rv_trace.h                    |  67 ++-
 lib/Kconfig.debug                             |   3 +
 tools/verification/ltl2ba/.gitignore          |   3 +
 tools/verification/ltl2ba/generate.py         | 154 +++++
 tools/verification/ltl2ba/ltl.py              | 556 ++++++++++++++++++
 tools/verification/ltl2ba/template.c          | 131 +++++
 tools/verification/ltl2ba/template.h          | 157 +++++
 37 files changed, 2262 insertions(+), 133 deletions(-)
 create mode 100644 Documentation/trace/rv/linear_temporal_logic.rst
 create mode 100644 Documentation/trace/rv/monitor_rtapp_block.rst
 create mode 100644 Documentation/trace/rv/monitor_rtapp_pagefault.rst
 delete mode 100644 arch/x86/include/asm/trace/common.h
 delete mode 100644 arch/x86/kernel/tracepoint.c
 rename {arch/x86/include/asm/trace => include/trace/events}/exceptions.h (55%)
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/ba.c
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/ba.h
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/ltl
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/rtapp_block.c
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/ba.c
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/ba.h
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/ltl
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/rtapp_pagefault.c
 create mode 100644 tools/verification/ltl2ba/.gitignore
 create mode 100755 tools/verification/ltl2ba/generate.py
 create mode 100644 tools/verification/ltl2ba/ltl.py
 create mode 100644 tools/verification/ltl2ba/template.c
 create mode 100644 tools/verification/ltl2ba/template.h
Signature · 1 line
2.39.5
author
#1 lore raw

PATCH 1/10 Add #undef TRACE_INCLUDE_FILE

Without "#undef TRACE_INCLUDE_FILE", there could be a build error due to TRACE_INCLUDE_FILE being redefined. Therefore add it.

Also fix a typo while at it.

---
 kernel/trace/rv/rv_trace.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/trace/rv/rv_trace.h +2 -1
index 5e65097423ba..96264233cac5 100644
@@ -123,8 +123,9 @@ DECLARE_EVENT_CLASS(error_da_monitor_id,
 #endif /* CONFIG_DA_MON_EVENTS_ID */
 #endif /* _TRACE_RV_H */
 
-/* This part ust be outside protection */
+/* This part must be outside protection */
 #undef TRACE_INCLUDE_PATH
 #define TRACE_INCLUDE_PATH .
+#undef TRACE_INCLUDE_FILE
 #define TRACE_INCLUDE_FILE rv_trace
 #include <trace/define_trace.h>
Signature · 1 line
2.39.5
author
#2 lore raw

PATCH 2/10 Let the reactors take care of buffers

In reply to Nam Cao

Each RV monitor has one static buffer to send to the reactors. If multiple errors are detected at the same time, the one buffer could be overwritten.

Instead, leave it to the reactors to handle buffering.

---
Cc: Petr Mladek <[email protected]>
Cc: John Ogness <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>

---
 include/linux/printk.h           |  1 +
 include/linux/rv.h               | 11 +++---
 include/rv/da_monitor.h          | 61 ++++++--------------------------
 kernel/printk/internal.h         |  1 -
 kernel/trace/rv/reactor_panic.c  |  7 +---
 kernel/trace/rv/reactor_printk.c |  8 +++--
 kernel/trace/rv/rv_reactors.c    |  2 +-
 7 files changed, 26 insertions(+), 65 deletions(-)
include/linux/printk.h +1 -0
index 4217a9f412b2..11e49b299312 100644
@@ -154,6 +154,7 @@ int vprintk_emit(int facility, int level,
 
 asmlinkage __printf(1, 0)
 int vprintk(const char *fmt, va_list args);
+__printf(1, 0) int vprintk_deferred(const char *fmt, va_list args);
 
 asmlinkage __printf(1, 2) __cold
 int _printk(const char *fmt, ...);
include/linux/rv.h +7 -4
index 8883b41d88ec..5482651ed020 100644
@@ -38,7 +38,7 @@ union rv_task_monitor {
 struct rv_reactor {
 	const char		*name;
 	const char		*description;
-	void			(*react)(char *msg);
+	void			(*react)(const char *msg, ...);
 };
 #endif
 
@@ -49,9 +49,7 @@ struct rv_monitor {
 	int			(*enable)(void);
 	void			(*disable)(void);
 	void			(*reset)(void);
-#ifdef CONFIG_RV_REACTORS
-	void			(*react)(char *msg);
-#endif
+	void			(*react)(const char *msg, ...);
 };
 
 bool rv_monitoring_on(void);
@@ -64,6 +62,11 @@ void rv_put_task_monitor_slot(int slot);
 bool rv_reacting_on(void);
 int rv_unregister_reactor(struct rv_reactor *reactor);
 int rv_register_reactor(struct rv_reactor *reactor);
+#else
+static inline bool rv_reacting_on(void)
+{
+	return false;
+}
 #endif /* CONFIG_RV_REACTORS */
 
 #endif /* CONFIG_RV */
include/rv/da_monitor.h +10 -51

84 lines not on this page · raw message

kernel/printk/internal.h +0 -1
index a91bdf802967..28afdeb58412 100644
@@ -71,7 +71,6 @@ int vprintk_store(int facility, int level,
 		  const char *fmt, va_list args);
 
 __printf(1, 0) int vprintk_default(const char *fmt, va_list args);
-__printf(1, 0) int vprintk_deferred(const char *fmt, va_list args);
 
 void __printk_safe_enter(void);
 void __printk_safe_exit(void);
kernel/trace/rv/reactor_panic.c +1 -6
index 0186ff4cbd0b..4addabc9bcf1 100644
@@ -13,15 +13,10 @@
 #include <linux/init.h>
 #include <linux/rv.h>
 
-static void rv_panic_reaction(char *msg)
-{
-	panic(msg);
-}
-
 static struct rv_reactor rv_panic = {
 	.name = "panic",
 	.description = "panic the system if an exception is found.",
-	.react = rv_panic_reaction
+	.react = panic
 };
 
 static int __init register_react_panic(void)
kernel/trace/rv/reactor_printk.c +6 -2
index 178759dbf89f..a15db3fc8b82 100644
@@ -12,9 +12,13 @@
 #include <linux/init.h>
 #include <linux/rv.h>
 
-static void rv_printk_reaction(char *msg)
+static void rv_printk_reaction(const char *msg, ...)
 {
-	printk_deferred(msg);
+	va_list args;
+
+	va_start(args, msg);
+	vprintk_deferred(msg, args);
+	va_end(args);
 }
 
 static struct rv_reactor rv_printk = {
kernel/trace/rv/rv_reactors.c +1 -1
index 7b49cbe388d4..885661fe2b6e 100644
@@ -468,7 +468,7 @@ void reactor_cleanup_monitor(struct rv_monitor_def *mdef)
 /*
  * Nop reactor register
  */
-static void rv_nop_reaction(char *msg)
+static void rv_nop_reaction(const char *msg, ...)
 {
 }
 
Signature · 1 line
2.39.5
maintainer
#3 lore raw

Re: [PATCH 02/10] rv: Let the reactors take care of buffers

Quoted 109 lines · Nam Cao

Each RV monitor has one static buffer to send to the reactors. If multiple
errors are detected at the same time, the one buffer could be overwritten.

Instead, leave it to the reactors to handle buffering.

---
Cc: Petr Mladek <[email protected]>
Cc: John Ogness <[email protected]>
Cc: Sergey Senozhatsky <[email protected]>
---
 include/linux/printk.h           |  1 + include/linux/rv.h               | 11 +++--- include/rv/da_monitor.h          | 61 ++++++------------------------ --
 kernel/printk/internal.h         |  1 - kernel/trace/rv/reactor_panic.c  |  7 +--- kernel/trace/rv/reactor_printk.c |  8 +++-- kernel/trace/rv/rv_reactors.c    |  2 +-

 7 files changed, 26 insertions(+), 65 deletions(-)
include/rv/da_monitor.h +0 -0
index 510c88bfabd4..c55d45544a16 100644
@@ -16,58 +16,11 @@
  #include <linux/bug.h>
  #include <linux/sched.h>

include/rv/da_monitor.h +0 -3
-#ifdef CONFIG_RV_REACTORS
-
-#define DECLARE_RV_REACTING_HELPERS(name,

type) \
-static char
REACT_MSG_##name[1024]; \
-

												\

-static inline char *format_react_msg_##name(type curr_state, type
event) \
-
{ \

include/rv/da_monitor.h +0 -1
-	snprintf(REACT_MSG_##name,

1024, \

include/rv/da_monitor.h +0 -1
-		 "rv: monitor %s does not allow event %s on state

%s\n", \

include/rv/da_monitor.h +0 -1
-		

#name, \

include/rv/da_monitor.h +0 -1
-		

model_get_event_name_##name(event), \

include/rv/da_monitor.h +0 -1
-		

model_get_state_name_##name(curr_state)); \

include/rv/da_monitor.h +0 -1
-	return

REACT_MSG_##name; \
-
} \
-

												\

-static void cond_react_##name(char
*msg) \
-
{ \

include/rv/da_monitor.h +0 -1
-	if

(rv_##name.react) \
-

		rv_##name.react(msg);								\
-
}												\
-
												\

-static bool
rv_reacting_on_##name(void) \
-
{ \

include/rv/da_monitor.h +0 -1
-	return

rv_reacting_on(); \

include/rv/da_monitor.h +0 -5
-}
-
-#else /* CONFIG_RV_REACTOR */
-
-#define DECLARE_RV_REACTING_HELPERS(name,

type) \
-static inline char *format_react_msg_##name(type curr_state, type
event) \
-
{ \

include/rv/da_monitor.h +0 -1
-	return

NULL; \
-
} \
-

												\

-static void cond_react_##name(char
*msg) \
-
{ \
-

	return;											\
-
}												\
-
												\

-static bool
rv_reacting_on_##name(void) \
-
{ \

include/rv/da_monitor.h +0 -1
-	return

0; \

include/rv/da_monitor.h line 63

Quoted 3 lines
include/rv/da_monitor.h +0 -3
-}
-#endif
-

I don't think you need to remove those helper functions, why not just having format_react_msg_ prepare the arguments for react?

cond_react might be mildly useful also for ltl, we may think about putting it somewhere else and/or refactoring it a bit to include reacting_on (which is indeed global and doesn't require a per-monitor wrapper).

Quoted 104 lines

 /*
  * Generic helpers for all types of deterministic automata monitors. */
 #define DECLARE_DA_MON_GENERIC_HELPERS(name,
type) \

				\
-DECLARE_RV_REACTING_HELPERS(name,
type)								\
-
												\
 /*								
				\

  * da_monitor_reset_##name - reset a monitor and setting it to init
state \

*/ \

include/rv/da_monitor.h +0 -0
@@ -170,8 +123,11 @@ da_event_##name(struct da_monitor *da_mon, enum
 events_##name event)				\

  return
true; \
  }

				\
 								
				\
include/rv/da_monitor.h +0 -1
-	if

(rv_reacting_on_##name()) \
-

		cond_react_##name(format_react_msg_##name(curr_state, event));			\
include/rv/da_monitor.h +1 -0
+	if (rv_reacting_on() &&

rv_##name.react) \

include/rv/da_monitor.h +1 -0
+		rv_##name.react("rv: monitor %s does not allow event

%s on state %s\n", \

include/rv/da_monitor.h +1 -0
+				#name,				
				\
include/rv/da_monitor.h +1 -0
+				model_get_event_name_##name(event),
				\
include/rv/da_monitor.h +1 -0
+				model_get_state_name_##name(curr_sta

te)); \

				\
 	trace_error_##name(model_get_state_name_##name(curr_state),
				\


model_get_event_name_##name(event)); \

include/rv/da_monitor.h +0 -0
@@ -202,8 +158,11 @@ static inline bool da_event_##name(struct

da_monitor *da_mon, struct task_struct
  return
true; \
  }

				\
 								
				\
include/rv/da_monitor.h +0 -1
-	if

(rv_reacting_on_##name()) \
-

		cond_react_##name(format_react_msg_##name(curr_state, event));			\
include/rv/da_monitor.h +1 -0
+	if (rv_reacting_on() &&

rv_##name.react) \

include/rv/da_monitor.h +1 -0
+		rv_##name.react("rv: monitor %s does not allow event

%s on state %s\n", \

include/rv/da_monitor.h +1 -0
+				#name,				
				\
include/rv/da_monitor.h +1 -0
+				model_get_event_name_##name(event),
				\
include/rv/da_monitor.h +1 -0
+				model_get_state_name_##name(curr_sta

te)); \

				\

  trace_error_##name(tsk-

Quoted 1 line

pid, \


model_get_state_name_##name(curr_state), \

kernel/printk/internal.h +0 -0
index a91bdf802967..28afdeb58412 100644
@@ -71,7 +71,6 @@ int vprintk_store(int facility, int level,
  		  const char *fmt, va_list args);


 __printf(1, 0) int vprintk_default(const char *fmt, va_list args); -__printf(1, 0) int vprintk_deferred(const char *fmt, va_list args);
 void __printk_safe_enter(void);
 void __printk_safe_exit(void);

kernel/trace/rv/reactor_panic.c +0 -0
index 0186ff4cbd0b..4addabc9bcf1 100644
@@ -13,15 +13,10 @@
  #include <linux/init.h>
  #include <linux/rv.h>

kernel/trace/rv/reactor_panic.c +0 -5
-static void rv_panic_reaction(char *msg)
-{
-	panic(msg);
-}
-

 static struct rv_reactor rv_panic = {
  .name = "panic",
  .description = "panic the system if an exception is found.",

kernel/trace/rv/reactor_panic.c +1 -1
-	.react = rv_panic_reaction
+	.react = panic
Quoted 1 line

 };

For the sake of verbosity, I would still keep a wrapper function around panic, just to show directly from this file how should a react() function look like, as well as allowing future modifications, if needed. Not that the additional function call would be much of a problem during panic, I believe.

Good improvement overall, thanks.

Gabriele

Quoted 38 lines


 static int __init register_react_panic(void)

kernel/trace/rv/reactor_printk.c +0 -0
index 178759dbf89f..a15db3fc8b82 100644
@@ -12,9 +12,13 @@
  #include <linux/init.h>
  #include <linux/rv.h>

kernel/trace/rv/reactor_printk.c +1 -1
-static void rv_printk_reaction(char *msg)
+static void rv_printk_reaction(const char *msg, ...)

 {

kernel/trace/rv/reactor_printk.c +5 -1
-	printk_deferred(msg);
+	va_list args;
+
+	va_start(args, msg);
+	vprintk_deferred(msg, args);
+	va_end(args);

 }

 static struct rv_reactor rv_printk = {

kernel/trace/rv/rv_reactors.c +1 -1
index 7b49cbe388d4..885661fe2b6e 100644
@@ -468,7 +468,7 @@ void reactor_cleanup_monitor(struct
 rv_monitor_def *mdef)
  /*
   * Nop reactor register
   */
-static void rv_nop_reaction(char *msg)
+static void rv_nop_reaction(const char *msg, ...)
  {
  }

author
#4 lore raw

Re: [PATCH 02/10] rv: Let the reactors take care of buffers

Hi Gabriele,

Quoted 85 lines · Gabriele Monaco
Quoted 84 lines · Nam Cao
include/rv/da_monitor.h +0 -0
index 510c88bfabd4..c55d45544a16 100644
@@ -16,58 +16,11 @@
  #include <linux/bug.h>
  #include <linux/sched.h>

include/rv/da_monitor.h +0 -3
-#ifdef CONFIG_RV_REACTORS
-
-#define DECLARE_RV_REACTING_HELPERS(name,

type) \
-static char
REACT_MSG_##name[1024]; \
-

												\

-static inline char *format_react_msg_##name(type curr_state, type
event) \
-
{ \

include/rv/da_monitor.h +0 -1
-	snprintf(REACT_MSG_##name,

1024, \

include/rv/da_monitor.h +0 -1
-		 "rv: monitor %s does not allow event %s on state

%s\n", \

include/rv/da_monitor.h +0 -1
-		

#name, \

include/rv/da_monitor.h +0 -1
-		

model_get_event_name_##name(event), \

include/rv/da_monitor.h +0 -1
-		

model_get_state_name_##name(curr_state)); \

include/rv/da_monitor.h +0 -1
-	return

REACT_MSG_##name; \
-
} \
-

												\

-static void cond_react_##name(char
*msg) \
-
{ \

include/rv/da_monitor.h +0 -1
-	if

(rv_##name.react) \
-

		rv_##name.react(msg);								\
-
}												\
-
												\

-static bool
rv_reacting_on_##name(void) \
-
{ \

include/rv/da_monitor.h +0 -1
-	return

rv_reacting_on(); \

include/rv/da_monitor.h +0 -5
-}
-
-#else /* CONFIG_RV_REACTOR */
-
-#define DECLARE_RV_REACTING_HELPERS(name,

type) \
-static inline char *format_react_msg_##name(type curr_state, type
event) \
-
{ \

include/rv/da_monitor.h +0 -1
-	return

NULL; \
-
} \
-

												\

-static void cond_react_##name(char
*msg) \
-
{ \
-

	return;											\
-
}												\
-
												\

-static bool
rv_reacting_on_##name(void) \
-
{ \

Quoted 8 lines

include/rv/da_monitor.h

Quoted 5 lines
include/rv/da_monitor.h +0 -1
-	return

0; \

include/rv/da_monitor.h +0 -3
-}
-#endif
-

I don't think you need to remove those helper functions, why not just having format_react_msg_ prepare the arguments for react?

I'm not sure what you mean. Making format_react_msg_* a macro that is preprocessed into arguments? Then make cond_react_*() a variadic function, so that we can "pass" format_react_msg_* to it?

Going that way would also need a vreact() variant, as cond_react_*() cannot pass on the variadic arguments to react().

Instead, is it cleaner to do the below?

include/rv/da_monitor.h +10 -23
index 510c88bfabd4..e185ebf894a4 100644
@@ -19,22 +19,14 @@
 #ifdef CONFIG_RV_REACTORS
 
 #define DECLARE_RV_REACTING_HELPERS(name, type)							\
-static char REACT_MSG_##name[1024];								\
-												\
-static inline char *format_react_msg_##name(type curr_state, type event)			\
-{												\
-	snprintf(REACT_MSG_##name, 1024,							\
-		 "rv: monitor %s does not allow event %s on state %s\n",			\
-		 #name,										\
-		 model_get_event_name_##name(event),						\
-		 model_get_state_name_##name(curr_state));					\
-	return REACT_MSG_##name;								\
-}												\
-												\
-static void cond_react_##name(char *msg)							\
+static void cond_react_##name(type curr_state, type event)					\
 {												\
-	if (rv_##name.react)									\
-		rv_##name.react(msg);								\
+	if (!rv_##name.react)									\
+		return;										\
+	rv_##name.react("rv: monitor %s does not allow event %s on state %s\n",			\
+			#name,									\
+			model_get_event_name_##name(event),					\
+			model_get_state_name_##name(curr_state));				\
 }												\
 												\
 static bool rv_reacting_on_##name(void)								\
@@ -45,12 +37,7 @@ static bool rv_reacting_on_##name(void)								\
 #else /* CONFIG_RV_REACTOR */
 
 #define DECLARE_RV_REACTING_HELPERS(name, type)							\
-static inline char *format_react_msg_##name(type curr_state, type event)			\
-{												\
-	return NULL;										\
-}												\
-												\
-static void cond_react_##name(char *msg)							\
+static void cond_react_##name(type curr_state, type event)					\
 {												\
 	return;											\
 }												\
@@ -171,7 +158,7 @@ da_event_##name(struct da_monitor *da_mon, enum events_##name event)				\
 	}											\
 												\
 	if (rv_reacting_on_##name())								\
-		cond_react_##name(format_react_msg_##name(curr_state, event));			\
+		cond_react_##name(curr_state, event);						\
 												\
 	trace_error_##name(model_get_state_name_##name(curr_state),				\
 			   model_get_event_name_##name(event));					\
@@ -203,7 +190,7 @@ static inline bool da_event_##name(struct da_monitor *da_mon, struct task_struct
 	}											\
 												\
 	if (rv_reacting_on_##name())								\
-		cond_react_##name(format_react_msg_##name(curr_state, event));			\
+		cond_react_##name(curr_state, event);						\
 												\
 	trace_error_##name(tsk->pid,								\
 			   model_get_state_name_##name(curr_state),				\

Best regards,
Nam

maintainer
#5 lore raw

Re: [PATCH 02/10] rv: Let the reactors take care of buffers

Quoted 134 lines · Nam Cao

Hi Gabriele,

Quoted 117 lines · Gabriele Monaco
Quoted 116 lines · Nam Cao
include/rv/da_monitor.h +0 -0
index 510c88bfabd4..c55d45544a16 100644
@@ -16,58 +16,11 @@
  #include <linux/bug.h>
  #include <linux/sched.h>

include/rv/da_monitor.h +0 -3
-#ifdef CONFIG_RV_REACTORS
-
-#define DECLARE_RV_REACTING_HELPERS(name,

type) \
-static char

REACT_MSG_##name[1024];						
		\

-

				\

-static inline char *format_react_msg_##name(type curr_state, type
event) \
-

{								
				\
include/rv/da_monitor.h +0 -1
-	snprintf(REACT_MSG_##name,

1024, \

include/rv/da_monitor.h +0 -1
-		 "rv: monitor %s does not allow event %s on

state
%s\n", \

include/rv/da_monitor.h +0 -1
-		

#name,

		\
include/rv/da_monitor.h +0 -1
-		

model_get_event_name_##name(event),

		\
include/rv/da_monitor.h +0 -1
-		
model_get_state_name_##name(curr_state));			
		\
include/rv/da_monitor.h +0 -1
-	return
REACT_MSG_##name;						
		\
-
}								
				\

-

				\
-static void cond_react_##name(char
*msg)							\
-
{								
				\
include/rv/da_monitor.h +0 -1
-	if

(rv_##name.react)

			\
-
		rv_##name.react(msg);				
				\
-
}								
				\

-

				\
-static bool
rv_reacting_on_##name(void)					
			\
-
{								
				\
include/rv/da_monitor.h +0 -1
-	return
rv_reacting_on();						
		\
include/rv/da_monitor.h +0 -5
-}
-
-#else /* CONFIG_RV_REACTOR */
-
-#define DECLARE_RV_REACTING_HELPERS(name,

type) \
-static inline char *format_react_msg_##name(type curr_state, type
event) \
-

{								
				\
include/rv/da_monitor.h +0 -1
-	return
NULL;								
		\
-
}								
				\

-

				\
-static void cond_react_##name(char
*msg)							\
-
{								
				\
-
	return;						
					\
-
}								
				\

-

				\
-static bool
rv_reacting_on_##name(void)					
			\
-
{								
				\
include/rv/da_monitor.h +0 -1
-	return

0;

Quoted 8 lines

include/rv/da_monitor.h

Quoted 4 lines
		\
include/rv/da_monitor.h +0 -3
-}
-#endif
-

I don't think you need to remove those helper functions, why not just
having format_react_msg_ prepare the arguments for react?

I'm not sure what you mean. Making format_react_msg_* a macro that is preprocessed into arguments? Then make cond_react_*() a variadic function,
so that we can "pass" format_react_msg_* to it?

Quoted 5 lines

Going that way would also need a vreact() variant, as cond_react_*() cannot
pass on the variadic arguments to react().

Instead, is it cleaner to do the below?

Hi Nam,

you're right, I got a bit confused, all I meant was to find a way not to repeat the arguments for implicit and per-task monitors. What you propose seems perfect to me.

Also for the sake of simplifying things, a bit like you started, we could have the reacting_on() check inside cond_react and drop the per- monitor function. I believe the initial idea was to have a reacting_on toggle for each monitor but since it isn't the case, we don't really need it.

Thanks,
Gabriele

Quoted 128 lines
include/rv/da_monitor.h +0 -0
index 510c88bfabd4..e185ebf894a4 100644
@@ -19,22 +19,14 @@
  #ifdef CONFIG_RV_REACTORS


 #define DECLARE_RV_REACTING_HELPERS(name,
type) \
-static char
REACT_MSG_##name[1024]; \
-

												\

-static inline char *format_react_msg_##name(type curr_state, type
event) \
-
{ \

include/rv/da_monitor.h +0 -1
-	snprintf(REACT_MSG_##name,

1024, \

include/rv/da_monitor.h +0 -1
-		 "rv: monitor %s does not allow event %s on state

%s\n", \

include/rv/da_monitor.h +0 -1
-		

#name, \

include/rv/da_monitor.h +0 -1
-		

model_get_event_name_##name(event), \

include/rv/da_monitor.h +0 -1
-		

model_get_state_name_##name(curr_state)); \

include/rv/da_monitor.h +0 -1
-	return

REACT_MSG_##name; \
-
} \
-

												\

-static void cond_react_##name(char
*msg) \
+static void cond_react_##name(type curr_state, type
event) \

 {								
				\
include/rv/da_monitor.h +0 -1
-	if

(rv_##name.react) \
-

		rv_##name.react(msg);								\
include/rv/da_monitor.h +1 -0
+	if

(!rv_##name.react) \

include/rv/da_monitor.h +1 -0
+		return;					
					\
include/rv/da_monitor.h +1 -0
+	rv_##name.react("rv: monitor %s does not allow event %s on

state %s\n", \

include/rv/da_monitor.h +1 -0
+			#name,					
				\
include/rv/da_monitor.h +1 -0
+			model_get_event_name_##name(event),	
				\
include/rv/da_monitor.h +1 -0
+			model_get_state_name_##name(curr_state));
				\
 }								
				\
 								
				\

 static bool
rv_reacting_on_##name(void) \

include/rv/da_monitor.h +0 -0
@@ -45,12 +37,7 @@ static bool
 rv_reacting_on_##name(void)								\
  #else /* CONFIG_RV_REACTOR */


 #define DECLARE_RV_REACTING_HELPERS(name,
type) \
-static inline char *format_react_msg_##name(type curr_state, type
event) \
-
{ \

include/rv/da_monitor.h +0 -1
-	return

NULL; \
-
} \
-

												\

-static void cond_react_##name(char
*msg) \
+static void cond_react_##name(type curr_state, type
event) \

 {								
				\
 	return;						
					\
 }								
				\
include/rv/da_monitor.h +0 -0
@@ -171,7 +158,7 @@ da_event_##name(struct da_monitor *da_mon, enum
 events_##name event)				\
  	}							
 				\

				\
 	if
(rv_reacting_on_##name())								\
-
		cond_react_##name(format_react_msg_##name(curr_state, event));			\
include/rv/da_monitor.h +1 -0
+		cond_react_##name(curr_state,

event); \

				\
 	trace_error_##name(model_get_state_name_##name(curr_state),
				\


model_get_event_name_##name(event)); \

include/rv/da_monitor.h +0 -0
@@ -203,7 +190,7 @@ static inline bool da_event_##name(struct

da_monitor *da_mon, struct task_struct
  }

				\
 								
				\
 	if
(rv_reacting_on_##name())								\
-
		cond_react_##name(format_react_msg_##name(curr_state, event));			\
include/rv/da_monitor.h +1 -0
+		cond_react_##name(curr_state,

event); \

				\

  trace_error_##name(tsk-

Quoted 1 line

pid, \


model_get_state_name_##name(curr_state), \

Best regards,
Nam

#6 lore raw

Re: [PATCH 02/10] rv: Let the reactors take care of buffers

In reply to Nam Cao
kernel test robot noticed the following build warnings:

Hi Nam,

kernel test robot noticed the following build warnings:

[auto build test WARNING on trace/for-next]
[also build test WARNING on tip/x86/core tip/x86/mm linus/master v6.14-rc6 next-20250313] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Nam-Cao/rv-Add-undef-TRACE_INCLUDE_FILE/20250312-011043
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/90868f1dd49680ec63c961ec8c72ceb64f1af091.1741708239.git.namcao%40linutronix.de
patch subject: [PATCH 02/10] rv: Let the reactors take care of buffers
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20250313/[email protected]/config)
compiler: sh4-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250313/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> kernel/trace/rv/reactor_panic.c:19:18: warning: initialization left-hand side might be a candidate for a format attribute [-Wsuggest-attribute=format]

19 | .react = panic
| ^~~~~

vim +19 kernel/trace/rv/reactor_panic.c

    15	
    16	static struct rv_reactor rv_panic = {
    17		.name = "panic",
    18		.description = "panic the system if an exception is found.",
  > 19		.react = panic
    20	};
    21	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#7 lore raw

Re: [PATCH 02/10] rv: Let the reactors take care of buffers

In reply to Nam Cao
kernel test robot noticed the following build errors:

Hi Nam,

kernel test robot noticed the following build errors:

[auto build test ERROR on trace/for-next]
[also build test ERROR on tip/x86/core tip/x86/mm linus/master v6.14-rc6 next-20250313] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Nam-Cao/rv-Add-undef-TRACE_INCLUDE_FILE/20250312-011043
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/90868f1dd49680ec63c961ec8c72ceb64f1af091.1741708239.git.namcao%40linutronix.de
patch subject: [PATCH 02/10] rv: Let the reactors take care of buffers
config: parisc-randconfig-002-20250313 (https://download.01.org/0day-ci/archive/20250313/[email protected]/config)
compiler: hppa-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250313/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/trace/rv/reactor_printk.c: In function 'rv_printk_reaction':

>> kernel/trace/rv/reactor_printk.c:20:9: error: implicit declaration of function 'vprintk_deferred'; did you mean '_printk_deferred'? [-Wimplicit-function-declaration]
      20 |         vprintk_deferred(msg, args);
         |         ^~~~~~~~~~~~~~~~
         |         _printk_deferred

vim +20 kernel/trace/rv/reactor_printk.c

    14	
    15	static void rv_printk_reaction(const char *msg, ...)
    16	{
    17		va_list args;
    18	
    19		va_start(args, msg);
  > 20		vprintk_deferred(msg, args);
    21		va_end(args);
    22	}
    23	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#8 lore raw

PATCH 3/10 Add infrastructure for linear temporal logic monitor

In reply to Nam Cao

Prepare the infrastructure for linear temporal logic monitors:

- add scripts for generating the monitors
- implement data structures

For details, see:
Documentation/trace/rv/linear_temporal_logic.rst

---
 .../trace/rv/linear_temporal_logic.rst        |  73 +++
 include/linux/rv.h                            |  26 +-
 kernel/fork.c                                 |   5 +-
 tools/verification/ltl2ba/.gitignore          |   3 +
 tools/verification/ltl2ba/generate.py         | 154 +++++
 tools/verification/ltl2ba/ltl.py              | 556 ++++++++++++++++++
 tools/verification/ltl2ba/template.c          | 131 +++++
 tools/verification/ltl2ba/template.h          | 157 +++++
 8 files changed, 1097 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/trace/rv/linear_temporal_logic.rst
 create mode 100644 tools/verification/ltl2ba/.gitignore
 create mode 100755 tools/verification/ltl2ba/generate.py
 create mode 100644 tools/verification/ltl2ba/ltl.py
 create mode 100644 tools/verification/ltl2ba/template.c
 create mode 100644 tools/verification/ltl2ba/template.h
Documentation/trace/rv/linear_temporal_logic.rst new file +73 -0
new file mode 100644
index 000000000000..9b0ce6a143ec
@@ -0,0 +1,73 @@
+Introduction
+============
+
+Deterministic automaton runtime verification monitor is a verification technique which checks that
+the kernel follows a specification in the form of deterministic automaton (DA). It does so by using
+tracepoints to monitor the kernel's execution trace, and verifying that the execution trace
+sastifies the specification.
+
+However, while attempting to implement DA monitors for some complex specifications, deterministic
+automaton is found to be inappropriate as the specification language. The automaton is complicated,
+hard to understand, and error-prone.
+
+Thus, RV monitors based on linear temporal logic (LTL for short) are introduced. This type of
+monitor uses LTL as specification, instead of DA. For some cases, writing the specification as LTL
+is more concise and intuitive.
+
+Documents regarding LTL are widely available on the internet, this document will not go into
+details.
+
+Grammar
+========
+
+Unlike some existing syntax, kernel's implementation of LTL is more verbose. This is motivated by
+considering that the people who read the LTL specifications may not be well-versed in LTL.
+
+Grammar:
+    ltl ::= opd | ( ltl ) | ltl binop ltl | unop ltl
+
+Operands (opd):
+    true, false, user-defined names consisting of upper-case characters, digits, and underscore.
+
+Unary Operators (unop):
+    always
+    eventually
+    not
+
+Binary Operators (binop):
+    until
+    and
+    or
+    imply
+    equivalent
+
+This grammar is ambiguous: operator precedence is not defined. Parentheses must be used.
+
+Monitor synthesis
+=================
+
+To synthesize an LTL into a kernel monitor, conversion scripts are provided:
+`tools/verification/ltl2ba`. The specification needs to be provided as a file, and it must have a
+"RULE = LTL" assignment, which specifies the LTL property to verify. For example:
+
+   .. code-block::
+
+    RULE = always (ACQUIRE imply ((not KILLED and not CRASHED) until RELEASE))
+
+The LTL can be broken down if required using sub-expressions. For example, the above is equivalent
+to:
+
+   .. code-block::
+
+    RULE = always (ACQUIRE imply (ALIVE until RELEASE))
+    ALIVE = not KILLED and not CRASHED
+
+The ltl file can be converted into C code:
+
+   .. code-block::
+
+    .tools/verification/ltl2ba/generate.py -l <ltl file> -n <model name> -o <output diretory>
+
+The above command generates `ba.c` and `ba.h`, the Buchi automaton that verifies the LTL property.
+The Buchi automaton needs to be manually glued to the kernel. Please see the comments in `ba.h` for
+further details.
include/linux/rv.h +22 -4
index 5482651ed020..6de4f93b390e 100644
@@ -10,6 +10,8 @@
 #define MAX_DA_NAME_LEN	24
 
 #ifdef CONFIG_RV
+#include <linux/types.h>
+
 /*
  * Deterministic automaton per-object variables.
  */
@@ -18,6 +20,24 @@ struct da_monitor {
 	unsigned int	curr_state;
 };
 
+enum ltl_truth_value {
+	LTL_FALSE,
+	LTL_TRUE,
+	LTL_UNDETERMINED,
+};
+
+/*
+ * In the future, if the number of atomic propositions or the custom data size is larger, we can
+ * switch to dynamic allocation. For now, the code is simpler this way.
+ */
+#define RV_MAX_LTL_ATOM 10
+#define RV_MAX_DATA_SIZE 16
+struct ltl_monitor {
+	unsigned int		state;
+	enum ltl_truth_value	atoms[RV_MAX_LTL_ATOM];
+	u8			data[RV_MAX_DATA_SIZE];
+};
+
 /*
  * Per-task RV monitors count. Nowadays fixed in RV_PER_TASK_MONITORS.
  * If we find justification for more monitors, we can think about
@@ -27,11 +47,9 @@ struct da_monitor {
 #define RV_PER_TASK_MONITORS		1
 #define RV_PER_TASK_MONITOR_INIT	(RV_PER_TASK_MONITORS)
 
-/*
- * Futher monitor types are expected, so make this a union.
- */
 union rv_task_monitor {
-	struct da_monitor da_mon;
+	struct da_monitor	da_mon;
+	struct ltl_monitor	ltl_mon;
 };
 
 #ifdef CONFIG_RV_REACTORS
kernel/fork.c +1 -4
index 735405a9c5f3..5d6c1caca702 100644
@@ -2127,10 +2127,7 @@ static void copy_oom_score_adj(u64 clone_flags, struct task_struct *tsk)
 #ifdef CONFIG_RV
 static void rv_task_fork(struct task_struct *p)
 {
-	int i;
-
-	for (i = 0; i < RV_PER_TASK_MONITORS; i++)
-		p->rv[i].da_mon.monitoring = false;
+	memset(p->rv, 0, sizeof(p->rv));
 }
 #else
 #define rv_task_fork(p) do {} while (0)
tools/verification/ltl2ba/.gitignore new file +3 -0
new file mode 100644
index 000000000000..9c47b9724860
@@ -0,0 +1,3 @@
+__pycache__/
+parsetab.py
+parser.out
tools/verification/ltl2ba/generate.py new file +154 -0

154 lines not on this page · raw message

tools/verification/ltl2ba/ltl.py new file +556 -0

556 lines not on this page · raw message

tools/verification/ltl2ba/template.c new file +131 -0

131 lines not on this page · raw message

tools/verification/ltl2ba/template.h new file +157 -0

157 lines not on this page · raw message

Signature · 1 line
2.39.5
maintainer
#9 lore raw

Re: [PATCH 03/10] rv: Add infrastructure for linear temporal logic monitor

Quoted 10 lines · Nam Cao

Prepare the infrastructure for linear temporal logic monitors:

  - add scripts for generating the monitors
  - implement data structures

For details, see:
Documentation/trace/rv/linear_temporal_logic.rst

---

Thanks for this. It's great to have a new type of monitor! I'll play with this a bit more, but please see my initial comments.

Quoted 78 lines

 .../trace/rv/linear_temporal_logic.rst        |  73 +++ include/linux/rv.h                            |  26 +- kernel/fork.c                                 |   5 +- tools/verification/ltl2ba/.gitignore          |   3 + tools/verification/ltl2ba/generate.py         | 154 +++++ tools/verification/ltl2ba/ltl.py              | 556
++++++++++++++++++
 tools/verification/ltl2ba/template.c          | 131 +++++ tools/verification/ltl2ba/template.h          | 157 +++++

 8 files changed, 1097 insertions(+), 8 deletions(-)

 create mode 100644 Documentation/trace/rv/linear_temporal_logic.rst create mode 100644 tools/verification/ltl2ba/.gitignore create mode 100755 tools/verification/ltl2ba/generate.py create mode 100644 tools/verification/ltl2ba/ltl.py create mode 100644 tools/verification/ltl2ba/template.c create mode 100644 tools/verification/ltl2ba/template.h

Documentation/trace/rv/linear_temporal_logic.rst new file +0 -0
new file mode 100644
index 000000000000..9b0ce6a143ec
 
[...]
include/linux/rv.h +0 -0
index 5482651ed020..6de4f93b390e 100644
@@ -10,6 +10,8 @@
  #define MAX_DA_NAME_LEN	24


 #ifdef CONFIG_RV

include/linux/rv.h +2 -0
+#include <linux/types.h>
+

 /*
  * Deterministic automaton per-object variables.
  */

include/linux/rv.h +0 -0
@@ -18,6 +20,24 @@ struct da_monitor {
  	unsigned int	curr_state;
  };

include/linux/rv.h +8 -0
+enum ltl_truth_value {
+	LTL_FALSE,
+	LTL_TRUE,
+	LTL_UNDETERMINED,
+};
+
+/*
+ * In the future, if the number of atomic propositions or the custom

data size is larger, we can
+ * switch to dynamic allocation. For now, the code is simpler this way.
+ */

include/linux/rv.h +8 -0
+#define RV_MAX_LTL_ATOM 10
+#define RV_MAX_DATA_SIZE 16
+struct ltl_monitor {
+	unsigned int		state;
+	enum ltl_truth_value	atoms[RV_MAX_LTL_ATOM];
+	u8			data[RV_MAX_DATA_SIZE];
+};
+

 /*
  * Per-task RV monitors count. Nowadays fixed in RV_PER_TASK_MONITORS.
  * If we find justification for more monitors, we can think about

include/linux/rv.h +0 -0
@@ -27,11 +47,9 @@ struct da_monitor {
  #define RV_PER_TASK_MONITORS		1
  #define RV_PER_TASK_MONITOR_INIT	(RV_PER_TASK_MONITORS)


-/*
- * Futher monitor types are expected, so make this a union.
- */
 union rv_task_monitor {

include/linux/rv.h +2 -1
-	struct da_monitor da_mon;
+	struct da_monitor	da_mon;
+	struct ltl_monitor	ltl_mon;
Quoted 1 line

 };

This adds quite some memory overhead if we have multiple per-task monitors (we might in the future) and we don't use this ltl monitors. What about keeping it conditionally compiled out?
You could define the struct only if e.g. CONFIG_RV_LTL_MONITORS is set, select it with any LTL monitor via Kconfig, then glue it somehow to have it readable.

tools/verification/ltl2ba/generate.py

Quoted 35 lines


 #ifdef CONFIG_RV_REACTORS

kernel/fork.c +1 -4
index 735405a9c5f3..5d6c1caca702 100644
@@ -2127,10 +2127,7 @@ static void copy_oom_score_adj(u64
 clone_flags, struct task_struct *tsk)
  #ifdef CONFIG_RV
  static void rv_task_fork(struct task_struct *p)
  {
-	int i;
-
-	for (i = 0; i < RV_PER_TASK_MONITORS; i++)
-		p->rv[i].da_mon.monitoring = false;
+	memset(p->rv, 0, sizeof(p->rv));
  }
  #else

 #define rv_task_fork(p) do {} while (0)

tools/verification/ltl2ba/.gitignore new file +3 -0
new file mode 100644
index 000000000000..9c47b9724860
@@ -0,0 +1,3 @@
+__pycache__/
+parsetab.py
+parser.out
tools/verification/ltl2ba/generate.py new file +0 -0
new file mode 100755
index 000000000000..52d5b3618e64

You may want to rename this script to something more unique, just in case one day we decide to make it possible to install this generator on the system (like dot2k/dot2c).

Quoted 220 lines
tools/verification/ltl2ba/ltl.py new file +0 -0
new file mode 100644
index 000000000000..aa3a11d78a8e
tools/verification/ltl2ba/template.c new file +0 -0
new file mode 100644
index 000000000000..31c5a209d71d
tools/verification/ltl2ba/template.h new file +5 -0
new file mode 100644
index 000000000000..65d342891e70
@@ -0,0 +1,157 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * This file is generated, do not edit.
+ *
+ * This file includes necessary functions to glue the Buchi

automaton and the kernel together.
+ * Some of these functions must be manually implemented (look for "Must be implemented", or just
+ * let the compiler tells you).
+ *
+ * Essentially, you need to manually define the meaning of the atomic propositions in the LTL
+ * property. The primary function for that is rv_%%MODEL_NAME%%_atom_update(), which can be called
+ * in tracepoints' handlers for example. In some specific cases where
+ * rv_%%MODEL_NAME%%_atom_update() is not convenient, rv_%%MODEL_NAME%%_atoms_fetch() can be used.
+ *
+ * rv_%%MODEL_NAME%%_init()/rv_%%MODEL_NAME%%_destroy() must be called while enabling/disabling
+ * the monitor.
+ *
+ * If the fields in struct ltl_monitor is not enough, extra custom data can be used. See
+ * rv_%%MODEL_NAME%%_get_data().
+ */

tools/verification/ltl2ba/template.h new file +29 -0
+
+#include <linux/sched.h>
+
+enum %%MODEL_NAME%%_atom {
+%%ATOM_LIST%%
+	NUM_ATOM
+};
+
+/**
+ * rv_%%MODEL_NAME%%_init
+ * @data_size:	required custom data size, can be zero
+ *
+ * Must be called while enabling the monitor
+ */
+int rv_%%MODEL_NAME%%_init(size_t data_size);
+
+/**
+ * rv_%%MODEL_NAME%%_destroy
+ *
+ * must be called while disabling the monitor
+ */
+void rv_%%MODEL_NAME%%_destroy(void);
+
+/**
+ * rv_%%MODEL_NAME%%_error - report violation of the LTL property
+ * @task:	the task violating the LTL property
+ * @mon:	the LTL monitor
+ *
+ * Must be implemented. This function should invoke the RV reactor

and the monitor's tracepoints.
+ */
+void rv_%%MODEL_NAME%%_error(struct task_struct *task, struct ltl_monitor *mon);

tools/verification/ltl2ba/template.h new file +5 -0
+
+extern int rv_%%MODEL_NAME%%_task_slot;
+
+/**
+ * rv_%%MODEL_NAME%%_get_monitor - get the struct ltl_monitor of a

task
+ */
+static inline struct ltl_monitor *rv_%%MODEL_NAME%%_get_monitor(struct task_struct *task)

tools/verification/ltl2ba/template.h new file +10 -0
+{
+	return &task->rv[rv_%%MODEL_NAME%%_task_slot].ltl_mon;
+}
+
+/**
+ * rv_%%MODEL_NAME%%_atoms_init - initialize the atomic propositions
+ * @task:	the task
+ * @mon:	the LTL monitor
+ *
+ * Must be implemented. This function is called during task

creation, and should initialize all
+ * atomic propositions. rv_%%MODEL_NAME%%_atom_set() should be used to implement this function.
+ *
+ * This function does not have to initialize atomic propositions that are updated by
+ * rv_%%MODEL_NAME%%_atoms_fetch(), because the two functions are called together.
+ */
+void rv_%%MODEL_NAME%%_atoms_init(struct task_struct *task, struct ltl_monitor *mon);

tools/verification/ltl2ba/template.h new file +7 -0
+
+/**
+ * rv_%%MODEL_NAME%%_atoms_fetch - fetch the atomic propositions
+ * @task:	the task
+ * @mon:	the LTL monitor
+ *
+ * Must be implemented. This function is called anytime the Buchi

automaton is triggered. Its
+ * intended purpose is to update the atomic propositions which are expensive to trace and can be
+ * easily read from @task. rv_%%MODEL_NAME%%_atom_set() should be used to implement this function.
+ *
+ * Using this function may cause incorrect verification result if it is important for the LTL that
+ * the atomic propositions must be updated at the correct time. Therefore, if it is possible,
+ * updating atomic propositions should be done with rv_%%MODEL_NAME%%_atom_update() instead.
+ *
+ * An example where this function is useful is with the LTL property:
+ *    always (RT imply not PAGEFAULT)
+ * (a realtime task does not raise page faults)
+ *
+ * In this example, adding tracepoints to track RT is complicated, because it is changed in
+ * differrent places (mutex's priority boosting, sched_setscheduler). Furthermore, for this LTL
+ * property, we don't care exactly when RT changes, as long as we have its correct value when
+ * PAGEFAULT==true. Therefore, it is better to update RT in rv_%%MODEL_NAME%%_atoms_fetch(), as it
+ * can easily be retrieved from task_struct.
+ *
+ * This function can be empty.
+ */
+void rv_%%MODEL_NAME%%_atoms_fetch(struct task_struct *task, struct ltl_monitor *mon);

tools/verification/ltl2ba/template.h new file +5 -0
+
+/**
+ * rv_%%MODEL_NAME%%_atom_update - update an atomic proposition
+ * @task:	the task
+ * @atom:	the atomic proposition, one of enum

%%MODEL_NAME%%_atom
+ * @value: the new value for @atom
+ *
+ * Update an atomic proposition and trigger the Buchi atomaton to check for violation of the LTL
+ * property. This function can be called in tracepoints' handler, for example.
+ */
+void rv_%%MODEL_NAME%%_atom_update(struct task_struct *task, unsigned int atom, bool value);

tools/verification/ltl2ba/template.h new file +5 -0
+
+/**
+ * rv_%%MODEL_NAME%%_atom_get - get an atomic proposition
+ * @mon:	the monitor
+ * @atom:	the atomic proposition, one of enum

%%MODEL_NAME%%_atom
+ *
+ * Returns the value of an atomic proposition.
+ */

tools/verification/ltl2ba/template.h new file +2 -0
+static inline
+enum ltl_truth_value rv_%%MODEL_NAME%%_atom_get(struct ltl_monitor

*mon, unsigned int atom)

tools/verification/ltl2ba/template.h new file +8 -0
+{
+	return mon->atoms[atom];
+}
+
+/**
+ * rv_%%MODEL_NAME%%_atom_set - set an atomic proposition
+ * @mon:	the monitor
+ * @atom:	the atomic proposition, one of enum

%%MODEL_NAME%%_atom
+ * @value: the new value for @atom
+ *
+ * Update an atomic proposition without triggering the Buchi automaton. This can be useful to
+ * implement rv_%%MODEL_NAME%%_atoms_fetch() and rv_%%MODEL_NAME%%_atoms_init().
+ *
+ * Another use case for this function is when multiple atomic propositions change at the same time,
+ * because calling rv_%%MODEL_NAME%%_atom_update() (and thus triggering the Buchi automaton)
+ * multiple times may be incorrect. In that case, rv_%%MODEL_NAME%%_atom_set() can be used to avoid
+ * triggering the Buchi automaton, and rv_%%MODEL_NAME%%_atom_update() is only used for the last
+ * atomic proposition.
+ */

tools/verification/ltl2ba/template.h new file +2 -0
+static inline
+void rv_%%MODEL_NAME%%_atom_set(struct ltl_monitor *mon, unsigned

int atom, bool value)

tools/verification/ltl2ba/template.h new file +9 -0
+{
+	mon->atoms[atom] = value;
+}
+
+/**
+ * rv_%%MODEL_NAME%%_get_data - get the custom data of this monitor.
+ * @mon: the monitor
+ *
+ * If this function is used, rv_%%MODEL_NAME%%_init() must have been

called with a positive
+ * data_size.
+ */
+static inline void *rv_%%MODEL_NAME%%_get_data(struct ltl_monitor *mon)

tools/verification/ltl2ba/template.h line 157

Quoted 3 lines
tools/verification/ltl2ba/template.h new file +3 -0
+{
+	return &mon->data;
+}

Do we need all those functions defined here? We could have them generated by the pre-processor just like with DA monitors.

Having a ltl_monitor.h defining all common utility functions and variables (here I'm assuming most are, indeed, common) may save a lot of maintenance.

Also I would rearrange monitors sources a bit differently, like putting everything that doesn't need manual intervention (states and atoms lists) in the header, remaining functions that may need tracepoint wiring or more complex stuff can stay in a single c file, a bit like current da monitors.

I see there seems to be more manual work in the main C file (e.g. rtapp_block.c), but I think it would look cleaner if all other code was generated by the preprocessor in a common header and all lists/inlined functions created by the script stay in a separate header (why not call it rtapp_block.h?).

What do you think?

Thanks,
Gabriele

maintainer
#10 lore raw

Re: [PATCH 03/10] rv: Add infrastructure for linear temporal logic monitor

Quoted 15 lines · On Wed, 12 Mar 2025 07:47:50 +0100 Gabriele Monaco <[email protected]> wrote:

-/*

Quoted 7 lines

- * Futher monitor types are expected, so make this a union.
- */
 union rv_task_monitor {

include/linux/rv.h +2 -1
-	struct da_monitor da_mon;
+	struct da_monitor	da_mon;
+	struct ltl_monitor	ltl_mon;

 };

This adds quite some memory overhead if we have multiple per-task monitors (we might in the future) and we don't use this ltl monitors. What about keeping it conditionally compiled out?
You could define the struct only if e.g. CONFIG_RV_LTL_MONITORS is set, select it with any LTL monitor via Kconfig, then glue it somehow to have it readable.

One thing to do if you compile it out, make it a stub structure, so you don't need to add #ifdef into the union.

struct ltl_monitor { int unused; };

Or something like that.

-- Steve

author
#11 lore raw

Re: [PATCH 03/10] rv: Add infrastructure for linear temporal logic monitor

In reply to Gabriele Monaco
Quoted 16 lines · Gabriele Monaco
Quoted 8 lines · Nam Cao

-/*
- * Futher monitor types are expected, so make this a union.
- */
 union rv_task_monitor {

include/linux/rv.h +2 -1
-	struct da_monitor da_mon;
+	struct da_monitor	da_mon;
+	struct ltl_monitor	ltl_mon;

 };

This adds quite some memory overhead if we have multiple per-task monitors (we might in the future) and we don't use this ltl monitors. What about keeping it conditionally compiled out?
You could define the struct only if e.g. CONFIG_RV_LTL_MONITORS is set, select it with any LTL monitor via Kconfig, then glue it somehow to have it readable.

Good point.

Quoted 10 lines

tools/verification/ltl2ba/generate.py

Quoted 6 lines
tools/verification/ltl2ba/generate.py new file +0 -0
new file mode 100755
index 000000000000..52d5b3618e64

You may want to rename this script to something more unique, just in case one day we decide to make it possible to install this generator on the system (like dot2k/dot2c).

Acked. Inspired by the dot2k name, maybe something like ltl2k.

Quoted 16 lines

tools/verification/ltl2ba/generate.py

Quoted 12 lines

+ * rv_%%MODEL_NAME%%_get_data - get the custom data of this monitor.
+ * @mon: the monitor
+ *
+ * If this function is used, rv_%%MODEL_NAME%%_init() must have been called with a positive
+ * data_size.
+ */
+static inline void *rv_%%MODEL_NAME%%_get_data(struct ltl_monitor *mon)

tools/verification/ltl2ba/generate.py new file +3 -0
+{
+	return &mon->data;
+}

Do we need all those functions defined here? We could have them generated by the pre-processor just like with DA monitors.

Quoted 3 lines

Having a ltl_monitor.h defining all common utility functions and variables (here I'm assuming most are, indeed, common) may save a lot of maintenance.

I avoided macros like with DA monitors, they are hard to grep. But your point on maintenance is true from my experience working on this series..

Pre-processor it is then.

Quoted 6 lines

Also I would rearrange monitors sources a bit differently, like putting everything that doesn't need manual intervention (states and atoms lists) in the header, remaining functions that may need tracepoint wiring or more complex stuff can stay in a single c file, a bit like current da monitors.

Quoted 7 lines

I see there seems to be more manual work in the main C file (e.g. rtapp_block.c), but I think it would look cleaner if all other code was generated by the preprocessor in a common header and all lists/inlined functions created by the script stay in a separate header (why not call it rtapp_block.h?).

What do you think?

Sounds better, let me give me a try.

Thanks so much for the suggestions,
Nam

author
#12 lore raw

PATCH 4/10 Add rtapp_block monitor

In reply to Nam Cao

Add an RV monitor to detect realtime tasks getting blocked. For the full description, see Documentation/trace/rv/monitor_rtapp_block.rst.

---
Cc: Peter Zijlstra <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Waiman Long <[email protected]>

---
 .../trace/rv/monitor_rtapp_block.rst          |  34 +++
 include/trace/events/lock.h                   |  12 +
 kernel/locking/rtmutex.c                      |   4 +
 kernel/trace/rv/Kconfig                       |  12 +-
 kernel/trace/rv/Makefile                      |   2 +
 kernel/trace/rv/monitors/rtapp_block/ba.c     | 146 +++++++++++
 kernel/trace/rv/monitors/rtapp_block/ba.h     | 166 +++++++++++++
 kernel/trace/rv/monitors/rtapp_block/ltl      |   9 +
 .../rv/monitors/rtapp_block/rtapp_block.c     | 232 ++++++++++++++++++
 kernel/trace/rv/rv_trace.h                    |  44 ++++
 lib/Kconfig.debug                             |   3 +
 11 files changed, 663 insertions(+), 1 deletion(-)
 create mode 100644 Documentation/trace/rv/monitor_rtapp_block.rst
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/ba.c
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/ba.h
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/ltl
 create mode 100644 kernel/trace/rv/monitors/rtapp_block/rtapp_block.c
Documentation/trace/rv/monitor_rtapp_block.rst new file +34 -0
new file mode 100644
index 000000000000..9cabbe66fa4a
@@ -0,0 +1,34 @@
+Monitor rtapp_block
+=======================
+
+- Name: rtapp_block - real time applications are undesirably blocked
+- Type: per-task linear temporal logic monitor
+- Author: Nam Cao <[email protected]>
+
+Introduction
+------------
+
+Real time threads could be blocked and fail to finish their execution timely. For instance, they
+need to access shared resources which are already acquired by other threads. Or they could be
+waiting for non-realtime threads to signal them to proceed: as the non-realtime threads are not
+prioritized by the scheduler, the execution of realtime threads could be delayed indefinitely.
+These scenarios are often unintentional, and cause unexpected latency to the realtime application.
+
+The rtapp_block monitor reports this type of scenario, by monitoring for:
+
+  * Realtime threads going to sleep without explicitly asking for it (namely, with nanosleep
+    syscall).
+  * Realtime threads are woken up by non-realtime threads.
+
+How to fix the monitor's warnings?
+----------------------------------
+
+There is no single answer, the solution needs to be evaluated depending on the specific cases.
+
+If the realtime thread is blocked trying to take a `pthread_mutex_t` which is already taken by a
+non-realtime thread, the solution could be enabling priority inheritance for the mutex, so that the
+blocking non-realtime thread would be priority-boosted to run at realtime priority.
+
+If realtime thread needs to wait for non-realtime thread to signal it to proceed, perhaps the design
+needs to be reconsidered to remove this dependency. Often, the work executed by the realtime thread
+needs not to be realtime at all.
include/trace/events/lock.h +12 -0
index 8e89baa3775f..d4b32194d47f 100644
@@ -138,6 +138,18 @@ TRACE_EVENT(contention_end,
 	TP_printk("%p (ret=%d)", __entry->lock_addr, __entry->ret)
 );
 
+#ifdef CONFIG_TRACE_RT_MUTEX_WAKE_WAITER
+DECLARE_TRACE(rt_mutex_wake_waiter_begin,
+		TP_PROTO(struct task_struct *task),
+		TP_ARGS(task))
+DECLARE_TRACE(rt_mutex_wake_waiter_end,
+		TP_PROTO(struct task_struct *task),
+		TP_ARGS(task))
+#else
+#define trace_rt_mutex_wake_waiter_begin(...)
+#define trace_rt_mutex_wake_waiter_end(...)
+#endif /* CONFIG_TRACE_RT_MUTEX */
+
 #endif /* _TRACE_LOCK_H */
 
 /* This part must be outside protection */
kernel/locking/rtmutex.c +4 -0
index 4a8df1800cbb..fc9cf4a2cf75 100644
@@ -561,6 +561,8 @@ static __always_inline void rt_mutex_wake_q_add(struct rt_wake_q_head *wqh,
 
 static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
 {
+	trace_rt_mutex_wake_waiter_begin(current);
+
 	if (IS_ENABLED(CONFIG_PREEMPT_RT) && wqh->rtlock_task) {
 		wake_up_state(wqh->rtlock_task, TASK_RTLOCK_WAIT);
 		put_task_struct(wqh->rtlock_task);
@@ -572,6 +574,8 @@ static __always_inline void rt_mutex_wake_up_q(struct rt_wake_q_head *wqh)
 
 	/* Pairs with preempt_disable() in mark_wakeup_next_waiter() */
 	preempt_enable();
+
+	trace_rt_mutex_wake_waiter_end(current);
 }
 
 /*
kernel/trace/rv/Kconfig +11 -1
index 8226352a0062..d65bf9bda2f2 100644
@@ -13,7 +13,7 @@ config DA_MON_EVENTS_ID
 
 menuconfig RV
 	bool "Runtime Verification"
-	depends on TRACING
+	select TRACING
 	help
 	  Enable the kernel runtime verification infrastructure. RV is a
 	  lightweight (yet rigorous) method that complements classical
@@ -29,6 +29,16 @@ source "kernel/trace/rv/monitors/wip/Kconfig"
 source "kernel/trace/rv/monitors/wwnr/Kconfig"
 # Add new monitors here
 
+config RV_MON_RTAPP_BLOCK
+	depends on RV
+	select DA_MON_EVENTS
+	select TRACE_IRQFLAGS
+	select TRACE_RT_MUTEX_WAKE_WAITER
+	bool "rtapp_block monitor"
+	help
+	  Enable rtapp_wakeup which monitors that realtime tasks are not blocked.
+	  For details, see Documentation/trace/rv/monitor_rtapp_block.rst.
+
 config RV_REACTORS
 	bool "Runtime verification reactors"
 	default y
kernel/trace/rv/Makefile +2 -0
index 188b64668e1f..6570a3116127 100644
@@ -5,6 +5,8 @@ ccflags-y += -I $(src)		# needed for trace events
 obj-$(CONFIG_RV) += rv.o
 obj-$(CONFIG_RV_MON_WIP) += monitors/wip/wip.o
 obj-$(CONFIG_RV_MON_WWNR) += monitors/wwnr/wwnr.o
+obj-$(CONFIG_RV_MON_RTAPP_BLOCK) += monitors/rtapp_block/ba.o \
+				    monitors/rtapp_block/rtapp_block.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/rtapp_block/ba.c new file +146 -0

146 lines not on this page · raw message

kernel/trace/rv/monitors/rtapp_block/ba.h new file +166 -0

166 lines not on this page · raw message

kernel/trace/rv/monitors/rtapp_block/ltl new file +9 -0
new file mode 100644
index 000000000000..781f0144a222
@@ -0,0 +1,9 @@
+RULE = always (WAKEUP_RT_TASK imply (RT or WAKEUP_WHITELIST))
+   and always ((USER_TASK and RT) imply (SLEEP imply INTENTIONAL_SLEEP))
+
+INTENTIONAL_SLEEP = DO_NANOSLEEP or FUTEX_LOCK_WITH_PI
+
+WAKEUP_WHITELIST = RT_MUTEX_WAKING_WAITER
+                or STOPPING_WOKEN_TASK
+                or WOKEN_TASK_IS_MIGRATION
+                or WOKEN_TASK_IS_RCU
kernel/trace/rv/monitors/rtapp_block/rtapp_block.c new file +232 -0

232 lines not on this page · raw message

kernel/trace/rv/rv_trace.h +44 -0

50 lines not on this page · raw message

lib/Kconfig.debug +3 -0
index 1af972a92d06..942318ef3f62 100644
@@ -1638,6 +1638,9 @@ config TRACE_IRQFLAGS
 	  Enables hooks to interrupt enabling and disabling for
 	  either tracing or lock debugging.
 
+config TRACE_RT_MUTEX_WAKE_WAITER
+	bool
+
 config TRACE_IRQFLAGS_NMI
 	def_bool y
 	depends on TRACE_IRQFLAGS
Signature · 1 line
2.39.5
maintainer
#13 lore raw

Re: [PATCH 04/10] rv: Add rtapp_block monitor

Quoted 14 lines · Nam Cao

Add an RV monitor to detect realtime tasks getting blocked. For the full
description, see Documentation/trace/rv/monitor_rtapp_block.rst.

---
Cc: Peter Zijlstra <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Boqun Feng <[email protected]>
Cc: Waiman Long <[email protected]>
---
 .../trace/rv/monitor_rtapp_block.rst          |  34 +++ include/trace/events/lock.h                   |  12 +

Quoted 8 lines

 kernel/locking/rtmutex.c                      |   4 + kernel/trace/rv/Kconfig                       |  12 +- kernel/trace/rv/Makefile                      |   2 + kernel/trace/rv/monitors/rtapp_block/ba.c     | 146 +++++++++++ kernel/trace/rv/monitors/rtapp_block/ba.h     | 166 +++++++++++++ kernel/trace/rv/monitors/rtapp_block/ltl      |   9 + .../rv/monitors/rtapp_block/rtapp_block.c     | 232
++++++++++++++++++

I see the creation of this type of monitor requires a bit more steps, but are you considering automatic generation of Kconfig and rtapp_block.c ?
We could reuse (export) some code from dot2k for that since the skeleton could be the same.

Not needed for this series but it would be very nice to experiment further.

I see the tracepoint generation is a bit complicated to generalise. Da monitors currently don't give much data, besides pointing to what failed in the model. If the user wants more, they can enable the triggering tracepoints (e.g. I want to see the exact details of a context switch, I enable that in the trace).

I get it isn't the quickest thing to do but makes the monitors very general. Do you think something like this could be done here too? Perhaps storing possible error messages in the monitor's header file (like the state names in a da monitor).

Quoted 147 lines

 kernel/trace/rv/rv_trace.h                    |  44 ++++ lib/Kconfig.debug                             |   3 +

 11 files changed, 663 insertions(+), 1 deletion(-)

 create mode 100644 Documentation/trace/rv/monitor_rtapp_block.rst create mode 100644 kernel/trace/rv/monitors/rtapp_block/ba.c create mode 100644 kernel/trace/rv/monitors/rtapp_block/ba.h create mode 100644 kernel/trace/rv/monitors/rtapp_block/ltl create mode 100644 kernel/trace/rv/monitors/rtapp_block/rtapp_block.c

Documentation/trace/rv/monitor_rtapp_block.rst new file +11 -0
new file mode 100644
index 000000000000..9cabbe66fa4a
@@ -0,0 +1,34 @@
+Monitor rtapp_block
+=======================
+
+- Name: rtapp_block - real time applications are undesirably blocked
+- Type: per-task linear temporal logic monitor
+- Author: Nam Cao <[email protected]>
+
+Introduction
+------------
+
+Real time threads could be blocked and fail to finish their

execution timely. For instance, they
+need to access shared resources which are already acquired by other threads. Or they could be
+waiting for non-realtime threads to signal them to proceed: as the non-realtime threads are not
+prioritized by the scheduler, the execution of realtime threads could be delayed indefinitely.
+These scenarios are often unintentional, and cause unexpected latency to the realtime application.

Documentation/trace/rv/monitor_rtapp_block.rst new file +2 -0
+
+The rtapp_block monitor reports this type of scenario, by monitoring

for:
+
+  * Realtime threads going to sleep without explicitly asking for it (namely, with nanosleep
+    syscall).
+  * Realtime threads are woken up by non-realtime threads.

Documentation/trace/rv/monitor_rtapp_block.rst new file +5 -0
+
+How to fix the monitor's warnings?
+----------------------------------
+
+There is no single answer, the solution needs to be evaluated

depending on the specific cases.

Documentation/trace/rv/monitor_rtapp_block.rst new file +2 -0
+
+If the realtime thread is blocked trying to take a `pthread_mutex_t`

which is already taken by a
+non-realtime thread, the solution could be enabling priority inheritance for the mutex, so that the
+blocking non-realtime thread would be priority-boosted to run at realtime priority.

Documentation/trace/rv/monitor_rtapp_block.rst new file +2 -0
+
+If realtime thread needs to wait for non-realtime thread to signal

it to proceed, perhaps the design
+needs to be reconsidered to remove this dependency. Often, the work executed by the realtime thread

Documentation/trace/rv/monitor_rtapp_block.rst new file +1 -0
+needs not to be realtime at all.
include/trace/events/lock.h +0 -0
index 8e89baa3775f..d4b32194d47f 100644
@@ -138,6 +138,18 @@ TRACE_EVENT(contention_end,
  	TP_printk("%p (ret=%d)", __entry->lock_addr, __entry->ret)
  );

include/trace/events/lock.h +12 -0
+#ifdef CONFIG_TRACE_RT_MUTEX_WAKE_WAITER
+DECLARE_TRACE(rt_mutex_wake_waiter_begin,
+		TP_PROTO(struct task_struct *task),
+		TP_ARGS(task))
+DECLARE_TRACE(rt_mutex_wake_waiter_end,
+		TP_PROTO(struct task_struct *task),
+		TP_ARGS(task))
+#else
+#define trace_rt_mutex_wake_waiter_begin(...)
+#define trace_rt_mutex_wake_waiter_end(...)
+#endif /* CONFIG_TRACE_RT_MUTEX */
+

 #endif /* _TRACE_LOCK_H */

 /* This part must be outside protection */

kernel/locking/rtmutex.c +0 -0
index 4a8df1800cbb..fc9cf4a2cf75 100644
[...]
kernel/trace/rv/Kconfig +0 -0
index 8226352a0062..d65bf9bda2f2 100644
[...]
kernel/trace/rv/Makefile +0 -0
index 188b64668e1f..6570a3116127 100644
[...]
kernel/trace/rv/monitors/rtapp_block/ba.c new file +0 -0
new file mode 100644
index 000000000000..5e99f79d5e74
@@ -0,0 +1,146 @@
[...]
kernel/trace/rv/monitors/rtapp_block/ba.h new file +7 -0
new file mode 100644
index 000000000000..c1ba88f6779a
@@ -0,0 +1,166 @@
[...]
+
+/**
+ * rv_rtapp_block_atoms_fetch - fetch the atomic propositions
+ * @task:	the task
+ * @mon:	the LTL monitor
+ *
+ * Must be implemented. This function is called anytime the Buchi

automaton is triggered. Its
+ * intended purpose is to update the atomic propositions which are expensive to trace and can be
+ * easily read from @task. rv_rtapp_block_atom_set() should be used to implement this function.
+ *
+ * Using this function may cause incorrect verification result if it is important for the LTL that
+ * the atomic propositions must be updated at the correct time. Therefore, if it is possible,
+ * updating atomic propositions should be done with rv_rtapp_block_atom_update() instead.
+ *
+ * An example where this function is useful is with the LTL property:
+ *    always (RT imply not PAGEFAULT)
+ * (a realtime task does not raise page faults)
+ *
+ * In this example, adding tracepoints to track RT is complicated, because it is changed in
+ * differrent places (mutex's priority boosting,

Quoted 8 lines

sched_setscheduler). Furthermore, for this LTL
+ * property, we don't care exactly when RT changes, as long as we have its correct value when
+ * PAGEFAULT==true. Therefore, it is better to update RT in rv_rtapp_block_atoms_fetch(), as it
+ * can easily be retrieved from task_struct.
+ *
+ * This function can be empty.

Personal preference, but what about having the examples only in the docs and point to those from here? Just to keep the code a bit slimmer.

kernel/trace/rv/monitors/rtapp_block/rtapp_block.c

Quoted 93 lines

+ */
+void rv_rtapp_block_atoms_fetch(struct task_struct *task, struct ltl_monitor *mon);

kernel/trace/rv/monitors/rtapp_block/ba.h new file +8 -0
+
+/**
+ * rv_rtapp_block_atom_update - update an atomic proposition
+ * @task:	the task
+ * @atom:	the atomic proposition, one of enum rtapp_block_atom
+ * @value:	the new value for @atom
+ *
+ * Update an atomic proposition and trigger the Buchi atomaton to

check for violation of the LTL
+ * property. This function can be called in tracepoints' handler, for example.
+ */
+void rv_rtapp_block_atom_update(struct task_struct *task, unsigned int atom, bool value);

kernel/trace/rv/monitors/rtapp_block/ba.h new file +10 -0
+
+/**
+ * rv_rtapp_block_atom_get - get an atomic proposition
+ * @mon:	the monitor
+ * @atom:	the atomic proposition, one of enum rtapp_block_atom
+ *
+ * Returns the value of an atomic proposition.
+ */
+static inline
+enum ltl_truth_value rv_rtapp_block_atom_get(struct ltl_monitor

*mon, unsigned int atom)

kernel/trace/rv/monitors/rtapp_block/ba.h new file +11 -0
+{
+	return mon->atoms[atom];
+}
+
+/**
+ * rv_rtapp_block_atom_set - set an atomic proposition
+ * @mon:	the monitor
+ * @atom:	the atomic proposition, one of enum rtapp_block_atom
+ * @value:	the new value for @atom
+ *
+ * Update an atomic proposition without triggering the Buchi

automaton. This can be useful to
+ * implement rv_rtapp_block_atoms_fetch() and rv_rtapp_block_atoms_init().
+ *
+ * Another use case for this function is when multiple atomic propositions change at the same time,
+ * because calling rv_rtapp_block_atom_update() (and thus triggering the Buchi automaton)
+ * multiple times may be incorrect. In that case, rv_rtapp_block_atom_set() can be used to avoid
+ * triggering the Buchi automaton, and rv_rtapp_block_atom_update() is only used for the last
+ * atomic proposition.
+ */

kernel/trace/rv/monitors/rtapp_block/ba.h new file +2 -0
+static inline
+void rv_rtapp_block_atom_set(struct ltl_monitor *mon, unsigned int

atom, bool value)

kernel/trace/rv/monitors/rtapp_block/ba.h new file +9 -0
+{
+	mon->atoms[atom] = value;
+}
+
+/**
+ * rv_rtapp_block_get_data - get the custom data of this monitor.
+ * @mon: the monitor
+ *
+ * If this function is used, rv_rtapp_block_init() must have been

called with a positive
+ * data_size.
+ */

kernel/trace/rv/monitors/rtapp_block/ba.h new file +4 -0
+static inline void *rv_rtapp_block_get_data(struct ltl_monitor *mon)
+{
+	return &mon->data;
+}
kernel/trace/rv/monitors/rtapp_block/ltl new file +2 -0
new file mode 100644
index 000000000000..781f0144a222
@@ -0,0 +1,9 @@
+RULE = always (WAKEUP_RT_TASK imply (RT or WAKEUP_WHITELIST))
+   and always ((USER_TASK and RT) imply (SLEEP imply

INTENTIONAL_SLEEP))

kernel/trace/rv/monitors/rtapp_block/ltl new file +7 -0
+
+INTENTIONAL_SLEEP = DO_NANOSLEEP or FUTEX_LOCK_WITH_PI
+
+WAKEUP_WHITELIST = RT_MUTEX_WAKING_WAITER
+                or STOPPING_WOKEN_TASK
+                or WOKEN_TASK_IS_MIGRATION
+                or WOKEN_TASK_IS_RCU
kernel/trace/rv/monitors/rtapp_block/rtapp_block.c new file +0 -0
new file mode 100644
index 000000000000..3f5b1efb7af0

I'm wondering if it would be cleaner to keep the specifications under tools/ (just like the dot files for da monitors).
Nevertheless we should be consistent with what we choose.

Thanks,
Gabriele

Quoted 22 lines
kernel/trace/rv/monitors/rtapp_block/rtapp_block.c new file +0 -0
[...]
kernel/trace/rv/rv_trace.h +0 -0
index 96264233cac5..79a7388b5c55 100644
[...]
lib/Kconfig.debug +0 -0
index 1af972a92d06..942318ef3f62 100644
@@ -1638,6 +1638,9 @@ config TRACE_IRQFLAGS

    Enables hooks to interrupt enabling and disabling for either tracing or lock debugging.

lib/Kconfig.debug +3 -0
+config TRACE_RT_MUTEX_WAKE_WAITER
+	bool
+

 config TRACE_IRQFLAGS_NMI
  def_bool y
  depends on TRACE_IRQFLAGS

#14 lore raw

Re: [PATCH 04/10] rv: Add rtapp_block monitor

In reply to Nam Cao
kernel test robot noticed the following build warnings:

Hi Nam,

kernel test robot noticed the following build warnings:

[auto build test WARNING on trace/for-next]
[also build test WARNING on tip/x86/core tip/x86/mm linus/master v6.14-rc6 next-20250313] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Nam-Cao/rv-Add-undef-TRACE_INCLUDE_FILE/20250312-011043
base:   https://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace for-next
patch link:    https://lore.kernel.org/r/c8a1658c6da343b6055cbfcd6ee5ddea48335d31.1741708239.git.namcao%40linutronix.de
patch subject: [PATCH 04/10] rv: Add rtapp_block monitor
config: hexagon-allyesconfig (https://download.01.org/0day-ci/archive/20250313/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250313/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:91:53: warning: omitting the parameter name in a function definition is a C23 extension [-Wc23-extensions]
      91 | static void handle_rt_mutex_wake_waiter_begin(void *, struct task_struct *task)
         |                                                     ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:96:51: warning: omitting the parameter name in a function definition is a C23 extension [-Wc23-extensions]
      96 | static void handle_rt_mutex_wake_waiter_end(void *, struct task_struct *task)
         |                                                   ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:101:45: warning: omitting the parameter name in a function definition is a C23 extension [-Wc23-extensions]
     101 | static void handle_sched_kthread_stop(void *, struct task_struct *task)
         |                                             ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:109:49: warning: omitting the parameter name in a function definition is a C23 extension [-Wc23-extensions]
     109 | static void handle_sched_kthread_stop_ret(void *, int)
         |                                                 ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:109:54: warning: omitting the parameter name in a function definition is a C23 extension [-Wc23-extensions]
     109 | static void handle_sched_kthread_stop_ret(void *, int)
         |                                                      ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:117:39: warning: omitting the parameter name in a function definition is a C23 extension [-Wc23-extensions]
     117 | static void handle_sched_wakeup(void *, struct task_struct *task)
         |                                       ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:164:2: error: call to undeclared function 'check_trace_callback_type_sys_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     164 |         rv_attach_trace_probe("rtapp_block", sys_enter, handle_sys_enter);
         |         ^
   include/rv/instrumentation.h:18:3: note: expanded from macro 'rv_attach_trace_probe'
      18 |                 check_trace_callback_type_##tp(rv_handler);                             \
         |                 ^
   <scratch space>:8:1: note: expanded from here
       8 | check_trace_callback_type_sys_enter
         | ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:164:2: error: call to undeclared function 'register_trace_sys_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   include/rv/instrumentation.h:19:13: note: expanded from macro 'rv_attach_trace_probe'
      19 |                 WARN_ONCE(register_trace_##tp(rv_handler, NULL),                        \
         |                           ^
   <scratch space>:9:1: note: expanded from here
       9 | register_trace_sys_enter
         | ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:165:2: error: call to undeclared function 'check_trace_callback_type_sys_exit'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     165 |         rv_attach_trace_probe("rtapp_block", sys_exit, handle_sys_exit);
         |         ^
   include/rv/instrumentation.h:18:3: note: expanded from macro 'rv_attach_trace_probe'
      18 |                 check_trace_callback_type_##tp(rv_handler);                             \
         |                 ^
   <scratch space>:14:1: note: expanded from here
      14 | check_trace_callback_type_sys_exit
         | ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:165:2: error: call to undeclared function 'register_trace_sys_exit'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
   include/rv/instrumentation.h:19:13: note: expanded from macro 'rv_attach_trace_probe'
      19 |                 WARN_ONCE(register_trace_##tp(rv_handler, NULL),                        \
         |                           ^
   <scratch space>:15:1: note: expanded from here
      15 | register_trace_sys_exit
         | ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:165:2: note: did you mean 'register_ftrace_direct'?
   include/rv/instrumentation.h:19:13: note: expanded from macro 'rv_attach_trace_probe'
      19 |                 WARN_ONCE(register_trace_##tp(rv_handler, NULL),                        \
         |                           ^
   <scratch space>:15:1: note: expanded from here
      15 | register_trace_sys_exit
         | ^
   include/linux/ftrace.h:535:19: note: 'register_ftrace_direct' declared here
     535 | static inline int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
         |                   ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:180:2: error: call to undeclared function 'unregister_trace_sys_enter'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     180 |         rv_detach_trace_probe("rtapp_block", sys_enter, handle_sys_enter);
         |         ^
   include/rv/instrumentation.h:28:3: note: expanded from macro 'rv_detach_trace_probe'
      28 |                 unregister_trace_##tp(rv_handler, NULL);                                \
         |                 ^
   <scratch space>:29:1: note: expanded from here
      29 | unregister_trace_sys_enter
         | ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:181:2: error: call to undeclared function 'unregister_trace_sys_exit'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
     181 |         rv_detach_trace_probe("rtapp_block", sys_exit, handle_sys_exit);
         |         ^
   include/rv/instrumentation.h:28:3: note: expanded from macro 'rv_detach_trace_probe'
      28 |                 unregister_trace_##tp(rv_handler, NULL);                                \
         |                 ^
   <scratch space>:30:1: note: expanded from here
      30 | unregister_trace_sys_exit
         | ^
   kernel/trace/rv/monitors/rtapp_block/rtapp_block.c:181:2: note: did you mean 'unregister_ftrace_direct'?
   include/rv/instrumentation.h:28:3: note: expanded from macro 'rv_detach_trace_probe'
      28 |                 unregister_trace_##tp(rv_handler, NULL);                                \
         |                 ^

<scratch space>:30:1: note: expanded from here
30 | unregister_trace_sys_exit
| ^
include/linux/ftrace.h:539:19: note: 'unregister_ftrace_direct' declared here
539 | static inline int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
| ^
6 warnings and 6 errors generated.

vim +91 kernel/trace/rv/monitors/rtapp_block/rtapp_block.c

    90	
  > 91	static void handle_rt_mutex_wake_waiter_begin(void *, struct task_struct *task)
    92	{
    93		rv_rtapp_block_atom_update(task, RT_MUTEX_WAKING_WAITER, true);
    94	}
    95	
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author
#15 lore raw

PATCH 5/10 x86/tracing: Remove redundant trace_pagefault_key

In reply to Nam Cao

trace_pagefault_key is used to determine whether the tracepoints are registered and whether to call trace functions. However, tracepoints already have this functionality built-in.

Remove this redundant key.

---
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: H. Peter Anvin <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Peter Zijlstra <[email protected]>

---
 arch/x86/include/asm/trace/common.h      | 12 ------------
 arch/x86/include/asm/trace/exceptions.h  | 18 ++++++------------
 arch/x86/include/asm/trace/irq_vectors.h |  1 -
 arch/x86/kernel/tracepoint.c             | 21 ---------------------
 arch/x86/mm/fault.c                      |  3 ---
 5 files changed, 6 insertions(+), 49 deletions(-)
 delete mode 100644 arch/x86/include/asm/trace/common.h
 delete mode 100644 arch/x86/kernel/tracepoint.c
arch/x86/include/asm/trace/common.h deleted +0 -12
deleted file mode 100644
index f0f9bcdb74d9..000000000000
@@ -1,12 +0,0 @@
-#ifndef _ASM_TRACE_COMMON_H
-#define _ASM_TRACE_COMMON_H
-
-#ifdef CONFIG_TRACING
-DECLARE_STATIC_KEY_FALSE(trace_pagefault_key);
-#define trace_pagefault_enabled()			\
-	static_branch_unlikely(&trace_pagefault_key)
-#else
-static inline bool trace_pagefault_enabled(void) { return false; }
-#endif
-
-#endif
arch/x86/include/asm/trace/exceptions.h +6 -12
index 6b1e87194809..34bc8214a2d7 100644
@@ -6,10 +6,6 @@
 #define _TRACE_PAGE_FAULT_H
 
 #include <linux/tracepoint.h>
-#include <asm/trace/common.h>
-
-extern int trace_pagefault_reg(void);
-extern void trace_pagefault_unreg(void);
 
 DECLARE_EVENT_CLASS(x86_exceptions,
 
@@ -34,15 +30,13 @@ DECLARE_EVENT_CLASS(x86_exceptions,
 		  (void *)__entry->address, (void *)__entry->ip,
 		  __entry->error_code) );
 
-#define DEFINE_PAGE_FAULT_EVENT(name)				\
-DEFINE_EVENT_FN(x86_exceptions, name,				\
-	TP_PROTO(unsigned long address,	struct pt_regs *regs,	\
-		 unsigned long error_code),			\
-	TP_ARGS(address, regs, error_code),			\
-	trace_pagefault_reg, trace_pagefault_unreg);
+DEFINE_EVENT(x86_exceptions, page_fault_user,
+	TP_PROTO(unsigned long address,	struct pt_regs *regs, unsigned long error_code),
+	TP_ARGS(address, regs, error_code));
 
-DEFINE_PAGE_FAULT_EVENT(page_fault_user);
-DEFINE_PAGE_FAULT_EVENT(page_fault_kernel);
+DEFINE_EVENT(x86_exceptions, page_fault_kernel,
+	TP_PROTO(unsigned long address,	struct pt_regs *regs, unsigned long error_code),
+	TP_ARGS(address, regs, error_code));
 
 #undef TRACE_INCLUDE_PATH
 #undef TRACE_INCLUDE_FILE
arch/x86/include/asm/trace/irq_vectors.h +0 -1
index 88e7f0f3bf62..7408bebdfde0 100644
@@ -6,7 +6,6 @@
 #define _TRACE_IRQ_VECTORS_H
 
 #include <linux/tracepoint.h>
-#include <asm/trace/common.h>
 
 #ifdef CONFIG_X86_LOCAL_APIC
 
arch/x86/kernel/tracepoint.c deleted +0 -21
deleted file mode 100644
index 03ae1caaa878..000000000000
@@ -1,21 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Copyright (C) 2013 Seiji Aguchi <[email protected]>
- */
-#include <linux/jump_label.h>
-#include <linux/atomic.h>
-
-#include <asm/trace/exceptions.h>
-
-DEFINE_STATIC_KEY_FALSE(trace_pagefault_key);
-
-int trace_pagefault_reg(void)
-{
-	static_branch_inc(&trace_pagefault_key);
-	return 0;
-}
-
-void trace_pagefault_unreg(void)
-{
-	static_branch_dec(&trace_pagefault_key);
-}
arch/x86/mm/fault.c +0 -3
index 296d294142c8..7e3e51fa1f95 100644
@@ -1455,9 +1455,6 @@ static __always_inline void
 trace_page_fault_entries(struct pt_regs *regs, unsigned long error_code,
 			 unsigned long address)
 {
-	if (!trace_pagefault_enabled())
-		return;
-
 	if (user_mode(regs))
 		trace_page_fault_user(address, regs, error_code);
 	else
Signature · 1 line
2.39.5
author
#16 lore raw

PATCH 6/10 x86/tracing: Move page fault trace points to generic

In reply to Nam Cao

Page fault trace points are interesting for other architectures as well. Move them to be generic.

---
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Peter Zijlstra <[email protected]>

---
 arch/x86/mm/fault.c                                 |  2 +-
 .../asm/trace => include/trace/events}/exceptions.h | 13 ++++---------
 2 files changed, 5 insertions(+), 10 deletions(-)
 rename {arch/x86/include/asm/trace => include/trace/events}/exceptions.h (79%)
arch/x86/mm/fault.c +1 -1
index 7e3e51fa1f95..ad4cb1502316 100644
@@ -38,7 +38,7 @@
 #include <asm/sev.h>			/* snp_dump_hva_rmpentry()	*/
 
 #define CREATE_TRACE_POINTS
-#include <asm/trace/exceptions.h>
+#include <trace/events/exceptions.h>
 
 /*
  * Returns 0 if mmiotrace is disabled, or if the fault is not
include/trace/events/exceptions.h renamed from arch/x86/include/asm/trace/exceptions.h +4 -9
similarity index 79%
rename from arch/x86/include/asm/trace/exceptions.h
rename to include/trace/events/exceptions.h
index 34bc8214a2d7..a631f8de8917 100644
@@ -7,7 +7,7 @@
 
 #include <linux/tracepoint.h>
 
-DECLARE_EVENT_CLASS(x86_exceptions,
+DECLARE_EVENT_CLASS(exceptions,
 
 	TP_PROTO(unsigned long address, struct pt_regs *regs,
 		 unsigned long error_code),
@@ -22,7 +22,7 @@ DECLARE_EVENT_CLASS(x86_exceptions,
 
 	TP_fast_assign(
 		__entry->address = address;
-		__entry->ip = regs->ip;
+		__entry->ip = instruction_pointer(regs);
 		__entry->error_code = error_code;
 	),
 
@@ -30,18 +30,13 @@ DECLARE_EVENT_CLASS(x86_exceptions,
 		  (void *)__entry->address, (void *)__entry->ip,
 		  __entry->error_code) );
 
-DEFINE_EVENT(x86_exceptions, page_fault_user,
+DEFINE_EVENT(exceptions, page_fault_user,
 	TP_PROTO(unsigned long address,	struct pt_regs *regs, unsigned long error_code),
 	TP_ARGS(address, regs, error_code));
-
-DEFINE_EVENT(x86_exceptions, page_fault_kernel,
+DEFINE_EVENT(exceptions, page_fault_kernel,
 	TP_PROTO(unsigned long address,	struct pt_regs *regs, unsigned long error_code),
 	TP_ARGS(address, regs, error_code));
 
-#undef TRACE_INCLUDE_PATH
-#undef TRACE_INCLUDE_FILE
-#define TRACE_INCLUDE_PATH .
-#define TRACE_INCLUDE_FILE exceptions
 #endif /*  _TRACE_PAGE_FAULT_H */
 
 /* This part must be outside protection */
Signature · 1 line
2.39.5
author
#17 lore raw

PATCH 7/10 arm64: mm: Add page fault trace points

In reply to Nam Cao

Add page fault trace points, which are useful to implement RV monitor which watches page faults.

---
Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: [email protected]

---
 arch/arm64/mm/fault.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/arm64/mm/fault.c +8 -0
index ef63651099a9..e3f096b0dffd 100644
@@ -44,6 +44,9 @@
 #include <asm/tlbflush.h>
 #include <asm/traps.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/exceptions.h>
+
 struct fault_info {
 	int	(*fn)(unsigned long far, unsigned long esr,
 		      struct pt_regs *regs);
@@ -559,6 +562,11 @@ static int __kprobes do_page_fault(unsigned long far, unsigned long esr,
 	if (kprobe_page_fault(regs, esr))
 		return 0;
 
+	if (user_mode(regs))
+		trace_page_fault_user(addr, regs, esr);
+	else
+		trace_page_fault_kernel(addr, regs, esr);
+
 	/*
 	 * If we're in an interrupt or have no user context, we must not take
 	 * the fault.
Signature · 1 line
2.39.5
author
#18 lore raw

PATCH 8/10 riscv: mm: Add page fault trace points

In reply to Nam Cao

Add page fault trace points, which are useful to implement RV monitor that watches page faults.

---
Cc: Paul Walmsley <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: [email protected]

---
 arch/riscv/mm/fault.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/riscv/mm/fault.c +8 -0
index 0194324a0c50..04ed6f8acae4 100644
@@ -20,6 +20,9 @@
 #include <asm/ptrace.h>
 #include <asm/tlbflush.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/exceptions.h>
+
 #include "../kernel/head.h"
 
 static void show_pte(unsigned long addr)
@@ -291,6 +294,11 @@ void handle_page_fault(struct pt_regs *regs)
 	if (kprobe_page_fault(regs, cause))
 		return;
 
+	if (user_mode(regs))
+		trace_page_fault_user(addr, regs, cause);
+	else
+		trace_page_fault_kernel(addr, regs, cause);
+
 	/*
 	 * Fault-in kernel-space virtual memory on-demand.
 	 * The 'reference' page table is init_mm.pgd.
Signature · 1 line
2.39.5
reviewer Acked-by on v1 8/10
#19 lore raw

Re: [PATCH 08/10] riscv: mm: Add page fault trace points

Hi Nam,

Quoted 30 lines · Nam Cao

Add page fault trace points, which are useful to implement RV monitor that watches page faults.

---
Cc: Paul Walmsley <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: [email protected]

---
  arch/riscv/mm/fault.c | 8 ++++++++
  1 file changed, 8 insertions(+)
arch/riscv/mm/fault.c +3 -0
index 0194324a0c50..04ed6f8acae4 100644
@@ -20,6 +20,9 @@
  #include <asm/ptrace.h>
  #include <asm/tlbflush.h>
 
+#define CREATE_TRACE_POINTS
+#include <trace/events/exceptions.h>
+
  #include "../kernel/head.h"
 
  static void show_pte(unsigned long addr)
@@ -291,6 +294,11 @@ void handle_page_fault(struct pt_regs *regs)
  	if (kprobe_page_fault(regs, cause))

arch/riscv/mm/fault.c void handle_page_fault(struct pt_regs *regs) · line 304

Quoted 10 lines
arch/riscv/mm/fault.c +5 -0
  		return;
 
+	if (user_mode(regs))
+		trace_page_fault_user(addr, regs, cause);
+	else
+		trace_page_fault_kernel(addr, regs, cause);
+
  	/*
  	 * Fault-in kernel-space virtual memory on-demand.
  	 * The 'reference' page table is init_mm.pgd.

For this riscv part, you can add:

Thanks,

Alex

author
#20 lore raw

PATCH 9/10 Add rtapp_pagefault monitor

In reply to Nam Cao

Real time applications can have unexpected latency due to page faults.

Add an RV LTL monitor to detect such cases. For complete description, see Documentation/trace/rv/monitor_rtapp_pagefault.rst.

The document is mostly from:
https://www.linutronix.de/blog/A-Checklist-for-Real-Time-Applications-in-Linux

---
Cc: Catalin Marinas <[email protected]>
Cc: Will Deacon <[email protected]>
Cc: Paul Walmsley <[email protected]>
Cc: Palmer Dabbelt <[email protected]>
Cc: Albert Ou <[email protected]>
Cc: Alexandre Ghiti <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: H. Peter Anvin <[email protected]>
Cc: Andy Lutomirski <[email protected]>
Cc: Peter Zijlstra <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 .../trace/rv/monitor_rtapp_pagefault.rst      |  36 ++++
 kernel/trace/rv/Kconfig                       |   8 +
 kernel/trace/rv/Makefile                      |   2 +
 kernel/trace/rv/monitors/rtapp_pagefault/ba.c | 139 +++++++++++++++
 kernel/trace/rv/monitors/rtapp_pagefault/ba.h | 158 ++++++++++++++++++
 kernel/trace/rv/monitors/rtapp_pagefault/ltl  |   1 +
 .../rtapp_pagefault/rtapp_pagefault.c         |  84 ++++++++++
 kernel/trace/rv/rv_trace.h                    |  20 +++
 8 files changed, 448 insertions(+)
 create mode 100644 Documentation/trace/rv/monitor_rtapp_pagefault.rst
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/ba.c
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/ba.h
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/ltl
 create mode 100644 kernel/trace/rv/monitors/rtapp_pagefault/rtapp_pagefault.c
Documentation/trace/rv/monitor_rtapp_pagefault.rst new file +36 -0
new file mode 100644
index 000000000000..0ee223d83d09
@@ -0,0 +1,36 @@
+Monitor rtapp_pagefault
+=======================
+
+- Name: rtapp_pagefault - realtime applications raising page faults
+- Type: per-task linear temporal logic monitor
+- Author: Nam Cao <[email protected]>
+
+Introduction
+------------
+
+One of the most devastating situations for a real-time application is the need to assign or "page
+in" memory. This can be due to the over-commitment behavior of Linux when accessing allocated or
+reserved memory for the first time. Or it can be paging in disk data (such as text segments) when
+calling functions for the first time. Whatever the case, it must be avoided in order to meet
+response requirements.
+
+The monitor reports these situation where real-time applications raise page faults.
+
+How to fix the monitor's warnings?
+----------------------------------
+
+The first thing a real-time application needs to do is configure glibc to use a single
+non-shrinkable heap for the application. This guarantees that a pool of readily accessible physical
+RAM can be made available to the real-time application. This is accomplished using the mallopt(3)
+function (M_MMAP_MAX=0, M_ARENA_MAX=1, M_TRIM_THRESHOLD=-1).
+
+Next, all allocated and mapped virtual memory must be assigned to physical RAM and locked so that it
+cannot be reclaimed for other purposes. This is accomplished using the mlockall(2) function
+(MCL_CURRENT | MCL_FUTURE).
+
+Finally, the amounts of stack and heap needed during the lifetime of the real-time application must
+be allocated and written to in order to trigger heap and stack assignments to physical RAM. This is
+known as pre-faulting and is usually accomplished by memsetting a large buffer within a stack frame
+and allocating, memsetting, and freeing a large heap buffer.
+
+Pitfall: Keep in mind that each thread will have its own stack.
kernel/trace/rv/Kconfig +8 -0
index d65bf9bda2f2..2822af1b2b4d 100644
@@ -39,6 +39,14 @@ config RV_MON_RTAPP_BLOCK
 	  Enable rtapp_wakeup which monitors that realtime tasks are not blocked.
 	  For details, see Documentation/trace/rv/monitor_rtapp_block.rst.
 
+config RV_MON_RTAPP_PAGEFAULT
+	depends on RV
+	select DA_MON_EVENTS
+	bool "rtapp_pagefault monitor"
+	help
+	  Enable rtapp_pagefault which monitors that real-time tasks do not raise page faults.
+	  See Documentation/trace/rv/monitor_rtapp_pagefault.rst for full details.
+
 config RV_REACTORS
 	bool "Runtime verification reactors"
 	default y
kernel/trace/rv/Makefile +2 -0
index 6570a3116127..7f21a679ad70 100644
@@ -7,6 +7,8 @@ obj-$(CONFIG_RV_MON_WIP) += monitors/wip/wip.o
 obj-$(CONFIG_RV_MON_WWNR) += monitors/wwnr/wwnr.o
 obj-$(CONFIG_RV_MON_RTAPP_BLOCK) += monitors/rtapp_block/ba.o \
 				    monitors/rtapp_block/rtapp_block.o
+obj-$(CONFIG_RV_MON_RTAPP_PAGEFAULT) += monitors/rtapp_pagefault/ba.o \
+					monitors/rtapp_pagefault/rtapp_pagefault.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/rtapp_pagefault/ba.c new file +139 -0

139 lines not on this page · raw message

kernel/trace/rv/monitors/rtapp_pagefault/ba.h new file +158 -0

158 lines not on this page · raw message

kernel/trace/rv/monitors/rtapp_pagefault/ltl new file +1 -0
new file mode 100644
index 000000000000..d7ce62102733
@@ -0,0 +1 @@
+RULE = always (RT imply not PAGEFAULT)
kernel/trace/rv/monitors/rtapp_pagefault/rtapp_pagefault.c new file +84 -0

84 lines not on this page · raw message

kernel/trace/rv/rv_trace.h +20 -0
index 79a7388b5c55..560581d7edcb 100644
@@ -165,6 +165,26 @@ TRACE_EVENT(rtapp_block_sleep_error,
 	TP_printk("rv: %s[%d](RT) is blocked\n", __get_str(comm), __entry->pid)
 );
 #endif
+#ifdef CONFIG_RV_MON_RTAPP_PAGEFAULT
+TRACE_EVENT(rtapp_pagefault_error,
+
+	TP_PROTO(struct task_struct *task),
+
+	TP_ARGS(task),
+
+	TP_STRUCT__entry(
+		__string(comm, task->comm)
+		__field(pid_t, pid)
+	),
+
+	TP_fast_assign(
+		__assign_str(comm);
+		__entry->pid = task->pid;
+	),
+
+	TP_printk("rv: %s[%d](RT) raises a page fault", __get_str(comm), __entry->pid)
+);
+#endif
 #endif /* _TRACE_RV_H */
 
 /* This part must be outside protection */
Signature · 1 line
2.39.5
author
#21 lore raw

PATCH 10/10 raise the number of per-task monitor to 2

In reply to Nam Cao

There are 2 monitors to check realtime applications. Users may want to enable them both.

---
 include/linux/rv.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/rv.h +1 -1
index 6de4f93b390e..224c2194f582 100644
@@ -44,7 +44,7 @@ struct ltl_monitor {
  * adding more or developing a dynamic method. So far, none of
  * these are justified.
  */
-#define RV_PER_TASK_MONITORS		1
+#define RV_PER_TASK_MONITORS		2
 #define RV_PER_TASK_MONITOR_INIT	(RV_PER_TASK_MONITORS)
 
 union rv_task_monitor {
Signature · 1 line
2.39.5
maintainer
#22 lore raw

Re: [PATCH 10/10] rv: raise the number of per-task monitor to 2

Quoted 20 lines · Nam Cao

There are 2 monitors to check realtime applications. Users may want to
enable them both.

---
 include/linux/rv.h | 2 +-

 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/rv.h +1 -1
index 6de4f93b390e..224c2194f582 100644
@@ -44,7 +44,7 @@ struct ltl_monitor {
   * adding more or developing a dynamic method. So far, none of
   * these are justified.
   */
-#define RV_PER_TASK_MONITORS		1
+#define RV_PER_TASK_MONITORS		2
  #define RV_PER_TASK_MONITOR_INIT	(RV_PER_TASK_MONITORS)
Quoted 2 lines


 union rv_task_monitor {

I was thinking this value could be defined in Kconfig. Here you are adding two monitors that belong with each other, that would fit nicely with nested monitors [1] (patch waiting for final Ack).

Your monitor (let's call it rtapp) would require 2 slots and you could enforce that in Kconfig.

Of course you don't have to add it to this series, but just as a heads up.

Cheers,
Gabriele

[1] - https://lore.kernel.org/lkml/[email protected]/