Skip to content

Commit 3af4006

Browse files
David Hildenbrandopsiff
authored andcommitted
mm/rmap: simplify PageAnonExclusive sanity checks when adding anon rmap
mainline inclusion from mainline-v6.7-rc1 category: performance Let's sanity-check PageAnonExclusive vs. mapcount in page_add_anon_rmap() and hugepage_add_anon_rmap() after setting PageAnonExclusive simply by re-reading the mapcounts. We can stop initializing the "first" variable in page_add_anon_rmap() and no longer need an atomic_inc_and_test() in hugepage_add_anon_rmap(). While at it, switch to VM_WARN_ON_FOLIO(). [david@redhat.com: update check for doubly-mapped page] Link: https://lkml.kernel.org/r/d8e5a093-2e22-c14b-7e64-6da280398d9f@redhat.com Link: https://lkml.kernel.org/r/20230913125113.313322-6-david@redhat.com Signed-off-by: David Hildenbrand <david@redhat.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Mike Kravetz <mike.kravetz@oracle.com> Cc: Muchun Song <muchun.song@linux.dev> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> (cherry picked from commit 132b180) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
1 parent 5a1649a commit 3af4006

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

mm/rmap.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ void page_add_anon_rmap(struct page *page, struct vm_area_struct *vma,
12331233
atomic_t *mapped = &folio->_nr_pages_mapped;
12341234
int nr = 0, nr_pmdmapped = 0;
12351235
bool compound = flags & RMAP_COMPOUND;
1236-
bool first = true;
1236+
bool first;
12371237

12381238
/* Is page being mapped by PTE? Is this its first map to be added? */
12391239
if (likely(!compound)) {
@@ -1262,9 +1262,6 @@ void page_add_anon_rmap(struct page *page, struct vm_area_struct *vma,
12621262
}
12631263
}
12641264

1265-
VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page);
1266-
VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page);
1267-
12681265
if (nr_pmdmapped)
12691266
__lruvec_stat_mod_folio(folio, NR_ANON_THPS, nr_pmdmapped);
12701267
if (nr)
@@ -1286,6 +1283,10 @@ void page_add_anon_rmap(struct page *page, struct vm_area_struct *vma,
12861283
}
12871284
if (flags & RMAP_EXCLUSIVE)
12881285
SetPageAnonExclusive(page);
1286+
/* While PTE-mapping a THP we have a PMD and a PTE mapping. */
1287+
VM_WARN_ON_FOLIO((atomic_read(&page->_mapcount) > 0 ||
1288+
(folio_test_large(folio) && folio_entire_mapcount(folio) > 1)) &&
1289+
PageAnonExclusive(page), folio);
12891290

12901291
/*
12911292
* For large folio, only mlock it if it's fully mapped to VMA. It's
@@ -2586,15 +2587,14 @@ void hugepage_add_anon_rmap(struct page *page, struct vm_area_struct *vma,
25862587
unsigned long address, rmap_t flags)
25872588
{
25882589
struct folio *folio = page_folio(page);
2589-
int first;
25902590

25912591
VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio);
25922592

2593-
first = atomic_inc_and_test(&folio->_entire_mapcount);
2594-
VM_BUG_ON_PAGE(!first && (flags & RMAP_EXCLUSIVE), page);
2595-
VM_BUG_ON_PAGE(!first && PageAnonExclusive(page), page);
2593+
atomic_inc(&folio->_entire_mapcount);
25962594
if (flags & RMAP_EXCLUSIVE)
25972595
SetPageAnonExclusive(page);
2596+
VM_WARN_ON_FOLIO(folio_entire_mapcount(folio) > 1 &&
2597+
PageAnonExclusive(page), folio);
25982598
}
25992599

26002600
void hugepage_add_new_anon_rmap(struct folio *folio,

0 commit comments

Comments
 (0)