• yangge's avatar
    mm/gup: clear the LRU flag of a page before adding to LRU batch · 33dfe920
    yangge authored
    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.
    
    Link: https://lkml.kernel.org/r/1720075944-27201-1-git-send-email-yangge1116@126.com
    Link: https://lkml.kernel.org/r/1720008153-16035-1-git-send-email-yangge1116@126.com
    Fixes: 9a4e9f3b ("mm: update get_user_pages_longterm to migrate pages allocated from CMA region")
    Signed-off-by: default avataryangge <yangge1116@126.com>
    Cc: Aneesh Kumar K.V <aneesh.kumar@linux.ibm.com>
    Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
    Cc: David Hildenbrand <david@redhat.com>
    Cc: Barry Song <21cnbao@gmail.com>
    Cc: Hugh Dickins <hughd@google.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    33dfe920
swap.c 31.5 KB