diff mbox series

KVM: loongarch: Add vcpu id check before create vcpu

Message ID 20240412084703.1407412-1-wjduan@linx-info.com (mailing list archive)
State New
Headers show
Series KVM: loongarch: Add vcpu id check before create vcpu | expand

Commit Message

Wujie Duan April 12, 2024, 8:47 a.m. UTC
Add a pre-allocation arch condition to checks that vcpu id should
smaller than max_vcpus

Signed-off-by: Wujie Duan <wjduan@linx-info.com>
---
 arch/loongarch/kvm/vcpu.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

maobibo April 12, 2024, 8:58 a.m. UTC | #1
On 2024/4/12 下午4:47, Wujie Duan wrote:
> Add a pre-allocation arch condition to checks that vcpu id should
> smaller than max_vcpus
> 
> Signed-off-by: Wujie Duan <wjduan@linx-info.com>
> ---
>   arch/loongarch/kvm/vcpu.c | 3 +++
>   1 file changed, 3 insertions(+)
> 
> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> index 3a8779065f73..d41cacf39583 100644
> --- a/arch/loongarch/kvm/vcpu.c
> +++ b/arch/loongarch/kvm/vcpu.c
> @@ -884,6 +884,9 @@ long kvm_arch_vcpu_async_ioctl(struct file *filp,
>   
>   int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
>   {
> +	if (id >= kvm->max_vcpus)
> +		return -EINVAL;
> +
>   	return 0;
>   }
>   
> 
Good catch, and thanks for your contribution.

Reviewed-by: bibo mao <maobibo@loongson.cn>
Sean Christopherson April 12, 2024, 6:41 p.m. UTC | #2
On Fri, Apr 12, 2024, Wujie Duan wrote:
> Add a pre-allocation arch condition to checks that vcpu id should
> smaller than max_vcpus
> 
> Signed-off-by: Wujie Duan <wjduan@linx-info.com>
> ---
>  arch/loongarch/kvm/vcpu.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
> index 3a8779065f73..d41cacf39583 100644
> --- a/arch/loongarch/kvm/vcpu.c
> +++ b/arch/loongarch/kvm/vcpu.c
> @@ -884,6 +884,9 @@ long kvm_arch_vcpu_async_ioctl(struct file *filp,
>  
>  int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
>  {
> +	if (id >= kvm->max_vcpus)
> +		return -EINVAL;

Do you have a testcase for this?  If I'm following the LoongArch code correctly,
I don't think this is actually necessary.

In arch/loongarch/include/asm/kvm_host.h:

  #define KVM_MAX_VCPUS			256

without a #define KVM_MAX_VCPU_IDS in loongarch/, AFAICT.  And so the common
code in include/linux/kvm_host.h will do:

  #ifndef KVM_MAX_VCPU_IDS
  #define KVM_MAX_VCPU_IDS KVM_MAX_VCPUS
  #endif

LoongArch's kvm_vm_ioctl_check_extension() reports that to userspace:

	case KVM_CAP_MAX_VCPU_ID:
		r = KVM_MAX_VCPU_IDS;

and the common kvm_vm_ioctl_create_vcpu() does:

	if (id >= KVM_MAX_VCPU_IDS)
		return -EINVAL;

and the common kvm_create_vm() does:

	kvm->max_vcpus = KVM_MAX_VCPUS;

with again no override of max_vcpus in LoongArch or common KVM.  So unless I'm
missing something, manually checking max_vcpus in LoongArch's kvm_arch_vcpu_precreate()
is unnecessary.
maobibo April 15, 2024, 2:04 a.m. UTC | #3
On 2024/4/13 上午2:41, Sean Christopherson wrote:
> On Fri, Apr 12, 2024, Wujie Duan wrote:
>> Add a pre-allocation arch condition to checks that vcpu id should
>> smaller than max_vcpus
>>
>> Signed-off-by: Wujie Duan <wjduan@linx-info.com>
>> ---
>>   arch/loongarch/kvm/vcpu.c | 3 +++
>>   1 file changed, 3 insertions(+)
>>
>> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
>> index 3a8779065f73..d41cacf39583 100644
>> --- a/arch/loongarch/kvm/vcpu.c
>> +++ b/arch/loongarch/kvm/vcpu.c
>> @@ -884,6 +884,9 @@ long kvm_arch_vcpu_async_ioctl(struct file *filp,
>>   
>>   int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
>>   {
>> +	if (id >= kvm->max_vcpus)
>> +		return -EINVAL;
> 
> Do you have a testcase for this?  If I'm following the LoongArch code correctly,
> I don't think this is actually necessary.
> 
> In arch/loongarch/include/asm/kvm_host.h:
> 
>    #define KVM_MAX_VCPUS			256
> 
> without a #define KVM_MAX_VCPU_IDS in loongarch/, AFAICT.  And so the common
> code in include/linux/kvm_host.h will do:
> 
>    #ifndef KVM_MAX_VCPU_IDS
>    #define KVM_MAX_VCPU_IDS KVM_MAX_VCPUS
>    #endif
> 
> LoongArch's kvm_vm_ioctl_check_extension() reports that to userspace:
> 
> 	case KVM_CAP_MAX_VCPU_ID:
> 		r = KVM_MAX_VCPU_IDS;
> 
> and the common kvm_vm_ioctl_create_vcpu() does:
> 
> 	if (id >= KVM_MAX_VCPU_IDS)
> 		return -EINVAL;
> 
> and the common kvm_create_vm() does:
> 
> 	kvm->max_vcpus = KVM_MAX_VCPUS;
> 
> with again no override of max_vcpus in LoongArch or common KVM.  So unless I'm
> missing something, manually checking max_vcpus in LoongArch's kvm_arch_vcpu_precreate()
> is unnecessary.
> 
yes, you are right. There is already such checking in function 
kvm_vm_ioctl_create_vcpu(), and it is unnecessary to add the same 
checking in function kvm_arch_vcpu_precreate().

And thanks for pointing it out.

Regards
Bibo Mao
diff mbox series

Patch

diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c
index 3a8779065f73..d41cacf39583 100644
--- a/arch/loongarch/kvm/vcpu.c
+++ b/arch/loongarch/kvm/vcpu.c
@@ -884,6 +884,9 @@  long kvm_arch_vcpu_async_ioctl(struct file *filp,
 
 int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id)
 {
+	if (id >= kvm->max_vcpus)
+		return -EINVAL;
+
 	return 0;
 }