From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.15 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796235364; b=wM6ZEYLuZgF87iXqx43LPFrj6iw+cjzQtnd0hUn8dsvkjnngoRAT5Ks2BQiJjMsR49mx d1WIoopReF2XKeH6N5a4grXHOTOxmDMdR6ThTBVsUwO3nPbx7e1DmZhI5kvEecsw6nwkK KwSWsmvjQEWjGjdp9uX3kka7wq70MjgGIPltVcP3EreX1DEsZdMW/0hYGbLLhPasvBfDN KyjznKIQwO4oQqRAdLd4gtrxMsCuDWDp259f4ugiDXxdDNNxHRt+RtZL3zC9VHqsXw4i/ rTG5ioifsANmDlepLzFgxNUnXzS9fKdmRFnBMERkrAtacBtwyBMf2O+xnVfrFbtD/6A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796235364; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:Content-Language:From:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=N6lDfHyfgGCQYHBrlHR/ctymKt9xP7j0uDL6JjIVmCk=; b=bhMqfdtVUtWc7qP36fgTmrpKH3mn6eORm2w38pSe9uO6L1PqfR2wmpcs88+0qYGoa+bG U7lEfetM14RMMYx73bpI111VlGYCS777in1fpk5RqQ6EUDPdoZrrFoFHD9+en3E2+LYKY UXoZ0NI0KGat4z02GvzrV/NyO5LWSbPcjRucDvGFcBC5sddUv/CxLuxAGUsP6DboOnJy9 Ry9nWe7/osAQSnj48MSMk0/HGerWo2q+1KxlBRCL4dOdNCvTsXKkEkA/ab22sn3TZC5gp tu5QU4xWTo3zp/uLwtquBdmMdzxhpAHSYLZVizjnc52sqd0U3JXWiK2BcXq8k2et6cA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=PsJNEKvP; arc=none smtp.remote-ip=95.215.58.15 Received: from mta1.migadu.com (out-15.mta1.migadu.com [95.215.58.15]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93CE5606A322 for ; Wed, 2 Dec 2026 18:16:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 93CE5606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=PsJNEKvP X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=J65gvoWJzY+khUo/Ae/0mKjidkw1U5bLqS+/5Wnh6g8=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796235360; v=1; x=1796840160; b=PsJNEKvPlaInIMZegQJ/OKbWo3yd9U9eFijG46XmAOpNkHOMlngIjb6eWpV5xRn/NHPHN0+8 QnMK5KVwNIaeuplTPcu8F5o7w+EwfPm3JaEM7bErpvYhUIc9QzzDQeBGrQQWJ5Z4iUSki23qaYy C+EMJvGM6Pe3JRDJvJk/ojpk= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id ab011749c69b97f6; Wed, 02 Dec 2026 18:16:00 +0000 X-Mizu-Trace-ID: ab011749c69b97f6 X-Migadu-Flow: FLOW_OUT Message-ID: <085b03b4-ec33-4032-a50c-9774d4205d6e@linux.dev> Date: Thu, 3 Dec 2026 02:15:54 +0800 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] rv: Represent uninitialised and dying DA monitors To: Gabriele Monaco , linux-kernel@vger.kernel.org, Steven Rostedt , Nam Cao , linux-trace-kernel@vger.kernel.org Cc: Karl Mehltretter References: <20261201073453.56226-2-gmonaco@redhat.com> Content-Language: en-US From: Wen Yang In-Reply-To: <20261201073453.56226-2-gmonaco@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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 > --- > > This patch relates to the discussion raised in [1] > > Cc: Karl Mehltretter > Cc: Wen Yang > Cc: Nam Cao > > [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 -- 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