Skip to contentSkip to conversation

make persistent huge zero folio read-only

RFC PATCH Superseded Xueyuan Chen 7 participants 23 messages Jun 9 – Jun 24

Subsystems (6)
Memory management x86 MM core arm64 +2 more subsystems

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover make persistent huge zero folio read-only

Hi all,

This series makes the persistent huge zero folio read-only in the direct map where the architecture can support it.

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.

Patch 1 adds a generic arch_make_pages_readonly() hook and uses it after the persistent huge zero folio is allocated. Patches 2 and 3 implement the hook for arm64 and x86.

If the hook is not implemented, or the architecture cannot safely update the mapping, the existing writable mapping is left in place.

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/CAHbLzkrXXe7r3n3jXgDKtwZhRqj=jDx9E6dLOULohnhBguvi9A@mail.gmail.com/

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/huge_memory: make persistent huge zero folio read-only arm64/mm: make pages read-only in the linear map x86/mm: make pages read-only in the direct map

 arch/arm64/mm/pageattr.c | 13 +++++++++++++
 arch/x86/mm/init.c       |  9 +++++++++
 include/linux/mm.h       |  2 ++
 mm/huge_memory.c         | 13 ++++++++++++-
 4 files changed, 36 insertions(+), 1 deletion(-)
Signature · 1 line
2.47.3
#1 lore raw

RFC PATCH v2 1/3 mm/huge_memory: make persistent huge zero folio read-only

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 persistent huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting the shared zero contents.

Add arch_make_pages_readonly() so mm code can request read-only direct-map protection for a page range. Direct-map protection is architecture-specific, so the generic weak implementation does nothing.

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/mm.h |  2 ++
 mm/huge_memory.c   | 13 ++++++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)
include/linux/mm.h +2 -0
index 0f2612a70fb1..02d33cf45b01 100644
@@ -2936,6 +2936,8 @@ static inline int arch_make_folio_accessible(struct folio *folio)
 }
 #endif
 
+bool arch_make_pages_readonly(struct page *page, int nr_pages);
+
 /*
  * Some inline functions in vmstat.h depend on page_zone()
  */
mm/huge_memory.c +12 -1
index b81d1ecb434b..2e9d01dc1197 100644
@@ -308,6 +308,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 	return 0;
 }
 
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}
+
 static struct shrinker *huge_zero_folio_shrinker;
 
 #ifdef CONFIG_SYSFS
@@ -982,8 +987,14 @@ 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;
+		}
+
+		arch_make_pages_readonly(folio_page(huge_zero_folio, 0),
+					HPAGE_PMD_NR);
+
 		return 0;
 	}
 
Signature · 1 line
2.47.3
#2 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 314

Quoted 4 lines · Xueyuan Chen
mm/huge_memory.c +4 -0
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}

This is a rather wonky function. It's going to cause all kinds of fun if it is used like this:

	arch_make_pages_readonly(syscall_table, 1);

It's also kinda weird to have it return a bool, and not check that bool at the single call site. Some things come to mind:

1. This function needs commenting. It needs to say what it does, when architectures should override it and what their implementations should look like. It needs to be clear that this can't be used for anything really important. What should architectures do with alias mappings? Are they allowed to touch non-direct map aliases? Are they required to?
2. The return type needs to be reconsidered. Is 'bool' even acceptable? Should it just be 'void' if callers can't do anything when it fails?
3. What should the naming be? "readonly" vs "ro". Should it have a "maybe" since it's kinda optional?
4. Should this new API be folio or page-based in the first place?
5. Is mm/huge_memory.c the right place to define a generic mm function, even a stub?

reviewer
#3 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Hi Dave,

Thanks for taking the time to review.

Quoted 10 lines · Dave Hansen

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 314

Quoted 4 lines · Xueyuan Chen
mm/huge_memory.c +4 -0
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}

This is a rather wonky function. It's going to cause all kinds of fun if it is used like this:

	arch_make_pages_readonly(syscall_table, 1);

Ouch, yeah, it is ...

Quoted 9 lines

It's also kinda weird to have it return a bool, and not check that bool at the single call site. Some things come to mind:

