Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH] rv: Represent uninitialised and dying DA monitors
@ 2026-12-01  7:34 Gabriele Monaco
  2026-12-01  7:57 ` sashiko-bot
  2026-12-01 15:36 ` Gabriele Monaco
  0 siblings, 2 replies; 3+ messages in thread
From: Gabriele Monaco @ 2026-12-01  7:34 UTC (permalink / raw)
  To: linux-kernel, Steven Rostedt, Gabriele Monaco, Nam Cao,
	linux-trace-kernel
  Cc: Karl Mehltretter, Wen Yang

DA monitors overload the monitoring field to represent states that don't
belong to it. For example, it is used to understand whether a timer (HA)
was set up and hence needs cancelling.
Racing events may cause a monitor to reset it's monitoring flag while
concurrently a timer is started, so the monitoring flag loses meaning.

Add 2 new flags in the struct da_monitor without increasing it's size:
* started: the monitor was started at least once
* dead: the destruction of the monitor was started

These flags keep their status on reset and allow to understand when a
monitor needs a timer to be set-up, can cancel an already initialised
timer, or should stop starting/handling events to avoid races.

These flags don't avoid use after free issues by themselves, but coupled
with an RCU grace period before freeing monitors (where applicable), can
guarantee no resources are used after free.

Fixes: 700782ec8f65 ("rv: Add automatic cleanup handlers for per-task HA monitors")
Fixes: f5587d1b6ec9 ("rv: Add Hybrid Automata monitor type")
Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
---

This patch relates to the discussion raised in [1]

Cc: Karl Mehltretter <kmehltretter@gmail.com>
Cc: Wen Yang <wen.yang@linux.dev>
Cc: Nam Cao <namcao@linutronix.de>

[1] - https://lore.kernel.org/lkml/a2ae62be718eb7401d19a4fd3b70a8e54198ec54.camel@redhat.com

 include/linux/rv.h      |  2 ++
 include/rv/da_monitor.h | 62 ++++++++++++++++++++++++++++++++---------
 include/rv/ha_monitor.h |  8 ++++--
 3 files changed, 56 insertions(+), 16 deletions(-)

diff --git a/include/linux/rv.h b/include/linux/rv.h
index 541ba404926a..23aca5d57eed 100644
--- a/include/linux/rv.h
+++ b/include/linux/rv.h
@@ -26,6 +26,8 @@
  */
 struct da_monitor {
 	bool		monitoring;
+	bool		started;
+	bool		dead;
 	unsigned int	curr_state;
 };
 
diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
index 4d479d2bb12d..6f0822fe31b6 100644
--- a/include/rv/da_monitor.h
+++ b/include/rv/da_monitor.h
@@ -105,6 +105,32 @@ static inline void da_monitor_reset_state(struct da_monitor *da_mon)
 	smp_store_release(&da_mon->curr_state, model_get_initial_state());
 }
 
+/*
+ * da_monitor_reset_init - reset a monitor and clear the startup flags
+ */
+static inline void da_monitor_reset_init(struct da_monitor *da_mon)
+{
+	WRITE_ONCE(da_mon->started, false);
+	WRITE_ONCE(da_mon->dead, false);
+	da_monitor_reset_state(da_mon);
+}
+
+/*
+ * da_monitor_started - returns true once da_monitor_start() ran
+ */
+static inline bool da_monitor_started(struct da_monitor *da_mon)
+{
+	return READ_ONCE(da_mon->started);
+}
+
+/*
+ * da_monitor_dead - returns true once the monitor storage is being destroyed
+ */
+static inline bool da_monitor_dead(struct da_monitor *da_mon)
+{
+	return READ_ONCE(da_mon->dead);
+}
+
 /*
  * da_monitor_reset - reset a monitor and setting it to init state
  */
@@ -124,6 +150,7 @@ static inline void da_monitor_start(struct da_monitor *da_mon)
 {
 	da_mon->curr_state = model_get_initial_state();
 	da_monitor_init_hook(da_mon);
+	WRITE_ONCE(da_mon->started, true);
 	/* Pairs with smp_load_acquire in da_monitoring(). */
 	smp_store_release(&da_mon->monitoring, 1);
 }
