Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp582072lqs; Tue, 5 Mar 2024 10:05:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWDisdSpS3R6RRX3e4k2uHJxiZQEZdzx9ob+fzi2+t60x9trjnQDxERgMfSOlhGarQ4kO10FVKAa12DMvsRMIYtfz8oWAikyQ+StgJH3Q== X-Google-Smtp-Source: AGHT+IF7npwcJFciWLZ7WEwZXgK+UbDqcCRX26OgdtJheOIIHelKosPKz43G5997GeqTb1p4TTRa X-Received: by 2002:a05:6a21:3405:b0:1a0:f3d0:15af with SMTP id yn5-20020a056a21340500b001a0f3d015afmr2603502pzb.34.1709661917749; Tue, 05 Mar 2024 10:05:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709661917; cv=pass; d=google.com; s=arc-20160816; b=NnCJ+fbIOqqD7bPNR2LMhd14HX2EYrFovJe5x2kq3laa8xfg2sjkB6ct4YUJnVWG7j WHFvcsUrsKXXu1BNh5W/x/CNSzY/lv7pgCDZhpsd5LE5flS04c8HH67qN2nZhzejFbDi pDEJ+HkUs2fp1X56ERPIKvLpv9bVf+93iW9I2sZFwri+wzGuCqAFhI9BlJEnItRI1f3R XPuZ94wqxj/dXu4qfd+R99z2IMeL1N4tgx40qeLtNwxQAFCp9qebZ07QRGqv66wFznuN sZ0IN5OxD/5MDAUQXudfnz9mxWy2vYJ1XfKyW507/i8x6CDfxrLt4A4YDLHzcbjP4acO yKHQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=pEbrJLbsSQI/fw5oAKGC5MieHsStoqch1a0BdEdHBlY=; fh=TZYs+FY80pQa3i/Q4lFkNyCPF/wPn8ZFgV0dXqlMTl8=; b=MB6QWAfsHdQhh8jxyfSUKiJPd2zKmQcw0jc1nw7DkIqdKD6qWt0NSSCl3vskg0e0Bo Ir7JFFHaef60Xt71CP9MTuUEawZrSVFWlneZS5wh4NSgRRzfy3kv6OVoWSPO2KqA1/QB 67K4TuuSJanLzHBuEejCyrmFFv+8hHv1/KzAGBjswN3e6LOhwsWAdTCCe73zyyCwF6Nk 3TMt7qUwEsRDKUdItCszRcg9rj/THz+2Xc5jWEsxzK0haI2VSQW4pVY74/J9UdQ/vfgH MHLu4S0Oz0eComK8JyGRXP9go8tzPtK5r583YSow/YuhGOY/KRIbmDhCyThuEiJEvJvp qdbw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="Lx/uVdSM"; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-92806-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92806-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. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id i22-20020a63d456000000b005dc42a836a3si10136316pgj.625.2024.03.05.10.05.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 10:05:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-92806-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=@google.com header.s=20230601 header.b="Lx/uVdSM"; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-92806-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-92806-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 57E85288499 for ; Tue, 5 Mar 2024 18:04:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9715D18EBB; Tue, 5 Mar 2024 18:04:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Lx/uVdSM" Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 1819718EAB for ; Tue, 5 Mar 2024 18:04:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709661865; cv=none; b=FxbSKn2rY6anNliwwaCoZV1VmMoou2Wgrcz5sLsgaoaoi8/EsuXQTStai4p7wJ63GebW+scgjuGEhzWqENrqAI1GkI6dQxzEGDQy3hYqbiMYXfZ0efUG04s028meA0h0kl4B2zZyNK/CWR2WD39QteA2gmFdISmDjp7y0exgPzs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709661865; c=relaxed/simple; bh=KlteQmYHydWNBOpxdpzf+ptjubVxDcE/oTAcS8ClTCY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YM5V+I+4UydtpHhwp0L5hcVZQhWwURs5V+H8SDmSksVprad8VkFP3wnRQ9LYFecBoubPuU0lK/AbJn4g9QPX2LGqEqS6vDw9zXVBVzEK3LaxI5aHkXTMIINKZxDkVis9NCZ3Mfda4JGOqtqdB4HX6RhZv32p4axprLvQ7NvGM4s= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Lx/uVdSM; arc=none smtp.client-ip=209.85.128.201 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=flex--seanjc.bounces.google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-609ce331defso12884257b3.1 for ; Tue, 05 Mar 2024 10:04:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709661863; x=1710266663; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=pEbrJLbsSQI/fw5oAKGC5MieHsStoqch1a0BdEdHBlY=; b=Lx/uVdSMQf992dTYkIderi7voShRPjzxBfQcJleun8akA1s316Flk0UoDnRtcO4ujY fQZ5op5ngmWIv2bWeSdXOXGtl4JXj9Kpdr3Y8zgWYcxSX2rlwpsGCaXqWcDuovjIlpXw PLNAq3TtOamsHeLPsc2fwpSkQpStyv9N11mTrutYPZ+91/FZOohLvHFHGAQxXasM48Jj 6VEWFbuPEcTcMy5MwqTnLhq59/p/71Y2HA87+mTgy51kvGXXmwwr5LEJEf7s0DxscZVo qv4AoFzFE6xGIV4x1yogkHo9XQpCg6GwADtYrnDqHBvOr97T1KeA8N2tt4X9js+fMvZ2 ySQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709661863; x=1710266663; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pEbrJLbsSQI/fw5oAKGC5MieHsStoqch1a0BdEdHBlY=; b=WHpzM3+JHEguska1fm80aT09XOvFwqs9HdAQ/nP/er//bi4beSZ3VOceqI++VHv2cH 11VUpAB25HCeCUNtcJngq4ZkbjmcvEz3/mtzRgZp2cL5z8/DTjv5WIgTz7JJld4C9Fka CNHUnloHgR6ovHmEl9LWLJl0h5cHb8CvqJg+DEVmKUJuFzCaZMDiqzC8zsM67uD5HNAS RnyC/I9BOnDkYXcwKmjodh51rMDwd/tdMNlTEobETz6gG0L6Igm9RmxzDUnKnL5wN6Dd bVPg31GkhkRn7izIu8fdGluq3nDNq72S7QUIy6kbKO9u/Mkdx1NcH6GQm68n/KX76vNb d2bQ== X-Forwarded-Encrypted: i=1; AJvYcCX8H2zFmgrMX873ZpeEsJaH1k1pJ0bVO0L56Dmjg2KnmnYgGTZHsCkkX3RyjxGaFeuGwCgVlMApMkWZYzIBzzNkvoqGh9GshhKVaOMx X-Gm-Message-State: AOJu0Yy+ixZSjtiGfkug9G50U6AJ0xk38XNzaIIhlTr5onnsrqYO3Ce1 5l7h1RQHeoJRTa60+1URMWi6M4zawUX7UqB1iPVGa9QWveuZnl536K3jdXGTT8NfsAKJOpMA/64 4Kw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:3507:b0:609:3c79:dbf1 with SMTP id fq7-20020a05690c350700b006093c79dbf1mr3570872ywb.8.1709661863151; Tue, 05 Mar 2024 10:04:23 -0800 (PST) Date: Tue, 5 Mar 2024 10:04:21 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240301075007.644152-1-sandipan.das@amd.com> <06061a28-88c0-404b-98a6-83cc6cc8c796@gmail.com> <8a846ba5-d346-422e-817b-e00ab9701f19@gmail.com> Message-ID: Subject: Re: [PATCH] KVM: x86/svm/pmu: Set PerfMonV2 global control bits correctly From: Sean Christopherson To: Like Xu Cc: Dapeng Mi , 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" Content-Type: text/plain; charset="us-ascii" 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. *sigh*