Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp868057lqs; Tue, 5 Mar 2024 21:37:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWCbs3dCw77L2E4oYZ1pg6/BjyI90Kr8BR54A84Z7TjRhPlReX3ic92AA1cJrbjGEjtHtiR9qqlrqurjKaBoFYKH87s47l+9A3HTkAXxA== X-Google-Smtp-Source: AGHT+IGwz0uNFxOOZvWc4TpPcX3K3UjYjRjJ7CzhkpfWsbMuiF0f67yKf8yVakk7cGtqwFOQGWTg X-Received: by 2002:a05:6808:1806:b0:3c1:83c3:969b with SMTP id bh6-20020a056808180600b003c183c3969bmr4915216oib.53.1709703461553; Tue, 05 Mar 2024 21:37:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709703461; cv=pass; d=google.com; s=arc-20160816; b=bhR1I77yfLVHpn4mxzFFaQ9YMGLgtXSvS0eSwdc+ZVwUdTPaCoMT0ynLOVtCOMfoex Ua4ADV65+l613EXHeZWfB0SeWH388MD0Ba2l6eHYkKPAoGjrFwgpLwf1bUyht/uCir54 A9yRAhvHNIoHyFw+FsN+0E81STd+RvnW+9knUxNJcSL3jqpOODMNKKRnhfm+emUSQq5t DpK8nnxdHkpDN58rd75E7GlEpYNAFPMd/9Cg7Uf5MQ5KEM36OM6i3MHxcmDQ9mLGJmr0 zoM0388Nbrd4yHw+2ADHz6131+9/+cOctVG3hLbCSQ+QRVZPkz3o0oAmLRcXjdet6+Fw VOBw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id:dkim-signature; bh=C5fIUJbtoQqwnPPcOq6ve5qYq3QKXlIA07oRej06OR8=; fh=ifEDYMPdVqmKtp+IPYwnMliNNkit3qc8RcTzknIfiYA=; b=TzskMIbq0Njw+z5FWyq7jyPG29Gat4gDoe8roR3HaySRPiyYq8fayy83YHr1hvErq2 81ijkon3b1IrG57UgcbTT7r/YC8YPf8hz2Abaf6wTnChRhLtMkw912Z9vlCSomHKPKRK O/Q7MIBq2HvQlYBXdM//W+ajItxS4+jh6pAFZtC+JSs1s3txb3DTpUi2pehD/oD/lrzi D4U4RoPnfgVk7phPwCBZbXxXxCB0YzlWVcJTfZlQ7mPEcuvNOhbF2ryBztgcf7Re6wTM TP3vsR8B7J0T916KxZZdflqHn1T51Jq2ogJFZBso/o+V3EiB/uZ1mbUbXBrdPJxEGduT 8JOQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mSuSfBfO; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-93244-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93244-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id s39-20020a056a0017a700b006e6276ac8acsi4301999pfg.340.2024.03.05.21.37.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 21:37:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-93244-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mSuSfBfO; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-93244-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93244-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E957C289E5E for ; Wed, 6 Mar 2024 02:33:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B53E0D51E; Wed, 6 Mar 2024 02:32:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mSuSfBfO" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.14]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1487117E9; Wed, 6 Mar 2024 02:32:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709692370; cv=none; b=XOpqtK4Kqf3NyDPYcBueUPW7JDv414EtzSF0DMx2jT+cDcSE8onn1IVgKf4+gnMTWVHkItnudmyGspPp/m7fsk+EffJ8KsAtEEnXgjQyHk0fTxEY2aG6ziemMcEnoTzE6ymhhuW72iChkFdoonxo0Bbd5p1UOgEs395pmDbyVXg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709692370; c=relaxed/simple; bh=aIGfNej6zLoTMAs10B0Xo/VB2CyyZGbqbt8RV9b3g5U=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=A80myY/uEcM5ZzsWNB1/lbQceQ2NYtCuPhlRE2O7ZH36ziyAIjiGbfynCbONNnzDvLQj4esR2jG0QQi3gKbT5Wy7dZUdvRla8hiw4FqcFcNaVqUgkNkot3GujUgv03ip0K4EgOrntlvICSvoYL91umjA0uKgcmxlQdoZlvD/Qus= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com; spf=none smtp.mailfrom=linux.intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=mSuSfBfO; arc=none smtp.client-ip=198.175.65.14 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=linux.intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1709692369; x=1741228369; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=aIGfNej6zLoTMAs10B0Xo/VB2CyyZGbqbt8RV9b3g5U=; b=mSuSfBfOVUNhIZCB1nAOq/Fq8ZGUHNBEYAbHmRNsWe9n2iaBRo/mqypb 9lSTimV25CsGkka1zqTXKLOlqMjXFAtnPwJ7N9cCJOzgIbot7bah0nEXz z6AAgeoArvsr2hYsXJHt2tWP2zsaooLAaDZhiUb1vmHu5ekogWBHCrEbO y0698bj0V5b6rm39gfMJLAkSudqp14QvW32BJolg9vu2yH4mtIcIqrUuE 7paE0xirq7G8xmKKPIUePD1GlI7UBW5h4rlYuC21/ZRnS2nXGbSwOkJ8p uH0rQtgrDUtL3/6zPTEWxGm2EJbT8H3bnBoiDiWqucqTRQzqRvE9/LD4Z Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11004"; a="8103554" X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="8103554" Received: from orviesa007.jf.intel.com ([10.64.159.147]) by orvoesa106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2024 18:32:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="10015749" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.239.60]) ([10.124.239.60]) by orviesa007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2024 18:32:45 -0800 Message-ID: <9f7f5e0b-de05-49f1-941f-29349d1b9280@linux.intel.com> Date: Wed, 6 Mar 2024 10:32:41 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] KVM: x86/svm/pmu: Set PerfMonV2 global control bits correctly Content-Language: en-US To: Sean Christopherson , Like Xu Cc: Sandipan Das , pbonzini@redhat.com, mizhang@google.com, jmattson@google.com, ravi.bangoria@amd.com, nikunj.dadhania@amd.com, santosh.shukla@amd.com, manali.shukla@amd.com, babu.moger@amd.com, kvm list , "linux-kernel@vger.kernel.org" References: <20240301075007.644152-1-sandipan.das@amd.com> <06061a28-88c0-404b-98a6-83cc6cc8c796@gmail.com> <8a846ba5-d346-422e-817b-e00ab9701f19@gmail.com> From: "Mi, Dapeng" In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 3/6/2024 2:04 AM, Sean Christopherson wrote: > On Tue, Mar 05, 2024, Sean Christopherson wrote: >> On Tue, Mar 05, 2024, Like Xu wrote: >> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c >> index 87cc6c8809ad..f61ce26aeb90 100644 >> --- a/arch/x86/kvm/pmu.c >> +++ b/arch/x86/kvm/pmu.c >> @@ -741,6 +741,8 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu) >> */ >> void kvm_pmu_refresh(struct kvm_vcpu *vcpu) >> { >> + struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); >> + >> if (KVM_BUG_ON(kvm_vcpu_has_run(vcpu), vcpu->kvm)) >> return; >> >> @@ -750,8 +752,18 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) >> */ >> kvm_pmu_reset(vcpu); >> >> - bitmap_zero(vcpu_to_pmu(vcpu)->all_valid_pmc_idx, X86_PMC_IDX_MAX); >> + bitmap_zero(pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX); >> static_call(kvm_x86_pmu_refresh)(vcpu); >> + >> + /* >> + * At RESET, both Intel and AMD CPUs set all enable bits for general >> + * purpose counters in IA32_PERF_GLOBAL_CTRL (so that software that >> + * was written for v1 PMUs don't unknowingly leave GP counters disabled >> + * in the global controls). Emulate that behavior when refreshing the >> + * PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL. >> + */ >> + if (kvm_pmu_has_perf_global_ctrl(pmu)) >> + pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0); >> } > Doh, this is based on kvm/kvm-uapi, I'll rebase to kvm-x86/next before posting. > > I'll also update the changelog to call out that KVM has always clobbered global_ctrl > during PMU refresh, i.e. there is no danger of breaking existing setups by > clobbering a value set by userspace, e.g. during live migration. > > Lastly, I'll also update the changelog to call out that KVM *did* actually set > the general purpose counter enable bits in global_ctrl at "RESET" until v6.0, > and that KVM intentionally removed that behavior because of what appears to be > an Intel SDM bug. > > Of course, in typical KVM fashion, that old code was also broken in its own way > (the history of this code is a comedy of errors). Initial vPMU support in commit > f5132b01386b ("KVM: Expose a version 2 architectural PMU to a guests") *almost* > got it right, but for some reason only set the bits if the guest PMU was > advertised as v1: > > if (pmu->version == 1) { > pmu->global_ctrl = (1 << pmu->nr_arch_gp_counters) - 1; > return; > } > > > Commit f19a0c2c2e6a ("KVM: PMU emulation: GLOBAL_CTRL MSR should be enabled on > reset") then tried to remedy that goof, but botched things and also enabled the > fixed counters: > > pmu->global_ctrl = ((1 << pmu->nr_arch_gp_counters) - 1) | > (((1ull << pmu->nr_arch_fixed_counters) - 1) << X86_PMC_IDX_FIXED); > pmu->global_ctrl_mask = ~pmu->global_ctrl; > > Which was KVM's behavior up until commit c49467a45fe0 ("KVM: x86/pmu: Don't overwrite > the pmu->global_ctrl when refreshing") incorrectly removed *everything*. Very > ironically, that commit came from Like. > > Author: Like Xu > Date: Tue May 10 12:44:07 2022 +0800 > > KVM: x86/pmu: Don't overwrite the pmu->global_ctrl when refreshing > > Assigning a value to pmu->global_ctrl just to set the value of > pmu->global_ctrl_mask is more readable but does not conform to the > specification. The value is reset to zero on Power up and Reset but > stays unchanged on INIT, like most other MSRs. > > But wait, it gets even better. Like wasn't making up that behavior, Intel's SDM > circa December 2022 states that "Global Perf Counter Controls" is '0' at Power-Up > and RESET. But then the March 2023 SDM rolls out and says > > IA32_PERF_GLOBAL_CTRL: Sets bits n-1:0 and clears the upper bits. > > So presumably someone at Intel noticed that what their CPUs do and what the > documentation says didn't match. It's a really long story... thanks for figuring it out. > > *sigh* >