[PATCH 00/12] mm: make userland page table freeing RCU-safe
From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org>
Date: 2026-09-01 11:05:11
Also in:
linux-alpha, linux-arch, linux-m68k, linux-mips, linux-mm, linux-riscv, linux-s390, linux-sh, linux-um, lkml, loongarch, sparclinux
The majority of architectures in the kernel defer page table freeing until
an RCU grace period has elapsed, this series converts all remaining
architectures to do so too and eliminates CONFIG_MMU_GATHER_RCU_TABLE_FREE
altogether.
This is important because it enables safe lockless page table walking under
RCU alone.
Doing so allows for reduced lock contention, avoids lock ordering concerns
and enables fast, efficient and correct page table walking as a result.
Additionally it removes a bunch of code and architecture-specific behaviour
which is always a beneficial thing to do.
There has been much recent work on this:
* In 2023 Hugh Dickins RCU-deferred khugepaged page table retraction in
commit 13cf577e6b66 ("mm/pgtable: add pte_free_defer() for pgtable as
page").
* Qi Zheng has done most of the work that made this possible starting with
the critical commit 718b13861d22 ("x86: mm: free page table pages by RCU
instead of semi RCU").
* Qi then went on to convert a large number of architectures in commit
e3ecf7c7d082 ("mm: pgtable: convert some architectures to use
tlb_remove_ptdesc()"), commit 44b079583f7d ("alpha: mm: enable
MMU_GATHER_RCU_TABLE_FREE") and the series to which it belongs.
* Qi then introduced the important CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE
option in commit 086498aed3f6 ("mm: convert __HAVE_ARCH_TLB_REMOVE_TABLE
to CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE config").
* Finally, and critically, Lance Yang then converted the batch allocation
fallback case to be RCU-safe in commit 1fb3d8c20bfa ("mm/mmu_gather:
replace IPI with synchronize_rcu() when batch allocation fails").
The work I do here is only possible due to the work Hugh, Qi, Lance and
others have done previously.
An initial task this series addresses is to zap deposited page tables after
an RCU grace period. Not doing so is currently safe, but for page table
walkers relying on RCU alone, it would not be.
The changes are largely mechanical - the majority of arches already have
the machinery required to support CONFIG_MMU_GATHER_RCU_TABLE_FREE and
simply needed configuration changes or small implementation changes to
switch over.
However some arches required extra attention - sh-X2, m68k-motorola and
sparc32.
sh-X2 allocates PMDs from the slab allocator and PTEs as normal. Therefore
CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE is set to customise page table freeing
and the LSB is used to encode which page table level is used, with
__tlb_remove_table() doing the right thing depending on this.
This pattern is repeated for m68k-motorola and sparc32 to account for
different page table levels. In each case, the page tables are aligned such
that sufficient bits are available in each case for encoding this
information.
m68k-motorola required the biggest change - since RCU page table freeing
uses call_rcu(), this means page table freeing can arise from softirq
context.
This was fixed with an IRQ-safe spin lock used in both get_pointer_table()
and free_pointer_table().
As part of this change, the logic for allocation of a new pointer table was
separated out into add_pointer_table() to make the locking more obviously
correct.
Finally, sparc32 was similar to m68k-motorola in that locking was required,
however this was already implemented via a spinlock, and only had to be
updated to be IRQ-safe.
Additionally, the nocache pool's bit_map lock was updated to be IRQ-safe
for softirq frees.
Separately, the PTE path can't take mm->page_table_lock from softirq (no mm
there), which is fine because the page reference count transitions are
atomic and fully ordered.
The series finally removes CONFIG_MMU_GATHER_RCU_TABLE_FREE and all related
configurations and code that supported !CONFIG_MMU_GATHER_RCU_TABLE_FREE.
As a result, page table walks can now be performed safely under RCU without
any risk of page tables being freed underneath a walker.
However, this is the only guarantee that this work provides - page table
walkers must still ensure that page table entries are as expected
throughout.
All changes have been build tested. As most of the conversions are simply
utilising existing mechanics that are known to work, this suffices for most
cases.
However those arches where significant changes have been made -
m68k-motorola, sparc32 and sh-X2 - have been tested further.
For each of these a boot test and stress test has been performed - fork 400
children, each mmap()'ing 2 MiB and touching every page then partially
munmap()'ing then exiting to trigger as much page table freeing as
possible.
All were found to be working correctly.
Note that sparc32 LEON SMP is not emulated, Andreas - do you have a means
of testing this?
Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org>
---
Lorenzo Stoakes (ARM) (12):
mm/huge_memory: zap deposited page tables after an RCU grace period
mm: enable MMU_GATHER_RCU_TABLE_FREE for most 2-level architectures
mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU riscv
mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU arm
mm: enable MMU_GATHER_RCU_TABLE_FREE for arc, microblaze, xtensa
mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc64
mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-coldfire
mm: enable MMU_GATHER_RCU_TABLE_FREE for sh-X2
mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-motorola
mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc32
mm: make userland page table freeing RCU-safe
mm: change the contract for free_pgtables(), update docs
Documentation/mm/process_addrs.rst | 6 ++
arch/Kconfig | 8 --
arch/alpha/Kconfig | 1 -
arch/arc/include/asm/pgalloc.h | 6 +-
arch/arm/Kconfig | 1 -
arch/arm64/Kconfig | 1 -
arch/loongarch/Kconfig | 1 -
arch/m68k/Kconfig | 1 +
arch/m68k/include/asm/mcf_pgalloc.h | 5 +-
arch/m68k/include/asm/motorola_pgalloc.h | 9 ++-
arch/m68k/mm/motorola.c | 121 ++++++++++++++++++++-----------
arch/microblaze/include/asm/pgalloc.h | 2 +-
arch/mips/Kconfig | 1 -
arch/parisc/Kconfig | 1 -
arch/powerpc/Kconfig | 1 -
arch/riscv/Kconfig | 1 -
arch/s390/Kconfig | 1 -
arch/sh/Kconfig | 1 +
arch/sh/include/asm/pgalloc.h | 6 +-
arch/sh/mm/pgtable.c | 20 +++++
arch/sparc/Kconfig | 4 +-
arch/sparc/include/asm/pgalloc_32.h | 7 +-
arch/sparc/include/asm/pgalloc_64.h | 8 --
arch/sparc/include/asm/tlb_64.h | 2 -
arch/sparc/lib/bitext.c | 14 ++--
arch/sparc/mm/srmmu.c | 26 ++++++-
arch/um/Kconfig | 1 -
arch/x86/Kconfig | 1 -
arch/xtensa/include/asm/tlb.h | 2 +-
include/asm-generic/tlb.h | 66 +++--------------
mm/Kconfig | 2 +-
mm/gup.c | 5 +-
mm/huge_memory.c | 2 +-
mm/mmu_gather.c | 30 ++------
mm/pgtable-generic.c | 18 ++++-
35 files changed, 194 insertions(+), 188 deletions(-)
---
base-commit: 88297631d4d42f6004cb39c0ba3da7d2d10a616f
change-id: 20260831-rcu-pagetable-freeing-84b6be830e20
Cheers,
--
Lorenzo Stoakes (ARM) [off-list ref]