* Re: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order
2026-05-11 2:54 [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order Ye Liu
@ 2026-05-11 3:11 ` Lance Yang
2026-05-11 5:41 ` David Hildenbrand (Arm)
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Lance Yang @ 2026-05-11 3:11 UTC (permalink / raw)
To: Ye Liu
Cc: Ye Liu, Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache, Xin Hao,
Lorenzo Stoakes, David Hildenbrand, Ryan Roberts, Dev Jain,
Barry Song, Andrew Morton, Andrew Morton, linux-mm, linux-kernel
On 2026/5/11 10:54, Ye Liu wrote:
> From: Ye Liu <liuye@kylinos.cn>
>
> __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@kernel.org>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
> ---
LGTM, thanks.
Reviewed-by: Lance Yang <lance.yang@linux.dev>
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order
2026-05-11 2:54 [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order Ye Liu
2026-05-11 3:11 ` Lance Yang
@ 2026-05-11 5:41 ` David Hildenbrand (Arm)
2026-05-11 5:45 ` Dev Jain
2026-05-11 14:20 ` Lorenzo Stoakes
3 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand (Arm) @ 2026-05-11 5:41 UTC (permalink / raw)
To: Ye Liu, Andrew Morton, Lorenzo Stoakes, Xin Hao
Cc: Ye Liu, Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Dev Jain, Barry Song, Lance Yang, Andrew Morton,
linux-mm, linux-kernel
On 5/11/26 04:54, Ye Liu wrote:
> From: Ye Liu <liuye@kylinos.cn>
>
> __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@kernel.org>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
> ---
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
--
Cheers,
David
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order
2026-05-11 2:54 [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order Ye Liu
2026-05-11 3:11 ` Lance Yang
2026-05-11 5:41 ` David Hildenbrand (Arm)
@ 2026-05-11 5:45 ` Dev Jain
2026-05-11 14:20 ` Lorenzo Stoakes
3 siblings, 0 replies; 5+ messages in thread
From: Dev Jain @ 2026-05-11 5:45 UTC (permalink / raw)
To: Ye Liu, Andrew Morton, David Hildenbrand, Lorenzo Stoakes,
Xin Hao
Cc: Ye Liu, Zi Yan, Baolin Wang, Liam R. Howlett, Nico Pache,
Ryan Roberts, Barry Song, Lance Yang, Andrew Morton, linux-mm,
linux-kernel
On 11/05/26 8:24 am, Ye Liu wrote:
> From: Ye Liu <liuye@kylinos.cn>
>
> __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@kernel.org>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
> ---
Reviewed-by: Dev Jain <dev.jain@arm.com>
> 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/20260506012130.9306-1-ye.liu@linux.dev/
>
> Changes since v1:
> - Add Fixes tag as suggested by Dev Jain and Lance Yang
> - Link: https://lore.kernel.org/all/20260501075708.327217-1-ye.liu@linux.dev/
>
> mm/khugepaged.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 5f4e009593e0..78735f34250a 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -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);
> /*
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order
2026-05-11 2:54 [PATCH v3] mm/khugepaged: fix inconsistent MMF_VM_HUGEPAGE flag due to allocation failure order Ye Liu
` (2 preceding siblings ...)
2026-05-11 5:45 ` Dev Jain
@ 2026-05-11 14:20 ` Lorenzo Stoakes
3 siblings, 0 replies; 5+ messages in thread
From: Lorenzo Stoakes @ 2026-05-11 14:20 UTC (permalink / raw)
To: Ye Liu
Cc: Andrew Morton, David Hildenbrand, Xin Hao, Ye Liu, Zi Yan,
Baolin Wang, Liam R. Howlett, Nico Pache, Ryan Roberts, Dev Jain,
Barry Song, Lance Yang, Andrew Morton, linux-mm, linux-kernel
On Mon, May 11, 2026 at 10:54:07AM +0800, Ye Liu wrote:
> From: Ye Liu <liuye@kylinos.cn>
>
> __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@kernel.org>
> Signed-off-by: Ye Liu <liuye@kylinos.cn>
LGTM, so:
Reviewed-by: Lorenzo Stoakes <ljs@kernel.org>
> ---
> 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/20260506012130.9306-1-ye.liu@linux.dev/
>
> Changes since v1:
> - Add Fixes tag as suggested by Dev Jain and Lance Yang
> - Link: https://lore.kernel.org/all/20260501075708.327217-1-ye.liu@linux.dev/
>
> mm/khugepaged.c | 7 +++++--
> 1 file changed, 5 insertions(+), 2 deletions(-)
>
> diff --git a/mm/khugepaged.c b/mm/khugepaged.c
> index 5f4e009593e0..78735f34250a 100644
> --- a/mm/khugepaged.c
> +++ b/mm/khugepaged.c
> @@ -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
>
^ permalink raw reply [flat|nested] 5+ messages in thread