Re: [PATCH 21/32] mm/migrate_device: mark folios leaving device memory dirty

From: Alistair Popple

Date: Wed Dec 02 2026 - 17:31:49 EST


On 2026-12-03 at 02:34 +1100, "David Hildenbrand (Arm)" <david@xxxxxxxxxx> wrote...
> On 12/2/26 05:15, Alistair Popple wrote:
> > On 2026-11-29 at 21:06 +1100, "David Hildenbrand (Arm)" <david@xxxxxxxxxx> wrote...
> >> Device-private page table entries are weird: they are non-present page
> >> table entries, and the device can write to the memory without the CPU
> >> noticing or the CPU page tables reflecting it.
> >
> > However the device itself could know.
>
> Right.
>
> >
> >> So there is no dirty bit on the PTE we could sync to the folio. And
> >> usually, nobody cares about the dirty bit for these folios, as these
> >> folios are not on the LRU. Device-coherent memory has the same problem:
> >> it is mapped through present PTEs, but the device writes through its own
> >> page tables and does not set the PTE dirty bit.
> >>
> >> There is one problem, though: when we migrate such a clean folio back
> >> to system RAM, the new folio will be clean, and will be mapped through
> >> clean PTEs.
> >
> > Unless the driver marks it dirty before finalizing migration. So this could
> > be part of the driver contract - it has to copy a dirty flag into the folio if
> > the page might be dirty. Of course no driver I looked at actually does that, so
> > until such a driver comes along I think this fix is fine.
>
> Yeah. But the question is also what it buys us. ZONE_DEVICE doesn't really need
> the dirty flag. It's only relevant once we move back to anon memory ... so any
> such change would need a proper motivation in why we should care.

Agreed. The only case I can think of is if the anon memory still existed in swap
and subsequently needed to be reclaimed. With this everything ever migrated to
ZONE_DEVICE would need to get written back. I doubt that's a common or important
use case though. If it was presumably someone would have noticed this bug sooner :-)

If we ever support migrating non-anon pages it could become more of an issue,
but that doesn't seem like something anyone wants either so I don't think we
should care.

- Alistair

> Thanks a lot for the review!
>
> --
> Cheers,
>
> David