Skip to contentSkip to conversation

powerpc/perf: Fix vpa-pmu driver to report correct numbers

PATCH In review Gautam Menghani 2 participants 7 messages Nov 6

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v3 cover powerpc/perf: Fix vpa-pmu driver to report correct numbers

The vpa-pmu driver can sometimes report incorrect context switch latency numbers:

$ perf stat -e vpa_pmu/l1_to_l2_lat/  --tid=2061  -I 1000
           time             counts unit events
     1.001022517 18,446,744,073,708,177,408      vpa_pmu/l1_to_l2_lat/
     2.002120674            345,092      vpa_pmu/l1_to_l2_lat/
     3.003171582            406,342      vpa_pmu/l1_to_l2_lat/
     4.004234838            340,836      vpa_pmu/l1_to_l2_lat/

These invalid numbers are observed because of 2 issues:

1. The PACA never points to the correct vCPU running on a host vCPU when vpa_pmu_add() is called.

2. When get_counter_data() reads the PACA counter values, it does not update the event->hw.prev_count.

Changes in V3:
1. Drop the usage of the PACA's vCPU pointer.
2. Introduce a new patch to fix an issue seen when a tracepoint and vpa-pmu driver concurrently enable/disable access to latency counters in VPA.

Gautam Menghani (3):
Revert "powerpc/perf: Add per-task/process monitoring to vpa_pmu
driver"
powerpc/perf: Update prev_count of event to get accurate values
KVM: PPC: Book3s HV: Ensure reliable usage of latency counters

 arch/powerpc/include/asm/kvm_book3s_64.h |  3 --
 arch/powerpc/kvm/book3s_hv.c             | 54 +++---------------------
 arch/powerpc/perf/vpa-pmu.c              | 16 ++-----
 3 files changed, 11 insertions(+), 62 deletions(-)
Signature · 1 line
2.56.0
#1 lore raw

PATCH v3 1/3 Revert "powerpc/perf: Add per-task/process monitoring to vpa_pmu driver"

This reverts commit f26f9933e3e31b2117b804b6b8932388db88a131.

The local_paca holds a pointer to the vCPU currently running on the given host CPU. When the vpa-pmu driver tries to get the vCPU pointer from local_paca, it either receives NULL or the pointer to the vCPU which was running on that host CPU previously. This is because the vCPU pointer in PACA is updated by kvmhv_run_single_vcpu(), which runs after vpa_pmu_add(). Also, on vCPU exit, the vCPU pointer in PACA is never cleared. This results in incorrect numbers being reported by the vpa-pmu driver.

Fix the problem by avoiding the use of vCPU pointer in PACA altogether. When a vCPU is scheduled in and out on a host vCPU, vpa_pmu_add() and vpa_pmu_del() can already calculate the correct latency values in event->count. Drop the usage of the vCPU pointer in PACA and just rely on the VPA counter values.

Fixes f26f9933e3e3 ("powerpc/perf: Add per-task/process monitoring to vpa_pmu driver")
---
 arch/powerpc/include/asm/kvm_book3s_64.h |  3 --
 arch/powerpc/kvm/book3s_hv.c             | 45 ------------------------
 arch/powerpc/perf/vpa-pmu.c              | 15 ++------
 3 files changed, 3 insertions(+), 60 deletions(-)
arch/powerpc/include/asm/kvm_book3s_64.h +0 -3
index b936e174eefd..f620e3126d68 100644
@@ -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 */
 
arch/powerpc/kvm/book3s_hv.c +0 -45

51 lines not on this page · raw message

arch/powerpc/perf/vpa-pmu.c +3 -12
index bff4cfab7b94..01d9196fcb0f 100644
@@ -98,22 +98,13 @@ 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();
-		else
-			data = kvmhv_get_l1_to_l2_cs_time();
+		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();
-		else
-			data = kvmhv_get_l2_to_l1_cs_time();
+		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();
-		else
-			data = kvmhv_get_l2_runtime_agg();
+		data = kvmhv_get_l2_runtime_agg();
 		break;
 	default:
 		data = 0;
Signature · 1 line
2.56.0
#2 lore raw

PATCH v3 2/3 powerpc/perf: Update prev_count of event to get accurate values

In reply to Gautam Menghani

When vpa_pmu_read() is called twice while a vCPU is running on the same host cpu, the "event->hw.prev_count" variable returns the same old value on the 2nd read which results in a larger value getting added to event->count.

Fix this by updating the event->hw.prev_count every time vpa_pmu_read() is called.

Fixes 176cda0619b6 ("powerpc/perf: Add perf interface to expose vpa counters")

---
v2 -> v3:
1. Use local64_xchg for atomic update.

