From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 E0D5D606A32A for ; Wed, 28 Oct 2026 15:32:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=10XhLY3P; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=ie83ix/d 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-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=tNuGjVpCCpWRRwNm7lQ2uEnHwZpiJtdBe9DMgOWbUgg=; b=10XhLY3PZGLE0q wN1beNncCLHsgwge604T66I1g3CBS4fSlm/2it7vxiqBLKxJQ96rh4wwROhXRAorFwxzlBNHMOLgB 2uFR4zSaS6HJn8NbFy9DEFHfa9J5CtLlKZcH0zMXQv5VKgYbFZ018cDzh1PFHtqfUszehXKlzdVjF aJmTRBiPDWGlh/eSxq6M5qbTIoXSVXwsH0ogg8VzKiGOod2rXO74bS+UynHY+zbDNwsGEQL7Zytgj LNe+GalqL1tt3L57QoI4b2q2Mw5l8GozyDBJm8++zUcZjVV0VzB9q0O5zZpThNIcl/G4GMz5exTV1 3g0fyky1TVsQ8SUuPaVA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xM5dJ-00000007OeS-2q6i; Wed, 28 Oct 2026 15:32:09 +0000 Received: from out-20.mta0.migadu.com ([2001:41d0:1004:224b::14] helo=mta0.migadu.com) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xM5dG-00000007Ocv-1TM1 for linux-arm-kernel@lists.infradead.org; Wed, 28 Oct 2026 15:32:07 +0000 X-Envelope-To: linux-arm-kernel@lists.infradead.org DKIM-Signature: a=rsa-sha256; bh=jRMgEeT/ywsGUFgWQfUZIBCXSXNOChhJpeoxPBa3hhY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1793201523; v=1; x=1793806323; b=ie83ix/dHSqljXkwuZVu1bU3xefrb1t5SmJiaZR8otsTzWZLEc1ZRO0i8VGP68TR2hAVwmOf yyLkNiIAdYQRCewmF62BQALvLZu5Uwge5aXA0MKunv/M10xSwTgncWPhTHkvGO73OU2DMdz0H0r aXOJSdvO1O5M+70ag2TdrjSQ= X-Envelope-To: linux-arm-kernel@lists.infradead.org Received: by smtp.migadu.com with ESMTPS id 4642add3ff1b0393; Wed, 28 Oct 2026 15:32:03 +0000 X-Mizu-Trace-ID: 4642add3ff1b0393 X-Migadu-Flow: FLOW_OUT From: Fuad Tabba To: Marc Zyngier , Oliver Upton , kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: Joey Gouly , Steffen Eiden , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon , Mark Rutland , Vincent Donnefort , kernel-team@android.com, linux-kernel@vger.kernel.org, Oculytic , Fuad Tabba Subject: [PATCH v3 2/3] KVM: arm64: Pass the PMU events to the hyp vCPU of non-protected guests Date: Wed, 28 Oct 2026 15:31:51 +0000 Message-ID: <20261028153152.1740044-3-fuad.tabba@linux.dev> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261028153152.1740044-1-fuad.tabba@linux.dev> References: <20261028153152.1740044-1-fuad.tabba@linux.dev> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261028_083206_526148_6718C264 X-CRM114-Status: GOOD ( 10.71 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-18.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Oliver Upton Before every run, the host records in vcpu->arch.pmu.events which perf counters to disable and enable around guest entry, and __pmu_switch_to_guest() applies them at EL2. Since commit be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()"), protected mode runs the hyp vCPU instead, and flush_hyp_vcpu() never copies the events into it. So the counters backing a non-protected guest's PMU are never enabled, and every counter the guest reads stays at 0, the cycle counter included. The host's counters that exclude guests keep counting the guest as well. Copy the events into the hyp vCPU of a non-protected guest on every run. The events only exist with CONFIG_HW_PERF_EVENTS, so the copy is built under it. Like mdcr_el2, they're host-controlled and never reach a protected guest. Fixes: be66e67f1750 ("KVM: arm64: Use the pKVM hyp vCPU structure in handle___kvm_vcpu_run()") Link: https://lore.kernel.org/all/20240305184840.636212-2-oliver.upton@linux.dev/ Signed-off-by: Oliver Upton [Fuad: non-protected copy only, tested on the hyp vCPU, under CONFIG_HW_PERF_EVENTS, new message] Tested-by: Vincent Donnefort Reviewed-by: Vincent Donnefort Co-developed-by: Fuad Tabba Signed-off-by: Fuad Tabba --- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index d715aa942577e..28a292deecb64 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -690,6 +690,16 @@ static void sync_debug_state(struct pkvm_hyp_vcpu *hyp_vcpu) host_vcpu->arch.external_debug_state = hyp_vcpu->vcpu.arch.external_debug_state; } +#ifdef CONFIG_HW_PERF_EVENTS +static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu) +{ + if (!pkvm_hyp_vcpu_is_protected(hyp_vcpu)) + hyp_vcpu->vcpu.arch.pmu.events = hyp_vcpu->host_vcpu->arch.pmu.events; +} +#else +static void flush_pmu_state(struct pkvm_hyp_vcpu *hyp_vcpu) {} +#endif + static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) { struct kvm_vcpu *host_vcpu = hyp_vcpu->host_vcpu; @@ -698,6 +708,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) fpsimd_sve_flush(); flush_debug_state(hyp_vcpu); + flush_pmu_state(hyp_vcpu); /* * If we deal with a non-protected guest and the state is potentially -- 2.47.3