Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9241000pxu; Mon, 28 Dec 2020 10:11:17 -0800 (PST) X-Google-Smtp-Source: ABdhPJw8zsAE9zBRPZ/6MlqyrLT5oyqEpfFKuuUIEP68Qppif0d8jqgdhp8M3OkGga3K4eB+B1Bi X-Received: by 2002:a17:906:e15:: with SMTP id l21mr42804321eji.509.1609179077394; Mon, 28 Dec 2020 10:11:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609179077; cv=none; d=google.com; s=arc-20160816; b=MYhTNKFxBax/GqasCK8PzDTN+5F/ohhDPhsfRsK7/+le83T73TKDWtT9/oBTgEdVva ffGshuVhYRJGnyXmFRzkPll/7Ui0FSmIakk3+pCljhr3NdTq07b9y1tsSvFL6BXwAibD IEzDup0RP9T7C5+9BVS3RHV32jtR9fColwSS8be36RnZ3mR4XdEGkZSdX3dGh8KlFj6p j79sHLyvMjiq0Gh86j119KBddTBaUe/+s1dIzbEZuib0IsKs/xTnPqnZHXoju8XL2IpA 3pE8kn53URJWTSa9uPU5IqeAuZzapZkBJ9QZbRpVzvHDSdd+F6lJ3R9kl4F1FEVNNykh 2gXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Z3Uq6oakSrJuLPLQN+iiAFCPfGiBNFlImF4dQWxiul0=; b=Gc8AlIu0S1QRKJC2R7TJYaLGGvsoTaOHJ3+xx4rk1anSAyzAU6gYfco6YHDKneZrAW WazbRmGdFZa2iGGF8V4jlG5Hn9MLYUbV0Dqe+FubP+uvlgoSjkNn0S4wysvJlkJ3PaFs oJiWlAzgGW48zurh8aJOKpFTQBOsWg13DJG4IjZG0KynoOPQ31enxEMw8WbKrxLkG5NF 0ergOOILm+uS2RV7MDcy8VEYQa/TXzAdgs4CG1QNifmDgMOuZfSENU+xWK8BtCK8+Eh4 eH+5VhOqOkb3/QLZNdOqdDIJZ166JSh5W7kmV7njq9M6dNDkjaTBE6i23NW/gtpJN48C ETEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YfLvwkYh; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p11si18639649eja.213.2020.12.28.10.10.54; Mon, 28 Dec 2020 10:11:17 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=YfLvwkYh; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388898AbgL1NZY (ORCPT + 99 others); Mon, 28 Dec 2020 08:25:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:53452 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388845AbgL1NZH (ORCPT ); Mon, 28 Dec 2020 08:25:07 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id C1C8422583; Mon, 28 Dec 2020 13:24:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609161891; bh=tsuIKlJOYDOke22E4Wgh0XlqUajb5Akxd2H2sT7+L9E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YfLvwkYheve/hUdPfVL7bNmlYY9kM8f4IAJSnz0vJTdxHnUM9mQhi05SWs3HqbRyF SD4SS7baXv1/EK89aC+5nLMuqZQpXdJxjTarcMgtPaHQFbIfMW3uh7e7A7Y2YAi9Jj N3SEfNc/PWKczbZvbl+0E9dFlQdWPgJH+k6/lhhQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jordan Niethe , Michael Ellerman , Sasha Levin Subject: [PATCH 4.19 116/346] powerpc/64: Set up a kernel stack for secondaries before cpu_restore() Date: Mon, 28 Dec 2020 13:47:15 +0100 Message-Id: <20201228124925.399049685@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228124919.745526410@linuxfoundation.org> References: <20201228124919.745526410@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jordan Niethe [ Upstream commit 3c0b976bf20d236c57adcefa80f86a0a1d737727 ] Currently in generic_secondary_smp_init(), cur_cpu_spec->cpu_restore() is called before a stack has been set up in r1. This was previously fine as the cpu_restore() functions were implemented in assembly and did not use a stack. However commit 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features") used __restore_cpu_cpufeatures() as the cpu_restore() function for a device-tree features based cputable entry. This is a C function and hence uses a stack in r1. generic_secondary_smp_init() is entered on the secondary cpus via the primary cpu using the OPAL call opal_start_cpu(). In OPAL, each hardware thread has its own stack. The OPAL call is ran in the primary's hardware thread. During the call, a job is scheduled on a secondary cpu that will start executing at the address of generic_secondary_smp_init(). Hence the value that will be left in r1 when the secondary cpu enters the kernel is part of that secondary cpu's individual OPAL stack. This means that __restore_cpu_cpufeatures() will write to that OPAL stack. This is not horribly bad as each hardware thread has its own stack and the call that enters the kernel from OPAL never returns, but it is still wrong and should be corrected. Create the temp kernel stack before calling cpu_restore(). As noted by mpe, for a kexec boot, the secondary CPUs are released from the spin loop at address 0x60 by smp_release_cpus() and then jump to generic_secondary_smp_init(). The call to smp_release_cpus() is in setup_arch(), and it comes before the call to emergency_stack_init(). emergency_stack_init() allocates an emergency stack in the PACA for each CPU. This address in the PACA is what is used to set up the temp kernel stack in generic_secondary_smp_init(). Move releasing the secondary CPUs to after the PACAs have been allocated an emergency stack, otherwise the PACA stack pointer will contain garbage and hence the temp kernel stack created from it will be broken. Fixes: 5a61ef74f269 ("powerpc/64s: Support new device tree binding for discovering CPU features") Signed-off-by: Jordan Niethe Signed-off-by: Michael Ellerman Link: https://lore.kernel.org/r/20201014072837.24539-1-jniethe5@gmail.com Signed-off-by: Sasha Levin --- arch/powerpc/kernel/head_64.S | 8 ++++---- arch/powerpc/kernel/setup-common.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/powerpc/kernel/head_64.S b/arch/powerpc/kernel/head_64.S index 389da790c1296..4f7b225d78cff 100644 --- a/arch/powerpc/kernel/head_64.S +++ b/arch/powerpc/kernel/head_64.S @@ -423,6 +423,10 @@ generic_secondary_common_init: /* From now on, r24 is expected to be logical cpuid */ mr r24,r5 + /* Create a temp kernel stack for use before relocation is on. */ + ld r1,PACAEMERGSP(r13) + subi r1,r1,STACK_FRAME_OVERHEAD + /* See if we need to call a cpu state restore handler */ LOAD_REG_ADDR(r23, cur_cpu_spec) ld r23,0(r23) @@ -451,10 +455,6 @@ generic_secondary_common_init: sync /* order paca.run and cur_cpu_spec */ isync /* In case code patching happened */ - /* Create a temp kernel stack for use before relocation is on. */ - ld r1,PACAEMERGSP(r13) - subi r1,r1,STACK_FRAME_OVERHEAD - b __secondary_start #endif /* SMP */ diff --git a/arch/powerpc/kernel/setup-common.c b/arch/powerpc/kernel/setup-common.c index 508244bcf19c2..7787a26d47777 100644 --- a/arch/powerpc/kernel/setup-common.c +++ b/arch/powerpc/kernel/setup-common.c @@ -929,8 +929,6 @@ void __init setup_arch(char **cmdline_p) /* On BookE, setup per-core TLB data structures. */ setup_tlb_core_data(); - - smp_release_cpus(); #endif /* Print various info about the machine that has been gathered so far. */ @@ -964,6 +962,8 @@ void __init setup_arch(char **cmdline_p) exc_lvl_early_init(); emergency_stack_init(); + smp_release_cpus(); + initmem_init(); #ifdef CONFIG_DUMMY_CONSOLE -- 2.27.0