All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters
@ 2026-10-23  9:20 Vincent Donnefort
  2026-10-23 12:29 ` Fuad Tabba
  2026-10-23 12:37 ` Marc Zyngier
  0 siblings, 2 replies; 6+ messages in thread
From: Vincent Donnefort @ 2026-10-23  9:20 UTC (permalink / raw)
  To: maz, oupton, kvmarm, linux-arm-kernel
  Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
	will, kernel-team, fuad.tabba, Vincent Donnefort

nr_event_counters is only used by the host to populate mdcr_el2.
However, host_data_ptr() for pKVM switches between the hypervisor copy
of kvm_host_data before it is deprivileged to the host copy after.

So kvm_init_host_debug_data() initialises nr_event_counters in the
hypervisor's kvm_host_data, while kvm_arm_setup_mdcr_el2() reads the
host copy which is 0. With a HPMN of 0, this stops programmable event
counters from counting a non-protected guest.

Always use the host copy for nr_event_counters.

Fixes: 2417218f2f23 ("KVM: arm64: Get rid of __kvm_get_mdcr_el2() and related warts")
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
 arch/arm64/kvm/debug.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
index f4d7b12045e8..a00771339c62 100644
--- a/arch/arm64/kvm/debug.c
+++ b/arch/arm64/kvm/debug.c
@@ -44,7 +44,7 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 	 * to disable guest access to the profiling and trace buffers
 	 */
 	vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN,
-					 *host_data_ptr(nr_event_counters));
+					 __this_cpu_read(kvm_host_data.nr_event_counters));
 	vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
 				MDCR_EL2_TPMS |
 				MDCR_EL2_TTRF |
@@ -80,8 +80,8 @@ void kvm_init_host_debug_data(void)
 								   ID_AA64DFR0_EL1_PMUVer_SHIFT);
 
 	if (pmuv3_implemented(pmuver))
-		*host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
-							      read_sysreg(pmcr_el0));
+		__this_cpu_write(kvm_host_data.nr_event_counters,
+				 FIELD_GET(ARMV8_PMU_PMCR_N, read_sysreg(pmcr_el0)));
 
 	*host_data_ptr(debug_brps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, BRPs, dfr0);
 	*host_data_ptr(debug_wrps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, WRPs, dfr0);

base-commit: c03c1a14507dfaf8b856c2608f9ac216021fc222
-- 
2.56.0.257.gf7497c7d27-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters
  2026-10-23  9:20 [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters Vincent Donnefort
@ 2026-10-23 12:29 ` Fuad Tabba
  2026-10-23 12:43   ` Marc Zyngier
  2026-10-23 12:37 ` Marc Zyngier
  1 sibling, 1 reply; 6+ messages in thread
From: Fuad Tabba @ 2026-10-23 12:29 UTC (permalink / raw)
  To: Vincent Donnefort
  Cc: maz, oupton, kvmarm, linux-arm-kernel, joey.gouly, seiden,
	suzuki.poulose, yuzenghui, catalin.marinas, will, kernel-team

Hi Vincent,

On Fri, 23 Oct 2026 10:20:07 +0100, Vincent Donnefort
<vdonnefort@google.com> wrote:
[...]
> So kvm_init_host_debug_data() initialises nr_event_counters in the
> hypervisor's kvm_host_data, while kvm_arm_setup_mdcr_el2() reads the
> host copy which is 0. With a HPMN of 0, this stops programmable event
> counters from counting a non-protected guest.

I tested this in pKVM with a non-protected guest counting with
perf_event_open(). On its own the patch doesn't change what the guest
sees: every counter, the cycle counter included, still reads 0. That's
a separate bug: flush_hyp_vcpu() never copies vcpu->arch.pmu.events to
the hyp vCPU, so the guest's events are never enabled on entry. I'll
send a fix for that shortly.

With that fix applied, the cycle counter counts without your patch but
a programmable counter still reads 0, and with your patch both count.

Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
Tested-by: Fuad Tabba <fuad.tabba@linux.dev>

Cheers,
/fuad


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters
  2026-10-23  9:20 [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters Vincent Donnefort
  2026-10-23 12:29 ` Fuad Tabba
@ 2026-10-23 12:37 ` Marc Zyngier
  2026-10-26 13:08   ` Vincent Donnefort
  1 sibling, 1 reply; 6+ messages in thread
From: Marc Zyngier @ 2026-10-23 12:37 UTC (permalink / raw)
  To: Vincent Donnefort
  Cc: oupton, kvmarm, linux-arm-kernel, joey.gouly, seiden,
	suzuki.poulose, yuzenghui, catalin.marinas, will, kernel-team,
	fuad.tabba

On Fri, 23 Oct 2026 10:20:07 +0100,
Vincent Donnefort <vdonnefort@google.com> wrote:
> 
> nr_event_counters is only used by the host to populate mdcr_el2.
> However, host_data_ptr() for pKVM switches between the hypervisor copy
> of kvm_host_data before it is deprivileged to the host copy after.
> 
> So kvm_init_host_debug_data() initialises nr_event_counters in the
> hypervisor's kvm_host_data, while kvm_arm_setup_mdcr_el2() reads the
> host copy which is 0. With a HPMN of 0, this stops programmable event
> counters from counting a non-protected guest.
> 
> Always use the host copy for nr_event_counters.
> 
> Fixes: 2417218f2f23 ("KVM: arm64: Get rid of __kvm_get_mdcr_el2() and related warts")
> Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> ---
>  arch/arm64/kvm/debug.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
> index f4d7b12045e8..a00771339c62 100644
> --- a/arch/arm64/kvm/debug.c
> +++ b/arch/arm64/kvm/debug.c
> @@ -44,7 +44,7 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
>  	 * to disable guest access to the profiling and trace buffers
>  	 */
>  	vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN,
> -					 *host_data_ptr(nr_event_counters));
> +					 __this_cpu_read(kvm_host_data.nr_event_counters));
>  	vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
>  				MDCR_EL2_TPMS |
>  				MDCR_EL2_TTRF |
> @@ -80,8 +80,8 @@ void kvm_init_host_debug_data(void)
>  								   ID_AA64DFR0_EL1_PMUVer_SHIFT);
>  
>  	if (pmuv3_implemented(pmuver))
> -		*host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
> -							      read_sysreg(pmcr_el0));
> +		__this_cpu_write(kvm_host_data.nr_event_counters,
> +				 FIELD_GET(ARMV8_PMU_PMCR_N, read_sysreg(pmcr_el0)));
>  
>  	*host_data_ptr(debug_brps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, BRPs, dfr0);
>  	*host_data_ptr(debug_wrps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, WRPs, dfr0);
> 

