Re: [v2 PATCH 1/2] mm: gup: fix the fast GUP race against THP collapse
From: Yang Shi
Date: Wed Sep 07 2022 - 17:24:09 EST
On Wed, Sep 7, 2022 at 2:22 PM Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> On Wed, 7 Sep 2022 11:01:43 -0700 Yang Shi <shy828301@xxxxxxxxx> wrote:
>
> > Since general RCU GUP fast was introduced in commit 2667f50e8b81 ("mm:
> > introduce a general RCU get_user_pages_fast()"), a TLB flush is no longer
> > sufficient to handle concurrent GUP-fast in all cases, it only handles
> > traditional IPI-based GUP-fast correctly. On architectures that send
> > an IPI broadcast on TLB flush, it works as expected. But on the
> > architectures that do not use IPI to broadcast TLB flush, it may have
> > the below race:
> >
> > CPU A CPU B
> > THP collapse fast GUP
> > gup_pmd_range() <-- see valid pmd
> > gup_pte_range() <-- work on pte
> > pmdp_collapse_flush() <-- clear pmd and flush
> > __collapse_huge_page_isolate()
> > check page pinned <-- before GUP bump refcount
> > pin the page
> > check PTE <-- no change
> > __collapse_huge_page_copy()
> > copy data to huge page
> > ptep_clear()
> > install huge pmd for the huge page
> > return the stale page
> > discard the stale page
> >
> > The race could be fixed by checking whether PMD is changed or not after
> > taking the page pin in fast GUP, just like what it does for PTE. If the
> > PMD is changed it means there may be parallel THP collapse, so GUP
> > should back off.
> >
> > Also update the stale comment about serializing against fast GUP in
> > khugepaged.
> >
> > Fixes: 2667f50e8b81 ("mm: introduce a general RCU get_user_pages_fast()")
>
> Is this not worth a -stable backport?
Yes, I think it is.