Skip to contentSkip to conversation

KVM: x86: Fix uninitialized eoi_exit_bitmap usage in vcpu_load_eoi_exitmap()

PATCH 黄乐 2 participants 2 messages Nov 15, 2021 KVM

lore
Share & tools
lore

黄乐

author
#0 lore raw
Quoted 9 lines
Quoted 7 lines · 黄乐

In vcpu_load_eoi_exitmap(), currently the eoi_exit_bitmap[4] array is initialized only when Hyper-V context is available, in other path it is just passed to kvm_x86_ops.load_eoi_exitmap() directly from on the stack, which would cause unexpected interrupt delivery/handling issues, e.g. an *old* linux kernel that relies on PIT to do clock calibration on KVM might randomly fail to boot.

Quoted 7 lines
Quoted 4 lines

Fix it by passing ioapic_handled_vectors to load_eoi_exitmap() when Hyper-V context is not available.

Fixes f2bc14b69c38 ("KVM: x86: hyper-v: Prepare to meet unallocated Hyper-V context")

Commit f2bc14b69c38 is not in stable tree I guess, it was merged in from 5.12, do we still need Cc this patch to stable maintainers?

Quoted 41 lines
Quoted 15 lines

---

arch/x86/kvm/x86.c +1 -4
index dc7eb5fddfd3..0699832504c9 100644
@@ -9547,11 +9547,14 @@ static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
       if (!kvm_apic_hw_enabled(vcpu->arch.apic))
               return;
 
-     if (to_hv_vcpu(vcpu))
-             bitmap_or((ulong *)eoi_exit_bitmap,
-                       vcpu->arch.ioapic_handled_vectors,
-                       to_hv_synic(vcpu)->vec_bitmap, 256);
+     if (!to_hv_vcpu(vcpu)) {

arch/x86/kvm/x86.c static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu) · line 9560

Quoted 10 lines
arch/x86/kvm/x86.c +6 -0
+             static_call(kvm_x86_load_eoi_exitmap)(
+                     vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors);
+             return;
+     }
 
+     bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
+               to_hv_synic(vcpu)->vec_bitmap, 256);
       static_call(kvm_x86_load_eoi_exitmap)(vcpu, eoi_exit_bitmap);
 }
 

My personal preference, however, would be to keep 'if (to_hv_vcpu(vcpu))' check and not invert it, i.e.:

        if (to_hv_vcpu(vcpu)) {
                bitmap_or((ulong *)eoi_exit_bitmap,
                 vcpu->arch.ioapic_handled_vectors,
                 to_hv_synic(vcpu)->vec_bitmap, 256);
                static_call(...)(vcpu, eoi_exit_bitmap)
                return;
        }

        static_call(...)(vcpu, (u64 *)vcpu->arch.ioapic_handled_vectors);
Quoted 1 line

to slightly reduce the code churn but it doesn't matter much.

Got it. Will send an updated one later. Thanks for suggestion!

Quoted 5 lines

Thanks!

--
Vitaly

#1 lore raw
Quoted 12 lines · 黄乐
Quoted 12 lines
Quoted 10 lines · 黄乐

In vcpu_load_eoi_exitmap(), currently the eoi_exit_bitmap[4] array is initialized only when Hyper-V context is available, in other path it is just passed to kvm_x86_ops.load_eoi_exitmap() directly from on the stack, which would cause unexpected interrupt delivery/handling issues, e.g. an *old* linux kernel that relies on PIT to do clock calibration on KVM might randomly fail to boot.

Fix it by passing ioapic_handled_vectors to load_eoi_exitmap() when Hyper-V context is not available.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Fixes f2bc14b69c38 ("KVM: x86: hyper-v: Prepare to meet unallocated Hyper-V context")

Commit f2bc14b69c38 is not in stable tree I guess, it was merged in from 5.12, do we still need Cc this patch to stable maintainers?

There are multiple stable trees, one for each major release. Not all of them are still supported but you don't need to care about it, 'Cc:
[email protected]' is just an indication for everyone who has f2bc14b69c38 in his tree (5.12+) that there's a fix available.

Signature · 1 line
Vitaly