Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp951311yba; Wed, 24 Apr 2019 12:19:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqzKRwHW0u2wl2L7OAU6QqRGk59vZEX8LvkDpqaOQNT9IkFEnkD31RVsnKIRmwWCn1AY/KBu X-Received: by 2002:a63:a35b:: with SMTP id v27mr15424216pgn.325.1556133559723; Wed, 24 Apr 2019 12:19:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556133559; cv=none; d=google.com; s=arc-20160816; b=LYM1E5gwuAnUNFXAr4r+/6BCJ4B/tZcMNaoF94864YZP/zG3AnnPfFaTEhGcfVr6cx kCZl43ITVtZR5+3uKJS9NEQlcNWISxYmvBB9fq9CeWlMMzsp1pun/fixZFkeFnbi/I6R f80FqVg1hNk0h2Ql/ujO3+0dwIiszPRUKM8gDu+R/BnfaPJmujPFE33xpowyyoJ3YbBN dwtdItCfTdgFe25exl2IteLMsPsbYuEu/V7hc1F8cvy+DJXsM+bpHUwdzYDVhoD9jkzk CsMMZY4LxhuPvSWvw8yT4011ar1vlEdDcjF9vSnSz3Lq6M16Vv4nexmoJGLoNnW107cJ +bfg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=fPXTtohmAWmrU4BpwTtTUZlq82aCf7QKaZqAbMhUQZw=; b=P7LDI8SCdbga9mAVVD2G6gaUhI56jhwNSDulQQ4bkQIOsIIC5mdXJTwBH2WZbUI4xd bVV79hU5qutF3IrHqRT/2Xqubp1NEb08KhZDAh+L80DZfUYpPkL9Vh+RtvZNjyXlJKbt atCW1w+RbM3tn8f/NZ+CuaAuMprjiyZOfyCxUwZ7X2gqLX0Hs+iS6SR0R9n1QbnQ3Zy/ fMx1d11gmtteoFI9wPMdvyhUZJ/ze2kZVJv+wmxlURpgmwN8xxQmqtSMxY8om5AwxRfy xq5tFv3TgwyTl9U60awKAqMdslG6XZVMT0i7THdaE9D3bJmbsHFoWqSNi6F++JusOomc Kqew== 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 x3si19151482plv.33.2019.04.24.12.19.04; Wed, 24 Apr 2019 12:19:19 -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 S1729565AbfDXOcj (ORCPT + 99 others); Wed, 24 Apr 2019 10:32:39 -0400 Received: from mga06.intel.com ([134.134.136.31]:15660 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726165AbfDXOcj (ORCPT ); Wed, 24 Apr 2019 10:32:39 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Apr 2019 07:32:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,390,1549958400"; d="scan'208";a="167504859" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.181]) by fmsmga001.fm.intel.com with ESMTP; 24 Apr 2019 07:32:38 -0700 Date: Wed, 24 Apr 2019 07:32:38 -0700 From: Sean Christopherson To: Like Xu Cc: kvm@vger.kernel.org, Paolo Bonzini , Thomas Gleixner , Len Brown , linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: x86: Add Intel CPUID.1F cpuid emulation support Message-ID: <20190424143238.GB18442@linux.intel.com> References: <1555915234-2536-1-git-send-email-like.xu@linux.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1555915234-2536-1-git-send-email-like.xu@linux.intel.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > + > + 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 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. > +} > + > #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)); > 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 >