Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp598160lqs; Tue, 5 Mar 2024 10:31:40 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUjAXjZ7t4NOmNW5x8Du+oo8TF27KOiyNVAqAHdTRv4aVE0AdjLg0uiPSgqqE+RQGqHMUhYuFf1voyQs2mfypZkS2fo99sT9J2KHNXYmw== X-Google-Smtp-Source: AGHT+IHoOEQBbQY0q/fgXplPbZ2LYUROxfh/s9h+rFOE1v+n+vioNzBGZaVkfvYUJSLHD1QeBV7G X-Received: by 2002:a05:6a00:2183:b0:6e4:d201:a39a with SMTP id h3-20020a056a00218300b006e4d201a39amr12352559pfi.22.1709663500604; Tue, 05 Mar 2024 10:31:40 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709663500; cv=pass; d=google.com; s=arc-20160816; b=qOkF9SoMcRmxq2+dnBLQVmMCYu6uwBCRLe88jHINhKnwuFQS3fI2WdyddDj8ekZ2Fw D+MMC+TuNGsYgxz9wJfdF8Lx2o9YTHqlEdGNLVH7Fgx/CHCTbKiZDKLnA59FklpaUZ7a QEu6eIXXlw3aZYkpmev5pjmVjIZDaJIWZLHYeUiq6sV+lkwN+fbgF/CDcPeqza/UCI3X iKM306G9G22sr43gBbEXB3VGc6PIRmeqYeAY6IATFxjgLD7Z6WBNT+8JtWYALfYSenzm JuHCQZkAorsNBibNNiA9sF7Wkf0r1SYpHXYpHmM8zNegXfuoGAvhGLQY4OomcBFiRPse rK2w== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:dkim-signature; bh=ikksnMYVs1Gg6hYyDAjEo6Ig2VkOOvTHqTs/Rnh5Euk=; fh=Mlu0YyybA16f5y/RUKbxI/Htzr0akqhIQfep1lnRLn0=; b=iEYz2WZHsXLbT6W5QueCgN6gpaWbsZs6uXXdhkoAxOM9IoBhZgPpcFIBAMfOcXJy9b hFK/sXD2YZzubrL7knnyVhx3QBXN8PPGqjGOSVheuEUNGV5NL4CdVc/hXzJ6KtKX1p1d Ujf6hcMGr0C161Ae4dfQosarU7QXznYQ7F/Q/hhxdB3mC+ykZ9RNkOGkkTKAQZmwbWjD +N6hqVUkzRnPSDIGcP6P+4YlkWe4lD54WiiOfwGpRylTGTikdjD42qEpfiAEMBWb7kzn L7WXlw5/MUYBTq42KxiW0W7uTKzVwzkXMEKi/0cnvNWwdJTJmgTUEFyA8i4n4JHWRtg3 AwvA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oKmQIFTL; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-92829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92829-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id ln9-20020a056a003cc900b006e62591c4fbsi3589909pfb.308.2024.03.05.10.31.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 10:31:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-92829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=oKmQIFTL; arc=pass (i=1 spf=pass spfdomain=google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-92829-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92829-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 4331928C278 for ; Tue, 5 Mar 2024 18:31:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 66BFD1A5BA; Tue, 5 Mar 2024 18:31:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oKmQIFTL" Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 082491400B for ; Tue, 5 Mar 2024 18:31:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709663491; cv=none; b=F+QSzEa9QL+lT0DP5/h32a38e7zyDjRHIWdMWN3Rlo8tynUd0Ky2bbZ/9pCQgoh/GXdk7uHWhRoPJ/jv7P5oxBr1GnjSuhyCHrC6kR7QaMvIcNOQI9vAipZyHo1a8Za9RGojZjtFNuuUQWCmiReg0+LuJFV9e2CnYTQyH3i98S0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709663491; c=relaxed/simple; bh=6e4RQ/zZF0nuA79N7nCfJ/8Itu/N9ppQhCFZQB2XfD4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=KZvBi0iHjGnqfnOKIFzM56WM664xRYHDouA0OwyTCpzoeGbCEab+F1AH0IgMpZ/O+5aRDYS0LYw1Mmr+AzLFbHpQazjEKGIWOJDz9LafmZlIDduWreyp/GgW9Du53IJObJnVECplzJ/F5n5eW4UArLmBO0j5qzJCK9lie5MBw4Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oKmQIFTL; arc=none smtp.client-ip=209.85.214.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-1dc1ff3ba1aso46001115ad.3 for ; Tue, 05 Mar 2024 10:31:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709663489; x=1710268289; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=ikksnMYVs1Gg6hYyDAjEo6Ig2VkOOvTHqTs/Rnh5Euk=; b=oKmQIFTLaJO8dnHnYoyEq5yY9PjBBOtC7v+irzzJKSiFLsfHL3uotiwH3AvAUqZ7vL blt1jCNfeOTlvc5gz5JoEw8Sivq6QZq9HhtztqlyElVK5yUN7m/fx17k4LgodLFldiPH 65riXrnDQTEk3rxYmf9QtZzKt9GXKieWMwBYDRgODfyvQqWPxXytk8nojP8A9AmtkDbc asVn8b+E+gQPycTzpxp0Ba4q0DVKovmAY20BFB7A8l22khmNyRodzd2+F4vQDidH17L9 d/dLT3bwGH8XIBMvJscJMwVtpYHN7zEr9lhhAS0eUpjLTJAzdKpK5oMsDw/MvEo63Aei gGrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709663489; x=1710268289; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=ikksnMYVs1Gg6hYyDAjEo6Ig2VkOOvTHqTs/Rnh5Euk=; b=wMccgOWI9SzgM5pvwYLAvcGbWNLXeQ+3UvyCrH5XDUbM8zAtfvF9jaN+n7DfVdDRwK q77ePCjanxNBcvdjbtTDteXmue+JJsr0NA93/ULRYmF6fRszfsoO8CbRi5ofI9b7Kz33 tcPJ/1dddPnQIB3AW4970zpeQ5oqHFNiW1Pez1K75eg5bDfdqZrJURZCMCz70AUecUs5 EGf45x/4htYhe754oAyVgE8O3BX2CbsimXJ7sUzJNqekl6GFnoOwmK2Tmr0XTkZIbues OKfWFBH/WNkybOrUdWEAkYXYGJddATkGUCM6SZdh0EfDcMevTjjOdCMWq6ZPWKoqTUfa ng8g== X-Forwarded-Encrypted: i=1; AJvYcCWH7CsmV1K2FPk/WaUe6UQEMCQPB03zEtKt+ggXQb6N/1i43Q2eBbaGRPJmhncZgF6jt8BgjoRkeFRFMrw09SCkrxxGCNZdjcBxAZHN X-Gm-Message-State: AOJu0Yy7jIwgi9+pDaNu59xXZqpOrFAFYzOQOlDDX424Ludtw/O2RRrF MsnjicIlQ7+5/NuNOTztfMz4QDS0yWKAbtsUE3r5yQhRH1/mAivFVvjSdkUWeA== X-Received: by 2002:a17:903:124b:b0:1dc:fae0:9073 with SMTP id u11-20020a170903124b00b001dcfae09073mr3403849plh.32.1709663488958; Tue, 05 Mar 2024 10:31:28 -0800 (PST) Received: from google.com (60.89.247.35.bc.googleusercontent.com. [35.247.89.60]) by smtp.gmail.com with ESMTPSA id y9-20020a17090264c900b001dcdfbad420sm10314243pli.149.2024.03.05.10.31.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 10:31:28 -0800 (PST) Date: Tue, 5 Mar 2024 18:31:25 +0000 From: Mingwei Zhang To: Sean Christopherson Cc: Like Xu , Dapeng Mi , Sandipan Das , pbonzini@redhat.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" Subject: Re: [PATCH] KVM: x86/svm/pmu: Set PerfMonV2 global control bits correctly Message-ID: References: <20240301075007.644152-1-sandipan.das@amd.com> <06061a28-88c0-404b-98a6-83cc6cc8c796@gmail.com> <8a846ba5-d346-422e-817b-e00ab9701f19@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Tue, Mar 05, 2024, 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. > Sean, can you update your commit message with the table name of the Intel SDM and the version of the Intel SDM (2023 version). It was quite hard to find where exactly SDM mentioned this, since I was using the 2022 version. Thanks. -Mingwei > *sigh*