pte_mkhuge() ORs _PAGE_SZHUGE into a PTE that already carries the base page size from _PAGE_FLAGS_HARD, which every PAGE_* protection includes. The PTEL size field is an encoding, not a set of flags: on SH-4 with 4 KiB pages and 64 KiB huge pages, _PAGE_SZ0 | _PAGE_SZ1 selects a 1 MiB page. Every hugetlb mapping is therefore loaded into the UTLB as a 1 MiB page whose physical base is the huge page's address rounded down to 1 MiB, so user accesses land in unrelated kernel memory. In a QEMU r2d guest, after touching a MAP_HUGETLB mapping at 0x30000000, QEMU's "info tlb" shows vpn=c0000 ppn=32e00 sz=3 size=1048576 and writing the two huge pages overwrites kernel data, for example page tables ("bad pgd 5a5a5a5a" on munmap) or a struct file: Fault in unaligned fixup: 0000 [#1] PC is at file_tty_write.isra.0+0x24/0x220 R1 : 5a5a5a5a The SH-X2 extended size field has the same problem: a 4 KiB base page (ESZ0) combined with 64 KiB huge pages (ESZ2) encodes 256 KiB. Only the configurations whose huge size encoding happens to contain the base encoding, such as 1 MiB over 4 KiB on SH-4, have worked. Clear the size field before setting the huge page size. All hugetlb PTE constructors, including huge_pte_modify() on mprotect() and the fork/COW paths, go through arch_make_huge_pte() and so pte_mkhuge(). With this change the same entry is loaded as vpn=c0000 ppn=32e00 sz=2 size=65536 and the mappings read back correctly after mprotect() and fork()/COW. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Cc: stable@vger.kernel.org Assisted-by: LLM Signed-off-by: Karl Mehltretter --- arch/sh/include/asm/pgtable_32.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/sh/include/asm/pgtable_32.h b/arch/sh/include/asm/pgtable_32.h index 5f51af18997b..1e6821a23eac 100644 --- a/arch/sh/include/asm/pgtable_32.h +++ b/arch/sh/include/asm/pgtable_32.h @@ -145,6 +145,8 @@ static inline unsigned long copy_ptea_attributes(unsigned long x) # elif defined(CONFIG_HUGETLB_PAGE_SIZE_64MB) # define _PAGE_SZHUGE (_PAGE_EXT_ESZ2 | _PAGE_EXT_ESZ3) # endif +# define _PAGE_SZHUGE_MASK (_PAGE_EXT_ESZ0 | _PAGE_EXT_ESZ1 | \ + _PAGE_EXT_ESZ2 | _PAGE_EXT_ESZ3) # define _PAGE_WIRED (_PAGE_EXT(_PAGE_EXT_WIRED)) #else # if defined(CONFIG_HUGETLB_PAGE_SIZE_64K) @@ -152,6 +154,7 @@ static inline unsigned long copy_ptea_attributes(unsigned long x) # elif defined(CONFIG_HUGETLB_PAGE_SIZE_1MB) # define _PAGE_SZHUGE (_PAGE_SZ0 | _PAGE_SZ1) # endif +# define _PAGE_SZHUGE_MASK (_PAGE_SZ_MASK) # define _PAGE_WIRED (0) #endif @@ -359,11 +362,11 @@ static inline pte_t pte_##fn(pte_t pte) { pte.pte_##h op; return pte; } */ PTE_BIT_FUNC(high, wrprotect, &= ~(_PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE)); PTE_BIT_FUNC(high, mkwrite_novma, |= _PAGE_EXT_USER_WRITE | _PAGE_EXT_KERN_WRITE); -PTE_BIT_FUNC(high, mkhuge, |= _PAGE_SZHUGE); +PTE_BIT_FUNC(high, mkhuge, = (pte.pte_high & ~_PAGE_SZHUGE_MASK) | _PAGE_SZHUGE); #else PTE_BIT_FUNC(low, wrprotect, &= ~_PAGE_RW); PTE_BIT_FUNC(low, mkwrite_novma, |= _PAGE_RW); -PTE_BIT_FUNC(low, mkhuge, |= _PAGE_SZHUGE); +PTE_BIT_FUNC(low, mkhuge, = (pte.pte_low & ~_PAGE_SZHUGE_MASK) | _PAGE_SZHUGE); #endif PTE_BIT_FUNC(low, mkclean, &= ~_PAGE_DIRTY); base-commit: fddfc3ec31799a932bb92f1b8a84cb3d1f963be9 -- 2.39.5 (Apple Git-154)