@@ -165,6 +192,10 @@ static inline bool da_monitor_handling_event(struct da_monitor *da_mon)
 	if (unlikely(!da_monitoring(da_mon)))
 		return 0;
 
+	/* storage is going away: a stale reader may still resolve it */
+	if (unlikely(da_monitor_dead(da_mon)))
+		return 0;
+
 	return 1;
 }
 
@@ -204,11 +235,11 @@ static void da_monitor_reset_all(void)
 }
 
 /*
- * da_monitor_reset_state_all - reset the single monitor
+ * da_monitor_reset_init_all - reset the single monitor
  */
-static inline void da_monitor_reset_state_all(void)
+static inline void da_monitor_reset_init_all(void)
 {
-	__da_monitor_reset_all(da_monitor_reset_state);
+	__da_monitor_reset_all(da_monitor_reset_init);
 }
 
 /*
@@ -216,7 +247,7 @@ static inline void da_monitor_reset_state_all(void)
  */
 static inline int da_monitor_init(void)
 {
-	da_monitor_reset_state_all();
+	da_monitor_reset_init_all();
 	return 0;
 }
 
@@ -274,11 +305,11 @@ static void da_monitor_reset_all(void)
 }
 
 /*
- * da_monitor_reset_state_all - reset all CPUs' monitor
+ * da_monitor_reset_init_all - reset all CPUs' monitor
  */
-static inline void da_monitor_reset_state_all(void)
+static inline void da_monitor_reset_init_all(void)
 {
-	__da_monitor_reset_all(da_monitor_reset_state);
+	__da_monitor_reset_all(da_monitor_reset_init);
 }
 
 /*
@@ -286,7 +317,7 @@ static inline void da_monitor_reset_state_all(void)
  */
 static inline int da_monitor_init(void)
 {
-	da_monitor_reset_state_all();
+	da_monitor_reset_init_all();
 	return 0;
 }
 
@@ -363,9 +394,9 @@ static void da_monitor_reset_all(void)
 	__da_monitor_reset_all(da_monitor_reset);
 }
 
