The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Wen Yang <wen.yang@linux.dev>
To: Gabriele Monaco <gmonaco@redhat.com>,
	linux-kernel@vger.kernel.org,
	Steven Rostedt <rostedt@goodmis.org>,
	Nam Cao <namcao@linutronix.de>,
	linux-trace-kernel@vger.kernel.org
Cc: Karl Mehltretter <kmehltretter@gmail.com>
Subject: Re: [PATCH] rv: Represent uninitialised and dying DA monitors
Date: Thu, 3 Dec 2026 02:15:54 +0800	[thread overview]
Message-ID: <085b03b4-ec33-4032-a50c-9774d4205d6e@linux.dev> (raw)
In-Reply-To: <20261201073453.56226-2-gmonaco@redhat.com>



On 12/1/26 15:34, 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 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
> 

Hi Gabriele,

Thanks, this is exactly what we need.
We v9 series will rebase on top of it.

One question: started now guards the timer instead of monitoring, which
was published with release/acquire. Should it follow the same pattern?

	...
	smp_store_release(&da_mon->started, true);
	return smp_load_acquire(&da_mon->started);

Reviewed-by: Wen Yang <wen.yang@linux.dev>

--
Best wishes,
Wen


>   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

      parent reply	other threads:[~2026-12-02 18:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  7:34 [PATCH] rv: Represent uninitialised and dying DA monitors Gabriele Monaco
2026-12-01 15:36 ` Gabriele Monaco
2026-12-02 18:15 ` Wen Yang [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=085b03b4-ec33-4032-a50c-9774d4205d6e@linux.dev \
    --to=wen.yang@linux.dev \
    --cc=gmonaco@redhat.com \
    --cc=kmehltretter@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=namcao@linutronix.de \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox