Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp2083904lqg; Mon, 4 Mar 2024 12:17:47 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXG17KztTBTcmM0AzJk1wVBuJB9ebeFaw+As57kOFeerlLD123vG3rrboUektUBVhxHfrHJVc1MFzDkAlnfwoXAtNFdJ2FGy0OL5kqxnA== X-Google-Smtp-Source: AGHT+IG5xx3sfedOME0vGFtjMMpPYlUcNseqj4TZcO2trCfguKhG4LiutgPrvZGDrjnt5gM99ns9 X-Received: by 2002:ad4:58aa:0:b0:68f:30ec:f357 with SMTP id ea10-20020ad458aa000000b0068f30ecf357mr782867qvb.32.1709583466797; Mon, 04 Mar 2024 12:17:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709583466; cv=pass; d=google.com; s=arc-20160816; b=mpMJarO9mF6Tn5bs1SeGh4E0rAte4KZoOTF9UMFl++ys9TwQbOUPWbGIAX8/xjODrC joKoNqueyh+rq13iAVhyVxbKPm9w6BKX9NcSMJStaMFUqBho9tiHN4yi9Bdb+77wCFEa yfziih+kqQjljXa/GyuD2ky/1KUIzipA7gpszkg7dIG54JcoedmQfH6F1W1nCNEicTNo M5Qn7kRvL+Xx2IMvvnatmr/+bAjkJ3O0DJi6PMBEZzFj6Tq2SOjZw6ORqlv+/IcImFaA u73y5dlwrh2bxOYgbxfPnGzY9vrs3+BDqHycY08NvKALnIwuICf0GPTjAY8BwDdr8iV3 K3bQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=D/7HbYGpfhKc4R/AKhfixpmLppzl+6hWOAPaqaIiEWg=; fh=ltxTvYw6seodncSoAokTp7Q6PcDzSFPahAc1RERnFs0=; b=NRe4J5ZGAWqRlGa5OB2kxjaq9W/rGergkCibeWgzqlszHItyFNezcyOqMv2/4bUcIB x+Dn0jkN7NG5LuVcJ+O51g/UzKmy9UoGctWiZmbPCPltfiayoe1LIZ4A4APvzDYvOhAL up23nCXwJ4bHkR8VQD+2wIyhKESOd2JNDHnE5G83fQ3ocxeZCwpjjFxT/nJjqcoQvQiY u8Zn1vsHaJm2dfZi+bziCu0tCKjQPrN+vUJRIHcn8FZ4PihpHw2fGHvZOFh23wKDTU6V U5Ym5bnKGWJYDpHjxGnmoJ7/mAFaQKejQkB/y5kVz7VYVQwvY7hfbGCmF//oFTDeClMm iE7w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=by2EyQaH; 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-91217-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91217-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p8-20020ad45f48000000b0068f3fccc515si10457394qvg.394.2024.03.04.12.17.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 12:17:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91217-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=by2EyQaH; 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-91217-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91217-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 79AAD1C2226B for ; Mon, 4 Mar 2024 20:17:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0AF202E636; Mon, 4 Mar 2024 20:17:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="by2EyQaH" 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 0BCE37AE56 for ; Mon, 4 Mar 2024 20:17:11 +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=1709583435; cv=none; b=Nqq4xbc3I5u+o/VpVxuWFwh/0gSCQHEWeill6yA4ISldQUzIimKbD/FA0lOsvcsE/k8ZOKnfN2up4mfrh/0S53zsfDtdzyeKgzJRfrQFzZJtki9JGhQlC357MdSCBJROTBvSTs/1cQotaEUQ7kQy5dw7tFfURfZksVNG1LAH7YE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709583435; c=relaxed/simple; bh=kCxdGUblvKvmrHONTMdMca6j8MkXm6abJWjQTd229yc=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=Yd+0uWRteEm0+Q8HIuiK8Acqkz0C0P6tB8b3IqFXUC8PwyGcb7uWzyl/YFLrLl9pyTi+jQCbVHbrq6NCwX2HNrgIL85FbDS321k9ZkqV2MHnMKRe16JBDH2GCkRiLSn2iFMFHuwEvuoGHauJtAghKuP+oR9Bv9K1k8v4I0B/Vm4= 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=by2EyQaH; 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-1dc3b4b9b62so42204365ad.1 for ; Mon, 04 Mar 2024 12:17:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709583428; x=1710188228; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=D/7HbYGpfhKc4R/AKhfixpmLppzl+6hWOAPaqaIiEWg=; b=by2EyQaHVwydyFEQCEoHVfjcgI0mF3hZe2jZxg6WiuzmtEvNc8wniDcqGcQwE3bQ91 x24NuJOYcGnaX6HM9nVs+UQg6A+7mG+g3jU/s+deaEiPCTvjBEq+9nlzt6FqCVZXf/zR zZ0/MnFYGlGVF2oTWoE/f5PdolfOnBeODcnXT3FxNcQ+srDsBVXX5a1X+n3UGYCEAoDM e0u1xoyCGSyOYBU7K/58kQoKSbTWReHKx7CH7XzWbFCpSWoH0t/YTSh89aYHaeh9VHw5 bl52ySUgLVUaA0Ekn+o+ZwP928cnIYkKuzF8hwpugZ7jiKXU2J4B9O1Rz2qYFsbpZNiw W2Lw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709583428; x=1710188228; h=in-reply-to:content-transfer-encoding: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=D/7HbYGpfhKc4R/AKhfixpmLppzl+6hWOAPaqaIiEWg=; b=wfZe2OgyF4pjUvuhNi/+7yMB5/AF9IadvRep6IoFbYbGpBSudnB6vK9uo7peQcf1Al XU3v9eCJQpx9GqKNLRGCvssMObXEanbI+v09KmtaQS5Z3BB32gD9TuRtXl3V4hHYklcp OiLzdfwVMuVaXCiX67ekuDyYMuz99rI3VcGG/+fmf8N+AZeFyTseoY35Vz3KRrm28bbp gH3FYQmxqoHEyMpY6g46bBpEEaJ2sgkrcrq0ExTwfu94F9V4dgYSnzYSSC5c4z/ldXil 0UlSK3J1FitUv53UtLi6GaGjN5toc/5WqXNUqpuDo643DzxLPiLV066715cQlWhpo67l Fc3w== X-Forwarded-Encrypted: i=1; AJvYcCUCc/RAyG46xcbAkYnUNNi+Wr5cT4iNC3JDlAKeX+ZfvC/re5SVJHNvmUmpkbyWcbYM1gTBFtOGbCx0T/i6b8LaC36163qF6SJC05K5 X-Gm-Message-State: AOJu0Yz/ff2JM7nGI545NnwPOC7Tkkk4uF7tBqQXXNOezRhQCQsy3EBX 1uVI3oCRbMUsHGxWQZ/aj1dIICa3jPc8PwQhDofxxDfJoJV+xOhvNXmaC3lyuQ== X-Received: by 2002:a17:902:dac2:b0:1dc:a28a:52b9 with SMTP id q2-20020a170902dac200b001dca28a52b9mr772318plx.8.1709583428036; Mon, 04 Mar 2024 12:17:08 -0800 (PST) Received: from google.com (176.13.105.34.bc.googleusercontent.com. [34.105.13.176]) by smtp.gmail.com with ESMTPSA id c5-20020a170902d48500b001db9cb62f7bsm8985870plg.153.2024.03.04.12.17.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 12:17:07 -0800 (PST) Date: Mon, 4 Mar 2024 20:17:04 +0000 From: Mingwei Zhang To: Sean Christopherson Cc: Dapeng Mi , Sandipan Das , Like Xu , 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> 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=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Mon, Mar 04, 2024, 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 > come from the capabilities of the PMU, not from the RESET value. > +1 > 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. > right. I am ok with either approach. Thanks. -Mingwei > > 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; > > >