Skip to contentSkip to conversation

rtapp monitor update

PATCH Superseded Nam Cao 2 participants 13 messages May 19 – May 25

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nam Cao

author
#0 lore raw

cover rtapp monitor update

Hi,

A couple of minor improvements to the rtapp monitor, making the monitor more informative to user and update the allow list regarding clock_nanosleep syscall.

Nam Cao (3):
rv/rtapp/sleep: Make the error more informative for user rv/rtapp/sleep: Update nanosleep rule
rv/rtapp: Add wakeup monitor

 kernel/trace/rv/Kconfig                       |   1 +
 kernel/trace/rv/Makefile                      |   1 +
 kernel/trace/rv/monitors/sleep/sleep.c        |  18 +-
 kernel/trace/rv/monitors/sleep/sleep.h        |  52 +++---
 kernel/trace/rv/monitors/wakeup/Kconfig       |  17 ++
 kernel/trace/rv/monitors/wakeup/wakeup.c      | 155 ++++++++++++++++++
 kernel/trace/rv/monitors/wakeup/wakeup.h      |  92 +++++++++++
 .../trace/rv/monitors/wakeup/wakeup_trace.h   |  14 ++
 kernel/trace/rv/rv_trace.h                    |   1 +
 tools/verification/models/rtapp/sleep.ltl     |   2 +-
 tools/verification/models/rtapp/wakeup.ltl    |   5 +
 11 files changed, 318 insertions(+), 40 deletions(-)
 create mode 100644 kernel/trace/rv/monitors/wakeup/Kconfig
 create mode 100644 kernel/trace/rv/monitors/wakeup/wakeup.c
 create mode 100644 kernel/trace/rv/monitors/wakeup/wakeup.h
 create mode 100644 kernel/trace/rv/monitors/wakeup/wakeup_trace.h
 create mode 100644 tools/verification/models/rtapp/wakeup.ltl
Signature · 1 line
2.47.3
author
#1 lore raw

PATCH 1/3 rv/rtapp/sleep: Make the error more informative for user

The rtapp/sleep monitor detects real-time tasks which go to sleep in an real-time-unsafe manner. If this happen, the monitor triggers a trace event in the sched_wakeup tracepoint's handler.

However, the invoking context of that trace event is not the most informative, because of the stack trace of that event is the wakeup's code path which is not very helpful:

74.669317: rv:error_sleep: condvar[254]: violation detected

    ltl_validate+0x345 ([kernel.kallsyms])
    handle_sched_wakeup+0x34 ([kernel.kallsyms])
    ttwu_do_activate+0xff ([kernel.kallsyms])
    sched_ttwu_pending+0x104 ([kernel.kallsyms])
    __flush_smp_call_function_queue+0x15b ([kernel.kallsyms])
    __sysvec_call_function_single+0x18 ([kernel.kallsyms])
    sysvec_call_function_single+0x66 ([kernel.kallsyms])
    asm_sysvec_call_function_single+0x1a ([kernel.kallsyms])
    pv_native_safe_halt+0xf ([kernel.kallsyms])
    default_idle+0x9 ([kernel.kallsyms])
    default_idle_call+0x33 ([kernel.kallsyms])
    do_idle+0x234 ([kernel.kallsyms])
    cpu_startup_entry+0x24 ([kernel.kallsyms])
    start_secondary+0xf8 ([kernel.kallsyms])
    common_startup_64+0x13e ([kernel.kallsyms])

What would be much more valuable is the stack trace of the task itself.

Change the update of WAKEUP from being in sched_wakeup trace point's handler to sched_exit trace point's handler. This makes the event happen in the task's context, making the stack trace far more informative for user:

rv:error_sleep: condvar[254]: violation detected

    ltl_validate+0x345 ([kernel.kallsyms])
    handle_sched_exit+0x39 ([kernel.kallsyms])
    __schedule+0x80f ([kernel.kallsyms])
    schedule+0x22 ([kernel.kallsyms])
    futex_do_wait+0x33 ([kernel.kallsyms])
    __futex_wait+0x8c ([kernel.kallsyms])
    futex_wait+0x73 ([kernel.kallsyms])
    do_futex+0xc6 ([kernel.kallsyms])
    __x64_sys_futex+0x121 ([kernel.kallsyms])
    do_syscall_64+0xf3 ([kernel.kallsyms])
    entry_SYSCALL_64_after_hwframe+0x77 ([kernel.kallsyms])
    __futex_abstimed_wait_common64+0xc6 (inlined)
    __futex_abstimed_wait_common+0xc6 (/usr/lib/x86_64-linux-gnu/libc.so.6)
---
 kernel/trace/rv/monitors/sleep/sleep.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/rv/monitors/sleep/sleep.c +4 -4
index 8dfe5ec13e19..0a36f5519e6b 100644
@@ -92,9 +92,9 @@ static void handle_sched_set_state(void *data, struct task_struct *task, int sta
 		ltl_atom_pulse(task, LTL_ABORT_SLEEP, true);
 }
 
-static void handle_sched_wakeup(void *data, struct task_struct *task)
+static void handle_sched_exit(void *data, bool is_switch)
 {
-	ltl_atom_pulse(task, LTL_WAKE, true);
+	ltl_atom_pulse(current, LTL_WAKE, true);
 }
 
 static void handle_sched_waking(void *data, struct task_struct *task)
@@ -200,7 +200,7 @@ static int enable_sleep(void)
 		return retval;
 
 	rv_attach_trace_probe("rtapp_sleep", sched_waking, handle_sched_waking);
