KVM: lapic: Check for in-kernel LAPIC before deferencing apic pointer [Linux 5.1]

KVM: lapic: Check for in-kernel LAPIC before deferencing apic pointer [Linux 5.1]

This Linux kernel change "KVM: lapic: Check for in-kernel LAPIC before deferencing apic pointer" is included in the Linux 5.1 release. This change is authored by Sean Christopherson <sean.j.christopherson [at] intel.com> on Thu Apr 25 19:01:09 2019 -0700. The commit for this change in Linux stable tree is b904cb8 (patch).

KVM: lapic: Check for in-kernel LAPIC before deferencing apic pointer

...to avoid dereferencing a null pointer when querying the per-vCPU
timer advance.

Fixes: 39497d7660d98 ("KVM: lapic: Track lapic timer advance per vCPU")
Reported-by: [email protected]
Signed-off-by: Sean Christopherson <[email protected]>
Reviewed-by: Konrad Rzeszutek Wilk <[email protected]>
Signed-off-by: Paolo Bonzini <[email protected]>

There are 6 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 arch/x86/kvm/lapic.c | 3 ---
 arch/x86/kvm/x86.c   | 3 ++-
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
index e0fa6fc..bd13fdd 100644
--- a/arch/x86/kvm/lapic.c
+++ b/arch/x86/kvm/lapic.c
@@ -1507,9 +1507,6 @@ void wait_lapic_expire(struct kvm_vcpu *vcpu)
    u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
    u64 guest_tsc, tsc_deadline, ns;

-   if (!lapic_in_kernel(vcpu))
-       return;
-
    if (apic->lapic_timer.expired_tscdeadline == 0)
        return;

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 5762131..b5edc8e 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7894,7 +7894,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
    }

    trace_kvm_entry(vcpu->vcpu_id);
-   if (vcpu->arch.apic->lapic_timer.timer_advance_ns)
+   if (lapic_in_kernel(vcpu) &&
+       vcpu->arch.apic->lapic_timer.timer_advance_ns)
        wait_lapic_expire(vcpu);
    guest_enter_irqoff();

Leave a Reply

Your email address will not be published. Required fields are marked *