From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 99690F36C2A for ; Mon, 20 Apr 2026 07:02:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:MIME-Version: References:In-Reply-To:Subject:Cc:To:From:Message-ID:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=2BbMSuXILs/YeVoonOxKs/M17r2ztnxX+ybvv6ldj+Q=; b=2wb4VG4VTHdH0/FhcRZkn8+u10 TEHpECQE49/e7OnfLT9GQ6V1sBq4TckT7Dx4a109BOQeHCR0noiudY58gPs6Ota1SC6uGPQlPrxTL gLNJLqLaNuhvO+2QBzRzhASOVqxCQqXRcPKqlXfKBOYqXklG34/3kyP9puPc99QjCnHxswbJrPQPV 74mOiZFogkBznOlS7U7nHOcORrO+FsDsZ4Mpdae6qKhTl6p05+EPjmZ58L7bDBVT8VXCu/0aGLOBd vXHdIWo11sZfYAYQL3Zs7SWowZO28fCZGCNdC1NpefM3ohDqNXpItN/1VZmT8btegMJtKX29a+lEL +CerGZ+g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1wEidq-00000006WjL-0n5F; Mon, 20 Apr 2026 07:01:58 +0000 Received: from sea.source.kernel.org ([172.234.252.31]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1wEidn-00000006Wiq-2LW0 for linux-arm-kernel@lists.infradead.org; Mon, 20 Apr 2026 07:01:56 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id C1F5E43CB8; Mon, 20 Apr 2026 07:01:54 +0000 (UTC) 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) 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260420_000155_645130_18FDF50C X-CRM114-Status: GOOD ( 41.60 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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.