Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp926603rdb; Sun, 18 Feb 2024 20:12:10 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXUYIV5f13ARH/3DwbJrJkZQXzxwKasXGg1KD2bpi7YSqKJQGOPyOpnYkYNm7beIojzd6lT9kepP8QvZa2ynWRGv77GKKR+X6ELOLqIYA== X-Google-Smtp-Source: AGHT+IGduQXwYJ/XB6YSfxHNQJG+2Y9a5SiUbNJthHYyAaK6c9lmEkaezKGSGTJpVMXyP3LbiTMW X-Received: by 2002:a05:6358:d090:b0:179:272e:54e9 with SMTP id jc16-20020a056358d09000b00179272e54e9mr11219607rwb.30.1708315929900; Sun, 18 Feb 2024 20:12:09 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708315929; cv=pass; d=google.com; s=arc-20160816; b=TAZ+HU5ITaYjdXA1dmLEwOV+0jXq96808nTXQTHd0NsoBfx47bEBwJzVpxiBsT4ORV 7qC6nXw1wKidABdkh6D5GTXa/aHhTEUjuNGGDIVTYg2By6KRjPAdek7ZTdH/J3TE2hEp Nce032Rg0z4PAujXVaKqFZ6UIGwdui6Fr6t7+jAyXMLGnynIvTgxuXg9QYNZrQWn6iV3 NoKvUVFtNxgbYQeBxbQXMTlny1KGkgkoU7Rg11vz8nFD4CxsZ9pPOTYLAIlTR+rItCd2 zcAO2uOxVIoADCuwhUXLl/6SfVG9hD+wU8U6jgDjAfryhbSr7KS43MGBcyQCbs8TcDX2 PlKw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:content-language:in-reply-to:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:user-agent:date :message-id:from:references:cc:to:subject; bh=53PRSbZpu/uiFWSSov+59k3ZHA5M1mK4mcjF1XtkYjY=; fh=8LSJ6bUYzYuj4PnA4CpGavysyxl2IC4oJ2qRM3h3oRE=; b=vkPbRCGf6Nk1P/9v1VXK++Y5M0Qxd70ZwBST704TBNvjkQ2CN0jWkVEWkuo5JfFHbh unUi4nk8oIFxkqu6xxN6f4JJ0oFwCSlZ7wmrfDi7w/HjoBT6NKa/B7+78PWzS5h5qqdd sap62ZQso8oGRmpm8LnbTWd/TzjnoN43zSb+J/+ewpkSG/Dm5mzDuVnpBwxcEEkiD92J t2U3Dj7lmimFgey4Kq2vfC5juJpfrF8REpA8Z9o7p6LbHmaB/sE6FUi5Uzss/0HbvfzI VCegkP+ycWa9dawJ+5KcUueOQ0TCDxsuWZjVoo23uknnt7xwQHwZtJ0D+88TsWqZKxAe K2jw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-70698-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70698-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id e26-20020a63745a000000b005d8c1b5a3bdsi3783417pgn.216.2024.02.18.20.12.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 20:12:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70698-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-70698-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70698-linux.lists.archive=gmail.com@vger.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8C81C28134A for ; Mon, 19 Feb 2024 04:12:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 16EFFB64B; Mon, 19 Feb 2024 04:11:55 +0000 (UTC) Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4949011734; Mon, 19 Feb 2024 04:11:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708315914; cv=none; b=D8B3DOd8yu0XKdur9bGf5iPc99N7l6uzidFofEWsRPXdYQS3TRfJ0IHp4h+TT5smpTGONUIGz3J98sndKJXFpD5bVd7yAXOjcExZ8hfQVPvs0Fi7EXpR7XI07u4h3CLXbo+Pwqi3zi4SZNpQ77wetP4yi0CQKiQorsTsohADrWA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708315914; c=relaxed/simple; bh=Pe9P/xDePK2BHCp3Enr8rBxxSlCFo7+ZwOE2XI9VlGU=; h=Subject:To:Cc:References:From:Message-ID:Date:MIME-Version: In-Reply-To:Content-Type; b=J9Tba59v2OqSMLJlqLLnVVO81KevNwdgFPfOtNoxdQ3ODmPg2aRHXDoGJC6IKGsD0OkjuFk9BMbYmbMXDSBOX6Bf7M+VjgLfwlIrECIls0pcOgFelnlKet8TgkYymbRywdzYD7MvX3MvBZyRhKOBAfcQNXTPZrOVYZpXvDmJT/I= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.20.42.173]) by gateway (Coremail) with SMTP id _____8CxSPAF1dJlxTQOAA--.38845S3; Mon, 19 Feb 2024 12:11:49 +0800 (CST) Received: from [10.20.42.173] (unknown [10.20.42.173]) by localhost.localdomain (Coremail) with SMTP id AQAAf8AxTs0C1dJl08M7AA--.20072S3; Mon, 19 Feb 2024 12:11:48 +0800 (CST) Subject: Re: [PATCH v4 4/6] LoongArch: Add paravirt interface for guest kernel To: Huacai Chen Cc: Tianrui Zhao , Juergen Gross , Paolo Bonzini , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, kvm@vger.kernel.org References: <20240201031950.3225626-1-maobibo@loongson.cn> <20240201031950.3225626-5-maobibo@loongson.cn> From: maobibo Message-ID: <63f8bd29-c0da-167b-187d-61c56eb081a6@loongson.cn> Date: Mon, 19 Feb 2024 12:11:52 +0800 User-Agent: Mozilla/5.0 (X11; Linux loongarch64; rv:68.0) Gecko/20100101 Thunderbird/68.7.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8AxTs0C1dJl08M7AA--.20072S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3JF13XF45Aw47Xw47GrW5XFc_yoW3Xr43pa 4DAF4kGa18GF1xArZ3Krsxurn8J397CF129Fy2qa4FyFZFvr1UXr1vgryq9FyDJa1kJ3W0 qFyrWw1a9a15tagCm3ZEXasCq-sJn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUvFb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r106r15M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4j6r4UJwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYIkI8VC2zVCFFI0UMc 02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUtVWrXwAv7VC2z280aVAF wI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcVAKI48JMxk0xIA0c2IEe2xFo4 CEbIxvr21l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG 67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MI IYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E 14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JV WxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUcHUq UUUUU On 2024/2/19 上午10:42, Huacai Chen wrote: > Hi, Bibo, > > On Thu, Feb 1, 2024 at 11:19 AM Bibo Mao wrote: >> >> The patch adds paravirt interface for guest kernel, function >> pv_guest_initi() firstly checks whether system runs on VM mode. If kernel >> runs on VM mode, it will call function kvm_para_available() to detect >> whether current VMM is KVM hypervisor. And the paravirt function can work >> only if current VMM is KVM hypervisor, since there is only KVM hypervisor >> supported on LoongArch now. >> >> This patch only adds paravirt interface for guest kernel, however there >> is not effective pv functions added here. >> >> Signed-off-by: Bibo Mao >> --- >> arch/loongarch/Kconfig | 9 ++++ >> arch/loongarch/include/asm/kvm_para.h | 7 ++++ >> arch/loongarch/include/asm/paravirt.h | 27 ++++++++++++ >> .../include/asm/paravirt_api_clock.h | 1 + >> arch/loongarch/kernel/Makefile | 1 + >> arch/loongarch/kernel/paravirt.c | 41 +++++++++++++++++++ >> arch/loongarch/kernel/setup.c | 2 + >> 7 files changed, 88 insertions(+) >> create mode 100644 arch/loongarch/include/asm/paravirt.h >> create mode 100644 arch/loongarch/include/asm/paravirt_api_clock.h >> create mode 100644 arch/loongarch/kernel/paravirt.c >> >> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig >> index 10959e6c3583..817a56dff80f 100644 >> --- a/arch/loongarch/Kconfig >> +++ b/arch/loongarch/Kconfig >> @@ -585,6 +585,15 @@ config CPU_HAS_PREFETCH >> bool >> default y >> >> +config PARAVIRT >> + bool "Enable paravirtualization code" >> + depends on AS_HAS_LVZ_EXTENSION >> + help >> + This changes the kernel so it can modify itself when it is run >> + under a hypervisor, potentially improving performance significantly >> + over full virtualization. However, when run without a hypervisor >> + the kernel is theoretically slower and slightly larger. >> + >> config ARCH_SUPPORTS_KEXEC >> def_bool y >> >> diff --git a/arch/loongarch/include/asm/kvm_para.h b/arch/loongarch/include/asm/kvm_para.h >> index 9425d3b7e486..41200e922a82 100644 >> --- a/arch/loongarch/include/asm/kvm_para.h >> +++ b/arch/loongarch/include/asm/kvm_para.h >> @@ -2,6 +2,13 @@ >> #ifndef _ASM_LOONGARCH_KVM_PARA_H >> #define _ASM_LOONGARCH_KVM_PARA_H >> >> +/* >> + * Hypcall code field >> + */ >> +#define HYPERVISOR_KVM 1 >> +#define HYPERVISOR_VENDOR_SHIFT 8 >> +#define HYPERCALL_CODE(vendor, code) ((vendor << HYPERVISOR_VENDOR_SHIFT) + code) >> + >> /* >> * LoongArch hypcall return code >> */ >> diff --git a/arch/loongarch/include/asm/paravirt.h b/arch/loongarch/include/asm/paravirt.h >> new file mode 100644 >> index 000000000000..b64813592ba0 >> --- /dev/null >> +++ b/arch/loongarch/include/asm/paravirt.h >> @@ -0,0 +1,27 @@ >> +/* SPDX-License-Identifier: GPL-2.0 */ >> +#ifndef _ASM_LOONGARCH_PARAVIRT_H >> +#define _ASM_LOONGARCH_PARAVIRT_H >> + >> +#ifdef CONFIG_PARAVIRT >> +#include >> +struct static_key; >> +extern struct static_key paravirt_steal_enabled; >> +extern struct static_key paravirt_steal_rq_enabled; >> + >> +u64 dummy_steal_clock(int cpu); >> +DECLARE_STATIC_CALL(pv_steal_clock, dummy_steal_clock); >> + >> +static inline u64 paravirt_steal_clock(int cpu) >> +{ >> + return static_call(pv_steal_clock)(cpu); >> +} > The steal time code can be removed in this patch, I think. > Originally I want to remove this piece of code, but it fails to compile if CONFIG_PARAVIRT is selected. Here is reference code, function paravirt_steal_clock() must be defined if CONFIG_PARAVIRT is selected. static __always_inline u64 steal_account_process_time(u64 maxtime) { #ifdef CONFIG_PARAVIRT if (static_key_false(¶virt_steal_enabled)) { u64 steal; steal = paravirt_steal_clock(smp_processor_id()); steal -= this_rq()->prev_steal_time; steal = min(steal, maxtime); account_steal_time(steal); this_rq()->prev_steal_time += steal; return steal; } #endif return 0; } >> + >> +int pv_guest_init(void); >> +#else >> +static inline int pv_guest_init(void) >> +{ >> + return 0; >> +} >> + >> +#endif // CONFIG_PARAVIRT >> +#endif >> diff --git a/arch/loongarch/include/asm/paravirt_api_clock.h b/arch/loongarch/include/asm/paravirt_api_clock.h >> new file mode 100644 >> index 000000000000..65ac7cee0dad >> --- /dev/null >> +++ b/arch/loongarch/include/asm/paravirt_api_clock.h >> @@ -0,0 +1 @@ >> +#include >> diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile >> index 3c808c680370..662e6e9de12d 100644 >> --- a/arch/loongarch/kernel/Makefile >> +++ b/arch/loongarch/kernel/Makefile >> @@ -48,6 +48,7 @@ obj-$(CONFIG_MODULES) += module.o module-sections.o >> obj-$(CONFIG_STACKTRACE) += stacktrace.o >> >> obj-$(CONFIG_PROC_FS) += proc.o >> +obj-$(CONFIG_PARAVIRT) += paravirt.o >> >> obj-$(CONFIG_SMP) += smp.o >> >> diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c >> new file mode 100644 >> index 000000000000..21d01d05791a >> --- /dev/null >> +++ b/arch/loongarch/kernel/paravirt.c >> @@ -0,0 +1,41 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +#include >> +#include >> +#include >> +#include >> +#include >> +#include >> + >> +struct static_key paravirt_steal_enabled; >> +struct static_key paravirt_steal_rq_enabled; >> + >> +static u64 native_steal_clock(int cpu) >> +{ >> + return 0; >> +} >> + >> +DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock); > The steal time code can be removed in this patch, I think. Ditto, the same reason with above. > >> + >> +static bool kvm_para_available(void) >> +{ >> + static int hypervisor_type; >> + int config; >> + >> + if (!hypervisor_type) { >> + config = read_cpucfg(CPUCFG_KVM_SIG); >> + if (!memcmp(&config, KVM_SIGNATURE, 4)) >> + hypervisor_type = HYPERVISOR_KVM; >> + } >> + >> + return hypervisor_type == HYPERVISOR_KVM; >> +} >> + >> +int __init pv_guest_init(void) >> +{ >> + if (!cpu_has_hypervisor) >> + return 0; >> + if (!kvm_para_available()) >> + return 0; >> + >> + return 1; >> +} >> diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c >> index edf2bba80130..de5c36dccc49 100644 >> --- a/arch/loongarch/kernel/setup.c >> +++ b/arch/loongarch/kernel/setup.c >> @@ -43,6 +43,7 @@ >> #include >> #include >> #include >> +#include >> #include >> #include >> #include >> @@ -367,6 +368,7 @@ void __init platform_init(void) >> pr_info("The BIOS Version: %s\n", b_info.bios_version); >> >> efi_runtime_init(); >> + pv_guest_init(); > I prefer use CONFIG_PARAVIRT here, though you have a dummy version for > !CONFIG_PARAVIRT, I think it is better to let others clearly know that > PARAVIRT is an optional feature. I remember that there is rule that CONFIG_xxx had better be used in header files rather than c code, so that the code looks neat. Am I wrong? Regards Bibo Mao > > Huacai > > > Huacai >> } >> >> static void __init check_kernel_sections_mem(void) >> -- >> 2.39.3 >> >>