mm: make persistent huge zero folio read-only
Subsystems (5)
v7 cover mm: make persistent huge zero folio read-only
The persistent huge zero folio is shared globally and must remain zero after initialization. As Jann Horn pointed out [1], kernel bugs can write to pages that are intended to be read-only, including in security-sensitive paths. Protecting the folio's direct-map mapping turns such writes into faults instead of silently corrupting the shared zero page.
This series makes that protection available to MM code and applies it to the persistent huge zero folio. It is best-effort: arm64 and x86 protect the permanent direct-map mapping, while highmem folios and architectures without support retain the existing behavior.
The interface remains page-based to match the existing direct-map helpers. The wider address-based conversion discussed in [2] can be handled separately. Permission changes and TLB invalidation stay together in the architecture code, avoiding a noflush interface that is easy to misuse.
Patches 2 and 3 add arm64 and x86 support. The approach follows the huge-zero-folio discussion in [3].
v6 -> v7:
- Rebase onto the latest mm-unstable and adapt to huge_zero_init() (per David).
- Switch to a page-based interface and move TLB flushing into the architecture implementations (per Will, Dave and David).
- https://lore.kernel.org/r/[email protected]/
v5 -> v6:
- Patch #01: Skip the direct-map permission change and TLB flush for highmem folios, which have no permanent direct-map mapping.
- https://lore.kernel.org/all/[email protected]/
RFC v4 -> v5:
- Drop the RFC tag.
- No code changes.
- https://lore.kernel.org/all/[email protected]/
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.
- https://lore.kernel.org/linux-mm/[email protected]/
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().
- https://lore.kernel.org/linux-mm/[email protected]/
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.
- https://lore.kernel.org/linux-mm/[email protected]/
Xueyuan Chen (3):
mm: make persistent huge zero folio read-only
arm64/mm: add set_direct_map_ro()
x86/mm: add set_direct_map_ro()
arch/arm64/include/asm/set_memory.h | 2 ++ arch/arm64/mm/pageattr.c | 12 ++++++++++++ arch/x86/include/asm/set_memory.h | 2 ++ arch/x86/mm/pat/set_memory.c | 10 ++++++++++ include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 6 files changed, 53 insertions(+), 3 deletions(-)
base-commit: 88297631d4d42f6004cb39c0ba3da7d2d10a616f
Signature · 1 line
2.47.3
PATCH v7 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 folio read-only in the direct map turns such writes into faults instead of silent zero-page corruption.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)
include/linux/set_memory.h +17 -0
@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);
mm/huge_memory.c +10 -3
@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h"
@@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio); -count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio); +count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR ); +return 0 ; }
Signature · 1 line
2.47.3
include/linux/set_memory.h static inline bool can_set_direct_map(void) · line 71
Quoted 4 lines · Xueyuan Chen
include/linux/set_memory.h +4
-0+static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +}
I still really think the stub here needs to return an error *or* the thing needs to just be void and tell folks that they can't know whether it worked or not.
Quoted 10 lines · Dave Hansen
include/linux/set_memory.h static inline bool can_set_direct_map(void) · line 71
Quoted 4 lines · Xueyuan Chen
include/linux/set_memory.h +4
-0+static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +}I still really think the stub here needs to return an error *or* the thing needs to just be void and tell folks that they can't know whether it worked or not.
Hi Dave,
I kept the stub returning 0 for consistency with the other set_direct_map*() stubs, which also return 0.
But returning -ENOSYS or -ENODEV works for me too if you prefer that.
Thanks,
Xueyuan
Hi Dave,
Quoted 9 lines · Dave Hansen
include/linux/set_memory.h static inline bool can_set_direct_map(void) · line 71
Quoted 4 lines · Xueyuan Chen
include/linux/set_memory.h +4
-0+static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +}I still really think the stub here needs to return an error *or* the thing needs to just be void and tell folks that they can't know whether it worked or not.
All other stubs return 0 here when an arch does not support set_direct_map. I'm for keeping them all consistent and fixing that technical debt as a whole rather than creating a one-off here.
Signature · 2 lines
Sincerely yours, Mike.
Quoted 9 lines · Mike Rapoport
Quoted 8 lines · Dave Hansen
include/linux/set_memory.h static inline bool can_set_direct_map(void) · line 71
Quoted 4 lines · Xueyuan Chen
include/linux/set_memory.h +4
-0+static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +}I still really think the stub here needs to return an error *or* the thing needs to just be void and tell folks that they can't know whether it worked or not.
Quoted 3 lines
All other stubs return 0 here when an arch does not support
set_direct_map. I'm for keeping them all consistent and fixing that technical debt as a whole rather than creating a one-off here.
Oof. I didn't realize the set_direct_map_*() functions all did this.
I do think it's generally a bad idea, and I'm also OK if it is not fixed up here.
Quoted 11 lines · Dave Hansen
Quoted 9 lines · Mike Rapoport
Quoted 8 lines · Dave Hansen
include/linux/set_memory.h static inline bool can_set_direct_map(void) · line 71
Quoted 4 lines · Xueyuan Chen
include/linux/set_memory.h +4
-0+static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +}I still really think the stub here needs to return an error *or* the thing needs to just be void and tell folks that they can't know whether it worked or not.
Quoted 8 lines
Quoted 3 lines
All other stubs return 0 here when an arch does not support
set_direct_map. I'm for keeping them all consistent and fixing that technical debt as a whole rather than creating a one-off here.Oof. I didn't realize the set_direct_map_*() functions all did this.
I do think it's generally a bad idea, and I'm also OK if it is not fixed up here.
OK, I'll leave it as-is then.
Thanks,
Xueyuan
Quoted 76 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +1
-2@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio);
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 10 lines
mm/huge_memory.c +8
-1-count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio); +count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );
Sorry, I don't remember if it was discussed previously, but why can't we use the existing set_memory_ro() here?
Quoted 7 lines
mm/huge_memory.c +1
-0+return 0 ; }Signature · 1 line
2.47.3
Signature · 2 lines
Sincerely yours, Mike.
Quoted 83 lines · Mike Rapoport
Quoted 81 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +4
-3@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio); -count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio);
Quoted 8 lines
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 5 lines
mm/huge_memory.c +5
-0+count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Sorry, I don't remember if it was discussed previously, but why can't we use the existing
set_memory_ro()here?
Hi Mike,
We want to change the linear map here, but arm64 set_memory_ro() only works on vmalloc addresses.
So a new helper is needed on arm64, and x86 implements the same helper to keep the two architectures consistent. On x86 it is basically set_memory_ro() minus the alias check.
Thanks,
Xueyuan
Quoted 12 lines
Quoted 8 lines
mm/huge_memory.c +1
-1+return 0 ; } --2.47.3
--
Sincerely yours,
Mike.
Quoted 93 lines · Xueyuan Chen
Quoted 83 lines · Mike Rapoport
Quoted 81 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +4
-3@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio); -count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio);Quoted 8 lines
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 5 lines
mm/huge_memory.c +5
-0+count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Sorry, I don't remember if it was discussed previously, but why can't we use the existing
set_memory_ro()here?
Quoted 4 lines
Hi Mike,
We want to change the linear map here, but arm64
set_memory_ro()only works on vmalloc addresses.
I believe this is an historical artifact. change_memory_common() already updates the linear map when a vmalloc mapping switches to RO and the system supports it.
I believe arm64::set_memory_ro() can change the linear map in the general case as well as long as can_set_direct_map() is true.
Quoted 19 lines
So a new helper is needed on arm64, and x86 implements the same helper to keep the two architectures consistent. On x86 it is basically
set_memory_ro()minus the alias check.Thanks,
XueyuanQuoted 12 lines
Quoted 8 lines
mm/huge_memory.c +1
-1+return 0 ; } --2.47.3
--
Sincerely yours,
Mike.
Signature · 2 lines
Sincerely yours, Mike.
Quoted 95 lines · Mike Rapoport
Quoted 93 lines · Xueyuan Chen
Quoted 91 lines · Mike Rapoport
Quoted 76 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +1
-2@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio);mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 10 lines
mm/huge_memory.c +8
-1-count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio); +count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Sorry, I don't remember if it was discussed previously, but why can't we use the existing
set_memory_ro()here?
Quoted 8 lines
Quoted 4 lines
Hi Mike,
We want to change the linear map here, but arm64
set_memory_ro()only works on vmalloc addresses.I believe this is an historical artifact.
change_memory_common()already updates the linear map when a vmalloc mapping switches to RO and the system supports it.
On x86, set_memory_ro() does work on direct-map addresses.
Quoted 4 lines
I believe
arm64::set_memory_ro()can change the linear map in the general case as well as long ascan_set_direct_map()is true.
On arm64, I'm reading arch/arm64/mm/pageattr.c, and the linear-map update in change_memory_common() is only reachable for vmalloc addresses:
set_memory_ro
change_memory_common
area = find_vm_area((void *)addr);
if (!area || ...)
return -EINVAL;So set_memory_ro() on a linear-map address returns -EINVAL on arm64.
Am I missing something here?
Quoted 23 lines
Quoted 4 lines
So a new helper is needed on arm64, and x86 implements the same helper to keep the two architectures consistent. On x86 it is basically
set_memory_ro()minus the alias check.Quoted 15 lines
Thanks,
XueyuanQuoted 12 lines
Quoted 8 lines
mm/huge_memory.c +1
-1+return 0 ; } --2.47.3
--
Sincerely yours,
Mike.--
Sincerely yours,
Mike.
Hi Xueyuan,
Quoted 30 lines · Xueyuan Chen
Quoted 9 lines · Mike Rapoport
Quoted 9 lines
Quoted 8 lines
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 5 lines
mm/huge_memory.c +4
-0+ +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Sorry, I don't remember if it was discussed previously, but why can't we use the existing
set_memory_ro()here?Quoted 8 lines
Quoted 4 lines
Hi Mike,
We want to change the linear map here, but arm64
set_memory_ro()only works on vmalloc addresses.I believe this is an historical artifact.
change_memory_common()already updates the linear map when a vmalloc mapping switches to RO and the system supports it.On x86,
set_memory_ro()does work on direct-map addresses.Quoted 4 lines
I believe
arm64::set_memory_ro()can change the linear map in the general case as well as long ascan_set_direct_map()is true.On arm64, I'm reading
arch/arm64/mm/pageattr.c, and the linear-map update inchange_memory_common()is only reachable for vmalloc addresses:
Quoted 7 lines
set_memory_ro change_memory_common area = find_vm_area((void *)addr); if (!area || ...) return -EINVAL;So
set_memory_ro()on a linear-map address returns-EINVALon arm64.
And I believe this is an historical artifact rather than necessity. For configurations that allow set_direct_map on arm64, change_memory_common can update direct map too, it just need to be implemented there.
Signature · 2 lines
Sincerely yours, Mike.
David Hildenbrand (Arm)
maintainerQuoted 27 lines · Mike Rapoport
Hi Xueyuan,
Quoted 24 lines · Xueyuan Chen
Quoted 5 lines · Mike Rapoport
I believe this is an historical artifact.
change_memory_common()already updates the linear map when a vmalloc mapping switches to RO and the system supports it.On x86,
set_memory_ro()does work on direct-map addresses.Quoted 4 lines
I believe
arm64::set_memory_ro()can change the linear map in the general case as well as long ascan_set_direct_map()is true.On arm64, I'm reading
arch/arm64/mm/pageattr.c, and the linear-map update inchange_memory_common()is only reachable for vmalloc addresses:set_memory_ro change_memory_common area = find_vm_area((void *)addr); if (!area || ...) return -EINVAL;
Quoted 6 lines
Quoted 1 line
So
set_memory_ro()on a linear-map address returns-EINVALon arm64.And I believe this is an historical artifact rather than necessity. For configurations that allow
set_direct_mapon arm64,change_memory_commoncan update direct map too, it just need to be implemented there.
Are we sure the other architectures do what we want in their set_memory_ro? Having a dedicated set_direct_map_ro() with clear (documented ;)) semantics does not sound too crazy to me.
Signature · 3 lines
Cheers, David
Quoted 28 lines · David Hildenbrand (Arm)
Quoted 27 lines · Mike Rapoport
Hi Xueyuan,
Quoted 24 lines · Xueyuan Chen
Quoted 5 lines · Mike Rapoport
I believe this is an historical artifact.
change_memory_common()already updates the linear map when a vmalloc mapping switches to RO and the system supports it.On x86,
set_memory_ro()does work on direct-map addresses.Quoted 4 lines
I believe
arm64::set_memory_ro()can change the linear map in the general case as well as long ascan_set_direct_map()is true.On arm64, I'm reading
arch/arm64/mm/pageattr.c, and the linear-map update inchange_memory_common()is only reachable for vmalloc addresses:set_memory_ro change_memory_common area = find_vm_area((void *)addr); if (!area || ...) return -EINVAL;
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
So
set_memory_ro()on a linear-map address returns-EINVALon arm64.And I believe this is an historical artifact rather than necessity. For configurations that allow
set_direct_mapon arm64,change_memory_commoncan update direct map too, it just need to be implemented there.Are we sure the other architectures do what we want in their
set_memory_ro?
It's a zoo right now :(
Although most of them do.
But we need to sort it out anyway because of increasingly growing demand for modifications of the kernel page tables.
I started something here:
https://lore.kernel.org/linux-mm/[email protected]/
Now that x86 CPA is nearly fixed, I can look into the next iteration.
Quoted 2 lines
Having a dedicated
set_direct_map_ro()with clear (documented ;)) semantics does not sound too crazy to me.
It's not too crazy, no.
Still making arm64 more aligned with x86 and other arches that update the direct/linear map in the set_memory_ro() seem a good thing on it's own.
Quoted 4 lines
Signature · 3 lines
Cheers, David
Signature · 2 lines
Sincerely yours, Mike.
David Hildenbrand (Arm)
maintainerQuoted 5 lines · Mike Rapoport
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 3 lines · Mike Rapoport
Hi Xueyuan,
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
And I believe this is an historical artifact rather than necessity. For configurations that allow
set_direct_mapon arm64,change_memory_commoncan update direct map too, it just need to be implemented there.Are we sure the other architectures do what we want in their
set_memory_ro?It's a zoo right now :(
Yeah, that's why I'm asking :)
Quoted 4 lines
Although most of them do.
But we need to sort it out anyway because of increasingly growing demand for modifications of the kernel page tables.
The question really is whether we want to use a single interface for all page tables.
I quite like a page-based directmap interface. Other page table areas are not necessarily page-based. It also expresses which types of operations we expect on the direct map.
One could, of course, use set_memory_ro() internally to implement set_direct_map_ro() (and similarly for other variants); but at least set_direct_map_*() would have clear, well documented semantics.
Quoted 6 lines
I started something here:
https://lore.kernel.org/linux-mm/[email protected]/Now that x86 CPA is nearly fixed, I can look into the next iteration.
Quoted 7 lines
Quoted 2 lines
Having a dedicated
set_direct_map_ro()with clear (documented ;)) semantics does not sound too crazy to me.It's not too crazy, no.
Still making arm64 more aligned with x86 and other arches that update the direct/linear map in the
set_memory_ro()seem a good thing on it's own.
Right, but I'd much rather user set_memory_ro() later to implement set_direct_map_ro() [initially on architectures where we made sure that it actually does what we expect? later maybe generically]
Signature · 3 lines
Cheers, David
Quoted 43 lines · David Hildenbrand (Arm)
Quoted 5 lines · Mike Rapoport
Quoted 4 lines · David Hildenbrand (Arm)
Quoted 3 lines · Mike Rapoport
Hi Xueyuan,
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
And I believe this is an historical artifact rather than necessity. For configurations that allow
set_direct_mapon arm64,change_memory_commoncan update direct map too, it just need to be implemented there.Are we sure the other architectures do what we want in their
set_memory_ro?It's a zoo right now :(
Yeah, that's why I'm asking :)
Quoted 4 lines
Although most of them do.
But we need to sort it out anyway because of increasingly growing demand for modifications of the kernel page tables.
The question really is whether we want to use a single interface for all page tables.
I quite like a page-based directmap interface. Other page table areas are not necessarily page-based. It also expresses which types of operations we expect on the direct map.
One could, of course, use
set_memory_ro()internally to implementset_direct_map_ro()(and similarly for other variants); but at least set_direct_map_*() would have clear, well documented semantics.Quoted 9 lines
I started something here:
https://lore.kernel.org/linux-mm/[email protected]/Now that x86 CPA is nearly fixed, I can look into the next iteration.
Quoted 2 lines
Having a dedicated
set_direct_map_ro()with clear (documented ;)) semantics does not sound too crazy to me.
Quoted 9 lines
Quoted 4 lines
It's not too crazy, no.
Still making arm64 more aligned with x86 and other arches that update the direct/linear map in the
set_memory_ro()seem a good thing on it's own.Right, but I'd much rather user
set_memory_ro()later to implementset_direct_map_ro()[initially on architectures where we made sure that it actually does what we expect? later maybe generically]
Hi David, Mike,
Thanks for the discussion. I'll keep the page-based set_direct_map_ro() interface. :)
Thanks,
Xueyuan
Quoted 4 lines
--
Cheers,David
David Hildenbrand (Arm)
maintainerQuoted 76 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +1
-2@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio);
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 10 lines
mm/huge_memory.c +8
-1-count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio); +count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );
Assuming we keep the page-based approach, can't we just move the highmem test in there?
Signature · 3 lines
Cheers, David
Quoted 83 lines · David Hildenbrand (Arm)
Quoted 81 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +4
-3@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio); -count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio);
Quoted 8 lines
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 5 lines
mm/huge_memory.c +5
-0+count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Assuming we keep the page-based approach, can't we just move the highmem test in there?
Hi David,
We can move the check into set_direct_map_ro(). However, on x86, set_direct_map_invalid_noflush() and set_direct_map_default_noflush() also expect callers to exclude highmem pages.
Would it make sense to make highmem a documented no-op for those helpers as well, so the direct-map APIs handle it consistently?
Thanks,
Xueyuan
Quoted 5 lines
--
Cheers,David
David Hildenbrand (Arm)
maintainerQuoted 93 lines · Xueyuan Chen
Quoted 83 lines · David Hildenbrand (Arm)
Quoted 81 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +4
-3@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio); -count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio);Quoted 8 lines
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 5 lines
mm/huge_memory.c +5
-0+count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Assuming we keep the page-based approach, can't we just move the highmem test in there?
Quoted 8 lines
Hi David,
We can move the check into
set_direct_map_ro(). However, on x86,set_direct_map_invalid_noflush()andset_direct_map_default_noflush()also expect callers to exclude highmem pages.Would it make sense to make highmem a documented no-op for those helpers as well, so the direct-map APIs handle it consistently?
I think getting something consistent for now is the most important thing. :)
Signature · 3 lines
Cheers, David
Quoted 97 lines · David Hildenbrand (Arm)
Quoted 95 lines · Xueyuan Chen
Quoted 83 lines · David Hildenbrand (Arm)
Quoted 81 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +4
-3@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio); -count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio);Quoted 8 lines
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 5 lines
mm/huge_memory.c +5
-0+count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Assuming we keep the page-based approach, can't we just move the highmem test in there?
Hi David,
Quoted 9 lines
Quoted 6 lines
We can move the check into
set_direct_map_ro(). However, on x86,set_direct_map_invalid_noflush()andset_direct_map_default_noflush()also expect callers to exclude highmem pages.Would it make sense to make highmem a documented no-op for those helpers as well, so the direct-map APIs handle it consistently?
I think getting something consistent for now is the most important thing. :)
Hi David,
OK, I'll move the highmem check into set_direct_map_ro().
Should I also update the x86 set_direct_map_invalid_noflush() and set_direct_map_default_noflush() helpers in this series to skip highmem pages?
Thanks,
Xueyuan
Quoted 4 lines
--
Cheers,David
Quoted 103 lines · Xueyuan Chen
Quoted 101 lines · David Hildenbrand (Arm)
Quoted 99 lines · Xueyuan Chen
Quoted 83 lines · David Hildenbrand (Arm)
Quoted 81 lines · Xueyuan Chen
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.
Add a page-based helper consistent with the existing direct-map interfaces. Handle TLB invalidation in the architecture implementation; unsupported architectures retain their current behavior.
Protect the folio after initialization. Skip highmem folios, which have no permanent direct-map mapping.
Inspired by Jann Horn's read-only zero page work [1] and follow-up discussion [3] with Yang Shi.
Suggested-by David Hildenbrand <[email protected]>Suggested-by Usama Arif <[email protected]>Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- include/linux/set_memory.h | 17 +++++++++++++++++ mm/huge_memory.c | 13 ++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-)include/linux/set_memory.h +17
-0@@ -54,6 +54,23 @@ static inline bool can_set_direct_map(void)#endif #endif /* CONFIG_ARCH_HAS_SET_DIRECT_MAP */ +#ifndef set_direct_map_ro +/** +* set_direct_map_ro - make a direct-map range read-only +* @page: first page in the direct-map range +* @nr: number of pages in the range +* +* Make the direct-map range starting at @page read-only and invalidate stale +* writable translations before returning. +* +* Return: 0 on success, or a negative error code on failure. +*/ +static inline int set_direct_map_ro (struct page *page,unsigned int nr) +{ +return 0 ; +} +#endif +#ifdef CONFIG_X86_64 int set_mce_nospec (unsigned long pfn);int clear_mce_nospec (unsigned long pfn);mm/huge_memory.c +4
-3@@ -42,6 +42,7 @@#include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> #include <linux/cleanup.h> +#include <linux/set_memory.h> #include <asm/tlb.h> #include "internal.h" @@ -291,10 +292,16 @@ static int __init huge_zero_init(void) huge_zero_folio =alloc_huge_zero_folio ();if (!huge_zero_folio) {pr_warn ("Allocating persistent huge zero folio failed\n" ); - }else { - huge_zero_pfn =folio_pfn (huge_zero_folio); -count_vm_event (THP_ZERO_PAGE_ALLOC ); +return 0 ; } + + huge_zero_pfn =folio_pfn (huge_zero_folio);Quoted 8 lines
mm/huge_memory.c static int __init huge_zero_init(void) · line 303
Quoted 5 lines
mm/huge_memory.c +5
-0+count_vm_event (THP_ZERO_PAGE_ALLOC ); + +/* Highmem folios have no permanent direct-map mapping to protect. */ +if (!folio_test_highmem (huge_zero_folio)) +set_direct_map_ro (folio_page (huge_zero_folio,0 ),HPAGE_PMD_NR );Assuming we keep the page-based approach, can't we just move the highmem test in there?
Hi David,
We can move the check into
set_direct_map_ro(). However, on x86,set_direct_map_invalid_noflush()andset_direct_map_default_noflush()also expect callers to exclude highmem pages.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Would it make sense to make highmem a documented no-op for those helpers as well, so the direct-map APIs handle it consistently?
I think getting something consistent for now is the most important thing. :)
Hi David,
OK, I'll move the highmem check into
set_direct_map_ro().
Consistent means leaving highmem check in the caller ...
Quoted 3 lines
Should I also update the x86
set_direct_map_invalid_noflush()andset_direct_map_default_noflush()helpers in this series to skip highmem pages?
... or checking it in all set_direct_map functions.
I'd keep the check in huge_zero_init() in this patchset.
Quoted 6 lines
Thanks,
XueyuanQuoted 4 lines
--
Cheers,David
Signature · 2 lines
Sincerely yours, Mike.
David Hildenbrand (Arm)
maintainerQuoted 13 lines · Mike Rapoport
Quoted 9 lines · Xueyuan Chen
Quoted 4 lines · David Hildenbrand (Arm)
I think getting something consistent for now is the most important thing. :)
Hi David,
OK, I'll move the highmem check into
set_direct_map_ro().Consistent means leaving highmem check in the caller ...
Quoted 7 lines
Quoted 3 lines
Should I also update the x86
set_direct_map_invalid_noflush()andset_direct_map_default_noflush()helpers in this series to skip highmem pages?... or checking it in all
set_direct_mapfunctions.I'd keep the check in
huge_zero_init()in this patchset.
Agreed!
Signature · 3 lines
Cheers, David
Quoted 19 lines · David Hildenbrand (Arm)
Quoted 17 lines · Mike Rapoport
Quoted 9 lines · Xueyuan Chen
Quoted 4 lines · David Hildenbrand (Arm)
I think getting something consistent for now is the most important thing. :)
Hi David,
OK, I'll move the highmem check into
set_direct_map_ro().Consistent means leaving highmem check in the caller ...
Quoted 3 lines
Should I also update the x86
set_direct_map_invalid_noflush()andset_direct_map_default_noflush()helpers in this series to skip highmem pages?
Quoted 6 lines
Quoted 3 lines
... or checking it in all
set_direct_mapfunctions.I'd keep the check in
huge_zero_init()in this patchset.Agreed!
OK, I'll keep the check in huge_zero_init() in this patchset.
Quoted 4 lines
--
Cheers,David
PATCH v7 2/3 arm64/mm: add set_direct_map_ro()
In reply to Xueyuan ChenImplement set_direct_map_ro() for arm64 and flush the TLB internally.
--- arch/arm64/include/asm/set_memory.h | 2 ++ arch/arm64/mm/pageattr.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)
arch/arm64/include/asm/set_memory.h +2 -0
@@ -13,6 +13,8 @@ int set_memory_valid(unsigned long addr, int numpages, int enable);int set_direct_map_invalid_noflush (struct page *page,unsigned int numpages);int set_direct_map_default_noflush (struct page *page,unsigned int numpages); +int set_direct_map_ro (struct page *page,unsigned int nr); +#define set_direct_map_ro set_direct_map_robool kernel_page_present (struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);
arch/arm64/mm/pageattr.c +12 -0
@@ -355,6 +355,18 @@ int realm_register_memory_enc_ops(void)return arm64_mem_crypt_ops_register (&realm_crypt_ops); } +int set_direct_map_ro (struct page *page,unsigned int nr) +{ +unsigned long addr = (unsigned long )page_address (page); + +if (!can_set_direct_map ()) +return 0 ; + +return __change_memory_common (addr,PAGE_SIZE * nr, +__pgprot (PTE_RDONLY ), +__pgprot (PTE_WRITE )); +} +#ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages (struct page *page,int numpages,int enable) {
Signature · 1 line
2.47.3
Re: [PATCH v7 2/3] arm64/mm: add set_direct_map_ro()
Quoted 32 lines · Xueyuan Chen
Implement
set_direct_map_ro()for arm64 and flush the TLB internally.Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- arch/arm64/include/asm/set_memory.h | 2 ++ arch/arm64/mm/pageattr.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)arch/arm64/include/asm/set_memory.h +2
-0@@ -13,6 +13,8 @@ int set_memory_valid(unsigned long addr, int numpages, int enable);int set_direct_map_invalid_noflush (struct page *page,unsigned int numpages);int set_direct_map_default_noflush (struct page *page,unsigned int numpages); +int set_direct_map_ro (struct page *page,unsigned int nr); +#define set_direct_map_ro set_direct_map_robool kernel_page_present (struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);arch/arm64/mm/pageattr.c +1
-0@@ -355,6 +355,18 @@ int realm_register_memory_enc_ops(void)return arm64_mem_crypt_ops_register (&realm_crypt_ops); } +int set_direct_map_ro (struct page *page,unsigned int nr)
arch/arm64/mm/pageattr.c int realm_register_memory_enc_ops(void) · line 368
Quoted 10 lines
arch/arm64/mm/pageattr.c +10
-0+{ +unsigned long addr = (unsigned long )page_address (page); + +if (!can_set_direct_map ()) +return 0 ; + +return __change_memory_common (addr,PAGE_SIZE * nr, +__pgprot (PTE_RDONLY ), +__pgprot (PTE_WRITE )); +}
I haven't quite followed whether or not you intend to send a v8 making set_memory_ro() handle linear addresses, but fwiw the implementation you have here at least looks correct to me, so:
Will
Re: [PATCH v7 2/3] arm64/mm: add set_direct_map_ro()
Quoted 49 lines · Will Deacon
Quoted 32 lines · Xueyuan Chen
Implement
set_direct_map_ro()for arm64 and flush the TLB internally.Co-developed-by Lance Yang <[email protected]>Signed-off-by Lance Yang <[email protected]>Signed-off-by Xueyuan Chen <[email protected]>--- arch/arm64/include/asm/set_memory.h | 2 ++ arch/arm64/mm/pageattr.c | 12 ++++++++++++ 2 files changed, 14 insertions(+)arch/arm64/include/asm/set_memory.h +2
-0@@ -13,6 +13,8 @@ int set_memory_valid(unsigned long addr, int numpages, int enable);int set_direct_map_invalid_noflush (struct page *page,unsigned int numpages);int set_direct_map_default_noflush (struct page *page,unsigned int numpages); +int set_direct_map_ro (struct page *page,unsigned int nr); +#define set_direct_map_ro set_direct_map_robool kernel_page_present (struct page *page);int set_memory_encrypted (unsigned long addr,int numpages);arch/arm64/mm/pageattr.c +1
-0@@ -355,6 +355,18 @@ int realm_register_memory_enc_ops(void)return arm64_mem_crypt_ops_register (&realm_crypt_ops); } +int set_direct_map_ro (struct page *page,unsigned int nr)arch/arm64/mm/pageattr.c int realm_register_memory_enc_ops(void) · line 368
Quoted 10 lines
arch/arm64/mm/pageattr.c +10
-0+{ +unsigned long addr = (unsigned long )page_address (page); + +if (!can_set_direct_map ()) +return 0 ; + +return __change_memory_common (addr,PAGE_SIZE * nr, +__pgprot (PTE_RDONLY ), +__pgprot (PTE_WRITE )); +}I haven't quite followed whether or not you intend to send a v8 making
set_memory_ro()handle linear addresses, but fwiw the implementation you have here at least looks correct to me, so:
Quoted 1 line
Acked-by Will Deacon <[email protected]>
Thanks for taking a look!
Quoted 2 lines
Will
PATCH v7 3/3 x86/mm: add set_direct_map_ro()
In reply to Xueyuan ChenImplement set_direct_map_ro() for x86 with a page-based interface. Clear _PAGE_RW and _PAGE_DIRTY through the CPA path and let the common CPA wrapper invalidate stale writable translations before returning.
--- arch/x86/include/asm/set_memory.h | 2 ++ arch/x86/mm/pat/set_memory.c | 10 ++++++++++ 2 files changed, 12 insertions(+)
arch/x86/include/asm/set_memory.h +2 -0
@@ -88,6 +88,8 @@ int set_pages_rw(struct page *page, int numpages);int set_direct_map_invalid_noflush (struct page *page,unsigned int nr);int set_direct_map_default_noflush (struct page *page,unsigned int nr); +int set_direct_map_ro (struct page *page,unsigned int nr); +#define set_direct_map_ro set_direct_map_robool kernel_page_present (struct page *page);extern int kernel_set_to_readonly;
arch/x86/mm/pat/set_memory.c +10 -0
@@ -2666,6 +2666,16 @@ int set_direct_map_default_noflush(struct page *page, unsigned int nr)return __set_pages_p (page, nr,0 ); } +int set_direct_map_ro (struct page *page,unsigned int nr) +{ +unsigned long addr = (unsigned long )page_address (page); + +/* Direct-map callers are expected to pass pages without aliases. */ +return change_page_attr_set_clr (&addr, nr,__pgprot (0 ), +__pgprot (_PAGE_RW | _PAGE_DIRTY),0 , +CPA_NO_CHECK_ALIAS ,NULL ); +} +#ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages (struct page *page,int numpages,int enable) {
Signature · 1 line
2.47.3