Received: by 2002:a05:7412:f690:b0:e2:908c:2ebd with SMTP id ej16csp347102rdb; Thu, 19 Oct 2023 06:22:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGDQnuUC4fefHDdcdnkjXdi7X15zyxchyQbQbwvg3YtV+1nNg5UUxy8Gwbg35tk6aNQlicf X-Received: by 2002:a05:6359:3015:b0:13c:eea2:d021 with SMTP id rf21-20020a056359301500b0013ceea2d021mr1761941rwb.9.1697721779052; Thu, 19 Oct 2023 06:22:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697721779; cv=none; d=google.com; s=arc-20160816; b=PbboaZSBcexAipZdomj+UhcVd1cvkTfMoZhLNwYsUKq0BekWt8JbIcetOWu8jKYjzh WwZvVnUXYPoDR0dTwCKvECsS/M/Bxol7rL5ypabrZEqabODMzAGh8CVMwg9welQil7Ue qrHCp/X9jrIhjb5sOr8KGebRYaBPezQRl2ghcjAa4im8WJEUBxBhhWsqly13tLifMnZE t9nJroqF0LMKD/03f3ETqDEsuTDlnZtCYOXEzpy0hvxL9dq/NFQYVJNGP/Gys2Nhn9M7 SbvuL6caCiPjTbRxyEzdmZ8j6kMf+0h8+UfwzEVmMqPg18DW0cfFUKOZip/GZ5i8J73k /6lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=R8cshwpkgCLbTspjqCkooWC3GYTc/d734V8fjxUCe9U=; fh=Xm+vKLYjfhIE047lfb665ggHyMtmFoHmKhLku/eLLyo=; b=RCEswDwobLw3NhKdjHBU8aO1mBFggvjwQ0u8HqB/A/B2/ip5cN+uZ42XUKgYPvyQiY J/ECf9vFjU5dbpkYVYnwF9RtorIL4lM3w65SB+vnYbZ0a6vj81eDB42+sMdF/1F+kxRC 06t2xt+h0+q3pNIpdREH0pZ3/2pS1F2p9TkVxMcLrxBKVde+OGPU6dYRdNSVYR+Poxna ixKUZhxHZymqWiSAsLq/eay0IP9sfcwzGkHwuvXJbaXyPmtKb8rnpBEnhgRP+GH5GTHr 805LwK63tFim5LXGph/xmZj3HFtKY9HtiYuQq76lCOOO3nQa/zORfyNaj0+PHCMRWHVr WjUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=B+DqpKCV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id t189-20020a6381c6000000b005b7f3f3ecc5si4474845pgd.735.2023.10.19.06.22.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Oct 2023 06:22:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=B+DqpKCV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 8D5C58236242; Thu, 19 Oct 2023 06:22:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345909AbjJSNWo (ORCPT + 99 others); Thu, 19 Oct 2023 09:22:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345962AbjJSNWj (ORCPT ); Thu, 19 Oct 2023 09:22:39 -0400 Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5228219A for ; Thu, 19 Oct 2023 06:22:37 -0700 (PDT) Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-53ed4688b9fso7185367a12.0 for ; Thu, 19 Oct 2023 06:22:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697721755; x=1698326555; darn=vger.kernel.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=B+DqpKCV7AjlrCoptxWpFe7LFl8btJ2lOOz65W8Qg7Ff1tTgq5Nq4a4Gcj+3zPcVQe DwrENh1AMCNaUK2A+v/jg/6uLMygPlK22Ze4wWo95SdM26B1yn94PIPKRj69HkCKVFbR CemeqnBrKruSOm4oy5zL5dZmJa1FBr9XzkYYI1VXQhq2bLiR5CJJOUCH88g75yqRMlBr UOyvQdqjupMwQxHsyQy7Vxlsfn3WJxk6j4Xc5F88c7RdlSrHU3zSAF+pRu825pHHPj+l TNzO7W82raSLAU146+XHim2t7Qgst57d9lNCO4p3FLn1u3I6U5hhczz+fzgO9DzOxSq8 rp8Q== 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=tmcDa/aCFc0qK7YMEjQ7DACUvKPc+tQdOkSwQ9VpP+CNspIe+sCeW4WaUCzrwgS45Q 3u1nPuuzsxs/mb06cB+NufBF6quf2AbraI45jErHpkmFqGupI65Ub1Li1tWLn+XREUX0 KZNLm3YyEbUIH/iZjxeE4Qv0sRevVV7qwPximmPRsZ+S9DX7982uwNW7t9QwNLXJJHwK +Kdl3JRjlOjMsiz+alnBafdIyv0lnxGfZeyAt4nBJLNiT5DsmEYosaot18f6SLgLuGZF 5bZxUc+wT7vVUkNenr/I3EnosyhtMBSUiNyR0yS8OIzQrChq0Pp4EzCmKD09wcj621C4 H0YA== X-Gm-Message-State: AOJu0YxIGzCg0R4+TSS3RqKbeI3GLVadf9bmB9U9i9z9z05z1476d88o KhGUR5C1aJLWfEK+YJrGzZ+Nig9jDmJRN118Ie0= 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-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Thu, 19 Oct 2023 06:22:55 -0700 (PDT) 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.