Skip to content

Commit 84d751a

Browse files
Fuad TabbaMarc Zyngier
authored andcommitted
KVM: arm64: Pass pmu events to hyp via vcpu
Instead of the host accessing hyp data directly, pass the pmu events of the current cpu to hyp via the vcpu. This adds 64 bits (in two fields) to the vcpu that need to be synced before every vcpu run in nvhe and protected modes. However, it isolates the hypervisor from the host, which allows us to use pmu in protected mode in a subsequent patch. No visible side effects in behavior intended. Signed-off-by: Fuad Tabba <tabba@google.com> Reviewed-by: Oliver Upton <oupton@google.com> Signed-off-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20220510095710.148178-4-tabba@google.com
1 parent e987a4c commit 84d751a

5 files changed

Lines changed: 32 additions & 28 deletions

File tree

arch/arm64/include/asm/kvm_host.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,8 @@ struct kvm_cpu_context {
254254
struct kvm_vcpu *__hyp_running_vcpu;
255255
};
256256

257-
struct kvm_pmu_events {
258-
u32 events_host;
259-
u32 events_guest;
260-
};
261-
262257
struct kvm_host_data {
263258
struct kvm_cpu_context host_ctxt;
264-
struct kvm_pmu_events pmu_events;
265259
};
266260

267261
struct kvm_host_psci_config {
@@ -796,6 +790,7 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);
796790
void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr);
797791
void kvm_clr_pmu_events(u32 clr);
798792

793+
struct kvm_pmu_events *kvm_get_pmu_events(void);
799794
void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu);
800795
void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
801796
#else

arch/arm64/kvm/arm.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,19 @@ static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)
751751
return ret;
752752
}
753753

754+
/*
755+
* Updates the vcpu's view of the pmu events for this cpu.
756+
* Must be called before every vcpu run after disabling interrupts, to ensure
757+
* that an interrupt cannot fire and update the structure.
758+
*/
759+
static void kvm_pmu_update_vcpu_events(struct kvm_vcpu *vcpu)
760+
{
761+
if (has_vhe() || !kvm_vcpu_has_pmu(vcpu))
762+
return;
763+
764+
vcpu->arch.pmu.events = *kvm_get_pmu_events();
765+
}
766+
754767
/**
755768
* kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code
756769
* @vcpu: The VCPU pointer
@@ -815,6 +828,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
815828

816829
kvm_vgic_flush_hwstate(vcpu);
817830

831+
kvm_pmu_update_vcpu_events(vcpu);
832+
818833
/*
819834
* Ensure we set mode to IN_GUEST_MODE after we disable
820835
* interrupts and before the final VCPU requests check.

arch/arm64/kvm/hyp/nvhe/switch.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,9 @@ static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)
123123
/**
124124
* Disable host events, enable guest events
125125
*/
126-
static bool __pmu_switch_to_guest(struct kvm_cpu_context *host_ctxt)
126+
static bool __pmu_switch_to_guest(struct kvm_vcpu *vcpu)
127127
{
128-
struct kvm_host_data *host;
129-
struct kvm_pmu_events *pmu;
130-
131-
host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
132-
pmu = &host->pmu_events;
128+
struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
133129

134130
if (pmu->events_host)
135131
write_sysreg(pmu->events_host, pmcntenclr_el0);
@@ -143,13 +139,9 @@ static bool __pmu_switch_to_guest(struct kvm_cpu_context *host_ctxt)
143139
/**
144140
* Disable guest events, enable host events
145141
*/
146-
static void __pmu_switch_to_host(struct kvm_cpu_context *host_ctxt)
142+
static void __pmu_switch_to_host(struct kvm_vcpu *vcpu)
147143
{
148-
struct kvm_host_data *host;
149-
struct kvm_pmu_events *pmu;
150-
151-
host = container_of(host_ctxt, struct kvm_host_data, host_ctxt);
152-
pmu = &host->pmu_events;
144+
struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;
153145

154146
if (pmu->events_guest)
155147
write_sysreg(pmu->events_guest, pmcntenclr_el0);
@@ -274,7 +266,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
274266
host_ctxt->__hyp_running_vcpu = vcpu;
275267
guest_ctxt = &vcpu->arch.ctxt;
276268

277-
pmu_switch_needed = __pmu_switch_to_guest(host_ctxt);
269+
pmu_switch_needed = __pmu_switch_to_guest(vcpu);
278270

279271
__sysreg_save_state_nvhe(host_ctxt);
280272
/*
@@ -336,7 +328,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)
336328
__debug_restore_host_buffers_nvhe(vcpu);
337329

338330
if (pmu_switch_needed)
339-
__pmu_switch_to_host(host_ctxt);
331+
__pmu_switch_to_host(vcpu);
340332

341333
/* Returning to host will clear PSR.I, remask PMR if needed */
342334
if (system_uses_irq_prio_masking())

arch/arm64/kvm/pmu.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*/
66
#include <linux/kvm_host.h>
77
#include <linux/perf_event.h>
8-
#include <asm/kvm_hyp.h>
8+
9+
static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
910

1011
/*
1112
* Given the perf event attributes and system type, determine
@@ -25,14 +26,9 @@ static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)
2526
return (attr->exclude_host != attr->exclude_guest);
2627
}
2728

28-
static struct kvm_pmu_events *kvm_get_pmu_events(void)
29+
struct kvm_pmu_events *kvm_get_pmu_events(void)
2930
{
30-
struct kvm_host_data *ctx = this_cpu_ptr_hyp_sym(kvm_host_data);
31-
32-
if (!ctx)
33-
return NULL;
34-
35-
return &ctx->pmu_events;
31+
return this_cpu_ptr(&kvm_pmu_events);
3632
}
3733

3834
/*

include/kvm/arm_pmu.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,14 @@ struct kvm_pmc {
2020
struct perf_event *perf_event;
2121
};
2222

23+
struct kvm_pmu_events {
24+
u32 events_host;
25+
u32 events_guest;
26+
};
27+
2328
struct kvm_pmu {
2429
struct irq_work overflow_work;
30+
struct kvm_pmu_events events;
2531
struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS];
2632
DECLARE_BITMAP(chained, ARMV8_PMU_MAX_COUNTER_PAIRS);
2733
int irq_num;

0 commit comments

Comments
 (0)