Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp2064693pxb; Fri, 25 Mar 2022 10:27:31 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwl9VSpypoX9bpPwZ8J/TBWUMBYooH11+DE1qX6JAF2q4tlutUvy5EIIFHm/F0T5Uo8HtM2 X-Received: by 2002:a17:902:b582:b0:14c:a63d:3df6 with SMTP id a2-20020a170902b58200b0014ca63d3df6mr12890253pls.51.1648229251743; Fri, 25 Mar 2022 10:27:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648229251; cv=none; d=google.com; s=arc-20160816; b=GX/UBgAuHfMeVDuVuAgubwe/MBw3oa1VqIzkWX+UTVfH1Cz0xGtbw9EbQOtkwqpNip uyCqJ/bX4lFyvIy0QvyfgpqP0W9hOuo9sd2eKzVa8MW1vu70+nsYywMdaPvOqN+5nn9y V6I3ZNd245ibm3DxEe7H2++F6BhaOO+meXp6RDnBM+BdGHDlBN5FUjkXoj0KjC+tHXvV xif5fi7WAvi8HbeKGb9CLVNbcLnxEvs8C7LDDxNdAiUEHovkJ4U1IsdFuc2Dq8EBcJsJ KH31oXOPVoLhROxSeaDTgpq/cdtfxO9HZjKACjb4GLxTj7dRHHrgWcw4xgE6YC1UCpnC FXTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature:dkim-filter; bh=EA2McNg9gBsMb+VOM3ot+UljuCXwcwnlphFiuTV1jjU=; b=GVGFJAMjum/PacWq27VOfiplpbsBBxraXQ/YaxieY7Nyd/O0vKtTqi9brxGC7QghEa VASFfGsA3JByXTwc88anr+eFeeLTvHMRJ5GRen24oxQ0FXSQZ2Scg7GZONTTUdiNaD5c hn7PTrNDnM8svVeQNVgcPtn3VDAnyjTRacTDsdnnTq68aQ41XBwIfmJI3TTtxGT39e14 JH/HOdCszFecFcmZ0Tqy+jCmNPmUbG9qt8rfbAywNVMNW6UGAmwWFlkdwWuezLTwm3SA QYBHlR6GdbX0TiCLuW6SkwbhxsK71pJ6FH8jViH4XslQQKIlLHvhBKwsp63wdB7WrvwQ wIzg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=MaddDudI; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id q9-20020a170902f78900b00153b2d16443si3022150pln.75.2022.03.25.10.27.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 25 Mar 2022 10:27:31 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@nifty.com header.s=dec2015msa header.b=MaddDudI; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 9E9D9100E0F; Fri, 25 Mar 2022 10:21:39 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358325AbiCYFMv (ORCPT + 99 others); Fri, 25 Mar 2022 01:12:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240824AbiCYFMu (ORCPT ); Fri, 25 Mar 2022 01:12:50 -0400 Received: from conssluserg-05.nifty.com (conssluserg-05.nifty.com [210.131.2.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 242D2C4E28; Thu, 24 Mar 2022 22:11:17 -0700 (PDT) Received: from mail-pg1-f177.google.com (mail-pg1-f177.google.com [209.85.215.177]) (authenticated) by conssluserg-05.nifty.com with ESMTP id 22P5Aeie023414; Fri, 25 Mar 2022 14:10:41 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-05.nifty.com 22P5Aeie023414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1648185041; bh=EA2McNg9gBsMb+VOM3ot+UljuCXwcwnlphFiuTV1jjU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=MaddDudIA8vuwBix+QMg/9YdmndNaQxESYOxmvTEDSFy+qJc5LbKWI8+tnkvuyOfR mFd/bVrEVdRxwnRY0W/FKS9MEQdXzOoR3Ye8OAb0dmlwhjX3W2OP6TAHyT6HYpKdUO GXcrDSckU4/f0sZK9AIoP4up99sC3Kbpgga7cerFTvab3bFzN/itR1VEW5t6bjrUuC XPLp4FQUDYZbpesN76t5RkzUxkexkLb5iDf2eLKWb8votdSYaYB8A5Zigg1gocZ6Ob fBEa/dADskqlU4k4fmrA7d//YUxhwvuC95DNpO3CYKbjYGH7ZNz+cTf8PpCxOvnFrC QfehRGsbCokkw== X-Nifty-SrcIP: [209.85.215.177] Received: by mail-pg1-f177.google.com with SMTP id t13so4329436pgn.8; Thu, 24 Mar 2022 22:10:41 -0700 (PDT) X-Gm-Message-State: AOAM530EkA7H2qGZW9B3zmDCzdqxqfW6nG/0Psoo3XkS5b7vayMtc4HY f/mAT44h1FCQr5RylGVeMNAUevQMuUJa+WoF/AQ= X-Received: by 2002:a05:6a00:234f:b0:4fa:f52b:46a1 with SMTP id j15-20020a056a00234f00b004faf52b46a1mr5328380pfj.32.1648185040175; Thu, 24 Mar 2022 22:10:40 -0700 (PDT) MIME-Version: 1.0 References: <20220323125523.79254-1-songmuchun@bytedance.com> <20220323125523.79254-2-songmuchun@bytedance.com> In-Reply-To: <20220323125523.79254-2-songmuchun@bytedance.com> From: Masahiro Yamada Date: Fri, 25 Mar 2022 14:09:56 +0900 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v5 1/4] mm: hugetlb_vmemmap: introduce STRUCT_PAGE_SIZE_IS_POWER_OF_2 To: Muchun Song Cc: Jonathan Corbet , mike.kravetz@oracle.com, Andrew Morton , "Luis R. Rodriguez" , Kees Cook , Iurii Zaikin , Oscar Salvador , David Hildenbrand , "open list:DOCUMENTATION" , Linux Kernel Mailing List , Linux Memory Management List , duanxiongchun@bytedance.com, smuchun@gmail.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 23, 2022 at 9:57 PM Muchun Song wrote: > > 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 > --- > 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 No, please do not do this. It is terrible to feed back this to Kconfig again. If you know this happens on !CONFIG_MEMCG && !CONFIG_SLUB on x86_64, why don't you add this dependency directly? If you want to avoid the run-time check, why don't you use BUILD_BUG_ON() ? > + > +##### > # 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 > -- > 2.11.0 > -- Best Regards Masahiro Yamada