Skip to contentSkip to conversation

target/arm: Always add pmu property for Armv7-A/R+

PATCH Superseded Akihiko Odaki 2 participants 3 messages Jan 4, 2025 – Jan 29, 2025 KVM

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 target/arm: Always add pmu property for Armv7-A/R+

kvm-steal-time and sve properties are added for KVM even if the corresponding features are not available. Always add pmu property for Armv7+. Note that the property is added only for Armv7-A/R+ as QEMU currently emulates PMU only for such versions, and a different version may have a different definition of PMU or may not have one at all.

---
The "pmu" property is added only when the PMU is available. This makes tests/qtest/arm-cpu-features.c fail as it reads the property to check the availability. Always add the property when the architecture defines the PMU even if it's not available to fix this.
---
Changes in v5:
- Rebased.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Split patch "target/arm/kvm: Fix PMU feature bit early" into "target/arm/kvm: Set PMU for host only when available" and "target/arm/kvm: Do not silently remove PMU".
- Changed to define PMU also for Armv7.
- Changed not to define PMU for M.
- Extracted patch "hvf: arm: Raise an exception for sysreg by default" from "hvf: arm: Properly disable PMU".
- Rebased.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Dropped patch "target/arm: Do not allow setting 'pmu' for hvf".
- Dropped patch "target/arm: Allow setting 'pmu' only for host and max".
- Dropped patch "target/arm/kvm: Report PMU unavailability".
- Added patch "target/arm/kvm: Fix PMU feature bit early".
- Added patch "hvf: arm: Do not advance PC when raising an exception".
- Added patch "hvf: arm: Properly disable PMU".
- Changed to check for Armv8 before adding PMU property.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Restricted writes to 'pmu' to host and max.
- Prohibited writes to 'pmu' for hvf.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 target/arm/cpu.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
target/arm/cpu.c +4 -1
index dcedadc89eaf..e76d42398eb2 100644
@@ -1761,6 +1761,10 @@ void arm_cpu_post_init(Object *obj)
 
     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
+
+        if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
+            object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
+        }
     }
 
     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {
@@ -1790,7 +1794,6 @@ void arm_cpu_post_init(Object *obj)
 
     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
         cpu->has_pmu = true;
-        object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
     }
 
     /*

---
base-commit: 38d0939b86e2eef6f6a622c6f1f7befda0146595
change-id: 20240629-pmu-ad5f67e2c5d0

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw
Quoted 7 lines · Akihiko Odaki

kvm-steal-time and sve properties are added for KVM even if the corresponding features are not available. Always add pmu property for Armv7+. Note that the property is added only for Armv7-A/R+ as QEMU currently emulates PMU only for such versions, and a different version may have a different definition of PMU or may not have one at all.

This isn't how we generally handle CPU properties corresponding to features. The standard setup is:
* if the CPU can't have feature foo, no property
* if the CPU does have feature foo, define a property, so the user can turn it off

See also my longer explanation in reply to this patch in v4:

https://lore.kernel.org/all/CAFEAcA_HWfCU09NfZDf6EC=rpvHn148avySCztQ8PqPBMFx4_Q@mail.gmail.com/

Quoted 6 lines

---
The "pmu" property is added only when the PMU is available. This makes tests/qtest/arm-cpu-features.c fail as it reads the property to check the availability. Always add the property when the architecture defines the PMU even if it's not available to fix this.

This seems to me like a bug in the test.

Quoted 16 lines
target/arm/cpu.c +4 -0
index dcedadc89eaf..e76d42398eb2 100644
@@ -1761,6 +1761,10 @@ void arm_cpu_post_init(Object *obj)
 
     if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
         qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
+
+        if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
+            object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
+        }
     }
 
     if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {

target/arm/cpu.c void arm_cpu_post_init(Object *obj) · line 1799

Quoted 7 lines
target/arm/cpu.c +0 -1
@@ -1790,7 +1794,6 @@ void arm_cpu_post_init(Object *obj)
 
     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
         cpu->has_pmu = true;
-        object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
     }
 
     /*

This would allow the user to enable the PMU on a CPU that says it doesn't have one. We don't generally permit that.

thanks
-- PMM

#2 lore raw
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Akihiko Odaki

kvm-steal-time and sve properties are added for KVM even if the corresponding features are not available. Always add pmu property for Armv7+. Note that the property is added only for Armv7-A/R+ as QEMU currently emulates PMU only for such versions, and a different version may have a different definition of PMU or may not have one at all.

Quoted 5 lines

This isn't how we generally handle CPU properties corresponding to features. The standard setup is:
* if the CPU can't have feature foo, no property
* if the CPU does have feature foo, define a property, so the user can turn it off

Is that really standard? The patch message says kvm-steal-time and sve properties are added even if the features are not available. Looking at other architectures, I can confirm that IvyBridge, an x86_64 CPU, has a property avx512f that can be set to true though the physical CPU model does not have one. I believe the situation is no different for RISC-V too.

Quoted 4 lines

See also my longer explanation in reply to this patch in v4:

https://lore.kernel.org/all/CAFEAcA_HWfCU09NfZDf6EC=rpvHn148avySCztQ8PqPBMFx4_Q@mail.gmail.com/

It explains well why the PMU of ARMv7 is different from other features like avx512f on x86_64 or RISC-V features; the architecture does not allow feature detection. However, as I noted in an earlier email, it also means explicitly disabling the PMU on ARMv7 is equally dangerous as enabling the PMU. So I see two logical design options:

1. Forbid to explicitly disable or enable the PMU on ARMv7 at all to avoid breaking the guest.
2. Allow explicitly disabling or enabling the PMU on ARMv7 under the assumption that the property will be used only by experienced users.

Quoted 39 lines
Quoted 7 lines

---
The "pmu" property is added only when the PMU is available. This makes tests/qtest/arm-cpu-features.c fail as it reads the property to check the availability. Always add the property when the architecture defines the PMU even if it's not available to fix this.

This seems to me like a bug in the test.

Quoted 16 lines
target/arm/cpu.c +4 -0
index dcedadc89eaf..e76d42398eb2 100644
@@ -1761,6 +1761,10 @@ void arm_cpu_post_init(Object *obj)
 
      if (!arm_feature(&cpu->env, ARM_FEATURE_M)) {
          qdev_property_add_static(DEVICE(obj), &arm_cpu_reset_hivecs_property);
+
+        if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
+            object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
+        }
      }
 
      if (arm_feature(&cpu->env, ARM_FEATURE_V8)) {

target/arm/cpu.c void arm_cpu_post_init(Object *obj) · line 1799

Quoted 7 lines
target/arm/cpu.c +0 -1
@@ -1790,7 +1794,6 @@ void arm_cpu_post_init(Object *obj)
 
      if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
          cpu->has_pmu = true;
-        object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
      }
 
      /*

This would allow the user to enable the PMU on a CPU that says it doesn't have one. We don't generally permit that.

thanks
-- PMM