-	rv_attach_trace_probe("rtapp_sleep", sched_wakeup, handle_sched_wakeup);
+	rv_attach_trace_probe("rtapp_sleep", sched_exit_tp, handle_sched_exit);
 	rv_attach_trace_probe("rtapp_sleep", sched_set_state_tp, handle_sched_set_state);
 	rv_attach_trace_probe("rtapp_sleep", contention_begin, handle_contention_begin);
 	rv_attach_trace_probe("rtapp_sleep", contention_end, handle_contention_end);
@@ -213,7 +213,7 @@ static int enable_sleep(void)
 static void disable_sleep(void)
 {
 	rv_detach_trace_probe("rtapp_sleep", sched_waking, handle_sched_waking);
-	rv_detach_trace_probe("rtapp_sleep", sched_wakeup, handle_sched_wakeup);
+	rv_detach_trace_probe("rtapp_sleep", sched_exit_tp, handle_sched_exit);
 	rv_detach_trace_probe("rtapp_sleep", sched_set_state_tp, handle_sched_set_state);
 	rv_detach_trace_probe("rtapp_sleep", contention_begin, handle_contention_begin);
 	rv_detach_trace_probe("rtapp_sleep", contention_end, handle_contention_end);
Signature · 1 line
2.47.3
maintainer
#2 lore raw

Re: [PATCH 1/3] rv/rtapp/sleep: Make the error more informative for user

Quoted 4 lines · Nam Cao

The rtapp/sleep monitor detects real-time tasks which go to sleep in an real-time-unsafe manner. If this happen, the monitor triggers a trace event in the sched_wakeup tracepoint's handler.

Ok so here WAKE is no longer tied to the wakeup event but to the end of the task switch.

So what happens if a task was not sleeping but just got preempted? Wouldn't that trigger WAKE (though that isn't a real wakeup) without RT_FRIENDLY_WAKE ?

Thanks,
Gabriele

Quoted 95 lines

However, the invoking context of that trace event is not the most informative, because of the stack trace of that event is the wakeup's code path which is not very helpful:

74.669317: rv:error_sleep: condvar[254]: violation detected ltl_validate+0x345 ([kernel.kallsyms]) handle_sched_wakeup+0x34 ([kernel.kallsyms]) ttwu_do_activate+0xff ([kernel.kallsyms]) sched_ttwu_pending+0x104 ([kernel.kallsyms]) __flush_smp_call_function_queue+0x15b ([kernel.kallsyms]) __sysvec_call_function_single+0x18 ([kernel.kallsyms]) sysvec_call_function_single+0x66 ([kernel.kallsyms]) asm_sysvec_call_function_single+0x1a ([kernel.kallsyms]) pv_native_safe_halt+0xf ([kernel.kallsyms]) default_idle+0x9 ([kernel.kallsyms]) default_idle_call+0x33 ([kernel.kallsyms]) do_idle+0x234 ([kernel.kallsyms]) cpu_startup_entry+0x24 ([kernel.kallsyms]) start_secondary+0xf8 ([kernel.kallsyms]) common_startup_64+0x13e ([kernel.kallsyms])

What would be much more valuable is the stack trace of the task itself.

Change the update of WAKEUP from being in sched_wakeup trace point's handler to sched_exit trace point's handler. This makes the event happen in the task's context, making the stack trace far more informative for user:

rv:error_sleep: condvar[254]: violation detected ltl_validate+0x345 ([kernel.kallsyms]) handle_sched_exit+0x39 ([kernel.kallsyms])
    __schedule+0x80f ([kernel.kallsyms])
    schedule+0x22 ([kernel.kallsyms])
    futex_do_wait+0x33 ([kernel.kallsyms])
    __futex_wait+0x8c ([kernel.kallsyms])
    futex_wait+0x73 ([kernel.kallsyms])
    do_futex+0xc6 ([kernel.kallsyms])
    __x64_sys_futex+0x121 ([kernel.kallsyms]) do_syscall_64+0xf3 ([kernel.kallsyms]) entry_SYSCALL_64_after_hwframe+0x77 ([kernel.kallsyms]) __futex_abstimed_wait_common64+0xc6 (inlined) __futex_abstimed_wait_common+0xc6 (/usr/lib/x86_64-linux-gnu/libc.so.6)

---
 kernel/trace/rv/monitors/sleep/sleep.c | 8 ++++----

 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/rv/monitors/sleep/sleep.c +0 -0
index 8dfe5ec13e19..0a36f5519e6b 100644
@@ -92,9 +92,9 @@ static void handle_sched_set_state(void *data, struct

task_struct *task, int sta
  ltl_atom_pulse(task, LTL_ABORT_SLEEP, true); }

kernel/trace/rv/monitors/sleep/sleep.c +1 -1
-static void handle_sched_wakeup(void *data, struct task_struct *task)
+static void handle_sched_exit(void *data, bool is_switch)

 {

kernel/trace/rv/monitors/sleep/sleep.c +1 -1
-	ltl_atom_pulse(task, LTL_WAKE, true);
+	ltl_atom_pulse(current, LTL_WAKE, true);

 }

 static void handle_sched_waking(void *data, struct task_struct *task)

kernel/trace/rv/monitors/sleep/sleep.c +0 -0
@@ -200,7 +200,7 @@ static int enable_sleep(void)
  		return retval;


  rv_attach_trace_probe("rtapp_sleep", sched_waking, handle_sched_waking);

kernel/trace/rv/monitors/sleep/sleep.c +0 -1
-	rv_attach_trace_probe("rtapp_sleep", sched_wakeup,

handle_sched_wakeup);

kernel/trace/rv/monitors/sleep/sleep.c +1 -0
+	rv_attach_trace_probe("rtapp_sleep", sched_exit_tp,

handle_sched_exit); rv_attach_trace_probe("rtapp_sleep", sched_set_state_tp, handle_sched_set_state); rv_attach_trace_probe("rtapp_sleep", contention_begin, handle_contention_begin); rv_attach_trace_probe("rtapp_sleep", contention_end, handle_contention_end);

