]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
KVM: arm64: Preserve host MPAM configuration when changing traps
authorBen Horgan <ben.horgan@arm.com>
Fri, 13 Mar 2026 14:45:41 +0000 (14:45 +0000)
committerJames Morse <james.morse@arm.com>
Fri, 27 Mar 2026 15:27:32 +0000 (15:27 +0000)
When KVM enables or disables MPAM traps to EL2 it clears all other bits in
MPAM2_EL2.  Notably, it clears the partition ids (PARTIDs) and performance
monitoring groups (PMGs). Avoid changing these bits in anticipation of
adding support for MPAM in the kernel. Otherwise, on a VHE system with the
host running at EL2 where MPAM2_EL2 and MPAM1_EL1 access the same register,
any attempt to use MPAM to monitor or partition resources for kernel space
would be foiled by running a KVM guest. Additionally, MPAM2_EL2.EnMPAMSM is
always set to 0 which causes MPAMSM_EL1 to always trap. Keep EnMPAMSM set
to 1 when not in a guest so that the kernel can use MPAMSM_EL1.

Tested-by: Gavin Shan <gshan@redhat.com>
Tested-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com>
Tested-by: Peter Newman <peternewman@google.com>
Tested-by: Zeng Heng <zengheng4@huawei.com>
Tested-by: Punit Agrawal <punit.agrawal@oss.qualcomm.com>
Tested-by: Jesse Chick <jessechick@os.amperecomputing.com>
Reviewed-by: Zeng Heng <zengheng4@huawei.com>
Reviewed-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com>
Reviewed-by: Jonathan Cameron <jonathan.cameron@huawei.com>
Reviewed-by: Gavin Shan <gshan@redhat.com>
Acked-by: Marc Zyngier <maz@kernel.org>
Signed-off-by: Ben Horgan <ben.horgan@arm.com>
Signed-off-by: James Morse <james.morse@arm.com>
arch/arm64/kvm/hyp/include/hyp/switch.h

index 2597e8bda86728d5e02e975c7453a940492355df..0b50ddd530f3ea51e1c2e8f640003a05a314febb 100644 (file)
@@ -267,7 +267,8 @@ static inline void __deactivate_traps_hfgxtr(struct kvm_vcpu *vcpu)
 
 static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
 {
-       u64 r = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
+       u64 clr = MPAM2_EL2_EnMPAMSM;
+       u64 set = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1;
 
        if (!system_supports_mpam())
                return;
@@ -277,18 +278,21 @@ static inline void  __activate_traps_mpam(struct kvm_vcpu *vcpu)
                write_sysreg_s(MPAMHCR_EL2_TRAP_MPAMIDR_EL1, SYS_MPAMHCR_EL2);
        } else {
                /* From v1.1 TIDR can trap MPAMIDR, set it unconditionally */
-               r |= MPAM2_EL2_TIDR;
+               set |= MPAM2_EL2_TIDR;
        }
 
-       write_sysreg_s(r, SYS_MPAM2_EL2);
+       sysreg_clear_set_s(SYS_MPAM2_EL2, clr, set);
 }
 
 static inline void __deactivate_traps_mpam(void)
 {
+       u64 clr = MPAM2_EL2_TRAPMPAM0EL1 | MPAM2_EL2_TRAPMPAM1EL1 | MPAM2_EL2_TIDR;
+       u64 set = MPAM2_EL2_EnMPAMSM;
+
        if (!system_supports_mpam())
                return;
 
-       write_sysreg_s(0, SYS_MPAM2_EL2);
+       sysreg_clear_set_s(SYS_MPAM2_EL2, clr, set);
 
        if (system_supports_mpam_hcr())
                write_sysreg_s(MPAMHCR_HOST_FLAGS, SYS_MPAMHCR_EL2);