make persistent huge zero folio read-only
Subsystems (6)
cover make persistent huge zero folio read-only
From: Xueyuan Chen <[email protected]>
Hi all,
This series makes the persistent huge zero folio read-only in the direct map.
The motivation comes from Jann Horn's read-only zero page work[1] and the follow-up discussion[2] with Yang Shi. As Jann pointed out, the kernel has had bugs, including security bugs, where pages taken with read-only semantics were later written to. For the huge zero folio, making the direct map read-only turns such writes into faults instead of silently corrupting shared zero contents.
The permission change is best effort. If the architecture cannot safely make the direct map read-only, the kernel keeps using the writable persistent huge zero folio.
Patch 1 adds the generic support for making the persistent huge zero folio read-only. Patches 2 and 3 add arm64 and x86 support.
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/CAHbLzkrXXe7r3n3jXgDKtwZhRqj=jDx9E6dLOULohnhBguvi9A@mail.gmail.com/
Xueyuan Chen (3):
mm: make persistent huge zero folio read-only arm64/mm: make huge zero folio read-only in linear map x86/mm: make huge zero folio read-only in direct map
arch/arm64/Kconfig | 1 + arch/arm64/mm/pageattr.c | 16 ++++++++++++++++ arch/x86/Kconfig | 1 + arch/x86/mm/init.c | 11 +++++++++++ include/linux/huge_mm.h | 5 +++++ mm/Kconfig | 17 +++++++++++++++++ mm/huge_memory.c | 25 ++++++++++++++++++++++++- 7 files changed, 75 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.3
RFC PATCH 1/3 mm: make persistent huge zero folio read-only
From: Xueyuan Chen <[email protected]>
The huge zero folio is shared globally, and its contents should never change after initialization. As Jann Horn pointed out[1], the kernel has had bugs, including security bugs, where read-only pages were later written to. If the huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting shared zero contents.
For the persistent huge zero folio, set this up once after the folio is allocated at boot.
The permission change is best-effort. If the architecture cannot safely make the direct map read-only, keep using the writable persistent huge zero folio.
While at it, mark the huge_zero_folio pointer itself __ro_after_init. READONLY_HUGE_ZERO_FOLIO depends on PERSISTENT_HUGE_ZERO_FOLIO, so the pointer is initialized during boot and never replaced.
This was inspired by Jann Horn's read-only zero page work[1] and follow-up discussion[2] with Yang Shi.
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/CAHbLzkrXXe7r3n3jXgDKtwZhRqj=jDx9E6dLOULohnhBguvi9A@mail.gmail.com/
--- include/linux/huge_mm.h | 5 +++++ mm/Kconfig | 17 +++++++++++++++++ mm/huge_memory.c | 25 ++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-)
include/linux/huge_mm.h +5 -0
@@ -5,6 +5,7 @@#include <linux/mm_types.h> #include <linux/fs.h> /* only for vma_is_dax() */ +#include <linux/init.h> #include <linux/kobject.h> vm_fault_t do_huge_pmd_anonymous_page (struct vm_fault *vmf);
@@ -554,6 +555,10 @@ static inline bool is_huge_zero_pmd(pmd_t pmd)struct folio *mm_get_huge_zero_folio (struct mm_struct *mm);void mm_put_huge_zero_folio (struct mm_struct *mm); +#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO +bool __init arch_make_huge_zero_folio_readonly (struct folio *folio); +#endif +static inline struct folio *get_persistent_huge_zero_folio (void ) {if (!IS_ENABLED (CONFIG_PERSISTENT_HUGE_ZERO_FOLIO ))
mm/Kconfig +17 -0
@@ -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. + config MM_ID def_bool n
mm/huge_memory.c +24 -1
@@ -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 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 ; }
Signature · 1 line
2.47.3
Dev Jain
reviewerRe: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
Quoted 90 lines · Xueyuan chen
From: Xueyuan Chen <[email protected]>
The huge zero folio is shared globally, and its contents should never change after initialization. As Jann Horn pointed out[1], the kernel has had bugs, including security bugs, where read-only pages were later written to. If the huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting shared zero contents.
For the persistent huge zero folio, set this up once after the folio is allocated at boot.
The permission change is best-effort. If the architecture cannot safely make the direct map read-only, keep using the writable persistent huge zero folio.
While at it, mark the
huge_zero_foliopointer itself__ro_after_init.READONLY_HUGE_ZERO_FOLIOdepends onPERSISTENT_HUGE_ZERO_FOLIO, so the pointer is initialized during boot and never replaced.This was inspired by Jann Horn's read-only zero page work[1] and follow-up discussion[2] with Yang Shi.
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/CAHbLzkrXXe7r3n3jXgDKtwZhRqj=jDx9E6dLOULohnhBguvi9A@mail.gmail.com/Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/huge_mm.h | 5 +++++ mm/Kconfig | 17 +++++++++++++++++ mm/huge_memory.c | 25 ++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-)include/linux/huge_mm.h +5
-0@@ -5,6 +5,7 @@#include <linux/mm_types.h> #include <linux/fs.h> /* only for vma_is_dax() */ +#include <linux/init.h> #include <linux/kobject.h> vm_fault_t do_huge_pmd_anonymous_page (struct vm_fault *vmf);@@ -554,6 +555,10 @@ static inline bool is_huge_zero_pmd(pmd_t pmd)struct folio *mm_get_huge_zero_folio (struct mm_struct *mm);void mm_put_huge_zero_folio (struct mm_struct *mm); +#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO +bool __init arch_make_huge_zero_folio_readonly (struct folio *folio); +#endif +static inline struct folio *get_persistent_huge_zero_folio (void ) {if (!IS_ENABLED (CONFIG_PERSISTENT_HUGE_ZERO_FOLIO ))mm/Kconfig +17
-0@@ -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. + config MM_ID def_bool nmm/huge_memory.c +0
-0
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 79
Quoted 5 lines
mm/huge_memory.c +2
-0@@ -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 ;
Can we guard this with CONFIG_PERSISTENT_HUGE_ZERO_FOLIO? Since in that case too the pointer is never written after init.
Quoted 7 lines
mm/huge_memory.c +2
-0+#else struct folio *huge_zero_folio__read_mostly ; +#endif unsigned long huge_zero_pfn__read_mostly = ~0UL ;unsigned long huge_anon_orders_always__read_mostly ;unsigned long huge_anon_orders_madvise__read_mostly ;
mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 315
Quoted 7 lines
mm/huge_memory.c +4
-0@@ -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));
I think READ_ONCE is not required since no one is going to change the pointer after allocation?
Quoted 28 lines
mm/huge_memory.c +16
-1+} +#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 ; }
Re: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
On Wed, May 27, 2026 at 07:02:55PM +0530, Dev Jain wrote:
[...]
Quoted 8 lines
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 79
Quoted 3 lines
static
atomic_thuge_zero_refcount;mm/huge_memory.c +2
-0+#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO +struct folio *huge_zero_folio__ro_after_init ;Can we guard this with
CONFIG_PERSISTENT_HUGE_ZERO_FOLIO? Since in that case too the pointer is never written after init.
Yes, that makes sense. The pointer lifetime is tied to CONFIG_PERSISTENT_HUGE_ZERO_FOLIO. I'll fix this in v2.
Quoted 10 lines
mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 315
Quoted 5 lines
mm/huge_memory.c +4
-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));I think
READ_ONCEis not required since no one is going to change the pointer after allocation?
Agreed, READ_ONCE is not needed here. I'll drop it in v2.
Thanks,
Xueyuan
Re: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
In reply to Xueyuan chenOn 5/26/26 20:56, Xueyuan chen wrote:> +#ifdefCONFIG_READONLY_HUGE_ZERO_FOLIO
include/linux/huge_mm.h static inline bool is_huge_zero_pmd(pmd_t pmd) · line 560
Quoted 2 lines
include/linux/huge_mm.h +2
-0+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.
Quoted 17 lines
static inline struct folio
*get_persistent_huge_zero_folio(void){include/linux/huge_mm.h +0
-0if (!IS_ENABLED (CONFIG_PERSISTENT_HUGE_ZERO_FOLIO ))mm/Kconfig +6
-0@@ -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
mm/Kconfig config PERSISTENT_HUGE_ZERO_FOLIO · line 805
Quoted 10 lines
mm/Kconfig +10
-0+ 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.
mm/huge_memory.c static unsigned long deferred_split_scan(struct shrinker *shrink, · line 82
Quoted 13 lines
mm/huge_memory.c +4
-0@@ -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.
Quoted 27 lines
mm/huge_memory.c +12
-1unsigned 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 ())
mm/huge_memory.c static int __init thp_shrinker_init(void) · line 993
Quoted 10 lines
mm/huge_memory.c +8
-0+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.
Re: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
Quoted 10 lines · Dave Hansen
Quoted 9 lines · Xueyuan chen
mm/Kconfig +9
-0+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
Quoted 8 lines
mm/Kconfig config PERSISTENT_HUGE_ZERO_FOLIO · line 805
Quoted 4 lines
mm/Kconfig +4
-0+ 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
Kconfigoptions here. Why? This adds "security" with _basically_ no extra runtime cost. The runtime cost is, what, usually one kernel TLB invalidation during boot?
Plus potentially a bit more TLB pressure from losing a huge PUD in the linear map, IDK how much we care about that.
Re: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
Quoted 18 lines · Jann Horn
Quoted 17 lines · Dave Hansen
mm/Kconfig config PERSISTENT_HUGE_ZERO_FOLIO · line 805
Quoted 13 lines · Xueyuan chen
mm/Kconfig +13
-0+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
Kconfigoptions here. Why? This adds "security" with _basically_ no extra runtime cost. The runtime cost is, what, usually one kernel TLB invalidation during boot?
Quoted 2 lines
Plus potentially a bit more TLB pressure from losing a huge PUD in the linear map, IDK how much we care about that.
This shouldn't be a big issue on ARM64. The most ARM64 machines have linear mapping mapped with PTE if rodata is on. Some machines with BBML2_NOABORT support have linear mapping mapped with PUD/PMD, but those machines typically have large memory, having 512 PMDs instead of 1 PUD shouldn't be a noticeable issue IMHO.
Thanks,
Yang
Lance Yang
reviewerRe: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
Quoted 21 lines · Yang Shi
Quoted 18 lines · Jann Horn
Quoted 17 lines · Dave Hansen
mm/Kconfig config PERSISTENT_HUGE_ZERO_FOLIO · line 805
Quoted 13 lines · Xueyuan chen
mm/Kconfig +13
-0+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
Kconfigoptions here. Why? This adds "security" with _basically_ no extra runtime cost. The runtime cost is, what, usually one kernel TLB invalidation during boot?
Quoted 8 lines
Quoted 2 lines
Plus potentially a bit more TLB pressure from losing a huge PUD in the linear map, IDK how much we care about that.
This shouldn't be a big issue on ARM64. The most ARM64 machines have linear mapping mapped with PTE if rodata is on. Some machines with
BBML2_NOABORTsupport have linear mapping mapped with PUD/PMD, but those machines typically have large memory, having 512 PMDs instead of 1 PUD shouldn't be a noticeable issue IMHO.
Cool! Thanks Dave, Jann, Yang!
Yeah, that sounds reasonable. No need for another Kconfig option here; one less knob for people to care about :D
For arm64, I think Yang has it:
1) Without BBML2_NOABORT, rodata=on already forces the linear map down to PTEs, so nothing really changes there for most machines.
2) With BBML2_NOABORT, this may cost us 512 PMDs instead of one PUD for that. I don't expect that to be noticeable either ;)
So let's drop the option in the next version.
Cheers, Lance
David Hildenbrand (Arm)
maintainerRe: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
In reply to Dave HansenQuoted 7 lines · Dave Hansen
On 5/26/26 20:56, Xueyuan chen wrote:> +#ifdef
CONFIG_READONLY_HUGE_ZERO_FOLIOinclude/linux/huge_mm.h static inline bool is_huge_zero_pmd(pmd_t pmd) · line 560
Quoted 2 lines
include/linux/huge_mm.h +2
-0+bool __init arch_make_huge_zero_folio_readonly (struct folio *folio); +#endif All of the #ifdeffery needs to die, IMNHO.
Quoted 7 lines
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
__initarch_make_folio_readonly(struct folio *folio)
I'm not sure whether we want a folio-based interface, though.
This will likely be the only folio (that will actually have a "struct folio" in the future) compound page we'll ever mark read-only ... whereby I'd guess we'd want other non-folio pages to be read-only in the future (encryption keys etc, maybe?).
So I'd instead use a page-range based interface or sth like that.
Signature · 3 lines
Cheers, David
Lance Yang
reviewerRe: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 7 lines · Dave Hansen
On 5/26/26 20:56, Xueyuan chen wrote:> +#ifdef
CONFIG_READONLY_HUGE_ZERO_FOLIOinclude/linux/huge_mm.h static inline bool is_huge_zero_pmd(pmd_t pmd) · line 560
Quoted 2 lines
include/linux/huge_mm.h +2
-0+bool __init arch_make_huge_zero_folio_readonly (struct folio *folio); +#endif All of the #ifdeffery needs to die, IMNHO.
Quoted 7 lines
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
__initarch_make_folio_readonly(struct folio *folio)
Quoted 8 lines
I'm not sure whether we want a folio-based interface, though.
This will likely be the only folio (that will actually have a "struct folio" in the future) compound page we'll ever mark read-only ... whereby I'd guess we'd want other non-folio pages to be read-only in the future (encryption keys etc, maybe?).
So I'd instead use a page-range based interface or sth like that.
Just to check, did you mean something like:
bool arch_make_pages_readonly(struct page *page, int nr_pages);
?
So it can work on pages as well, not only folios :)
David Hildenbrand (Arm)
maintainerRe: [RFC PATCH 1/3] mm: make persistent huge zero folio read-only
Quoted 25 lines · Lance Yang
Quoted 13 lines · David Hildenbrand (Arm)
Quoted 11 lines · Dave Hansen
CONFIG_READONLY_HUGE_ZERO_FOLIOAll 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
__initarch_make_folio_readonly(struct folio *folio)Quoted 8 lines
I'm not sure whether we want a folio-based interface, though.
This will likely be the only folio (that will actually have a "struct folio" in the future) compound page we'll ever mark read-only ... whereby I'd guess we'd want other non-folio pages to be read-only in the future (encryption keys etc, maybe?).
So I'd instead use a page-range based interface or sth like that.
Quoted 7 lines
Just to check, did you mean something like:
bool
arch_make_pages_readonly(struct page *page, int nr_pages);?
So it can work on pages as well, not only folios :)
Yes, that would have been my first idea if we want to easily allow other reuse in the future. Of course, we could add a folio wrapper that will likely mostly be used by a single user ... :)
Signature · 3 lines
Cheers, David
RFC PATCH 2/3 arm64/mm: make huge zero folio read-only in linear map
In reply to Xueyuan chenFrom: Xueyuan Chen <[email protected]>
Implement arch_make_huge_zero_folio_readonly() for arm64. Once allocated, try to make the folio read-only in the linear map so unexpected writes fault instead of corrupting shared zero contents.
Respect can_set_direct_map() before touching the linear map, and treat the pageattr update as best effort: it can still fail while splitting a leaf mapping or applying new permissions. If that happens, generic THP keeps using the writable persistent huge zero folio.
--- arch/arm64/Kconfig | 1 + arch/arm64/mm/pageattr.c | 16 ++++++++++++++++ 2 files changed, 17 insertions(+)
arch/arm64/Kconfig +1 -0
@@ -44,6 +44,7 @@ config ARM64 select ARCH_HAS_PREEMPT_LAZY select ARCH_HAS_PTDUMP select ARCH_HAS_PTE_SPECIAL + select ARCH_HAS_READONLY_HUGE_ZERO_FOLIO select ARCH_HAS_HW_PTE_YOUNG select ARCH_HAS_SETUP_DMA_OPS select ARCH_HAS_SET_DIRECT_MAP
arch/arm64/mm/pageattr.c +16 -0
@@ -3,7 +3,9 @@* Copyright (c) 2014, The Linux Foundation. All rights reserved. */ #include <linux/kernel.h> +#include <linux/init.h> #include <linux/mm.h> +#include <linux/huge_mm.h> #include <linux/module.h> #include <linux/mem_encrypt.h> #include <linux/sched.h>
@@ -147,6 +149,20 @@ static int __change_memory_common(unsigned long start, unsigned long size,return ret; } +#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO +bool __init arch_make_huge_zero_folio_readonly (struct folio *folio) +{ +unsigned long addr = (unsigned long )folio_address (folio); + +if (!can_set_direct_map ()) +return false; + +return !__change_memory_common (addr,PMD_SIZE , +__pgprot (PTE_RDONLY ), +__pgprot (PTE_WRITE )); +} +#endif +static int change_memory_common (unsigned long addr,int numpages,pgprot_t set_mask,pgprot_t clear_mask) {
Signature · 1 line
2.47.3
RFC PATCH 3/3 x86/mm: make huge zero folio read-only in direct map
In reply to Xueyuan chenFrom: Xueyuan Chen <[email protected]>
Implement arch_make_huge_zero_folio_readonly() for x86-64. Once allocated, try to make the folio read-only in the direct map so unexpected writes fault instead of corrupting shared zero contents.
The set_memory_ro() update is best effort: if it fails, generic THP keeps using the writable persistent huge zero folio.
--- arch/x86/Kconfig | 1 + arch/x86/mm/init.c | 11 +++++++++++ 2 files changed, 12 insertions(+)
arch/x86/Kconfig +1 -0
@@ -24,6 +24,7 @@ config X86_64 def_bool y depends on 64BIT # Options that are inherently 64-bit kernel only: + select ARCH_HAS_READONLY_HUGE_ZERO_FOLIO select ARCH_HAS_GIGANTIC_PAGE select ARCH_SUPPORTS_MSEAL_SYSTEM_MAPPINGS select ARCH_SUPPORTS_INT128 if CC_HAS_INT128
arch/x86/mm/init.c +11 -0
@@ -3,6 +3,8 @@#include <linux/ioport.h> #include <linux/swap.h> #include <linux/memblock.h> +#include <linux/mm.h> +#include <linux/huge_mm.h> #include <linux/swapfile.h> #include <linux/swapops.h> #include <linux/kmemleak.h>
@@ -38,6 +40,15 @@#include "mm_internal.h" +#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO +bool __init arch_make_huge_zero_folio_readonly (struct folio *folio) +{ +unsigned long addr = (unsigned long )folio_address (folio); + +return !set_memory_ro (addr,HPAGE_PMD_NR ); +} +#endif +/* * Tables translating between page_cache_type_t and pte encoding. *
Signature · 1 line
2.47.3
Quoted 4 lines · Xueyuan chen
The motivation comes from Jann Horn's read-only zero page work[1] and the follow-up discussion[2] with Yang Shi. As Jann pointed out, the kernel has had bugs, including security bugs, where pages taken with read-only semantics were later written to.
My overall concern with this is that it's just a code hack for the huge zero page and nothing else. It's a total one-off.
I think you need to make the case here that the huge zero page truly is a special snowflake and deserves a one-off special snowflake solution. Because it doesn't seem *that* crazy that there are more things that the kernel dynamically allocates that it wants to keep read only.
Maybe there aren't many things that get mapped to userspace like this. But the case needs to get made either way.
Lance Yang
reviewerHi Dave,
Sorry for the late reply.
Quoted 8 lines · Dave Hansen
Quoted 4 lines · Xueyuan chen
The motivation comes from Jann Horn's read-only zero page work[1] and the follow-up discussion[2] with Yang Shi. As Jann pointed out, the kernel has had bugs, including security bugs, where pages taken with read-only semantics were later written to.
My overall concern with this is that it's just a code hack for the huge zero page and nothing else. It's a total one-off.
Fair point. I was trying to keep the first version simple, so I special-cased the persistent huge zero folio. But agreed, the arch side should not grow a huge-zero-folio-only hook :)
Quoted 7 lines
I think you need to make the case here that the huge zero page truly is a special snowflake and deserves a one-off special snowflake solution. Because it doesn't seem *that* crazy that there are more things that the kernel dynamically allocates that it wants to keep read only.
Maybe there aren't many things that get mapped to userspace like this. But the case needs to get made either way.
For RFC v2, I think we should make the arch hook generic, probably arch_make_folio_readonly(), with a weak default implementation. The persistent huge zero folio would just be the first user.
But even with a generic helper, we still need to be careful about the model. I see two possible ones:
1) Treat "read-only in the direct map" as real folio state. Then core MM has to know about it and carry or clear that state in paths like migration and freeing. It also makes the possible direct-map split / TLB cost a more general MM issue, not just a cost tied to one caller ...
2) Only use the helper for folios with a simple lifetime: allocated once, not migrated, not freed, and not written after they are made read-only.
I'd start with 2. Keeps the arch API generic, but avoids teaching core MM a new folio state before we have a user that really needs that. The persistent huge zero folio still works fine with that model. It just becomes the first user instead of the special case :)
Thoughts?
Cheers, Lance