Skip to contentSkip to conversation

KVM: VMX: fix crash cleanup when KVM wasn't used

PATCH Applied Vitaly Kuznetsov 5 participants 11 messages Apr 1, 2020 – Sep 2, 2020

Subsystems (5)
x86 KVM KVM x86 Live update +1 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: VMX: fix crash cleanup when KVM wasn't used

If KVM wasn't used at all before we crash the cleanup procedure fails with

 BUG: unable to handle page fault for address: ffffffffffffffc8
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 23215067 P4D 23215067 PUD 23217067 PMD 0
 Oops: 0000 [#8] SMP PTI
 CPU: 0 PID: 3542 Comm: bash Kdump: loaded Tainted: G      D           5.6.0-rc2+ #823
 RIP: 0010:crash_vmclear_local_loaded_vmcss.cold+0x19/0x51 [kvm_intel]

The root cause is that loaded_vmcss_on_cpu list is not yet initialized, we initialize it in hardware_enable() but this only happens when we start a VM.

Previously, we used to have a bitmap with enabled CPUs and that was preventing [masking] the issue.

Initialized loaded_vmcss_on_cpu list earlier, right before we assign crash_vmclear_loaded_vmcss pointer. blocked_vcpu_on_cpu list and blocked_vcpu_on_cpu_lock are moved altogether for consistency.

Fixes 31603d4fc2bb ("KVM: VMX: Always VMCLEAR in-use VMCSes during crash with kexec support")
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +7 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
 	    !hv_get_vp_assist_page(cpu))
 		return -EFAULT;
 
-	INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-	INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-	spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
 	r = kvm_cpu_vmxon(phys_addr);
 	if (r)
 		return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-	int r;
