PrtGeSXzjJz72py_f_yz1FAePQ7gCDFClAnUrU47PM54-6qB0 Message-ID: Subject: Re: [PATCH 1/2] LoongArch: KVM: Compile the switch.S file directly into the kernel To: lixianglai Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, stable@vger.kernel.org, WANG Xuerui , Tianrui Zhao , Bibo Mao , Charlie Jenkins , Thomas Gleixner Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-DKIM: signer='kernel.org' status='pass' reason='' DKIMCheck: Server passes DKIM test, 0 Spam score X-Spam-Score: -1.2 (-) X-Spam-Report: Spam detection software, running on the system "witcher.mxrouting.net", has performed the tests listed below against this email. Information: https://mxroutedocs.com/directadmin/spamfilters/ --- Content analysis details: (-1.2 points) --- pts rule name description ---- ---------------------- ----------------------------------------- 0.0 RCVD_IN_DNSWL_BLOCKED RBL: ADMINISTRATOR NOTICE: The query to DNSWL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#DnsBlocklists-dnsbl-block for more information. [172.234.253.10 listed in list.dnswl.org] -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -1.0 MAILING_LIST_MULTI Multiple indicators imply a widely-seen list manager -0.0 DKIMWL_WL_HIGH DKIMwl.org - High trust sender SpamTally: Final spam score: -11 On Thu, Dec 18, 2025 at 7:08=E2=80=AFPM lixianglai = wrote: > > Hi Huacai Chen: > > Hi, Xianglai, > > > > On Wed, Dec 17, 2025 at 11:49=E2=80=AFAM Xianglai Li wrote: > >> If we directly compile the switch.S file into the kernel, the address = of > >> the kvm_exc_entry function will definitely be within the DMW memory ar= ea. > >> Therefore, we will no longer need to perform a copy relocation of > >> kvm_exc_entry. > >> > >> Based on the above description, compile switch.S directly into the ker= nel, > >> and then remove the copy relocation execution logic for the kvm_exc_en= try > >> function. > >> > >> Cc: stable@vger.kernel.org > >> Signed-off-by: Xianglai Li > >> --- > >> Cc: Huacai Chen > >> Cc: WANG Xuerui > >> Cc: Tianrui Zhao > >> Cc: Bibo Mao > >> Cc: Charlie Jenkins > >> Cc: Xianglai Li > >> Cc: Thomas Gleixner > >> > >> arch/loongarch/Kbuild | 2 +- > >> arch/loongarch/include/asm/asm-prototypes.h | 16 ++++++++++ > >> arch/loongarch/include/asm/kvm_host.h | 5 +-- > >> arch/loongarch/include/asm/kvm_vcpu.h | 20 ++++++------ > >> arch/loongarch/kvm/Makefile | 2 +- > >> arch/loongarch/kvm/main.c | 35 ++-----------------= -- > >> arch/loongarch/kvm/switch.S | 22 ++++++++++--- > >> 7 files changed, 49 insertions(+), 53 deletions(-) > >> > >> diff --git a/arch/loongarch/Kbuild b/arch/loongarch/Kbuild > >> index beb8499dd8ed..1c7a0dbe5e72 100644 > >> --- a/arch/loongarch/Kbuild > >> +++ b/arch/loongarch/Kbuild > >> @@ -3,7 +3,7 @@ obj-y +=3D mm/ > >> obj-y +=3D net/ > >> obj-y +=3D vdso/ > >> > >> -obj-$(CONFIG_KVM) +=3D kvm/ > >> +obj-$(subst m,y,$(CONFIG_KVM)) +=3D kvm/ > >> > >> # for cleaning > >> subdir- +=3D boot > >> diff --git a/arch/loongarch/include/asm/asm-prototypes.h b/arch/loonga= rch/include/asm/asm-prototypes.h > >> index 704066b4f736..eb591276d191 100644 > >> --- a/arch/loongarch/include/asm/asm-prototypes.h > >> +++ b/arch/loongarch/include/asm/asm-prototypes.h > >> @@ -20,3 +20,19 @@ asmlinkage void noinstr __no_stack_protector ret_fr= om_kernel_thread(struct task_ > >> s= truct pt_regs *regs, > >> i= nt (*fn)(void *), > >> v= oid *fn_arg); > >> + > >> +void kvm_exc_entry(void); > >> +int kvm_enter_guest(void *run, void *vcpu); > >> + > >> +#ifdef CONFIG_CPU_HAS_LSX > >> +void kvm_save_lsx(void *fpu); > >> +void kvm_restore_lsx(void *fpu); > >> +#endif > >> + > >> +#ifdef CONFIG_CPU_HAS_LASX > >> +void kvm_save_lasx(void *fpu); > >> +void kvm_restore_lasx(void *fpu); > >> +#endif > >> + > >> +void kvm_save_fpu(void *fpu); > >> +void kvm_restore_fpu(void *fpu); > >> diff --git a/arch/loongarch/include/asm/kvm_host.h b/arch/loongarch/in= clude/asm/kvm_host.h > >> index e4fe5b8e8149..0aa7679536cc 100644 > >> --- a/arch/loongarch/include/asm/kvm_host.h > >> +++ b/arch/loongarch/include/asm/kvm_host.h > >> @@ -85,7 +85,6 @@ struct kvm_context { > >> struct kvm_world_switch { > >> int (*exc_entry)(void); > >> int (*enter_guest)(struct kvm_run *run, struct kvm_vcpu *vcpu= ); > >> - unsigned long page_order; > >> }; > >> > >> #define MAX_PGTABLE_LEVELS 4 > >> @@ -344,11 +343,9 @@ enum hrtimer_restart kvm_swtimer_wakeup(struct hr= timer *timer); > >> void kvm_arch_flush_remote_tlbs_memslot(struct kvm *kvm, const struc= t kvm_memory_slot *memslot); > >> void kvm_init_vmcs(struct kvm *kvm); > >> void kvm_exc_entry(void); > >> -int kvm_enter_guest(struct kvm_run *run, struct kvm_vcpu *vcpu); > >> +int kvm_enter_guest(void *run, void *vcpu); > >> > >> extern unsigned long vpid_mask; > >> -extern const unsigned long kvm_exception_size; > >> -extern const unsigned long kvm_enter_guest_size; > >> extern struct kvm_world_switch *kvm_loongarch_ops; > >> > >> #define SW_GCSR (1 << 0) > >> diff --git a/arch/loongarch/include/asm/kvm_vcpu.h b/arch/loongarch/in= clude/asm/kvm_vcpu.h > >> index 3784ab4ccdb5..8af98a3d7b0c 100644 > >> --- a/arch/loongarch/include/asm/kvm_vcpu.h > >> +++ b/arch/loongarch/include/asm/kvm_vcpu.h > >> @@ -53,28 +53,28 @@ void kvm_deliver_exception(struct kvm_vcpu *vcpu); > >> > >> void kvm_own_fpu(struct kvm_vcpu *vcpu); > >> void kvm_lose_fpu(struct kvm_vcpu *vcpu); > >> -void kvm_save_fpu(struct loongarch_fpu *fpu); > >> -void kvm_restore_fpu(struct loongarch_fpu *fpu); > >> +void kvm_save_fpu(void *fpu); > >> +void kvm_restore_fpu(void *fpu); > > Why are these modifications needed? > In the assembly file switch.S, we used the macro definition > EXPORT_SYMBOL to export symbols without version information, > which led to an alarm during the compilation stage. In order to solve > this problem we need to put the symbol statement > in the file "arch/loongarch/include/asm/asm-prototypes.h", And function > declarations in the parameter types defined > in the header file "arch/loongarch/include/asm/kvm_host h", it is very > big, in order to reduce the > "arch/loongarch/include/asm/asm-prototypes.h" the contents of the file, > So we change the parameters in the function > declaration, then the function declaration directly into the file > "arch/loongarch/include/asm/asm-prototypes.h". I know what you want, but you needn't do this. You can simply add a line "struct loongarch_fpu;" in asm-prototypes.h before use. Huacai > > > > > Huacai > > > >> void kvm_restore_fcsr(struct loongarch_fpu *fpu); > >> > >> #ifdef CONFIG_CPU_HAS_LSX > >> int kvm_own_lsx(struct kvm_vcpu *vcpu); > >> -void kvm_save_lsx(struct loongarch_fpu *fpu); > >> -void kvm_restore_lsx(struct loongarch_fpu *fpu); > >> +void kvm_save_lsx(void *fpu); > >> +void kvm_restore_lsx(void *fpu); > >> #else > >> static inline int kvm_own_lsx(struct kvm_vcpu *vcpu) { return -EINVA= L; } > >> -static inline void kvm_save_lsx(struct loongarch_fpu *fpu) { } > >> -static inline void kvm_restore_lsx(struct loongarch_fpu *fpu) { } > >> +static inline void kvm_save_lsx(void *fpu) { } > >> +static inline void kvm_restore_lsx(void *fpu) { } > >> #endif > >> > >> #ifdef CONFIG_CPU_HAS_LASX > >> int kvm_own_lasx(struct kvm_vcpu *vcpu); > >> -void kvm_save_lasx(struct loongarch_fpu *fpu); > >> -void kvm_restore_lasx(struct loongarch_fpu *fpu); > >> +void kvm_save_lasx(void *fpu); > >> +void kvm_restore_lasx(void *fpu); > >> #else > >> static inline int kvm_own_lasx(struct kvm_vcpu *vcpu) { return -EINV= AL; } > >> -static inline void kvm_save_lasx(struct loongarch_fpu *fpu) { } > >> -static inline void kvm_restore_lasx(struct loongarch_fpu *fpu) { } > >> +static inline void kvm_save_lasx(void *fpu) { } > >> +static inline void kvm_restore_lasx(void *fpu) { } > >> #endif > >> > >> #ifdef CONFIG_CPU_HAS_LBT > >> diff --git a/arch/loongarch/kvm/Makefile b/arch/loongarch/kvm/Makefile > >> index cb41d9265662..fe665054f824 100644 > >> --- a/arch/loongarch/kvm/Makefile > >> +++ b/arch/loongarch/kvm/Makefile > >> @@ -11,7 +11,7 @@ kvm-y +=3D exit.o > >> kvm-y +=3D interrupt.o > >> kvm-y +=3D main.o > >> kvm-y +=3D mmu.o > >> -kvm-y +=3D switch.o > >> +obj-y +=3D switch.o > >> kvm-y +=3D timer.o > >> kvm-y +=3D tlb.o > >> kvm-y +=3D vcpu.o > >> diff --git a/arch/loongarch/kvm/main.c b/arch/loongarch/kvm/main.c > >> index 80ea63d465b8..67d234540ed4 100644 > >> --- a/arch/loongarch/kvm/main.c > >> +++ b/arch/loongarch/kvm/main.c > >> @@ -340,8 +340,7 @@ void kvm_arch_disable_virtualization_cpu(void) > >> > >> static int kvm_loongarch_env_init(void) > >> { > >> - int cpu, order, ret; > >> - void *addr; > >> + int cpu, ret; > >> struct kvm_context *context; > >> > >> vmcs =3D alloc_percpu(struct kvm_context); > >> @@ -357,30 +356,8 @@ static int kvm_loongarch_env_init(void) > >> return -ENOMEM; > >> } > >> > >> - /* > >> - * PGD register is shared between root kernel and kvm hypervis= or. > >> - * So world switch entry should be in DMW area rather than TLB= area > >> - * to avoid page fault reenter. > >> - * > >> - * In future if hardware pagetable walking is supported, we wo= n't > >> - * need to copy world switch code to DMW area. > >> - */ > >> - order =3D get_order(kvm_exception_size + kvm_enter_guest_size)= ; > >> - addr =3D (void *)__get_free_pages(GFP_KERNEL, order); > >> - if (!addr) { > >> - free_percpu(vmcs); > >> - vmcs =3D NULL; > >> - kfree(kvm_loongarch_ops); > >> - kvm_loongarch_ops =3D NULL; > >> - return -ENOMEM; > >> - } > >> - > >> - memcpy(addr, kvm_exc_entry, kvm_exception_size); > >> - memcpy(addr + kvm_exception_size, kvm_enter_guest, kvm_enter_g= uest_size); > >> - flush_icache_range((unsigned long)addr, (unsigned long)addr + = kvm_exception_size + kvm_enter_guest_size); > >> - kvm_loongarch_ops->exc_entry =3D addr; > >> - kvm_loongarch_ops->enter_guest =3D addr + kvm_exception_size; > >> - kvm_loongarch_ops->page_order =3D order; > >> + kvm_loongarch_ops->exc_entry =3D (void *)kvm_exc_entry; > >> + kvm_loongarch_ops->enter_guest =3D (void *)kvm_enter_guest; > >> > >> vpid_mask =3D read_csr_gstat(); > >> vpid_mask =3D (vpid_mask & CSR_GSTAT_GIDBIT) >> CSR_GSTAT_GID= BIT_SHIFT; > >> @@ -414,16 +391,10 @@ static int kvm_loongarch_env_init(void) > >> > >> static void kvm_loongarch_env_exit(void) > >> { > >> - unsigned long addr; > >> - > >> if (vmcs) > >> free_percpu(vmcs); > >> > >> if (kvm_loongarch_ops) { > >> - if (kvm_loongarch_ops->exc_entry) { > >> - addr =3D (unsigned long)kvm_loongarch_ops->exc= _entry; > >> - free_pages(addr, kvm_loongarch_ops->page_order= ); > >> - } > >> kfree(kvm_loongarch_ops); > >> } > >> > >> diff --git a/arch/loongarch/kvm/switch.S b/arch/loongarch/kvm/switch.S > >> index f1768b7a6194..93845ce53651 100644 > >> --- a/arch/loongarch/kvm/switch.S > >> +++ b/arch/loongarch/kvm/switch.S > >> @@ -5,6 +5,7 @@ > >> > >> #include > >> #include > >> +#include > >> #include > >> #include > >> #include > >> @@ -100,10 +101,18 @@ > >> * - is still in guest mode, such as pgd table/vmid r= egisters etc, > >> * - will fix with hw page walk enabled in future > >> * load kvm_vcpu from reserved CSR KVM_VCPU_KS, and save a2 t= o KVM_TEMP_KS > >> + * > >> + * PGD register is shared between root kernel and kvm hypervis= or. > >> + * So world switch entry should be in DMW area rather than TLB= area > >> + * to avoid page fault reenter. > >> + * > >> + * In future if hardware pagetable walking is supported, we wo= n't > >> + * need to copy world switch code to DMW area. > >> */ > >> .text > >> .cfi_sections .debug_frame > >> SYM_CODE_START(kvm_exc_entry) > >> + .p2align PAGE_SHIFT > >> UNWIND_HINT_UNDEFINED > >> csrwr a2, KVM_TEMP_KS > >> csrrd a2, KVM_VCPU_KS > >> @@ -190,8 +199,8 @@ ret_to_host: > >> kvm_restore_host_gpr a2 > >> jr ra > >> > >> -SYM_INNER_LABEL(kvm_exc_entry_end, SYM_L_LOCAL) > >> SYM_CODE_END(kvm_exc_entry) > >> +EXPORT_SYMBOL(kvm_exc_entry) > >> > >> /* > >> * int kvm_enter_guest(struct kvm_run *run, struct kvm_vcpu *vcpu) > >> @@ -215,8 +224,8 @@ SYM_FUNC_START(kvm_enter_guest) > >> /* Save kvm_vcpu to kscratch */ > >> csrwr a1, KVM_VCPU_KS > >> kvm_switch_to_guest > >> -SYM_INNER_LABEL(kvm_enter_guest_end, SYM_L_LOCAL) > >> SYM_FUNC_END(kvm_enter_guest) > >> +EXPORT_SYMBOL(kvm_enter_guest) > >> > >> SYM_FUNC_START(kvm_save_fpu) > >> fpu_save_csr a0 t1 > >> @@ -224,6 +233,7 @@ SYM_FUNC_START(kvm_save_fpu) > >> fpu_save_cc a0 t1 t2 > >> jr ra > >> SYM_FUNC_END(kvm_save_fpu) > >> +EXPORT_SYMBOL(kvm_save_fpu) > >> > >> SYM_FUNC_START(kvm_restore_fpu) > >> fpu_restore_double a0 t1 > >> @@ -231,6 +241,7 @@ SYM_FUNC_START(kvm_restore_fpu) > >> fpu_restore_cc a0 t1 t2 > >> jr ra > >> SYM_FUNC_END(kvm_restore_fpu) > >> +EXPORT_SYMBOL(kvm_restore_fpu) > >> > >> #ifdef CONFIG_CPU_HAS_LSX > >> SYM_FUNC_START(kvm_save_lsx) > >> @@ -239,6 +250,7 @@ SYM_FUNC_START(kvm_save_lsx) > >> lsx_save_data a0 t1 > >> jr ra > >> SYM_FUNC_END(kvm_save_lsx) > >> +EXPORT_SYMBOL(kvm_save_lsx) > >> > >> SYM_FUNC_START(kvm_restore_lsx) > >> lsx_restore_data a0 t1 > >> @@ -246,6 +258,7 @@ SYM_FUNC_START(kvm_restore_lsx) > >> fpu_restore_csr a0 t1 t2 > >> jr ra > >> SYM_FUNC_END(kvm_restore_lsx) > >> +EXPORT_SYMBOL(kvm_restore_lsx) > >> #endif > >> > >> #ifdef CONFIG_CPU_HAS_LASX > >> @@ -255,6 +268,7 @@ SYM_FUNC_START(kvm_save_lasx) > >> lasx_save_data a0 t1 > >> jr ra > >> SYM_FUNC_END(kvm_save_lasx) > >> +EXPORT_SYMBOL(kvm_save_lasx) > >> > >> SYM_FUNC_START(kvm_restore_lasx) > >> lasx_restore_data a0 t1 > >> @@ -262,10 +276,8 @@ SYM_FUNC_START(kvm_restore_lasx) > >> fpu_restore_csr a0 t1 t2 > >> jr ra > >> SYM_FUNC_END(kvm_restore_lasx) > >> +EXPORT_SYMBOL(kvm_restore_lasx) > >> #endif > >> - .section ".rodata" > >> -SYM_DATA(kvm_exception_size, .quad kvm_exc_entry_end - kvm_exc_entry) > >> -SYM_DATA(kvm_enter_guest_size, .quad kvm_enter_guest_end - kvm_enter_= guest) > >> > >> #ifdef CONFIG_CPU_HAS_LBT > >> STACK_FRAME_NON_STANDARD kvm_restore_fpu > >> -- > >> 2.39.1 > >> > >> > From - Fri Dec 19 13:23:49 2025 X-Mozilla-Status: 0001 X-Mozilla-Status2: 00000000 Return-Path: Delivered-To: hi@josie.lol Received: from witcher.mxrouting.net by witcher.mxrouting.net with LMTP id WK8hJ29PRWmRNRkAYBR5ng (envelope-from ) for ; Fri, 19 Dec 2025 13:13:19 +0000 Return-path: Envelope-to: hi@josie.lol Delivery-date: Fri, 19 Dec 2025 13:13:19 +0000 Received: from sea.lore.kernel.org ([172.234.253.10]) by witcher.mxrouting.net with esmtps (TLS1.3) tls TLS_AES_256_GCM_SHA384 (Exim 4.98) (envelope-from ) id 1vWaIJ-00000007NBd-17G4 for hi@josie.lol; Fri, 19 Dec 2025 13:13:19 +0000 Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by sea.lore.kernel.org (Postfix) with ESMTP id 08E5A3079AAF for ; Fri, 19 Dec 2025 13:11:56 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EE6D332D451; Fri, 19 Dec 2025 13:06:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b="As4NLXXo" X-Original-To: stable@vger.kernel.org Received: from bali.collaboradmins.com (bali.collaboradmins.com [148.251.105.195]) (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 16A0632D422 for ; Fri, 19 Dec 2025 13:06:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.251.105.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766149584; cv=none; b=eo9oyq+r/aiIBiQRomIS1MDD+t5E4LyfSFV7xMdi3UmsKgADNyMwcmqGY5mEqIr7q0WfiRTqp2hhB9Gomm9K8yQmGWu/VljM++upMmgL0P8MyLLWb3b68a6adPuR6ZoNp35dI/TDut/tjZ8blGaJgi3Dv6bW1RkijO/IGtAc4yk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766149584; c=relaxed/simple; bh=xwEXc0YZI1B/QU3CrKy/Saw1yLKl2MvE/Ri9dIAvG70=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=igclcPBClrdNtS9sdfT3+9gjgMadvR0LCAWvR2O/Gs5vM0cMFbTRATQlGZbvS5To9NV6lGLtYHDowpQJ6LPUeKRkACTANCzcjYQ6ZVhcfVBbCPCn1wbY3VnsKzm7BvR08gv1pIn4CLONMFwE7caILRhuFiOUjiOo7XANWS0rM6c= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass (2048-bit key) header.d=collabora.com header.i=@collabora.com header.b=As4NLXXo; arc=none smtp.client-ip=148.251.105.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1766149581; bh=xwEXc0YZI1B/QU3CrKy/Saw1yLKl2MvE/Ri9dIAvG70=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=As4NLXXo8GNRgelCMvEZPX+33lORBHDFOzEMNTIKL1laBiig82d9prp2gbgJH/v5l kh0cWJ1TIjAn2QwboOAdcJfTiOOAT+36ZokuajJu9s4g1MIMP9T9diTpLy+30Awr40 9J+jxMOGr8NzwundFzv42Oj5bfDH041NFBEU/j6187sr8MP0mf5NjU/BvPx29WWn1X P/0tN7i+s84bqCst3SulDpKecWITOQtyu7jpndXP3zhI9KMGWW1t15PzzYPRFEVHEl qNTh09bmJMnaV30TTd7kjk9qFvcfUj8/wkr3mzipPhf4lN7LtRCQFZDHua0D8kP16Z k6NNKUwCJ8bMQ== Received: from fedora