Skip to contentSkip to conversation

KVM: SVM: Initialize ir_list and ir_list_lock regardless of AVIC enablement

PATCH In review Suravee Suthikulpanit 2 participants 4 messages Sep 22, 2020 – Oct 3, 2020

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: SVM: Initialize ir_list and ir_list_lock regardless of AVIC enablement

The struct vcpu_svm.ir_list and ir_list_lock are being accessed even when AVIC is not enabled, while current code only initialize the list and the lock only when AVIC is enabled. This ended up trigger NULL pointer dereference bug in the function vm_ir_list_del with the following call trace:

    svm_update_pi_irte+0x3c2/0x550 [kvm_amd]
    ? proc_create_single_data+0x41/0x50
    kvm_arch_irq_bypass_add_producer+0x40/0x60 [kvm]
    __connect+0x5f/0xb0 [irqbypass]
    irq_bypass_register_producer+0xf8/0x120 [irqbypass]
    vfio_msi_set_vector_signal+0x1de/0x2d0 [vfio_pci]
    vfio_msi_set_block+0x77/0xe0 [vfio_pci]
    vfio_pci_set_msi_trigger+0x25c/0x2f0 [vfio_pci]
    vfio_pci_set_irqs_ioctl+0x88/0xb0 [vfio_pci]
    vfio_pci_ioctl+0x2ea/0xed0 [vfio_pci]
    ? alloc_file_pseudo+0xa5/0x100
    vfio_device_fops_unl_ioctl+0x26/0x30 [vfio]
    ? vfio_device_fops_unl_ioctl+0x26/0x30 [vfio]
    __x64_sys_ioctl+0x96/0xd0
    do_syscall_64+0x37/0x80
    entry_SYSCALL_64_after_hwframe+0x44/0xa9

Therefore, move the initialziation code before checking for AVIC enabled so that it is always excuted.

Fixes dfa20099e26e ("KVM: SVM: Refactor AVIC vcpu initialization into avic_init_vcpu()")
---
 arch/x86/kvm/svm/avic.c | 2 --
 arch/x86/kvm/svm/svm.c  | 3 +++
 2 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/avic.c +0 -2
index ac830cd50830..1ccf13783785 100644
@@ -572,8 +572,6 @@ int avic_init_vcpu(struct vcpu_svm *svm)
 	if (ret)
 		return ret;
 
-	INIT_LIST_HEAD(&svm->ir_list);
-	spin_lock_init(&svm->ir_list_lock);
 	svm->dfr_reg = APIC_DFR_FLAT;
 
 	return ret;
arch/x86/kvm/svm/svm.c +3 -0
index c44f3e9140d5..714d791fe5a5 100644
@@ -1225,6 +1225,9 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)
 	svm_init_osvw(vcpu);
 	vcpu->arch.microcode_version = 0x01000065;
 
+	INIT_LIST_HEAD(&svm->ir_list);
+	spin_lock_init(&svm->ir_list_lock);
+
 	return 0;
 
 free_page4:
Signature · 1 line
2.17.1
#1 lore raw

Hi,

Are there any issues or concerns about this patch?

Thank you,
Suravee

Quoted 61 lines · Suravee Suthikulpanit

The struct vcpu_svm.ir_list and ir_list_lock are being accessed even when AVIC is not enabled, while current code only initialize the list and the lock only when AVIC is enabled. This ended up trigger NULL pointer dereference bug in the function vm_ir_list_del with the following call trace:

     svm_update_pi_irte+0x3c2/0x550 [kvm_amd]
     ? proc_create_single_data+0x41/0x50
     kvm_arch_irq_bypass_add_producer+0x40/0x60 [kvm]
     __connect+0x5f/0xb0 [irqbypass]
     irq_bypass_register_producer+0xf8/0x120 [irqbypass]
     vfio_msi_set_vector_signal+0x1de/0x2d0 [vfio_pci]
     vfio_msi_set_block+0x77/0xe0 [vfio_pci]
     vfio_pci_set_msi_trigger+0x25c/0x2f0 [vfio_pci]
     vfio_pci_set_irqs_ioctl+0x88/0xb0 [vfio_pci]
     vfio_pci_ioctl+0x2ea/0xed0 [vfio_pci]
     ? alloc_file_pseudo+0xa5/0x100
     vfio_device_fops_unl_ioctl+0x26/0x30 [vfio]
     ? vfio_device_fops_unl_ioctl+0x26/0x30 [vfio]
     __x64_sys_ioctl+0x96/0xd0
     do_syscall_64+0x37/0x80
     entry_SYSCALL_64_after_hwframe+0x44/0xa9

Therefore, move the initialziation code before checking for AVIC enabled so that it is always excuted.

Fixes dfa20099e26e ("KVM: SVM: Refactor AVIC vcpu initialization into avic_init_vcpu()")
---
  arch/x86/kvm/svm/avic.c | 2 --
  arch/x86/kvm/svm/svm.c  | 3 +++
  2 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/avic.c +0 -2
index ac830cd50830..1ccf13783785 100644
@@ -572,8 +572,6 @@ int avic_init_vcpu(struct vcpu_svm *svm)
  	if (ret)
  		return ret;
 
-	INIT_LIST_HEAD(&svm->ir_list);
-	spin_lock_init(&svm->ir_list_lock);
  	svm->dfr_reg = APIC_DFR_FLAT;
 
  	return ret;
arch/x86/kvm/svm/svm.c +3 -0
index c44f3e9140d5..714d791fe5a5 100644
@@ -1225,6 +1225,9 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)
  	svm_init_osvw(vcpu);
  	vcpu->arch.microcode_version = 0x01000065;
 
+	INIT_LIST_HEAD(&svm->ir_list);
+	spin_lock_init(&svm->ir_list_lock);
+
  	return 0;
 
  free_page4:
maintainer
#2 lore raw
Quoted 3 lines · Suravee Suthikulpanit

Hi,

Are there any issues or concerns about this patch?

Yes, sorry I haven't replied yet. Looks like Linus is doing an -rc8 so there's plenty of time to have it in 5.9.

The thing I'm wondering is, why is svm_update_pi_irte doing anything if you don't have AVIC enabled? In other word, this might not be the root cause of the bug. You always get to the "else" branch of the loop of course, and I'm not sure how irq_set_vcpu_affinity returns something with pi.prev_ga_tag set.

Thanks,

Paolo

Quoted 63 lines

Thank you,
Suravee

Quoted 58 lines · Suravee Suthikulpanit

The struct vcpu_svm.ir_list and ir_list_lock are being accessed even when AVIC is not enabled, while current code only initialize the list and the lock only when AVIC is enabled. This ended up trigger NULL pointer dereference bug in the function vm_ir_list_del with the following call trace:

     svm_update_pi_irte+0x3c2/0x550 [kvm_amd]
     ? proc_create_single_data+0x41/0x50
     kvm_arch_irq_bypass_add_producer+0x40/0x60 [kvm]
     __connect+0x5f/0xb0 [irqbypass]
     irq_bypass_register_producer+0xf8/0x120 [irqbypass]
     vfio_msi_set_vector_signal+0x1de/0x2d0 [vfio_pci]
     vfio_msi_set_block+0x77/0xe0 [vfio_pci]
     vfio_pci_set_msi_trigger+0x25c/0x2f0 [vfio_pci]
     vfio_pci_set_irqs_ioctl+0x88/0xb0 [vfio_pci]
     vfio_pci_ioctl+0x2ea/0xed0 [vfio_pci]
     ? alloc_file_pseudo+0xa5/0x100
     vfio_device_fops_unl_ioctl+0x26/0x30 [vfio]
     ? vfio_device_fops_unl_ioctl+0x26/0x30 [vfio]
     __x64_sys_ioctl+0x96/0xd0
     do_syscall_64+0x37/0x80
     entry_SYSCALL_64_after_hwframe+0x44/0xa9

Therefore, move the initialziation code before checking for AVIC enabled so that it is always excuted.

Fixes dfa20099e26e ("KVM: SVM: Refactor AVIC vcpu initialization into

avic_init_vcpu()")

---
  arch/x86/kvm/svm/avic.c | 2 --
  arch/x86/kvm/svm/svm.c  | 3 +++

  2 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/avic.c +0 -1
index ac830cd50830..1ccf13783785 100644
@@ -572,8 +572,6 @@ int avic_init_vcpu(struct vcpu_svm *svm)
       if (ret)
           return ret;
   -    INIT_LIST_HEAD(&svm->ir_list);
-    spin_lock_init(&svm->ir_list_lock);
       svm->dfr_reg = APIC_DFR_FLAT;
         return ret;
arch/x86/kvm/svm/svm.c +2 -0
index c44f3e9140d5..714d791fe5a5 100644
@@ -1225,6 +1225,9 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu)
       svm_init_osvw(vcpu);
       vcpu->arch.microcode_version = 0x01000065;
   +    INIT_LIST_HEAD(&svm->ir_list);
+    spin_lock_init(&svm->ir_list_lock);
+
       return 0;

    free_page4:

#3 lore raw

Paolo,

Quoted 5 lines · Paolo Bonzini
Quoted 3 lines · Suravee Suthikulpanit

Hi,

Are there any issues or concerns about this patch?

Quoted 8 lines

Yes, sorry I haven't replied yet. Looks like Linus is doing an -rc8 so there's plenty of time to have it in 5.9.

The thing I'm wondering is, why is svm_update_pi_irte doing anything if you don't have AVIC enabled? In other word, this might not be the root cause of the bug. You always get to the "else" branch of the loop of course, and I'm not sure how irq_set_vcpu_affinity returns something with pi.prev_ga_tag set.

You are right. pi_prev_ga_tag needs to be initialized before used (in case AVIC is not enabled). I have already sent out another patch to properly fix the issue instead with subject
(KVM: SVM: Initialize prev_ga_tag before use).

Thanks,
Suravee