Message ID | 20240201031950.3225626-5-maobibo@loongson.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel+bounces-47568-ouuuleilei=gmail.com@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:106:209c:c626 with SMTP id mn5csp181007dyc; Wed, 31 Jan 2024 19:21:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IF5n1Hb8Dn07i+aXgcgeQXzoT+ZGeYPsxowsT2Wk3+XQFIEAVnqF89qNoopA+7Jj8zX4wSR X-Received: by 2002:a05:6102:21ca:b0:468:bb6:a089 with SMTP id r10-20020a05610221ca00b004680bb6a089mr3475102vsg.25.1706757707032; Wed, 31 Jan 2024 19:21:47 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706757707; cv=pass; d=google.com; s=arc-20160816; b=NfjSwwue8/IrduQqe5tgzJRmNlxG6eFs5outrQlLbeA9x/dUKBvqhNPtsPYm5BCLnV yrQg+zLMFnI42viXWk1ypijL2udTwDk+qWC6vM4rQ9NKWA7V9CWjqayCkDFXDxAj0AQa TeyK+Xb1e9+75r1sO2IDeiyUc5eRYIXPK59ifboDA7Z+L9etRd0Ws4MHF5wJQi5x8umY tfIytof2X7juAgNBqshJsqTK1F4Nge66HJUlhUIzRaRJHG4MmhwpPwEIcjiK92FiaCSU 6VyobUjF4zRbLNR8xgjLZiSjbIXetA8LbzmkvJDjPKsCMRnuvdC4tdUZYZWix9woHFB6 Lcug== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from; bh=fmZgrXtSBxa81KPB6lUkUFJl47hVBaiReyzzq+7OErc=; fh=dhpasAjd/V7pjP0E8mEbN2p3BgJsm7RhbE/JH4YS7CI=; b=yvElv7TPrDq5DGFBW8Q4EFZF9d6usXVAYRGJiYVH5BPZPI+GvTsWE9S0OWTkIVTNqt 3EqfocELAWJdFQQ9Nq+c3OkPNZsW05tLNmZRwe7tr7yOMHLdWs1HKWFpt+kPmhMCLlKp NoqMPm1VgtEHBm6sCy0eOFDBvN92Ed2Euj3M2NLwjX0A7ODOJ0bAwnGWbsR/a6EeSnOP ALoMp9Ls4V35trB28SapNRWxP14rlU2/Acdmndw4ELzGtvVpf5uwXih7vYFK2Hf5UyiN fsD1j4GBwL7hnhsQHeSgnVzrVlDS3v41cVpfDttNGo2kxQjrRB94IvCeJgRZedtWo4qg FbkQ==; 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-47568-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47568-ouuuleilei=gmail.com@vger.kernel.org" X-Forwarded-Encrypted: i=1; AJvYcCW7xt4WLZhEYgCh1FLoP5kmQi6+hBoq7TlSLyTHB96ydM7o5K4ztZ9OoWFypnTtzgpt578WZa1JBRCXSChiKBTD1T3E6A== Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 3-20020ad45b83000000b0068c69ce7eb0si3170991qvp.364.2024.01.31.19.21.46 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Jan 2024 19:21:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-47568-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-47568-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-47568-ouuuleilei=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 C06F61C223EC for <ouuuleilei@gmail.com>; Thu, 1 Feb 2024 03:21:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 22C013FB34; Thu, 1 Feb 2024 03:20:01 +0000 (UTC) Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C3093B786; Thu, 1 Feb 2024 03:19:55 +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=1706757598; cv=none; b=nAlIz4xhrhLFBAkq/BWJhLFzSMo5CL65uIWxbhV5HiXYwrY3kqis1zK5YJTGkLs9MGh6bbTYWYbZreVx3MDXARslkr0tKb+LIbFV5pxX2L+oZX04uNJkvNTsuGwiU/+9DiouRTF7wctsh7L/Z+zPcvxRiauoyresixmmE/Z6CKQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706757598; c=relaxed/simple; bh=0ZTxd1BxJsW/4rbA8jFXv+6trpyo0NlYbtXO8WO78hA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qJ+lZZt7TUOicN8u6z6KKWSZDqbn7SzVH+wNJDIPFNSarS0KMB2L6/AWH/94I/IK2bgmc96cTvLZEmToRb6aCW2Mv+PiyoNkP9uo09s6EuPjrO+Pn8gCnB8k3H/ZSZSK309S+pEyZC7RLIdvLD+x5V9gG/CQkzVZ0dpPjk1x3aE= 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.2.5.213]) by gateway (Coremail) with SMTP id _____8CxSPDaDbtlQFQJAA--.28111S3; Thu, 01 Feb 2024 11:19:54 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxfRPWDbtltkIrAA--.3273S6; Thu, 01 Feb 2024 11:19:52 +0800 (CST) From: Bibo Mao <maobibo@loongson.cn> To: Huacai Chen <chenhuacai@kernel.org>, Tianrui Zhao <zhaotianrui@loongson.cn>, Juergen Gross <jgross@suse.com>, Paolo Bonzini <pbonzini@redhat.com> Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, kvm@vger.kernel.org Subject: [PATCH v4 4/6] LoongArch: Add paravirt interface for guest kernel Date: Thu, 1 Feb 2024 11:19:48 +0800 Message-Id: <20240201031950.3225626-5-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240201031950.3225626-1-maobibo@loongson.cn> References: <20240201031950.3225626-1-maobibo@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: <linux-kernel.vger.kernel.org> List-Subscribe: <mailto:linux-kernel+subscribe@vger.kernel.org> List-Unsubscribe: <mailto:linux-kernel+unsubscribe@vger.kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8DxfRPWDbtltkIrAA--.3273S6 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoW3Xw1xtFyxuw4kWw48Wr15GFX_yoW7tr15pa 4DAr4kWa1kGFn3A393KrWY9r15Jws7Cry29Fy3u34FyFsFqr1UXr4vgryqvFyDta1kJay0 gFyrGws0ga1UAabCm3ZEXasCq-sJn29KB7ZKAUJUUUU7529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUBIb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU tVWrXwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxkF7I0En4kS14v26r126r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY 6r1j6r4UMxCIbckI1I0E14v26r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7 xvwVAFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xII jxv20xvE14v26ryj6F1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw2 0EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x02 67AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU0epB3UUUUU== X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1789665169444678278 X-GMAIL-MSGID: 1789665169444678278 |
Series |
LoongArch: Add pv ipi support on LoongArch VM
|
|
Commit Message
maobibo
Feb. 1, 2024, 3:19 a.m. UTC
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 <maobibo@loongson.cn>
---
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
Comments
Hi, Bibo, On Thu, Feb 1, 2024 at 11:19 AM Bibo Mao <maobibo@loongson.cn> 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 <maobibo@loongson.cn> > --- > 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 <linux/static_call_types.h> > +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/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 <asm/paravirt.h> > 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 <linux/export.h> > +#include <linux/types.h> > +#include <linux/jump_label.h> > +#include <linux/kvm_para.h> > +#include <asm/paravirt.h> > +#include <linux/static_call.h> > + > +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 = 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 <asm/efi.h> > #include <asm/loongson.h> > #include <asm/numa.h> > +#include <asm/paravirt.h> > #include <asm/pgalloc.h> > #include <asm/sections.h> > #include <asm/setup.h> > @@ -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 > >
On 2024/2/19 上午10:42, Huacai Chen wrote: > Hi, Bibo, > > On Thu, Feb 1, 2024 at 11:19 AM Bibo Mao <maobibo@loongson.cn> 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 <maobibo@loongson.cn> >> --- >> 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 <linux/static_call_types.h> >> +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 <asm/paravirt.h> >> 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 <linux/export.h> >> +#include <linux/types.h> >> +#include <linux/jump_label.h> >> +#include <linux/kvm_para.h> >> +#include <asm/paravirt.h> >> +#include <linux/static_call.h> >> + >> +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 <asm/efi.h> >> #include <asm/loongson.h> >> #include <asm/numa.h> >> +#include <asm/paravirt.h> >> #include <asm/pgalloc.h> >> #include <asm/sections.h> >> #include <asm/setup.h> >> @@ -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 >> >>
On Mon, Feb 19, 2024 at 12:11 PM maobibo <maobibo@loongson.cn> wrote: > > > > On 2024/2/19 上午10:42, Huacai Chen wrote: > > Hi, Bibo, > > > > On Thu, Feb 1, 2024 at 11:19 AM Bibo Mao <maobibo@loongson.cn> 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 <maobibo@loongson.cn> > >> --- > >> 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 <linux/static_call_types.h> > >> +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; > } OK, then keep it. > > >> + > >> +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 <asm/paravirt.h> > >> 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 <linux/export.h> > >> +#include <linux/types.h> > >> +#include <linux/jump_label.h> > >> +#include <linux/kvm_para.h> > >> +#include <asm/paravirt.h> > >> +#include <linux/static_call.h> > >> + > >> +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 <asm/efi.h> > >> #include <asm/loongson.h> > >> #include <asm/numa.h> > >> +#include <asm/paravirt.h> > >> #include <asm/pgalloc.h> > >> #include <asm/sections.h> > >> #include <asm/setup.h> > >> @@ -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? That depends on what we want, sometimes we want to hide the details, but sometimes we want to give others a notice. And there is another problem: if you want to centralize all pv init functions, it is better to use pv_features_init() rather than pv_guest_init(); if you want to give each feature an init function, then we don't need pv_guest_init here, and we can then add a pv_ipi_init() in the last patch. Huacai > > Regards > Bibo Mao > > > > Huacai > > > > > > Huacai > >> } > >> > >> static void __init check_kernel_sections_mem(void) > >> -- > >> 2.39.3 > >> > >> >
On 2024/2/19 下午4:48, Huacai Chen wrote: > On Mon, Feb 19, 2024 at 12:11 PM maobibo <maobibo@loongson.cn> wrote: >> >> >> >> On 2024/2/19 上午10:42, Huacai Chen wrote: >>> Hi, Bibo, >>> >>> On Thu, Feb 1, 2024 at 11:19 AM Bibo Mao <maobibo@loongson.cn> 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 <maobibo@loongson.cn> >>>> --- >>>> 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 <linux/static_call_types.h> >>>> +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; >> } > OK, then keep it. > >> >>>> + >>>> +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 <asm/paravirt.h> >>>> 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 <linux/export.h> >>>> +#include <linux/types.h> >>>> +#include <linux/jump_label.h> >>>> +#include <linux/kvm_para.h> >>>> +#include <asm/paravirt.h> >>>> +#include <linux/static_call.h> >>>> + >>>> +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 <asm/efi.h> >>>> #include <asm/loongson.h> >>>> #include <asm/numa.h> >>>> +#include <asm/paravirt.h> >>>> #include <asm/pgalloc.h> >>>> #include <asm/sections.h> >>>> #include <asm/setup.h> >>>> @@ -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? > That depends on what we want, sometimes we want to hide the details, > but sometimes we want to give others a notice. I want to keep code clean here :) > > And there is another problem: if you want to centralize all pv init > functions, it is better to use pv_features_init() rather than > pv_guest_init(); if you want to give each feature an init function, > then we don't need pv_guest_init here, and we can then add a > pv_ipi_init() in the last patch. Currently I have no idea how to add other pv features like pv stealtimer, I will consider this when adding other pv features. pv_ipi_init/pv_guest_init is both ok for me, pv_ipi_init is better for now. Regards Bibo Mao > > Huacai > >> >> Regards >> Bibo Mao >>> >>> Huacai >>> >>> >>> Huacai >>>> } >>>> >>>> static void __init check_kernel_sections_mem(void) >>>> -- >>>> 2.39.3 >>>> >>>> >>
On Mon, Feb 19, 2024 at 5:21 PM maobibo <maobibo@loongson.cn> wrote: > > > > On 2024/2/19 下午4:48, Huacai Chen wrote: > > On Mon, Feb 19, 2024 at 12:11 PM maobibo <maobibo@loongson.cn> wrote: > >> > >> > >> > >> On 2024/2/19 上午10:42, Huacai Chen wrote: > >>> Hi, Bibo, > >>> > >>> On Thu, Feb 1, 2024 at 11:19 AM Bibo Mao <maobibo@loongson.cn> 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 <maobibo@loongson.cn> > >>>> --- > >>>> 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 <linux/static_call_types.h> > >>>> +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; > >> } > > OK, then keep it. > > > >> > >>>> + > >>>> +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 <asm/paravirt.h> > >>>> 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 <linux/export.h> > >>>> +#include <linux/types.h> > >>>> +#include <linux/jump_label.h> > >>>> +#include <linux/kvm_para.h> > >>>> +#include <asm/paravirt.h> > >>>> +#include <linux/static_call.h> > >>>> + > >>>> +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 <asm/efi.h> > >>>> #include <asm/loongson.h> > >>>> #include <asm/numa.h> > >>>> +#include <asm/paravirt.h> > >>>> #include <asm/pgalloc.h> > >>>> #include <asm/sections.h> > >>>> #include <asm/setup.h> > >>>> @@ -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? > > That depends on what we want, sometimes we want to hide the details, > > but sometimes we want to give others a notice. > I want to keep code clean here :) > > > > > And there is another problem: if you want to centralize all pv init > > functions, it is better to use pv_features_init() rather than > > pv_guest_init(); if you want to give each feature an init function, > > then we don't need pv_guest_init here, and we can then add a > > pv_ipi_init() in the last patch. > Currently I have no idea how to add other pv features like pv > stealtimer, I will consider this when adding other pv features. > pv_ipi_init/pv_guest_init is both ok for me, pv_ipi_init is better for now. Then you want to add an init function for each feature, so please rename to pv_ipi_init(), move to the last patch and in loongson_smp_setup(). Huacai > > Regards > Bibo Mao > > > > > Huacai > > > >> > >> Regards > >> Bibo Mao > >>> > >>> Huacai > >>> > >>> > >>> Huacai > >>>> } > >>>> > >>>> static void __init check_kernel_sections_mem(void) > >>>> -- > >>>> 2.39.3 > >>>> > >>>> > >> >
On 2024/2/19 下午5:38, Huacai Chen wrote: > On Mon, Feb 19, 2024 at 5:21 PM maobibo <maobibo@loongson.cn> wrote: >> >> >> >> On 2024/2/19 下午4:48, Huacai Chen wrote: >>> On Mon, Feb 19, 2024 at 12:11 PM maobibo <maobibo@loongson.cn> wrote: >>>> >>>> >>>> >>>> On 2024/2/19 上午10:42, Huacai Chen wrote: >>>>> Hi, Bibo, >>>>> >>>>> On Thu, Feb 1, 2024 at 11:19 AM Bibo Mao <maobibo@loongson.cn> 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 <maobibo@loongson.cn> >>>>>> --- >>>>>> 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 <linux/static_call_types.h> >>>>>> +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; >>>> } >>> OK, then keep it. >>> >>>> >>>>>> + >>>>>> +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 <asm/paravirt.h> >>>>>> 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 <linux/export.h> >>>>>> +#include <linux/types.h> >>>>>> +#include <linux/jump_label.h> >>>>>> +#include <linux/kvm_para.h> >>>>>> +#include <asm/paravirt.h> >>>>>> +#include <linux/static_call.h> >>>>>> + >>>>>> +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 <asm/efi.h> >>>>>> #include <asm/loongson.h> >>>>>> #include <asm/numa.h> >>>>>> +#include <asm/paravirt.h> >>>>>> #include <asm/pgalloc.h> >>>>>> #include <asm/sections.h> >>>>>> #include <asm/setup.h> >>>>>> @@ -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? >>> That depends on what we want, sometimes we want to hide the details, >>> but sometimes we want to give others a notice. >> I want to keep code clean here :) >> >>> >>> And there is another problem: if you want to centralize all pv init >>> functions, it is better to use pv_features_init() rather than >>> pv_guest_init(); if you want to give each feature an init function, >>> then we don't need pv_guest_init here, and we can then add a >>> pv_ipi_init() in the last patch. >> Currently I have no idea how to add other pv features like pv >> stealtimer, I will consider this when adding other pv features. >> pv_ipi_init/pv_guest_init is both ok for me, pv_ipi_init is better for now. > Then you want to add an init function for each feature, so please > rename to pv_ipi_init(), move to the last patch and in > loongson_smp_setup(). Sure, will change function name with pv_ipi_init() and move sentences calling this function to last patch. Regards Bibo Mao > > Huacai > >> >> Regards >> Bibo Mao >> >>> >>> Huacai >>> >>>> >>>> Regards >>>> Bibo Mao >>>>> >>>>> Huacai >>>>> >>>>> >>>>> Huacai >>>>>> } >>>>>> >>>>>> static void __init check_kernel_sections_mem(void) >>>>>> -- >>>>>> 2.39.3 >>>>>> >>>>>> >>>> >>
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 <linux/static_call_types.h> +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 <asm/paravirt.h> 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 <linux/export.h> +#include <linux/types.h> +#include <linux/jump_label.h> +#include <linux/kvm_para.h> +#include <asm/paravirt.h> +#include <linux/static_call.h> + +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); + +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 <asm/efi.h> #include <asm/loongson.h> #include <asm/numa.h> +#include <asm/paravirt.h> #include <asm/pgalloc.h> #include <asm/sections.h> #include <asm/setup.h> @@ -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(); } static void __init check_kernel_sections_mem(void)