From patchwork Tue Jul 21 04:17:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Senozhatsky X-Patchwork-Id: 11674925 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8760D138C for ; Tue, 21 Jul 2020 04:19:42 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 60AE920792 for ; Tue, 21 Jul 2020 04:19:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="dRket2HB"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bqhSzg4Q" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 60AE920792 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=2/5r7BO/8AtTjRlZ8g3X23B8kYQeY7Ql4WcdoZMqcXo=; b=dRket2HBRi2U7JwmAHKbiu7uN M4/3xvfQpSZuK2C+0S7VCr2EPu/jjMO2RjJAeALPWxEA+Y/Hzy2YQlNTzfmlfv7dK90e1wCd1q3rD bGvMq44HGGOpqs0ki4/jVi5/6oIGs8i4cTP0tcUtbwOhJ3DWV0oZTLhmXy3OFr3pXnmwqPs1dfJXu TU5wR4v2JNmBG1lYva9sNNdPPZgdOzcmGBRXCOO48MVIhYe/ki1uMApWXOI5auEjFunzE3XTMwoGH OZqkJTNzs+9ZtFw+H6a7AHhX+GxW8tHVQT56EJoHVHNbTARJkQxLZdbD7x7EIHi0Bp6BFRmtxqDH2 c67ib6V9A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jxjjJ-0005iU-2p; Tue, 21 Jul 2020 04:18:13 +0000 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jxjjF-0005hp-Q6 for linux-arm-kernel@lists.infradead.org; Tue, 21 Jul 2020 04:18:11 +0000 Received: by mail-pg1-x544.google.com with SMTP id j19so11322599pgm.11 for ; Mon, 20 Jul 2020 21:18:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=QhRkRM1m4qgsAo9eZ39otcpFvlPpO24W7mp8anbogBw=; b=bqhSzg4QOmAx8vDaoW+qgWApTlOrQm4qYUFBFPFk5eJ0mJKUnH+PBOy0rrPoVLuSvj G5nYm2pK68MYqj6x+TCFJB+f5jSKEM8VQr4aztw9FuTmTF+mMsEgmg/KgI32RN54P7bo Bn2WB2MaubV4PEisl9ole2vBWAje7/6zJzcOgvSaMXBVKWeu1m54/+zJ11AFQ9IAPZFj 91rrJTGVdFDuaP2F5FGqiWP3c4TtwHUsRxjZmJ/tHtgrRdyF4+P1bosWHGypJBEIDb/5 Aw3/2v/i3mKymhzPQPRC/tYJPoKK3JPXXYId/NMpEGilerA3j8tWesrhzp4XC8U4VWSl qwfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=QhRkRM1m4qgsAo9eZ39otcpFvlPpO24W7mp8anbogBw=; b=aJmunoY7eX0GOfWyVgaILHVH0l85hUCAgv0cv1quFuEK5jsm/b0eG3C9HrcwecG4kI U+L0QvSf0UtMVJRXpEGA6no1gb4lhZQNClCTz4SABGI+GZs7MOAMveM4I+c9gCgMCdOQ 5iOUn7SG6zpNma1U4T9iUWFmCKugmAYfvSJ8T8pAa65aIMXGoSXv2gpgxr05IDXUCZl4 CsvricPSxIEtoe015O1f8pjSWS0SV6xEUF3G4N3gM+3v1y3qLbrUzqitODE25PrdemdG BVZ7+rPjzWB6oWm7XDZZHGgEV9faD8DGO0hfSZM+b19TljaK/jX9llr386cNbqTxT1VL 5JUg== X-Gm-Message-State: AOAM531UrKgbVN7R6NR1+8Jlcbx5TV4D6EPuuFcgduHz7X102BFDd0GV DdDMx5U1fS7l3UkNL5arecQ= X-Google-Smtp-Source: ABdhPJx4iEq46mQUJiDEqXIx/wrx7qJP7HCND86/POVGHX0/iSnb09hQdnlfUa/C3gaz4EjedXYGBg== X-Received: by 2002:aa7:9a84:: with SMTP id w4mr21905832pfi.13.1595305088045; Mon, 20 Jul 2020 21:18:08 -0700 (PDT) Received: from localhost.localdomain ([2409:10:2e40:5100:6e29:95ff:fe2d:8f34]) by smtp.gmail.com with ESMTPSA id e28sm18467270pfm.177.2020.07.20.21.18.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jul 2020 21:18:07 -0700 (PDT) From: Sergey Senozhatsky To: Will Deacon , Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose Subject: [RFC][PATCH 2/4] arm64: add guest pvstate support Date: Tue, 21 Jul 2020 13:17:40 +0900 Message-Id: <20200721041742.197354-3-sergey.senozhatsky@gmail.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200721041742.197354-1-sergey.senozhatsky@gmail.com> References: <20200721041742.197354-1-sergey.senozhatsky@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200721_001809_960522_0E148452 X-CRM114-Status: GOOD ( 21.88 ) X-Spam-Score: -0.2 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.2 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:544 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [sergey.senozhatsky[at]gmail.com] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: joelaf@google.com, linux-kernel@vger.kernel.org, Sergey Senozhatsky , suleiman@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org PV-state is a per-CPU struct, which, for the time being, holds boolean `preempted' vCPU state. During the startup, given that host supports PV-state, each guest vCPU sends a pointer to its per-CPU variable to the host as a payload with the SMCC HV call, so that host can update vCPU state when it puts or loads vCPU. This has impact on the guest's scheduler - it does check the state of the vCPU it wants to run a task on: [..] wake_up_process() try_to_wake_up() select_task_rq_fair() available_idle_cpu() vcpu_is_preempted() Some sched benchmarks data is available on the github page [0]. [0] https://github.com/sergey-senozhatsky/arm64-vcpu_is_preempted Signed-off-by: Sergey Senozhatsky --- arch/arm64/include/asm/paravirt.h | 15 ++++ arch/arm64/kernel/Makefile | 2 +- arch/arm64/kernel/paravirt-state.c | 117 +++++++++++++++++++++++++++++ arch/arm64/kernel/paravirt.c | 4 +- arch/arm64/kernel/time.c | 1 + 5 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 arch/arm64/kernel/paravirt-state.c diff --git a/arch/arm64/include/asm/paravirt.h b/arch/arm64/include/asm/paravirt.h index cf3a0fd7c1a7..1bf164b2041b 100644 --- a/arch/arm64/include/asm/paravirt.h +++ b/arch/arm64/include/asm/paravirt.h @@ -7,12 +7,22 @@ struct static_key; extern struct static_key paravirt_steal_enabled; extern struct static_key paravirt_steal_rq_enabled; +struct pvstate_vcpu_info { + bool preempted; + u8 reserved[63]; +}; + +struct pv_state_ops { + bool (*vcpu_is_preempted)(int cpu); +}; + struct pv_time_ops { unsigned long long (*steal_clock)(int cpu); }; struct paravirt_patch_template { struct pv_time_ops time; + struct pv_state_ops state; }; extern struct paravirt_patch_template pv_ops; @@ -22,10 +32,15 @@ static inline u64 paravirt_steal_clock(int cpu) return pv_ops.time.steal_clock(cpu); } +bool native_vcpu_is_preempted(int cpu); +bool paravirt_vcpu_is_preempted(int cpu); + +int __init pv_state_init(void); int __init pv_time_init(void); #else +#define pv_state_init() do {} while (0) #define pv_time_init() do {} while (0) #endif // CONFIG_PARAVIRT diff --git a/arch/arm64/kernel/Makefile b/arch/arm64/kernel/Makefile index 5fb9b728459b..18974d5e798d 100644 --- a/arch/arm64/kernel/Makefile +++ b/arch/arm64/kernel/Makefile @@ -48,7 +48,7 @@ obj-$(CONFIG_ARMV8_DEPRECATED) += armv8_deprecated.o obj-$(CONFIG_ACPI) += acpi.o obj-$(CONFIG_ACPI_NUMA) += acpi_numa.o obj-$(CONFIG_ARM64_ACPI_PARKING_PROTOCOL) += acpi_parking_protocol.o -obj-$(CONFIG_PARAVIRT) += paravirt.o +obj-$(CONFIG_PARAVIRT) += paravirt.o paravirt-state.o obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o obj-$(CONFIG_HIBERNATION) += hibernate.o hibernate-asm.o obj-$(CONFIG_KEXEC_CORE) += machine_kexec.o relocate_kernel.o \ diff --git a/arch/arm64/kernel/paravirt-state.c b/arch/arm64/kernel/paravirt-state.c new file mode 100644 index 000000000000..4ae92a84c73d --- /dev/null +++ b/arch/arm64/kernel/paravirt-state.c @@ -0,0 +1,117 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#define pr_fmt(fmt) "arm-pvstate: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +static DEFINE_PER_CPU(struct pvstate_vcpu_info, vcpus_states); + +bool native_vcpu_is_preempted(int cpu) +{ + return false; +} + +static bool pv_vcpu_is_preempted(int cpu) +{ + struct pvstate_vcpu_info *st; + + st = &per_cpu(vcpus_states, cpu); + return READ_ONCE(st->preempted); +} + +bool paravirt_vcpu_is_preempted(int cpu) +{ + return pv_ops.state.vcpu_is_preempted(cpu); +} + +static bool has_pvstate(void) +{ + struct arm_smccc_res res; + + /* To detect the presence of PV time support we require SMCCC 1.1+ */ + if (arm_smccc_1_1_get_conduit() == SMCCC_CONDUIT_NONE) + return false; + + arm_smccc_1_1_invoke(ARM_SMCCC_ARCH_FEATURES_FUNC_ID, + ARM_SMCCC_HV_PV_STATE_FEATURES, + &res); + + if (res.a0 != SMCCC_RET_SUCCESS) + return false; + return true; +} + +static int __pvstate_cpu_hook(unsigned int cpu, int event) +{ + struct arm_smccc_res res; + struct pvstate_vcpu_info *st; + + st = &per_cpu(vcpus_states, cpu); + arm_smccc_1_1_invoke(event, virt_to_phys(st), &res); + if (res.a0 != SMCCC_RET_SUCCESS) + return -EINVAL; + return 0; +} + +static int pvstate_cpu_init(unsigned int cpu) +{ + int ret = __pvstate_cpu_hook(cpu, ARM_SMCCC_HV_PV_STATE_INIT); + + if (ret) + pr_warn("Unable to ARM_SMCCC_HV_PV_STATE_INIT\n"); + return ret; +} + +static int pvstate_cpu_release(unsigned int cpu) +{ + int ret = __pvstate_cpu_hook(cpu, ARM_SMCCC_HV_PV_STATE_RELEASE); + + if (ret) + pr_warn("Unable to ARM_SMCCC_HV_PV_STATE_RELEASE\n"); + return ret; +} + +static int pvstate_register_hooks(void) +{ + int ret; + + ret = cpuhp_setup_state(CPUHP_AP_ARM_KVMPV_STARTING, + "hypervisor/arm/pvstate:starting", + pvstate_cpu_init, + pvstate_cpu_release); + if (ret < 0) + pr_warn("Failed to register CPU hooks\n"); + return ret; +} + +static int __pvstate_init(void) +{ + return pvstate_register_hooks(); +} + +int __init pv_state_init(void) +{ + int ret; + + if (!has_pvstate()) + return 0; + + ret = __pvstate_init(); + if (ret) + return ret; + + pv_ops.state.vcpu_is_preempted = pv_vcpu_is_preempted; + return 0; +} diff --git a/arch/arm64/kernel/paravirt.c b/arch/arm64/kernel/paravirt.c index 295d66490584..3fec7563ac27 100644 --- a/arch/arm64/kernel/paravirt.c +++ b/arch/arm64/kernel/paravirt.c @@ -26,7 +26,9 @@ struct static_key paravirt_steal_enabled; struct static_key paravirt_steal_rq_enabled; -struct paravirt_patch_template pv_ops; +struct paravirt_patch_template pv_ops = { + .state.vcpu_is_preempted = native_vcpu_is_preempted, +}; EXPORT_SYMBOL_GPL(pv_ops); struct pv_time_stolen_time_region { diff --git a/arch/arm64/kernel/time.c b/arch/arm64/kernel/time.c index eebbc8d7123e..50c55792f72b 100644 --- a/arch/arm64/kernel/time.c +++ b/arch/arm64/kernel/time.c @@ -68,4 +68,5 @@ void __init time_init(void) lpj_fine = arch_timer_rate / HZ; pv_time_init(); + pv_state_init(); }