Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1732602yba; Thu, 25 Apr 2019 05:00:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqxOFZ3FVocFI5Qd2kVNTl3K6M6BWJa36ZDNVgX9qEiMYRRxQhevurf0NlJ6gfCBTth3yf08 X-Received: by 2002:a65:51c5:: with SMTP id i5mr15028464pgq.189.1556193600367; Thu, 25 Apr 2019 05:00:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556193600; cv=none; d=google.com; s=arc-20160816; b=f1RQzS5h/5UuLS86R+1E8lZ9gMVF3FRtkTRB99PRtJZre19vr5AeiGFpc4UBMrRC4y 7OAGdCksHM2pc1lAL7bvIhYSmhkE5oNgA/QfrlvJ6Us4lhLfI/8p1sIWX4oBDDDtzb9g XbUNbzNNEFWMrdQfZrQFepRh2plwFUIUNNaWGwHns/T/SeL+0Wbk269EiVUTNNQ3MAkt 3HFISq+X6icl+xBmBW1+/9RJhdiSJ39BljQngyF/JPvSRBwZjkhc7aKN5ZiwImFJ/Wis 1ETtw6ov+R610XUhusJMrIURX2wuSS/O7xQjV+Ga369ZRLJ6yqtcwn9475i8ar6tVdcF Lx2w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id; bh=Pr48s3POreLCiEZpEkAWkFEkPc5/7sl/sTwxKUXC9pg=; b=PIfaYn6J9HaQjcPr0EDI56ZY2+E3j6C2w2qxnhhus1beY+tFT9SSymwpRjpJt/GNRb MG05/SoUX34w1AwMp2qedBgl1b+c6Uzp3/VPOOhaAr5KekFQvEVKVG71RdJrwlg/hDf7 rdUi98g4+eM4++wGw/+NVtzkNNlAWDHApUd+mgcCoE9NOIpk4FuTA2fgiKP3TULgUi2j DcIXnzNanIfQwIOCaQmBbi1LdAKv9+AJJfYzmxoBRdknrZQo/10IjEiQ3Rotfh0sa5BJ JzobbSC4ZTSnqj0v5auPq08HByIMCizCmZtULv07sNQsyzP78oiZb7MELQsEgWyEk4ps s74g== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 33si22452528plh.3.2019.04.25.04.59.45; Thu, 25 Apr 2019 05:00:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729661AbfDYGd0 (ORCPT + 99 others); Thu, 25 Apr 2019 02:33:26 -0400 Received: from mga04.intel.com ([192.55.52.120]:52113 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726229AbfDYGd0 (ORCPT ); Thu, 25 Apr 2019 02:33:26 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Apr 2019 23:33:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,392,1549958400"; d="scan'208";a="340598753" Received: from lxy-dell.sh.intel.com ([10.239.159.145]) by fmsmga005.fm.intel.com with ESMTP; 24 Apr 2019 23:33:24 -0700 Message-ID: Subject: Re: [PATCH] KVM: x86: Add Intel CPUID.1F cpuid emulation support From: Xiaoyao Li To: Like Xu , Sean Christopherson Cc: kvm@vger.kernel.org, Paolo Bonzini , Thomas Gleixner , Len Brown , linux-kernel@vger.kernel.org Date: Thu, 25 Apr 2019 14:30:09 +0800 In-Reply-To: <30857e40-05b6-0f4c-d07c-919de08c90ac@linux.intel.com> References: <1555915234-2536-1-git-send-email-like.xu@linux.intel.com> <20190424143238.GB18442@linux.intel.com> <30857e40-05b6-0f4c-d07c-919de08c90ac@linux.intel.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5 (3.28.5-2.el7) Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2019-04-25 at 14:02 +0800, Like Xu wrote: > On 2019/4/25 12:18, Xiaoyao Li wrote: > > On Thu, 2019-04-25 at 10:58 +0800, Like Xu wrote: > > > On 2019/4/24 22:32, Sean Christopherson wrote: > > > > Now that I understand how min() works... > > > > > > > > On Mon, Apr 22, 2019 at 02:40:34PM +0800, Like Xu wrote: > > > > > Expose Intel V2 Extended Topology Enumeration Leaf to guest only when > > > > > host system has multiple software-visible die within each package. > > > > > > > > > > Signed-off-by: Like Xu > > > > > --- > > > > > arch/x86/kvm/cpuid.c | 13 +++++++++++++ > > > > > 1 file changed, 13 insertions(+) > > > > > > > > > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > > > > > index fd39516..9fc14f2 100644 > > > > > --- a/arch/x86/kvm/cpuid.c > > > > > +++ b/arch/x86/kvm/cpuid.c > > > > > @@ -65,6 +65,16 @@ u64 kvm_supported_xcr0(void) > > > > > return xcr0; > > > > > } > > > > > > > > > > +/* We need to check if the host cpu has multi-chip packaging > > > > > technology. > > > > > */ > > > > > +static bool kvm_supported_intel_mcp(void) > > > > > +{ > > > > > + u32 eax, ignored; > > > > > + > > > > > + cpuid_count(0x1f, 0, &eax, &ignored, &ignored, &ignored); > > > > > > > > This is broken because of how CPUID works for unsupported input leafs: > > > > > > > > If a value entered for CPUID.EAX is higher than the maximum input > > > > value > > > > for basic or extended function for that processor then the data for > > > > the > > > > highest basic information leaf is returned. > > > > > > > > For example, my system with a max basic leaf of 0x16 returns 0x00000e74 > > > > for CPUID.1F.EAX. > > > > > > You're right and the cpuid.1f.eax check is unreliable after I checked a > > > few machines. > > > > > > > > > > > > + > > > > > + return boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && (eax != > > > > > 0); > > > > > > > > Checking 'eax != 0' is broken as it will be '0' when SMT is > > > > disabled. ecx > > > > is the obvious choice since bits 15:8 are guaranteed to be non-zero when > > > > the leaf is valid. > > > > > > I agree with this and ecx[15:8] makes sense. > > > > > > > > > > > I think we can skip the vendor check. AFAIK, CPUID.1F isn't used by > > > > AMD, > > > > and since AMD and Intel try to maintain a semblance of CPUID > > > > compatibility > > > > it seems more likely that AMD/Hygon would implement CPUID.1F as-is > > > > rather > > > > than repurpose it to mean something else entirely. > > > > > > If it's true, let's skip the vendor check. > > > > > > // I have to mention that AMD already has MCP CPUs. > > > > > > > > > > > > +} > > > > > + > > > > > #define F(x) bit(X86_FEATURE_##x) > > > > > > > > > > int kvm_update_cpuid(struct kvm_vcpu *vcpu) > > > > > @@ -426,6 +436,7 @@ static inline int __do_cpuid_ent(struct > > > > > kvm_cpuid_entry2 *entry, u32 function, > > > > > switch (function) { > > > > > case 0: > > > > > entry->eax = min(entry->eax, (u32)(f_intel_pt ? 0x14 : > > > > > 0xd)); > > > > > + entry->eax = kvm_supported_intel_mcp() ? 0x1f : entry- > > > > > >eax; > > > > > > > > If we put everything together, I think the code can be reduced to: > > > > > > > > /* comment about multi-chip leaf... */ > > > > if (entry->eax >= 0x1f && cpuid_ecx(0x1f)) > > > > entry->eax = 0x1f; > > > > else > > > > entry->eax = min(entry->eax, > > > > (u32)(f_intel_pt ? 0x14 : > > > > 0xd)); > > > > > > Based on: > > > > > > ECX Bits 07 - 00: Level number. Same value in ECX input. > > > Bits 15 - 08: Level type. > > > Bits 31 - 16: Reserved. > > > > > > how about using an increasing order: > > > > > > entry->eax = min(entry->eax, (u32)(f_intel_pt ? 0x14 : 0xd)); > > > > > > // ... more checks when eax is between 0x14 and 0x1f if any > > > > > > /* Check if the host cpu has multi-chip packaging technology.*/ > > > if (((cpuid_ecx(0x1f) >> 8) & 0xff) != 0) > > > entry->eax = 0x1f; > > > > As Sean pointed out, you cannot rely on the output of cpuid.1f to indicate > > the > > existence of leaf 1f. If maximum basic leaf supported is smaller than 1f, > > the > > data returned by cpuid_ecx(0x1f) is the actual highest basic information > > leaf of > > the hardware. > > I don't think so. > > > So using "entry->eax >= 0x1f" from cpuid.0H is and only is the right way to > > check the existence of leaf 1f. > > > > We can simply use (cpuid_ecx(0x1f) & 0x0000ff00) to avoid the unnecessory > > shifting operation. > > I borrowed this "unnecessory" shifting operation from host > check_extended_topology_leaf() and we may do better on this. > > > Besides, the problem of simply using cpuid_exc(0x1f) in Sean's codes is that > > we > > cannot assmue the reserved bits 31:16 of ECX is always 0 for the future > > generation. > > It's true cause the statement in public spec is not "Reserved = 0" but > "Bits 31 - 16: Reserved". > > > > > In my opinion, Sean's codes is OK and much simple and clear. > > If the host cpuid.0.eax is greater than 0x1f but actually it doesn't > have multi-chip packaging technology and we may want to expose > entry->eax to some value smaller than 0x1f but greater than 0x14, much > effort needs to apply on Sean's code. > > My improvement is good to overwrite cpuid.0.eax in future usage > from the perspective of kvm feature setting not just from value check. Alright, there is something wrong in your code that you haven't realised. When you do entry->eax = min(entry->eax, (u32)(f_intel_pt ? 0x14 : 0xd)); it changes the entry->eax if entry->eax > 0x14. So you cannot directly use cpuid_ecx(0x1f). At least, you need to cache the value of entry->eax, like: u32 max_leaf = entry->eax; entry->eax = min(entry->eax, (u32)(f_intel_pt ? 0x14 : 0xd)); //...leaf between 0x14 and 0x1f if (max_leaf >= 0x1f && (cpuid_ecx(0x1f) & 0x0000ff00)) entry->eax = 0x1f; However, handling in increasing order in totally wrong. Since it's to report the max the leaf supported, we should handle in descending order, which is what Sean does. > > All need to do is using (cpuid_ecx(0x1f) & 0x0000ff00) to verify the leaf.1f > > is > > valid. > > > > Thanks, > > -Xiaoyao > > > // ... more checks when eax greater than 0x1f if any > > > > > > are we OK with it? > > > > > > > > break; > > > > > case 1: > > > > > entry->edx &= kvm_cpuid_1_edx_x86_features; > > > > > @@ -544,6 +555,8 @@ static inline int __do_cpuid_ent(struct > > > > > kvm_cpuid_entry2 *entry, u32 function, > > > > > entry->edx = edx.full; > > > > > break; > > > > > } > > > > > + /* function 0x1f has additional index. */ > > > > > + case 0x1f: > > > > > /* function 0xb has additional index. */ > > > > > case 0xb: { > > > > > int i, level_type; > > > > > -- > > > > > 1.8.3.1 > > > > > > > > > > > > > > > > >