Skip to content

Commit f44d93e

Browse files
committed
powerpc/perf: Add perf interface to expose vpa counters
JIRA: https://issues.redhat.com/browse/RHEL-77935 upstream ======== commit 176cda0 Author: Kajol Jain <kjain@linux.ibm.com> Date: Mon Nov 18 17:11:11 2024 +0530 description =========== To support performance measurement for KVM on PowerVM(KoP) feature, PowerVM hypervisor has added couple of new software counters in Virtual Process Area(VPA) of the partition. Commit e1f288d ("KVM: PPC: Book3S HV nestedv2: Add support for reading VPA counters for pseries guests") have updated the paca fields with corresponding changes. Proposed perf interface is to expose these new software counters for monitoring of context switch latencies and runtime aggregate. Perf interface driver is called "vpa_pmu" and it has dependency on KVM and perf, hence added new config called "VPA_PMU" which depends on "CONFIG_KVM_BOOK3S_64_HV" and "CONFIG_HV_PERF_CTRS". Since, kvm and kvm_host are currently compiled as built-in modules, this perf interface takes the same path and registered as a module. vpa_pmu perf interface needs access to some of the kvm functions and structures like kvmhv_get_l2_counters_status(), hence kvm_book3s_64.h and kvm_ppc.h are included. Below are the events added to monitor KoP: vpa_pmu/l1_to_l2_lat/ vpa_pmu/l2_to_l1_lat/ vpa_pmu/l2_runtime_agg/ and vpa_pmu driver supports only per-cpu monitoring with this patch. Example usage: [command]# perf stat -e vpa_pmu/l1_to_l2_lat/ -a -I 1000 1.001017682 727,200 vpa_pmu/l1_to_l2_lat/ 2.003540491 1,118,824 vpa_pmu/l1_to_l2_lat/ 3.005699458 1,919,726 vpa_pmu/l1_to_l2_lat/ 4.007827011 2,364,630 vpa_pmu/l1_to_l2_lat/ Signed-off-by: Kajol Jain <kjain@linux.ibm.com> Co-developed-by: Madhavan Srinivasan <maddy@linux.ibm.com> Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://patch.msgid.link/20241118114114.208964-1-kjain@linux.ibm.com Signed-off-by: Michael Petlan <mpetlan@redhat.com>
1 parent 9f8d780 commit f44d93e

File tree

5 files changed

+232
-0
lines changed

5 files changed

+232
-0
lines changed

arch/powerpc/include/asm/kvm_book3s_64.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,9 @@ int kmvhv_counters_tracepoint_regfunc(void);
688688
void kmvhv_counters_tracepoint_unregfunc(void);
689689
int kvmhv_get_l2_counters_status(void);
690690
void kvmhv_set_l2_counters_status(int cpu, bool status);
691+
u64 kvmhv_get_l1_to_l2_cs_time(void);
692+
u64 kvmhv_get_l2_to_l1_cs_time(void);
693+
u64 kvmhv_get_l2_runtime_agg(void);
691694

692695
#endif /* CONFIG_KVM_BOOK3S_HV_POSSIBLE */
693696

arch/powerpc/kvm/book3s_hv.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4153,6 +4153,7 @@ void kvmhv_set_l2_counters_status(int cpu, bool status)
41534153
else
41544154
lppaca_of(cpu).l2_counters_enable = 0;
41554155
}
4156+
EXPORT_SYMBOL(kvmhv_set_l2_counters_status);
41564157

41574158
int kmvhv_counters_tracepoint_regfunc(void)
41584159
{
@@ -4192,6 +4193,24 @@ static void do_trace_nested_cs_time(struct kvm_vcpu *vcpu)
41924193
*l2_runtime_agg_ptr = l2_runtime_ns;
41934194
}
41944195

