Re: [PATCH v2 06/13] mm/migrate_device: don't let go of the fault page

From: Lorenzo Stoakes (ARM)

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


On Wed, Nov 18, 2026 at 04:31:25PM +0100, David Hildenbrand (Arm) wrote:
> On 11/13/26 22:45, Lorenzo Stoakes (ARM) wrote:
> > From: Josef Bacik <josef@xxxxxxxxxxxxxx>
> >
> > When migrate_vma_split_folio() has split the folio of the fault page,
> > the lock and the reference which the fault handler has taken are at the
> > former head page.
> >
> > It then moves them to the folio which now holds the fault page, by
> > taking a reference on and locking that folio, then unlocking and putting
> > the former head.
> >
> > split_folio() has unlocked the new folio and has dropped its own
> > reference on it, so until folio_get() nothing but the page table entry
> > holds it.
> >
> > Only the mmap lock is held for read, which does not prevent another thread
> > from zapping the entry with MADV_DONTNEED.
> >
> > If this happens the folio is freed which results in folio_get() taking a
> > reference on a free device page, which the driver can hand out again at any
> > time.
> >
> > In addition, folio_lock() can sleep here with the lock of the head page
> > held.
> >
> > Utilise split_huge_page() which will leave the lock and reference at the
> > fault page correctly.
> >
> > Fixes: 022a12deda53 ("mm/migrate_device: handle partially mapped folios during collection")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Josef Bacik <josef@xxxxxxxxxxxxxx>
> > [ ljs: tweaked commit message ]
> > Tested-by: Gregory Price (Meta) <gourry@xxxxxxxxxx>
> > Signed-off-by: Lorenzo Stoakes (ARM) <ljs@xxxxxxxxxx>
> > ---
>
> This code is so bad we should remove all of that from the tree, seriously.

Yes stuff like this really does remind me of many scenes from the all-time
classic movie Event Horizon... :)

>
> Acked-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>

Thanks! :)

>
> --
> Cheers,
>
> David

--
Cheers, Lorenzo