From patchwork Mon Nov 16 20:43:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Brazdil X-Patchwork-Id: 11910859 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=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT 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 3D006C3E8C5 for ; Mon, 16 Nov 2020 21:12:43 +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 CC0C620888 for ; Mon, 16 Nov 2020 21:12:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="p8iXrXdb"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="CePHDUN1" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CC0C620888 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com 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-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=x2wjHktK2GPxf3VIrXHRBgfsESh5ufbdE0+f6pzSZHA=; b=p8iXrXdb+psO9DImAfgQ7jA67 fLfXTV2X3G8ezdoj6QqVhgz01wdzD5ryPrDgoxfg8G+kbXqJHJeHEgoIFG/3fAVytC4jcS0VgGiFB t4Js/k4O1JRkn5iJgWp5ALSNqmGPuxDNWEjj4oczIngGOLOjiTJ6KBfCf/fGlc+d505Q5jBiPN/Ni IZ6jodbGmC+eJPBYNWD+yGScie9ahraaTyJu5IVajgXmbxxXeupdaYL+JqCRqME4UQWKWrqf+YdVN H/o3Fkv3xTmtHVnYlid4MVbHxRyndotfj5nXF4kkbhwrV+Vw0Zsm6kKm/iRoLe4Knx3GCxlqUpDi5 Oi7Th5sIQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kelQ6-0001Ra-3l; Mon, 16 Nov 2020 20:48:14 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kelM6-0007qL-Qh for linux-arm-kernel@lists.infradead.org; Mon, 16 Nov 2020 20:44:11 +0000 Received: by mail-wm1-x344.google.com with SMTP id d142so590669wmd.4 for ; Mon, 16 Nov 2020 12:44:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wD+a3FpezM46vtDRvUYZ5HtVvRxQTV1ZGJA/wg4Ex98=; b=CePHDUN1veATU5Fbosl/2glcCRr5WzC8phqSaumNvMV+29qseMvtZ5AYULHe86VL0Y Fl0rKdltr3pyBfvDMv2DYbtiug1MoFDP0prQEQ8QkT6PbcqArN6MEiHuSA3PxAxCk7IY URLQBAJTWMpZJ1b72mXGCIC5AicuMS45rYFWJQuTvwx26dLIvMvieTi/iK/y+VGo+a03 cbD5e/0T+nHVCXYB+UkwBwooP+59t6Xv3IvoPW5qLJMcW6BcLhslZAIzfLgjirBkAjGz dW4LjnXf2FVaT6dljBPU4tSL0J8oSTM4rgT+xYCiRsYqLo6+tciGHlUB1LqJbaHGdTE0 +A1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wD+a3FpezM46vtDRvUYZ5HtVvRxQTV1ZGJA/wg4Ex98=; b=m1zeDXAFkhD9Ypko37n2FLCqTp8cV96GfGkrrtHWAAnOobJKoYyCnQT1FpUV+U8uNn gR2bPyt8fM34nefEJ/+aOMr6lKh4ZW9/hNhIvG13i2s3hhA++bu5r097e1JNLKYD3BwM RBjAm7xWvI1BCVQPcC8pis59yi9qAbhCYihC2q9SAKLe5cxZuspKTgJKzvc3Ns9kLg1H 6h90DB1HzQ/+J1mfByh5v1AZa6cXNnGQcT5aoe4V5V5WfPOkcrIr81hWpSRGNHvKnq7a GBGzIAQP9Y9FWcMWi2+WjcJGFa7zc+NKEV3T/7mJW8GZJn0pH8qopo0+uBoTWEEKQrIL u2CQ== X-Gm-Message-State: AOAM530hUg9i///6EHnULSycrQFzpMLb/uBsnV7jsiVwwkfILeN+xM8a 82YBfmdm5PwqnA/MpncVRwh+EmhLiQqthMkOKgQ= X-Google-Smtp-Source: ABdhPJzetTRH/cFVZbrbGHGqSRHTJfKqrhpUhK6CneVfklDTfCbBbV/l/O0oOWso6KYVg8ahVpsLgQ== X-Received: by 2002:a05:600c:4147:: with SMTP id h7mr713498wmm.186.1605559445403; Mon, 16 Nov 2020 12:44:05 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:bc40:bd71:373a:1b33]) by smtp.gmail.com with ESMTPSA id t5sm565383wmg.19.2020.11.16.12.44.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 16 Nov 2020 12:44:04 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [PATCH v2 20/24] kvm: arm64: Intercept host's CPU_SUSPEND PSCI SMCs Date: Mon, 16 Nov 2020 20:43:14 +0000 Message-Id: <20201116204318.63987-21-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201116204318.63987-1-dbrazdil@google.com> References: <20201116204318.63987-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201116_154407_029307_4C8DEF8A X-CRM114-Status: GOOD ( 22.60 ) 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 , kernel-team@android.com, Lorenzo Pieralisi , Andrew Walbran , Suzuki K Poulose , Marc Zyngier , Quentin Perret , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , David Brazdil , Will Deacon , Julien Thierry , Andrew Scull Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a handler of CPU_SUSPEND host PSCI SMCs. The SMC can either enter a sleep state indistinguishable from a WFI or a deeper sleep state that behaves like a CPU_OFF+CPU_ON. The handler saves r0,pc of the host and makes the same call to EL3 with the hyp CPU entry point. It either returns back to the handler and then back to the host, or wakes up into the entry point and initializes EL2 state before dropping back to EL1. There is a simple atomic lock around the reset state struct to protect from races with CPU_ON. A well-behaved host should never run CPU_ON against an already online core, and the kernel indeed does not allow that, so if the core sees its reset state struct locked, it will return a non-spec error code PENDING_ON. This protects the hypervisor state and avoids the need for more complicated locking and/or tracking power state of individual cores. Signed-off-by: David Brazdil --- arch/arm64/kvm/hyp/nvhe/psci-relay.c | 39 +++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/psci-relay.c b/arch/arm64/kvm/hyp/nvhe/psci-relay.c index 2daf52b59846..313ef42f0eab 100644 --- a/arch/arm64/kvm/hyp/nvhe/psci-relay.c +++ b/arch/arm64/kvm/hyp/nvhe/psci-relay.c @@ -121,6 +121,39 @@ static void release_reset_state(struct kvm_host_psci_state *cpu_state) atomic_set_release(&cpu_state->pending_on, 0); } +static int psci_cpu_suspend(u64 func_id, struct kvm_cpu_context *host_ctxt) +{ + u64 power_state = host_ctxt->regs.regs[1]; + unsigned long pc = host_ctxt->regs.regs[2]; + unsigned long r0 = host_ctxt->regs.regs[3]; + struct kvm_host_psci_state *cpu_state; + struct kvm_nvhe_init_params *cpu_params; + int ret; + + cpu_state = this_cpu_ptr(&kvm_host_psci_state); + cpu_params = this_cpu_ptr(&kvm_init_params); + + /* + * Lock the reset state struct. This fails if the host has concurrently + * called CPU_ON with this CPU as target. The kernel keeps track of + * online CPUs, so that should never happen. If it does anyway, return + * a non-spec error. This avoids the need for spinlocks. + */ + if (!try_acquire_reset_state(cpu_state, pc, r0)) + return PSCI_RET_ALREADY_ON; + + /* + * Will either return if shallow sleep state, or wake up into the entry + * point if it is a deep sleep state. + */ + ret = psci_call(func_id, power_state, + __hyp_pa(hyp_symbol_addr(__kvm_hyp_cpu_entry)), + __hyp_pa(cpu_params)); + + release_reset_state(cpu_state); + return ret; +} + static int psci_cpu_on(u64 func_id, struct kvm_cpu_context *host_ctxt) { u64 mpidr = host_ctxt->regs.regs[1]; @@ -178,7 +211,9 @@ asmlinkage void __noreturn __kvm_hyp_psci_cpu_entry(void) static unsigned long psci_0_1_handler(u64 func_id, struct kvm_cpu_context *host_ctxt) { - if (func_id == kvm_host_psci_function_id[PSCI_FN_CPU_OFF]) + if (func_id == kvm_host_psci_function_id[PSCI_FN_CPU_SUSPEND]) + return psci_cpu_suspend(func_id, host_ctxt); + else if (func_id == kvm_host_psci_function_id[PSCI_FN_CPU_OFF]) return psci_forward(host_ctxt); else if (func_id == kvm_host_psci_function_id[PSCI_FN_CPU_ON]) return psci_cpu_on(func_id, host_ctxt); @@ -202,6 +237,8 @@ static unsigned long psci_0_2_handler(u64 func_id, struct kvm_cpu_context *host_ case PSCI_0_2_FN_SYSTEM_RESET: psci_forward_noreturn(host_ctxt); unreachable(); + case PSCI_0_2_FN64_CPU_SUSPEND: + return psci_cpu_suspend(func_id, host_ctxt); case PSCI_0_2_FN64_CPU_ON: return psci_cpu_on(func_id, host_ctxt); default: