freenode
Kernel & Low-Level

Linux RFC would replace seven set_memory implementations with one core

Mike Rapoport's series, based on x86's page-attribute code, aims to stop each architecture fixing the same kernel mapping bugs differently.

Mike Rapoport of Microsoft has posted a second request for comments that would collapse the kernel's seven independent implementations of set_memory and set_direct_map into one generic core in the memory-management code, built on x86's change-page-attribute logic.

The APIs change protections on kernel mappings: making text read-only, marking ranges non-executable, and updating the direct map that aliases physical memory. Demand for that machinery is rising. Work on removing the direct map for guest_memfd, and on address-space isolation's direct-map management, both need reliable kernel page-table updates. A use-after-free fix earlier this year had to be applied in a subtly different way on each architecture, because there was no single place to correct the logic.

Rapoport argues x86 is the right base because it is the most versatile and most battle-tested of the implementations. It keeps large mappings when it can, splits them only when it must, updates the direct-map alias, serializes requests against itself, and checks the result. "The others are catching up one bug at a time," he wrote.

A shared core would also let protection updates become all-or-nothing. Today a request that fails halfway leaves the pages it already processed changed, and the caller has no real way to recover. The same core could give debug_pagealloc and KFENCE one atomic mode on every architecture, and later let arm64 (where BBML3 is available) and s390 collapse large mappings again and use a read-only-execute cache for executable memory.

The conversion is not a pure rename. RISC-V's own walk splits large pages it does not need to change, including the linear-map alias of a vmalloc area, page by page; it writes empty entries; it can dereference a null page array for non-RAM mappings such as ioremap; and it flushes the entire TLB even when the caller asked for no flush. s390 splits every large page a request touches, even when one protection covers the whole page, and leaves the identity-map alias of the kernel image writable after the image itself is write-protected. arm64 walks the tables twice, always splitting both ends of a request down to base pages, and still refuses anything but a single vmalloc area, a leftover from when a live mapping could not be split at all. The series also drops unused combined operations, such as RISC-V's combined writable-and-non-executable call, whose only user was freeing init memory.

Architectures would keep hooks for the page-table writes themselves: s390 needs its own table allocator because early boot and its CMMA state cannot use the generic page allocator, and arm64 needs a way to batch barriers in lazy MMU mode without the core disabling preemption. The series is an RFC, not a merge request, posted as one conversion so reviewers can see whether the consolidation leaves conceptual gaps.