From patchwork Wed Jul 8 00:23:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Song Bao Hua (Barry Song)" X-Patchwork-Id: 11650499 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E3BF2739 for ; Wed, 8 Jul 2020 00:25:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B855320771 for ; Wed, 8 Jul 2020 00:25:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B855320771 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=hisilicon.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id AB5166B0075; Tue, 7 Jul 2020 20:25:18 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A667E6B0078; Tue, 7 Jul 2020 20:25:18 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 954F86B007B; Tue, 7 Jul 2020 20:25:18 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0074.hostedemail.com [216.40.44.74]) by kanga.kvack.org (Postfix) with ESMTP id 750A16B0075 for ; Tue, 7 Jul 2020 20:25:18 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 26E7C8248D52 for ; Wed, 8 Jul 2020 00:25:18 +0000 (UTC) X-FDA: 77013014316.20.music71_4a0305d26eb8 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 21289180C0609 for ; Wed, 8 Jul 2020 00:25:17 +0000 (UTC) X-Spam-Summary: 1,0,0,33114b0e21b8269e,d41d8cd98f00b204,song.bao.hua@hisilicon.com,,RULES_HIT:2:41:355:379:421:541:582:800:960:973:988:989:1152:1260:1261:1277:1311:1313:1314:1345:1431:1437:1515:1516:1518:1535:1605:1606:1730:1747:1777:1792:1963:2194:2198:2199:2200:2393:2559:2562:2693:2731:2741:3138:3139:3140:3141:3142:3167:3865:3866:3867:3868:3870:3871:3872:3874:4117:4250:4321:4605:5007:6261:6630:6742:7875:7974:8603:8660:9592:10004:11026:11473:11658:11914:12043:12295:12296:12297:12438:12555:12679:12895:13141:13148:13230:13894:13972:14394:21080:21451:21627:21939:30054:30056:30064:30070,0,RBL:45.249.212.35:@hisilicon.com:.lbl8.mailshell.net-62.14.2.100 64.201.201.201;04yg6jtiruph1frn166is49ij1knpyc5pm6fuwx8egz7ydbw5ramnangjt7rhu7.c33unofgwzqzmfr6r5dzdndfiduhzty7amep7enijiwczrtno6mpzjwxr5tgh44.a-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:24,LUA_SUMMARY:none X-HE-Tag: music71_4a0305d26eb8 X-Filterd-Recvd-Size: 6842 Received: from huawei.com (szxga07-in.huawei.com [45.249.212.35]) by imf03.hostedemail.com (Postfix) with ESMTP for ; Wed, 8 Jul 2020 00:25:16 +0000 (UTC) Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 551C3A5C8F42FA658B63; Wed, 8 Jul 2020 08:25:12 +0800 (CST) Received: from SWX921481.china.huawei.com (10.126.200.214) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.487.0; Wed, 8 Jul 2020 08:25:03 +0800 From: Barry Song To: CC: , , , , , Barry Song , Roman Gushchin , Catalin Marinas , Will Deacon , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H . Peter Anvin" , Mike Kravetz , Mike Rapoport , "Anshuman Khandual" , Jonathan Cameron Subject: [PATCH] mm/hugetlb: split hugetlb_cma in nodes with memory Date: Wed, 8 Jul 2020 12:23:07 +1200 Message-ID: <20200708002307.26716-1-song.bao.hua@hisilicon.com> X-Mailer: git-send-email 2.21.0.windows.1 MIME-Version: 1.0 X-Originating-IP: [10.126.200.214] X-CFilter-Loop: Reflected X-Rspamd-Queue-Id: 21289180C0609 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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: Rather than splitting huge_cma in online nodes, it is better to do it in nodes with memory. For an ARM64 server with four numa nodes and only node0 has memory. If I set hugetlb_cma=4G in bootargs, without this patch, I got the below printk: hugetlb_cma: reserve 4096 MiB, up to 1024 MiB per node hugetlb_cma: reserved 1024 MiB on node 0 hugetlb_cma: reservation failed: err -12, node 1 hugetlb_cma: reservation failed: err -12, node 2 hugetlb_cma: reservation failed: err -12, node 3 hugetlb_cma size is broken once the system has nodes without memory. With this patch, I got the below printk: hugetlb_cma: reserve 4096 MiB, up to 4096 MiB per node hugetlb_cma: reserved 4096 MiB on node 0 So this patch fixes the broken hugetlb_cma size on arm64. Jonathan Cameron tested this patch on x86 platform. Jonathan figured out x86 is much different with arm64. hugetlb_cma size has never broken on x86. On arm64 all nodes are marked online at the same time. On x86, only nodes with memory are initially marked as online: initmem_init()->x86_numa_init()->numa_init()-> numa_register_memblks()->alloc_node_data()->node_set_online() So at time of the existing cma setup call only the memory containing nodes are online. The other nodes are brought up much later. Thus, the change is simply to fix ARM64. A change is needed to x86 only because the inherent assumptions in cma_hugetlb_reserve() have changed. Fixes: cf11e85fc08c ("mm: hugetlb: optionally allocate gigantic hugepages using cma") Cc: Roman Gushchin Cc: Catalin Marinas Cc: Will Deacon Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: H. Peter Anvin Cc: Mike Kravetz Cc: Mike Rapoport Cc: Andrew Morton Cc: Anshuman Khandual Cc: Jonathan Cameron Signed-off-by: Barry Song --- arch/arm64/mm/init.c | 18 +++++++++--------- arch/x86/kernel/setup.c | 13 ++++++++++--- mm/hugetlb.c | 4 ++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 1e93cfc7c47a..f6090ef6812b 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -420,15 +420,6 @@ void __init bootmem_init(void) arm64_numa_init(); - /* - * must be done after arm64_numa_init() which calls numa_init() to - * initialize node_online_map that gets used in hugetlb_cma_reserve() - * while allocating required CMA size across online nodes. - */ -#ifdef CONFIG_ARM64_4K_PAGES - hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); -#endif - /* * Sparsemem tries to allocate bootmem in memory_present(), so must be * done after the fixed reservations. @@ -438,6 +429,15 @@ void __init bootmem_init(void) sparse_init(); zone_sizes_init(min, max); + /* + * must be done after zone_sizes_init() which calls node_set_state() to + * setup node_states[N_MEMORY] that gets used in hugetlb_cma_reserve() + * while allocating required CMA size across nodes with memory. + */ +#ifdef CONFIG_ARM64_4K_PAGES + hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); +#endif + memblock_dump_all(); } diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index a3767e74c758..fdb3a934b6c6 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -1164,9 +1164,6 @@ void __init setup_arch(char **cmdline_p) initmem_init(); dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT); - if (boot_cpu_has(X86_FEATURE_GBPAGES)) - hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); - /* * Reserve memory for crash kernel after SRAT is parsed so that it * won't consume hotpluggable memory. @@ -1180,6 +1177,16 @@ void __init setup_arch(char **cmdline_p) x86_init.paging.pagetable_init(); + /* + * must be done after zone_sizes_init() which calls node_set_state() to + * setup node_states[N_MEMORY] that gets used in hugetlb_cma_reserve() + * while allocating required CMA size across nodes with memory. + * And zone_sizes_init() is done in x86_init.paging.pagetable_init() + * which is typically paging_init(). + */ + if (boot_cpu_has(X86_FEATURE_GBPAGES)) + hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT); + kasan_init(); /* diff --git a/mm/hugetlb.c b/mm/hugetlb.c index d293c823121e..3a0ad49187e4 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5699,12 +5699,12 @@ void __init hugetlb_cma_reserve(int order) * If 3 GB area is requested on a machine with 4 numa nodes, * let's allocate 1 GB on first three nodes and ignore the last one. */ - per_node = DIV_ROUND_UP(hugetlb_cma_size, nr_online_nodes); + per_node = DIV_ROUND_UP(hugetlb_cma_size, num_node_state(N_MEMORY)); pr_info("hugetlb_cma: reserve %lu MiB, up to %lu MiB per node\n", hugetlb_cma_size / SZ_1M, per_node / SZ_1M); reserved = 0; - for_each_node_state(nid, N_ONLINE) { + for_each_node_state(nid, N_MEMORY) { int res; size = min(per_node, hugetlb_cma_size - reserved);