]> Git Repo - linux.git/commitdiff
mm: improve folio_likely_mapped_shared() using the mapcount of large folios
authorDavid Hildenbrand <[email protected]>
Tue, 9 Apr 2024 19:22:48 +0000 (21:22 +0200)
committerAndrew Morton <[email protected]>
Mon, 6 May 2024 00:53:28 +0000 (17:53 -0700)
We can now read the mapcount of large folios very efficiently.  Use it to
improve our handling of partially-mappable folios, falling back to making
a guess only in case the folio is not "obviously mapped shared".

We can now better detect partially-mappable folios where the first page is
not mapped as "mapped shared", reducing "false negatives"; but false
negatives are still possible.

While at it, fixup a wrong comment (false positive vs.  false negative)
for KSM folios.

Link: https://lkml.kernel.org/r/[email protected]
Signed-off-by: David Hildenbrand <[email protected]>
Reviewed-by: Yin Fengwei <[email protected]>
Cc: Chris Zankel <[email protected]>
Cc: Hugh Dickins <[email protected]>
Cc: John Paul Adrian Glaubitz <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Matthew Wilcox (Oracle) <[email protected]>
Cc: Max Filippov <[email protected]>
Cc: Miaohe Lin <[email protected]>
Cc: Muchun Song <[email protected]>
Cc: Naoya Horiguchi <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Richard Chang <[email protected]>
Cc: Rich Felker <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Yang Shi <[email protected]>
Cc: Yoshinori Sato <[email protected]>
Cc: Zi Yan <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
include/linux/mm.h

index 05947782162516472e4932f63b95ed55ebeee44b..c7470da5797f137a41c6faa2b815578831f0602e 100644 (file)
@@ -2184,7 +2184,7 @@ static inline size_t folio_size(struct folio *folio)
  *       indicate "mapped shared" (false positive) when two VMAs in the same MM
  *       cover the same file range.
  *    #. For (small) KSM folios, the return value can wrongly indicate "mapped
- *       shared" (false negative), when the folio is mapped multiple times into
+ *       shared" (false positive), when the folio is mapped multiple times into
  *       the same MM.
  *
  * Further, this function only considers current page table mappings that
@@ -2201,7 +2201,22 @@ static inline size_t folio_size(struct folio *folio)
  */
 static inline bool folio_likely_mapped_shared(struct folio *folio)
 {
-       return page_mapcount(folio_page(folio, 0)) > 1;
+       int mapcount = folio_mapcount(folio);
+
+       /* Only partially-mappable folios require more care. */
+       if (!folio_test_large(folio) || unlikely(folio_test_hugetlb(folio)))
+               return mapcount > 1;
+
+       /* A single mapping implies "mapped exclusively". */
+       if (mapcount <= 1)
+               return false;
+
+       /* If any page is mapped more than once we treat it "mapped shared". */
+       if (folio_entire_mapcount(folio) || mapcount > folio_nr_pages(folio))
+               return true;
+
+       /* Let's guess based on the first subpage. */
+       return atomic_read(&folio->_mapcount) > 0;
 }
 
 #ifndef HAVE_ARCH_MAKE_PAGE_ACCESSIBLE
This page took 0.070244 seconds and 4 git commands to generate.