Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp4069913rdg; Wed, 18 Oct 2023 14:08:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGuiqadyeta9pXT4gxJTXF8rIkAsrX0U/EBjgk8vJAHaEnwk7gIKiaISDBgkfayvKVcByHe X-Received: by 2002:a17:90a:2fc8:b0:27d:3a2a:1e2c with SMTP id n8-20020a17090a2fc800b0027d3a2a1e2cmr370107pjm.15.1697663302822; Wed, 18 Oct 2023 14:08:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697663302; cv=none; d=google.com; s=arc-20160816; b=h7xaIkcDGBjjIG07OzlHmy7MxeS7JMYpBX4zZ6MWXDLNOyGgzFTEpL5Mf4NsSN4/NU URsCfctM8LdWiyVsuclnuXLXpJ4ffKpFbrtJ+sbntr+eKb8WglG4u7G5NQK5l95Yw/1T wVq82UYHC0a7yosqt0yrotLa32CWhooSS9jZsyoPXKSlRPRtkcYOprYpY6eSwM+sltpf PqYGQsarFGrEdzhU7QPNsX4pWuWu02tBXQxNgeir0xYRdhXStd2JW510WKw63n5bn8bl dasjs1ardiEinun3Ab47kUrWlqZmcRerenbglGmgi6lmgopDLz+CobxqTJ18BijmKrDU +CyA== 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=PVTN0DwGw7en9yc7Uz7tI+IpU6nI5xXmVaMMJdRBSvM=; fh=rrE0I4kmoLmrsjfr0+4jU5L6rSBvqFwibOXN2tmPIjw=; b=u52cnMHRPfKGP00L15UAG7O/GpVA0bESLZWo6eKFre6Sm4x4KVzaYkNYjO4N2H7lcs PkGcgKhl1D0Yx6n8FrhKkIJDKtR8cxKtBVRIq0U78/Uorgl4HTy1b0r59rwrl1n6EL2L 3f8MKyCKCFgA4HX4WUx4GGMYAJgfjfb/41OvWl0ALa1Gbeit8RSKJFlN4Ge/0/f0flwW MXXuPLBF/1eX5t8ztBE/SA8UT9sCyfI/c3bTt/etyFQP60ga0YIe6vOXa5V0+c14mEk7 DHLT6oFBTcgXittS8DkCEqXf7TxzGSz1P5zUUIiy3RndPBTH5JxLZp4WqBZtNEQ33Mcc HdYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1bl+0Keb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id fy12-20020a17090b020c00b0027cfcb6bfc8si631338pjb.96.2023.10.18.14.08.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 14:08:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1bl+0Keb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id E737D81D770E; Wed, 18 Oct 2023 14:08:21 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231993AbjJRVIR (ORCPT + 99 others); Wed, 18 Oct 2023 17:08:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229894AbjJRVIQ (ORCPT ); Wed, 18 Oct 2023 17:08:16 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E59D913D for ; Wed, 18 Oct 2023 14:08:13 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id d9443c01a7336-1c9e994fd94so48028315ad.3 for ; Wed, 18 Oct 2023 14:08:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1697663293; x=1698268093; 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=PVTN0DwGw7en9yc7Uz7tI+IpU6nI5xXmVaMMJdRBSvM=; b=1bl+0KebzxdBeUoGPQ9rb0ky0fzroznmmfg0SS05gFbG/GXvgJ9W/Ic8P4pDQ1oLkO lXvBy+9RtvC9DyGT2p1h3vQnGbpdQaeESexH/fo4V3amnpa+R8/2rqUToNVEvRJDzOA1 ELX+XLmLZiuVI3Gkn3NqIZQ/kWU1xPMY6CXux8fa2S999IWX/g3w+pCNmYX2y/otcwvS kbjMKQjDHgnpgxBrXYO1JXUiarqIeBCG/Ucisqco7xvgLQFogq3uJPEHAyrL73lvsqtV FeNHGTtjCa2zB44kCDdkh5494G+yW+425BTGcHFwt76/TfPlS8Bq7Apz0vzaDWGcizOU MQhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697663293; x=1698268093; 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=PVTN0DwGw7en9yc7Uz7tI+IpU6nI5xXmVaMMJdRBSvM=; b=exuH175MuLJ4xWYmSmCeAhrHB+RIVSbttwWaq40xnJko6+hd9gQqRayLKHwV3OIYRJ NbpQkIBHPQZnoI/9GoKybG5NIH3xsfP7CaP5Nw9FDW+3rvjjEAdULfMe5wAzSXi1Gvo9 MkzPD1cxkErfn7YXC3lijLMS3oKj4zrp1+RR9CmUv96XUT10WsY/BpwackzGUV6OCETc M9AYtflwxjEWq+B0BSVeitWKHxF6KrZQ1w92VAD7tPCMcknqwEHG//7K+NbY+TinZUoW QaTxT7iSApdnqgvrMFYUHCtlpVenhnL6Aol0CTVPNq18hL+1B2XYfzopeAs9Sxu8aNhQ bCTw== X-Gm-Message-State: AOJu0YzQtIofMJbDOfFiJKomSynbKwSEtNZ94r2PLNINeB42sDPoCGO5 EEdaIx9v28hCIaXgRHd2//fXhhZha28= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:654d:b0:1ca:8868:8c97 with SMTP id d13-20020a170902654d00b001ca88688c97mr12081pln.0.1697663293202; Wed, 18 Oct 2023 14:08:13 -0700 (PDT) Date: Wed, 18 Oct 2023 14:08:11 -0700 In-Reply-To: <20230927230811.2997443-1-xin@zytor.com> Mime-Version: 1.0 References: <20230927230811.2997443-1-xin@zytor.com> Message-ID: Subject: Re: [PATCH 1/1] KVM: VMX: Cleanup VMX basic information defines and usages From: Sean Christopherson To: "Xin Li (Intel)" Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, weijiang.yang@intel.com Content-Type: text/plain; charset="us-ascii" 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,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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Wed, 18 Oct 2023 14:08:22 -0700 (PDT) On Wed, Sep 27, 2023, Xin Li (Intel) wrote: > From: Xin Li > > Add IA32_VMX_BASIC MSR bitfield shift macros and use them to define VMX > basic information bitfields. Why? Unless something actually uses the shift independently, just define the BIT_ULL(...) straightaway. > Add VMX_BASIC_FEATURES and VMX_BASIC_RESERVED_BITS to form a valid bitmask > of IA32_VMX_BASIC MSR. As a result, to add a new VMX basic feature bit, > just change the 2 new macros in the header file. Not if a new feature bit lands in the middle of one of the reserved ranges, then the developer will have to update at least three macros, and add a new one. More below. > Also replace hardcoded VMX basic numbers with the new VMX basic macros. > > Tested-by: Shan Kang > Signed-off-by: Xin Li > --- > arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------ > arch/x86/kvm/vmx/nested.c | 10 +++------ > arch/x86/kvm/vmx/vmx.c | 2 +- > tools/arch/x86/include/asm/msr-index.h | 31 ++++++++++++++++++++------ Please drop the tools/ update, copying kernel headers into tools is a perf tools thing that I want no part of. https://lore.kernel.org/all/Y8bZ%2FJ98V5i3wG%2Fv@google.com > 4 files changed, 52 insertions(+), 22 deletions(-) > > diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h > index 1d111350197f..4607448ff805 100644 > --- a/arch/x86/include/asm/msr-index.h > +++ b/arch/x86/include/asm/msr-index.h > @@ -1084,13 +1084,30 @@ > #define MSR_IA32_VMX_PROCBASED_CTLS3 0x00000492 > > /* VMX_BASIC bits and bitmasks */ > -#define VMX_BASIC_VMCS_SIZE_SHIFT 32 > -#define VMX_BASIC_TRUE_CTLS (1ULL << 55) > -#define VMX_BASIC_64 0x0001000000000000LLU > -#define VMX_BASIC_MEM_TYPE_SHIFT 50 > -#define VMX_BASIC_MEM_TYPE_MASK 0x003c000000000000LLU > -#define VMX_BASIC_MEM_TYPE_WB 6LLU > -#define VMX_BASIC_INOUT 0x0040000000000000LLU > +#define VMX_BASIC_VMCS_SIZE_SHIFT 32 > +#define VMX_BASIC_ALWAYS_0 BIT_ULL(31) > +#define VMX_BASIC_RESERVED_RANGE_1 GENMASK_ULL(47, 45) > +#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT 48 > +#define VMX_BASIC_32BIT_PHYS_ADDR_ONLY BIT_ULL(VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT) > +#define VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT 49 > +#define VMX_BASIC_DUAL_MONITOR_TREATMENT BIT_ULL(VMX_BASIC_DUAL_MONITOR_TREATMENT_SHIFT) > +#define VMX_BASIC_MEM_TYPE_SHIFT 50 > +#define VMX_BASIC_MEM_TYPE_WB 6LLU > +#define VMX_BASIC_INOUT_SHIFT 54 > +#define VMX_BASIC_INOUT BIT_ULL(VMX_BASIC_INOUT_SHIFT) > +#define VMX_BASIC_TRUE_CTLS_SHIFT 55 > +#define VMX_BASIC_TRUE_CTLS BIT_ULL(VMX_BASIC_TRUE_CTLS_SHIFT) > +#define VMX_BASIC_RESERVED_RANGE_2 GENMASK_ULL(63, 56) > + > +#define VMX_BASIC_FEATURES \ Maybe VMX_BASIC_FEATURES_MASK to make it more obvious it's a mask of multiple bits? > + (VMX_BASIC_DUAL_MONITOR_TREATMENT | \ > + VMX_BASIC_INOUT | \ > + VMX_BASIC_TRUE_CTLS) > + > +#define VMX_BASIC_RESERVED_BITS \ > + (VMX_BASIC_ALWAYS_0 | \ > + VMX_BASIC_RESERVED_RANGE_1 | \ > + VMX_BASIC_RESERVED_RANGE_2) I don't see any value in defining VMX_BASIC_RESERVED_RANGE_1 and VMX_BASIC_RESERVED_RANGE_2 separately. Or VMX_BASIC_ALWAYS_0 for the matter. And I don't think these macros need to go in msr-index.h, e.g. just define them above vmx_restore_vmx_basic() as that's likely going to be the only user, ever. And what's really missing is a static_assert() or BUILD_BUG_ON() to ensure that VMX_BASIC_FEATURES doesn't overlap with VMX_BASIC_RESERVED_BITS. > /* Resctrl MSRs: */ > /* - Intel: */ > diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c > index c5ec0ef51ff7..5280ba944c87 100644 > --- a/arch/x86/kvm/vmx/nested.c > +++ b/arch/x86/kvm/vmx/nested.c > @@ -1203,21 +1203,17 @@ static bool is_bitwise_subset(u64 superset, u64 subset, u64 mask) > > static int vmx_restore_vmx_basic(struct vcpu_vmx *vmx, u64 data) > { > - const u64 feature_and_reserved = > - /* feature (except bit 48; see below) */ > - BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) | > - /* reserved */ > - BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56); > u64 vmx_basic = vmcs_config.nested.basic; > > - if (!is_bitwise_subset(vmx_basic, data, feature_and_reserved)) > + if (!is_bitwise_subset(vmx_basic, data, > + VMX_BASIC_FEATURES | VMX_BASIC_RESERVED_BITS)) > return -EINVAL; > > /* > * KVM does not emulate a version of VMX that constrains physical > * addresses of VMX structures (e.g. VMCS) to 32-bits. > */ > - if (data & BIT_ULL(48)) > + if (data & VMX_BASIC_32BIT_PHYS_ADDR_ONLY) > return -EINVAL; > > if (vmx_basic_vmcs_revision_id(vmx_basic) != > diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c > index 72e3943f3693..f597243d6a72 100644 > --- a/arch/x86/kvm/vmx/vmx.c > +++ b/arch/x86/kvm/vmx/vmx.c > @@ -2701,7 +2701,7 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, > > #ifdef CONFIG_X86_64 > /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ > - if (vmx_msr_high & (1u<<16)) > + if (vmx_msr_high & (1u << (VMX_BASIC_32BIT_PHYS_ADDR_ONLY_SHIFT - 32))) In all honestly, I find the existing code easier to read. I'm definitely not saying the existing code is good, but IMO this is at best a wash. I would much rather we do something like this and move away from the hi/lo crud entirely: diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 86ce9efe6c66..f103980c3d02 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -2693,28 +2693,28 @@ static int setup_vmcs_config(struct vmcs_config *vmcs_conf, _vmexit_control &= ~x_ctrl; } - rdmsr(MSR_IA32_VMX_BASIC, vmx_msr_low, vmx_msr_high); + rdmsrl(MSR_IA32_VMX_BASIC, vmx_msr); /* IA-32 SDM Vol 3B: VMCS size is never greater than 4kB. */ - if ((vmx_msr_high & 0x1fff) > PAGE_SIZE) + if ((VMX_BASIC_VMCS_SIZE(vmx_msr) > PAGE_SIZE) return -EIO; #ifdef CONFIG_X86_64 /* IA-32 SDM Vol 3B: 64-bit CPUs always have VMX_BASIC_MSR[48]==0. */ - if (vmx_msr_high & (1u<<16)) + if (vmx_msr & VMX_BASIC_32BIT_PHYS_ADDR_ONLY) return -EIO; #endif /* Require Write-Back (WB) memory type for VMCS accesses. */ - if (((vmx_msr_high >> 18) & 15) != 6) + if (VMX_BASIC_VMCS_MEMTYPE(vmx_msr) != VMX_BASIC_MEM_TYPE_WB) return -EIO; rdmsrl(MSR_IA32_VMX_MISC, misc_msr); - vmcs_conf->size = vmx_msr_high & 0x1fff; - vmcs_conf->basic_cap = vmx_msr_high & ~0x1fff; + vmcs_conf->size = VMX_BASIC_VMCS_SIZE(vmx_msr); + vmcs_conf->basic_cap = ????(vmx_msr); - vmcs_conf->revision_id = vmx_msr_low; + vmcs_conf->revision_id = (u32)vmx_msr; vmcs_conf->pin_based_exec_ctrl = _pin_based_exec_control; vmcs_conf->cpu_based_exec_ctrl = _cpu_based_exec_control;