Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3178134lqp; Tue, 26 Mar 2024 01:35:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV6lukjmMQBesatiTEaBQgHc15f1jZNqvVrRJfLRs04M2vPlMP/x1VldyYQ4HjJxvwmk0rAWTUyFttd8x+3TvN5odvncmudYOMV6bg26g== X-Google-Smtp-Source: AGHT+IFIOMZHTvfNerDtCz2Df5L78mLOwCrHCLjizjBMq1JcPdKTxHxk/pzsT2jo6z4BIcEHKoNO X-Received: by 2002:a05:620a:222e:b0:78a:2a5f:853d with SMTP id n14-20020a05620a222e00b0078a2a5f853dmr2109039qkh.31.1711442138752; Tue, 26 Mar 2024 01:35:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711442138; cv=pass; d=google.com; s=arc-20160816; b=G/5tsjFgxTmzcC1IXXdLSbBdMfLJTYxEvfN5WaAJTJdlAwISWMmtGOxqMYXpMPldGl I2PSWd0CQycPWTE8y5xtRpL/By/E7aqiHEGPOhoVniDVIZ8hIm28enlRh/301KIWbyuW GuNdBbIf2BkI1KkUPIF8Dz55NQ5idPnuMTzLpJymLaQlgPlNMiH1IaapUiwqn7KOlPAO DRZF/R82pxzVvFA+wyzhxM58tIyZxVilIuk8lSU9WT0sxtQk+Jl02hH5X00Z7l+O0oyM 2XTAUG8OV59jmgQPwMQDfgidhFu5j3HJKBHG2o7XTvlPo7M6EGozeJK2mQPFv2DM57DZ XvMg== 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=Id2aPpvk+2Nerp3BlQbTBWEiPAq/BdnfhkfmVEJ0Wxs=; fh=BiF0hEshLlgkfLjpsxHbCrGkBkMCi7pW69Ltatg7IXs=; b=K1caHHorUcs3MPUJnqs7AvnskrcuBzdVva8iwmMoBDl6s/GHUJ86Zamj/z/fcdATUP XjgDjJ0XEuoHi/waj8HFVojGEQ4Uz7ALgKBDf026JKHGtXzWyQY4YiWMyjWC9N+VsFkF V6BWumkLurYugq3GapQsoMBNcji+2vkwKXXTXwYvY0xepUPgm7E7vtRcwQyqtz8BAJOH 1b0SvRLXrCwTpQz2xPnmSQrCQq6OS9GgD89xV3FXKyJbRtpAJ2QoB3BouX0433Opavr9 TJmgzinV2qUfOM/H50utmN9FYghcMX5lABx4R1ALOMT8ojvPIUN9OqQd9X907Kae3dO/ GXPA==; 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-118556-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118556-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 d4-20020a05620a204400b00789e2766c14si7207620qka.168.2024.03.26.01.35.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Mar 2024 01:35:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118556-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; arc=pass (i=1 spf=pass spfdomain=loongson.cn); spf=pass (google.com: domain of linux-kernel+bounces-118556-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118556-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 66C2D1C32A94 for ; Tue, 26 Mar 2024 08:35:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D276112B82; Tue, 26 Mar 2024 08:35:29 +0000 (UTC) Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4CC68748D; Tue, 26 Mar 2024 08:35:25 +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=1711442129; cv=none; b=Ng3aANeEApZcLhr1glTizqOpkeaB9jCxuQ7GtiCnprWaB/z1DHH8XAo1YCoFQVoCM8N054LcJZL/wURycOC/0QuQpzufVrN3f9TF1bELP/UPc9DQtEfBiDfI087lajOeoqVF9X7jAoaXIUmAyl2cObrW/ImbN4JRGCkxsSZQ1Yo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711442129; c=relaxed/simple; bh=h9EeJQUPGscodmSjnmp0D/vEOTmiEFfN3yvTqvtFhuk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pOPXy3UxSIJWgIauPNb8krngOrmfzZt3vXjhAceV8fbP8b8dqJIBHmJ8Q0pc0tz1XQ1m9BZ40d+8EIUbBryBuTr1jyL2ir3Z7DLIzMLyUg4IpWAxHKMG7Oadgrc+s+wtmF+a6WE4zOXtwQ+hzWoo0H9973/yQmMiNi4dXdDD4nk= 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 _____8AxuugzhAJm2UoeAA--.51243S3; Tue, 26 Mar 2024 16:15:47 +0800 (CST) Received: from localhost.localdomain (unknown [10.2.5.213]) by localhost.localdomain (Coremail) with SMTP id AQAAf8Bx7c4yhAJm3aZoAA--.10447S2; Tue, 26 Mar 2024 16:15:46 +0800 (CST) From: Bibo Mao To: Tianrui Zhao , Huacai Chen Cc: Juergen Gross , kvm@vger.kernel.org, loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, virtualization@lists.linux.dev, x86@kernel.org Subject: [PATCH 2/2] LoongArch: Add steal time support in guest side Date: Tue, 26 Mar 2024 16:15:46 +0800 Message-Id: <20240326081546.973106-1-maobibo@loongson.cn> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240326080625.898051-1-maobibo@loongson.cn> References: <20240326080625.898051-1-maobibo@loongson.cn> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID:AQAAf8Bx7c4yhAJm3aZoAA--.10447S2 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7KY7 ZEXasCq-sGcSsGvfJ3UbIjqfuFe4nvWSU5nxnvy29KBjDU0xBIdaVrnUUvcSsGvfC2Kfnx nUUI43ZEXa7xR_UUUUUUUUU== X-Gw-Check: e5acd0b5fd9bcff5 Percpu struct kvm_steal_time is added here, its size is 64 bytes and also defined as 64 bytes, so that the whole structure is in one physical page. When vcpu is onlined, function pv_register_steal_time() is called. This function will pass physical address of struct kvm_steal_time and tells hypervisor to enable steal time. When vcpu is offline, physical address is set as 0 and tells hypervisor to disable steal time. Signed-off-by: Bibo Mao --- arch/loongarch/include/asm/paravirt.h | 5 + arch/loongarch/kernel/paravirt.c | 130 ++++++++++++++++++++++++++ arch/loongarch/kernel/time.c | 2 + 3 files changed, 137 insertions(+) diff --git a/arch/loongarch/include/asm/paravirt.h b/arch/loongarch/include/asm/paravirt.h index 58f7b7b89f2c..fe27fb5e82b8 100644 --- a/arch/loongarch/include/asm/paravirt.h +++ b/arch/loongarch/include/asm/paravirt.h @@ -17,11 +17,16 @@ static inline u64 paravirt_steal_clock(int cpu) } int pv_ipi_init(void); +int __init pv_time_init(void); #else static inline int pv_ipi_init(void) { return 0; } +static inline int pv_time_init(void) +{ + return 0; +} #endif // CONFIG_PARAVIRT #endif diff --git a/arch/loongarch/kernel/paravirt.c b/arch/loongarch/kernel/paravirt.c index 9044ed62045c..56182c64ab38 100644 --- a/arch/loongarch/kernel/paravirt.c +++ b/arch/loongarch/kernel/paravirt.c @@ -5,10 +5,13 @@ #include #include #include +#include #include struct static_key paravirt_steal_enabled; struct static_key paravirt_steal_rq_enabled; +static DEFINE_PER_CPU(struct kvm_steal_time, steal_time) __aligned(64); +static int has_steal_clock; static u64 native_steal_clock(int cpu) { @@ -17,6 +20,57 @@ static u64 native_steal_clock(int cpu) DEFINE_STATIC_CALL(pv_steal_clock, native_steal_clock); +static bool steal_acc = true; +static int __init parse_no_stealacc(char *arg) +{ + steal_acc = false; + return 0; +} +early_param("no-steal-acc", parse_no_stealacc); + +static u64 para_steal_clock(int cpu) +{ + u64 steal; + struct kvm_steal_time *src; + int version; + + src = &per_cpu(steal_time, cpu); + do { + + version = src->version; + /* Make sure that the version is read before the steal */ + virt_rmb(); + steal = src->steal; + /* Make sure that the steal is read before the next version */ + virt_rmb(); + + } while ((version & 1) || (version != src->version)); + return steal; +} + +static int pv_register_steal_time(void) +{ + int cpu = smp_processor_id(); + struct kvm_steal_time *st; + unsigned long addr; + + if (!has_steal_clock) + return -EPERM; + + st = &per_cpu(steal_time, cpu); + addr = per_cpu_ptr_to_phys(st); + + /* The whole structure kvm_steal_time should be one page */ + if (PFN_DOWN(addr) != PFN_DOWN(addr + sizeof(*st))) { + pr_warn("Illegal PV steal time addr %lx\n", addr); + return -EFAULT; + } + + addr |= KVM_STEAL_PHYS_VALID; + kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, KVM_FEATURE_STEAL_TIME, addr); + return 0; +} + #ifdef CONFIG_SMP static void pv_send_ipi_single(int cpu, unsigned int action) { @@ -110,6 +164,32 @@ static void pv_init_ipi(void) if (r < 0) panic("SWI0 IRQ request failed\n"); } + +static void pv_disable_steal_time(void) +{ + if (has_steal_clock) + kvm_hypercall2(KVM_HCALL_FUNC_NOTIFY, KVM_FEATURE_STEAL_TIME, 0); +} + +static int pv_cpu_online(unsigned int cpu) +{ + unsigned long flags; + + local_irq_save(flags); + pv_register_steal_time(); + local_irq_restore(flags); + return 0; +} + +static int pv_cpu_down_prepare(unsigned int cpu) +{ + unsigned long flags; + + local_irq_save(flags); + pv_disable_steal_time(); + local_irq_restore(flags); + return 0; +} #endif static bool kvm_para_available(void) @@ -149,3 +229,53 @@ int __init pv_ipi_init(void) return 1; } + +static void pv_cpu_reboot(void *unused) +{ + pv_disable_steal_time(); +} + +static int pv_reboot_notify(struct notifier_block *nb, unsigned long code, + void *unused) +{ + on_each_cpu(pv_cpu_reboot, NULL, 1); + return NOTIFY_DONE; +} + +static struct notifier_block pv_reboot_nb = { + .notifier_call = pv_reboot_notify, +}; + +int __init pv_time_init(void) +{ + int feature; + + if (!cpu_has_hypervisor) + return 0; + if (!kvm_para_available()) + return 0; + + feature = read_cpucfg(CPUCFG_KVM_FEATURE); + if (!(feature & KVM_FEATURE_STEAL_TIME)) + return 0; + + has_steal_clock = 1; + if (pv_register_steal_time()) { + has_steal_clock = 0; + return 0; + } + + register_reboot_notifier(&pv_reboot_nb); + static_call_update(pv_steal_clock, para_steal_clock); + static_key_slow_inc(¶virt_steal_enabled); + if (steal_acc) + static_key_slow_inc(¶virt_steal_rq_enabled); + +#ifdef CONFIG_SMP + if (cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "loongarch/pv:online", + pv_cpu_online, pv_cpu_down_prepare) < 0) + pr_err("Failed to install cpu hotplug callbacks\n"); +#endif + pr_info("Using stolen time PV\n"); + return 0; +} diff --git a/arch/loongarch/kernel/time.c b/arch/loongarch/kernel/time.c index fd5354f9be7c..46d7d40c87e3 100644 --- a/arch/loongarch/kernel/time.c +++ b/arch/loongarch/kernel/time.c @@ -15,6 +15,7 @@ #include #include +#include #include u64 cpu_clock_freq; @@ -214,4 +215,5 @@ void __init time_init(void) constant_clockevent_init(); constant_clocksource_init(); + pv_time_init(); } -- 2.39.3