4196+
u64 kvmhv_get_l1_to_l2_cs_time(void)
4197+
{
4198+
return tb_to_ns(be64_to_cpu(get_lppaca()->l1_to_l2_cs_tb));
4199+
}
4200+
EXPORT_SYMBOL(kvmhv_get_l1_to_l2_cs_time);
4201+
4202+
u64 kvmhv_get_l2_to_l1_cs_time(void)
4203+
{
4204+
return tb_to_ns(be64_to_cpu(get_lppaca()->l2_to_l1_cs_tb));
4205+
}
4206+
EXPORT_SYMBOL(kvmhv_get_l2_to_l1_cs_time);
4207+
4208+
u64 kvmhv_get_l2_runtime_agg(void)
4209+
{
4210+
return tb_to_ns(be64_to_cpu(get_lppaca()->l2_runtime_tb));
4211+
}
4212+
EXPORT_SYMBOL(kvmhv_get_l2_runtime_agg);
4213+
41954214
#else
41964215
int kvmhv_get_l2_counters_status(void)
41974216
{

arch/powerpc/perf/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ obj-$(CONFIG_FSL_EMB_PERF_EVENT_E500) += e500-pmu.o e6500-pmu.o
1616

1717
obj-$(CONFIG_HV_PERF_CTRS) += hv-24x7.o hv-gpci.o hv-common.o
1818

19+
obj-$(CONFIG_VPA_PMU) += vpa-pmu.o
20+
1921
obj-$(CONFIG_PPC_8xx) += 8xx-pmu.o
2022

2123
obj-$(CONFIG_PPC64) += $(obj64-y)

arch/powerpc/perf/vpa-pmu.c

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Performance monitoring support for Virtual Processor Area(VPA) based counters
4+
*
5+
* Copyright (C) 2024 IBM Corporation
6+
*/
7+
#define pr_fmt(fmt) "vpa_pmu: " fmt
8+
9+
#include <linux/module.h>
10+
#include <linux/perf_event.h>
11+
#include <asm/kvm_ppc.h>
12+
#include <asm/kvm_book3s_64.h>
13+
14+
#define MODULE_VERS "1.0"
15+
#define MODULE_NAME "pseries_vpa_pmu"
16+
17+
#define EVENT(_name, _code) enum{_name = _code}
18+
19+
#define VPA_PMU_EVENT_VAR(_id) event_attr_##_id
20+
#define VPA_PMU_EVENT_PTR(_id) (&event_attr_##_id.attr.attr)
21+
22+
static ssize_t vpa_pmu_events_sysfs_show(struct device *dev,
23+
struct device_attribute *attr, char *page)
24+
{
25+
struct perf_pmu_events_attr *pmu_attr;
26+
27+
pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
28+
29+
return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
30+
}
31+
32+
#define VPA_PMU_EVENT_ATTR(_name, _id) \
33+
PMU_EVENT_ATTR(_name, VPA_PMU_EVENT_VAR(_id), _id, \
34+
vpa_pmu_events_sysfs_show)
35+
36+
EVENT(L1_TO_L2_CS_LAT, 0x1);
37+
EVENT(L2_TO_L1_CS_LAT, 0x2);
38+
EVENT(L2_RUNTIME_AGG, 0x3);
39+
40+
VPA_PMU_EVENT_ATTR(l1_to_l2_lat, L1_TO_L2_CS_LAT);
41+
VPA_PMU_EVENT_ATTR(l2_to_l1_lat, L2_TO_L1_CS_LAT);
42+
VPA_PMU_EVENT_ATTR(l2_runtime_agg, L2_RUNTIME_AGG);
43+
44+
static struct attribute *vpa_pmu_events_attr[] = {
45+
VPA_PMU_EVENT_PTR(L1_TO_L2_CS_LAT),
46+
VPA_PMU_EVENT_PTR(L2_TO_L1_CS_LAT),
47+
VPA_PMU_EVENT_PTR(L2_RUNTIME_AGG),
48+
NULL
49+
};
50+
51+
static const struct attribute_group vpa_pmu_events_group = {
52+
.name = "events",
53+
.attrs = vpa_pmu_events_attr,
54+
};
55+
56+
PMU_FORMAT_ATTR(event, "config:0-31");
57+
static struct attribute *vpa_pmu_format_attr[] = {
58+
&format_attr_event.attr,
59+
NULL,
60+
};
61+
62+
static struct attribute_group vpa_pmu_format_group = {
63+
.name = "format",
64+
.attrs = vpa_pmu_format_attr,
65+
};
66+
67+
static const struct attribute_group *vpa_pmu_attr_groups[] = {
68+
&vpa_pmu_events_group,
69+
&vpa_pmu_format_group,
70+
NULL
71+
};
72+
73+
static int vpa_pmu_event_init(struct perf_event *event)
74+
{
75+
if (event->attr.type != event->pmu->type)
76+
return -ENOENT;
77+
78+
/* it does not support event sampling mode */
79+
if (is_sampling_event(event))
80+
return -EOPNOTSUPP;
81+
82+
/* no branch sampling */
83+
if (has_branch_stack(event))
84+
return -EOPNOTSUPP;
85+
86+
/* Invalid event code */
87+
if ((event->attr.config <= 0) || (event->attr.config > 3))
88+
return -EINVAL;
89+
90+
return 0;
91+
}
92+
93+
static unsigned long get_counter_data(struct perf_event *event)
94+
{
95+
unsigned int config = event->attr.config;
96+
u64 data;
97+
98+
switch (config) {
99+
case L1_TO_L2_CS_LAT:
100+
data = kvmhv_get_l1_to_l2_cs_time();
101+
break;
102+
case L2_TO_L1_CS_LAT:
103+
data = kvmhv_get_l2_to_l1_cs_time();
104+
break;
105+
case L2_RUNTIME_AGG:
106+
data = kvmhv_get_l2_runtime_agg();
107+
break;
108+
default:
109+
data = 0;
110+
break;
111+
}
112+
113+
return data;
114+
}
115+
116+
static int vpa_pmu_add(struct perf_event *event, int flags)
117+
{
118+
u64 data;
119+
120+
kvmhv_set_l2_counters_status(smp_processor_id(), true);
121+
122+
data = get_counter_data(event);
123+
local64_set(&event->hw.prev_count, data);
124+
125+
return 0;
126+
}
127+
128+
static void vpa_pmu_read(struct perf_event *event)
129+
{
130+
u64 prev_data, new_data, final_data;
131+
132+
prev_data = local64_read(&event->hw.prev_count);
133+
new_data = get_counter_data(event);
134+
final_data = new_data - prev_data;
135+
136+
local64_add(final_data, &event->count);
137+
}
138+
139+
static void vpa_pmu_del(struct perf_event *event, int flags)
140+
{
141+
vpa_pmu_read(event);
142+
143+
/*
144+
* Disable vpa counter accumulation
145+
*/
146+
kvmhv_set_l2_counters_status(smp_processor_id(), false);
147+
}
148+
149+
static struct pmu vpa_pmu = {
150+
.task_ctx_nr = perf_sw_context,
151+
.name = "vpa_pmu",
152+
.event_init = vpa_pmu_event_init,
153+
.add = vpa_pmu_add,
154+
.del = vpa_pmu_del,
155+
.read = vpa_pmu_read,
156+
.attr_groups = vpa_pmu_attr_groups,
157+
.capabilities = PERF_PMU_CAP_NO_EXCLUDE | PERF_PMU_CAP_NO_INTERRUPT,
158+
};
159+
160+
static int __init pseries_vpa_pmu_init(void)
161+
{
162+
/*
163+
* List of current Linux on Power platforms and
164+
* this driver is supported only in PowerVM LPAR
165+
* (L1) platform.
166+
*
167+
* Enabled Linux on Power Platforms
168+
* ----------------------------------------
169+
* [X] PowerVM LPAR (L1)
170+
* [ ] KVM Guest On PowerVM KoP(L2)
171+
* [ ] Baremetal(PowerNV)
172+
* [ ] KVM Guest On PowerNV
173+
*/
174+
if (!firmware_has_feature(FW_FEATURE_LPAR) || is_kvm_guest())
175+
return -ENODEV;
176+
177+
perf_pmu_register(&vpa_pmu, vpa_pmu.name, -1);
178+
pr_info("Virtual Processor Area PMU registered.\n");
179+
180+
return 0;
181+
}
182+
183+
static void __exit pseries_vpa_pmu_cleanup(void)
184+
{
185+
perf_pmu_unregister(&vpa_pmu);
186+
pr_info("Virtual Processor Area PMU unregistered.\n");
187+
}
188+
189+
module_init(pseries_vpa_pmu_init);
190+
module_exit(pseries_vpa_pmu_cleanup);
191+
MODULE_DESCRIPTION("Perf Driver for pSeries VPA pmu counter");
192+
MODULE_AUTHOR("Kajol Jain <kjain@linux.ibm.com>");
193+
MODULE_AUTHOR("Madhavan Srinivasan <maddy@linux.ibm.com>");
194+
MODULE_LICENSE("GPL");

arch/powerpc/platforms/pseries/Kconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ config HV_PERF_CTRS
141141

142142
If unsure, select Y.
143143

144+
config VPA_PMU
145+
tristate "VPA PMU events"
146+
depends on KVM_BOOK3S_64_HV && HV_PERF_CTRS
147+
help
148+
Enable access to the VPA PMU counters via perf. This enables
149+
code that support measurement for KVM on PowerVM(KoP) feature.
150+
PAPR hypervisor has introduced three new counters in the VPA area
151+
of LPAR CPUs for KVM L2 guest observability. Two for context switches
152+
from host to guest and vice versa, and one counter for getting
153+
the total time spent inside the KVM guest. This config enables code
154+
that access these software counters via perf.
155+
156+
If unsure, Select N.
157+
144158
config IBMVIO
145159
depends on PPC_PSERIES
146160
bool

0 commit comments

Comments
 (0)