KVM: arm64: Do not communicate host pmu event changes by accessing hyp data
v4 cover KVM: arm64: Do not communicate host pmu event changes by accessing hyp data
Hi,
This series changes the way KVM communicates host pmu event changes to the hypervisor in nvhe and protected mode. Instead of accessing hyp data directly from the host, the data is passed to hyp via the loaded vcpu. This provides more isolation between the host and the hypervisor, and allows us to use pmu in protected mode.
This series is based on kvmarm/next (8c22fd2d4cfa).
Changes since V3 [1]:
- Fix/clarify commit messages (Oliver)
- Reduce scope of kvm_pmu_events (Oliver/Marc)
Changes since V2 [2]:
- Update the guest view of the pmu events after interrupts have been disabled (Marc)
Changes since V1 [3]:
- Repack struct kvm_pmu to remove all (two) holes (Marc)
- Updating the guest's view of the pmu events before every vcpu run rather than vcpu load to avoid missing updates (Marc)
Thanks,
/fuad
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
[3] https://lore.kernel.org/linux-arm-kernel/[email protected]/
Fuad Tabba (4):
KVM: arm64: Wrapper for getting pmu_events
KVM: arm64: Repack struct kvm_pmu to reduce size
KVM: arm64: Pass pmu events to hyp via vcpu
KVM: arm64: Reenable pmu in Protected Mode
arch/arm64/include/asm/kvm_host.h | 7 +----- arch/arm64/kvm/arm.c | 15 ++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 20 +++++----------- arch/arm64/kvm/pmu-emul.c | 3 +-- arch/arm64/kvm/pmu.c | 40 ++++++++++++++++++------------- include/kvm/arm_pmu.h | 10 ++++++-- 6 files changed, 54 insertions(+), 41 deletions(-)
base-commit: 8c22fd2d4cfaab2ae3c5859496c894df58ab014b
Signature · 6 lines
2.36.0.512.ge40c2bad7a-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 1/4 KVM: arm64: Wrapper for getting pmu_events
Eases migrating away from using hyp data and simplifies the code.
No functional change intended.
--- arch/arm64/kvm/pmu.c | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-)
arch/arm64/kvm/pmu.c +26 -16
@@ -25,21 +25,31 @@ static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)return (attr->exclude_host != attr->exclude_guest); } +static struct kvm_pmu_events *kvm_get_pmu_events (void ) +{ +struct kvm_host_data *ctx =this_cpu_ptr_hyp_sym (kvm_host_data); + +if (!ctx) +return NULL ; + +return &ctx->pmu_events; +} +/* * Add events to track that we may want to switch at guest entry/exit * time. */ void kvm_set_pmu_events (u32 set,struct perf_event_attr *attr) { -struct kvm_host_data *ctx =this_cpu_ptr_hyp_sym (kvm_host_data); +struct kvm_pmu_events *pmu =kvm_get_pmu_events (); -if (!kvm_arm_support_pmu_v3 () || !ctx || !kvm_pmu_switch_needed (attr)) +if (!kvm_arm_support_pmu_v3 () || !pmu || !kvm_pmu_switch_needed (attr))return ;if (!attr->exclude_host) - ctx->pmu_events.events_host |= set; + pmu->events_host |= set;if (!attr->exclude_guest) - ctx->pmu_events.events_guest |= set; + pmu->events_guest |= set; }/*
@@ -47,13 +57,13 @@ void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr)*/ void kvm_clr_pmu_events (u32 clr) { -struct kvm_host_data *ctx =this_cpu_ptr_hyp_sym (kvm_host_data); +struct kvm_pmu_events *pmu =kvm_get_pmu_events (); -if (!kvm_arm_support_pmu_v3 () || !ctx) +if (!kvm_arm_support_pmu_v3 () || !pmu)return ; - ctx->pmu_events.events_host &= ~clr; - ctx->pmu_events.events_guest &= ~clr; + pmu->events_host &= ~clr; + pmu->events_guest &= ~clr; }#define PMEVTYPER_READ_CASE (idx) \
@@ -169,16 +179,16 @@ static void kvm_vcpu_pmu_disable_el0(unsigned long events)*/ void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu) { -struct kvm_host_data *host; +struct kvm_pmu_events *pmu;u32 events_guest, events_host;if (!kvm_arm_support_pmu_v3 () || !has_vhe ())return ;preempt_disable (); - host =this_cpu_ptr_hyp_sym (kvm_host_data); - events_guest = host->pmu_events.events_guest; - events_host = host->pmu_events.events_host; + pmu =kvm_get_pmu_events (); + events_guest = pmu->events_guest; + events_host = pmu->events_host;kvm_vcpu_pmu_enable_el0 (events_guest);kvm_vcpu_pmu_disable_el0 (events_host);
@@ -190,15 +200,15 @@ void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu)*/ void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu) { -struct kvm_host_data *host; +struct kvm_pmu_events *pmu;u32 events_guest, events_host;if (!kvm_arm_support_pmu_v3 () || !has_vhe ())return ; - host =this_cpu_ptr_hyp_sym (kvm_host_data); - events_guest = host->pmu_events.events_guest; - events_host = host->pmu_events.events_host; + pmu =kvm_get_pmu_events (); + events_guest = pmu->events_guest; + events_host = pmu->events_host;kvm_vcpu_pmu_enable_el0 (events_host);kvm_vcpu_pmu_disable_el0 (events_guest);
Signature · 6 lines
2.36.0.512.ge40c2bad7a-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 2/4 KVM: arm64: Repack struct kvm_pmu to reduce size
In reply to Fuad Tabbastruct kvm_pmu has 2 holes using 10 bytes. This is instantiated in all vcpus, so it adds up. Repack the structures to remove the holes.
No functional change intended.
--- include/kvm/arm_pmu.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
include/kvm/arm_pmu.h +2 -2
@@ -21,12 +21,12 @@ struct kvm_pmc { };struct kvm_pmu { -int irq_num; +struct irq_work overflow_work;struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS ]; DECLARE_BITMAP(chained, ARMV8_PMU_MAX_COUNTER_PAIRS); +int irq_num;bool created;bool irq_level; -struct irq_work overflow_work; };struct arm_pmu_entry {
Signature · 6 lines
2.36.0.512.ge40c2bad7a-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 3/4 KVM: arm64: Pass pmu events to hyp via vcpu
In reply to Fuad TabbaInstead 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.
--- arch/arm64/include/asm/kvm_host.h | 7 +------ arch/arm64/kvm/arm.c | 15 +++++++++++++++ arch/arm64/kvm/hyp/nvhe/switch.c | 20 ++++++-------------- arch/arm64/kvm/pmu.c | 12 ++++-------- include/kvm/arm_pmu.h | 6 ++++++ 5 files changed, 32 insertions(+), 28 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -6
@@ -273,14 +273,8 @@ struct kvm_cpu_context {struct kvm_vcpu *__hyp_running_vcpu; }; -struct kvm_pmu_events { -u32 events_host; -u32 events_guest; -}; -struct kvm_host_data {struct kvm_cpu_context host_ctxt; -struct kvm_pmu_events pmu_events; };struct kvm_host_psci_config {
@@ -821,6 +815,7 @@ void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu);void kvm_set_pmu_events (u32 set,struct perf_event_attr *attr);void kvm_clr_pmu_events (u32 clr); +struct kvm_pmu_events *kvm_get_pmu_events (void );void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu);void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu);#else
arch/arm64/kvm/arm.c +15 -0
@@ -817,6 +817,19 @@ static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu)return ret; } +/* +* Updates the vcpu's view of the pmu events for this cpu. +* Must be called before every vcpu run after disabling interrupts, to ensure +* that an interrupt cannot fire and update the structure. +*/ +static void kvm_pmu_update_vcpu_events (struct kvm_vcpu *vcpu) +{ +if (has_vhe () || !kvm_vcpu_has_pmu (vcpu)) +return ; + + vcpu->arch.pmu.events = *kvm_get_pmu_events (); +} +/** * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code * @vcpu: The VCPU pointer
@@ -882,6 +895,8 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)kvm_vgic_flush_hwstate (vcpu); +kvm_pmu_update_vcpu_events (vcpu); +/* * Ensure we set mode to IN_GUEST_MODE after we disable * interrupts and before the final VCPU requests check.
arch/arm64/kvm/hyp/nvhe/switch.c +6 -14
@@ -153,13 +153,9 @@ static void __hyp_vgic_restore_state(struct kvm_vcpu *vcpu)/* * Disable host events, enable guest events */ -static bool __pmu_switch_to_guest (struct kvm_cpu_context *host_ctxt) +static bool __pmu_switch_to_guest (struct kvm_vcpu *vcpu) { -struct kvm_host_data *host; -struct kvm_pmu_events *pmu; - - host = container_of(host_ctxt, struct kvm_host_data, host_ctxt); - pmu = &host->pmu_events; +struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;if (pmu->events_host)write_sysreg (pmu->events_host, pmcntenclr_el0);
@@ -173,13 +169,9 @@ static bool __pmu_switch_to_guest(struct kvm_cpu_context *host_ctxt)/* * Disable guest events, enable host events */ -static void __pmu_switch_to_host (struct kvm_cpu_context *host_ctxt) +static void __pmu_switch_to_host (struct kvm_vcpu *vcpu) { -struct kvm_host_data *host; -struct kvm_pmu_events *pmu; - - host = container_of(host_ctxt, struct kvm_host_data, host_ctxt); - pmu = &host->pmu_events; +struct kvm_pmu_events *pmu = &vcpu->arch.pmu.events;if (pmu->events_guest)write_sysreg (pmu->events_guest, pmcntenclr_el0);
@@ -304,7 +296,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu) host_ctxt->__hyp_running_vcpu = vcpu; guest_ctxt = &vcpu->arch.ctxt; - pmu_switch_needed =__pmu_switch_to_guest (host_ctxt); + pmu_switch_needed =__pmu_switch_to_guest (vcpu);__sysreg_save_state_nvhe (host_ctxt);/*
@@ -366,7 +358,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)__debug_restore_host_buffers_nvhe (vcpu);if (pmu_switch_needed) -__pmu_switch_to_host (host_ctxt); +__pmu_switch_to_host (vcpu);/* Returning to host will clear PSR.I, remask PMR if needed */ if (system_uses_irq_prio_masking ())
arch/arm64/kvm/pmu.c +4 -8
@@ -5,7 +5,8 @@*/ #include <linux/kvm_host.h> #include <linux/perf_event.h> -#include <asm/kvm_hyp.h> + +static DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);/* * Given the perf event attributes and system type, determine
@@ -25,14 +26,9 @@ static bool kvm_pmu_switch_needed(struct perf_event_attr *attr)return (attr->exclude_host != attr->exclude_guest); } -static struct kvm_pmu_events *kvm_get_pmu_events (void ) +struct kvm_pmu_events *kvm_get_pmu_events (void ) { -struct kvm_host_data *ctx =this_cpu_ptr_hyp_sym (kvm_host_data); - -if (!ctx) -return NULL ; - -return &ctx->pmu_events; +return this_cpu_ptr (&kvm_pmu_events); }/*
include/kvm/arm_pmu.h +6 -0
@@ -20,8 +20,14 @@ struct kvm_pmc {struct perf_event *perf_event; }; +struct kvm_pmu_events { +u32 events_host; +u32 events_guest; +}; +struct kvm_pmu {struct irq_work overflow_work; +struct kvm_pmu_events events;struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS ]; DECLARE_BITMAP(chained, ARMV8_PMU_MAX_COUNTER_PAIRS);int irq_num;
Signature · 6 lines
2.36.0.512.ge40c2bad7a-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 3/4] KVM: arm64: Pass pmu events to hyp via vcpu
Quoted 11 lines · Fuad Tabba
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 <[email protected]>
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
PATCH v4 4/4 KVM: arm64: Reenable pmu in Protected Mode
In reply to Fuad TabbaNow that the pmu code does not access hyp data, reenable it in protected mode.
Once fully supported, protected VMs will not have pmu support, since that could leak information. However, non-protected VMs in protected mode should have pmu support if available.
--- arch/arm64/kvm/pmu-emul.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -2
@@ -774,8 +774,7 @@ void kvm_host_pmu_init(struct arm_pmu *pmu) {struct arm_pmu_entry *entry; -if (pmu->pmuver ==0 || pmu->pmuver ==ID_AA64DFR0_PMUVER_IMP_DEF || -is_protected_kvm_enabled ()) +if (pmu->pmuver ==0 || pmu->pmuver ==ID_AA64DFR0_PMUVER_IMP_DEF )return ;mutex_lock (&arm_pmus_lock);
Signature · 6 lines
2.36.0.512.ge40c2bad7a-goog _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Re: [PATCH v4 4/4] KVM: arm64: Reenable pmu in Protected Mode
Quoted 8 lines · Fuad Tabba
Now that the pmu code does not access hyp data, reenable it in protected mode.
Once fully supported, protected VMs will not have pmu support, since that could leak information. However, non-protected VMs in protected mode should have pmu support if available.
Signed-off-by Fuad Tabba <[email protected]>
_______________________________________________
kvmarm mailing list
[email protected]
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Marc Zyngier
maintainerHi Fuad,
Quoted 9 lines · Fuad Tabba
Hi,
This series changes the way KVM communicates host pmu event changes to the hypervisor in nvhe and protected mode. Instead of accessing hyp data directly from the host, the data is passed to hyp via the loaded vcpu. This provides more isolation between the host and the hypervisor, and allows us to use pmu in protected mode.
This series is based on kvmarm/next (
8c22fd2d4cfa).
In the future, please don't do that. *Always* base your patches on something stable (preferably an early -rc, as I usually start pulling stuff in around -rc3).
kvmarm/next is a moving target, and basing things on top of it results in an uncontrolled dependency chain. If your series depends on something and makes it impossible to base your patches on an rc, please state so (this isn't the case here).
Thanks,
M.
Signature · 5 lines
Jazz is not dead. It just smells funny... _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
Quoted 9 lines · Fuad Tabba
This series changes the way KVM communicates host pmu event changes to the hypervisor in nvhe and protected mode. Instead of accessing hyp data directly from the host, the data is passed to hyp via the loaded vcpu. This provides more isolation between the host and the hypervisor, and allows us to use pmu in protected mode.
This series is based on kvmarm/next (
8c22fd2d4cfa).[...]
Applied to next, thanks!
[1/4] KVM: arm64: Wrapper for getting pmu_events
commit: 3cb8a091a7016be0ee5420ff6c1928972e3ce23e
[2/4] KVM: arm64: Repack struct kvm_pmu to reduce size
commit: e987a4c60f9755b2f7a19bf1b5ef2eb74c90579b
[3/4] KVM: arm64: Pass pmu events to hyp via vcpu
commit: 84d751a019a9792f5b4884e1d598b603c360ec22
[4/4] KVM: arm64: Reenable pmu in Protected Mode
commit: 722625c6f4c5b6a9953d6af04c7bb1a6e12830b3Cheers,
M.
Signature · 7 lines
Without deviation from the norm, progress is not possible. _______________________________________________ kvmarm mailing list [email protected] https://lists.cs.columbia.edu/mailman/listinfo/kvmarm