[RFC PATCH 3/3] x86/mm: make huge zero folio read-only in direct map
From: Xueyuan chen
Date: Tue May 26 2026 - 23:57:57 EST
From: Xueyuan Chen <xueyuan.chen21@xxxxxxxxx>
Implement arch_make_huge_zero_folio_readonly() for x86-64. Once allocated,
try to make the folio read-only in the direct map so unexpected writes
fault instead of corrupting shared zero contents.
The set_memory_ro() update is best effort: if it fails, generic THP keeps
using the writable persistent huge zero folio.
Co-developed-by: Lance Yang <lance.yang@xxxxxxxxx>
Signed-off-by: Lance Yang <lance.yang@xxxxxxxxx>
Signed-off-by: Xueyuan Chen <xueyuan.chen21@xxxxxxxxx>
---
arch/x86/Kconfig | 1 +
arch/x86/mm/init.c | 11 +++++++++++
2 files changed, 12 insertions(+)
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index f3f7cb01d69d..81f9478d2803 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -24,6 +24,7 @@ config X86_64
def_bool y
depends on 64BIT
# Options that are inherently 64-bit kernel only:
+ select ARCH_HAS_READONLY_HUGE_ZERO_FOLIO
select ARCH_HAS_GIGANTIC_PAGE
select ARCH_SUPPORTS_MSEAL_SYSTEM_MAPPINGS
select ARCH_SUPPORTS_INT128 if CC_HAS_INT128
diff --git a/arch/x86/mm/init.c b/arch/x86/mm/init.c
index fb67217fddcd..ef721aa2ff0c 100644
--- a/arch/x86/mm/init.c
+++ b/arch/x86/mm/init.c
@@ -3,6 +3,8 @@
#include <linux/ioport.h>
#include <linux/swap.h>
#include <linux/memblock.h>
+#include <linux/mm.h>
+#include <linux/huge_mm.h>
#include <linux/swapfile.h>
#include <linux/swapops.h>
#include <linux/kmemleak.h>
@@ -38,6 +40,15 @@
#include "mm_internal.h"
+#ifdef CONFIG_READONLY_HUGE_ZERO_FOLIO
+bool __init arch_make_huge_zero_folio_readonly(struct folio *folio)
+{
+ unsigned long addr = (unsigned long)folio_address(folio);
+
+ return !set_memory_ro(addr, HPAGE_PMD_NR);
+}
+#endif
+
/*
* Tables translating between page_cache_type_t and pte encoding.
*
--
2.47.3