[PATCH] mm: fix cache flush for shmem page that are swap backed.

From: JÃrÃme Glisse
Date: Wed May 27 2020 - 20:18:59 EST


This might be a shmem page that is in a sense a file that
can be mapped multiple times in different processes at
possibly different virtual addresses (fork + mremap). So
return the shmem mapping that will allow any arch code to
find all mappings of the page.

Note that even if page is not anonymous then the page might
have a NULL page->mapping field if it is being truncated,
but then it is fine as each pte poiting to the page will be
remove and cache flushing should be handled properly by that
part of the code.

Signed-off-by: Jéme Glisse <jglisse@xxxxxxxxxx>
Cc: "Huang, Ying" <ying.huang@xxxxxxxxx>
Cc: Michal Hocko <mhocko@xxxxxxxx>
Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx>
Cc: Russell King <linux@xxxxxxxxxxxxxxx>
Cc: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx>
Cc: Mike Rapoport <rppt@xxxxxxxxxxxxxxxxxx>
Cc: "David S. Miller" <davem@xxxxxxxxxxxxx>
Cc: "James E.J. Bottomley" <jejb@xxxxxxxxxxxxxxxx>
---
mm/util.c | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/mm/util.c b/mm/util.c
index 988d11e6c17c..ec8739ab0cc3 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -685,8 +685,24 @@ EXPORT_SYMBOL(page_mapping);
*/
struct address_space *page_mapping_file(struct page *page)
{
- if (unlikely(PageSwapCache(page)))
+ if (unlikely(PageSwapCache(page))) {
+ /*
+ * This might be a shmem page that is in a sense a file that
+ * can be mapped multiple times in different processes at
+ * possibly different virtual addresses (fork + mremap). So
+ * return the shmem mapping that will allow any arch code to
+ * find all mappings of the page.
+ *
+ * Note that even if page is not anonymous then the page might
+ * have a NULL page->mapping field if it is being truncated,
+ * but then it is fine as each pte poiting to the page will be
+ * remove and cache flushing should be handled properly by that
+ * part of the code.
+ */
+ if (!PageAnon(page))
+ return page->mapping;
return NULL;
+ }
return page_mapping(page);
}

--
2.26.2


--/04w6evG8XlLl3ft--