]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
LoongArch: KVM: Restore guest PMU if it is enabled
authorBibo Mao <maobibo@loongson.cn>
Sun, 9 Nov 2025 08:02:09 +0000 (16:02 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 24 Nov 2025 09:35:58 +0000 (10:35 +0100)
commit 5001bcf86edf2de02f025a0f789bcac37fa040e6 upstream.

On LoongArch system, guest PMU hardware is shared by guest and host but
PMU interrupt is separated. PMU is pass-through to VM, and there is PMU
context switch when exit to host and return to guest.

There is optimiation to check whether PMU is enabled by guest. If not,
it is not necessary to return to guest. However, if it is enabled, PMU
context for guest need switch on. Now KVM_REQ_PMU notification is set
on vCPU context switch, but it is missing if there is no vCPU context
switch while PMU is used by guest VM, so fix it.

Cc: <stable@vger.kernel.org>
Fixes: f4e40ea9f78f ("LoongArch: KVM: Add PMU support for guest")
Signed-off-by: Bibo Mao <maobibo@loongson.cn>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/loongarch/kvm/vcpu.c

index b5439a10b7652beda93c7b0f545323f33284b5a2..fffea69191f7c2d4e178dc51baea7d73db7306ad 100644 (file)
@@ -127,6 +127,9 @@ static void kvm_lose_pmu(struct kvm_vcpu *vcpu)
         * Clear KVM_LARCH_PMU if the guest is not using PMU CSRs when
         * exiting the guest, so that the next time trap into the guest.
         * We don't need to deal with PMU CSRs contexts.
+        *
+        * Otherwise set the request bit KVM_REQ_PMU to restore guest PMU
+        * before entering guest VM
         */
        val = kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL0);
        val |= kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL1);
@@ -134,6 +137,8 @@ static void kvm_lose_pmu(struct kvm_vcpu *vcpu)
        val |= kvm_read_sw_gcsr(csr, LOONGARCH_CSR_PERFCTRL3);
        if (!(val & KVM_PMU_EVENT_ENABLED))
                vcpu->arch.aux_inuse &= ~KVM_LARCH_PMU;
+       else
+               kvm_make_request(KVM_REQ_PMU, vcpu);
 
        kvm_restore_host_pmu(vcpu);
 }