From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 8EAAC3AE6FC for ; Fri, 8 May 2026 23:13:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282038; cv=none; b=YvK1N0TXOPLm3+0QDIakqScYg5s2NXNtzu1okb0A2BeZbFF9UvK8vVsEY7g4hd/54mX8hMMeOw9ThIi/8PfBtLSD2I4YzpU/uwnNjn+bU0/RJ9pWb2zV/yzKtABAi259qQL2Ibep43o9xYBQlyXz6j5Xsh4TuSfzDzrKogWRuZc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282038; c=relaxed/simple; bh=yx/8znDbwZackXRIS3uVWDYIbK9dKkMXLjcg4TqN5Us=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Tb+5nhNCZ36LfWwn+G7gnQ5s1KNozlmxRO3WX8jgDHnJOhbcLExhq0ihsa2JZ9wAHY3oSN3lPW/RnwyYvkqyCuyImv52MbRf7U6f2IDl+WnW70gHG5CiOQEyQIkkcgJ6pzOBJp9HB/fb1n9I8FT58pOwAXl2qKGuZFZj1vP4PJc= 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=Pzh2WYPB; arc=none smtp.client-ip=209.85.216.74 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="Pzh2WYPB" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-3662668b825so4043819a91.3 for ; Fri, 08 May 2026 16:13:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282037; x=1778886837; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=+D5quk3d/QgNQ3+BOhnf9E+QRhuhNCjSe9w0CI0JGNM=; b=Pzh2WYPBvEmk9SbutkfErFW52bj9Qstoqx8RBAekfuaODEXCYLQlEPxAYM6d0wrSk6 lCmv0fFQ/XLJJX8H3Gf0D42oKh4wht84k7hv0XCsn04G4bK2nPoZJlku/eGnwvpMSZdK nI25FUL3vCONkiFdAe5hfRRqFgYcZ93u3QX41x66q35qV1vkfXkMKMpSAVumn106hMov W/eB5zTredZaduIILTd8t1th/8LqjeCDxAFDidiIpvX43gdZmBImjPseLgEiy2VZyWRF 11dAdR1kMIJLiwK8AeN3hRytX2oW2aJpO+kapJK9YrK5rHUyGAUYzeX0uBdOCw6NRhKn Rhrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282037; x=1778886837; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=+D5quk3d/QgNQ3+BOhnf9E+QRhuhNCjSe9w0CI0JGNM=; b=RpIQLcCRmcVeyx1cy0qWigFItBXTJ8Q4oqZZOpI4kQ8PTJI5t0gyqgWE4ruA4JUJly 56yn0ekFKpBOv1ZwBbF0dOCHZKU3r88CDcALnY5azcMJLcQCg0s3+qTXyWk9/gK0KeqY AVrNJIyFEX3cx5ze/rj0pJpGPyIPd90MBQwzfSlF3aLR1uJMDs/bJxhL6YEVZY5Sbubo Y6+B9IYw7BBvqiUXylauN2/Za4C7QxfE3PBdrzpidzAO5FgeCdrUnSqS0dOn7VfYB552 6kx4Rev+1N3C6JNx90BJ6pHnjxNGjV3rysypy77ig6LcU1GvHPxO5SXtfTJEI6gcTZre DgJw== X-Forwarded-Encrypted: i=1; AFNElJ/SuOP5gXutQdMfMopSeyChAQTrjdQwwQnRgPvwhC5kyD2rMIM3YGTxoWMr7u457RMWGMmOw/ZvaRNGbDY=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2qLi0VPXV3eJBfqm6DQ8ut0xj5+TYgIx0TdczVxGccDToEH+E OEJ5v3gvbiZKk6FRSRD/ZqgU9I/ZG3BJR2S7xxXbb5ZbyCI+dQMLuDRTgJEoOU79cmTbA+XiHDK h7/QXZA== X-Received: from pfbgg21.prod.google.com ([2002:a05:6a00:6315:b0:838:27b2:c6d]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:4324:b0:398:4a1f:8a54 with SMTP id adf61e73a8af0-3aa5a8308ccmr16370081637.2.1778282036646; Fri, 08 May 2026 16:13:56 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:45 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-2-seanjc@google.com> Subject: [PATCH v3 1/9] perf/x86/intel: Ensure guest PEBS path doesn't set unwanted PERF_GLOBAL_CTRL bits From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When reinstating PEBS counters into PERF_GLOBAL_CTRL for a KVM guest, mask the value with perf's desired/original PERF_GLOBAL_CTRL value to ensure KVM doesn't unintentionally enable counters. This _should_ be a nop, as arr[pebs_enable].guest is derived from cpuc->pebs_enabled, which should be a subset of x86_pmu.intel_ctrl, but paranoia is cheap in this case. Signed-off-by: Sean Christopherson Reviewed-by: Dapeng Mi --- arch/x86/events/intel/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index d9488ade0f8e..b70dc35fcceb 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -5066,7 +5066,7 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) arr[pebs_enable].guest &=3D ~kvm_pmu->host_cross_mapped_mask; arr[global_ctrl].guest &=3D ~kvm_pmu->host_cross_mapped_mask; /* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ - arr[global_ctrl].guest |=3D arr[pebs_enable].guest; + arr[global_ctrl].guest |=3D intel_ctrl & arr[pebs_enable].guest; } =20 return arr; --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 844703B0ADD for ; Fri, 8 May 2026 23:13:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282040; cv=none; b=Dfs0RFUCVlAVib4Y1wB8D1b5tWIGgpLshZoO0YT/I0LnqmwbgTlRt6vHayiOblkVsfmBqv/96EyPHI4+oJq81QIBDM57lyZn3+U2Oi3yuFT2qI2t4v8Pu9FuQQvdjDcu2eSRqQp+NniKchNSls3eRNrS98mcIVKeQO7kFNV+mcc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282040; c=relaxed/simple; bh=iJzNfQSCA5VjS8Kr8kZpQXZ0VSjA9HwsvTs8Uq9Dfvo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lt7ffJzFp8lAR5SqYJ4kcbQioLy7uPPxO6YKkRRUw3neMopYazE8oGv/OcWyaiLkfooOcYz+ytewQ3gYPbZ8B3/l61Z6Ivv56pxUVIfRu85PmeSiL4rJc7JvadJWzp0OA5A3mEMpl1rNgPCyuOEWYARZ3Q39XHHjV3hypRic+TY= 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=DPefgnhk; arc=none smtp.client-ip=209.85.215.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="DPefgnhk" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c797e31a9b7so2847161a12.1 for ; Fri, 08 May 2026 16:13:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282038; x=1778886838; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=nL2nuU0zBSPyTxPi7rivKlYOUZruT8VJE95GVfuQzuo=; b=DPefgnhkR5NDDbYo/PwWMfqwoxdIrExi54zgNbN4GwfKDTgIBusszGsNHnuC6sl4FW vDijxANNFATAy28/ASkOajt85R8Kp2es+TChXb1TvxD8v+Ef/qPgQ4AVvTuumBS1UOmR kHWh8Gq5qD5Mm/y0Jq48Ns8ETdQb4vmW6ciarRu6pYJfCGmk60EjpA3dUgeVEFAa+6/N GK7qG2bUGGxu3vTqTg1b0v1VGhM3sTqEyJRscEGjpsweQkQu6MuwuZ9l3Fdl7qtiFd57 2kNmxevUxXfloxWExu7SCxETmWL31sm5VAZ3siscKSYe2/Zt3rYCEOIFbYNtMIv3/A/O cvlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282038; x=1778886838; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nL2nuU0zBSPyTxPi7rivKlYOUZruT8VJE95GVfuQzuo=; b=cBWxjHOwJiFuOlCbTGursuY2Sfsw/m13g5BHTb7SN12WED8vlBPujZyk6ZuLTNIbKq 0lVq1tURtbIncHGPz83sTgV99xwcCRNVPp1lxM/zqaCh2MusdUEY9OQ0HEjN6qFP804m 1rhn5jpVrlqRNZWjbmAzp0SvFJUj/EhbDS1+X3zbkg23Kd4y6YZece8g2XBWOw+1/waM jTwc5I7bIH80cod7IJVuEUHNbNiHx1y/K2kIeU9j6rE0MVNCIStpJ8kXNTpIfjsOYMPG EDcYo485Yu1WBpYBQzU9F6C/gcrOLbANJO/pk5s9Yvu790PlBzQbI1zVJahnJh2CIV5f yPfQ== X-Forwarded-Encrypted: i=1; AFNElJ+cwe0b2x+MJdzckcbnlIGF1nF97hjgOCpZ7LKsAICGng4lMMVQRiwaemxeJ2LspOOzQ22G+qLXi80jBQc=@vger.kernel.org X-Gm-Message-State: AOJu0YzbrZ0uC9xREUy9uioA1918MHuPXwBX5CukGA8VI4BkTEcolhN0 WwRQunqqM54iie62xcINIpB8ak5BOtCTcbxB7oYovz2a5bohFOzkZ5CghEneCob+NqAFYBwpStf 4C/x/1A== X-Received: from pfjt21.prod.google.com ([2002:a05:6a00:21d5:b0:82f:a2bb:a7cf]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:8009:b0:835:cc47:6ff7 with SMTP id d2e1a72fcca58-83a5eb3a49bmr14404275b3a.50.1778282037655; Fri, 08 May 2026 16:13:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:46 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-3-seanjc@google.com> Subject: [PATCH v3 2/9] perf/x86/intel: Don't write PEBS_ENABLED on host<=>guest xfers if CPU has isolation From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, *never* insert an entry for PEBS_ENABLED if the CPU properly isolates PEBS events, in which case disabling counters via PERF_GLOBAL_CTRL is sufficient to prevent unwanted PEBS events in the guest (or host). Because perf loads PEBS_ENABLE with the unfiltered cpu_hw_events.pebs_enabled, i.e. with both host and guest masks, there is no need to load different values for the guest versus host, perf+KVM can and should simply control which counters are enabled/disabled via PERF_GLOBAL_CTRL. Avoiding touching PEBS_ENABLED "fixes" a bug where PEBS_ENABLED can end up with "stuck" bits if a PEBS event is throttled between generating the list and actually entering the guest (Intel CPUs can't arbtitrarily block NMIs). Fixes in quotes because leaving PEBS_ENABLED as-is doesn't fix the underlying problem of perf (via PMIs) being able to modify state after the perf<=3D>KVM handoff. But not writing PEBS_ENABLED is desirable no matter what, as stating the obvious, leaving PEBS_ENABLED as-is avoids three MSR writes on every VMX transition: one each on entry/exit, and one more explicit WRMSR to zero PEBS_ENABLED before VM-Entry (KVM assumes the only reason PEBS_ENABLED is in the load list is if the CPU lacks isolation and thus needs a quiescent period). Opportunistically add comments to (better) explain the rules for generating the set of PEBS counters that will be active while the guest is running, along with a FIXME for the suspected hack-a-fix where perf disables guest PEBS if _any_ PEBS event is configured to count in the host (commit 854250329c02 ("KVM: x86/pmu: Disable guest PEBS temporarily in two rare situations") doesn't explain the motivation, at all). Fixes: c59a1f106f5c ("KVM: x86/pmu: Add IA32_PEBS_ENABLE MSR emulation for = extended PEBS") Cc: Jim Mattson Cc: Mingwei Zhang Cc: Stephane Eranian Signed-off-by: Sean Christopherson Reviewed-by: Dapeng Mi --- arch/x86/events/intel/core.c | 55 ++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 18 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index b70dc35fcceb..13cd12d3eeee 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -4999,12 +4999,15 @@ static struct perf_guest_switch_msr *intel_guest_ge= t_msrs(int *nr, void *data) struct kvm_pmu *kvm_pmu =3D (struct kvm_pmu *)data; u64 intel_ctrl =3D hybrid(cpuc->pmu, intel_ctrl); u64 pebs_mask =3D cpuc->pebs_enabled & x86_pmu.pebs_capable; - int global_ctrl, pebs_enable; + u64 guest_pebs_mask; + int global_ctrl; =20 /* * In addition to obeying exclude_guest/exclude_host, remove bits being * used for PEBS when running a guest, because PEBS writes to virtual - * addresses (not physical addresses). + * addresses (not physical addresses). If the guest wants to utilize + * PEBS, and PEBS can safely enabled in the guest, bits for the guest's + * PEBS-enabled counters will be OR'd back in as appropriate. */ *nr =3D 0; global_ctrl =3D (*nr)++; @@ -5051,24 +5054,40 @@ static struct perf_guest_switch_msr *intel_guest_ge= t_msrs(int *nr, void *data) }; } =20 - pebs_enable =3D (*nr)++; - arr[pebs_enable] =3D (struct perf_guest_switch_msr){ - .msr =3D MSR_IA32_PEBS_ENABLE, - .host =3D cpuc->pebs_enabled & ~cpuc->intel_ctrl_guest_mask, - .guest =3D pebs_mask & ~cpuc->intel_ctrl_host_mask & kvm_pmu->pebs_enabl= e, - }; + /* + * Restrict guest PEBS events to counters that (a) perf supports, (b) + * the guest wants to use for PEBS, (c) are not excluded from counting + * in the guest, and (d) _are_ excluded from counting in the host. + */ + guest_pebs_mask =3D pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & + ~cpuc->intel_ctrl_host_mask & + cpuc->intel_ctrl_guest_mask; =20 - if (arr[pebs_enable].host) { - /* Disable guest PEBS if host PEBS is enabled. */ - arr[pebs_enable].guest =3D 0; - } else { - /* Disable guest PEBS thoroughly for cross-mapped PEBS counters. */ - arr[pebs_enable].guest &=3D ~kvm_pmu->host_cross_mapped_mask; - arr[global_ctrl].guest &=3D ~kvm_pmu->host_cross_mapped_mask; - /* Set hw GLOBAL_CTRL bits for PEBS counter when it runs for guest */ - arr[global_ctrl].guest |=3D intel_ctrl & arr[pebs_enable].guest; - } + /* + * Disable counters where the guest PMC is different than the host PMC + * being used on behalf of the guest, as the PEBS record includes + * PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the + * wrong counter(s). + */ + guest_pebs_mask &=3D ~kvm_pmu->host_cross_mapped_mask; =20 + /* + * FIXME: Allow guest and host usage of PEBS events to co-exist instead + * of disabling guest PEBS entirely if the host is using PEBS. + * What exactly goes wrong if guest and host are using PEBS is + * unknown. + */ + if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) + guest_pebs_mask =3D 0; + + /* + * Do NOT mess with PEBS_ENABLED. As above, disabling counters via + * PERF_GLOBAL_CTRL is sufficient, and loading a stale PEBS_ENABLED, + * e.g. on VM-Exit, can put the system in a bad state. Simply enable + * counters in PERF_GLOBAL_CTRL, as perf load PEBS_ENABLED with the + * full value, i.e. perf *also* relies on PERF_GLOBAL_CTRL. + */ + arr[global_ctrl].guest |=3D guest_pebs_mask; return arr; } =20 --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 A20BC3B3880 for ; Fri, 8 May 2026 23:13:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282041; cv=none; b=M4LdZQ3OAC/c5jX5sUNBy78bfmK/cIrkvJblrTszRp65MwOC+0/TEd4LfqxcUojmRY94vxAcxyPYjn5GjOfxuQICYgSBppmH0fzSYX626LMaaRrP3QRhbzFUqa8O9Cy92rtVNxQJrIhdQ/twCBozPjsAcFl07uEDxrlRj4EgWxE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282041; c=relaxed/simple; bh=Cs8x6xkY1ZiIqH1MFpLULYp8Rr4Fz288wc2+f1BQbdM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OIUS+KvL/f/ppnV9kefvL7OcAKW485yMW2NZz2sJOcQ7Hx8w9NqtuTsuQ8uaNX4AXWUfRUNBIcvUXuuFNEpBMCoixRGFAxn7npWxsrgLZ19ssBCWidsUHnfOOU1/5/QdJ7sKuTmWljTGUjll4vwABeQ2OEDpv70U91GO5MMXoD8= 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=hjkp0TZ0; arc=none smtp.client-ip=209.85.215.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hjkp0TZ0" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-b6097ca315bso3703170a12.3 for ; Fri, 08 May 2026 16:13:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282039; x=1778886839; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4v9hsAEokHAnzUckzAwcqgBfPt3hYzElKEYLdatb1h4=; b=hjkp0TZ0I4XPp44zECGIxveGN0+23QkyFIZvoZawuGh8hxRy4sAPjF9D4jwuT/vcdW 5bS0hr0qki+7LjXj9duiPKIr8i5pvY/HIFDipYqQ0vCNAvnADLhu6kzPWJsJ0bWXCr3W kAM27OkUd7edk8LAkya4iEu+y8LKVtfi4PeoRnxUZF/I/gg/D+EmAKCFLxFbK8VNGHJc PXjTsZYAhnStd6wj32I1Vk78V/m9RBmIey3dSU9I6nXA4i5nJ+52vv+8EXwmbK2QWjuI ok9X/jjC5ZhH+6F5r1DVHjnYyyYKXULNHH+KqFURd7LXMLaBtPM4KZSHvhMhrA9wztUK N1Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282039; x=1778886839; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4v9hsAEokHAnzUckzAwcqgBfPt3hYzElKEYLdatb1h4=; b=eaKJFvU49LH4rZHiBe+cEHpJkSPI6Pcak3BmzaoaHg1bGLa1pl+mcCnuGpT18j4Vmk 7xdT8qiqT6CrgtF5jyrXrndSfgwJE+tiWigRUFbibFS1ViDC8HvCJQgrBK4j6w24XXdu 3nJM9qH60m/iXdTT2Qk8b1/0tRO7ibFyYcixDu/7WoN/Mte2PG4MVieFjHlIALk0Kqiv 5z4n1QsXcOj6tk1QDXYZmd6TckaxnSbG+VdvpVjAgrzTCPObgRVYRKxewwK+aEbTKaVX vLFuaTV+hz2gwiWXK/PBY/wUxaZvXQ1lg3evCEn/weJTdGROrarHC0CLNUNOnJE9iDKt x15A== X-Forwarded-Encrypted: i=1; AFNElJ/GOS5rufz4oMtSDGSsNkvAqQ9Sbh/KB6Jubz4cttT5LCvmuP9my3+gVlcvKLzt+a2uVRHqtdh9YkrEGv0=@vger.kernel.org X-Gm-Message-State: AOJu0Yy5o3FtBB1gXavMAgILDMyQ4o5kV+PM0SFW8eDllbVTzsy+OL20 nlvRyVmZL4ZvW19UHrZKgIEV6BQ41JXhuh3sBr2QsxZylikf8kwD2zGNI4CkDcrQkzQhoqzXgp+ UhsdCBw== X-Received: from pgbfq18.prod.google.com ([2002:a05:6a02:2992:b0:c7b:acc3:8dec]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:94c7:b0:398:a060:a967 with SMTP id adf61e73a8af0-3aad426e435mr199226637.11.1778282038735; Fri, 08 May 2026 16:13:58 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:47 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-4-seanjc@google.com> Subject: [PATCH v3 3/9] perf/x86/intel: Don't context switch DS_AREA (and PEBS config) if PEBS is unused From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When filling the list of MSRs to be loaded by KVM on VM-Enter and VM-Exit, load the guest values for DS_AREA and (conditionally) MSR_PEBS_DATA_CFG if and only if PEBS will be active in the guest, i.e. only if a PEBS record may be generated while running the guest. As shown by the !pebs_ept path, it's perfectly safe to run with the host's DS_AREA, so long as PEBS-enabled counters are disabled via PERF_GLOBAL_CTRL. Omitting DS_AREA and MSR_PEBS_DATA_CFG when PEBS is unused saves two MSR writes per MSR on each VMX transition, i.e. eliminates two/four pointless MSR writes on each VMX roundtrip when PEBS isn't being used by the guest. Fixes: c59a1f106f5c ("KVM: x86/pmu: Add IA32_PEBS_ENABLE MSR emulation for = extended PEBS") Cc: Jim Mattson Cc: Mingwei Zhang Cc: Stephane Eranian Reviewed-by: Jim Mattson Reviewed-by: Dapeng Mi Signed-off-by: Sean Christopherson --- arch/x86/events/intel/core.c | 39 +++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 13cd12d3eeee..0e9ac2e9b5e7 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -5037,23 +5037,14 @@ static struct perf_guest_switch_msr *intel_guest_ge= t_msrs(int *nr, void *data) return arr; } =20 + /* + * If the guest won't use PEBS or the CPU doesn't support PEBS in the + * guest, then there's nothing more to do as disabling PMCs via + * PERF_GLOBAL_CTRL is sufficient on CPUs with guest/host isolation. + */ if (!kvm_pmu || !x86_pmu.pebs_ept) return arr; =20 - arr[(*nr)++] =3D (struct perf_guest_switch_msr){ - .msr =3D MSR_IA32_DS_AREA, - .host =3D (unsigned long)cpuc->ds, - .guest =3D kvm_pmu->ds_area, - }; - - if (x86_pmu.intel_cap.pebs_baseline) { - arr[(*nr)++] =3D (struct perf_guest_switch_msr){ - .msr =3D MSR_PEBS_DATA_CFG, - .host =3D cpuc->active_pebs_data_cfg, - .guest =3D kvm_pmu->pebs_data_cfg, - }; - } - /* * Restrict guest PEBS events to counters that (a) perf supports, (b) * the guest wants to use for PEBS, (c) are not excluded from counting @@ -5080,6 +5071,26 @@ static struct perf_guest_switch_msr *intel_guest_get= _msrs(int *nr, void *data) if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) guest_pebs_mask =3D 0; =20 + /* + * Context switch DS_AREA and PEBS_DATA_CFG if and only if PEBS will be + * active in the guest; if no records will be generated while the guest + * is running, then simply keep the host values resident in hardware. + */ + arr[(*nr)++] =3D (struct perf_guest_switch_msr){ + .msr =3D MSR_IA32_DS_AREA, + .host =3D (unsigned long)cpuc->ds, + .guest =3D guest_pebs_mask ? kvm_pmu->ds_area : (unsigned long)cpuc->ds, + }; + + if (x86_pmu.intel_cap.pebs_baseline) { + arr[(*nr)++] =3D (struct perf_guest_switch_msr){ + .msr =3D MSR_PEBS_DATA_CFG, + .host =3D cpuc->active_pebs_data_cfg, + .guest =3D guest_pebs_mask ? kvm_pmu->pebs_data_cfg : + cpuc->active_pebs_data_cfg, + }; + } + /* * Do NOT mess with PEBS_ENABLED. As above, disabling counters via * PERF_GLOBAL_CTRL is sufficient, and loading a stale PEBS_ENABLED, --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.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 A2FE83B5846 for ; Fri, 8 May 2026 23:14:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282042; cv=none; b=XcY7WtvPvZCr0c5VrmWC8VXpGLgMl3/WofZEA6/jXTYGlcuSLZX3yRiOlMJwam4IXyvBG+9ZKw2/7OilWW99AhS6KEN5GNHFDUHaL4Nh3vveJPWqPgMXAURuHd9ptDbAoRVVw0xczL24kkMXs0RV3BamkVPWl1C4cFM7ZwTVuS0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282042; c=relaxed/simple; bh=zC4BgdIkH0zEFk6RlMRGIuylwnchi/hIcehvbfaf81g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=EiVJFeBe5HXtT9dzIb8nmFrDaTjpGDWCdStveZGerLgQ2BwVrUjel7QR+AOPjLgVw/rGFwmBhUP0VWD2DYgBwnysSFdHgipMbsF60pKYmYHzt49OyzAOKe8gilEvTRFHO2q0vIop9RtTtj4bIF15sqEionUQLGGU0HRkkI/P2Nc= 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=AHbKioBV; arc=none smtp.client-ip=209.85.215.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="AHbKioBV" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c82660dbb0dso715520a12.2 for ; Fri, 08 May 2026 16:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282040; x=1778886840; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=XL8sVld4hY/ipwFKhW3uhAJLAQJEgZCQ2/7huQZafKA=; b=AHbKioBVXN5bmDSoZDV8n3LyPtKdWgQd86YFm43vXPOynhtb0oMGejUk1w35+uQu2+ uNfH6Hico5Hjc/3tbCeVkBUHJuiQUQeBOiz89sYPdNvqzV9+Z7/CH4tDaNzBiTMJnTvq 23OLIcKZ9M640hn+oTDpwfLl2Whsd7QpimL5HnupyYAWkToGG62fiwi2bqZ4OiLrtL5l Ctg8C/063hSVv1IESZCV0iEhTCQMw/MyYkabKebEXups6noM9RRYmK9KnYZ5SIqXWRds itEfFkXUhjG4yY8reXtv7T5Y5YKxYJHe7+6AYSmZTsM7/VRF+S7PI0Jr/u/8Jy93iPG8 U73g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282040; x=1778886840; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=XL8sVld4hY/ipwFKhW3uhAJLAQJEgZCQ2/7huQZafKA=; b=S8deEZ3dXdSjgaiEHu6cFWDBM6EU4nGTl553rCYBVZx62+R+I82Vx02X/h0XFzvA5x 9EYeVlsTipJnVcRTHPiR/tT3LdooSbJWrAYXZYvFtk+gpOTSzuJw25lFYBdVY+DmsoBE eScIcBeY2gIx1EmmLQbMmSBO/ujubG3pXpEjr/FIHHegqXb2K4KqeaoU8RgJTYCh2Az7 cB8Yrw99HYoOIjMu2p30LuIX4hn/AmLevIAXE9VDq4lxWsVbGYNzRvWaw6U3USnwrk1t ZE6z0oVBsioXfukYaUJv2xphC7oGGS02FW15FTjpuWTBLSEQlTsbVODepzIZJLZg+KvG 7Vyw== X-Forwarded-Encrypted: i=1; AFNElJ8r2v4cnwKNZVh+2Uar28CoXOk1xQF3yd81pGyrULogHFRjJbrR7siCfj6ht/msOozvipSjXNzyeHFkb9I=@vger.kernel.org X-Gm-Message-State: AOJu0YxggpJbMwGPf6Fz6xDE7/uSUWurh5qAUAI5cqh5/zmxpYM8Gtxe 3VbqRL+sHwfkVawBAmF6AH2tA6qKccOH6Is9x2o0y4L+0D9JZyUEGVixMJNPfckHiLHRe/eq37E I/924tA== X-Received: from pgno26.prod.google.com ([2002:a63:7e5a:0:b0:c81:2646:5294]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:939d:b0:398:6ea8:21f7 with SMTP id adf61e73a8af0-3aa5a9559ebmr15365421637.15.1778282039831; Fri, 08 May 2026 16:13:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:48 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-5-seanjc@google.com> Subject: [PATCH v3 4/9] perf/x86/intel: Make @data a mandatory param for intel_guest_get_msrs() From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop "support" for passing a NULL @data/@kvm_pmu param when getting guest MSRs. KVM, the only in-tree user, unconditionally passes a non-NULL pointer, and carrying code that suggests @data may be NULL is confusing, e.g. incorrectly implies that there are scenarios where KVM doesn't pass a PMU context. Fixes: 8183a538cd95 ("KVM: x86/pmu: Add IA32_DS_AREA MSR emulation to suppo= rt guest DS") Cc: Jim Mattson Cc: Mingwei Zhang Cc: Stephane Eranian Reviewed-by: Jim Mattson Reviewed-by: Dapeng Mi Signed-off-by: Sean Christopherson --- arch/x86/events/intel/core.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 0e9ac2e9b5e7..e9f5a6143e71 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -5038,11 +5038,11 @@ static struct perf_guest_switch_msr *intel_guest_ge= t_msrs(int *nr, void *data) } =20 /* - * If the guest won't use PEBS or the CPU doesn't support PEBS in the - * guest, then there's nothing more to do as disabling PMCs via - * PERF_GLOBAL_CTRL is sufficient on CPUs with guest/host isolation. + * If the CPU doesn't support PEBS in the guest, then there's nothing + * more to do as disabling PMCs via PERF_GLOBAL_CTRL is sufficient on + * CPUs with guest/host isolation. */ - if (!kvm_pmu || !x86_pmu.pebs_ept) + if (!x86_pmu.pebs_ept) return arr; =20 /* --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 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 CA4E03B8BC0 for ; Fri, 8 May 2026 23:14:01 +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=1778282043; cv=none; b=KkUsKIqRzSKMMlrXajGBljWWBnWNUDHYFpndroPpEVXTabTbwgRgwmRelpf3DC4i46WupWYBX3wzb+32jdnrzY7m1zSwUD9jB8snDlBLLDlpV7GIH/iJ1H7UEqUZi7CUvMttCkhvZqZKTIizTOeLAwFfnVUXwzjzyTgdNenPtMA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282043; c=relaxed/simple; bh=tFFcUCT/MCFy+fJtCQEO3MKGwK66Cka9jNQ0yPI80Po=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eULcNaXnr2zJhE+4GCwxOiOZIRNuR1r4vG9CqOF3Is+/R74weBb4r/1Lf+PzuuqlHpUD5IFSYWjb922rzXv5x/InvGYrtbeflqumaAe9Uii8zFbkXtWnv/PIGpkWMhAk99l+vB7bnNj6TB8tF56YD/02NTzKTqW7fOA/FcyY3C4= 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=c6wPquH1; 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="c6wPquH1" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-367c2d149efso416240a91.1 for ; Fri, 08 May 2026 16:14:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282041; x=1778886841; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=0wF4wWCedm61mWhSze2xS0e/agLsW85x4eBMYp/sCRc=; b=c6wPquH1tHJZDiLIQil2o7/M1vRDTYI33M50Rzp+UgMcQpY0jMeOFSftKV1Oe00x7f Nh+XIitWDnUb+CQ7mT6eanOb8QrSp6NOqCDmiiKk+vUEOB4ecdNcMgGoCGLaAFm/tE48 Kt9T4PVIC8TR2R31XWZGIbR13SrIc3mMejXegEG2rUB/ho/K5bPPs/S1ZFGqwRVvsF4M 5W3QTqgGok7D4lDcP544Y9yu0yuiyUCrRDJu+cGHgK3buJ7hHTWfqMS1ZU1cG+J8II8A rdEUHH3hJAeDm4nlWTppTbgwg8n3ocNBazKAS6RRhUOXzy4O2vzIeTngqAboKUnwUKpM fZHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282041; x=1778886841; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0wF4wWCedm61mWhSze2xS0e/agLsW85x4eBMYp/sCRc=; b=lxp7nEca3E4n/x6nHZWQ/SCbfvbsQltMAZsQHLhbByLmBk1DrVz6HF1qzMPeKAyXiw nUWNACcRQpWBQsQFA2sVLnHWMNXOeEaq1M+vvU7eF2YWYOigc0CZrN/rbkM34bQIIsew y1191v0plIx3KV4nRjwpDlpkb5Pvaxhy1vGKOQu6W2tkT9CvGVyyY/xg5hVizczeawLi Mrmc+UldNbXCLRH4DhXQlJ3rK+qdsRCglfEDFEyH2yw0q7sahOG/cMj2UPYiBSnrSM11 rbt0kHPmVGxaKj7+L9N0ypQrKrrhl5E5K5I94kZo6VneAu1SsJ6DiIfHY23CLxgZ7pSJ 5PKA== X-Forwarded-Encrypted: i=1; AFNElJ9f1wd4+KTb2V0bspBqLwip9a+DQZTmVRZdES2MNHjzC4VzSLa06E3hwYUZWQekl19nAwKGyJeSJPFk/A8=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+cLI5TgSQlBvXDqwLrAdkPDevemUrN+wz/tGzWNYufDl3XKzU borMwGSPuG95G0q7yPL+s9bYr1NQtiT9YPhHWPJU5VjoPvTcnZFGbhS6Mg7OocB0EqmN4PpEe0Y PC9L3pQ== X-Received: from pghi22.prod.google.com ([2002:a63:e916:0:b0:c79:7975:38ca]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:748a:b0:3a2:d79c:4149 with SMTP id adf61e73a8af0-3aa5ab70824mr16585267637.28.1778282040861; Fri, 08 May 2026 16:14:00 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:49 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-6-seanjc@google.com> Subject: [PATCH v3 5/9] perf/x86/intel: Invert names of intel_ctrl_{guest,host}_mask From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rename intel_ctrl_{guest,host}_mask to intel_ctrl_exclude_{host,guest}_mask to more accurately capture what they actually track. Specifically, an event that is excluded from the guest is NOT guaranteed to count in the host, and vice versa, as it legal (albeit bizarre) to configure an event to exclude both the host and the guest, i.e. to not count at all. Subjectively (though anyone who disagrees is wrong), aligning with perf_event_attr.exclude_{guest,host} also makes all related code much easier to follow. No functional change intended. Suggested-by: Jim Mattson Signed-off-by: Sean Christopherson Reviewed-by: Dapeng Mi --- arch/x86/events/intel/core.c | 22 +++++++++++----------- arch/x86/events/intel/lbr.c | 2 +- arch/x86/events/perf_event.h | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index e9f5a6143e71..7f7c7927b70b 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -2535,7 +2535,7 @@ static void __intel_pmu_enable_all(int added, bool pm= i) } =20 wrmsrq(MSR_CORE_PERF_GLOBAL_CTRL, - intel_ctrl & ~cpuc->intel_ctrl_guest_mask); + intel_ctrl & ~cpuc->intel_ctrl_exclude_host_mask); =20 if (test_bit(INTEL_PMC_IDX_FIXED_BTS, cpuc->active_mask)) { struct perf_event *event =3D @@ -2733,9 +2733,9 @@ static inline void intel_set_masks(struct perf_event = *event, int idx) struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); =20 if (event->attr.exclude_host) - __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); + __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask); if (event->attr.exclude_guest) - __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); + __set_bit(idx, (unsigned long *)&cpuc->intel_ctrl_exclude_guest_mask); if (event_is_checkpointed(event)) __set_bit(idx, (unsigned long *)&cpuc->intel_cp_status); } @@ -2744,8 +2744,8 @@ static inline void intel_clear_masks(struct perf_even= t *event, int idx) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); =20 - __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_guest_mask); - __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_host_mask); + __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask); + __clear_bit(idx, (unsigned long *)&cpuc->intel_ctrl_exclude_guest_mask); __clear_bit(idx, (unsigned long *)&cpuc->intel_cp_status); } =20 @@ -3473,7 +3473,7 @@ static void x86_pmu_handle_guest_pebs(struct pt_regs = *regs, struct perf_sample_data *data) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); - u64 guest_pebs_idxs =3D cpuc->pebs_enabled & ~cpuc->intel_ctrl_host_mask; + u64 guest_pebs_idxs =3D cpuc->pebs_enabled & ~cpuc->intel_ctrl_exclude_gu= est_mask; struct perf_event *event =3D NULL; int bit; =20 @@ -5013,8 +5013,8 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) global_ctrl =3D (*nr)++; arr[global_ctrl] =3D (struct perf_guest_switch_msr){ .msr =3D MSR_CORE_PERF_GLOBAL_CTRL, - .host =3D intel_ctrl & ~cpuc->intel_ctrl_guest_mask, - .guest =3D intel_ctrl & ~cpuc->intel_ctrl_host_mask & ~pebs_mask, + .host =3D intel_ctrl & ~cpuc->intel_ctrl_exclude_host_mask, + .guest =3D intel_ctrl & ~cpuc->intel_ctrl_exclude_guest_mask & ~pebs_mas= k, }; =20 if (!x86_pmu.ds_pebs) @@ -5051,8 +5051,8 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) * in the guest, and (d) _are_ excluded from counting in the host. */ guest_pebs_mask =3D pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & - ~cpuc->intel_ctrl_host_mask & - cpuc->intel_ctrl_guest_mask; + ~cpuc->intel_ctrl_exclude_guest_mask & + cpuc->intel_ctrl_exclude_host_mask; =20 /* * Disable counters where the guest PMC is different than the host PMC @@ -5068,7 +5068,7 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) * What exactly goes wrong if guest and host are using PEBS is * unknown. */ - if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) + if (pebs_mask & ~cpuc->intel_ctrl_exclude_host_mask) guest_pebs_mask =3D 0; =20 /* diff --git a/arch/x86/events/intel/lbr.c b/arch/x86/events/intel/lbr.c index 72f2adcda7c6..1298049246d7 100644 --- a/arch/x86/events/intel/lbr.c +++ b/arch/x86/events/intel/lbr.c @@ -713,7 +713,7 @@ static inline bool vlbr_exclude_host(void) struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); =20 return test_bit(INTEL_PMC_IDX_FIXED_VLBR, - (unsigned long *)&cpuc->intel_ctrl_guest_mask); + (unsigned long *)&cpuc->intel_ctrl_exclude_host_mask); } =20 void intel_pmu_lbr_enable_all(bool pmi) diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index fad87d3c8b2c..cc0aeeb34eb5 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -339,8 +339,8 @@ struct cpu_hw_events { /* * Intel host/guest exclude bits */ - u64 intel_ctrl_guest_mask; - u64 intel_ctrl_host_mask; + u64 intel_ctrl_exclude_host_mask; + u64 intel_ctrl_exclude_guest_mask; struct perf_guest_switch_msr guest_switch_msrs[X86_PMC_IDX_MAX]; =20 /* --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 29F4A3BA25F for ; Fri, 8 May 2026 23:14:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282044; cv=none; b=K3Sb4J5l3Ab5PIknwn92FIbxiBD4v19aA5NjnpLf6XY3nTHR0BrjwK+oz2QbUmVEdAVvRUvanGnF4S+0KAUy5XtZrxXjV8P4kViLCW9lqN6LJH4jr8IPJez45elCe7bzBPTV8DyRnewp3prNH6lVyLQmyH97p86LZsO/2AUN9EU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282044; c=relaxed/simple; bh=awV2Von+T5AhuhIFGgH37MajHZCuQxUpjUlB3MTEo/g=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MtB0noMmi1xKUAyEu6/vPJjFuPDYTwBIcQbirdxFVat1UugSlF4RX35czyepLBP4iRDH1wdVVKNL+UDbMs5pKm5S+F+kmZwTZhp9o4XUiftQUhFDXSx/Rmgh1n55QyDEA4oWNQHNftcPIhBMKb46Lk2eCTcLhj5LJFB4XpqQbvY= 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=fD8MOjk3; arc=none smtp.client-ip=209.85.215.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fD8MOjk3" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c70f19f0f37so1437290a12.0 for ; Fri, 08 May 2026 16:14:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282042; x=1778886842; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=WiBynQaBCrCTrVvB2B5pzVamCqZLon4JBoEx1jyUfTY=; b=fD8MOjk3FqfBwbMLz14p9JrE+KJQQrNPf2rXTQh/jQ5VYvBeKIuYU9ixOXgElSAn5t FJVs5vs90KQLSZUEVk5+mp57W9RnNkVfTSTll3B6AHKcinprszLYz3tWgBZtn2kCbYI0 exr1iC8mar+WbRKQzMKxD4lGjJWuAk6mZneUupes6UMQNVGa+hzXsl48yX3ZRuLcA1uk uWGGAY324S3O3dyfD9cvuCgt0t/xU43XLuXXT8Pq/IHliqmvedetJ2YFBxqk1Szw5HmA enuS/6Swye75GNGEerAKOnCcK3/5KPhtL53xvFG0f24SYU1OErrnzHxbXmHFeqUaTQyX OJXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282042; x=1778886842; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WiBynQaBCrCTrVvB2B5pzVamCqZLon4JBoEx1jyUfTY=; b=KltceiJOSTbLDWVVR8LAeVyKURVs6/m5MF+NYzRdqXpd1rkBu+Q8IZffS1P6UVeMYy tP7/+hqLqAa8A9y4J+oew9M9uRQgEzQ4NHqXNaNg8J2MrM9DmlVLnbWvVeVVoZLsZArC zHNHP7CJEDJNwqZXzQICmEGc9sF9mLvxoZai2uFCfgeFuchXBQ8ymx9bSa/sYsZZZ5of DgCBYhZNK7cQGU0ytFq38Gzp//y9nlwE7cianco9/3BmBv0+zbT4MxsBkTw9861JfB/D /6BuW0FjG5cHXfMZFswqDmTNODExbDbgix4GN4tXMw0juiET4lzbktVb6oS5Rfg3il+R hIoA== X-Forwarded-Encrypted: i=1; AFNElJ+IczZNL+01OHV/Gve6ekMLPlvlZGjurgEJsSIvlyNXCnFSg0KeWWph4boKcBiJ3k1sAahul4IUyirGFlo=@vger.kernel.org X-Gm-Message-State: AOJu0YyWyH9lGUKxXbE2m+3H4dn8j6gMk6wXdI1oncxzsCitMdrPb/yc T5/NtbKCOy6zzPw05y3YdORmIkYQj+wwMYIo+IY4Qv4PWo3fnWTxpku5LX1Wxt9HL55wWnuNnip EkhpaIw== X-Received: from pgbcr5.prod.google.com ([2002:a05:6a02:4105:b0:c79:67c5:c6b0]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:9186:b0:3a2:cbd1:11e with SMTP id adf61e73a8af0-3aa8c098d8emr8683210637.14.1778282042086; Fri, 08 May 2026 16:14:02 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:50 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-7-seanjc@google.com> Subject: [PATCH v3 6/9] perf/x86: KVM: Have perf define a dedicated struct for getting guest PEBS data From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Have perf define a struct for getting guest PEBS data from KVM instead of poking into the kvm_pmu structure. Passing in an entire "struct kvm_pmu" _as an opaque pointer_ to get at four fields is silly, especially since one of the fields exists purely to convey information to perf, i.e. isn't used by KVM. Perf should also own its APIs, i.e. define what fields/data it needs, not rely on KVM to throw fields into data structures that effectively hold KVM-internal state. Opportunistically rephrase the comment about cross-mapped counters to explain *why* PEBS needs to be disabled. Reviewed-by: Dapeng Mi Reviewed-by: Jim Mattson Signed-off-by: Sean Christopherson --- arch/x86/events/core.c | 5 +++-- arch/x86/events/intel/core.c | 16 ++++++++-------- arch/x86/events/perf_event.h | 3 ++- arch/x86/include/asm/kvm_host.h | 9 --------- arch/x86/include/asm/perf_event.h | 12 ++++++++++-- arch/x86/kvm/vmx/pmu_intel.c | 17 ++++++++++++++--- arch/x86/kvm/vmx/vmx.c | 11 ++++++++--- arch/x86/kvm/vmx/vmx.h | 2 +- 8 files changed, 46 insertions(+), 29 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 810ab21ffd99..e6f788e72e72 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -723,9 +723,10 @@ void x86_pmu_disable_all(void) } } =20 -struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data) +struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, + struct x86_guest_pebs *guest_pebs) { - return static_call(x86_pmu_guest_get_msrs)(nr, data); + return static_call(x86_pmu_guest_get_msrs)(nr, guest_pebs); } EXPORT_SYMBOL_FOR_KVM(perf_guest_get_msrs); =20 diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 7f7c7927b70b..e9acfc3f3a82 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -14,7 +14,6 @@ #include #include #include -#include =20 #include #include @@ -4992,11 +4991,11 @@ static int intel_pmu_hw_config(struct perf_event *e= vent) * when it uses {RD,WR}MSR, which should be handled by the KVM context, * specifically in the intel_pmu_{get,set}_msr(). */ -static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, void *d= ata) +static struct perf_guest_switch_msr *intel_guest_get_msrs(int *nr, + struct x86_guest_pebs *guest_pebs) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); struct perf_guest_switch_msr *arr =3D cpuc->guest_switch_msrs; - struct kvm_pmu *kvm_pmu =3D (struct kvm_pmu *)data; u64 intel_ctrl =3D hybrid(cpuc->pmu, intel_ctrl); u64 pebs_mask =3D cpuc->pebs_enabled & x86_pmu.pebs_capable; u64 guest_pebs_mask; @@ -5050,7 +5049,7 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) * the guest wants to use for PEBS, (c) are not excluded from counting * in the guest, and (d) _are_ excluded from counting in the host. */ - guest_pebs_mask =3D pebs_mask & intel_ctrl & kvm_pmu->pebs_enable & + guest_pebs_mask =3D pebs_mask & intel_ctrl & guest_pebs->enable & ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask; =20 @@ -5060,7 +5059,7 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) * PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the * wrong counter(s). */ - guest_pebs_mask &=3D ~kvm_pmu->host_cross_mapped_mask; + guest_pebs_mask &=3D ~guest_pebs->cross_mapped_mask; =20 /* * FIXME: Allow guest and host usage of PEBS events to co-exist instead @@ -5079,14 +5078,14 @@ static struct perf_guest_switch_msr *intel_guest_ge= t_msrs(int *nr, void *data) arr[(*nr)++] =3D (struct perf_guest_switch_msr){ .msr =3D MSR_IA32_DS_AREA, .host =3D (unsigned long)cpuc->ds, - .guest =3D guest_pebs_mask ? kvm_pmu->ds_area : (unsigned long)cpuc->ds, + .guest =3D guest_pebs_mask ? guest_pebs->ds_area : (unsigned long)cpuc->= ds, }; =20 if (x86_pmu.intel_cap.pebs_baseline) { arr[(*nr)++] =3D (struct perf_guest_switch_msr){ .msr =3D MSR_PEBS_DATA_CFG, .host =3D cpuc->active_pebs_data_cfg, - .guest =3D guest_pebs_mask ? kvm_pmu->pebs_data_cfg : + .guest =3D guest_pebs_mask ? guest_pebs->data_cfg : cpuc->active_pebs_data_cfg, }; } @@ -5102,7 +5101,8 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) return arr; } =20 -static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr, void *da= ta) +static struct perf_guest_switch_msr *core_guest_get_msrs(int *nr, + struct x86_guest_pebs *guest_pebs) { struct cpu_hw_events *cpuc =3D this_cpu_ptr(&cpu_hw_events); struct perf_guest_switch_msr *arr =3D cpuc->guest_switch_msrs; diff --git a/arch/x86/events/perf_event.h b/arch/x86/events/perf_event.h index cc0aeeb34eb5..9183b3607962 100644 --- a/arch/x86/events/perf_event.h +++ b/arch/x86/events/perf_event.h @@ -1023,7 +1023,8 @@ struct x86_pmu { /* * Intel host/guest support (KVM) */ - struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr, void *data); + struct perf_guest_switch_msr *(*guest_get_msrs)(int *nr, + struct x86_guest_pebs *guest_pebs); =20 /* * Check period value for PERF_EVENT_IOC_PERIOD ioctl. diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index c470e40a00aa..91b070168947 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -600,15 +600,6 @@ struct kvm_pmu { u64 pebs_data_cfg; u64 pebs_data_cfg_rsvd; =20 - /* - * If a guest counter is cross-mapped to host counter with different - * index, its PEBS capability will be temporarily disabled. - * - * The user should make sure that this mask is updated - * after disabling interrupts and before perf_guest_get_msrs(); - */ - u64 host_cross_mapped_mask; - /* * The gate to release perf_events not marked in * pmc_in_use only once in a vcpu time slice. diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index 752cb319d5ea..bc7e48f6f4a8 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -786,11 +786,19 @@ extern void perf_load_guest_lvtpc(u32 guest_lvtpc); extern void perf_put_guest_lvtpc(void); #endif =20 +struct x86_guest_pebs { + u64 enable; + u64 ds_area; + u64 data_cfg; + u64 cross_mapped_mask; +}; #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL) -extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *da= ta); +extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, + struct x86_guest_pebs *guest_pebs); extern void x86_perf_get_lbr(struct x86_pmu_lbr *lbr); #else -struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, void *data); +struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, + struct x86_guest_pebs *guest_pebs); static inline void x86_perf_get_lbr(struct x86_pmu_lbr *lbr) { memset(lbr, 0, sizeof(*lbr)); diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 27eb76e6b6a0..e65adb3dc066 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -736,11 +736,21 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu) intel_pmu_release_guest_lbr_event(vcpu); } =20 -void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu) +u64 intel_pmu_get_cross_mapped_mask(struct kvm_pmu *pmu) { - struct kvm_pmc *pmc =3D NULL; + u64 host_cross_mapped_mask; + struct kvm_pmc *pmc; int bit, hw_idx; =20 + /* + * Provide a mask of counters that are cross-mapped between the guest + * and the host, i.e. where a guest PMC is mapped to a host PMC with a + * different index. PEBS records hold a PERF_GLOBAL_STATUS snapshot, + * and so PEBS-enabled counters need to hold the correct index so as + * not to confuse the guest. + */ + host_cross_mapped_mask =3D 0; + kvm_for_each_pmc(pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) { if (!pmc_is_locally_enabled(pmc) || !pmc_is_globally_enabled(pmc) || !pmc->perf_event) @@ -752,8 +762,9 @@ void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu) */ hw_idx =3D pmc->perf_event->hw.idx; if (hw_idx !=3D pmc->idx && hw_idx > -1) - pmu->host_cross_mapped_mask |=3D BIT_ULL(hw_idx); + host_cross_mapped_mask |=3D BIT_ULL(hw_idx); } + return host_cross_mapped_mask; } =20 static bool intel_pmu_is_mediated_pmu_supported(struct x86_pmu_capability = *host_pmu) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index a29896a9ef14..9f0a028cf10b 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7313,12 +7313,17 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx= *vmx) if (kvm_vcpu_has_mediated_pmu(&vmx->vcpu)) return; =20 - pmu->host_cross_mapped_mask =3D 0; + struct x86_guest_pebs guest_pebs =3D { + .enable =3D pmu->pebs_enable, + .ds_area =3D pmu->ds_area, + .data_cfg =3D pmu->pebs_data_cfg, + }; + if (pmu->pebs_enable & pmu->global_ctrl) - intel_pmu_cross_mapped_check(pmu); + guest_pebs.cross_mapped_mask =3D intel_pmu_get_cross_mapped_mask(pmu); =20 /* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ - msrs =3D perf_guest_get_msrs(&nr_msrs, (void *)pmu); + msrs =3D perf_guest_get_msrs(&nr_msrs, &guest_pebs); if (!msrs) return; =20 diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index db84e8001da5..0c4563472940 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -659,7 +659,7 @@ static __always_inline struct vcpu_vmx *to_vmx(struct k= vm_vcpu *vcpu) return container_of(vcpu, struct vcpu_vmx, vcpu); } =20 -void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu); +u64 intel_pmu_get_cross_mapped_mask(struct kvm_pmu *pmu); int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu); void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu); =20 --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.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 001A03BBA04 for ; Fri, 8 May 2026 23:14:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282045; cv=none; b=lc+6tTN8r0JMBgSZIHCVWPuMKtKgGrrR5wbo7t9p9sA/xIao8TSKj/d31Fw0E7m5+DqZIDDppsnABPXTxfPbvTk5kktjlg+xkFeARlIYdPmT1b4b9kfRRqVLWEyIN5hPbwuWjJyU+tAgtBjJKmMacU0U6TdeRyY3dtu6cIp5iWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282045; c=relaxed/simple; bh=otxpZlAkHL5y/QJE3G7rXySXlAmg9ZfAXrLlLt/iukw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eXX7RJ7OUC1xbBmE+AwPpluwlkikgiMtOK3VQiFQilRXp0GabGOR/0EY97b8TFabA6Duz1cPcvshcQUsbx0J3n2dp8ABo1y6bH2EBakaZS8fVXypeZQY39qHGwldMxXy1trPUxa0k5Zx28CVj8aAHexNxnZgx2JWDHbtZZzhUkQ= 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=aHSEJIo8; arc=none smtp.client-ip=209.85.214.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="aHSEJIo8" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2b9a6d84522so46650545ad.2 for ; Fri, 08 May 2026 16:14:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282043; x=1778886843; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=3hn/vCE+SheJ11Toa1ORgidQGCGEmHGjeA0kV3k6HSI=; b=aHSEJIo8yiUuaIJPqXXdH5YPIOyhvJxNQ4wU6b8VYp+d56noOimiqNwf/4knnhrt3H lxeXwaIe0uu1RwYW9agkuyCeSyjDxw1vGvvz+ql/TeM/TPfOkxG1E05+5mOWVmcJLWUp QdmqGW21M04iJf/U/NKP930Ri8p+ROpOlhKEq3Inq+SdlhifImO+p++NdOOc1EowHqSE DRJL3qXfZl7xPXXVVpwl/tUrqLbUGmXN221REiomrbICJDUeKeyhOr5d5GOcTYVgRvHZ cCLqMCWalLqdhV6K2nMlGMeaceZ1lZW7rKEuYxCkfuOsN51Ler3W6cuhDsOihyTy/Zex wXQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282043; x=1778886843; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3hn/vCE+SheJ11Toa1ORgidQGCGEmHGjeA0kV3k6HSI=; b=SYA1LYZ4fEOx3RRFvlhzt2VctkEDnEfNm+/cNoc74Bx+l/CQHluvaMMpKHySCvkDF+ 6CTl+yihf+2mwnQpnBM8rYOIStpO0wTIZidduCt0I2UeQnR9/nHhoOHQJofc1XQgt1HY mf61ZdqkEXMBtTAv6WecnDkbDAv1YQu/f+xD8QVy7yerJpTywd9K+v2+OBzihOhsw5Ev VPV/GbhCWnXpXufnHBJI5dnY+inLA3hn1FtKPoLBRRqC230I9x1gm09VD3swzvGoQn9O MXGkur9rj8mWezwSXZf+pZiYClWjQtbagQUYVF7HhrTvPxZkmu94wUb9V9b7hgvP0mK0 biwA== X-Forwarded-Encrypted: i=1; AFNElJ8SYVet/Mu8FoqdXHpIR6cCymJjW5HlwOJq7lNrMG6doLBX1kO95dlpGFgHhxLliAoGAg3w6RJ7s4bF4Wg=@vger.kernel.org X-Gm-Message-State: AOJu0YzAIt3M0xxErJpZoUStAryDXpDg+i0GpoYeuR978P85uIE//iRt QbXCcUrwTXkjtNiKvF8U4LERIGe2Ds3kacMgM5dgVnOFA+x1O2SeznhyNtrjfAcyt5lrrIp9OmA ODPANTQ== X-Received: from plot2.prod.google.com ([2002:a17:902:8c82:b0:2b2:50b1:327c]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c410:b0:2ba:839e:15dc with SMTP id d9443c01a7336-2bc7a9b46a8mr2444545ad.17.1778282043244; Fri, 08 May 2026 16:14:03 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:51 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-8-seanjc@google.com> Subject: [PATCH v3 7/9] perf/x86/intel: KVM: Handle cross-mapped PEBS PMCs entirely within KVM From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now that perf operates on a KVM-provided snapshot of PMU state, handled cross-mapped PEBS counters entirely in KVM by clearing unusable counters from the to-be-enabled mask instead of foisting the work on perf. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Dapeng Mi --- arch/x86/events/intel/core.c | 8 -------- arch/x86/include/asm/perf_event.h | 1 - arch/x86/kvm/vmx/vmx.c | 10 ++++++++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index e9acfc3f3a82..8f6be0cc4c4b 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -5053,14 +5053,6 @@ static struct perf_guest_switch_msr *intel_guest_get= _msrs(int *nr, ~cpuc->intel_ctrl_exclude_guest_mask & cpuc->intel_ctrl_exclude_host_mask; =20 - /* - * Disable counters where the guest PMC is different than the host PMC - * being used on behalf of the guest, as the PEBS record includes - * PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the - * wrong counter(s). - */ - guest_pebs_mask &=3D ~guest_pebs->cross_mapped_mask; - /* * FIXME: Allow guest and host usage of PEBS events to co-exist instead * of disabling guest PEBS entirely if the host is using PEBS. diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_= event.h index bc7e48f6f4a8..19f874a79ab0 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -790,7 +790,6 @@ struct x86_guest_pebs { u64 enable; u64 ds_area; u64 data_cfg; - u64 cross_mapped_mask; }; #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL) extern struct perf_guest_switch_msr *perf_guest_get_msrs(int *nr, diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 9f0a028cf10b..fbe3ce5f5a51 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7319,8 +7319,14 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx = *vmx) .data_cfg =3D pmu->pebs_data_cfg, }; =20 - if (pmu->pebs_enable & pmu->global_ctrl) - guest_pebs.cross_mapped_mask =3D intel_pmu_get_cross_mapped_mask(pmu); + /* + * Disable counters where the guest PMC is different than the host PMC + * being used on behalf of the guest, as the PEBS record includes + * PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the + * wrong counter(s). + */ + if (guest_pebs.enable & pmu->global_ctrl) + guest_pebs.enable &=3D ~intel_pmu_get_cross_mapped_mask(pmu); =20 /* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ msrs =3D perf_guest_get_msrs(&nr_msrs, &guest_pebs); --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.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 140283BE14A for ; Fri, 8 May 2026 23:14:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282046; cv=none; b=Nln5DLapAvgiLGru/rZzIVDoF+RrPjwgQQZT12LZ8ibaHjaOyPAs4XEsTEGJV5wLYrCIm9o+y2wQkRW2k4D/zfFsUiM6SmUdvY/uQ9siQYuLewXVV2ddDMW7e3Okw+QwzoLr7Jmgl5ujSFuyh9oHD3EWgcfTYD34eoGL7PTYlNM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282046; c=relaxed/simple; bh=Jr9GHQcf3RdzwwtRnCItKo4sCUWRHjF+O7j7BWlNpGw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=R3cOzKKfHmZusmokYP9YtKHEvmqF3/42eAVtulzm5zECmnWjGVKbr4H4UM8rNIRlYw1mrsKbhhRp4K99HBnokBqOnb/t6w82VasRAq/KxBB6w1A9BuunfTjwxSa6RT/CUCyBCftHxCCP3AOo919V4wmgYX5+2yBTbk+v6GDpCIM= 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=sON+IBCw; arc=none smtp.client-ip=209.85.215.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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sON+IBCw" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-c823549b1fcso3167037a12.0 for ; Fri, 08 May 2026 16:14:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282044; x=1778886844; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ESlcbjSp3UYDyPbVz7XSSV6N6GJseh6UMdqjjv4ba1M=; b=sON+IBCwcvnbU450MAwPaIOM6GWgEGV6mN6Apzsw8aoqjTFCc47TwyMCnEDkmNA5PK GCjIvk/gdJyxRlO+a+nP1aHvNO34WqNtSiCav8M7yYN1VvjZcJbhyaWTYr3iAOLUzVBe 427k9vzXa9I8YPrhUjY4HNTcR6fDthO7w960JazhlOJbqD8SPDE5FND9hafcypGg7dhQ 5qvspLJiHLLViNGnPRuIUKCBGKIBdlcKNzYhQdbNKaQPLS/ml/mskJQSuWigHN0t3l7b znhxvTNKlAFjBJea8GR2fqWj5+/QFNU2XvWsxg1jNy4vscXAt3XNRCX/nlaQBnegDBDb P3bA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282044; x=1778886844; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ESlcbjSp3UYDyPbVz7XSSV6N6GJseh6UMdqjjv4ba1M=; b=PmwdEGjeFIW5+qk9SJZ6cW2eAQE735YclYnVhKzsNz8XxxN8carQNeN/C3WU+WlHy5 sjAOVJmjFoOJsYG6zt0DqCPXz87m+3+k+dbVdauro3yOfA7SaoW+TTWTq7s6bzW812ag 6RWcpnbeqc4WaOJoP/UPvJf9f2Nbrul92crngmTZ5OV4KojaQBsm12tE3NN39zp2N0UW T3/8n2McnaNsdsgvUSVN1r7a/hz5eqwx+Xl3xnTm/BPakpXA6zoYEFI91qnx1/F8HaSu xrs6TnoSP/Ps7VT8KpIugc/7LZ5/m3qqSDe8PznaDz6ba1SkMZs8bw+Um4hbAbLGpGD9 ILoA== X-Forwarded-Encrypted: i=1; AFNElJ/hDGxpa2LxHG5xdANR13fLRTmrYuJw8B1GGGwMsPpCJtoWEqubQGZ9LkZ51PX8Y0tZJxw4ETCi/6TUfsI=@vger.kernel.org X-Gm-Message-State: AOJu0YzUABiKtirgeAIIorndLLKndCjRht59zh3/M0fYj1EwOzch3b0u z5sZ8TtKG/pcHrh3wtKouDEP+3mtG5wCoHMG3wRsOgHHsTgX4zTQbydFI3CMcbmWcnNPseatrQd 5wVjmqA== X-Received: from pgbq24.prod.google.com ([2002:a63:5c18:0:b0:c79:8315:4734]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:3941:b0:3a2:9c28:9c3c with SMTP id adf61e73a8af0-3aa5a831d73mr16499680637.11.1778282044263; Fri, 08 May 2026 16:14:04 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:52 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-9-seanjc@google.com> Subject: [PATCH v3 8/9] KVM: VMX: Drop a redundant pmu->global_ctrl check when processing pebs_enable From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Drop a redundant check that a PMC is globally enabled when looking for PEBS counters that are cross-mapped between the guest and the host. The for-loop explicitly iterates over pmu->global_ctrl, and since PEBS requires PMU v2+, kvm_pmu_has_perf_global_ctrl() must be true, and thus pmc_is_globally_enabled() is simply checking that the bit is set in pmu->global_ctrl. No functional change intended. Signed-off-by: Sean Christopherson Reviewed-by: Dapeng Mi --- arch/x86/kvm/vmx/pmu_intel.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index e65adb3dc066..659fe097b904 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -752,8 +752,7 @@ u64 intel_pmu_get_cross_mapped_mask(struct kvm_pmu *pmu) host_cross_mapped_mask =3D 0; =20 kvm_for_each_pmc(pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) { - if (!pmc_is_locally_enabled(pmc) || - !pmc_is_globally_enabled(pmc) || !pmc->perf_event) + if (!pmc_is_locally_enabled(pmc) || !pmc->perf_event) continue; =20 /* --=20 2.54.0.563.g4f69b47b94-goog From nobody Sat Jun 13 07:47:12 2026 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 04D6A3BED69 for ; Fri, 8 May 2026 23:14:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282047; cv=none; b=j8Hx1ddQ1pFmpRMry8X3+iWtkqJ+DmVbpRFu/eOjDbVRo4SPxYrSrUil1vwyzObdw0/4NH/oAtsQMk+NtuL+kMY3aFbfnRkFQiROWfJC6HyXj9tcRUnFXKoAf2Mk5i3tnD8DuZoUNwfKtMzzb6AXLjliepHYXKKPW1+IKivrzT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778282047; c=relaxed/simple; bh=QUSnhE9ck70LWBa0E8tFu6PQQvS6B9ynchkFpZ3LobQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aaqtDWJAxs4u4IrroBsKgUbmfTdqWoNqUW9SNtp3OYGvNqNOGtjAgFjoE9qb7NOyXtVS3SE0TuKpPSf3Qcov6ufwR7oeEy7vWtP4iqMjGIAL3GnelzcNdQePT98z35kdOGCZoDWreO9Em+2CPl8MLAGmVcLmZFxWt2ycfZ56IRw= 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=VFoSOovv; arc=none smtp.client-ip=209.85.216.74 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="VFoSOovv" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-354c44bf176so3011158a91.0 for ; Fri, 08 May 2026 16:14:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778282045; x=1778886845; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=Tth6ytDURza4+B9GhJKjukpnUFdXZZplI0rD3xV9jqc=; b=VFoSOovvx+lQu5yRR8Ucn0k5rXbEXku8B0lKbcTUvMd/74jTt/BsXl30jXOEjqplNI tQMxysvypgHU+VljU0tJznwQvOXVwc1Pe3OyJe96cS+xfcMumsVpesOZbtv9IKJfWOj5 p6vnn87RFsaQGGpKLFFN8oA3s2BRHDLLpcJN/wEinhlQfL5/gCQzdt0ZlEjn7GTKxfTk 3ObVKeSIgqsQh1rm5y5v68T1aSG9lJeDKj7+mgDriCiJJ/7cM5qWMWxwFHWXAkdcJtm/ gQkQddQ1VzkM+df6eEvaa8yQQcC6VlYGRY6s0+3rUPc+EC+qDbpUxjlwD/JmmugTyMtG fZGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778282045; x=1778886845; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Tth6ytDURza4+B9GhJKjukpnUFdXZZplI0rD3xV9jqc=; b=pk8x0gw0+KDAd5X2btaE8kyGQcpTBGkYWFyodSVpCzc3LulI+GVqpEizihSH5uzDn/ 9nKGgKuoOTd9PtVNxWwFSyrlG06ywW5IBgNvu5kAi632DDCuXxqjlHQAiFkRIhtWT5sF 3RGgVq3uZJYIuA0Exxj+EzWuE2qkc+FkQXQPZ2FnUt6LS36Ygaj0WwUy3ZhfXA2e0Utj OlCmunGEopSy2s8q9/Yku8GzuLXbHyX5s210ke6TnRbpqzL4ti7ltDrrh6ZL+FM94HuX Dw8z48SCkfC7+HbT9O3O/H47eRfqf/+t50ES7LvwEY3JbtAfh29N19Zr+wgu1WTlaGq3 swJA== X-Forwarded-Encrypted: i=1; AFNElJ8v+mI/RxhRCSfRvoXLVUUDDgA+sg1UXIJ+cZLpGntk91UDU1mCfqSURDaGysXWv0u/GCGm216CM8VFH2c=@vger.kernel.org X-Gm-Message-State: AOJu0YzYX/CUDiRJ1SkWY5yrh0C3A31r7xC+nNMsK3nkOJvpcApSU3Fb rS0OBviUQu5c0oAQ+x4pDHgYFerqmYejkVxuoJ7xayEEaxElgLHTCVtwbBYA5B/pXc0AOIrJtUd 2fV0tqQ== X-Received: from pgne29.prod.google.com ([2002:a63:745d:0:b0:c79:81df:1175]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:5683:b0:359:87a8:e65c with SMTP id 98e67ed59e1d1-365ac080573mr15363603a91.17.1778282045290; Fri, 08 May 2026 16:14:05 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 8 May 2026 16:13:53 -0700 In-Reply-To: <20260508231353.406465-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260508231353.406465-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260508231353.406465-10-seanjc@google.com> Subject: [PATCH v3 9/9] KVM: VMX: Only tell perf to enable PEBS counters for fully enabled PMCs From: Sean Christopherson To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Jim Mattson , Mingwei Zhang , Stephane Eranian , Dapeng Mi Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When passing the guest's requested PEBS_ENABLE (or rather, KVM's version of PEBS_ENABLE on behalf of the guest), omit counters that are locally disable and/or don't have a perf event (due to contention), in addition to omitting counters that are cross-mapped in the host. In practice, this should be a nop as perf will already have disabled the associated counter, i.e. cpuc->pebs_enabled should have been cleared, but paranoia is cheap, and the existing code _looks_ wrong. Signed-off-by: Sean Christopherson Reviewed-by: Dapeng Mi --- arch/x86/kvm/vmx/pmu_intel.c | 30 ++++++++++++++++-------------- arch/x86/kvm/vmx/vmx.c | 11 +---------- arch/x86/kvm/vmx/vmx.h | 15 ++++++++++++++- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index 659fe097b904..1e420c8bca9d 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -736,34 +736,36 @@ static void intel_pmu_cleanup(struct kvm_vcpu *vcpu) intel_pmu_release_guest_lbr_event(vcpu); } =20 -u64 intel_pmu_get_cross_mapped_mask(struct kvm_pmu *pmu) +u64 __intel_pmu_compute_pebs_enable(struct kvm_pmu *pmu) { - u64 host_cross_mapped_mask; + u64 guest_pebs_enable =3D pmu->pebs_enable & pmu->global_ctrl; + u64 pebs_enable =3D 0; struct kvm_pmc *pmc; int bit, hw_idx; =20 /* - * Provide a mask of counters that are cross-mapped between the guest - * and the host, i.e. where a guest PMC is mapped to a host PMC with a - * different index. PEBS records hold a PERF_GLOBAL_STATUS snapshot, - * and so PEBS-enabled counters need to hold the correct index so as - * not to confuse the guest. + * Omit counters that are locally disabled, don't have a perf event, or + * ended up with a perf event that is using a different counter than + * the guest, i.e. where the guest PMC is different than the host PMC + * being used on behalf of the guest. PEBS records include + * PERF_GLOBAL_STATUS, and so using a counter with a different index + * means the guest will see overflow status for the wrong counter(s). */ - host_cross_mapped_mask =3D 0; - - kvm_for_each_pmc(pmu, pmc, bit, (unsigned long *)&pmu->global_ctrl) { + kvm_for_each_pmc(pmu, pmc, bit, (unsigned long *)&guest_pebs_enable) { if (!pmc_is_locally_enabled(pmc) || !pmc->perf_event) continue; =20 /* - * A negative index indicates the event isn't mapped to a + * Note, a negative index indicates the event isn't mapped to a * physical counter in the host, e.g. due to contention. */ hw_idx =3D pmc->perf_event->hw.idx; - if (hw_idx !=3D pmc->idx && hw_idx > -1) - host_cross_mapped_mask |=3D BIT_ULL(hw_idx); + if (hw_idx !=3D pmc->idx) + continue; + + pebs_enable |=3D BIT_ULL(pmc->idx); } - return host_cross_mapped_mask; + return pebs_enable; } =20 static bool intel_pmu_is_mediated_pmu_supported(struct x86_pmu_capability = *host_pmu) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index fbe3ce5f5a51..31675e5cf563 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7314,20 +7314,11 @@ static void atomic_switch_perf_msrs(struct vcpu_vmx= *vmx) return; =20 struct x86_guest_pebs guest_pebs =3D { - .enable =3D pmu->pebs_enable, + .enable =3D intel_pmu_compute_pebs_enable(pmu), .ds_area =3D pmu->ds_area, .data_cfg =3D pmu->pebs_data_cfg, }; =20 - /* - * Disable counters where the guest PMC is different than the host PMC - * being used on behalf of the guest, as the PEBS record includes - * PERF_GLOBAL_STATUS, i.e. the guest will see overflow status for the - * wrong counter(s). - */ - if (guest_pebs.enable & pmu->global_ctrl) - guest_pebs.enable &=3D ~intel_pmu_get_cross_mapped_mask(pmu); - /* Note, nr_msrs may be garbage if perf_guest_get_msrs() returns NULL. */ msrs =3D perf_guest_get_msrs(&nr_msrs, &guest_pebs); if (!msrs) diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h index 0c4563472940..b055731efd2d 100644 --- a/arch/x86/kvm/vmx/vmx.h +++ b/arch/x86/kvm/vmx/vmx.h @@ -659,7 +659,20 @@ static __always_inline struct vcpu_vmx *to_vmx(struct = kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_vmx, vcpu); } =20 -u64 intel_pmu_get_cross_mapped_mask(struct kvm_pmu *pmu); +u64 __intel_pmu_compute_pebs_enable(struct kvm_pmu *pmu); + +static inline u64 intel_pmu_compute_pebs_enable(struct kvm_pmu *pmu) +{ + /* + * Avoid the function call overhead in the common case that the guest + * isn't using PEBS. + */ + if (!(pmu->pebs_enable & pmu->global_ctrl)) + return 0; + + return __intel_pmu_compute_pebs_enable(pmu); +} + int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu); void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu); =20 --=20 2.54.0.563.g4f69b47b94-goog