kernel/trace/rv/monitors/sleep/sleep.c +1 -1
@@ -213,7 +213,7 @@ static int enable_sleep(void)
  static void disable_sleep(void)
  {
  	rv_detach_trace_probe("rtapp_sleep", sched_waking,
 handle_sched_waking);
-	rv_detach_trace_probe("rtapp_sleep", sched_wakeup,
 handle_sched_wakeup);
+	rv_detach_trace_probe("rtapp_sleep", sched_exit_tp,
 handle_sched_exit);

  rv_detach_trace_probe("rtapp_sleep", sched_set_state_tp, handle_sched_set_state); rv_detach_trace_probe("rtapp_sleep", contention_begin, handle_contention_begin); rv_detach_trace_probe("rtapp_sleep", contention_end, handle_contention_end);

author
#3 lore raw

Re: [PATCH 1/3] rv/rtapp/sleep: Make the error more informative for user

Quoted 10 lines · Gabriele Monaco
Quoted 3 lines · Nam Cao

The rtapp/sleep monitor detects real-time tasks which go to sleep in an real-time-unsafe manner. If this happen, the monitor triggers a trace event in the sched_wakeup tracepoint's handler.

Ok so here WAKE is no longer tied to the wakeup event but to the end of the task switch.

So what happens if a task was not sleeping but just got preempted? Wouldn't that trigger WAKE (though that isn't a real wakeup) without RT_FRIENDLY_WAKE ?

The monitor's rule says that when a task sleeps, it will not wake without rt-friendly wake. If it "wakes" without initial sleep, it is simply ignored.

I can change the name to be SCHEDULE_IN or something like that to make it clearer.

Nam

maintainer
#4 lore raw

Re: [PATCH 1/3] rv/rtapp/sleep: Make the error more informative for user

Quoted 11 lines · Nam Cao
Quoted 10 lines · Gabriele Monaco
Quoted 4 lines · Nam Cao

The rtapp/sleep monitor detects real-time tasks which go to sleep in an real-time-unsafe manner. If this happen, the monitor triggers a trace event
in the sched_wakeup tracepoint's handler.

Ok so here WAKE is no longer tied to the wakeup event but to the end of the task
switch.

Quoted 7 lines
Quoted 3 lines

So what happens if a task was not sleeping but just got preempted? Wouldn't that
trigger WAKE (though that isn't a real wakeup) without RT_FRIENDLY_WAKE ?

The monitor's rule says that when a task sleeps, it will not wake without rt-friendly wake. If it "wakes" without initial sleep, it is simply ignored.

Right makes sense.

Quoted 3 lines

I can change the name to be SCHEDULE_IN or something like that to make it clearer.

Yeah that'd be clearer, or at least a comment stating this somewhere.

Thanks,
Gabriele

author
#5 lore raw

PATCH 2/3 rv/rtapp/sleep: Update nanosleep rule

In reply to Nam Cao

CLOCK_REALTIME is the only clock that often is misused in real-time applications. The other clocks either are safe for real-time uses (CLOCK_TAI, CLOCK_MONOTONIC, CLOCK_BOOTTIME) or are unlikely to be misused (CLOCK_AUX, CLOCK_PROCESS_CPUTIME_ID).

The rtapp monitor's purpose is warning people about common mistakes with real-time design. However, warning about all clock types generates too much false positives.

Update the monitor to only warn about CLOCK_REALTIME.

---
 kernel/trace/rv/monitors/sleep/sleep.c    | 10 ++---
 kernel/trace/rv/monitors/sleep/sleep.h    | 52 +++++++++++------------
 tools/verification/models/rtapp/sleep.ltl |  2 +-
 3 files changed, 28 insertions(+), 36 deletions(-)
kernel/trace/rv/monitors/sleep/sleep.c +3 -7
index 0a36f5519e6b..e01ac56b3f4a 100644
@@ -43,9 +43,7 @@ static void ltl_atoms_init(struct task_struct *task, struct ltl_monitor *mon, bo
 	ltl_atom_set(mon, LTL_WOKEN_BY_EQUAL_OR_HIGHER_PRIO, false);
 
 	if (task_creation) {
-		ltl_atom_set(mon, LTL_KTHREAD_SHOULD_STOP, false);
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, false);
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, false);
+		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_REALTIME, false);
 		ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, false);
 		ltl_atom_set(mon, LTL_CLOCK_NANOSLEEP, false);
 		ltl_atom_set(mon, LTL_FUTEX_WAIT, false);
@@ -136,8 +134,7 @@ static void handle_sys_enter(void *data, struct pt_regs *regs, long id)
 	case __NR_clock_nanosleep_time64:
 #endif
 		syscall_get_arguments(current, regs, args);
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, args[0] == CLOCK_MONOTONIC);
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, args[0] == CLOCK_TAI);
+		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_REALTIME, args[0] == CLOCK_REALTIME);
 		ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, args[1] == TIMER_ABSTIME);
 		ltl_atom_update(current, LTL_CLOCK_NANOSLEEP, true);
 		break;
@@ -178,8 +175,7 @@ static void handle_sys_exit(void *data, struct pt_regs *regs, long ret)
 
 	ltl_atom_set(mon, LTL_FUTEX_LOCK_PI, false);
 	ltl_atom_set(mon, LTL_FUTEX_WAIT, false);
-	ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, false);
-	ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, false);
+	ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_REALTIME, false);
 	ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, false);
 	ltl_atom_set(mon, LTL_EPOLL_WAIT, false);
 	ltl_atom_update(current, LTL_CLOCK_NANOSLEEP, false);
