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 E1C36CDB465 for ; Thu, 19 Oct 2023 13:22:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5D1278D01A4; Thu, 19 Oct 2023 09:22:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 581748D0019; Thu, 19 Oct 2023 09:22:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46FF78D01A4; Thu, 19 Oct 2023 09:22:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 38A978D0019 for ; Thu, 19 Oct 2023 09:22:39 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 06EFE40282 for ; Thu, 19 Oct 2023 13:22:39 +0000 (UTC) X-FDA: 81362275638.22.BA31828 Received: from mail-ej1-f49.google.com (mail-ej1-f49.google.com [209.85.218.49]) by imf08.hostedemail.com (Postfix) with ESMTP id 32B3D160026 for ; Thu, 19 Oct 2023 13:22:36 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OiJ34yWH; spf=pass (imf08.hostedemail.com: domain of zhiguangni01@gmail.com designates 209.85.218.49 as permitted sender) smtp.mailfrom=zhiguangni01@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1697721757; 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=R8cshwpkgCLbTspjqCkooWC3GYTc/d734V8fjxUCe9U=; b=dhBVYYMAre+aNkKws7ARpgRU576+OIVjQmL3vC3132i7auzO0DlFbpxELVDhIIALuRONeF M9vM8acD1gB2vsOYSrLr3fQpfCmFTj2fJ8LKd7UyotkPqZdd/WP2tjwkbe8uIrcScTz4XV e8ithZWyjc7LBGhMm7LnABh686XcDuY= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=OiJ34yWH; spf=pass (imf08.hostedemail.com: domain of zhiguangni01@gmail.com designates 209.85.218.49 as permitted sender) smtp.mailfrom=zhiguangni01@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1697721757; a=rsa-sha256; cv=none; b=tlwwUA0v8qiAUoVeBBD0CdovPuZaWF93Kc8TiGGW/dkhfhRxSXydAV8ootynVi7QTW+QH6 pUrlkfps+irMEDpVGssPJhTI6ePpQXjiwEeh7N3x8dDXsMAeHOMAAKKCjHx01RK9HLSnH6 ZURE6bt0JJl/8TNAG5k/BsMm3pjiLoM= Received: by mail-ej1-f49.google.com with SMTP id a640c23a62f3a-99c3d3c3db9so1272550866b.3 for ; Thu, 19 Oct 2023 06:22:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697721755; x=1698326555; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=R8cshwpkgCLbTspjqCkooWC3GYTc/d734V8fjxUCe9U=; b=OiJ34yWHw07lFbO44rH1RV+7ajZ9tGBhrCGWA38Ym8a5WkSvkaw8D2NPB1ROVpRCkc EYcSwhxho0hu8+1YFNDXsudvZM5BCOODINKH2U7L+3NuUFx6PFX5l+h+WWtgiLYeRTtc 8eqnMS8TTKL7s4Cy93WpxO602NyGEOYnn7TFL0pvU9cndt+Xtu5pNaxuOZqH6tMaF3J9 Z2QV0OnO8LVc/c/aun8wdeUgL4W7qfjRFEH+p81vL3UDcn79tNpWKObZ9qgNEa4NfqjX nwYN1zftljZuHN9fshwXI+nBDYyv0Y0ui+zE2Uj/Tda6igMpDGSRFWKmjidVizqCYuBi 5l5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697721755; x=1698326555; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=R8cshwpkgCLbTspjqCkooWC3GYTc/d734V8fjxUCe9U=; b=pfcA6EXaL/GsPHbrOV7x+AVkdDfHXtfQZb2GYkx1Cy2e8yQI9KOKo+Z81xI55nUC9Z jMY4VuFq2vcKYCQvT2MPRDWn6knYtylw4qOWlowbC/Txj3wIzbwb5GE8li6j1m4VZkBW Xaos3ExFHq9+RBLiFlptLk4moWcRaqEFda+HPS+9XaD8W0MUP8wTOBnL1hNF/kbgSS8W Du2NTVRSK13qxYaE1x/Tr7sbiV8ryGBCelpiQ7flcE3ctqdxjUdz3thBU33mmUNNOign UYVBujjQwEL3w4UmrjimsAJ193u9z+8GxR2tqJiJ+3J4Cw5IIajzbG2dkvF5/owEtx1r +ZPQ== X-Gm-Message-State: AOJu0YxI5+s/WD9iuY6NPCIRS1KPbn6DAOorrok3xSxfdQUN+Nnr7Lzj MPGiCd0N1NzafajQUlf56yZDtYx5DWTMEtrX7kQ= X-Google-Smtp-Source: AGHT+IFPL/1FhYZwjLu6LkmXsGrA81vCOzj3CTlwBJoJIEQ18X/ItdKqq5VrIGrSD1ZtYqQYtpfFY5X5OPQFNKtCuQM= X-Received: by 2002:a17:907:2d0f:b0:9ae:6a51:87c3 with SMTP id gs15-20020a1709072d0f00b009ae6a5187c3mr1975859ejc.9.1697721755248; Thu, 19 Oct 2023 06:22:35 -0700 (PDT) MIME-Version: 1.0 References: <20231017083033.118643-1-zhiguangni01@gmail.com> <20231019073625.GB2824@kernel.org> In-Reply-To: <20231019073625.GB2824@kernel.org> From: Liam Ni Date: Thu, 19 Oct 2023 21:22:23 +0800 Message-ID: Subject: Re: [PATCH V5] NUMA: optimize detection of memory with no node id assigned by firmware To: Mike Rapoport 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 Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 32B3D160026 X-Rspam-User: X-Stat-Signature: dsqkosgziwn9be3foisrcumps5sn7ikj X-Rspamd-Server: rspam01 X-HE-Tag: 1697721756-378142 X-HE-Meta: U2FsdGVkX1++oZ7YQPSpW5fy64rP1sr1VFAnMYWlemzNbaT2l8Mv/ZpX34HJJNFCC+YU5wWJDQYhFN3Fc3qQcxdjQRzieHSl8mLDSYIteWbzHgt6zWwf8yIr1AenV6Goch2i0X+YYWankBuwdBgNgqbkP75fzXO7BomsAgqXqFvtlFdvFdpe02NAk8hFWbqxGzenZYaw6h8O96soovyGvP3//Du1eOnSYyZsYHoHmWxo2iB6y1syBT9iRWthzEOPFVbo0bZv2ZcQg4l/GsthmUFBs8xV7eYIkBk5dqXDIuNSXO/raFdOenhomi7slcAR0VKBtvEVLCmyz3OtcV/jyO0aTX/Vhsw60g2rKV2uG0svzzqUThaZAIvHA6MbT2xEPefa+brehyRZl9yGQZyoBxqpD5ubkeRxFTWQHJTxOPYKY1Dz4oR1gpyO3VSNC8vb2UDn+cCYdYrtfwK2MQiTLIzcKiiEgYxIr2O3bQXkplJeUPMkDr/NerHzsJwme5FJy72zq20J+HWyUAWi4OqKkaBzOCYlPBk1sSZtiPTSGCB8HPWK1Muhlu3IZ4JMY1EERXYcVUFXPtOQUZz4PuaU7iWdrXPvujVJIP8+BqPtchWwlG4yhxXBS30FvDqVuPT2cQDICdT4Y6zfkyf+4lhcGxr1nXdYsc4Y+pakTtML2e0kSMo3BVFl8SdfdI+Cy4qinJo/V10sQJV5Re0HebT7x4FBMQDPbez+O5Px7RQ2vQmfVVn4t78cSrBQE6OvZDj9ryBM1CypWctfi/wqiyCzrukb7Gp2zJ3eNavw7aHAiy6H/tstEPqPDawG5UJi2Ylw9strC94j2PArliRzyZu8Md2MuCqS7Tmgmvx6siAPDU8yBNyP+Q3I0uwiLiqqDD+p2O8ekeKOONOC8TH8F7DFt/9hO63yu6GVZrlCKAsfY4Elu2rjnuEneT6+eomlWnI2CSC6q9c8hZVYtJMu2rB Z/sjnkvS XnNWAP43BC4rRFR6PWAuHJTACj2YwQzAoDMGAOL2iWXBPVeMP5mCIosyYDU0xeVt761YTo9xDlqOmlkmWh4pTGLZfh6TB1fe3ywN3Zn3LBJSEjF4Yayn9Frn4KU7JegS+7ISsZaMtRt25sVzryinxvE5XfI0Mdb/dcug+9yNsJksN2cU8GcJlO5ME17vdS5fgGTKrDfCLwFnxO7NiH4sD+TZR3wYU/e4EfMivscDCVTv9Ns/wGYx7gjIkWML4N1lq1kul1tHJST8+R1ev+c0Lxu6ZkpCuJRoxc4OLr2DPc5dPVfPFRq522lLKPzWD1GCenTeav1Q4E1KHTtFdYpm//ecH+FhMJFjlEqPaA97v2TceHPCn/hAs4kL28B005XpKDnGdLPdw9BgproGFrotybyCoYGGLM91jui1U2R59oFQ733tQzZJlF8iGPQ== 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: Thanks, I will prepare V6 based on your suggestion. On Thu, 19 Oct 2023 at 15:36, Mike Rapoport wrote: > > 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.