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 BACBCCDB465 for ; Thu, 19 Oct 2023 07:36:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A7C98D0172; Thu, 19 Oct 2023 03:36:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 457A08D0110; Thu, 19 Oct 2023 03:36:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2F7D78D0172; Thu, 19 Oct 2023 03:36:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1D4488D0110 for ; Thu, 19 Oct 2023 03:36:49 -0400 (EDT) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E98F41CC00A for ; Thu, 19 Oct 2023 07:36:48 +0000 (UTC) X-FDA: 81361404096.29.C8DC5F1 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf10.hostedemail.com (Postfix) with ESMTP id 90DFDC0024 for ; Thu, 19 Oct 2023 07:36:46 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Uz86yeEo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 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=1697701007; 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=xbPvtj2SjCBDj5MOBDOnCfW0DHpBKEPhAgwBXZaH9w8=; b=1NhHyxWZd34bapDRX8h5gVKqvPTV8uckmLk8A8R8F24C4ZYpdgXNb7ufsmgD1JHK0RWV3S AcEGg8gACy4FFLL8uxRlqocfeKnnAtrRiItnjRQFuXPcSmiOzlx2oYtNvg6rmVxRgoLcKr CDrhVx0iA4bMp9lumkZ/tlohsAbVwBQ= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=Uz86yeEo; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf10.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697701007; a=rsa-sha256; cv=none; b=gR53X49/l/Ygaw9VV5bH0Mx/8M06LnW8TIhb2RBz9+gjS5CTDBNbHDZ4Z9NcARNXulm2RE Ez81j44C/pvgVc9TS/uXQ8YNulU14TFCWd52jDh6vYuBO4REC0JtMHnix1z/MbbD9DjZzk lW20N2zH/FZ4nvrWvju7qCpE50TnBZw= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id A5214CE295B; Thu, 19 Oct 2023 07:36:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54669C433C7; Thu, 19 Oct 2023 07:36:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1697701001; bh=1N4xqqxOlozUS6OFkZvzJkSNGpLhAKLz8zkUg9AX4l8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Uz86yeEo+4rJQd1joYCr0DJ3UEq4WWefPN841VPLcADI/aoJB7oJkd1LAFPl+SdG6 VgkiM8ekwjFm4MAe04jV7oE/aPuTwuo1JlveOqGgBNVhrDPEfE9qVD8+xaaeJHjtRh npn9W2+8goCqBIplpCngJhqlt/k6HISK4JzErprE40+itghz/1fh36NSnVmqKbhrbY 0LVHegsXHl70rnvyH6SSw3wEFgx78M0wO4wvya4+eAZfS8rJ/PkaO2Wx7Gn0G/I4xV OO6GzMAB3d+oHph00FY4VjYwd107IqU6pU6bIogv+uMavPvzORT5XlCfHfaWg6N2ca CTsYbMBne5+Qg== Date: Thu, 19 Oct 2023 10:36:25 +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@linux.intel.com, luto@kernel.org, peterz@infradead.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, akpm@linux-foundation.org, maobibo@loongson.cn, chenfeiyang@loongson.cn, zhoubinbin@loongson.cn Subject: Re: [PATCH V5] NUMA: optimize detection of memory with no node id assigned by firmware Message-ID: <20231019073625.GB2824@kernel.org> References: <20231017083033.118643-1-zhiguangni01@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20231017083033.118643-1-zhiguangni01@gmail.com> X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 90DFDC0024 X-Stat-Signature: oqa8jgiysy44jhwrxheup3rqrr6jkf4a X-Rspam-User: X-HE-Tag: 1697701006-576766 X-HE-Meta: U2FsdGVkX1+E/X9UCOJ+Z4kYxKWZYfPahRboBvnAaPtaKwyivHn5x4ZXn2Ukb+4JBmPYJHenPJovqon2eE9jOZbVN6HF0Tc1HYWz312tw9/xoHVqk4ZqpSH6LfSRc7kJaUJtSX/ViEEkADEKcLvZWGg1dZ84lBIxQ3EWMNpngHQNnj3WVnptQim56io+ieVMgU92khw0Bf+MEpYxB1UEP07CTlEuCWfGVgkQHVsAGdwGn88sRBtq4xPUBYHPaXoQDeVNfMjTUxYfyEQx2MqfwbbuvxgWBeh6ov2yl+pgfbSQrlcofXyycCQqEw+cwI27HtM4yy1VYl6+eZkt1PGME9TS6lWJ5mg0XkaAABgMENvO+qAPIGRZsRNi7eHHVJ/vcWUYwRV9ZmwuIK3umWTfte/bqtExTJdN2tUJ5hzePlIbhywo9SoYJbQ/g4QzIl54r/F7HhWV+q8+pGydMZ0ZTWX66jg5r+bNf1eGtOxyCISZkxaMWPHIFsKBneriywvlUTaOgSAc1X0fmvTjT38VvIGrQ8M8mRve40Vym6NtPJn8kLHI8cMU1lXeq8VR1dCl50uDJyxkEkJvtixaq4YoBUt2Pju4D8QPeJ7V8524RvWLIxVC+4s6BvTS6TBykOj5H/4xDwahin+21zZrs17nFnfJ80kc9qd/MyyfJBGZC3qnO05zWJIrHuloYFtSR+vbuYjKEM+bFzVYw0DKH5IWRBXQjKx/JPAFoPHf7Um1qCpIiLkHddk0JAJk+R3XzaE2i8FsP1MXydKkVAA4qXIP3gSjQUQec/JxmcEcSQd+tcnGm88VFs1+3624XWkvTToEEi1ER2qzYXkCiQRNAFIQWVImA9yC6YvWpJ13ngwo4igH0Mzu1M/KW+R373f/UJAtas6dULjseYFm2m/K4eIIhibUq4Y1HRjA/OPHgbORWI/zydwzio8MLYBd1wf3M+xH4+AJXZuMHptI2k9DeOa MFJk+pp0 sGyciq6BAHkxKTuc5oErmFcZn16uP/A2snp+Fnc7l/IV4K/hsN58xU8nU/B03WLjNVVG6MGrcq0c3MlMcT8zXVKZJ+/u+zp/Er0HL6+Q/StDSRpnms1XTfTpHU8aeDHX4siuGtwTHw5rLaZhTUz9/nMZMDNnhkRshuJi34TcwLV/Ir0dmbfWnPVuZ/kQnpteu2cznBHVpHmum+qOIKDqzRKeMdw== 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: On Tue, Oct 17, 2023 at 04:30:33PM +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..fca94d16be34 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 >> 12) == false) No magic constants please. Either use SZ_1M >> PAGE_SIZE here, or make threshold in bytes and convert it to number of pages in memblock_validate_numa_coverage(). Besides, no need to compare to false, if (!memblock_validate_numa_coverage()) will do > 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..95376e7c263e 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 >> 12)) > 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..4f1f2d8a8119 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 - calculating memory with no node id assigned by firmware > + * @threshold_pages: threshold memory of no node id assigned > + * > + * calculating memory with no node id assigned by firmware, > + * If the number is less than the @threshold_pages, it returns true, > + * otherwise it returns false. > + * > + * Return: > + * true on success, false on failure. > + */ I'd suggest the below version: /** * 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) > +{ > + 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.