-static inline void da_monitor_reset_state_all(void)
+static inline void da_monitor_reset_init_all(void)
 {
-	__da_monitor_reset_all(da_monitor_reset_state);
+	__da_monitor_reset_all(da_monitor_reset_init);
 }
 
 /*
@@ -384,7 +415,7 @@ static int da_monitor_init(void)
 
 	task_mon_slot = slot;
 
-	da_monitor_reset_state_all();
+	da_monitor_reset_init_all();
 	return 0;
 }
 
@@ -572,6 +603,7 @@ static inline void da_destroy_storage(da_id_type id)
 
 	if (!mon_storage)
 		return;
+	WRITE_ONCE(mon_storage->rv.da_mon.dead, true);
 	da_monitor_reset_hook(&mon_storage->rv.da_mon);
 	hash_del_rcu(&mon_storage->node);
 	kfree_rcu(mon_storage, rcu);
@@ -592,9 +624,9 @@ static void da_monitor_reset_all(void)
 	__da_monitor_reset_all(da_monitor_reset);
 }
 
-static inline void da_monitor_reset_state_all(void)
+static inline void da_monitor_reset_init_all(void)
 {
-	__da_monitor_reset_all(da_monitor_reset_state);
+	__da_monitor_reset_all(da_monitor_reset_init);
 }
 
 static inline int da_monitor_init(void)
@@ -747,6 +779,8 @@ static inline bool __da_handle_start_event(struct da_monitor *da_mon,
 {
 	if (!da_monitor_enabled())
 		return 0;
+	if (unlikely(da_monitor_dead(da_mon)))
+		return 0;
 	if (unlikely(!da_monitoring(da_mon))) {
 		da_monitor_start(da_mon);
 		return 0;
@@ -762,6 +796,8 @@ static inline bool __da_handle_start_run_event(struct da_monitor *da_mon,
 {
 	if (!da_monitor_enabled())
 		return 0;
+	if (unlikely(da_monitor_dead(da_mon)))
+		return 0;
 	if (unlikely(!da_monitoring(da_mon)))
 		da_monitor_start(da_mon);
 
diff --git a/include/rv/ha_monitor.h b/include/rv/ha_monitor.h
index 0952a3f161c3..2ad3111142f1 100644
--- a/include/rv/ha_monitor.h
+++ b/include/rv/ha_monitor.h
@@ -186,7 +186,8 @@ static inline void ha_monitor_init_env(struct da_monitor *da_mon)
 	struct ha_monitor *ha_mon = to_ha_monitor(da_mon);
 
 	ha_monitor_reset_all_stored(ha_mon);
-	ha_setup_timer(ha_mon);
+	if (!da_monitor_started(da_mon))
+		ha_setup_timer(ha_mon);
 }
 
 /*
@@ -201,7 +202,7 @@ static inline void ha_monitor_reset_env(struct da_monitor *da_mon)
 {
 	struct ha_monitor *ha_mon = to_ha_monitor(da_mon);
 
-	if (likely(da_monitoring(da_mon)))
+	if (likely(da_monitor_started(da_mon)))
 		ha_cancel_timer(ha_mon);
 }
 
@@ -249,7 +250,8 @@ static void ha_handle_sched_process_exit(void *data, struct task_struct *p,
 {
 	struct da_monitor *da_mon = da_get_monitor(p);
 
-	if (likely(da_monitoring(da_mon))) {
+	WRITE_ONCE(da_mon->dead, true);
+	if (likely(da_monitor_started(da_mon))) {
 		da_monitor_reset(da_mon);
 		ha_cancel_timer_sync(to_ha_monitor(da_mon));
 	}

base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rv: Represent uninitialised and dying DA monitors
  2026-12-01  7:34 [PATCH] rv: Represent uninitialised and dying DA monitors Gabriele Monaco
@ 2026-12-01  7:57 ` sashiko-bot
  2026-12-01 15:36 ` Gabriele Monaco
  1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-01  7:57 UTC (permalink / raw)
  To: Gabriele Monaco, linux-trace-kernel

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] rv: missing release-acquire ordering on started in
  da_monitor_started()
- [Medium] rv: TOCTOU race on da_mon->dead leaves active timer in freed
  storage
- [Medium] rv: uncanceled lingering timer in ha_monitor_init_env() on
  restart
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [Critical] rv: data race in __da_handle_start_run_event() [1]
--

commit 6a4e0fa63b29a97676bbea1165f00da0397f7006
Author: Gabriele Monaco <gmonaco@redhat.com>

rv: Represent uninitialised and dying DA monitors

This commit adds started and dead flags to struct da_monitor to track
whether a monitor has been initialized at least once and whether its
storage is being destroyed, updating HA timer setup and cleanup paths to
check these flags instead of monitoring.

> diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h
> index 6fc5ef8142aca..807547639a42c 100644
> --- a/include/rv/da_monitor.h
> +++ b/include/rv/da_monitor.h
> @@ -96,6 +96,32 @@ static inline void da_monitor_reset_state(struct da_monitor *da_mon)
>  	smp_store_release(&da_mon->curr_state, model_get_initial_state());
>  }
>  
> +/*
> + * da_monitor_reset_init - reset a monitor and clear the startup flags
> + */
> +static inline void da_monitor_reset_init(struct da_monitor *da_mon)
> +{
> +	WRITE_ONCE(da_mon->started, false);
> +	WRITE_ONCE(da_mon->dead, false);
> +	da_monitor_reset_state(da_mon);
> +}
> +
> +/*
> + * da_monitor_started - returns true once da_monitor_start() ran
> + */
> +static inline bool da_monitor_started(struct da_monitor *da_mon)
> +{
> +	return READ_ONCE(da_mon->started);
> +}
> +
> +/*
> + * da_monitor_dead - returns true once the monitor storage is being destroyed
> + */
> +static inline bool da_monitor_dead(struct da_monitor *da_mon)
> +{
> +	return READ_ONCE(da_mon->dead);
> +}
> +
>  /*
>   * da_monitor_reset - reset a monitor and setting it to init state
>   */
> @@ -115,6 +141,7 @@ static inline void da_monitor_start(struct da_monitor *da_mon)
>  {
>  	da_mon->curr_state = model_get_initial_state();
>  	da_monitor_init_hook(da_mon);
> +	WRITE_ONCE(da_mon->started, true);
>  	/* Pairs with smp_load_acquire in da_monitoring(). */
>  	smp_store_release(&da_mon->monitoring, 1);
>  }