1. This function needs commenting. It needs to say what it does, when architectures should override it and what their implementations should look like. It needs to be clear that this can't be used for anything really important. What should architectures do with alias mappings? Are they allowed to touch non-direct map aliases? Are they required to?

Agreed. Needs a real comment ...

Just meant as a best-effort direct/linear-map permission chang, nothing stronger than that. I should spell out what happens, or does not happen, to non-direct-map aliases, if anything, and make clear callers cannot treat this as a hard guarantee :D

Quoted 2 lines

2. The return type needs to be reconsidered. Is 'bool' even acceptable? Should it just be 'void' if callers can't do anything when it fails?

Maybe ignoring it is OK now, but someone may need the return value later?

Quoted 2 lines

3. What should the naming be? "readonly" vs "ro". Should it have a "maybe" since it's kinda optional?

Fair point. "make" may be overstating it a bit ...

With a return value, arch_try_make_pages_readonly() sounds about right to me. If we end up with void and pure best-effort semantics, maybe arch_maybe_make_pages_readonly() fits better :)

Quoted 1 line

4. Should this new API be folio or page-based in the first place?

For page vs folio, I was mostly following David's RFC v1 suggestion.

Current caller is a folio, sure, but the page-range helper leaves room for non-folio users later. Happy to add a simple folio wrapper if that reads better ;)

Quoted 2 lines

5. Is mm/huge_memory.c the right place to define a generic mm function, even a stub?

Ah, you're right! My bad, wrong place for a generic stub. Will move it out for RFC v3.

Thanks, Lance

reviewer
#4 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Hi,

Quoted 14 lines · Lance Yang

Hi Dave,

Thanks for taking the time to review.

Quoted 9 lines · Dave Hansen

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 314

Quoted 4 lines · Xueyuan Chen
mm/huge_memory.c +4 -0
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}

This is a rather wonky function. It's going to cause all kinds of fun if it is used like this:

Quoted 3 lines
Quoted 1 line
	arch_make_pages_readonly(syscall_table, 1);

Ouch, yeah, it is ...

We already have set_direct_map* APIs, why don't you add a new one there? set_direct_map_ro() for example.

Quoted 10 lines
Quoted 9 lines

It's also kinda weird to have it return a bool, and not check that bool at the single call site. Some things come to mind:

1. This function needs commenting. It needs to say what it does, when architectures should override it and what their implementations should look like. It needs to be clear that this can't be used for anything really important. What should architectures do with alias mappings? Are they allowed to touch non-direct map aliases? Are they required to?

Quoted 6 lines

Agreed. Needs a real comment ...

Just meant as a best-effort direct/linear-map permission chang, nothing stronger than that. I should spell out what happens, or does not happen, to non-direct-map aliases, if anything, and make clear callers cannot treat this as a hard guarantee :D

It's not only about highmem, anything that changes the direct map might fail to allocate memory when splitting larger mappings.

Quoted 5 lines
Quoted 2 lines

2. The return type needs to be reconsidered. Is 'bool' even acceptable? Should it just be 'void' if callers can't do anything when it fails?

Maybe ignoring it is OK now, but someone may need the return value later?

Quoted 8 lines
Quoted 2 lines

3. What should the naming be? "readonly" vs "ro". Should it have a "maybe" since it's kinda optional?

Fair point. "make" may be overstating it a bit ...

With a return value, arch_try_make_pages_readonly() sounds about right to me. If we end up with void and pure best-effort semantics, maybe arch_maybe_make_pages_readonly() fits better :)

Realistically, I wouldn't expect 32-bit configs to enable PERSISTENT_HUGE_ZERO_FOLIO or even THP, so naming this function to reflect 32-bit behaviour seems odd going forward.

Quoted 8 lines
Quoted 1 line

4. Should this new API be folio or page-based in the first place?

For page vs folio, I was mostly following David's RFC v1 suggestion.

Current caller is a folio, sure, but the page-range helper leaves room for non-folio users later. Happy to add a simple folio wrapper if that reads better ;)

Quoted 5 lines
Quoted 2 lines

5. Is mm/huge_memory.c the right place to define a generic mm function, even a stub?

Ah, you're right! My bad, wrong place for a generic stub. Will move it out for RFC v3.

We have include/linux/set_memory.h for such function declarations and their stubs.

Quoted 2 lines

Thanks, Lance

