Re: [PATCH V4] mm/gup: Clear the LRU flag of a page before adding to LRU batch
From: Hugh Dickins
Date: Fri Jul 12 2024 - 04:56:41 EST
On Thu, 4 Jul 2024, yangge1116@xxxxxxx wrote:
> From: yangge <yangge1116@xxxxxxx>
>
> If a large number of CMA memory are configured in system (for example, the
> CMA memory accounts for 50% of the system memory), starting a virtual
> virtual machine with device passthrough, it will
> call pin_user_pages_remote(..., FOLL_LONGTERM, ...) to pin memory.
> Normally if a page is present and in CMA area, pin_user_pages_remote()
> will migrate the page from CMA area to non-CMA area because of
> FOLL_LONGTERM flag. But the current code will cause the migration failure
> due to unexpected page refcounts, and eventually cause the virtual machine
> fail to start.
>
> If a page is added in LRU batch, its refcount increases one, remove the
> page from LRU batch decreases one. Page migration requires the page is not
> referenced by others except page mapping. Before migrating a page, we
> should try to drain the page from LRU batch in case the page is in it,
> however, folio_test_lru() is not sufficient to tell whether the page is
> in LRU batch or not, if the page is in LRU batch, the migration will fail.
>
> To solve the problem above, we modify the logic of adding to LRU batch.
> Before adding a page to LRU batch, we clear the LRU flag of the page so
> that we can check whether the page is in LRU batch by folio_test_lru(page).
> It's quite valuable, because likely we don't want to blindly drain the LRU
> batch simply because there is some unexpected reference on a page, as
> described above.
>
> This change makes the LRU flag of a page invisible for longer, which
> may impact some programs. For example, as long as a page is on a LRU
> batch, we cannot isolate it, and we cannot check if it's an LRU page.
> Further, a page can now only be on exactly one LRU batch. This doesn't
> seem to matter much, because a new page is allocated from buddy and
> added to the lru batch, or be isolated, it's LRU flag may also be
> invisible for a long time.
>
> Fixes: 9a4e9f3b2d73 ("mm: update get_user_pages_longterm to migrate pages allocated from CMA region")
> Cc: <stable@xxxxxxxxxxxxxxx>
> Signed-off-by: yangge <yangge1116@xxxxxxx>
This is an interesting patch, and may (but might not) be a very good one.
I have no objection to it going forward to 6.11-rc, but I do object to
the Cc stable, and its current placing in mm-hotfixes-unstable (if that
means it's to be rushed into 6.10 final).
This is a subtle change to PG_lru handling, altering how that has been
managed since 5.11 (and before). I have not observed any ill effect
from this patch, but I'm not at all confident in understanding its
implications - though perhaps braver and quicker minds are confident.
To judge by the commit message, it's entirely to suit the
if (!folio_test_lru(folio) && drain_allow) {
line in collect_longterm_unpinnable_folios(). And it is attractive
to associate the PG_lru with the unraised refcount.
But I worry that it may not be to the benefit of others: for example,
page reclaim's isolate_lru_folios() will no longer be able to isolate
folios on the LRU while they're on an fbatch. Which may be okay (in
the dirty case, pageout() loses interest once it finds refcount raised,
so nothing lost there), or might not be.
It's good to try this, but please don't rush it in.
Thanks,
Hugh