From nobody Sat Jun 20 14:13:05 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 0BF2730DD2A for ; Tue, 14 Apr 2026 19:14:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776194073; cv=none; b=hNMzGB/QfGyLvo6p/sDJZfYWhfRYpW9JpcB6vGBJf6YSuMrXpIiyelKxf+JppN4EgeAp1wW99NcfyVSpqE54zJSVfIy1AcJMp8FMon90v54d68pL7XTgzUaZn1xjkYsOQbyxnhrzZedJpPc0t5SO+ka8DaNToJaY5wvVfqycyk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776194073; c=relaxed/simple; bh=uiGyZyPT5hlzy1iTKDD7iGlyzr3ZC4NXS8HayNsphJg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XxU1FZJAREJDfkZotgwrfUBxinHZTjZqVYZx3ChRWMLeeFFCP0yr1VfwY//5sa86Fasp954XXd+rS7kovD5cDEfC+15Sxob40qWtHf34H3nSnoCPjZ6DriXmxFBseSYxVqbw0kWnSTUjUKLKPZjSr39IKtL9E0s+pg54sfNNxDY= 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=HzY2Q1r3; arc=none smtp.client-ip=209.85.210.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="HzY2Q1r3" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82c89d4ce16so3920488b3a.2 for ; Tue, 14 Apr 2026 12:14:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776194071; x=1776798871; 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=xz+UZ8XCmjNlzGjmh5Kd0VePfg/dEcfDpAcdkqpwPgc=; b=HzY2Q1r3T3VtH36PW3hsGoX+oFMl2X+pjFIV6ZDwObKDiPAOiLw/cLaGSF2CLdUJMo 6+DmxyarAhbWvzxdz5QOgUZc4krHvSl4cTicPUhZeW/G8fXMk7KAtWJZ40nF3P1iuS1z 2KdDR6YMgBjEb8qpKcr++2o8brdU7wgY3JCgxcN/VQEr8Y+AUf2yO5qECSa0v4JqZMrk NQAMItn0TfjTkahQ8x/aARisDeWRZGZQERgoX68/O3nn9DhsNJ2k2EVvTHg9dj9c/4Ao QzPuiHPftG8hnveBNav8bRQSyHIRv9iJQLawVQHGcsYGiVQiqkOvT4vvTM+96GMgdDDR xWvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776194071; x=1776798871; 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=xz+UZ8XCmjNlzGjmh5Kd0VePfg/dEcfDpAcdkqpwPgc=; b=NrP3axrX5dMr1ikShdfIAccx+Z/fMhYgS3sT3yRZw3HAg+EFIieH7dBTrNDfWe4Hh3 IE4x9n75C0x7ibEV9NNaEojYDYFwDr98olK3kAa5zX/KzeIxzeHVceF2ApzrHz6GLV8B KevZ8e3nDvPai8PYKqOWIIgJmOQ0IhQCvanbkFbLOplAUAu6AvE3BDS+hRx4N/O9ZUL0 QH2jE/qFMqe+NKDvXLW5xYHmIL94E1UCGqDK5kDul9v3x68BPBseGNV0lPBW/GUxBcFl YGauunQ5UbW1Qx6F1SVFm17ST0gTEMGaHWsOFZ383VVP7Adbf/naCZeQhVsiOHOTu0Xw BKoQ== X-Forwarded-Encrypted: i=1; AFNElJ9rNGEzf2vi1/fwyd9Foc835p65EVcrWq6/hLSgeqLx65ivmlgNIXhT032f9YAn8dk5yHImMpIkvy5LikM=@vger.kernel.org X-Gm-Message-State: AOJu0YyqhTXqUj8to+2LVRw38ToXLKg92thMQLSldwQaNi9rGeWpeROD tCXq6nHZqTfpHCNfp/YO381QNv1tWyK8Hn5j9rEK60ZZauUqxHAgXOW/gAJCnk5/t3F+ITwzvZA s3VQvTw== X-Received: from pfbna39.prod.google.com ([2002:a05:6a00:3e27:b0:82f:6c3:2b84]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1946:b0:82c:dde3:f2ef with SMTP id d2e1a72fcca58-82f0c35f7c9mr18298797b3a.50.1776194071225; Tue, 14 Apr 2026 12:14:31 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Apr 2026 12:14:22 -0700 In-Reply-To: <20260414191425.2697918-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: <20260414191425.2697918-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.rc0.605.g598a273b03-goog Message-ID: <20260414191425.2697918-2-seanjc@google.com> Subject: [PATCH 1/4] 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 , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini Cc: 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 better generating the list and actually entering the guest (Intel CPUs can't arbtitrarily block NMIs). And stating the obvious, leaving PEBS_ENABLED as-is avoids two MSR writes on every VMX transition. 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 --- arch/x86/events/intel/core.c | 42 ++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 793335c3ce78..002d809f82ef 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 =3D pebs_mask & ~cpuc->intel_ctrl_host_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,25 @@ 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, - }; - - 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 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). Similarly, disallow PEBS in the guest if the host + * is using PEBS, to avoid bleeding host state into PEBS records. + */ + guest_pebs_mask &=3D kvm_pmu->pebs_enable & ~kvm_pmu->host_cross_mapped_m= ask; + if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) + guest_pebs_mask =3D 0; =20 + /* + * 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.rc0.605.g598a273b03-goog From nobody Sat Jun 20 14:13:05 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 F0C0B313550 for ; Tue, 14 Apr 2026 19:14:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776194075; cv=none; b=IXPra98p01YVWslVXeTI2R/nhVQRUl5qG/+2r+Eynx0grJjoMi+3SJCz3MVECQLanCmBnvc4Y9+ut6MZ4RFGfLm919H9iZNVaxEK0eLP1P3bU0/zd0+9A2TfDVUtqqUL4lOyrdrs5l1zbqXifiCAiIt1B4uZ3oJ5ttT5r0pgUU8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776194075; c=relaxed/simple; bh=KvIG1l/WweA6fzYjmK6M8jEGrTyoO4PTSmR+fEG8fq8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kQbz8XR4v6k+Jx2cE+Ur3atfluZOFvWzRIw44zyCc32ww4XUbz4BA08AeDmhmUt3/03YjFjJbYy/grF4YI32Sztn9e62cBWN9xTipI5tTLs+Wfbuq6b/KEh5NVAVuF+2DpdgoVC4IXhZSs0sXeZZhoFqXLQ78JHVLVgnMBwBsXw= 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=Bpg3D5eC; arc=none smtp.client-ip=209.85.210.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="Bpg3D5eC" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82f460260cfso2469428b3a.2 for ; Tue, 14 Apr 2026 12:14:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776194073; x=1776798873; 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=zD3eOn27bjJqO09iUaVSsE+WSP9GPb4HLOOe5wuJNrI=; b=Bpg3D5eCNq2Hm8PSmF3I6MT/t5SnpRJGPSFyuo5vbwmCJSsvg22BnJZtCzrEjox8jk Sr9ovUI8PuDDfUm1gdJ70tukRqq508V1hL0+IT2FWTngaP/yApbhTdSxsIaAxXlqbvDo RpmE/e4KTrWRThj7WtEkWL6EIXkbtWGWnC0Aeqi4l0Dfyy4MQ6QTym4dRmivlfEGzO3b y8BIgDVVrPWvXHP+m8aRLs6NeKjgqKkqjMTyimwLmltglXW8zPv7qzrCQWNqzDpGd43e vO7i81RrCYXCm4OpUg1yOoeQxuL3kmWXkf3/+GxrARyTMdokrQU4txnX12j0sK7SwEi1 sQkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776194073; x=1776798873; 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=zD3eOn27bjJqO09iUaVSsE+WSP9GPb4HLOOe5wuJNrI=; b=p5qV0ZHBiZ0pnvHIYZ5bFOPaQXfwQnXIOcyX9oax3tcyG1wwzzt3dq7ZS0w67cUnYB JGnfMbpGB1mb5R2ESnsiHtpqULe5wvNJMGZ8Z5MHMGo9NUoz8XhSConuIHQnhIW0yWzR stOeirnwDLUepN4oNvccVXLl/mevKsqSD6thjAWzlANp5HyU1DjrLm05oORf8hzvXX6G 9W7z/+2toFrOBzesr1momTHjNOupyO9tFyRdGoMxvh8ipVhIqGnX2cXsHcn2j7u+Jufl yJYSXurwJLO9k7CVtt6d3C04zsrOFvRxjC/Dz79/X1rvXPGli/WyJByYb/vb7x1c4Abe etUw== X-Forwarded-Encrypted: i=1; AFNElJ9TNmAEL4MWbqyBVLIoxtTVpLFNtM4I+8nhkme/d8AynayFq8bTgN7UbmpadtI1nXXEDVf9rSsB+J+2BuE=@vger.kernel.org X-Gm-Message-State: AOJu0YwQWDxgPNpYVGy/JZ8G3qwPUTzZQ8CAR4oXN97XXKsrl2g2mFnH g9VZni2BL+2A96NIlSjkJJm4jE+hDx/7IhFLver/ZyAi8sSW3oZyEnnu1kQApVn8h4w6uzG/pV4 A2ewCvQ== X-Received: from pfgs24.prod.google.com ([2002:a05:6a00:1798:b0:7fb:ed58:5e4b]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1306:b0:823:d2c:b156 with SMTP id d2e1a72fcca58-82f0c2691e2mr19192062b3a.5.1776194073165; Tue, 14 Apr 2026 12:14:33 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Apr 2026 12:14:23 -0700 In-Reply-To: <20260414191425.2697918-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: <20260414191425.2697918-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.rc0.605.g598a273b03-goog Message-ID: <20260414191425.2697918-3-seanjc@google.com> Subject: [PATCH 2/4] 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 , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini Cc: 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, insert DS_AREA and (conditionally) MSR_PEBS_DATA_CFG into the list 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 !x86_pmu.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 Signed-off-by: Sean Christopherson Reviewed-by: Jim Mattson --- arch/x86/events/intel/core.c | 41 ++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index 002d809f82ef..20a153aa33cb 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, - }; - } - /* * Disable counters where the guest PMC is different than the host PMC * being used on behalf of the guest, as the PEBS record includes @@ -5065,6 +5056,28 @@ 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 running with host values is safe (see above). + */ + if (!guest_pebs_mask) + return arr; + + 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, + }; + } + /* * 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.rc0.605.g598a273b03-goog From nobody Sat Jun 20 14:13:05 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 0CA4831ED8B for ; Tue, 14 Apr 2026 19:14:35 +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=1776194078; cv=none; b=Xjg3iYWt/W+MB3Mz8fKhfewx8ckpqdxATj5y0CsPFr5kNjpwGMgQXqltjxpX1qtJHUlugH+jW1DC+MulPwVwPJuI1YNu6r4xXf1I4M1fVFVNnuiyBeFlymJYHaJIBRXmi1HeXjukZFZ+6tYjsZvezD/geq9V1Iel/94qApS6te4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776194078; c=relaxed/simple; bh=d9A313GV9WgaQHzZDmV8dxWqw2AWoUgVAlRQ4qRl5Uw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Z3rDWcQqUpD9+oAWvbpgB59lFxba+9p+a0V5XDeNXhzQgw1CgcaUM7rFy0QAXxKVoM3Oo3TeeaSnkUObyyFwmWJQO8sZzVNpn3zsGTI7pnF+XXJ4hzyg+ATRcgubbEHumIsbaxh5HCwse95fAERvd2yjdbI6Fwm+d5gIjUM1b60= 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=auM/Bm5h; 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="auM/Bm5h" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2b2d83e7461so77679075ad.3 for ; Tue, 14 Apr 2026 12:14:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776194075; x=1776798875; 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=EDNdHgefY9I1l6kvBzLoX3J+XQ/bw8PLks7K5tWj/aE=; b=auM/Bm5h37D+P/F1KK+5Q6amUxpUr6D8+0DN4NjvKfFtZz7yYo80rCs02pWAI3QyZU jLwmTo0zGdmewBEmr7xQAa4yFrg/ho6+QazOPa4NeG0b8HcJBpWhCToG/oWwfsefiVqe h9F+vn2BND+DFggdMOdaxbvTW8z6XEhTTdq31AbYyK4srz8/uCVIwGBM+tndehiglWZl WzcL/G827GEg9uQr2WYGg0PfV6qGIb2uxv8C2Q0eU8dAQph7OAAG8NamytkEnPWsidBX GnWYaqPVH0BvDom28KpJTlsHX3IIvzvGSU3EnomocOkSRv5Orps8xZ/ahx36tFIk1yD6 0Zvw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776194075; x=1776798875; 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=EDNdHgefY9I1l6kvBzLoX3J+XQ/bw8PLks7K5tWj/aE=; b=WEdQz+SnZraggUWKnBo3Aee+9IoPDqOkfeUu//2pcFOFrkRGIlHuyM0fFVyzKuOgTJ FSRGIOS1/xIOqV9O90HG0iRhFEgijnML78zcj6YghEHaMHZ01yIQ8/3mLS0Gzi5olcmL s//dEMB21WrwnWWacNCzWLcCkcPPwkXr74M2FqFGn6kMLHY3FCiJHVvvrs8a/4Wd+bMm GMLcCTnnPRNdPvN+3tJoz4fJ118aLb81NhrxiNOHDtU/Yc2/kONhOGn6R3vvr+EKYMqa 4a9DyGQYXtRkRHM7ZmECyzGAaJ0QgeAzbRw4O1a9kL20MtTtFCzq0ZjO6C9z367BQbox vMpg== X-Forwarded-Encrypted: i=1; AFNElJ/eWvhgw1gPPR1RdKQ1AMz7GFw+4dm/TrhDHgNx5xP+UPUFJhVVOyOtLl7Hde5HMglUk+rgWkLpiBmzMBQ=@vger.kernel.org X-Gm-Message-State: AOJu0YyEKEx+rRsrpsqTo7m6u3znBT4J9I4aaYCTMomoef5spQtcyykB 9ilALco9QeuaYQihSoIw+C805DRo7dQDb+MAscuBTrZKKd2XY1jYVwUXfHPnMFD8lNyQznHwYxl /JqHtaQ== X-Received: from plnz12.prod.google.com ([2002:a17:902:834c:b0:2b2:4cd5:e119]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:38cc:b0:2b0:60f1:de58 with SMTP id d9443c01a7336-2b2d5a95893mr194326015ad.45.1776194075229; Tue, 14 Apr 2026 12:14:35 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Apr 2026 12:14:24 -0700 In-Reply-To: <20260414191425.2697918-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: <20260414191425.2697918-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.rc0.605.g598a273b03-goog Message-ID: <20260414191425.2697918-4-seanjc@google.com> Subject: [PATCH 3/4] 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 , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini Cc: 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 Signed-off-by: Sean Christopherson Reviewed-by: Jim Mattson --- 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 20a153aa33cb..9a78731deea2 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.rc0.605.g598a273b03-goog From nobody Sat Jun 20 14:13:05 2026 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.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 DB98F314A9F for ; Tue, 14 Apr 2026 19:14:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776194079; cv=none; b=nSci+d1jnm5w+qirdP/+do8vXOHCpRhvbwTrXw1fVrZknX6uPkdXvLWJchOyHKZW2xAhp6nshZpTNRfM3nLHrf0VgcHNCekaeCTArkurW/+qQz7ZyUYsYKwzKKpuOjR4EeN9H4wmpvP+fH6sw9/iWxhHj+XReFQVQGfjNAzHz6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1776194079; c=relaxed/simple; bh=MsbAe7EnaPZ5nTEko681Nsp2L/NCmCj7xIq9wgHtl2w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RnWWCVaJSpvxdautuV+WYy5ui/yGDn647gn/ciZyngMdB6851GUioIomdxOCItFYMvaXvSt8ATbs0oqPmTNgyhS9tKnOFyyWMgSaeRHo3iWB3Sh8qh2ZmttOJ6mPYNPziez98rUz7sPhtHaIK9ST8TOwSHF1AK/ii07rvWOtBew= 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=Tq+sNEIN; arc=none smtp.client-ip=209.85.210.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="Tq+sNEIN" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82c1e1a6cfbso4211636b3a.0 for ; Tue, 14 Apr 2026 12:14:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1776194077; x=1776798877; 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=kdmfahh/EOWDmhZ848Vt8AX6Mt80XdZTFosxlYs7Rec=; b=Tq+sNEIN00kicn9c/HSdpIfL8yJumh32DCH6SV5YoUb09fL636Olubg/0Tj3bw5aHZ ykHQ4Zxwu4SRtmQ/1gPOasvjOPfGVMW9QOkZwQRVDSt0PqtJfsD+CPor6gO85mHAyhRK zh18IsXEa0KyLhDRfTuWrJA//Tr6tAlstUMilI+NMQ4B5iyW0PsAmay02wgi/GVTpSyc 4kKElHlSAgPwH7R/LztUVJhN893BtWlNgT/eAtTc4DdHIUrLXDSoogLb0mnTd7YlZ8WN 2ZcTZ92uExM5ahSjkC9SnwijYzvj4MBgkJawqJAV5SArgd+ACr3sUAahMnSY1jNzne0C SiBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776194077; x=1776798877; 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=kdmfahh/EOWDmhZ848Vt8AX6Mt80XdZTFosxlYs7Rec=; b=LxC4/E29ImiscfosuqRJ5F3ZnUQory+XcyBKS1z8bb3AX7IfagxGuxAcv5SlGSha4l V2MngSAi/QLBMkV3dfiLnF6x1DVfszoXUu15J6D4tWrUSnFMbox4Q4vcmRRgnGz+6BTa jV7nfF6pWiGe1BVzzuGhrgo1kt2gVH92uvLIiJCXJvwmcfydZSi3SmecwrBkCLKMtul1 na/96QR3Qs+u6S19RVViEikPEBy2N3eRVUeUUsSbICEB6VyM1un9JQcqN2Iu9bV6ttlF DLoFVa57VXvh2Xg1khseY5tX3m0pLrE5mUFApBRp+A+gARsfFb8y++OTCUygRE2hjU++ 4qog== X-Forwarded-Encrypted: i=1; AFNElJ+4jx1Jfxzk5MgBlZE6Xe2HCfUeA9ORU9zvtQRcMBGQJCx67o6KlM1LEWbDhcQ0XO2j6yDgg6W6RkyO8e4=@vger.kernel.org X-Gm-Message-State: AOJu0YyN7UbzbP62a5xil0z3YeE8cWutDjtyndjZ7DIfNWjdLIUX51zF fbpuRcgZWVxGWQC3atilcvA+g/bIngJsCcUvICrxjJJ6E8VRNVpjywPNTld/MjDaOTY8jY5Oobh f2QmlcQ== X-Received: from pfbg28.prod.google.com ([2002:a05:6a00:ae1c:b0:829:a18b:94dc]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d0f:b0:82c:d9d0:f482 with SMTP id d2e1a72fcca58-82f0c3b7ad9mr21219736b3a.46.1776194077106; Tue, 14 Apr 2026 12:14:37 -0700 (PDT) Reply-To: Sean Christopherson Date: Tue, 14 Apr 2026 12:14:25 -0700 In-Reply-To: <20260414191425.2697918-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: <20260414191425.2697918-1-seanjc@google.com> X-Mailer: git-send-email 2.54.0.rc0.605.g598a273b03-goog Message-ID: <20260414191425.2697918-5-seanjc@google.com> Subject: [PATCH 4/4] 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 , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini Cc: 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 Signed-off-by: Sean Christopherson --- arch/x86/events/core.c | 5 +++-- arch/x86/events/intel/core.c | 14 +++++++------- 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 | 20 +++++++++++++++++--- arch/x86/kvm/vmx/vmx.c | 11 +++++++---- arch/x86/kvm/vmx/vmx.h | 2 +- 8 files changed, 47 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 9a78731deea2..eded12764775 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 =3D pebs_mask & ~cpuc->intel_ctrl_host_mask; @@ -5052,7 +5051,7 @@ static struct perf_guest_switch_msr *intel_guest_get_= msrs(int *nr, void *data) * wrong counter(s). Similarly, disallow PEBS in the guest if the host * is using PEBS, to avoid bleeding host state into PEBS records. */ - guest_pebs_mask &=3D kvm_pmu->pebs_enable & ~kvm_pmu->host_cross_mapped_m= ask; + guest_pebs_mask &=3D guest_pebs->enable & ~guest_pebs->cross_mapped_mask; if (pebs_mask & ~cpuc->intel_ctrl_guest_mask) guest_pebs_mask =3D 0; =20 @@ -5067,14 +5066,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 kvm_pmu->ds_area, + .guest =3D guest_pebs->ds_area, }; =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 kvm_pmu->pebs_data_cfg, + .guest =3D guest_pebs->data_cfg, }; } =20 @@ -5089,7 +5088,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 fad87d3c8b2c..19d811ca6b05 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 ff5acb8b199b..5340d8bb1d92 100644 --- a/arch/x86/include/asm/perf_event.h +++ b/arch/x86/include/asm/perf_event.h @@ -769,11 +769,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..0197007593f3 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -736,11 +736,24 @@ 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 + if (!(pmu->pebs_enable & pmu->global_ctrl)) + return 0; + + /* + * 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 +765,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..e6c1c64a8c94 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7313,12 +7313,15 @@ 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; - if (pmu->pebs_enable & pmu->global_ctrl) - intel_pmu_cross_mapped_check(pmu); + 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, + .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.rc0.605.g598a273b03-goog