Received: by 2002:a05:7412:cfc7:b0:fc:a2b0:25d7 with SMTP id by7csp903545rdb; Sun, 18 Feb 2024 18:42:51 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU2kjtnHSFMOrdDfEj8nu6w6mzSb2qrU/YkoejZcW83BWxsJplBjvoJjt+8MqTeiu2Xpc1gvpn81jDP8YObZqRApF9smG4XKFf+5NGDzA== X-Google-Smtp-Source: AGHT+IEygKGn9HGOTCHwtwRXFJnohL5+hAlPAr1OceqIaD+yrs0/Wc7mHvaBDz7GB+rSxKv8bhrt X-Received: by 2002:a05:651c:1a1f:b0:2d2:2b8c:aa1c with SMTP id by31-20020a05651c1a1f00b002d22b8caa1cmr3569546ljb.17.1708310571034; Sun, 18 Feb 2024 18:42:51 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708310571; cv=pass; d=google.com; s=arc-20160816; b=ASQjILz7zWxsVPE18F26rPQA4f7FBg4JxnNG0pyv9dFasdRwfjJ+Dwysq4Vm+faLKv xXNyWYztnwH4nBSbiuzCPRNm2RXyg269/mVn0iA1Yrr36GTFz1YS3/9bxdSztS1grGCC n2PDia4B7Z/66PdZzwgROmk5JzAe+LU8HtXRYIGUxm5NrsdM8xCHN+8KrjHb3VL80qlJ rcE/sQ9NI2KHHY4H9EMc9XR6rNopwQ0a7OYHvGtFCzVoQEZOohgjKYvqJxThA/QORrDT qYFvj1NyspfTwOsKv5LpGx+GOG8nlf8GEtk9Xx604Ru1f+MpSgtRDB2UwCO2RHn9ZyBG b1hw== 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=r4YFSxbX7Qi5FOepxX++i1GY5SO0tWUbx8gxAOYcjZU=; fh=RNWQV3RvFKfTmI4czIOTDaTl2XYhNsDJVcbytCLimrw=; b=uLCE2dH6CsQa3Sthch9qYVEUuI4gl6gL/JG8uGhLzpNuqXqC6vz9xZPdA5yTGarBlj 0qylaCUHeLi0dlD5eggQK/MmT65fq53vtVidxFKssh7bg30Xf418IhH+NxlshPcz2OzU gnv6iUzPf2j/0+RCHuxy8ZVl8nQZQ/UWVbw4oGGwdGKJ7CmnetuDi8+SIAsMR2ZgM5LL UNMjEkaOr8vqerCOj72KKsL1hU04xFpGR6YSEd3Xn8Z8MwvW09iD22+n3TZvzTpjPCPV k2YzLQcy7zcwl27pSMjNDApadLC4l2iiUjbgvNx57aR/06C5mfnA5wLSvugHVv5G418C 2Ayw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ECg+JqKa; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-70661-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70661-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id ba7-20020a0564021ac700b005633f37a2d1si2013304edb.365.2024.02.18.18.42.50 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 18:42:51 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70661-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ECg+JqKa; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-70661-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70661-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 9A98A1F21600 for ; Mon, 19 Feb 2024 02:42:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id DF9C93FE1; Mon, 19 Feb 2024 02:42:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="ECg+JqKa" 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 DBD171FB3; Mon, 19 Feb 2024 02:42:38 +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=1708310558; cv=none; b=FAO+yhcwDCx70S1fCqciYXgJbL+YZq07USnyMAawYzyopdsD9bZT5s6g5l6LHO585CT+peuXyGgiRggp9/ZOoblcmSQBCUo5njdUDoxWNzrj0OaoWzRDXgMgCX/Vm1yPTl3WyY8aRwSECYfJGbrETJhuW4PrwuTRVM6Vgruk9eg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708310558; c=relaxed/simple; bh=9eS8p4zQXK+QfbnY/+cqnnC0ZGwjNn2ED2zDqwqqimg=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=rtSyn7uxsOz9p++M2r1njh3hCow8hoj1Zh0qffftc9to988VdhuFSyUTAnMuLR1/Z3HzsEq+kd2U9InzBCu45HnNzbmezXB54IzlH7kw1VsIOf82fiFT0RiOAAsKoouyEfM/nvKkH/8km3/qh5y5fVlukLmxS2shvRrKzbctNQ0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ECg+JqKa; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id C0134C433A6; Mon, 19 Feb 2024 02:42:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1708310558; bh=9eS8p4zQXK+QfbnY/+cqnnC0ZGwjNn2ED2zDqwqqimg=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ECg+JqKaXHCHcmbtKPFiYqw9gPpZvbPWu+TCbSjv+XAUXOPQt1H6KKcVT0WKkS46y mdajhbZgKfaGiwnh2/Mi/ya00EmB5ykz5h73WwVGODZZwIzEapRPCIixfd3EApxCRI eNyHko1BOTalMcu8+/RgzjulLfxC5q7UN1e45DgY4tBct3BakSHmZpczLV5elefRZO zGS2MS6//jix2SByrolbDFCKGv3RkRwdQHYCTtWn+Ahks+4rmnNuLe0eZHmqmLF4IS yzXzWkRyfCuvaKBcsRQvvECSzGXh0zcv6BIArPjtJbqSUjuvmLXt5XSJ06zYnFgvRY afb4m2K7BVLBQ== Received: by mail-lf1-f47.google.com with SMTP id 2adb3069b0e04-512b36bb97eso344447e87.1; Sun, 18 Feb 2024 18:42:38 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXqMfDHcUwangtFkCDleVICGVAuwkbFJLwIDTsJFY2MPMKJXT8esvAsskjJoPpWHGAx9tHHc1HSKXBxLPnP8heF6nWNFDeuI339LAqLTEmhZRpILNK91c67V7oFYFdMunOK X-Gm-Message-State: AOJu0YzRi6nRMke/fus86je7uF0L9Cmhqmj/FJ+J2ZYSLwF2aEdK3oBV kzjdNmoj1zkB1XiRjod7WYLbzTotV5++F9vuT0OPIgY9EDhz10gTWafqO1MM74vhnPH+kJA6p4/ J2qpSZXNt7N2pOp4ECbZeeiG0tf0= X-Received: by 2002:a05:6512:230a:b0:510:323d:6401 with SMTP id o10-20020a056512230a00b00510323d6401mr8619302lfu.53.1708310556963; Sun, 18 Feb 2024 18:42:36 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240201031950.3225626-1-maobibo@loongson.cn> <20240201031950.3225626-5-maobibo@loongson.cn> In-Reply-To: <20240201031950.3225626-5-maobibo@loongson.cn> From: Huacai Chen Date: Mon, 19 Feb 2024 10:42:35 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v4 4/6] LoongArch: Add paravirt interface for guest kernel To: Bibo Mao Cc: Tianrui Zhao , Juergen Gross , Paolo Bonzini , 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 Hi, Bibo, On Thu, Feb 1, 2024 at 11:19=E2=80=AFAM Bibo Mao wrot= e: > > 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 significa= ntly > + over full virtualization. However, when run without a hypervis= or > + 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/inclu= de/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_SHI= FT) + code) > + > /* > * LoongArch hypcall return code > */ > diff --git a/arch/loongarch/include/asm/paravirt.h b/arch/loongarch/inclu= de/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. > + > +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/loong= arch/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/Makef= ile > index 3c808c680370..662e6e9de12d 100644 > --- a/arch/loongarch/kernel/Makefile > +++ b/arch/loongarch/kernel/Makefile > @@ -48,6 +48,7 @@ obj-$(CONFIG_MODULES) +=3D module.o module-sect= ions.o > obj-$(CONFIG_STACKTRACE) +=3D stacktrace.o > > obj-$(CONFIG_PROC_FS) +=3D proc.o > +obj-$(CONFIG_PARAVIRT) +=3D paravirt.o > > obj-$(CONFIG_SMP) +=3D smp.o > > diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/par= avirt.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. > + > +static bool kvm_para_available(void) > +{ > + static int hypervisor_type; > + int config; > + > + if (!hypervisor_type) { > + config =3D read_cpucfg(CPUCFG_KVM_SIG); > + if (!memcmp(&config, KVM_SIGNATURE, 4)) > + hypervisor_type =3D HYPERVISOR_KVM; > + } > + > + return hypervisor_type =3D=3D 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. Huacai Huacai > } > > static void __init check_kernel_sections_mem(void) > -- > 2.39.3 > >