From patchwork Fri Nov 20 06:43:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 18CC4C5519F for ; Fri, 20 Nov 2020 06:46:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 62A5622256 for ; Fri, 20 Nov 2020 06:46:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="Gs8kPAy5" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 62A5622256 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BD75C6B006C; Fri, 20 Nov 2020 01:46:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B88166B006E; Fri, 20 Nov 2020 01:46:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A9D7B6B0070; Fri, 20 Nov 2020 01:46:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0104.hostedemail.com [216.40.44.104]) by kanga.kvack.org (Postfix) with ESMTP id 723F46B006C for ; Fri, 20 Nov 2020 01:46:22 -0500 (EST) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id ECCB6362D for ; Fri, 20 Nov 2020 06:46:21 +0000 (UTC) X-FDA: 77503862562.20.box54_550c77627349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 39F35180C07A3 for ; Fri, 20 Nov 2020 06:46:19 +0000 (UTC) X-HE-Tag: box54_550c77627349 X-Filterd-Recvd-Size: 14270 Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by imf45.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:46:18 +0000 (UTC) Received: by mail-pl1-f172.google.com with SMTP id u2so4359918pls.10 for ; Thu, 19 Nov 2020 22:46:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=W2Lt/HFpEA/hqI+q/d/AJuv9tCMbUi3JtA5McU8GXbw=; b=Gs8kPAy5p2OKhN0qfI9MeJWNh47fe+aceNM9qQssxX9E93keNPck6PStj9lOxSw0Jy 6bUkx3mwPZrfZLAsZr1fsC40SEG7kvzkUBGJuHVSA3dODkltjbrUfs2w0FcwSiykrhdT jZ2Xw7C04wv6ZPe6sK0Up0oHQKNaXueHX6rxc/k0s8JiUDqwt8Jrz5sscm39zScFNEdH 94A75KuI+jQUKiL1PvnmRlUAlM5QylM1/7rpNc6GNF0z6smlNz2kz5wGr869DckEqhDG iuUI0PHIRKUmjeDCHknXaX1PxnYBr3dGNTWIwypNkda6v6+1d3ToDb6nkr1QgB1Z0YL5 1R4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=W2Lt/HFpEA/hqI+q/d/AJuv9tCMbUi3JtA5McU8GXbw=; b=oLyM+SYc3KjGlg/GC3jgelZ5UfqZ0Whxgw3QbAri1nCqtRR2rQlI7tMxnQ9EsAY96V VL7+3p4IHA9YV/96ft84uRaHlZrsgMgMNCNJp1HOmIMFRkY5dT1PjjysN+gSHZ1yWPX0 AEhAH18JuyriDkUYsQ59N69cTojFv1GQURmuV/Q7BSXKR0KcbjrX62Iih11RVHi3t8aI TatrOJgb4fdd5I6Y5W+ZRHIiVrF+ToiLds7mlvHm/KSJCHcbC6RCorv87Eku00pnr69i 1liBe58D7TpMT8fgZqamToiDUrCBY7ayWEZGH5JPWb/OfFBZTDRKrlShVQVEQosC/qQ7 AmfQ== X-Gm-Message-State: AOAM5335inLwRnrfKxzp7wwRtRbxSEam3wWtdU+82SgTyHMcRMSHywnM kSE5RHqw77FHjrnPuK4uApI1dw== X-Google-Smtp-Source: ABdhPJzQEN7kGdbkSn24up+1LDZ/qXsvJpM2G0u9HKPFAl5rNW1HAAcvs7gnuqowznk35PO5lL236Q== X-Received: by 2002:a17:902:b283:b029:d6:b2a7:3913 with SMTP id u3-20020a170902b283b02900d6b2a73913mr12753438plr.54.1605854777115; Thu, 19 Nov 2020 22:46:17 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.46.07 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:46:16 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 01/21] mm/memory_hotplug: Move bootmem info registration API to bootmem_info.c Date: Fri, 20 Nov 2020 14:43:05 +0800 Message-Id: <20201120064325.34492-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: Move bootmem info registration common API to individual bootmem_info.c for later patch use. This is just code movement without any functional change. Signed-off-by: Muchun Song Acked-by: Mike Kravetz Reviewed-by: Oscar Salvador --- arch/x86/mm/init_64.c | 1 + include/linux/bootmem_info.h | 27 ++++++++++++ include/linux/memory_hotplug.h | 23 ---------- mm/Makefile | 1 + mm/bootmem_info.c | 99 ++++++++++++++++++++++++++++++++++++++++++ mm/memory_hotplug.c | 91 +------------------------------------- 6 files changed, 129 insertions(+), 113 deletions(-) create mode 100644 include/linux/bootmem_info.h create mode 100644 mm/bootmem_info.c diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index b5a3fa4033d3..c7f7ad55b625 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include diff --git a/include/linux/bootmem_info.h b/include/linux/bootmem_info.h new file mode 100644 index 000000000000..65bb9b23140f --- /dev/null +++ b/include/linux/bootmem_info.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __LINUX_BOOTMEM_INFO_H +#define __LINUX_BOOTMEM_INFO_H + +#include + +/* + * Types for free bootmem stored in page->lru.next. These have to be in + * some random range in unsigned long space for debugging purposes. + */ +enum { + MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE = 12, + SECTION_INFO = MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE, + MIX_SECTION_INFO, + NODE_INFO, + MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE = NODE_INFO, +}; + +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE +void __init register_page_bootmem_info_node(struct pglist_data *pgdat); +#else +static inline void register_page_bootmem_info_node(struct pglist_data *pgdat) +{ +} +#endif + +#endif /* __LINUX_BOOTMEM_INFO_H */ diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 51a877fec8da..19e5d067294c 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -33,18 +33,6 @@ struct vmem_altmap; ___page; \ }) -/* - * Types for free bootmem stored in page->lru.next. These have to be in - * some random range in unsigned long space for debugging purposes. - */ -enum { - MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE = 12, - SECTION_INFO = MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE, - MIX_SECTION_INFO, - NODE_INFO, - MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE = NODE_INFO, -}; - /* Types for control the zone type of onlined and offlined memory */ enum { /* Offline the memory. */ @@ -209,13 +197,6 @@ static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat) #endif /* CONFIG_NUMA */ #endif /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */ -#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE -extern void __init register_page_bootmem_info_node(struct pglist_data *pgdat); -#else -static inline void register_page_bootmem_info_node(struct pglist_data *pgdat) -{ -} -#endif extern void put_page_bootmem(struct page *page); extern void get_page_bootmem(unsigned long ingo, struct page *page, unsigned long type); @@ -254,10 +235,6 @@ static inline int mhp_notimplemented(const char *func) return -ENOSYS; } -static inline void register_page_bootmem_info_node(struct pglist_data *pgdat) -{ -} - static inline int try_online_node(int nid) { return 0; diff --git a/mm/Makefile b/mm/Makefile index d5649f1c12c0..752111587c99 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -82,6 +82,7 @@ obj-$(CONFIG_SLAB) += slab.o obj-$(CONFIG_SLUB) += slub.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_FAILSLAB) += failslab.o +obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o obj-$(CONFIG_MEMTEST) += memtest.o obj-$(CONFIG_MIGRATION) += migrate.o diff --git a/mm/bootmem_info.c b/mm/bootmem_info.c new file mode 100644 index 000000000000..39fa8fc120bc --- /dev/null +++ b/mm/bootmem_info.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * linux/mm/bootmem_info.c + * + * Copyright (C) + */ +#include +#include +#include +#include +#include + +#ifndef CONFIG_SPARSEMEM_VMEMMAP +static void register_page_bootmem_info_section(unsigned long start_pfn) +{ + unsigned long mapsize, section_nr, i; + struct mem_section *ms; + struct page *page, *memmap; + struct mem_section_usage *usage; + + section_nr = pfn_to_section_nr(start_pfn); + ms = __nr_to_section(section_nr); + + /* Get section's memmap address */ + memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); + + /* + * Get page for the memmap's phys address + * XXX: need more consideration for sparse_vmemmap... + */ + page = virt_to_page(memmap); + mapsize = sizeof(struct page) * PAGES_PER_SECTION; + mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; + + /* remember memmap's page */ + for (i = 0; i < mapsize; i++, page++) + get_page_bootmem(section_nr, page, SECTION_INFO); + + usage = ms->usage; + page = virt_to_page(usage); + + mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; + + for (i = 0; i < mapsize; i++, page++) + get_page_bootmem(section_nr, page, MIX_SECTION_INFO); + +} +#else /* CONFIG_SPARSEMEM_VMEMMAP */ +static void register_page_bootmem_info_section(unsigned long start_pfn) +{ + unsigned long mapsize, section_nr, i; + struct mem_section *ms; + struct page *page, *memmap; + struct mem_section_usage *usage; + + section_nr = pfn_to_section_nr(start_pfn); + ms = __nr_to_section(section_nr); + + memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); + + register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); + + usage = ms->usage; + page = virt_to_page(usage); + + mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; + + for (i = 0; i < mapsize; i++, page++) + get_page_bootmem(section_nr, page, MIX_SECTION_INFO); +} +#endif /* !CONFIG_SPARSEMEM_VMEMMAP */ + +void __init register_page_bootmem_info_node(struct pglist_data *pgdat) +{ + unsigned long i, pfn, end_pfn, nr_pages; + int node = pgdat->node_id; + struct page *page; + + nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; + page = virt_to_page(pgdat); + + for (i = 0; i < nr_pages; i++, page++) + get_page_bootmem(node, page, NODE_INFO); + + pfn = pgdat->node_start_pfn; + end_pfn = pgdat_end_pfn(pgdat); + + /* register section info */ + for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { + /* + * Some platforms can assign the same pfn to multiple nodes - on + * node0 as well as nodeN. To avoid registering a pfn against + * multiple nodes we check that this pfn does not already + * reside in some other nodes. + */ + if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) + register_page_bootmem_info_section(pfn); + } +} diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index baded53b9ff9..2da4ad071456 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -167,96 +168,6 @@ void put_page_bootmem(struct page *page) } } -#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE -#ifndef CONFIG_SPARSEMEM_VMEMMAP -static void register_page_bootmem_info_section(unsigned long start_pfn) -{ - unsigned long mapsize, section_nr, i; - struct mem_section *ms; - struct page *page, *memmap; - struct mem_section_usage *usage; - - section_nr = pfn_to_section_nr(start_pfn); - ms = __nr_to_section(section_nr); - - /* Get section's memmap address */ - memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); - - /* - * Get page for the memmap's phys address - * XXX: need more consideration for sparse_vmemmap... - */ - page = virt_to_page(memmap); - mapsize = sizeof(struct page) * PAGES_PER_SECTION; - mapsize = PAGE_ALIGN(mapsize) >> PAGE_SHIFT; - - /* remember memmap's page */ - for (i = 0; i < mapsize; i++, page++) - get_page_bootmem(section_nr, page, SECTION_INFO); - - usage = ms->usage; - page = virt_to_page(usage); - - mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; - - for (i = 0; i < mapsize; i++, page++) - get_page_bootmem(section_nr, page, MIX_SECTION_INFO); - -} -#else /* CONFIG_SPARSEMEM_VMEMMAP */ -static void register_page_bootmem_info_section(unsigned long start_pfn) -{ - unsigned long mapsize, section_nr, i; - struct mem_section *ms; - struct page *page, *memmap; - struct mem_section_usage *usage; - - section_nr = pfn_to_section_nr(start_pfn); - ms = __nr_to_section(section_nr); - - memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); - - register_page_bootmem_memmap(section_nr, memmap, PAGES_PER_SECTION); - - usage = ms->usage; - page = virt_to_page(usage); - - mapsize = PAGE_ALIGN(mem_section_usage_size()) >> PAGE_SHIFT; - - for (i = 0; i < mapsize; i++, page++) - get_page_bootmem(section_nr, page, MIX_SECTION_INFO); -} -#endif /* !CONFIG_SPARSEMEM_VMEMMAP */ - -void __init register_page_bootmem_info_node(struct pglist_data *pgdat) -{ - unsigned long i, pfn, end_pfn, nr_pages; - int node = pgdat->node_id; - struct page *page; - - nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; - page = virt_to_page(pgdat); - - for (i = 0; i < nr_pages; i++, page++) - get_page_bootmem(node, page, NODE_INFO); - - pfn = pgdat->node_start_pfn; - end_pfn = pgdat_end_pfn(pgdat); - - /* register section info */ - for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) { - /* - * Some platforms can assign the same pfn to multiple nodes - on - * node0 as well as nodeN. To avoid registering a pfn against - * multiple nodes we check that this pfn does not already - * reside in some other nodes. - */ - if (pfn_valid(pfn) && (early_pfn_to_nid(pfn) == node)) - register_page_bootmem_info_section(pfn); - } -} -#endif /* CONFIG_HAVE_BOOTMEM_INFO_NODE */ - static int check_pfn_span(unsigned long pfn, unsigned long nr_pages, const char *reason) { From patchwork Fri Nov 20 06:43:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56583C64E69 for ; Fri, 20 Nov 2020 06:46:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BFC83223B0 for ; Fri, 20 Nov 2020 06:46:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="pwmEpgcz" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BFC83223B0 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 438076B0070; Fri, 20 Nov 2020 01:46:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E5886B0071; Fri, 20 Nov 2020 01:46:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D54D6B0072; Fri, 20 Nov 2020 01:46:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id E3ADA6B0070 for ; Fri, 20 Nov 2020 01:46:30 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 68AC11EF2 for ; Fri, 20 Nov 2020 06:46:30 +0000 (UTC) X-FDA: 77503862940.24.field08_100dd6a27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id 484131A4A0 for ; Fri, 20 Nov 2020 06:46:30 +0000 (UTC) X-HE-Tag: field08_100dd6a27349 X-Filterd-Recvd-Size: 8885 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf04.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:46:29 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id v5so2884483pff.10 for ; Thu, 19 Nov 2020 22:46:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YQYB74FkwOxFI1h+l2VXl2pw2h4ZcuxqFh5PVtqZk4E=; b=pwmEpgczKBWSrmu3+OTWkDG1aT41ToDlPRJelOynJ/7xzSG5T0D4fsfdFjuI7ljqAy U+cpoGvvk2ZqTCKQVrnjxeLcs/1S5G/LaBpHsKXLzxkxuSGwA1N9l29P5WZiN7PqciiW ffE3Iq2mUoM+CLcwlifGlIqqMbzHmRS2hLZcqvXkm7tFQn9U45LGm7QRnmlIRNitgh5u pl2alLzUNNEtAwOXUSelwHy8bCkcjSJGg1zejEhwXm3ifjS4D/LAAtYotCCMVBQEAd18 +CQ3jM7fB89tnMcGuyu8wj55wbRW2jrodEQLFvqI97FbnQO4kyees+R6oXAAAW/KuWSJ TFiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YQYB74FkwOxFI1h+l2VXl2pw2h4ZcuxqFh5PVtqZk4E=; b=Kca0MANpFSqMIWC80JmKPkihHNlRHQRkBJufeymbHg3+baeU0o+0gDYCT7oQlOIwFM K6MozG+l5HsA1BAGL5JRjABeEbH8r3HfXRDiosb3S+xEoQmhrp75Fqnfutcd7hGi/VH6 AYIFLcC7pbi43qizDIU+5xo6c/OZBqXfgNZRBX8PvjS/YwuGCwE/XIip16FdOCXSqLbg cwphAxVE9s5n1VpJ9xptCBLG3MUPHQupwSm5ltEl4Bm26eihHW693XvCwKkb+Zak16qZ wG7XD3OdrJhMhN97VMJ0W+M8L9CgMM9V8xnIvwAXkMLylo4NZmEfJV4Q161UPqINPd+i fGtw== X-Gm-Message-State: AOAM5312Hz5pBBMbdCFMsU/Pcet61kjLiTmHseDO/zO7qDdS2PS/2MUu 7XCjF3yjfuerGFCS/qFVnA1pEQ== X-Google-Smtp-Source: ABdhPJy/Lr2EMMaN4T7P86gH4a5O1RgE6FVFDvRJ7NZ9ZPNffEsEmVe1gyL+ORGEMts78lSncyF5dA== X-Received: by 2002:a17:90a:5e43:: with SMTP id u3mr7794752pji.216.1605854788065; Thu, 19 Nov 2020 22:46:28 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.46.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:46:27 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 02/21] mm/memory_hotplug: Move {get,put}_page_bootmem() to bootmem_info.c Date: Fri, 20 Nov 2020 14:43:06 +0800 Message-Id: <20201120064325.34492-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: In the later patch, we will use {get,put}_page_bootmem() to initialize the page for vmemmap or free vmemmap page to buddy. So move them out of CONFIG_MEMORY_HOTPLUG_SPARSE. This is just code movement without any functional change. Signed-off-by: Muchun Song Acked-by: Mike Kravetz Reviewed-by: Oscar Salvador --- arch/x86/mm/init_64.c | 2 +- include/linux/bootmem_info.h | 13 +++++++++++++ include/linux/memory_hotplug.h | 4 ---- mm/bootmem_info.c | 25 +++++++++++++++++++++++++ mm/memory_hotplug.c | 27 --------------------------- mm/sparse.c | 1 + 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index c7f7ad55b625..0a45f062826e 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1572,7 +1572,7 @@ int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node, return err; } -#if defined(CONFIG_MEMORY_HOTPLUG_SPARSE) && defined(CONFIG_HAVE_BOOTMEM_INFO_NODE) +#ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void register_page_bootmem_memmap(unsigned long section_nr, struct page *start_page, unsigned long nr_pages) { diff --git a/include/linux/bootmem_info.h b/include/linux/bootmem_info.h index 65bb9b23140f..4ed6dee1adc9 100644 --- a/include/linux/bootmem_info.h +++ b/include/linux/bootmem_info.h @@ -18,10 +18,23 @@ enum { #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void __init register_page_bootmem_info_node(struct pglist_data *pgdat); + +void get_page_bootmem(unsigned long info, struct page *page, + unsigned long type); +void put_page_bootmem(struct page *page); #else static inline void register_page_bootmem_info_node(struct pglist_data *pgdat) { } + +static inline void put_page_bootmem(struct page *page) +{ +} + +static inline void get_page_bootmem(unsigned long info, struct page *page, + unsigned long type) +{ +} #endif #endif /* __LINUX_BOOTMEM_INFO_H */ diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 19e5d067294c..c9f3361fe84b 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -197,10 +197,6 @@ static inline void arch_refresh_nodedata(int nid, pg_data_t *pgdat) #endif /* CONFIG_NUMA */ #endif /* CONFIG_HAVE_ARCH_NODEDATA_EXTENSION */ -extern void put_page_bootmem(struct page *page); -extern void get_page_bootmem(unsigned long ingo, struct page *page, - unsigned long type); - void get_online_mems(void); void put_online_mems(void); diff --git a/mm/bootmem_info.c b/mm/bootmem_info.c index 39fa8fc120bc..fcab5a3f8cc0 100644 --- a/mm/bootmem_info.c +++ b/mm/bootmem_info.c @@ -10,6 +10,31 @@ #include #include +void get_page_bootmem(unsigned long info, struct page *page, unsigned long type) +{ + page->freelist = (void *)type; + SetPagePrivate(page); + set_page_private(page, info); + page_ref_inc(page); +} + +void put_page_bootmem(struct page *page) +{ + unsigned long type; + + type = (unsigned long) page->freelist; + BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || + type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); + + if (page_ref_dec_return(page) == 1) { + page->freelist = NULL; + ClearPagePrivate(page); + set_page_private(page, 0); + INIT_LIST_HEAD(&page->lru); + free_reserved_page(page); + } +} + #ifndef CONFIG_SPARSEMEM_VMEMMAP static void register_page_bootmem_info_section(unsigned long start_pfn) { diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 2da4ad071456..ae57eedc341f 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -142,32 +141,6 @@ static void release_memory_resource(struct resource *res) } #ifdef CONFIG_MEMORY_HOTPLUG_SPARSE -void get_page_bootmem(unsigned long info, struct page *page, - unsigned long type) -{ - page->freelist = (void *)type; - SetPagePrivate(page); - set_page_private(page, info); - page_ref_inc(page); -} - -void put_page_bootmem(struct page *page) -{ - unsigned long type; - - type = (unsigned long) page->freelist; - BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || - type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); - - if (page_ref_dec_return(page) == 1) { - page->freelist = NULL; - ClearPagePrivate(page); - set_page_private(page, 0); - INIT_LIST_HEAD(&page->lru); - free_reserved_page(page); - } -} - static int check_pfn_span(unsigned long pfn, unsigned long nr_pages, const char *reason) { diff --git a/mm/sparse.c b/mm/sparse.c index b25ad8e64839..a4138410d890 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "internal.h" #include From patchwork Fri Nov 20 06:43:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919567 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 61674C56201 for ; Fri, 20 Nov 2020 06:46:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C7D4F223BE for ; Fri, 20 Nov 2020 06:46:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="ddlgsc9a" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C7D4F223BE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 213B76B0072; Fri, 20 Nov 2020 01:46:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C2EF6B0073; Fri, 20 Nov 2020 01:46:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D96F6B0074; Fri, 20 Nov 2020 01:46:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0134.hostedemail.com [216.40.44.134]) by kanga.kvack.org (Postfix) with ESMTP id C91526B0072 for ; Fri, 20 Nov 2020 01:46:39 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 62A10180AD837 for ; Fri, 20 Nov 2020 06:46:39 +0000 (UTC) X-FDA: 77503863318.19.nose57_3a09d9627349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin19.hostedemail.com (Postfix) with ESMTP id 48B141AD1B9 for ; Fri, 20 Nov 2020 06:46:39 +0000 (UTC) X-HE-Tag: nose57_3a09d9627349 X-Filterd-Recvd-Size: 5271 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf10.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:46:38 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id g7so6941681pfc.2 for ; Thu, 19 Nov 2020 22:46:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HbZE3hLPN+y4fZ+XDemkVZGrMn++MM+PXc+bcKxkyTw=; b=ddlgsc9aJsqPVq5fGeJt+X/ZQDQwAN5aZciP0XGHarhI6Nw/kv2uYbf8pALflOPDng NlHcU4f7e6w+J7m5KV07mvu6AWmk3bFVO2ZiH13JRIR04zcMF0SYwHKCN+48MZj2d+6X CFRtJzAwC9PpdgLDHOzJsVovdSdoByQtLk3NuNZY8tSHaBjkFhnCSBiIbgIZytARMijD +TEAHTXGjLuO8DrcIfZZQsESQXaE2wlc76W0xFNwSnh0ntthmym0fyZWvgYm4iBxkevy Ly6rL9wS7cDAvGp6z05ix/wjT46BCMjRj9a43QVrQ01leZmx90byJCQ+Qe74wu6mFqZp Sdfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HbZE3hLPN+y4fZ+XDemkVZGrMn++MM+PXc+bcKxkyTw=; b=acDZEEpJDu3A48u1ucev1mgtJSYDWW2uUQYNyfRj8PvjoB8HUZuqrm6yvFPpb8ouEI qXwGZYA3qtY/7/iUaFSHiQjPlp5QYUvUbnce1LbSvl4Uq66Od2zm236K6NVqDGESLZFx mLLpu248nYP/GpvySIjXYgdu5lOFug6VUmEmsgq4uCcC+cipTXXokSMeSWY7kvVtW1ZP DP91LUHnSFEh9mrQ2QDR/j697AWTqTRYOmPKI0ZpbkNIOoFcp6VbFvoqztBwJv31Uwht NlOcbkeapkHPc1/MrAFsAG7dFPcYjjj67OfHhjXOUj5xotMtOzzleNVwnuWtTPsYyXVL xacg== X-Gm-Message-State: AOAM5315N5PjNcrYv2L3BLA1xlKwZurVVbn9zeMaoiF7mDwlaNqiMFgH pXiM20bBO9wvHgr1thf5n+S1NA== X-Google-Smtp-Source: ABdhPJwrv3QKWbdRQidbZcDMmub+4jw1ViBixLnsgA9kLSF0wXLcGC+wV5JBbDHtGxKTMQPT1ZHxvQ== X-Received: by 2002:a17:90a:4dc8:: with SMTP id r8mr8278165pjl.1.1605854797793; Thu, 19 Nov 2020 22:46:37 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.46.28 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:46:37 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 03/21] mm/hugetlb: Introduce a new config HUGETLB_PAGE_FREE_VMEMMAP Date: Fri, 20 Nov 2020 14:43:07 +0800 Message-Id: <20201120064325.34492-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: The purpose of introducing HUGETLB_PAGE_FREE_VMEMMAP is to configure whether to enable the feature of freeing unused vmemmap associated with HugeTLB pages. Now only support x86. Signed-off-by: Muchun Song --- arch/x86/mm/init_64.c | 2 +- fs/Kconfig | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 0a45f062826e..0435bee2e172 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1225,7 +1225,7 @@ static struct kcore_list kcore_vsyscall; static void __init register_page_bootmem_info(void) { -#ifdef CONFIG_NUMA +#if defined(CONFIG_NUMA) || defined(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP) int i; for_each_online_node(i) diff --git a/fs/Kconfig b/fs/Kconfig index 976e8b9033c4..4961dd488444 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -245,6 +245,20 @@ config HUGETLBFS config HUGETLB_PAGE def_bool HUGETLBFS +config HUGETLB_PAGE_FREE_VMEMMAP + def_bool HUGETLB_PAGE + depends on X86 + depends on SPARSEMEM_VMEMMAP + depends on HAVE_BOOTMEM_INFO_NODE + help + When using HUGETLB_PAGE_FREE_VMEMMAP, the system can save up some + memory from pre-allocated HugeTLB pages when they are not used. + 6 pages per 2MB HugeTLB page and 4094 per 1GB HugeTLB page. + + When the pages are going to be used or freed up, the vmemmap array + representing that range needs to be remapped again and the pages + we discarded earlier need to be rellocated again. + config MEMFD_CREATE def_bool TMPFS || HUGETLBFS From patchwork Fri Nov 20 06:43:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919569 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A41DC5519F for ; Fri, 20 Nov 2020 06:46:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 97F8D2222F for ; Fri, 20 Nov 2020 06:46:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="EBLSrBH7" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 97F8D2222F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2AE3D6B0074; Fri, 20 Nov 2020 01:46:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2850A6B0075; Fri, 20 Nov 2020 01:46:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 127A96B007B; Fri, 20 Nov 2020 01:46:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0067.hostedemail.com [216.40.44.67]) by kanga.kvack.org (Postfix) with ESMTP id CE67E6B0074 for ; Fri, 20 Nov 2020 01:46:50 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 6FDDA3649 for ; Fri, 20 Nov 2020 06:46:50 +0000 (UTC) X-FDA: 77503863780.07.road42_20129ba27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin07.hostedemail.com (Postfix) with ESMTP id 5409B1808E59D for ; Fri, 20 Nov 2020 06:46:50 +0000 (UTC) X-HE-Tag: road42_20129ba27349 X-Filterd-Recvd-Size: 14822 Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:46:49 +0000 (UTC) Received: by mail-pg1-f176.google.com with SMTP id 34so6472383pgp.10 for ; Thu, 19 Nov 2020 22:46:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iRfIaKfSk4gw/H9VYs5z/FHDAf2tShQGD8WKzq8TDA8=; b=EBLSrBH7YeOd2gjIHGCeLdFlu9RrgT3qC1HA+1fPvgY+Q4xicVAgK/OeJsWKFWkYhl qwOQlkBpFp7Dfni0pBAXrx3dhG/9qZmVx3gOAtQcrSnpdJKAWBKyITosLbsYRV3fPfUK apXco7AuKdfkyRhgx1BrWO3c9YTkeTlGJ+Y4ZfEl9QdxQ4csoXUbWKF/RUMmtOtLgH8q 4iLcypWcOd+b4+CZjYPeYTDz9hyMCqGjCTrrOtNG157N5kH17iS0S/oT8yDy0HhTasNH gLu4dyls4iUupLwNABjxSZMiDCVnF20ibjdo07/vhFpK4UrmmT0NsiRsx6UQPq3SLn5n Miiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iRfIaKfSk4gw/H9VYs5z/FHDAf2tShQGD8WKzq8TDA8=; b=Mh1EV4TdiMOT8L33ovLDtqZAJSRzERd6XmPs7ZX7/F+pn6Gh0roMopJo8Z645faCZY dwlctEz2REjAcVG1sm0C8F9i6RrxmRfc9qDztNtBiUI0GgjOnO+smDwTUvis8jP7J7Qt KNGbICyoBP1+hLu/uETVOFC2zBmlzPezDG8lSoWXCvmAuvZG9hMBnImyAgGkt3LOV6pN /LjkZZ1DWxkDr+fx3UYuIq3mRWmyjSW+XrysAn0gFVofmkBrVB6DGwW+u4Oy1RnQoyOI GJwPR9dhfQ6oXsGuesafohShjTFIuo3HVMSi9TsyN2mi9nrfFxbr57mxcyRKgczNxO7b Zo0Q== X-Gm-Message-State: AOAM533ZL/oOeOm/yoVCJZnhobwPRsFqZU/wsQhl+zorPEOHmvDoGz0t mufVq9XDxwYPWLNWVVITRBBdFg== X-Google-Smtp-Source: ABdhPJxQ8aHtUvLLx7J9LD29ryu8DiJo4QeNBWYWaHjPcLOfy1mi3QvFyj2UQ+zvYgJo7e2EUavhVA== X-Received: by 2002:a62:7e81:0:b029:197:c3e2:4ad9 with SMTP id z123-20020a627e810000b0290197c3e24ad9mr5062995pfc.35.1605854808233; Thu, 19 Nov 2020 22:46:48 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.46.38 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:46:47 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 04/21] mm/hugetlb: Introduce nr_free_vmemmap_pages in the struct hstate Date: Fri, 20 Nov 2020 14:43:08 +0800 Message-Id: <20201120064325.34492-5-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: Every HugeTLB has more than one struct page structure. The 2M HugeTLB has 512 struct page structure and 1G HugeTLB has 4096 struct page structures. We __know__ that we only use the first 4(HUGETLB_CGROUP_MIN_ORDER) struct page structures to store metadata associated with each HugeTLB. There are a lot of struct page structures(8 page frames for 2MB HugeTLB page and 4096 page frames for 1GB HugeTLB page) associated with each HugeTLB page. For tail pages, the value of compound_head is the same. So we can reuse first page of tail page structures. We map the virtual addresses of the remaining pages of tail page structures to the first tail page struct, and then free these page frames. Therefore, we need to reserve two pages as vmemmap areas. So we introduce a new nr_free_vmemmap_pages field in the hstate to indicate how many vmemmap pages associated with a HugeTLB page that we can free to buddy system. Signed-off-by: Muchun Song Acked-by: Mike Kravetz --- include/linux/hugetlb.h | 3 ++ mm/Makefile | 1 + mm/hugetlb.c | 3 ++ mm/hugetlb_vmemmap.c | 134 ++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 20 ++++++++ 5 files changed, 161 insertions(+) create mode 100644 mm/hugetlb_vmemmap.c create mode 100644 mm/hugetlb_vmemmap.h diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index d5cc5f802dd4..eed3dd3bd626 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -492,6 +492,9 @@ struct hstate { unsigned int nr_huge_pages_node[MAX_NUMNODES]; unsigned int free_huge_pages_node[MAX_NUMNODES]; unsigned int surplus_huge_pages_node[MAX_NUMNODES]; +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP + unsigned int nr_free_vmemmap_pages; +#endif #ifdef CONFIG_CGROUP_HUGETLB /* cgroup control files */ struct cftype cgroup_files_dfl[7]; diff --git a/mm/Makefile b/mm/Makefile index 752111587c99..2a734576bbc0 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -71,6 +71,7 @@ obj-$(CONFIG_FRONTSWAP) += frontswap.o obj-$(CONFIG_ZSWAP) += zswap.o obj-$(CONFIG_HAS_DMA) += dmapool.o obj-$(CONFIG_HUGETLBFS) += hugetlb.o +obj-$(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP) += hugetlb_vmemmap.o obj-$(CONFIG_NUMA) += mempolicy.o obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 81a41aa080a5..f88032c24667 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -42,6 +42,7 @@ #include #include #include "internal.h" +#include "hugetlb_vmemmap.h" int hugetlb_max_hstate __read_mostly; unsigned int default_hstate_idx; @@ -3285,6 +3286,8 @@ void __init hugetlb_add_hstate(unsigned int order) snprintf(h->name, HSTATE_NAME_LEN, "hugepages-%lukB", huge_page_size(h)/1024); + hugetlb_vmemmap_init(h); + parsed_hstate = h; } diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c new file mode 100644 index 000000000000..1afe245395e5 --- /dev/null +++ b/mm/hugetlb_vmemmap.c @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Free some vmemmap pages of HugeTLB + * + * Copyright (c) 2020, Bytedance. All rights reserved. + * + * Author: Muchun Song + * + * The struct page structures (page structs) are used to describe a physical + * page frame. By default, there is a one-to-one mapping from a page frame to + * it's corresponding page struct. + * + * The HugeTLB pages consist of multiple base page size pages and is supported + * by many architectures. See hugetlbpage.rst in the Documentation directory + * for more details. On the x86 architecture, HugeTLB pages of size 2MB and 1GB + * are currently supported. Since the base page size on x86 is 4KB, a 2MB + * HugeTLB page consists of 512 base pages and a 1GB HugeTLB page consists of + * 4096 base pages. For each base page, there is a corresponding page struct. + * + * Within the HugeTLB subsystem, only the first 4 page structs are used to + * contain unique information about a HugeTLB page. HUGETLB_CGROUP_MIN_ORDER + * provides this upper limit. The only 'useful' information in the remaining + * page structs is the compound_head field, and this field is the same for all + * tail pages. + * + * By removing redundant page structs for HugeTLB pages, memory can returned to + * the buddy allocator for other uses. + * + * When the system boot up, every 2M HugeTLB has 512 struct page structs which + * size is 8 pages(sizeof(struct page) * 512 / PAGE_SIZE). + * + * HugeTLB struct pages(8 pages) page frame(8 pages) + * +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ + * | | | 0 | -------------> | 0 | + * | | +-----------+ +-----------+ + * | | | 1 | -------------> | 1 | + * | | +-----------+ +-----------+ + * | | | 2 | -------------> | 2 | + * | | +-----------+ +-----------+ + * | | | 3 | -------------> | 3 | + * | | +-----------+ +-----------+ + * | | | 4 | -------------> | 4 | + * | 2MB | +-----------+ +-----------+ + * | | | 5 | -------------> | 5 | + * | | +-----------+ +-----------+ + * | | | 6 | -------------> | 6 | + * | | +-----------+ +-----------+ + * | | | 7 | -------------> | 7 | + * | | +-----------+ +-----------+ + * | | + * | | + * | | + * +-----------+ + * + * The value of page->compound_head is the same for all tail pages. The first + * page of page structs (page 0) associated with the HugeTLB page contains the 4 + * page structs necessary to describe the HugeTLB. The only use of the remaining + * pages of page structs (page 1 to page 7) is to point to page->compound_head. + * Therefore, we can remap pages 2 to 7 to page 1. Only 2 pages of page structs + * will be used for each HugeTLB page. This will allow us to free the remaining + * 6 pages to the buddy allocator. + * + * Here is how things look after remapping. + * + * HugeTLB struct pages(8 pages) page frame(8 pages) + * +-----------+ ---virt_to_page---> +-----------+ mapping to +-----------+ + * | | | 0 | -------------> | 0 | + * | | +-----------+ +-----------+ + * | | | 1 | -------------> | 1 | + * | | +-----------+ +-----------+ + * | | | 2 | ----------------^ ^ ^ ^ ^ ^ + * | | +-----------+ | | | | | + * | | | 3 | ------------------+ | | | | + * | | +-----------+ | | | | + * | | | 4 | --------------------+ | | | + * | 2MB | +-----------+ | | | + * | | | 5 | ----------------------+ | | + * | | +-----------+ | | + * | | | 6 | ------------------------+ | + * | | +-----------+ | + * | | | 7 | --------------------------+ + * | | +-----------+ + * | | + * | | + * | | + * +-----------+ + * + * When a HugeTLB is freed to the buddy system, we should allocate 6 pages for + * vmemmap pages and restore the previous mapping relationship. + * + * Apart from 2MB HugeTLB page, we also have 1GB HugeTLB page. It is similar + * to the 2MB HugeTLB page. We also can use this approach to free the vmemmap + * pages. + * + * In this case, for the 1GB HugeTLB page, we can save 4088 pages(There are + * 4096 pages for struct page structs, we reserve 2 pages for vmemmap and 8 + * pages for page tables. So we can save 4088 pages). This is a very substantial + * gain. + */ +#define pr_fmt(fmt) "HugeTLB Vmemmap: " fmt + +#include "hugetlb_vmemmap.h" + +/* + * There are a lot of struct page structures(8 page frames for 2MB HugeTLB page + * and 4096 page frames for 1GB HugeTLB page) associated with each HugeTLB page. + * For tail pages, the value of compound_head is the same. So we can reuse first + * page of tail page structures. We map the virtual addresses of the remaining + * pages of tail page structures to the first tail page struct, and then free + * these page frames. Therefore, we need to reserve two pages as vmemmap areas. + */ +#define RESERVE_VMEMMAP_NR 2U + +void __init hugetlb_vmemmap_init(struct hstate *h) +{ + unsigned int order = huge_page_order(h); + unsigned int vmemmap_pages; + + vmemmap_pages = ((1 << order) * sizeof(struct page)) >> PAGE_SHIFT; + /* + * The head page and the first tail page are not to be freed to buddy + * system, the others page will map to the first tail page. So there + * are the remaining pages that can be freed. + * + * Could RESERVE_VMEMMAP_NR be greater than @vmemmap_pages? This is + * not expected to happen unless the system is corrupted. So on the + * safe side, it is only a safety net. + */ + if (likely(vmemmap_pages > RESERVE_VMEMMAP_NR)) + h->nr_free_vmemmap_pages = vmemmap_pages - RESERVE_VMEMMAP_NR; + + pr_debug("can free %d vmemmap pages for %s\n", h->nr_free_vmemmap_pages, + h->name); +} diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h new file mode 100644 index 000000000000..40c0c7dfb60d --- /dev/null +++ b/mm/hugetlb_vmemmap.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Free some vmemmap pages of HugeTLB + * + * Copyright (c) 2020, Bytedance. All rights reserved. + * + * Author: Muchun Song + */ +#ifndef _LINUX_HUGETLB_VMEMMAP_H +#define _LINUX_HUGETLB_VMEMMAP_H +#include + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +void __init hugetlb_vmemmap_init(struct hstate *h); +#else +static inline void hugetlb_vmemmap_init(struct hstate *h) +{ +} +#endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ +#endif /* _LINUX_HUGETLB_VMEMMAP_H */ From patchwork Fri Nov 20 06:43:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919571 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1477C5519F for ; Fri, 20 Nov 2020 06:47:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2AA472222F for ; Fri, 20 Nov 2020 06:47:01 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="VmrGqdVE" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2AA472222F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 916EB6B007B; Fri, 20 Nov 2020 01:47:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EFFB6B007D; Fri, 20 Nov 2020 01:47:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 804346B007E; Fri, 20 Nov 2020 01:47:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0086.hostedemail.com [216.40.44.86]) by kanga.kvack.org (Postfix) with ESMTP id 47E3D6B007B for ; Fri, 20 Nov 2020 01:47:00 -0500 (EST) Received: from smtpin11.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id D0F5A1EF2 for ; Fri, 20 Nov 2020 06:46:59 +0000 (UTC) X-FDA: 77503864158.11.wind99_300d5c027349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin11.hostedemail.com (Postfix) with ESMTP id AE51E180F8B86 for ; Fri, 20 Nov 2020 06:46:59 +0000 (UTC) X-HE-Tag: wind99_300d5c027349 X-Filterd-Recvd-Size: 7629 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf15.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:46:59 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id 34so6472691pgp.10 for ; Thu, 19 Nov 2020 22:46:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=88Vyf1Pcm29kb9drVzemkW2MmMhaFRrGbBNvTcfU0mI=; b=VmrGqdVEOmmHQIibPHpVJJjw83uIZZNbNV7ajzRc+ac94AKkBDewjWcEn0w1pLdNww SYgU3lDsbdUs2nBjB9CVxNVWksSQOgZSz7RhoKAsntzXOatfNxp/bnOl96fstM6HxRWc Qw55+TZP9tbhXQXk7PXbT3kZFCGah5DtZGIH2jzYKhqRynAxkqd+qkdAPrV/wo8sBlhw 5V1y3bMzOQSzjsuoyXb5S74L1+IlvSnynuGzUEpYLTQJy1tZjK0CxMF9LfsMR2ILa70e Fpc7/r2DvD1XMTOSAbac2R+eixL4bGYtNgC6cBB2bYxFmZ0F5CgUSwqb6xIeeGSJ7a70 7dFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=88Vyf1Pcm29kb9drVzemkW2MmMhaFRrGbBNvTcfU0mI=; b=U7qlJIU9uPHjoaIyc15phcsyRPxHDmMv19eNTm934cTXJU5DhwFuSPJFZnct2hXVx1 BxhF+i0DEAOJnfRf+ax/fkrq3X16cLmyz78jWOpqWbE/AN2NbgGkfvy8OAg7etDoaj+v 4uu2umxGYAjUaVvOUhQgDvzZpL0d9eZvrFUnJo41P+6hGlfLMrKCE+V2UC/iqdRWqDFc 4kzb5PE0MbXQjePOeSEh7vgCPcRVcJhZ+lQ5GTkelltaJ1NSIJr3WZ2c9XVhANKCwRl6 V57wKx5Qt9qU6GQLSpcraw/ZOclh5TRpnMR1mwOvD8/NDnBX9dadzb5xq6gB2yaONz7U iumQ== X-Gm-Message-State: AOAM530au4uhSSLwhDIGtw/LXvQACHRSjsmk5klwhdSb+q5y9EaPnlX7 DzE3qi/+UJvQDI3pjKvjdrOvOg== X-Google-Smtp-Source: ABdhPJyA2pbz2sUo0O1TKkJnOt8g0Z0zF62vjdl9xLhDjvD5IAhMOMWolvPOKpNYn7tiG+C8sefhJw== X-Received: by 2002:aa7:8552:0:b029:18e:f030:e514 with SMTP id y18-20020aa785520000b029018ef030e514mr12036552pfn.2.1605854818095; Thu, 19 Nov 2020 22:46:58 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.46.48 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:46:57 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 05/21] mm/hugetlb: Introduce pgtable allocation/freeing helpers Date: Fri, 20 Nov 2020 14:43:09 +0800 Message-Id: <20201120064325.34492-6-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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 x86_64, vmemmap is always PMD mapped if the machine has hugepages support and if we have 2MB contiguous pages and PMD alignment. If we want to free the unused vmemmap pages, we have to split the huge PMD firstly. So we should pre-allocate pgtable to split PMD to PTE. Signed-off-by: Muchun Song Suggested-by: Oscar Salvador Acked-by: Mike Kravetz --- mm/hugetlb_vmemmap.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 11 ++++++++ 2 files changed, 87 insertions(+) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 1afe245395e5..ec70980000d8 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -99,6 +99,8 @@ */ #define pr_fmt(fmt) "HugeTLB Vmemmap: " fmt +#include +#include #include "hugetlb_vmemmap.h" /* @@ -111,6 +113,80 @@ */ #define RESERVE_VMEMMAP_NR 2U +#ifndef VMEMMAP_HPAGE_SHIFT +#define VMEMMAP_HPAGE_SHIFT HPAGE_SHIFT +#endif +#define VMEMMAP_HPAGE_ORDER (VMEMMAP_HPAGE_SHIFT - PAGE_SHIFT) +#define VMEMMAP_HPAGE_NR (1 << VMEMMAP_HPAGE_ORDER) +#define VMEMMAP_HPAGE_SIZE ((1UL) << VMEMMAP_HPAGE_SHIFT) +#define VMEMMAP_HPAGE_MASK (~(VMEMMAP_HPAGE_SIZE - 1)) + +static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) +{ + return h->nr_free_vmemmap_pages; +} + +static inline unsigned int vmemmap_pages_per_hpage(struct hstate *h) +{ + return free_vmemmap_pages_per_hpage(h) + RESERVE_VMEMMAP_NR; +} + +static inline unsigned long vmemmap_pages_size_per_hpage(struct hstate *h) +{ + return (unsigned long)vmemmap_pages_per_hpage(h) << PAGE_SHIFT; +} + +static inline unsigned int pgtable_pages_to_prealloc_per_hpage(struct hstate *h) +{ + unsigned long vmemmap_size = vmemmap_pages_size_per_hpage(h); + + /* + * No need pre-allocate page tables when there is no vmemmap pages + * to free. + */ + if (!free_vmemmap_pages_per_hpage(h)) + return 0; + + return ALIGN(vmemmap_size, VMEMMAP_HPAGE_SIZE) >> VMEMMAP_HPAGE_SHIFT; +} + +void vmemmap_pgtable_free(struct page *page) +{ + struct page *pte_page, *t_page; + + list_for_each_entry_safe(pte_page, t_page, &page->lru, lru) { + list_del(&pte_page->lru); + pte_free_kernel(&init_mm, page_to_virt(pte_page)); + } +} + +int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page) +{ + unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h); + + /* + * Use the huge page lru list to temporarily store the preallocated + * pages. The preallocated pages are used and the list is emptied + * before the huge page is put into use. When the huge page is put + * into use by prep_new_huge_page() the list will be reinitialized. + */ + INIT_LIST_HEAD(&page->lru); + + while (nr--) { + pte_t *pte_p; + + pte_p = pte_alloc_one_kernel(&init_mm); + if (!pte_p) + goto out; + list_add(&virt_to_page(pte_p)->lru, &page->lru); + } + + return 0; +out: + vmemmap_pgtable_free(page); + return -ENOMEM; +} + void __init hugetlb_vmemmap_init(struct hstate *h) { unsigned int order = huge_page_order(h); diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 40c0c7dfb60d..9eca6879c0a4 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -12,9 +12,20 @@ #ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP void __init hugetlb_vmemmap_init(struct hstate *h); +int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page); +void vmemmap_pgtable_free(struct page *page); #else static inline void hugetlb_vmemmap_init(struct hstate *h) { } + +static inline int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page) +{ + return 0; +} + +static inline void vmemmap_pgtable_free(struct page *page) +{ +} #endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */ From patchwork Fri Nov 20 06:43:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919573 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A66AC5519F for ; Fri, 20 Nov 2020 06:47:12 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id EDAE52222F for ; Fri, 20 Nov 2020 06:47:10 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="lQtTc4p2" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EDAE52222F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6F6BF6B007E; Fri, 20 Nov 2020 01:47:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6CCA96B0080; Fri, 20 Nov 2020 01:47:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5E2516B0081; Fri, 20 Nov 2020 01:47:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0070.hostedemail.com [216.40.44.70]) by kanga.kvack.org (Postfix) with ESMTP id 273C76B007E for ; Fri, 20 Nov 2020 01:47:10 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id A4879180AD837 for ; Fri, 20 Nov 2020 06:47:09 +0000 (UTC) X-FDA: 77503864578.17.pain07_001129527349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin17.hostedemail.com (Postfix) with ESMTP id 8CFCF180D0181 for ; Fri, 20 Nov 2020 06:47:09 +0000 (UTC) X-HE-Tag: pain07_001129527349 X-Filterd-Recvd-Size: 5321 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf16.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:47:09 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id q28so6504805pgk.1 for ; Thu, 19 Nov 2020 22:47:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9VN22FTK/cp8gibu50+JlwycxClP9nhjODvnGGV4tug=; b=lQtTc4p2yBd1UOH2ODIo4IfUun/3YveKsroBrt/z05tHCQ7bizuXanOEqJk1Qjjutd utIgpzzvRhDMQ6v/t6lQVerFUuFfpWtu7NjhOTcfNxn2JdmEke2/UD1WxhMDXygCHzF3 b/hXHUxHWkm6lSBs1d9sc0tIDhhA2ndRN0NkdUh8caMOLwtchHRhhGN5UyShY8Wemd06 mjBE+2Pl3whNHKbLaFl0ukQ31HqaNNWzxH2cK4B5ZG6H5Hf0H/wqZ8x6TegDWiWypdMZ URFjKhezi7NvGxsfWLVNwd0rk+gsAUyGGSNL6NdCr8uz9+C0JSe/BzXZx+yFNfNtvGH3 jkdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9VN22FTK/cp8gibu50+JlwycxClP9nhjODvnGGV4tug=; b=VXspKdxhtkv50/FvKptdDmKpEiSlHI50ryrhZUxnbILIMZgyefGvVW8zYXeL98N9PJ rWjFsUKxRsC4z8s9hP6/k/PM0DDo4SFDFLR8pH8SOhjEsrtd3igKQen/ibbL0CC4UXmS KC0BeDn281EUsuyZDOinQg4rR8ehz/B8oTvX7WKfNDKI6enjG/QwFwvHquBcJDJbO708 H+qBcPbDY71MkFlekM70R0c+7QJyIw8QEjhRgl80QRQ3JIvwDF9KZth8kjBkLGO/+j6Y WuTV8OSIfr6iNy/OlQ5MJ4wEQ8peLI2zB1w9PKo8yhDuE8EBOl3W8J4jKYiE9eiB4hlo duvg== X-Gm-Message-State: AOAM531AlHEBw4bWa62YasQJNhNU1NI6AYSRn+f2vw3LpjEuAjw2/Kv3 3SmOhTEsMZtt7EMp4r/hjz7lSw== X-Google-Smtp-Source: ABdhPJxxy0AXK/uMW8d0jud0uccMRuTn/ZHVTN15mkHEhZy7MdQXjCEqDU31cygr5QSODMyZCgtrIQ== X-Received: by 2002:a17:90a:62c8:: with SMTP id k8mr8484104pjs.33.1605854827824; Thu, 19 Nov 2020 22:47:07 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.46.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:47:07 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 06/21] mm/bootmem_info: Introduce {free,prepare}_vmemmap_page() Date: Fri, 20 Nov 2020 14:43:10 +0800 Message-Id: <20201120064325.34492-7-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: In the later patch, we can use the free_vmemmap_page() to free the unused vmemmap pages and initialize a page for vmemmap page using via prepare_vmemmap_page(). Signed-off-by: Muchun Song --- include/linux/bootmem_info.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/include/linux/bootmem_info.h b/include/linux/bootmem_info.h index 4ed6dee1adc9..239e3cc8f86c 100644 --- a/include/linux/bootmem_info.h +++ b/include/linux/bootmem_info.h @@ -3,6 +3,7 @@ #define __LINUX_BOOTMEM_INFO_H #include +#include /* * Types for free bootmem stored in page->lru.next. These have to be in @@ -22,6 +23,29 @@ void __init register_page_bootmem_info_node(struct pglist_data *pgdat); void get_page_bootmem(unsigned long info, struct page *page, unsigned long type); void put_page_bootmem(struct page *page); + +static inline void free_vmemmap_page(struct page *page) +{ + VM_WARN_ON(!PageReserved(page) || page_ref_count(page) != 2); + + /* bootmem page has reserved flag in the reserve_bootmem_region */ + if (PageReserved(page)) { + unsigned long magic = (unsigned long)page->freelist; + + if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) + put_page_bootmem(page); + else + WARN_ON(1); + } +} + +static inline void prepare_vmemmap_page(struct page *page) +{ + unsigned long section_nr = pfn_to_section_nr(page_to_pfn(page)); + + get_page_bootmem(section_nr, page, SECTION_INFO); + mark_page_reserved(page); +} #else static inline void register_page_bootmem_info_node(struct pglist_data *pgdat) { From patchwork Fri Nov 20 06:43:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919575 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D82DC56201 for ; Fri, 20 Nov 2020 06:47:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0B0202222F for ; Fri, 20 Nov 2020 06:47:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="CibrVpa1" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0B0202222F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8C7C66B005C; Fri, 20 Nov 2020 01:47:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8A0586B0068; Fri, 20 Nov 2020 01:47:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B6076B0081; Fri, 20 Nov 2020 01:47:20 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0085.hostedemail.com [216.40.44.85]) by kanga.kvack.org (Postfix) with ESMTP id 43D4B6B005C for ; Fri, 20 Nov 2020 01:47:20 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id D786C8249980 for ; Fri, 20 Nov 2020 06:47:19 +0000 (UTC) X-FDA: 77503864998.09.fork26_351752127349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id C1203180AD81F for ; Fri, 20 Nov 2020 06:47:19 +0000 (UTC) X-HE-Tag: fork26_351752127349 X-Filterd-Recvd-Size: 8106 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf02.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:47:19 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id q34so6466849pgb.11 for ; Thu, 19 Nov 2020 22:47:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=y8+6kFiX35+SYRYCMY/5tIq0PVNkZdMDuwu0+2DoVOg=; b=CibrVpa1DK/y4ZBSiLT6b4E/pdTpwIde9RU+znTztJxfn8U/3iyalIR7uOR/nTAAhc wDWncwLhE1bUVLjI4eUEk8T0hyY1WbPhR+y4q9SJGPViYRcZvqDRxOnf0bWZ/f06izMB VpprHhjU8mInlWAAQVe3/ediZ21TCiudNAsvVJ1VaBn2xSXfumC9Lg9XidyFo+CiOC+n VjHad2x99Ldm9ApW4Sj0aCNweF3Ohy3T+FROsEuVhiE7QZqmLk0EddgvzPmSGjYb1rOC pTQBWzrxRTebJuYuTpI+8q6AtWTj3rVp0xnY2sUVtJQ5HRB7EzW1bAnnvIVnxHYfEqg5 pk3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=y8+6kFiX35+SYRYCMY/5tIq0PVNkZdMDuwu0+2DoVOg=; b=gdpMlW7iMHe02q6N7KUFZ49uBB7twupIJjDC8sitOA1YvPHLcPJL7FEDGCMIz1oqyu XxQnw3MJprYaosjsuPmvxAVuLB4HbfAFUutbF4/eVi/EFvg9tENhAqJ0R28o0wjEMb8P XXCYbwX05BTpDA9BUKWOXyA+Sd55ykwAoY00CZMVqk44V7AvOV92/xwejfUFmpebI4Ov Fmz3vsd8peEhWkgR3nzqusjfAsOR8z6eu97LE4MdbzZKcoZQyzRFNIgyYkQtX7RRm6ZA GOIifQf6U1Ajco7u3ozMWM4PbzqzFwsSvbSu5rjJNqtauyc2k1tZ+Ayjhwc+SULGSQog gGbA== X-Gm-Message-State: AOAM533j6Iu4vlXIB/psu8p3X14VE8gdF6y91PclR0jKByg45jjNEx9c sFPWlUJVcVdFwHpd3t+pLS97Gw== X-Google-Smtp-Source: ABdhPJwiM8/hpCe4kOH5I+hIrt6fADSa/eg/PqPGaiVEXY38h+crBZPI5lED4J/HD569nN1T8rp3hQ== X-Received: by 2002:a17:90a:9dc2:: with SMTP id x2mr8720214pjv.98.1605854837972; Thu, 19 Nov 2020 22:47:17 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.47.08 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:47:17 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 07/21] mm/bootmem_info: Combine bootmem info and type into page->freelist Date: Fri, 20 Nov 2020 14:43:11 +0800 Message-Id: <20201120064325.34492-8-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: The page->private shares storage with page->ptl. In the later patch, we will use the page->ptl. So here we combine bootmem info and type into page->freelist so that we can do not use page->private. Signed-off-by: Muchun Song --- arch/x86/mm/init_64.c | 2 +- include/linux/bootmem_info.h | 18 ++++++++++++++++-- mm/bootmem_info.c | 12 ++++++------ mm/sparse.c | 4 ++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 0435bee2e172..9b738c6cb659 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -883,7 +883,7 @@ static void __meminit free_pagetable(struct page *page, int order) if (PageReserved(page)) { __ClearPageReserved(page); - magic = (unsigned long)page->freelist; + magic = page_bootmem_type(page); if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) { while (nr_pages--) put_page_bootmem(page++); diff --git a/include/linux/bootmem_info.h b/include/linux/bootmem_info.h index 239e3cc8f86c..95ae80838680 100644 --- a/include/linux/bootmem_info.h +++ b/include/linux/bootmem_info.h @@ -6,7 +6,7 @@ #include /* - * Types for free bootmem stored in page->lru.next. These have to be in + * Types for free bootmem stored in page->freelist. These have to be in * some random range in unsigned long space for debugging purposes. */ enum { @@ -17,6 +17,20 @@ enum { MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE = NODE_INFO, }; +#define BOOTMEM_TYPE_BITS (ilog2(MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE) + 1) +#define BOOTMEM_TYPE_MAX ((1UL << BOOTMEM_TYPE_BITS) - 1) +#define BOOTMEM_INFO_MAX (ULONG_MAX >> BOOTMEM_TYPE_BITS) + +static inline unsigned long page_bootmem_type(struct page *page) +{ + return (unsigned long)page->freelist & BOOTMEM_TYPE_MAX; +} + +static inline unsigned long page_bootmem_info(struct page *page) +{ + return (unsigned long)page->freelist >> BOOTMEM_TYPE_BITS; +} + #ifdef CONFIG_HAVE_BOOTMEM_INFO_NODE void __init register_page_bootmem_info_node(struct pglist_data *pgdat); @@ -30,7 +44,7 @@ static inline void free_vmemmap_page(struct page *page) /* bootmem page has reserved flag in the reserve_bootmem_region */ if (PageReserved(page)) { - unsigned long magic = (unsigned long)page->freelist; + unsigned long magic = page_bootmem_type(page); if (magic == SECTION_INFO || magic == MIX_SECTION_INFO) put_page_bootmem(page); diff --git a/mm/bootmem_info.c b/mm/bootmem_info.c index fcab5a3f8cc0..9baf163965fd 100644 --- a/mm/bootmem_info.c +++ b/mm/bootmem_info.c @@ -12,9 +12,9 @@ void get_page_bootmem(unsigned long info, struct page *page, unsigned long type) { - page->freelist = (void *)type; - SetPagePrivate(page); - set_page_private(page, info); + BUG_ON(info > BOOTMEM_INFO_MAX); + BUG_ON(type > BOOTMEM_TYPE_MAX); + page->freelist = (void *)((info << BOOTMEM_TYPE_BITS) | type); page_ref_inc(page); } @@ -22,14 +22,12 @@ void put_page_bootmem(struct page *page) { unsigned long type; - type = (unsigned long) page->freelist; + type = page_bootmem_type(page); BUG_ON(type < MEMORY_HOTPLUG_MIN_BOOTMEM_TYPE || type > MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE); if (page_ref_dec_return(page) == 1) { page->freelist = NULL; - ClearPagePrivate(page); - set_page_private(page, 0); INIT_LIST_HEAD(&page->lru); free_reserved_page(page); } @@ -101,6 +99,8 @@ void __init register_page_bootmem_info_node(struct pglist_data *pgdat) int node = pgdat->node_id; struct page *page; + BUILD_BUG_ON(MEMORY_HOTPLUG_MAX_BOOTMEM_TYPE > BOOTMEM_TYPE_MAX); + nr_pages = PAGE_ALIGN(sizeof(struct pglist_data)) >> PAGE_SHIFT; page = virt_to_page(pgdat); diff --git a/mm/sparse.c b/mm/sparse.c index a4138410d890..fca5fa38c2bc 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -740,12 +740,12 @@ static void free_map_bootmem(struct page *memmap) >> PAGE_SHIFT; for (i = 0; i < nr_pages; i++, page++) { - magic = (unsigned long) page->freelist; + magic = page_bootmem_type(page); BUG_ON(magic == NODE_INFO); maps_section_nr = pfn_to_section_nr(page_to_pfn(page)); - removing_section_nr = page_private(page); + removing_section_nr = page_bootmem_info(page); /* * When this function is called, the removing section is From patchwork Fri Nov 20 06:43:12 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919577 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D903CC56201 for ; Fri, 20 Nov 2020 06:47:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 523F8223AC for ; Fri, 20 Nov 2020 06:47:30 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="M6IhaA17" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 523F8223AC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D5D276B006C; Fri, 20 Nov 2020 01:47:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D340D6B006E; Fri, 20 Nov 2020 01:47:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BD3EF6B0081; Fri, 20 Nov 2020 01:47:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0002.hostedemail.com [216.40.44.2]) by kanga.kvack.org (Postfix) with ESMTP id 842A76B006C for ; Fri, 20 Nov 2020 01:47:29 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 180DF3649 for ; Fri, 20 Nov 2020 06:47:29 +0000 (UTC) X-FDA: 77503865418.14.bee84_2e0870a27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id EE4A318229818 for ; Fri, 20 Nov 2020 06:47:28 +0000 (UTC) X-HE-Tag: bee84_2e0870a27349 X-Filterd-Recvd-Size: 6253 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:47:28 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id y22so4367608plr.6 for ; Thu, 19 Nov 2020 22:47:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=GD9EigNNKRkrYvbwZ0SeMCORAMhN9mHUi42wBVrQ1sc=; b=M6IhaA17NTPYeoH5WAYNp/+O2LM6jFTQPmYOzJTbEw9D2jVsZvJ5a/I5mYV8i81jNI qb9mbcwMf/JxbvZwRp7xElyiRzktBY4kKk+QQEksn2nZRl318paFEMfF6YrhcNlvXVWH +O1EIb0vY07XgagefcqaCPL5s0MKcQxpTp8Z5Vll5k9ntdQBNdCl3CE1r5Bkybk7yKLP 9UWKn4hpDxJYm99a51hvyqCu9VBlvQs1bI+08VPiqkMFOm1duJurnOIzOFx2DvQ0L08K 3Z2uzz6Nii9C0pawN8UISX6hAZLa29BPoHzdayJTN6g9TjLZKp2TAwq8IA0D66j5umdu KGAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=GD9EigNNKRkrYvbwZ0SeMCORAMhN9mHUi42wBVrQ1sc=; b=oX/TVaIh/RjNIa9SkHfQ/HQsttKfu9M1Qu+4xQNOWLr8+dxGlpBknmdFnUaND0aRjG Y7o1BKtUpfnvdg2SNZgTitQvX04JHEtcwNwsGxenJs/YIAC5xFZbT3fb/6MO2gcQkdy7 gKBDjB+COq8EIThwIMBpVMnc3hpNZfddtkUBIKcSZYpYI7lhmazdsjUaju8XNeSThS3x kqDtm8vEuNrP8YQLKSOodZtiwFACzj6mMmerqTWuxXSWo+2Yr+CG2XVlNWxXlZGxcTap w+kQ5BTXA95TmUm+hplH+ehCjzR23S3ieG+r+QXdnoT1GQmC3l0C8MXea35F4baloDLE nbvg== X-Gm-Message-State: AOAM533iSfHLvItuiNpYbzVxFAwsSwWrFryj6eTXt+fx4G+adW5GwZVm z7auSEzcrRj+VEnBBA2HPhsoCA== X-Google-Smtp-Source: ABdhPJyefP+Gp0PGH1d3/XSCN8gM3MTiZzdY/wm6TKrtmOrDyrITyhpoOVQgIKb9R9DZmvQ6/vrZCg== X-Received: by 2002:a17:902:7d89:b029:d7:de0d:1a7f with SMTP id a9-20020a1709027d89b02900d7de0d1a7fmr12146683plm.69.1605854847604; Thu, 19 Nov 2020 22:47:27 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.47.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:47:26 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 08/21] mm/hugetlb: Initialize page table lock for vmemmap Date: Fri, 20 Nov 2020 14:43:12 +0800 Message-Id: <20201120064325.34492-9-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: In the later patch, we will use the vmemmap page table lock to guard the splitting of the vmemmap PMD. So initialize the vmemmap page table lock. Signed-off-by: Muchun Song --- mm/hugetlb_vmemmap.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index ec70980000d8..bc8546df4a51 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -99,6 +99,8 @@ */ #define pr_fmt(fmt) "HugeTLB Vmemmap: " fmt +#include +#include #include #include #include "hugetlb_vmemmap.h" @@ -208,3 +210,70 @@ void __init hugetlb_vmemmap_init(struct hstate *h) pr_debug("can free %d vmemmap pages for %s\n", h->nr_free_vmemmap_pages, h->name); } + +static int __init vmemmap_pud_entry(pud_t *pud, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + struct page *page = pud_page(*pud); + + /* + * The page->private shares storage with page->ptl. So make sure + * that the PG_private is not set and initialize page->private to + * zero. + */ + VM_BUG_ON_PAGE(PagePrivate(page), page); + set_page_private(page, 0); + + BUG_ON(!pmd_ptlock_init(page)); + + return 0; +} + +static void __init vmemmap_ptlock_init_section(unsigned long start_pfn) +{ + unsigned long section_nr; + struct mem_section *ms; + struct page *memmap, *memmap_end; + struct mm_struct *mm = &init_mm; + + const struct mm_walk_ops ops = { + .pud_entry = vmemmap_pud_entry, + }; + + section_nr = pfn_to_section_nr(start_pfn); + ms = __nr_to_section(section_nr); + memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); + memmap_end = memmap + PAGES_PER_SECTION; + + mmap_read_lock(mm); + BUG_ON(walk_page_range_novma(mm, (unsigned long)memmap, + (unsigned long)memmap_end, + &ops, NULL, NULL)); + mmap_read_unlock(mm); +} + +static void __init vmemmap_ptlock_init_node(int nid) +{ + unsigned long pfn, end_pfn; + struct pglist_data *pgdat = NODE_DATA(nid); + + pfn = pgdat->node_start_pfn; + end_pfn = pgdat_end_pfn(pgdat); + + for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) + vmemmap_ptlock_init_section(pfn); +} + +static int __init vmemmap_ptlock_init(void) +{ + int nid; + + if (!hugepages_supported()) + return 0; + + for_each_online_node(nid) + vmemmap_ptlock_init_node(nid); + + return 0; +} +core_initcall(vmemmap_ptlock_init); From patchwork Fri Nov 20 06:43:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919579 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 85BA3C56201 for ; Fri, 20 Nov 2020 06:47:41 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D01E222256 for ; Fri, 20 Nov 2020 06:47:40 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="VDYWSh90" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D01E222256 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 520406B0081; Fri, 20 Nov 2020 01:47:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4CF6B6B0082; Fri, 20 Nov 2020 01:47:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 397126B0083; Fri, 20 Nov 2020 01:47:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0046.hostedemail.com [216.40.44.46]) by kanga.kvack.org (Postfix) with ESMTP id 014A46B0081 for ; Fri, 20 Nov 2020 01:47:39 -0500 (EST) Received: from smtpin17.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id EC7818249980 for ; Fri, 20 Nov 2020 06:47:38 +0000 (UTC) X-FDA: 77503865796.17.floor22_2d0c78d27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin17.hostedemail.com (Postfix) with ESMTP id CE993180D0181 for ; Fri, 20 Nov 2020 06:47:38 +0000 (UTC) X-HE-Tag: floor22_2d0c78d27349 X-Filterd-Recvd-Size: 13283 Received: from mail-pg1-f193.google.com (mail-pg1-f193.google.com [209.85.215.193]) by imf36.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:47:38 +0000 (UTC) Received: by mail-pg1-f193.google.com with SMTP id m9so6499617pgb.4 for ; Thu, 19 Nov 2020 22:47:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mJbL/jFi9pmO2QuyNj8ci+I/nodhiq10ASdGvT5u7rc=; b=VDYWSh90Hpy68K3XXqueDxEF/DBXJRAaK5UXwi2gRu+lmdNAAI58qTYI/BJlsM8yKN Yl/m/QsLNKU0SJFEQVIcuV3PQSIz0+2QTY08llRoxUqWsoGyxv37MxN84pEqFk/fAVa3 Ur7k11B7mstnT2MuYKfcwR6LhpisSx3stwSipzGAyJJko+8l1R3IldUjB6CTZbUvqRLx RzpU7zVZqH+N4yt2tqSGn+6yqkVLesupSuYFNhOe77yZWocu0VpTqE4gctS7sBNiHDoi 27ZSAPMKh0+lc/dA2/wxt4xYztgJW3+eWfRJ4UWNjXWZAOdLABM+P/x3ykGvvi5nwrP7 0CIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mJbL/jFi9pmO2QuyNj8ci+I/nodhiq10ASdGvT5u7rc=; b=nBTIDC9UbmOTeIjQKqOfF+HBGZAO6ae3Wams+wUVY/dHfZWqW7wA2kDMSR+k0bEvle 46OU0hqWxtdBkbYhHywv6rDuor01aQ1E83ClJtT9bZ2lzTV0ibW5MrQTOOu857YPg5sb XbJfdLQVheeerZSkD3m/9b2CndQoSyENO0khYzU5YYsjEIFEPgh/qneML7r0N4pg+DIu gkPvSrjqkLFvzyTY09q8ZDd44oTKe+UB/GeDMge/VLK8eytnDDJNBr0+4gSYL3Rez6a7 XmlawSR8zgOe3ZGAdtbcsilspgmmNZoGjSnpsdg1BOwbmfDYLM+06m9yN4nmlsPS45TZ 1Ncg== X-Gm-Message-State: AOAM531rUe/aLpp9KzrNhZizmBq+ErUQRKe47qp0CFSBR8lypzNW9q0Q PKdPf/GdE4mjaD/pKJ6XQr/rcg== X-Google-Smtp-Source: ABdhPJxT4Kwoeb604BotL/Sr3/8OUTJ5ATTO9CiXzQyDCcZQEcS87yH6sYv/gqVY+XzFrpCeMui4kw== X-Received: by 2002:a17:90a:588e:: with SMTP id j14mr8622117pji.30.1605854857065; Thu, 19 Nov 2020 22:47:37 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.47.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:47:36 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 09/21] mm/hugetlb: Free the vmemmap pages associated with each hugetlb page Date: Fri, 20 Nov 2020 14:43:13 +0800 Message-Id: <20201120064325.34492-10-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: When we allocate a hugetlb page from the buddy, we should free the unused vmemmap pages associated with it. We can do that in the prep_new_huge_page(). Signed-off-by: Muchun Song --- arch/x86/include/asm/hugetlb.h | 9 ++ arch/x86/include/asm/pgtable_64_types.h | 8 ++ mm/hugetlb.c | 16 +++ mm/hugetlb_vmemmap.c | 188 ++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 5 + 5 files changed, 226 insertions(+) diff --git a/arch/x86/include/asm/hugetlb.h b/arch/x86/include/asm/hugetlb.h index 1721b1aadeb1..c601fe042832 100644 --- a/arch/x86/include/asm/hugetlb.h +++ b/arch/x86/include/asm/hugetlb.h @@ -4,6 +4,15 @@ #include #include +#include + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge(pmd_t *pmd) +{ + return pmd_large(*pmd); +} +#endif #define hugepages_supported() boot_cpu_has(X86_FEATURE_PSE) diff --git a/arch/x86/include/asm/pgtable_64_types.h b/arch/x86/include/asm/pgtable_64_types.h index 52e5f5f2240d..bedbd2e7d06c 100644 --- a/arch/x86/include/asm/pgtable_64_types.h +++ b/arch/x86/include/asm/pgtable_64_types.h @@ -139,6 +139,14 @@ extern unsigned int ptrs_per_p4d; # define VMEMMAP_START __VMEMMAP_BASE_L4 #endif /* CONFIG_DYNAMIC_MEMORY_LAYOUT */ +/* + * VMEMMAP_SIZE - allows the whole linear region to be covered by + * a struct page array. + */ +#define VMEMMAP_SIZE (1UL << (__VIRTUAL_MASK_SHIFT - PAGE_SHIFT - \ + 1 + ilog2(sizeof(struct page)))) +#define VMEMMAP_END (VMEMMAP_START + VMEMMAP_SIZE) + #define VMALLOC_END (VMALLOC_START + (VMALLOC_SIZE_TB << 40) - 1) #define MODULES_VADDR (__START_KERNEL_map + KERNEL_IMAGE_SIZE) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index f88032c24667..a0ce6f33a717 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1499,6 +1499,14 @@ void free_huge_page(struct page *page) static void prep_new_huge_page(struct hstate *h, struct page *page, int nid) { + free_huge_page_vmemmap(h, page); + /* + * Because we store preallocated pages on @page->lru, + * vmemmap_pgtable_free() must be called before the + * initialization of @page->lru in INIT_LIST_HEAD(). + */ + vmemmap_pgtable_free(page); + INIT_LIST_HEAD(&page->lru); set_compound_page_dtor(page, HUGETLB_PAGE_DTOR); set_hugetlb_cgroup(page, NULL); @@ -1751,6 +1759,14 @@ static struct page *alloc_fresh_huge_page(struct hstate *h, if (!page) return NULL; + if (vmemmap_pgtable_prealloc(h, page)) { + if (hstate_is_gigantic(h)) + free_gigantic_page(page, huge_page_order(h)); + else + put_page(page); + return NULL; + } + if (hstate_is_gigantic(h)) prep_compound_gigantic_page(page, huge_page_order(h)); prep_new_huge_page(h, page, page_to_nid(page)); diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index bc8546df4a51..6f8a735e0dd3 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -102,6 +102,7 @@ #include #include #include +#include #include #include "hugetlb_vmemmap.h" @@ -114,6 +115,8 @@ * these page frames. Therefore, we need to reserve two pages as vmemmap areas. */ #define RESERVE_VMEMMAP_NR 2U +#define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) +#define TAIL_PAGE_REUSE -1 #ifndef VMEMMAP_HPAGE_SHIFT #define VMEMMAP_HPAGE_SHIFT HPAGE_SHIFT @@ -123,6 +126,21 @@ #define VMEMMAP_HPAGE_SIZE ((1UL) << VMEMMAP_HPAGE_SHIFT) #define VMEMMAP_HPAGE_MASK (~(VMEMMAP_HPAGE_SIZE - 1)) +#define vmemmap_hpage_addr_end(addr, end) \ +({ \ + unsigned long __boundary; \ + __boundary = ((addr) + VMEMMAP_HPAGE_SIZE) & VMEMMAP_HPAGE_MASK; \ + (__boundary - 1 < (end) - 1) ? __boundary : (end); \ +}) + +#ifndef vmemmap_pmd_huge +#define vmemmap_pmd_huge vmemmap_pmd_huge +static inline bool vmemmap_pmd_huge(pmd_t *pmd) +{ + return pmd_huge(*pmd); +} +#endif + static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) { return h->nr_free_vmemmap_pages; @@ -189,6 +207,176 @@ int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page) return -ENOMEM; } +/* + * Walk a vmemmap address to the pmd it maps. + */ +static pmd_t *vmemmap_to_pmd(unsigned long page) +{ + pgd_t *pgd; + p4d_t *p4d; + pud_t *pud; + pmd_t *pmd; + + if (page < VMEMMAP_START || page >= VMEMMAP_END) + return NULL; + + pgd = pgd_offset_k(page); + if (pgd_none(*pgd)) + return NULL; + p4d = p4d_offset(pgd, page); + if (p4d_none(*p4d)) + return NULL; + pud = pud_offset(p4d, page); + + if (pud_none(*pud) || pud_bad(*pud)) + return NULL; + pmd = pmd_offset(pud, page); + + return pmd; +} + +static inline spinlock_t *vmemmap_pmd_lock(pmd_t *pmd) +{ + return pmd_lock(&init_mm, pmd); +} + +static inline int freed_vmemmap_hpage(struct page *page) +{ + return atomic_read(&page->_mapcount) + 1; +} + +static inline int freed_vmemmap_hpage_inc(struct page *page) +{ + return atomic_inc_return_relaxed(&page->_mapcount) + 1; +} + +static inline int freed_vmemmap_hpage_dec(struct page *page) +{ + return atomic_dec_return_relaxed(&page->_mapcount) + 1; +} + +static inline void free_vmemmap_page_list(struct list_head *list) +{ + struct page *page, *next; + + list_for_each_entry_safe(page, next, list, lru) { + list_del(&page->lru); + free_vmemmap_page(page); + } +} + +static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, + unsigned long start, + unsigned long end, + struct list_head *free_pages) +{ + /* Make the tail pages are mapped read-only. */ + pgprot_t pgprot = PAGE_KERNEL_RO; + pte_t entry = mk_pte(reuse, pgprot); + unsigned long addr; + + for (addr = start; addr < end; addr += PAGE_SIZE, ptep++) { + struct page *page; + pte_t old = *ptep; + + VM_WARN_ON(!pte_present(old)); + page = pte_page(old); + list_add(&page->lru, free_pages); + + set_pte_at(&init_mm, addr, ptep, entry); + } +} + +static void __free_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd, + unsigned long addr, + struct list_head *free_pages) +{ + unsigned long next; + unsigned long start = addr + RESERVE_VMEMMAP_SIZE; + unsigned long end = addr + vmemmap_pages_size_per_hpage(h); + struct page *reuse = NULL; + + addr = start; + do { + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[TAIL_PAGE_REUSE]); + + next = vmemmap_hpage_addr_end(addr, end); + __free_huge_page_pte_vmemmap(reuse, ptep, addr, next, + free_pages); + } while (pmd++, addr = next, addr != end); + + flush_tlb_kernel_range(start, end); +} + +static void split_vmemmap_pmd(pmd_t *pmd, pte_t *pte_p, unsigned long addr) +{ + int i; + pgprot_t pgprot = PAGE_KERNEL; + struct mm_struct *mm = &init_mm; + struct page *page; + pmd_t old_pmd, _pmd; + + old_pmd = READ_ONCE(*pmd); + page = pmd_page(old_pmd); + pmd_populate_kernel(mm, &_pmd, pte_p); + + for (i = 0; i < VMEMMAP_HPAGE_NR; i++, addr += PAGE_SIZE) { + pte_t entry, *pte; + + entry = mk_pte(page + i, pgprot); + pte = pte_offset_kernel(&_pmd, addr); + VM_BUG_ON(!pte_none(*pte)); + set_pte_at(mm, addr, pte, entry); + } + + /* make pte visible before pmd */ + smp_wmb(); + pmd_populate_kernel(mm, pmd, pte_p); +} + +static void split_vmemmap_huge_page(struct page *head, pmd_t *pmd) +{ + struct page *pte_page, *t_page; + unsigned long start = (unsigned long)head & VMEMMAP_HPAGE_MASK; + unsigned long addr = start; + + list_for_each_entry_safe(pte_page, t_page, &head->lru, lru) { + list_del(&pte_page->lru); + VM_BUG_ON(freed_vmemmap_hpage(pte_page)); + split_vmemmap_pmd(pmd++, page_to_virt(pte_page), addr); + addr += VMEMMAP_HPAGE_SIZE; + } + + flush_tlb_kernel_range(start, addr); +} + +void free_huge_page_vmemmap(struct hstate *h, struct page *head) +{ + pmd_t *pmd; + spinlock_t *ptl; + LIST_HEAD(free_pages); + + if (!free_vmemmap_pages_per_hpage(h)) + return; + + pmd = vmemmap_to_pmd((unsigned long)head); + BUG_ON(!pmd); + + ptl = vmemmap_pmd_lock(pmd); + if (vmemmap_pmd_huge(pmd)) + split_vmemmap_huge_page(head, pmd); + + __free_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, &free_pages); + freed_vmemmap_hpage_inc(pmd_page(*pmd)); + spin_unlock(ptl); + + free_vmemmap_page_list(&free_pages); +} + void __init hugetlb_vmemmap_init(struct hstate *h) { unsigned int order = huge_page_order(h); diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 9eca6879c0a4..a9425d94ed8b 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -14,6 +14,7 @@ void __init hugetlb_vmemmap_init(struct hstate *h); int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page); void vmemmap_pgtable_free(struct page *page); +void free_huge_page_vmemmap(struct hstate *h, struct page *head); #else static inline void hugetlb_vmemmap_init(struct hstate *h) { @@ -27,5 +28,9 @@ static inline int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page) static inline void vmemmap_pgtable_free(struct page *page) { } + +static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head) +{ +} #endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */ From patchwork Fri Nov 20 06:43:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919581 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B9BCC56201 for ; Fri, 20 Nov 2020 06:47:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 92CA7223AC for ; Fri, 20 Nov 2020 06:47:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="Se35lDK/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 92CA7223AC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 23CD66B0072; Fri, 20 Nov 2020 01:47:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E9226B0073; Fri, 20 Nov 2020 01:47:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0891A6B0083; Fri, 20 Nov 2020 01:47:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0143.hostedemail.com [216.40.44.143]) by kanga.kvack.org (Postfix) with ESMTP id B93346B0072 for ; Fri, 20 Nov 2020 01:47:48 -0500 (EST) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 54B9E1EF2 for ; Fri, 20 Nov 2020 06:47:48 +0000 (UTC) X-FDA: 77503866216.18.help89_5200a0c27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin18.hostedemail.com (Postfix) with ESMTP id 337C4100EC66E for ; Fri, 20 Nov 2020 06:47:48 +0000 (UTC) X-HE-Tag: help89_5200a0c27349 X-Filterd-Recvd-Size: 10289 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf39.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:47:47 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id f18so6478834pgi.8 for ; Thu, 19 Nov 2020 22:47:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=F4ocrBp5RtbwrXGoGGGzEKCWuDM80WIWOrkJq24b7IM=; b=Se35lDK/XQgYJK1nao211OIIU2qYhqgO1R6rKGuX9Vr0lFX4czymPTlGW+jBkHVTa9 hDd5u944BykhdpGl0L8yX0gU8XH9nTxoQG9ghUMh15iii0TqsArsh6TkQvt3zkCoee6b XB9n6wnWzZgccn4Bv+CR4NEW89sSobfOhfawE1ZWPTFgzEaIZNDw4vMzevrwvnaseAhq zoPQPI1AxNZwREynm80OIaHAzMeCpHb8Kwsb4p6R3EQkReAl5MBLQtz3OKWsZld3WVmP d/UhIqQzAAPOIlRILXO0EO8/f3eUf9RjeN7aLQntzw4Ndk+qJ4f+NtC4ta1mrDIVBag7 NKeA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=F4ocrBp5RtbwrXGoGGGzEKCWuDM80WIWOrkJq24b7IM=; b=l30w3/n78VKhkFE28cICgck0Nw6gHBZGPyTXdLvLF54qmi03on2Ejsc+N0GpET7mFL LH+t9ckUDCUjUiwB7AfUZLDjShL/TYsAtnvn2TWtCxEz0kztRB/b8pMNpE4Unu7Fbvc6 qsEUgsusVeNo3kKXIMmv2hxpLmZ7dV0Qr+irEE5AXAZIVuaOeUExhyoWGGmAYCNdtsPn lpFg9o69kSYA6xCA8YE5D9hMt9ZTWvuQods2WPIZPTGnw1sD6rHNdFH3W+9WHU25qzKz zIrbCWfawgpXHrZuzFRPQhJpwCKPtz6GyDqWqiThGCpW2JqTFDHBZu4whkf00SZr5w6d G+8w== X-Gm-Message-State: AOAM533Z4RN6g5MeUPet4eS0bs0iQffypGCAbNAtzeBOH4/LhZtm4Ygq zM2O/+CYHKfr8l+nQ63/ZJ9zEg== X-Google-Smtp-Source: ABdhPJwDQrgUMesiO1qtRhsL0SWbv3mx4C+Bq9qqub0lRaxnzDCkdADO40LR2/elDjmk33XOqDhU9Q== X-Received: by 2002:a63:1445:: with SMTP id 5mr15576117pgu.357.1605854866690; Thu, 19 Nov 2020 22:47:46 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.47.37 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:47:46 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 10/21] mm/hugetlb: Defer freeing of hugetlb pages Date: Fri, 20 Nov 2020 14:43:14 +0800 Message-Id: <20201120064325.34492-11-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: In the subsequent patch, we will allocate the vmemmap pages when free huge pages. But update_and_free_page() is be called from a non-task context(and hold hugetlb_lock), we can defer the actual freeing in a workqueue to prevent use GFP_ATOMIC to allocate the vmemmap pages. Signed-off-by: Muchun Song --- mm/hugetlb.c | 98 +++++++++++++++++++++++++++++++++++++++++++++------- mm/hugetlb_vmemmap.c | 5 --- mm/hugetlb_vmemmap.h | 10 ++++++ 3 files changed, 96 insertions(+), 17 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index a0ce6f33a717..4aabf12aca9b 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1221,7 +1221,7 @@ static void destroy_compound_gigantic_page(struct page *page, __ClearPageHead(page); } -static void free_gigantic_page(struct page *page, unsigned int order) +static void __free_gigantic_page(struct page *page, unsigned int order) { /* * If the page isn't allocated using the cma allocator, @@ -1288,20 +1288,100 @@ static struct page *alloc_gigantic_page(struct hstate *h, gfp_t gfp_mask, { return NULL; } -static inline void free_gigantic_page(struct page *page, unsigned int order) { } +static inline void __free_gigantic_page(struct page *page, + unsigned int order) { } static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { } #endif -static void update_and_free_page(struct hstate *h, struct page *page) +static void __free_hugepage(struct hstate *h, struct page *page); + +/* + * As update_and_free_page() is be called from a non-task context(and hold + * hugetlb_lock), we can defer the actual freeing in a workqueue to prevent + * use GFP_ATOMIC to allocate a lot of vmemmap pages. + * + * update_hpage_vmemmap_workfn() locklessly retrieves the linked list of + * pages to be freed and frees them one-by-one. As the page->mapping pointer + * is going to be cleared in update_hpage_vmemmap_workfn() anyway, it is + * reused as the llist_node structure of a lockless linked list of huge + * pages to be freed. + */ +static LLIST_HEAD(hpage_update_freelist); + +static void update_hpage_vmemmap_workfn(struct work_struct *work) { - int i; + struct llist_node *node; + struct page *page; + + node = llist_del_all(&hpage_update_freelist); + + while (node) { + page = container_of((struct address_space **)node, + struct page, mapping); + node = node->next; + page->mapping = NULL; + __free_hugepage(page_hstate(page), page); + cond_resched(); + } +} +static DECLARE_WORK(hpage_update_work, update_hpage_vmemmap_workfn); + +static inline void __update_and_free_page(struct hstate *h, struct page *page) +{ + /* No need to allocate vmemmap pages */ + if (!free_vmemmap_pages_per_hpage(h)) { + __free_hugepage(h, page); + return; + } + + /* + * Defer freeing to avoid using GFP_ATOMIC to allocate vmemmap + * pages. + * + * Only call schedule_work() if hpage_update_freelist is previously + * empty. Otherwise, schedule_work() had been called but the workfn + * hasn't retrieved the list yet. + */ + if (llist_add((struct llist_node *)&page->mapping, + &hpage_update_freelist)) + schedule_work(&hpage_update_work); +} + +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP +static inline void free_gigantic_page(struct hstate *h, struct page *page) +{ + __free_gigantic_page(page, huge_page_order(h)); +} +#else +static inline void free_gigantic_page(struct hstate *h, struct page *page) +{ + /* + * Temporarily drop the hugetlb_lock, because + * we might block in __free_gigantic_page(). + */ + spin_unlock(&hugetlb_lock); + __free_gigantic_page(page, huge_page_order(h)); + spin_lock(&hugetlb_lock); +} +#endif + +static void update_and_free_page(struct hstate *h, struct page *page) +{ if (hstate_is_gigantic(h) && !gigantic_page_runtime_supported()) return; h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid(page)]--; + + __update_and_free_page(h, page); +} + +static void __free_hugepage(struct hstate *h, struct page *page) +{ + int i; + for (i = 0; i < pages_per_huge_page(h); i++) { page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | 1 << PG_dirty | @@ -1313,14 +1393,8 @@ static void update_and_free_page(struct hstate *h, struct page *page) set_compound_page_dtor(page, NULL_COMPOUND_DTOR); set_page_refcounted(page); if (hstate_is_gigantic(h)) { - /* - * Temporarily drop the hugetlb_lock, because - * we might block in free_gigantic_page(). - */ - spin_unlock(&hugetlb_lock); destroy_compound_gigantic_page(page, huge_page_order(h)); - free_gigantic_page(page, huge_page_order(h)); - spin_lock(&hugetlb_lock); + free_gigantic_page(h, page); } else { __free_pages(page, huge_page_order(h)); } @@ -1761,7 +1835,7 @@ static struct page *alloc_fresh_huge_page(struct hstate *h, if (vmemmap_pgtable_prealloc(h, page)) { if (hstate_is_gigantic(h)) - free_gigantic_page(page, huge_page_order(h)); + free_gigantic_page(h, page); else put_page(page); return NULL; diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 6f8a735e0dd3..eda7e3a0b67c 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -141,11 +141,6 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) } #endif -static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) -{ - return h->nr_free_vmemmap_pages; -} - static inline unsigned int vmemmap_pages_per_hpage(struct hstate *h) { return free_vmemmap_pages_per_hpage(h) + RESERVE_VMEMMAP_NR; diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index a9425d94ed8b..4175b44f88bc 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -15,6 +15,11 @@ void __init hugetlb_vmemmap_init(struct hstate *h); int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page); void vmemmap_pgtable_free(struct page *page); void free_huge_page_vmemmap(struct hstate *h, struct page *head); + +static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) +{ + return h->nr_free_vmemmap_pages; +} #else static inline void hugetlb_vmemmap_init(struct hstate *h) { @@ -32,5 +37,10 @@ static inline void vmemmap_pgtable_free(struct page *page) static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head) { } + +static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) +{ + return 0; +} #endif /* CONFIG_HUGETLB_PAGE_FREE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */ From patchwork Fri Nov 20 06:43:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919583 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A6F3C56201 for ; Fri, 20 Nov 2020 06:47:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E9B67223AC for ; Fri, 20 Nov 2020 06:47:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="zqYQ8zq+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E9B67223AC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 741DE6B0074; Fri, 20 Nov 2020 01:47:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 717CD6B0075; Fri, 20 Nov 2020 01:47:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 608206B0083; Fri, 20 Nov 2020 01:47:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0139.hostedemail.com [216.40.44.139]) by kanga.kvack.org (Postfix) with ESMTP id 274E36B0074 for ; Fri, 20 Nov 2020 01:47:58 -0500 (EST) Received: from smtpin13.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id B18BA181AEF23 for ; Fri, 20 Nov 2020 06:47:57 +0000 (UTC) X-FDA: 77503866594.13.air62_6012b8927349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin13.hostedemail.com (Postfix) with ESMTP id 9331518140B60 for ; Fri, 20 Nov 2020 06:47:57 +0000 (UTC) X-HE-Tag: air62_6012b8927349 X-Filterd-Recvd-Size: 8577 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf43.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:47:57 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id w4so6468039pgg.13 for ; Thu, 19 Nov 2020 22:47:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=iceBPaQt02dZVydXdfu8Z74PD0dATJP+BLl41c9vKHU=; b=zqYQ8zq+KGjONa5xXaxh1Cizv0zHOcPwll+/EnQLF3uDtTXOMzgoAt8zYMkw3/Byhg qNNNvuQx9dgYaVxLdcy2agYVjh9us53tRfZg+JgLPrNjOhTIukAbuGGSmukQ1G84XCet zR+PnyYpBpXDggqqQz2n2NlnePxyUoyCCAm6saqrVT3LggXkSqkcjdb3YCIA+MKHrhGl B1tlDflcmDn7G6SgrcHxVRW0LYo0/WOPNHO5vlC0A0ld7iMs+FBL/birywemvak77h3Q Q9jNdJJiVNCYiiUoDeL5JsxjuDFYlSeaboyXCZ2LmZnr/h6axGSUkngzFC2igBUaxEqi urWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=iceBPaQt02dZVydXdfu8Z74PD0dATJP+BLl41c9vKHU=; b=cjr6DK8IbhLtZbje6qnygbVHGvZCII8sg+9fMAmWaPemH2GvmSjTEcHkKyKfUORaK2 Km/ITX2VM2E/s6ONym+HJF2EAPO/mxdnw8AU40rPSIceCWdA0dFlGg0pctuIa5uKfWbl LHCdc1Ze8pxRatB4U/zihATOBHL59+iC5nZYv+R4HC93e8/25Z/mqtg9zSu9WXwwCoNU 7QzlyOzypTlCVjpxkHzPb9CUW1LE+ho2YYxuFKTusS7kLHJbACmtXXvx7PPHhJHhBI6c uoxPJ0OHETkP26PibkbSvoc/7bG+I19O8VRMi9puIoTqRGiRm30yslURhnHTJZ6aE9dd TJMw== X-Gm-Message-State: AOAM531dVubbl89i7v3KuivHI2ZQUFF5jGVRL7wEcQATasR2th+zx3k3 PM58Wq/2tfnilOQUBR6o8gyZqw== X-Google-Smtp-Source: ABdhPJwLLBPH98amlvEq9aU2cWlfLONjOmDPxpfXjkJQZsa142xhIgPM7fFlephFAKAAy1Zk2y7XTQ== X-Received: by 2002:a62:5406:0:b029:18c:8dac:4a99 with SMTP id i6-20020a6254060000b029018c8dac4a99mr12507984pfb.19.1605854876174; Thu, 19 Nov 2020 22:47:56 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.47.47 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:47:55 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 11/21] mm/hugetlb: Allocate the vmemmap pages associated with each hugetlb page Date: Fri, 20 Nov 2020 14:43:15 +0800 Message-Id: <20201120064325.34492-12-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: When we free a hugetlb page to the buddy, we should allocate the vmemmap pages associated with it. We can do that in the __free_hugepage(). Signed-off-by: Muchun Song --- mm/hugetlb.c | 2 ++ mm/hugetlb_vmemmap.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 5 +++ 3 files changed, 107 insertions(+) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 4aabf12aca9b..ba927ae7f9bd 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1382,6 +1382,8 @@ static void __free_hugepage(struct hstate *h, struct page *page) { int i; + alloc_huge_page_vmemmap(h, page); + for (i = 0; i < pages_per_huge_page(h); i++) { page[i].flags &= ~(1 << PG_locked | 1 << PG_error | 1 << PG_referenced | 1 << PG_dirty | diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index eda7e3a0b67c..361c4174e222 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -117,6 +117,8 @@ #define RESERVE_VMEMMAP_NR 2U #define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) #define TAIL_PAGE_REUSE -1 +#define GFP_VMEMMAP_PAGE \ + (GFP_KERNEL | __GFP_NOFAIL | __GFP_MEMALLOC) #ifndef VMEMMAP_HPAGE_SHIFT #define VMEMMAP_HPAGE_SHIFT HPAGE_SHIFT @@ -250,6 +252,104 @@ static inline int freed_vmemmap_hpage_dec(struct page *page) return atomic_dec_return_relaxed(&page->_mapcount) + 1; } +static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, + unsigned long start, + unsigned long end, + struct list_head *remap_pages) +{ + pgprot_t pgprot = PAGE_KERNEL; + void *from = page_to_virt(reuse); + unsigned long addr; + + for (addr = start; addr < end; addr += PAGE_SIZE) { + void *to; + struct page *page; + pte_t entry, old = *ptep; + + page = list_first_entry_or_null(remap_pages, struct page, lru); + list_del(&page->lru); + to = page_to_virt(page); + copy_page(to, from); + + /* + * Make sure that any data that writes to the @to is made + * visible to the physical page. + */ + flush_kernel_vmap_range(to, PAGE_SIZE); + + prepare_vmemmap_page(page); + + entry = mk_pte(page, pgprot); + set_pte_at(&init_mm, addr, ptep++, entry); + + VM_BUG_ON(!pte_present(old) || pte_page(old) != reuse); + } +} + +static void __remap_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd, + unsigned long addr, + struct list_head *remap_pages) +{ + unsigned long next; + unsigned long start = addr + RESERVE_VMEMMAP_SIZE; + unsigned long end = addr + vmemmap_pages_size_per_hpage(h); + struct page *reuse = NULL; + + addr = start; + do { + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[TAIL_PAGE_REUSE]); + + next = vmemmap_hpage_addr_end(addr, end); + __remap_huge_page_pte_vmemmap(reuse, ptep, addr, next, + remap_pages); + } while (pmd++, addr = next, addr != end); + + flush_tlb_kernel_range(start, end); +} + +static inline void alloc_vmemmap_pages(struct hstate *h, struct list_head *list) +{ + int i; + + for (i = 0; i < free_vmemmap_pages_per_hpage(h); i++) { + struct page *page; + + /* This should not fail */ + page = alloc_page(GFP_VMEMMAP_PAGE); + list_add_tail(&page->lru, list); + } +} + +void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) +{ + pmd_t *pmd; + spinlock_t *ptl; + LIST_HEAD(remap_pages); + + if (!free_vmemmap_pages_per_hpage(h)) + return; + + alloc_vmemmap_pages(h, &remap_pages); + + pmd = vmemmap_to_pmd((unsigned long)head); + BUG_ON(!pmd); + + ptl = vmemmap_pmd_lock(pmd); + __remap_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, + &remap_pages); + if (!freed_vmemmap_hpage_dec(pmd_page(*pmd))) { + /* + * Todo: + * Merge pte to huge pmd if it has ever been split. + */ + } + spin_unlock(ptl); +} + static inline void free_vmemmap_page_list(struct list_head *list) { struct page *page, *next; diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 4175b44f88bc..6dfa7ed6f88a 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -14,6 +14,7 @@ void __init hugetlb_vmemmap_init(struct hstate *h); int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page); void vmemmap_pgtable_free(struct page *page); +void alloc_huge_page_vmemmap(struct hstate *h, struct page *head); void free_huge_page_vmemmap(struct hstate *h, struct page *head); static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) @@ -34,6 +35,10 @@ static inline void vmemmap_pgtable_free(struct page *page) { } +static inline void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) +{ +} + static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head) { } From patchwork Fri Nov 20 06:43:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919585 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 402C4C56201 for ; Fri, 20 Nov 2020 06:48:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B76F724181 for ; Fri, 20 Nov 2020 06:48:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="pYEZWv1T" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B76F724181 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 45BB06B0083; Fri, 20 Nov 2020 01:48:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 40A726B0085; Fri, 20 Nov 2020 01:48:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FB016B0087; Fri, 20 Nov 2020 01:48:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0060.hostedemail.com [216.40.44.60]) by kanga.kvack.org (Postfix) with ESMTP id EA42E6B0083 for ; Fri, 20 Nov 2020 01:48:07 -0500 (EST) Received: from smtpin14.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 80E173649 for ; Fri, 20 Nov 2020 06:48:07 +0000 (UTC) X-FDA: 77503867014.14.pen49_5c16ea827349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin14.hostedemail.com (Postfix) with ESMTP id 649F518229818 for ; Fri, 20 Nov 2020 06:48:07 +0000 (UTC) X-HE-Tag: pen49_5c16ea827349 X-Filterd-Recvd-Size: 8658 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:48:06 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id c66so6940436pfa.4 for ; Thu, 19 Nov 2020 22:48:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=40WMQjbHo+8UGODnuWlLj4XyZtBc8woPFg0/w4orzdo=; b=pYEZWv1T0tL0lGNesIdLrYCxfDzq2+HKtxnPtQ7I+tcNYvWauOzOqq0rdsqoL3WCcz gSbp3+T3jVQU2RQum5FMqHrZWgxIfr2wp5A9v8Cdhd5DGbXmD2L4bxkzY42kmjG9cIUH fAi5wKFAFOzIfV1MLERSFvqnsWmR0MiuMwB6eS3DtARTH1H+WS+z2Zvfs+I9znah3LUG BcPOCYm53JSvO+hY+tWTemSSZs5KVGP1P4qBwSB3Xz5mMDup831mnAdWrSkDgEtY9uGJ gLFXOzJvW8cnd23ADjilhcGhbk8n2VG4Jvg4RLxQ2Qiv1a/m4igF7uP0iyCqD9lg+Miv h38g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=40WMQjbHo+8UGODnuWlLj4XyZtBc8woPFg0/w4orzdo=; b=T1AT/V6bYO655UZ8Xl4qxOCryvnesgFz7DNoywYlq18NUAKHSC7t+2d2p8mSm+Pww+ dRDAfAsY6jYy2DTzBEiEEvjd0NWmQD3jBhuCa1IQFzcojbWpTBKmX/hPc3JYscjyQ+3g PZRF3DhtwJFrVw4Nn5CaPPtTvq47r3eDWWfM1peIWtLyfDxPYxrwaA7iWhwCnmJDMwY1 w8H/xtRd837qRD8PsyE1SaMPdwgYxw2V/rJZK9yvvLYNP2y6kUMRArZxpo/gneRLOUPU kNC1ZW4haWSP5CL4iBZNG+MahDBfoKT6THj5z/GJ8pK5R5F+KmGpvc+H2dlfB4f0ivPa aSQQ== X-Gm-Message-State: AOAM53295rbUQTlaP/W7fXBUEGZFE+/hQfs7QFviEDAZ2rFb0Vha3rxV s/zVoBJ3aS/CoBMo6zqqP/XBZQ== X-Google-Smtp-Source: ABdhPJzXjuTO9HWUVhtlJ0CQGQ7elxxZ9bu50VSldbcP9gEyfov/85MTLykB2qt4TG3pamTawMR/rg== X-Received: by 2002:a17:90a:b303:: with SMTP id d3mr4023927pjr.207.1605854885928; Thu, 19 Nov 2020 22:48:05 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.47.56 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:48:05 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 12/21] mm/hugetlb: Introduce remap_huge_page_pmd_vmemmap helper Date: Fri, 20 Nov 2020 14:43:16 +0800 Message-Id: <20201120064325.34492-13-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: The __free_huge_page_pmd_vmemmap and __remap_huge_page_pmd_vmemmap are almost the same code. So introduce remap_free_huge_page_pmd_vmemmap helper to simplify the code. Signed-off-by: Muchun Song --- mm/hugetlb_vmemmap.c | 108 +++++++++++++++++++++------------------------------ 1 file changed, 45 insertions(+), 63 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 361c4174e222..06e2b8a7b7c8 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -252,6 +252,47 @@ static inline int freed_vmemmap_hpage_dec(struct page *page) return atomic_dec_return_relaxed(&page->_mapcount) + 1; } +static inline void free_vmemmap_page_list(struct list_head *list) +{ + struct page *page, *next; + + list_for_each_entry_safe(page, next, list, lru) { + list_del(&page->lru); + free_vmemmap_page(page); + } +} + +typedef void (*remap_pte_fn)(struct page *reuse, pte_t *ptep, + unsigned long start, unsigned long end, + struct list_head *pages); + +static void remap_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd, + unsigned long addr, + struct list_head *pages, + remap_pte_fn remap_fn) +{ + unsigned long next; + unsigned long start = addr + RESERVE_VMEMMAP_SIZE; + unsigned long end = addr + vmemmap_pages_size_per_hpage(h); + struct page *reuse = NULL; + + flush_cache_vunmap(start, end); + + addr = start; + do { + pte_t *ptep; + + ptep = pte_offset_kernel(pmd, addr); + if (!reuse) + reuse = pte_page(ptep[TAIL_PAGE_REUSE]); + + next = vmemmap_hpage_addr_end(addr, end); + remap_fn(reuse, ptep, addr, next, pages); + } while (pmd++, addr = next, addr != end); + + flush_tlb_kernel_range(start, end); +} + static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, unsigned long end, @@ -286,31 +327,6 @@ static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, } } -static void __remap_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd, - unsigned long addr, - struct list_head *remap_pages) -{ - unsigned long next; - unsigned long start = addr + RESERVE_VMEMMAP_SIZE; - unsigned long end = addr + vmemmap_pages_size_per_hpage(h); - struct page *reuse = NULL; - - addr = start; - do { - pte_t *ptep; - - ptep = pte_offset_kernel(pmd, addr); - if (!reuse) - reuse = pte_page(ptep[TAIL_PAGE_REUSE]); - - next = vmemmap_hpage_addr_end(addr, end); - __remap_huge_page_pte_vmemmap(reuse, ptep, addr, next, - remap_pages); - } while (pmd++, addr = next, addr != end); - - flush_tlb_kernel_range(start, end); -} - static inline void alloc_vmemmap_pages(struct hstate *h, struct list_head *list) { int i; @@ -339,8 +355,8 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) BUG_ON(!pmd); ptl = vmemmap_pmd_lock(pmd); - __remap_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, - &remap_pages); + remap_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, &remap_pages, + __remap_huge_page_pte_vmemmap); if (!freed_vmemmap_hpage_dec(pmd_page(*pmd))) { /* * Todo: @@ -350,16 +366,6 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) spin_unlock(ptl); } -static inline void free_vmemmap_page_list(struct list_head *list) -{ - struct page *page, *next; - - list_for_each_entry_safe(page, next, list, lru) { - list_del(&page->lru); - free_vmemmap_page(page); - } -} - static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, unsigned long end, @@ -382,31 +388,6 @@ static void __free_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, } } -static void __free_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd, - unsigned long addr, - struct list_head *free_pages) -{ - unsigned long next; - unsigned long start = addr + RESERVE_VMEMMAP_SIZE; - unsigned long end = addr + vmemmap_pages_size_per_hpage(h); - struct page *reuse = NULL; - - addr = start; - do { - pte_t *ptep; - - ptep = pte_offset_kernel(pmd, addr); - if (!reuse) - reuse = pte_page(ptep[TAIL_PAGE_REUSE]); - - next = vmemmap_hpage_addr_end(addr, end); - __free_huge_page_pte_vmemmap(reuse, ptep, addr, next, - free_pages); - } while (pmd++, addr = next, addr != end); - - flush_tlb_kernel_range(start, end); -} - static void split_vmemmap_pmd(pmd_t *pmd, pte_t *pte_p, unsigned long addr) { int i; @@ -465,7 +446,8 @@ void free_huge_page_vmemmap(struct hstate *h, struct page *head) if (vmemmap_pmd_huge(pmd)) split_vmemmap_huge_page(head, pmd); - __free_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, &free_pages); + remap_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, &free_pages, + __free_huge_page_pte_vmemmap); freed_vmemmap_hpage_inc(pmd_page(*pmd)); spin_unlock(ptl); From patchwork Fri Nov 20 06:43:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919587 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C919FC5519F for ; Fri, 20 Nov 2020 06:48:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4B055206B6 for ; Fri, 20 Nov 2020 06:48:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="oHuG7Cqv" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4B055206B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CBF7E6B007B; Fri, 20 Nov 2020 01:48:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C954A6B007D; Fri, 20 Nov 2020 01:48:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BABC86B007E; Fri, 20 Nov 2020 01:48:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 849916B007B for ; Fri, 20 Nov 2020 01:48:17 -0500 (EST) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 142088249980 for ; Fri, 20 Nov 2020 06:48:17 +0000 (UTC) X-FDA: 77503867434.07.lock50_5c1096327349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin07.hostedemail.com (Postfix) with ESMTP id F0AC91800119E for ; Fri, 20 Nov 2020 06:48:16 +0000 (UTC) X-HE-Tag: lock50_5c1096327349 X-Filterd-Recvd-Size: 5868 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf47.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:48:16 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id v12so6912709pfm.13 for ; Thu, 19 Nov 2020 22:48:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Do8GcGO4xmr+cusNrmUBw5tiCH8/VB9at1POMWv3fh0=; b=oHuG7CqvR273FTn61sbH+LZ9ciiaqHc/fvv8a2VuhUVaTlyz45DJpff3068NZzNE9n f2Y9K+e3P7w2Y/egDOo4mCzZyAA4wuLl3nZmhdYb7rbZSijfzmdPN0ODv3ZN+R2nnS/w aEw6mRLycjPA2344UXwW95+0Da3049dnNjhMOHWOpkrLQ9dWMX8JYK7PkhPFDaGIaPKS 5y5/qZKW4xEnOcpveJgGLWLEcoyURo/p0FFD4RIkZlwA+yGL4i5aTsTMlFXY1PEde1VD bzJXbBlBWtnqpB91w3r9ijKBKplKusdCXHIFQTEo/rpijlyDBpbjr+6fnKmzbFKHYlk6 er3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Do8GcGO4xmr+cusNrmUBw5tiCH8/VB9at1POMWv3fh0=; b=qrtb/Oj4l2b9Xk0U+XbsygbQpTWU6yJ55ifXgQLnqUF+n7+44ceqDqE59vJHjpB8u7 rszjaXlXwgdNU2P7knJGM+HMManD2CMjtBTnbj0lwpQGhRIphCUgEX1vk+w9Acbapeyi ec/S8Qg6H2Z7SwTTN076h1fX0GRaaPLl3idyLlsCWh3KHMdW26xierEdDOFAN3C/60Kr FBgZcqwaJ1iSwn+qnEkimO3xkEUYy8BAbrycesblIxLcYgrKkwFIvrPVhNoKAt6liFyU CrxClnbQpSx1ecRgHXPv0RouDIHxHdg35dtCPhLq0Ibdq8gphsYeCaKE6/xqqvznkSF5 FLDg== X-Gm-Message-State: AOAM530qnoxLTchRNneRNcjPhc4aapBnCXLHhej9HNfNbIC7dCkmUPPv QbRqjKSR0x6pIa9UCLN3oC+1BA== X-Google-Smtp-Source: ABdhPJydpnegg1dAIfXPFOT/olIsplN9RWAEQRFf2e6HdaBkydyhP4+NMb0O7oxmIGQlT2jDgLzaZA== X-Received: by 2002:a17:90a:ca97:: with SMTP id y23mr8185536pjt.186.1605854895707; Thu, 19 Nov 2020 22:48:15 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.48.06 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:48:15 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 13/21] mm/hugetlb: Use PG_slab to indicate split pmd Date: Fri, 20 Nov 2020 14:43:17 +0800 Message-Id: <20201120064325.34492-14-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: When we allocate hugetlb page from buddy, we may need split huge pmd to pte. When we free the hugetlb page, we can merge pte to pmd. So we need to distinguish whether the previous pmd has been split. The page table is not allocated from slab. So we can reuse the PG_slab to indicate that the pmd has been split. Signed-off-by: Muchun Song --- mm/hugetlb_vmemmap.c | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 06e2b8a7b7c8..e2ddc73ce25f 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -293,6 +293,25 @@ static void remap_huge_page_pmd_vmemmap(struct hstate *h, pmd_t *pmd, flush_tlb_kernel_range(start, end); } +static inline bool pmd_split(pmd_t *pmd) +{ + return PageSlab(pmd_page(*pmd)); +} + +static inline void set_pmd_split(pmd_t *pmd) +{ + /* + * We should not use slab for page table allocation. So we can set + * PG_slab to indicate that the pmd has been split. + */ + __SetPageSlab(pmd_page(*pmd)); +} + +static inline void clear_pmd_split(pmd_t *pmd) +{ + __ClearPageSlab(pmd_page(*pmd)); +} + static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, unsigned long start, unsigned long end, @@ -357,11 +376,12 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) ptl = vmemmap_pmd_lock(pmd); remap_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, &remap_pages, __remap_huge_page_pte_vmemmap); - if (!freed_vmemmap_hpage_dec(pmd_page(*pmd))) { + if (!freed_vmemmap_hpage_dec(pmd_page(*pmd)) && pmd_split(pmd)) { /* * Todo: * Merge pte to huge pmd if it has ever been split. */ + clear_pmd_split(pmd); } spin_unlock(ptl); } @@ -443,8 +463,10 @@ void free_huge_page_vmemmap(struct hstate *h, struct page *head) BUG_ON(!pmd); ptl = vmemmap_pmd_lock(pmd); - if (vmemmap_pmd_huge(pmd)) + if (vmemmap_pmd_huge(pmd)) { split_vmemmap_huge_page(head, pmd); + set_pmd_split(pmd); + } remap_huge_page_pmd_vmemmap(h, pmd, (unsigned long)head, &free_pages, __free_huge_page_pte_vmemmap); From patchwork Fri Nov 20 06:43:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919589 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 13413C5519F for ; Fri, 20 Nov 2020 06:48:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7450E206B6 for ; Fri, 20 Nov 2020 06:48:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="Hdg9qI+B" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7450E206B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id EB99B6B005C; Fri, 20 Nov 2020 01:48:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E91366B0068; Fri, 20 Nov 2020 01:48:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D834C6B007E; Fri, 20 Nov 2020 01:48:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0067.hostedemail.com [216.40.44.67]) by kanga.kvack.org (Postfix) with ESMTP id A0CD26B005C for ; Fri, 20 Nov 2020 01:48:27 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 3A3D3180AD820 for ; Fri, 20 Nov 2020 06:48:27 +0000 (UTC) X-FDA: 77503867854.25.trade13_630cd5e27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin25.hostedemail.com (Postfix) with ESMTP id 1768518015434 for ; Fri, 20 Nov 2020 06:48:27 +0000 (UTC) X-HE-Tag: trade13_630cd5e27349 X-Filterd-Recvd-Size: 8599 Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.195]) by imf33.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:48:26 +0000 (UTC) Received: by mail-pf1-f195.google.com with SMTP id v5so2888567pff.10 for ; Thu, 19 Nov 2020 22:48:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=fPRCn8/kiXAueLuQQpe5BNdwIIijLQR0RASycvhGpiY=; b=Hdg9qI+B/zArNnptrPOekO3F5Sv+7WQ850J/5YBXdZBHBc3v8aXdpEebbBU+ZNUvu9 kBaASy+YQSDYvO+l3cAkvWy8afEN1dYK9ZNDa3Eukh9Lb8cT+8ZSHdyR1PlcgH6Q5YOm GBMhHVk3LwTwXq+yPufzgU6rHL0c1VMDrEkBWD6N1kege8+75amTfwFh4ZkpzCqUwMmd nSRKZOJ0yG0/NQ0W+xhMVtdoTRx/KEFW0uFBuSbYJKHypluo0aCz3H4HiJM7zWGF/EsM 8iYDpQdcRIuRsH2viPUOTUG8LIWyyAR0oFh1rbnvK/Ews3d+1eTir/b1kh97ITDUESDb o0fQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fPRCn8/kiXAueLuQQpe5BNdwIIijLQR0RASycvhGpiY=; b=FkoDCbBWKP1erf1jxN7lKqAWsYLS0jgvzuBLNztzNf5SRcJ6yR59IXdGmckwBRxNRg nShHulw5hIe5lucXKnV3LjykhYLKUuRzeObYgio7u1Fr3tvtA4Y5MrSV1xrVXUnaQIzB 12HDfD2OAnrlNGDAG3LVy60HEDHYrVYdhRyV47nbwL734chiyBWiaLpNiV152Fwj16Qh oFkBrV6FL/YcFr3u3sQpW2jmexWy51QXeF+0Bx4dllp8mGo6pocoAiDPbCH/b+x6CzaI TpnYPnFNdx15eYkEKrI0TO1/LZPZx/Q1nn6l6tgL6YoG+I+Cp5PBYD2nJFixMual9HBQ sOIw== X-Gm-Message-State: AOAM530BTRgS7E8VN+FbkJy6judM5dAAM9ue/o7/lNNP4nsR1VcjcwEq eadHBITM3F/7xWDXOy++vXGZyQ== X-Google-Smtp-Source: ABdhPJx4u46vHQNchdnfimXCIyMWc/51pYcv2O01OlE739ipscOuIyXRdkQKW5MvYwDW6CRWcxH3mQ== X-Received: by 2002:a63:504f:: with SMTP id q15mr12829737pgl.119.1605854905654; Thu, 19 Nov 2020 22:48:25 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.48.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:48:25 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 14/21] mm/hugetlb: Support freeing vmemmap pages of gigantic page Date: Fri, 20 Nov 2020 14:43:18 +0800 Message-Id: <20201120064325.34492-15-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: The gigantic page is allocated by bootmem, if we want to free the unused vmemmap pages. We also should allocate the page table. So we also allocate page tables from bootmem. Signed-off-by: Muchun Song --- include/linux/hugetlb.h | 3 +++ mm/hugetlb.c | 5 +++++ mm/hugetlb_vmemmap.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ mm/hugetlb_vmemmap.h | 13 +++++++++++ 4 files changed, 81 insertions(+) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index eed3dd3bd626..da18fc9ed152 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -506,6 +506,9 @@ struct hstate { struct huge_bootmem_page { struct list_head list; struct hstate *hstate; +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP + pte_t *vmemmap_pte; +#endif }; struct page *alloc_huge_page(struct vm_area_struct *vma, diff --git a/mm/hugetlb.c b/mm/hugetlb.c index ba927ae7f9bd..055604d07046 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -2607,6 +2607,7 @@ static void __init gather_bootmem_prealloc(void) WARN_ON(page_count(page) != 1); prep_compound_huge_page(page, h->order); WARN_ON(PageReserved(page)); + gather_vmemmap_pgtable_init(m, page); prep_new_huge_page(h, page, page_to_nid(page)); put_page(page); /* free it into the hugepage allocator */ @@ -2659,6 +2660,10 @@ static void __init hugetlb_hstate_alloc_pages(struct hstate *h) break; cond_resched(); } + + if (hstate_is_gigantic(h)) + i -= gather_vmemmap_pgtable_prealloc(); + if (i < h->max_huge_pages) { char buf[32]; diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index e2ddc73ce25f..3629165d8158 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -103,6 +103,7 @@ #include #include #include +#include #include #include "hugetlb_vmemmap.h" @@ -204,6 +205,65 @@ int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page) return -ENOMEM; } +unsigned long __init gather_vmemmap_pgtable_prealloc(void) +{ + struct huge_bootmem_page *m, *tmp; + unsigned long nr_free = 0; + + list_for_each_entry_safe(m, tmp, &huge_boot_pages, list) { + struct hstate *h = m->hstate; + unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h); + unsigned int pgtable_size; + + if (!nr) + continue; + + pgtable_size = nr << PAGE_SHIFT; + m->vmemmap_pte = memblock_alloc_try_nid(pgtable_size, + PAGE_SIZE, 0, MEMBLOCK_ALLOC_ACCESSIBLE, + NUMA_NO_NODE); + if (!m->vmemmap_pte) { + nr_free++; + list_del(&m->list); + memblock_free_early(__pa(m), huge_page_size(h)); + } + } + + return nr_free; +} + +void __init gather_vmemmap_pgtable_init(struct huge_bootmem_page *m, + struct page *page) +{ + struct hstate *h = m->hstate; + unsigned long pte = (unsigned long)m->vmemmap_pte; + unsigned int nr = pgtable_pages_to_prealloc_per_hpage(h); + + /* + * Use the huge page lru list to temporarily store the preallocated + * pages. The preallocated pages are used and the list is emptied + * before the huge page is put into use. When the huge page is put + * into use by prep_new_huge_page() the list will be reinitialized. + */ + INIT_LIST_HEAD(&page->lru); + + while (nr--) { + struct page *pte_page = virt_to_page(pte); + + __ClearPageReserved(pte_page); + list_add(&pte_page->lru, &page->lru); + pte += PAGE_SIZE; + } + + /* + * If we had gigantic hugepages allocated at boot time, we need + * to restore the 'stolen' pages to totalram_pages in order to + * fix confusing memory reports from free(1) and another + * side-effects, like CommitLimit going negative. + */ + adjust_managed_page_count(page, nr); +} + /* * Walk a vmemmap address to the pmd it maps. */ diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 6dfa7ed6f88a..779d3cb9333f 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -14,6 +14,9 @@ void __init hugetlb_vmemmap_init(struct hstate *h); int vmemmap_pgtable_prealloc(struct hstate *h, struct page *page); void vmemmap_pgtable_free(struct page *page); +unsigned long __init gather_vmemmap_pgtable_prealloc(void); +void __init gather_vmemmap_pgtable_init(struct huge_bootmem_page *m, + struct page *page); void alloc_huge_page_vmemmap(struct hstate *h, struct page *head); void free_huge_page_vmemmap(struct hstate *h, struct page *head); @@ -35,6 +38,16 @@ static inline void vmemmap_pgtable_free(struct page *page) { } +static inline unsigned long gather_vmemmap_pgtable_prealloc(void) +{ + return 0; +} + +static inline void gather_vmemmap_pgtable_init(struct huge_bootmem_page *m, + struct page *page) +{ +} + static inline void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) { } From patchwork Fri Nov 20 06:43:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919591 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3A4AC5519F for ; Fri, 20 Nov 2020 06:48:38 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4442E2222F for ; Fri, 20 Nov 2020 06:48:38 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="mVgZ4mHI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4442E2222F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CE4A76B006C; Fri, 20 Nov 2020 01:48:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C964C6B006E; Fri, 20 Nov 2020 01:48:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B84286B007E; Fri, 20 Nov 2020 01:48:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0164.hostedemail.com [216.40.44.164]) by kanga.kvack.org (Postfix) with ESMTP id 809BC6B006C for ; Fri, 20 Nov 2020 01:48:37 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0DABF181AEF09 for ; Fri, 20 Nov 2020 06:48:37 +0000 (UTC) X-FDA: 77503868274.12.rain03_1215ee527349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id DEDED18027C96 for ; Fri, 20 Nov 2020 06:48:36 +0000 (UTC) X-HE-Tag: rain03_1215ee527349 X-Filterd-Recvd-Size: 6862 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf06.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:48:36 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id v21so6505960pgi.2 for ; Thu, 19 Nov 2020 22:48:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7YF/BLlniOkOqlOLuNwhoC17uEIWhpvSsCIax+QfC3Y=; b=mVgZ4mHIEX3s8Zda4GGNN1XhpnpwWrK9FhJz4NVGZ5Z0494vBrBobx06xswFqn6/rr 1lV4CwYDyAGLXE9MJI2t4mqIiHFKxstajuTtTufgI/wvjninvZLxP5JZR8DJc/pOTbYq 9YyU7YaNviau0//pkxfy24hNjNdwNuhjaZFTOHWDaOrTzeaqf1+jMT/VI16TCvykK3PY piX4KgJYDj95dbFMDCGIIrnC2P8OSPP391tzjX74bg8YJcFG+kZRoAi0lR/nWInRPjiv IdHeMT/EfNxJVoogxH1Ta+EiWnYxZFHGg6cvzSnZQtnO4ZGVqQAYxxLV9AtigYQBL6Ei YKLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7YF/BLlniOkOqlOLuNwhoC17uEIWhpvSsCIax+QfC3Y=; b=J+LuNJGBt+xKIPs3aVUAsir58s6ejutbwVQOsWvFs0irlOwBbOPqV8tl4Z2s6cT499 fc05AQ2gy19qTObwBF60ra+8NZ6BhWVvRLA7vUa/wBH/3o3W2YHiSc4L3hViers3qLb7 o/khPCDeZJqZ5s5+Q/7K7PKOkLgQt1WgHn0nv1ZKsSUboy33jFL5XE9zUeqhk1+p3DiU R86JNa/834C19QOE0Txp5yeYx2tpfHAfCbgBgavi+yv/rmwZ/ts/7hrsNGugpOIv3SLl sifcBdIu3g52puYstHGuUw4N+zsD61TcyOZLQTF8skdFKer15+hdbi4zlBxHQ5XB3Rad pI5w== X-Gm-Message-State: AOAM530XJsyhC0MFnfrbPLsfIxbRduiyJUTiqxk5zzRVSD1mN1Va+kH0 Re450ybjez0DkCXklgGUDRy2Tw== X-Google-Smtp-Source: ABdhPJzUxMIedTA7fjy9+8PXQ5X870hMnht0hWNgRLugfp9zuqD73EKTYWSoXGdNOucbnRdCj2YQyw== X-Received: by 2002:aa7:84d0:0:b029:18b:fac7:29b with SMTP id x16-20020aa784d00000b029018bfac7029bmr12420886pfn.29.1605854915274; Thu, 19 Nov 2020 22:48:35 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.48.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:48:34 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 15/21] mm/hugetlb: Set the PageHWPoison to the raw error page Date: Fri, 20 Nov 2020 14:43:19 +0800 Message-Id: <20201120064325.34492-16-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: Because we reuse the first tail page, if we set PageHWPosion on a tail page. It indicates that we may set PageHWPoison on a series of pages. So we can use the head[4].mapping to record the real error page index and set the raw error page PageHWPoison later. Signed-off-by: Muchun Song --- mm/hugetlb.c | 11 +++-------- mm/hugetlb_vmemmap.h | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 055604d07046..b853aacd5c16 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1383,6 +1383,7 @@ static void __free_hugepage(struct hstate *h, struct page *page) int i; alloc_huge_page_vmemmap(h, page); + subpage_hwpoison_deliver(page); for (i = 0; i < pages_per_huge_page(h); i++) { page[i].flags &= ~(1 << PG_locked | 1 << PG_error | @@ -1944,14 +1945,8 @@ int dissolve_free_huge_page(struct page *page) int nid = page_to_nid(head); if (h->free_huge_pages - h->resv_huge_pages == 0) goto out; - /* - * Move PageHWPoison flag from head page to the raw error page, - * which makes any subpages rather than the error page reusable. - */ - if (PageHWPoison(head) && page != head) { - SetPageHWPoison(page); - ClearPageHWPoison(head); - } + + set_subpage_hwpoison(head, page); list_del(&head->lru); h->free_huge_pages--; h->free_huge_pages_node[nid]--; diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 779d3cb9333f..65e94436ffff 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -20,6 +20,29 @@ void __init gather_vmemmap_pgtable_init(struct huge_bootmem_page *m, void alloc_huge_page_vmemmap(struct hstate *h, struct page *head); void free_huge_page_vmemmap(struct hstate *h, struct page *head); +static inline void subpage_hwpoison_deliver(struct page *head) +{ + struct page *page = head; + + if (PageHWPoison(head)) + page = head + page_private(head + 4); + + /* + * Move PageHWPoison flag from head page to the raw error page, + * which makes any subpages rather than the error page reusable. + */ + if (page != head) { + SetPageHWPoison(page); + ClearPageHWPoison(head); + } +} + +static inline void set_subpage_hwpoison(struct page *head, struct page *page) +{ + if (PageHWPoison(head)) + set_page_private(head + 4, page - head); +} + static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) { return h->nr_free_vmemmap_pages; @@ -56,6 +79,22 @@ static inline void free_huge_page_vmemmap(struct hstate *h, struct page *head) { } +static inline void subpage_hwpoison_deliver(struct page *head) +{ +} + +static inline void set_subpage_hwpoison(struct page *head, struct page *page) +{ + /* + * Move PageHWPoison flag from head page to the raw error page, + * which makes any subpages rather than the error page reusable. + */ + if (PageHWPoison(head) && page != head) { + SetPageHWPoison(page); + ClearPageHWPoison(head); + } +} + static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) { return 0; From patchwork Fri Nov 20 06:43:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919593 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E826FC5519F for ; Fri, 20 Nov 2020 06:48:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6E1A2206B6 for ; Fri, 20 Nov 2020 06:48:47 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="oWDyzyq+" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6E1A2206B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F08DF6B007E; Fri, 20 Nov 2020 01:48:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E6A3F6B0080; Fri, 20 Nov 2020 01:48:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D331C6B0081; Fri, 20 Nov 2020 01:48:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0195.hostedemail.com [216.40.44.195]) by kanga.kvack.org (Postfix) with ESMTP id 97C516B007E for ; Fri, 20 Nov 2020 01:48:46 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 35F273649 for ; Fri, 20 Nov 2020 06:48:46 +0000 (UTC) X-FDA: 77503868652.06.angle93_4808b5a27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 182E31004E96D for ; Fri, 20 Nov 2020 06:48:46 +0000 (UTC) X-HE-Tag: angle93_4808b5a27349 X-Filterd-Recvd-Size: 5416 Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.195]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:48:45 +0000 (UTC) Received: by mail-pf1-f195.google.com with SMTP id y7so6914095pfq.11 for ; Thu, 19 Nov 2020 22:48:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cH/Hq+5EQ/oi9mKzhIRZsbYyftv0OzxuKDhe9q6sjjU=; b=oWDyzyq+dmGpl9uNGNrMrsM6EXsNni7+X0imlQSsWu4R1nxGalcVhmbeoUlxgFYiA8 9Oy8mwvmKN2m4utS4QX/h/Gt4xfyYaHVOfrpU3wVsrh74EWb37tASIzHMGqCMR95HZr6 Gghyyb2TOGsOwFqD16ajMcQt71XhiHnPcpry55159JjoEXpeEpmQq9s2/VS4JfJ+FOM2 Dz5L2yie3tf8kPA8V93tIb/ZgFaVs/OXnh3je1kWzyjfgpxE0W9/eRgDYFRlL4lYFzWs kLBDAEvYUarT7qLPadNxBUQGXeCim/43gLgJEECnN3hSvGLIUF+mlAp+h4WPl9X+Lzra vSZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cH/Hq+5EQ/oi9mKzhIRZsbYyftv0OzxuKDhe9q6sjjU=; b=Ykm7rKt9U2HRKet1MvIqxFqs6EIBSD1d69s831spbbJMJRg2EzzOu4pveeT7vFnDP4 5dr0clQGb3VqMnYu91cWJi0ALun8xmv4axq3lfKceaHJhjuapVIfBPTENHizjMUqoMYs eyRR92aEUKr1lCH8EsnwqgAsYtndhK6+XPcc+s4yUfrbxkW04n1o6zX12q1K+zFUpCxD cnmlDQ41Ckq3Sk15QvaOonQhY6g1bS1Q74N4++Yjiu3YHOR32EuVDbVF7/JYdZuVmnwy mWJZVPTdpL57XvAR9BXWQB2I5PiJXqgqqA78cH9+gohQ+oUJKtlPwby/Y742X5FnjZCQ jmHQ== X-Gm-Message-State: AOAM532PsT7RUhMJlIBuQfBx38WeH+yJ+a4PjhYpnT6xooBKuyOyxScR 3xpuAHzPAiMIvWH2L5LJxApIrw== X-Google-Smtp-Source: ABdhPJwDvG7GsuYtBL0kSSFWUAuwbp52LipL66IhZf9RaKJAS1nviAYWM5o/UWaxVi0myGODxMhyHg== X-Received: by 2002:a17:90a:a481:: with SMTP id z1mr1626175pjp.202.1605854924788; Thu, 19 Nov 2020 22:48:44 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.48.35 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:48:44 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 16/21] mm/hugetlb: Flush work when dissolving hugetlb page Date: Fri, 20 Nov 2020 14:43:20 +0800 Message-Id: <20201120064325.34492-17-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: We should flush work when dissolving a hugetlb page to make sure that the hugetlb page is freed to the buddy. Signed-off-by: Muchun Song --- mm/hugetlb.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index b853aacd5c16..9aad0b63d369 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1328,6 +1328,12 @@ static void update_hpage_vmemmap_workfn(struct work_struct *work) } static DECLARE_WORK(hpage_update_work, update_hpage_vmemmap_workfn); +static inline void flush_hpage_update_work(struct hstate *h) +{ + if (free_vmemmap_pages_per_hpage(h)) + flush_work(&hpage_update_work); +} + static inline void __update_and_free_page(struct hstate *h, struct page *page) { /* No need to allocate vmemmap pages */ @@ -1928,6 +1934,7 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed, int dissolve_free_huge_page(struct page *page) { int rc = -EBUSY; + struct hstate *h = NULL; /* Not to disrupt normal path by vainly holding hugetlb_lock */ if (!PageHuge(page)) @@ -1941,8 +1948,9 @@ int dissolve_free_huge_page(struct page *page) if (!page_count(page)) { struct page *head = compound_head(page); - struct hstate *h = page_hstate(head); int nid = page_to_nid(head); + + h = page_hstate(head); if (h->free_huge_pages - h->resv_huge_pages == 0) goto out; @@ -1956,6 +1964,14 @@ int dissolve_free_huge_page(struct page *page) } out: spin_unlock(&hugetlb_lock); + + /* + * We should flush work before return to make sure that + * the HugeTLB page is freed to the buddy. + */ + if (!rc && h) + flush_hpage_update_work(h); + return rc; } From patchwork Fri Nov 20 06:43:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919595 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31E65C5519F for ; Fri, 20 Nov 2020 06:48:57 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B324D223AC for ; Fri, 20 Nov 2020 06:48:56 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="lcwWZUVW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B324D223AC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3E4BC6B0072; Fri, 20 Nov 2020 01:48:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 36F5B6B0073; Fri, 20 Nov 2020 01:48:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19B1A6B0081; Fri, 20 Nov 2020 01:48:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0169.hostedemail.com [216.40.44.169]) by kanga.kvack.org (Postfix) with ESMTP id CF93E6B0072 for ; Fri, 20 Nov 2020 01:48:55 -0500 (EST) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6A0CD181AEF1F for ; Fri, 20 Nov 2020 06:48:55 +0000 (UTC) X-FDA: 77503869030.23.roof52_2707a6727349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id 4EC7B37604 for ; Fri, 20 Nov 2020 06:48:55 +0000 (UTC) X-HE-Tag: roof52_2707a6727349 X-Filterd-Recvd-Size: 6737 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf27.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:48:54 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id j19so6487050pgg.5 for ; Thu, 19 Nov 2020 22:48:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eD78AzWQVV1uAAuXAFqLOGFAQosJqi4Yg9b3koUXsus=; b=lcwWZUVWnsQa5LT0KgaEjI0iwQ9Zy5KTdkDwdkaYkIYrx0sageBcgcvZ02a8P7IinQ cH5lrqUx09ERWxttsalm94SKQsQY0j6YCySKE/qz+9qixGVDly1/Cm5JbHmwcIDBD7WL qtkr0vBPhTrsvRVOh3aJI+DhhOqf+WOwZwne/kV3PDRrWDacby9e6XbuOJjfECG0xIhY rMH/tVXPlloLTOHZI44HQN35LHMa+2J6nR1JL8jBr+E/4YTbzBFNHhM59vHTB+3J+PB2 l6ZJRCDsJJ0nRZmFn+SzJpi7oN3oQhm7q0KA10Asz0SBMEKJBD6xgv+9zezxopyUkfdG mYvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eD78AzWQVV1uAAuXAFqLOGFAQosJqi4Yg9b3koUXsus=; b=BoxyxMLQd8wUg9yJOWp/gBYQSih15aCLGU5HhbRRVxRdtd3TR2YneU9YoZeRYk5dCW IKssTH9R/4KHj9H75qUU61j3+2ErQ7YDDAO4okG4ffgGN/e2uuWFHWv9+mBteu88YFld vLiol+2jD/C88sn2ywa/D/j4QtFTZ2YoYMRi3S6sbVtAhyo0tcm/rNK4zkzLqAQ0pdaj oFukdkDwKXfVec/2unCylzmA/REQxMfbTlRbM23LFS4OrLugDbIIVuoQROPd6pD/gF5v Pw4gllxW15kzCS396UPcKxKO/H3a0FTuSkMZGU2GSC5fPoXSwtS93mYauAaLUGBAz5Ge L3xQ== X-Gm-Message-State: AOAM531nzrvKhdYxoXx92IhSgPAn9MWr7bLgakgFh7ZPHKHRyYiwOIjc FRAUaUhcEQBidbRQNJU4hqfkag== X-Google-Smtp-Source: ABdhPJzVi5Mi23xJ7xuPD8J+lf/CK5R/RCg+2yoXfUf7ZH72VA+vSBnxOfiz+7uMBS0fZyuYnbVoyA== X-Received: by 2002:a63:5826:: with SMTP id m38mr15569054pgb.240.1605854934100; Thu, 19 Nov 2020 22:48:54 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.48.45 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:48:53 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 17/21] mm/hugetlb: Add a kernel parameter hugetlb_free_vmemmap Date: Fri, 20 Nov 2020 14:43:21 +0800 Message-Id: <20201120064325.34492-18-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: Add a kernel parameter hugetlb_free_vmemmap to disable the feature of freeing unused vmemmap pages associated with each hugetlb page on boot. Signed-off-by: Muchun Song --- Documentation/admin-guide/kernel-parameters.txt | 9 +++++++++ Documentation/admin-guide/mm/hugetlbpage.rst | 3 +++ mm/hugetlb_vmemmap.c | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 5debfe238027..ccf07293cb63 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1551,6 +1551,15 @@ Documentation/admin-guide/mm/hugetlbpage.rst. Format: size[KMG] + hugetlb_free_vmemmap= + [KNL] When CONFIG_HUGETLB_PAGE_FREE_VMEMMAP is set, + this controls freeing unused vmemmap pages associated + with each HugeTLB page. + Format: { on (default) | off } + + on: enable the feature + off: disable the feature + hung_task_panic= [KNL] Should the hung task detector generate panics. Format: 0 | 1 diff --git a/Documentation/admin-guide/mm/hugetlbpage.rst b/Documentation/admin-guide/mm/hugetlbpage.rst index f7b1c7462991..7d6129ee97dd 100644 --- a/Documentation/admin-guide/mm/hugetlbpage.rst +++ b/Documentation/admin-guide/mm/hugetlbpage.rst @@ -145,6 +145,9 @@ default_hugepagesz will all result in 256 2M huge pages being allocated. Valid default huge page size is architecture dependent. +hugetlb_free_vmemmap + When CONFIG_HUGETLB_PAGE_FREE_VMEMMAP is set, this disables freeing + unused vmemmap pages associated each HugeTLB page. When multiple huge page sizes are supported, ``/proc/sys/vm/nr_hugepages`` indicates the current number of pre-allocated huge pages of the default size. diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 3629165d8158..c958699d1393 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -144,6 +144,22 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) } #endif +static bool hugetlb_free_vmemmap_disabled __initdata; + +static int __init early_hugetlb_free_vmemmap_param(char *buf) +{ + if (!buf) + return -EINVAL; + + if (!strcmp(buf, "off")) + hugetlb_free_vmemmap_disabled = true; + else if (strcmp(buf, "on")) + return -EINVAL; + + return 0; +} +early_param("hugetlb_free_vmemmap", early_hugetlb_free_vmemmap_param); + static inline unsigned int vmemmap_pages_per_hpage(struct hstate *h) { return free_vmemmap_pages_per_hpage(h) + RESERVE_VMEMMAP_NR; @@ -541,6 +557,11 @@ void __init hugetlb_vmemmap_init(struct hstate *h) unsigned int order = huge_page_order(h); unsigned int vmemmap_pages; + if (hugetlb_free_vmemmap_disabled) { + pr_info("disable free vmemmap pages for %s\n", h->name); + return; + } + vmemmap_pages = ((1 << order) * sizeof(struct page)) >> PAGE_SHIFT; /* * The head page and the first tail page are not to be freed to buddy From patchwork Fri Nov 20 06:43:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919597 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5425FC56201 for ; Fri, 20 Nov 2020 06:49:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BC2962222F for ; Fri, 20 Nov 2020 06:49:06 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="BSAwH9Ej" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BC2962222F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 433A76B0074; Fri, 20 Nov 2020 01:49:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E2B66B0075; Fri, 20 Nov 2020 01:49:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 25E116B0081; Fri, 20 Nov 2020 01:49:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0210.hostedemail.com [216.40.44.210]) by kanga.kvack.org (Postfix) with ESMTP id DC5676B0074 for ; Fri, 20 Nov 2020 01:49:05 -0500 (EST) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 62A6E8249980 for ; Fri, 20 Nov 2020 06:49:05 +0000 (UTC) X-FDA: 77503869450.06.frame43_0b1714727349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin06.hostedemail.com (Postfix) with ESMTP id 4213E1004F4C0 for ; Fri, 20 Nov 2020 06:49:05 +0000 (UTC) X-HE-Tag: frame43_0b1714727349 X-Filterd-Recvd-Size: 8884 Received: from mail-pf1-f196.google.com (mail-pf1-f196.google.com [209.85.210.196]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:49:04 +0000 (UTC) Received: by mail-pf1-f196.google.com with SMTP id v12so6914334pfm.13 for ; Thu, 19 Nov 2020 22:49:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0qctfG3gJUmERYZTVTo1F5sjcFprZo6qFB7UJqlVwrU=; b=BSAwH9EjraRir3cww1CoVRTE007x/+7QVNzoG3PzekHH09mBhCaKqnz0OwQ7KJaM8v 4MIZPvBj7KhagHnsUUaqrc0uZg5EeoXlpj0glXH4bDPYzqofIiIKvZCKH6YVmvOGk9Xy wJqkGAZmW5KiSzSjMrWCG3e0D3/MriQ4fl12o3f5x51ks9uU0ST9Tfn8JwpOf8Qco5+o cADPZQF0Vwc06JfqKUC7uVoxxkv9G2imezKCGbZOffF7M9OT+Jdt4PblHsw0MZM4uWl/ PspPPIdboh+lt4z+vnrzW99u0/etMku4PGz9yjTSirplS0GUwPJSY5vfFKGpSvH+cmNb jhkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0qctfG3gJUmERYZTVTo1F5sjcFprZo6qFB7UJqlVwrU=; b=bBzQabu96lBVGCs96Z6AtKl2Qbtxhzh4XS+9vLRT6+6smFUQuBVwcSBW7JSy3d9obM iA0/Wd4KBqyzI/Tba8uLtwuwjPHQkpcQcO3fl/GTJpNsqlzm49AE8aGX+06s/fDedsDl uoESmNUSM3aT9bfunP+qfkA1n3zwtTHYpxxM7HCtP/+9AITjldhlgtYFVNVz0Y1pP3x9 8xPzXNOSGgCGWpFc/b/HhBFZvtBNXWn7NuOAzPt4spzc4yWH1aNLlHtI68w/RrQXV0J2 gJfiN+i2Ojz6mrjm4awGNVHSfR5AvZL428WVBh07waOWYN3PNs1gHIb/AwbebtlQT6NM firw== X-Gm-Message-State: AOAM5316y2ZHCBCjdCyfK3XrIcDOWApp/Py36RqFTGCek6OK506Wcbzq Wam8mJtjjtvMYeKKd24QNTn6bA== X-Google-Smtp-Source: ABdhPJybMhpIUy0g3aoXxNsEUl7G769sCoNjZL2PNUfGzHQDHuk/8+/MvlB7JNgeRr0UwMeYalJzEg== X-Received: by 2002:a17:90a:460b:: with SMTP id w11mr8456571pjg.12.1605854943868; Thu, 19 Nov 2020 22:49:03 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.48.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:49:03 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 18/21] mm/hugetlb: Merge pte to huge pmd only for gigantic page Date: Fri, 20 Nov 2020 14:43:22 +0800 Message-Id: <20201120064325.34492-19-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: Merge pte to huge pmd if it has ever been split. Now only support gigantic page which's vmemmap pages size is an integer multiple of PMD_SIZE. This is the simplest case to handle. Signed-off-by: Muchun Song --- arch/x86/include/asm/hugetlb.h | 8 +++ mm/hugetlb_vmemmap.c | 118 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/hugetlb.h b/arch/x86/include/asm/hugetlb.h index c601fe042832..1de1c519a84a 100644 --- a/arch/x86/include/asm/hugetlb.h +++ b/arch/x86/include/asm/hugetlb.h @@ -12,6 +12,14 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) { return pmd_large(*pmd); } + +#define vmemmap_pmd_mkhuge vmemmap_pmd_mkhuge +static inline pmd_t vmemmap_pmd_mkhuge(struct page *page) +{ + pte_t entry = pfn_pte(page_to_pfn(page), PAGE_KERNEL_LARGE); + + return __pmd(pte_val(entry)); +} #endif #define hugepages_supported() boot_cpu_has(X86_FEATURE_PSE) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index c958699d1393..bf2b6b3e75af 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -144,6 +144,14 @@ static inline bool vmemmap_pmd_huge(pmd_t *pmd) } #endif +#ifndef vmemmap_pmd_mkhuge +#define vmemmap_pmd_mkhuge vmemmap_pmd_mkhuge +static inline pmd_t vmemmap_pmd_mkhuge(struct page *page) +{ + return pmd_mkhuge(mk_pmd(page, PAGE_KERNEL)); +} +#endif + static bool hugetlb_free_vmemmap_disabled __initdata; static int __init early_hugetlb_free_vmemmap_param(char *buf) @@ -422,6 +430,104 @@ static void __remap_huge_page_pte_vmemmap(struct page *reuse, pte_t *ptep, } } +static void __replace_huge_page_pte_vmemmap(pte_t *ptep, unsigned long start, + unsigned int nr, struct page *huge, + struct list_head *free_pages) +{ + unsigned long addr; + unsigned long end = start + (nr << PAGE_SHIFT); + pgprot_t pgprot = PAGE_KERNEL; + + for (addr = start; addr < end; addr += PAGE_SIZE, ptep++) { + struct page *page; + pte_t old = *ptep; + pte_t entry; + + prepare_vmemmap_page(huge); + + entry = mk_pte(huge++, pgprot); + VM_WARN_ON(!pte_present(old)); + page = pte_page(old); + list_add(&page->lru, free_pages); + + set_pte_at(&init_mm, addr, ptep, entry); + } +} + +static void replace_huge_page_pmd_vmemmap(pmd_t *pmd, unsigned long start, + struct page *huge, + struct list_head *free_pages) +{ + unsigned long end = start + VMEMMAP_HPAGE_SIZE; + + flush_cache_vunmap(start, end); + __replace_huge_page_pte_vmemmap(pte_offset_kernel(pmd, start), start, + VMEMMAP_HPAGE_NR, huge, free_pages); + flush_tlb_kernel_range(start, end); +} + +static pte_t *merge_vmemmap_pte(pmd_t *pmdp, unsigned long addr) +{ + pte_t *pte; + struct page *page; + + pte = pte_offset_kernel(pmdp, addr); + page = pte_page(*pte); + set_pmd(pmdp, vmemmap_pmd_mkhuge(page)); + + return pte; +} + +static void merge_huge_page_pmd_vmemmap(pmd_t *pmd, unsigned long start, + struct page *huge, + struct list_head *free_pages) +{ + replace_huge_page_pmd_vmemmap(pmd, start, huge, free_pages); + pte_free_kernel(&init_mm, merge_vmemmap_pte(pmd, start)); + flush_tlb_kernel_range(start, start + VMEMMAP_HPAGE_SIZE); +} + +static inline void dissolve_compound_page(struct page *page, unsigned int order) +{ + int i; + unsigned int nr_pages = 1 << order; + + for (i = 1; i < nr_pages; i++) + set_page_count(page + i, 1); +} + +static void merge_gigantic_page_vmemmap(struct hstate *h, struct page *head, + pmd_t *pmd) +{ + LIST_HEAD(free_pages); + unsigned long addr = (unsigned long)head; + unsigned long end = addr + vmemmap_pages_size_per_hpage(h); + + for (; addr < end; addr += VMEMMAP_HPAGE_SIZE) { + void *to; + struct page *page; + + page = alloc_pages(GFP_VMEMMAP_PAGE & ~__GFP_NOFAIL, + VMEMMAP_HPAGE_ORDER); + if (!page) + goto out; + + dissolve_compound_page(page, VMEMMAP_HPAGE_ORDER); + to = page_to_virt(page); + memcpy(to, (void *)addr, VMEMMAP_HPAGE_SIZE); + + /* + * Make sure that any data that writes to the + * @to is made visible to the physical page. + */ + flush_kernel_vmap_range(to, VMEMMAP_HPAGE_SIZE); + + merge_huge_page_pmd_vmemmap(pmd++, addr, page, &free_pages); + } +out: + free_vmemmap_page_list(&free_pages); +} + static inline void alloc_vmemmap_pages(struct hstate *h, struct list_head *list) { int i; @@ -454,10 +560,18 @@ void alloc_huge_page_vmemmap(struct hstate *h, struct page *head) __remap_huge_page_pte_vmemmap); if (!freed_vmemmap_hpage_dec(pmd_page(*pmd)) && pmd_split(pmd)) { /* - * Todo: - * Merge pte to huge pmd if it has ever been split. + * Merge pte to huge pmd if it has ever been split. Now only + * support gigantic page which's vmemmap pages size is an + * integer multiple of PMD_SIZE. This is the simplest case + * to handle. */ clear_pmd_split(pmd); + + if (IS_ALIGNED(vmemmap_pages_per_hpage(h), VMEMMAP_HPAGE_NR)) { + spin_unlock(ptl); + merge_gigantic_page_vmemmap(h, head, pmd); + return; + } } spin_unlock(ptl); } From patchwork Fri Nov 20 06:43:23 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919599 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DB338C56201 for ; Fri, 20 Nov 2020 06:49:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 577AF22404 for ; Fri, 20 Nov 2020 06:49:18 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="ilarJVpi" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 577AF22404 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D30FA6B0081; Fri, 20 Nov 2020 01:49:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CBB186B0082; Fri, 20 Nov 2020 01:49:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5CD96B0083; Fri, 20 Nov 2020 01:49:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0181.hostedemail.com [216.40.44.181]) by kanga.kvack.org (Postfix) with ESMTP id 769BA6B0081 for ; Fri, 20 Nov 2020 01:49:17 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0EA8A180AD81F for ; Fri, 20 Nov 2020 06:49:17 +0000 (UTC) X-FDA: 77503869954.24.band80_250b8ae27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id DF9D41A4A0 for ; Fri, 20 Nov 2020 06:49:16 +0000 (UTC) X-HE-Tag: band80_250b8ae27349 X-Filterd-Recvd-Size: 9173 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf10.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:49:16 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id 62so6481692pgg.12 for ; Thu, 19 Nov 2020 22:49:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RKbgeHukBWWau0oXAXahnUGuDDx8LJgRTsdrkFi6LMQ=; b=ilarJVpiysC2TwGERjYBOQId0CfOFqTeTxO7s8DGA84pEF0YJlXiJnIUO2LxN4OTMZ IIPz9cMkunEhpWSzBqOy/UFDV64Jxgz2bdA8RXKQMQi2tjD/5SUXakR4JCnKB1UWL9f3 9ycO2r3mHuVQ0oUiI/Nq19yr2Pwn5Zd1BTHlkA4J2zhUNADCS9oHgyOomHo2P0tmiEY/ bLMkbOQyMv5iDmEYIe7+S4TLTsxaJ7W9z1po4juPLslOYOgzXl4F4k3ufV8TUzLPcA9o 5L6RFeE/bN6W49M9knzp2Ne9Pn0QduTQxtRvzv6EmRZMEHdIkOFE7ttB6xfTg/sKq7Ru X2UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RKbgeHukBWWau0oXAXahnUGuDDx8LJgRTsdrkFi6LMQ=; b=GJbIKfp/SQuJ0MSUdhOOoIxbLccbiqht18XsdvWNE9bRaxAF60Z8Ijtmryl4v7Ggmf DAHlwTLGVQSwzMjFxN5Z0pNEuD5jjUQHLgKb4L3Wv9hw9PS9mvDa04JGhMtJIMf3/8o9 2Hru4Rh/dNIE0WD/MO2v87PJhjZIb4gcQoNlpOQZSK40It52LN7F2QZQ7DrDDyxHyCbE 4QcqJkp9kGuyKbKwGt9Iy0NhEtjs0VioQZCtY4z1aOz/V398IOQtv2onah0qbpYQW8i1 h4Adr4B+zCp7SDP+tsG5u+8zs5YPYef6TRyrJsCezduWy5IUbxTLdctBac0KoR2x+R26 gb7g== X-Gm-Message-State: AOAM531ko536w9930AoJPitiIT6tpZH9Mqi1Feu2S9YFyowENhStAC76 UTt/1WtiJWBvUy3TI9pzPCJjpw== X-Google-Smtp-Source: ABdhPJw3lX9EfMCu83bPE2RgBSOWlgHv04Op3EI6DAr4jn8pAlfR7gcrTul/y/lkJ3qjOK3OjW4pPw== X-Received: by 2002:aa7:9699:0:b029:18a:e057:c44 with SMTP id f25-20020aa796990000b029018ae0570c44mr13140619pfk.34.1605854955543; Thu, 19 Nov 2020 22:49:15 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.49.04 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:49:14 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 19/21] mm/hugetlb: Gather discrete indexes of tail page Date: Fri, 20 Nov 2020 14:43:23 +0800 Message-Id: <20201120064325.34492-20-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: For hugetlb page, there are more metadata to save in the struct page. But the head struct page cannot meet our needs, so we have to abuse other tail struct page to store the metadata. In order to avoid conflicts caused by subsequent use of more tail struct pages, we can gather these discrete indexes of tail struct page In this case, it will be easier to add a new tail page index later. Signed-off-by: Muchun Song --- include/linux/hugetlb.h | 13 +++++++++++++ include/linux/hugetlb_cgroup.h | 15 +++++++++------ mm/hugetlb.c | 12 ++++++------ mm/hugetlb_vmemmap.h | 4 ++-- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index da18fc9ed152..fa9d38a3ac6f 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -28,6 +28,19 @@ typedef struct { unsigned long pd; } hugepd_t; #include #include +enum { + SUBPAGE_INDEX_ACTIVE = 1, /* reuse page flags of PG_private */ + SUBPAGE_INDEX_TEMPORARY, /* reuse page->mapping */ +#ifdef CONFIG_CGROUP_HUGETLB + SUBPAGE_INDEX_CGROUP = SUBPAGE_INDEX_TEMPORARY,/* reuse page->private */ + SUBPAGE_INDEX_CGROUP_RSVD, /* reuse page->private */ +#endif +#ifdef CONFIG_HUGETLB_PAGE_FREE_VMEMMAP + SUBPAGE_INDEX_HWPOISON, /* reuse page->private */ +#endif + NR_USED_SUBPAGE, +}; + struct hugepage_subpool { spinlock_t lock; long count; diff --git a/include/linux/hugetlb_cgroup.h b/include/linux/hugetlb_cgroup.h index 2ad6e92f124a..3d3c1c49efe4 100644 --- a/include/linux/hugetlb_cgroup.h +++ b/include/linux/hugetlb_cgroup.h @@ -24,8 +24,9 @@ struct file_region; /* * Minimum page order trackable by hugetlb cgroup. * At least 4 pages are necessary for all the tracking information. - * The second tail page (hpage[2]) is the fault usage cgroup. - * The third tail page (hpage[3]) is the reservation usage cgroup. + * The second tail page (hpage[SUBPAGE_INDEX_CGROUP]) is the fault + * usage cgroup. The third tail page (hpage[SUBPAGE_INDEX_CGROUP_RSVD]) + * is the reservation usage cgroup. */ #define HUGETLB_CGROUP_MIN_ORDER 2 @@ -66,9 +67,9 @@ __hugetlb_cgroup_from_page(struct page *page, bool rsvd) if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER) return NULL; if (rsvd) - return (struct hugetlb_cgroup *)page[3].private; + return (void *)page_private(page + SUBPAGE_INDEX_CGROUP_RSVD); else - return (struct hugetlb_cgroup *)page[2].private; + return (void *)page_private(page + SUBPAGE_INDEX_CGROUP); } static inline struct hugetlb_cgroup *hugetlb_cgroup_from_page(struct page *page) @@ -90,9 +91,11 @@ static inline int __set_hugetlb_cgroup(struct page *page, if (compound_order(page) < HUGETLB_CGROUP_MIN_ORDER) return -1; if (rsvd) - page[3].private = (unsigned long)h_cg; + set_page_private(page + SUBPAGE_INDEX_CGROUP_RSVD, + (unsigned long)h_cg); else - page[2].private = (unsigned long)h_cg; + set_page_private(page + SUBPAGE_INDEX_CGROUP, + (unsigned long)h_cg); return 0; } diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 9aad0b63d369..dfa982f4b525 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -1429,20 +1429,20 @@ struct hstate *size_to_hstate(unsigned long size) bool page_huge_active(struct page *page) { VM_BUG_ON_PAGE(!PageHuge(page), page); - return PageHead(page) && PagePrivate(&page[1]); + return PageHead(page) && PagePrivate(&page[SUBPAGE_INDEX_ACTIVE]); } /* never called for tail page */ static void set_page_huge_active(struct page *page) { VM_BUG_ON_PAGE(!PageHeadHuge(page), page); - SetPagePrivate(&page[1]); + SetPagePrivate(&page[SUBPAGE_INDEX_ACTIVE]); } static void clear_page_huge_active(struct page *page) { VM_BUG_ON_PAGE(!PageHeadHuge(page), page); - ClearPagePrivate(&page[1]); + ClearPagePrivate(&page[SUBPAGE_INDEX_ACTIVE]); } /* @@ -1454,17 +1454,17 @@ static inline bool PageHugeTemporary(struct page *page) if (!PageHuge(page)) return false; - return (unsigned long)page[2].mapping == -1U; + return (unsigned long)page[SUBPAGE_INDEX_TEMPORARY].mapping == -1U; } static inline void SetPageHugeTemporary(struct page *page) { - page[2].mapping = (void *)-1U; + page[SUBPAGE_INDEX_TEMPORARY].mapping = (void *)-1U; } static inline void ClearPageHugeTemporary(struct page *page) { - page[2].mapping = NULL; + page[SUBPAGE_INDEX_TEMPORARY].mapping = NULL; } static void __free_huge_page(struct page *page) diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 65e94436ffff..d9c1f45e93ae 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -25,7 +25,7 @@ static inline void subpage_hwpoison_deliver(struct page *head) struct page *page = head; if (PageHWPoison(head)) - page = head + page_private(head + 4); + page = head + page_private(head + SUBPAGE_INDEX_HWPOISON); /* * Move PageHWPoison flag from head page to the raw error page, @@ -40,7 +40,7 @@ static inline void subpage_hwpoison_deliver(struct page *head) static inline void set_subpage_hwpoison(struct page *head, struct page *page) { if (PageHWPoison(head)) - set_page_private(head + 4, page - head); + set_page_private(head + SUBPAGE_INDEX_HWPOISON, page - head); } static inline unsigned int free_vmemmap_pages_per_hpage(struct hstate *h) From patchwork Fri Nov 20 06:43:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919601 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F7C6C5519F for ; Fri, 20 Nov 2020 06:49:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B14E02222F for ; Fri, 20 Nov 2020 06:49:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="IF1543fD" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B14E02222F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3E8026B0083; Fri, 20 Nov 2020 01:49:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 36A266B0085; Fri, 20 Nov 2020 01:49:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 234096B0087; Fri, 20 Nov 2020 01:49:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0223.hostedemail.com [216.40.44.223]) by kanga.kvack.org (Postfix) with ESMTP id DB82C6B0083 for ; Fri, 20 Nov 2020 01:49:27 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 7A126364B for ; Fri, 20 Nov 2020 06:49:27 +0000 (UTC) X-FDA: 77503870374.30.maid80_4b14d5c27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id 5B158180B3C83 for ; Fri, 20 Nov 2020 06:49:27 +0000 (UTC) X-HE-Tag: maid80_4b14d5c27349 X-Filterd-Recvd-Size: 4383 Received: from mail-pf1-f194.google.com (mail-pf1-f194.google.com [209.85.210.194]) by imf01.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:49:26 +0000 (UTC) Received: by mail-pf1-f194.google.com with SMTP id b63so6917595pfg.12 for ; Thu, 19 Nov 2020 22:49:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZS5213ZHM0b8+f9VjLOiz+1LLqtvqurjNhuD3Z1QgXw=; b=IF1543fDtb+kWKsmeCbYQJE2RKvL98vTCrDCvCNTBaznuoXtdRoONmgowwM077EA1r D1ImG20rzchmyhXqIqqQl53pDl81FzaCOANREsMX20Bi7my3JSv37VpMcPo1QyGhIC/W kjfxyOme9/t0QUXrphAihullLYJaXkDZySzXMhDSBQvRjmeANFmzXuAMUQd4VO5Eg0cX zqAICNAblp8DQIt5wYJuW3S84M82AYFj9qGqTx0bj8pyiBqhsIlfrFd+0NqTblPnHo68 dOWR+79BIzlGrP7e0lvS7TCmkXC4ENFNlq3FoNoiYne32Hm7nCVaWDfJw2hnCO290CeN VWHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZS5213ZHM0b8+f9VjLOiz+1LLqtvqurjNhuD3Z1QgXw=; b=XXxs5qyQfWUI5mSfk2j/D8eRY1zXzg1xj8sxmepipqPKESN02TygP597RujYA/qd40 oLcgVUl8kDFh6DPyJVlRmQG1qSaJFTdaHaYuMBAAkq/i/ugPOp+MzGKCpzF4gV1Qxu96 AbIgfS0RvDO9izc/A0dG53L+cPGw6TFIaZRhp4wqmRJncDZ+xQJ/3QNvyXUVN5v/uHet sOx76/s7yD1Ne92tnVEGcvTY/H7EVnkBEA7d0thnd0LZ95U6F7zIC+J9I1VyiKUd2NFx QJjfh5CvVcLKIoC781AXeOaIOA1+LhFtz9LM362l3cSMHHWeT8I+XU9NAvZao1ZDwGQy qFEA== X-Gm-Message-State: AOAM532X0lr/gWQJ4FirgFbA1Y/r/TZWXacBwEf5S3aXTGbz6+AcO8bp DvtwNvxXxcv97pI4Ri8qpYtTeA== X-Google-Smtp-Source: ABdhPJy/oDjOsfTURQw/n8WyCu22EFp43ZL/0t48CLQVCDRZ8hdfpHxhDoWHshVvTsOfv6IeLOJdAw== X-Received: by 2002:a17:90a:7101:: with SMTP id h1mr3714686pjk.26.1605854965992; Thu, 19 Nov 2020 22:49:25 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.49.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:49:25 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 20/21] mm/hugetlb: Add BUILD_BUG_ON to catch invalid usage of tail struct page Date: Fri, 20 Nov 2020 14:43:24 +0800 Message-Id: <20201120064325.34492-21-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: There are only `RESERVE_VMEMMAP_SIZE / sizeof(struct page)` struct pages can be used when CONFIG_HUGETLB_PAGE_FREE_VMEMMAP, so add a BUILD_BUG_ON to catch this invalid usage of tail struct page. Signed-off-by: Muchun Song --- mm/hugetlb_vmemmap.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index bf2b6b3e75af..c3b3fc041903 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -750,6 +750,9 @@ static int __init vmemmap_ptlock_init(void) { int nid; + BUILD_BUG_ON(NR_USED_SUBPAGE >= + RESERVE_VMEMMAP_SIZE / sizeof(struct page)); + if (!hugepages_supported()) return 0; From patchwork Fri Nov 20 06:43:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 11919603 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8FDEAC63798 for ; Fri, 20 Nov 2020 06:49:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 05D71223AC for ; Fri, 20 Nov 2020 06:49:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=bytedance-com.20150623.gappssmtp.com header.i=@bytedance-com.20150623.gappssmtp.com header.b="H2aA2xao" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 05D71223AC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=bytedance.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 89BFC6B005C; Fri, 20 Nov 2020 01:49:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 826416B0068; Fri, 20 Nov 2020 01:49:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C6196B0087; Fri, 20 Nov 2020 01:49:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id 2E1416B005C for ; Fri, 20 Nov 2020 01:49:39 -0500 (EST) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id BEA9E180AD81F for ; Fri, 20 Nov 2020 06:49:38 +0000 (UTC) X-FDA: 77503870836.02.quill55_60140be27349 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin02.hostedemail.com (Postfix) with ESMTP id 9B2D410097AA0 for ; Fri, 20 Nov 2020 06:49:38 +0000 (UTC) X-HE-Tag: quill55_60140be27349 X-Filterd-Recvd-Size: 4436 Received: from mail-pf1-f195.google.com (mail-pf1-f195.google.com [209.85.210.195]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Fri, 20 Nov 2020 06:49:38 +0000 (UTC) Received: by mail-pf1-f195.google.com with SMTP id y7so6915914pfq.11 for ; Thu, 19 Nov 2020 22:49:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=SdvvqKnmPQDzzEKZQavt4aA9vX77aS/RCbk9rPPVeB4=; b=H2aA2xaoiWnwW6nf39Kv1EUsqrKNerzQbrslq/NI7IVdIRJqgVKSNb1QLSqFeexpQM s2W0bOQA2Z64MXFCeBPxIhsqOPs/fle/YtmNMi/7ymQt8cIMHdL2FqrPekNTKBVqwmXJ XFDSPuJ1TaON1L0Hee6XRFlcq2QRhwzojGlwxWgbZiM4xfDN1mM0UzgJia6txEDmnfl6 ZUBSo75B0Qdjne4F2h4rdu0tuC79BS6n+m8abxUxiRMbbYPpGvcpqJKU6sE5stAVk5wM wt29LVkQUPffCEZn/FBagi1xtxYlpnhHFdv4TlqFdBDgiAJFAmz4i2qEBSI/2RAq/K6V ezpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=SdvvqKnmPQDzzEKZQavt4aA9vX77aS/RCbk9rPPVeB4=; b=btMiDLmMki+Y0IdM5WuDzjNnVcC/9bqWrBHVg0uEInb8RBE46vzPL9GfweM1zDt0La ilv6OZ6Ijxjhr1et+PXt929PwgCQeQlZ1klFAUq0800Ri5/IFjOOTo7epDO2rp77nEJU P2OGo9KsrP0ihbmd6VidLk1DAGzC70HCqkKUBwE0IvshtRXAE9wRYXCrNsK6VLgCaluI wcwIWUwu4vo+gQMZRjIFd+GmMvt+9tWKR0HtlR4jYWpnA+EsloYGZCvKp74xjXjeMNNd JULDbZyytPEgpHeMw5RL+wXYkk17/ldl51wUp6rcXGEw322LjyzaYRpSr8GypkmMRUsk GBbw== X-Gm-Message-State: AOAM532Nd6yL4305Y+tyWEZE0JHqAQXMHuoRiI++iu1AtoU6vhihGUB4 IWc32/IoUB3GegkjpwTHHIxelQ== X-Google-Smtp-Source: ABdhPJzHqJGyY5pbu9G4vQoTS2YSYmL0vWhnJb+/ViUAprblRJ1z84SdXKgyNJ2fDva3na3APIqFKg== X-Received: by 2002:a63:1445:: with SMTP id 5mr15580297pgu.357.1605854977294; Thu, 19 Nov 2020 22:49:37 -0800 (PST) Received: from localhost.localdomain ([103.136.221.72]) by smtp.gmail.com with ESMTPSA id 23sm2220278pfx.210.2020.11.19.22.49.26 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 19 Nov 2020 22:49:36 -0800 (PST) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, viro@zeniv.linux.org.uk, akpm@linux-foundation.org, paulmck@kernel.org, mchehab+huawei@kernel.org, pawan.kumar.gupta@linux.intel.com, rdunlap@infradead.org, oneukum@suse.com, anshuman.khandual@arm.com, jroedel@suse.de, almasrymina@google.com, rientjes@google.com, willy@infradead.org, osalvador@suse.de, mhocko@suse.com, song.bao.hua@hisilicon.com Cc: duanxiongchun@bytedance.com, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, Muchun Song Subject: [PATCH v5 21/21] mm/hugetlb: Disable freeing vmemmap if struct page size is not power of two Date: Fri, 20 Nov 2020 14:43:25 +0800 Message-Id: <20201120064325.34492-22-songmuchun@bytedance.com> X-Mailer: git-send-email 2.21.0 (Apple Git-122) In-Reply-To: <20201120064325.34492-1-songmuchun@bytedance.com> References: <20201120064325.34492-1-songmuchun@bytedance.com> MIME-Version: 1.0 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: We only can free the unused vmemmap to the buddy system when the size of struct page is a power of two. Signed-off-by: Muchun Song --- mm/hugetlb_vmemmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index c3b3fc041903..7bb749a3eea2 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -671,7 +671,8 @@ void __init hugetlb_vmemmap_init(struct hstate *h) unsigned int order = huge_page_order(h); unsigned int vmemmap_pages; - if (hugetlb_free_vmemmap_disabled) { + if (hugetlb_free_vmemmap_disabled || + !is_power_of_2(sizeof(struct page))) { pr_info("disable free vmemmap pages for %s\n", h->name); return; }