Linux Trace Kernel
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Gabriele Monaco" <gmonaco@redhat.com>,
	linux-trace-kernel@vger.kernel.org
Subject: Re: [PATCH] rv: Represent uninitialised and dying DA monitors
Date: Tue, 01 Dec 2026 07:57:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-242802@kernel.org> (raw)
In-Reply-To: <20261201073453.56226-2-gmonaco@redhat.com>

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

      reply	other threads:[~2026-12-01  7:57 UTC|newest]

Thread overview: 2+ 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  7:57 ` sashiko-bot [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=sashiko-outbox-242802@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=gmonaco@redhat.com \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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