v1 -> v2:
1. Add stable/fixes tags.

 arch/powerpc/perf/vpa-pmu.c | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/perf/vpa-pmu.c +1 -0
index 01d9196fcb0f..3296877eb317 100644
@@ -132,6 +132,7 @@ static void vpa_pmu_read(struct perf_event *event)
 
 	prev_data = local64_read(&event->hw.prev_count);
 	new_data = get_counter_data(event);
+	prev_data = local64_xchg(&event->hw.prev_count, new_data);
 	final_data = new_data - prev_data;
 
 	local64_add(final_data, &event->count);
Signature · 1 line
2.56.0
#3 lore raw

Re: [PATCH v3 2/3] powerpc/perf: Update prev_count of event to get accurate values

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] powerpc/perf: dead store to prev_data from local64_read() in vpa_pmu_read() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] powerpc/perf: dead store to prev_data from local64_read() in vpa_pmu_read()
--

commit e6b6a01ca205d4ad136547505bcf1dcf984b82ba
Author: Gautam Menghani <[email protected]>

powerpc/perf: Update prev_count of event to get accurate values

This commit updates event->hw.prev_count in vpa_pmu_read() using local64_xchg() so that consecutive counter reads accumulate only the delta since the last read.

arch/powerpc/perf/vpa-pmu.c static void vpa_pmu_read(struct perf_event *event) · line 135

Quoted 9 lines
arch/powerpc/perf/vpa-pmu.c +1 -0
index 01d9196fcb0f6..3296877eb317b 100644
@@ -132,6 +132,7 @@ static void vpa_pmu_read(struct perf_event *event)
 
 	prev_data = local64_read(&event->hw.prev_count);
 	new_data = get_counter_data(event);
+	prev_data = local64_xchg(&event->hw.prev_count, new_data);

[Severity: Low]
Can the earlier local64_read(&event->hw.prev_count) call be dropped now that local64_xchg() unconditionally overwrites prev_data before it is used? Because local64_read() expands to a volatile READ_ONCE() load, the compiler cannot elide the redundant read on every vpa_pmu_read() call.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#4 lore raw

PATCH v3 3/3 KVM: PPC: Book3s HV: Ensure reliable usage of latency counters

In reply to Gautam Menghani

The kvmppc_vcpu_stats tracepoint and the vpa-pmu driver can both enable and disable the latency counters in the VPA. When both are used together, they can step on each other and produce unreliable numbers. Since the VPA element for enabling/disabling counters (l2_counters_enable) continues counting with any value > 0, introduce a spinlock to ensure that the tracepoint and vpa-pmu driver can work correctly together.

Fixes e1f288d2f9c6 ("KVM: PPC: Book3S HV nestedv2: Add support for reading VPA counters for pseries guests")
---
 arch/powerpc/kvm/book3s_hv.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
arch/powerpc/kvm/book3s_hv.c +7 -2
index 5fa6f93548ad..e1bde3c6b0c4 100644
@@ -4142,14 +4142,19 @@ int kvmhv_get_l2_counters_status(void)
 		get_lppaca()->l2_counters_enable;
 }
 
+static DEFINE_RAW_SPINLOCK(l2_counters_lock);
 void kvmhv_set_l2_counters_status(int cpu, bool status)
 {
+	unsigned long flags;
+
 	if (!firmware_has_feature(FW_FEATURE_LPAR))
 		return;
+	raw_spin_lock_irqsave(&l2_counters_lock, flags);
 	if (status)
-		lppaca_of(cpu).l2_counters_enable = 1;
+		lppaca_of(cpu).l2_counters_enable++;
 	else
-		lppaca_of(cpu).l2_counters_enable = 0;
+		lppaca_of(cpu).l2_counters_enable--;
+	raw_spin_unlock_irqrestore(&l2_counters_lock, flags);
 }
 EXPORT_SYMBOL(kvmhv_set_l2_counters_status);
 
Signature · 1 line
2.56.0
#5 lore raw

Re: [PATCH v3 3/3] KVM: PPC: Book3s HV: Ensure reliable usage of latency counters

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] KVM: PPC: Book3S HV: u8 overflow in kvmhv_set_l2_counters_status() - [Medium] KVM: PPC: Book3S HV…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] KVM: PPC: Book3S HV: u8 overflow in kvmhv_set_l2_counters_status()
- [Medium] KVM: PPC: Book3S HV: l2_counters_enable underflow across CPU hotplug
--

commit c5caaeddc35ba638841a3b81b1767bed8f97a3e3
Author: Gautam Menghani <[email protected]>

KVM: PPC: Book3s HV: Ensure reliable usage of latency counters

This commit replaces the boolean assignment of l2_counters_enable in kvmhv_set_l2_counters_status() with spinlock-protected increments and decrements so the kvmppc_vcpu_stats tracepoint and vpa-pmu driver can enable VPA latency counters concurrently.

