Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp21061996rwd; Thu, 29 Jun 2023 10:22:05 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7zq7O4xUTD98ID3gobmgxfltz/3UKpA/nIYKrc2WfuZ5Wo9w9Zy6Vsi73zux+GEeH8ScCE X-Received: by 2002:a05:6a20:4986:b0:11a:2908:bb5c with SMTP id fs6-20020a056a20498600b0011a2908bb5cmr298819pzb.28.1688059325135; Thu, 29 Jun 2023 10:22:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688059325; cv=none; d=google.com; s=arc-20160816; b=KLZoT0yaZjmKmzw89ql0W+0HAYKBksAA1Eyk6x96KufgMx1fclTW7hIBr66irTLG83 eGCZAjac+We7t0TUNMx4FIdhMEY6GXV7K8YZuYHXPRYyXPwu4fcIOy5adR/zRdzRLKP2 3HZRVgRaPjiYZk/MmdJIPD9NHlDLzvfEbDBLempcfCNqyI40P1YoSpCb2ypaGBm1vfrR KsywYWA75Cn7KiwRRyYQIRCFXCBVf+qFcaPlQQRkRaE37EoG//4Nm2WZ0Jg57T23uJSk PYStsrznG1rTvp+4P3KMSCu2a8hFu3SaF1JMYHQk6xLaHkw2PJmI8S4YB4vdWvfiHZiE ItZA== 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:from:subject :message-id:references:mime-version:in-reply-to:date:dkim-signature; bh=xMCpFTi4MEZLbHfM6PilnIIZPnyzbN27S0/lWwHLYHI=; fh=8ldW+SC3L4cWX5UmYhSx1eKM12TLdYKPov6oskRwyUU=; b=bvc2+JtthohNuaAC/Bqr6M4VRmt0k+21nP2YO96YAu2hL1Y6Vdu+C83c0aqoTqdCJ3 3OHkXZx+Otp1lHQaqbZix5fPh4hSpwntFdg32L8pklWTOnfbnBGmvTyNP2IoArLprSA1 whlB0Yb4TEpgoC+LGip5hh8x6TLL62ywWXYlhd5FgikXFqszLOxMScFhdoAc4PR40OAd aeXZyS6zN08mBU7rVRxCiHbUs8G70hy2pscyJsUSJ2E09vpQSyUlPtiXg99JsIdiWEeU IYybZETvfbcEw3CbMPFO6ociGB9E9VSu5B6BFY2zGJAAImcdhS3OIX9IJ5fUsSSP7Kjy BwlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="GMBD/Lpz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fd22-20020a056a002e9600b00660b5630908si11292080pfb.269.2023.06.29.10.21.25; Thu, 29 Jun 2023 10:22:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b="GMBD/Lpz"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232047AbjF2Q0f (ORCPT + 99 others); Thu, 29 Jun 2023 12:26:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229910AbjF2Q0d (ORCPT ); Thu, 29 Jun 2023 12:26:33 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D1F82CF for ; Thu, 29 Jun 2023 09:26:32 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-c38c87ea9ceso1349793276.1 for ; Thu, 29 Jun 2023 09:26:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1688055992; x=1690647992; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=xMCpFTi4MEZLbHfM6PilnIIZPnyzbN27S0/lWwHLYHI=; b=GMBD/LpzELPsRbxZKKzblbYWJ+sxqCFJh8sqXPWlxGp8U0A+UAgRmx8mtN4RFIKJLS tWShDw0jJHclaaBavEJWjBB6dGPUFZoVr5twyAtBHN6qebVtacCXdYDnd93pZVkyKoZw 0+WJ/aWT6RPc8Os5dCvJyiYl0a4On2PNUBwQYaQq5U78afKLGz6jg3nVF/2Qhr5TQX0q EK4td6N0JI9F5orF6SckKRYtzUbWYAUY5E5soJH6YcA1rYDGWhTJIbgdvG8mpTgqleWV cCtNrROuiOoldk/CP0QNltmh6FMzC+iqKVsmsBNTqPYeMGPgjNuoHfAlcELNKCgayqlJ pn6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688055992; x=1690647992; h=content-transfer-encoding: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=xMCpFTi4MEZLbHfM6PilnIIZPnyzbN27S0/lWwHLYHI=; b=LBxUIQNvKMOD9p1ywpph6FEIqz9C7nscWz+T5wlQzwTEZpkT1722SZVZS+RXOHHmKk c6+hhRcO2ubOffFM3irKnim3Nb/pbEdNxa7zGkjRg6KaR1g6JS7vF4YQcMbo04I65mie 6uPc3RtOqTlLtcZDYKjOBZTYySJqIeaH2Pr1fVBmX2IlROGJdEw/L5g7DOGs9chbGeQE NGA9VNvr2k9tvBo97mCIM83qSB47YkPqFWoilKDLhrcn5H8W7lu5gfixDHuJqpoU+dSF W/RxM/4I7+Ybqzdc7AK757CuTuTS44anOikJK4Kr44jA0JbPJulrVM40UnXIEWrSa2ZE RAlQ== X-Gm-Message-State: ABy/qLbMQ3S8VcoFazuMGKsvFwtt9bWDdCQnCcdHCveTk651edYThWyB FzfZF+eJt3vNn2/4S4MskJw3eKqAFbQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1105:b0:bc3:cdb7:4ec8 with SMTP id o5-20020a056902110500b00bc3cdb74ec8mr66497ybu.6.1688055992045; Thu, 29 Jun 2023 09:26:32 -0700 (PDT) Date: Thu, 29 Jun 2023 09:26:30 -0700 In-Reply-To: <9d9e9156-b9d9-86b4-9d20-77305e1e4d63@linux.intel.com> Mime-Version: 1.0 References: <20230217231022.816138-1-seanjc@google.com> <20230217231022.816138-2-seanjc@google.com> <9d9e9156-b9d9-86b4-9d20-77305e1e4d63@linux.intel.com> Message-ID: Subject: Re: [PATCH 01/12] KVM: x86: Add a framework for enabling KVM-governed x86 features From: Sean Christopherson To: Binbin Wu Cc: Paolo Bonzini , Vitaly Kuznetsov , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 29, 2023, Binbin Wu wrote: >=20 >=20 > On 2/18/2023 7:10 AM, Sean Christopherson wrote: > > diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm= _host.h > > index 792a6037047a..cd660de02f7b 100644 > > --- a/arch/x86/include/asm/kvm_host.h > > +++ b/arch/x86/include/asm/kvm_host.h > > @@ -835,6 +835,17 @@ struct kvm_vcpu_arch { > > struct kvm_cpuid_entry2 *cpuid_entries; > > struct kvm_hypervisor_cpuid kvm_cpuid; > > + /* > > + * Track whether or not the guest is allowed to use features that are > > + * governed by KVM, where "governed" means KVM needs to manage state > > + * and/or explicitly enable the feature in hardware. Typically, but > > + * not always, governed features can be used by the guest if and only > > + * if both KVM and userspace want to expose the feature to the guest. > > + */ > > + struct { > > + u32 enabled; > Although there are some guidances/preconditions of using the framework, > is it possible that u32 will be ran out quickly after people starts to us= e > the framework? It's definitely possible. And there's no reason to limit this to a u32, I = really have no idea why I did that.=20 Ah, it's because "struct kvm_vcpu_arch" is defined in arch/x86/include/asm/= kvm_host.h, and I didn't want to expose governed_features.h in arch/x86/include/asm. H= rm, that's really annoying. Aha! A better workaround for that conudrum would be to define an explicit = "max" and use that, with a FIXME to call out that this really should use KVM_NR_GOVERNED_FEATURES directly. I have aspirations of moving kvm_host.h= to arch//kvm, at which point this can be cleaned up by declaring "enum kvm_governed_features" in kvm_host.h (though it'll likely be named somethin= g like kvm_arch.h at that point). /* * FIXME: Drop this macro and use KVM_NR_GOVERNED_FEATURES directly * when "struct kvm_vcpu_arch" is no longer defined in an * arch/x86/include/asm header. The max is mostly arbitrary, i.e. * can be increased as necessary. */ #define KVM_MAX_NR_GOVERNED_FEATURES BITS_PER_LONG /* * Track whether or not the guest is allowed to use features that are * governed by KVM, where "governed" means KVM needs to manage state * and/or explicitly enable the feature in hardware. Typically, but * not always, governed features can be used by the guest if and only * if both KVM and userspace want to expose the feature to the guest. */ struct { DECLARE_BITMAP(enabled, KVM_MAX_NR_GOVERNED_FEATURES); } governed_features; > Of course, I noticed there is build=EF=BF=BD bug check on the length, it = should be > OK to increase the length when needed. > > +static __always_inline int kvm_governed_feature_index(unsigned int x86= _feature) > > +{ > > + switch (x86_feature) { > > +#define KVM_GOVERNED_FEATURE(x) case x: return KVM_GOVERNED_##x; > > +#include "governed_features.h" > > + default: > > + return -1; > > + } > > +} > > + > > +static __always_inline int kvm_is_governed_feature(unsigned int x86_fe= ature) > Is it better to use bool instead of int? Yes, this definitely should return a bool. Thanks!