From patchwork Wed Apr 13 14:47:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12812096 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BF48C4332F for ; Wed, 13 Apr 2022 14:48:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B9D06B0074; Wed, 13 Apr 2022 10:48:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 969186B0075; Wed, 13 Apr 2022 10:48:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E18F6B0078; Wed, 13 Apr 2022 10:48:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.27]) by kanga.kvack.org (Postfix) with ESMTP id 6E39B6B0074 for ; Wed, 13 Apr 2022 10:48:36 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 308BD256F2 for ; Wed, 13 Apr 2022 14:48:36 +0000 (UTC) X-FDA: 79352137032.03.8081C85 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf06.hostedemail.com (Postfix) with ESMTP id 898A518000B for ; Wed, 13 Apr 2022 14:48:35 +0000 (UTC) Received: by mail-pj1-f44.google.com with SMTP id md4so2244808pjb.4 for ; Wed, 13 Apr 2022 07:48:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=wzudrV45q6zi3Aa2lP4e9vSlvpqaHzUTvfK2vJ9QrmY=; b=fky8LNA9FUVJ/dBlEU0cJVH6m3R1P8rsWI+MS2++Z9uWhKn+oIQyjRsCMp61O9Z3S3 RbXndwfzCIM0T27I3GA8fL20AZkHkHcyCT9NqNJC4aXG7PuG3CAGIfgqm6vdm6z7tFCM Xa4cpzcZ2PdChT3I1ao7B0WLGkKOCyPe0keKB0774uElyHAxPCWB2yQENchFF9P1yvP2 ZEZAGFlKhEZX3mU3hCEIYp+cUYuY60AlIosuJcChM8QmDEzMeX4LsiYS8VZwLXSuM260 gpOAo1WCAlZXeQRdYQvrXcHc1IShBPwR0PHte641H5ggBQ1S0MFi89xKOubw0oQaXFw7 G7dw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=wzudrV45q6zi3Aa2lP4e9vSlvpqaHzUTvfK2vJ9QrmY=; b=VRfgHez+M2ywVqNucYwPTSG/1WcZOIUWwHk0iEQVLAelhLX0zYKNoeZsBbwxHBuwmd SjqCTNHFcjYzsBM4uJaLpwe3Kq73mTikisb/qHr2v7b71Bau6YjS66Ft5WzEloNf2WF/ r3m/C8habAy+O8pvNPjQklAqvM9WkuMatOwrQwjahhbULpGFwyOiHy8Y+VQExLTGkk2P SbpKbssPST5v5mq5PJ4u+v0vEHWT1yf8hG38Jjw/EHsBotzlDMDScYd1f2enJbj6OplW gaihAh4abRjBQy9ZW1ZBnvgX7CWfg+Z1HcaGLHuE+lP2D/ibEdxxqSq2Opaxfi+Zjkc8 NPmQ== X-Gm-Message-State: AOAM533qRbSuXCkDy6DZmk9sw3JzdYseRtjqJ4lV4vPTgb8PdR9pVb2m TrtSzhwB+qUQiiU5Kt8FREQLDA== X-Google-Smtp-Source: ABdhPJwzwm1n18p78CamL33qrvfFAMUpojtWkwa+L0pqwaKdhS70fN63EPpwCvouUW4+2Nlv0u9egg== X-Received: by 2002:a17:90a:8591:b0:1b9:da10:2127 with SMTP id m17-20020a17090a859100b001b9da102127mr11290399pjn.13.1649861314495; Wed, 13 Apr 2022 07:48:34 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.245]) by smtp.gmail.com with ESMTPSA id p27-20020a056a000a1b00b004f3f63e3cf2sm47817257pfh.58.2022.04.13.07.48.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 07:48:34 -0700 (PDT) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, akpm@linux-foundation.org, mcgrof@kernel.org, keescook@chromium.org, yzaikin@google.com, osalvador@suse.de, david@redhat.com, masahiroy@kernel.org Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v8 1/4] mm: hugetlb_vmemmap: introduce CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP Date: Wed, 13 Apr 2022 22:47:45 +0800 Message-Id: <20220413144748.84106-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220413144748.84106-1-songmuchun@bytedance.com> References: <20220413144748.84106-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 898A518000B X-Rspam-User: Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fky8LNA9; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf06.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.44 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-Stat-Signature: pzyasx3znr7bnar6g1cro17o3j47xpei X-HE-Tag: 1649861315-487274 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: If the size of "struct page" is not the power of two but with the feature of minimizing overhead of struct page associated with each HugeTLB is enabled, then the vmemmap pages of HugeTLB will be corrupted after remapping (panic is about to happen in theory). But this only exists when !CONFIG_MEMCG && !CONFIG_SLUB on x86_64. However, it is not a conventional configuration nowadays. So it is not a real word issue, just the result of a code review. But we have to prevent anyone from configuring that combined configurations. In order to avoid many checks like "is_power_of_2 (sizeof(struct page))" through mm/hugetlb_vmemmap.c. Introduce a new macro CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP to represent the size of struct page is power of two and CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP is configured. Then make the codes of this feature depends on this new macro. Then we could prevent anyone do any unexpected configurations. A new autoconf_ext.h is introduced as well, which serves as an extension for autoconf.h since those special configurations (e.g. CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP here) are rely on the autoconf.h (generated from Kconfig), so we cannot embed those configurations into Kconfig. After this change, it would be easy if someone want to do the similar thing (add a new CONFIG) in the future. Signed-off-by: Muchun Song Suggested-by: Luis Chamberlain --- Kbuild | 19 +++++++++++++++++++ arch/x86/mm/init_64.c | 2 +- include/linux/hugetlb.h | 2 +- include/linux/kconfig.h | 4 ++++ include/linux/mm.h | 2 +- include/linux/page-flags.h | 2 +- kernel/autoconf_ext.c | 26 ++++++++++++++++++++++++++ mm/hugetlb_vmemmap.c | 8 ++------ mm/hugetlb_vmemmap.h | 4 ++-- mm/sparse-vmemmap.c | 4 ++-- scripts/mod/Makefile | 2 ++ 11 files changed, 61 insertions(+), 14 deletions(-) create mode 100644 kernel/autoconf_ext.c diff --git a/Kbuild b/Kbuild index fa441b98c9f6..83c0d5a418d1 100644 --- a/Kbuild +++ b/Kbuild @@ -2,6 +2,12 @@ # # Kbuild for top-level directory of the kernel +# autoconf_ext.h is generated last since it depends on other generated headers, +# however those other generated headers may include autoconf_ext.h. Use the +# following macro to avoid circular dependency. + +KBUILD_CFLAGS_KERNEL += -D__EXCLUDE_AUTOCONF_EXT_H + ##### # Generate bounds.h @@ -37,6 +43,19 @@ $(offsets-file): arch/$(SRCARCH)/kernel/asm-offsets.s FORCE $(call filechk,offsets,__ASM_OFFSETS_H__) ##### +# Generate autoconf_ext.h. + +autoconf_ext-file := include/generated/autoconf_ext.h + +always-y += $(autoconf_ext-file) +targets += kernel/autoconf_ext.s + +kernel/autoconf_ext.s: $(bounds-file) $(timeconst-file) $(offsets-file) + +$(autoconf_ext-file): kernel/autoconf_ext.s FORCE + $(call filechk,offsets,__LINUX_AUTOCONF_EXT_H__) + +##### # Check for missing system calls always-y += missing-syscalls diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 4b9e0012bbbf..9b8dfa6e4da8 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1268,7 +1268,7 @@ static struct kcore_list kcore_vsyscall; static void __init register_page_bootmem_info(void) { -#if defined(CONFIG_NUMA) || defined(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) +#if defined(CONFIG_NUMA) || defined(CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP) int i; for_each_online_node(i) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index ac2ece9e9c79..d42de8abd2b6 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -623,7 +623,7 @@ 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_OPTIMIZE_VMEMMAP +#ifdef CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP unsigned int optimize_vmemmap_pages; #endif #ifdef CONFIG_CGROUP_HUGETLB diff --git a/include/linux/kconfig.h b/include/linux/kconfig.h index 20d1079e92b4..00796794f177 100644 --- a/include/linux/kconfig.h +++ b/include/linux/kconfig.h @@ -4,6 +4,10 @@ #include +#if defined(__KERNEL__) && !defined(__EXCLUDE_AUTOCONF_EXT_H) +#include +#endif + #ifdef CONFIG_CPU_BIG_ENDIAN #define __BIG_ENDIAN 4321 #else diff --git a/include/linux/mm.h b/include/linux/mm.h index e0ad13486035..4c36f77a5745 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3186,7 +3186,7 @@ static inline void print_vma_addr(char *prefix, unsigned long rip) } #endif -#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +#ifdef CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP int vmemmap_remap_free(unsigned long start, unsigned long end, unsigned long reuse); int vmemmap_remap_alloc(unsigned long start, unsigned long end, diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index b70124b9c7c1..e409b10cd677 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -199,7 +199,7 @@ enum pageflags { #ifndef __GENERATING_BOUNDS_H -#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +#ifdef CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP DECLARE_STATIC_KEY_MAYBE(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON, hugetlb_optimize_vmemmap_key); diff --git a/kernel/autoconf_ext.c b/kernel/autoconf_ext.c new file mode 100644 index 000000000000..8475735c6fc9 --- /dev/null +++ b/kernel/autoconf_ext.c @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Generate definitions needed by the preprocessor. + * This code generates raw asm output which is post-processed + * to extract and format the required data. + */ +#include +#include +#include + +int main(void) +{ + if (IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) && + is_power_of_2(sizeof(struct page))) { + /* + * The 2nd parameter of DEFINE() will go into the comments. Do + * not pass 1 directly to it to make the generated macro more + * clear for the readers. + */ + DEFINE(CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP, + IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP) && + is_power_of_2(sizeof(struct page))); + } + + return 0; +} diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 2655434a946b..be73782cc1cf 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -178,6 +178,7 @@ #include "hugetlb_vmemmap.h" +#ifdef CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP /* * There are a lot of struct page structures associated with each HugeTLB page. * For tail pages, the value of compound_head is the same. So we can reuse first @@ -194,12 +195,6 @@ EXPORT_SYMBOL(hugetlb_optimize_vmemmap_key); static int __init hugetlb_vmemmap_early_param(char *buf) { - /* We cannot optimize if a "struct page" crosses page boundaries. */ - if (!is_power_of_2(sizeof(struct page))) { - pr_warn("cannot free vmemmap pages because \"struct page\" crosses page boundaries\n"); - return 0; - } - if (!buf) return -EINVAL; @@ -300,3 +295,4 @@ void __init hugetlb_vmemmap_init(struct hstate *h) pr_info("can optimize %d vmemmap pages for %s\n", h->optimize_vmemmap_pages, h->name); } +#endif /* CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP */ diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 109b0a53b6fe..3afae3ff37fa 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -10,7 +10,7 @@ #define _LINUX_HUGETLB_VMEMMAP_H #include -#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +#ifdef CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP int hugetlb_vmemmap_alloc(struct hstate *h, struct page *head); void hugetlb_vmemmap_free(struct hstate *h, struct page *head); void hugetlb_vmemmap_init(struct hstate *h); @@ -41,5 +41,5 @@ static inline unsigned int hugetlb_optimize_vmemmap_pages(struct hstate *h) { return 0; } -#endif /* CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */ +#endif /* CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP */ #endif /* _LINUX_HUGETLB_VMEMMAP_H */ diff --git a/mm/sparse-vmemmap.c b/mm/sparse-vmemmap.c index 52f36527bab3..6c7f1a9ce2dd 100644 --- a/mm/sparse-vmemmap.c +++ b/mm/sparse-vmemmap.c @@ -34,7 +34,7 @@ #include #include -#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP +#ifdef CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP /** * struct vmemmap_remap_walk - walk vmemmap page table * @@ -420,7 +420,7 @@ int vmemmap_remap_alloc(unsigned long start, unsigned long end, return 0; } -#endif /* CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP */ +#endif /* CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP */ /* * Allocate a block of memory to be used to back the virtual memory map diff --git a/scripts/mod/Makefile b/scripts/mod/Makefile index c9e38ad937fd..f82ab128c086 100644 --- a/scripts/mod/Makefile +++ b/scripts/mod/Makefile @@ -1,6 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 OBJECT_FILES_NON_STANDARD := y CFLAGS_REMOVE_empty.o += $(CC_FLAGS_LTO) +# See comments in Kbuild +KBUILD_CFLAGS_KERNEL += -D__EXCLUDE_AUTOCONF_EXT_H hostprogs-always-y += modpost mk_elfconfig always-y += empty.o From patchwork Wed Apr 13 14:47:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12812097 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8A94C433FE for ; Wed, 13 Apr 2022 14:48:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4B26A6B0075; Wed, 13 Apr 2022 10:48:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4600D6B0078; Wed, 13 Apr 2022 10:48:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 300B46B007B; Wed, 13 Apr 2022 10:48:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id 216096B0075 for ; Wed, 13 Apr 2022 10:48:42 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id D2B34803FE for ; Wed, 13 Apr 2022 14:48:41 +0000 (UTC) X-FDA: 79352137242.05.6918B3A Received: from mail-pl1-f169.google.com (mail-pl1-f169.google.com [209.85.214.169]) by imf07.hostedemail.com (Postfix) with ESMTP id 483B940004 for ; Wed, 13 Apr 2022 14:48:41 +0000 (UTC) Received: by mail-pl1-f169.google.com with SMTP id s14so2158695plk.8 for ; Wed, 13 Apr 2022 07:48:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=0UA2E7Swg+iesG+ux9AwzGNddrfmiNsjUbQGq/dsMiM=; b=qy9bm5h22bSwGRfwhbePUNdpcjM3mbKq7ygfInxMwnDotiXu2yFUlYBXkhCnlMwPOW Lo2SdpeT//ZkMmHnrxy9LBo5B95Tlnt+22gl8vhDUY9E6Z7VmlJqtv4TQCKfIppDEkuA n3OZ1/n5jMzs4/wXvQkg2zjrHNln5L/W2mIZPXdGC9gnqfOEcJjfpGUtXMrOAstS2drY XD2xConyvFGywLEX099TYe/QaqPF5qS8isNoPOJt+c7hjXq6KTZNriLARnsksUWFqSoe 6+Zxaa/iEbJMLdAX3guDL3tWIpzNGg33TqEGTq0Fa3U0TzSo4hXMQmyOW3sbhipZXbtm A2gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0UA2E7Swg+iesG+ux9AwzGNddrfmiNsjUbQGq/dsMiM=; b=JovInuIjBXJX1IKmZW1s4wC1O9sfe704FjJo0rgxOf0/huXlEN/MxSXq4hRUe42W7k uVdTGURb+JJdYdM4U81oGYgbKHRA5VMAu99lBIt9qcR+9W3uIMftETPZhLYKMXfIKMSX FLVGVGZTWR2frD2XWyPB2T8hIOCM+GCFom0/GNz5JLkVZWBD7ucEGaIqiNcNwvsZ2Zel c9FBSvpie50wrwTtWnEZ5NhPD+O6RTICa5PgNHNP6nVbgQsBSGPc/GEoE+PUjB0oVv5z bOAb/XLyK/LC4G13vTpRJ8ENj/StHWXr/rfZtqM6W0Tkdl3RwCGVZ5YwDinFngbfWLGA sUyQ== X-Gm-Message-State: AOAM531O+/4vn+u4tofZ3KJnNCMeQBvSAXLW0m7Db7JHe3VZeiMzGSPw Yd5+NV2eRuxAMk8vRFgkXM+nYg== X-Google-Smtp-Source: ABdhPJyX5qDb668ls6tZ6opY0Ql76EWScJCRk2AzqYp8TKQVr1l9ARVh218HOC97y2V1F7Wd/U1wCw== X-Received: by 2002:a17:90b:3e82:b0:1c7:2920:7c54 with SMTP id rj2-20020a17090b3e8200b001c729207c54mr11381401pjb.2.1649861320371; Wed, 13 Apr 2022 07:48:40 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.245]) by smtp.gmail.com with ESMTPSA id p27-20020a056a000a1b00b004f3f63e3cf2sm47817257pfh.58.2022.04.13.07.48.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 07:48:40 -0700 (PDT) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, akpm@linux-foundation.org, mcgrof@kernel.org, keescook@chromium.org, yzaikin@google.com, osalvador@suse.de, david@redhat.com, masahiroy@kernel.org Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v8 2/4] mm: memory_hotplug: override memmap_on_memory when hugetlb_free_vmemmap=on Date: Wed, 13 Apr 2022 22:47:46 +0800 Message-Id: <20220413144748.84106-3-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220413144748.84106-1-songmuchun@bytedance.com> References: <20220413144748.84106-1-songmuchun@bytedance.com> MIME-Version: 1.0 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=qy9bm5h2; spf=pass (imf07.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.169 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 483B940004 X-Stat-Signature: r9txgxfdyskp4eb6x7had131uo94skr7 X-HE-Tag: 1649861321-998130 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 "hugetlb_free_vmemmap=on" and "memory_hotplug.memmap_on_memory" are both passed to boot cmdline, the variable of "memmap_on_memory" will be set to 1 even if the vmemmap pages will not be allocated from the hotadded memory since the former takes precedence over the latter. In the next patch, we want to enable or disable the feature of freeing vmemmap pages of HugeTLB via sysctl. We need a way to know if the feature of memory_hotplug.memmap_on_memory is enabled when enabling the feature of freeing vmemmap pages since those two features are not compatible, however, the variable of "memmap_on_memory" cannot indicate this nowadays. Do not set "memmap_on_memory" to 1 when both parameters are passed to cmdline, in this case, "memmap_on_memory" could indicate if this feature is enabled by the users. Also introduce mhp_memmap_on_memory() helper to move the definition of "memmap_on_memory" to the scope of CONFIG_MHP_MEMMAP_ON_MEMORY. In the next patch, mhp_memmap_on_memory() will also be exported to be used in hugetlb_vmemmap.c. Signed-off-by: Muchun Song --- mm/memory_hotplug.c | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 74430f88853d..f6eab03397d3 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -42,14 +42,36 @@ #include "internal.h" #include "shuffle.h" +#ifdef CONFIG_MHP_MEMMAP_ON_MEMORY +static int memmap_on_memory_set(const char *val, const struct kernel_param *kp) +{ + if (hugetlb_optimize_vmemmap_enabled()) + return 0; + return param_set_bool(val, kp); +} + +static const struct kernel_param_ops memmap_on_memory_ops = { + .flags = KERNEL_PARAM_OPS_FL_NOARG, + .set = memmap_on_memory_set, + .get = param_get_bool, +}; /* * memory_hotplug.memmap_on_memory parameter */ static bool memmap_on_memory __ro_after_init; -#ifdef CONFIG_MHP_MEMMAP_ON_MEMORY -module_param(memmap_on_memory, bool, 0444); +module_param_cb(memmap_on_memory, &memmap_on_memory_ops, &memmap_on_memory, 0444); MODULE_PARM_DESC(memmap_on_memory, "Enable memmap on memory for memory hotplug"); + +static inline bool mhp_memmap_on_memory(void) +{ + return memmap_on_memory; +} +#else +static inline bool mhp_memmap_on_memory(void) +{ + return false; +} #endif enum { @@ -1272,9 +1294,7 @@ bool mhp_supports_memmap_on_memory(unsigned long size) * altmap as an alternative source of memory, and we do not exactly * populate a single PMD. */ - return memmap_on_memory && - !hugetlb_optimize_vmemmap_enabled() && - IS_ENABLED(CONFIG_MHP_MEMMAP_ON_MEMORY) && + return mhp_memmap_on_memory() && size == memory_block_size_bytes() && IS_ALIGNED(vmemmap_size, PMD_SIZE) && IS_ALIGNED(remaining_size, (pageblock_nr_pages << PAGE_SHIFT)); @@ -2081,7 +2101,7 @@ static int __ref try_remove_memory(u64 start, u64 size) * We only support removing memory added with MHP_MEMMAP_ON_MEMORY in * the same granularity it was added - a single memory block. */ - if (memmap_on_memory) { + if (mhp_memmap_on_memory()) { nr_vmemmap_pages = walk_memory_blocks(start, size, NULL, get_nr_vmemmap_pages_cb); if (nr_vmemmap_pages) { From patchwork Wed Apr 13 14:47:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12812098 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63A1DC433F5 for ; Wed, 13 Apr 2022 14:48:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E749D6B0078; Wed, 13 Apr 2022 10:48:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E22676B007B; Wed, 13 Apr 2022 10:48:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9CF06B007D; Wed, 13 Apr 2022 10:48:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id BC0D56B0078 for ; Wed, 13 Apr 2022 10:48:47 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 8D2EA21A8A for ; Wed, 13 Apr 2022 14:48:47 +0000 (UTC) X-FDA: 79352137494.17.94E2AAD Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) by imf31.hostedemail.com (Postfix) with ESMTP id 22FA220006 for ; Wed, 13 Apr 2022 14:48:46 +0000 (UTC) Received: by mail-pl1-f180.google.com with SMTP id c12so2167974plr.6 for ; Wed, 13 Apr 2022 07:48:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=EfqzshiMxPgqo91bqRrogKECfyK/4BP/Cl7KQelj6Dg=; b=yKhiAYWaSOYJQwBNVwtujizWquYP4rI6ZlWZ1+KiSkaey8GNgzlk7/TSAQbLtl65q4 k0IUUazxHJMIdN3CJ/ARThnVKC1EdJUA8kR16DeUTkejbuH5jXQ10dSsciIzfDeeLad6 imdw0nic9VVQdddcq7k1pvPAP8ytBSdWAePW1rmKZUqnsDOzJEo4cz1i1L7ChnejwQAK 4fTQuEz4UgoboV1lUUtJkndrq7ssp/8DvFy/umR9lzeiffegqFV/wyLDDpcX0x9Ev5kD xoxy59LZX+TOoDoT4sQz1CK2pXdhVzNMZcuxupQZWhrkMX8aZOvAbGXxQGg4AxXyExKu oMUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=EfqzshiMxPgqo91bqRrogKECfyK/4BP/Cl7KQelj6Dg=; b=3h938HKbD/5OKdNylDNxEq69FNeqH7//D5OyapD/TtMPA+ELIKXCLSXXnXx5RpQOfT dYnU/d3PLsgZBrCosUS4xoKYEeSD7ykWRjWXk7UU0+TesQtpZzQXiGF15am1fie3FuwK 2QiSDs6ApKUAbD7st+eRIWA3GEZGNVqf257VHx+yz8b15q8DoBzJHLtZldQQmcbd8xeV hwu58kTb+zf9UG1tpGshKFkOEOx8EXWFXSyuGBGo+f5n1fc5iqxhLS8E275R/UqPiTGK NP5CuEVFINi6UYr+3kQm6OlXIth97GEO5yonzaJSHZSOP6RIdRVewxDVuIFQCY22pwpt vVTw== X-Gm-Message-State: AOAM530aQpEOsF6bJjcXaDtFNhybFvUr/kKQIZqUTJfHPusTGjZ6akus DlLQ5FRRHCnShMpulRD6Txr/IA== X-Google-Smtp-Source: ABdhPJw9+0J/Pv1eSXmSk5scRsk+PgJb9qJVP7Gb0TeMWiDl/M3OE9EtQ9tF40Ia+6ctpIzKdiHd1Q== X-Received: by 2002:a17:90b:3b8c:b0:1c6:ed78:67e2 with SMTP id pc12-20020a17090b3b8c00b001c6ed7867e2mr11221387pjb.163.1649861326236; Wed, 13 Apr 2022 07:48:46 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.245]) by smtp.gmail.com with ESMTPSA id p27-20020a056a000a1b00b004f3f63e3cf2sm47817257pfh.58.2022.04.13.07.48.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 07:48:45 -0700 (PDT) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, akpm@linux-foundation.org, mcgrof@kernel.org, keescook@chromium.org, yzaikin@google.com, osalvador@suse.de, david@redhat.com, masahiroy@kernel.org Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v8 3/4] mm: hugetlb_vmemmap: use kstrtobool for hugetlb_vmemmap param parsing Date: Wed, 13 Apr 2022 22:47:47 +0800 Message-Id: <20220413144748.84106-4-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220413144748.84106-1-songmuchun@bytedance.com> References: <20220413144748.84106-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 418bde63egc97immwcezue6ndkaowehd Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=yKhiAYWa; spf=pass (imf31.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.214.180 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com; dmarc=pass (policy=none) header.from=bytedance.com X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 22FA220006 X-HE-Tag: 1649861326-589251 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: Use kstrtobool rather than open coding "on" and "off" parsing in mm/hugetlb_vmemmap.c, which is more powerful to handle all kinds of parameters like 'Yy1Nn0' or [oO][NnFf] for "on" and "off". Signed-off-by: Muchun Song --- Documentation/admin-guide/kernel-parameters.txt | 6 +++--- mm/hugetlb_vmemmap.c | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index f3cf9f21f6eb..6ea428023d51 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1669,10 +1669,10 @@ enabled. Allows heavy hugetlb users to free up some more memory (7 * PAGE_SIZE for each 2MB hugetlb page). - Format: { on | off (default) } + Format: { [oO][Nn]/Y/y/1 | [oO][Ff]/N/n/0 (default) } - on: enable the feature - off: disable the feature + [oO][Nn]/Y/y/1: enable the feature + [oO][Ff]/N/n/0: disable the feature Built with CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON=y, the default is on. diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index be73782cc1cf..4b6a5cf16f11 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -195,15 +195,15 @@ EXPORT_SYMBOL(hugetlb_optimize_vmemmap_key); static int __init hugetlb_vmemmap_early_param(char *buf) { - if (!buf) + bool enable; + + if (kstrtobool(buf, &enable)) return -EINVAL; - if (!strcmp(buf, "on")) + if (enable) static_branch_enable(&hugetlb_optimize_vmemmap_key); - else if (!strcmp(buf, "off")) - static_branch_disable(&hugetlb_optimize_vmemmap_key); else - return -EINVAL; + static_branch_disable(&hugetlb_optimize_vmemmap_key); return 0; } From patchwork Wed Apr 13 14:47:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12812099 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id DA9C7C433EF for ; Wed, 13 Apr 2022 14:48:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6AD676B007B; Wed, 13 Apr 2022 10:48:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 65ADF6B007D; Wed, 13 Apr 2022 10:48:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4D56F6B007E; Wed, 13 Apr 2022 10:48:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0175.hostedemail.com [216.40.44.175]) by kanga.kvack.org (Postfix) with ESMTP id 3C0F46B007B for ; Wed, 13 Apr 2022 10:48:53 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id DDF228249980 for ; Wed, 13 Apr 2022 14:48:52 +0000 (UTC) X-FDA: 79352137704.24.880B069 Received: from mail-pj1-f43.google.com (mail-pj1-f43.google.com [209.85.216.43]) by imf18.hostedemail.com (Postfix) with ESMTP id 7A3EA1C0006 for ; Wed, 13 Apr 2022 14:48:52 +0000 (UTC) Received: by mail-pj1-f43.google.com with SMTP id o5so2287423pjr.0 for ; Wed, 13 Apr 2022 07:48:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=tOkU/RARB4HzUE+K871kdn+JSt0MwPZIDE1q0TKS/DQ=; b=mKPqOFmxEH9CoM02doouEtiGFnJrlZwt9FmlhOLSXsZuL4T2BxOinNhqDb43NzewNV 3uK80ApdASLEhxhXGZ7dInOL9YWfw+Jfkk6+xxvmNtAmXVeeVtywymnXZJWIS4x7/p7J wuO2Unw4zZke3jMY4dkPSLquEv7Gj5XmEEBNimZdd3+TNt5qG/HSLJ6msuC4ZYVNOEcA qWSOQEQKzno9//5ZQ8/+RSS+ZWbtNswgw0plsdpIu22AC238rL/X/PtIoiER3maocqH6 vnhFjzb3tygXITelZH+pLm884cfpAqER+sOao5Cakmsvoe8jJ2GXNqlBjMB+bPy1Plxi 01Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=tOkU/RARB4HzUE+K871kdn+JSt0MwPZIDE1q0TKS/DQ=; b=MpHMMuFIxqT5VVE954PvxmoZ6x4QjGhq4UqMhWxIDjoUIQ/b/QREZ0YBbO9QQBJSga ZBDwLLelSTeFpRFZQqILi9aNRnwcs/mOcCCcxzIS5tSStN/ald/fFq6U9lb3HX4bmOWy QgRsexjRe0L57MgB/AAc5ZAA6HlEcM/V/rJWEZKCYTxHPhtkVrjKUlJxjcpOH73/kJ3x rL6fjgZ4PUzU6KgJaZvXBNjUkZglgVRwdye6KQEhUoR0Nj7aYmXNmcBLYEMeJyfE4FPG LkxCpuwdZSmZnKbGBphBOzii28IP+CIab6F7yx0BBnVksBI3OmTQKYqAzvkd7dzyImQ8 6XaA== X-Gm-Message-State: AOAM532lu+sIgy4otq3QS+djvsmH5gJ1i7dbtOBQThdse+6TFbCsGwge G8hijibo2w5HStEtoc0J6usYiQ== X-Google-Smtp-Source: ABdhPJwzoezVyzZJ8M0AKsSv86EOAHQksT4ccTnz3V2lO6KsN0CQpIl4iHn+pi3KWU7ZLbsvkQYNZw== X-Received: by 2002:a17:902:c410:b0:158:8a80:5087 with SMTP id k16-20020a170902c41000b001588a805087mr8196520plk.28.1649861331462; Wed, 13 Apr 2022 07:48:51 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.245]) by smtp.gmail.com with ESMTPSA id p27-20020a056a000a1b00b004f3f63e3cf2sm47817257pfh.58.2022.04.13.07.48.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Apr 2022 07:48:51 -0700 (PDT) From: Muchun Song To: corbet@lwn.net, mike.kravetz@oracle.com, akpm@linux-foundation.org, mcgrof@kernel.org, keescook@chromium.org, yzaikin@google.com, osalvador@suse.de, david@redhat.com, masahiroy@kernel.org Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, duanxiongchun@bytedance.com, smuchun@gmail.com, Muchun Song Subject: [PATCH v8 4/4] mm: hugetlb_vmemmap: add hugetlb_optimize_vmemmap sysctl Date: Wed, 13 Apr 2022 22:47:48 +0800 Message-Id: <20220413144748.84106-5-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220413144748.84106-1-songmuchun@bytedance.com> References: <20220413144748.84106-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Stat-Signature: wc1uhjpx744fnc3um8c557i4swgp4398 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 7A3EA1C0006 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=mKPqOFmx; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf18.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.216.43 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-Rspam-User: X-HE-Tag: 1649861332-33798 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 must add hugetlb_free_vmemmap=on (or "off") to the boot cmdline and reboot the server to enable or disable the feature of optimizing vmemmap pages associated with HugeTLB pages. However, rebooting usually takes a long time. So add a sysctl to enable or disable the feature at runtime without rebooting. Once enabled, the vmemmap pages of subsequent allocation of HugeTLB pages from buddy allocator will be optimized (7 pages per 2MB HugeTLB page and 4095 pages per 1GB HugeTLB page), whereas already allocated HugeTLB pages will not be optimized. When those optimized HugeTLB pages are freed from the HugeTLB pool to the buddy allocator, the vmemmap pages representing that range needs to be remapped again and the vmemmap pages discarded earlier need to be rellocated again. If your use case is that HugeTLB pages are allocated 'on the fly' instead of being pulled from the HugeTLB pool, you should weigh the benefits of memory savings against the more overhead of allocation or freeing HugeTLB pages between the HugeTLB pool and the buddy allocator. Another behavior to note is that if the system is under heavy memory pressure, it could prevent the user from freeing HugeTLB pages from the HugeTLB pool to the buddy allocator since the allocation of vmemmap pages could be failed, you have to retry later if your system encounter this situation. Once disabled, the vmemmap pages of subsequent allocation of HugeTLB pages from buddy allocator will not be optimized, whereas already optimized HugeTLB pages will not be affected. If you want to make sure there is no optimized HugeTLB pages, you can set "nr_hugepages" to 0 first and then disable this. Signed-off-by: Muchun Song --- Documentation/admin-guide/sysctl/vm.rst | 27 ++++++++++ include/linux/memory_hotplug.h | 9 ++++ mm/hugetlb_vmemmap.c | 90 +++++++++++++++++++++++++++++---- mm/hugetlb_vmemmap.h | 4 +- mm/memory_hotplug.c | 7 +-- 5 files changed, 121 insertions(+), 16 deletions(-) diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index 747e325ebcd0..5c804ada85c5 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -562,6 +562,33 @@ Change the minimum size of the hugepage pool. See Documentation/admin-guide/mm/hugetlbpage.rst +hugetlb_optimize_vmemmap +======================== + +Enable (set to 1) or disable (set to 0) the feature of optimizing vmemmap pages +associated with each HugeTLB page. + +Once enabled, the vmemmap pages of subsequent allocation of HugeTLB pages from +buddy allocator will be optimized (7 pages per 2MB HugeTLB page and 4095 pages +per 1GB HugeTLB page), whereas already allocated HugeTLB pages will not be +optimized. When those optimized HugeTLB pages are freed from the HugeTLB pool +to the buddy allocator, the vmemmap pages representing that range needs to be +remapped again and the vmemmap pages discarded earlier need to be rellocated +again. If your use case is that HugeTLB pages are allocated 'on the fly' +instead of being pulled from the HugeTLB pool, you should weigh the benefits of +memory savings against the more overhead of allocation or freeing HugeTLB pages +between the HugeTLB pool and the buddy allocator. Another behavior to note is +that if the system is under heavy memory pressure, it could prevent the user +from freeing HugeTLB pages from the HugeTLB pool to the buddy allocator since +the allocation of vmemmap pages could be failed, you have to retry later if +your system encounter this situation. + +Once disabled, the vmemmap pages of subsequent allocation of HugeTLB pages from +buddy allocator will not be optimized, whereas already optimized HugeTLB pages +will not be affected. If you want to make sure there is no optimized HugeTLB +pages, you can set "nr_hugepages" to 0 first and then disable this. + + nr_hugepages_mempolicy ====================== diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 7ab15d6fb227..93f2cbea0f9b 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -348,4 +348,13 @@ void arch_remove_linear_mapping(u64 start, u64 size); extern bool mhp_supports_memmap_on_memory(unsigned long size); #endif /* CONFIG_MEMORY_HOTPLUG */ +#ifdef CONFIG_MHP_MEMMAP_ON_MEMORY +bool mhp_memmap_on_memory(void); +#else +static inline bool mhp_memmap_on_memory(void) +{ + return false; +} +#endif + #endif /* __LINUX_MEMORY_HOTPLUG_H */ diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 4b6a5cf16f11..94571d63916d 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -176,6 +176,7 @@ */ #define pr_fmt(fmt) "HugeTLB: " fmt +#include #include "hugetlb_vmemmap.h" #ifdef CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP @@ -189,21 +190,40 @@ #define RESERVE_VMEMMAP_NR 1U #define RESERVE_VMEMMAP_SIZE (RESERVE_VMEMMAP_NR << PAGE_SHIFT) +enum vmemmap_optimize_mode { + VMEMMAP_OPTIMIZE_OFF, + VMEMMAP_OPTIMIZE_ON, +}; + DEFINE_STATIC_KEY_MAYBE(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP_DEFAULT_ON, hugetlb_optimize_vmemmap_key); EXPORT_SYMBOL(hugetlb_optimize_vmemmap_key); +static enum vmemmap_optimize_mode vmemmap_optimize_mode = + IS_ENABLED(CONFIG_HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON); + +static void vmemmap_optimize_mode_switch(enum vmemmap_optimize_mode to) +{ + if (vmemmap_optimize_mode == to) + return; + + if (to == VMEMMAP_OPTIMIZE_OFF) + static_branch_dec(&hugetlb_optimize_vmemmap_key); + else + static_branch_inc(&hugetlb_optimize_vmemmap_key); + vmemmap_optimize_mode = to; +} + static int __init hugetlb_vmemmap_early_param(char *buf) { bool enable; + enum vmemmap_optimize_mode mode; if (kstrtobool(buf, &enable)) return -EINVAL; - if (enable) - static_branch_enable(&hugetlb_optimize_vmemmap_key); - else - static_branch_disable(&hugetlb_optimize_vmemmap_key); + mode = enable ? VMEMMAP_OPTIMIZE_ON : VMEMMAP_OPTIMIZE_OFF; + vmemmap_optimize_mode_switch(mode); return 0; } @@ -236,8 +256,10 @@ int hugetlb_vmemmap_alloc(struct hstate *h, struct page *head) */ ret = vmemmap_remap_alloc(vmemmap_addr, vmemmap_end, vmemmap_reuse, GFP_KERNEL | __GFP_NORETRY | __GFP_THISNODE); - if (!ret) + if (!ret) { ClearHPageVmemmapOptimized(head); + static_branch_dec(&hugetlb_optimize_vmemmap_key); + } return ret; } @@ -251,6 +273,8 @@ void hugetlb_vmemmap_free(struct hstate *h, struct page *head) if (!vmemmap_pages) return; + static_branch_inc(&hugetlb_optimize_vmemmap_key); + vmemmap_addr += RESERVE_VMEMMAP_SIZE; vmemmap_end = vmemmap_addr + (vmemmap_pages << PAGE_SHIFT); vmemmap_reuse = vmemmap_addr - PAGE_SIZE; @@ -260,7 +284,9 @@ void hugetlb_vmemmap_free(struct hstate *h, struct page *head) * to the page which @vmemmap_reuse is mapped to, then free the pages * which the range [@vmemmap_addr, @vmemmap_end] is mapped to. */ - if (!vmemmap_remap_free(vmemmap_addr, vmemmap_end, vmemmap_reuse)) + if (vmemmap_remap_free(vmemmap_addr, vmemmap_end, vmemmap_reuse)) + static_branch_dec(&hugetlb_optimize_vmemmap_key); + else SetHPageVmemmapOptimized(head); } @@ -277,9 +303,6 @@ void __init hugetlb_vmemmap_init(struct hstate *h) BUILD_BUG_ON(__NR_USED_SUBPAGE >= RESERVE_VMEMMAP_SIZE / sizeof(struct page)); - if (!hugetlb_optimize_vmemmap_enabled()) - return; - vmemmap_pages = (nr_pages * sizeof(struct page)) >> PAGE_SHIFT; /* * The head page is not to be freed to buddy allocator, the other tail @@ -295,4 +318,53 @@ void __init hugetlb_vmemmap_init(struct hstate *h) pr_info("can optimize %d vmemmap pages for %s\n", h->optimize_vmemmap_pages, h->name); } + +#ifdef CONFIG_PROC_SYSCTL +static int hugetlb_optimize_vmemmap_handler(struct ctl_table *table, int write, + void *buffer, size_t *length, + loff_t *ppos) +{ + int ret; + enum vmemmap_optimize_mode mode; + static DEFINE_MUTEX(sysctl_mutex); + + if (write && !capable(CAP_SYS_ADMIN)) + return -EPERM; + + mutex_lock(&sysctl_mutex); + mode = vmemmap_optimize_mode; + table->data = &mode; + ret = proc_dointvec_minmax(table, write, buffer, length, ppos); + if (write && !ret) + vmemmap_optimize_mode_switch(mode); + mutex_unlock(&sysctl_mutex); + + return ret; +} + +static struct ctl_table hugetlb_vmemmap_sysctls[] = { + { + .procname = "hugetlb_optimize_vmemmap", + .maxlen = sizeof(enum vmemmap_optimize_mode), + .mode = 0644, + .proc_handler = hugetlb_optimize_vmemmap_handler, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, + { } +}; + +static __init int hugetlb_vmemmap_sysctls_init(void) +{ + /* + * The vmemmap pages cannot be optimized if + * "memory_hotplug.memmap_on_memory" is enabled. + */ + if (!mhp_memmap_on_memory()) + register_sysctl_init("vm", hugetlb_vmemmap_sysctls); + + return 0; +} +late_initcall(hugetlb_vmemmap_sysctls_init); +#endif /* CONFIG_PROC_SYSCTL */ #endif /* CONFIG_HUGETLB_PAGE_HAS_OPTIMIZE_VMEMMAP */ diff --git a/mm/hugetlb_vmemmap.h b/mm/hugetlb_vmemmap.h index 3afae3ff37fa..63ae2766ffe0 100644 --- a/mm/hugetlb_vmemmap.h +++ b/mm/hugetlb_vmemmap.h @@ -21,7 +21,9 @@ void hugetlb_vmemmap_init(struct hstate *h); */ static inline unsigned int hugetlb_optimize_vmemmap_pages(struct hstate *h) { - return h->optimize_vmemmap_pages; + if (hugetlb_optimize_vmemmap_enabled()) + return h->optimize_vmemmap_pages; + return 0; } #else static inline int hugetlb_vmemmap_alloc(struct hstate *h, struct page *head) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index f6eab03397d3..af844a01e956 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -63,15 +63,10 @@ static bool memmap_on_memory __ro_after_init; module_param_cb(memmap_on_memory, &memmap_on_memory_ops, &memmap_on_memory, 0444); MODULE_PARM_DESC(memmap_on_memory, "Enable memmap on memory for memory hotplug"); -static inline bool mhp_memmap_on_memory(void) +bool mhp_memmap_on_memory(void) { return memmap_on_memory; } -#else -static inline bool mhp_memmap_on_memory(void) -{ - return false; -} #endif enum {