Signature · 2 lines
Sincerely yours,
Mike.
reviewer
#5 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 1 line · Mike Rapoport

Hi,

Hi,

Quoted 16 lines · Lance Yang
Quoted 14 lines

Hi Dave,

Thanks for taking the time to review.

Quoted 9 lines · Dave Hansen

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 314

Quoted 4 lines · Xueyuan Chen
mm/huge_memory.c +4 -0
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}

This is a rather wonky function. It's going to cause all kinds of fun if it is used like this:

Quoted 6 lines
Quoted 3 lines
Quoted 1 line
	arch_make_pages_readonly(syscall_table, 1);

Ouch, yeah, it is ...

We already have set_direct_map* APIs, why don't you add a new one there? set_direct_map_ro() for example.

Good point. I was trying to make this generic, but that's a bit too cute. This is really a direct-map thing, so adding a set_direct_map_ro() helper there makes more sense.

Quoted 13 lines
Quoted 13 lines
Quoted 10 lines

It's also kinda weird to have it return a bool, and not check that bool at the single call site. Some things come to mind:

1. This function needs commenting. It needs to say what it does, when architectures should override it and what their implementations should look like. It needs to be clear that this can't be used for anything really important. What should architectures do with alias mappings? Are they allowed to touch non-direct map aliases? Are they required to?

Agreed. Needs a real comment ...

Quoted 7 lines
Quoted 4 lines

Just meant as a best-effort direct/linear-map permission chang, nothing stronger than that. I should spell out what happens, or does not happen, to non-direct-map aliases, if anything, and make clear callers cannot treat this as a hard guarantee :D

It's not only about highmem, anything that changes the direct map might fail to allocate memory when splitting larger mappings.

Yes, exactly.

Quoted 11 lines
Quoted 11 lines
Quoted 3 lines

2. The return type needs to be reconsidered. Is 'bool' even acceptable? Should it just be 'void' if callers can't do anything when it fails?

Maybe ignoring it is OK now, but someone may need the return value later?

Quoted 2 lines

3. What should the naming be? "readonly" vs "ro". Should it have a "maybe" since it's kinda optional?

Fair point. "make" may be overstating it a bit ...

Quoted 7 lines
Quoted 3 lines

With a return value, arch_try_make_pages_readonly() sounds about right to me. If we end up with void and pure best-effort semantics, maybe arch_maybe_make_pages_readonly() fits better :)

Realistically, I wouldn't expect 32-bit configs to enable PERSISTENT_HUGE_ZERO_FOLIO or even THP, so naming this function to reflect 32-bit behaviour seems odd going forward.

Cool with that, if no one objects. No need to bake that into the name I guess. int return plus a comment should be enough, just like set_direct_map_*() family already does :)

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

4. Should this new API be folio or page-based in the first place?

For page vs folio, I was mostly following David's RFC v1 suggestion.

Current caller is a folio, sure, but the page-range helper leaves room for non-folio users later. Happy to add a simple folio wrapper if that reads better ;)

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

5. Is mm/huge_memory.c the right place to define a generic mm function, even a stub?

Ah, you're right! My bad, wrong place for a generic stub. Will move it out for RFC v3.

We have include/linux/set_memory.h for such function declarations and their stubs.

Yep, will move it there :)

Thanks, Lance

#6 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

In reply to Dave Hansen
Quoted 14 lines · Dave Hansen

mm/huge_memory.c static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink, · line 314

Quoted 4 lines · Xueyuan Chen
mm/huge_memory.c +4 -0
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}

This is a rather wonky function. It's going to cause all kinds of fun if it is used like this:

	arch_make_pages_readonly(syscall_table, 1);

It's also kinda weird to have it return a bool, and not check that bool at the single call site. Some things come to mind:

Quoted 6 lines

1. This function needs commenting. It needs to say what it does, when architectures should override it and what their implementations should look like. It needs to be clear that this can't be used for anything really important. What should architectures do with alias mappings? Are they allowed to touch non-direct map aliases? Are they required to?

Yes, kerneldoc please.

Quoted 4 lines

2. The return type needs to be reconsidered. Is 'bool' even acceptable? Should it just be 'void' if callers can't do anything when it fails?
3. What should the naming be? "readonly" vs "ro". Should it have a "maybe" since it's kinda optional?

