Skip to content

Commit 4277335

Browse files
reijiw-kvmoupton
authored andcommitted
KVM: arm64: Select default PMU in KVM_ARM_VCPU_INIT handler
Future changes to KVM's sysreg emulation will rely on having a valid PMU instance to determine the number of implemented counters (PMCR_EL0.N). This is earlier than when userspace is expected to modify the vPMU device attributes, where the default is selected today. Select the default PMU when handling KVM_ARM_VCPU_INIT such that it is available in time for sysreg emulation. Reviewed-by: Sebastian Ott <[email protected]> Co-developed-by: Marc Zyngier <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Signed-off-by: Reiji Watanabe <[email protected]> Signed-off-by: Raghavendra Rao Ananta <[email protected]> Link: https://lore.kernel.org/r/[email protected] [Oliver: rewrite changelog] Signed-off-by: Oliver Upton <[email protected]>
1 parent 1616ca6 commit 4277335

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

arch/arm64/kvm/arm.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,21 @@ static bool kvm_vcpu_init_changed(struct kvm_vcpu *vcpu,
12291229
return !bitmap_equal(vcpu->arch.features, &features, KVM_VCPU_MAX_FEATURES);
12301230
}
12311231

1232+
static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)
1233+
{
1234+
struct kvm *kvm = vcpu->kvm;
1235+
int ret = 0;
1236+
1237+
/*
1238+
* When the vCPU has a PMU, but no PMU is set for the guest
1239+
* yet, set the default one.
1240+
*/
1241+
if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu)
1242+
ret = kvm_arm_set_default_pmu(kvm);
1243+
1244+
return ret;
1245+
}
1246+
12321247
static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
12331248
const struct kvm_vcpu_init *init)
12341249
{
@@ -1244,6 +1259,10 @@ static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
12441259

12451260
bitmap_copy(vcpu->arch.features, &features, KVM_VCPU_MAX_FEATURES);
12461261

1262+
ret = kvm_setup_vcpu(vcpu);
1263+
if (ret)
1264+
goto out_unlock;
1265+
12471266
/* Now we know what it is, we can reset it. */
12481267
ret = kvm_reset_vcpu(vcpu);
12491268
if (ret) {

arch/arm64/kvm/pmu-emul.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -717,10 +717,9 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void)
717717
* It is still necessary to get a valid cpu, though, to probe for the
718718
* default PMU instance as userspace is not required to specify a PMU
719719
* type. In order to uphold the preexisting behavior KVM selects the
720-
* PMU instance for the core where the first call to the
721-
* KVM_ARM_VCPU_PMU_V3_CTRL attribute group occurs. A dependent use case
722-
* would be a user with disdain of all things big.LITTLE that affines
723-
* the VMM to a particular cluster of cores.
720+
* PMU instance for the core during vcpu init. A dependent use
721+
* case would be a user with disdain of all things big.LITTLE that
722+
* affines the VMM to a particular cluster of cores.
724723
*
725724
* In any case, userspace should just do the sane thing and use the UAPI
726725
* to select a PMU type directly. But, be wary of the baggage being
@@ -893,7 +892,7 @@ static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
893892
* where vCPUs can be scheduled on any core but the guest
894893
* counters could stop working.
895894
*/
896-
static int kvm_arm_set_default_pmu(struct kvm *kvm)
895+
int kvm_arm_set_default_pmu(struct kvm *kvm)
897896
{
898897
struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
899898

@@ -946,13 +945,6 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
946945
if (vcpu->arch.pmu.created)
947946
return -EBUSY;
948947

949-
if (!kvm->arch.arm_pmu) {
950-
int ret = kvm_arm_set_default_pmu(kvm);
951-
952-
if (ret)
953-
return ret;
954-
}
955-
956948
switch (attr->attr) {
957949
case KVM_ARM_VCPU_PMU_V3_IRQ: {
958950
int __user *uaddr = (int __user *)(long)attr->addr;

include/kvm/arm_pmu.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void kvm_vcpu_pmu_resync_el0(void);
101101
})
102102

103103
u8 kvm_arm_pmu_get_pmuver_limit(void);
104+
int kvm_arm_set_default_pmu(struct kvm *kvm);
104105

105106
#else
106107
struct kvm_pmu {
@@ -174,6 +175,11 @@ static inline u8 kvm_arm_pmu_get_pmuver_limit(void)
174175
}
175176
static inline void kvm_vcpu_pmu_resync_el0(void) {}
176177

178+
static inline int kvm_arm_set_default_pmu(struct kvm *kvm)
179+
{
180+
return -ENODEV;
181+
}
182+
177183
#endif
178184

179185
#endif

0 commit comments

Comments
 (0)