Re: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order

From: Lance Yang

Date: Sun May 10 2026 - 23:11:44 EST




On 2026/5/11 10:54, Ye Liu wrote:
From: Ye Liu <liuye@xxxxxxxxxx>

__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"")
Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
Signed-off-by: Ye Liu <liuye@xxxxxxxxxx>
---

LGTM, thanks.

Reviewed-by: Lance Yang <lance.yang@xxxxxxxxx>