Re: [PATCH v3 1/5] mm/migrate_device: avoid leaking folio references during THP splitting
From: Lorenzo Stoakes (ARM)
Date: Mon Nov 23 2026 - 12:19:50 EST
On Mon, Nov 23, 2026 at 08:32:07AM -0800, Usama Arif wrote:
> migrate_vma_split_unmapped_folio() takes an extra folio reference because
> split_huge_pmd_address(..., true) drops one when it splits a PMD.
>
> However, migrate_vma only holds mmap_lock for reading. A concurrent
> MADV_DONTNEED can clear the migration PMD, or split it into PTE migration
> entries, before the split helper takes the PMD lock. The helper then does
> nothing and leaves the extra reference behind. folio_split_unmapped()
> fails with -EAGAIN due to the unexpected reference, and the reference is
> never dropped, so the source THP is leaked.
>
> If a THP is faulted in at the same address inside that window it is
> worse: freeze = true replaces the new folio's mappings with migration
> entries that nothing will ever remove, and drops a reference the caller
> never took.
>
> An existing migration PMD is expanded into PTE migration entries even
> with freeze = false. Use that mode and remove the extra folio_get(). The
> reference already held by migration keeps the source folio alive whether
> or not its PMD migration entry still exists.
>
> Reported-by: Zi Yan <ziy@xxxxxxxxxx>
> Closes: https://lore.kernel.org/all/DLZ48KMS7YG8.3BTM0UUU8CTU2@xxxxxxxxxx/
> Fixes: 4265d67e405a ("mm/migrate_device: add THP splitting during migration")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Usama Arif <usama.arif@xxxxxxxxx>
> Reviewed-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
> Reviewed-by: Zi Yan <ziy@xxxxxxxxxx>
> Reviewed-by: Kiryl Shutsemau (Meta) <kas@xxxxxxxxxx>
LGTM so:
Reviewed-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> ---
> mm/migrate_device.c | 7 +------
> 1 file changed, 1 insertion(+), 6 deletions(-)
>
> diff --git a/mm/migrate_device.c b/mm/migrate_device.c
> index 9e19fadfa65c67..3286bb8a4b73ee 100644
> --- a/mm/migrate_device.c
> +++ b/mm/migrate_device.c
> @@ -920,12 +920,7 @@ static int migrate_vma_split_unmapped_folio(struct migrate_vma *migrate,
> unsigned long flags;
> int ret = 0;
>
> - /*
> - * take a reference, since split_huge_pmd_address() with freeze = true
> - * drops a reference at the end.
> - */
> - folio_get(folio);
> - split_huge_pmd_address(migrate->vma, addr, true);
> + split_huge_pmd_address(migrate->vma, addr, false);
> ret = folio_split_unmapped(folio, 0);
> if (ret)
> return ret;
> --
> 2.53.1-Meta
>
--
Cheers, Lorenzo