From patchwork Thu Oct 26 02:03:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Liam Ni X-Patchwork-Id: 13437171 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 6B96DC0032E for ; Thu, 26 Oct 2023 02:03:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C76326B02C3; Wed, 25 Oct 2023 22:03:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C261F6B02C4; Wed, 25 Oct 2023 22:03:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AEDC26B02C5; Wed, 25 Oct 2023 22:03:42 -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 9F08D6B02C3 for ; Wed, 25 Oct 2023 22:03:42 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3E79EB5E75 for ; Thu, 26 Oct 2023 02:03:42 +0000 (UTC) X-FDA: 81385966284.14.C58E396 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf21.hostedemail.com (Postfix) with ESMTP id 870711C000B for ; Thu, 26 Oct 2023 02:03:40 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QmiZNNf+; spf=pass (imf21.hostedemail.com: domain of zhiguangni01@gmail.com designates 209.85.216.44 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=1698285820; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=B5Aqv6dlA0H+Btg56Nn64Q5cxiHM9wUKXyXUnjiSU3A=; b=0IL2N1LtM7PsAL91TTDsUTxbQYAv1AAPk0QEmgrHUkX4K0PRNwZmuszLyl30AvsquK0u2F RyoCN1/1SfDU9zg0Abr44rQQqRGWyG+/D7Zq0MlD2ax+WH/9IRVKCZ7itY3C9NEW3kxB5Y +yQVemE/PHmPkHGMBXgUE6Ic1hJHZTo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698285820; a=rsa-sha256; cv=none; b=SD0IOutGrNfXqY028CZDkzQpQ1mkhutGzyQNqNvvaiJ+X+ow+yrwNuJR87Z1KKVaWmK75W Sdi1K02fxwSGw4EPGhBD+ihsyq5z9MB/6pyg7wWtAYkGfave5FC5B8FJB2rKtrnCrJrPn5 1CxoUbu7Pm9SiadiNgzbRNHl4RDQfIs= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QmiZNNf+; spf=pass (imf21.hostedemail.com: domain of zhiguangni01@gmail.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=zhiguangni01@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-27d0251d305so272397a91.2 for ; Wed, 25 Oct 2023 19:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698285819; x=1698890619; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=B5Aqv6dlA0H+Btg56Nn64Q5cxiHM9wUKXyXUnjiSU3A=; b=QmiZNNf+Xw6OklVZGFtOef9wwFqN1vkgHvOB33JguG2rWQ8SnUNP+Y3pllmOZbz42R kOwE8RljMVuR00KjkKy43r7OlU7EOdl5AKgf10djDFw5fW6mjKUmU2wVZF9Sgp4RUSXl QDX8HH5YPgW3BA73IRCA7M0MAn2mM6TEpV5HhVJ4jQuUi8OiqVTvAH4wuHg0IsEmP/wR W6PvHubnOiiDlsOyiSB13CWmAT83fCWdYhN28W89nEGyS4ZJUziEKkAyRHjiiYH5NRjq qj4V0YLB+OWuBuSnLN0cN47hlmxSd8U6SAXsGKQUAaaYAfsDM6xPfk2VjOAspEolM3lA wN9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698285819; x=1698890619; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=B5Aqv6dlA0H+Btg56Nn64Q5cxiHM9wUKXyXUnjiSU3A=; b=XFfZYpx2OwFIJuw3Ru7CsvopwNGYn9ku4A4dRqLZqw+lrCIj9jyyw6G6KKEk3SIFgv RlSrxbsUYRGHZEobUXGLQaE6gMoiV3F9FzPybjyKn80UF8S7usO4+zDiSKnowliVF7nA 8CLVIWCKEWJOGPxgWazYO/xU/ncMGk18jOWLEvM3b1yW+cZcclOTHoEZGj8a/e4QjPlE ZOA58P7kBbUz21TS4oWyR10HsXV1oeMDGGZUh4VMdUdOh+NhsfwD9ka/YDPEhV3zyc5H XQgummmVQ2p6qAAMQU2xn/nl6BdzfAAyQzOimcpyMtzK++XyQIZq0iyon+M0P1vakHGS /nOw== X-Gm-Message-State: AOJu0YzSeDP7IiJ6SHmIVuo9l4cxN0E2p+mlyA0Qi5nPlGlOwszSiO+1 5AlEOKqE32hze4gxtyklrmcSltFd+Oc= X-Google-Smtp-Source: AGHT+IHtZOR273U91ceyKReQ/5kBhO+DzSBKVAs+uLU758ZOC3HkSK5KXuOScCLjzVRRYYvpCqf6LQ== X-Received: by 2002:a17:90b:4a4e:b0:27c:f9e7:30fd with SMTP id lb14-20020a17090b4a4e00b0027cf9e730fdmr13993763pjb.7.1698285818787; Wed, 25 Oct 2023 19:03:38 -0700 (PDT) Received: from localhost.localdomain ([180.169.121.82]) by smtp.gmail.com with ESMTPSA id 22-20020a17090a1a1600b0027d0d4d4128sm597371pjk.25.2023.10.25.19.03.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Oct 2023 19:03:38 -0700 (PDT) From: Liam Ni To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, rppt@kernel.org Cc: 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, zhiguangni01@gmail.com, zhoubinbin@loongson.cn Subject: [RESEND PATCH V7] NUMA: optimize detection of memory with no node id assigned by firmware Date: Thu, 26 Oct 2023 10:03:29 +0800 Message-Id: <20231026020329.327329-1-zhiguangni01@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 870711C000B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 84zw9xsub789bxb5s6iongemkugrr3as X-HE-Tag: 1698285820-505487 X-HE-Meta: U2FsdGVkX19Ac8MvoaKbj+IUNm+3yEkvMnBLTEaZP6/e2Dmnd6bHHG3DhD5T7TXV0fUHK5L/m9uOpLt9CGKwcZC3djbpXX33tkx3INXC0eGU8mDX6nKC/wWZnIDdLiBSobn7JMTaXces3n+D6HVEU8QA5UT+Sq1ZcWV9VbsMaC9Mks9RBTePjLKLfu6dXq+zhrpKQR7tRCrZ2NErVxSpA9iK1GEs/mLueTyMBlVnCpkyMs9R2uZ0R+N9UkA29hJeYDwPPp43T+mXos1+PKjbGuA9mLytWZ0jXzUoDFmdwA3KkV7fbp0NKgxnyBKRlYYP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rdfUYo5Q8jFIG/JNV5p3hO8+r/mPoD7uL0RsGsDe5JaP48jKl 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: 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 Reviewed-by: Mike Rapoport (IBM) --- 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..0e69679bfc8d 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)) 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..4079c9edaa93 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)) return -EINVAL; /* Finally register nodes. */ diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 1c1072e3ca06..a94efe977539 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(unsigned long threshold_bytes); 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..397d61c4896a 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_bytes: maximal number of pages that can have unassigned node + * ID (in bytes). + * + * A buggy firmware may report memory that does not belong to any node. + * Check if amount of such memory is below @threshold_bytes. + * + * Return: true on success, false on failure. + */ +bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_bytes) +{ + 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 << PAGE_SHIFT) >= threshold_bytes) { + 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