[PATCH 0/2] sh: mm: fix hugetlb on SH-4
From: Karl Mehltretter
Date: Sat Sep 26 2026 - 14:39:40 EST
Muchun Song noted in the review of the Arm hugetlb alignment fix [1]
that SuperH has the same arch_get_unmapped_area() gap. Reproducing it
in QEMU turned up a second, older bug that corrupts memory once the
mappings are aligned.
Patch 1 fixes the page size in hugetlb PTEs. pte_mkhuge() ORs the huge
size into the base page size bits, so 64 KiB huge pages over 4 KiB
pages are loaded into the TLB as 1 MiB pages. User writes then land in
unrelated kernel memory. This dates back to the start of git history.
Patch 2 aligns hugetlb mappings to the huge page size. Without it
mmap(MAP_HUGETLB) can return an address that is not aligned to the huge
page size, and process exit hits the BUG_ON() in
__unmap_hugepage_range(). This is a 6.13
regression.
Patch 1 goes first because patch 2 alone makes the corruption reachable
through a plain mmap(). Both are marked for stable.
Tested on QEMU r2d (SH7751R, rts7751r2dplus_defconfig plus HUGETLBFS
with 64 KiB huge pages, gcc 15.2) on top of fddfc3ec3179. A test init
maps two huge pages with the default and legacy layouts and with
MAP_FIXED, writes and reads back every 4 KiB, mprotect()s the mapping
read-only and back, and checks a COW write in a forked child:
kernel MAP_FIXED default/legacy mmap
base memory corrupt misaligned, BUG at mm/hugetlb.c:5215
patch 1 pass misaligned, BUG
patches 1+2 pass pass
SH-X2 (X2TLB) is only build tested (r7785rp_defconfig with 64 KiB huge
pages).
QEMU's SH-4 TLB emulation flushes only the first 4 KiB of a 64 KiB or
1 MiB entry when the entry is dropped [2]. Keep that in mind when
testing hugetlb on QEMU.
[1] https://lore.kernel.org/r/5AB6FE85-4CB9-41EF-A700-BC5E071F29F5@xxxxxxxxx
[2] https://gitlab.com/qemu-project/qemu/-/issues/4598
Karl Mehltretter (2):
sh: mm: replace the page size bits in pte_mkhuge()
sh: mm: align hugetlb mappings to the huge page size
arch/sh/include/asm/pgtable_32.h | 7 +++++--
arch/sh/mm/mmap.c | 11 +++++++++--
2 files changed, 14 insertions(+), 4 deletions(-)
base-commit: fddfc3ec31799a932bb92f1b8a84cb3d1f963be9
--
2.39.5 (Apple Git-154)