We're adjusting the directmap, remapping a r/w page to be r/o. I think we should be very clear about which transition we expect+support.

Also, I rather hate the "set_memory" naming scheme ... "set_direct_map" is clearer. Anyhow ...

Now we are throwing a "arch_make_pages_*" into the mix.

Should it really contain the "arch"?
Should it really contain the "make" ?

Why can't we just reuse set_memory_ro and pass address+nr_pages? (highmem check? Could that be moved in there?)

Or do we want a "change_direct_map_ro()" / "remap_direct_map_ro" interface?

Quoted 3 lines

4. Should this new API be folio or page-based in the first place?
5. Is mm/huge_memory.c the right place to define a generic mm function, even a stub?

+1

Signature · 3 lines
Cheers,

David
#7 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

On Wed, Jun 17, 2026 at 01:50:08PM +0200, David Hildenbrand (Arm) wrote:

Hi, David

Quoted 1 line

Yes, kerneldoc please.

Ack.

Quoted 9 lines

We're adjusting the directmap, remapping a r/w page to be r/o. I think we should be very clear about which transition we expect+support.

Also, I rather hate the "set_memory" naming scheme ... "set_direct_map" is clearer. Anyhow ...

Now we are throwing a "arch_make_pages_*" into the mix.

Quoted 9 lines

Should it really contain the "arch"?
Should it really contain the "make" ?

Why can't we just reuse set_memory_ro and pass address+nr_pages? (highmem check? Could that be moved in there?)

Or do we want a "change_direct_map_ro()" / "remap_direct_map_ro" interface?

How about naming it int set_direct_map_ro(struct page *page, unsigned nr)?

Quoted 5 lines
Quoted 3 lines

4. Should this new API be folio or page-based in the first place?
5. Is mm/huge_memory.c the right place to define a generic mm function, even a stub?

+1

Ack, will move it out in RFC v3.

Thanks, Xueyuan

#8 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 21 lines · Xueyuan Chen

On Wed, Jun 17, 2026 at 01:50:08PM +0200, David Hildenbrand (Arm) wrote:

Hi, David

Quoted 1 line

Yes, kerneldoc please.

Ack.

Quoted 12 lines

We're adjusting the directmap, remapping a r/w page to be r/o. I think we should be very clear about which transition we expect+support.

Also, I rather hate the "set_memory" naming scheme ... "set_direct_map" is clearer. Anyhow ...

Now we are throwing a "arch_make_pages_*" into the mix.

Should it really contain the "arch"?
Should it really contain the "make" ?

Quoted 8 lines
Quoted 6 lines

Why can't we just reuse set_memory_ro and pass address+nr_pages? (highmem check? Could that be moved in there?)

Or do we want a "change_direct_map_ro()" / "remap_direct_map_ro" interface?

How about naming it int set_direct_map_ro(struct page *page, unsigned nr)?

To distinguish it from "set_memory*" cruft, maybe best to use "remap" or "adjust" instead.

Signature · 3 lines
Cheers,

David
#9 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

On Thu, Jun 18, 2026 at 11:06:41AM +0200, David Hildenbrand (Arm) wrote:

Hi, David

[...]

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines

We're adjusting the directmap, remapping a r/w page to be r/o. I think we should be very clear about which transition we expect+support.

Also, I rather hate the "set_memory" naming scheme ... "set_direct_map" is clearer. Anyhow ...

Now we are throwing a "arch_make_pages_*" into the mix.

Should it really contain the "arch"?
Should it really contain the "make" ?

Why can't we just reuse set_memory_ro and pass address+nr_pages? (highmem check? Could that be moved in there?)

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Or do we want a "change_direct_map_ro()" / "remap_direct_map_ro" interface?

How about naming it int set_direct_map_ro(struct page *page, unsigned nr)?

To distinguish it from "set_memory*" cruft, maybe best to use "remap" or "adjust" instead.

OK, how about remap_direct_map_ro? If no objections, I will add this API to set_memory.h in the next version.

Thanks, Xueyuan

#10 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 8 lines · Xueyuan Chen

On Thu, Jun 18, 2026 at 11:06:41AM +0200, David Hildenbrand (Arm) wrote:

Hi, David

[...]

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 1 line

