Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp1277348lqh; Mon, 6 May 2024 02:41:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVpcWOVbKeA2aXpRVuUcmRWb5EUMzGyBBinW3D+G5JCFnBljPddMP8OHx1F+Hm+aqc/bHfNO8orORiU3csWtqHw7d7WcUtn9602uYBssQ== X-Google-Smtp-Source: AGHT+IExAHxGWT9sQgyz8melf5vWHaVnrKdiUzlnfSMABQcmEMcn4PQpUcslaP2eeSxaXxIsuJ5G X-Received: by 2002:a19:8c58:0:b0:516:d0c2:3ff9 with SMTP id i24-20020a198c58000000b00516d0c23ff9mr7512018lfj.67.1714988503017; Mon, 06 May 2024 02:41:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714988502; cv=pass; d=google.com; s=arc-20160816; b=gFXlDCOgqktnWdXgmcgN/HZzTebfBtDgLxh6DzHxK7+Fw/Z2IPWrwly/vlz0NH8j/U yDSa0jBJ8fgUnJgTpu7n9/oh6jIS/C71wxBsWZYSwedbEgmuBpndjnxD3WE/Jd1FWLye ImrENihLjlHRwxaIVwBb5jqWGnXrw2Fy8QlduWNHqJpmAYAm9ZDhgC2lRX8BQizGBCfm SnabbVU9Tb8UWev0l16xlCgcQirZgSpbpXHwH5zUQ/HFjfxcaPyTjY02ygIAitg391TS ygonmW2zx4HdI2U96HUd18WxE6yIiGME6dG0QmWr/SXRwucqdJdhXZwSq/JpmjW8dNjg xHBg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=S8IAZYSOffqKqcYsLqXTVYxW3Obl+hVbijNIsfJPT3w=; fh=ngsXDZZqiY12meHmFV+Y0X26XW3s8gXI3tB0/4qvV90=; b=0JHs569pcjTI7q/k4IEKaM5i1OVFjO6f6LDZ5tw/UG8GfAdVKGGMV5qx0a4OnENuFP TqiV+UF/iefbgIt08YJXKtFlSEXgq/TWDiv4mnE3tS8kTBR4PPO29IMW807N4liC/3y7 8SRh4sBa4L8Z36pAR97JGlNZYXaNaAOy6DrNKZqLNq2jMy3Zou4mEmT9ItLJnjX0I/Oz 62UoF+oXS3E934IVx4UIG0ckFx8ESQwuzABb9u2OeYngFZ7nVRX/qfkjsXdgeiECZFI+ KtApVH0geIffwIOJ9vwHSzZVs9ewbCcEM+vl/UaK2uZoGmEb+i4qDRdQk1eJcAyQ9SBR NjCA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rNRA7H3D; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-169594-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169594-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id d5-20020a1709061f4500b00a598d187c55si3664323ejk.447.2024.05.06.02.41.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 02:41:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-169594-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rNRA7H3D; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-169594-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169594-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 8BC3E1F2339D for ; Mon, 6 May 2024 09:41:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B4612142E61; Mon, 6 May 2024 09:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="rNRA7H3D" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 11DBB1428F0; Mon, 6 May 2024 09:40:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714988447; cv=none; b=DZ3SSlgYo67H/vcu3F793EQceg3OdJD24C3FLr4QWcXZOc2Z9nYPIB4xuSeFKQf8ER9N9zG1xF5b2nanPmRnHyuSysyOtWlx6v3ikRu1U1ZzX06WSVqt8a3fYJIZokbdH+S9zXrv8DfWVzANBQfzi8TtfiLbFg20iDXFJ6YDyhM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714988447; c=relaxed/simple; bh=OgQ9MowgHFwqdbCqnIGHFJQ9fmqnsqKgRezIERIf+aQ=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=FsBIqqOK7NkUVLsvu16f2tWyLeWV3LdODqeZ6l84MRX6DdradnRrx9kWmDSnC9GvOaaWQF2+xGFvP+eaQ7kc2wTvnjtaA1Sfkh3ABJooS2jTp2Y//OO7L6TbtxITLLWBmOiIH8TT3DwCIEErW334z6V4PiA46/qQj5YxZ053Y9A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rNRA7H3D; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0205C4AF66; Mon, 6 May 2024 09:40:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1714988446; bh=OgQ9MowgHFwqdbCqnIGHFJQ9fmqnsqKgRezIERIf+aQ=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=rNRA7H3Dj7AItY2YOD5xeEisKQKO+PzQaEb3N1u7ZbR07Bzn5QwDj40bkxYrGDEyv JQ1uUxJbUINdHAr2QxeMKhX7XKjXfFLvxSsutNzbToL2uuv5LeUK8IutPN2+XplJvQ L+LGEyTco+kQ1R4+Xvqa7emRGAHa/bP5XWUAWMzTyY4HAUDYYReP+8o2shKXCcZcGx sa7cZEnCUAzRiBfklR9J5Qmx2AZ6T2aBtVdOyQKpS5Ptw7rd5rp/fOP+0A2OjdwCJA R7v0s9BkuMln/RuFhkRhbI5o4AQONaUnNoTiecAuVLMw6B3beS9Lwt9Ua+WryMmN5K dm4cUDE2qSC0g== Received: by mail-lj1-f174.google.com with SMTP id 38308e7fff4ca-2dd6a7ae2dcso28248991fa.1; Mon, 06 May 2024 02:40:46 -0700 (PDT) X-Forwarded-Encrypted: i=1; AJvYcCXzjnXYj/tlKEEUY2Z1jffFFIq4vLSfJkY3PXMpRO8nFvbPOYewuFj5ZmiI9FYTpbe6k5WQlsggQmmXmF00MXnNqERu+gKX/8kNLsR2lVSgt2oUCmzN2ZfHwhBeHd5YE9G3 X-Gm-Message-State: AOJu0Yyl+mZaJMsVksRpAQKG2nahxcjBv7UDB3EZwiJVoCgn4wAQ2z/q 5HghsstJrp31YwVc6/ZBKWcJrpKv4wgU9tF66Uo2mt3M87NEXQ8nPcnwN8VhuWvg5FXOSJV8BmO aDRlp6haM54IpZk/a4/+01Dg4Y78= X-Received: by 2002:a2e:984c:0:b0:2e1:d94a:771d with SMTP id e12-20020a2e984c000000b002e1d94a771dmr8860373ljj.4.1714988444858; Mon, 06 May 2024 02:40:44 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240428100518.1642324-1-maobibo@loongson.cn> <20240428100518.1642324-5-maobibo@loongson.cn> <7335dcde-1b3a-1260-ac62-d2d9fcbd6a78@loongson.cn> <540aa8dd-eada-1f77-0a20-38196fb5472a@loongson.cn> In-Reply-To: <540aa8dd-eada-1f77-0a20-38196fb5472a@loongson.cn> From: Huacai Chen Date: Mon, 6 May 2024 17:40:35 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v8 4/6] LoongArch: KVM: Add vcpu search support from physical cpuid To: maobibo Cc: Tianrui Zhao , Juergen Gross , Paolo Bonzini , Jonathan Corbet , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, kvm@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, May 6, 2024 at 5:35=E2=80=AFPM maobibo wrote: > > > > On 2024/5/6 =E4=B8=8B=E5=8D=884:59, Huacai Chen wrote: > > On Mon, May 6, 2024 at 4:18=E2=80=AFPM maobibo wr= ote: > >> > >> > >> > >> On 2024/5/6 =E4=B8=8B=E5=8D=883:06, Huacai Chen wrote: > >>> Hi, Bibo, > >>> > >>> On Mon, May 6, 2024 at 2:36=E2=80=AFPM maobibo = wrote: > >>>> > >>>> > >>>> > >>>> On 2024/5/6 =E4=B8=8A=E5=8D=889:49, Huacai Chen wrote: > >>>>> Hi, Bibo, > >>>>> > >>>>> On Sun, Apr 28, 2024 at 6:05=E2=80=AFPM Bibo Mao wrote: > >>>>>> > >>>>>> Physical cpuid is used for interrupt routing for irqchips such as > >>>>>> ipi/msi/extioi interrupt controller. And physical cpuid is stored > >>>>>> at CSR register LOONGARCH_CSR_CPUID, it can not be changed once vc= pu > >>>>>> is created and physical cpuid of two vcpus cannot be the same. > >>>>>> > >>>>>> Different irqchips have different size declaration about physical = cpuid, > >>>>>> max cpuid value for CSR LOONGARCH_CSR_CPUID on 3A5000 is 512, max = cpuid > >>>>>> supported by IPI hardware is 1024, 256 for extioi irqchip, and 655= 36 > >>>>>> for MSI irqchip. > >>>>>> > >>>>>> The smallest value from all interrupt controllers is selected now, > >>>>>> and the max cpuid size is defines as 256 by KVM which comes from > >>>>>> extioi irqchip. > >>>>>> > >>>>>> Signed-off-by: Bibo Mao > >>>>>> --- > >>>>>> arch/loongarch/include/asm/kvm_host.h | 26 ++++++++ > >>>>>> arch/loongarch/include/asm/kvm_vcpu.h | 1 + > >>>>>> arch/loongarch/kvm/vcpu.c | 93 +++++++++++++++++++= +++++++- > >>>>>> arch/loongarch/kvm/vm.c | 11 ++++ > >>>>>> 4 files changed, 130 insertions(+), 1 deletion(-) > >>>>>> > >>>>>> diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarc= h/include/asm/kvm_host.h > >>>>>> index 2d62f7b0d377..3ba16ef1fe69 100644 > >>>>>> --- a/arch/loongarch/include/asm/kvm_host.h > >>>>>> +++ b/arch/loongarch/include/asm/kvm_host.h > >>>>>> @@ -64,6 +64,30 @@ struct kvm_world_switch { > >>>>>> > >>>>>> #define MAX_PGTABLE_LEVELS 4 > >>>>>> > >>>>>> +/* > >>>>>> + * Physical cpu id is used for interrupt routing, there are diffe= rent > >>>>>> + * definitions about physical cpuid on different hardwares. > >>>>>> + * For LOONGARCH_CSR_CPUID register, max cpuid size if 512 > >>>>>> + * For IPI HW, max dest CPUID size 1024 > >>>>>> + * For extioi interrupt controller, max dest CPUID size is 256 > >>>>>> + * For MSI interrupt controller, max supported CPUID size is 655= 36 > >>>>>> + * > >>>>>> + * Currently max CPUID is defined as 256 for KVM hypervisor, in f= uture > >>>>>> + * it will be expanded to 4096, including 16 packages at most. An= d every > >>>>>> + * package supports at most 256 vcpus > >>>>>> + */ > >>>>>> +#define KVM_MAX_PHYID 256 > >>>>>> + > >>>>>> +struct kvm_phyid_info { > >>>>>> + struct kvm_vcpu *vcpu; > >>>>>> + bool enabled; > >>>>>> +}; > >>>>>> + > >>>>>> +struct kvm_phyid_map { > >>>>>> + int max_phyid; > >>>>>> + struct kvm_phyid_info phys_map[KVM_MAX_PHYID]; > >>>>>> +}; > >>>>>> + > >>>>>> struct kvm_arch { > >>>>>> /* Guest physical mm */ > >>>>>> kvm_pte_t *pgd; > >>>>>> @@ -71,6 +95,8 @@ struct kvm_arch { > >>>>>> unsigned long invalid_ptes[MAX_PGTABLE_LEVELS]; > >>>>>> unsigned int pte_shifts[MAX_PGTABLE_LEVELS]; > >>>>>> unsigned int root_level; > >>>>>> + spinlock_t phyid_map_lock; > >>>>>> + struct kvm_phyid_map *phyid_map; > >>>>>> > >>>>>> s64 time_offset; > >>>>>> struct kvm_context __percpu *vmcs; > >>>>>> diff --git a/arch/loongarch/include/asm/kvm_vcpu.h b/arch/loongarc= h/include/asm/kvm_vcpu.h > >>>>>> index 0cb4fdb8a9b5..9f53950959da 100644 > >>>>>> --- a/arch/loongarch/include/asm/kvm_vcpu.h > >>>>>> +++ b/arch/loongarch/include/asm/kvm_vcpu.h > >>>>>> @@ -81,6 +81,7 @@ void kvm_save_timer(struct kvm_vcpu *vcpu); > >>>>>> void kvm_restore_timer(struct kvm_vcpu *vcpu); > >>>>>> > >>>>>> int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, struct kvm= _interrupt *irq); > >>>>>> +struct kvm_vcpu *kvm_get_vcpu_by_cpuid(struct kvm *kvm, int cpuid= ); > >>>>>> > >>>>>> /* > >>>>>> * Loongarch KVM guest interrupt handling > >>>>>> diff --git a/arch/loongarch/kvm/vcpu.c b/arch/loongarch/kvm/vcpu.c > >>>>>> index 3a8779065f73..b633fd28b8db 100644 > >>>>>> --- a/arch/loongarch/kvm/vcpu.c > >>>>>> +++ b/arch/loongarch/kvm/vcpu.c > >>>>>> @@ -274,6 +274,95 @@ static int _kvm_getcsr(struct kvm_vcpu *vcpu,= unsigned int id, u64 *val) > >>>>>> return 0; > >>>>>> } > >>>>>> > >>>>>> +static inline int kvm_set_cpuid(struct kvm_vcpu *vcpu, u64 val) > >>>>>> +{ > >>>>>> + int cpuid; > >>>>>> + struct loongarch_csrs *csr =3D vcpu->arch.csr; > >>>>>> + struct kvm_phyid_map *map; > >>>>>> + > >>>>>> + if (val >=3D KVM_MAX_PHYID) > >>>>>> + return -EINVAL; > >>>>>> + > >>>>>> + cpuid =3D kvm_read_sw_gcsr(csr, LOONGARCH_CSR_ESTAT); > >>>>>> + map =3D vcpu->kvm->arch.phyid_map; > >>>>>> + spin_lock(&vcpu->kvm->arch.phyid_map_lock); > >>>>>> + if (map->phys_map[cpuid].enabled) { > >>>>>> + /* > >>>>>> + * Cpuid is already set before > >>>>>> + * Forbid changing different cpuid at runtime > >>>>>> + */ > >>>>>> + if (cpuid !=3D val) { > >>>>>> + /* > >>>>>> + * Cpuid 0 is initial value for vcpu, mayb= e invalid > >>>>>> + * unset value for vcpu > >>>>>> + */ > >>>>>> + if (cpuid) { > >>>>>> + spin_unlock(&vcpu->kvm->arch.phyid= _map_lock); > >>>>>> + return -EINVAL; > >>>>>> + } > >>>>>> + } else { > >>>>>> + /* Discard duplicated cpuid set */ > >>>>>> + spin_unlock(&vcpu->kvm->arch.phyid_map_loc= k); > >>>>>> + return 0; > >>>>>> + } > >>>>>> + } > >>>>> I have changed the logic and comments when I apply, you can double > >>>>> check whether it is correct. > >>>> I checkout the latest version, the modification in function > >>>> kvm_set_cpuid() is good for me. > >>> Now the modified version is like this: > >>> > >>> + if (map->phys_map[cpuid].enabled) { > >>> + /* Discard duplicated CPUID set operation */ > >>> + if (cpuid =3D=3D val) { > >>> + spin_unlock(&vcpu->kvm->arch.phyid_map_lock); > >>> + return 0; > >>> + } > >>> + > >>> + /* > >>> + * CPUID is already set before > >>> + * Forbid changing different CPUID at runtime > >>> + * But CPUID 0 is the initial value for vcpu, so allow > >>> + * changing from 0 to others > >>> + */ > >>> + if (cpuid) { > >>> + spin_unlock(&vcpu->kvm->arch.phyid_map_lock); > >>> + return -EINVAL; > >>> + } > >>> + } > >>> But I still doubt whether we should allow changing from 0 to others > >>> while map->phys_map[cpuid].enabled is 1. > >> It is necessary since the default sw cpuid is zero :-( And we can > >> optimize it in later, such as set INVALID cpuid in function > >> kvm_arch_vcpu_create() and logic will be simple in function kvm_set_cp= uid(). > > In my opinion, if a vcpu with a uninitialized default physid=3D0, then > > map->phys_map[cpuid].enabled should be 0, then code won't come here. > > And if a vcpu with a real physid=3D0, then map->phys_map[cpuid].enabled > > is 1, but we shouldn't allow it to change physid in this case. > yes, that is actually a problem. > > vcpu0 firstly set physid=3D0, and vcpu0 set physid=3D1 again is not allow= ed. > vcpu0 firstly set physid=3D0, and vcpu1 set physid=3D1 is allowed. So can we simply drop the if (cpuid) checking? That means: + if (map->phys_map[cpuid].enabled) { + /* Discard duplicated CPUID set operation */ + if (cpuid =3D=3D val) { + spin_unlock(&vcpu->kvm->arch.phyid_map_lock); + return 0; + } + + spin_unlock(&vcpu->kvm->arch.phyid_map_lock); + return -EINVAL; + } Huacai > > > > > > Huacai > > > >> > >> Regards > >> Bibo Mao > >> > >>> > >>> Huacai > >>> > >>>>> > >>>>>> + > >>>>>> + if (map->phys_map[val].enabled) { > >>>>>> + /* > >>>>>> + * New cpuid is already set with other vcpu > >>>>>> + * Forbid sharing the same cpuid between different= vcpus > >>>>>> + */ > >>>>>> + if (map->phys_map[val].vcpu !=3D vcpu) { > >>>>>> + spin_unlock(&vcpu->kvm->arch.phyid_map_loc= k); > >>>>>> + return -EINVAL; > >>>>>> + } > >>>>>> + > >>>>>> + /* Discard duplicated cpuid set operation*/ > >>>>>> + spin_unlock(&vcpu->kvm->arch.phyid_map_lock); > >>>>>> + return 0; > >>>>>> + } > >>>>>> + > >>>>>> + kvm_write_sw_gcsr(csr, LOONGARCH_CSR_CPUID, val); > >>>>>> + map->phys_map[val].enabled =3D true; > >>>>>> + map->phys_map[val].vcpu =3D vcpu; > >>>>>> + if (map->max_phyid < val) > >>>>>> + map->max_phyid =3D val; > >>>>>> + spin_unlock(&vcpu->kvm->arch.phyid_map_lock); > >>>>>> + return 0; > >>>>>> +} > >>>>>> + > >>>>>> +struct kvm_vcpu *kvm_get_vcpu_by_cpuid(struct kvm *kvm, int cpuid= ) > >>>>>> +{ > >>>>>> + struct kvm_phyid_map *map; > >>>>>> + > >>>>>> + if (cpuid >=3D KVM_MAX_PHYID) > >>>>>> + return NULL; > >>>>>> + > >>>>>> + map =3D kvm->arch.phyid_map; > >>>>>> + if (map->phys_map[cpuid].enabled) > >>>>>> + return map->phys_map[cpuid].vcpu; > >>>>>> + > >>>>>> + return NULL; > >>>>>> +} > >>>>>> + > >>>>>> +static inline void kvm_drop_cpuid(struct kvm_vcpu *vcpu) > >>>>>> +{ > >>>>>> + int cpuid; > >>>>>> + struct loongarch_csrs *csr =3D vcpu->arch.csr; > >>>>>> + struct kvm_phyid_map *map; > >>>>>> + > >>>>>> + map =3D vcpu->kvm->arch.phyid_map; > >>>>>> + cpuid =3D kvm_read_sw_gcsr(csr, LOONGARCH_CSR_ESTAT); > >>>>>> + if (cpuid >=3D KVM_MAX_PHYID) > >>>>>> + return; > >>>>>> + > >>>>>> + if (map->phys_map[cpuid].enabled) { > >>>>>> + map->phys_map[cpuid].vcpu =3D NULL; > >>>>>> + map->phys_map[cpuid].enabled =3D false; > >>>>>> + kvm_write_sw_gcsr(csr, LOONGARCH_CSR_CPUID, 0); > >>>>>> + } > >>>>>> +} > >>>>> While kvm_set_cpuid() is protected by a spinlock, do kvm_drop_cpuid= () > >>>>> and kvm_get_vcpu_by_cpuid() also need it? > >>>>> > >>>> It is good to me that spinlock is added in function kvm_drop_cpuid()= . > >>>> And thinks for the efforts. > >>>> > >>>> Regards > >>>> Bibo Mao > >>>>>> + > >>>>>> static int _kvm_setcsr(struct kvm_vcpu *vcpu, unsigned int id,= u64 val) > >>>>>> { > >>>>>> int ret =3D 0, gintc; > >>>>>> @@ -291,7 +380,8 @@ static int _kvm_setcsr(struct kvm_vcpu *vcpu, = unsigned int id, u64 val) > >>>>>> kvm_set_sw_gcsr(csr, LOONGARCH_CSR_ESTAT, gintc= ); > >>>>>> > >>>>>> return ret; > >>>>>> - } > >>>>>> + } else if (id =3D=3D LOONGARCH_CSR_CPUID) > >>>>>> + return kvm_set_cpuid(vcpu, val); > >>>>>> > >>>>>> kvm_write_sw_gcsr(csr, id, val); > >>>>>> > >>>>>> @@ -943,6 +1033,7 @@ void kvm_arch_vcpu_destroy(struct kvm_vcpu *v= cpu) > >>>>>> hrtimer_cancel(&vcpu->arch.swtimer); > >>>>>> kvm_mmu_free_memory_cache(&vcpu->arch.mmu_page_cache); > >>>>>> kfree(vcpu->arch.csr); > >>>>>> + kvm_drop_cpuid(vcpu); > >>>>> I think this line should be before the above kfree(), otherwise you > >>>>> get a "use after free". > >>>>> > >>>>> Huacai > >>>>> > >>>>>> > >>>>>> /* > >>>>>> * If the vCPU is freed and reused as another vCPU, we = don't want the > >>>>>> diff --git a/arch/loongarch/kvm/vm.c b/arch/loongarch/kvm/vm.c > >>>>>> index 0a37f6fa8f2d..6006a28653ad 100644 > >>>>>> --- a/arch/loongarch/kvm/vm.c > >>>>>> +++ b/arch/loongarch/kvm/vm.c > >>>>>> @@ -30,6 +30,14 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned = long type) > >>>>>> if (!kvm->arch.pgd) > >>>>>> return -ENOMEM; > >>>>>> > >>>>>> + kvm->arch.phyid_map =3D kvzalloc(sizeof(struct kvm_phyid_m= ap), > >>>>>> + GFP_KERNEL_ACCOUNT); > >>>>>> + if (!kvm->arch.phyid_map) { > >>>>>> + free_page((unsigned long)kvm->arch.pgd); > >>>>>> + kvm->arch.pgd =3D NULL; > >>>>>> + return -ENOMEM; > >>>>>> + } > >>>>>> + > >>>>>> kvm_init_vmcs(kvm); > >>>>>> kvm->arch.gpa_size =3D BIT(cpu_vabits - 1); > >>>>>> kvm->arch.root_level =3D CONFIG_PGTABLE_LEVELS - 1; > >>>>>> @@ -44,6 +52,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned l= ong type) > >>>>>> for (i =3D 0; i <=3D kvm->arch.root_level; i++) > >>>>>> kvm->arch.pte_shifts[i] =3D PAGE_SHIFT + i * (P= AGE_SHIFT - 3); > >>>>>> > >>>>>> + spin_lock_init(&kvm->arch.phyid_map_lock); > >>>>>> return 0; > >>>>>> } > >>>>>> > >>>>>> @@ -51,7 +60,9 @@ void kvm_arch_destroy_vm(struct kvm *kvm) > >>>>>> { > >>>>>> kvm_destroy_vcpus(kvm); > >>>>>> free_page((unsigned long)kvm->arch.pgd); > >>>>>> + kvfree(kvm->arch.phyid_map); > >>>>>> kvm->arch.pgd =3D NULL; > >>>>>> + kvm->arch.phyid_map =3D NULL; > >>>>>> } > >>>>>> > >>>>>> int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) > >>>>>> -- > >>>>>> 2.39.3 > >>>>>> > >>>> > >> >