Received: by 2002:ab2:6d45:0:b0:1fb:d597:ff75 with SMTP id d5csp238675lqr; Wed, 5 Jun 2024 04:54:23 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV3PID4oX6RWFI6jkXk3D7zZtLhE/5zxPHKQ+cAyNN87VT1KumM/QOYqEVC6hjXa3nf7QbKHwUsJW2wvnRTlRuXZBlvKt0v1ndh7noWqQ== X-Google-Smtp-Source: AGHT+IFZA06vUK8RIvUR5PMT7NJ7ZXGMfUlBe12fG3aslNATbpasquwMe0A141Xh6XGN/QpC0Jw9 X-Received: by 2002:a05:6a00:298c:b0:6ec:fdfe:9bde with SMTP id d2e1a72fcca58-703e59f17famr2461208b3a.25.1717588460530; Wed, 05 Jun 2024 04:54:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717588460; cv=pass; d=google.com; s=arc-20160816; b=zBlwJvUkXxgt7byOQG1sIDNGTwyL2wzzwKQ3o86TJCtUnHpqqecqYB60Q+eceZlMu2 0eksy5sgezMXMgJ9RiQNWOCF+PItrwkf00MJkPq2fMMJPbLdhlERv0e5g+ewUCghSfoF FYqgkKMPUBiyumyIoGKg6zgpiTYhB+mCe+ZaEv6OK6mjiLL5dPwAleYwnXPHM38Vj+cO dLEREKu6/vImnyUGoyKmiRRq8qoVcMy7aqP0RkPI0obN7zshGGsJrm7qQGX/w/KfUBVE ecg9jc9+3MURwqXqkM4+7lDE9vQMtXgHyxgI/CDLA0X9bdhzln+m/apgvwCLOeJp14Zo 12Og== 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=mIG1URlRnxs+BNSYUUJ4PuwI6vpHH0B1Ox8wvWEWnUM=; fh=hBZNaBlMLRepPz1w2wBf8TAACz89vdGFJHmZGymLCoU=; b=Zsv/Yv7a8vYS1IEgQFPuekN1a4SxjngkkfBrp9yiGTdhhKispUe3zT5F+dKUA3nxlM o+QQk6hOZPrDnS56nY0yCvVgTKAEnhaSvFiVlAuVV/UB+7dW5BsN1jCgVrPLlXUFLZ94 JP6CSub7b2F98K390MDE376UjDN0ciP3Rc0KFCOGGDuNjAexB8mHlEBTg0GJvdhWiEhJ DGIDM3OCbvtUanxXqoTiK2GmuXTPNiBk9PLc/oz/yc58+9s3ZWODA7uJ7viUl9O7Kn67 NWLkaeuIVtVfYemYye86PCgr3jC7kF641mnR8zsLIXJ6DXxKRJF+WZz4sVzApClfJLPP c0PA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JTc6iI5X; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202316-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202316-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id 41be03b00d2f7-6c35c73b82asi4033554a12.730.2024.06.05.04.54.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Jun 2024 04:54:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202316-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JTc6iI5X; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-202316-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202316-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 21C7628F83B for ; Wed, 5 Jun 2024 11:54:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D21C197504; Wed, 5 Jun 2024 11:50:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="JTc6iI5X" 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 66F23194143; Wed, 5 Jun 2024 11:50:10 +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=1717588210; cv=none; b=DpRw+gyJjxqVlyLQYsfJfEm3TM3ehQDBcoxXI/7VVv/9JopFmzkoi1ZxcTKbRJ5KFH+Ci2rRB7RJK90VQYxcyQxJTD/1eNnzwhMOytINdLZoW/e7opjOH85ZV5NWiENCCMTpRdsIAMvKKU85q2yhxs/9gSf54xxxJVZ4eNPpVNQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717588210; c=relaxed/simple; bh=0HRuKE8VlXUba+j3EwHzj90KT4DQxSJsorHaMRo2+XQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RUwOhn1qTlAO5N1kul6YytgyN7kwDyqWKPBJ82KYThZh0IMj/9UUJvlOmNJGInDuk3nXT92Ry1z0liyeRYIcBy2ECydTeFB1BnDA+zgs/+/BKbxdjLDLWYxgIL6vXafhOz8K77rZLmcQ5SbMMwskoShTaOMRsgdpFAQm7a/TnC4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=JTc6iI5X; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB49BC4AF0A; Wed, 5 Jun 2024 11:50:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717588210; bh=0HRuKE8VlXUba+j3EwHzj90KT4DQxSJsorHaMRo2+XQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=JTc6iI5Xt2gKkG6jzOLpgp9JosGSx32VAglx5WX3G2X2sRgBDpIM9ChF7WDq+GHBE cturpVQV9rI/nKk+lShBopKjb0v5c7jNQQg6dxIbtOwJOoFpwsTdJKK4zDXwSi2RYE s7RwqJHwqfFZRqhzLC73vkblm98Xz/h716R6kZBid7jMKLP4LjLOgwz2yd6O61Shq5 3b3ODe5E3QJwAZkDUEL/YgW7C45P8d6OGVZ27cDG0WLUZT+TpXAJvBFO2h5fBdZAyX V6SRCgojTr+9u+C9Rt2EuGDBTpp3Yk65p6TPM9ZhkGb9kf6+Jlb9o9U1IOKlhINLj8 8LAa8X/N9sTWg== From: Mark Brown Date: Wed, 05 Jun 2024 12:41:28 +0100 Subject: [PATCH 2/4] arm64/fpsimd: Discover maximum vector length implemented by any CPU 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-2-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=4299; i=broonie@kernel.org; h=from:subject:message-id; bh=0HRuKE8VlXUba+j3EwHzj90KT4DQxSJsorHaMRo2+XQ=; b=owEBbQGS/pANAwAKASTWi3JdVIfQAcsmYgBmYFDniQKKB3lNFxU6WDccvOf/Ww+JlNUgz3udSCRy bnFnw/uJATMEAAEKAB0WIQSt5miqZ1cYtZ/in+ok1otyXVSH0AUCZmBQ5wAKCRAk1otyXVSH0OqyB/ wNfv5HhFD0WNmHYdHM/Rb5yPlCnrAftBkUrzdEtnR5ro/4hHSlplNLYs57As2SqP7e8hwPe0TYSYFk DhvCSELsGfkDFn7PYz+os1aOPgFGHO0SVZ/pJuFLC/ZqI9argK5wg7VoQHZUP9yAy7N3ji7kC5KxLr 8iEa8CzZaIdaMX90p0o4RMO2D7kwv0A0k5VLLaQHZDEUGbbJARn+KBR+huwM3AFw0dvSYc+VkHCc23 po3tNZVqfEeB3xHfXKK2OMkY+NDfGRbSX24mznjzplDD1VlVAFO3O28BAe6S+zYU3o0BBPituikoEH Ae+vh9utlClmvraRzvOBzvbt361e0i X-Developer-Key: i=broonie@kernel.org; a=openpgp; fpr=3F2568AAC26998F9E813A1C5C3F436CA30F5D8EB When discovering the vector lengths for SVE and SME we do not currently record the maximum VL supported on any individual CPU. This is expected to be the same for all CPUs but the architecture allows asymmetry, if we do encounter an asymmetric system then some CPUs may support VLs higher than the maximum Linux will use. Since the pKVM hypervisor needs to support saving and restoring anything the host can physically set it needs to know the maximum value any CPU could have, add support for enumerating it and validation for late CPUs. Signed-off-by: Mark Brown --- arch/arm64/include/asm/fpsimd.h | 13 +++++++++++++ arch/arm64/kernel/fpsimd.c | 26 +++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/fpsimd.h b/arch/arm64/include/asm/fpsimd.h index 51c21265b4fa..cd19713c9deb 100644 --- a/arch/arm64/include/asm/fpsimd.h +++ b/arch/arm64/include/asm/fpsimd.h @@ -188,6 +188,9 @@ struct vl_info { int max_vl; int max_virtualisable_vl; + /* Maximum vector length observed on any CPU */ + int max_cpu_vl; + /* * Set of available vector lengths, * where length vq encoded as bit __vq_to_bit(vq): @@ -278,6 +281,11 @@ static inline int vec_max_virtualisable_vl(enum vec_type type) return vl_info[type].max_virtualisable_vl; } +static inline int vec_max_cpu_vl(enum vec_type type) +{ + return vl_info[type].max_cpu_vl; +} + static inline int sve_max_vl(void) { return vec_max_vl(ARM64_VEC_SVE); @@ -288,6 +296,11 @@ static inline int sve_max_virtualisable_vl(void) return vec_max_virtualisable_vl(ARM64_VEC_SVE); } +static inline int sve_max_cpu_vl(void) +{ + return vec_max_cpu_vl(ARM64_VEC_SVE); +} + /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */ static inline bool vq_available(enum vec_type type, unsigned int vq) { diff --git a/arch/arm64/kernel/fpsimd.c b/arch/arm64/kernel/fpsimd.c index 22542fb81812..27f3593547f1 100644 --- a/arch/arm64/kernel/fpsimd.c +++ b/arch/arm64/kernel/fpsimd.c @@ -129,6 +129,7 @@ __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX] = { .min_vl = SVE_VL_MIN, .max_vl = SVE_VL_MIN, .max_virtualisable_vl = SVE_VL_MIN, + .max_cpu_vl = SVE_VL_MIN, }, #endif #ifdef CONFIG_ARM64_SME @@ -1041,8 +1042,13 @@ static void vec_probe_vqs(struct vl_info *info, void __init vec_init_vq_map(enum vec_type type) { struct vl_info *info = &vl_info[type]; + unsigned long b; + vec_probe_vqs(info, info->vq_map); bitmap_copy(info->vq_partial_map, info->vq_map, SVE_VQ_MAX); + + b = find_first_bit(info->vq_map, SVE_VQ_MAX); + info->max_cpu_vl = __bit_to_vl(b); } /* @@ -1054,11 +1060,16 @@ void vec_update_vq_map(enum vec_type type) { struct vl_info *info = &vl_info[type]; DECLARE_BITMAP(tmp_map, SVE_VQ_MAX); + unsigned long b; vec_probe_vqs(info, tmp_map); bitmap_and(info->vq_map, info->vq_map, tmp_map, SVE_VQ_MAX); bitmap_or(info->vq_partial_map, info->vq_partial_map, tmp_map, SVE_VQ_MAX); + + b = find_first_bit(tmp_map, SVE_VQ_MAX); + if (__bit_to_vl(b) > info->max_cpu_vl) + info->max_cpu_vl = __bit_to_vl(b); } /* @@ -1069,9 +1080,10 @@ int vec_verify_vq_map(enum vec_type type) { struct vl_info *info = &vl_info[type]; DECLARE_BITMAP(tmp_map, SVE_VQ_MAX); - unsigned long b; + unsigned long b, max_vl; vec_probe_vqs(info, tmp_map); + max_vl = __bit_to_vl(find_first_bit(tmp_map, SVE_VQ_MAX)); bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX); if (bitmap_intersects(tmp_map, info->vq_map, SVE_VQ_MAX)) { @@ -1083,6 +1095,18 @@ int vec_verify_vq_map(enum vec_type type) if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available()) return 0; + /* + * pKVM allocates and uses storage for host state based on the + * largest per-PE VL, reject new PEs with a larger maximum. + */ + if (is_protected_kvm_enabled()) { + if (max_vl > info->max_cpu_vl) { + pr_warn("%s: cpu%d: would increase maximum VL\n", + info->name, smp_processor_id()); + return -EINVAL; + } + } + /* * For KVM, it is necessary to ensure that this CPU doesn't * support any vector length that guests may have probed as -- 2.39.2