kernel/trace/rv/monitors/sleep/sleep.h +24 -28

90 lines not on this page · raw message

tools/verification/models/rtapp/sleep.ltl +1 -1
index 6f26c4810f78..2637bc48a620 100644
@@ -9,7 +9,7 @@ RT_VALID_SLEEP_REASON = FUTEX_WAIT
 
 RT_FRIENDLY_NANOSLEEP = CLOCK_NANOSLEEP
                     and NANOSLEEP_TIMER_ABSTIME
-                    and (NANOSLEEP_CLOCK_MONOTONIC or NANOSLEEP_CLOCK_TAI)
+                    and not NANOSLEEP_CLOCK_REALTIME
 
 RT_FRIENDLY_WAKE = WOKEN_BY_EQUAL_OR_HIGHER_PRIO
                 or WOKEN_BY_HARDIRQ
Signature · 1 line
2.47.3
maintainer
#6 lore raw

Re: [PATCH 2/3] rv/rtapp/sleep: Update nanosleep rule

Quoted 8 lines · Nam Cao

CLOCK_REALTIME is the only clock that often is misused in real-time applications. The other clocks either are safe for real-time uses (CLOCK_TAI, CLOCK_MONOTONIC, CLOCK_BOOTTIME) or are unlikely to be misused (CLOCK_AUX, CLOCK_PROCESS_CPUTIME_ID).

The rtapp monitor's purpose is warning people about common mistakes with real-time design. However, warning about all clock types generates too much false positives.

I'm fine with the change, but are those really false positives?

From what I understand before this change we would report any non-rt-friendly clock type, now only realtime.
What we are skipping would still be true positives, just so uncommon not to justify the extra complexity, right?

Or do you mean an RT task using CLOCK_AUX is a false positive because likely the users weren't even trying to do real-time?

Thanks,
Gabriele

Quoted 182 lines

Update the monitor to only warn about CLOCK_REALTIME.

---
 kernel/trace/rv/monitors/sleep/sleep.c    | 10 ++--- kernel/trace/rv/monitors/sleep/sleep.h    | 52 +++++++++++------------ tools/verification/models/rtapp/sleep.ltl |  2 +-

 3 files changed, 28 insertions(+), 36 deletions(-)
kernel/trace/rv/monitors/sleep/sleep.c +0 -0
index 0a36f5519e6b..e01ac56b3f4a 100644
@@ -43,9 +43,7 @@ static void ltl_atoms_init(struct task_struct *task, struct

ltl_monitor *mon, bo
  ltl_atom_set(mon, LTL_WOKEN_BY_EQUAL_OR_HIGHER_PRIO, false);
  if (task_creation) {

kernel/trace/rv/monitors/sleep/sleep.c +1 -3
-		ltl_atom_set(mon, LTL_KTHREAD_SHOULD_STOP, false);
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, false);
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, false);
+		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_REALTIME, false);

  ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, false); ltl_atom_set(mon, LTL_CLOCK_NANOSLEEP, false); ltl_atom_set(mon, LTL_FUTEX_WAIT, false);

kernel/trace/rv/monitors/sleep/sleep.c +0 -0
@@ -136,8 +134,7 @@ static void handle_sys_enter(void *data, struct pt_regs
 *regs, long id)

  case __NR_clock_nanosleep_time64:
 #endif
  syscall_get_arguments(current, regs, args);

kernel/trace/rv/monitors/sleep/sleep.c +0 -1
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, args[0] ==

CLOCK_MONOTONIC);

kernel/trace/rv/monitors/sleep/sleep.c +0 -1
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, args[0] ==

CLOCK_TAI);

kernel/trace/rv/monitors/sleep/sleep.c +1 -0
+		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_REALTIME, args[0] ==

CLOCK_REALTIME);
  ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, args[1] == TIMER_ABSTIME);
  ltl_atom_update(current, LTL_CLOCK_NANOSLEEP, true); break;

kernel/trace/rv/monitors/sleep/sleep.c +0 -0
@@ -178,8 +175,7 @@ static void handle_sys_exit(void *data, struct pt_regs
 *regs, long ret)


  ltl_atom_set(mon, LTL_FUTEX_LOCK_PI, false); ltl_atom_set(mon, LTL_FUTEX_WAIT, false);

kernel/trace/rv/monitors/sleep/sleep.c +1 -2
-	ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, false);
-	ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, false);
+	ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_REALTIME, false);

  ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, false); ltl_atom_set(mon, LTL_EPOLL_WAIT, false); ltl_atom_update(current, LTL_CLOCK_NANOSLEEP, false);

