From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 AE27522D784 for ; Fri, 16 May 2025 20:54:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747428884; cv=none; b=rxoY/DkzHXEyQg5zc8fUiEH5wDI53EnmUgs786AKh59JtW2VWk+R0B2zLDBYVRldjYibSstHaSEem68DXwgA6Rd/SX5IzFTfprcx/dzIzc+0bSvqLtyrmrHnA67UPCqPXWi/BvO/5JSym4Lz1ucUOnCsLn5MQ/x5LKiNRqjWL5o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747428884; c=relaxed/simple; bh=QWFG0Ylh5oGGQ0y5MXw7J0v9cLYgY8UG3wLXOKRNB6I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fG/v4H4Tb6IrVThfxWqHFfUsqseAVk9LGz53P0EX41rZekEGv5/skb8PJjcid4Fj56Q3BB3TdDyhIuDb2udDLMQLRNRdVNVVAFwK4Nth7zs+m4d3U97rTDiT1Vo62wMpfWWGERECjcYvsops2fEO9KOnXYcf2tbL8IJY8XIblDQ= 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=P3B7bveB; arc=none smtp.client-ip=209.85.210.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="P3B7bveB" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-74299055c3dso3260963b3a.0 for ; Fri, 16 May 2025 13:54:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1747428881; x=1748033681; 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=pUEdh5iBFBvuEiqTMp07Lu/KDXTezSp1m3vHIrINdEA=; b=P3B7bveBkhciad9rpRDaft6/0HMMZhqJpG1nEqbLj0xg7kYpV3HaLTu52g5UGJDvko Vqg9uYus3ZFT9vj40IHW7WeniOxJXjeaYh2lCNE0/RPGuEhvUQGCLBYORocESV9cwVf4 aaBnXSbVwzu6dgn6zcucOKbaSTrPU1z2mVP6rLMuBJLXlXZZNqVlDx9Tn2krXkKBzmlk RvW96lbx5MmYIBF7mGAs53/j+gMcnBnM1U1FlkNKncDYQPr+2ervoCAa1veL2W2Ns79G izjRG+89orqrv9SIQT1954b5s3Yyw/bee2kWd2si3c+/uT8E86LPMOxg1mQ4lESPZwqE v4EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747428881; x=1748033681; 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=pUEdh5iBFBvuEiqTMp07Lu/KDXTezSp1m3vHIrINdEA=; b=THhI2Hst47lAlEXjW8MoZk4tnRD5fa/csdMvmAKIzex/ntjml/Mbo/BcuzH9Q5j2Wv mk6Lm0jMj+XWZG/KyVHNBcLM+TZAmMIlK+vmg3ZLcGSGD89a3Kz6wswV4Lktvp5LLIv9 SCw/8wef4AUOSqtDzucTuTx6QLX0qmXQ4S5aiKFrBvFwo98tf2D2crtFChknXZe0vebm ij9K++ihts3uqIhqvTO0HmqkNc/I62D9wkoIT4fvTUHD4c4p1PRJXxp3vbt3JNlSlZhW b01kFe1pdz7k3lIKn0pzgZsJmoIUMUwJ/h+4kKVveS6wTddOMXGJVhSGuUzZDYz/WTxF JkUQ== X-Forwarded-Encrypted: i=1; AJvYcCWtlx1isC7GOfOnaBAD+JFhakfGFQeaedawaqmMgu8LykZ/JPv6wT3OngIFdPtPoSuMiIvTPFkEkcuYPlcVxF3X@vger.kernel.org X-Gm-Message-State: AOJu0YyOkK02LXWbfD/YLxE5cr0pqH5PoPcMIja5R8gvVKzUNhqrORu0 26H4V6Vl8E+NQRclDGRA5YrLbSiX2UnyTUflLV1V7s0hrPh35FIO0vg5vbBrbDS2aC36OSOTdL6 htcGMAg== X-Google-Smtp-Source: AGHT+IEKXMvi6p5AYfeWYVVxoOJp0+T+HCMSwZBivOJAwnUFY68sURx/OiAsfMQfNNX7Rux9VtkV5C8ZiFM= X-Received: from pgid6.prod.google.com ([2002:a63:ed06:0:b0:b26:de99:6f81]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:6f83:b0:1f5:619a:8f79 with SMTP id adf61e73a8af0-21621876910mr7462694637.1.1747428880970; Fri, 16 May 2025 13:54:40 -0700 (PDT) Date: Fri, 16 May 2025 13:54:34 -0700 In-Reply-To: <2d0d274c-6bc0-43c7-a8a8-92aa11872675@linux.intel.com> 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-28-mizhang@google.com> <2d0d274c-6bc0-43c7-a8a8-92aa11872675@linux.intel.com> Message-ID: Subject: Re: [PATCH v4 27/38] KVM: x86/pmu: Handle PMU MSRs interception and event filtering From: Sean Christopherson To: Dapeng Mi 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 , Jim Mattson , Sandipan Das , Zide Chen , Eranian Stephane , Shukla Manali , Nikunj Dadhania Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable On Fri, May 16, 2025, Dapeng Mi wrote: > On 3/25/2025 1:31 AM, Mingwei Zhang wrote: > > + if (kvm_mediated_pmu_enabled(pmu_to_vcpu(pmu))) { > > + bool allowed =3D check_pmu_event_filter(pmc); > > + > > + if (pmc_is_gp(pmc)) { > > + if (allowed) > > + pmc->eventsel_hw |=3D pmc->eventsel & > > + ARCH_PERFMON_EVENTSEL_ENABLE; > > + else > > + pmc->eventsel_hw &=3D ~ARCH_PERFMON_EVENTSEL_ENABLE; > > + } else { > > + int idx =3D pmc->idx - KVM_FIXED_PMC_BASE_IDX; > > + > > + if (allowed) > > + pmu->fixed_ctr_ctrl_hw =3D pmu->fixed_ctr_ctrl; >=20 > Sean, just found there is a potential bug here.=C2=A0 The > "pmu->fixed_ctr_ctrl_hw" should not be assigned to "pmu->fixed_ctr_ctrl" > here, otherwise the other filtered fixed counter (not this allowed fixed > counter) could be enabled accidentally. >=20 > diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c > index ba9d336f1d1d..f32e5f66f73b 100644 > --- a/arch/x86/kvm/pmu.c > +++ b/arch/x86/kvm/pmu.c > @@ -473,7 +473,8 @@ static int reprogram_counter(struct kvm_pmc *pmc) > =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=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 int idx = =3D pmc->idx - KVM_FIXED_PMC_BASE_IDX; >=20 > =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=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (allo= wed) > -=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=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->fixed_ctr_ctrl_hw =3D pmu->fixed_ctr= _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=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->fixed_ctr_ctrl_hw |=3D pmu->fixed_ct= r_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=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=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=C2=A0 > intel_fixed_bits_by_idx(idx, 0xf); Hmm, I think that's fine, since pmu->fixed_ctr_ctrl should have changed? B= ut I'd rather play it safe and do (completely untested): if (pmc_is_gp(pmc)) { pmc->eventsel_hw &=3D ~ARCH_PERFMON_EVENTSEL_ENABLE; if (allowed) pmc->eventsel_hw |=3D pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE; } else { u64 mask =3D intel_fixed_bits_by_idx(pmc->idx - KVM_FIXED_PMC_BASE_IDX, 0= xf); pmu->fixed_ctr_ctrl_hw &=3D ~mask; if (allowed) pmu->fixed_ctr_ctrl_hw |=3D pmu->fixed_ctr_ctrl & mask; }