All of lore.kernel.org
 help / color / mirror / Atom feed
From: Yang Shi <shy828301@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: david@redhat.com, peterx@redhat.com,
	kirill.shutemov@linux.intel.com, jhubbard@nvidia.com,
	jgg@nvidia.com, hughd@google.com, aneesh.kumar@linux.ibm.com,
	linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org,
	linux-kernel@vger.kernel.org
Subject: Re: [v2 PATCH 1/2] mm: gup: fix the fast GUP race against THP collapse
Date: Wed, 7 Sep 2022 14:23:48 -0700	[thread overview]
Message-ID: <CAHbLzkr==CoCP6U+zNhX8_Ug+sG-P7xwyuf_1yCWwKF8XzGj3Q@mail.gmail.com> (raw)
In-Reply-To: <20220907142246.c7d6f55e1b56fba3e16d974b@linux-foundation.org>

On Wed, Sep 7, 2022 at 2:22 PM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Wed,  7 Sep 2022 11:01:43 -0700 Yang Shi <shy828301@gmail.com> 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.

WARNING: multiple messages have this Message-ID (diff)
From: Yang Shi <shy828301@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: david@redhat.com, jhubbard@nvidia.com, hughd@google.com,
	linux-kernel@vger.kernel.org, peterx@redhat.com,
	linux-mm@kvack.org, jgg@nvidia.com, aneesh.kumar@linux.ibm.com,
	linuxppc-dev@lists.ozlabs.org, kirill.shutemov@linux.intel.com
Subject: Re: [v2 PATCH 1/2] mm: gup: fix the fast GUP race against THP collapse
Date: Wed, 7 Sep 2022 14:23:48 -0700	[thread overview]
Message-ID: <CAHbLzkr==CoCP6U+zNhX8_Ug+sG-P7xwyuf_1yCWwKF8XzGj3Q@mail.gmail.com> (raw)
In-Reply-To: <20220907142246.c7d6f55e1b56fba3e16d974b@linux-foundation.org>

On Wed, Sep 7, 2022 at 2:22 PM Andrew Morton <akpm@linux-foundation.org> wrote:
>
> On Wed,  7 Sep 2022 11:01:43 -0700 Yang Shi <shy828301@gmail.com> 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.

  reply	other threads:[~2022-09-07 21:24 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-07 18:01 [v2 PATCH 1/2] mm: gup: fix the fast GUP race against THP collapse Yang Shi
2022-09-07 18:01 ` Yang Shi
2022-09-07 18:01 ` [v2 PATCH 2/2] powerpc/64s/radix: don't need to broadcast IPI for radix pmd collapse flush Yang Shi
2022-09-07 18:01   ` Yang Shi
2022-09-07 19:34   ` David Hildenbrand
2022-09-07 20:12   ` Peter Xu
2022-09-07 20:12     ` Peter Xu
2022-09-07 21:22 ` [v2 PATCH 1/2] mm: gup: fix the fast GUP race against THP collapse Andrew Morton
2022-09-07 21:22   ` Andrew Morton
2022-09-07 21:23   ` Yang Shi [this message]
2022-09-07 21:23     ` Yang Shi
2022-09-08  0:06 ` John Hubbard
2022-09-08  0:06   ` John Hubbard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHbLzkr==CoCP6U+zNhX8_Ug+sG-P7xwyuf_1yCWwKF8XzGj3Q@mail.gmail.com' \
    --to=shy828301@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=aneesh.kumar@linux.ibm.com \
    --cc=david@redhat.com \
    --cc=hughd@google.com \
    --cc=jgg@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=kirill.shutemov@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=peterx@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.