From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796110633; b=dgvqp2m098H6PXDLKnOGLN4Cz8QuW4emZ6ewALkcMLp23FT08FCPC5Qp6PRUp+KP9WPn s/Ep8VWYHxRKnY2nbMPxFMn4BC0ciMZ5Yalrf9/urVS49AK0LqwvpUICe9xqH8Pdjm9LV 5brQQd85VjS59H/rfyXYo0h4/3i0gOPRG4/SmcwvUeDC7KnrfeJ7qTbmMASYfkYF9OPFL wOeHf3Fl9109sDMBRswp1fzTJ7WrE8MsDXVVtg51n/asPUGUc7f1NKlQht0pDSl3GcrEt ylF0S+q2wSLRUToDw56k+4FHSmfkgFdt06eSZb3q+cZtu3LqSoFYaWzfOzdCYAkPnMQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796110633; h=DMARC-Filter:DKIM-Signature:X-MC-Rep:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:MIME-Version:X-Scanned-By:X-Mimecast-MFC-PROC-ID: X-Mimecast-Originator:Content-Transfer-Encoding:content-type; bh=WKa7Or6+ElgGaYPDysTmQCqsdhUD7TpIi9cjxGMkGuM=; b=A0AFdsMtpl26BL96j2PnoHFv44cfyheac5bHeiiTiTVbxMBzOhgJu5U+FS5tFVL3KVo/ wCNLUSjN9aUWl7/1GImekcq7sMh7tMsKvI2mYnxWTCWbQHG93W2dNQ7u0yXR0wf2KbzbZ Om8NONcUt0zdAIjLHPJvUf8ZbJcwqdwUKZHUK7yAh1B10BpxaxDOlbU4N7JRjOSIGSem1 r2nf4qdoAP2oG69PzHa3wovn6i7a+/p78IuQUy+6P2f7FnjlgWTst4VxY4bremxn6rU8+ 0NdAkMIFqUI6bYeQH4oWJr8h9NMzZGOdUXtW+QpPhHUz6e/L8i5a4Q7XlG+UzNjn2bQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=D8EFvM0K; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A49DC606A31A for ; Tue, 1 Dec 2026 07:37:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A49DC606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=D8EFvM0K DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796110632; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=WKa7Or6+ElgGaYPDysTmQCqsdhUD7TpIi9cjxGMkGuM=; b=D8EFvM0KqDA4HOsUF4/pmuhllise5h1+jDfQGRCF12rM+uqdh1rA1Zrd8C0Mpp4ALvfGmw 9bIGT51IN53CSicp7VPWls9oAK9NjO/QH5+Big7rutOW3h7N2frHAFwOyIk7oMACbtcRTP QFvd+ZA9xh4FJiToGs8SS0Pz2m29fRw= X-MC-Rep: binary re single Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-159-NJPznuGjOfe-ql6_Rcv0TA-1; Tue, 1 Dec 2026 07:37:07 +0000 X-MC-Unique: NJPznuGjOfe-ql6_Rcv0TA-1 X-Mimecast-MFC-AGG-ID: NJPznuGjOfe-ql6_Rcv0TA_1796110626 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 70110195FE21; Tue, 1 Dec 2026 07:37:06 +0000 (UTC) Received: from gmonaco-thinkpadt14gen3.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 97794300022B; Tue, 1 Dec 2026 07:37:03 +0000 (UTC) From: Gabriele Monaco To: linux-kernel@vger.kernel.org, Steven Rostedt , Gabriele Monaco , Nam Cao , linux-trace-kernel@vger.kernel.org Cc: Karl Mehltretter , Wen Yang Subject: [PATCH] rv: Represent uninitialised and dying DA monitors Date: Tue, 1 Dec 2026 08:34:53 +0100 Message-ID: <20261201073453.56226-2-gmonaco@redhat.com> X-Mailing-List: linux-trace-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-MFC-PROC-ID: AbYs7RJ9VlvLOR5rCJlmurLrb0Aoo7qo-r0nTVvpPfw_1796110626 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true 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 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