mmthprmap-simplify-compound-page-mapcount-handling.patch removed from -mm tree
The quilt patch titled
Subject: mm,thp,rmap: simplify compound page mapcount handling
has been removed from the -mm tree. Its filename was
mmthprmap-simplify-compound-page-mapcount-handling.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: Hugh Dickins <[email protected]>
Subject: mm,thp,rmap: simplify compound page mapcount handling
Date: Wed, 2 Nov 2022 18:51:38 -0700 (PDT)
Compound page (folio) mapcount calculations have been different for anon and file (or shmem) THPs, and involved the obscure PageDoubleMap flag. And each huge mapping and unmapping of a file (or shmem) THP involved atomically incrementing and decrementing the mapcount of every subpage of that huge page, dirtying many struct page cachelines.
Add subpages_mapcount field to the struct folio and first tail page, so that the total of subpage mapcounts is available in one place near the
head: then page_mapcount() and total_mapcount() and page_mapped(), and their folio equivalents, are so quick that anon and file and hugetlb don't need to be optimized differently. Delete the unloved PageDoubleMap.
page_add and page_remove rmap functions must now maintain the subpages_mapcount as well as the subpage _mapcount, when dealing with pte mappings of huge pages; and correct maintenance of NR_ANON_MAPPED and NR_FILE_MAPPED statistics still needs reading through the subpages, using nr_subpages_unmapped() - but only when first or last pmd mapping finds subpages_mapcount raised (double-map case, not the common case).
But are those counts (used to decide when to split an anon THP, and in vmscan's pagecache_reclaimable heuristic) correctly maintained? Not
quite: since page_remove_rmap() (and also split_huge_pmd()) is often called without page lock, there can be races when a subpage pte mapcount 0<->1 while compound pmd mapcount 0<->1 is scanning - races which the previous implementation had prevented. The statistics might become inaccurate, and even drift down until they underflow through 0. That is not good enough, but is better dealt with in a followup patch.
Update a few comments on first and second tail page overlaid fields. hugepage_add_new_anon_rmap() has to "increment" compound_mapcount, but subpages_mapcount and compound_pincount are already correctly at 0, so delete its reinitialization of compound_pincount.
A simple 100 X munmap(mmap(2GB, MAP_SHARED|MAP_POPULATE, tmpfs), 2GB) took 18 seconds on small pages, and used to take 1 second on huge pages, but now takes 119 milliseconds on huge pages. Mapping by pmds a second time used to take 860ms and now takes 92ms; mapping by pmds after mapping by ptes (when the scan is needed) used to take 870ms and now takes 495ms. But there might be some benchmarks which would show a slowdown, because tail struct pages now fall out of cache until final freeing checks them.
---
Documentation/mm/transhuge.rst | 18 --- include/linux/mm.h | 85 +++++++++++++----- include/linux/mm_types.h | 21 +++- include/linux/page-flags.h | 21 ---- include/linux/rmap.h | 2 mm/debug.c | 5 - mm/folio-compat.c | 6 - mm/huge_memory.c | 36 +------ mm/hugetlb.c | 2 mm/khugepaged.c | 11 -- mm/page_alloc.c | 27 +++-- mm/rmap.c | 142 +++++++++++++++++-------------- mm/util.c | 79 ----------------- 13 files changed, 194 insertions(+), 261 deletions(-)
Documentation/mm/transhuge.rst renamed from Documentation/mm/transhuge.rst~mmthprmap-simplify-compound-page-mapcount-handling +0 -18
@@ -125,24 +125,6 @@ pages: ->_mapcount of all sub-pages in order to have race-free detection of last unmap of subpages. -PageDoubleMap() indicates that the page is *possibly* mapped with PTEs. - -For anonymous pages, PageDoubleMap() also indicates ->_mapcount in all -subpages is offset up by one. This additional reference is required to -get race-free detection of unmap of subpages when we have them mapped with -both PMDs and PTEs. - -This optimization is required to lower the overhead of per-subpage mapcount -tracking. The alternative is to alter ->_mapcount in all subpages on each -map/unmap of the whole compound page. - -For anonymous pages, we set PG_double_map when a PMD of the page is split -for the first time, but still have a PMD mapping. The additional references -go away with the last compound_mapcount. - -File pages get PG_double_map set on the first map of the page with PTE and -goes away when the page gets evicted from the page cache. - 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.h renamed from include/linux/mm.h~mmthprmap-simplify-compound-page-mapcount-handling +62 -23
127 lines not on this page · raw message
include/linux/mm_types.h renamed from include/linux/mm_types.h~mmthprmap-simplify-compound-page-mapcount-handling +18 -3
@@ -142,6 +142,7 @@ struct page {unsigned char compound_dtor;unsigned char compound_order;atomic_t compound_mapcount; +atomic_t subpages_mapcount;atomic_t compound_pincount;#ifdef CONFIG_64BIT unsigned int compound_nr;/* 1 << compound_order */
@@ -270,7 +271,8 @@ struct page {* @_head_1: Points to the folio. Do not use. * @_folio_dtor: Which destructor to use for this folio. * @_folio_order: Do not use directly, call folio_order(). -* @_total_mapcount: Do not use directly, call folio_entire_mapcount(). +* @_compound_mapcount: Do not use directly, call folio_entire_mapcount(). +* @_subpages_mapcount: Do not use directly, call folio_mapcount(). * @_pincount: Do not use directly, call folio_maybe_dma_pinned(). * @_folio_nr_pages: Do not use directly, call folio_nr_pages(). * @_flags_2: For alignment. Do not use.
@@ -323,7 +325,8 @@ struct folio {unsigned long _head_1;unsigned char _folio_dtor;unsigned char _folio_order; -atomic_t _total_mapcount; +atomic_t _compound_mapcount; +atomic_t _subpages_mapcount;atomic_t _pincount;#ifdef CONFIG_64BIT unsigned int _folio_nr_pages;
@@ -365,7 +368,8 @@ FOLIO_MATCH(flags, _flags_1);FOLIO_MATCH (compound_head, _head_1);FOLIO_MATCH (compound_dtor, _folio_dtor);FOLIO_MATCH (compound_order, _folio_order); -FOLIO_MATCH (compound_mapcount, _total_mapcount); +FOLIO_MATCH (compound_mapcount, _compound_mapcount); +FOLIO_MATCH (subpages_mapcount, _subpages_mapcount);FOLIO_MATCH (compound_pincount, _pincount);#ifdef CONFIG_64BIT FOLIO_MATCH (compound_nr, _folio_nr_pages);
@@ -388,11 +392,22 @@ static inline atomic_t *folio_mapcount_preturn &tail->compound_mapcount; } +static inline atomic_t *folio_subpages_mapcount_ptr (struct folio *folio) +{ +struct page *tail = &folio->page +1 ; +return &tail->subpages_mapcount; +} +static inline atomic_t *compound_mapcount_ptr (struct page *page) {return &page[1 ].compound_mapcount; } +static inline atomic_t *subpages_mapcount_ptr (struct page *page) +{ +return &page[1 ].subpages_mapcount; +} +static inline atomic_t *compound_pincount_ptr (struct page *page) {return &page[1 ].compound_pincount;
include/linux/page-flags.h renamed from include/linux/page-flags.h~mmthprmap-simplify-compound-page-mapcount-handling +0 -21
@@ -176,9 +176,6 @@ enum pageflags {/* SLOB */ PG_slob_free = PG_private, -/* Compound pages. Stored in first tail page's flags */ - PG_double_map = PG_workingset, - #ifdef CONFIG_MEMORY_FAILURE/* * Compound pages. Stored in first tail page's flags.
@@ -874,29 +871,11 @@ static inline int PageTransTail(struct p {return PageTail (page); } - -/* -* PageDoubleMap indicates that the compound page is mapped with PTEs as well -* as PMDs. -* -* This is required for optimization of rmap operations for THP: we can postpone -* per small page mapcount accounting (and its overhead from atomic operations) -* until the first PMD split. -* -* For the page PageDoubleMap means ->_mapcount in all sub-pages is offset up -* by one. This reference will go away with last compound_mapcount. -* -* See also __split_huge_pmd_locked() and page_remove_anon_compound_rmap(). -*/ -PAGEFLAG(DoubleMap, double_map, PF_SECOND) - TESTSCFLAG(DoubleMap, double_map, PF_SECOND)#else TESTPAGEFLAG_FALSE(TransHuge, transhuge) TESTPAGEFLAG_FALSE(TransCompound, transcompound) TESTPAGEFLAG_FALSE(TransCompoundMap, transcompoundmap) TESTPAGEFLAG_FALSE(TransTail, transtail) -PAGEFLAG_FALSE(DoubleMap, double_map) - TESTSCFLAG_FALSE(DoubleMap, double_map)#endif #if defined(CONFIG_MEMORY_FAILURE ) && defined(CONFIG_TRANSPARENT_HUGEPAGE )
include/linux/rmap.h renamed from include/linux/rmap.h~mmthprmap-simplify-compound-page-mapcount-handling +2 -0
@@ -206,6 +206,8 @@ void hugepage_add_new_anon_rmap(struct pstatic inline void __page_dup_rmap (struct page *page,bool compound) { +if (!compound &&PageCompound (page)) +atomic_inc (subpages_mapcount_ptr (compound_head (page)));atomic_inc (compound ?compound_mapcount_ptr (page) : &page->_mapcount); }
mm/debug.c renamed from mm/debug.c~mmthprmap-simplify-compound-page-mapcount-handling +3 -2
@@ -94,9 +94,10 @@ static void __dump_page(struct page *pag page,page_ref_count (head), mapcount, mapping, page_to_pgoff(page), page_to_pfn(page));if (compound) { -pr_warn ("head:%p order:%u compound_mapcount:%d compound_pincount:%d\n" , +pr_warn ("head:%p order:%u compound_mapcount:%d subpages_mapcount:%d compound_pincount:%d\n" , head,compound_order (head), -folio_entire_mapcount (folio), +head_compound_mapcount (head), +head_subpages_mapcount (head),head_compound_pincount (head)); }
mm/folio-compat.c renamed from mm/folio-compat.c~mmthprmap-simplify-compound-page-mapcount-handling +0 -6
@@ -39,12 +39,6 @@ void wait_for_stable_page(struct page *p }EXPORT_SYMBOL_GPL (wait_for_stable_page); -bool page_mapped (struct page *page) -{ -return folio_mapped (page_folio (page)); -} -EXPORT_SYMBOL (page_mapped); -void mark_page_accessed (struct page *page) {folio_mark_accessed (page_folio (page));
mm/huge_memory.c renamed from mm/huge_memory.c~mmthprmap-simplify-compound-page-mapcount-handling +8 -28
@@ -2142,6 +2142,7 @@ static void __split_huge_pmd_locked(struVM_BUG_ON_PAGE (!page_count (page), page);page_ref_add (page,HPAGE_PMD_NR -1 ); +atomic_add (HPAGE_PMD_NR ,subpages_mapcount_ptr (page));/* * Without "freeze", we'll simply split the PMD, propagating the
@@ -2225,33 +2226,8 @@ static void __split_huge_pmd_locked(strupte_unmap (pte); } -if (!pmd_migration) { -/* -* Set PG_double_map before dropping compound_mapcount to avoid -* false-negative page_mapped(). -*/ -if (compound_mapcount (page) >1 && - !TestSetPageDoubleMap (page)) { -for (i =0 ; i <HPAGE_PMD_NR ; i++) -atomic_inc (&page[i]._mapcount); - } - -lock_page_memcg (page); -if (atomic_add_negative (-1 ,compound_mapcount_ptr (page))) { -/* Last compound_mapcount is gone. */ -__mod_lruvec_page_state (page,NR_ANON_THPS , - -HPAGE_PMD_NR ); -if (TestClearPageDoubleMap (page)) { -/* No need in mapcount reference anymore */ -for (i =0 ; i <HPAGE_PMD_NR ; i++) -atomic_dec (&page[i]._mapcount); - } - } -unlock_page_memcg (page); - -/* Above is effectively page_remove_rmap(page, vma, true) */ -munlock_vma_page (page, vma, true); - } +if (!pmd_migration) +page_remove_rmap (page, vma, true);smp_wmb ();/* make pte visible before pmd */ pmd_populate (mm, pmd, pgtable);
@@ -2453,7 +2429,7 @@ static void __split_huge_page_tail(struc (1L << PG_dirty) | LRU_GEN_MASK | LRU_REFS_MASK)); -/* ->mapping in first tail page is compound_mapcount */ +/* ->mapping in first and second tail page is replaced by other uses */ VM_BUG_ON_PAGE (tail >2 && page_tail->mapping !=TAIL_MAPPING , page_tail); page_tail->mapping = head->mapping;
@@ -2463,6 +2439,10 @@ static void __split_huge_page_tail(struc* page->private should not be set in tail pages with the exception * of swap cache pages that store the swp_entry_t in tail pages. * Fix up and warn once if private is unexpectedly set. +* +* What of 32-bit systems, on which head[1].compound_pincount overlays +* head[1].private? No problem: THP_SWAP is not enabled on 32-bit, and +* compound_pincount must be 0 for folio_ref_freeze() to have succeeded. */ if (!folio_test_swapcache (page_folio (head))) {VM_WARN_ON_ONCE_PAGE (page_tail->private !=0 , page_tail);
mm/hugetlb.c renamed from mm/hugetlb.c~mmthprmap-simplify-compound-page-mapcount-handling +2 -0
@@ -1333,6 +1333,7 @@ static void __destroy_compound_gigantic_struct page *p;atomic_set (compound_mapcount_ptr (page),0 ); +atomic_set (subpages_mapcount_ptr (page),0 );atomic_set (compound_pincount_ptr (page),0 );for (i =1 ; i < nr_pages; i++) {
@@ -1852,6 +1853,7 @@ static bool __prep_compound_gigantic_pagset_compound_head (p, page); }atomic_set (compound_mapcount_ptr (page),-1 ); +atomic_set (subpages_mapcount_ptr (page),0 );atomic_set (compound_pincount_ptr (page),0 );return true;
mm/khugepaged.c renamed from mm/khugepaged.c~mmthprmap-simplify-compound-page-mapcount-handling +2 -9
@@ -1238,15 +1238,8 @@ static int hpage_collapse_scan_pmd(struc/* * Check if the page has any GUP (or other external) pins. * -* Here the check is racy it may see total_mapcount > refcount -* in some cases. -* For example, one process with one forked child process. -* The parent has the PMD split due to MADV_DONTNEED, then -* the child is trying unmap the whole PMD, but khugepaged -* may be scanning the parent between the child has -* PageDoubleMap flag cleared and dec the mapcount. So -* khugepaged may see total_mapcount > refcount. -* +* Here the check may be racy: +* it may see total_mapcount > refcount in some cases? * But such case is ephemeral we could always retry collapse * later. However it may report false positive if the page * has excessive GUP pins (i.e. 512). Anyway the same check
mm/page_alloc.c renamed from mm/page_alloc.c~mmthprmap-simplify-compound-page-mapcount-handling +16 -11
@@ -798,6 +798,7 @@ static void prep_compound_head(struct paset_compound_page_dtor (page,COMPOUND_PAGE_DTOR );set_compound_order (page, order);atomic_set (compound_mapcount_ptr (page),-1 ); +atomic_set (subpages_mapcount_ptr (page),0 );atomic_set (compound_pincount_ptr (page),0 ); }
@@ -1324,11 +1325,19 @@ static int free_tail_pages_check(struct }switch (page - head_page) {case 1 : -/* the first tail page: ->mapping may be compound_mapcount() */ -if (unlikely (compound_mapcount (page))) { +/* the first tail page: these may be in place of ->mapping */ +if (unlikely (head_compound_mapcount (head_page))) {bad_page (page,"nonzero compound_mapcount" ); goto out; } +if (unlikely (head_subpages_mapcount (head_page))) { +bad_page (page,"nonzero subpages_mapcount" ); + goto out; + } +if (unlikely (head_compound_pincount (head_page))) { +bad_page (page,"nonzero compound_pincount" ); + goto out; + }break ;case 2 :/*
@@ -1431,10 +1440,8 @@ static __always_inline bool free_pages_pVM_BUG_ON_PAGE (compound &&compound_order (page) != order, page); -if (compound) { -ClearPageDoubleMap (page); +if (compound)ClearPageHasHWPoisoned (page); - }for (i =1 ; i < (1 << order); i++) {if (compound) bad +=free_tail_pages_check (page, page + i);
@@ -6874,13 +6881,11 @@ static void __ref memmap_init_compound(sset_page_count (page,0 );/* -* The first tail page stores compound_mapcount_ptr() and -* compound_order() and the second tail page stores -* compound_pincount_ptr(). Call prep_compound_head() after -* the first and second tail pages have been initialized to -* not have the data overwritten. +* The first tail page stores important compound page info. +* Call prep_compound_head() after the first tail page has +* been initialized, to not have the data overwritten. */ -if (pfn == head_pfn +2 ) +if (pfn == head_pfn +1 )prep_compound_head (head, order); } }
mm/rmap.c renamed from mm/rmap.c~mmthprmap-simplify-compound-page-mapcount-handling +81 -61
263 lines not on this page · raw message
mm/util.c renamed from mm/util.c~mmthprmap-simplify-compound-page-mapcount-handling +0 -79
91 lines not on this page · raw message
_
Patches currently in -mm which might be from [email protected] are