How about naming it int set_direct_map_ro(struct page *page, unsigned nr)?

To distinguish it from "set_memory*" cruft, maybe best to use "remap" or "adjust" instead.

OK, how about remap_direct_map_ro? If no objections, I will add this API to set_memory.h in the next version.

Best to wait for some feedback.

I do wonder whether we want to pass an address instead of a page.

https://lore.kernel.org/r/[email protected]

Wants to convert existing ones as well.

That would imply that the caller must check for highmem.

But then, we could just use existing set_memory_ro(), right?

Signature · 3 lines
Cheers,

David
#11 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

On Thu, Jun 18, 2026 at 02:36:25PM +0200, David Hildenbrand (Arm) wrote:

Hi, David

[...]

Quoted 1 line

Best to wait for some feedback.

Sure.

Quoted 10 lines

I do wonder whether we want to pass an address instead of a page.

https://lore.kernel.org/r/[email protected]

Wants to convert existing ones as well.

That would imply that the caller must check for highmem.

But then, we could just use existing set_memory_ro(), right?

Agreed. Passing an address and reusing the existing set_memory_ro() definitely makes things simpler.

However, there is an arm64 specific limitation:
currently, the set_memory_r* api on arm64 only support the vmap region and do not handle linear map addresses.

If we go this route, should I extend the arm64 set_memory_r* implementation in the next version? The plan would be to make it check for the bblm2 feature and modify the linear map PTEs accordingly. What do you think?

Thanks,
Xueyuan

#12 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 24 lines · Xueyuan Chen

On Thu, Jun 18, 2026 at 02:36:25PM +0200, David Hildenbrand (Arm) wrote:

Hi, David

[...]

Quoted 1 line

Best to wait for some feedback.

Sure.

Quoted 10 lines

I do wonder whether we want to pass an address instead of a page.

https://lore.kernel.org/r/[email protected]

Wants to convert existing ones as well.

That would imply that the caller must check for highmem.

But then, we could just use existing set_memory_ro(), right?

Agreed. Passing an address and reusing the existing set_memory_ro() definitely makes things simpler.

Quoted 8 lines

However, there is an arm64 specific limitation:
currently, the set_memory_r* api on arm64 only support the vmap region and do not handle linear map addresses.

If we go this route, should I extend the arm64 set_memory_r* implementation in the next version? The plan would be to make it check for the bblm2 feature and modify the linear map PTEs accordingly. What do you think?

Good point! It's not really clear on which ranges set_memory*() is supposed to work ...

arm64 only works on vmalloc/vmap, x86 and riscv on ordinary directmap ... what a mess.

Having a new direct-map specific function with clear semantics might indeed avoid even messing with that.

So, yeah, given that we have

set_direct_map_invalid_noflush
set_direct_map_default_noflush
set_direct_map_valid_noflush

Let's add a

	set_direct_map_ro()

Or (my preference)

change_direct_map_ro()

But given the existing naming scheme ... maybe just set_direct_map_ro() and we'll clean this up another day.

Now, should there also be a "_noflush" in there, or who is supposed to flush the TLB (or don't we flush at all, because it's used early during boot so far)?

In any case, for this function we should add excessive documentation and define clear semantics.

Signature · 3 lines
Cheers,

David
#13 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 31 lines · David Hildenbrand (Arm)
Quoted 22 lines · Xueyuan Chen

On Thu, Jun 18, 2026 at 02:36:25PM +0200, David Hildenbrand (Arm) wrote:

Hi, David

[...]

Quoted 1 line

Best to wait for some feedback.

Sure.

Quoted 10 lines

I do wonder whether we want to pass an address instead of a page.

https://lore.kernel.org/r/[email protected]

Wants to convert existing ones as well.

That would imply that the caller must check for highmem.

But then, we could just use existing set_memory_ro(), right?

Agreed. Passing an address and reusing the existing set_memory_ro() definitely makes things simpler.

Quoted 8 lines

However, there is an arm64 specific limitation:
currently, the set_memory_r* api on arm64 only support the vmap region and do not handle linear map addresses.

If we go this route, should I extend the arm64 set_memory_r* implementation in the next version? The plan would be to make it check for the bblm2 feature and modify the linear map PTEs accordingly. What do you think?

