Skip to contentSkip to conversation

mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order

PATCH Applied Ye Liu 6 participants 6 messages May 11 – May 13

Subsystems (2)
lore
Share & tools
lore

Ye Liu

author
#0 lore raw

PATCH v3 1/1 mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order

From: Ye Liu <[email protected]>

__khugepaged_enter() sets MMF_VM_HUGEPAGE before allocating the corresponding mm_slot. If mm_slot_alloc() fails, the function returns with the flag set but without inserting the mm into the khugepaged tracking structures, leaving the mm in an inconsistent state where future registration attempts are skipped.

Fix this by reordering: allocate the mm_slot first, then check and set the flag. If the flag is already set, free the allocated slot and return. This ensures the flag is only set when the mm is successfully registered in the khugepaged tracking structures.

Fixes 16618670276a ("mm: khugepaged: avoid pointless allocation for "struct mm_slot"")

---
Changes since v2:
- Reorder to allocate mm_slot first, free it when flag already set, as suggested by David, Dev Jain and Lance Yang
- Update the subject line to better match the patch.
- Link: https://lore.kernel.org/all/[email protected]/

Changes since v1:
- Add Fixes tag as suggested by Dev Jain and Lance Yang
- Link: https://lore.kernel.org/all/[email protected]/

 mm/khugepaged.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/khugepaged.c +5 -2
index 5f4e009593e0..78735f34250a 100644
@@ -437,13 +437,16 @@ void __khugepaged_enter(struct mm_struct *mm)
 
 	/* __khugepaged_exit() must not run from under us */
 	VM_BUG_ON_MM(collapse_test_exit(mm), mm);
-	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
-		return;
 
 	slot = mm_slot_alloc(mm_slot_cache);
 	if (!slot)
 		return;
 
+	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm))) {
+		mm_slot_free(mm_slot_cache, slot);
+		return;
+	}
+
 	spin_lock(&khugepaged_mm_lock);
 	mm_slot_insert(mm_slots_hash, mm, slot);
 	/*
Signature · 1 line
2.43.0
reviewer Reviewed-by on v3 1/1
#1 lore raw
Quoted 13 lines · Ye Liu

From: Ye Liu <[email protected]>

__khugepaged_enter() sets MMF_VM_HUGEPAGE before allocating the corresponding mm_slot. If mm_slot_alloc() fails, the function returns with the flag set but without inserting the mm into the khugepaged tracking structures, leaving the mm in an inconsistent state where future registration attempts are skipped.

Fix this by reordering: allocate the mm_slot first, then check and set the flag. If the flag is already set, free the allocated slot and return. This ensures the flag is only set when the mm is successfully registered in the khugepaged tracking structures.

Quoted 4 lines
Fixes 16618670276a ("mm: khugepaged: avoid pointless allocation for "struct mm_slot"")

---

LGTM, thanks.

maintainer Acked-by on v3 1/1
#2 lore raw
In reply to Ye Liu
Quoted 13 lines · Ye Liu

From: Ye Liu <[email protected]>

__khugepaged_enter() sets MMF_VM_HUGEPAGE before allocating the corresponding mm_slot. If mm_slot_alloc() fails, the function returns with the flag set but without inserting the mm into the khugepaged tracking structures, leaving the mm in an inconsistent state where future registration attempts are skipped.

Fix this by reordering: allocate the mm_slot first, then check and set the flag. If the flag is already set, free the allocated slot and return. This ensures the flag is only set when the mm is successfully registered in the khugepaged tracking structures.

Quoted 4 lines
Fixes 16618670276a ("mm: khugepaged: avoid pointless allocation for "struct mm_slot"")

---

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v3 1/1
#3 lore raw
In reply to Ye Liu
Quoted 13 lines · Ye Liu

From: Ye Liu <[email protected]>

__khugepaged_enter() sets MMF_VM_HUGEPAGE before allocating the corresponding mm_slot. If mm_slot_alloc() fails, the function returns with the flag set but without inserting the mm into the khugepaged tracking structures, leaving the mm in an inconsistent state where future registration attempts are skipped.

Fix this by reordering: allocate the mm_slot first, then check and set the flag. If the flag is already set, free the allocated slot and return. This ensures the flag is only set when the mm is successfully registered in the khugepaged tracking structures.

Quoted 4 lines
Fixes 16618670276a ("mm: khugepaged: avoid pointless allocation for "struct mm_slot"")

---

Quoted 36 lines

Changes since v2:
- Reorder to allocate mm_slot first, free it when flag already set, as suggested by David, Dev Jain and Lance Yang
- Update the subject line to better match the patch.
- Link: https://lore.kernel.org/all/[email protected]/

Changes since v1:
- Add Fixes tag as suggested by Dev Jain and Lance Yang
- Link: https://lore.kernel.org/all/[email protected]/

 mm/khugepaged.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/khugepaged.c +5 -2
index 5f4e009593e0..78735f34250a 100644
@@ -437,13 +437,16 @@ void __khugepaged_enter(struct mm_struct *mm)
 
 	/* __khugepaged_exit() must not run from under us */
 	VM_BUG_ON_MM(collapse_test_exit(mm), mm);
