1 From 0e0e9bd5f7b9d40fd03b70092367247d52da1db0 Mon Sep 17 00:00:00 2001
2 From: Yin Fengwei <fengwei.yin@intel.com>
3 Date: Tue, 8 Aug 2023 10:09:17 +0800
4 Subject: madvise:madvise_free_pte_range(): don't use mapcount() against large folio for sharing check
6 From: Yin Fengwei <fengwei.yin@intel.com>
8 commit 0e0e9bd5f7b9d40fd03b70092367247d52da1db0 upstream.
10 Commit 98b211d6415f ("madvise: convert madvise_free_pte_range() to use a
11 folio") replaced the page_mapcount() with folio_mapcount() to check
12 whether the folio is shared by other mapping.
14 It's not correct for large folios. folio_mapcount() returns the total
15 mapcount of large folio which is not suitable to detect whether the folio
18 Use folio_estimated_sharers() which returns a estimated number of shares.
19 That means it's not 100% correct. It should be OK for madvise case here.
21 User-visible effects is that the THP is skipped when user call madvise.
22 But the correct behavior is THP should be split and processed then.
24 NOTE: this change is a temporary fix to reduce the user-visible effects
25 before the long term fix from David is ready.
27 Link: https://lkml.kernel.org/r/20230808020917.2230692-4-fengwei.yin@intel.com
28 Fixes: 98b211d6415f ("madvise: convert madvise_free_pte_range() to use a folio")
29 Signed-off-by: Yin Fengwei <fengwei.yin@intel.com>
30 Reviewed-by: Yu Zhao <yuzhao@google.com>
31 Reviewed-by: Ryan Roberts <ryan.roberts@arm.com>
32 Cc: David Hildenbrand <david@redhat.com>
33 Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
34 Cc: Matthew Wilcox <willy@infradead.org>
35 Cc: Minchan Kim <minchan@kernel.org>
36 Cc: Vishal Moola (Oracle) <vishal.moola@gmail.com>
37 Cc: Yang Shi <shy828301@gmail.com>
38 Cc: <stable@vger.kernel.org>
39 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
40 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
43 1 file changed, 2 insertions(+), 2 deletions(-)
47 @@ -666,8 +666,8 @@ static int madvise_free_pte_range(pmd_t
48 * deactivate all pages.
50 if (folio_test_large(folio)) {
51 - if (folio_mapcount(folio) != 1)
53 + if (folio_estimated_sharers(folio) != 1)
56 if (!folio_trylock(folio)) {