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 X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 75A0DC00A89 for ; Thu, 5 Nov 2020 11:30:54 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id 8468B2078E for ; Thu, 5 Nov 2020 11:30:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="rplNhh2/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8468B2078E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvmarm-bounces@lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id D56D34B73C; Thu, 5 Nov 2020 06:30:52 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@kernel.org Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 13DJe3vC3+fs; Thu, 5 Nov 2020 06:30:50 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 919DB4B742; Thu, 5 Nov 2020 06:30:50 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 470204B6B0 for ; Thu, 5 Nov 2020 06:30:49 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id HJre2xcammfM for ; Thu, 5 Nov 2020 06:30:48 -0500 (EST) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 2842A4B3CB for ; Thu, 5 Nov 2020 06:30:48 -0500 (EST) Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C04C12083B; Thu, 5 Nov 2020 11:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604575846; bh=DHA+HMmRj/vljpoB6PI9AKkrWsALaL5kz7s/bPUod+g=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=rplNhh2/x15GoJw1yq2xcGLWRcSdakUzgWbwmTVznU3ZJVWM5SMdka+hUtwGF0iOi w/AJEBkHKi6Dmq8uyEc0YHX7ZRCaxvDE8mzfYwCtLPilLZf7UCyj30N2hy9H0oW9cV sVNJYhUUsD/oxcHj9ZgxnZc+rdEZHbB2longFGb0= Received: from disco-boy.misterjones.org ([51.254.78.96] helo=www.loen.fr) by disco-boy.misterjones.org with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94) (envelope-from ) id 1kadTY-007oYq-7R; Thu, 05 Nov 2020 11:30:44 +0000 MIME-Version: 1.0 Date: Thu, 05 Nov 2020 11:30:44 +0000 From: Marc Zyngier To: David Brazdil Subject: Re: [RFC PATCH 18/26] kvm: arm64: Intercept PSCI_CPU_OFF host SMC calls In-Reply-To: <20201104183630.27513-19-dbrazdil@google.com> References: <20201104183630.27513-1-dbrazdil@google.com> <20201104183630.27513-19-dbrazdil@google.com> User-Agent: Roundcube Webmail/1.4.9 Message-ID: <0ebade5427b2d9a020cd33da64cb9d13@kernel.org> X-Sender: maz@kernel.org X-SA-Exim-Connect-IP: 51.254.78.96 X-SA-Exim-Rcpt-To: dbrazdil@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, mark.rutland@arm.com, lorenzo.pieralisi@arm.com, qperret@google.com, ascull@google.com, 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 Cc: Lorenzo Pieralisi , kernel-team@android.com, Catalin Marinas , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Tejun Heo , Dennis Zhou , Christoph Lameter , Will Deacon , kvmarm@lists.cs.columbia.edu X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On 2020-11-04 18:36, David Brazdil wrote: > Add a handler of the CPU_OFF PSCI host SMC trapped in KVM nVHE hyp > code. > When invoked, it changes the recorded state of the core to OFF before > forwarding the call to EL3. If the call fails, it changes the state > back > to ON and returns the error to the host. > > Signed-off-by: David Brazdil > --- > arch/arm64/kvm/hyp/nvhe/psci.c | 30 +++++++++++++++++++++++++++++- > 1 file changed, 29 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c > b/arch/arm64/kvm/hyp/nvhe/psci.c > index c3d0a6246c66..00dc0cab860c 100644 > --- a/arch/arm64/kvm/hyp/nvhe/psci.c > +++ b/arch/arm64/kvm/hyp/nvhe/psci.c > @@ -13,6 +13,8 @@ > #include > #include > > +#include > + > /* Config options set by the host. */ > u32 kvm_host_psci_version = PSCI_VERSION(0, 0); > u32 kvm_host_psci_function_id[PSCI_FN_MAX]; > @@ -20,6 +22,7 @@ s64 hyp_physvirt_offset; > > #define __hyp_pa(x) ((phys_addr_t)(x) + hyp_physvirt_offset) > > +static DEFINE_PER_CPU(hyp_spinlock_t, psci_cpu_lock); > DEFINE_PER_CPU(enum kvm_nvhe_psci_state, psci_cpu_state); > > static u64 get_psci_func_id(struct kvm_cpu_context *host_ctxt) > @@ -76,9 +79,32 @@ static __noreturn unsigned long > psci_forward_noreturn(struct kvm_cpu_context *ho > hyp_panic(); /* unreachable */ > } > > +static int psci_cpu_off(u64 func_id, struct kvm_cpu_context > *host_ctxt) > +{ > + hyp_spinlock_t *cpu_lock = this_cpu_ptr(&psci_cpu_lock); > + enum kvm_nvhe_psci_state *cpu_power = this_cpu_ptr(&psci_cpu_state); > + u32 power_state = (u32)host_ctxt->regs.regs[1]; > + int ret; > + > + /* Change the recorded state to OFF before forwarding the call. */ > + hyp_spin_lock(cpu_lock); > + *cpu_power = KVM_NVHE_PSCI_CPU_OFF; > + hyp_spin_unlock(cpu_lock); So at this point, another CPU can observe the vcpu being "off", and issue a PSCI_ON, which may result in an "already on". I'm not sure this is an actual issue, but it is worth documenting. What is definitely missing is a rational about *why* we need to track the state of the vcpus. I naively imagined that we could directly proxy the PSCI calls to EL3, only repainting PC for PSCI_ON. Thanks, M. -- Jazz is not dead. It just smells funny... _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm 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 X-Spam-Level: X-Spam-Status: No, score=-10.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B76F7C00A89 for ; Thu, 5 Nov 2020 11:31:25 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 218E52053B for ; Thu, 5 Nov 2020 11:31:21 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="I6Yx+oKe"; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="rplNhh2/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 218E52053B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Type: Content-Transfer-Encoding:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Message-ID:References:In-Reply-To:Subject:To:From: Date:MIME-Version:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=BVrT18JuPRx9DLRw+SOa6u90KQi4b+wHQrRWEq5RCkc=; b=I6Yx+oKe9HrYkWlJx0ql3606C OOwWqx6wVsCuTX2cGQ9J7RQ6r/yIle7pFaQ4cqojBb3Bw89Y9hRd4dNUz6jH7mUcc7eFMKS7AW6Xs PHSGEJhzSpQd7WFfAfQUKJl32jhvK4i11uKIy/L4y0vNXI2a7d2fnTd1qnAmwJmEYgOorK1+jv/IR fM6Nxbn3FkEbQ+FUX+r9vW9Qf/U35HZtt01SUALwPTZGMVzgX380esPjN/Q32La56rfjYBLhawE17 XXRvmGSob6ZfYs5GnFh5IXocnHlDfD+1/R36Jj9B8H/OIRJCt735tiVTui010nAJRMJrrYF5X6xad kN2kHHwdQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kadTf-00010s-Ih; Thu, 05 Nov 2020 11:30:51 +0000 Received: from mail.kernel.org ([198.145.29.99]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kadTc-0000zO-AK for linux-arm-kernel@lists.infradead.org; Thu, 05 Nov 2020 11:30:49 +0000 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C04C12083B; Thu, 5 Nov 2020 11:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604575846; bh=DHA+HMmRj/vljpoB6PI9AKkrWsALaL5kz7s/bPUod+g=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=rplNhh2/x15GoJw1yq2xcGLWRcSdakUzgWbwmTVznU3ZJVWM5SMdka+hUtwGF0iOi w/AJEBkHKi6Dmq8uyEc0YHX7ZRCaxvDE8mzfYwCtLPilLZf7UCyj30N2hy9H0oW9cV sVNJYhUUsD/oxcHj9ZgxnZc+rdEZHbB2longFGb0= Received: from disco-boy.misterjones.org ([51.254.78.96] helo=www.loen.fr) by disco-boy.misterjones.org with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94) (envelope-from ) id 1kadTY-007oYq-7R; Thu, 05 Nov 2020 11:30:44 +0000 MIME-Version: 1.0 Date: Thu, 05 Nov 2020 11:30:44 +0000 From: Marc Zyngier To: David Brazdil Subject: Re: [RFC PATCH 18/26] kvm: arm64: Intercept PSCI_CPU_OFF host SMC calls In-Reply-To: <20201104183630.27513-19-dbrazdil@google.com> References: <20201104183630.27513-1-dbrazdil@google.com> <20201104183630.27513-19-dbrazdil@google.com> User-Agent: Roundcube Webmail/1.4.9 Message-ID: <0ebade5427b2d9a020cd33da64cb9d13@kernel.org> X-Sender: maz@kernel.org X-SA-Exim-Connect-IP: 51.254.78.96 X-SA-Exim-Rcpt-To: dbrazdil@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, mark.rutland@arm.com, lorenzo.pieralisi@arm.com, qperret@google.com, ascull@google.com, 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-20201105_063048_676718_B82EDBB5 X-CRM114-Status: GOOD ( 22.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , Lorenzo Pieralisi , kernel-team@android.com, Suzuki K Poulose , Catalin Marinas , Quentin Perret , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Tejun Heo , Dennis Zhou , Christoph Lameter , Will Deacon , kvmarm@lists.cs.columbia.edu, Julien Thierry , Andrew Scull Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On 2020-11-04 18:36, David Brazdil wrote: > Add a handler of the CPU_OFF PSCI host SMC trapped in KVM nVHE hyp > code. > When invoked, it changes the recorded state of the core to OFF before > forwarding the call to EL3. If the call fails, it changes the state > back > to ON and returns the error to the host. > > Signed-off-by: David Brazdil > --- > arch/arm64/kvm/hyp/nvhe/psci.c | 30 +++++++++++++++++++++++++++++- > 1 file changed, 29 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c > b/arch/arm64/kvm/hyp/nvhe/psci.c > index c3d0a6246c66..00dc0cab860c 100644 > --- a/arch/arm64/kvm/hyp/nvhe/psci.c > +++ b/arch/arm64/kvm/hyp/nvhe/psci.c > @@ -13,6 +13,8 @@ > #include > #include > > +#include > + > /* Config options set by the host. */ > u32 kvm_host_psci_version = PSCI_VERSION(0, 0); > u32 kvm_host_psci_function_id[PSCI_FN_MAX]; > @@ -20,6 +22,7 @@ s64 hyp_physvirt_offset; > > #define __hyp_pa(x) ((phys_addr_t)(x) + hyp_physvirt_offset) > > +static DEFINE_PER_CPU(hyp_spinlock_t, psci_cpu_lock); > DEFINE_PER_CPU(enum kvm_nvhe_psci_state, psci_cpu_state); > > static u64 get_psci_func_id(struct kvm_cpu_context *host_ctxt) > @@ -76,9 +79,32 @@ static __noreturn unsigned long > psci_forward_noreturn(struct kvm_cpu_context *ho > hyp_panic(); /* unreachable */ > } > > +static int psci_cpu_off(u64 func_id, struct kvm_cpu_context > *host_ctxt) > +{ > + hyp_spinlock_t *cpu_lock = this_cpu_ptr(&psci_cpu_lock); > + enum kvm_nvhe_psci_state *cpu_power = this_cpu_ptr(&psci_cpu_state); > + u32 power_state = (u32)host_ctxt->regs.regs[1]; > + int ret; > + > + /* Change the recorded state to OFF before forwarding the call. */ > + hyp_spin_lock(cpu_lock); > + *cpu_power = KVM_NVHE_PSCI_CPU_OFF; > + hyp_spin_unlock(cpu_lock); So at this point, another CPU can observe the vcpu being "off", and issue a PSCI_ON, which may result in an "already on". I'm not sure this is an actual issue, but it is worth documenting. What is definitely missing is a rational about *why* we need to track the state of the vcpus. I naively imagined that we could directly proxy the PSCI calls to EL3, only repainting PC for PSCI_ON. Thanks, M. -- Jazz is not dead. It just smells funny... _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel 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 X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF0B8C4741F for ; Thu, 5 Nov 2020 11:34:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5E5E42083B for ; Thu, 5 Nov 2020 11:34:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604576057; bh=DHA+HMmRj/vljpoB6PI9AKkrWsALaL5kz7s/bPUod+g=; h=Date:From:To:Cc:Subject:In-Reply-To:References:List-ID:From; b=zz8pvIWW8DsQhyou5j5Mb4KJbQ9Ls27HF4T/WRK1svzmHWb6Z3S69sVIMD7DmGJo7 0a/QdZlUUF9D0ov8R4NwOAWZYQwmETtxXRT21AIKu4X+Iw7b6B7L4nqnY2BV+1WIr5 OynuB86mSfPeqrz0CWN34NEYEPJxnbYFd4m+NutI= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730238AbgKELcF (ORCPT ); Thu, 5 Nov 2020 06:32:05 -0500 Received: from mail.kernel.org ([198.145.29.99]:53434 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729748AbgKELar (ORCPT ); Thu, 5 Nov 2020 06:30:47 -0500 Received: from disco-boy.misterjones.org (disco-boy.misterjones.org [51.254.78.96]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C04C12083B; Thu, 5 Nov 2020 11:30:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604575846; bh=DHA+HMmRj/vljpoB6PI9AKkrWsALaL5kz7s/bPUod+g=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=rplNhh2/x15GoJw1yq2xcGLWRcSdakUzgWbwmTVznU3ZJVWM5SMdka+hUtwGF0iOi w/AJEBkHKi6Dmq8uyEc0YHX7ZRCaxvDE8mzfYwCtLPilLZf7UCyj30N2hy9H0oW9cV sVNJYhUUsD/oxcHj9ZgxnZc+rdEZHbB2longFGb0= Received: from disco-boy.misterjones.org ([51.254.78.96] helo=www.loen.fr) by disco-boy.misterjones.org with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94) (envelope-from ) id 1kadTY-007oYq-7R; Thu, 05 Nov 2020 11:30:44 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Thu, 05 Nov 2020 11:30:44 +0000 From: Marc Zyngier To: David Brazdil Cc: kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, James Morse , Julien Thierry , Suzuki K Poulose , Catalin Marinas , Will Deacon , Dennis Zhou , Tejun Heo , Christoph Lameter , Mark Rutland , Lorenzo Pieralisi , Quentin Perret , Andrew Scull , kernel-team@android.com Subject: Re: [RFC PATCH 18/26] kvm: arm64: Intercept PSCI_CPU_OFF host SMC calls In-Reply-To: <20201104183630.27513-19-dbrazdil@google.com> References: <20201104183630.27513-1-dbrazdil@google.com> <20201104183630.27513-19-dbrazdil@google.com> User-Agent: Roundcube Webmail/1.4.9 Message-ID: <0ebade5427b2d9a020cd33da64cb9d13@kernel.org> X-Sender: maz@kernel.org X-SA-Exim-Connect-IP: 51.254.78.96 X-SA-Exim-Rcpt-To: dbrazdil@google.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, will@kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, mark.rutland@arm.com, lorenzo.pieralisi@arm.com, qperret@google.com, ascull@google.com, 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2020-11-04 18:36, David Brazdil wrote: > Add a handler of the CPU_OFF PSCI host SMC trapped in KVM nVHE hyp > code. > When invoked, it changes the recorded state of the core to OFF before > forwarding the call to EL3. If the call fails, it changes the state > back > to ON and returns the error to the host. > > Signed-off-by: David Brazdil > --- > arch/arm64/kvm/hyp/nvhe/psci.c | 30 +++++++++++++++++++++++++++++- > 1 file changed, 29 insertions(+), 1 deletion(-) > > diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c > b/arch/arm64/kvm/hyp/nvhe/psci.c > index c3d0a6246c66..00dc0cab860c 100644 > --- a/arch/arm64/kvm/hyp/nvhe/psci.c > +++ b/arch/arm64/kvm/hyp/nvhe/psci.c > @@ -13,6 +13,8 @@ > #include > #include > > +#include > + > /* Config options set by the host. */ > u32 kvm_host_psci_version = PSCI_VERSION(0, 0); > u32 kvm_host_psci_function_id[PSCI_FN_MAX]; > @@ -20,6 +22,7 @@ s64 hyp_physvirt_offset; > > #define __hyp_pa(x) ((phys_addr_t)(x) + hyp_physvirt_offset) > > +static DEFINE_PER_CPU(hyp_spinlock_t, psci_cpu_lock); > DEFINE_PER_CPU(enum kvm_nvhe_psci_state, psci_cpu_state); > > static u64 get_psci_func_id(struct kvm_cpu_context *host_ctxt) > @@ -76,9 +79,32 @@ static __noreturn unsigned long > psci_forward_noreturn(struct kvm_cpu_context *ho > hyp_panic(); /* unreachable */ > } > > +static int psci_cpu_off(u64 func_id, struct kvm_cpu_context > *host_ctxt) > +{ > + hyp_spinlock_t *cpu_lock = this_cpu_ptr(&psci_cpu_lock); > + enum kvm_nvhe_psci_state *cpu_power = this_cpu_ptr(&psci_cpu_state); > + u32 power_state = (u32)host_ctxt->regs.regs[1]; > + int ret; > + > + /* Change the recorded state to OFF before forwarding the call. */ > + hyp_spin_lock(cpu_lock); > + *cpu_power = KVM_NVHE_PSCI_CPU_OFF; > + hyp_spin_unlock(cpu_lock); So at this point, another CPU can observe the vcpu being "off", and issue a PSCI_ON, which may result in an "already on". I'm not sure this is an actual issue, but it is worth documenting. What is definitely missing is a rational about *why* we need to track the state of the vcpus. I naively imagined that we could directly proxy the PSCI calls to EL3, only repainting PC for PSCI_ON. Thanks, M. -- Jazz is not dead. It just smells funny...