From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A2BA288C08; Mon, 20 Apr 2026 07:01:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776668515; cv=none; b=Kn1qUmUFfW4q6iA5rM49cR/2Rf7458yQrymAG9LZmziWHF9oWh45+/gx4fVKcqxujm4rXb+X8hnzz6bRxd6sCUP5AfszyKziHJaxM1PvLaaLv616qeWYqGvrOCvoa86ulX5gaIADwVFLDUtPhxRFLmx+dmHEbqjo4Kl0E9uO6S0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776668515; c=relaxed/simple; bh=2HubVmemKYFSrpjdVlLvyLMzuA44GJ7AxdEWaTIopnc=; h=Date:Message-ID:From:To:Cc:Subject:In-Reply-To:References: MIME-Version:Content-Type; b=OK2wyIhOWXa5us76790Qw0ygWzVIB3ike1cQ7r3tqfTFoYEW9m8mNYxBYfVxx0vBlmDIv/Z5FWzwCIF7Y6yKvi3dX7cVIcV7kAnh2xL9/UpDQNngFpKVgWz72DnLXu7wZ8HBMbeYmTSnZFdESVJ+8PyFtmwwbmI0UgZKUZpU7No= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YOoS5Wx6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YOoS5Wx6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95BBFC19425; Mon, 20 Apr 2026 07:01:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1776668514; bh=2HubVmemKYFSrpjdVlLvyLMzuA44GJ7AxdEWaTIopnc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=YOoS5Wx6pMZKMZlHW21lbBbviCZhxubZdgxsA5GEOdEF+RMDw+un8C/pDE2dE88W1 QIPOW1WJ9wC+EUw+ikTEoHL/aHm8POBVuM99jhiygKKqp8sluMUIQKxs7Fm8q45l2R FD/lAPTX3x51DyEOcxfERLKVSxw1mOFWkJ/Q1RDJ6woo8j5PXAMGu76M75jfBaxvMQ iB5Eg8bM2xbBf1fSvLksohlm4V1RiGmP+aAr4E3Qq5cK5Op3ox663otj5ljWjc1psQ 6c8cEwZgIq1aYGMGHLnhY7HX6UXVDMByfegV4FefqoJqMO0NS8404or0Dzmw/FM0O/ qC9LVkf/tnk6w== Received: from sofa.misterjones.org ([185.219.108.64] helo=goblin-girl.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wEidk-0000000CylX-00qd; Mon, 20 Apr 2026 07:01:52 +0000 Date: Mon, 20 Apr 2026 08:01:51 +0100 Message-ID: <86se8q15eo.wl-maz@kernel.org> From: Marc Zyngier To: Akihiko Odaki Cc: Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Kees Cook , "Gustavo A. R. Silva" , Paolo Bonzini , Jonathan Corbet , Shuah Khan , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, devel@daynix.com, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: Re: [PATCH v7 2/4] KVM: arm64: PMU: Protect the list of PMUs with RCU In-Reply-To: <483e5cf2-a54c-4781-ac6d-49f5bc7128ba@rsg.ci.i.u-tokyo.ac.jp> References: <20260418-hybrid-v7-0-2bf39ad009bf@rsg.ci.i.u-tokyo.ac.jp> <20260418-hybrid-v7-2-2bf39ad009bf@rsg.ci.i.u-tokyo.ac.jp> <87mryzauib.wl-maz@kernel.org> <483e5cf2-a54c-4781-ac6d-49f5bc7128ba@rsg.ci.i.u-tokyo.ac.jp> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: odaki@rsg.ci.i.u-tokyo.ac.jp, oupton@kernel.org, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, kees@kernel.org, gustavoars@kernel.org, pbonzini@redhat.com, corbet@lwn.net, shuah@kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org, devel@daynix.com, kvm@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false On Mon, 20 Apr 2026 07:21:45 +0100, Akihiko Odaki wrote: > > On 2026/04/19 23:34, Marc Zyngier wrote: > > On Sat, 18 Apr 2026 09:14:24 +0100, > > Akihiko Odaki wrote: > >> > >> Convert the list of PMUs to a RCU-protected list that has primitives to > >> avoid read-side contention. > >> > >> Signed-off-by: Akihiko Odaki > >> --- > >> arch/arm64/kvm/pmu-emul.c | 14 ++++++-------- > >> 1 file changed, 6 insertions(+), 8 deletions(-) > >> > >> diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c > >> index 59ec96e09321..ef5140bbfe28 100644 > >> --- a/arch/arm64/kvm/pmu-emul.c > >> +++ b/arch/arm64/kvm/pmu-emul.c > >> @@ -7,9 +7,9 @@ > >> #include > >> #include > >> #include > >> -#include > >> #include > >> #include > >> +#include > >> #include > >> #include > >> #include > >> @@ -26,7 +26,6 @@ static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc); > >> bool kvm_supports_guest_pmuv3(void) > >> { > >> - guard(mutex)(&arm_pmus_lock); > >> return !list_empty(&arm_pmus); > > > > Please read include/linux/rculist.h and the discussion about the > > interaction of list_empty() with RCU-protected lists. How about using > > list_first_or_null_rcu() for peace of mind? > > list_first_or_null_rcu() is useful to replace a sequence of > list_empty() and list_first_entry() that is protected by a lock, but > this function instead requires the invariant that nobody deletes an > element from the list, and list_first_or_null_rcu() does not allow > removing the requirement. > > The header file says: > > Where are list_empty_rcu() and list_first_entry_rcu()? > > > > They do not exist because they would lead to subtle race conditions: > > > > if (!list_empty_rcu(mylist)) { > > struct foo *bar = list_first_entry_rcu(mylist, struct foo, > > list_member); > > do_something(bar); > > } > > > > The list might be non-empty when list_empty_rcu() checks it, but it > > might have become empty by the time that list_first_entry_rcu() > > rereads the ->next pointer, which would result in a SEGV. > > > > When not using RCU, it is OK for list_first_entry() to re-read that > > pointer because both functions should be protected by some lock that > > blocks writers. > > > > When using RCU, list_empty() uses READ_ONCE() to fetch the > > RCU-protected ->next pointer and then compares it to the address of > > the list head. However, it neither dereferences this pointer nor > > provides this pointer to its caller. Thus, READ_ONCE() suffices > > (that is, rcu_dereference() is not needed), which means that > > list_empty() can be used anywhere you would want to use > > list_empty_rcu(). Just don't expect anything useful to happen if you > > do a subsequent lockless call to list_first_entry_rcu()!!! > > > > See list_first_or_null_rcu for an alternative. > > However, kvm_supports_guest_pmuv3() locked a mutex when calling > list_empty() and unlocked it immediately after that, instead of > re-reading list_first_entry(). This construct inherently had a race > condition with code that deletes an element; when the caller of > kvm_supports_guest_pmuv3() decides to enable guest PMUv3, the host PMU > may have been gone. But it was still safe because no one deletes an > element. > > The same logic also applies when using RCU. As the comment says, we > can use list_empty() instead of the hypothetical list_empty_rcu() > macro because we don't expect it to magically enable something like > list_first_entry_rcu(). This function instead keep relying on the fact > that no one deletes an element of the list. And that's exactly the sort of thing I am trying to plan for. *Should* we introduce a way to remove PMUs from the list, this predicate becomes unsafe. So I want at least a comment explaining this to the unsuspecting reader, as this is rather subtle. M. -- Without deviation from the norm, progress is not possible.