Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp525186pxb; Wed, 27 Jan 2021 13:54:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJwlEY974ChqgCo9cNRKOjhvJBGXIV2F/DeZZmIDxyoGA3dkm15uCi7V5pYcAndbrJmsdjN6 X-Received: by 2002:a17:906:d159:: with SMTP id br25mr8228825ejb.398.1611784473780; Wed, 27 Jan 2021 13:54:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611784473; cv=none; d=google.com; s=arc-20160816; b=0NuYDXCahLgqnf3nzUiEDkUmiZdqjsqyFZKj7W8gHAKQD1v6NfPxih5CjvY0Atn9h+ SeZvxMFriwip979szTYGTGSCAjwr5ZUYHF8lANbJZCKgo9UvllVrGosJNP08Vf1NsOXw EoiCbFpFUfMe8sVF0cf8WK2niT1N3f9tTgCrlpify39IpUhLR+JIcM+jQAonjMvynSRp qhyY++DABTzcXkLY6mAYAUVrUGF4qfRhyESaiD7ZdhlZTa6lVWJ8YrK9zrW3yJtwwNQg zZWl8uk5mUU9sCHCNChPXj8a73SXItg9BDPE6zHwdJ1I7+3Kw8dVfZDLmYmk3KD8X0bm lqvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=ydaRB29RJtt52FSXiTlXaP4Ln7O0VcYefbrxc6K6p2E=; b=f6ye27/3y6yKTHMTnqBEWGXPBO4Suq5l36czXPiHa0AtdINKSbrbttkUkdrAJWICKs gAmB7mevUdkNwodOeMLK5PZ5Qb8R7XNrwrA4U/s4OWE7TlxEjdwNOtkUzI3qXf+JBYYW iJxAGwJfglbvMzSVNCLiMFsjfHGYIdn7WgX6NKqWl4hfcwx8O1uOT4yI9ICSfJ7+5DgT ynXoK0xWWQmdR2pmsl0ekZ59seZKi7ENTiuPmcqUe44wyv+K7ABg5L0esV0Kouh9wIfb kswRGZthGw50x+VFMftIwTHivNZTQxCvmj6VRrLYq20x3Akv6G9qWmyKDEJfeGb8zcS7 QIyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bGqKgf67; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g7si1639405edr.423.2021.01.27.13.54.07; Wed, 27 Jan 2021 13:54:33 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bGqKgf67; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234863AbhA0JxY (ORCPT + 99 others); Wed, 27 Jan 2021 04:53:24 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:44650 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233073AbhA0Juo (ORCPT ); Wed, 27 Jan 2021 04:50:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1611740937; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ydaRB29RJtt52FSXiTlXaP4Ln7O0VcYefbrxc6K6p2E=; b=bGqKgf67KKIUzVBmXq3hC/mQqRaFij+zlZRuoDwEoSNK/va2Em4PDtXiw3shcrxVT2p2uk KKZUgmSO1LXoBRthbkboA1eSeo4RUb3Vza+qen87GqqyVtvKUoXXgR5UHDFPfsfm3LaEuT dJhxiaAVF0tbGanfCtSiBKKiTs7J6yU= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-534-pBG9u6L1Nbe4LMNa8CxCWw-1; Wed, 27 Jan 2021 04:48:55 -0500 X-MC-Unique: pBG9u6L1Nbe4LMNa8CxCWw-1 Received: by mail-ej1-f71.google.com with SMTP id jg11so437799ejc.23 for ; Wed, 27 Jan 2021 01:48:55 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=ydaRB29RJtt52FSXiTlXaP4Ln7O0VcYefbrxc6K6p2E=; b=Mo/N3g6SZL7M98yQPP1jhqYl/3I+KEpi5PD3+vBTXSRnnITMu8f6W3/7HqHbxbKKMb bkiIg3z/v2SaSLhimETNc/cHWDDC8iEZekWEmxJ867t8wksG4MV1zTdhgkKtt3mE2hOD w/N6wUpO1USYcyIfCIBJhUMBmYN+d3oYlEJy6FIu1V6z6ySIBjTOizxpPFXv5iz9LuOe 0ntbrC34A4+nu2k+W9Q/NGzYD3MCj6+y9KGQK15dVyC8X3W+6aWZetHQnXNedFCBCMlc rTIRJfoU5XqWfH8Q1o3GP6lPtUFQbNaoyiBi6UF1G1XqSxYfSfOpBPq99UOGT4/iaEAN XHlA== X-Gm-Message-State: AOAM5312tAexL2z99N1BVPGbW1RxIdG/U9rdfMNt56GGCBbocNgz5sYy XU1VKlrXKc7hUhvRiYX1/7iiWoy5Ev6xZ811gB7ogBIRGnPpGZBL8l+YRT4WM1W5F+mlnptYIpF f4WdN/ly9tr2mRZSsLm7jS7MT X-Received: by 2002:a05:6402:2346:: with SMTP id r6mr8473469eda.8.1611740934251; Wed, 27 Jan 2021 01:48:54 -0800 (PST) X-Received: by 2002:a05:6402:2346:: with SMTP id r6mr8473459eda.8.1611740934102; Wed, 27 Jan 2021 01:48:54 -0800 (PST) Received: from ?IPv6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id q4sm570488ejx.8.2021.01.27.01.48.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 27 Jan 2021 01:48:52 -0800 (PST) Subject: Re: [RESEND v13 03/10] KVM: x86/pmu: Use IA32_PERF_CAPABILITIES to adjust features visibility To: Like Xu Cc: Ingo Molnar , Thomas Gleixner , Borislav Petkov , "H . Peter Anvin" , ak@linux.intel.com, wei.w.wang@intel.com, kan.liang@intel.com, x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Joerg Roedel , Jim Mattson , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li References: <20210108013704.134985-1-like.xu@linux.intel.com> <20210108013704.134985-4-like.xu@linux.intel.com> From: Paolo Bonzini Message-ID: <5da57047-db15-adb6-f3bc-6152753d0b49@redhat.com> Date: Wed, 27 Jan 2021 10:48:50 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 27/01/21 07:04, Like Xu wrote: > On 2021/1/26 17:42, Paolo Bonzini wrote: >> On 08/01/21 02:36, Like Xu wrote: >>> >>> @@ -401,6 +398,9 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) >>>          pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED; >>>          pmu->fixed_counters[i].current_config = 0; >>>      } >>> + >>> +    vcpu->arch.perf_capabilities = guest_cpuid_has(vcpu, >>> X86_FEATURE_PDCM) ? >>> +        vmx_get_perf_capabilities() : 0; >> >> There is one thing I don't understand with this patch: intel_pmu_init >> is not called when CPUID is changed.  So I would have thought that >> anything that uses guest_cpuid_has must stay in intel_pmu_refresh.  As >> I understand it vcpu->arch.perf_capabilities is always set to 0 >> (vmx_get_perf_capabilities is never called), and kvm_set_msr_common >> would fail to set any bit in the MSR.  What am I missing? >> >> In addition, the code of patch 4: >> >> +    if (!intel_pmu_lbr_is_enabled(vcpu)) { >> +        vcpu->arch.perf_capabilities &= ~PMU_CAP_LBR_FMT; >> +        lbr_desc->records.nr = 0; >> +    } >> >> is not okay after MSR changes.  The value written by the host must be >> either rejected (with "return 1") or applied unchanged. >> >> Fortunately I think this code is dead if you move the check in >> kvm_set_msr from patch 9 to patch 4.  However, in patch 9 >> vmx_get_perf_capabilities() must only set the LBR format bits if >> intel_pmu_lbr_is_compatible(vcpu). > > Thanks for the guidance. How about handling it in this way: > > In the intel_pmu_init(): > >     vcpu->arch.perf_capabilities = 0; >     lbr_desc->records.nr = 0; > > In the intel_pmu_refresh(): > >     if (guest_cpuid_has(vcpu, X86_FEATURE_PDCM)) { >         vcpu->arch.perf_capabilities = vmx_get_perf_capabilities(); >         if (!lbr_desc->records.nr) >             vcpu->arch.perf_capabilities &= ~PMU_CAP_LBR_FMT; >     } > > In the vmx_set_msr(): > >     case MSR_IA32_PERF_CAPABILITIES: >         // set up lbr_desc->records.nr >         if (!intel_pmu_lbr_is_compatible(vcpu)) Maybe pass msr_info.data to intel_pmu_lbr_is_compatible? Otherwise seems okay, thanks Like. Paolo