From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793201526; b=06ZyQkkpGsA3BdUuQKZme1c4FhDPHjUSmbvmqmDFlC3s7wyfExYqBpefB9lQlRo7EJBh 3dHB5qIUM7AFkCTsGP14xc0xWIEQJyBalLdc/+JsMjMn6onc/HgbpxBlRjSzjAg5pcZkW D8QZa8XpX78mpzzMXsCqPYDktwcGP/InuOUgWQvqJWdkc1xQ5krV6zE1NAmyrDUHqhxAe jAU9PjK1KOqk7gWR1fqIYkGD/SGUfWJhxBkTUSkAw47RRiMYCyv72znaDimOMUh9bgJ8A LpnjovF6J2T3LBK8a6KgNDpFjkjBc+tegSvnePwIocy/LZAUmwafTdX46WvJgtPGsUA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793201526; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=tNuGjVpCCpWRRwNm7lQ2uEnHwZpiJtdBe9DMgOWbUgg=; b=xIWa1IMOoSQPpBDRb6oFQSoFla/JmgLjp5LF+K+DElfEoZBbHHj4PL1s3JF9tF/y4rkX +9zNA3uoTgHhhIlv20VYWoI63QbnmVWCqYao+UYOlslxANpg9I4YPnoITnlv+g2lUlwXO dk8qoNKMOGkml9KgXG4wFuCSd66o/sBytBzxhglPKAhrsrDOl1DtyRUrfbBXLetC7wL4M HdA2kwb13DQZHs3y8dgEsS40xBwe3Uw0mjDW2++TS9oQjUF2MgYMVwHtYXKfwi804vMt2 iI3nO76T5gL7cWtc17ll+HqmHdQ3JqO1Rqan3AHrGyZXHdMeMgsDQZ9lslCQutdL8Yg== 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=lYaw0wP2; arc=none smtp.remote-ip=91.218.175.18 Received: from mta0.migadu.com (out-18.mta0.migadu.com [91.218.175.18]) (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 D2B0B606A324 for ; Wed, 28 Oct 2026 15:32:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2B0B606A324 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=lYaw0wP2 X-Envelope-To: linux-kernel@vger.kernel.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=1793201524; v=1; x=1793806324; b=lYaw0wP2MvxggRGSjHvCVwgFvxxpSvyghKhU1rmVbY3Ve+Lnt1i9GS2sJz71yHNivRjQ8UdL 0qsT1ABMmi6XURuEuZD4ip158/Ks1gHdaHNT0F45U6ovSAeprr6/e9xaFC+Gq5qLthWJjjh76DL Q8SKEaPp52lGf+rT43dr7Djw= X-Envelope-To: linux-kernel@vger.kernel.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-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 Content-Transfer-Encoding: 8bit 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