Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp6986539rdb; Wed, 3 Jan 2024 00:01:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IGHrmK3kI+bH1+lcKyQHq4A9PKziwYWm6okXNs3IgnjX7+3pYBKw5+zkyMeO2jyysOAk1Q3 X-Received: by 2002:a05:6808:e85:b0:3ba:f4a:4315 with SMTP id k5-20020a0568080e8500b003ba0f4a4315mr15625616oil.102.1704268864787; Wed, 03 Jan 2024 00:01:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704268864; cv=none; d=google.com; s=arc-20160816; b=Rot2aAFJtqU2RsLdE0eP5OJADfT00ZO9Oz0lIYJcf7Zqq81ZJ4I2f5PKw1ur6yT9vB d0H0HoTd6qBGjMh5uael2IvOCMzJWFxZLBb+wglxW+nWWwXs/bieN7HHeY5pEMsyD0qy Ckf9WHIMPDRJIwB/8p1rrnkQiQhksDqf2sJgE8KmO/ajQ+HCOUfqd1Ckq7WjhMxwsiv9 4k/O5SvHpLobkoaguaHWk945ogerFHNpjtoDsIDvOIIjXu3uNhNKVFcJlGR/E8YHGi7j u/u+8Koe/tmHijSPI0Lof2Q/z6Ff026b9Bb8TAfMue3njQE1Hghvb3ImFlMBL/QghmgW o5wg== ARC-Message-Signature: i=1; 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=6sQ8DJC5c6DmLx4fDgT+loJg4ahm9npzcCeDjrHmpFU=; fh=gNFcRUPLZWglbhFH6Vr96QUUBEOFtdSlrTLZLTge4mU=; b=KrkjAy/Eb6gYjMZKSIPaxi3xD47nmIQ812bLyWSz4yi9JzjOfL46RG368HuucgAWNS 6Co/Ng1lZP8ZoCKb5lFrJ0/Ly3azT6Imv2X9rqs7Xwbgs/u9RBLd8KPnRuXRVnSOQKYs b9mYXt9QwbapDXwHqPUgq00qTsUhVEU0L9MAKw3NxnCanXELnhAW5mc2EPffctl+BAp1 8ROuJLNBW89PmQeNyJxBAof0h3kFU+vjUyxYn2NylDpznX256+QUa4ytojQIuiKmaA+R 7yWaVpoOr8h79/0sZY1z1+FiAJ2CeHu0NC7uOTZAfffa//LxS93iszoZiwKJHi8KuxQ7 Zpuw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-15256-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15256-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id g16-20020a0cdf10000000b0067f6e9e68ebsi28698854qvl.419.2024.01.03.00.01.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Jan 2024 00:01:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-15256-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel+bounces-15256-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-15256-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 7AAD81C22F1A for ; Wed, 3 Jan 2024 08:01:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CFB02182A2; Wed, 3 Jan 2024 08:00:51 +0000 (UTC) X-Original-To: linux-kernel@vger.kernel.org Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F26B7182A4; Wed, 3 Jan 2024 08:00:48 +0000 (UTC) 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 _____8CxquotFJVlJXgBAA--.4973S3; Wed, 03 Jan 2024 16:00:45 +0800 (CST) Received: from [10.20.42.173] (unknown [10.20.42.173]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bxyr0pFJVl7XAYAA--.44424S3; Wed, 03 Jan 2024 16:00:43 +0800 (CST) Subject: Re: [PATCH 4/5] LoongArch: Add paravirt interface for guest kernel To: =?UTF-8?B?SsO8cmdlbiBHcm/Dnw==?= , Huacai Chen , Tianrui Zhao Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, kvm@vger.kernel.org References: <20240103071615.3422264-1-maobibo@loongson.cn> <20240103071615.3422264-5-maobibo@loongson.cn> <66c15a1b-fb28-4653-982f-37494a01cd4f@suse.com> From: maobibo Message-ID: <4240d67f-1e5d-f865-c16e-32b64aea8099@loongson.cn> Date: Wed, 3 Jan 2024 16:00:41 +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: <66c15a1b-fb28-4653-982f-37494a01cd4f@suse.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8Bxyr0pFJVl7XAYAA--.44424S3 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3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wI0_Gr0_Gr1UYxBI daVFxhVjvjDU0xZFpf9x07j1WlkUUUUU= On 2024/1/3 下午3:40, Jürgen Groß wrote: > On 03.01.24 08:16, Bibo Mao wrote: >> The patch add paravirt interface for guest kernel, it checks whether >> system runs on VM mode. If it is, it will detect hypervisor type. And >> returns true it is KVM hypervisor, else return false. Currently only >> KVM hypervisor is supported, so there is only hypervisor detection >> for KVM type. > > I guess you are talking of pv_guest_init() here? Or do you mean > kvm_para_available()? yes, it is pv_guest_init. It will be better if all hypervisor detection is called in function pv_guest_init. Currently there is only kvm hypervisor, kvm_para_available is hard-coded in pv_guest_init here. I can split file paravirt.c into paravirt.c and kvm.c, paravirt.c is used for hypervisor detection, and move code relative with pv_ipi into kvm.c Regards Bibo Mao > >> >> Signed-off-by: Bibo Mao >> --- >>   arch/loongarch/Kconfig                        |  8 ++++ >>   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, 87 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 ee123820a476..940e5960d297 100644 >> --- a/arch/loongarch/Kconfig >> +++ b/arch/loongarch/Kconfig >> @@ -564,6 +564,14 @@ config CPU_HAS_PREFETCH >>       bool >>       default y >> +config PARAVIRT >> +    bool "Enable paravirtualization code" >> +    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); >> +} >> + >> +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); > > This is the 4th arch with the same definition of native_steal_clock() and > pv_steal_clock. I think we should add a common file kernel/paravirt.c and > move the related functions from the archs into the new file. > > If you don't want to do that I can prepare a series. > >> + >> +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 d183a745fb85..fa680bdd0bd1 100644 >> --- a/arch/loongarch/kernel/setup.c >> +++ b/arch/loongarch/kernel/setup.c >> @@ -43,6 +43,7 @@ >>   #include >>   #include >>   #include >> +#include >>   #include >>   #include >>   #include >> @@ -376,6 +377,7 @@ void __init platform_init(void) >>       pr_info("The BIOS Version: %s\n", b_info.bios_version); >>       efi_runtime_init(); >> +    pv_guest_init(); > > Any reason pv_guest_init() needs to return a value at all, seeing that > you don't > use the returned value? > > > Juergen