Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3439859pxb; Mon, 1 Mar 2021 10:02:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJzbTQ5SFm5xA5qNnplqUfZsR3Zn7vXOv0r2oZnA0Alv3qRoe3lm16adrUga46PqlQnQxpgs X-Received: by 2002:a17:906:cecc:: with SMTP id si12mr17178435ejb.461.1614621741677; Mon, 01 Mar 2021 10:02:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614621741; cv=none; d=google.com; s=arc-20160816; b=BraC0iHEJfL50Gk85zDnJ9I4quirTOznvbkJkFX+Vai/fcEOb98sVdxW5SOHL9DIQ2 ZxrmZU2QkHAtI5C/o2hLPl2A5tGIMI0zhBGfJul/G0kjWfZsaKycz3dYivwB1Bjn6JDB 7gXQmRvu+/byrYUumx9DzpEcrtUGSpR/8XQBBM5ZJC/trmffIejqHdEO0hbemikZl9i1 1mK5jhQU03p+8nBrIe1uLMbU4gPhAY8exjDZEwKnOT4C+fo2gwAfNZBlhXw4qZ0zz+uX Y78fwuDgzN8q1XETgblwwsWNustyT43+W/6soc+uboPU2oFmMv+MaJDaNmAsaFpBR464 ZNIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-language:content-transfer-encoding :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:subject:from; bh=60DJyX0CUjb6LlVe5OX9xa4YLyS+BiGGqWeb1ewAdv8=; b=RcGA5AcHGxGI3z3J/lyr286dEKkfUuY1uR0QVbg0Ej5oMQbbu+Ohfw3hXj93wxSZMd 8OPIa9PutnYF7LLZnKOm62zbPV+Mav3265SC0b38jpJsOv7JNdgs1XEdcYTQPxEF93ml KqOpqSqXaUGO7BE1is+C8Yz5Uvsr1iMDt1HTuZZBnaDO8J1T1LNKujuQ9OcVnYNBBbCP W+DRkrivVYMViQYBGh0TOTdATM2wgUJYOSbm0NWj5gU+DAw4OEVwuQ7GEQjncOj4B4jS lCy0hvOXQd+z7SmD+evZxgckJLN82ZABdlCp3L0+eVxfB7VvpkwUSR24lkd9Vs/c+WfU c0qA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p6si11672680ejc.540.2021.03.01.10.01.49; Mon, 01 Mar 2021 10:02:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239283AbhCAR7f (ORCPT + 99 others); Mon, 1 Mar 2021 12:59:35 -0500 Received: from foss.arm.com ([217.140.110.172]:33520 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232932AbhCAQdF (ORCPT ); Mon, 1 Mar 2021 11:33:05 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 231561042; Mon, 1 Mar 2021 08:32:17 -0800 (PST) Received: from [192.168.0.110] (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 220633F73C; Mon, 1 Mar 2021 08:32:14 -0800 (PST) From: Alexandru Elisei Subject: Re: [PATCH v4 04/19] kvm: arm64: nvhe: Save the SPE context early To: Suzuki K Poulose , linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org, mathieu.poirier@linaro.org, mike.leach@linaro.org, anshuman.khandual@arm.com, leo.yan@linaro.org, stable@vger.kernel.org, Christoffer Dall , Marc Zyngier , Will Deacon , Catalin Marinas , Mark Rutland References: <20210225193543.2920532-1-suzuki.poulose@arm.com> <20210225193543.2920532-5-suzuki.poulose@arm.com> Message-ID: Date: Mon, 1 Mar 2021 16:32:23 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.0 MIME-Version: 1.0 In-Reply-To: <20210225193543.2920532-5-suzuki.poulose@arm.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Content-Language: en-US Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Suzuki, On 2/25/21 7:35 PM, Suzuki K Poulose wrote: > The nvhe hyp saves the SPE context, flushing any unwritten Perhaps that can be reworded to "The nVHE world switch code saves [..]". Also, according to my understanding of "context", that means saving *all* the related registers. But KVM saves only *one* register, PMSCR_EL1. I think stating that KVM disables sampling and drains the buffer would be more accurate. > data before we switch to the guest. But this operation is > performed way too late, because : > - The ownership of the SPE is transferred to EL2. i.e, I think the Arm ARM defines only the ownership of the SPE *buffer* (buffer owning regime), not the ownership of SPE as a whole. > using EL2 translations. (MDCR_EL2_E2PB == 0) I think "EL2 translations" is bit too vague, EL2 stage 1 translation would be more precise, since we're talking only about the nVHE case. > - The guest Stage1 is loaded. > > Thus the flush could use the host EL1 virtual address, > but use the EL2 translations instead. Fix this by It's not *could*, it's *will*. The SPE buffer will use the buffer pointer programmed by the host at EL1, but will attempt to translate it using EL2 stage 1 translation, where it's (probably) not mapped. > and before we change the ownership to EL2. Same comment about ownership. > The restore path is doing the right thing. > > Fixes: 014c4c77aad7 ("KVM: arm64: Improve debug register save/restore flow") > Cc: stable@vger.kernel.org > Cc: Christoffer Dall > Cc: Marc Zyngier > Cc: Will Deacon > Cc: Catalin Marinas > Cc: Mark Rutland > Cc: Alexandru Elisei > Signed-off-by: Suzuki K Poulose > --- > New patch. > --- > arch/arm64/include/asm/kvm_hyp.h | 5 +++++ > arch/arm64/kvm/hyp/nvhe/debug-sr.c | 12 ++++++++++-- > arch/arm64/kvm/hyp/nvhe/switch.c | 12 +++++++++++- > 3 files changed, 26 insertions(+), 3 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_hyp.h b/arch/arm64/include/asm/kvm_hyp.h > index c0450828378b..385bd7dd3d39 100644 > --- a/arch/arm64/include/asm/kvm_hyp.h > +++ b/arch/arm64/include/asm/kvm_hyp.h > @@ -83,6 +83,11 @@ void sysreg_restore_guest_state_vhe(struct kvm_cpu_context *ctxt); > void __debug_switch_to_guest(struct kvm_vcpu *vcpu); > void __debug_switch_to_host(struct kvm_vcpu *vcpu); > > +#ifdef __KVM_NVHE_HYPERVISOR__ > +void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu); > +void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu); > +#endif > + > void __fpsimd_save_state(struct user_fpsimd_state *fp_regs); > void __fpsimd_restore_state(struct user_fpsimd_state *fp_regs); > > diff --git a/arch/arm64/kvm/hyp/nvhe/debug-sr.c b/arch/arm64/kvm/hyp/nvhe/debug-sr.c > index 91a711aa8382..f401724f12ef 100644 > --- a/arch/arm64/kvm/hyp/nvhe/debug-sr.c > +++ b/arch/arm64/kvm/hyp/nvhe/debug-sr.c > @@ -58,16 +58,24 @@ static void __debug_restore_spe(u64 pmscr_el1) > write_sysreg_s(pmscr_el1, SYS_PMSCR_EL1); > } > > -void __debug_switch_to_guest(struct kvm_vcpu *vcpu) > +void __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu) > { > /* Disable and flush SPE data generation */ > __debug_save_spe(&vcpu->arch.host_debug_state.pmscr_el1); > +} > + > +void __debug_switch_to_guest(struct kvm_vcpu *vcpu) > +{ > __debug_switch_to_guest_common(vcpu); > } > > -void __debug_switch_to_host(struct kvm_vcpu *vcpu) > +void __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu) > { > __debug_restore_spe(vcpu->arch.host_debug_state.pmscr_el1); > +} > + > +void __debug_switch_to_host(struct kvm_vcpu *vcpu) > +{ > __debug_switch_to_host_common(vcpu); > } > > diff --git a/arch/arm64/kvm/hyp/nvhe/switch.c b/arch/arm64/kvm/hyp/nvhe/switch.c > index f3d0e9eca56c..10eed66136a0 100644 > --- a/arch/arm64/kvm/hyp/nvhe/switch.c > +++ b/arch/arm64/kvm/hyp/nvhe/switch.c > @@ -192,6 +192,15 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu) > pmu_switch_needed = __pmu_switch_to_guest(host_ctxt); > > __sysreg_save_state_nvhe(host_ctxt); > + /* > + * For nVHE, we must save and disable any SPE > + * buffers, as the translation regime is going I'm not sure what "save and disable any SPE buffers" means. The code definitely doesn't save anything related to the SPE buffer. Maybe you're trying to say that it must drain the buffer contents to memory? Also, SPE has only *one* buffer. > + * to be loaded with that of the guest. And we must > + * save host context for SPE, before we change the > + * ownership to EL2 (via MDCR_EL2_E2PB == 0) and before Same comments about "save host context for SPE" (from what I understand that "context" means, KVM doesn't do that) and ownership (SPE doesn't have an ownership, the SPE buffer has an owning translation regime). > + * we load guest Stage1. > + */ > + __debug_save_host_buffers_nvhe(vcpu); > > __adjust_pc(vcpu); > > @@ -234,11 +243,12 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu) > if (vcpu->arch.flags & KVM_ARM64_FP_ENABLED) > __fpsimd_save_fpexc32(vcpu); > > + __debug_switch_to_host(vcpu); > /* > * This must come after restoring the host sysregs, since a non-VHE > * system may enable SPE here and make use of the TTBRs. > */ > - __debug_switch_to_host(vcpu); > + __debug_restore_host_buffers_nvhe(vcpu); > > if (pmu_switch_needed) > __pmu_switch_to_host(host_ctxt); The patch looks correct to me. There are several things that are wrong with the way KVM drains the SPE buffer in __debug_switch_to_guest(): 1. The buffer is drained after the guest's stage 1 is loaded in __sysreg_restore_state_nvhe() -> __sysreg_restore_el1_state(). 2. The buffer is drained after HCR_EL2.VM is set in __activate_traps() -> ___activate_traps(), which means that the buffer would have use the guest's stage 1 + host's stage 2 for address translation if not 3 below. 3. And finally, the buffer is drained after MDCR_EL2.E2PB is set to 0b00 in __activate_traps() -> __activate_traps_common() (vcpu->arch.mdcr_el2 is computed in kvm_arch_vcpu_ioctl_run() -> kvm_arm_setup_debug() before __kvm_vcpu_run(), which means that the buffer will end up using the EL2 stage 1 translation because of the ISB after sampling is disabled. Your fix looks correct to me, we drain the buffer and disable event sampling before we start restoring any of the state associated with the guest, and we re-enable profiling after we restore all the host's state relevant for profiling. Thanks, Alex