Skip to contentSkip to conversation

target/i386: Set maximum APIC ID to KVM prior to vCPU creation

PATCH Applied Zeng Guang 4 participants 6 messages Aug 25, 2022 – Oct 26, 2022 KVM

lore
Share & tools
lore

Zeng Guang

author
#0 lore raw

PATCH v3 1/1 target/i386: Set maximum APIC ID to KVM prior to vCPU creation

Specify maximum possible APIC ID assigned for current VM session to KVM prior to the creation of vCPUs. By this setting, KVM can set up VM-scoped data structure indexed by the APIC ID, e.g. Posted-Interrupt Descriptor pointer table to support Intel IPI virtualization, with the most optimal memory footprint.

It can be achieved by calling KVM_ENABLE_CAP for KVM_CAP_MAX_VCPU_ID capability once KVM has enabled it. Ignoring the return error if KVM doesn't support this capability yet.

---
 hw/i386/x86.c              | 4 ++++
 target/i386/kvm/kvm-stub.c | 5 +++++
 target/i386/kvm/kvm.c      | 5 +++++
 target/i386/kvm/kvm_i386.h | 1 +
 4 files changed, 15 insertions(+)
hw/i386/x86.c +4 -0
index 050eedc0c8..4831193c86 100644
@@ -139,6 +139,10 @@ void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
         exit(EXIT_FAILURE);
     }
 
+    if (kvm_enabled()) {
+        kvm_set_max_apic_id(x86ms->apic_id_limit);
+    }
+
     possible_cpus = mc->possible_cpu_arch_ids(ms);
     for (i = 0; i < ms->smp.cpus; i++) {
         x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
target/i386/kvm/kvm-stub.c +5 -0
index f6e7e4466e..e052f1c7b0 100644
@@ -44,3 +44,8 @@ bool kvm_hyperv_expand_features(X86CPU *cpu, Error **errp)
 {
     abort();
 }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    return;
+}
target/i386/kvm/kvm.c +5 -0
index f148a6d52f..af4ef1e8f0 100644
@@ -5428,3 +5428,8 @@ void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask)
         mask &= ~BIT_ULL(bit);
     }
 }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    kvm_vm_enable_cap(kvm_state, KVM_CAP_MAX_VCPU_ID, 0, max_apic_id);
+}
target/i386/kvm/kvm_i386.h +1 -0
index 4124912c20..c133b32a58 100644
@@ -54,4 +54,5 @@ uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address);
 bool kvm_enable_sgx_provisioning(KVMState *s);
 void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask);
 
+void kvm_set_max_apic_id(uint32_t max_apic_id);
 #endif
Signature · 1 line
2.27.0
author
#1 lore raw

Kindly PING!

Quoted 69 lines · Zeng Guang

Specify maximum possible APIC ID assigned for current VM session to KVM prior to the creation of vCPUs. By this setting, KVM can set up VM-scoped data structure indexed by the APIC ID, e.g. Posted-Interrupt Descriptor pointer table to support Intel IPI virtualization, with the most optimal memory footprint.

It can be achieved by calling KVM_ENABLE_CAP for KVM_CAP_MAX_VCPU_ID capability once KVM has enabled it. Ignoring the return error if KVM doesn't support this capability yet.

---
  hw/i386/x86.c              | 4 ++++
  target/i386/kvm/kvm-stub.c | 5 +++++
  target/i386/kvm/kvm.c      | 5 +++++
  target/i386/kvm/kvm_i386.h | 1 +
  4 files changed, 15 insertions(+)
hw/i386/x86.c +4 -0
index 050eedc0c8..4831193c86 100644
@@ -139,6 +139,10 @@ void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
          exit(EXIT_FAILURE);
      }
 
+    if (kvm_enabled()) {
+        kvm_set_max_apic_id(x86ms->apic_id_limit);
+    }
+
      possible_cpus = mc->possible_cpu_arch_ids(ms);
      for (i = 0; i < ms->smp.cpus; i++) {
          x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
target/i386/kvm/kvm-stub.c +5 -0
index f6e7e4466e..e052f1c7b0 100644
@@ -44,3 +44,8 @@ bool kvm_hyperv_expand_features(X86CPU *cpu, Error **errp)
  {
      abort();
  }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    return;
+}
target/i386/kvm/kvm.c +5 -0
index f148a6d52f..af4ef1e8f0 100644
@@ -5428,3 +5428,8 @@ void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask)
          mask &= ~BIT_ULL(bit);
      }
  }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    kvm_vm_enable_cap(kvm_state, KVM_CAP_MAX_VCPU_ID, 0, max_apic_id);
+}
target/i386/kvm/kvm_i386.h +1 -0
index 4124912c20..c133b32a58 100644
@@ -54,4 +54,5 @@ uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address);
  bool kvm_enable_sgx_provisioning(KVMState *s);
  void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask);
 
+void kvm_set_max_apic_id(uint32_t max_apic_id);
  #endif
author
#2 lore raw

PING again !
This QEMU patch is to optimize max APIC ID set for current VM session introduced since linux v6.0. It's also compatible with previous linux version.

Thanks.

Quoted 72 lines · Zeng Guang

Kindly PING!

Quoted 69 lines · Zeng Guang

Specify maximum possible APIC ID assigned for current VM session to KVM prior to the creation of vCPUs. By this setting, KVM can set up VM-scoped data structure indexed by the APIC ID, e.g. Posted-Interrupt Descriptor pointer table to support Intel IPI virtualization, with the most optimal memory footprint.

It can be achieved by calling KVM_ENABLE_CAP for KVM_CAP_MAX_VCPU_ID capability once KVM has enabled it. Ignoring the return error if KVM doesn't support this capability yet.

