Re: [PATCH] LoongArch: KVM: Allow to set pv_feature until vCPU run

Bibo Mao <[email protected]> Thu, 16 Jul 2026 14:08:49 +0800
Newsgroups dev.linux.lists.loongarch,org.kernel.vger.kvm,org.kernel.vger.linux-kernel,org.kernel.vger.linux-kselftest
Message-ID <[email protected]>

On 2026/7/16 下午12:27, Huacai Chen wrote:
> Hi, Bibo,
> 
> On Thu, Jul 16, 2026 at 9:45 AM Bibo Mao <[email protected]> wrote:
>>
>> Now pv_feature can be set only once, there is problem with VM migration.
>> Where it is set when vCPU is created and after migration, here it is
>> allow to set for many times, until vCPU starts to run.
>>
>> Signed-off-by: Bibo Mao <[email protected]>
>> ---
>>   arch/loongarch/include/asm/kvm_host.h |  4 +++-
>>   arch/loongarch/kvm/vcpu.c             | 15 +++++++++++----
>>   2 files changed, 14 insertions(+), 5 deletions(-)
>>
>> diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/include/asm/kvm_host.h
>> index 23cfbecebbd7..af376fc44c44 100644
>> --- a/arch/loongarch/include/asm/kvm_host.h
>> +++ b/arch/loongarch/include/asm/kvm_host.h
>> @@ -163,7 +163,6 @@ enum emulation_result {
>>   #define KVM_LARCH_SWCSR_LATEST (0x1 << 3)
>>   #define KVM_LARCH_HWCSR_USABLE (0x1 << 4)
>>
>> -#define LOONGARCH_PV_FEAT_UPDATED      BIT_ULL(63)
>>   #define LOONGARCH_PV_FEAT_MASK         (BIT(KVM_FEATURE_IPI) |         \
>>                                           BIT(KVM_FEATURE_PREEMPT) |     \
>>                                           BIT(KVM_FEATURE_STEAL_TIME) |  \
>> @@ -250,6 +249,9 @@ struct kvm_vcpu_arch {
>>          /* cpucfg */
>>          u32 cpucfg[KVM_MAX_CPUCFG_REGS];
>>
>> +       /* VCPU ran at least once */
>> +       bool ran_atleast_once;
> So long a name? Maybe has_run is enough?
well, will use has_run for short.

Regards
Bibo Mao
> 
> Huacai
>> +
>>          /* paravirt steal time */
>>          struct {
>>                  u64 guest_addr;
>> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
>> index 20c207d80e31..ce6a1b06d50d 100644
>> --- a/arch/loongarch/kvm/vcpu.c
>> +++ b/arch/loongarch/kvm/vcpu.c
>> @@ -1164,11 +1164,14 @@ static int kvm_loongarch_cpucfg_set_attr(struct kvm_vcpu *vcpu,
>>                  if (val & ~valid)
>>                          return -EINVAL;
>>
>> -               /* All vCPUs need set the same PV features */
>> -               if ((kvm->arch.pv_features & LOONGARCH_PV_FEAT_UPDATED)
>> -                               && ((kvm->arch.pv_features & valid) != val))
>> +               if ((kvm->arch.pv_features & valid) == val)
>> +                       return 0;
>> +
>> +               if (vcpu->arch.ran_atleast_once)
>>                          return -EINVAL;
>> -               kvm->arch.pv_features = val | LOONGARCH_PV_FEAT_UPDATED;
>> +
>> +               /* All vCPUs need set the same PV features */
>> +               kvm->arch.pv_features = val;
>>                  return 0;
>>          default:
>>                  return -ENXIO;
>> @@ -1851,6 +1854,10 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
>>          int r = -EINTR;
>>          struct kvm_run *run = vcpu->run;
>>
>> +       /* Mark this VCPU ran at least once */
>> +       if (!vcpu->arch.ran_atleast_once)
>> +               vcpu->arch.ran_atleast_once = true;
>> +
>>          if (vcpu->mmio_needed) {
>>                  if (!vcpu->mmio_is_write)
>>                          kvm_complete_mmio_read(vcpu, run);
>>
>> base-commit: a13c140cc289c0b7b3770bce5b3ad42ab35074aa
>> --
>> 2.39.3
>>