mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb.patch removed from -mm tree
The quilt patch titled
Subject: mm/rmap: basic MM owner tracking for large folios (!hugetlb)
has been removed from the -mm tree. Its filename was
mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb.patch
This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
------------------------------------------------------
From: David Hildenbrand <[email protected]>
Subject: mm/rmap: basic MM owner tracking for large folios (!hugetlb)
Date: Mon, 3 Mar 2025 17:30:05 +0100
For small folios, we traditionally use the mapcount to decide whether it was "certainly mapped exclusively" by a single MM (mapcount == 1) or whether it "maybe mapped shared" by multiple MMs (mapcount > 1). For PMD-sized folios that were PMD-mapped, we were able to use a similar mechanism (single PMD mapping), but for PTE-mapped folios and in the future folios that span multiple PMDs, this does not work.
So we need a different mechanism to handle large folios. Let's add a new mechanism to detect whether a large folio is "certainly mapped exclusively", or whether it is "maybe mapped shared".
We'll use this information next to optimize CoW reuse for PTE-mapped anonymous THP, and to convert folio_likely_mapped_shared() to folio_maybe_mapped_shared(), independent of per-page mapcounts.
For each large folio, we'll have two slots, whereby a slot stores:
(1) an MM id: unique id assigned to each MM
(2) a per-MM mapcount
If a slot is unoccupied, it can be taken by the next MM that maps folio page.
In addition, we'll remember the current state -- "mapped exclusively" vs. "maybe mapped shared" -- and use a bit spinlock to sync on updates and to reduce the total number of atomic accesses on updates. In the future, it might be possible to squeeze a proper spinlock into "struct folio". For now, keep it simple, as we require the whole thing with THP only, that is incompatible with RT.
As we have to squeeze this information into the "struct folio" of even folios of order-1 (2 pages), and we generally want to reduce the required metadata, we'll assign each MM a unique ID that can fit into an int. In total, we can squeeze everything into 4x int (2x long) on 64bit.
32bit support is a bit challenging, because we only have 2x long == 2x int in order-1 folios. But we can make it work for now, because we neither expect many MMs nor very large folios on 32bit.
We will reliably detect folios as "mapped exclusively" vs. "mapped shared" as long as only two MMs map pages of a folio at one point in time -- for example with fork() and short-lived child processes, or with apps that hand over state from one instance to another.
As soon as three MMs are involved at the same time, we might detect "maybe mapped shared" although the folio is "mapped exclusively".
Example 1:
(1) App1 faults in a (shmem/file-backed) folio page -> Tracked as MM0
(2) App2 faults in a folio page -> Tracked as MM1
(4) App1 unmaps all folio pages
-> We will detect "mapped exclusively".
Example 2:
(1) App1 faults in a (shmem/file-backed) folio page -> Tracked as MM0
(2) App2 faults in a folio page -> Tracked as MM1
(3) App3 faults in a folio page -> No slot available, tracked as "unknown"
(4) App1 and App2 unmap all folio pages
-> We will detect "maybe mapped shared".
Make use of __always_inline to keep possible performance degradation when (un)mapping large folios to a minimum.
Note: by squeezing the two flags into the "unsigned long" that stores the MM ids, we can use non-atomic __bit_spin_unlock() and non-atomic setting/clearing of the "maybe mapped shared" bit, effectively not adding any new atomics on the hot path when updating the large mapcount + new metadata, which further helps reduce the runtime overhead in micro-benchmarks.
---
Documentation/mm/transhuge.rst | 8 + include/linux/mm_types.h | 49 +++++++++ include/linux/page-flags.h | 4 include/linux/rmap.h | 165 +++++++++++++++++++++++++++++++ kernel/fork.c | 36 ++++++ mm/Kconfig | 4 mm/internal.h | 5 mm/page_alloc.c | 10 + 8 files changed, 281 insertions(+)
Documentation/mm/transhuge.rst renamed from Documentation/mm/transhuge.rst~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +8 -0
@@ -120,11 +120,19 @@ pages: and also increment/decrement folio->_nr_pages_mapped by ENTIRELY_MAPPED when _entire_mapcount goes from -1 to 0 or 0 to -1. + We also maintain the two slots for tracking MM owners (MM ID and + corresponding mapcount), and the current status ("maybe mapped shared" vs. + "mapped exclusively"). + - map/unmap of individual pages with PTE entry increment/decrement page->_mapcount, increment/decrement folio->_large_mapcount and also increment/decrement folio->_nr_pages_mapped when page->_mapcount goes from -1 to 0 or 0 to -1 as this counts the number of pages mapped by PTE. + We also maintain the two slots for tracking MM owners (MM ID and + corresponding mapcount), and the current status ("maybe mapped shared" vs. + "mapped exclusively"). + split_huge_page internally has to distribute the refcounts in the head page to the tail pages before clearing all PG_head/tail bits from the page structures. It can be done easily for refcounts taken by page table
include/linux/mm_types.h renamed from include/linux/mm_types.h~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +49 -0
73 lines not on this page · raw message
include/linux/page-flags.h renamed from include/linux/page-flags.h~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +4 -0
@@ -1185,6 +1185,10 @@ static inline int folio_has_private(consreturn !!(folio->flags &PAGE_FLAGS_PRIVATE ); } +static inline bool folio_test_large_maybe_mapped_shared (const struct folio *folio) +{ +return test_bit (FOLIO_MM_IDS_SHARED_BITNUM , &folio->_mm_ids); +}#undef PF_ANY#undef PF_HEAD#undef PF_NO_TAIL
include/linux/rmap.h renamed from include/linux/rmap.h~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +165 -0
183 lines not on this page · raw message
kernel/fork.c renamed from kernel/fork.c~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +36 -0
@@ -802,6 +802,36 @@ static int dup_mmap(struct mm_struct *mm#define mm_free_pgd (mm)#endif /* CONFIG_MMU */ +#ifdef CONFIG_MM_ID +static DEFINE_IDA (mm_ida); + +static inline int mm_alloc_id (struct mm_struct *mm) +{ +int ret; + + ret =ida_alloc_range (&mm_ida,MM_ID_MIN ,MM_ID_MAX ,GFP_KERNEL ); +if (ret <0 ) +return ret; + mm->mm_id = ret; +return 0 ; +} + +static inline void mm_free_id (struct mm_struct *mm) +{ +const mm_id_t id = mm->mm_id; + + mm->mm_id =MM_ID_DUMMY ; +if (id ==MM_ID_DUMMY ) +return ; +if (WARN_ON_ONCE (id <MM_ID_MIN || id >MM_ID_MAX )) +return ; +ida_free (&mm_ida, id); +} +#else /* !CONFIG_MM_ID */ +static inline int mm_alloc_id (struct mm_struct *mm) {return 0 ; } +static inline void mm_free_id (struct mm_struct *mm) {} +#endif /* CONFIG_MM_ID */ +static void check_mm (struct mm_struct *mm) {int i;
@@ -905,6 +935,7 @@ void __mmdrop(struct mm_struct *mm)WARN_ON_ONCE (mm == current->active_mm);mm_free_pgd (mm); +mm_free_id (mm);destroy_context (mm);mmu_notifier_subscriptions_destroy (mm);check_mm (mm);
@@ -1289,6 +1320,9 @@ static struct mm_struct *mm_init(structif (mm_alloc_pgd (mm)) goto fail_nopgd; +if (mm_alloc_id (mm)) + goto fail_noid; +if (init_new_context (p, mm)) goto fail_nocontext;
@@ -1308,6 +1342,8 @@ fail_pcpu: fail_cid:destroy_context (mm); fail_nocontext: +mm_free_id (mm); +fail_noid:mm_free_pgd (mm); fail_nopgd:free_mm (mm);
mm/internal.h renamed from mm/internal.h~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +5 -0
@@ -763,6 +763,11 @@ static inline void prep_compound_head(stfolio_set_order (folio, order);atomic_set (&folio->_large_mapcount,-1 );atomic_set (&folio->_nr_pages_mapped,0 ); +if (IS_ENABLED (CONFIG_MM_ID )) { + folio->_mm_ids =0 ; + folio->_mm_id_mapcount[0 ] =-1 ; + folio->_mm_id_mapcount[1 ] =-1 ; + }if (IS_ENABLED (CONFIG_64BIT ) || order >1 ) {atomic_set (&folio->_pincount,0 );atomic_set (&folio->_entire_mapcount,-1 );
mm/Kconfig renamed from mm/Kconfig~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +4 -0
@@ -815,11 +815,15 @@ config ARCH_WANT_GENERAL_HUGETLB config ARCH_WANTS_THP_SWAP def_bool n +config MM_ID + def_bool n + menuconfig TRANSPARENT_HUGEPAGE bool "Transparent Hugepage Support" depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE && !PREEMPT_RT select COMPACTION select XARRAY_MULTI + select MM_ID help Transparent Hugepages allows the kernel to use huge pages and huge tlb transparently to the applications whenever possible.
mm/page_alloc.c renamed from mm/page_alloc.c~mm-rmap-basic-mm-owner-tracking-for-large-folios-hugetlb +10 -0
@@ -955,6 +955,16 @@ static int free_tail_page_prepare(structbad_page (page,"nonzero nr_pages_mapped" ); goto out; } +if (IS_ENABLED (CONFIG_MM_ID )) { +if (unlikely (folio->_mm_id_mapcount[0 ] !=-1 )) { +bad_page (page,"nonzero mm mapcount 0" ); + goto out; + } +if (unlikely (folio->_mm_id_mapcount[1 ] !=-1 )) { +bad_page (page,"nonzero mm mapcount 1" ); + goto out; + } + }if (IS_ENABLED (CONFIG_64BIT )) {if (unlikely (atomic_read (&folio->_entire_mapcount) +1 )) {bad_page (page,"nonzero entire_mapcount" );
_
Patches currently in -mm which might be from [email protected] are