Message ID | 20231024001636.890236-2-jmattson@google.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ce89:0:b0:403:3b70:6f57 with SMTP id p9csp1629626vqx; Mon, 23 Oct 2023 17:17:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGGp1GPHPIU+lWj8RdVFhe1wDs2lQ9qDYeYbacnFkiOQ8CYNcoFQk1IpN4CWApJXya8/TT3 X-Received: by 2002:a17:902:fb8f:b0:1bf:c59:c944 with SMTP id lg15-20020a170902fb8f00b001bf0c59c944mr7095542plb.22.1698106629828; Mon, 23 Oct 2023 17:17:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698106629; cv=none; d=google.com; s=arc-20160816; b=g8hpr3LSZkay3wMal2ArfYG80vAX5HR4Y4iRT0vDJB37U1E0yXNa3Upfe+nmNzwiLE LLAPWuStBo25V2eRgP/8AftxGyzhcyaZlQ5Ufw0XwOROmm/iDzkguASI31cU1xWhDlYl qwsfcrSwo+VyhhDi19zBtiuv5bFUuxWYKLgg/AY5G66/ylS5Hy4sG81J6jmRAhJq/DMC Vy684zyYc6vV9MPRrKiXXDVZKdYWlWVP3rdrKdVSZV3fuVZtIG4XWeKP9kP5uQf0TTRU /l7VILbukXroGo5hP3Xb5Rn8iB4SOw/ph5jWc+7Fe/MLzR6VOsR3s7kARgLeG9RP6tTW Cs8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=jq7rtvs1J7SUa9QYH0420MH6Fu/kuIgYkCbKvTY6XYY=; fh=v4tBNjFz0MhCe7RmlB14AySRf8hmC1xADAhhevdW76k=; b=wBiwM/hYNAo9Zby1oICaInP/BWRsmmaymy65OeAYhp5GN7OlMD+YN9KddJCYNxnwwV aAxWoY2y8bzSEOp5jkbhDrp/pp9IwUEp5ttxPh+6dvuB1co1SOBp6mp68lnH3StTdRFJ 7oZjlIdYgIwXtSIWOCKlrzd1brBqbtfyKTV63YVad/YYponCUYiRAT4SLmWUpwB2elHv HkQymlfvGKQuka0qYGMCT79kAQFO56i9MVCT3FZGAE7Qd/WK/uS6C5H8PmpJKBxCh5mY R98FQv/hl5bgLzwEJV29bGBlywyJpyTAp+sAcqFDaCowxGiQTwXS4yqs0mUG8SbKwIyx KS4A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SjvGOKPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id s2-20020a170902ea0200b001b829a32f2dsi7634182plg.457.2023.10.23.17.17.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 17:17:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=SjvGOKPc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id E221C803EF04; Mon, 23 Oct 2023 17:17:07 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231565AbjJXAQx (ORCPT <rfc822;aposhian.dev@gmail.com> + 27 others); Mon, 23 Oct 2023 20:16:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52398 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230421AbjJXAQv (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 23 Oct 2023 20:16:51 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9902F10C for <linux-kernel@vger.kernel.org>; Mon, 23 Oct 2023 17:16:49 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7af53bde4so53769537b3.0 for <linux-kernel@vger.kernel.org>; Mon, 23 Oct 2023 17:16:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698106609; x=1698711409; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=jq7rtvs1J7SUa9QYH0420MH6Fu/kuIgYkCbKvTY6XYY=; b=SjvGOKPcCTxWApoChHPHp/hxH4zxspSqapH2ECYLSyOZllFII4jQ93D3yk8bLRq6wt pec5GR9sq+shHUclsbw/cIHLvcJYCgAkViHQprjbbJdekNXvPSqVAaioTuo0OYsmJhWL u/JA8zeE5RmuEcWVloySqUa2CxOeYjegZJfQm8eQiCdD2MIOXbUGL3m+y/+09/iQPVEN uxdaB7w35KFBiIzLBcboVzVaBi7+e8C0pQQ/CKIkMrN0Xa97ZNZjF6ja6bGf465oO88n KqYaWUYi2XFkf9IG7IwWBBIXxP6A8f4Vvd3vng2yZwH5bekMVHJE3g3LBpGFVHSYCWrg zIqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698106609; x=1698711409; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=jq7rtvs1J7SUa9QYH0420MH6Fu/kuIgYkCbKvTY6XYY=; b=tQrEF/MCScfabPTCiTflvlnYsd09bouAmoh7ZRt6ywjs2bZG8LQcssBmApJZviFeGj 3wE98JTlGMdpgzRLR6GYZiRuN0KX80y2OnDOaw7xn6AxuT2lkSN2iykh4ooXHsO5ZAdM ImCmvEI/3C3JFQGbT9gCZxE/mUpdsDWRYZuPrIhN6juDf62qlh5RtX7OHl6rrTSZ6A59 +2qtFuD3BtDkju8EHLIFhNSgK3p1d5jbUb+a484CrvI8iUffVZifSdXIRqNfo+3tFGNS G1i6RtYj9sc40R1/1pFYkPwAz0Sf7aJpCVTpq6GyQoklnPCtReqTTSOrSQjz4xaDtWsu cwRg== X-Gm-Message-State: AOJu0YwlxBPAjnsxtcXdXDqAq6eQCDVk1qZKEiBWAo9s8RxODM+3OOl1 JFkjWl1zCFrX5nB4X72gqoVoKy8nONZxRA== X-Received: from loggerhead.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:29a]) (user=jmattson job=sendgmr) by 2002:a0d:cc91:0:b0:5a8:15c4:5314 with SMTP id o139-20020a0dcc91000000b005a815c45314mr234855ywd.4.1698106608846; Mon, 23 Oct 2023 17:16:48 -0700 (PDT) Date: Mon, 23 Oct 2023 17:16:36 -0700 In-Reply-To: <20231024001636.890236-1-jmattson@google.com> Mime-Version: 1.0 References: <20231024001636.890236-1-jmattson@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Message-ID: <20231024001636.890236-2-jmattson@google.com> Subject: [PATCH 2/2] KVM: x86: Use a switch statement in __feature_translate() From: Jim Mattson <jmattson@google.com> To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, "'Paolo Bonzini '" <pbonzini@redhat.com>, "'Sean Christopherson '" <seanjc@google.com> Cc: Jim Mattson <jmattson@google.com> Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 23 Oct 2023 17:17:07 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780593857298621909 X-GMAIL-MSGID: 1780593857298621909 |
Series |
[1/2] KVM: x86: Advertise CPUID.(EAX=7,ECX=2):EDX[5:0] to userspace
|
|
Commit Message
Jim Mattson
Oct. 24, 2023, 12:16 a.m. UTC
The compiler will probably do better than linear search.
No functional change intended.
Signed-off-by: Jim Mattson <jmattson@google.com>
---
arch/x86/kvm/reverse_cpuid.h | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
Comments
On Mon, Oct 23, 2023, Jim Mattson wrote:
> The compiler will probably do better than linear search.
It shouldn't matter, KVM relies on the compiler to resolve the translation at
compile time, e.g. the result is fed into reverse_cpuid_check().
I.e. we should pick whatever is least ugly.
On Mon, Oct 23, 2023, Sean Christopherson wrote: > On Mon, Oct 23, 2023, Jim Mattson wrote: > > The compiler will probably do better than linear search. > > It shouldn't matter, KVM relies on the compiler to resolve the translation at > compile time, e.g. the result is fed into reverse_cpuid_check(). > > I.e. we should pick whatever is least ugly. What if we add a macro to generate each case statement? It's arguably a wee bit more readable, and also eliminates the possibility of returning the wrong feature due to copy+paste errors, e.g. nothing would break at compile time if we goofed and did: case X86_FEATURE_SGX1: return KVM_X86_FEATURE_SGX1; case X86_FEATURE_SGX2: return KVM_X86_FEATURE_SGX1; If you've no objection, I'll push this: -- Author: Jim Mattson <jmattson@google.com> Date: Mon Oct 23 17:16:36 2023 -0700 KVM: x86: Use a switch statement and macros in __feature_translate() Use a switch statement with macro-generated case statements to handle translating feature flags in order to reduce the probability of runtime errors due to copy+paste goofs, to make compile-time errors easier to debug, and to make the code more readable. E.g. the compiler won't directly generate an error for duplicate if statements if (x86_feature == X86_FEATURE_SGX1) return KVM_X86_FEATURE_SGX1; else if (x86_feature == X86_FEATURE_SGX2) return KVM_X86_FEATURE_SGX1; and so instead reverse_cpuid_check() will fail due to the untranslated entry pointing at a Linux-defined leaf, which provides practically no hint as to what is broken arch/x86/kvm/reverse_cpuid.h:108:2: error: call to __compiletime_assert_450 declared with 'error' attribute: BUILD_BUG_ON failed: x86_leaf == CPUID_LNX_4 BUILD_BUG_ON(x86_leaf == CPUID_LNX_4); ^ whereas duplicate case statements very explicitly point at the offending code: arch/x86/kvm/reverse_cpuid.h:125:2: error: duplicate case value '361' KVM_X86_TRANSLATE_FEATURE(SGX2); ^ arch/x86/kvm/reverse_cpuid.h:124:2: error: duplicate case value '360' KVM_X86_TRANSLATE_FEATURE(SGX1); ^ And without macros, the opposite type of copy+paste goof doesn't generate any error at compile-time, e.g. this yields no complaints: case X86_FEATURE_SGX1: return KVM_X86_FEATURE_SGX1; case X86_FEATURE_SGX2: return KVM_X86_FEATURE_SGX1; Note, __feature_translate() is forcibly inlined and the feature is known at compile-time, so the code generation between an if-elif sequence and a switch statement should be identical. Signed-off-by: Jim Mattson <jmattson@google.com> Link: https://lore.kernel.org/r/20231024001636.890236-2-jmattson@google.com [sean: use a macro, rewrite changelog] Signed-off-by: Sean Christopherson <seanjc@google.com> diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h index 17007016d8b5..aadefcaa9561 100644 --- a/arch/x86/kvm/reverse_cpuid.h +++ b/arch/x86/kvm/reverse_cpuid.h @@ -116,20 +116,19 @@ static __always_inline void reverse_cpuid_check(unsigned int x86_leaf) */ static __always_inline u32 __feature_translate(int x86_feature) { - if (x86_feature == X86_FEATURE_SGX1) - return KVM_X86_FEATURE_SGX1; - else if (x86_feature == X86_FEATURE_SGX2) - return KVM_X86_FEATURE_SGX2; - else if (x86_feature == X86_FEATURE_SGX_EDECCSSA) - return KVM_X86_FEATURE_SGX_EDECCSSA; - else if (x86_feature == X86_FEATURE_CONSTANT_TSC) - return KVM_X86_FEATURE_CONSTANT_TSC; - else if (x86_feature == X86_FEATURE_PERFMON_V2) - return KVM_X86_FEATURE_PERFMON_V2; - else if (x86_feature == X86_FEATURE_RRSBA_CTRL) - return KVM_X86_FEATURE_RRSBA_CTRL; +#define KVM_X86_TRANSLATE_FEATURE(f) \ + case X86_FEATURE_##f: return KVM_X86_FEATURE_##f - return x86_feature; + switch (x86_feature) { + KVM_X86_TRANSLATE_FEATURE(SGX1); + KVM_X86_TRANSLATE_FEATURE(SGX2); + KVM_X86_TRANSLATE_FEATURE(SGX_EDECCSSA); + KVM_X86_TRANSLATE_FEATURE(CONSTANT_TSC); + KVM_X86_TRANSLATE_FEATURE(PERFMON_V2); + KVM_X86_TRANSLATE_FEATURE(RRSBA_CTRL); + default: + return x86_feature; + } } static __always_inline u32 __feature_leaf(int x86_feature)
On Thu, Nov 30, 2023 at 12:28 PM Sean Christopherson <seanjc@google.com> wrote: > > On Mon, Oct 23, 2023, Sean Christopherson wrote: > > On Mon, Oct 23, 2023, Jim Mattson wrote: > > > The compiler will probably do better than linear search. > > > > It shouldn't matter, KVM relies on the compiler to resolve the translation at > > compile time, e.g. the result is fed into reverse_cpuid_check(). > > > > I.e. we should pick whatever is least ugly. > > What if we add a macro to generate each case statement? It's arguably a wee bit > more readable, and also eliminates the possibility of returning the wrong feature > due to copy+paste errors, e.g. nothing would break at compile time if we goofed > and did: > > case X86_FEATURE_SGX1: > return KVM_X86_FEATURE_SGX1; > case X86_FEATURE_SGX2: > return KVM_X86_FEATURE_SGX1; > > If you've no objection, I'll push this: :barf: Um, okay. > -- > Author: Jim Mattson <jmattson@google.com> > Date: Mon Oct 23 17:16:36 2023 -0700 > > KVM: x86: Use a switch statement and macros in __feature_translate() > > Use a switch statement with macro-generated case statements to handle > translating feature flags in order to reduce the probability of runtime > errors due to copy+paste goofs, to make compile-time errors easier to > debug, and to make the code more readable. > > E.g. the compiler won't directly generate an error for duplicate if > statements > > if (x86_feature == X86_FEATURE_SGX1) > return KVM_X86_FEATURE_SGX1; > else if (x86_feature == X86_FEATURE_SGX2) > return KVM_X86_FEATURE_SGX1; > > and so instead reverse_cpuid_check() will fail due to the untranslated > entry pointing at a Linux-defined leaf, which provides practically no > hint as to what is broken > > arch/x86/kvm/reverse_cpuid.h:108:2: error: call to __compiletime_assert_450 declared with 'error' attribute: > BUILD_BUG_ON failed: x86_leaf == CPUID_LNX_4 > BUILD_BUG_ON(x86_leaf == CPUID_LNX_4); > ^ > whereas duplicate case statements very explicitly point at the offending > code: > > arch/x86/kvm/reverse_cpuid.h:125:2: error: duplicate case value '361' > KVM_X86_TRANSLATE_FEATURE(SGX2); > ^ > arch/x86/kvm/reverse_cpuid.h:124:2: error: duplicate case value '360' > KVM_X86_TRANSLATE_FEATURE(SGX1); > ^ > > And without macros, the opposite type of copy+paste goof doesn't generate > any error at compile-time, e.g. this yields no complaints: > > case X86_FEATURE_SGX1: > return KVM_X86_FEATURE_SGX1; > case X86_FEATURE_SGX2: > return KVM_X86_FEATURE_SGX1; > > Note, __feature_translate() is forcibly inlined and the feature is known > at compile-time, so the code generation between an if-elif sequence and a > switch statement should be identical. > > Signed-off-by: Jim Mattson <jmattson@google.com> > Link: https://lore.kernel.org/r/20231024001636.890236-2-jmattson@google.com > [sean: use a macro, rewrite changelog] > Signed-off-by: Sean Christopherson <seanjc@google.com> > > diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h > index 17007016d8b5..aadefcaa9561 100644 > --- a/arch/x86/kvm/reverse_cpuid.h > +++ b/arch/x86/kvm/reverse_cpuid.h > @@ -116,20 +116,19 @@ static __always_inline void reverse_cpuid_check(unsigned int x86_leaf) > */ > static __always_inline u32 __feature_translate(int x86_feature) > { > - if (x86_feature == X86_FEATURE_SGX1) > - return KVM_X86_FEATURE_SGX1; > - else if (x86_feature == X86_FEATURE_SGX2) > - return KVM_X86_FEATURE_SGX2; > - else if (x86_feature == X86_FEATURE_SGX_EDECCSSA) > - return KVM_X86_FEATURE_SGX_EDECCSSA; > - else if (x86_feature == X86_FEATURE_CONSTANT_TSC) > - return KVM_X86_FEATURE_CONSTANT_TSC; > - else if (x86_feature == X86_FEATURE_PERFMON_V2) > - return KVM_X86_FEATURE_PERFMON_V2; > - else if (x86_feature == X86_FEATURE_RRSBA_CTRL) > - return KVM_X86_FEATURE_RRSBA_CTRL; > +#define KVM_X86_TRANSLATE_FEATURE(f) \ > + case X86_FEATURE_##f: return KVM_X86_FEATURE_##f > > - return x86_feature; > + switch (x86_feature) { > + KVM_X86_TRANSLATE_FEATURE(SGX1); > + KVM_X86_TRANSLATE_FEATURE(SGX2); > + KVM_X86_TRANSLATE_FEATURE(SGX_EDECCSSA); > + KVM_X86_TRANSLATE_FEATURE(CONSTANT_TSC); > + KVM_X86_TRANSLATE_FEATURE(PERFMON_V2); > + KVM_X86_TRANSLATE_FEATURE(RRSBA_CTRL); > + default: > + return x86_feature; > + } > } > > static __always_inline u32 __feature_leaf(int x86_feature) >
diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h index 17007016d8b5..da52f5ea0351 100644 --- a/arch/x86/kvm/reverse_cpuid.h +++ b/arch/x86/kvm/reverse_cpuid.h @@ -116,20 +116,22 @@ static __always_inline void reverse_cpuid_check(unsigned int x86_leaf) */ static __always_inline u32 __feature_translate(int x86_feature) { - if (x86_feature == X86_FEATURE_SGX1) + switch (x86_feature) { + case X86_FEATURE_SGX1: return KVM_X86_FEATURE_SGX1; - else if (x86_feature == X86_FEATURE_SGX2) + case X86_FEATURE_SGX2: return KVM_X86_FEATURE_SGX2; - else if (x86_feature == X86_FEATURE_SGX_EDECCSSA) + case X86_FEATURE_SGX_EDECCSSA: return KVM_X86_FEATURE_SGX_EDECCSSA; - else if (x86_feature == X86_FEATURE_CONSTANT_TSC) + case X86_FEATURE_CONSTANT_TSC: return KVM_X86_FEATURE_CONSTANT_TSC; - else if (x86_feature == X86_FEATURE_PERFMON_V2) + case X86_FEATURE_PERFMON_V2: return KVM_X86_FEATURE_PERFMON_V2; - else if (x86_feature == X86_FEATURE_RRSBA_CTRL) + case X86_FEATURE_RRSBA_CTRL: return KVM_X86_FEATURE_RRSBA_CTRL; - - return x86_feature; + default: + return x86_feature; + } } static __always_inline u32 __feature_leaf(int x86_feature)