Quoted 5 lines

Good point! It's not really clear on which ranges set_memory*() is supposed to work ...

arm64 only works on vmalloc/vmap, x86 and riscv on ordinary directmap ... what a mess.

arm64 version works on vmalloc/vmap *and* the linear map alias if rodata is on (a.k.a. full before). It should be not hard to make it work for linear map address (w/o vmalloc/vmap alias). I think we can just follow riscv's implementation by doing:

if (vmalloc or vmap address)
    ...
else if (linear map address)
    ...

Thanks,
Yang

Quoted 28 lines

Having a new direct-map specific function with clear semantics might indeed avoid even messing with that.

So, yeah, given that we have

set_direct_map_invalid_noflush
set_direct_map_default_noflush
set_direct_map_valid_noflush

Let's add a

	set_direct_map_ro()

Or (my preference)

change_direct_map_ro()

But given the existing naming scheme ... maybe just set_direct_map_ro() and we'll clean this up another day.

Now, should there also be a "_noflush" in there, or who is supposed to flush the TLB (or don't we flush at all, because it's used early during boot so far)?

In any case, for this function we should add excessive documentation and define clear semantics.

#14 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

In reply to David Hildenbrand (Arm)

On Fri, Jun 19, 2026 at 01:09:09PM +0200, David Hildenbrand (Arm) wrote:

Hi, David

[...]

Quoted 21 lines

Having a new direct-map specific function with clear semantics might indeed avoid even messing with that.

So, yeah, given that we have

set_direct_map_invalid_noflush
set_direct_map_default_noflush
set_direct_map_valid_noflush

Let's add a

	set_direct_map_ro()

Or (my preference)

change_direct_map_ro()

But given the existing naming scheme ... maybe just set_direct_map_ro() and we'll clean this up another day.

Quoted 6 lines

Now, should there also be a "_noflush" in there, or who is supposed to flush the TLB (or don't we flush at all, because it's used early during boot so far)?

In any case, for this function we should add excessive documentation and define clear semantics.

After talking with Lance, we're leaning toward a direct-map helper:

set_direct_map_ro_noflush(const void *addr, unsigned long nr_pages)

Contract should be pretty explicit: direct/linear map only, no alias handling, no TLB flush. If a caller needs stale entries gone, it has to flush itself.

Huge zero folio only uses this early during boot so far, so noflush should be OK. Will document that.

Thanks,
Xueyuan

maintainer
#15 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

In reply to Xueyuan Chen
Quoted 34 lines · On Tue, 9 Jun 2026 22:37:59 +0800 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 persistent huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting the shared zero contents.

Add arch_make_pages_readonly() so mm code can request read-only direct-map protection for a page range. Direct-map protection is architecture-specific, so the generic weak implementation does nothing.

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/

...

mm/huge_memory.c +5 -0
@@ -308,6 +308,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 	return 0;
 }
 
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}
+
 static struct shrinker *huge_zero_folio_shrinker;
 
 #ifdef CONFIG_SYSFS
@@ -982,8 +987,14 @@ static int __init thp_shrinker_init(void)
 		 * that get_huge_zero_folio() will most likely not fail as

mm/huge_memory.c static int __init thp_shrinker_init(void) · line 996

Quoted 10 lines
mm/huge_memory.c +6 -1
 		 * 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;
+		}
+
+		arch_make_pages_readonly(folio_page(huge_zero_folio, 0),
+					HPAGE_PMD_NR);

Can it simply pass the folio?

reviewer
#16 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 33 lines · Andrew Morton
Quoted 31 lines · On Tue, 9 Jun 2026 22:37:59 +0800 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 persistent huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting the shared zero contents.

Add arch_make_pages_readonly() so mm code can request read-only direct-map protection for a page range. Direct-map protection is architecture-specific, so the generic weak implementation does nothing.

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/

...

mm/huge_memory.c +5 -0
@@ -308,6 +308,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 	return 0;
 }
 
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}
+
 static struct shrinker *huge_zero_folio_shrinker;
 
Quoted 15 lines

mm/huge_memory.c static int __init thp_shrinker_init(void) · line 996

Quoted 13 lines
mm/huge_memory.c +6 -1
 #ifdef CONFIG_SYSFS
