Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp2651893ybf; Mon, 2 Mar 2020 13:01:44 -0800 (PST) X-Google-Smtp-Source: ADFU+vueGM4mCsXYx58eFMbn/3PhXRrQD+iJGdA/UHLHQga+e5gncH8dEJlczbsRNXr/rzKXWr5g X-Received: by 2002:a05:6830:1e37:: with SMTP id t23mr897561otr.16.1583182903890; Mon, 02 Mar 2020 13:01:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583182903; cv=none; d=google.com; s=arc-20160816; b=mGAXrXirwZZ6ob0juyHfrK0gwWPpEGd2wZ8Sm0mrQqAAC7fB4mMbBrkoFUasliWNvY 2OmcXWT8Chb1zs07PsQfX/RpX8UM9mkInFHsx+E0GmuiKO2iAFArrUIrWCH+0QcKgn2L 4NnSWKL/7YypW0Tw8W/Ec5sZyGBkedIwFntrj2N74ONi9EXvfn1Bt0Fjb7KRrqWtEG3b 9dcqK+kAd9nW4D/5VSxYSpNudTEe7C67ejH/4k/L9CH0NOKeITBxylUhteywrROAiWut 2tWyEeawmKNQJ/b4pb+9ThnXxvJiFW4B+oYLs2p1caHBmR5npRcMFo9ycvV7b8bdh84e nVtA== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=b4mjPjTm6n4DXsNJQtEBf6lZMrcfCaZm4wUbBzz3/Ec=; b=RkHG7frSaH1QRPvHZ/XZ5BqWgC5eBhp6CDZzRIhzvOLOxqBjt0MPGLtTzFqXYy4Qoa autWtj0zqn4R7lh2+TxQgENXXVkaAwSErMcwmPidBz2FBeNGoOUffQgdQ8fUB1x5rlgY xAEnfV/14xFciFm3EqPkruq+ciwLytaL9gLXO547Hy1h2cCCas6mrgDinAWv4722St5b 4srNVPCW7/o5tpQ8AGAjB763WIxHxBdIrlTRuSj3qRQjPBDuLL3hYGhbIR86s9fXgrXw xQfhpT9zvr+b0dyFJz6WfVhQypsy1bAYx1NXyazHBwb2bF2VZseb2KtJD3D541yqjovI Fu5Q== 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=siemens.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q11si5153799oti.270.2020.03.02.13.01.31; Mon, 02 Mar 2020 13:01:43 -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=siemens.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726755AbgCBVAF (ORCPT + 99 others); Mon, 2 Mar 2020 16:00:05 -0500 Received: from gecko.sbs.de ([194.138.37.40]:33531 "EHLO gecko.sbs.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725911AbgCBVAF (ORCPT ); Mon, 2 Mar 2020 16:00:05 -0500 Received: from mail2.sbs.de (mail2.sbs.de [192.129.41.66]) by gecko.sbs.de (8.15.2/8.15.2) with ESMTPS id 022Kxcb9030366 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 2 Mar 2020 21:59:39 +0100 Received: from [139.25.68.37] ([139.25.68.37]) by mail2.sbs.de (8.15.2/8.15.2) with ESMTP id 022Kxc3c022116; Mon, 2 Mar 2020 21:59:38 +0100 Subject: Re: [PATCH 1/6] KVM: x86: Fix tracing of CPUID.function when function is out-of-range To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li References: <20200302195736.24777-1-sean.j.christopherson@intel.com> <20200302195736.24777-2-sean.j.christopherson@intel.com> <188dc96a-6a3b-4021-061a-0f11cbb9f177@siemens.com> <20200302204940.GG6244@linux.intel.com> From: Jan Kiszka Message-ID: <25cf56fc-3bed-fa1b-f8e3-62ac58663e09@siemens.com> Date: Mon, 2 Mar 2020 21:59:38 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: <20200302204940.GG6244@linux.intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 02.03.20 21:49, Sean Christopherson wrote: > 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. I think I've seen that, a cap to 0x10, with QEMU and '-cpu host# when providing intentionally bogus values to cpuid. Jan > > 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. > -- Siemens AG, Corporate Technology, CT RDA IOT SES-DE Corporate Competence Center Embedded Linux