| Message ID | 20260911-rcu-pagetable-freeing-v3-0-7b8c86103821@kernel.org |
|---|---|
| Headers | show |
| Series | mm: make userland page table freeing RCU-safe | expand |
On Fri, 11 Sep 2026 20:36:47 +0100 "Lorenzo Stoakes (ARM)" <ljs@kernel.org> wrote: > 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. Thanks, I've updated mm.git's mm-unstable branch to this version. > v3: > * Added Kiryl's tag to series (thanks! :) > * Corrected commit message, comment indentation in 11/12 as per Kiryl. > * Corrected 12/12 documentation as per Kiryl. Here's how v3 altered mm.git: Documentation/mm/process_addrs.rst | 4 ++-- include/asm-generic/tlb.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) --- a/Documentation/mm/process_addrs.rst~b +++ a/Documentation/mm/process_addrs.rst @@ -541,8 +541,8 @@ We establish basic locking rules when in after an RCU grace period has elapsed. However, any entry found must be revalidated after the page table lock is taken (such as the :c:func:`!pmd_same` recheck performed by :c:func:`!pte_offset_map_lock`) - before it is acted upon. Changing an entry always requires the page table - lock. + before it is acted upon. Changing an entry requires the page table + lock and one of the locks that excludes teardown (mmap or VMA lock). * Reads from and writes to page table entries must be *appropriately* atomic. See the section on atomicity below for details. * Populating previously empty entries requires that the mmap or VMA locks are --- a/include/asm-generic/tlb.h~b +++ a/include/asm-generic/tlb.h @@ -151,8 +151,8 @@ * Page directories (__p*_free_tlb()) are always freed via tlb_remove_table(), * after an RCU grace period (see mm/mmu_gather.c). * - * This serialises against software page-table walkers, including architectures - * which do not use IPIs for remote TLB invalidates. + * This serialises against software page-table walkers, including architectures + * which do not use IPIs for remote TLB invalidates. * * An architecture is expected to provide __tlb_remove_table() (see * HAVE_ARCH_TLB_REMOVE_TABLE) or use the generic __tlb_remove_table(), which
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? v3: * Added Kiryl's tag to series (thanks! :) * Corrected commit message, comment indentation in 11/12 as per Kiryl. * Corrected 12/12 documentation as per Kiryl. v2: * Updated commit message to reflect CONFIG_MMU dependency for xtensa as per Suren. * Updated 12/12 to correctly describe the new contract re: readers/writes as per Kiryl. Also fixed up typos there as per Kiryl, Suren and slightly reworded for clarity, and updated commit message to reflect. * Updated 11/12 to reflect the fact the change is for userland only as per Kiryl, also added a note about write locks similarly to 12/12. * Fixed bug as reported by sashiko for the sparc32 change (10/12) - the refcounts are insufficient on their own, so replace the page_table_lock altogether with an IRQ-safe spinlock. https://lore.kernel.org/r/20260908-rcu-pagetable-freeing-v2-0-1f60b64e878e@kernel.org v1: https://patch.msgid.link/20260901-rcu-pagetable-freeing-v1-0-5456a81c8212@kernel.org 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 | 32 ++++++-- 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 | 15 +++- 35 files changed, 194 insertions(+), 191 deletions(-) --- base-commit: 88297631d4d42f6004cb39c0ba3da7d2d10a616f change-id: 20260831-rcu-pagetable-freeing-84b6be830e20 Best regards,