[Severity: Medium]
Does da_mon->started need release-acquire ordering between
da_monitor_start() and da_monitor_started()?

Commit 2b9a8c27417e ("rv: Fix monitor start ordering and memory ordering
for monitoring flag") paired smp_store_release(&da_mon->monitoring, 1) with
smp_load_acquire(&da_mon->monitoring) so that timer initialization stores
in da_monitor_init_hook() -> ha_setup_timer() are visible before another
CPU observes monitoring == 1.

This patch replaces da_monitoring(da_mon) with da_monitor_started(da_mon)
in ha_monitor_reset_env() and ha_handle_sched_process_exit(), but accesses
da_mon->started with plain WRITE_ONCE() and READ_ONCE().

CPU0                              CPU1
da_monitor_start()
  da_monitor_init_hook()
    ha_setup_timer()
  WRITE_ONCE(da_mon->started, 1)
                                  ha_monitor_reset_env()
                                    if (da_monitor_started(da_mon))
                                      ha_cancel_timer(ha_mon)
                                        hrtimer_try_to_cancel()
                                          /* hrtimer->base == NULL */

On weakly ordered architectures such as arm64, CPU1 can observe
da_mon->started == true before the stores in ha_setup_timer() are visible,
causing ha_cancel_timer() or ha_cancel_timer_sync() to operate on an
uninitialized timer and dereference a NULL hrtimer->base.  Using
smp_store_release(&da_mon->started, true) paired with
smp_load_acquire(&da_mon->started) would preserve the ordering.

[ ... ]

> @@ -562,6 +593,7 @@ static inline void da_destroy_storage(da_id_type id)
>  
>  	if (!mon_storage)
>  		return;
> +	WRITE_ONCE(mon_storage->rv.da_mon.dead, true);
>  	da_monitor_reset_hook(&mon_storage->rv.da_mon);
>  	hash_del_rcu(&mon_storage->node);
>  	kfree_rcu(mon_storage, rcu);

[Severity: Medium]
Can an in-flight event handler that passed da_monitor_dead() before dead is
set arm the timer after da_destroy_storage() (or
ha_handle_sched_process_exit()) has already cancelled it?

CPU0                              CPU1
__da_handle_start_run_event()
  if (da_monitor_dead(da_mon))
                                  da_destroy_storage()
                                    WRITE_ONCE(da_mon.dead, true)
                                    da_monitor_reset_hook()
                                      /* started == false, skips cancel */
                                    hash_del_rcu(&mon_storage->node)
                                    kfree_rcu(mon_storage, rcu)
  da_monitor_start(da_mon)
    ha_setup_timer()
  __da_handle_event_common()
    ...
      ha_start_timer_ns()  <-- timer armed after cancel

The same race occurs if da_mon->started was already true and CPU0 is inside
da_event() before reaching ha_setup_invariants() -> ha_start_timer_ns()
when CPU1 runs da_monitor_reset_hook().  Once the RCU grace period elapses,
mon_storage (or task_struct for per-task monitors) is freed while the timer
remains queued, causing a use-after-free when the timer expires.

[ ... ]

> @@ -736,6 +768,8 @@ static inline bool __da_handle_start_event(struct da_monitor *da_mon,
>  {
>  	if (!da_monitor_enabled())
>  		return 0;
> +	if (unlikely(da_monitor_dead(da_mon)))
> +		return 0;
>  	if (unlikely(!da_monitoring(da_mon))) {
>  		da_monitor_start(da_mon);
>  		return 0;
> @@ -751,6 +785,8 @@ static inline bool __da_handle_start_run_event(struct da_monitor *da_mon,
>  {
>  	if (!da_monitor_enabled())
>  		return 0;
> +	if (unlikely(da_monitor_dead(da_mon)))
> +		return 0;
>  	if (unlikely(!da_monitoring(da_mon)))
>  		da_monitor_start(da_mon);

[Severity: Critical]
This problem wasn't introduced by this patch, but nothing serializes
concurrent start events in __da_handle_start_run_event() or
__da_handle_start_event() [1], can two CPUs still call
da_monitor_start() and ha_setup_timer() concurrently on the same monitor?

CPU0                              CPU1
__da_handle_start_run_event()     __da_handle_start_run_event()
  if (!da_monitoring(da_mon))
                                    if (!da_monitoring(da_mon))
    da_monitor_start()                da_monitor_start()
      ha_monitor_init_env()             ha_monitor_init_env()
        if (!da_monitor_started())        if (!da_monitor_started())
          ha_setup_timer()                  ha_setup_timer()  <-- corruption

Both CPUs can observe !da_monitoring(da_mon) and !da_monitor_started(da_mon)
before either sets started or monitoring, running timer_setup() or
hrtimer_setup() concurrently on the same timer (or re-initializing an
already queued timer) and corrupting timer list and debugobjects state.

> diff --git a/include/rv/ha_monitor.h b/include/rv/ha_monitor.h
> index 6e1c7fe5449a3..67a0d50e99bf2 100644
> --- a/include/rv/ha_monitor.h
> +++ b/include/rv/ha_monitor.h
> @@ -186,7 +186,8 @@ static inline void ha_monitor_init_env(struct da_monitor *da_mon)
>  	struct ha_monitor *ha_mon = to_ha_monitor(da_mon);
>  
>  	ha_monitor_reset_all_stored(ha_mon);
> -	ha_setup_timer(ha_mon);
> +	if (!da_monitor_started(da_mon))
> +		ha_setup_timer(ha_mon);
>  }

[Severity: Medium]
Should ha_monitor_init_env() cancel an existing timer when
da_monitor_started(da_mon) is already true?

When a reset races with an event arming the timer (the scenario described
in the changelog), the monitor is left with monitoring == 0,
started == true, and a queued timer.  On the next start event,
da_monitor_start() -> ha_monitor_init_env() resets env_store[] to
ENV_INVALID_VALUE, skips ha_setup_timer() because started is true, and sets
curr_state to the initial state and monitoring = 1 without cancelling the
lingering timer.  If that timer expires before another state transition
re-arms or cancels it, __ha_monitor_timer_callback() sees
da_monitor_handling_event() == true and triggers a false-positive
ha_react() in the initial state.

[1] https://sashiko.dev/#/bug/linux-554ed765-b9c7-486b-bf80-1a4691988567

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261201073453.56226-2-gmonaco@redhat.com?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rv: Represent uninitialised and dying DA monitors
  2026-12-01  7:34 [PATCH] rv: Represent uninitialised and dying DA monitors Gabriele Monaco
  2026-12-01  7:57 ` sashiko-bot
@ 2026-12-01 15:36 ` Gabriele Monaco
  1 sibling, 0 replies; 3+ messages in thread
From: Gabriele Monaco @ 2026-12-01 15:36 UTC (permalink / raw)
  To: linux-kernel, Steven Rostedt, Nam Cao, linux-trace-kernel
  Cc: Karl Mehltretter, Wen Yang

On Tue, 2026-12-01 at 08:34 +0100, Gabriele Monaco wrote:
> DA monitors overload the monitoring field to represent states that don't
> belong to it. For example, it is used to understand whether a timer (HA)
> was set up and hence needs cancelling.
> Racing events may cause a monitor to reset it's monitoring flag while
> concurrently a timer is started, so the monitoring flag loses meaning.
> 
> Add 2 new flags in the struct da_monitor without increasing it's size:
> * started: the monitor was started at least once
> * dead: the destruction of the monitor was started
> 
> These flags keep their status on reset and allow to understand when a
> monitor needs a timer to be set-up, can cancel an already initialised
> timer, or should stop starting/handling events to avoid races.
> 
> These flags don't avoid use after free issues by themselves, but coupled
> with an RCU grace period before freeing monitors (where applicable), can
> guarantee no resources are used after free.
> 
> Fixes: 700782ec8f65 ("rv: Add automatic cleanup handlers for per-task HA
> monitors")
> Fixes: f5587d1b6ec9 ("rv: Add Hybrid Automata monitor type")
> Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>
> ---

This approach is yet again opening a can of worms..

In the end of the day what Karl proposed (initialise timers once at the
beginning) is probably the best and simplest solution.

I'm going to try and draft something like that, adding a path for per-task
monitors too.

Gabriele

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01 15:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-01  7:34 [PATCH] rv: Represent uninitialised and dying DA monitors Gabriele Monaco
2026-12-01  7:57 ` sashiko-bot
2026-12-01 15:36 ` Gabriele Monaco

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox