Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp2643769ybf; Mon, 2 Mar 2020 12:51:00 -0800 (PST) X-Google-Smtp-Source: ADFU+vtbL8QMCtMwYI6YNNYgdudyl7mj+C95ynZt0K1hX+fMYddl4fJnhIrFJuMUnzLm24oZbjxM X-Received: by 2002:a05:6830:4cd:: with SMTP id s13mr771183otd.181.1583182260444; Mon, 02 Mar 2020 12:51:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583182260; cv=none; d=google.com; s=arc-20160816; b=YAblR8sMTnBP8yBqvBp7Zwvdc4YdAFMoXPMDswkGo9pTw1SHAl3dWoLAEiKW3yqtD/ E3ZSUgCKIk2XTnnybA/JbQg0sokCjj4Gp9IYkefhW1vjPsQv8dsgVLYhj24IvcAHGjYl L3osiuqIK//D/VXM0YHZ8D/UPkZp5FB+KShdvVXGdRqDbj+tTImDIZqsKw4RbjLW1p3D e4JlbI/dAsKl4CF4cPfpMY15qAwHdc9+1fhhz1QqsVuM+2qkHHSMJY+AEIA1c/Ac2DK9 k++VKAtmKF9zEz12lGyPHgcjOi8mxLawxJb58B7etgWPFxMPadeic253uQOlrwrp9GsP lXIA== 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=uPCxpWh2jX8uow8/lvfk8Lq1cONFPW46zSghSu6rBaM=; b=tTSE6k8KcetR7MHKvupTJ2rGZwt5dcKMHjbiSOkDSs/DDUmxN8w5REK6krPYN8GWIU 5PQ6pZYzZG7LZmQ66I8UG0Fw24wX93ebYl6/tSCvGTsWwMue0FmZU3iIxNhwvEo+qvyD xa/kdnBG2sM1/OJCk9isD4EUSUC3VTwjjk+EkAhrPXmeTSejVybvGQJM9pMmlrRf/xQg oKSgiBHrOEzfUgyYrMI3nqcQvDvErSJ5dhi3oxFgoltf5ySeUXUnztCkuMKL/6zByUXk aOBfsGtlLW/sZfOUScCB0QDIeo9U/1Z4tR0NYUmcBwGi6AsgSfjD+7OH3f+c15HEzReK qyFA== 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 g35si41594otg.32.2020.03.02.12.50.48; Mon, 02 Mar 2020 12:51:00 -0800 (PST) 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 S1726954AbgCBUtl (ORCPT + 99 others); Mon, 2 Mar 2020 15:49:41 -0500 Received: from mga07.intel.com ([134.134.136.100]:13572 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725911AbgCBUtl (ORCPT ); Mon, 2 Mar 2020 15:49:41 -0500 X-Amp-Result: UNKNOWN X-Amp-Original-Verdict: FILE UNKNOWN X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 02 Mar 2020 12:49:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.70,508,1574150400"; d="scan'208";a="440279368" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.202]) by fmsmga006.fm.intel.com with ESMTP; 02 Mar 2020 12:49:40 -0800 Date: Mon, 2 Mar 2020 12:49:40 -0800 From: Sean Christopherson To: Jan Kiszka Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li Subject: Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range Message-ID: <20200302204940.GG6244@linux.intel.com> References: <20200302195736.24777-1-sean.j.christopherson@intel.com> <20200302195736.24777-2-sean.j.christopherson@intel.com> <188dc96a-6a3b-4021-061a-0f11cbb9f177@siemens.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <188dc96a-6a3b-4021-061a-0f11cbb9f177@siemens.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 On Mon, Mar 02, 2020 at 09:26:54PM +0100, Jan Kiszka wrote: > On 02.03.20 20:57, Sean Christopherson wrote: > >Rework kvm_cpuid() to query entry->function when adjusting the output > >values so that the original function (in the aptly named "function") is > >preserved for tracing. This fixes a bug where trace_kvm_cpuid() will > >trace the max function for a range instead of the requested function if > >the requested function is out-of-range and an entry for the max function > >exists. > > > >Fixes: 43561123ab37 ("kvm: x86: Improve emulation of CPUID leaves 0BH and 1FH") > >Reported-by: Jan Kiszka > >Cc: Jim Mattson > >Cc: Xiaoyao Li > >Signed-off-by: Sean Christopherson > >--- > > arch/x86/kvm/cpuid.c | 15 +++++++-------- > > 1 file changed, 7 insertions(+), 8 deletions(-) > > > >diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > >index b1c469446b07..6be012937eba 100644 > >--- a/arch/x86/kvm/cpuid.c > >+++ b/arch/x86/kvm/cpuid.c > >@@ -997,12 +997,12 @@ static bool cpuid_function_in_range(struct kvm_vcpu *vcpu, u32 function) > > return max && function <= max->eax; > > } > >+/* Returns true if the requested leaf/function exists in guest CPUID. */ > > bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, > > u32 *ecx, u32 *edx, bool check_limit) > > { > >- u32 function = *eax, index = *ecx; > >+ const u32 function = *eax, index = *ecx; > > struct kvm_cpuid_entry2 *entry; > >- struct kvm_cpuid_entry2 *max; > > bool found; > > entry = kvm_find_cpuid_entry(vcpu, function, index); > >@@ -1015,18 +1015,17 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, > > */ > > if (!entry && check_limit && !guest_cpuid_is_amd(vcpu) && > > !cpuid_function_in_range(vcpu, function)) { > >- max = kvm_find_cpuid_entry(vcpu, 0, 0); > >- if (max) { > >- function = max->eax; > >- entry = kvm_find_cpuid_entry(vcpu, function, index); > >- } > >+ entry = kvm_find_cpuid_entry(vcpu, 0, 0); > >+ if (entry) > >+ entry = kvm_find_cpuid_entry(vcpu, entry->eax, index); > > } > > if (entry) { > > *eax = entry->eax; > > *ebx = entry->ebx; > > *ecx = entry->ecx; > > *edx = entry->edx; > >- if (function == 7 && index == 0) { > >+ > >+ if (entry->function == 7 && index == 0) { > > u64 data; > > if (!__kvm_get_msr(vcpu, MSR_IA32_TSX_CTRL, &data, true) && > > (data & TSX_CTRL_CPUID_CLEAR)) > > > > What about the !entry case below this? It was impacted by the function > capping so far, not it's no longer. Hmm, the only way the output would be different is in a really contrived scenario where userspace doesn't provide an entry for the max basic leaf. The !entry path can only be reached with "orig_function != function" if orig_function is out of range and there is no entry for the max basic leaf. The adjustments for 0xb/0x1f require the max basic leaf to be 0xb or 0x1f, and to take effect with !entry would require there to be a CPUID.max.1 but not a CPUID.max.0. That'd be a violation of Intel's SDM, i.e. it's bogus userspace input and IMO can be ignored.