mm: make userland page table freeing RCU-safe
Subsystems (30)
Lorenzo Stoakes (ARM)
maintainerv5 cover mm: make userland page table freeing RCU-safe
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 deposit a freshly allocated PTE page table and RCU-free the existing PTE page table. Not doing so is currently safe, but for page table walkers relying on RCU alone, it would not be.
Additionally, it makes it possible to implement lockless RCU-only page table walkers which otherwise would have required the PTE PTL.
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 a BH-disabling spin lock used in both get_pointer_table() and free_pointer_table() (softirq being the only asynchronous context in which the lock is taken).
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 possible to emulate.)
v5:
* Collected tags (thanks all! :)
* Updated 1/12 to correctly set result to SCAN_ALLOC_HUGE_PAGE_FAIL should the page table allocation fail, as per Lance.
* Updated 1/12 to rename alloc_deposit_pte() to alloc_deposit_pte_table() and dropped the powerpc hash MMU paragraph from the commit message, as per David.
* Updated 9/12 to use a BH-disabling spinlock rather than an IRQ-safe one, as per Matthew.
* Updated the subject line of 11/12 to clarify that the patch removes now-unneeded code, as per David.
* Updated 12/12 documentation as per David, Lance.
v4:
* Updated the 1st patch to allocate a new page table on PTE deposit rather than deposit a page table that might currently be being walked by an RCU-only page table walker, as per David.
https://lore.kernel.org/r/[email protected]
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.
https://lore.kernel.org/r/[email protected]
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/[email protected]
v1:
https://patch.msgid.link/[email protected]
---
Lorenzo Stoakes (ARM) (12):
mm/khugepaged: deposit a newly allocated page table on collapse
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: userland pgtable freeing is RCU-safe now, remove leftover bits
mm: change the contract for free_pgtables(), update docs
Documentation/mm/process_addrs.rst | 11 +++ 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 | 119 ++++++++++++++++++++----------- 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/khugepaged.c | 34 ++++++++- mm/mmu_gather.c | 30 ++------ mm/pgtable-generic.c | 15 ++-- 35 files changed, 228 insertions(+), 192 deletions(-)
---
base-commit: 88297631d4d42f6004cb39c0ba3da7d2d10a616f
change-id: 20260831-rcu-pagetable-freeing-84b6be830e20
Best regards,
Signature · 1 line
Lorenzo Stoakes (ARM) <[email protected]>
Lorenzo Stoakes (ARM)
maintainerPATCH v5 1/12 mm/khugepaged: deposit a newly allocated page table on collapse
collapse_huge_page() deposits a PTE page table on PMD collapse in order that it can be utilised for subsequent split operations, meaning that those operations do not need to perform an allocation (as they are in a context where it might be unwise).
However the PTE page table which is deposited is the one which is currently mapped by the PMD entry that is in the process of being collapsed.
Once deposited, the PTE page table may be used in a split of any other unrelated PMD entry.
This is currently not an issue as this operation is performed with VMA/mmap write lock + anon rmap locks held, so ordinary page table walkers will never accidentally end up walking the wrong thing, and GUP-fast is protected by an IPI via tlb_remove_table_sync_one().
However, the series to which this commit belongs implements RCU-safe page table traversal, at which point this becomes problematic.
This can be resolved by using pte_offset_map_lock() which gates on a PTE PTL and a pmd_same() check, but lockless walks are unsafe as things stand.
Resolve this by simply allocating a new, zeroed, PTE page table to deposit at the point of collapse.
This path is already costly and an allocation has already been performed for the huge folio, so this allocation is statistical noise in terms of performance and memory usage at this point.
With this PTE page table deposited, RCU-free the existing PTE page table so it is safe for page table walkers to traverse within a grace period.
This also brings this deposit case in line with all other page table deposit logic which deposit a fresh page table.
Additionally, this was the only place in the kernel that displaced a page table like this, so eliminating it also helps consistency.
Since khugepaged runs as a kernel thread, do a little dance in alloc_deposit_pte_table() to correctly charge the allocation.
This is already done for the folio allocation via alloc_charge_folio() but no such wrapper exists for a page table allocation.
--- mm/khugepaged.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-)
mm/khugepaged.c +32 -2
@@ -1278,6 +1278,23 @@ static enum scan_result alloc_charge_folio(struct folio **foliop, struct mm_strureturn SCAN_SUCCEED ; } +static pgtable_t alloc_deposit_pte_table (struct mm_struct *mm) +{ +/* +* khugepaged is run from a kernel thread, so need to manually set the +* correct memcg so the allocation gets charged correctly. +*/ +struct mem_cgroup *memcg =get_mem_cgroup_from_mm (mm); +struct mem_cgroup *old_memcg =set_active_memcg (memcg); +pgtable_t pgtable; + + pgtable =pte_alloc_one (mm); + +set_active_memcg (old_memcg); +mem_cgroup_put (memcg); +return pgtable; +} +/* * collapse_huge_page() expects the mmap_lock to be unlocked before entering and * will always return with the lock unlocked, to avoid holding the mmap_lock
@@ -1293,7 +1310,7 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long sLIST_HEAD (compound_pagelist);pmd_t *pmd, _pmd;pte_t *pte =NULL ; -pgtable_t pgtable; +pgtable_t pgtable =NULL ;struct folio *folio;spinlock_t *pmd_ptl, *pte_ptl;enum scan_result result =SCAN_FAIL ;
@@ -1310,6 +1327,14 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long s goto out_nolock; } +if (is_pmd_order (order)) { + pgtable =alloc_deposit_pte_table (mm); +if (!pgtable) { + result =SCAN_ALLOC_HUGE_PAGE_FAIL ; + goto out_nolock; + } + } +mmap_read_lock (mm); result =hugepage_vma_revalidate (mm, pmd_addr,/*expect_anon=*/ true, &vma, cc, order);
@@ -1433,8 +1458,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long sspin_lock (pmd_ptl);VM_WARN_ON_ONCE (!pmd_none (*pmd));if (is_pmd_order (order)) { - pgtable =pmd_pgtable (_pmd);pgtable_trans_huge_deposit (mm, pmd, pgtable); + pgtable =NULL ;map_anon_folio_pmd_nopf (folio, pmd, vma, pmd_addr); }else {/*
@@ -1453,6 +1478,9 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long s }spin_unlock (pmd_ptl); +if (is_pmd_order (order)) +pte_free_defer (mm,pmd_pgtable (_pmd)); + folio =NULL ; result =SCAN_SUCCEED ;
@@ -1463,6 +1491,8 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long sanon_vma_unlock_write (vma->anon_vma);mmap_write_unlock (mm); out_nolock: +if (pgtable) +pte_free (mm, pgtable);if (folio)folio_put (folio);trace_mm_collapse_huge_page (mm, result ==SCAN_SUCCEED , result, order);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 01/12] mm/khugepaged: deposit a newly allocated page table on collapse
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
collapse_huge_page()deposits a PTE page table on PMD collapse in order that it can be utilised for subsequent split operations, meaning that those operations do not need to perform an allocation (as they are in a context where it might be unwise).However the PTE page table which is deposited is the one which is currently mapped by the PMD entry that is in the process of being collapsed.
Once deposited, the PTE page table may be used in a split of any other unrelated PMD entry.
This is currently not an issue as this operation is performed with VMA/mmap write lock + anon rmap locks held, so ordinary page table walkers will never accidentally end up walking the wrong thing, and GUP-fast is protected by an IPI via
tlb_remove_table_sync_one().
[ ... ]
Quoted 4 lines
Reviewed-by Baolin Wang <[email protected]>Reviewed-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Lorenzo Stoakes (ARM)
maintainerPATCH v5 2/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for most 2-level architectures
In reply to Lorenzo Stoakes (ARM)Commit e3ecf7c7d082 ("mm: pgtable: convert some architectures to use tlb_remove_ptdesc()") updated a number of architectures from using pagetable_dtor() + tlb_remove_page_ptdesc() to using tlb_remove_ptdesc() in __pte_free_tlb().
This is meaningful as tlb_remove_ptdesc() allows for RCU page table freeing if CONFIG_MMU_GATHER_RCU_TABLE_FREE is specified.
The csky, hexagon, nios2, openrisc, sh (except X2) and m68k-sun3 architectures all have 2 levels of page tables, so the only page tables ever freed by mmu_gather are PTEs, so this update suffices to ensure that every page table freed by the mmu_gather mechanism is freed under RCU.
Therefore, update all of these architectures to select CONFIG_MMU_GATHER_RCU_TABLE_FREE.
This forms part of an overall effort to switch every architecture to this mode.
--- arch/csky/Kconfig | 1 + arch/hexagon/Kconfig | 1 + arch/m68k/Kconfig | 1 + arch/nios2/Kconfig | 1 + arch/openrisc/Kconfig | 1 + arch/sh/Kconfig | 1 + 6 files changed, 6 insertions(+)
arch/csky/Kconfig +1 -0
@@ -96,6 +96,7 @@ config CSKY select HAVE_SYSCALL_TRACEPOINTS select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU select LOCK_MM_AND_FIND_VMA + select MMU_GATHER_RCU_TABLE_FREE select MAY_HAVE_SPARSE_IRQ select MODULES_USE_ELF_RELA if MODULES select OF
arch/hexagon/Kconfig +1 -0
@@ -23,6 +23,7 @@ config HEXAGON # select HAVE_CLK select GENERIC_ATOMIC64 select HAVE_PERF_EVENTS + select MMU_GATHER_RCU_TABLE_FREE # GENERIC_ALLOCATOR is used by dma_alloc_coherent() select GENERIC_ALLOCATOR select GENERIC_IRQ_PROBE
arch/m68k/Kconfig +1 -0
@@ -36,6 +36,7 @@ config M68K select HAVE_MOD_ARCH_SPECIFIC select HAVE_UID16 select MMU_GATHER_NO_RANGE if MMU + select MMU_GATHER_RCU_TABLE_FREE if MMU && SUN3 select MODULES_USE_ELF_REL select MODULES_USE_ELF_RELA select NO_DMA if !MMU && !COLDFIRE
arch/nios2/Kconfig +1 -0
@@ -19,6 +19,7 @@ config NIOS2 select HAVE_PAGE_SIZE_4KB select IRQ_DOMAIN select LOCK_MM_AND_FIND_VMA + select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA select OF select OF_EARLY_FLATTREE
arch/openrisc/Kconfig +1 -0
@@ -35,6 +35,7 @@ config OPENRISC select GENERIC_ATOMIC64 select GENERIC_CLOCKEVENTS_BROADCAST select GENERIC_SMP_IDLE_THREAD + select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA select HAVE_DEBUG_STACKOVERFLOW select OR1K_PIC
arch/sh/Kconfig +1 -0
@@ -61,6 +61,7 @@ config SUPERH select HAVE_SYSCALL_TRACEPOINTS select IRQ_FORCED_THREADING select LOCK_MM_AND_FIND_VMA + select MMU_GATHER_RCU_TABLE_FREE if MMU && !X2TLB select MODULES_USE_ELF_RELA select NEED_SG_DMA_LENGTH select NO_DMA if !MMU && !DMA_COHERENT
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 02/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for most 2-level architectures
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 16 lines
Commit
e3ecf7c7d082("mm: pgtable: convert some architectures to usetlb_remove_ptdesc()") updated a number of architectures from usingpagetable_dtor()+tlb_remove_page_ptdesc()to usingtlb_remove_ptdesc()in__pte_free_tlb().This is meaningful as
tlb_remove_ptdesc()allows for RCU page table freeing ifCONFIG_MMU_GATHER_RCU_TABLE_FREEis specified.The csky, hexagon, nios2, openrisc, sh (except X2) and m68k-sun3 architectures all have 2 levels of page tables, so the only page tables ever freed by
mmu_gatherare PTEs, so this update suffices to ensure that every page table freed by themmu_gathermechanism is freed under RCU.Therefore, update all of these architectures to select
CONFIG_MMU_GATHER_RCU_TABLE_FREE.
Quoted 6 lines
This forms part of an overall effort to switch every architecture to this mode.
Reviewed-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Lorenzo Stoakes (ARM)
maintainerPATCH v5 3/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU riscv
In reply to Lorenzo Stoakes (ARM)Currently riscv gates MMU_GATHER_RCU_TABLE_FREE on CONFIG_SMP and CONFIG_MMU.
Commit 69be3fb111e7 ("riscv: enable MMU_GATHER_RCU_TABLE_FREE for SMP && MMU") enabled CONFIG_MMU_GATHER_RCU_TABLE_FREE for CONFIG_SMP, CONFIG_MMU riscv builds.
This is expressly for the safety of GUP-fast walkers (CONFIG_HAVE_GUP_FAST is enabled if CONFIG_MMU is enabled).
Naturally a single core system does not encounter issues with software page table walkers being correctly synchronised across cores, as there is only a single core.
However, CONFIG_PREEMPT_RCU is still available on a riscv UP system, so for a future RCU-only page table walker, this guarantee is required to prevent concurrent page table teardown.
All page table freeing is already done via tlb_remove_ptdesc() so the conditions of CONFIG_MMU_GATHER_RCU_TABLE_FREE are already met.
This forms part of an overall effort to switch every architecture to this mode.
--- arch/riscv/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/riscv/Kconfig +1 -1
@@ -208,7 +208,7 @@ config RISCV select IRQ_FORCED_THREADING select KASAN_VMALLOC if KASAN select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE if SMP && MMU + select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_RELA if MODULES select OF select OF_EARLY_FLATTREE
Signature · 7 lines
2.55.0 _______________________________________________ linux-snps-arc mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-snps-arc
[email protected]
AI reviewRe: [PATCH v5 03/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU riscv
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 21 lines
Currently riscv gates
MMU_GATHER_RCU_TABLE_FREEonCONFIG_SMPandCONFIG_MMU.Commit
69be3fb111e7("riscv: enableMMU_GATHER_RCU_TABLE_FREEfor SMP && MMU") enabledCONFIG_MMU_GATHER_RCU_TABLE_FREEforCONFIG_SMP,CONFIG_MMUriscv builds.This is expressly for the safety of GUP-fast walkers (
CONFIG_HAVE_GUP_FASTis enabled ifCONFIG_MMUis enabled).Naturally a single core system does not encounter issues with software page table walkers being correctly synchronised across cores, as there is only a single core.
However,
CONFIG_PREEMPT_RCUis still available on a riscv UP system, so for a future RCU-only page table walker, this guarantee is required to prevent concurrent page table teardown.All page table freeing is already done via
tlb_remove_ptdesc()so the conditions ofCONFIG_MMU_GATHER_RCU_TABLE_FREEare already met.
Quoted 7 lines
This forms part of an overall effort to switch every architecture to this mode.
Acked-by Lance Yang <[email protected]>Tested-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Lorenzo Stoakes (ARM)
maintainerPATCH v5 4/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU arm
In reply to Lorenzo Stoakes (ARM)Commit a0ad5496b2b3 ("arm: mm: enable HAVE_RCU_TABLE_FREE logic") enabled CONFIG_MMU_GATHER_RCU_TABLE_FREE (then named HAVE_RCU_TABLE_FREE) for SMP arm architectures with LPAE enabled.
Regardless of whether CONFIG_ARM_LPAE is enabled or not, the same page table freeing functions __pte_free_tlb() and __pmd_free_tlb() are used.
Non-LPAE PMD page tables are folded into the PGD and freed by pgd_free() (PGD freeing is not part of mmu_gather page table freeing in any case), so this is a noop in this case.
Since commit 358d1c39c82a ("arm: convert various functions to use ptdescs") both LPAE and non-LPAE PTE page table freeing uses tlb_remove_ptdesc().
Thus all page table freeing is performed under RCU with CONFIG_MMU_GATHER_RCU_TABLE_FREE enabled for LPAE and non-LPAE and thus it need not be gated on LPAE.
A UP arm system can set CONFIG_PREEMPT_RCU, so a future pure RCU page table walker requires MMU_GATHER_RCU_TABLE_FREE to be enabled on UP as well, even if concurrent GUP fast is not possible there.
Therefore, it is both safe and desirable to set CONFIG_MMU_GATHER_RCU_TABLE_FREE for all MMU arm architectures (nommu does not perform mmu_gather operations).
This forms part of an overall effort to switch every architecture to this mode.
--- arch/arm/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm/Kconfig +1 -1
@@ -134,7 +134,7 @@ config ARM select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP select HAVE_POSIX_CPU_TIMERS_TASK_WORK - select MMU_GATHER_RCU_TABLE_FREE if SMP && ARM_LPAE + select MMU_GATHER_RCU_TABLE_FREE if MMU select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RSEQ select HAVE_RUST if CPU_LITTLE_ENDIAN && CPU_32v7 && !KASAN
Signature · 7 lines
2.55.0 _______________________________________________ linux-snps-arc mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-snps-arc
[email protected]
AI reviewRe: [PATCH v5 04/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU arm
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
Commit
a0ad5496b2b3("arm: mm: enableHAVE_RCU_TABLE_FREElogic") enabledCONFIG_MMU_GATHER_RCU_TABLE_FREE(then namedHAVE_RCU_TABLE_FREE) for SMP arm architectures with LPAE enabled.Regardless of whether
CONFIG_ARM_LPAEis enabled or not, the same page table freeing functions__pte_free_tlb()and__pmd_free_tlb()are used.Non-LPAE PMD page tables are folded into the PGD and freed by
pgd_free()(PGD freeing is not part ofmmu_gatherpage table freeing in any case), so this is a noop in this case.Since commit
358d1c39c82a("arm: convert various functions to use ptdescs") both LPAE and non-LPAE PTE page table freeing usestlb_remove_ptdesc().
Quoted 7 lines
Thus all page table freeing is performed under RCU with [ ... ]
Tested-by Lance Yang <[email protected]>Acked-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Lorenzo Stoakes (ARM)
maintainerPATCH v5 5/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for arc, microblaze, xtensa
In reply to Lorenzo Stoakes (ARM)Each of these architectures directly free page tables without routing these changes through tlb_remove_ptdesc().
The use of tlb_remove_ptdesc() is required for CONFIG_MMU_GATHER_RCU_TABLE_FREE to correctly free page tables under RCU, so simply update these architectures to use these functions.
Since none of the architectures share page tables or do anything unusual, nothing complicated is required here.
Therefore this is simply a mechanical change - convert __pud_free_tlb(), __pmd_free_tlb() and __pte_free_tlb() to use tlb_remove_ptdesc() as required.
At the point this is in place, all mmu_gather page table freeing is performed under RCU, and thus MMU_GATHER_RCU_TABLE_FREE is selected for each architecture.
Note that CONFIG_MMU_GATHER_RCU_TABLE_FREE is dependent on CONFIG_MMU for xtensa to reflect the fact that nommu does not implement page table gathering.
This forms part of an overall effort to switch every architecture to this mode.
--- arch/arc/Kconfig | 1 + arch/arc/include/asm/pgalloc.h | 6 +++--- arch/microblaze/Kconfig | 1 + arch/microblaze/include/asm/pgalloc.h | 2 +- arch/xtensa/Kconfig | 1 + arch/xtensa/include/asm/tlb.h | 2 +- 6 files changed, 8 insertions(+), 5 deletions(-)
arch/arc/Kconfig +1 -0
@@ -47,6 +47,7 @@ config ARC select HAVE_SYSCALL_TRACEPOINTS select IRQ_DOMAIN select LOCK_MM_AND_FIND_VMA + select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA select OF select OF_EARLY_FLATTREE
arch/arc/include/asm/pgalloc.h +3 -3
@@ -72,7 +72,7 @@ static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4dp, pud_t *pudp) set_p4d(p4dp, __p4d((unsigned long)pudp)); } -#define __pud_free_tlb (tlb, pmd, addr) pud_free((tlb)->mm, pmd) +#define __pud_free_tlb (tlb, pmd, addr) tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd))#endif
@@ -83,10 +83,10 @@ static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)set_pud (pudp,__pud ((unsigned long )pmdp)); } -#define __pmd_free_tlb (tlb, pmd, addr) pmd_free((tlb)->mm, pmd) +#define __pmd_free_tlb (tlb, pmd, addr) tlb_remove_ptdesc((tlb), virt_to_ptdesc(pmd))#endif -#define __pte_free_tlb (tlb, pte, addr) pte_free((tlb)->mm, pte) +#define __pte_free_tlb (tlb, pte, addr) tlb_remove_ptdesc((tlb), page_ptdesc(pte))#endif /* _ASM_ARC_PGALLOC_H */
arch/microblaze/Kconfig +1 -0
@@ -41,6 +41,7 @@ config MICROBLAZE select PCI_SYSCALL if PCI select CPU_NO_EFFICIENT_FFS select MMU_GATHER_NO_RANGE + select MMU_GATHER_RCU_TABLE_FREE select SPARSE_IRQ select ZONE_DMA select TRACE_IRQFLAGS_SUPPORT
arch/microblaze/include/asm/pgalloc.h +1 -1
@@ -25,7 +25,7 @@ extern void __bad_pte(pmd_t *pmd);extern pte_t *pte_alloc_one_kernel (struct mm_struct *mm); -#define __pte_free_tlb (tlb, pte, addr) pte_free((tlb)->mm, (pte)) +#define __pte_free_tlb (tlb, pte, addr) tlb_remove_ptdesc((tlb), page_ptdesc(pte))#define pmd_populate (mm, pmd, pte) \ (pmd_val(*(pmd)) = (unsigned long)page_address(pte))
arch/xtensa/Kconfig +1 -0
@@ -55,6 +55,7 @@ config XTENSA select HAVE_VIRT_CPU_ACCOUNTING_GEN select IRQ_DOMAIN select LOCK_MM_AND_FIND_VMA + select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_RELA select PERF_USE_VMALLOC select TRACE_IRQFLAGS_SUPPORT
arch/xtensa/include/asm/tlb.h +1 -1
@@ -16,7 +16,7 @@#include <asm-generic/tlb.h> -#define __pte_free_tlb (tlb, pte, address) pte_free((tlb)->mm, pte) +#define __pte_free_tlb (tlb, pte, address) tlb_remove_ptdesc((tlb), page_ptdesc(pte))void check_tlb_sanity (void );
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 05/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for arc, microblaze, xtensa
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 22 lines
Each of these architectures directly free page tables without routing these changes through
tlb_remove_ptdesc().The use of
tlb_remove_ptdesc()is required forCONFIG_MMU_GATHER_RCU_TABLE_FREEto correctly free page tables under RCU, so simply update these architectures to use these functions.Since none of the architectures share page tables or do anything unusual, nothing complicated is required here.
Therefore this is simply a mechanical change - convert
__pud_free_tlb(),__pmd_free_tlb()and__pte_free_tlb()to usetlb_remove_ptdesc()as required.At the point this is in place, all
mmu_gatherpage table freeing is performed under RCU, and thusMMU_GATHER_RCU_TABLE_FREEis selected for each architecture.Note that
CONFIG_MMU_GATHER_RCU_TABLE_FREEis dependent onCONFIG_MMUfor xtensa to reflect the fact that nommu does not implement page table gathering.
Quoted 6 lines
This forms part of an overall effort to switch every architecture to this mode.
Acked-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Lorenzo Stoakes (ARM)
maintainerPATCH v5 6/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc64
In reply to Lorenzo Stoakes (ARM)Commit 4a0100f7546f ("sparc64: use RCU page table freeing") enabled CONFIG_MMU_GATHER_RCU_TABLE_FREE for SMP sparc64 architectures, expressly for GUP-fast page table walkers.
Naturally, UP systems do not have to worry about concurrent GUP fast operations.
However, CONFIG_PREEMPT_RCU is also available even on a UP system, so a future pure-RCU page table walker requires MMU_GATHER_RCU_TABLE_FREE to be enabled on UP, even if concurrent GUP fast is not possible there.
To enable future pure-RCU page table walkers, enable MMU_GATHER_RCU_TABLE_FREE unconditionally.
With this change, it is no longer necessary to have !CONFIG_SMP pgtable_free_tlb(), so also remove this now dead code.
This forms part of an overall effort to switch every architecture to this mode.
--- arch/sparc/Kconfig | 4 ++-- arch/sparc/include/asm/pgalloc_64.h | 8 -------- 2 files changed, 2 insertions(+), 10 deletions(-)
arch/sparc/Kconfig +2 -2
@@ -75,8 +75,8 @@ config SPARC64 select HAVE_FUNCTION_GRAPH_TRACER select HAVE_KRETPROBES select HAVE_KPROBES - select MMU_GATHER_RCU_TABLE_FREE if SMP - select HAVE_ARCH_TLB_REMOVE_TABLE if SMP + select MMU_GATHER_RCU_TABLE_FREE + select HAVE_ARCH_TLB_REMOVE_TABLE select MMU_GATHER_MERGE_VMAS select MMU_GATHER_NO_FLUSH_CACHE select HAVE_ARCH_TRANSPARENT_HUGEPAGE
arch/sparc/include/asm/pgalloc_64.h +0 -8
@@ -74,8 +74,6 @@ void pte_free_defer(struct mm_struct *mm, pgtable_t pgtable);void pgtable_free (void *table,bool is_page); -#ifdef CONFIG_SMP -struct mmu_gather ;void tlb_remove_table (struct mmu_gather *,void *);
@@ -96,12 +94,6 @@ static inline void __tlb_remove_table(void *_table) is_page = true;pgtable_free (table, is_page); } -#else /* CONFIG_SMP */ -static inline void pgtable_free_tlb (struct mmu_gather *tlb,void *table,bool is_page) -{ -pgtable_free (table, is_page); -} -#endif /* !CONFIG_SMP */ static inline void __pte_free_tlb (struct mmu_gather *tlb,pte_t *pte,unsigned long address)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 06/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc64
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
Commit
4a0100f7546f("sparc64: use RCU page table freeing") enabledCONFIG_MMU_GATHER_RCU_TABLE_FREEfor SMP sparc64 architectures, expressly for GUP-fast page table walkers.Naturally, UP systems do not have to worry about concurrent GUP fast operations.
However,
CONFIG_PREEMPT_RCUis also available even on a UP system, so a future pure-RCU page table walker requiresMMU_GATHER_RCU_TABLE_FREEto be enabled on UP, even if concurrent GUP fast is not possible there.To enable future pure-RCU page table walkers, enable
MMU_GATHER_RCU_TABLE_FREEunconditionally.With this change, it is no longer necessary to have !CONFIG_SMP
pgtable_free_tlb(), so also remove this now dead code.
Quoted 7 lines
This forms part of an overall effort to switch every architecture to this mode.
Tested-by Lance Yang <[email protected]>Acked-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Lorenzo Stoakes (ARM)
maintainerPATCH v5 7/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-coldfire
In reply to Lorenzo Stoakes (ARM)Similar to sun3, the coldfire variant of m68k uses 2-level page tables.
Update its __pte_free_tlb() function to use tlb_remove_ptdesc() in order that, with CONFIG_MMU_GATHER_RCU_TABLE_FREE, page tables are freed under RCU.
The page tables occupy a page each and have no odd semantics, so this change suffices to allow enabling of CONFIG_MMU_GATHER_RCU_TABLE_FREE for m68k-coldfire, so do so.
This forms part of an overall effort to switch every architecture to this mode.
--- arch/m68k/Kconfig | 2 +- arch/m68k/include/asm/mcf_pgalloc.h | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-)
arch/m68k/Kconfig +1 -1
@@ -36,7 +36,7 @@ config M68K select HAVE_MOD_ARCH_SPECIFIC select HAVE_UID16 select MMU_GATHER_NO_RANGE if MMU - select MMU_GATHER_RCU_TABLE_FREE if MMU && SUN3 + select MMU_GATHER_RCU_TABLE_FREE if MMU && (SUN3 || COLDFIRE) select MODULES_USE_ELF_REL select MODULES_USE_ELF_RELA select NO_DMA if !MMU && !COLDFIRE
arch/m68k/include/asm/mcf_pgalloc.h +1 -4
@@ -39,10 +39,7 @@ extern inline pmd_t *pmd_alloc_kernel(pgd_t *pgd, unsigned long address)static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pgtable,unsigned long address) { -struct ptdesc *ptdesc =virt_to_ptdesc (pgtable); - -pagetable_dtor (ptdesc); -pagetable_free (ptdesc); +tlb_remove_ptdesc (tlb,virt_to_ptdesc (pgtable)); }static inline pgtable_t pte_alloc_one (struct mm_struct *mm)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 07/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-coldfire
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines
Similar to sun3, the coldfire variant of m68k uses 2-level page tables.
Update its
__pte_free_tlb()function to usetlb_remove_ptdesc()in order that, withCONFIG_MMU_GATHER_RCU_TABLE_FREE, page tables are freed under RCU.The page tables occupy a page each and have no odd semantics, so this change suffices to allow enabling of
CONFIG_MMU_GATHER_RCU_TABLE_FREEfor m68k-coldfire, so do so.
Quoted 8 lines
This forms part of an overall effort to switch every architecture to this mode.
Acked-by Greg Ungerer <[email protected]>Tested-by Greg Ungerer <[email protected]>Acked-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Lorenzo Stoakes (ARM)
maintainerPATCH v5 8/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for sh-X2
In reply to Lorenzo Stoakes (ARM)Currently, non-x2 sh specifies CONFIG_MMU_GATHER_RCU_TABLE_FREE allowing RCU page table freeing.
sh-X2 is problematic because it utilises slab-allocated PMD page tables, and thus tlb_remove_ptdesc() cannot be used in these cases.
All other sh variants are fine as commit e3ecf7c7d082 ("mm: pgtable:
convert some architectures to use tlb_remove_ptdesc()") already converted page table freeing to use tlb_remove_ptdesc(), which does so after an RCU grace period when CONFIG_MMU_GATHER_RCU_TABLE_FREE is specified.
Resolve this issue by firstly specifying CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE for sh-X2, so the arch can provide its own __tlb_remove_table() implementation (called after the RCU grace period).
Then, convert __pmd_free_tlb() to tag the pointer to the PMD, and have __tlb_remove_table() check this tag to determine whether to free via the slab or to use pagetable_dtor_free().
This follows the pattern used by sparc64 as implemented in commit 4a0100f7546f ("sparc64: use RCU page table freeing").
Previously __pmd_free_tlb() freed PMD page tables immediately, before any TLB flush IPI. This seems to be a pre-existing bug, which this change also resolves.
CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE is only specified for sh-X2, as setting it disables CONFIG_PT_RECLAIM and causes __tlb_remove_table_one() to call tlb_remove_table_sync_rcu() and synchronize_rcu() in turn, and this is not necessary for other sh variants.
This forms part of an overall effort to switch every architecture to this mode.
--- arch/sh/Kconfig | 3 ++- arch/sh/include/asm/pgalloc.h | 6 +++++- arch/sh/mm/pgtable.c | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-)
arch/sh/Kconfig +2 -1
@@ -33,6 +33,7 @@ config SUPERH select HAVE_ARCH_AUDITSYSCALL select HAVE_ARCH_KGDB select HAVE_ARCH_SECCOMP_FILTER + select HAVE_ARCH_TLB_REMOVE_TABLE if X2TLB select HAVE_ARCH_TRACEHOOK select HAVE_DEBUG_BUGVERBOSE select HAVE_DEBUG_KMEMLEAK
@@ -61,7 +62,7 @@ config SUPERH select HAVE_SYSCALL_TRACEPOINTS select IRQ_FORCED_THREADING select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE if MMU && !X2TLB + select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_RELA select NEED_SG_DMA_LENGTH select NO_DMA if !MMU && !DMA_COHERENT
arch/sh/include/asm/pgalloc.h +5 -1
@@ -17,7 +17,11 @@ extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);extern void pud_populate (struct mm_struct *mm,pud_t *pudp,pmd_t *pmd);extern pmd_t *pmd_alloc_one (struct mm_struct *mm,unsigned long address);extern void pmd_free (struct mm_struct *mm,pmd_t *pmd); -#define __pmd_free_tlb (tlb, pmdp, addr) pmd_free((tlb)->mm, (pmdp)) +extern void __tlb_remove_table (void *table); + +/* PMDs are slab-allocated, tag so they are freed correctly. */ +#define __pmd_free_tlb (tlb, pmdp, addr) \ + tlb_remove_table((tlb), (void *)((unsigned long)(pmdp) | 1))#endif static inline void pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmd,
arch/sh/mm/pgtable.c +20 -0
@@ -56,4 +56,24 @@ void pmd_free(struct mm_struct *mm, pmd_t *pmd) {kmem_cache_free (pmd_cachep, pmd); } + +static void __tlb_remove_table_slab (void *table) +{ +kmem_cache_free (pmd_cachep, table); +} + +static void __tlb_remove_table_pgtable (void *table) +{ +pagetable_dtor_free (table); +} + +void __tlb_remove_table (void *table) +{ +const unsigned long addr = (unsigned long )table; + +if (addr &1 ) +__tlb_remove_table_slab ((void *)(addr & ~1UL )); +else +__tlb_remove_table_pgtable (table); +}#endif /* PAGETABLE_LEVELS > 2 */
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 08/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for sh-X2
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines
Currently, non-x2 sh specifies
CONFIG_MMU_GATHER_RCU_TABLE_FREEallowing RCU page table freeing.sh-X2 is problematic because it utilises slab-allocated PMD page tables, and thus
tlb_remove_ptdesc()cannot be used in these cases.All other sh variants are fine as commit
e3ecf7c7d082("mm: pgtable:
convert some architectures to usetlb_remove_ptdesc()") already converted page table freeing to usetlb_remove_ptdesc(), which does so after an RCU grace period whenCONFIG_MMU_GATHER_RCU_TABLE_FREEis specified.Resolve this issue by firstly specifying
CONFIG_HAVE_ARCH_TLB_REMOVE_TABLEfor sh-X2, so the arch can provide its own__tlb_remove_table()implementation (called after the RCU grace period).
Quoted 6 lines
[ ... ]
mode.Acked-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Lorenzo Stoakes (ARM)
maintainerPATCH v5 9/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-motorola
In reply to Lorenzo Stoakes (ARM)sun3 and coldfire are already supported, however motorola requires a little more care.
Here, custom table removal logic is required, so CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE is enabled for m68k-motorola.
Firstly as part of this change, the page table level must be communicated to the underlying __tlb_remove_table() implementation.
Take advantage of the fact that page tables are aligned by more than enough to permit setting TABLE_PTE or TABLE_PMD in the low bits of the pointer, and store this there.
Then update __pte_free_tlb() and __pmd_free_tlb() to pass this through, then have __tlb_remove_table() decode this and pass it to free_pointer_table().
The page table freeing is performed via call_rcu(), so free_pointer_table() now will be invoked from softirq context, and as such may be re-entrant.
Introduce a spinlock to handle this and hold it over the time a given ptable entry is being referenced in both get_pointer_table() and free_pointer_table().
As softirq is the only asynchronous context in which the lock is taken, it suffices to disable bottom halves while holding it.
In order to make things a little easier in this respect, separate out the logic for adding a new ptable entry into add_pointer_table() and only hold the lock during ptable entry insertion in this case.
Note that original list_add_tail(new, dp) added new prior to dp, which is ptable_list[type].next, i.e. after ptable_list[type].
The equivalent therefore is list_add(new, &ptable_list[type]), which adds new after ptable_list[type], only without needing to make reference to dp.
Note that, as m68k-motorola specifies CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE, it does not enable CONFIG_PT_RECLAIM. This isn't meaningfully impactful.
With this applied, all of m68k implements CONFIG_MMU_GATHER_RCU_TABLE_FREE.
This forms part of an overall effort to switch every architecture to this mode.
--- arch/m68k/Kconfig | 3 +- arch/m68k/include/asm/motorola_pgalloc.h | 9 +-- arch/m68k/mm/motorola.c | 119 ++++++++++++++++++++----------- 3 files changed, 84 insertions(+), 47 deletions(-)
arch/m68k/Kconfig +2 -1
@@ -29,6 +29,7 @@ config M68K select HAVE_ARCH_LIBGCC_H select HAVE_ARCH_SECCOMP select HAVE_ARCH_SECCOMP_FILTER + select HAVE_ARCH_TLB_REMOVE_TABLE if MMU_MOTOROLA select HAVE_ASM_MODVERSIONS select HAVE_DEBUG_BUGVERBOSE select HAVE_EFFICIENT_UNALIGNED_ACCESS if !CPU_HAS_NO_UNALIGNED
@@ -36,7 +37,7 @@ config M68K select HAVE_MOD_ARCH_SPECIFIC select HAVE_UID16 select MMU_GATHER_NO_RANGE if MMU - select MMU_GATHER_RCU_TABLE_FREE if MMU && (SUN3 || COLDFIRE) + select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_REL select MODULES_USE_ELF_RELA select NO_DMA if !MMU && !COLDFIRE
arch/m68k/include/asm/motorola_pgalloc.h +5 -4
@@ -17,6 +17,7 @@ enum m68k_table_types {extern void init_pointer_table (void *table,int type);extern void *get_pointer_table (struct mm_struct *mm,int type);extern int free_pointer_table (void *table,int type); +extern void __tlb_remove_table (void *table);/* * Allocate and free page tables. The xxx_kernel() versions are
@@ -47,7 +48,7 @@ static inline void pte_free(struct mm_struct *mm, pgtable_t pgtable)static inline void __pte_free_tlb (struct mmu_gather *tlb,pgtable_t pgtable,unsigned long address) { -free_pointer_table (pgtable,TABLE_PTE ); +tlb_remove_table (tlb, (void *)((unsigned long )pgtable |TABLE_PTE )); }
@@ -61,10 +62,10 @@ static inline int pmd_free(struct mm_struct *mm, pmd_t *pmd)return free_pointer_table (pmd,TABLE_PMD ); } -static inline int __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd, -unsigned long address) +static inline void __pmd_free_tlb (struct mmu_gather *tlb,pmd_t *pmd, +unsigned long address) { -return free_pointer_table (pmd,TABLE_PMD ); +tlb_remove_table (tlb, (void *)((unsigned long )pmd |TABLE_PMD )); }
arch/m68k/mm/motorola.c +77 -42
169 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 09/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-motorola
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines
sun3 and coldfire are already supported, however motorola requires a little more care.
Here, custom table removal logic is required, so
CONFIG_HAVE_ARCH_TLB_REMOVE_TABLEis enabled for m68k-motorola.Firstly as part of this change, the page table level must be communicated to the underlying
__tlb_remove_table()implementation.Take advantage of the fact that page tables are aligned by more than enough to permit setting
TABLE_PTEorTABLE_PMDin the low bits of the pointer, and store this there.
Quoted 8 lines
Then update
__pte_free_tlb()and__pmd_free_tlb()to pass this through, then have__tlb_remove_table()decode this and pass it to
[ ... ]Acked-by Lance Yang <[email protected]>Tested-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Lorenzo Stoakes (ARM)
maintainerPATCH v5 10/12 mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc32
In reply to Lorenzo Stoakes (ARM)Careful handling is required for sparc32 which implements page tables as part of a shared backing page.
To support this, a custom __tlb_remove_table() function is required, as specified by CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE.
This allows __pte_free_tlb() and __pmd_free_tlb() to specify which page table level is being freed, which is transmitted to __tlb_remove_table() through setting the lowest bit of the page table to 1 for a PMD and 0 for a PTE (the page tables are 256-byte aligned so this is safe to do).
Next, since the page table freeing is done via RCU callback, and thus might be executed in softirq context, update the spin locks to IRQ save/restore.
Then, in __tlb_remove_table(), figure out whether to free a PMD page table via free_pmd_fast() or a PTE via the newly introduced __pte_free() function, using the lower bit encoded in __pte_free_tlb() or __pmd_free_tlb() to determine which to call.
As part of this change use this spin lock rather than mm->page_table_lock for all shared page table exclusion, as RCU freeing means that page tables can be freed from soft IRQ context so both don't have an mm and also mm->page_table_lock is not IRQ-safe.
Note that the specification of CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE disables CONFIG_PT_RECLAIM for sparc32, which mirrors sparc64.
This forms part of an overall effort to switch every architecture to this mode, and with it complete, means every architecture now supports CONFIG_MMU_GATHER_RCU_TABLE_FREE.
--- arch/sparc/Kconfig | 2 ++ arch/sparc/include/asm/pgalloc_32.h | 7 +++++-- arch/sparc/lib/bitext.c | 14 +++++++------- arch/sparc/mm/srmmu.c | 32 +++++++++++++++++++++++++++----- 4 files changed, 41 insertions(+), 14 deletions(-)
arch/sparc/Kconfig +2 -0
@@ -64,6 +64,8 @@ config SPARC32 select HAVE_UID16 select HAVE_PAGE_SIZE_4KB select LOCK_MM_AND_FIND_VMA + select MMU_GATHER_RCU_TABLE_FREE + select HAVE_ARCH_TLB_REMOVE_TABLE select OLD_SIGACTION select ZONE_DMA
arch/sparc/include/asm/pgalloc_32.h +5 -2
@@ -48,7 +48,9 @@ static inline void free_pmd_fast(pmd_t * pmd) }#define pmd_free (mm, pmd) free_pmd_fast(pmd) -#define __pmd_free_tlb (tlb, pmd, addr) pmd_free((tlb)->mm, pmd) + +#define __pmd_free_tlb (tlb, pmd, addr) \ + tlb_remove_table((tlb), (void *)((unsigned long)(pmd) | 1UL))#define pmd_populate (mm, pmd, pte) pmd_set(pmd, pte)
@@ -72,6 +74,7 @@ static inline void free_pte_fast(pte_t *pte)#define pte_free_kernel (mm, pte) free_pte_fast(pte)void pte_free (struct mm_struct * mm,pgtable_t pte); -#define __pte_free_tlb (tlb, pte, addr) pte_free((tlb)->mm, pte) +void __tlb_remove_table (void *table); +#define __pte_free_tlb (tlb, pte, addr) tlb_remove_table((tlb), (void *)(pte))#endif /* _SPARC_PGALLOC_H */
arch/sparc/lib/bitext.c +7 -7
@@ -22,8 +22,6 @@* @align: requested alignment * * Returns offset in the map or -1 if out of space. -* -* Not safe to call from an interrupt (uses spin_lock). */ int bit_map_string_get (struct bit_map *t,int len,int align) {
@@ -31,6 +29,7 @@ int bit_map_string_get(struct bit_map *t, int len, int align)int off_new;int align1;int i, color; +unsigned long flags;if (t->num_colors) {/* align is overloaded to be the page color */
@@ -50,7 +49,7 @@ int bit_map_string_get(struct bit_map *t, int len, int align)BUG (); color &= align1; -spin_lock (&t->lock); +spin_lock_irqsave (&t->lock, flags);if (len < t->last_size) offset = t->first_free;else
@@ -64,7 +63,7 @@ int bit_map_string_get(struct bit_map *t, int len, int align)if (offset >= t->size) offset =0 ;if (count + len > t->size) { -spin_unlock (&t->lock); +spin_unlock_irqrestore (&t->lock, flags);/* P3 */ printk (KERN_ERR "bitmap out: size %d used %d off %d len %d align %d count %d\n" , t->size, t->used, offset, len, align, count);
@@ -90,7 +89,7 @@ int bit_map_string_get(struct bit_map *t, int len, int align) t->last_off =0 ; t->used += len; t->last_size = len; -spin_unlock (&t->lock); +spin_unlock_irqrestore (&t->lock, flags);return offset; } }
@@ -103,10 +102,11 @@ int bit_map_string_get(struct bit_map *t, int len, int align)void bit_map_clear (struct bit_map *t,int offset,int len) {int i; +unsigned long flags;if (t->used < len)BUG ();/* Much too late to do any good, but alas... */ -spin_lock (&t->lock); +spin_lock_irqsave (&t->lock, flags);for (i =0 ; i < len; i++) {if (test_bit (offset + i, t->map) ==0 )BUG ();
@@ -115,7 +115,7 @@ void bit_map_clear(struct bit_map *t, int offset, int len)if (offset < t->first_free) t->first_free = offset; t->used -= len; -spin_unlock (&t->lock); +spin_unlock_irqrestore (&t->lock, flags); }void bit_map_init (struct bit_map *t,unsigned long *map,int size)
arch/sparc/mm/srmmu.c +27 -5
@@ -340,38 +340,60 @@ pgd_t *get_pgd_fast(void)* Alignments up to the page size are the same for physical and virtual * addresses of the nocache area. */ + +static DEFINE_SPINLOCK (pte_page_lock); +pgtable_t pte_alloc_one (struct mm_struct *mm) { +unsigned long flags;pte_t *ptep;struct page *page;if (!(ptep =pte_alloc_one_kernel (mm)))return NULL ; page =pfn_to_page (__nocache_pa ((unsigned long )ptep) >>PAGE_SHIFT ); -spin_lock (&mm->page_table_lock); +spin_lock_irqsave (&pte_page_lock, flags);if (page_ref_inc_return (page) ==2 && !pagetable_pte_ctor (mm,page_ptdesc (page))) {page_ref_dec (page); ptep =NULL ; } -spin_unlock (&mm->page_table_lock); +spin_unlock_irqrestore (&pte_page_lock, flags);return ptep; } -void pte_free (struct mm_struct *mm,pgtable_t ptep) +static void __pte_free (pgtable_t ptep) {struct page *page; +unsigned long flags; page =pfn_to_page (__nocache_pa ((unsigned long )ptep) >>PAGE_SHIFT ); -spin_lock (&mm->page_table_lock); +spin_lock_irqsave (&pte_page_lock, flags);if (page_ref_dec_return (page) ==1 )pagetable_dtor (page_ptdesc (page)); -spin_unlock (&mm->page_table_lock); +spin_unlock_irqrestore (&pte_page_lock, flags);srmmu_free_nocache (ptep,SRMMU_PTE_TABLE_SIZE ); } +void pte_free (struct mm_struct *mm,pgtable_t ptep) +{ +__pte_free (ptep); +} + +void __tlb_remove_table (void *table) +{ +const unsigned long encoded = (unsigned long )table; +const unsigned long addr = encoded & ~1UL ; +const bool is_pmd = encoded &1 ; + +if (is_pmd) +free_pmd_fast ((pmd_t *)addr); +else +__pte_free ((pgtable_t )addr); +} +/* context handling - a dynamically sized pool is used */ #define NO_CONTEXT -1
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 10/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc32
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines
Careful handling is required for sparc32 which implements page tables as part of a shared backing page.
To support this, a custom
__tlb_remove_table()function is required, as specified byCONFIG_HAVE_ARCH_TLB_REMOVE_TABLE.This allows
__pte_free_tlb()and__pmd_free_tlb()to specify which page table level is being freed, which is transmitted to__tlb_remove_table()through setting the lowest bit of the page table to 1 for a PMD and 0 for a PTE (the page tables are 256-byte aligned so this is safe to do).Next, since the page table freeing is done via RCU callback, and thus might be executed in softirq context, update the spin locks to IRQ save/restore.
Quoted 7 lines
Then, in
__tlb_remove_table(), figure out whether to free a PMD page table [ ... ]Tested-by Lance Yang <[email protected]>Acked-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Lorenzo Stoakes (ARM)
maintainerPATCH v5 11/12 mm: userland pgtable freeing is RCU-safe now, remove leftover bits
In reply to Lorenzo Stoakes (ARM)Now every architecture has been converted to support CONFIG_MMU_GATHER_RCU_TABLE_FREE, this configuration option no longer makes any sense to keep around.
Therefore remove it, and remove all the dead code that existed for !CONFIG_MMU_GATHER_RCU_TABLE_FREE architectures previously.
Additionally, CONFIG_MMU_GATHER_TABLE_FREE is no longer necessary, as all architectures instead use CONFIG_HAVE_ARCH_TLB_REMOVE_TABLE when a custom __tlb_remove_table() is required, so remove this too.
A number of architectures only enabled CONFIG_MMU_GATHER_RCU_TABLE_FREE if CONFIG_MMU was set, however the mmu_gather logic only actually does something meaningful if CONFIG_MMU is set (mmu_gather.c is only compiled in this case, for instance).
As a result, there's no need to gate any of this logic on CONFIG_MMU explicitly.
CONFIG_PT_RECLAIM however does have a strict dependency on CONFIG_MMU, so make this dependency explicit.
Additionally, correct comments to remove references to non-RCU page table gathering and make it clear that this is not 'semi-RCU', nor has it been since commit 1fb3d8c20bfa ("mm/mmu_gather: replace IPI with synchronize_rcu() when batch allocation fails").
With this change in place the kernel policy is now that userspace page tables are freed after an RCU grace period, and thus it is now safe to unconditionally perform page table walks under RCU, safe in the knowledge that page tables will not be freed underneath the walker.
This is all that is guaranteed, however, so naturally it is still incumbent upon page table walkers to ensure that the page table entries are as expected.
--- arch/Kconfig | 8 ----- arch/alpha/Kconfig | 1 - arch/arc/Kconfig | 1 - arch/arm/Kconfig | 1 - arch/arm64/Kconfig | 1 - arch/csky/Kconfig | 1 - arch/hexagon/Kconfig | 1 - arch/loongarch/Kconfig | 1 - arch/m68k/Kconfig | 1 - arch/microblaze/Kconfig | 1 - arch/mips/Kconfig | 1 - arch/nios2/Kconfig | 1 - arch/openrisc/Kconfig | 1 - arch/parisc/Kconfig | 1 - arch/powerpc/Kconfig | 1 - arch/riscv/Kconfig | 1 - arch/s390/Kconfig | 1 - arch/sh/Kconfig | 1 - arch/sparc/Kconfig | 2 -- arch/sparc/include/asm/tlb_64.h | 2 -- arch/um/Kconfig | 1 - arch/x86/Kconfig | 1 - arch/xtensa/Kconfig | 1 - include/asm-generic/tlb.h | 66 ++++++----------------------------------- mm/Kconfig | 2 +- mm/gup.c | 5 ++-- mm/mmu_gather.c | 30 ++++--------------- 27 files changed, 18 insertions(+), 117 deletions(-)
arch/Kconfig +0 -8
@@ -526,13 +526,6 @@ config HAVE_ARCH_JUMP_LABEL config HAVE_ARCH_JUMP_LABEL_RELATIVE bool -config MMU_GATHER_TABLE_FREE - bool - -config MMU_GATHER_RCU_TABLE_FREE - bool - select MMU_GATHER_TABLE_FREE - config MMU_GATHER_PAGE_SIZE bool
@@ -548,7 +541,6 @@ config MMU_GATHER_MERGE_VMAS config MMU_GATHER_NO_GATHER bool - depends on MMU_GATHER_TABLE_FREE config ARCH_WANT_IRQS_OFF_ACTIVATE_MM bool
arch/alpha/Kconfig +0 -1
@@ -42,7 +42,6 @@ config ALPHA select ARCH_STACKWALK select CPU_NO_EFFICIENT_FFS if !ALPHA_EV67 select MMU_GATHER_NO_RANGE - select MMU_GATHER_RCU_TABLE_FREE select SPARSEMEM_EXTREME if SPARSEMEM select ZONE_DMA select TRACE_IRQFLAGS_SUPPORT
arch/arc/Kconfig +0 -1
@@ -47,7 +47,6 @@ config ARC select HAVE_SYSCALL_TRACEPOINTS select IRQ_DOMAIN select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA select OF select OF_EARLY_FLATTREE
arch/arm/Kconfig +0 -1
@@ -134,7 +134,6 @@ config ARM select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP select HAVE_POSIX_CPU_TIMERS_TASK_WORK - select MMU_GATHER_RCU_TABLE_FREE if MMU select HAVE_REGS_AND_STACK_ACCESS_API select HAVE_RSEQ select HAVE_RUST if CPU_LITTLE_ENDIAN && CPU_32v7 && !KASAN
arch/arm64/Kconfig +0 -1
@@ -221,7 +221,6 @@ config ARM64 select HAVE_RELIABLE_STACKTRACE select HAVE_POSIX_CPU_TIMERS_TASK_WORK select HAVE_FUNCTION_ARG_ACCESS_API - select MMU_GATHER_RCU_TABLE_FREE select HAVE_RSEQ select HAVE_RUST if RUSTC_SUPPORTS_ARM64 select HAVE_STACKPROTECTOR
arch/csky/Kconfig +0 -1
@@ -96,7 +96,6 @@ config CSKY select HAVE_SYSCALL_TRACEPOINTS select HOTPLUG_CORE_SYNC_DEAD if HOTPLUG_CPU select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE select MAY_HAVE_SPARSE_IRQ select MODULES_USE_ELF_RELA if MODULES select OF
arch/hexagon/Kconfig +0 -1
@@ -23,7 +23,6 @@ config HEXAGON # select HAVE_CLK select GENERIC_ATOMIC64 select HAVE_PERF_EVENTS - select MMU_GATHER_RCU_TABLE_FREE # GENERIC_ALLOCATOR is used by dma_alloc_coherent() select GENERIC_ALLOCATOR select GENERIC_IRQ_PROBE
arch/loongarch/Kconfig +0 -1
@@ -188,7 +188,6 @@ config LOONGARCH select IRQ_LOONGARCH_CPU select LOCK_MM_AND_FIND_VMA select MMU_GATHER_MERGE_VMAS if MMU - select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA if MODULES select NEED_PER_CPU_EMBED_FIRST_CHUNK select NEED_PER_CPU_PAGE_FIRST_CHUNK
arch/m68k/Kconfig +0 -1
@@ -37,7 +37,6 @@ config M68K select HAVE_MOD_ARCH_SPECIFIC select HAVE_UID16 select MMU_GATHER_NO_RANGE if MMU - select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_REL select MODULES_USE_ELF_RELA select NO_DMA if !MMU && !COLDFIRE
arch/microblaze/Kconfig +0 -1
@@ -41,7 +41,6 @@ config MICROBLAZE select PCI_SYSCALL if PCI select CPU_NO_EFFICIENT_FFS select MMU_GATHER_NO_RANGE - select MMU_GATHER_RCU_TABLE_FREE select SPARSE_IRQ select ZONE_DMA select TRACE_IRQFLAGS_SUPPORT
arch/mips/Kconfig +0 -1
@@ -97,7 +97,6 @@ config MIPS select IRQ_FORCED_THREADING select ISA if EISA select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_REL if MODULES select MODULES_USE_ELF_RELA if MODULES && 64BIT select PERF_USE_VMALLOC
arch/nios2/Kconfig +0 -1
@@ -19,7 +19,6 @@ config NIOS2 select HAVE_PAGE_SIZE_4KB select IRQ_DOMAIN select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA select OF select OF_EARLY_FLATTREE
arch/openrisc/Kconfig +0 -1
@@ -35,7 +35,6 @@ config OPENRISC select GENERIC_ATOMIC64 select GENERIC_CLOCKEVENTS_BROADCAST select GENERIC_SMP_IDLE_THREAD - select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA select HAVE_DEBUG_STACKOVERFLOW select OR1K_PIC
arch/parisc/Kconfig +0 -1
@@ -80,7 +80,6 @@ config PARISC select GENERIC_CLOCKEVENTS select CPU_NO_EFFICIENT_FFS select THREAD_INFO_IN_TASK - select MMU_GATHER_RCU_TABLE_FREE select NEED_DMA_MAP_STATE select NEED_SG_DMA_LENGTH select HAVE_ARCH_KGDB
arch/powerpc/Kconfig +0 -1
@@ -307,7 +307,6 @@ config PPC select KASAN_VMALLOC if KASAN && EXECMEM select LOCK_MM_AND_FIND_VMA select MMU_GATHER_PAGE_SIZE - select MMU_GATHER_RCU_TABLE_FREE select HAVE_ARCH_TLB_REMOVE_TABLE select MMU_GATHER_MERGE_VMAS select MMU_LAZY_TLB_SHOOTDOWN if PPC_BOOK3S_64
arch/riscv/Kconfig +0 -1
@@ -208,7 +208,6 @@ config RISCV select IRQ_FORCED_THREADING select KASAN_VMALLOC if KASAN select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_RELA if MODULES select OF select OF_EARLY_FLATTREE
arch/s390/Kconfig +0 -1
@@ -267,7 +267,6 @@ config S390 select LOCK_MM_AND_FIND_VMA select MMU_GATHER_MERGE_VMAS select MMU_GATHER_NO_GATHER - select MMU_GATHER_RCU_TABLE_FREE select MODULES_USE_ELF_RELA select NEED_DMA_MAP_STATE if PCI select NEED_PER_CPU_EMBED_FIRST_CHUNK
arch/sh/Kconfig +0 -1
@@ -62,7 +62,6 @@ config SUPERH select HAVE_SYSCALL_TRACEPOINTS select IRQ_FORCED_THREADING select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_RELA select NEED_SG_DMA_LENGTH select NO_DMA if !MMU && !DMA_COHERENT
arch/sparc/Kconfig +0 -2
@@ -64,7 +64,6 @@ config SPARC32 select HAVE_UID16 select HAVE_PAGE_SIZE_4KB select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE select HAVE_ARCH_TLB_REMOVE_TABLE select OLD_SIGACTION select ZONE_DMA
@@ -77,7 +76,6 @@ config SPARC64 select HAVE_FUNCTION_GRAPH_TRACER select HAVE_KRETPROBES select HAVE_KPROBES - select MMU_GATHER_RCU_TABLE_FREE select HAVE_ARCH_TLB_REMOVE_TABLE select MMU_GATHER_MERGE_VMAS select MMU_GATHER_NO_FLUSH_CACHE
arch/sparc/include/asm/tlb_64.h +0 -2
@@ -29,9 +29,7 @@ void flush_tlb_pending(void);* and therefore we don't need a TLBI when freeing page-table pages. */ -#ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE #define tlb_needs_table_invalidate () (false) -#endif #include <asm-generic/tlb.h>
arch/um/Kconfig +0 -1
@@ -44,7 +44,6 @@ config UML select HAVE_SYSCALL_TRACEPOINTS select THREAD_INFO_IN_TASK select SPARSE_IRQ - select MMU_GATHER_RCU_TABLE_FREE config MMU bool
arch/x86/Kconfig +0 -1
@@ -283,7 +283,6 @@ config X86 select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP select ASYNC_KERNEL_PGTABLE_FREE if IOMMU_SVA - select MMU_GATHER_RCU_TABLE_FREE select MMU_GATHER_MERGE_VMAS select HAVE_POSIX_CPU_TIMERS_TASK_WORK select HAVE_REGS_AND_STACK_ACCESS_API
arch/xtensa/Kconfig +0 -1
@@ -55,7 +55,6 @@ config XTENSA select HAVE_VIRT_CPU_ACCOUNTING_GEN select IRQ_DOMAIN select LOCK_MM_AND_FIND_VMA - select MMU_GATHER_RCU_TABLE_FREE if MMU select MODULES_USE_ELF_RELA select PERF_USE_VMALLOC select TRACE_IRQFLAGS_SUPPORT
include/asm-generic/tlb.h +9 -57
107 lines not on this page · raw message
mm/Kconfig +1 -1
@@ -1465,7 +1465,7 @@ config HAVE_ARCH_TLB_REMOVE_TABLE config PT_RECLAIM def_bool y - depends on MMU_GATHER_RCU_TABLE_FREE && !HAVE_ARCH_TLB_REMOVE_TABLE + depends on MMU && !HAVE_ARCH_TLB_REMOVE_TABLE help Try to reclaim empty user page table pages in paths other than munmap and exit_mmap path.
mm/gup.c +3 -2
@@ -2700,8 +2700,9 @@ EXPORT_SYMBOL(get_user_pages_unlocked);* Before activating this code, please be aware that the following assumptions * are currently made: * -* *) Either MMU_GATHER_RCU_TABLE_FREE is enabled, and tlb_remove_table() is used to -* free pages containing page tables or TLB flushing requires IPI broadcast. +* *) tlb_remove_table() is used to free pages containing page tables, with +* the free deferred until an RCU grace period has elapsed (see +* mm/mmu_gather.c). * * *) ptes can be read atomically by the architecture. *
mm/mmu_gather.c +5 -25
@@ -218,8 +218,6 @@ bool __tlb_remove_page_size(struct mmu_gather *tlb, struct page *page, int page_#endif /* MMU_GATHER_NO_GATHER */ -#ifdef CONFIG_MMU_GATHER_TABLE_FREE -static void __tlb_remove_table_free (struct mmu_table_batch *batch) {int i;
@@ -230,10 +228,8 @@ static void __tlb_remove_table_free(struct mmu_table_batch *batch)free_page ((unsigned long )batch); } -#ifdef CONFIG_MMU_GATHER_RCU_TABLE_FREE -/* -* Semi RCU freeing of the page directories. +* RCU freeing of the page directories. * * This is needed by some architectures to implement software pagetable walkers. *
@@ -259,13 +255,13 @@ static void __tlb_remove_table_free(struct mmu_table_batch *batch)* means. * * What we do is batch the freed directory pages (tables) and RCU free them. -* We use the sched RCU variant, as that guarantees that IRQ/preempt disabling -* holds off grace periods. +* Disabling IRQs or preemption holds off RCU grace periods, so this protects +* both rcu_read_lock() and IRQ-disabling walkers. * * However, in order to batch these pages we need to allocate storage, this * allocation is deep inside the MM code and can thus easily fail on memory -* pressure. To guarantee progress we fall back to single table freeing, see -* the implementation of tlb_remove_table_one(). +* pressure. To guarantee progress we fall back to single table freeing, which +* is also RCU-deferred - see the implementation of tlb_remove_table_one(). * */
@@ -315,15 +311,6 @@ void tlb_remove_table_sync_rcu(void)synchronize_rcu (); } -#else /* !CONFIG_MMU_GATHER_RCU_TABLE_FREE */ - -static void tlb_remove_table_free (struct mmu_table_batch *batch) -{ -__tlb_remove_table_free (batch); -} - -#endif /* CONFIG_MMU_GATHER_RCU_TABLE_FREE */ -/* * If we want tlb_remove_table() to imply TLB invalidates. */
@@ -403,13 +390,6 @@ static inline void tlb_table_init(struct mmu_gather *tlb) tlb->batch =NULL ; } -#else /* !CONFIG_MMU_GATHER_TABLE_FREE */ - -static inline void tlb_table_flush (struct mmu_gather *tlb) { } -static inline void tlb_table_init (struct mmu_gather *tlb) { } - -#endif /* CONFIG_MMU_GATHER_TABLE_FREE */ -static void tlb_flush_mmu_free (struct mmu_gather *tlb) {tlb_table_flush (tlb);
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 11/12] mm: userland pgtable freeing is RCU-safe now, remove leftover bits
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
Now every architecture has been converted to support
CONFIG_MMU_GATHER_RCU_TABLE_FREE, this configuration option no longer makes any sense to keep around.Therefore remove it, and remove all the dead code that existed for !CONFIG_MMU_GATHER_RCU_TABLE_FREE architectures previously.
Additionally,
CONFIG_MMU_GATHER_TABLE_FREEis no longer necessary, as all architectures instead useCONFIG_HAVE_ARCH_TLB_REMOVE_TABLEwhen a custom__tlb_remove_table()is required, so remove this too.A number of architectures only enabled
CONFIG_MMU_GATHER_RCU_TABLE_FREEifCONFIG_MMUwas set, however themmu_gatherlogic only actually does something meaningful ifCONFIG_MMUis set (mmu_gather.cis only compiled in this case, for instance).
[ ... ]
Quoted 4 lines
Reviewed-by Lance Yang <[email protected]>Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Lorenzo Stoakes (ARM)
maintainerPATCH v5 12/12 mm: change the contract for free_pgtables(), update docs
In reply to Lorenzo Stoakes (ARM)Now that page tables are freed after an RCU grace period, it is safe for read-only page table walkers to walk page table ranges that are being concurrently torn down, provided the mm is kept alive via mmgrab().
It is however unsafe for writers to do so, as they must obtain an appropriate lock to do so safely.
Update the pte_offset_map_lock()'s comment block to reflect this.
Similarly update the process addresses documentation.
--- Documentation/mm/process_addrs.rst | 11 +++++++++++ mm/pgtable-generic.c | 15 +++++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-)
Documentation/mm/process_addrs.rst +11 -0
@@ -537,6 +537,17 @@ We establish basic locking rules when interacting with page tables: * When changing a page table entry the page table lock for that page table **must** be held, except if you can safely assume nobody can access the page tables concurrently (such as on invocation of :c:func:`!free_pgtables`). +* Page tables may be *walked* under RCU alone, as page tables are freed only + 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 requires the page table lock + and one of the locks that excludes teardown (any one of the mmap, VMA or + rmap locks). +* When traversing page tables under RCU alone it is important to take care + when operating upon leaf entries - if the value is operated upon (for + instance getting the folio associated with a PTE) an appropriate lock must + be taken to prevent concurrent modification. * 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
mm/pgtable-generic.c +11 -4
@@ -385,10 +385,17 @@ pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,* Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will * be read-only/read-write protected. * -* Note that free_pgtables(), used after unmapping detached vmas, or when -* exiting the whole mm, does not take page table lock before freeing a page -* table, and may not use RCU at all: "outsiders" like khugepaged should avoid -* pte_offset_map() and co once the vma is detached from mm or mm_users is zero. +* Note that free_pgtables(), used after unmapping detached vmas or when exiting +* the whole mm, does not take a page table lock before freeing a page table. +* +* As page table freeing itself is RCU-safe, page table readers can safely run +* concurrently with page table teardown. +* +* However, writers CANNOT as, without a lock being held, nothing prevents +* concurrent teardown. +* +* Also note that the PGD itself is freed at mmdrop() time, not under RCU - so +* the walker must keep the mm alive either by pinning the mm or the VMA. */ pte_t *pte_offset_map_lock (struct mm_struct *mm,pmd_t *pmd,unsigned long addr,spinlock_t **ptlp)
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v5 12/12] mm: change the contract for free_pgtables(), update docs
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines
Now that page tables are freed after an RCU grace period, it is safe for read-only page table walkers to walk page table ranges that are being concurrently torn down, provided the mm is kept alive via
mmgrab().It is however unsafe for writers to do so, as they must obtain an appropriate lock to do so safely.
Quoted 6 lines
Update the pte_offset_map_lock()'s comment block to reflect this.
Similarly update the process addresses documentation.
Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Re: [PATCH v5 12/12] mm: change the contract for free_pgtables(), update docs
In reply to Lorenzo Stoakes (ARM)Quoted 7 lines · Lorenzo Stoakes (ARM)
Now that page tables are freed after an RCU grace period, it is safe for read-only page table walkers to walk page table ranges that are being concurrently torn down, provided the mm is kept alive via
mmgrab().It is however unsafe for writers to do so, as they must obtain an appropriate lock to do so safely.
Quoted 7 lines
Update the pte_offset_map_lock()'s comment block to reflect this.
Similarly update the process addresses documentation.
Signed-off-by Lorenzo Stoakes (ARM) <[email protected]>---
Signature · 3 lines
Cheers, David
Andrew Morton
maintainerQuoted 10 lines · On Fri, 25 Sep 2026 21:09:35 +0100 "Lorenzo Stoakes (ARM)" <[email protected]>…
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_FREEaltogether.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.
Thanks, I've updated mm-unstable to this version.
Quoted 4 lines
v5:
* Collected tags (thanks all! :)
* Updated 1/12 to correctly set result toSCAN_ALLOC_HUGE_PAGE_FAILshould the page table allocation fail, as per Lance.
Quoted 8 lines
* Updated 1/12 to rename
alloc_deposit_pte()toalloc_deposit_pte_table()and dropped the powerpc hash MMU paragraph from the commit message, as per David.
* Updated 9/12 to use a BH-disabling spinlock rather than an IRQ-safe one, as per Matthew.
* Updated the subject line of 11/12 to clarify that the patch removes now-unneeded code, as per David.
* Updated 12/12 documentation as per David, Lance.
Here's how v5 altered mm.git:
Documentation/mm/process_addrs.rst | 9 +++++++-- arch/m68k/mm/motorola.c | 16 +++++++--------- mm/khugepaged.c | 4 ++-- 3 files changed, 16 insertions(+), 13 deletions(-)
arch/m68k/mm/motorola.c renamed from arch/m68k/mm/motorola.c~b +7 -9
@@ -181,7 +181,7 @@ static void *add_pointer_table(struct mm new =PD_PTABLE (pt_addr);PD_MARKBITS (new) =ptable_mask (type) -1 ; - scoped_guard(spinlock_irqsave, &ptable_lock) + scoped_guard(spinlock_bh, &ptable_lock)list_add (new, &ptable_list[type]);return (pmd_t *)pt_addr;
@@ -191,16 +191,15 @@ void *get_pointer_table(struct mm_struct {unsigned int tmp, off;unsigned long mask; -unsigned long flags;ptable_desc *dp;void *ret; -spin_lock_irqsave (&ptable_lock, flags); +spin_lock_bh (&ptable_lock); dp = ptable_list[type].next; mask =list_empty (&ptable_list[type]) ?0 :PD_MARKBITS (dp);if (mask ==0 ) { -spin_unlock_irqrestore (&ptable_lock, flags); +spin_unlock_bh (&ptable_lock);return add_pointer_table (mm, type); }
@@ -213,7 +212,7 @@ void *get_pointer_table(struct mm_struct } ret =ptdesc_address (PD_PTDESC (dp)) + off; -spin_unlock_irqrestore (&ptable_lock, flags); +spin_unlock_bh (&ptable_lock);return ret; }
@@ -223,9 +222,8 @@ int free_pointer_table(void *table, intunsigned long ptable = (unsigned long )table;unsigned long pt_addr = ptable &PAGE_MASK ;unsigned int mask =1U << ((ptable - pt_addr)/ptable_size (type)); -unsigned long flags; -spin_lock_irqsave (&ptable_lock, flags); +spin_lock_bh (&ptable_lock); dp =PD_PTABLE (pt_addr);if (PD_MARKBITS (dp) & mask)
@@ -236,7 +234,7 @@ int free_pointer_table(void *table, intif (PD_MARKBITS (dp) ==ptable_mask (type)) {/* all tables in ptdesc are free, free ptdesc */ list_del (dp); -spin_unlock_irqrestore (&ptable_lock, flags); +spin_unlock_bh (&ptable_lock);mmu_page_dtor ((void *)pt_addr);pagetable_dtor_free (virt_to_ptdesc ((void *)pt_addr));
@@ -249,7 +247,7 @@ int free_pointer_table(void *table, intlist_move (dp, &ptable_list[type]); } -spin_unlock_irqrestore (&ptable_lock, flags); +spin_unlock_bh (&ptable_lock);return 0 ; }
Documentation/mm/process_addrs.rst renamed from Documentation/mm/process_addrs.rst~b +7 -2
@@ -541,8 +541,13 @@ 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 requires the page table - lock and one of the locks that excludes teardown (mmap or VMA lock). + before it is acted upon. Changing an entry requires the page table lock + and one of the locks that excludes teardown (any one of the mmap, VMA or + rmap locks). +* When traversing page tables under RCU alone it is important to take care + when operating upon leaf entries - if the value is operated upon (for + instance getting the folio associated with a PTE) an appropriate lock must + be taken to prevent concurrent modification. * 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
mm/khugepaged.c renamed from mm/khugepaged.c~b +2 -2
@@ -1278,7 +1278,7 @@ static enum scan_result alloc_charge_folreturn SCAN_SUCCEED ; } -static pgtable_t alloc_deposit_pte (struct mm_struct *mm) +static pgtable_t alloc_deposit_pte_table (struct mm_struct *mm) {/* * khugepaged is run from a kernel thread, so need to manually set the
@@ -1328,7 +1328,7 @@ static enum scan_result collapse_huge_pa }if (is_pmd_order (order)) { - pgtable =alloc_deposit_pte (mm); + pgtable =alloc_deposit_pte_table (mm);if (!pgtable) { result =SCAN_ALLOC_HUGE_PAGE_FAIL ; goto out_nolock;
_
This series was applied to riscv/linux.git (fixes) by Andrew Morton <[email protected]>:
Hello:
This series was applied to riscv/linux.git (fixes) by Andrew Morton <[email protected]>:
Quoted 9 lines · On Fri, 25 Sep 2026 21:09:35 +0100 you 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_FREEaltogether.This is important because it enables safe lockless page table walking under RCU alone.
[...]
Here is the summary with links:
- [v5,01/12] mm/khugepaged: deposit a newly allocated page table on collapse
https://git.kernel.org/riscv/c/eb823057e092 - [v5,02/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for most 2-level architectures https://git.kernel.org/riscv/c/d22254fd57f0 - [v5,03/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU riscv https://git.kernel.org/riscv/c/5ae75414416f - [v5,04/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for MMU arm https://git.kernel.org/riscv/c/8e645d98fb78 - [v5,05/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for arc, microblaze, xtensa https://git.kernel.org/riscv/c/799bd3c4be5f - [v5,06/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc64 https://git.kernel.org/riscv/c/461a24a81215 - [v5,07/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-coldfire https://git.kernel.org/riscv/c/ccdaaf591664 - [v5,08/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for sh-X2 https://git.kernel.org/riscv/c/a72df945113a - [v5,09/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for m68k-motorola https://git.kernel.org/riscv/c/0f5cbeebe00e - [v5,10/12] mm: enable MMU_GATHER_RCU_TABLE_FREE for sparc32 https://git.kernel.org/riscv/c/35dd856770f3 - [v5,11/12] mm: userland pgtable freeing is RCU-safe now, remove leftover bits https://git.kernel.org/riscv/c/35e340a47888 - [v5,12/12] mm: change the contract for free_pgtables(), update docs https://git.kernel.org/riscv/c/a5c0a39d0e57
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html