Re: [PATCH 2/5] mm,hugetlb: Document the reason to lock the folio in the faulting path

From: David Hildenbrand
Date: Tue Jun 17 2025 - 07:27:41 EST




I see, this makes a lot of sense, thanks for walking me through David!

Well, I hope the same logic applies to hugetlb :D

Alright, then, with all this clear now we should:

- Not take any locks on hugetlb_fault()->hugetlb_wp(), hugetlb_wp() will take it
if it's an anonymous folio (re-use check)
- Drop the lock in hugetlb_no_page() after we have mapped the page in
the pagetables
- hugetlb_wp() will take the lock IFF the folio is anonymous

This will lead to something like the following:

diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index dfa09fc3b2c6..4d48cda8a56d 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -6198,6 +6198,8 @@ static vm_fault_t hugetlb_wp(struct vm_fault *vmf)
* in scenarios that used to work. As a side effect, there can still
* be leaks between processes, for example, with FOLL_GET users.
*/
+ if (folio_test_anon(old_folio))
+ folio_lock(old_folio);

If holding the PTL, this would not work. You'd have to unlock PTL, lock folio, retake PTL, check pte_same.

if (folio_mapcount(old_folio) == 1 && folio_test_anon(old_folio)) {
if (!PageAnonExclusive(&old_folio->page)) {
folio_move_anon_rmap(old_folio, vma);
@@ -6212,6 +6214,8 @@ static vm_fault_t hugetlb_wp(struct vm_fault *vmf)
}
VM_BUG_ON_PAGE(folio_test_anon(old_folio) &&
PageAnonExclusive(&old_folio->page), &old_folio->page);
+ if (folio_test_anon(old_folio))
+ folio_unlock(old_folio);

[...]

This should be patch#2 with something like "Sorting out locking" per
title, and maybe explaining a bit more why the lock in hugelb_wp for
anonymous folios.

Jup.


--
Cheers,

David / dhildenb