From patchwork Wed Mar 23 12:55:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muchun Song X-Patchwork-Id: 12789758 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 CF593C433EF for ; Wed, 23 Mar 2022 12:57:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43D926B0073; Wed, 23 Mar 2022 08:57:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3EBED6B0074; Wed, 23 Mar 2022 08:57:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 28C8E6B0075; Wed, 23 Mar 2022 08:57:22 -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 1AC066B0073 for ; Wed, 23 Mar 2022 08:57:22 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EB28D60FB1 for ; Wed, 23 Mar 2022 12:57:21 +0000 (UTC) X-FDA: 79275651882.13.F28D189 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by imf11.hostedemail.com (Postfix) with ESMTP id 3E2F94000F for ; Wed, 23 Mar 2022 12:57:21 +0000 (UTC) Received: by mail-pg1-f169.google.com with SMTP id k14so1073438pga.0 for ; Wed, 23 Mar 2022 05:57:21 -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=8U++b74ADZPjIiOkZZbim39PJyVay6HYO7+jpHyJ7BU=; b=fuvlOqlnx4uwam7PbWcvOe9tU8gNtjEOl+4ijCOT65bO9wP7Hl7SprBh6d1rQVhw3t 7XW8w8EomUGuwR04Iik6xJW8mJUKc/FISyN3Tlfxr4qxfANF/Ar1WO9NW7oI1PXVWXKM Z3uOIqqJRTxopgmzOVGCHLh48mVCZwsMx+8MxQJV5b8ywKU/uUY34MsDjePT98oshcrM lg+vV1H4wiCBvksP8h9SWYwWpi7nCgJn2cjEAPOnXdMRCDSRaIILcVKQz0wYvy46EKtS hNpvq2cHKJIGg85cYYd9u10qBt0aURe1Oh/c/Co/ouAxxzcDZFqay0SvL5oBOxsCVHu0 a12w== 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=8U++b74ADZPjIiOkZZbim39PJyVay6HYO7+jpHyJ7BU=; b=gclqUA4DS68fxQukJlnNPsg0n7JVyj+HU74FMPXYDuXMxzFKPsBjEW2D3sQSnAPbVL Xdz6rm7WyKJ72GhIGpxMEiVmZMeDjIjEKBGnbH3VEc0wHh6RtxdC3IF7SSlNOxqfzKme URL5ex9c9IVbzH6t5r5F9Qobdpac1stGOMysntQAu9hGC+bB5RuNOUuFhYwnF3p7EhSb 3yBTLxifEHtEXA4BNiXVyNPGMCueSMsFm3vEEo5g7YNd6iH/oLGHtDSiymcBCI37ODG+ UwrsEw/LQkGIXjPxAMmyBBeQjcVlHlKotfdqtBheNvmKvFB01s8C7feafGuqsV9EYH7/ X5/g== X-Gm-Message-State: AOAM532Zrelz1FHpigjkKdtX5WFK9vPhirsiQoQC173bX9QFn00x4JaJ 2bfp4a4L+l9MDPIwT1pImspqTA== X-Google-Smtp-Source: ABdhPJxME2zOZXrnFN9UyKcNEF/huqi8q5lIdipbwtt+diUrlSh84HhmazStaDoumJy/UyhW5GLunw== X-Received: by 2002:a62:6101:0:b0:4fa:7ff4:b543 with SMTP id v1-20020a626101000000b004fa7ff4b543mr22479202pfb.75.1648040240170; Wed, 23 Mar 2022 05:57:20 -0700 (PDT) Received: from FVFYT0MHHV2J.bytedance.net ([139.177.225.238]) by smtp.gmail.com with ESMTPSA id k185-20020a6384c2000000b003821dcd9020sm15716517pgd.27.2022.03.23.05.57.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Mar 2022 05:57:19 -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 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 v5 1/4] mm: hugetlb_vmemmap: introduce STRUCT_PAGE_SIZE_IS_POWER_OF_2 Date: Wed, 23 Mar 2022 20:55:20 +0800 Message-Id: <20220323125523.79254-2-songmuchun@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220323125523.79254-1-songmuchun@bytedance.com> References: <20220323125523.79254-1-songmuchun@bytedance.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 3E2F94000F X-Stat-Signature: bqxd7dny9okdaydroqi9859a84rq7a1g X-Rspam-User: Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fuvlOqln; dmarc=pass (policy=none) header.from=bytedance.com; spf=pass (imf11.hostedemail.com: domain of songmuchun@bytedance.com designates 209.85.215.169 as permitted sender) smtp.mailfrom=songmuchun@bytedance.com X-HE-Tag: 1648040241-610676 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 and this feature 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 configuration. In order to avoid many checks like "is_power_of_2 (sizeof(struct page))" through mm/hugetlb_vmemmap.c. Introduce STRUCT_PAGE_SIZE_IS_POWER_OF_2 to detect if the size of struct page is power of 2 and make this feature depends on this new config. Then we could prevent anyone do any unexpected configuration. Signed-off-by: Muchun Song Suggested-by: Luis Chamberlain Reported-by: kernel test robot Reported-by: kernel test robot --- Kbuild | 14 ++++++++++++++ fs/Kconfig | 1 + include/linux/mm_types.h | 2 ++ mm/Kconfig | 3 +++ mm/hugetlb_vmemmap.c | 6 ------ mm/struct_page_size.c | 19 +++++++++++++++++++ scripts/check_struct_page_po2.sh | 9 +++++++++ 7 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 mm/struct_page_size.c create mode 100755 scripts/check_struct_page_po2.sh diff --git a/Kbuild b/Kbuild index fa441b98c9f6..21415c3b2728 100644 --- a/Kbuild +++ b/Kbuild @@ -37,6 +37,20 @@ $(offsets-file): arch/$(SRCARCH)/kernel/asm-offsets.s FORCE $(call filechk,offsets,__ASM_OFFSETS_H__) ##### +# Generate struct_page_size.h. + +struct_page_size-file := include/generated/struct_page_size.h + +always-y := $(struct_page_size-file) +targets := mm/struct_page_size.s + +mm/struct_page_size.s: $(timeconst-file) $(bounds-file) + +$(struct_page_size-file): mm/struct_page_size.s FORCE + $(call filechk,offsets,__LINUX_STRUCT_PAGE_SIZE_H__) + $(Q)$(MAKE) -f $(srctree)/Makefile syncconfig + +##### # Check for missing system calls always-y += missing-syscalls diff --git a/fs/Kconfig b/fs/Kconfig index 7f2455e8e18a..856d2e9f5aef 100644 --- a/fs/Kconfig +++ b/fs/Kconfig @@ -249,6 +249,7 @@ config HUGETLB_PAGE_FREE_VMEMMAP def_bool HUGETLB_PAGE depends on X86_64 depends on SPARSEMEM_VMEMMAP + depends on STRUCT_PAGE_SIZE_IS_POWER_OF_2 config HUGETLB_PAGE_FREE_VMEMMAP_DEFAULT_ON bool "Default freeing vmemmap pages of HugeTLB to on" diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 8834e38c06a4..5fbff44a4310 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -223,6 +223,7 @@ struct page { #endif } _struct_page_alignment; +#ifndef __GENERATING_STRUCT_PAGE_SIZE_IS_POWER_OF_2_H /** * struct folio - Represents a contiguous set of bytes. * @flags: Identical to the page flags. @@ -844,5 +845,6 @@ enum fault_flag { FAULT_FLAG_INSTRUCTION = 1 << 8, FAULT_FLAG_INTERRUPTIBLE = 1 << 9, }; +#endif /* !__GENERATING_STRUCT_PAGE_SIZE_IS_POWER_OF_2_H */ #endif /* _LINUX_MM_TYPES_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 034d87953600..9314bd34f49e 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -2,6 +2,9 @@ menu "Memory Management options" +config STRUCT_PAGE_SIZE_IS_POWER_OF_2 + def_bool $(success,test "$(shell, $(srctree)/scripts/check_struct_page_po2.sh)" = 1) + config SELECT_MEMORY_MODEL def_bool y depends on ARCH_SELECT_MEMORY_MODEL diff --git a/mm/hugetlb_vmemmap.c b/mm/hugetlb_vmemmap.c index 791626983c2e..33ecb77c2b2a 100644 --- a/mm/hugetlb_vmemmap.c +++ b/mm/hugetlb_vmemmap.c @@ -194,12 +194,6 @@ EXPORT_SYMBOL(hugetlb_free_vmemmap_enabled_key); static int __init early_hugetlb_free_vmemmap_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; diff --git a/mm/struct_page_size.c b/mm/struct_page_size.c new file mode 100644 index 000000000000..5749609aa1b3 --- /dev/null +++ b/mm/struct_page_size.c @@ -0,0 +1,19 @@ +// 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. + */ + +#define __GENERATING_STRUCT_PAGE_SIZE_IS_POWER_OF_2_H +/* Include headers that define the enum constants of interest */ +#include +#include +#include + +int main(void) +{ + DEFINE(STRUCT_PAGE_SIZE_IS_POWER_OF_2, is_power_of_2(sizeof(struct page))); + + return 0; +} diff --git a/scripts/check_struct_page_po2.sh b/scripts/check_struct_page_po2.sh new file mode 100755 index 000000000000..1764ef9a4f1d --- /dev/null +++ b/scripts/check_struct_page_po2.sh @@ -0,0 +1,9 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# Check if the size of "struct page" is power of 2 + +file="include/generated/struct_page_size.h" +if [ -f "$file" ]; then + grep STRUCT_PAGE_SIZE_IS_POWER_OF_2 "$file" | cut -d' ' -f3 +fi