kernel/trace/rv/monitors/sleep/sleep.h +2 -6
index 95dc2727c059..ed1ac7ad008e 100644
@@ -20,8 +20,7 @@ enum ltl_atom {
  	LTL_FUTEX_WAIT,
  	LTL_KERNEL_THREAD,
  	LTL_KTHREAD_SHOULD_STOP,
-	LTL_NANOSLEEP_CLOCK_MONOTONIC,
-	LTL_NANOSLEEP_CLOCK_TAI,
+	LTL_NANOSLEEP_CLOCK_REALTIME,
  	LTL_NANOSLEEP_TIMER_ABSTIME,
  	LTL_RT,
  	LTL_SLEEP,
@@ -46,8 +45,7 @@ static const char *ltl_atom_str(enum ltl_atom atom)
  		"fu_wa",
  		"ker_th",
  		"kth_sh_st",
-		"na_cl_mo",
-		"na_cl_ta",
+		"na_cl_re",
  		"na_ti_ab",
  		"rt",
  		"sl",
@@ -87,8 +85,7 @@ static void ltl_start(struct task_struct *task, struct
 ltl_monitor *mon)
  	bool sleep = test_bit(LTL_SLEEP, mon->atoms);
  	bool rt = test_bit(LTL_RT, mon->atoms);
  	bool nanosleep_timer_abstime = test_bit(LTL_NANOSLEEP_TIMER_ABSTIME,
 mon->atoms);
-	bool nanosleep_clock_tai = test_bit(LTL_NANOSLEEP_CLOCK_TAI, mon-
 >atoms);
-	bool nanosleep_clock_monotonic =

test_bit(LTL_NANOSLEEP_CLOCK_MONOTONIC, mon->atoms);

kernel/trace/rv/monitors/sleep/sleep.h +1 -0
+	bool nanosleep_clock_realtime =

test_bit(LTL_NANOSLEEP_CLOCK_REALTIME, mon->atoms);
  bool kthread_should_stop = test_bit(LTL_KTHREAD_SHOULD_STOP, mon-

Quoted 1 line

atoms);

  bool kernel_thread = test_bit(LTL_KERNEL_THREAD, mon->atoms); bool futex_wait = test_bit(LTL_FUTEX_WAIT, mon->atoms);

kernel/trace/rv/monitors/sleep/sleep.h +10 -10
@@ -97,17 +94,17 @@ static void ltl_start(struct task_struct *task, struct
 ltl_monitor *mon)
  	bool clock_nanosleep = test_bit(LTL_CLOCK_NANOSLEEP, mon->atoms);
  	bool block_on_rt_mutex = test_bit(LTL_BLOCK_ON_RT_MUTEX, mon->atoms);
  	bool abort_sleep = test_bit(LTL_ABORT_SLEEP, mon->atoms);
-	bool val42 = task_is_rcu || task_is_migration;
-	bool val43 = futex_lock_pi || val42;
-	bool val5 = block_on_rt_mutex || val43;
-	bool val34 = abort_sleep || kthread_should_stop;
-	bool val35 = woken_by_nmi || val34;
-	bool val36 = woken_by_hardirq || val35;
-	bool val14 = woken_by_equal_or_higher_prio || val36;
+	bool val41 = task_is_rcu || task_is_migration;
+	bool val42 = futex_lock_pi || val41;
+	bool val5 = block_on_rt_mutex || val42;
+	bool val33 = abort_sleep || kthread_should_stop;
+	bool val34 = woken_by_nmi || val33;
+	bool val35 = woken_by_hardirq || val34;
+	bool val14 = woken_by_equal_or_higher_prio || val35;
  	bool val13 = !wake;
-	bool val26 = nanosleep_clock_monotonic || nanosleep_clock_tai;
-	bool val27 = nanosleep_timer_abstime && val26;
-	bool val18 = clock_nanosleep && val27;
+	bool val25 = !nanosleep_clock_realtime;
+	bool val26 = nanosleep_timer_abstime && val25;
+	bool val18 = clock_nanosleep && val26;
  	bool val20 = val18 || epoll_wait;
  	bool val9 = futex_wait || val20;

  bool val11 = val9 || kernel_thread;

kernel/trace/rv/monitors/sleep/sleep.h +0 -0
@@ -138,8 +135,7 @@ ltl_possible_next_states(struct ltl_monitor *mon, unsigned

int state, unsigned l
  bool sleep = test_bit(LTL_SLEEP, mon->atoms);
  bool rt = test_bit(LTL_RT, mon->atoms);
  bool nanosleep_timer_abstime = test_bit(LTL_NANOSLEEP_TIMER_ABSTIME, mon->atoms);

kernel/trace/rv/monitors/sleep/sleep.h +0 -1
-	bool nanosleep_clock_tai = test_bit(LTL_NANOSLEEP_CLOCK_TAI, mon-
Quoted 1 line

atoms);

kernel/trace/rv/monitors/sleep/sleep.h +0 -1
-	bool nanosleep_clock_monotonic =

test_bit(LTL_NANOSLEEP_CLOCK_MONOTONIC, mon->atoms);

kernel/trace/rv/monitors/sleep/sleep.h +1 -0
+	bool nanosleep_clock_realtime =

test_bit(LTL_NANOSLEEP_CLOCK_REALTIME, mon->atoms);
  bool kthread_should_stop = test_bit(LTL_KTHREAD_SHOULD_STOP, mon-

Quoted 1 line

atoms);

  bool kernel_thread = test_bit(LTL_KERNEL_THREAD, mon->atoms); bool futex_wait = test_bit(LTL_FUTEX_WAIT, mon->atoms);

kernel/trace/rv/monitors/sleep/sleep.h +0 -0
@@ -148,17 +144,17 @@ ltl_possible_next_states(struct ltl_monitor *mon,

unsigned int state, unsigned l
  bool clock_nanosleep = test_bit(LTL_CLOCK_NANOSLEEP, mon->atoms); bool block_on_rt_mutex = test_bit(LTL_BLOCK_ON_RT_MUTEX, mon->atoms); bool abort_sleep = test_bit(LTL_ABORT_SLEEP, mon->atoms);

kernel/trace/rv/monitors/sleep/sleep.h +7 -7
-	bool val42 = task_is_rcu || task_is_migration;
-	bool val43 = futex_lock_pi || val42;
-	bool val5 = block_on_rt_mutex || val43;
-	bool val34 = abort_sleep || kthread_should_stop;
-	bool val35 = woken_by_nmi || val34;
-	bool val36 = woken_by_hardirq || val35;
-	bool val14 = woken_by_equal_or_higher_prio || val36;
+	bool val41 = task_is_rcu || task_is_migration;
+	bool val42 = futex_lock_pi || val41;
+	bool val5 = block_on_rt_mutex || val42;
+	bool val33 = abort_sleep || kthread_should_stop;
+	bool val34 = woken_by_nmi || val33;
+	bool val35 = woken_by_hardirq || val34;
+	bool val14 = woken_by_equal_or_higher_prio || val35;

  bool val13 = !wake;

kernel/trace/rv/monitors/sleep/sleep.h +3 -3
-	bool val26 = nanosleep_clock_monotonic || nanosleep_clock_tai;
-	bool val27 = nanosleep_timer_abstime && val26;
-	bool val18 = clock_nanosleep && val27;
+	bool val25 = !nanosleep_clock_realtime;
+	bool val26 = nanosleep_timer_abstime && val25;
+	bool val18 = clock_nanosleep && val26;

  bool val20 = val18 || epoll_wait;
  bool val9 = futex_wait || val20;
  bool val11 = val9 || kernel_thread;

tools/verification/models/rtapp/sleep.ltl +0 -0
index 6f26c4810f78..2637bc48a620 100644
@@ -9,7 +9,7 @@ RT_VALID_SLEEP_REASON = FUTEX_WAIT


 RT_FRIENDLY_NANOSLEEP = CLOCK_NANOSLEEP
                     and NANOSLEEP_TIMER_ABSTIME
-                    and (NANOSLEEP_CLOCK_MONOTONIC or NANOSLEEP_CLOCK_TAI) +                    and not NANOSLEEP_CLOCK_REALTIME

 RT_FRIENDLY_WAKE = WOKEN_BY_EQUAL_OR_HIGHER_PRIO
                 or WOKEN_BY_HARDIRQ

author
#7 lore raw

Re: [PATCH 2/3] rv/rtapp/sleep: Update nanosleep rule

Quoted 12 lines · Gabriele Monaco
Quoted 8 lines · Nam Cao

CLOCK_REALTIME is the only clock that often is misused in real-time applications. The other clocks either are safe for real-time uses (CLOCK_TAI, CLOCK_MONOTONIC, CLOCK_BOOTTIME) or are unlikely to be misused (CLOCK_AUX, CLOCK_PROCESS_CPUTIME_ID).

The rtapp monitor's purpose is warning people about common mistakes with real-time design. However, warning about all clock types generates too much false positives.

I'm fine with the change, but are those really false positives?

Quoted 7 lines

From what I understand before this change we would report any non-rt-friendly clock type, now only realtime.
What we are skipping would still be true positives, just so uncommon not to justify the extra complexity, right?

Or do you mean an RT task using CLOCK_AUX is a false positive because likely the users weren't even trying to do real-time?

CLOCK_BOOTTIME is fine for RT.

CLOCK_AUX is created for real-time use cases, so the monitor shouldn't warn about it. But this clock can also be arbitrarily set, which can be unsafe (e.g. you want to deploy the air bag in 10us, but someone changes the clock and you have to wait 10s).

We probably can detect if a clock used by an RT task is set, but I am not sure how complicated that is, if even possible. For now we are assuming that user won't do such thing, then CLOCK_AUX is fine for RT uses.

CLOCK_PROCESS_CPUTIME_ID is a strange one and shouldn't be used for real-time. But I think it's obvious from the description that you cannot do real-time with that.

Only CLOCK_REALTIME is a mistake people often make, probably has something to do with the deceptive name.

Nam

maintainer
#8 lore raw

Re: [PATCH 2/3] rv/rtapp/sleep: Update nanosleep rule

In reply to Nam Cao

kernel/trace/rv/monitors/sleep/sleep.c static void ltl_atoms_init(struct task_struct *task, struct ltl_monitor *mon, bo · line 46

Quoted 11 lines · Nam Cao
kernel/trace/rv/monitors/sleep/sleep.c +0 -0
index 0a36f5519e6b..e01ac56b3f4a 100644
@@ -43,9 +43,7 @@ static void ltl_atoms_init(struct task_struct *task, struct

ltl_monitor *mon, bo
  ltl_atom_set(mon, LTL_WOKEN_BY_EQUAL_OR_HIGHER_PRIO, false);
  if (task_creation) {

kernel/trace/rv/monitors/sleep/sleep.c +0 -1
-		ltl_atom_set(mon, LTL_KTHREAD_SHOULD_STOP, false);

Was it intentional to remove this too? It seems to me now it's cleared only for existing user threads.

Aren't we sure new kthreads aren't stopping?

Quoted 6 lines
kernel/trace/rv/monitors/sleep/sleep.c +1 -2
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_MONOTONIC, false);
-		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_TAI, false);
+		ltl_atom_set(mon, LTL_NANOSLEEP_CLOCK_REALTIME, false);

  ltl_atom_set(mon, LTL_NANOSLEEP_TIMER_ABSTIME, false); ltl_atom_set(mon, LTL_CLOCK_NANOSLEEP, false); ltl_atom_set(mon, LTL_FUTEX_WAIT, false);

You missed removing a couple of these in the next branch, the monitor doesn't build.

Thanks,
Gabriele

author
#9 lore raw

Re: [PATCH 2/3] rv/rtapp/sleep: Update nanosleep rule

Quoted 4 lines · Gabriele Monaco

Was it intentional to remove this too? It seems to me now it's cleared only for existing user threads.

Aren't we sure new kthreads aren't stopping?

...

Quoted 2 lines

You missed removing a couple of these in the next branch, the monitor doesn't build.

These are just mistakes from me, sorry about that. Not sure what I was smoking while sending these patches..

Nam

author
#10 lore raw

PATCH 3/3 rv/rtapp: Add wakeup monitor

In reply to Nam Cao

Add a wakeup monitor to detect a lower-priority task waking up a higher-priority task.

The rtapp/sleep monitor already detects this. However, that monitor triggers an error in the context of the woken task and user only gets the stacktrace of that task. It is also extremely useful to get the stacktrace of the waking task, which this monitor offers. In other words, this monitor complements the rtapp/sleep monitor.

---
 kernel/trace/rv/Kconfig                       |   1 +
 kernel/trace/rv/Makefile                      |   1 +
 kernel/trace/rv/monitors/wakeup/Kconfig       |  17 ++
 kernel/trace/rv/monitors/wakeup/wakeup.c      | 155 ++++++++++++++++++
 kernel/trace/rv/monitors/wakeup/wakeup.h      |  92 +++++++++++
 .../trace/rv/monitors/wakeup/wakeup_trace.h   |  14 ++
 kernel/trace/rv/rv_trace.h                    |   1 +
 tools/verification/models/rtapp/wakeup.ltl    |   5 +
 8 files changed, 286 insertions(+)
 create mode 100644 kernel/trace/rv/monitors/wakeup/Kconfig
 create mode 100644 kernel/trace/rv/monitors/wakeup/wakeup.c
 create mode 100644 kernel/trace/rv/monitors/wakeup/wakeup.h
 create mode 100644 kernel/trace/rv/monitors/wakeup/wakeup_trace.h
 create mode 100644 tools/verification/models/rtapp/wakeup.ltl
kernel/trace/rv/Kconfig +1 -0
index 3884b14df375..4d3a14a0bac2 100644
@@ -76,6 +76,7 @@ source "kernel/trace/rv/monitors/opid/Kconfig"
 source "kernel/trace/rv/monitors/rtapp/Kconfig"
 source "kernel/trace/rv/monitors/pagefault/Kconfig"
 source "kernel/trace/rv/monitors/sleep/Kconfig"
+source "kernel/trace/rv/monitors/wakeup/Kconfig"
 # Add new rtapp monitors here
 
 source "kernel/trace/rv/monitors/stall/Kconfig"
kernel/trace/rv/Makefile +1 -0
index 94498da35b37..c2c0e4142eb4 100644
@@ -20,6 +20,7 @@ obj-$(CONFIG_RV_MON_OPID) += monitors/opid/opid.o
 obj-$(CONFIG_RV_MON_STALL) += monitors/stall/stall.o
 obj-$(CONFIG_RV_MON_DEADLINE) += monitors/deadline/deadline.o
 obj-$(CONFIG_RV_MON_NOMISS) += monitors/nomiss/nomiss.o
+obj-$(CONFIG_RV_MON_WAKEUP) += monitors/wakeup/wakeup.o
 # Add new monitors here
 obj-$(CONFIG_RV_REACTORS) += rv_reactors.o
 obj-$(CONFIG_RV_REACT_PRINTK) += reactor_printk.o
kernel/trace/rv/monitors/wakeup/Kconfig new file +17 -0
new file mode 100644
index 000000000000..3cf11c5cd5f7
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_WAKEUP
+	depends on RV
+	depends on RV_MON_RTAPP
+	depends on HAVE_SYSCALL_TRACEPOINTS
+	select TRACE_IRQFLAGS
+	default y
+	select LTL_MON_EVENTS_ID
+	bool "wakeup monitor"
+	help
+	  This monitor detects a lower-priority task waking up a
+	  higher-priority task. The RV_MON_SLEEP monitor already
+	  detects this case, but this monitor detects in the context
+	  of the waking task instead. This and RV_MON_SLEEP can be
+	  enabled together to get the stacktrace of both the waking
+	  task and the woken task.
kernel/trace/rv/monitors/wakeup/wakeup.c new file +155 -0

155 lines not on this page · raw message

kernel/trace/rv/monitors/wakeup/wakeup.h new file +92 -0

92 lines not on this page · raw message

kernel/trace/rv/monitors/wakeup/wakeup_trace.h new file +14 -0
new file mode 100644
index 000000000000..7e056183f920
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Snippet to be included in rv_trace.h
+ */
+
+#ifdef CONFIG_RV_MON_WAKEUP
+DEFINE_EVENT(event_ltl_monitor_id, event_wakeup,
+	     TP_PROTO(struct task_struct *task, char *states, char *atoms, char *next),
+	     TP_ARGS(task, states, atoms, next));
+DEFINE_EVENT(error_ltl_monitor_id, error_wakeup,
+	     TP_PROTO(struct task_struct *task),
+	     TP_ARGS(task));
+#endif /* CONFIG_RV_MON_WAKEUP */
kernel/trace/rv/rv_trace.h +1 -0
index 9622c269789c..2f8a932432c9 100644
@@ -241,6 +241,7 @@ DECLARE_EVENT_CLASS(error_ltl_monitor_id,
 );
 #include <monitors/pagefault/pagefault_trace.h>
 #include <monitors/sleep/sleep_trace.h>
+#include <monitors/wakeup/wakeup_trace.h>
 // Add new monitors based on CONFIG_LTL_MON_EVENTS_ID here
 #endif /* CONFIG_LTL_MON_EVENTS_ID */
 
tools/verification/models/rtapp/wakeup.ltl new file +5 -0
new file mode 100644
index 000000000000..a5d63ca0811a
@@ -0,0 +1,5 @@
+RULE = always (((RT and USER_THREAD) imply
+		(not (WOKEN_BY_LOWER_PRIO or WOKEN_BY_SOFTIRQ)) or ALLOWLIST))
+
+ALLOWLIST = BLOCK_ON_RT_MUTEX
+         or FUTEX_LOCK_PI
Signature · 1 line
2.47.3
maintainer
#11 lore raw

Re: [PATCH 3/3] rv/rtapp: Add wakeup monitor

Quoted 10 lines · Nam Cao

Add a wakeup monitor to detect a lower-priority task waking up a higher-priority task.

The rtapp/sleep monitor already detects this. However, that monitor triggers an error in the context of the woken task and user only gets the stacktrace of that task. It is also extremely useful to get the stacktrace of the waking task, which this monitor offers. In other words, this monitor complements the rtapp/sleep monitor.

Looks neat, so the idea here is that we are looking at the same events sleep would react for, just from a different perspective, so it does make sense to run them both together.

You may want to set

depends on RV_PER_TASK_MONITORS >= 3

in rtapp/Kconfig. Though if we plan to add more per-task monitors we may need to find a better solution.

tools/verification/models/rtapp/wakeup.ltl line 5

Quoted 13 lines
tools/verification/models/rtapp/wakeup.ltl new file +2 -0
new file mode 100644
index 000000000000..a5d63ca0811a
@@ -0,0 +1,5 @@
+RULE = always (((RT and USER_THREAD) imply
+		(not (WOKEN_BY_LOWER_PRIO or WOKEN_BY_SOFTIRQ)) or

ALLOWLIST))

tools/verification/models/rtapp/wakeup.ltl new file +3 -0
+
+ALLOWLIST = BLOCK_ON_RT_MUTEX
+         or FUTEX_LOCK_PI

So here the events and atoms are similar to the ones in sleep, but since we fail on the waking event, we are going to see it from the perspective of the waker task, right?

But are those really equivalent? Why do we do RT and USER_THREAD here while there is a much more nuanced set of conditions in sleep?

If I understand it correctly, sleep can monitor some kernel threads but this monitor does not, is there a reason for that? Are we just not interested in the waker for kernel threads?

Quoted 14 lines
kernel/trace/rv/monitors/wakeup/Kconfig new file +7 -0
new file mode 100644
index 000000000000..3cf11c5cd5f7
@@ -0,0 +1,17 @@
+# SPDX-License-Identifier: GPL-2.0-only
+#
+config RV_MON_WAKEUP
+	depends on RV
+	depends on RV_MON_RTAPP
+	depends on HAVE_SYSCALL_TRACEPOINTS
+	select TRACE_IRQFLAGS

kernel/trace/rv/monitors/wakeup/Kconfig line 17

Quoted 10 lines
kernel/trace/rv/monitors/wakeup/Kconfig new file +10 -0
+	default y
+	select LTL_MON_EVENTS_ID
+	bool "wakeup monitor"
+	help
+	  This monitor detects a lower-priority task waking up a
+	  higher-priority task. The RV_MON_SLEEP monitor already
+	  detects this case, but this monitor detects in the context
+	  of the waking task instead. This and RV_MON_SLEEP can be
+	  enabled together to get the stacktrace of both the waking
+	  task and the woken task.

I'm not sure if there is any better terminology, but "waking" task makes me think of the task that is about to be woken, though it can mean also that task that is waking another (what you probably mean here).

What about using the waker/wakee terminology?

I see the kernel (events/sched.h) uses waking as well, but it says waking context (which a bit clearer to me than waking task). May be worth running it through an LLM which can produce more English-native unambiguous wording, or maybe I'm just flipping..

Also please document it in Documentation/trace/rv/monitor_rtapp.rst

Thanks,
Gabriele

author
#12 lore raw

Re: [PATCH 3/3] rv/rtapp: Add wakeup monitor

Quoted 10 lines · Gabriele Monaco

Looks neat, so the idea here is that we are looking at the same events sleep would react for, just from a different perspective, so it does make sense to run them both together.

You may want to set

depends on RV_PER_TASK_MONITORS >= 3

in rtapp/Kconfig. Though if we plan to add more per-task monitors we may need to find a better solution.

Yeah, perhaps some sort of list instead of the current fixed-length array.

Quoted 18 lines

tools/verification/models/rtapp/wakeup.ltl line 5

Quoted 13 lines
tools/verification/models/rtapp/wakeup.ltl new file +2 -0
new file mode 100644
index 000000000000..a5d63ca0811a
@@ -0,0 +1,5 @@
+RULE = always (((RT and USER_THREAD) imply
+		(not (WOKEN_BY_LOWER_PRIO or WOKEN_BY_SOFTIRQ)) or

ALLOWLIST))

tools/verification/models/rtapp/wakeup.ltl new file +3 -0
+
+ALLOWLIST = BLOCK_ON_RT_MUTEX
+         or FUTEX_LOCK_PI

So here the events and atoms are similar to the ones in sleep, but since we fail on the waking event, we are going to see it from the perspective of the waker task, right?

Quoted 6 lines

But are those really equivalent? Why do we do RT and USER_THREAD here while there is a much more nuanced set of conditions in sleep?

If I understand it correctly, sleep can monitor some kernel threads but this monitor does not, is there a reason for that? Are we just not interested in the waker for kernel threads?

Urgh, initially I had a patch which drops kernel threads in the sleep monitor, but finally I decided to drop that patch. This is the leftover of that.

You are right that we should be consistent and consider kernel threads here as well.

Quoted 5 lines

I'm not sure if there is any better terminology, but "waking" task makes me think of the task that is about to be woken, though it can mean also that task that is waking another (what you probably mean here).

What about using the waker/wakee terminology?

waker/wakee would be clearer.

Quoted 6 lines

I see the kernel (events/sched.h) uses waking as well, but it says waking context (which a bit clearer to me than waking task). May be worth running it through an LLM which can produce more English-native unambiguous wording, or maybe I'm just flipping..

Also please document it in Documentation/trace/rv/monitor_rtapp.rst

Right, thanks for the reminder.

Nam