@@ -982,8 +987,14 @@ 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;
+		}
+
+		arch_make_pages_readonly(folio_page(huge_zero_folio, 0),
+					HPAGE_PMD_NR);

Can it simply pass the folio?

Right, this came from the RFC v1 discussion[1]. David preferred a page- range helper for possible future non-folio callers, not something folio- only.

Of course, we could also add a folio wrapper on top of that if needed :)

[1] https://lore.kernel.org/linux-mm/[email protected]/

Thanks, Lance

#17 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 51 lines · Lance Yang
Quoted 33 lines · Andrew Morton
Quoted 31 lines · On Tue, 9 Jun 2026 22:37:59 +0800 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 persistent huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting the shared zero contents.

Add arch_make_pages_readonly() so mm code can request read-only direct-map protection for a page range. Direct-map protection is architecture-specific, so the generic weak implementation does nothing.

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/

...

mm/huge_memory.c +5 -0
@@ -308,6 +308,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 	return 0;
 }
 
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}
+
 static struct shrinker *huge_zero_folio_shrinker;
 
Quoted 15 lines

mm/huge_memory.c static int __init thp_shrinker_init(void) · line 996

Quoted 13 lines
mm/huge_memory.c +6 -1
 #ifdef CONFIG_SYSFS
@@ -982,8 +987,14 @@ 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;
+		}
+
+		arch_make_pages_readonly(folio_page(huge_zero_folio, 0),
+					HPAGE_PMD_NR);

Can it simply pass the folio?

Quoted 5 lines

Right, this came from the RFC v1 discussion[1]. David preferred a page- range helper for possible future non-folio callers, not something folio- only.

Of course, we could also add a folio wrapper on top of that if needed :)

Best to document that as part of the patch description: we don't really expect to have a lot of read-only folios in the near future (zero page is rather special; maybe it won't even be a folio in the future).

Signature · 3 lines
Cheers,

David
reviewer
#18 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 56 lines · David Hildenbrand (Arm)
Quoted 55 lines · Lance Yang
Quoted 33 lines · Andrew Morton
Quoted 31 lines · On Tue, 9 Jun 2026 22:37:59 +0800 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 persistent huge zero folio is read-only in the direct map, such writes fault instead of silently corrupting the shared zero contents.

Add arch_make_pages_readonly() so mm code can request read-only direct-map protection for a page range. Direct-map protection is architecture-specific, so the generic weak implementation does nothing.

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/

...

mm/huge_memory.c +5 -0
@@ -308,6 +308,11 @@ static unsigned long shrink_huge_zero_folio_scan(struct shrinker *shrink,
 	return 0;
 }
 
+bool __weak arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	return false;
+}
+
 static struct shrinker *huge_zero_folio_shrinker;
 
Quoted 15 lines

mm/huge_memory.c static int __init thp_shrinker_init(void) · line 996

Quoted 13 lines
mm/huge_memory.c +6 -1
 #ifdef CONFIG_SYSFS
@@ -982,8 +987,14 @@ 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;
+		}
+
+		arch_make_pages_readonly(folio_page(huge_zero_folio, 0),
+					HPAGE_PMD_NR);

Can it simply pass the folio?

Right, this came from the RFC v1 discussion[1]. David preferred a page- range helper for possible future non-folio callers, not something folio- only.

Quoted 5 lines
Quoted 1 line

Of course, we could also add a folio wrapper on top of that if needed :)

Best to document that as part of the patch description: we don't really expect to have a lot of read-only folios in the near future (zero page is rather special; maybe it won't even be a folio in the future).

Ah, good to know, thanks. Will spell that out in RFC v3.

Maybe something like this?

The huge zero page is pretty special case, and maybe it won't even be a folio in the future. Since read-only folios are unlikely to become a common thing, a page-range helper is the cleaner fit.

#19 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 15 lines · Lance Yang
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Lance Yang

Right, this came from the RFC v1 discussion[1]. David preferred a page- range helper for possible future non-folio callers, not something folio- only.

Quoted 5 lines
Quoted 1 line

Of course, we could also add a folio wrapper on top of that if needed :)

Best to document that as part of the patch description: we don't really expect to have a lot of read-only folios in the near future (zero page is rather special; maybe it won't even be a folio in the future).

Quoted 7 lines

