Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp893272lqs; Tue, 5 Mar 2024 22:57:34 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXnJS2UB3SCqXNkJOIZt7zy9ht5IAt/8u4+jNZ5ZM0h/hdhX5ZjPA53UB9cW4KmyK/jq++jfUqa9jIYzDVpmJJe2HBAiA3VPd5iEck/TA== X-Google-Smtp-Source: AGHT+IFDUUW6Hwa7YYWqflCRxRGd+SJTszevdukMGdpMv4g7RtppOQrUf6oaUQU5TkHlQr7uj8FA X-Received: by 2002:a05:6359:4588:b0:178:688e:fb21 with SMTP id no8-20020a056359458800b00178688efb21mr4577602rwb.7.1709708254117; Tue, 05 Mar 2024 22:57:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709708254; cv=pass; d=google.com; s=arc-20160816; b=QaY5CTrBYrqaogr787/225sZ1lHfeisLIpN35C8R9nis5wv8gjt7KIXZ/a46RcI6ts WzDU66RX0Rc7xPrLhkeyILeXjw55B2z3wkIAfsINkI0D+8sTtFVDSXCNKJZOkchmsUoQ M73ctDZ65FnMS5UkEuIjIgT3Lv+2wYhTJy+sBxl9rDjor5eKwONDD5tTLMbaHpkIgrL9 CCwLHpzby0sUFTloorkPsz4Tb618C8AyQVwlvHVY2FK327oRvyzRQUgOfTPnQKHSt0On 2nh3GhDVTm1ZuFGVca744Sgy6HGV0Q26VJnjzhYbAPV8AOc1AJ5WArm/gKcX7q9YbQNr boMQ== 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=NGLZ5FtpCvj/bWH9ZwFDDfG5GKnhsR5ULA/bP2oFWRs=; fh=XoCmz3KuH+gjN5CFHB8C0V2GSNJQTYq42Ryz3lHBTck=; b=Fh/a+hazXZm+kaIPnFWFxEa3sODfpEYGf3G2he6bgQX6PzB5giuKN/yJ2daNODHvOr v2OY7PidMirSSqjA0pDI0kd8cje6ZV4Y2wPAszmzJs7Ybndx8ONzdmu4kOAmFvuaa9x0 69cM95JiViKTmtWBK9lgsnWs+Mz+Ci8loiskkXoHDnzQENjxoqfpMo7I6KHWIexwEjlD P3dNtSWD3RtA0bkPDbRHEN+dSZcwZQsbhi0z6aIpqCaapstFTc6M0Xo+8yP9mM7bwHlL s/fv+6qFeg5zKvreRG+7ZSmW2edVxaFaBYAcgJSAScgS41NJXh7ARpn+Fi6sLx2sXyHF 3XzA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=mTfJrO2I; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-93250-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93250-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 r9-20020a63fc49000000b005e4d67d3842si1150189pgk.468.2024.03.05.22.57.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 22:57:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-93250-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=mTfJrO2I; arc=pass (i=1 dkim=pass dkdomain=intel.com dmarc=pass fromdomain=linux.intel.com); spf=pass (google.com: domain of linux-kernel+bounces-93250-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93250-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 B43D028BE17 for ; Wed, 6 Mar 2024 02:48:33 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 64C046FBD; Wed, 6 Mar 2024 02:48:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="mTfJrO2I" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.15]) (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 96B1A6117; Wed, 6 Mar 2024 02:48:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.15 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709693304; cv=none; b=CDOumlU2c0kp/melMYV0+krVS/P6pNUrmJzXX4pxXpYznvyLI9j3xjtj0Jc7EoYeROf1tZIPx/vPUx825pbuvUB6sqg7WsP7Yb9rjGpSQbF1H7FJfzD74ufgp0lJh0hieaJvtKk9HXBqy4kdtMt5Qr9bCmkI0um2BZR+7WChx40= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709693304; c=relaxed/simple; bh=hHT9l3jfKJR+AUAfQNpmrgJhnGQrE6S26vFxeGU+PKg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=PlANCoX83mLFwHymQpMA3yzUicyM4E1XvlB5B8V+TAZr8HtgKX9yShX0ILwvL/LL2aIUkjfYqP/bA1qDoILGOCmq+H4hSga5IIHUxkSOZxLzkQa8Rz79D6E/TtEZoBaCb7dUBNqDu7Sd+ooqWnT+bHEoMdgKy8is2LLPBbXWovE= 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=mTfJrO2I; arc=none smtp.client-ip=192.198.163.15 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=1709693302; x=1741229302; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=hHT9l3jfKJR+AUAfQNpmrgJhnGQrE6S26vFxeGU+PKg=; b=mTfJrO2IJFM4Rq+2SKImLv4h29NC4q1TCqRz3nQepKDsEjk/ETmQUGGD f7QlazZWbRTpSz+rw1YeZ5+tOAOgxLVRf6m7oU98RVbc7+1gLyiZrXfOK eyE6OqjMMec3VizSn+HhqtJaWSE+TuCZkzIuNFK6uWinZhG4rFA6Ds9pT Z+WunrR/MJ7Z/siV7Z/SF/Y9aVk8BdPj99lYaqkNaExVEwH1eka84gOZl 2twizYDLxRQr6teBFDBI1a61PglvNgPlneN6eK0YkLdmtvF57V2JfcKtY Cxa4Joo+hmh+4gPfpC6gRvIhWlkz9UHyErD5JyXnRn6mH9okPBkLkL+0J Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11004"; a="4456616" X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="4456616" Received: from orviesa002.jf.intel.com ([10.64.159.142]) by fmvoesa109.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2024 18:48:21 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,207,1705392000"; d="scan'208";a="40473773" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.239.60]) ([10.124.239.60]) by orviesa002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Mar 2024 18:48:18 -0800 Message-ID: <8bb0e8d7-3f1e-48f6-b14b-23b47892dc4b@linux.intel.com> Date: Wed, 6 Mar 2024 10:48:15 +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 Cc: Sandipan Das , Like Xu , 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> From: "Mi, Dapeng" In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 3/5/2024 3:46 AM, Sean Christopherson wrote: > On Mon, Mar 04, 2024, Dapeng Mi wrote: >> On 3/1/2024 5:00 PM, Sandipan Das wrote: >>> On 3/1/2024 2:07 PM, Like Xu wrote: >>>> On 1/3/2024 3:50 pm, Sandipan Das wrote: >>>>> With PerfMonV2, a performance monitoring counter will start operating >>>>> only when both the PERF_CTLx enable bit as well as the corresponding >>>>> PerfCntrGlobalCtl enable bit are set. >>>>> >>>>> When the PerfMonV2 CPUID feature bit (leaf 0x80000022 EAX bit 0) is set >>>>> for a guest but the guest kernel does not support PerfMonV2 (such as >>>>> kernels older than v5.19), the guest counters do not count since the >>>>> PerfCntrGlobalCtl MSR is initialized to zero and the guest kernel never >>>>> writes to it. >>>> If the vcpu has the PerfMonV2 feature, it should not work the way legacy >>>> PMU does. Users need to use the new driver to operate the new hardware, >>>> don't they ? One practical approach is that the hypervisor should not set >>>> the PerfMonV2 bit for this unpatched 'v5.19' guest. >>>> >>> My understanding is that the legacy method of managing the counters should >>> still work because the enable bits in PerfCntrGlobalCtl are expected to be >>> set. The AMD PPR does mention that the PerfCntrEn bitfield of PerfCntrGlobalCtl >>> is set to 0x3f after a system reset. That way, the guest kernel can use either >> If so, please add the PPR description here as comments. > Or even better, make that architectural behavior that's documented in the APM. > >>>>> --- >>>>>   arch/x86/kvm/svm/pmu.c | 1 + >>>>>   1 file changed, 1 insertion(+) >>>>> >>>>> diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c >>>>> index b6a7ad4d6914..14709c564d6a 100644 >>>>> --- a/arch/x86/kvm/svm/pmu.c >>>>> +++ b/arch/x86/kvm/svm/pmu.c >>>>> @@ -205,6 +205,7 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu) >>>>>       if (pmu->version > 1) { >>>>>           pmu->global_ctrl_mask = ~((1ull << pmu->nr_arch_gp_counters) - 1); >>>>>           pmu->global_status_mask = pmu->global_ctrl_mask; >>>>> +        pmu->global_ctrl = ~pmu->global_ctrl_mask; >> It seems to be more easily understand to calculate global_ctrl firstly and >> then derive the globol_ctrl_mask (negative logic). > Hrm, I'm torn. On one hand, awful name aside (global_ctrl_mask should really be > something like global_ctrl_rsvd_bits), the computation of the reserved bits should Yeah, same feeling here. global_ctrl_mask is ambiguous and users are hard to get its real meaning just from the name and have to read the all the code. global_ctrl_rsvd_bits looks  to be a better name. There are several other variables with similar name "xxx_mask". Sean, do you think it's a good time to change the name for all these variables? Thanks. > come from the capabilities of the PMU, not from the RESET value. > > On the other hand, setting _all_ non-reserved bits will likely do the wrong thing > if AMD ever adds bits in PerfCntGlobalCtl that aren't tied to general purpose > counters. But, that's a future theoretical problem, so I'm inclined to vote for > Sandipan's approach. > >> diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c >> index e886300f0f97..7ac9b080aba6 100644 >> --- a/arch/x86/kvm/svm/pmu.c >> +++ b/arch/x86/kvm/svm/pmu.c >> @@ -199,7 +199,8 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu) >> kvm_pmu_cap.num_counters_gp); >> >>         if (pmu->version > 1) { >> -               pmu->global_ctrl_mask = ~((1ull << pmu->nr_arch_gp_counters) >> - 1); >> +               pmu->global_ctrl = (1ull << pmu->nr_arch_gp_counters) - 1; >> +               pmu->global_ctrl_mask = ~pmu->global_ctrl; >>                 pmu->global_status_mask = pmu->global_ctrl_mask; >>         } >> >>>>>       } >>>>>         pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << 48) - 1;