This really looks like the wrong approach.

host_data_ptr() should be reliable, and always point at the correct
data, irrespective of the backend. If host_data_ptr() returns a
pointer to stale data on pKVM, then it is purely a pKVM bug, and
that's what needs fixing by synchronising the two copies by whatever
means necessary.

Randomly picking a different accessor is not an acceptable outcome.

	M.

-- 
Without deviation from the norm, progress is not possible.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters
  2026-10-23 12:29 ` Fuad Tabba
@ 2026-10-23 12:43   ` Marc Zyngier
  2026-10-23 12:45     ` Fuad Tabba
  0 siblings, 1 reply; 6+ messages in thread
From: Marc Zyngier @ 2026-10-23 12:43 UTC (permalink / raw)
  To: Fuad Tabba
  Cc: Vincent Donnefort, oupton, kvmarm, linux-arm-kernel, joey.gouly,
	seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
	kernel-team

On Fri, 23 Oct 2026 13:29:04 +0100,
Fuad Tabba <fuad.tabba@linux.dev> wrote:
> 
> Hi Vincent,
> 
> On Fri, 23 Oct 2026 10:20:07 +0100, Vincent Donnefort
> <vdonnefort@google.com> wrote:
> [...]
> > So kvm_init_host_debug_data() initialises nr_event_counters in the
> > hypervisor's kvm_host_data, while kvm_arm_setup_mdcr_el2() reads the
> > host copy which is 0. With a HPMN of 0, this stops programmable event
> > counters from counting a non-protected guest.
> 
> I tested this in pKVM with a non-protected guest counting with
> perf_event_open(). On its own the patch doesn't change what the guest
> sees: every counter, the cycle counter included, still reads 0. That's
> a separate bug: flush_hyp_vcpu() never copies vcpu->arch.pmu.events to
> the hyp vCPU, so the guest's events are never enabled on entry. I'll
> send a fix for that shortly.

Please synchronise with Vincent and sent this as a series fixing PMU
handling on pKVM, not as discrete patches.

The approach taken for this patch is wrong anyway, so you have plenty
of opportunity to work together.

Thanks,

	M.

-- 
Without deviation from the norm, progress is not possible.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters
  2026-10-23 12:43   ` Marc Zyngier
