From: Oliver Upton <oupton@google.com>
To: Fuad Tabba <tabba@google.com>
Cc: will@kernel.org, maz@kernel.org, catalin.marinas@arm.com,
kernel-team@android.com, kvmarm@lists.cs.columbia.edu,
linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v3 3/4] KVM: arm64: Pass pmu events to hyp via vcpu
Date: Mon, 9 May 2022 20:21:45 +0000 [thread overview]
Message-ID: <Ynl32cU6hkuXRQuZ@google.com> (raw)
In-Reply-To: <20220509120330.3043773-4-tabba@google.com>
On Mon, May 09, 2022 at 12:03:29PM +0000, Fuad Tabba wrote:
> 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>
> ---
> arch/arm64/include/asm/kvm_host.h | 8 ++------
> 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, 33 insertions(+), 28 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> index dfd360404dd8..90476e713643 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -273,14 +273,8 @@ struct kvm_cpu_context {
> struct kvm_vcpu *__hyp_running_vcpu;
> };
>
> -struct kvm_pmu_events {
> - u32 events_host;
> - u32 events_guest;
> -};
> -
Looks like you're moving this to arm_pmu.h as well. Probably a better
home for it, but unclear why it is done in this patch.
> struct kvm_host_data {
> struct kvm_cpu_context host_ctxt;
> - struct kvm_pmu_events pmu_events;
> };
>
Are we going to need this struct any more since it now has a single
member?
> struct kvm_host_psci_config {
> @@ -763,6 +757,7 @@ void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 syndrome);
> struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
>
> DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
> +DECLARE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
Why do you need this declaration? I don't see the percpu data being
accessed outside of pmu.c.
> static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
> {
> @@ -821,6 +816,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
> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> index 2adb5832a756..86bcdb2a23a8 100644
> --- a/arch/arm64/kvm/arm.c
> +++ b/arch/arm64/kvm/arm.c
> @@ -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.
> diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
> index 0716163313d6..c61120ec8d1a 100644
> --- a/arch/arm64/kvm/hyp/nvhe/switch.c
> +++ b/arch/arm64/kvm/hyp/nvhe/switch.c
> @@ -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())
> diff --git a/arch/arm64/kvm/pmu.c b/arch/arm64/kvm/pmu.c
> index 4bd38ff34221..c19bf6e4969e 100644
> --- a/arch/arm64/kvm/pmu.c
> +++ b/arch/arm64/kvm/pmu.c
> @@ -5,7 +5,8 @@
> */
> #include <linux/kvm_host.h>
> #include <linux/perf_event.h>
> -#include <asm/kvm_hyp.h>
> +
> +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)
Why not make this function visible in patch 1? It seems benign even
though there are no other users at that moment outside of the
compilation unit.
--
Thanks,
Oliver
_______________________________________________
kvmarm mailing list
kvmarm@lists.cs.columbia.edu
https://lists.cs.columbia.edu/mailman/listinfo/kvmarm
WARNING: multiple messages have this Message-ID (diff)
From: Oliver Upton <oupton@google.com>
To: Fuad Tabba <tabba@google.com>
Cc: kvmarm@lists.cs.columbia.edu, kernel-team@android.com,
maz@kernel.org, catalin.marinas@arm.com, will@kernel.org,
linux-arm-kernel@lists.infradead.org
Subject: Re: [PATCH v3 3/4] KVM: arm64: Pass pmu events to hyp via vcpu
Date: Mon, 9 May 2022 20:21:45 +0000 [thread overview]
Message-ID: <Ynl32cU6hkuXRQuZ@google.com> (raw)
In-Reply-To: <20220509120330.3043773-4-tabba@google.com>
On Mon, May 09, 2022 at 12:03:29PM +0000, Fuad Tabba wrote:
> 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>
> ---
> arch/arm64/include/asm/kvm_host.h | 8 ++------
> 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, 33 insertions(+), 28 deletions(-)
>
> diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
> index dfd360404dd8..90476e713643 100644
> --- a/arch/arm64/include/asm/kvm_host.h
> +++ b/arch/arm64/include/asm/kvm_host.h
> @@ -273,14 +273,8 @@ struct kvm_cpu_context {
> struct kvm_vcpu *__hyp_running_vcpu;
> };
>
> -struct kvm_pmu_events {
> - u32 events_host;
> - u32 events_guest;
> -};
> -
Looks like you're moving this to arm_pmu.h as well. Probably a better
home for it, but unclear why it is done in this patch.
> struct kvm_host_data {
> struct kvm_cpu_context host_ctxt;
> - struct kvm_pmu_events pmu_events;
> };
>
Are we going to need this struct any more since it now has a single
member?
> struct kvm_host_psci_config {
> @@ -763,6 +757,7 @@ void kvm_set_sei_esr(struct kvm_vcpu *vcpu, u64 syndrome);
> struct kvm_vcpu *kvm_mpidr_to_vcpu(struct kvm *kvm, unsigned long mpidr);
>
> DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data);
> +DECLARE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events);
Why do you need this declaration? I don't see the percpu data being
accessed outside of pmu.c.
> static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt)
> {
> @@ -821,6 +816,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
> diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c
> index 2adb5832a756..86bcdb2a23a8 100644
> --- a/arch/arm64/kvm/arm.c
> +++ b/arch/arm64/kvm/arm.c
> @@ -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.
> diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c
> index 0716163313d6..c61120ec8d1a 100644
> --- a/arch/arm64/kvm/hyp/nvhe/switch.c
> +++ b/arch/arm64/kvm/hyp/nvhe/switch.c
> @@ -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())
> diff --git a/arch/arm64/kvm/pmu.c b/arch/arm64/kvm/pmu.c
> index 4bd38ff34221..c19bf6e4969e 100644
> --- a/arch/arm64/kvm/pmu.c
> +++ b/arch/arm64/kvm/pmu.c
> @@ -5,7 +5,8 @@
> */
> #include <linux/kvm_host.h>
> #include <linux/perf_event.h>
> -#include <asm/kvm_hyp.h>
> +
> +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)
Why not make this function visible in patch 1? It seems benign even
though there are no other users at that moment outside of the
compilation unit.
--
Thanks,
Oliver
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
next prev parent reply other threads:[~2022-05-09 20:21 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-09 12:03 [PATCH v3 0/4] KVM: arm64: Do not communicate host pmu event changes by accessing hyp data Fuad Tabba
2022-05-09 12:03 ` Fuad Tabba
2022-05-09 12:03 ` [PATCH v3 1/4] KVM: arm64: Wrapper for getting pmu_events Fuad Tabba
2022-05-09 12:03 ` Fuad Tabba
2022-05-09 19:41 ` Oliver Upton
2022-05-09 19:41 ` Oliver Upton
2022-05-09 12:03 ` [PATCH v3 2/4] KVM: arm64: Repack struct kvm_pmu to reduce size Fuad Tabba
2022-05-09 12:03 ` Fuad Tabba
2022-05-09 19:57 ` Oliver Upton
2022-05-09 19:57 ` Oliver Upton
2022-05-10 6:38 ` Fuad Tabba
2022-05-10 6:38 ` Fuad Tabba
2022-05-09 12:03 ` [PATCH v3 3/4] KVM: arm64: Pass pmu events to hyp via vcpu Fuad Tabba
2022-05-09 12:03 ` Fuad Tabba
2022-05-09 20:21 ` Oliver Upton [this message]
2022-05-09 20:21 ` Oliver Upton
2022-05-10 6:43 ` Fuad Tabba
2022-05-10 6:43 ` Fuad Tabba
2022-05-10 9:21 ` Marc Zyngier
2022-05-10 9:21 ` Marc Zyngier
2022-05-09 12:03 ` [PATCH v3 4/4] KVM: arm64: Reenable pmu in Protected Mode Fuad Tabba
2022-05-09 12:03 ` Fuad Tabba
2022-05-09 22:36 ` Oliver Upton
2022-05-09 22:36 ` Oliver Upton
2022-05-10 6:45 ` Fuad Tabba
2022-05-10 6:45 ` Fuad Tabba
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Ynl32cU6hkuXRQuZ@google.com \
--to=oupton@google.com \
--cc=catalin.marinas@arm.com \
--cc=kernel-team@android.com \
--cc=kvmarm@lists.cs.columbia.edu \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=maz@kernel.org \
--cc=tabba@google.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.