Skip to contentSkip to conversation

target/loongarch/kvm: set pv features per vCPU in kvm_arch_init_vcpu

PATCH Superseded Tao Cui 2 participants 4 messages Jun 25 – Jul 9

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tao Cui

author
#0 lore raw

PATCH 1/1 target/loongarch/kvm: set pv features per vCPU in kvm_arch_init_vcpu

From: Tao Cui <[email protected]>

kvm_set_pv_features() programs the KVM_FEATURE cpucfg attribute, which is a per-vCPU setting. It was called from kvm_arch_put_registers() under a function-local static guard, so it ran only once for the whole VM and only the first vCPU actually got its pv features pushed to KVM.

Move the call to kvm_arch_init_vcpu(), which runs once per vCPU, right after the pv features are computed, and drop the static guard.

---
 target/loongarch/kvm/kvm.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
target/loongarch/kvm/kvm.c +6 -9
index d6539c12ac..985812c828 100644
@@ -816,7 +816,6 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp)
 int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
 {
     int ret;
-    static int once;
 
     ret = kvm_loongarch_put_regs_core(cs);
     if (ret) {
@@ -843,14 +842,6 @@ int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
         return ret;
     }
 
-    if (!once) {
-        ret = kvm_set_pv_features(cs);
-        if (ret) {
-            return ret;
-        }
-        once = 1;
-    }
-
     if (level >= KVM_PUT_FULL_STATE) {
         /*
          * only KVM_PUT_FULL_STATE is required, kvm kernel will clear
@@ -1209,6 +1200,12 @@ int kvm_arch_init_vcpu(CPUState *cs)
         return ret;
     }
 
+    /* pv_features is a per-vCPU attribute; set it here, once per vCPU. */
+    ret = kvm_set_pv_features(cs);
+    if (ret < 0) {
+        return ret;
+    }
+
     ret = kvm_cpu_check_ptw(cs, &local_err);
     if (ret < 0) {
         error_report_err(local_err);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 43 lines · Tao Cui

From: Tao Cui <[email protected]>

kvm_set_pv_features() programs the KVM_FEATURE cpucfg attribute, which is a per-vCPU setting. It was called from kvm_arch_put_registers() under a function-local static guard, so it ran only once for the whole VM and only the first vCPU actually got its pv features pushed to KVM.

Move the call to kvm_arch_init_vcpu(), which runs once per vCPU, right after the pv features are computed, and drop the static guard.

---
  target/loongarch/kvm/kvm.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
target/loongarch/kvm/kvm.c +0 -9
index d6539c12ac..985812c828 100644
@@ -816,7 +816,6 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp)
  int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
  {
      int ret;
-    static int once;
 
      ret = kvm_loongarch_put_regs_core(cs);
      if (ret) {
@@ -843,14 +842,6 @@ int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
          return ret;
      }
 
-    if (!once) {
-        ret = kvm_set_pv_features(cs);
-        if (ret) {
-            return ret;
-        }
-        once = 1;
-    }
-
      if (level >= KVM_PUT_FULL_STATE) {
          /*
           * only KVM_PUT_FULL_STATE is required, kvm kernel will clear

target/loongarch/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1207

Quoted 8 lines
target/loongarch/kvm/kvm.c +5 -0
@@ -1209,6 +1200,12 @@ int kvm_arch_init_vcpu(CPUState *cs)
          return ret;
      }
 
+    /* pv_features is a per-vCPU attribute; set it here, once per vCPU. */
+    ret = kvm_set_pv_features(cs);
+    if (ret < 0) {
+        return ret;
+    }

I prefer the old method. The pv_features is one vCPU state instead, and it will be added in vCPU VMState if migration is supported on different host kernel version.

Here is host feature detection, not vCPU state setting.

Regards
Bibo Mao

Quoted 5 lines
target/loongarch/kvm/kvm.c +1 -0
+
      ret = kvm_cpu_check_ptw(cs, &local_err);
      if (ret < 0) {
          error_report_err(local_err);
author
#2 lore raw
Quoted 53 lines · Bibo Mao
Quoted 43 lines · Tao Cui

From: Tao Cui <[email protected]>

kvm_set_pv_features() programs the KVM_FEATURE cpucfg attribute, which is a per-vCPU setting. It was called from kvm_arch_put_registers() under a function-local static guard, so it ran only once for the whole VM and only the first vCPU actually got its pv features pushed to KVM.

Move the call to kvm_arch_init_vcpu(), which runs once per vCPU, right after the pv features are computed, and drop the static guard.

---
  target/loongarch/kvm/kvm.c | 15 ++++++---------

  1 file changed, 6 insertions(+), 9 deletions(-)
target/loongarch/kvm/kvm.c +0 -8
index d6539c12ac..985812c828 100644
@@ -816,7 +816,6 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp)
   int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
   {
       int ret;
-    static int once;
         ret = kvm_loongarch_put_regs_core(cs);
       if (ret) {
@@ -843,14 +842,6 @@ int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
           return ret;
       }
   -    if (!once) {
-        ret = kvm_set_pv_features(cs);
-        if (ret) {
-            return ret;
-        }
-        once = 1;
-    }
-
       if (level >= KVM_PUT_FULL_STATE) {
           /*
            * only KVM_PUT_FULL_STATE is required, kvm kernel will clear
@@ -1209,6 +1200,12 @@ int kvm_arch_init_vcpu(CPUState *cs)
           return ret;
       }
Quoted 5 lines

  +    /* pv_features is a per-vCPU attribute; set it here, once per vCPU. */ +    ret = kvm_set_pv_features(cs);
+    if (ret < 0) {
+        return ret;
+    }

I prefer the old method. The pv_features is one vCPU state instead, and it will be added in vCPU VMState if migration is supported on different host kernel version.

Quoted 1 line

Here is host feature detection, not vCPU state setting.

Thanks, that makes sense. init_vcpu() is the host-detection path; the per-vCPU write belongs in kvm_arch_put_registers(). I'll keep it there, drop the VM-global static guard, and push per-vCPU under level >= KVM_PUT_FULL_STATE, the same gate kvm_set_stealtime() already uses just below.

For migration across kernel versions, I'll add env->pv_features as a sub-section of vmstate_loongarch_cpu (leaving version 4 untouched) so the value travels with the vCPU.

Thanks,
Tao

Quoted 9 lines

Regards
Bibo Mao

Quoted 5 lines

+
      ret = kvm_cpu_check_ptw(cs, &local_err);
      if (ret < 0) {
          error_report_err(local_err);

#3 lore raw
Quoted 64 lines · Tao Cui
Quoted 53 lines · Bibo Mao
Quoted 41 lines · Tao Cui

From: Tao Cui <[email protected]>

kvm_set_pv_features() programs the KVM_FEATURE cpucfg attribute, which is a per-vCPU setting. It was called from kvm_arch_put_registers() under a function-local static guard, so it ran only once for the whole VM and only the first vCPU actually got its pv features pushed to KVM.

Move the call to kvm_arch_init_vcpu(), which runs once per vCPU, right after the pv features are computed, and drop the static guard.

---
   target/loongarch/kvm/kvm.c | 15 ++++++---------
   1 file changed, 6 insertions(+), 9 deletions(-)
target/loongarch/kvm/kvm.c +0 -8
index d6539c12ac..985812c828 100644
@@ -816,7 +816,6 @@ int kvm_arch_get_registers(CPUState *cs, Error **errp)
   int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
   {
       int ret;
-    static int once;
         ret = kvm_loongarch_put_regs_core(cs);
       if (ret) {
@@ -843,14 +842,6 @@ int kvm_arch_put_registers(CPUState *cs, KvmPutState level, Error **errp)
           return ret;
       }
   -    if (!once) {
-        ret = kvm_set_pv_features(cs);
-        if (ret) {
-            return ret;
-        }
-        once = 1;
-    }
-
       if (level >= KVM_PUT_FULL_STATE) {
           /*
            * only KVM_PUT_FULL_STATE is required, kvm kernel will clear

target/loongarch/kvm/kvm.c int kvm_arch_init_vcpu(CPUState *cs) · line 1206

Quoted 7 lines
target/loongarch/kvm/kvm.c +4 -0
@@ -1209,6 +1200,12 @@ int kvm_arch_init_vcpu(CPUState *cs)
           return ret;
       }
   +    /* pv_features is a per-vCPU attribute; set it here, once per vCPU. */
+    ret = kvm_set_pv_features(cs);
+    if (ret < 0) {
+        return ret;
+    }

I prefer the old method. The pv_features is one vCPU state instead, and it will be added in vCPU VMState if migration is supported on different host kernel version.

Quoted 1 line

Here is host feature detection, not vCPU state setting.

Thanks, that makes sense. init_vcpu() is the host-detection path; the per-vCPU write belongs in kvm_arch_put_registers(). I'll keep it there, drop the VM-global static guard, and push per-vCPU under level >= KVM_PUT_FULL_STATE, the same gate kvm_set_stealtime() already uses just below.

Quoted 3 lines

For migration across kernel versions, I'll add env->pv_features as a sub-section of vmstate_loongarch_cpu (leaving version 4 untouched) so the value travels with the vCPU.

yes, instead pv_features is the value of pseudo register CPUCFG_KVM_FEATURE 0x40000004, defined in kernel file arch/loongarch/include/uapi/asm/kvm_para.h.

Also also remember to add field no_pv_feature in LoongArchCPU object and VirtMachineClass. So that with QEMU 11.1 machine type, no_pv_feature is true, with higer version than QEMU 11.1, no_pv_feature is default false.

Regards
Bibo Mao

Quoted 14 lines

Thanks,
Tao

Quoted 9 lines

Regards
Bibo Mao

Quoted 5 lines
target/loongarch/kvm/kvm.c +1 -0
+
       ret = kvm_cpu_check_ptw(cs, &local_err);
       if (ret < 0) {
           error_report_err(local_err);