From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (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 AD52823B60A for ; Wed, 9 Jul 2025 15:53:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752076413; cv=none; b=HragmU0YA+Egceg9uw16gRxekd7bQQ7CXHYLrT2OZcjHY5ie/QXvQ4knSoIefPJe0OGFySCnbftn8PoGR//nobOYaiwaTIRBIlTzoOMMdMXjt1VMwFueY+U2B8bCbRBh+3GJaBa/b5PLKHJtjaYr7mNn5m3VK1xdyyhT+MG8b7s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752076413; c=relaxed/simple; bh=25FMe8x/OW3Lp3qeP0k9f4sApdVqQUoIa/qrXWF7ALg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=abcjnYDGkwe94dxsVRpmcc9hjYWPuntLpoygtn+8UmiaD4HhuelZVeGXpKuywO0P5d9khF9XpHqAkz15e8AG+QznlraLhoM5+Pk5a1n+RiDwSK1VbfJb49U9kpouFxFOh0MrQI9N6EiJKfouLVgvYolk9acpgncVJlNPnXkwNsw= 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=sQehQtzA; arc=none smtp.client-ip=209.85.216.73 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sQehQtzA" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-3132c8437ffso122552a91.1 for ; Wed, 09 Jul 2025 08:53:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1752076411; x=1752681211; 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=qTbopsyjLwZagxO+PemAykhJLuXJRZ0JL1WGf/NIPOI=; b=sQehQtzA0nyaAE7SI/A6x44LYofxmv/20YzS47mUHxH2mucH37uewhRd5N9L8ycMK8 eAwPzpPNnkXhUmcuEtZaFbcGZ4+HpI9xHgpfPQ7RBiaSrTq69M5jY53UBHnxUQTeHD8m vDPDI2ceDr5gxPcimlmj19mflLloJetLCYdufnn5reGGkPvK8TszT3g/823NR8mqwF2u CGS0Wo8KQ515IBVQ86XYIDWjneGrhK7vgDO2iF2yidQl96aNHRggyC4/JEiotFaAAnK0 PRJN4Bcy/KkmRoWAe85xX8Cdeyubuu+PGVV4FaVDKmEyrD269nW7wyeSVYJfh6XfeqDx eoVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1752076411; x=1752681211; 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=qTbopsyjLwZagxO+PemAykhJLuXJRZ0JL1WGf/NIPOI=; b=j6Cy8cbNcf/Pg1N2WrXgRJPhrINvLjYQGDf80PZghFqp8M6xWNUEogAvQUWOOuWKFY KJ2Tp9cmPI+IDeZwEZMamQnmU5PfQS8htQWVDifP9efadu3ZjRFS4za7hZZVNpEKPdcb ArAi48oS8f18F4KyXoNviMyxF5ZrH9JYXoP2FciEKUBEBrfn7KEbvrkYxFFJzKD1TiSJ RYm2a0gNgzo2BSBPy0mqNgrgDrDGX5f3Jt7zlEo0ourP5NdEmv4FWWjPxYzRki65wsBC P4HmAZAOPJ9FUVjyfSnPsO6eo2MZqympMHawsJVr579FXiVIopcyXJjTIf2dnCFh/+F4 mRZg== X-Forwarded-Encrypted: i=1; AJvYcCX9D9xMNtlDZy06bmazDVsKogKAMK4IT5ZjSo+qL77LACeSzx2oWpvFOE1pCsoUTrqBqtx3uEDTzt4uQAHpJKUA@vger.kernel.org X-Gm-Message-State: AOJu0Yz2ecksi8fEq0WiqrOnui2W7/QFn90tJKDETYGFhURSaHpahO58 tqmaYR2KXaymyguWaXiNmfTejNXa/RmhQrB4YDaiK+ucA2HjIHPvyuGWkjt22fsl/pSUXwoP3Tb 5dibRSg== X-Google-Smtp-Source: AGHT+IGjePHxm4yhTC07WPmOQKWcW69yFAbNV3Ly82vKbgJPL+sCUOW7xQYxaSJ26UKg7Zgl6um4WrjHcrM= X-Received: from pjbpq7.prod.google.com ([2002:a17:90b:3d87:b0:313:17cf:434f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2e47:b0:311:ff18:b84b with SMTP id 98e67ed59e1d1-31c3c2f3c1amr536725a91.25.1752076411095; Wed, 09 Jul 2025 08:53:31 -0700 (PDT) Date: Wed, 9 Jul 2025 08:53:29 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250324173121.1275209-1-mizhang@google.com> <20250324173121.1275209-21-mizhang@google.com> Message-ID: Subject: Re: [PATCH v4 20/38] KVM: x86/pmu: Check if mediated vPMU can intercept rdpmc From: Sean Christopherson To: Sandipan Das Cc: Mingwei Zhang , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Paolo Bonzini , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Liang@google.com, Kan , "H. Peter Anvin" , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, Yongwei Ma , Xiong Zhang , Dapeng Mi , Jim Mattson , Zide Chen , Eranian Stephane , Shukla Manali , Nikunj Dadhania Content-Type: text/plain; charset="us-ascii" On Mon, May 26, 2025, Sandipan Das wrote: > > @@ -212,6 +212,18 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu) > > bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters); > > } > > > > +static void amd_pmu_refresh(struct kvm_vcpu *vcpu) > > +{ > > + struct vcpu_svm *svm = to_svm(vcpu); > > + > > + __amd_pmu_refresh(vcpu); > > + > > + if (kvm_rdpmc_in_guest(vcpu)) > > + svm_clr_intercept(svm, INTERCEPT_RDPMC); > > + else > > + svm_set_intercept(svm, INTERCEPT_RDPMC); > > +} > > + > > After putting kprobes on kvm_pmu_rdpmc(), I noticed that RDPMC instructions were > getting intercepted for the secondary vCPUs. This happens because when secondary > vCPUs come up, kvm_vcpu_reset() gets called after guest CPUID has been updated. > While RDPMC interception is initially disabled in the kvm_pmu_refresh() path, it > gets re-enabled in the kvm_vcpu_reset() path as svm_vcpu_reset() calls init_vmcb(). > We should consider adding the following change to avoid that. Revisiting this code after the MSR interception rework, I think we should go for a more complete, big-hammer solution. Rather than manipulate intercepts during kvm_pmu_refresh(), do the updates as part of the "common" recalc intercepts flow. And then to trigger recalc on PERF_CAPABILITIES writes, turn KVM_REQ_MSR_FILTER_CHANGED into a generic KVM_REQ_RECALC_INTERCEPTS. That way there's one path for calculating dynamic intercepts, which should make it much more difficult for us to screw up things like reacting to MSR filter changes. And providing a single path avoids needing to have a series of back-and-forth calls between common x86 code, PMU code, and vendor code.