Ah, good to know, thanks. Will spell that out in RFC v3.

Maybe something like this?

The huge zero page is pretty special case, and maybe it won't even be a folio in the future. Since read-only folios are unlikely to become a common thing, a page-range helper is the cleaner fit.

Right. And if read-only folios in FSes become a real thing, we can always add infrastructure for that.

Signature · 3 lines
Cheers,

David
reviewer
#20 lore raw

Re: [RFC PATCH v2 1/3] mm/huge_memory: make persistent huge zero folio read-only

Quoted 18 lines · David Hildenbrand (Arm)
Quoted 17 lines · Lance Yang
Quoted 7 lines · David Hildenbrand (Arm)
Quoted 6 lines · Lance Yang

Right, this came from the RFC v1 discussion[1]. David preferred a page- range helper for possible future non-folio callers, not something folio- only.

Quoted 5 lines
Quoted 1 line

Of course, we could also add a folio wrapper on top of that if needed :)

Best to document that as part of the patch description: we don't really expect to have a lot of read-only folios in the near future (zero page is rather special; maybe it won't even be a folio in the future).

Ah, good to know, thanks. Will spell that out in RFC v3.

Quoted 8 lines
Quoted 5 lines

Maybe something like this?

The huge zero page is pretty special case, and maybe it won't even be a folio in the future. Since read-only folios are unlikely to become a common thing, a page-range helper is the cleaner fit.

Right. And if read-only folios in FSes become a real thing, we can always add infrastructure for that.

Got it, that draws the line nicely :P

#21 lore raw

RFC PATCH v2 2/3 arm64/mm: make pages read-only in the linear map

In reply to Xueyuan Chen

Implement arch_make_pages_readonly() for arm64. Make the corresponding linear-map range read-only so unexpected writes fault instead of corrupting shared contents.

Respect can_set_direct_map() before touching the linear map. If arm64 cannot safely update the linear map at page granularity, leave the mapping unchanged.

---
 arch/arm64/mm/pageattr.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
arch/arm64/mm/pageattr.c +13 -0
index ce035e1b4eaf..da97ec7d5195 100644
@@ -3,6 +3,7 @@
  * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  */
 #include <linux/kernel.h>
+#include <linux/init.h>
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/mem_encrypt.h>
@@ -147,6 +148,18 @@ static int __change_memory_common(unsigned long start, unsigned long size,
 	return ret;
 }
 
+bool arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	unsigned long addr = (unsigned long)page_address(page);
+
+	if (!can_set_direct_map())
+		return false;
+
+	return !__change_memory_common(addr, PAGE_SIZE * nr_pages,
+				       __pgprot(PTE_RDONLY),
+				       __pgprot(PTE_WRITE));
+}
+
 static int change_memory_common(unsigned long addr, int numpages,
 				pgprot_t set_mask, pgprot_t clear_mask)
 {
Signature · 1 line
2.47.3
#22 lore raw

RFC PATCH v2 3/3 x86/mm: make pages read-only in the direct map

In reply to Xueyuan Chen

Implement arch_make_pages_readonly() for x86. Make the corresponding direct-map range read-only so unexpected writes fault instead of corrupting shared contents.

Reject highmem pages because they have no permanent direct-map address. Treat the set_memory_ro() update as best effort. If it fails, leave the mapping unchanged.

---
 arch/x86/mm/init.c | 9 +++++++++
 1 file changed, 9 insertions(+)
arch/x86/mm/init.c +9 -0
index fb67217fddcd..ff0a7003eaeb 100644
@@ -3,6 +3,7 @@
 #include <linux/ioport.h>
 #include <linux/swap.h>
 #include <linux/memblock.h>
+#include <linux/mm.h>
 #include <linux/swapfile.h>
 #include <linux/swapops.h>
 #include <linux/kmemleak.h>
@@ -38,6 +39,14 @@
 
 #include "mm_internal.h"
 
+bool arch_make_pages_readonly(struct page *page, int nr_pages)
+{
+	if (PageHighMem(page))
+		return false;
+
+	return !set_memory_ro((unsigned long)page_address(page), nr_pages);
+}
+
 /*
  * Tables translating between page_cache_type_t and pte encoding.
  *
Signature · 1 line
2.47.3