-	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
-		return;
 
 	slot = mm_slot_alloc(mm_slot_cache);
 	if (!slot)
 		return;
 
+	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm))) {
+		mm_slot_free(mm_slot_cache, slot);
+		return;
+	}
+
 	spin_lock(&khugepaged_mm_lock);
 	mm_slot_insert(mm_slots_hash, mm, slot);
 	/*
Reviewed-by on v3 1/1
#4 lore raw
In reply to Ye Liu
Quoted 8 lines · Ye Liu

From: Ye Liu <[email protected]>

__khugepaged_enter() sets MMF_VM_HUGEPAGE before allocating the corresponding mm_slot. If mm_slot_alloc() fails, the function returns with the flag set but without inserting the mm into the khugepaged tracking structures, leaving the mm in an inconsistent state where future registration attempts are skipped.

Quoted 8 lines

Fix this by reordering: allocate the mm_slot first, then check and set the flag. If the flag is already set, free the allocated slot and return. This ensures the flag is only set when the mm is successfully registered in the khugepaged tracking structures.

Fixes 16618670276a ("mm: khugepaged: avoid pointless allocation for "struct mm_slot"")

LGTM, so:

Quoted 40 lines

---
Changes since v2:
- Reorder to allocate mm_slot first, free it when flag already set, as suggested by David, Dev Jain and Lance Yang
- Update the subject line to better match the patch.
- Link: https://lore.kernel.org/all/[email protected]/

Changes since v1:
- Add Fixes tag as suggested by Dev Jain and Lance Yang
- Link: https://lore.kernel.org/all/[email protected]/

 mm/khugepaged.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
mm/khugepaged.c +5 -3
index 5f4e009593e0..78735f34250a 100644
@@ -437,13 +437,16 @@ void __khugepaged_enter(struct mm_struct *mm)
 
 	/* __khugepaged_exit() must not run from under us */
 	VM_BUG_ON_MM(collapse_test_exit(mm), mm);
-	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm)))
-		return;
 
 	slot = mm_slot_alloc(mm_slot_cache);
 	if (!slot)
 		return;
 
+	if (unlikely(mm_flags_test_and_set(MMF_VM_HUGEPAGE, mm))) {
+		mm_slot_free(mm_slot_cache, slot);
+		return;
+	}
+
 	spin_lock(&khugepaged_mm_lock);
 	mm_slot_insert(mm_slots_hash, mm, slot);
 	/*
--

2.43.0

reviewer Reviewed-by on v3 1/1
#5 lore raw
In reply to Ye Liu
Quoted 13 lines · Ye Liu

From: Ye Liu <[email protected]>

__khugepaged_enter() sets MMF_VM_HUGEPAGE before allocating the corresponding mm_slot. If mm_slot_alloc() fails, the function returns with the flag set but without inserting the mm into the khugepaged tracking structures, leaving the mm in an inconsistent state where future registration attempts are skipped.

Fix this by reordering: allocate the mm_slot first, then check and set the flag. If the flag is already set, free the allocated slot and return. This ensures the flag is only set when the mm is successfully registered in the khugepaged tracking structures.

Quoted 4 lines
Fixes 16618670276a ("mm: khugepaged: avoid pointless allocation for "struct mm_slot"")

---

LGTM.