Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp1111470lqo; Fri, 17 May 2024 10:59:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWslvQsLyS38Q5RvELmIY9Xr9EW3qmEipXkuMcbzQAIW0F+kt5Nj3YntYM+ZhkHMssrgHL+cWFneBQ+2uW+jkbxkQEdFSEqe1fvy/SAZA== X-Google-Smtp-Source: AGHT+IFv5lSS1323+I1eZA5kvpqoyvl5NSgoU+wxhuw9cjNZZaGHS88kT6JDfj12DCDyylW1hx5m X-Received: by 2002:a05:6a21:2727:b0:1a3:e2c4:956e with SMTP id adf61e73a8af0-1afde0d54e0mr22076277637.25.1715968782517; Fri, 17 May 2024 10:59:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715968782; cv=pass; d=google.com; s=arc-20160816; b=g+OD3LGnlybW+so1+OkjDvVyACUzkWQGhRZfPBU7lPMhPCNAlrJ2WidPTRQ3GIa6UP 0/884jaujlN5lpiPuAqifAp9ydMAyS/1RjEmHLfHfMZstVslNkZjlWNnZvHWq1UOhTYS GOkcj9D70E4GgMu1fSdRlZ5w6QS3n2n3t2W1ymfp72tgHUOxgVNQxIk5lCj7V/r9mTId tcff71niJ0bAuP14nKZOPDipHQh0sFgvSdRuDMEs2VPqpQuLTuKxqmYC9J6V319t/p8E uWPkWdyYuZc0bptD43T/1ne5RV1A5B8VBwkHdQWVaz6PCXCce3qo3wcWZFRwcM4PqBkF qn5A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=yZo8Omcfrrzpm7znpVQTWfAM28fGARZSq2t82c2wUfM=; fh=VnPNHiLNHjZ1QrLP9SGVUvA526gUWwSJWhfqRG3C9bQ=; b=F+N4LnOmjio0gSP0fLuwh/zJ2+VK6u6py1x7PqR4Q7tEdl9/S09lS3uNOwfBSEmWXL 5jVbi4pcTLCs1DfzSMRewNlSH1dzxM3TY6j+ejT6KA9fJnSPwvZ/+9hWuTzLYMulJoHR e1JiWnvL4IS7yQDQL7pDrqcyZL2ZIShlumr+2rkzDIhY+fLNyEKYCFMvzgWi24W3XZPO j/1/4JKQNnHU54JLs2uMBWqbIp32bezz9/iUSOe9bd0yldM/UF9PA0/VIcwo1ryOeLm8 rR8f/Qk1jZSRyadGn39gUaykykOACE8jOcDNKDWhxTTDz2mFVbivIGlfM8vo5B0d2n7N vS6g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ix1DwjJO; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-182456-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182456-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d2e1a72fcca58-6f4d2b41a36si17524408b3a.328.2024.05.17.10.59.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 10:59:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-182456-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Ix1DwjJO; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-182456-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182456-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 4E98528A1D6 for ; Fri, 17 May 2024 17:47:03 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 37DCF14F9F5; Fri, 17 May 2024 17:40:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ix1DwjJO" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C38FC14F114 for ; Fri, 17 May 2024 17:40:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715967630; cv=none; b=gxvVj/4auX3AbPHnORr49BMCyfPHZE40biKsq1iqxqlr+K9sacWTGO5QU6biXtnSQMBUR4mjEl3Y00+7RxMJN8otwZOtjeo5SrOPPuka9dMKYfxS/wOVen7+DjAHfVYVFrXLSzoMHGAX5UqTZOwCJaYhcqXsktAlysJKCcDlzqY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715967630; c=relaxed/simple; bh=wWIigWH75I8A7NtCSPNqBEenhjFRXun0UIOqrvSwPfo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rCGZqEtoM6DAuo2Vi9p62Lp2IiLTaw/vBI7UBntMCM+bB23hCIcxsCOryVyvlogsfXsb33mIOn6/+js0tbRVPS0OYuzAuLQRyyF+5u574SXSdDKeQFmIHwffwuKYCrloeiqGjgWzdlI+/YRP33eMFCLfGL5Zca47SC5sc/gLbrc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ix1DwjJO; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-61e0c1f7169so123941357b3.0 for ; Fri, 17 May 2024 10:40:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715967628; x=1716572428; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=yZo8Omcfrrzpm7znpVQTWfAM28fGARZSq2t82c2wUfM=; b=Ix1DwjJOp/AOI+3ytVL36QXRf46yHUtn0svCUVFLNH0pI7kAYHi/zCsmVDf5HexpeB PvQizOruYskb/un9e4enmM1+b0D0iV99Ru27GyhKj8+fQPZK9ifX9YdfrMgusLFnP63o pcCSxkFaUin6H4HFLK+Uudu9SUXkdA1MJt2QLyCTjH7yC+tQ/tfJYT/PyrjaDmCz2yh1 nBzei4oE4UkCdiyt1cdyxSrT5FBVsZ0/oyrVwzYjZFlrBD2x02CPmkcGwIV9AxG838Zh jetWlKrgW1WPSLRKnh3SpnJQHqlbEy+HIHpJsUP8gWHwBVXHxE0sbOhaQozS9ryRuq8h MP7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715967628; x=1716572428; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yZo8Omcfrrzpm7znpVQTWfAM28fGARZSq2t82c2wUfM=; b=S+fl+7vxJwM/uAx7WxhbQfLqJDGMhOGfDH57pOsfz5RgDYWT7SpK7DpD1S1s0ShY8W pBvh6i2PQ88j4Cqo7ctmdWIpTv2S5upMdTGpU4EtvtPLOb8mX1OzWMiCRdl74YNk+RoB BviJEptQQnqPVe3kwU8bxT2EvwrICxXstB6VUQ2lQjqcNCTeMp0jnDDetZxLzbRX/2BO VSPCH7B/GwFnj02pqduxkUNM1Q/oCZr8Bijs9BkFJKAtDYcTYnJcFB/Yyv9oenDSr/Yf rYDOuKfAWyI3wlyCBmGZ2ftcruFWYFpfr7/iBVed/3ch7oRz31ET73J9/KXEMtFj3ZB3 dnRQ== X-Forwarded-Encrypted: i=1; AJvYcCVs0uYGQveUp666CpLFXIZAdhAOKT84DmzDd3x0PMgEnKTAri+sydS70YQ1GS11k3+dxRMHWRSSLhKWlgHGjsAeaVPZiwnE+8fbTyky X-Gm-Message-State: AOJu0Yx0fDHnAVOJ83HwzsYbNDB11ZgVzOVIjI5ERNieBQZ/v5jZApkP euA/y34EEvqw6gX7zr9bLnYwj3ayHiGcuO8s52UUl1fAiBku0CtYyX5CvkPNxm4+/+t1LObWveX sNg== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:dd44:0:b0:61b:ebf1:77a with SMTP id 00721157ae682-620991623a6mr52732597b3.0.1715967627900; Fri, 17 May 2024 10:40:27 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 17 May 2024 10:39:00 -0700 In-Reply-To: <20240517173926.965351-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240517173926.965351-1-seanjc@google.com> X-Mailer: git-send-email 2.45.0.215.g3402c0e53f-goog Message-ID: <20240517173926.965351-24-seanjc@google.com> Subject: [PATCH v2 23/49] KVM: x86: Handle kernel- and KVM-defined CPUID words in a single helper From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hou Wenlong , Kechen Lu , Oliver Upton , Maxim Levitsky , Binbin Wu , Yang Weijiang , Robert Hoo Content-Type: text/plain; charset="UTF-8" Merge kvm_cpu_cap_init() and kvm_cpu_cap_init_kvm_defined() into a single helper. The only advantage of separating the two was to make it somewhat obvious that KVM directly initializes the KVM-defined words, whereas using a common helper will allow for hardening both kernel- and KVM-defined CPUID words without needing copy+paste. No functional change intended. Signed-off-by: Sean Christopherson --- arch/x86/kvm/cpuid.c | 44 +++++++++++++++----------------------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index f2bd2f5c4ea3..8efffd48cdf1 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -622,37 +622,23 @@ static __always_inline u32 raw_cpuid_get(struct cpuid_reg cpuid) return *__cpuid_entry_get_reg(&entry, cpuid.reg); } -/* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */ -static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf) +static __always_inline void kvm_cpu_cap_init(u32 leaf, u32 mask) { const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); - reverse_cpuid_check(leaf); + /* + * For kernel-defined leafs, mask the boot CPU's pre-populated value. + * For KVM-defined leafs, explicitly set the leaf, as KVM is the one + * and only authority. + */ + if (leaf < NCAPINTS) + kvm_cpu_caps[leaf] &= mask; + else + kvm_cpu_caps[leaf] = mask; kvm_cpu_caps[leaf] &= raw_cpuid_get(cpuid); } -static __always_inline -void kvm_cpu_cap_init_kvm_defined(enum kvm_only_cpuid_leafs leaf, u32 mask) -{ - /* Use kvm_cpu_cap_init for leafs that aren't KVM-only. */ - BUILD_BUG_ON(leaf < NCAPINTS); - - kvm_cpu_caps[leaf] = mask; - - __kvm_cpu_cap_mask(leaf); -} - -static __always_inline void kvm_cpu_cap_init(enum cpuid_leafs leaf, u32 mask) -{ - /* Use kvm_cpu_cap_init_kvm_defined for KVM-only leafs. */ - BUILD_BUG_ON(leaf >= NCAPINTS); - - kvm_cpu_caps[leaf] &= mask; - - __kvm_cpu_cap_mask(leaf); -} - void kvm_set_cpu_caps(void) { memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps)); @@ -740,12 +726,12 @@ void kvm_set_cpu_caps(void) F(AMX_FP16) | F(AVX_IFMA) | F(LAM) ); - kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, + kvm_cpu_cap_init(CPUID_7_1_EDX, F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI) | F(AMX_COMPLEX) ); - kvm_cpu_cap_init_kvm_defined(CPUID_7_2_EDX, + kvm_cpu_cap_init(CPUID_7_2_EDX, F(INTEL_PSFD) | F(IPRED_CTRL) | F(RRSBA_CTRL) | F(DDPD_U) | F(BHI_CTRL) | F(MCDT_NO) ); @@ -755,7 +741,7 @@ void kvm_set_cpu_caps(void) X86_64_F(XFD) ); - kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, + kvm_cpu_cap_init(CPUID_12_EAX, SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA) ); @@ -781,7 +767,7 @@ void kvm_set_cpu_caps(void) if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64)) kvm_cpu_cap_set(X86_FEATURE_GBPAGES); - kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, + kvm_cpu_cap_init(CPUID_8000_0007_EDX, SF(CONSTANT_TSC) ); @@ -835,7 +821,7 @@ void kvm_set_cpu_caps(void) kvm_cpu_cap_check_and_set(X86_FEATURE_IBPB_BRTYPE); kvm_cpu_cap_check_and_set(X86_FEATURE_SRSO_NO); - kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, + kvm_cpu_cap_init(CPUID_8000_0022_EAX, F(PERFMON_V2) ); -- 2.45.0.215.g3402c0e53f-goog