mm: make persistent huge zero folio read-only
Subsystems (5)
v5 cover mm: make persistent huge zero folio read-only
The persistent huge zero folio is shared globally and should stay zero after initialization. As Jann Horn pointed out[1], kernel bugs have ended up writing to pages that were meant to be read-only, including in security-sensitive cases. Making the folio read-only in the direct map turns such writes into faults instead of silent zero-page corruption.
This series adds set_direct_map_ro_noflush() so mm code can make a direct-map range read-only, then uses it for the persistent huge zero folio. The helper is direct-map specific, takes an address-based range as discussed for set_direct_map* helpers[2], and leaves TLB invalidation to the caller.
The folio is allocated and zeroed through the writable direct map before thp_shrinker_init() changes its permissions. thp_shrinker_init() is called from hugepage_init(), which is registered as a subsys_initcall and runs after SMP initialization. Writable TLB entries may therefore already be cached when the page-table permissions change. Patch 1 flushes the exact direct-map range immediately after the noflush page-table update. Keeping the flush at the call site preserves the helper's explicit noflush contract and follows existing direct-map helper users such as secretmem and hibernation.
This is the first non-RFC posting of the series. The direct-map helper interface and its TLB invalidation contract have stabilized through the RFC discussion, and the series is now intended for regular review and possible merging.
Patches 2 and 3 add arm64 and x86 implementations.
RFC v4 -> v5:
- Drop the RFC tag.
- No code changes.
RFC v3 -> RFC v4:
- Patch #01: Flush the direct-map range after changing it read-only, since the folio was cleared through writable mappings after SMP initialization (per Usama, thanks!).
- Patch #01: Keep the flush in the caller to preserve the set_direct_map_ro_noflush() contract and make the flushed range explicit.
- Patch #01: Clarify the noflush API contract and the reason stale writable translations must be invalidated.
RFC v2 -> RFC v3:
- Patch #01: Replace arch_make_pages_readonly() with set_direct_map_ro_noflush() in the existing set_direct_map* family (per Mike and David, thanks!).
- Patch #01: Use a direct-map address and number of pages, and document the direct-map-only and no-TLB-flush semantics (per David, thanks!).
- Patch #02 and #03: Update the arm64 and x86 implementations for set_direct_map_ro_noflush().
RFC v1 -> RFC v2:
- Patch #01: Drop the READONLY_HUGE_ZERO_FOLIO Kconfig option (per Dave, thanks!).
- Patch #01: Replace the huge-zero-folio-specific hook with a generic page-range hook (per David, thanks!).
- Patch #02 and #03: Update the arm64 and x86 implementations for the new hook.
Xueyuan Chen (3):
mm: make persistent huge zero folio read-only
arm64/mm: add set_direct_map_ro_noflush()
x86/mm: add set_direct_map_ro_noflush()
arch/arm64/include/asm/set_memory.h | 2 ++ arch/arm64/mm/pageattr.c | 10 ++++++++++ arch/x86/include/asm/set_memory.h | 2 ++ arch/x86/mm/pat/set_memory.c | 15 +++++++++++++++ include/linux/set_memory.h | 29 +++++++++++++++++++++++++++++ mm/huge_memory.c | 16 +++++++++++++++- 6 files changed, 73 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.3
PATCH v5 1/3 mm: make persistent huge zero folio read-only
The persistent huge zero folio is shared globally and should stay zero after initialization. As Jann Horn pointed out[1], kernel bugs have ended up writing to pages that were meant to be read-only, including in security-sensitive cases. Making the persistent huge zero folio read-only in the direct map turns such writes into faults instead of silent zero-page corruption.
Add set_direct_map_ro_noflush() so mm code can make a direct-map range read-only. Use an address-based signature to match ongoing direct-map helper work[2], where existing page-based helpers may move the same way. The helper is direct-map specific and leaves TLB invalidation to its caller. Architectures without direct-map permission support keep existing behavior through the generic stub.
The folio is allocated and zeroed through the writable direct map before thp_shrinker_init() changes its permissions. thp_shrinker_init() is called from hugepage_init(), which is registered as a subsys_initcall and runs after SMP initialization. Stale writable kernel TLB entries may therefore exist. Flush the direct-map range immediately after the page-table update so they cannot bypass the read-only mapping.
Treat the direct-map permission change as best-effort. Architectures that do not implement the helper keep the existing behavior via the generic stub.
Inspired by Jann Horn's read-only zero page work[1] and follow-up discussion[3] with Yang Shi.
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/CAHbLzkrXXe7r3n3jXgDKtwZhRqj=jDx9E6dLOULohnhBguvi9A@mail.gmail.com/
--- include/linux/set_memory.h | 29 +++++++++++++++++++++++++++++ mm/huge_memory.c | 16 +++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-)
include/linux/set_memory.h +29 -0
@@ -40,6 +40,24 @@ static inline int set_direct_map_valid_noflush(struct page *page,return 0 ; } +/** +* set_direct_map_ro_noflush - make a direct-map range read-only +* @addr: start address in the direct map +* @nr_pages: number of pages starting at @addr +* +* Make the direct-map range starting at @addr read-only without invalidating +* TLBs. Callers must either ensure that no stale writable translations can +* be used, or treat the permission change as a best-effort hardening step. +* +* Return: 0 on success or when direct-map permission changes are unsupported, +* or a negative errno on failure. +*/ +static inline int set_direct_map_ro_noflush (const void *addr, +unsigned long nr_pages) +{ +return 0 ; +} +static inline bool kernel_page_present (struct page *page) {return true;
@@ -56,6 +74,17 @@ static inline bool can_set_direct_map(void) }#define can_set_direct_map can_set_direct_map#endif + +#ifndef set_direct_map_ro_noflush +/* See the comment above the generic fallback for the _noflush contract. */ +static inline int set_direct_map_ro_noflush (const void *addr, +unsigned long nr_pages) +{ +return 0 ; +} + +#define set_direct_map_ro_noflush set_direct_map_ro_noflush +#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ #ifdef CONFIG_X86_64
mm/huge_memory.c +15 -1
@@ -40,8 +40,10 @@#include <linux/pgalloc.h> #include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> +#include <linux/set_memory.h> #include <asm/tlb.h> +#include <asm/tlbflush.h> #include "internal.h" #include "swap.h"
@@ -932,6 +934,8 @@ static int __init thp_shrinker_init(void)shrinker_register (deferred_split_shrinker);if (IS_ENABLED (CONFIG_PERSISTENT_HUGE_ZERO_FOLIO )) { +unsigned long addr; +/* * Bump the reference of the huge_zero_folio and do not * initialize the shrinker.
@@ -940,8 +944,18 @@ 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 ; + } + + addr = (unsigned long )folio_address (huge_zero_folio); +/* +* The folio was zeroed through the writable direct map. Flush +* after the page-table update to invalidate stale translations. +*/ +set_direct_map_ro_noflush ((void *)addr,HPAGE_PMD_NR ); +flush_tlb_kernel_range (addr, addr +HPAGE_PMD_SIZE );return 0 ; }
Signature · 1 line
2.47.3
PATCH v5 2/3 arm64/mm: add set_direct_map_ro_noflush()
In reply to Xueyuan ChenImplement set_direct_map_ro_noflush() for arm64 with update_range_prot() on the linear map, setting PTE_RDONLY and clearing PTE_WRITE. Keep the existing can_set_direct_map() guard and leave TLB invalidation to the caller.
--- arch/arm64/include/asm/set_memory.h | 2 ++ arch/arm64/mm/pageattr.c | 10 ++++++++++ 2 files changed, 12 insertions(+)
arch/arm64/include/asm/set_memory.h +2 -0
@@ -14,6 +14,8 @@ int set_memory_valid(unsigned long addr, int numpages, int enable);int set_direct_map_invalid_noflush (struct page *page);int set_direct_map_default_noflush (struct page *page);int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_ro_noflush (const void *addr,unsigned long nr_pages); +#define set_direct_map_ro_noflush set_direct_map_ro_noflushbool kernel_page_present (struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);
arch/arm64/mm/pageattr.c +10 -0
@@ -365,6 +365,16 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)return set_memory_valid (addr, nr, valid); } +int set_direct_map_ro_noflush (const void *addr,unsigned long nr_pages) +{ +if (!can_set_direct_map ()) +return 0 ; + +return update_range_prot ((unsigned long )addr,PAGE_SIZE * nr_pages, +__pgprot (PTE_RDONLY ), +__pgprot (PTE_WRITE )); +} +#ifdef CONFIG_DEBUG_PAGEALLOC /* * This is - apart from the return value - doing the same
Signature · 1 line
2.47.3
PATCH v5 3/3 x86/mm: add set_direct_map_ro_noflush()
In reply to Xueyuan ChenImplement set_direct_map_ro_noflush() for x86 using CPA directly on the passed direct-map address. Clear _PAGE_RW and _PAGE_DIRTY, keep alias checks disabled like the existing direct-map _noflush helpers, and leave TLB invalidation to the caller.
--- arch/x86/include/asm/set_memory.h | 2 ++ arch/x86/mm/pat/set_memory.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+)
arch/x86/include/asm/set_memory.h +2 -0
@@ -89,6 +89,8 @@ int set_pages_rw(struct page *page, int numpages);int set_direct_map_invalid_noflush (struct page *page);int set_direct_map_default_noflush (struct page *page);int set_direct_map_valid_noflush (struct page *page,unsigned nr,bool valid); +int set_direct_map_ro_noflush (const void *addr,unsigned long nr_pages); +#define set_direct_map_ro_noflush set_direct_map_ro_noflushbool kernel_page_present (struct page *page);extern int kernel_set_to_readonly;
arch/x86/mm/pat/set_memory.c +15 -0
@@ -2662,6 +2662,21 @@ int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid)return __set_pages_np (page, nr); } +int set_direct_map_ro_noflush (const void *addr,unsigned long nr_pages) +{ +unsigned long tempaddr = (unsigned long )addr; +struct cpa_data cpa = { + .vaddr = &tempaddr, + .pgd =NULL , + .numpages = nr_pages, + .mask_set =__pgprot (0 ), + .mask_clr =__pgprot (_PAGE_RW | _PAGE_DIRTY), + .flags =CPA_NO_CHECK_ALIAS , + }; + +return __change_page_attr_set_clr (&cpa,1 ); +} +#ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages (struct page *page,int numpages,int enable) {
Signature · 1 line
2.47.3
Andrew Morton
maintainerQuoted 11 lines · On Mon, 27 Jul 2026 22:34:23 +0800 Xueyuan Chen <[email protected]>…
The persistent huge zero folio is shared globally and should stay zero after initialization. As Jann Horn pointed out[1], kernel bugs have ended up writing to pages that were meant to be read-only, including in security-sensitive cases. Making the folio read-only in the direct map turns such writes into faults instead of silent zero-page corruption.
This series adds
set_direct_map_ro_noflush()so mm code can make a direct-map range read-only, then uses it for the persistent huge zero folio. The helper is direct-map specific, takes an address-based range as discussed for set_direct_map* helpers[2], and leaves TLB invalidation to the caller.
Thanks. AI review asked about a few things, some pre-existing. Includes a possible pre-existing ARM barrier issue in arch/arm64/mm/pageattr.c
https://sashiko.dev/#/patchset/[email protected]
Quoted 14 lines · Andrew Morton
Quoted 11 lines · On Mon, 27 Jul 2026 22:34:23 +0800 Xueyuan Chen <[email protected]>…
The persistent huge zero folio is shared globally and should stay zero after initialization. As Jann Horn pointed out[1], kernel bugs have ended up writing to pages that were meant to be read-only, including in security-sensitive cases. Making the folio read-only in the direct map turns such writes into faults instead of silent zero-page corruption.
This series adds
set_direct_map_ro_noflush()so mm code can make a direct-map range read-only, then uses it for the persistent huge zero folio. The helper is direct-map specific, takes an address-based range as discussed for set_direct_map* helpers[2], and leaves TLB invalidation to the caller.
Quoted 4 lines
Thanks. AI review asked about a few things, some pre-existing. Includes a possible pre-existing ARM barrier issue in
arch/arm64/mm/pageattr.chttps://sashiko.dev/#/patchset/[email protected]
Hi Andrew,
Thanks. The highmem case is valid. A highmem folio has no permanent direct-map mapping, so v6 will skip the permission update and TLB flush in that case.
The huge_zero_pfn race is addressed by Lorenzo's mm-hotfixes series:
https://lore.kernel.org/r/[email protected]
I don't think the MTE report needs a change. Commit 1d0cb4c8864a ("arm64: mte: Ensure the cleared tags are visible before setting the PTE") added the smp_wmb() specifically for this ordering.
I'll send v6 shortly.
Thanks
Xueyuan