From patchwork Wed Nov 4 18:36:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 11881909 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 09448C2D0A3 for ; Wed, 4 Nov 2020 18:42:13 +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 7C605206C1 for ; Wed, 4 Nov 2020 18:42:12 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="UNg2u/Fc"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="il549hLI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7C605206C1 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.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=AsGdxrgjlttRQjOc+7NdGlRcAuIZz+Of+Nc3xvV/kGQ=; b=UNg2u/FcVjjCF9FnVuJ3sKwc7 TCBXyCPn5fiPIf4Urrf93GzLGIGhLhvUh8+mGU1okrIIdXgU71L+hZw9Br36cnQjE0vlCI9GWb2ao urD4OvbhgG/Ic8m1x6GhJO5L5dfbuk3/QK5hovwk4PQcv0/5CmPqEENGc2M/TezIHluRCMH0Eukjr 0dgB2IWx0QJD5/RvzJ2jUbivseaVx8g7WS74fTdWxrktwBBb5Q84R96PVvwonzG4jVbGeYdFdxNK5 TqG8dhsBN9rfQID8oKUaolOxVKs0eqpc8ePqtNGIf1ONYDaC8l/XNvAy+3qBXc1ZZcce6QY/eRaCm fhEl9n0wg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaNix-0004a8-Lw; Wed, 04 Nov 2020 18:41:35 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaNf5-000203-Uz for linux-arm-kernel@lists.infradead.org; Wed, 04 Nov 2020 18:37:36 +0000 Received: by mail-wr1-x444.google.com with SMTP id w14so23123623wrs.9 for ; Wed, 04 Nov 2020 10:37:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HMYGIc+ZZLbnQbcx34p2ubDooBhnfGZkiSjL1q0xiNQ=; b=il549hLIFQg0qGoV2QrtJtovxpI7mz6Lm9xqwFQAuBrKk0Z6AC8ta8m/IhticSSWAg nunkDYzvs4sOKQEUNrHD/WF3I2uhhw/LMXkFntRbcYdyktGXJhujOyKn7jXnmAQUZjKX HWqGa+gOsc0TyXjwlKX20YcgvfXtgqTp7D+0BzkajGuiEkOeV+lo+Wug+iAynKlmeoIF fBs0MAQU3p+DN2M/yPDkSBwhSnnCxMkEh3Gygat3MvnDjwWQd21yPXJWRfdjbn8kXXgF Ja1pWoR4q5WXTD7ryuVkLcK79AhJCwCunoJp8jfhSnsqvKNqPlNwzC4naVUk6tC/hjBw D7Iw== 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=HMYGIc+ZZLbnQbcx34p2ubDooBhnfGZkiSjL1q0xiNQ=; b=Qn4paxaOJbX8l/d8Hh8V2mkpTTDeihk7y6mu2v59wd4MKRcp2CprabVAJ6o/fCiBl6 mE0p/AjoPqOasFSXaKFuLyDN5+ksHx5zbqj3KWAXk7/xqrGUexPRKPVjZzYbNbdfASCy HOO1WUzIZL4820BZqX82Fp0IlnSoSg7DFdDabaNtarKP/l5lksh+Km/+rn1y0Bg7vCCQ qGfDqkh8gVmrZAjDGuLplCXTlAtmpLej6jFGDFpBUWEPuVtEgspFW4UVHETD6tSRIDUe xUsdV/92WTiPX1RuMPTPFJ7W5B76VEJiV58E4u08pkT0v8DOHuOvUUGPx+7B5gDY0jlB 7O8A== X-Gm-Message-State: AOAM530hS38YBkPxvemAs3jfttZeccRlnNAoN19FueWqCCnxfukt0+MJ yFq2uR4tOFouVHam01skp/UUXw== X-Google-Smtp-Source: ABdhPJzPIMW7rJz/s7+my12k8/M0wSzNOf/au+3oC9aJIIDx/rVVLNmLVAtHZQ7yu1CygOZyQlw0UQ== X-Received: by 2002:a5d:4a4b:: with SMTP id v11mr4973650wrs.364.1604515054453; Wed, 04 Nov 2020 10:37:34 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:c8d2:30f2:53c6:bc2]) by smtp.gmail.com with ESMTPSA id w11sm3705303wmg.36.2020.11.04.10.37.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 04 Nov 2020 10:37:33 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [RFC PATCH 26/26] kvm: arm64: Fix EL2 mode availability checks Date: Wed, 4 Nov 2020 18:36:30 +0000 Message-Id: <20201104183630.27513-27-dbrazdil@google.com> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201104183630.27513-1-dbrazdil@google.com> References: <20201104183630.27513-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201104_133736_076434_2F30F147 X-CRM114-Status: GOOD ( 18.98 ) 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: Mark Rutland , Lorenzo Pieralisi , kernel-team@android.com, Suzuki K Poulose , Marc Zyngier , Quentin Perret , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , David Brazdil , Will Deacon , Julien Thierry , Andrew Scull Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org With nVHE hyp code interception host's PSCI CPU_ON/OFF/SUSPEND SMCs, from the host's perspective new CPUs start booting in EL1 while previously they would have booted in EL2. The kernel logic which keeps track of the mode CPUs were booted in needs to be adjusted to account for this fact. Add a static key enabled if KVM nVHE initialization is successful. When the key is enabled, is_hyp_mode_available continues to report `true` because its users either treat it a check whether KVM will be / has been initialized, or whether stub HVCs can be made (eg. hibernate). is_hyp_mode_mismatched is changed to report `false` when the key is enabled. That's because all cores' modes matched at the point of KVM init and KVM will not allow cores not present at init to boot. That said, the function is never used after KVM is initialized. Signed-off-by: David Brazdil --- arch/arm64/include/asm/virt.h | 16 ++++++++++++++++ arch/arm64/kvm/arm.c | 5 +++++ 2 files changed, 21 insertions(+) diff --git a/arch/arm64/include/asm/virt.h b/arch/arm64/include/asm/virt.h index 6069be50baf9..597430b5f5aa 100644 --- a/arch/arm64/include/asm/virt.h +++ b/arch/arm64/include/asm/virt.h @@ -65,9 +65,18 @@ extern u32 __boot_cpu_mode[2]; void __hyp_set_vectors(phys_addr_t phys_vector_base); void __hyp_reset_vectors(void); +DECLARE_STATIC_KEY_FALSE(kvm_nvhe_initialized); + /* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available(void) { + /* + * If KVM nVHE is initialized, all CPUs must have been booted in EL2. + * Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && static_branch_unlikely(&kvm_nvhe_initialized)) + return true; + return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 && __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2); } @@ -75,6 +84,13 @@ static inline bool is_hyp_mode_available(void) /* Check if the bootloader has booted CPUs in different modes */ static inline bool is_hyp_mode_mismatched(void) { + /* + * If KVM nVHE is initialized, all CPUs must have been booted in EL2. + * Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && static_branch_unlikely(&kvm_nvhe_initialized)) + return false; + return __boot_cpu_mode[0] != __boot_cpu_mode[1]; } diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 3dff6af69eca..e93956d6235d 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -47,6 +47,8 @@ __asm__(".arch_extension virt"); #endif +DEFINE_STATIC_KEY_FALSE(kvm_nvhe_initialized); + DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); static DEFINE_PER_CPU(unsigned long, kvm_arm_hyp_stack_page); @@ -1841,6 +1843,9 @@ int kvm_arch_init(void *opaque) if (err) goto out_hyp; + if (!in_hyp_mode) + static_branch_enable(&kvm_nvhe_initialized); + if (in_hyp_mode) kvm_info("VHE mode initialized successfully\n"); else