From: Dave Hansen <dave.hansen@intel.com>
To: Xueyuan chen <xueyuan.chen21@gmail.com>,
akpm@linux-foundation.org, linux-mm@kvack.org
Cc: linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org, x86@kernel.org,
catalin.marinas@arm.com, will@kernel.org, tglx@kernel.org,
mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com,
hpa@zytor.com, david@kernel.org, ljs@kernel.org, ziy@nvidia.com,
baolin.wang@linux.alibaba.com, ryan.roberts@arm.com,
dev.jain@arm.com, lance.yang@linux.dev,
yang@os.amperecomputing.com, jannh@google.com
Subject: Re: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
Date: Wed, 27 May 2026 08:55:08 -0700 [thread overview]
Message-ID: <4e0696ee-895c-486d-aacc-71146f0e9047@intel.com> (raw)
In-Reply-To: <20260527035607.14919-2-xueyuan.chen21@gmail.com>
On 5/26/26 20:56, Xueyuan chen wrote:> +#ifdef
CONFIG_READONLY_HUGE_ZERO_FOLIO
> +bool __init arch_make_huge_zero_folio_readonly(struct folio *folio);
> +#endif
All of the #ifdeffery needs to die, IMNHO.
This function is also a bad idea. There is nothing "huge zero" specific
about it. It takes any old folio and tries to make it read only.
Just make it:
bool __init arch_make_folio_readonly(struct folio *folio)
Make it a weak symbol with a stub in some mm/foo.c file, and then the
architectures can override it if they want.
> static inline struct folio *get_persistent_huge_zero_folio(void)
> {
> if (!IS_ENABLED(CONFIG_PERSISTENT_HUGE_ZERO_FOLIO))
> diff --git a/mm/Kconfig b/mm/Kconfig
> index 776b67c66e82..f31200816646 100644
> --- a/mm/Kconfig
> +++ b/mm/Kconfig
> @@ -787,6 +787,23 @@ config PERSISTENT_HUGE_ZERO_FOLIO
> Say Y if your system has lots of memory. Say N if you are
> memory constrained.
>
> +config ARCH_HAS_READONLY_HUGE_ZERO_FOLIO
> + bool
> +
> +config READONLY_HUGE_ZERO_FOLIO
> + bool "Map the huge zero folio read-only in the direct map"
> + depends on PERSISTENT_HUGE_ZERO_FOLIO
> + depends on ARCH_HAS_READONLY_HUGE_ZERO_FOLIO
> + help
> + The persistent huge zero folio is shared globally, and nothing
> + should ever change its contents after initialization.
> +
> + When supported, mark the folio read-only in the direct map so such
> + writes trigger a fault instead of silently corrupting the zero contents.
> +
> + If the permission change is not supported, the kernel keeps using
> + the writable persistent huge zero folio.
I vote for no Kconfig options here. Why? This adds "security" with
_basically_ no extra runtime cost. The runtime cost is, what, usually
one kernel TLB invalidation during boot?
If you desperately need one, you can add it without a prompt so folks
need to edit .config files to change the values.
> diff --git a/mm/huge_memory.c b/mm/huge_memory.c
> index bf9b480bb3b0..c568755dd58e 100644
> --- a/mm/huge_memory.c
> +++ b/mm/huge_memory.c
> @@ -75,7 +75,11 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
> static bool split_underused_thp = true;
>
> static atomic_t huge_zero_refcount;
> +#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO
> +struct folio *huge_zero_folio __ro_after_init;
> +#else
> struct folio *huge_zero_folio __read_mostly;
> +#endif
#ifdefs bad. Bad. Bad. Bad. ;)
Seriously, just pick one. Lowest common denominator is more important
that being precisely correct all the time.
> unsigned long huge_zero_pfn __read_mostly = ~0UL;
> unsigned long huge_anon_orders_always __read_mostly;
> unsigned long huge_anon_orders_madvise __read_mostly;
> @@ -305,6 +309,18 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
> return 0;
> }
>
> +#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO
> +static bool __init make_huge_zero_folio_readonly(void)
> +{
> + return arch_make_huge_zero_folio_readonly(READ_ONCE(huge_zero_folio));
> +}
> +#else
> +static bool __init make_huge_zero_folio_readonly(void)
> +{
> + return false;
> +}
> +#endif
> +
> static struct shrinker *huge_zero_folio_shrinker;
>
> #ifdef CONFIG_SYSFS
> @@ -965,8 +981,15 @@ static int __init thp_shrinker_init(void)
> * that get_huge_zero_folio() will most likely not fail as
> * thp_shrinker_init() is invoked early on during boot.
> */
> - if (!get_huge_zero_folio())
> + if (!get_huge_zero_folio()) {
> pr_warn("Allocating persistent huge zero folio failed\n");
> + return 0;
> + }
> +
> + if (IS_ENABLED(CONFIG_READONLY_HUGE_ZERO_FOLIO) &&
> + !make_huge_zero_folio_readonly())
> + pr_warn("Making persistent huge zero folio read-only failed\n");
> +
> return 0;
The IS_ENABLED() doesn't do anything from what I can tell. This is all
in one .c file. The compiler can see that
make_huge_zero_folio_readonly() is always false. It should optimize out
the pr_warn() for you.
next prev parent reply other threads:[~2026-05-27 15:55 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-27 3:56 [RFC PATCH 0/3] " Xueyuan chen
2026-05-27 3:56 ` [RFC PATCH 1/3] mm: " Xueyuan chen
2026-05-27 13:32 ` Dev Jain
2026-05-27 23:03 ` Xueyuan Chen
2026-05-27 15:55 ` Dave Hansen [this message]
2026-05-27 16:20 ` Jann Horn
2026-05-28 18:43 ` Yang Shi
2026-05-29 3:09 ` Lance Yang
2026-06-01 13:49 ` David Hildenbrand (Arm)
2026-06-01 15:43 ` Lance Yang
2026-06-01 15:46 ` David Hildenbrand (Arm)
2026-05-27 3:56 ` [RFC PATCH 2/3] arm64/mm: make huge zero folio read-only in linear map Xueyuan chen
2026-05-27 3:56 ` [RFC PATCH 3/3] x86/mm: make huge zero folio read-only in direct map Xueyuan chen
2026-05-27 15:58 ` [RFC PATCH 0/3] make persistent huge zero folio read-only Dave Hansen
2026-05-30 7:46 ` Lance Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4e0696ee-895c-486d-aacc-71146f0e9047@intel.com \
--to=dave.hansen@intel.com \
--cc=akpm@linux-foundation.org \
--cc=baolin.wang@linux.alibaba.com \
--cc=bp@alien8.de \
--cc=catalin.marinas@arm.com \
--cc=dave.hansen@linux.intel.com \
--cc=david@kernel.org \
--cc=dev.jain@arm.com \
--cc=hpa@zytor.com \
--cc=jannh@google.com \
--cc=lance.yang@linux.dev \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ljs@kernel.org \
--cc=mingo@redhat.com \
--cc=ryan.roberts@arm.com \
--cc=tglx@kernel.org \
--cc=will@kernel.org \
--cc=x86@kernel.org \
--cc=xueyuan.chen21@gmail.com \
--cc=yang@os.amperecomputing.com \
--cc=ziy@nvidia.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome