Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp239056lqr; Wed, 5 Jun 2024 04:55:20 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVZKs7nxef9ueMqZVLUDyFZuWa8w1p9qvY59LWyGjyfpFaPqxJaR4jxS09lLmM3AMNdMkhLtL/nY/lX+gcgT6Wg0WiCLpzBqkUUgWRwbA== X-Google-Smtp-Source: AGHT+IHhKDBZK8d4gR6bZzYxy1lw3A6htNygSZTj3xrUMhhXB3q5SMmw+IDfLDb0ld3PfWs9c5wO X-Received: by 2002:a17:906:f6c6:b0:a68:f3ac:cae9 with SMTP id a640c23a62f3a-a69549c8167mr447465866b.33.1717588520369; Wed, 05 Jun 2024 04:55:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717588520; cv=pass; d=google.com; s=arc-20160816; b=cCbrmMnJIns908HVGp3fQppZZrYHPpZCPQIWT3OZKuROutglPGRiIx6KFnTZAtlyfC mYNkuUASuCrwC2dvqggEV8Q6Wi2M+Nj4Ty/Yp/GXG7JPwch06kVxXi07kZN1p9/gZAr/ 8JWMII7AXaEWJcOMe9mbvz/FqwEJJCmKCuyCqoDEUS0yz18hz44k113JS8GxaN4cb2dU nJhTy3rR5b03Qlq0gLpYsUwrtsNmfKu7sPFflASw9ozA+7jkvUE52SHeOilKyNn8l5Z+ X4sLO8/2gHUO7HIK6NgT9gZEBr782Mlm+7Y+omVNQmkyike0dw5gSvq0uTNcVGI8JRHI YmJA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :subject:date:from:dkim-signature; bh=Opd3A7s84qlzwKyOZlIUZ/pQ0b5O9yKQ/e+oIiMrphg=; fh=hBZNaBlMLRepPz1w2wBf8TAACz89vdGFJHmZGymLCoU=; b=RWuJgJave28lCQLrsUUqQ3s1GgPgSJB5eNBvvAzIPskR1COVPW2Wg3RcAR/X+tstpi Gn5Df/AqYqiZ+VCcEaNsQI7FUbzkWI+eFavOCOcZaZo1+FHmrelr9clYg/c+7vf5ah7k 0w2vRhmxobWMhn7rGmaAnug6i5ZoBQ3qMvotC49PFn7sGm8OEx/unxrPEQOAtPmnXs/d fGK+DW8yePirfyko7hGon9HTkBWZXVNxZFPn6JBpQ+GLKU/D+hbSLeMs6p/t0BZ7VgnK JrZeTlHEExzXfJO0mkZmqYCrFgGS3NTsXso1Ie+sgmdF2qEuz6zhbRQAy2hqhvYzir9Z cI0Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LAlnpnZY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202321-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202321-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a68e8bb2875si389754366b.741.2024.06.05.04.55.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 04:55:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202321-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LAlnpnZY; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202321-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202321-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E156B1F21BDF for ; Wed, 5 Jun 2024 11:55:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B4DA71990BA; Wed, 5 Jun 2024 11:50:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LAlnpnZY" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B06A713791C; Wed, 5 Jun 2024 11:50:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717588215; cv=none; b=eFFg/7qbVhU8yBQVimXWlNGalT14z4/KvGR0uhoUjkg3eeHuDsD2JoKuX2fnIPmfR7VbGKG00+D7ZRqAfvBTkJa26sQruB+xQg2wvCdPeVPaAnXwQcwYX0LinoSquxN/lAGhnFVW6f+bcS20poM8XIP53TtCuc+hgZl2kG+BUFM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717588215; c=relaxed/simple; bh=I+qiD9f7AIjkeTzE7i3sCyRtTk9/3BCUmNFuB2K/uis=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dilUVyDmKS/u7J+un8PhmewZ3mT+wxs1q+jsO8OKTzeAVPQdZVlDRdxbLoIWWdBFtbwJr6tJ3T4orDZDyXZrkG+CxJUpFgd/IioWurLEyK+TDiCulOIgXG6vosS3sX55dG6HVyojV04QCFmn+Q5OcVSfdFkeWXhk4yzzM788+cA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LAlnpnZY; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 445BEC4AF08; Wed, 5 Jun 2024 11:50:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717588215; bh=I+qiD9f7AIjkeTzE7i3sCyRtTk9/3BCUmNFuB2K/uis=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LAlnpnZYLXMLUpTvG3MQVyE+K6rpEEFqw4vUwMUmPLD5xDfHwoSWshyluK3r9vLSi Pkm44HYBiFqaMrp7zVwDhsImoH0ogYo56Quk5PxLqo0wbmwHuUgNCNg4pTBVsNHNV6 nn76CiUxAiOa2D8tyHhG9HR3rLSNCvToPBwiTWCYN+BTdfHKk76Jslemkq3As8asPJ UN2UzbEgOlPfgJorsQQrZktTxjFQ7lOVcdpwMwscEcBOWIHo6mqfIBYX+yDsj7TI3O IiG4zT7SSzpv2LSo3tIRcShRBwZaLncdu2Mi1iZBO7YzThWu3m1spjTroRC0m8qO60 sBneQuG06drFg== From: Mark Brown Date: Wed, 05 Jun 2024 12:41:30 +0100 Subject: [PATCH 4/4] KVM: arm64: Avoid underallocating storage for host SVE state Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20240605-kvm-arm64-fix-pkvm-sve-vl-v1-4-680d6b43b4c1@kernel.org> References: <20240605-kvm-arm64-fix-pkvm-sve-vl-v1-0-680d6b43b4c1@kernel.org> In-Reply-To: <20240605-kvm-arm64-fix-pkvm-sve-vl-v1-0-680d6b43b4c1@kernel.org> To: Catalin Marinas , Will Deacon , Marc Zyngier , Oliver Upton , James Morse , Suzuki K Poulose , Fuad Tabba Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kvmarm@lists.linux.dev, Mark Brown X-Mailer: b4 0.14-dev-d4707 X-Developer-Signature: v=1; a=openpgp-sha256; l=4775; i=broonie@kernel.org; h=from:subject:message-id; bh=I+qiD9f7AIjkeTzE7i3sCyRtTk9/3BCUmNFuB2K/uis=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmYFDpNLP6xsTXnP4x+K63OmWAJYiakPHrQkrNYpo1 ZXtEslWJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZmBQ6QAKCRAk1otyXVSH0K9cB/ 49BkO45ApDn9aeWm8mUJ7CZsd+CKe1eEiMHrx3B2uFlNMFMa2p6dYhjR3kVwQJBgSKZNTsBSvfQW/b lTwH0VLX38XW3mnZDcisC9jPnwSe1OXSdaBnrR/DUTEgYr0AHHg3TGVcRFlGADgEBFxK1NUz1M5A/v EtS9jcxTuzkrKolSa7nRDIPjICi0nC1riWydKf5Gy4O9vVWybhQKBoG7cQx+4AmvwkISFmxvU+Kqz6 dE58oIqi3KKMKri4kd7H3KWfrX8FI8zcdV/J+NsVvhZ06HCMP1oFWVxun2qOoHHYxlPhxFgVeCcLSk K0XMNgb6L16jUyGCpHiZSmMb3F/z9m X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB We size the allocation for the host SVE state using the maximum VL shared by all CPUs in the host. As observed during review on an asymmetric system this may be less than the maximum VL supported on some of the CPUs. Since the pKVM hypervisor saves and restores the host state using the maximum VL for the current CPU this may lead to buffer overflows, fix this by changing pKVM to use the maximum VL for any CPU to size allocations and limit host configurations. Fixes: 66d5b53e20a6 ("KVM: arm64: Allocate memory mapped at hyp for host sve state in pKVM") Signed-off-by: Mark Brown --- arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/include/asm/kvm_hyp.h | 2 +- arch/arm64/include/asm/kvm_pkvm.h | 2 +- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 ++-- arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +- arch/arm64/kvm/reset.c | 6 +++--- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 36b8e97bf49e..a28fae10596f 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -76,7 +76,7 @@ static inline enum kvm_mode kvm_get_mode(void) { return KVM_MODE_NONE; }; DECLARE_STATIC_KEY_FALSE(userspace_irqchip_in_use); extern unsigned int __ro_after_init kvm_sve_max_vl; -extern unsigned int __ro_after_init kvm_host_sve_max_vl; +extern unsigned int __ro_after_init kvm_host_sve_max_cpu_vl; int __init kvm_arm_init_sve(void); u32 __attribute_const__ kvm_target_cpu(void); diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h index 7510383d78a6..47426df69875 100644 --- a/arch/arm64/include/asm/kvm_hyp.h +++ b/arch/arm64/include/asm/kvm_hyp.h @@ -144,6 +144,6 @@ extern u64 kvm_nvhe_sym(id_aa64smfr0_el1_sys_val); extern unsigned long kvm_nvhe_sym(__icache_flags); extern unsigned int kvm_nvhe_sym(kvm_arm_vmid_bits); -extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_vl); +extern unsigned int kvm_nvhe_sym(kvm_host_sve_max_cpu_vl); #endif /* __ARM64_KVM_HYP_H__ */ diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm_pkvm.h index cd56acd9a842..6fc0cf42fca3 100644 --- a/arch/arm64/include/asm/kvm_pkvm.h +++ b/arch/arm64/include/asm/kvm_pkvm.h @@ -134,7 +134,7 @@ static inline size_t pkvm_host_sve_state_size(void) return 0; return size_add(sizeof(struct cpu_sve_state), - SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_vl))); + SVE_SIG_REGS_SIZE(sve_vq_from_vl(kvm_host_sve_max_cpu_vl))); } #endif /* __ARM64_KVM_PKVM_H__ */ diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c index bd8f671e848c..d232775b72c9 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c +++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c @@ -90,8 +90,8 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu) hyp_vcpu->vcpu.arch.ctxt = host_vcpu->arch.ctxt; hyp_vcpu->vcpu.arch.sve_state = kern_hyp_va(host_vcpu->arch.sve_state); - /* Limit guest vector length to the maximum supported by the host. */ - hyp_vcpu->vcpu.arch.sve_max_vl = min(host_vcpu->arch.sve_max_vl, kvm_host_sve_max_vl); + /* Limit guest vector length to the maximum supported by any CPU. */ + hyp_vcpu->vcpu.arch.sve_max_vl = min(host_vcpu->arch.sve_max_vl, kvm_host_sve_max_cpu_vl); hyp_vcpu->vcpu.arch.hw_mmu = host_vcpu->arch.hw_mmu; diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c index 95cf18574251..08e825de09d1 100644 --- a/arch/arm64/kvm/hyp/nvhe/pkvm.c +++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c @@ -18,7 +18,7 @@ unsigned long __icache_flags; /* Used by kvm_get_vttbr(). */ unsigned int kvm_arm_vmid_bits; -unsigned int kvm_host_sve_max_vl; +unsigned int kvm_host_sve_max_cpu_vl; /* * Set trap register values based on features in ID_AA64PFR0. diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 3fc8ca164dbe..59cccb477cf3 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -32,7 +32,7 @@ /* Maximum phys_shift supported for any VM on this host */ static u32 __ro_after_init kvm_ipa_limit; -unsigned int __ro_after_init kvm_host_sve_max_vl; +unsigned int __ro_after_init kvm_host_sve_max_cpu_vl; /* * ARMv8 Reset Values @@ -52,8 +52,8 @@ int __init kvm_arm_init_sve(void) { if (system_supports_sve()) { kvm_sve_max_vl = sve_max_virtualisable_vl(); - kvm_host_sve_max_vl = sve_max_vl(); - kvm_nvhe_sym(kvm_host_sve_max_vl) = kvm_host_sve_max_vl; + kvm_host_sve_max_cpu_vl = sve_max_cpu_vl(); + kvm_nvhe_sym(kvm_host_sve_max_cpu_vl) = kvm_host_sve_max_cpu_vl; /* * The get_sve_reg()/set_sve_reg() ioctl interface will need -- 2.39.2