Quoted 9 lines
arch/powerpc/kvm/book3s_hv.c +1 -0
index 5fa6f93548add..e1bde3c6b0c4e 100644
@@ -4142,14 +4142,19 @@ int kvmhv_get_l2_counters_status(void)
 		get_lppaca()->l2_counters_enable;
 }
 
+static DEFINE_RAW_SPINLOCK(l2_counters_lock);

arch/powerpc/kvm/book3s_hv.c int kvmhv_get_l2_counters_status(void) · line 4154

Quoted 10 lines
arch/powerpc/kvm/book3s_hv.c +4 -1
 void kvmhv_set_l2_counters_status(int cpu, bool status)
 {
+	unsigned long flags;
+
 	if (!firmware_has_feature(FW_FEATURE_LPAR))
 		return;
+	raw_spin_lock_irqsave(&l2_counters_lock, flags);
 	if (status)
-		lppaca_of(cpu).l2_counters_enable = 1;
+		lppaca_of(cpu).l2_counters_enable++;

[Severity: Medium]
Can lppaca_of(cpu).l2_counters_enable overflow here?

In struct lppaca, l2_counters_enable is a u8, while vpa_pmu_event_init() places no upper bound on how many perf_sw_context events can be opened and scheduled on the same CPU via vpa_pmu_add(). When 256 consumers enable the counters on one CPU, l2_counters_enable wraps to 0, causing the hypervisor to stop accumulating VPA latency counters and kvmhv_get_l2_counters_status() to return 0 while active consumers are running.

arch/powerpc/kvm/book3s_hv.c int kvmhv_get_l2_counters_status(void) · line 4159

Quoted 6 lines
arch/powerpc/kvm/book3s_hv.c +2 -1
 	else
-		lppaca_of(cpu).l2_counters_enable = 0;
+		lppaca_of(cpu).l2_counters_enable--;
+	raw_spin_unlock_irqrestore(&l2_counters_lock, flags);
 }
 EXPORT_SYMBOL(kvmhv_set_l2_counters_status);

[Severity: Medium]
Could lppaca_of(cpu).l2_counters_enable underflow across CPU hotplug?

kvmhv_counters_tracepoint_regfunc() and kvmhv_counters_tracepoint_unregfunc() both iterate over for_each_present_cpu(cpu) without CPU hotplug callbacks:

arch/powerpc/kvm/book3s_hv.c:kvmhv_counters_tracepoint_regfunc() {
	for_each_present_cpu(cpu) {
		kvmhv_set_l2_counters_status(cpu, true);
	}
	return 0;
}

If a CPU is hot-added via pseries_add_processor() while the kvmppc_vcpu_stats tracepoint is enabled, disabling the tracepoint decrements l2_counters_enable on that CPU from 0 to 255 (or drops an active vpa_pmu event's reference), leaving the VPA counters enabled after all users exit. Conversely, hot-removing a CPU while the tracepoint is active skips the decrement on that CPU when the tracepoint is later disabled.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#6 lore raw
In reply to Gautam Menghani

Forgot to add linuxppc-dev

Quoted 39 lines · Gautam Menghani

The vpa-pmu driver can sometimes report incorrect context switch latency numbers:

$ perf stat -e vpa_pmu/l1_to_l2_lat/  --tid=2061  -I 1000
           time             counts unit events
     1.001022517 18,446,744,073,708,177,408      vpa_pmu/l1_to_l2_lat/
     2.002120674            345,092      vpa_pmu/l1_to_l2_lat/
     3.003171582            406,342      vpa_pmu/l1_to_l2_lat/
     4.004234838            340,836      vpa_pmu/l1_to_l2_lat/

These invalid numbers are observed because of 2 issues:

1. The PACA never points to the correct vCPU running on a host vCPU when vpa_pmu_add() is called.

2. When get_counter_data() reads the PACA counter values, it does not update the event->hw.prev_count.

Changes in V3:
1. Drop the usage of the PACA's vCPU pointer.
2. Introduce a new patch to fix an issue seen when a tracepoint and vpa-pmu driver concurrently enable/disable access to latency counters in VPA.

Gautam Menghani (3):
Revert "powerpc/perf: Add per-task/process monitoring to vpa_pmu
driver"
powerpc/perf: Update prev_count of event to get accurate values
KVM: PPC: Book3s HV: Ensure reliable usage of latency counters

 arch/powerpc/include/asm/kvm_book3s_64.h |  3 --
 arch/powerpc/kvm/book3s_hv.c             | 54 +++---------------------
 arch/powerpc/perf/vpa-pmu.c              | 16 ++-----
 3 files changed, 11 insertions(+), 62 deletions(-)
Signature · 1 line
2.56.0