Re: [PATCH] mm/migrate_device: consolidate compound folio handling
From: David Hildenbrand (Arm)
Date: Fri Sep 11 2026 - 14:09:44 EST
On 9/11/26 08:13, Hui Su wrote:
> Commit dc41e961a269 ("mm/migrate_device: avoid out-of-bounds writes for
> compound folios") added handling for compound folios that do not fit in
> the remaining PFN array.
>
> migrate_device_range() and migrate_device_pfns() duplicate the logic for
> locking device PFNs, encoding compound folios, and handling this boundary
> condition.
>
> A compound folio cannot be represented partially for migration. Warn when
> one does not fit in the remaining PFN array, while retaining the existing
> defensive handling: release any lock and reference acquired for the
> current folio, clear the remaining entries, and stop collecting.
>
> Move the shared collection and encoding logic into a helper so both
> interfaces handle compound folios consistently. Also use memset() for
> the compound-folio tail entries instead of open-coding the clearing loop.
>
> Document that an encountered compound folio must fit entirely in the
> remaining range or PFN array.
>
> Link: https://lore.kernel.org/r/c99ca53a-73ef-4a0c-8738-eba1cc89bea2@xxxxxxxxxx
> Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
> Signed-off-by: Hui Su <sh_def@xxxxxxx>
> ---
> Tested on x86_64 with KASAN enabled:
>
> - Built mm/migrate_device.o and the kernel successfully.
> - Ran the HMM migrate_anon_huge_zero selftest; the private-device case
> passed.
> - No KASAN report or truncated compound-folio WARN was observed.
>
> mm/migrate_device.c | 90 ++++++++++++++++++++++++---------------------
> 1 file changed, 49 insertions(+), 41 deletions(-)
>
> diff --git a/mm/migrate_device.c b/mm/migrate_device.c
> index 009bfa8b212d..c57eadea6565 100644
> --- a/mm/migrate_device.c
> +++ b/mm/migrate_device.c
> @@ -1392,6 +1392,39 @@ static unsigned long migrate_device_pfn_lock(unsigned long pfn)
> return migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
> }
>
> +/*
> + * Collect a device folio into the page-granular PFN array.
> + *
> + * Return the number of entries consumed, or 0 if the folio does not fit in
> + * the remaining array.
> + */
> +static unsigned int migrate_device_collect_folio(unsigned long *src_pfn,
> + unsigned long pfn,
> + unsigned long remaining)
Two tab indent please.
> +{
> + struct folio *folio = page_folio(pfn_to_page(pfn));
> + unsigned int nr;
> +
> + *src_pfn = migrate_device_pfn_lock(pfn);
> + nr = folio_nr_pages(folio);
> +
> + if (WARN_ON_ONCE(nr > remaining)) {
Just to be sure: there is no way we can currently validly trigger this, right?
> + if (*src_pfn & MIGRATE_PFN_MIGRATE) {
> + folio_unlock(folio);
> + folio_put(folio);
> + }
> + memset(src_pfn, 0, remaining * sizeof(*src_pfn));
> + return 0;
> + }
> +
> + if (nr > 1) {
> + *src_pfn |= MIGRATE_PFN_COMPOUND;
> + memset(src_pfn + 1, 0, (nr - 1) * sizeof(*src_pfn));
> + }
> +
> + return nr;
> +}
Looks much cleaner.
--
Cheers,
David