KVM: arm64: PMU: Assume PMU presence in pmu-emul.c
authorAkihiko Odaki <akihiko.odaki@daynix.com>
Sat, 15 Mar 2025 09:12:11 +0000 (18:12 +0900)
committerOliver Upton <oliver.upton@linux.dev>
Mon, 17 Mar 2025 17:42:22 +0000 (10:42 -0700)
commitbe5ccac3f15ea6f0e9086a98dc3ee4f15e873ccb
tree6c66113e8ad2b43c55c0b97b3b4f56acf34fd6a9
parentf2aeb7bbd5745fbcf7f0769e29a184e24924b9a9
KVM: arm64: PMU: Assume PMU presence in pmu-emul.c

Many functions in pmu-emul.c checks kvm_vcpu_has_pmu(vcpu). A favorable
interpretation is defensive programming, but it also has downsides:

- It is confusing as it implies these functions are called without PMU
  although most of them are called only when a PMU is present.

- It makes semantics of functions fuzzy. For example, calling
  kvm_pmu_disable_counter_mask() without PMU may result in no-op as
  there are no enabled counters, but it's unclear what
  kvm_pmu_get_counter_value() returns when there is no PMU.

- It allows callers without checking kvm_vcpu_has_pmu(vcpu), but it is
  often wrong to call these functions without PMU.

- It is error-prone to duplicate kvm_vcpu_has_pmu(vcpu) checks into
  multiple functions. Many functions are called for system registers,
  and the system register infrastructure already employs less
  error-prone, comprehensive checks.

Check kvm_vcpu_has_pmu(vcpu) in callers of these functions instead,
and remove the obsolete checks from pmu-emul.c. The only exceptions are
the functions that implement ioctls as they have definitive semantics
even when the PMU is not present.

Signed-off-by: Akihiko Odaki <akihiko.odaki@daynix.com>
Reviewed-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20250315-pmc-v5-2-ecee87dab216@daynix.com
Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
arch/arm64/kvm/arm.c
arch/arm64/kvm/emulate-nested.c
arch/arm64/kvm/pmu-emul.c
arch/arm64/kvm/sys_regs.c