Re: [PATCH 04/13] mm/migrate_device: allocate THP deferred split heads

From: Balbir Singh

Date: Wed Nov 11 2026 - 18:08:11 EST


On Wed, Nov 11, 2026 at 06:13:18PM +0000, Lorenzo Stoakes (ARM) wrote:
> From: Josef Bacik <josef@xxxxxxxxxxxxxx>
>
> A THP that device migration maps into an empty PMD can end up on the wrong
> deferred split list when it is later queued for a deferred split.
>
> This occurs if the memcg has no heads yet (resulting in a warning if
> CONFIG_DEBUG_VM is set), where deferred_split_folio() will then queue the
> folio on the parent's list.
>
> Once a later THP fault in the same memcg allocates the heads,
> __folio_unqueue_deferred_split() and the split path resolve folio_memcg()
> to the memcg's own, now existing, list and take its lock. They then
> list_del_init() a folio that is linked on the parent's list, racing with
> anything holding the parent's lock, and both lists' nr_items counts go
> wrong.
>
> Resolve the issue by calling folio_memcg_alloc_deferred() after the charge
> in migrate_vma_insert_huge_pmd_page() and falling back like a failed charge
> if that fails, as __do_huge_pmd_anonymous_page() does.
>
> This was originally discovered by code inspection.
>
> Fixes: fafaeceb89a5 ("mm: switch deferred split shrinker to list_lru")
> Cc: <stable@xxxxxxxxxxxxxxx>
> Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
> [ ljs: tweak commit message, remove unnecessary comment ]
> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> ---
> mm/migrate_device.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/mm/migrate_device.c b/mm/migrate_device.c
> index 9e19fadfa65c..9e5dab8e1b77 100644
> --- a/mm/migrate_device.c
> +++ b/mm/migrate_device.c
> @@ -833,6 +833,13 @@ static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
> goto abort;
> }
>
> + if (folio_memcg_alloc_deferred(folio)) {
> + count_vm_event(THP_FAULT_FALLBACK);
> + count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_FALLBACK);
> + ret = -ENOMEM;
> + goto abort;
> + }
> +
> __folio_mark_uptodate(folio);
>
> pgtable = pte_alloc_one(vma->vm_mm);
>

Could you also clarify, if it is just the nr_items accounting that is a
concern?

Acked-by: Balbir Singh <balbirs@xxxxxxxxxx>