Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp198423rdb; Thu, 19 Oct 2023 01:27:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFJEdKZ+w9xzJTWMnohKaZWAd2oEjL1xJSxE5K3Oen7qGRGYM7hEa1EZ1Izv67jyoWCcCFE X-Received: by 2002:a05:6359:b9a:b0:164:a045:e459 with SMTP id gf26-20020a0563590b9a00b00164a045e459mr1465613rwb.16.1697704058768; Thu, 19 Oct 2023 01:27:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697704058; cv=none; d=google.com; s=arc-20160816; b=tr6sbsylBJbMB3P201sbgONI6mn6aHYipFvl4at+X2XG//I1FWwJDdcvXjrLQjn6Bw YnC0sm+m0sOJw4ie/pu5Z4+WQdAJF7NTUNsMkdIM7+EjSxNkx3Wi2H7bRkKEZNgxdhTs MH7Riav6OugCxr6tUBqsfiOnMHaSsUDGYAErclcniSizUHImYua6hDpZ+PbDxTPKx7je FJR+UZa+DR487mhFGjQUqXP1o6ZbWegdZ5CEPsWG0/CpWemol0jSJkxLN34F/IF7gFig VBng63RE4Jz5VsjI9OP53/r3KXK9FCB5H1FUs6sXwrQLSBZxc7AaUzRoaB6D+SaOhfGG jAIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:autocrypt :from:content-language:references:cc:to:subject:user-agent :mime-version:date:message-id:dkim-signature:dkim-filter; bh=qC604qCWixi6qsUg2/vSLQ4O7LWbdngoJ3dxnuhZYvA=; fh=pnTbaDeZo4WsvKpnxVEPwJjMS+sDVolrx0w89IfeDkg=; b=b/0w6LbVoIGpUkqZRdKv9N7NULakIeWDMEat/OxynbMjKz23DnNA16NayFx+pD19b5 lmChl4/0yjbw19g6itDy3YwgM8FApsVUAjyhWkAAtjX8f15Zv+YVo9gkzehuBeCfH76+ nF3wJYtaVDw4jd0HGMTLjGZGe2+A4KTr+SEBvkdI27TRw/4sG41TumwNkOYKfjHUXqIB etvvg15N97NFuiJf+p21JgHceCUBAIHyHZP7+9c2DRO0bR8WdK9VnxWjdq9nVPRa0XPo KunSAOW87y05zdZi8/lpTynZYLwRZcqGKsDnxl2Cq7b30V1xPAQXZzHftUOLHwSdKLZV 6vZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2023101201 header.b=TlgSLd9T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from agentk.vger.email (agentk.vger.email. [2620:137:e000::3:2]) by mx.google.com with ESMTPS id a26-20020aa78e9a000000b0068fc7ab1fd5si5569237pfr.269.2023.10.19.01.27.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 01:27:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) client-ip=2620:137:e000::3:2; Authentication-Results: mx.google.com; dkim=pass header.i=@zytor.com header.s=2023101201 header.b=TlgSLd9T; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:2 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 0C4068135C60; Thu, 19 Oct 2023 01:27:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235278AbjJSI1E (ORCPT + 99 others); Thu, 19 Oct 2023 04:27:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38716 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235216AbjJSI1B (ORCPT ); Thu, 19 Oct 2023 04:27:01 -0400 Received: from mail.zytor.com (unknown [IPv6:2607:7c80:54:3::138]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1382C130; Thu, 19 Oct 2023 01:26:58 -0700 (PDT) Received: from [192.168.7.187] ([76.103.185.250]) (authenticated bits=0) by mail.zytor.com (8.17.1/8.17.1) with ESMTPSA id 39J8Q8Ld647733 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NO); Thu, 19 Oct 2023 01:26:09 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 mail.zytor.com 39J8Q8Ld647733 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2023101201; t=1697703970; bh=qC604qCWixi6qsUg2/vSLQ4O7LWbdngoJ3dxnuhZYvA=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=TlgSLd9TI0qJpEMVFbiIXJbJqGDxigCpfHI+LdG1Exh6lnuv2ayGdeyUboAEt/JKK AJQva+NPHcu1JOWsLAwG6zlQBrjfkuewFb6ZjGAbqbK0OJ1D9zbYRsJvcz1kHvVZha E9smW1diVC54KW88eh/am5aDKy8E6VuKit7oM73rshM/qf/9U+Ihm2oFvIYGxKR6Cv hc5a/jFFXIsI/byF/ba5C035UZsVVT9FRHnCjfHE30K5QytYHjih3qHY1nQfbVJUmX 9w5oVQgFdjuoyByhfrAlapJThqEm6TOfjEIip7Q6kg4tw4zzIC9UN7Nr/eLlUbPnyl V8Yf/2/eL8Mog== Message-ID: <20bb3005-bf6c-4fe1-bf0d-6d37e0ce1a77@zytor.com> Date: Thu, 19 Oct 2023 01:26:06 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] KVM: VMX: Cleanup VMX basic information defines and usages To: Sean Christopherson 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 References: <20230927230811.2997443-1-xin@zytor.com> Content-Language: en-US From: Xin Li Autocrypt: addr=xin@zytor.com; keydata= xsDNBGUPz1cBDACS/9yOJGojBFPxFt0OfTWuMl0uSgpwk37uRrFPTTLw4BaxhlFL0bjs6q+0 2OfG34R+a0ZCuj5c9vggUMoOLdDyA7yPVAJU0OX6lqpg6z/kyQg3t4jvajG6aCgwSDx5Kzg5 Rj3AXl8k2wb0jdqRB4RvaOPFiHNGgXCs5Pkux/qr0laeFIpzMKMootGa4kfURgPhRzUaM1vy bsMsL8vpJtGUmitrSqe5dVNBH00whLtPFM7IbzKURPUOkRRiusFAsw0a1ztCgoFczq6VfAVu raTye0L/VXwZd+aGi401V2tLsAHxxckRi9p3mc0jExPc60joK+aZPy6amwSCy5kAJ/AboYtY VmKIGKx1yx8POy6m+1lZ8C0q9b8eJ8kWPAR78PgT37FQWKYS1uAroG2wLdK7FiIEpPhCD+zH wlslo2ETbdKjrLIPNehQCOWrT32k8vFNEMLP5G/mmjfNj5sEf3IOKgMTMVl9AFjsINLHcxEQ 6T8nGbX/n3msP6A36FDfdSEAEQEAAc0WWGluIExpIDx4aW5Aenl0b3IuY29tPsLBDQQTAQgA NxYhBIUq/WFSDTiOvUIqv2u9DlcdrjdRBQJlD89XBQkFo5qAAhsDBAsJCAcFFQgJCgsFFgID AQAACgkQa70OVx2uN1HUpgv/cM2fsFCQodLArMTX5nt9yqAWgA5t1srri6EgS8W3F+3Kitge tYTBKu6j5BXuXaX3vyfCm+zajDJN77JHuYnpcKKr13VcZi1Swv6Jx1u0II8DOmoDYLb1Q2ZW v83W55fOWJ2g72x/UjVJBQ0sVjAngazU3ckc0TeNQlkcpSVGa/qBIHLfZraWtdrNAQT4A1fa sWGuJrChBFhtKbYXbUCu9AoYmmbQnsx2EWoJy3h7OjtfFapJbP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/lRbetHuNFtbh3xgFIJx 2RpgVDP4xB3f8r0I+y6ua+p7fgOjDLyoFjubRGed0Be45JJQEn7A3CSb6Xu7NYobnxfkwAGZ Q81a2XtvNS7Aj6NWVoOQB5KbM4yosO5+Me1V1SkX2jlnn26JPEvbV3KRFcwV5RnDxm4OQTSk PYbAkjBbm+tuJ/Sm+5Yp5T/BnKz21FoCS8uvTiziHj2H7Cuekn6F8EYhegONm+RVg3vikOpn gao85i4HwQTK9/D1wgJIQkdwWXVMZ6q/OALaBp82vQ2U9sjTyFXgDjglgh00VRAHP7u1Rcu4 l75w1xInsg== In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Thu, 19 Oct 2023 01:27:29 -0700 (PDT) On 10/18/2023 2:08 PM, Sean Christopherson wrote: >> 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. Well, reading "BIT_ULL(49) | BIT_ULL(54) | BIT_ULL(55) |" is hard. But Lemme remove those shifts not being used now. >> 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. yes, it is the case for VMX nested exception feature bit. > >> 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 why can't we simply remove tools/arch/x86/include/asm/msr-index.h? > >> 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? This is better! > >> + (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. hmm, I'm overusing macros, better do: #define VMX_BASIC_RESERVED_BITS \ (BIT_ULL(31) | GENMASK_ULL(47, 45) | GENMASK_ULL(63, 56)) Probably should also move VMX MSR field defs from msr-index.h to a vmx header file. > > 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. good idea, human beings are not good at such jobs by staring at it. > >> /* 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: I ever saw a TDX patch does most of this cleanup. > > 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; > -- Thanks! Xin