Skip to contentSkip to conversation

mm/huge_memory: update file PMD counter before folio_put()

PATCH Applied Yin Tirui 5 participants 7 messages May 26 – May 27

Subsystems (3)
lore
Share & tools
lore

Yin Tirui

author
#0 lore raw

PATCH 1/1 mm/huge_memory: update file PMD counter before folio_put()

__split_huge_pmd_locked() updates the file/shmem RSS counter after dropping the PMD mapping's folio reference. If folio_put() drops the last reference, mm_counter_file() can later read freed folio state via folio_test_swapbacked().

Move the counter update before folio_put().

Fixes fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")
---
 mm/huge_memory.c | 2 ++
 1 file changed, 2 insertions(+)
mm/huge_memory.c +2 -0
index 0135c29a4372..a5f4a48b7b77 100644
@@ -3145,7 +3145,9 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 			if (!folio_test_referenced(folio) && pmd_young(old_pmd))
 				folio_set_referenced(folio);
 			folio_remove_rmap_pmd(folio, page, vma);
+			add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
 			folio_put(folio);
+			return;
 		}
 		add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
 		return;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Yin Tirui

__split_huge_pmd_locked() updates the file/shmem RSS counter after dropping the PMD mapping's folio reference. If folio_put() drops the last reference, mm_counter_file() can later read freed folio state via folio_test_swapbacked().

Move the counter update before folio_put().

Fixes fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")

That's an old commit :) I mean I suspect we're probably not actually ever dropping the folio ref to 0 here since we never had a report since ~2018.

The page cache keeping a reference I guess?

But doesn't mean we shouldn't fix this on principal/there being some way this could happen.

Quoted 2 lines

LGTM, so:

Quoted 9 lines
---
 mm/huge_memory.c | 2 ++
 1 file changed, 2 insertions(+)
mm/huge_memory.c +0 -0
index 0135c29a4372..a5f4a48b7b77 100644

mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3150

Quoted 6 lines
mm/huge_memory.c +2 -0
@@ -3145,7 +3145,9 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 			if (!folio_test_referenced(folio) && pmd_young(old_pmd))
 				folio_set_referenced(folio);
 			folio_remove_rmap_pmd(folio, page, vma);
+			add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
 			folio_put(folio);
+			return;

Hmm, sucks to duplicate like this, but for purposes of backport and getting this resolved fine, we can clean it up later.

Quoted 6 lines
mm/huge_memory.c +0 -1
 		}
 		add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
 		return;
--

2.43.0

Cheers, Lorenzo

author
#2 lore raw
Quoted 8 lines · Lorenzo Stoakes
Quoted 7 lines · Yin Tirui

__split_huge_pmd_locked() updates the file/shmem RSS counter after dropping the PMD mapping's folio reference. If folio_put() drops the last reference, mm_counter_file() can later read freed folio state via folio_test_swapbacked().

Move the counter update before folio_put().

Quoted 8 lines
Quoted 1 line
Fixes fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")

That's an old commit :) I mean I suspect we're probably not actually ever dropping the folio ref to 0 here since we never had a report since ~2018.

The page cache keeping a reference I guess?

But doesn't mean we shouldn't fix this on principal/there being some way this could happen.

Yes, agreed.

Quoted 6 lines
Quoted 3 lines

LGTM, so:

Thanks!

Quoted 4 lines
Quoted 4 lines
---
  mm/huge_memory.c | 2 ++
  1 file changed, 2 insertions(+)
Quoted 13 lines

mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3150

Quoted 11 lines
mm/huge_memory.c +2 -0
index 0135c29a4372..a5f4a48b7b77 100644
@@ -3145,7 +3145,9 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
  			if (!folio_test_referenced(folio) && pmd_young(old_pmd))
  				folio_set_referenced(folio);
  			folio_remove_rmap_pmd(folio, page, vma);
+			add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
  			folio_put(folio);
+			return;

Hmm, sucks to duplicate like this, but for purposes of backport and getting this resolved fine, we can clean it up later.

Yes, that was my intention as well: keep this patch minimal for backport. I will post the related refactoring patch soon.

Quoted 8 lines
Quoted 6 lines
mm/huge_memory.c +0 -1
  		}
  		add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
  		return;
--

2.43.0

Cheers, Lorenzo

Signature · 1 line
Yin Tirui
maintainer Acked-by
#3 lore raw
In reply to Lorenzo Stoakes
Quoted 6 lines · Lorenzo Stoakes
Quoted 5 lines · Yin Tirui

__split_huge_pmd_locked() updates the file/shmem RSS counter after dropping the PMD mapping's folio reference. If folio_put() drops the last reference, mm_counter_file() can later read freed folio state via folio_test_swapbacked().

Quoted 8 lines
Quoted 3 lines

Move the counter update before folio_put().

Fixes fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")

That's an old commit :) I mean I suspect we're probably not actually ever dropping the folio ref to 0 here since we never had a report since ~2018.

The page cache keeping a reference I guess?

I assume we could be racing with truncation.

Truncation would have to trigger unmap itself before we do the folio_remove_rmap_pmd().

While the race could happen in theory I think, I do assume this would be rather hard to trigger.

Quoted 10 lines

But doesn't mean we shouldn't fix this on principal/there being some way this could happen.

Quoted 2 lines

LGTM, so:

Signature · 3 lines
Cheers,

David
#4 lore raw
Quoted 16 lines · David Hildenbrand (Arm)
Quoted 6 lines · Lorenzo Stoakes
Quoted 5 lines · Yin Tirui

__split_huge_pmd_locked() updates the file/shmem RSS counter after dropping the PMD mapping's folio reference. If folio_put() drops the last reference, mm_counter_file() can later read freed folio state via folio_test_swapbacked().

Quoted 8 lines
Quoted 3 lines

Move the counter update before folio_put().

Fixes fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")

That's an old commit :) I mean I suspect we're probably not actually ever dropping the folio ref to 0 here since we never had a report since ~2018.

The page cache keeping a reference I guess?

Quoted 7 lines

I assume we could be racing with truncation.

Truncation would have to trigger unmap itself before we do the folio_remove_rmap_pmd().

While the race could happen in theory I think, I do assume this would be rather hard to trigger.

Yeah, I mean unless we missed it somehow it seems like any such race if it exists is very tiny.

But obviously we really do need to fix this! :)

Quoted 10 lines
Quoted 10 lines

But doesn't mean we shouldn't fix this on principal/there being some way this could happen.

Quoted 2 lines

LGTM, so:

Quoted 8 lines
Quoted 1 line

--
Cheers,

David

Cheers, Lorenzo

reviewer Reviewed-by on v1 1/1
#5 lore raw
In reply to Yin Tirui
Quoted 11 lines · Yin Tirui

__split_huge_pmd_locked() updates the file/shmem RSS counter after dropping the PMD mapping's folio reference. If folio_put() drops the last reference, mm_counter_file() can later read freed folio state via folio_test_swapbacked().

Move the counter update before folio_put().

Fixes fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")

---

Thanks! Feel free to add:

reviewer Reviewed-by on v1 1/1
#6 lore raw
In reply to Yin Tirui
Quoted 11 lines · Yin Tirui

__split_huge_pmd_locked() updates the file/shmem RSS counter after dropping the PMD mapping's folio reference. If folio_put() drops the last reference, mm_counter_file() can later read freed folio state via folio_test_swapbacked().

Move the counter update before folio_put().

Fixes fadae2953072 ("thp: use mm_file_counter to determine update which rss counter")

---

Quoted 17 lines
 mm/huge_memory.c | 2 ++
 1 file changed, 2 insertions(+)
mm/huge_memory.c +2 -0
index 0135c29a4372..a5f4a48b7b77 100644
@@ -3145,7 +3145,9 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 			if (!folio_test_referenced(folio) && pmd_young(old_pmd))
 				folio_set_referenced(folio);
 			folio_remove_rmap_pmd(folio, page, vma);
+			add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
 			folio_put(folio);
+			return;
 		}
 		add_mm_counter(mm, mm_counter_file(folio), -HPAGE_PMD_NR);
 		return;