[PATCH] mm/hugetlb: Fix vmsplice case on memory leak once more

From: Peter Xu
Date: Fri Apr 26 2024 - 11:41:12 EST


Signed-off-by: Peter Xu <peterx@xxxxxxxxxx>
---
mm/hugetlb.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index 417fc5cdb6ee..1ca102013561 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -5961,10 +5961,13 @@ static vm_fault_t hugetlb_wp(struct folio *pagecache_folio,

retry_avoidcopy:
/*
- * If no-one else is actually using this page, we're the exclusive
- * owner and can reuse this page.
+ * If the page is marked exlusively owned (e.g. longterm pinned),
+ * we can reuse it. Otherwise if no-one else is using this page,
+ * we can savely set the exclusive bit and reuse it.
*/
- if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) {
+ if (folio_test_anon(old_folio) &&
+ (PageAnonExclusive(&old_folio->page) ||
+ folio_ref_count(old_folio) == 1)) {
if (!PageAnonExclusive(&old_folio->page)) {
folio_move_anon_rmap(old_folio, vma);
SetPageAnonExclusive(&old_folio->page);
--
2.44.0


--
Peter Xu