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=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL 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 5277BC2D0A3 for ; Mon, 9 Nov 2020 11:33:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 03B012084C for ; Mon, 9 Nov 2020 11:33:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="q9g/yH0W" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729506AbgKILde (ORCPT ); Mon, 9 Nov 2020 06:33:34 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35630 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729650AbgKILdX (ORCPT ); Mon, 9 Nov 2020 06:33:23 -0500 Received: from mail-wm1-x343.google.com (mail-wm1-x343.google.com [IPv6:2a00:1450:4864:20::343]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08FEFC0613D4 for ; Mon, 9 Nov 2020 03:33:22 -0800 (PST) Received: by mail-wm1-x343.google.com with SMTP id h62so7598648wme.3 for ; Mon, 09 Nov 2020 03:33:21 -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=NS+1ZuglZo9Zn7yUnAclLe5esR1R1/5yk9FHSFQ7d8k=; b=q9g/yH0WIaLvx219DqcXDw1PkbjnU9PKjXT0aixjKFwDfVrboe09k14qFs5wzeSKNF jz414gJie7cGqI6l1RvEB3fXslz27Oht8VEEZXRaVSWglOGKUTV41SgHoKBSRvdEnW4h fPWQ2mIpmCCFyeMkXcBodWBvtonybukFkMho8gtFiz2xo6Yh2v0wNIqODsV1Bd90W+V6 qJFDd3M2lmQC6Q8J9RJ6UNKXZ51/wOhOyXnoOAgBEpp1F/ciYNUFSuwpZETWHgIy4vbe gqowPqSa+VdGf44IWcD9TzZ9HOW6Oy9XZsuOdaLKHChTP7J75C7a+tVq7gn6DgFmOQDQ T0TQ== 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=NS+1ZuglZo9Zn7yUnAclLe5esR1R1/5yk9FHSFQ7d8k=; b=ZyhAVlfJz3jXES2xjdPT5VVKysExZCTS/9qlbmes9LJRXp9Q2+PiXuMWpH02UpPkeM M97XsyUov6JTyK8LNn2jRxp/HGRT9+86pahOkeA0G4P+p+FfgYmJDJNitmEaVnsCUhEw 1XQiBocGkCXZH8cDAa+KiBPjKx817GnT/fqGmsUfEZ/JJ9laus3u2XqOoc8+qEsdm+4n Y5Gm81XwYFf3XWELQJl4J/vUOTKO1OKoLO8SKLv4JzMO3DFtCmczry2YoMSNSmdXJw+m pqc4RGWvrTsz0HIkNUhp64y4Ql5Pi8Nx4dAc2pI4DOewUuDA4lIvJhm3mfKWvPENlWOV kCFQ== X-Gm-Message-State: AOAM531nbIj0Pxw0jBl82bTYdDNxD+QLVJBT1k1GrIrKHPCYoRCejhdg pFT5rYLk5a/XxEyY0cYr9949CktOBW60Usic X-Google-Smtp-Source: ABdhPJw+z6E2az4tGXEKJ+aoSJh6Li+u4guKF5JI2ND/5xzgqTGZfkNsdEo5CHTN+G3OtJpOjMODLA== X-Received: by 2002:a1c:6456:: with SMTP id y83mr14727428wmb.59.1604921600616; Mon, 09 Nov 2020 03:33:20 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:209d:10b7:c480:3e1f]) by smtp.gmail.com with ESMTPSA id n128sm5063892wmb.46.2020.11.09.03.33.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Nov 2020 03:33:19 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Marc Zyngier , 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 , Andrew Walbran , kernel-team@android.com, David Brazdil Subject: [PATCH v1 20/24] kvm: arm64: Intercept host's CPU_SUSPEND PSCI SMCs Date: Mon, 9 Nov 2020 11:32:29 +0000 Message-Id: <20201109113233.9012-21-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109113233.9012-1-dbrazdil@google.com> References: <20201109113233.9012-1-dbrazdil@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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.c | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c b/arch/arm64/kvm/hyp/nvhe/psci.c index f9b82a87bf44..ec00036a1613 100644 --- a/arch/arm64/kvm/hyp/nvhe/psci.c +++ b/arch/arm64/kvm/hyp/nvhe/psci.c @@ -127,6 +127,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_symbol(__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]; @@ -180,7 +213,9 @@ asmlinkage void __noreturn kvm_host_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); @@ -204,6 +239,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: -- 2.29.2.222.g5d2a92d10f8-goog 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=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT 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 49755C56201 for ; Mon, 9 Nov 2020 11:33:27 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id B35E12220B for ; Mon, 9 Nov 2020 11:33:26 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="q9g/yH0W" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B35E12220B Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com 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 53C3A4B4E1; Mon, 9 Nov 2020 06:33:26 -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=@google.com 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 K52IQanmWZie; Mon, 9 Nov 2020 06:33:25 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id B695B4B741; Mon, 9 Nov 2020 06:33:24 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 6DC584B310 for ; Mon, 9 Nov 2020 06:33:23 -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 Tt1VTjn+v5BG for ; Mon, 9 Nov 2020 06:33:22 -0500 (EST) Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id D9B934B327 for ; Mon, 9 Nov 2020 06:33:21 -0500 (EST) Received: by mail-wm1-f67.google.com with SMTP id 10so6882641wml.2 for ; Mon, 09 Nov 2020 03:33:21 -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=NS+1ZuglZo9Zn7yUnAclLe5esR1R1/5yk9FHSFQ7d8k=; b=q9g/yH0WIaLvx219DqcXDw1PkbjnU9PKjXT0aixjKFwDfVrboe09k14qFs5wzeSKNF jz414gJie7cGqI6l1RvEB3fXslz27Oht8VEEZXRaVSWglOGKUTV41SgHoKBSRvdEnW4h fPWQ2mIpmCCFyeMkXcBodWBvtonybukFkMho8gtFiz2xo6Yh2v0wNIqODsV1Bd90W+V6 qJFDd3M2lmQC6Q8J9RJ6UNKXZ51/wOhOyXnoOAgBEpp1F/ciYNUFSuwpZETWHgIy4vbe gqowPqSa+VdGf44IWcD9TzZ9HOW6Oy9XZsuOdaLKHChTP7J75C7a+tVq7gn6DgFmOQDQ T0TQ== 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=NS+1ZuglZo9Zn7yUnAclLe5esR1R1/5yk9FHSFQ7d8k=; b=dYW1Lt3Lz1gjfgyfu1F0oYpZDXWryt/pH05SBcpRKhvEcJXCHuwXymcMrB31/zcPZn wUnJOjoiuLS5eoF/cwYiWRQ3Jk937YKJnja07pW2zB6j+6zb4GoVQtJM7zo9UocFov+m g7lMUdjW9I+DrB1YCgNGonpjn3FVnsZnH/v6HPGD5UGxhc3d3d08ZKfV1TJGjw0IyaaD 28DUxjo4MbWOnsXcsf0T6Acnl+dQ+Gn1swt8/CSahOaXubLgeEsUoKsIs3BewyUlLckS WQBTXzkR0Hq57f2P+riz3I1V4hd93f24+P5yNyMRt6o5syHn6SgGSE1JWFAAC824FpHx RpbA== X-Gm-Message-State: AOAM533fBrn1Levr7LHAXrZ0IkGpghkX0WfWYN/VzkQxzRsvqZWl6JUk JD8OMqV/Rl7cN8zuQ5pS3M4ieR2/04TihZhW X-Google-Smtp-Source: ABdhPJw+z6E2az4tGXEKJ+aoSJh6Li+u4guKF5JI2ND/5xzgqTGZfkNsdEo5CHTN+G3OtJpOjMODLA== X-Received: by 2002:a1c:6456:: with SMTP id y83mr14727428wmb.59.1604921600616; Mon, 09 Nov 2020 03:33:20 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:209d:10b7:c480:3e1f]) by smtp.gmail.com with ESMTPSA id n128sm5063892wmb.46.2020.11.09.03.33.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Nov 2020 03:33:19 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [PATCH v1 20/24] kvm: arm64: Intercept host's CPU_SUSPEND PSCI SMCs Date: Mon, 9 Nov 2020 11:32:29 +0000 Message-Id: <20201109113233.9012-21-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109113233.9012-1-dbrazdil@google.com> References: <20201109113233.9012-1-dbrazdil@google.com> MIME-Version: 1.0 Cc: kernel-team@android.com, Lorenzo Pieralisi , Andrew Walbran , Marc Zyngier , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , Will Deacon 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-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu 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.c | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c b/arch/arm64/kvm/hyp/nvhe/psci.c index f9b82a87bf44..ec00036a1613 100644 --- a/arch/arm64/kvm/hyp/nvhe/psci.c +++ b/arch/arm64/kvm/hyp/nvhe/psci.c @@ -127,6 +127,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_symbol(__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]; @@ -180,7 +213,9 @@ asmlinkage void __noreturn kvm_host_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); @@ -204,6 +239,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: -- 2.29.2.222.g5d2a92d10f8-goog _______________________________________________ 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=-12.8 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 94B5EC388F7 for ; Mon, 9 Nov 2020 11:40:20 +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 2A66320731 for ; Mon, 9 Nov 2020 11:40:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="a2vPn/5u"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="q9g/yH0W" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2A66320731 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=4E9+3DVoStq+1xy6wM1KPLT62O1ShEkt5NPav/PvPUU=; b=a2vPn/5u15fL1OMQaKg4jcgPl Iz/jBwR55HqYnZVJv6RPlcj6Eo2ecuVCft/Wd2M9c1ijxnxS6qV0v+qOjxP2AyLJrcPbSle7WoKDx +xAk9kRzF62QGLWlrXxR7g/h/I+T5dJ7H1ja+d8hZuv2o8x9K0LS7CaCufjPlyaOOvEeBJf3TKjZ1 DctKACd7cUu74uqWo1rJKwRX+PjoZjUo9/nvB7lfu13PJu7LPlJI4JuIL54r12YFienAAzlVx/ex+ G/OyZ5t1U1A4ijbK5OR0VPr7i3X1BxloWFTFRaF2yZEtgh2NxTPnTMhfEzFYAscoKSwmCJzNwYzLD 9bVFUNmBw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kc5V8-0006vM-DN; Mon, 09 Nov 2020 11:38:22 +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 1kc5QH-0004Yq-Ve for linux-arm-kernel@lists.infradead.org; Mon, 09 Nov 2020 11:33:35 +0000 Received: by mail-wm1-x344.google.com with SMTP id d142so7644211wmd.4 for ; Mon, 09 Nov 2020 03:33:21 -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=NS+1ZuglZo9Zn7yUnAclLe5esR1R1/5yk9FHSFQ7d8k=; b=q9g/yH0WIaLvx219DqcXDw1PkbjnU9PKjXT0aixjKFwDfVrboe09k14qFs5wzeSKNF jz414gJie7cGqI6l1RvEB3fXslz27Oht8VEEZXRaVSWglOGKUTV41SgHoKBSRvdEnW4h fPWQ2mIpmCCFyeMkXcBodWBvtonybukFkMho8gtFiz2xo6Yh2v0wNIqODsV1Bd90W+V6 qJFDd3M2lmQC6Q8J9RJ6UNKXZ51/wOhOyXnoOAgBEpp1F/ciYNUFSuwpZETWHgIy4vbe gqowPqSa+VdGf44IWcD9TzZ9HOW6Oy9XZsuOdaLKHChTP7J75C7a+tVq7gn6DgFmOQDQ T0TQ== 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=NS+1ZuglZo9Zn7yUnAclLe5esR1R1/5yk9FHSFQ7d8k=; b=kXYEnB7RudSoNdSjPQxDgNeDYkIlR9xfAM3Xd86OijN5JXxOUT97IRTMcwapV3x7F4 fmpQTBHuhQhactbENjjZjfoQL95uEeEERUQ/htbUkxFu28CwzlT+nL/PHisH0QlJu+oM wz4rm4gbjSBe7BFKegNeEf5py+FNsaFQYfXylM+jAyghkLPAFGmgozCfywg//HWzCHSr U9gMdg0wKd0w9XLrt9YLI9hmKpgggKMDBGxeQDwxSY1FSi6KqTW5stciAWipiIoyconb P+zu3E0yOsmLwhT4n7e0b+sMNqfgOmrqXWt46imsw2Kii7TWvDePr9ohIWghcA5bEEe8 4BZQ== X-Gm-Message-State: AOAM5304eAQJj2pvjI44VE5KSrhZ9Es6dOw2enAOO2fRjow4IRv6BAQQ qb8M+FZQtpv/5LnOpyuKRQ+vaQ== X-Google-Smtp-Source: ABdhPJw+z6E2az4tGXEKJ+aoSJh6Li+u4guKF5JI2ND/5xzgqTGZfkNsdEo5CHTN+G3OtJpOjMODLA== X-Received: by 2002:a1c:6456:: with SMTP id y83mr14727428wmb.59.1604921600616; Mon, 09 Nov 2020 03:33:20 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:209d:10b7:c480:3e1f]) by smtp.gmail.com with ESMTPSA id n128sm5063892wmb.46.2020.11.09.03.33.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Nov 2020 03:33:19 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [PATCH v1 20/24] kvm: arm64: Intercept host's CPU_SUSPEND PSCI SMCs Date: Mon, 9 Nov 2020 11:32:29 +0000 Message-Id: <20201109113233.9012-21-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109113233.9012-1-dbrazdil@google.com> References: <20201109113233.9012-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201109_063322_315228_8C02ECCB X-CRM114-Status: GOOD ( 22.66 ) 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 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 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.c | 39 +++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/psci.c b/arch/arm64/kvm/hyp/nvhe/psci.c index f9b82a87bf44..ec00036a1613 100644 --- a/arch/arm64/kvm/hyp/nvhe/psci.c +++ b/arch/arm64/kvm/hyp/nvhe/psci.c @@ -127,6 +127,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_symbol(__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]; @@ -180,7 +213,9 @@ asmlinkage void __noreturn kvm_host_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); @@ -204,6 +239,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: -- 2.29.2.222.g5d2a92d10f8-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel