Skip to contentSkip to conversation

mm: mlock: avoid folio_within_range() on KSM pages

PATCH Applied Hugh Dickins 2 participants 2 messages Oct 24, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: mlock: avoid folio_within_range() on KSM pages

Since mm-hotfixes-stable commit dc68badcede4 ("mm: mlock: update mlock_pte_range to handle large folio") I've just occasionally seen VM_WARN_ON_FOLIO(folio_test_ksm) warnings from folio_within_range(), in a splurge after testing with KSM hyperactive.

folio_referenced_one()'s use of folio_within_vma() is safe because it checks folio_test_large() first; but allow_mlock_munlock() needs to do the same to avoid those warnings (or check !folio_test_ksm() itself? or move either check into folio_within_range()? hard to tell without more examples of its use).

Fixes dc68badcede4 ("mm: mlock: update mlock_pte_range to handle large folio")
---
 mm/mlock.c | 4 ++++
 1 file changed, 4 insertions(+)
mm/mlock.c +4 -0
index aa44456200e3..086546ac5766 100644
@@ -346,6 +346,10 @@ static inline bool allow_mlock_munlock(struct folio *folio,
 	if (!(vma->vm_flags & VM_LOCKED))
 		return true;
 
+	/* folio_within_range() cannot take KSM, but any small folio is OK */
+	if (!folio_test_large(folio))
+		return true;
+
 	/* folio not in range [start, end), skip mlock */
 	if (!folio_within_range(folio, vma, start, end))
 		return false;
Signature · 1 line
2.35.3
Reviewed-by on v1 1/1
#1 lore raw

Hi Huge,

Quoted 10 lines · Hugh Dickins

Since mm-hotfixes-stable commit dc68badcede4 ("mm: mlock: update mlock_pte_range to handle large folio") I've just occasionally seen VM_WARN_ON_FOLIO(folio_test_ksm) warnings from folio_within_range(), in a splurge after testing with KSM hyperactive.

folio_referenced_one()'s use of folio_within_vma() is safe because it checks folio_test_large() first; but allow_mlock_munlock() needs to do the same to avoid those warnings (or check !folio_test_ksm() itself? or move either check into folio_within_range()? hard to tell without more examples of its use).

Checking folio_test_large() here looks fine to me now. If KSM could support large folio in the future (Not sure whether this will happen in the future), we could revise.

Quoted 3 lines
Fixes dc68badcede4 ("mm: mlock: update mlock_pte_range to handle large folio")

Thanks a lot for catching this issue and fixing it.