Skip to content

Commit 097e431

Browse files
Like XuPeter Zijlstra
authored andcommitted
perf/x86: Add constraint to create guest LBR event without hw counter
The hypervisor may request the perf subsystem to schedule a time window to directly access the LBR records msrs for its own use. Normally, it would create a guest LBR event with callstack mode enabled, which is scheduled along with other ordinary LBR events on the host but in an exclusive way. To avoid wasting a counter for the guest LBR event, the perf tracks its hw->idx via INTEL_PMC_IDX_FIXED_VLBR and assigns it with a fake VLBR counter with the help of new vlbr_constraint. As with the BTS event, there is actually no hardware counter assigned for the guest LBR event. Signed-off-by: Like Xu <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent b2d6504 commit 097e431

File tree

5 files changed

+45
-1
lines changed

5 files changed

+45
-1
lines changed

arch/x86/events/core.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ static inline void x86_assign_hw_event(struct perf_event *event,
11041104

11051105
switch (hwc->idx) {
11061106
case INTEL_PMC_IDX_FIXED_BTS:
1107+
case INTEL_PMC_IDX_FIXED_VLBR:
11071108
hwc->config_base = 0;
11081109
hwc->event_base = 0;
11091110
break;

arch/x86/events/intel/core.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,20 @@ intel_bts_constraints(struct perf_event *event)
26212621
return NULL;
26222622
}
26232623

2624+
/*
2625+
* Note: matches a fake event, like Fixed2.
2626+
*/
2627+
static struct event_constraint *
2628+
intel_vlbr_constraints(struct perf_event *event)
2629+
{
2630+
struct event_constraint *c = &vlbr_constraint;
2631+
2632+
if (unlikely(constraint_match(c, event->hw.config)))
2633+
return c;
2634+
2635+
return NULL;
2636+
}
2637+
26242638
static int intel_alt_er(int idx, u64 config)
26252639
{
26262640
int alt_idx = idx;
@@ -2811,6 +2825,10 @@ __intel_get_event_constraints(struct cpu_hw_events *cpuc, int idx,
28112825
{
28122826
struct event_constraint *c;
28132827

2828+
c = intel_vlbr_constraints(event);
2829+
if (c)
2830+
return c;
2831+
28142832
c = intel_bts_constraints(event);
28152833
if (c)
28162834
return c;

arch/x86/events/intel/lbr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,3 +1363,7 @@ int x86_perf_get_lbr(struct x86_pmu_lbr *lbr)
13631363
return 0;
13641364
}
13651365
EXPORT_SYMBOL_GPL(x86_perf_get_lbr);
1366+
1367+
struct event_constraint vlbr_constraint =
1368+
FIXED_EVENT_CONSTRAINT(INTEL_FIXED_VLBR_EVENT,
1369+
(INTEL_PMC_IDX_FIXED_VLBR - INTEL_PMC_IDX_FIXED));

arch/x86/events/perf_event.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,7 @@ void release_ds_buffers(void);
990990
void reserve_ds_buffers(void);
991991

992992
extern struct event_constraint bts_constraint;
993+
extern struct event_constraint vlbr_constraint;
993994

994995
void intel_pmu_enable_bts(u64 config);
995996

arch/x86/include/asm/perf_event.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,29 @@ struct x86_pmu_capability {
192192
#define GLOBAL_STATUS_UNC_OVF BIT_ULL(61)
193193
#define GLOBAL_STATUS_ASIF BIT_ULL(60)
194194
#define GLOBAL_STATUS_COUNTERS_FROZEN BIT_ULL(59)
195-
#define GLOBAL_STATUS_LBRS_FROZEN BIT_ULL(58)
195+
#define GLOBAL_STATUS_LBRS_FROZEN_BIT 58
196+
#define GLOBAL_STATUS_LBRS_FROZEN BIT_ULL(GLOBAL_STATUS_LBRS_FROZEN_BIT)
196197
#define GLOBAL_STATUS_TRACE_TOPAPMI BIT_ULL(55)
197198

199+
/*
200+
* We model guest LBR event tracing as another fixed-mode PMC like BTS.
201+
*
202+
* We choose bit 58 because it's used to indicate LBR stack frozen state
203+
* for architectural perfmon v4, also we unconditionally mask that bit in
204+
* the handle_pmi_common(), so it'll never be set in the overflow handling.
205+
*
206+
* With this fake counter assigned, the guest LBR event user (such as KVM),
207+
* can program the LBR registers on its own, and we don't actually do anything
208+
* with then in the host context.
209+
*/
210+
#define INTEL_PMC_IDX_FIXED_VLBR (GLOBAL_STATUS_LBRS_FROZEN_BIT)
211+
212+
/*
213+
* Pseudo-encoding the guest LBR event as event=0x00,umask=0x1b,
214+
* since it would claim bit 58 which is effectively Fixed26.
215+
*/
216+
#define INTEL_FIXED_VLBR_EVENT 0x1b00
217+
198218
/*
199219
* Adaptive PEBS v4
200220
*/

0 commit comments

Comments
 (0)