Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp730585rdb; Thu, 30 Nov 2023 17:39:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IE4wOq6a+GxH+4O6SeXsbMDbgvoeu7+uZBPD+/VidfZDIdH7Zq9pERFvZyGQkZ6nIgSAmBE X-Received: by 2002:a05:6830:90:b0:6cd:896:e363 with SMTP id a16-20020a056830009000b006cd0896e363mr1581540oto.37.1701394794671; Thu, 30 Nov 2023 17:39:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701394794; cv=none; d=google.com; s=arc-20160816; b=rBruhaHaPq4zYGMz3ZsuPXtiTweUf6woElC1KEsrrqH+JF8L5UvflqoKN2tSHuKnM5 QK4cxKbrZy8rUxwa828e3S45pXEB+CwQx0NmVh3GJf0kpM3Sm/n+qB7UOoxy7fubdJSQ epszHv8wbssdd80VcOtDzWFqWsJkJiG0PT5dYhmk1ODTF8fxtoP6hlR7wI58LCN9GwYY kiVEbgDH8P8L0ydeWjvoLThWyMeag02GwbxvY3jIG0D5NaXW+uDR7hFSYikFGyqaE7ea kjBEu28lCqMiajo1Aj8+QaZWfEMZe9y/16/5eAt05vYlZzu0XWlzwUH/0f+0mZFxgu7T AS9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=0W7uBrHgrZxccbgeiWrjico6sL2HB9xoyfVd95MNJBY=; fh=POBaQVDMwuNDLAaROhkRRxG6N2/ZY0zeKVZI31MSKt0=; b=C39EsTN1q/fFzavPp0fNJqEfUtCqdnG0950VRm7KchXTZMUGWcKGakaROt751Ycxzo lZzWUF6Ezolp83nPB/AsvVvIaFHERTsp0ssEj4YHbsjnDG0PbnCBVBukYyiJ3WZ+RZwO BHAM80B/QS58eiaK7V3HT2VtaISXaEJ2vnKPRXEveZoQBMRxfXhQpcH23s9Bbhx/R1lD mV69cZG9fMdXZYCmnckhZ/2FUzjMN+Z3md1+OOAdSZeDBQ+aYfFQ3jwgi2G3Clb9xJH7 Rb1ghsD+5vlnCfT1Jx7kcLJs1829hwOH4WJhhcIMy0gMgunWCKHkAUe8sVt2WzRW/G7v Dk0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=eWEj2ptk; 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 Return-Path: Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id q31-20020a63f95f000000b005b8f1c4aa49si2510341pgk.895.2023.11.30.17.39.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Nov 2023 17:39:54 -0800 (PST) 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=eWEj2ptk; 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 581F880936E6; Thu, 30 Nov 2023 17:39:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229448AbjLABjg (ORCPT + 99 others); Thu, 30 Nov 2023 20:39:36 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46842 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbjLABje (ORCPT ); Thu, 30 Nov 2023 20:39:34 -0500 Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2CE3610FC for ; Thu, 30 Nov 2023 17:39:39 -0800 (PST) Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-548ae9a5eeaso3791a12.1 for ; Thu, 30 Nov 2023 17:39:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701394777; x=1701999577; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0W7uBrHgrZxccbgeiWrjico6sL2HB9xoyfVd95MNJBY=; b=eWEj2ptkoEn3rIBufL3ozyoxd/J/UvpCPVJrvq4vfgdrFqbBe9cIin0nlSLUxReZVG wX04IxkbxkNkE/5J0zxeOYYjU89KfKd47xHH1qh/NCT43kmWlhjzz6AkAYVFmHmQq6cj z6Lv6scyYaI5R/xGMrdjdwwkmRBEHZddtCLSXu9eXCjN41p9VV6NpqmEQn5oI0oFRWRx cAI234H2XUN0IqKpffoBhGBxrNA9eRL9j98DrSOlIHHDrtdh6/w7PWMbQdb+7N8q8bvz 9DBmypIYdHDNkBZlrPVmB8Ra9gbZkWgjYHjszYbacjYrj4frgauSgAObcNFljBCzCBvL eOuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701394777; x=1701999577; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0W7uBrHgrZxccbgeiWrjico6sL2HB9xoyfVd95MNJBY=; b=CLqQGAQgrWhJeDPaXKWy4raxoYcurSmrTKl+cU75SFFIdCGkcA0L8GH0eR29my2C/Z H9zbIWmek1oeMFO5/NSwzNGoc7aRN+X3kCuzxShHt+oJV3lNVXEZVbZ9XEirR8s38xAc MWw8drQsJzYRPMoh1qj7xGcQQgvQyI9PfKNWvE+4cl8c0E7FZ4Se5wE/0bSrMl4eUhDx ObxyhnS4MACKP4xTg/GvZcz0JAK5pvFEYt7I7q4zHjaQK/cUsMrmfe2WydJSPQVVxnDI nUQNOUxIlG7SfLRE8YB9801YEG/crPk1QCo9zXhXsnvrMYChbnCehW1vPQhCUmdkDGin trrQ== X-Gm-Message-State: AOJu0Yw8HYxmAR7Z1aG1cDgBiR58focql79A2sIDbW+RDn4aeHYclPQK nmEhv3jct5P5Cu1F+kkzQmvGy4O/pfW0ipu/iYfmbA== X-Received: by 2002:a50:d547:0:b0:54c:384b:e423 with SMTP id f7-20020a50d547000000b0054c384be423mr19703edj.5.1701394777495; Thu, 30 Nov 2023 17:39:37 -0800 (PST) MIME-Version: 1.0 References: <20231024001636.890236-1-jmattson@google.com> <20231024001636.890236-2-jmattson@google.com> In-Reply-To: From: Jim Mattson Date: Thu, 30 Nov 2023 17:39:25 -0800 Message-ID: Subject: Re: [PATCH 2/2] KVM: x86: Use a switch statement in __feature_translate() To: Sean Christopherson Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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,T_SCC_BODY_TEXT_LINE, 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: 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]); Thu, 30 Nov 2023 17:39:51 -0800 (PST) On Thu, Nov 30, 2023 at 12:28=E2=80=AFPM Sean Christopherson 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 translat= ion 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 > 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 runti= me > 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 =3D=3D X86_FEATURE_SGX1) > return KVM_X86_FEATURE_SGX1; > else if (x86_feature =3D=3D X86_FEATURE_SGX2) > return KVM_X86_FEATURE_SGX1; > > and so instead reverse_cpuid_check() will fail due to the untranslate= d > 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_as= sert_450 declared with 'error' attribute: > BUILD_BUG_ON failed: x86_leaf = =3D=3D CPUID_LNX_4 > BUILD_BUG_ON(x86_leaf =3D=3D CPUID_LNX_4); > ^ > whereas duplicate case statements very explicitly point at the offend= ing > code: > > arch/x86/kvm/reverse_cpuid.h:125:2: error: duplicate case value '36= 1' > KVM_X86_TRANSLATE_FEATURE(SGX2); > ^ > arch/x86/kvm/reverse_cpuid.h:124:2: error: duplicate case value '36= 0' > KVM_X86_TRANSLATE_FEATURE(SGX1); > ^ > > And without macros, the opposite type of copy+paste goof doesn't gene= rate > 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 kn= own > at compile-time, so the code generation between an if-elif sequence a= nd a > switch statement should be identical. > > Signed-off-by: Jim Mattson > Link: https://lore.kernel.org/r/20231024001636.890236-2-jmattson@goog= le.com > [sean: use a macro, rewrite changelog] > Signed-off-by: Sean Christopherson > > 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(uns= igned int x86_leaf) > */ > static __always_inline u32 __feature_translate(int x86_feature) > { > - if (x86_feature =3D=3D X86_FEATURE_SGX1) > - return KVM_X86_FEATURE_SGX1; > - else if (x86_feature =3D=3D X86_FEATURE_SGX2) > - return KVM_X86_FEATURE_SGX2; > - else if (x86_feature =3D=3D X86_FEATURE_SGX_EDECCSSA) > - return KVM_X86_FEATURE_SGX_EDECCSSA; > - else if (x86_feature =3D=3D X86_FEATURE_CONSTANT_TSC) > - return KVM_X86_FEATURE_CONSTANT_TSC; > - else if (x86_feature =3D=3D X86_FEATURE_PERFMON_V2) > - return KVM_X86_FEATURE_PERFMON_V2; > - else if (x86_feature =3D=3D 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) >