From: Gautam Menghani <gautam@linux.ibm.com>
To: maddy@linux.ibm.com, atrajeev@linux.ibm.com, npiggin@gmail.com,
mpe@ellerman.id.au, chleroy@kernel.org
Cc: Gautam Menghani <gautam@linux.ibm.com>,
linuxppc-dev@lists.ozlabs.org, kvm@vger.kernel.org,
linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: [PATCH v2 2/3] powerpc/perf: Use the aggregate context switch values from vcpu struct
Date: Thu, 13 Aug 2026 15:15:31 +0530 [thread overview]
Message-ID: <20260813094535.10083-3-gautam@linux.ibm.com> (raw)
In-Reply-To: <20260813094535.10083-1-gautam@linux.ibm.com>
The vpa-pmu driver reports incorrect numbers in 2 scenarios:
1. The vCPU process gets rescheduled to a different host cpu
- Incorrect numbers are observed here because the PACA is per-host cpu
resource, and the KVM vCPUs can be rescheduled to different
host cpus. This causes the vpa-pmu driver to subtract wrong values
when vCPUs are rescheduled.
2. The vCPU is not running when vpa_pmu_read() is called.
- In this case get_counter_data() returns 0, and this can result in
negative numbers getting reported.
Fix the above issues by using the aggregate values from the vcpu
structure to capture and report the difference in counter values.
Fixes: 176cda0619b6 ("powerpc/perf: Add perf interface to expose vpa counters")
Cc: <stable@vger.kernel.org>
Signed-off-by: Gautam Menghani <gautam@linux.ibm.com>
---
v1 -> v2:
1. Fix the output for "perf stat --cpu ..."
2. Add stable/fixes tags
arch/powerpc/include/asm/kvm_book3s_64.h | 3 --
arch/powerpc/kvm/book3s_hv.c | 45 ------------------------
arch/powerpc/perf/vpa-pmu.c | 24 +++++++++----
3 files changed, 18 insertions(+), 54 deletions(-)
diff --git a/arch/powerpc/include/asm/kvm_book3s_64.h b/arch/powerpc/include/asm/kvm_book3s_64.h
index b936e174eefd..f620e3126d68 100644
--- a/arch/powerpc/include/asm/kvm_book3s_64.h
+++ b/arch/powerpc/include/asm/kvm_book3s_64.h
@@ -691,9 +691,6 @@ void kvmhv_set_l2_counters_status(int cpu, bool status);
u64 kvmhv_get_l1_to_l2_cs_time(void);
u64 kvmhv_get_l2_to_l1_cs_time(void);
u64 kvmhv_get_l2_runtime_agg(void);
-u64 kvmhv_get_l1_to_l2_cs_time_vcpu(void);
-u64 kvmhv_get_l2_to_l1_cs_time_vcpu(void);
-u64 kvmhv_get_l2_runtime_agg_vcpu(void);
#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
diff --git a/arch/powerpc/kvm/book3s_hv.c b/arch/powerpc/kvm/book3s_hv.c
index 342168b8bfc8..b9285b7f1fed 100644
--- a/arch/powerpc/kvm/book3s_hv.c
+++ b/arch/powerpc/kvm/book3s_hv.c
@@ -4189,51 +4189,6 @@ u64 kvmhv_get_l2_runtime_agg(void)
}
EXPORT_SYMBOL(kvmhv_get_l2_runtime_agg);
-u64 kvmhv_get_l1_to_l2_cs_time_vcpu(void)
-{
- struct kvm_vcpu *vcpu;
- struct kvm_vcpu_arch *arch;
-
- vcpu = local_paca->kvm_hstate.kvm_vcpu;
- if (vcpu) {
- arch = &vcpu->arch;
- return arch->l1_to_l2_cs;
- } else {
- return 0;
- }
-}
-EXPORT_SYMBOL(kvmhv_get_l1_to_l2_cs_time_vcpu);
-
-u64 kvmhv_get_l2_to_l1_cs_time_vcpu(void)
-{
- struct kvm_vcpu *vcpu;
- struct kvm_vcpu_arch *arch;
-
- vcpu = local_paca->kvm_hstate.kvm_vcpu;
- if (vcpu) {
- arch = &vcpu->arch;
- return arch->l2_to_l1_cs;
- } else {
- return 0;
- }
-}
-EXPORT_SYMBOL(kvmhv_get_l2_to_l1_cs_time_vcpu);
-
-u64 kvmhv_get_l2_runtime_agg_vcpu(void)
-{
- struct kvm_vcpu *vcpu;
- struct kvm_vcpu_arch *arch;
-
- vcpu = local_paca->kvm_hstate.kvm_vcpu;
- if (vcpu) {
- arch = &vcpu->arch;
- return arch->l2_runtime_agg;
- } else {
- return 0;
- }
-}
-EXPORT_SYMBOL(kvmhv_get_l2_runtime_agg_vcpu);
-
#else
int kvmhv_get_l2_counters_status(void)
{
diff --git a/arch/powerpc/perf/vpa-pmu.c b/arch/powerpc/perf/vpa-pmu.c
index bff4cfab7b94..e79d98447c74 100644
--- a/arch/powerpc/perf/vpa-pmu.c
+++ b/arch/powerpc/perf/vpa-pmu.c
@@ -91,7 +91,7 @@ static int vpa_pmu_event_init(struct perf_event *event)
return 0;
}
-static unsigned long get_counter_data(struct perf_event *event)
+static unsigned long get_counter_data(struct kvm_vcpu *vcpu, struct perf_event *event)
{
unsigned int config = event->attr.config;
u64 data;
@@ -99,19 +99,19 @@ static unsigned long get_counter_data(struct perf_event *event)
switch (config) {
case L1_TO_L2_CS_LAT:
if (event->attach_state & PERF_ATTACH_TASK)
- data = kvmhv_get_l1_to_l2_cs_time_vcpu();
+ data = vcpu->arch.l1_to_l2_cs;
else
data = kvmhv_get_l1_to_l2_cs_time();
break;
case L2_TO_L1_CS_LAT:
if (event->attach_state & PERF_ATTACH_TASK)
- data = kvmhv_get_l2_to_l1_cs_time_vcpu();
+ data = vcpu->arch.l2_to_l1_cs;
else
data = kvmhv_get_l2_to_l1_cs_time();
break;
case L2_RUNTIME_AGG:
if (event->attach_state & PERF_ATTACH_TASK)
- data = kvmhv_get_l2_runtime_agg_vcpu();
+ data = vcpu->arch.l2_runtime_agg;
else
data = kvmhv_get_l2_runtime_agg();
break;
@@ -126,21 +126,33 @@ static unsigned long get_counter_data(struct perf_event *event)
static int vpa_pmu_add(struct perf_event *event, int flags)
{
u64 data;
+ struct kvm_vcpu *vcpu;
+ vcpu = local_paca->kvm_hstate.kvm_vcpu;
+ if (!vcpu)
+ goto out;
+
+ event->pmu_private = vcpu;
kvmhv_set_l2_counters_status(smp_processor_id(), true);
- data = get_counter_data(event);
+ data = get_counter_data(vcpu, event);
local64_set(&event->hw.prev_count, data);
+out:
return 0;
}
static void vpa_pmu_read(struct perf_event *event)
{
u64 prev_data, new_data, final_data;
+ struct kvm_vcpu *vcpu;
+
+ vcpu = (struct kvm_vcpu *) event->pmu_private;
+ if (!vcpu)
+ return;
prev_data = local64_read(&event->hw.prev_count);
- new_data = get_counter_data(event);
+ new_data = get_counter_data(vcpu, event);
final_data = new_data - prev_data;
local64_add(final_data, &event->count);
--
2.54.0
next prev parent reply other threads:[~2026-08-13 9:46 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-13 9:45 [PATCH v2 0/3] powerpc/perf: Fix vpa-pmu driver to report correct numbers Gautam Menghani
2026-08-13 9:45 ` [PATCH v2 1/3] KVM: PPC: Book3S HV: Maintain aggregate context switch values for vCPUs Gautam Menghani
2026-08-13 10:03 ` sashiko-bot
2026-08-13 9:45 ` Gautam Menghani [this message]
2026-08-13 9:58 ` [PATCH v2 2/3] powerpc/perf: Use the aggregate context switch values from vcpu struct sashiko-bot
2026-08-13 9:45 ` [PATCH v2 3/3] powerpc/perf: Update prev_count of event to get accurate values Gautam Menghani
2026-08-13 10:01 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260813094535.10083-3-gautam@linux.ibm.com \
--to=gautam@linux.ibm.com \
--cc=atrajeev@linux.ibm.com \
--cc=chleroy@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linuxppc-dev@lists.ozlabs.org \
--cc=maddy@linux.ibm.com \
--cc=mpe@ellerman.id.au \
--cc=npiggin@gmail.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.