+	int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
 	/*
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
 		return r;
 	}
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+		INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+		spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+	}
+
 #ifdef CONFIG_KEXEC_CORE
 	rcu_assign_pointer(crash_vmclear_loaded_vmcss,
 			   crash_vmclear_local_loaded_vmcss);
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 51 lines · Vitaly Kuznetsov

If KVM wasn't used at all before we crash the cleanup procedure fails with

 BUG: unable to handle page fault for address: ffffffffffffffc8
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 23215067 P4D 23215067 PUD 23217067 PMD 0
 Oops: 0000 [#8] SMP PTI
 CPU: 0 PID: 3542 Comm: bash Kdump: loaded Tainted: G      D           5.6.0-rc2+ #823
 RIP: 0010:crash_vmclear_local_loaded_vmcss.cold+0x19/0x51 [kvm_intel]

The root cause is that loaded_vmcss_on_cpu list is not yet initialized, we initialize it in hardware_enable() but this only happens when we start a VM.

Previously, we used to have a bitmap with enabled CPUs and that was preventing [masking] the issue.

Initialized loaded_vmcss_on_cpu list earlier, right before we assign crash_vmclear_loaded_vmcss pointer. blocked_vcpu_on_cpu list and blocked_vcpu_on_cpu_lock are moved altogether for consistency.

Fixes 31603d4fc2bb ("KVM: VMX: Always VMCLEAR in-use VMCSes during crash with kexec support")
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
 	    !hv_get_vp_assist_page(cpu))
 		return -EFAULT;
 
-	INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-	INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-	spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
 	r = kvm_cpu_vmxon(phys_addr);
 	if (r)
 		return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-	int r;
+	int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
 	/*

arch/x86/kvm/vmx/vmx.c static int __init vmx_init(void) · line 8062

Quoted 7 lines
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
 		return r;
 	}
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+		INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+		spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));

Hmm, part of me thinks the posted interrupt per_cpu variables should continue to be initialized during hardware_enable(). But it's a small part of me :-)

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +2 -0
+	}
+
 #ifdef CONFIG_KEXEC_CORE
 	rcu_assign_pointer(crash_vmclear_loaded_vmcss,
 			   crash_vmclear_local_loaded_vmcss);
Signature · 1 line
2.25.1
maintainer Applied
#2 lore raw
In reply to Vitaly Kuznetsov
Quoted 54 lines · Vitaly Kuznetsov

If KVM wasn't used at all before we crash the cleanup procedure fails with

 BUG: unable to handle page fault for address: ffffffffffffffc8
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 23215067 P4D 23215067 PUD 23217067 PMD 0
 Oops: 0000 [#8] SMP PTI
 CPU: 0 PID: 3542 Comm: bash Kdump: loaded Tainted: G      D           5.6.0-rc2+ #823
 RIP: 0010:crash_vmclear_local_loaded_vmcss.cold+0x19/0x51 [kvm_intel]

The root cause is that loaded_vmcss_on_cpu list is not yet initialized, we initialize it in hardware_enable() but this only happens when we start a VM.

Previously, we used to have a bitmap with enabled CPUs and that was preventing [masking] the issue.

Initialized loaded_vmcss_on_cpu list earlier, right before we assign crash_vmclear_loaded_vmcss pointer. blocked_vcpu_on_cpu list and blocked_vcpu_on_cpu_lock are moved altogether for consistency.

Fixes 31603d4fc2bb ("KVM: VMX: Always VMCLEAR in-use VMCSes during crash with kexec support")
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
 	    !hv_get_vp_assist_page(cpu))
 		return -EFAULT;
 
-	INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-	INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-	spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
 	r = kvm_cpu_vmxon(phys_addr);
 	if (r)
 		return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-	int r;
+	int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
 	/*
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
 		return r;
 	}

arch/x86/kvm/vmx/vmx.c static int __init vmx_init(void) · line 8067

Quoted 10 lines
arch/x86/kvm/vmx/vmx.c +6 -0
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+		INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+		spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+	}
+
 #ifdef CONFIG_KEXEC_CORE
 	rcu_assign_pointer(crash_vmclear_loaded_vmcss,
 			   crash_vmclear_local_loaded_vmcss);

Queued, thanks.

Paolo

Tested-by on v1 1/1
#3 lore raw
In reply to Vitaly Kuznetsov
Quoted 16 lines · Vitaly Kuznetsov

If KVM wasn't used at all before we crash the cleanup procedure fails with

 BUG: unable to handle page fault for address: ffffffffffffffc8
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 23215067 P4D 23215067 PUD 23217067 PMD 0
 Oops: 0000 [#8] SMP PTI
 CPU: 0 PID: 3542 Comm: bash Kdump: loaded Tainted: G      D           5.6.0-rc2+ #823
 RIP: 0010:crash_vmclear_local_loaded_vmcss.cold+0x19/0x51 [kvm_intel]

The root cause is that loaded_vmcss_on_cpu list is not yet initialized, we initialize it in hardware_enable() but this only happens when we start a VM.

Previously, we used to have a bitmap with enabled CPUs and that was preventing [masking] the issue.

Quoted 6 lines

Initialized loaded_vmcss_on_cpu list earlier, right before we assign crash_vmclear_loaded_vmcss pointer. blocked_vcpu_on_cpu list and blocked_vcpu_on_cpu_lock are moved altogether for consistency.

Fixes 31603d4fc2bb ("KVM: VMX: Always VMCLEAR in-use VMCSes during crash with kexec support")

Kdump kernel hang can be reproduced on a bare metal machine of Intel always, issue disappeared with this patch applied. Feel free to add:

Quoted 44 lines
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +7 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
 	    !hv_get_vp_assist_page(cpu))
 		return -EFAULT;
 
-	INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-	INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-	spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
 	r = kvm_cpu_vmxon(phys_addr);
 	if (r)
 		return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-	int r;
+	int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
 	/*
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
 		return r;
 	}
 
+	for_each_possible_cpu(cpu) {
+		INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+		INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+		spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+	}
+
 #ifdef CONFIG_KEXEC_CORE
 	rcu_assign_pointer(crash_vmclear_loaded_vmcss,
 			   crash_vmclear_local_loaded_vmcss);
Signature · 1 line
2.25.1
#4 lore raw
In reply to Vitaly Kuznetsov
Quoted 52 lines · Vitaly Kuznetsov

If KVM wasn't used at all before we crash the cleanup procedure fails with

 BUG: unable to handle page fault for address: ffffffffffffffc8
 #PF: supervisor read access in kernel mode
 #PF: error_code(0x0000) - not-present page
 PGD 23215067 P4D 23215067 PUD 23217067 PMD 0
 Oops: 0000 [#8] SMP PTI
 CPU: 0 PID: 3542 Comm: bash Kdump: loaded Tainted: G      D           5.6.0-rc2+ #823
 RIP: 0010:crash_vmclear_local_loaded_vmcss.cold+0x19/0x51 [kvm_intel]

The root cause is that loaded_vmcss_on_cpu list is not yet initialized, we initialize it in hardware_enable() but this only happens when we start a VM.

Previously, we used to have a bitmap with enabled CPUs and that was preventing [masking] the issue.

Initialized loaded_vmcss_on_cpu list earlier, right before we assign crash_vmclear_loaded_vmcss pointer. blocked_vcpu_on_cpu list and blocked_vcpu_on_cpu_lock are moved altogether for consistency.

Fixes 31603d4fc2bb ("KVM: VMX: Always VMCLEAR in-use VMCSes during crash with kexec support")
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
            !hv_get_vp_assist_page(cpu))
                return -EFAULT;
 
-       INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-       INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-       spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
        r = kvm_cpu_vmxon(phys_addr);
        if (r)
                return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-       int r;
+       int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
        /*

arch/x86/kvm/vmx/vmx.c static int __init vmx_init(void) · line 8063

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
                return r;
        }
 
+       for_each_possible_cpu(cpu) {
+               INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+               INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+               spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+       }

Just above this chunk, we have:

r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);

if (r) {
        vmx_exit();

...

If we take that early exit, because vmx_setup_l1d_flush() fails, we won't initialize loaded_vmcss_on_cpu. However, vmx_exit() calls kvm_exit(), which calls on_each_cpu(hardware_disable_nolock, NULL, 1). Hardware_disable_nolock() then calls kvm_arch_hardware_disable(), which calls kvm_x86_ops.hardware_disable() [the vmx.c hardware_disable()], which calls vmclear_local_loaded_vmcss().

I believe that vmclear_local_loaded_vmcss() will then try to dereference a NULL pointer, since per_cpu(loaded_vmcss_on_cpu, cpu) is uninitialzed.

Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +0 -1
 #ifdef CONFIG_KEXEC_CORE
        rcu_assign_pointer(crash_vmclear_loaded_vmcss,
                           crash_vmclear_local_loaded_vmcss);
--

2.25.1

#5 lore raw
Quoted 53 lines · Jim Mattson
Quoted 29 lines · Vitaly Kuznetsov
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
            !hv_get_vp_assist_page(cpu))
                return -EFAULT;
 
-       INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-       INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-       spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
        r = kvm_cpu_vmxon(phys_addr);
        if (r)
                return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-       int r;
+       int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
        /*

arch/x86/kvm/vmx/vmx.c static int __init vmx_init(void) · line 8063

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
                return r;
        }
 
+       for_each_possible_cpu(cpu) {
+               INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+               INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+               spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+       }

Just above this chunk, we have:

r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);

if (r) {
        vmx_exit();

...

If we take that early exit, because vmx_setup_l1d_flush() fails, we won't initialize loaded_vmcss_on_cpu. However, vmx_exit() calls kvm_exit(), which calls on_each_cpu(hardware_disable_nolock, NULL, 1). Hardware_disable_nolock() then calls kvm_arch_hardware_disable(), which calls kvm_x86_ops.hardware_disable() [the vmx.c hardware_disable()], which calls vmclear_local_loaded_vmcss().

Quoted 3 lines

I believe that vmclear_local_loaded_vmcss() will then try to dereference a NULL pointer, since per_cpu(loaded_vmcss_on_cpu, cpu) is uninitialzed.

I agree the code is a mess (kvm_init() and kvm_exit() included), but I'm pretty sure hardware_disable_nolock() is guaranteed to be a nop as it's impossible for kvm_usage_count to be non-zero if vmx_init() hasn't finished.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/vmx/vmx.c +0 -1
 #ifdef CONFIG_KEXEC_CORE
        rcu_assign_pointer(crash_vmclear_loaded_vmcss,
                           crash_vmclear_local_loaded_vmcss);
--

2.25.1

#6 lore raw
Quoted 55 lines · Sean Christopherson
Quoted 53 lines · Jim Mattson
Quoted 29 lines · Vitaly Kuznetsov
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
            !hv_get_vp_assist_page(cpu))
                return -EFAULT;
 
-       INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-       INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-       spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
        r = kvm_cpu_vmxon(phys_addr);
        if (r)
                return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-       int r;
+       int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
        /*

arch/x86/kvm/vmx/vmx.c static int __init vmx_init(void) · line 8063

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
                return r;
        }
 
+       for_each_possible_cpu(cpu) {
+               INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+               INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+               spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+       }

Just above this chunk, we have:

r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);

if (r) {
        vmx_exit();

...

If we take that early exit, because vmx_setup_l1d_flush() fails, we won't initialize loaded_vmcss_on_cpu. However, vmx_exit() calls kvm_exit(), which calls on_each_cpu(hardware_disable_nolock, NULL, 1). Hardware_disable_nolock() then calls kvm_arch_hardware_disable(), which calls kvm_x86_ops.hardware_disable() [the vmx.c hardware_disable()], which calls vmclear_local_loaded_vmcss().

Quoted 8 lines
Quoted 3 lines

I believe that vmclear_local_loaded_vmcss() will then try to dereference a NULL pointer, since per_cpu(loaded_vmcss_on_cpu, cpu) is uninitialzed.

I agree the code is a mess (kvm_init() and kvm_exit() included), but I'm pretty sure hardware_disable_nolock() is guaranteed to be a nop as it's impossible for kvm_usage_count to be non-zero if vmx_init() hasn't finished.

Unless I'm missing something, there's no check for a non-zero kvm_usage_count on this path. There is such a check in hardware_disable_all_nolock(), but not in hardware_disable_nolock().

#7 lore raw
Quoted 62 lines · Jim Mattson
Quoted 59 lines · Sean Christopherson
Quoted 56 lines · Jim Mattson
Quoted 29 lines · Vitaly Kuznetsov
---
 arch/x86/kvm/vmx/vmx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -5
index 3aba51d782e2..39a5dde12b79 100644
@@ -2257,10 +2257,6 @@ static int hardware_enable(void)
            !hv_get_vp_assist_page(cpu))
                return -EFAULT;
 
-       INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
-       INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
-       spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
-
        r = kvm_cpu_vmxon(phys_addr);
        if (r)
                return r;
@@ -8006,7 +8002,7 @@ module_exit(vmx_exit);
 
 static int __init vmx_init(void)
 {
-       int r;
+       int r, cpu;
 
 #if IS_ENABLED(CONFIG_HYPERV)
        /*

arch/x86/kvm/vmx/vmx.c static int __init vmx_init(void) · line 8063

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +5 -0
@@ -8060,6 +8056,12 @@ static int __init vmx_init(void)
                return r;
        }
 
+       for_each_possible_cpu(cpu) {
+               INIT_LIST_HEAD(&per_cpu(loaded_vmcss_on_cpu, cpu));
+               INIT_LIST_HEAD(&per_cpu(blocked_vcpu_on_cpu, cpu));
+               spin_lock_init(&per_cpu(blocked_vcpu_on_cpu_lock, cpu));
+       }

Just above this chunk, we have:

r = vmx_setup_l1d_flush(vmentry_l1d_flush_param);

if (r) {
        vmx_exit();

...

If we take that early exit, because vmx_setup_l1d_flush() fails, we won't initialize loaded_vmcss_on_cpu. However, vmx_exit() calls kvm_exit(), which calls on_each_cpu(hardware_disable_nolock, NULL, 1). Hardware_disable_nolock() then calls kvm_arch_hardware_disable(), which calls kvm_x86_ops.hardware_disable() [the vmx.c hardware_disable()], which calls vmclear_local_loaded_vmcss().

I believe that vmclear_local_loaded_vmcss() will then try to dereference a NULL pointer, since per_cpu(loaded_vmcss_on_cpu, cpu) is uninitialzed.

Quoted 8 lines
Quoted 4 lines

I agree the code is a mess (kvm_init() and kvm_exit() included), but I'm pretty sure hardware_disable_nolock() is guaranteed to be a nop as it's impossible for kvm_usage_count to be non-zero if vmx_init() hasn't finished.

Unless I'm missing something, there's no check for a non-zero kvm_usage_count on this path. There is such a check in hardware_disable_all_nolock(), but not in hardware_disable_nolock().

However, cpus_hardware_enabled shouldn't have any bits set, so everything's fine. Nothing to see here, after all.

#8 lore raw
Quoted 9 lines · Jim Mattson
Quoted 8 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson

I agree the code is a mess (kvm_init() and kvm_exit() included), but I'm pretty sure hardware_disable_nolock() is guaranteed to be a nop as it's impossible for kvm_usage_count to be non-zero if vmx_init() hasn't finished.

Quoted 6 lines
Quoted 3 lines

Unless I'm missing something, there's no check for a non-zero kvm_usage_count on this path. There is such a check in hardware_disable_all_nolock(), but not in hardware_disable_nolock().

However, cpus_hardware_enabled shouldn't have any bits set, so everything's fine. Nothing to see here, after all.

Ugh, I forgot that hardware_disable_all_nolock() does a BUG_ON() instead of bailing on !kvm_usage_count.

#9 lore raw
Quoted 14 lines · Sean Christopherson
Quoted 13 lines · Jim Mattson
Quoted 11 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson

I agree the code is a mess (kvm_init() and kvm_exit() included), but I'm pretty sure hardware_disable_nolock() is guaranteed to be a nop as it's impossible for kvm_usage_count to be non-zero if vmx_init() hasn't finished.

Unless I'm missing something, there's no check for a non-zero kvm_usage_count on this path. There is such a check in hardware_disable_all_nolock(), but not in hardware_disable_nolock().

Quoted 5 lines
Quoted 2 lines

However, cpus_hardware_enabled shouldn't have any bits set, so everything's fine. Nothing to see here, after all.

Ugh, I forgot that hardware_disable_all_nolock() does a BUG_ON() instead of bailing on !kvm_usage_count.

But we can't hit this BUG_ON(), right? I'm failing to see how hardware_disable_all_nolock() can be reached with kvm_usage_count==0.

Signature · 1 line
Vitaly
#10 lore raw
Quoted 16 lines · Vitaly Kuznetsov
Quoted 14 lines · Sean Christopherson
Quoted 13 lines · Jim Mattson
Quoted 11 lines · Jim Mattson
Quoted 4 lines · Sean Christopherson

I agree the code is a mess (kvm_init() and kvm_exit() included), but I'm pretty sure hardware_disable_nolock() is guaranteed to be a nop as it's impossible for kvm_usage_count to be non-zero if vmx_init() hasn't finished.

Unless I'm missing something, there's no check for a non-zero kvm_usage_count on this path. There is such a check in hardware_disable_all_nolock(), but not in hardware_disable_nolock().

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

However, cpus_hardware_enabled shouldn't have any bits set, so everything's fine. Nothing to see here, after all.

Ugh, I forgot that hardware_disable_all_nolock() does a BUG_ON() instead of bailing on !kvm_usage_count.

But we can't hit this BUG_ON(), right? I'm failing to see how hardware_disable_all_nolock() can be reached with kvm_usage_count==0.

Correct, I was mostly talking to myself.