--- /dev/null
+From 2f406263e3e954aa24c1248edcfa9be0c1bb30fa Mon Sep 17 00:00:00 2001
+From: Yin Fengwei <fengwei.yin@intel.com>
+Date: Tue, 8 Aug 2023 10:09:15 +0800
+Subject: madvise:madvise_cold_or_pageout_pte_range(): don't use mapcount() against large folio for sharing check
+
+From: Yin Fengwei <fengwei.yin@intel.com>
+
+commit 2f406263e3e954aa24c1248edcfa9be0c1bb30fa upstream.
+
+Patch series "don't use mapcount() to check large folio sharing", v2.
+
+In madvise_cold_or_pageout_pte_range() and madvise_free_pte_range(),
+folio_mapcount() is used to check whether the folio is shared. But it's
+not correct as folio_mapcount() returns total mapcount of large folio.
+
+Use folio_estimated_sharers() here as the estimated number is enough.
+
+This patchset will fix the cases:
+User space application call madvise() with MADV_FREE, MADV_COLD and
+MADV_PAGEOUT for specific address range. There are THP mapped to the
+range. Without the patchset, the THP is skipped. With the patch, the
+THP will be split and handled accordingly.
+
+David reported the cow self test skip some cases because of MADV_PAGEOUT
+skip THP:
+https://lore.kernel.org/linux-mm/9e92e42d-488f-47db-ac9d-75b24cd0d037@intel.com/T/#mbf0f2ec7fbe45da47526de1d7036183981691e81
+and I confirmed this patchset make it work again.
+
+
+This patch (of 3):
+
+Commit 07e8c82b5eff ("madvise: convert madvise_cold_or_pageout_pte_range()
+to use folios") replaced the page_mapcount() with folio_mapcount() to
+check whether the folio is shared by other mapping.
+
+It's not correct for large folio. folio_mapcount() returns the total
+mapcount of large folio which is not suitable to detect whether the folio
+is shared.
+
+Use folio_estimated_sharers() which returns a estimated number of shares.
+That means it's not 100% correct. It should be OK for madvise case here.
+
+User-visible effects is that the THP is skipped when user call madvise.
+But the correct behavior is THP should be split and processed then.
+
+NOTE: this change is a temporary fix to reduce the user-visible effects
+before the long term fix from David is ready.
+
+Link: https://lkml.kernel.org/r/20230808020917.2230692-1-fengwei.yin@intel.com
+Link: https://lkml.kernel.org/r/20230808020917.2230692-2-fengwei.yin@intel.com
+Fixes: 07e8c82b5eff ("madvise: convert madvise_cold_or_pageout_pte_range() to use folios")
+Signed-off-by: Yin Fengwei <fengwei.yin@intel.com>
+Reviewed-by: Yu Zhao <yuzhao@google.com>
+Reviewed-by: Ryan Roberts <ryan.roberts@arm.com>
+Cc: David Hildenbrand <david@redhat.com>
+Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
+Cc: Matthew Wilcox <willy@infradead.org>
+Cc: Minchan Kim <minchan@kernel.org>
+Cc: Vishal Moola (Oracle) <vishal.moola@gmail.com>
+Cc: Yang Shi <shy828301@gmail.com>
+Cc: <stable@vger.kernel.org>
+Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ mm/madvise.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+--- a/mm/madvise.c
++++ b/mm/madvise.c
+@@ -376,7 +376,7 @@ static int madvise_cold_or_pageout_pte_r
+ folio = pfn_folio(pmd_pfn(orig_pmd));
+
+ /* Do not interfere with other mappings of this folio */
+- if (folio_mapcount(folio) != 1)
++ if (folio_estimated_sharers(folio) != 1)
+ goto huge_unlock;
+
+ if (pageout_anon_only_filter && !folio_test_anon(folio))
+@@ -448,7 +448,7 @@ regular_folio:
+ * are sure it's worth. Split it if we are only owner.
+ */
+ if (folio_test_large(folio)) {
+- if (folio_mapcount(folio) != 1)
++ if (folio_estimated_sharers(folio) != 1)
+ break;
+ if (pageout_anon_only_filter && !folio_test_anon(folio))
+ break;
--- /dev/null
+From 0e0e9bd5f7b9d40fd03b70092367247d52da1db0 Mon Sep 17 00:00:00 2001
+From: Yin Fengwei <fengwei.yin@intel.com>
+Date: Tue, 8 Aug 2023 10:09:17 +0800
+Subject: madvise:madvise_free_pte_range(): don't use mapcount() against large folio for sharing check
+
+From: Yin Fengwei <fengwei.yin@intel.com>
+
+commit 0e0e9bd5f7b9d40fd03b70092367247d52da1db0 upstream.
+
+Commit 98b211d6415f ("madvise: convert madvise_free_pte_range() to use a
+folio") replaced the page_mapcount() with folio_mapcount() to check
+whether the folio is shared by other mapping.
+
+It's not correct for large folios. folio_mapcount() returns the total
+mapcount of large folio which is not suitable to detect whether the folio
+is shared.
+
+Use folio_estimated_sharers() which returns a estimated number of shares.
+That means it's not 100% correct. It should be OK for madvise case here.
+
+User-visible effects is that the THP is skipped when user call madvise.
+But the correct behavior is THP should be split and processed then.
+
+NOTE: this change is a temporary fix to reduce the user-visible effects
+before the long term fix from David is ready.
+
+Link: https://lkml.kernel.org/r/20230808020917.2230692-4-fengwei.yin@intel.com
+Fixes: 98b211d6415f ("madvise: convert madvise_free_pte_range() to use a folio")
+Signed-off-by: Yin Fengwei <fengwei.yin@intel.com>
+Reviewed-by: Yu Zhao <yuzhao@google.com>
+Reviewed-by: Ryan Roberts <ryan.roberts@arm.com>
+Cc: David Hildenbrand <david@redhat.com>
+Cc: Kefeng Wang <wangkefeng.wang@huawei.com>
+Cc: Matthew Wilcox <willy@infradead.org>
+Cc: Minchan Kim <minchan@kernel.org>
+Cc: Vishal Moola (Oracle) <vishal.moola@gmail.com>
+Cc: Yang Shi <shy828301@gmail.com>
+Cc: <stable@vger.kernel.org>
+Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
+Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
+---
+ mm/madvise.c | 4 ++--
+ 1 file changed, 2 insertions(+), 2 deletions(-)
+
+--- a/mm/madvise.c
++++ b/mm/madvise.c
+@@ -666,8 +666,8 @@ static int madvise_free_pte_range(pmd_t
+ * deactivate all pages.
+ */
+ if (folio_test_large(folio)) {
+- if (folio_mapcount(folio) != 1)
+- goto out;
++ if (folio_estimated_sharers(folio) != 1)
++ break;
+ folio_get(folio);
+ if (!folio_trylock(folio)) {
+ folio_put(folio);