---
   hw/i386/x86.c              | 4 ++++
   target/i386/kvm/kvm-stub.c | 5 +++++
   target/i386/kvm/kvm.c      | 5 +++++
   target/i386/kvm/kvm_i386.h | 1 +
   4 files changed, 15 insertions(+)
hw/i386/x86.c +4 -0
index 050eedc0c8..4831193c86 100644
@@ -139,6 +139,10 @@ void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
           exit(EXIT_FAILURE);
       }
 
+    if (kvm_enabled()) {
+        kvm_set_max_apic_id(x86ms->apic_id_limit);
+    }
+
       possible_cpus = mc->possible_cpu_arch_ids(ms);
       for (i = 0; i < ms->smp.cpus; i++) {
           x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
target/i386/kvm/kvm-stub.c +5 -0
index f6e7e4466e..e052f1c7b0 100644
@@ -44,3 +44,8 @@ bool kvm_hyperv_expand_features(X86CPU *cpu, Error **errp)
   {
       abort();
   }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    return;
+}
target/i386/kvm/kvm.c +5 -0
index f148a6d52f..af4ef1e8f0 100644
@@ -5428,3 +5428,8 @@ void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask)
           mask &= ~BIT_ULL(bit);
       }
   }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    kvm_vm_enable_cap(kvm_state, KVM_CAP_MAX_VCPU_ID, 0, max_apic_id);
+}
target/i386/kvm/kvm_i386.h +1 -0
index 4124912c20..c133b32a58 100644
@@ -54,4 +54,5 @@ uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address);
   bool kvm_enable_sgx_provisioning(KVMState *s);
   void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask);
 
+void kvm_set_max_apic_id(uint32_t max_apic_id);
   #endif
#3 lore raw
Quoted 70 lines · Zeng Guang

PING again !
This QEMU patch is to optimize max APIC ID set for current VM session introduced since linux v6.0. It's also compatible with previous linux version.

Thanks.

Quoted 62 lines · Zeng Guang

Kindly PING!

Quoted 59 lines · Zeng Guang

Specify maximum possible APIC ID assigned for current VM session to KVM prior to the creation of vCPUs. By this setting, KVM can set up VM-scoped data structure indexed by the APIC ID, e.g. Posted-Interrupt Descriptor pointer table to support Intel IPI virtualization, with the most optimal memory footprint.

It can be achieved by calling KVM_ENABLE_CAP for KVM_CAP_MAX_VCPU_ID capability once KVM has enabled it. Ignoring the return error if KVM doesn't support this capability yet.

---
   hw/i386/x86.c              | 4 ++++
   target/i386/kvm/kvm-stub.c | 5 +++++
   target/i386/kvm/kvm.c      | 5 +++++
   target/i386/kvm/kvm_i386.h | 1 +
   4 files changed, 15 insertions(+)
hw/i386/x86.c +4 -0
index 050eedc0c8..4831193c86 100644
@@ -139,6 +139,10 @@ void x86_cpus_init(X86MachineState *x86ms, int default_cpu_version)
           exit(EXIT_FAILURE);
       }
+    if (kvm_enabled()) {
+        kvm_set_max_apic_id(x86ms->apic_id_limit);
+    }
+
       possible_cpus = mc->possible_cpu_arch_ids(ms);
       for (i = 0; i < ms->smp.cpus; i++) {
           x86_cpu_new(x86ms, possible_cpus->cpus[i].arch_id, &error_fatal);
target/i386/kvm/kvm-stub.c +5 -0
index f6e7e4466e..e052f1c7b0 100644
@@ -44,3 +44,8 @@ bool kvm_hyperv_expand_features(X86CPU *cpu, Error **errp)
   {
       abort();
   }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    return;
+}
target/i386/kvm/kvm.c +5 -0
index f148a6d52f..af4ef1e8f0 100644
@@ -5428,3 +5428,8 @@ void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask)
           mask &= ~BIT_ULL(bit);
       }
   }
+
+void kvm_set_max_apic_id(uint32_t max_apic_id)
+{
+    kvm_vm_enable_cap(kvm_state, KVM_CAP_MAX_VCPU_ID, 0, max_apic_id);
+}
target/i386/kvm/kvm_i386.h +0 -0
Quoted 8 lines
Quoted 8 lines

target/i386/kvm/kvm_i386.h uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address); · line 57

Quoted 8 lines

index 4124912c20..c133b32a58 100644

target/i386/kvm/kvm_i386.h +1 -0
@@ -54,4 +54,5 @@ uint64_t kvm_swizzle_msi_ext_dest_id(uint64_t address);
   bool kvm_enable_sgx_provisioning(KVMState *s);
   void kvm_request_xsave_components(X86CPU *cpu, uint64_t mask);
+void kvm_set_max_apic_id(uint32_t max_apic_id);
   #endif

Looks ok on the surface, but this is Paolo's area.

Signature · 1 line
MST
Acked-by on v3 1/1
#4 lore raw
In reply to Zeng Guang
Quoted 11 lines · Zeng Guang

Specify maximum possible APIC ID assigned for current VM session to KVM prior to the creation of vCPUs. By this setting, KVM can set up VM-scoped data structure indexed by the APIC ID, e.g. Posted-Interrupt Descriptor pointer table to support Intel IPI virtualization, with the most optimal memory footprint.

It can be achieved by calling KVM_ENABLE_CAP for KVM_CAP_MAX_VCPU_ID capability once KVM has enabled it. Ignoring the return error if KVM doesn't support this capability yet.

Signature · 1 line
Peter Xu