@ 2026-10-23 12:45     ` Fuad Tabba
  0 siblings, 0 replies; 6+ messages in thread
From: Fuad Tabba @ 2026-10-23 12:45 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: Vincent Donnefort, oupton, kvmarm, linux-arm-kernel, joey.gouly,
	seiden, suzuki.poulose, yuzenghui, catalin.marinas, will,
	kernel-team

On Fri, 23 Oct 2026 at 13:43, Marc Zyngier <maz@kernel.org> wrote:
[...]
> > I tested this in pKVM with a non-protected guest counting with
> > perf_event_open(). On its own the patch doesn't change what the guest
> > sees: every counter, the cycle counter included, still reads 0. That's
> > a separate bug: flush_hyp_vcpu() never copies vcpu->arch.pmu.events to
> > the hyp vCPU, so the guest's events are never enabled on entry. I'll
> > send a fix for that shortly.
>
> Please synchronise with Vincent and sent this as a series fixing PMU
> handling on pKVM, not as discrete patches.
>
> The approach taken for this patch is wrong anyway, so you have plenty
> of opportunity to work together.

Will do.

Cheers,
/fuad
>
> Thanks,
>
>         M.
>
> --
> Without deviation from the norm, progress is not possible.
>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters
  2026-10-23 12:37 ` Marc Zyngier
@ 2026-10-26 13:08   ` Vincent Donnefort
  0 siblings, 0 replies; 6+ messages in thread
From: Vincent Donnefort @ 2026-10-26 13:08 UTC (permalink / raw)
  To: Marc Zyngier
  Cc: oupton, kvmarm, linux-arm-kernel, joey.gouly, seiden,
	suzuki.poulose, yuzenghui, catalin.marinas, will, kernel-team,
	fuad.tabba

On Fri, Oct 23, 2026 at 01:37:45PM +0100, Marc Zyngier wrote:
> On Fri, 23 Oct 2026 10:20:07 +0100,
> Vincent Donnefort <vdonnefort@google.com> wrote:
> > 
> > nr_event_counters is only used by the host to populate mdcr_el2.
> > However, host_data_ptr() for pKVM switches between the hypervisor copy
> > of kvm_host_data before it is deprivileged to the host copy after.
> > 
> > So kvm_init_host_debug_data() initialises nr_event_counters in the
> > hypervisor's kvm_host_data, while kvm_arm_setup_mdcr_el2() reads the
> > host copy which is 0. With a HPMN of 0, this stops programmable event
> > counters from counting a non-protected guest.
> > 
> > Always use the host copy for nr_event_counters.
> > 
> > Fixes: 2417218f2f23 ("KVM: arm64: Get rid of __kvm_get_mdcr_el2() and related warts")
> > Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
> > ---
> >  arch/arm64/kvm/debug.c | 6 +++---
> >  1 file changed, 3 insertions(+), 3 deletions(-)
> > 
> > diff --git a/arch/arm64/kvm/debug.c b/arch/arm64/kvm/debug.c
> > index f4d7b12045e8..a00771339c62 100644
> > --- a/arch/arm64/kvm/debug.c
> > +++ b/arch/arm64/kvm/debug.c
> > @@ -44,7 +44,7 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
> >  	 * to disable guest access to the profiling and trace buffers
> >  	 */
> >  	vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN,
> > -					 *host_data_ptr(nr_event_counters));
> > +					 __this_cpu_read(kvm_host_data.nr_event_counters));
> >  	vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
> >  				MDCR_EL2_TPMS |
> >  				MDCR_EL2_TTRF |
> > @@ -80,8 +80,8 @@ void kvm_init_host_debug_data(void)
> >  								   ID_AA64DFR0_EL1_PMUVer_SHIFT);
> >  
> >  	if (pmuv3_implemented(pmuver))
> > -		*host_data_ptr(nr_event_counters) = FIELD_GET(ARMV8_PMU_PMCR_N,
> > -							      read_sysreg(pmcr_el0));
> > +		__this_cpu_write(kvm_host_data.nr_event_counters,
> > +				 FIELD_GET(ARMV8_PMU_PMCR_N, read_sysreg(pmcr_el0)));
> >  
> >  	*host_data_ptr(debug_brps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, BRPs, dfr0);
> >  	*host_data_ptr(debug_wrps) = SYS_FIELD_GET(ID_AA64DFR0_EL1, WRPs, dfr0);
> > 
> 
> This really looks like the wrong approach.
> 
> host_data_ptr() should be reliable, and always point at the correct
> data, irrespective of the backend. If host_data_ptr() returns a
> pointer to stale data on pKVM, then it is purely a pKVM bug, and
> that's what needs fixing by synchronising the two copies by whatever
> means necessary.

I'll update both copies and make a common series Fuad.

Thanks

-- 
Vincent


> 
> Randomly picking a different accessor is not an acceptable outcome.
> 
> 	M.
> 
> -- 
> Without deviation from the norm, progress is not possible.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-10-26 13:09 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-10-23  9:20 [PATCH v1] KVM: arm64: Use the host kvm_host_data for nr_event_counters Vincent Donnefort
2026-10-23 12:29 ` Fuad Tabba
2026-10-23 12:43   ` Marc Zyngier
2026-10-23 12:45     ` Fuad Tabba
2026-10-23 12:37 ` Marc Zyngier
2026-10-26 13:08   ` Vincent Donnefort

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.