Received: by 2002:ab2:3141:0:b0:1ed:23cc:44d1 with SMTP id i1csp2068151lqg; Mon, 4 Mar 2024 11:46:39 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXx+N9IEZMrP/tnEEJxB6i3N8j5QS5f7zPzHOSHrkOCxRxAgSVdqv+4o+t78/UuGNB67B1W89bhqmV6IXT6LH8gGw1pq/Bc2sP6an3feA== X-Google-Smtp-Source: AGHT+IGQx4eB7zVx4wcyv0m9GxY8qo3DzVky2vT5FLTCo0GD6qDHe1uIkRGqnXAfOk3EZLGRoTTA X-Received: by 2002:a05:620a:450b:b0:788:2443:185a with SMTP id t11-20020a05620a450b00b007882443185amr5500894qkp.31.1709581599141; Mon, 04 Mar 2024 11:46:39 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709581599; cv=pass; d=google.com; s=arc-20160816; b=z7o0uk1mHl83k0Si6+STeejL+0tDb+OJfLwdzdhzFUSY0H3lY/8SwNLR8ph8KCZjpE oJHOtXuszVkd5l6TnAEyyhuy20Z4BuY6CYCIZLAxGY/r5BN5Q5455lgSoRlN5bVBNq7i exUj/Y5S7F3UTlYoRbrqFko0Jh1fubgS3CO5ZWaN7SAIwUrlz/jsj0Kx26sH6jNDsHKF haByDgXknCh6OVCO6r811uIX4C1MniyA2RmvuPUtN7u85+vZcita3vw77E08SXRJg4jO 77CAki9muABoAi2p9YO4NtvihPxmgvFYHhcdJYEIJLI5W2FdMED7k+CoyFGTUrybA2uE t2pg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :in-reply-to:date:dkim-signature; bh=VWzHdRna3zMy+XLYBNvG6plLaOTh5XkeubVVBNUfIw0=; fh=rW/9ZI29AoW4pw4Kt6VeqAr6199xw/lCaEyQ7FSiOws=; b=cNhyuiwXz5yx/Rg9IFOyBUeqgtKkVlK5QvD6kGw++YpFlxCe2vvAWX15TxH4sfF8kq kaWQ+VZp7WywRgaXRROYU0OuFhXFpOLC9qNyo9gJ4FK3jI8x9iXCJ9A6Ess6MbypbuyC k5V+IxQFjenUn6zz6CSYcxg9QqgIHUdSUx6+aDtMtthZ6ihB0QKm3niI4YD0Bg64eykL rQTD519APsMJMPn6s4aJIVK/vW6cOBXdHA4l8aVrj4/X9NlMHerGai3RAeopK1GFBm3c +adsk+Pzn+MAI+GRDzVUFbUfXldlCWshqJGAWHchFzbTuhEB+sOaP5Phl4zYkedsJH3D 7Syw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=if4l83O9; 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-91188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91188-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. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id wh15-20020a05620a56cf00b007882901e27bsi2769262qkn.30.2024.03.04.11.46.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 04 Mar 2024 11:46:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-91188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=if4l83O9; 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-91188-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-91188-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 CD5F11C21261 for ; Mon, 4 Mar 2024 19:46:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 80D5279DDC; Mon, 4 Mar 2024 19:46:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="if4l83O9" Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 124FC4653A for ; Mon, 4 Mar 2024 19:46:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709581588; cv=none; b=sW4fAQwArsEGq4+lvSOEoD+JFgt2DqYK2OJpZSpaUpS+VFZA9v8xAMEtejh6/fm/kg9mvedjmPfOnre9FsaUzNmdQwiMrc2ZmDozmTtvr7EW2ths/e8+F24aWHZinsRsSqzznu9Zk3/XFQ2vLWwT5x+WdXiQefEq+e8JD8K6M3E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709581588; c=relaxed/simple; bh=aOqHk6SVd7G59dSUt4yGvRnSNmhLPrAQTYd8u3xfOEA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DdD9W+3cCzwbuU1T4LSmAt6JbgaZF0btLNd+h8YRqwOJ0g7d2c5RFJAgT+dwdaQ0GbY3iY1+TcbgBycqgf4KvIt6wQvU5RJ3boMlpDD/nX1MA4M5mG3ZW2h9cJznlPPhpIHD30ZrJk5gsYnUXYzPdRxA2/zV8fViyKRHNEDDqPs= 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=if4l83O9; arc=none smtp.client-ip=209.85.219.202 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-yb1-f202.google.com with SMTP id 3f1490d57ef6-dc6b26eef6cso6608635276.3 for ; Mon, 04 Mar 2024 11:46:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709581586; x=1710186386; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:from:to:cc:subject:date:message-id :reply-to; bh=VWzHdRna3zMy+XLYBNvG6plLaOTh5XkeubVVBNUfIw0=; b=if4l83O9zqyZF+RJplfo5dSbH52VmsnlZcb+cEBy+Lu4BBdvRbRyDcv4zlSU83N7P7 UDaQLd1QTNkBkKokjLT7a3ShrDisPSDo4wJD0cBJ/ucXhCYN3nNmDJZnobOWiT2YBPbK AJRAmwIu+/Co35w2pOV16b8FkR894pC8PTLgb4FaJA+twPq/cAA7cCGZINXCEPI7MbyX CphkbzYw/qG7Li38Qr9yfmKMo7DRF10VrDTBRaZGqxWptKn/4RXSMtR7JzwpU2kVVXo+ dMHZmWrAzUk1+W+UA6P94dutGKyXKz4CsYsmuTl4XrmqXWgbqYvmszayMSGMHz+kpvdc vT5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709581586; x=1710186386; h=content-transfer-encoding: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=VWzHdRna3zMy+XLYBNvG6plLaOTh5XkeubVVBNUfIw0=; b=M3ScB20bwWZNKB2iToOxOcGVY0kDm2S2F1XZ8KVwT9EeYXZXnACDg8hCKCVLwImKMP C4CuzGSh85GlzrBmt6w6Ap6ooR+5MStckQsojS9NHWkqQjezH1SVKcp8KJJEFPBFPXR1 mRKrEVqDZXNXWnk05lM+JE1vL7xrmekx8e7Kp5bPMA0FMHQDHY/bQ0srZqqGFdxgYxRh XI7OJKoGGs8FuXFOrLo1ZTEOOCDuakiI/MHnbrLoOTEERu3z4LZsHL/js6gWw7eewfvX d2QQegg8wvA1K+NMlHM3N4n+vQ+vhPDT8O9lSObYszDaoNtmbGWSBUtPOjpPGcRJq9Rr A/PQ== X-Forwarded-Encrypted: i=1; AJvYcCVH/Rel8a3e2RorpMEi+hwC3grrIItRL72H7oJBSw3WxPpAPfsW63dXttO5FaLc82Qzn3ePofBK1me1msAHm33ty/woQtzhL6SlDaCb X-Gm-Message-State: AOJu0Yyg4HrBAWcYM9aqfody6mtXJR0bNryV0JQ+zhgsQ1mLTZu8pl43 n0FPUf86aEQ7fAZc3DEbumn5yHwJPIFxwCZjiJtn+qjspHI41NfaZ+JUbwZHSXn/sCJ6RCd4zPy ahQ== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1143:b0:dc7:6cfa:dc59 with SMTP id p3-20020a056902114300b00dc76cfadc59mr389188ybu.4.1709581585728; Mon, 04 Mar 2024 11:46:25 -0800 (PST) Date: Mon, 4 Mar 2024 11:46:24 -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> Message-ID: Subject: Re: [PATCH] KVM: x86/svm/pmu: Set PerfMonV2 global control bits correctly From: Sean Christopherson To: Dapeng Mi 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" Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 04, 2024, Dapeng Mi wrote: >=20 > 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 operati= ng > > > > only when both the PERF_CTLx enable bit as well as the correspondin= g > > > > PerfCntrGlobalCtl enable bit are set. > > > >=20 > > > > 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 a= s > > > > kernels older than v5.19), the guest counters do not count since th= e > > > > PerfCntrGlobalCtl MSR is initialized to zero and the guest kernel n= ever > > > > writes to it. > > > If the vcpu has the PerfMonV2 feature, it should not work the way leg= acy > > > PMU does. Users need to use the new driver to operate the new hardwar= e, > > > don't they ? One practical approach is that the hypervisor should not= set > > > the PerfMonV2 bit for this unpatched 'v5.19' guest. > > >=20 > > My understanding is that the legacy method of managing the counters sho= uld > > still work because the enable bits in PerfCntrGlobalCtl are expected to= be > > set. The AMD PPR does mention that the PerfCntrEn bitfield of PerfCntrG= lobalCtl > > is set to 0x3f after a system reset. That way, the guest kernel can use= either >=20 > If so, please add the PPR description here as comments. Or even better, make that architectural behavior that's documented in the A= PM. > > > > --- > > > > =C2=A0 arch/x86/kvm/svm/pmu.c | 1 + > > > > =C2=A0 1 file changed, 1 insertion(+) > > > >=20 > > > > 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 *vc= pu) > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (pmu->version > 1) { > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pmu->global= _ctrl_mask =3D ~((1ull << pmu->nr_arch_gp_counters) - 1); > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pmu->global= _status_mask =3D pmu->global_ctrl_mask; > > > > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pmu->global_ctrl =3D ~p= mu->global_ctrl_mask; >=20 > It seems to be more easily understand to calculate global_ctrl firstly an= d > then derive the globol_ctrl_mask (negative logic). Hrm, I'm torn. On one hand, awful name aside (global_ctrl_mask should real= ly 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. 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 purpo= se counters. But, that's a future theoretical problem, so I'm inclined to vot= e 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); >=20 > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (pmu->version > 1) { > -=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 pmu->global_ctrl_mask =3D ~((1ull << pmu->nr_arch_gp_counters) > - 1); > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 pmu->global_ctrl =3D (1ull << pmu->nr_arch_gp_counters) - 1; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 pmu->global_ctrl_mask =3D ~pmu->global_ctrl; > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0=C2=A0 pmu->global_status_mask =3D pmu->global_ctrl_mask; > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } >=20 > > > > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > > > > =C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 pmu->counter_bitmask[KVM_PMC= _GP] =3D ((u64)1 << 48) - 1; > >=20