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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFEA8C47258 for ; Tue, 23 Jan 2024 07:34:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A4CC46B0085; Tue, 23 Jan 2024 02:34:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9383B6B0087; Tue, 23 Jan 2024 02:34:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73BA86B0089; Tue, 23 Jan 2024 02:34:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5F0986B0085 for ; Tue, 23 Jan 2024 02:34:47 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 425D81A037E for ; Tue, 23 Jan 2024 07:34:47 +0000 (UTC) X-FDA: 81709763814.17.675AA5D Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by imf03.hostedemail.com (Postfix) with ESMTP id 7C01C20009 for ; Tue, 23 Jan 2024 07:34:43 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of huangpei@loongson.cn designates 114.242.206.163 as permitted sender) smtp.mailfrom=huangpei@loongson.cn; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705995285; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Lm94j0c1aYyC4JtjDfGE4fjGGX65r8/rPv/JefWXCJM=; b=P3oUxsbYxkbn/c6iN8AG5D/cRMZ48Z8fYzTGSuKIcJGeC2DRj5mPdRtJN4Hbe5xkySCXDR uYux8K49f7QheVzAKARFnT8frrWqX22KkmPOsrFjhWTWUStDCPmY2U91EcgvRQOa+JJI8/ nN0qPan7CvIJv5t79hhTYF7jnG7jVZQ= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; spf=pass (imf03.hostedemail.com: domain of huangpei@loongson.cn designates 114.242.206.163 as permitted sender) smtp.mailfrom=huangpei@loongson.cn; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705995285; a=rsa-sha256; cv=none; b=MHCPmgVR4A6xnNPETYauPxOXxVIK8xE6WlBwZXFGvCQj1/Ehwf7epm+R9emWzlaaFhgYpe pbPfQuRwwics4MpdZFyIo7n1kn1aOi4wo46pWbedgACEhWp3XI+ebvjN/T4su/xiI3FSqP ZAxdgJQ3UAmQQVG6Jzvx2LQVMwf8ocA= Received: from loongson.cn (unknown [111.9.175.10]) by gateway (Coremail) with SMTP id _____8BxVfGthadlORUBAA--.5685S3; Wed, 17 Jan 2024 15:45:49 +0800 (CST) Received: from Board-3A3000 (unknown [111.9.175.10]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxF82qhadlsg8GAA--.676S2; Wed, 17 Jan 2024 15:45:48 +0800 (CST) Date: Wed, 17 Jan 2024 15:45:46 +0800 From: Huang Pei To: Mike Rapoport Cc: Yajun Deng , Jiaxun Yang , linux-mm@kvack.org, Bibo Mao , linux-mips@vger.kernel.org, Paul Burton , Li Xuefeng , Yang Tiezhu , Gao Juxin , Huacai Chen , Thomas Bogendoerfer , linux-kernel@vger.kernel.org Subject: Re: memblock_reserve for unadded region (was: [PATCH] MIPS: loongson64: fix boot failure) Message-ID: <20240117074546.gm7lejjt7whjtwgr@Board-3A3000> References: <20231225093025.23215-1-huangpei@loongson.cn> <731134fd-4b3d-418c-84ee-80646bffcc01@flygoat.com> <20240116122304.qwzy7san2vgspt2x@Board-3A3000> <3fc2f75e-d163-1ad1-009a-0e4538011885@linux.dev> <20240117030113.gs2fjs6vydthsc6l@Board-3A3000> <234ddaeb-5988-858d-6ba1-0fef90fb7a87@linux.dev> <20240117035910.453ofvotbvkzje6k@Board-3A3000> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-CM-TRANSID:AQAAf8BxF82qhadlsg8GAA--.676S2 X-CM-SenderInfo: xkxd0whshlqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWfJr18XFyDur1kGr15Xr4kXwc_yoWDAr17pr 1fJF17CrW8Jr18Jr4Utr15Gr1jyw1Yk3WUXr1DJr18Xr1qkr13Xr18Xr10gryDJrW8JF12 qr1UJr12vw1UAwcCm3ZEXasCq-sJn29KB7ZKAUJUUUU5529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUUk0b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Jr0_JF4l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12xvs2x26I8E6xACxx 1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1Y6r17McIj6I8E87Iv 67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IY64vIr41l42xK82IYc2Ij64 vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8G jcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2I x0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK 8VAvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I 0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUcVc_UUUUU X-Gw-Check: 325535f2152225ab X-Rspamd-Queue-Id: 7C01C20009 X-Rspam-User: X-Stat-Signature: 4wju7w5fyoeeny1fsys48zh8t19ssq7w X-Rspamd-Server: rspam01 X-HE-Tag: 1705995283-932624 X-HE-Meta: U2FsdGVkX191AdEXyvjF37KnGiHrRryslHi6ZfGCAdyhaPUrWIDYhfwGuldPPFNJTzXJke8DV9mOkTjAjsA3juVuWIGttF+wIndvBRqGVtWnn88xbsNRx9ptD6PMZDPmNwMgMQIUJLSms2cN+P8hoLad7ltcrypZMg7J/LvaeVN6C94oelJBSP+TPLhFIFWD/VElvOIP459nIMnwq1lD2YpYZ2hbS+4/rnTuGEbs0lSpKY98F0F8+BwROvCPi7j98B9ZVqv22z0eu37LzsfStoq3fSVNBlTZX8v7bmwZ7nC/jpsaEP06a0dyv5Vpsfpu4hiGuV0je/8HDK1ucvnHKcC1e2vw8sPNHrlVHDcE96JhDrc9WmN7HDALeEsgxjcG6GqWUwQoUgrO/tTICGyCuRax/buYfQtVDHfswJSx9ZHsh4KUeUivAfuqTgH5H92nfywOHmdJ272jwv6d0GMp0DTMU2P7WWijK+kYklEEcnj4csme2FwiuEjxF3U/ft3FbyT+IBOC0zLwaugcAcuT4sy6shBzJmxKKN327AD5QuI/pIhFgiqU82gZ/cYLl+k5PiXQvzEsCb9E8+9FXeQXS7ZR9uHVPTRu6LDnbnXhChu+nQoqrOoRLQHF6poNQuKMQlO7JafVq6yqeF0kcXhFCHDuQ1fwRD1YXaRZHz29zi8F1jjzTK5x2nTAV/pyeiDtVJQkbI3DhxNJrtW7tiWJsaQvmxNMaloIaBCCY3VA/2bEEcOFecOGIqScXoGhLDAZQY+02FDzhuW/t/M3gPzEzLN02sHvwSCNQzvivZfWAfBUdn9cfbmc63rCOblcsJpsoNR6gm0AhPbadFV+3p0/AD8Be90iNwpzPYTK4cND/GaGrmTOgXCk3SyhYCTHp92HKyrwOHJbPTZL18KlP2166tKJYmQcxFSnm+8onIYzgCOxWPfBXEQIfBjIS1soqf9mSXfPzEKCyenLjuZxle8 Pw5drl84 R6asLhIWqqXjMrkOg9VB7ZUgLT/nge7uLchUHpSu7axPd2MEFhZJ0X6ePVzBj8TU1rMYto6E5Wu05YkI7DzrpUIM7cswGErrmnn9FT6dm2/3Z8oKdRcGBm2I6kTLnF/wN/9wMD0WojvfEtUF3tRV5ccc500ypH8+Aui7yA92Ozs88C2ASCGVkqfJ0sw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Wed, Jan 17, 2024 at 08:46:34AM +0200, Mike Rapoport wrote: > On Wed, Jan 17, 2024 at 11:59:10AM +0800, Huang Pei wrote: > > On Wed, Jan 17, 2024 at 11:17:18AM +0800, Yajun Deng wrote: > > > > > > On 2024/1/17 11:01, Huang Pei wrote: > > > > On Wed, Jan 17, 2024 at 10:20:00AM +0800, Yajun Deng wrote: > > > > > On 2024/1/16 20:23, Huang Pei wrote: > > > > > > On Tue, Jan 16, 2024 at 10:39:04AM +0200, Mike Rapoport wrote: > > > > > > > On Mon, Jan 15, 2024 at 02:08:21PM +0000, Jiaxun Yang wrote: > > > > > > > > Hi mm folks, > > > > > > > > > > > > > > > > Just a quick question, what is the expected behavior of memblock_reserve > > > > > > > > a region that is not added to memblock with memblock_add? > > > > > > > > > > > > > > > > I'm unable to find any documentation about memblock_reserve in comments and > > > > > > > > boot-time-mm, but as per my understanding to the code, this should be a > > > > > > > > legit usage? > > > > > > > Yes, memblock allows reserving memory that was not added to memblock with > > > > > > > memblock_add(). > > > > > > I think arch/platform specific code should fix this bug, like, > > > > > > -------------------------------------------------------------------------- > > > > > > //for loongson64 > > > > > > memblock_set_node(0, 1ULL << 44, &memblock.reserved, 0); > > > > > > > > > > > > -------------------------------------------------------------------------- > > > > > > > > > > > > or maybe memblock provide something like memblock_reserve_node > > > > > Hi pei, > > > > > > > > > > Can you test the following patch to see if it fixes this bug? > > > > > > > > > > diff --git a/mm/mm_init.c b/mm/mm_init.c > > > > > index 2c19f5515e36..97721d99fdce 100644 > > > > > --- a/mm/mm_init.c > > > > > +++ b/mm/mm_init.c > > > > > @@ -708,6 +708,9 @@ static void __meminit init_reserved_page(unsigned long > > > > > pfn, int nid) > > > > >         pg_data_t *pgdat; > > > > >         int zid; > > > > > > > > > > +       if (unlikely(nid == NUMA_NO_NODE || nid >= MAX_NUMNODES)) > > > > > +               nid = early_pfn_to_nid(pfn); > > > > > + > > > > >         if (early_page_initialised(pfn, nid)) > > > > >                 return; > > IMO this will fix the bug. Before 61167ad5fecd we had nid = first_online_node > for reserved pages that didn't have nid set in memblock.reserved. After > 61167ad5fecd we try to initialize these pages with MAX_NUMNODES and > obviously crash when accessing node structure. > > I think that the check for a valid nid should be moved to > memmap_init_reserved_pages() though. An entire reserved region will have > nid set to MAX_NUMNODES, so there is no point to check every page in it. > > > > > I do not think this fix set the right nid, ONLY arch/platform know that > > > > nid > > Why does it matter to have the right nid in a reserved page that is not > part of usable memory? > IMO, if a reserved page DO have a valid nid, and archs knows that, archs should set it right, and this is just the case of loongson64(and loongarch). > That's true that only arch knows on which node those reserved pages are, > but core mm does not care about reserved pages that are not in memory. > > > > > int __meminit early_pfn_to_nid(unsigned long pfn) > > > > { > > > > static DEFINE_SPINLOCK(early_pfn_lock); > > > > int nid; > > > > > > > > spin_lock(&early_pfn_lock); > > > > nid = __early_pfn_to_nid(pfn, > > > > &early_pfnnid_cache); > > > > if (nid < 0) > > > > //!!!first_online_node MAY NOT be the node the pfn belong to!!! > > > > nid = first_online_node; > > > > > > > > spin_unlock(&early_pfn_lock); > > > > > > > > return > > > > nid; > > > > } > > > > > > > > > Okay, I don't think this bug is caused by commit 61167ad5fecd ("mm: pass nid > > > to reserve_bootmem_region()"), > > > > > > because even if you revert this commit, it will still get nid by > > > early_pfn_to_nid(). Did I get that right? > > > > Yes, more accurately, this bug is exposed by commit 61167ad5fecd. My > > previous fix is based on presumptions that memory_reserve should reserve memory > > added by memblock_add{,_node}, if going across this limitation, there need > > to set the valid nid for reserved memory region. > > > > > > > > > > > > > > > > In practical we run into uninitialized nid of reserved block problem, should > > > > > > > > we fix it > > > > > > > > in our usage, or on memblock side? > > > > > > > Apparently it's a bug in memblock :( > > > > > > > > > > > > > > If you revert 61167ad5fecd ("mm: pass nid to reserve_bootmem_region()") > > > > > > > does the issue disappear? > > > > > > Yes, I git bisect this commit. > > > > > > > > > > > > But I don't think it is a bug in memblock. IMO, memblock_reserve under > > > > > > NUMA set nid of reserved region to MAX_NUMNODES, which is the point > > > > > > that cause the "memblock_get_region_node from memmap_init_reserved_pages " > > > > > > passing a invalid node id(aka MAX_NUMNODES) to "reserver_bootmem_region > > > > > > -> init_reserved_page -> early_pfn_to_nid". If arch-specific code DOES NOT > > > > > > initialize the nid of reserved region(only it know that), or the reserved > > > > > > region NOT within a memblock added by memblock_add, memblock can not > > > > > > give a valid node id to the reserved region. Commit 61167ad5fecd ("mm: pass nid to > > > > > > reserve_bootmem_region()") just reveals the embarrassment case by an > > > > > > out of bound memory access. > > > > > > > > > > > > > > Thanks > > > > > > > > > > > > > > > > 在 2023/12/25 09:30, Huang Pei 写道: > > > > > > > > > Since commit 61167ad5fecd("mm: pass nid to reserve_bootmem_region()), > > > > > > > > > loongson64 booting failed with CONFIG_DEFERRED_STRUCT_PAGE_INIT like > > > > > > > > > this: > > > > > > > > > ---------------------------------------------------------------------- > > > > > > > > > Call Trace: > > > > > > > > > [] reserve_bootmem_region+0xa8/0x184 > > > > > > > > > [] memblock_free_all+0x104/0x2a8 > > > > > > > > > [] mem_init+0x84/0x94 > > > > > > > > > [] mm_core_init+0xf8/0x308 > > > > > > > > > [] start_kernel+0x43c/0x86c > > > > > > > > > > > > > > > > > > Code: 10400028 2402fff0 de420000 0203182b 14600022 > > > > > > > > > 64420070 00003025 24040003 > > > > > > > > > > > > > > > > > > ---[ end trace 0000000000000000 ]--- > > > > > > > > > Kernel panic - not syncing: Attempted to kill the idle task! > > > > > > > > > ---[ end Kernel panic - not syncing: Attempted to kill the idle task! ]--- > > > > > > > > > ---------------------------------------------------------------------- > > > > > > > > > > > > > > > > > > The root cause is no memory region "0x0-0x1fffff" paired with > > > > > > > > > memory-reserved region "0x0-0x1fffff" and "0x0-0xfff", with "memblock > > > > > > > > > =debug": > > > > > > > > > > > > > > > > > > ---------------------------------------------------------------------- > > > > > > > > > memory[0x0] [0x0000000000200000-0x000000000effffff], > > > > > > > > > 0x000000000ee00000 bytes on node 0 flags: 0x0 !!!!here > > > > > > > > > memory[0x1] [0x0000000090000000-0x00000000fdffffff], > > > > > > > > > 0x000000006e000000 bytes on node 0 flags: 0x0 > > > > > > > > > memory[0x2] [0x0000000100000000-0x000000027fffffff], > > > > > > > > > 0x0000000180000000 bytes on node 0 flags: 0x0 > > > > > > > > > memory[0x3] [0x0000100000000000-0x000010000fffffff], > > > > > > > > > 0x0000000010000000 bytes on node 1 flags: 0x0 > > > > > > > > > memory[0x4] [0x0000100090000000-0x000010027fffffff], > > > > > > > > > 0x00000001f0000000 bytes on node 1 flags: 0x0 > > > > > > > > > reserved.cnt = 0x1f > > > > > > > > > reserved[0x0] [0x0000000000000000-0x000000000190c80a], > > > > > > > > > 0x000000000190c80b bytes flags: 0x0 !!!!oops 0x0-0x1fffff not in memory[0] > > > > > > > > > reserved[0x1] [0x000000000190c810-0x000000000190eea3], > > > > > > > > > 0x0000000000002694 bytes flags: 0x0 > > > > > > > > > ---------------------------------------------------------------------- > > > > > > > > > > > > > > > > > > It caused memory-reserved region "0x0-0x1fffff" without valid node id > > > > > > > > > in "memblock_get_region_node" from "memmap_init_reserved_pages", lead to > > > > > > > > > "reserve_bootmem_region-> init_reserved_page -> early_pfn_to_nid()" > > > > > > > > > accessing "node_data" out of bound. > > > > > > > > > > > > > > > > > > To fix this bug, we should remove unnecessary memory block reservation. > > > > > > > > > > > > > > > > > > +. no need to reserve 0x0-0x1fffff below kernel loading address, since > > > > > > > > > it is not registered by "memblock_add_node" > > > > > > > > > > > > > > > > > > +. no need to reserve 0x0-0xfff for exception handling if it is not > > > > > > > > > registered by "memblock_add" either. > > > > > > > > > > > > > > > > > > Fixes: commit 61167ad5fecd("mm: pass nid to reserve_bootmem_region()) > > > > > > > > > Signed-off-by: Huang Pei > > > > > > > > > --- > > > > > > > > > arch/mips/kernel/traps.c | 3 ++- > > > > > > > > > arch/mips/loongson64/numa.c | 2 -- > > > > > > > > > 2 files changed, 2 insertions(+), 3 deletions(-) > > > > > > > > > > > > > > > > > > diff --git a/arch/mips/kernel/traps.c b/arch/mips/kernel/traps.c > > > > > > > > > index 246c6a6b0261..9b632b4c10c3 100644 > > > > > > > > > --- a/arch/mips/kernel/traps.c > > > > > > > > > +++ b/arch/mips/kernel/traps.c > > > > > > > > > @@ -2007,7 +2007,8 @@ unsigned long vi_handlers[64]; > > > > > > > > > void reserve_exception_space(phys_addr_t addr, unsigned long size) > > > > > > > > > { > > > > > > > > > - memblock_reserve(addr, size); > > > > > > > > > + if(memblock_is_region_memory(addr, size)) > > > > > > > > > + memblock_reserve(addr, size); > > > > > > > > > } > > > > > > > > > void __init *set_except_vector(int n, void *addr) > > > > > > > > > diff --git a/arch/mips/loongson64/numa.c b/arch/mips/loongson64/numa.c > > > > > > > > > index 8f61e93c0c5b..0f516dde81da 100644 > > > > > > > > > --- a/arch/mips/loongson64/numa.c > > > > > > > > > +++ b/arch/mips/loongson64/numa.c > > > > > > > > > @@ -130,8 +130,6 @@ static void __init node_mem_init(unsigned int node) > > > > > > > > > memblock_reserve((node_addrspace_offset | 0xfe000000), > > > > > > > > > 32 << 20); > > > > > > > > > - /* Reserve pfn range 0~node[0]->node_start_pfn */ > > > > > > > > > - memblock_reserve(0, PAGE_SIZE * start_pfn); > > > > > > > > > } > > > > > > > > > } > > > > > > > > -- > > > > > > > > --- > > > > > > > > Jiaxun Yang > > > > > > > > > > > > > > > -- > > > > > > > Sincerely yours, > > > > > > > Mike. > > > > -- > Sincerely yours, > Mike.