From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 365BDC433F5 for ; Thu, 5 May 2022 09:02:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Subject:Cc:To:From:Message-ID:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Ntnscx19Vx1hcydEW1YFQ8zNzyvlqevplnkc6Vsg68o=; b=GUTwrbckZwLXu6 8Rdf7fb+aAnTZQzvMLQwHvrmjht7x/nliZOA7VVZV6Qoto+TmUlDYUXr2c0mN9cOV9tHG4kgPumIL 1ME4uecWX4R779z8nXSahdp7HsB0lBkPMOLnKpOi2KgmxgVDPBIbtsU+5ak38Cf9Ax9QQ3cVWxWAq hq9io74iCZdiblx6Okxn5X2w+9cWMKgiJcRVEwCTOawyx1JjU3mpXkAknJXGoOPGYUZMeaKakHtor arPqVYTkCnu30UQSLi09qBvZsZmQQVTBtmcTgau8nba3fK0YserZmmIGDxIvoz+lRvbMfR6OmeC5l FsPlKv0y3nck7tisXIAw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nmXLz-00Eu3m-EH; Thu, 05 May 2022 09:00:55 +0000 Received: from ams.source.kernel.org ([2604:1380:4601:e00::1]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nmXLv-00Eu2B-3k for linux-arm-kernel@lists.infradead.org; Thu, 05 May 2022 09:00:53 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 51691B82C06; Thu, 5 May 2022 09:00:49 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E7DA4C385A4; Thu, 5 May 2022 09:00:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1651741248; bh=BUsf1fIBA5aADpS52qetD4YREZhe1FozuBh6BsYEu7c=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=goXc0i14lR9YAn3HJ4tfoakLg46AJ4YZS4v8sbHeuQfaS/6w3fHCNA/y+Y+9LuA92 7OM92QJS0oDyn+5v1krPWLV+s7d8F1Plji+DnMG6f55SN2FM12x6g6WAjpIOpY0wM6 9TfNTq2kUlBX8DhC7Px3gv894nv7e/DvllPq/ISnEwCyMntkhYOr9m7v5eYLbwyApT KUVwKhqAlFN0RvkUga9lWVw1ejuEgdojJsqMQJl+OAvPPPLnAfN77dSBPj/GA3MymO RLce47WB+lD63pw1Y8dAOgHXSZWP0LtTEoETwxgB85ch+jBVjMsSYTgJvgWk8S4VdQ b4wCw7N6YhPdQ== Received: from sofa.misterjones.org ([185.219.108.64] helo=why.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nmXLo-0099qZ-Np; Thu, 05 May 2022 10:00:45 +0100 Date: Thu, 05 May 2022 10:00:44 +0100 Message-ID: <871qx88ibn.wl-maz@kernel.org> From: Marc Zyngier To: Fuad Tabba Cc: kvmarm@lists.cs.columbia.edu, will@kernel.org, qperret@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, drjones@redhat.com, linux-arm-kernel@lists.infradead.org, kernel-team@android.com Subject: Re: [PATCH v1 2/3] KVM: arm64: Pass pmu events to hyp via vcpu In-Reply-To: <20220408084052.3310931-3-tabba@google.com> References: <20220408084052.3310931-1-tabba@google.com> <20220408084052.3310931-3-tabba@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") X-SA-Exim-Connect-IP: 185.219.108.64 X-SA-Exim-Rcpt-To: tabba@google.com, kvmarm@lists.cs.columbia.edu, will@kernel.org, qperret@google.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, drjones@redhat.com, linux-arm-kernel@lists.infradead.org, kernel-team@android.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220505_020051_485971_A2109B1A X-CRM114-Status: GOOD ( 32.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, 08 Apr 2022 09:40:51 +0100, Fuad Tabba wrote: > > Instead of accessing hyp data, pass the pmu events of the current > cpu to hyp via the loaded vcpu. This really deserves a more thorough explanation. What this is doing is moving the part of the shared memory between EL1 and EL2 into each vcpu, effectively multiplying the memory usage by the number of vcpus in the system. It isn't a big deal (what are 64 bits between friends?), but this is definitely worth calling out. > > No functional change intended. > > Signed-off-by: Fuad Tabba > --- > arch/arm64/include/asm/kvm_host.h | 8 ++------ > arch/arm64/kvm/arm.c | 2 +- > arch/arm64/kvm/hyp/nvhe/switch.c | 20 ++++++-------------- > arch/arm64/kvm/pmu.c | 22 +++++++++++++--------- > include/kvm/arm_pmu.h | 6 ++++++ > 5 files changed, 28 insertions(+), 30 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h > index 0e96087885fe..b5cdfb6cb9c7 100644 > --- a/arch/arm64/include/asm/kvm_host.h > +++ b/arch/arm64/include/asm/kvm_host.h > @@ -244,14 +244,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 { > @@ -728,6 +722,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); > > static inline void kvm_init_host_cpu_context(struct kvm_cpu_context *cpu_ctxt) > { > @@ -781,6 +776,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); > > +void kvm_vcpu_pmu_load(struct kvm_vcpu *vcpu); > 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 ba9165e84396..e6f76d843558 100644 > --- a/arch/arm64/kvm/arm.c > +++ b/arch/arm64/kvm/arm.c > @@ -400,7 +400,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) > if (has_vhe()) > kvm_vcpu_load_sysregs_vhe(vcpu); > kvm_arch_vcpu_load_fp(vcpu); > - kvm_vcpu_pmu_restore_guest(vcpu); > + kvm_vcpu_pmu_load(vcpu); > if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) > kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); > > diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c > index 6410d21d8695..ff7b29fb9787 100644 > --- a/arch/arm64/kvm/hyp/nvhe/switch.c > +++ b/arch/arm64/kvm/hyp/nvhe/switch.c > @@ -123,13 +123,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); > @@ -143,13 +139,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); > @@ -274,7 +266,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); > /* > @@ -336,7 +328,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 310d47c9990f..8f722692fb58 100644 > --- a/arch/arm64/kvm/pmu.c > +++ b/arch/arm64/kvm/pmu.c > @@ -5,7 +5,8 @@ > */ > #include > #include > -#include > + > +DEFINE_PER_CPU(struct kvm_pmu_events, kvm_pmu_events); > > /* > * Given the perf event attributes and system type, determine > @@ -27,12 +28,7 @@ static bool kvm_pmu_switch_needed(struct perf_event_attr *attr) > > static struct kvm_pmu_events *get_kvm_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); > } > > /* > @@ -43,7 +39,7 @@ void kvm_set_pmu_events(u32 set, struct perf_event_attr *attr) > { > struct kvm_pmu_events *pmu = get_kvm_pmu_events(); > > - if (!kvm_arm_support_pmu_v3() || !pmu || !kvm_pmu_switch_needed(attr)) > + if (!kvm_arm_support_pmu_v3() || !kvm_pmu_switch_needed(attr)) > return; > > if (!attr->exclude_host) > @@ -59,7 +55,7 @@ void kvm_clr_pmu_events(u32 clr) > { > struct kvm_pmu_events *pmu = get_kvm_pmu_events(); > > - if (!kvm_arm_support_pmu_v3() || !pmu) > + if (!kvm_arm_support_pmu_v3()) > return; > > pmu->events_host &= ~clr; > @@ -213,3 +209,11 @@ void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu) > kvm_vcpu_pmu_enable_el0(events_host); > kvm_vcpu_pmu_disable_el0(events_guest); > } > + > +void kvm_vcpu_pmu_load(struct kvm_vcpu *vcpu) > +{ > + kvm_vcpu_pmu_restore_guest(vcpu); > + > + if (kvm_arm_support_pmu_v3() && !has_vhe()) > + vcpu->arch.pmu.events = *get_kvm_pmu_events(); What guarantees do you have that the structure isn't updated between load() and the point where the data is used? For example, can an interrupt enable or disable a counter behind our back? I have the feeling it can happen. > +} > diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h > index 20193416d214..0b3898e0313f 100644 > --- a/include/kvm/arm_pmu.h > +++ b/include/kvm/arm_pmu.h > @@ -20,6 +20,11 @@ struct kvm_pmc { > struct perf_event *perf_event; > }; > > +struct kvm_pmu_events { > + u32 events_host; > + u32 events_guest; > +}; > + > struct kvm_pmu { > int irq_num; > struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS]; > @@ -27,6 +32,7 @@ struct kvm_pmu { > bool created; > bool irq_level; > struct irq_work overflow_work; > + struct kvm_pmu_events events; This makes be realise that the packing of this structure is absolutely awful, and that has ripple effects in the vcpu struct. Can you please reorganise the struct so that irq_num, created and irq_level are at the end? Thanks, M. -- Without deviation from the norm, progress is not possible. _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel