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 7AE9BC25B47 for ; Wed, 25 Oct 2023 05:46:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 672C96B0314; Wed, 25 Oct 2023 01:46:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5FDEF6B0315; Wed, 25 Oct 2023 01:46:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 49CF36B0316; Wed, 25 Oct 2023 01:46:38 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 35C356B0314 for ; Wed, 25 Oct 2023 01:46:38 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E8186C015A for ; Wed, 25 Oct 2023 05:46:37 +0000 (UTC) X-FDA: 81382899234.14.10EEE60 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf13.hostedemail.com (Postfix) with ESMTP id 20EE320010 for ; Wed, 25 Oct 2023 05:46:34 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C7caWDWk; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698212795; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hYKxVtR0qIolLNmBlPtADfW6N52GnpFotATJ9ITeUoA=; b=vZ6TfVQPyklBwYdFaG97f57X6RFmxkzM8X2crNvO4L1ktJkggJB8reyRqyCnVCVh0Pt0IJ 2rozQcic8jv1mo8HMQrdfL7b7XMhy7KAsUO1jzXBlbx0GlFaej4x5dj478SslpF/iPuawW Oe3fyZLlvTm/WuGxv5wGO+ar6AiZCT8= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=C7caWDWk; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of rppt@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698212795; a=rsa-sha256; cv=none; b=c5r1T+QinWbCoQ7NFsWP9EqhPo8aw7HsljZ4QrTDMGM7DZeZ5C8ZTNHBhkBVnYCEQPlBGP 8H/qhnaDIrSMkwvy2SvE13YT3qry+IC1GVgxZcxsaISk1REm2pucfsmc7ucBIuR6/e4QWa a0o7H4vNMMK46PhOENSn4jtFech4JQI= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by ams.source.kernel.org (Postfix) with ESMTP id 1CA16B82B98; Wed, 25 Oct 2023 05:46:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 031BCC433C8; Wed, 25 Oct 2023 05:46:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1698212792; bh=gb9BVji0VYp7LxmEzIRU/AWqHxH7pf0Z7AqfkYt4rJc=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=C7caWDWkVFFW7JMO/rSqBJYixStQIxsnNF+qOXVHxNTYEF66hljARBFjKQtdS4Mqp jGKLJgvSUrLFpx673Wsgro7IyHJSLFurhyZyIcDXGZrZeJCjqZObGKdHeeoEgamLNA KRba/TSeA8f/Bxxb0leT4Bt2+NhCcdN+GUBweaxtEybXvqCObVuleT/ikIPWZIb2pO OwgIoVlHepUJDP16l/rFDfhLxSH+ss9MktaYj0riU3SwZoqrj1gB1WKatLaT5JxX51 YrgaMbZl0PakMpgjzQ+cxi5hEvxEZZCUkZPWDzVDpgGYmDk4aE02oHCiy529a/n/o+ mzUu5g6Fwua9g== Date: Wed, 25 Oct 2023 08:46:16 +0300 From: Mike Rapoport To: Liam Ni Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, chenhuacai@kernel.org, kernel@xen0n.name, Dave Hansen , luto@kernel.org, peterz@infradead.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" , Andrew Morton , maobibo@loongson.cn, chenfeiyang@loongson.cn, zhoubinbin@loongson.cn Subject: Re: [PATCH V6] NUMA: optimize detection of memory with no node id assigned by firmware Message-ID: <20231025054616.GG2824@kernel.org> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 20EE320010 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: t1ft8xaoecqb5ib5gfgmmtn7so51oc31 X-HE-Tag: 1698212794-77372 X-HE-Meta: U2FsdGVkX1/MldYF8LKFR5zOtUsGU1oJY6z5bMFflgdLqS1kFRuakFJ4iFpBjKbwHI0Tk3q1nC+4w8vk/286YlE6Zi6rl5Jj9i6puhpqK0TVitWsVHy0QNoe/msmO9TjRb3MupmwzlyiK9Y72dvcX+YOIyfjYdNxn3fNyktk43JF9Mc3TPsvxIdW+R2h/hU4nMhaiw2+u60GkQqYiTY/7kSIiAGyme5R5hR2J2friJI/LwKPxqusfrj2xlwXHOkfPga8XttwtDnUiLNwC8B00HB9rP5C93z+bDuEHRiK7wLKPbo5y8u5j4XYQlpX7YRoGhw67DgQXRi/8PfhXUM4AdxL7CqHyO+gKUza20eAVGR25dFH7hzKy7TYcieKixEEq5Zgx1plWbkuFiRdq6hs0Y2dIRvhfb74i6XyshLGyOEOpkZ5g4St1aGocYftNKGGWMLYyysPudm2oJt8H8LcsIXOEBx8jFzV8v2OAtQuGq5lIQ0mr6WmeCC56/ds1z/g6cGgORpOaS0m8dOvWTorcSYfSqR4/vRXGJQIFpSUH5kHa/7eKwhSdkjmC3nLvJwXQvBj+ASMJig1vpbMoy/jl1VEBAg+T6dYMumnox9NWcP8JyejPfPjU7NqP3RKDnTLiXxDKdAacnxePu6d2wtfQ8DrX3swasV61y6rruO0AZqGwb/cRMoo0+jHMSAW9s8+KVYl6gXcUUzOMj4w3fjZbPio67a67lzYZPA+N+CG7c85lxwuMVBtCn6PunbsIzKoOTZKT5p/dWw9wii+h7ht6tHYQ0idleeqhbYT/K02d428x32K1121vlGImmLRWmSOCnw4utCpIy4AumKSaFhYFtxhFwg2dZ+90kl+lD0it9a29WRv6GvjV7RRDKf+A5vZuk44e+TPQmenNXmt1pcvo+7Gy8k2YR0dx9xesH5cAbBpb0HNpQSgYiCuYOGb0dWU+EA/qXsXdc+JopBVvX9 F/Efk8S5 rmGYc+vFFHZADEfQ+zqzc3ad+QmhCjRfMkhNV7rmhdiiG7nRv2OOQFAxjiaLOWkT6lDPuA/vNdFPy+HtHxqJ2sJ4p6W7+GrN4uTDnTh/3RvJTo78J+FjPhJkedyf6QPP7Vkx/P0Wsg/CBHCOoXpi7m5kK/4p3Ns1tsTrgj+3DekIH/ApOzv6CpDdNtNV8EN1pl8avpWhvdc5o3JDw92490BarqQ== 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 Sat, Oct 21, 2023 at 09:15:20PM +0800, Liam Ni wrote: > Sanity check that makes sure the nodes cover all memory loops over > numa_meminfo to count the pages that have node id assigned by the firmware, > then loops again over memblock.memory to find the total amount of memory > and in the end checks that the difference between the total memory and > memory that covered by nodes is less than some threshold. Worse, the loop > over numa_meminfo calls __absent_pages_in_range() that also partially > traverses memblock.memory. > > It's much simpler and more efficient to have a single traversal of > memblock.memory that verifies that amount of memory not covered by nodes is > less than a threshold. > > Introduce memblock_validate_numa_coverage() that does exactly that and use > it instead of numa_meminfo_cover_memory(). > > Signed-off-by: Liam Ni > --- > arch/loongarch/kernel/numa.c | 28 +--------------------------- > arch/x86/mm/numa.c | 34 ++-------------------------------- > include/linux/memblock.h | 1 + > mm/memblock.c | 34 ++++++++++++++++++++++++++++++++++ > 4 files changed, 38 insertions(+), 59 deletions(-) > > diff --git a/arch/loongarch/kernel/numa.c b/arch/loongarch/kernel/numa.c > index cb00804826f7..84e1b046ab52 100644 > --- a/arch/loongarch/kernel/numa.c > +++ b/arch/loongarch/kernel/numa.c > @@ -226,32 +226,6 @@ static void __init node_mem_init(unsigned int node) > > #ifdef CONFIG_ACPI_NUMA > > -/* > - * Sanity check to catch more bad NUMA configurations (they are amazingly > - * common). Make sure the nodes cover all memory. > - */ > -static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi) > -{ > - int i; > - u64 numaram, biosram; > - > - numaram = 0; > - for (i = 0; i < mi->nr_blks; i++) { > - u64 s = mi->blk[i].start >> PAGE_SHIFT; > - u64 e = mi->blk[i].end >> PAGE_SHIFT; > - > - numaram += e - s; > - numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e); > - if ((s64)numaram < 0) > - numaram = 0; > - } > - max_pfn = max_low_pfn; > - biosram = max_pfn - absent_pages_in_range(0, max_pfn); > - > - BUG_ON((s64)(biosram - numaram) >= (1 << (20 - PAGE_SHIFT))); > - return true; > -} > - > static void __init add_node_intersection(u32 node, u64 start, u64 > size, u32 type) > { > static unsigned long num_physpages; > @@ -396,7 +370,7 @@ int __init init_numa_memory(void) > return -EINVAL; > > init_node_memblock(); > - if (numa_meminfo_cover_memory(&numa_meminfo) == false) > + if (!memblock_validate_numa_coverage(SZ_1M >> PAGE_SIZE)) > return -EINVAL; > > for_each_node_mask(node, node_possible_map) { > diff --git a/arch/x86/mm/numa.c b/arch/x86/mm/numa.c > index 2aadb2019b4f..39772d551924 100644 > --- a/arch/x86/mm/numa.c > +++ b/arch/x86/mm/numa.c > @@ -447,37 +447,6 @@ int __node_distance(int from, int to) > } > EXPORT_SYMBOL(__node_distance); > > -/* > - * Sanity check to catch more bad NUMA configurations (they are amazingly > - * common). Make sure the nodes cover all memory. > - */ > -static bool __init numa_meminfo_cover_memory(const struct numa_meminfo *mi) > -{ > - u64 numaram, e820ram; > - int i; > - > - numaram = 0; > - for (i = 0; i < mi->nr_blks; i++) { > - u64 s = mi->blk[i].start >> PAGE_SHIFT; > - u64 e = mi->blk[i].end >> PAGE_SHIFT; > - numaram += e - s; > - numaram -= __absent_pages_in_range(mi->blk[i].nid, s, e); > - if ((s64)numaram < 0) > - numaram = 0; > - } > - > - e820ram = max_pfn - absent_pages_in_range(0, max_pfn); > - > - /* We seem to lose 3 pages somewhere. Allow 1M of slack. */ > - if ((s64)(e820ram - numaram) >= (1 << (20 - PAGE_SHIFT))) { > - printk(KERN_ERR "NUMA: nodes only cover %LuMB of your %LuMB > e820 RAM. Not used.\n", > - (numaram << PAGE_SHIFT) >> 20, > - (e820ram << PAGE_SHIFT) >> 20); > - return false; > - } > - return true; > -} > - > /* > * Mark all currently memblock-reserved physical memory (which covers the > * kernel's own memory ranges) as hot-unswappable. > @@ -583,7 +552,8 @@ static int __init numa_register_memblks(struct > numa_meminfo *mi) > return -EINVAL; > } > } > - if (!numa_meminfo_cover_memory(mi)) > + > + if (!memblock_validate_numa_coverage(SZ_1M >> PAGE_SIZE)) > return -EINVAL; > > /* Finally register nodes. */ > diff --git a/include/linux/memblock.h b/include/linux/memblock.h > index 1c1072e3ca06..727242f4b54a 100644 > --- a/include/linux/memblock.h > +++ b/include/linux/memblock.h > @@ -120,6 +120,7 @@ int memblock_physmem_add(phys_addr_t base, > phys_addr_t size); > void memblock_trim_memory(phys_addr_t align); > bool memblock_overlaps_region(struct memblock_type *type, > phys_addr_t base, phys_addr_t size); > +bool memblock_validate_numa_coverage(const u64 threshold_pages); > int memblock_mark_hotplug(phys_addr_t base, phys_addr_t size); > int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); > int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); > diff --git a/mm/memblock.c b/mm/memblock.c > index 0863222af4a4..ebea0c204eaa 100644 > --- a/mm/memblock.c > +++ b/mm/memblock.c > @@ -734,6 +734,40 @@ int __init_memblock memblock_add(phys_addr_t > base, phys_addr_t size) > return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); > } > > +/** > + * memblock_validate_numa_coverage - check if amount of memory with > + * no node ID assigned is less than a threshold > + * @threshold_pages: maximal number of pages that can have unassigned node > + * ID (in pages). > + * > + * A buggy firmware may report memory that does not belong to any node. > + * Check if amount of such memory is below @threshold_pages. > + * > + * Return: true on success, false on failure. > + */ > +bool __init_memblock memblock_validate_numa_coverage(const u64 threshold_pages) With threshold in bytes there will be less conversions from pages to bytes. Also, I don't think const has much value here and unsigned long should suffice. > +{ > + unsigned long nr_pages = 0; > + unsigned long start_pfn, end_pfn, mem_size_mb; > + int nid, i; > + > + /* calculate lose page */ > + for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { > + if (nid == NUMA_NO_NODE) > + nr_pages += end_pfn - start_pfn; > + } > + > + if (nr_pages >= threshold_pages) { > + mem_size_mb = memblock_phys_mem_size() >> 20; > + pr_err("NUMA: no nodes coverage for %luMB of %luMB RAM\n", > + (nr_pages << PAGE_SHIFT) >> 20, mem_size_mb); > + return false; > + } > + > + return true; > +} > + > + > /** > * memblock_isolate_range - isolate given range into disjoint memblocks > * @type: memblock type to isolate range for > -- > 2.25.1 -- Sincerely yours, Mike.