]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob
8a94aeb40140c84df19ad196712f6dcd4f4b0878
[thirdparty/kernel/stable-queue.git] /
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
5
6 From: Yin Fengwei <fengwei.yin@intel.com>
7
8 commit 0e0e9bd5f7b9d40fd03b70092367247d52da1db0 upstream.
9
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.
13
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
16 is shared.
17
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.
20
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.
23
24 NOTE: this change is a temporary fix to reduce the user-visible effects
25 before the long term fix from David is ready.
26
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>
41 ---
42 include/linux/mm.h | 19 +++++++++++++++++++
43 mm/madvise.c | 4 ++--
44 2 files changed, 21 insertions(+), 2 deletions(-)
45
46 --- a/include/linux/mm.h
47 +++ b/include/linux/mm.h
48 @@ -1727,6 +1727,25 @@ static inline size_t folio_size(struct f
49 return PAGE_SIZE << folio_order(folio);
50 }
51
52 +/**
53 + * folio_estimated_sharers - Estimate the number of sharers of a folio.
54 + * @folio: The folio.
55 + *
56 + * folio_estimated_sharers() aims to serve as a function to efficiently
57 + * estimate the number of processes sharing a folio. This is done by
58 + * looking at the precise mapcount of the first subpage in the folio, and
59 + * assuming the other subpages are the same. This may not be true for large
60 + * folios. If you want exact mapcounts for exact calculations, look at
61 + * page_mapcount() or folio_total_mapcount().
62 + *
63 + * Return: The estimated number of processes sharing a folio.
64 + */
65 +static inline int folio_estimated_sharers(struct folio *folio)
66 +{
67 + return page_mapcount(folio_page(folio, 0));
68 +}
69 +
70 +
71 #ifndef HAVE_ARCH_MAKE_PAGE_ACCESSIBLE
72 static inline int arch_make_page_accessible(struct page *page)
73 {
74 --- a/mm/madvise.c
75 +++ b/mm/madvise.c
76 @@ -654,8 +654,8 @@ static int madvise_free_pte_range(pmd_t
77 * deactivate all pages.
78 */
79 if (folio_test_large(folio)) {
80 - if (folio_mapcount(folio) != 1)
81 - goto out;
82 + if (folio_estimated_sharers(folio) != 1)
83 + break;
84 folio_get(folio);
85 if (!folio_trylock(folio)) {
86 folio_put(folio);