From: Jann Horn <jannh@google.com>
To: Qi Zheng <zhengqi.arch@bytedance.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Pavel Emelianov <xemul@virtuozzo.com>,
Andrea Arcangeli <aarcange@redhat.com>,
Hugh Dickins <hughd@google.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH 1/2] userfaultfd: Fix pmd_trans_huge() recheck race
Date: Tue, 13 Aug 2024 16:57:48 +0200 [thread overview]
Message-ID: <CAG48ez3u4tCaP6MSjxwqZQ70teFJHag7z9wRmd8LJXXee3tTTw@mail.gmail.com> (raw)
In-Reply-To: <59bf3c2e-d58b-41af-ab10-3e631d802229@bytedance.com>
On Tue, Aug 13, 2024 at 8:19 AM Qi Zheng <zhengqi.arch@bytedance.com> wrote:
>
> Hi Jann,
>
> On 2024/8/13 00:42, Jann Horn wrote:
> > The following race can occur:
> >
> > mfill_atomic other thread
> > ============ ============
> > <zap PMD>
> > pmdp_get_lockless() [reads none pmd]
> > <bail if trans_huge>
> > <if none:>
> > <pagefault creates transhuge zeropage>
> > __pte_alloc [no-op]
> > <zap PMD>
> > <bail if pmd_trans_huge(*dst_pmd)>
> > BUG_ON(pmd_none(*dst_pmd))
> >
> > I have experimentally verified this in a kernel with extra mdelay() calls;
> > the BUG_ON(pmd_none(*dst_pmd)) triggers.
> >
> > On kernels newer than commit 0d940a9b270b ("mm/pgtable: allow
> > pte_offset_map[_lock]() to fail"), this can't lead to anything worse than
> > a BUG_ON(), since the page table access helpers are actually designed to
> > deal with page tables concurrently disappearing; but on older kernels
> > (<=6.4), I think we could probably theoretically race past the two BUG_ON()
> > checks and end up treating a hugepage as a page table.
> >
> > Cc: stable@vger.kernel.org
> > Fixes: c1a4de99fada ("userfaultfd: mcopy_atomic|mfill_zeropage: UFFDIO_COPY|UFFDIO_ZEROPAGE preparation")
> > Signed-off-by: Jann Horn <jannh@google.com>
> > ---
> > mm/userfaultfd.c | 3 ++-
> > 1 file changed, 2 insertions(+), 1 deletion(-)
> >
> > diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
> > index e54e5c8907fa..ec3750467aa5 100644
> > --- a/mm/userfaultfd.c
> > +++ b/mm/userfaultfd.c
> > @@ -801,7 +801,8 @@ static __always_inline ssize_t mfill_atomic(struct userfaultfd_ctx *ctx,
> > break;
> > }
> > /* If an huge pmd materialized from under us fail */
> > - if (unlikely(pmd_trans_huge(*dst_pmd))) {
> > + dst_pmdval = pmdp_get_lockless(dst_pmd);
> > + if (unlikely(pmd_none(dst_pmdval) || pmd_trans_huge(dst_pmdval))) {
>
> Before commit 0d940a9b270b, should we also check for
> is_pmd_migration_entry(), pmd_devmap() and pmd_bad() here?
Oooh. I think you're right that this check is insufficient, thanks for
spotting that.
I think I should probably change the check to something like this?
if (unlikely(!pmd_present(dst_pmdval) || pmd_trans_huge(dst_pmdval) ||
pmd_devmap(dst_pmdval) || pmd_bad(dst_pmdval))) {
!pmd_present() implies !is_pmd_migration_entry(). And the pmd_bad() at
the end shouldn't be necessary if everything is working right, I'm
just tacking it on to be safe.
I'll send a v2 with this change soon.
(Alternatively, pmd_leaf() might be useful here, but then we'd have to
figure an alternate way of doing this for the backport.)
next prev parent reply other threads:[~2024-08-13 14:58 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-12 16:42 [PATCH 0/2] userfaultfd: fix races around pmd_trans_huge() check Jann Horn
2024-08-12 16:42 ` [PATCH 1/2] userfaultfd: Fix pmd_trans_huge() recheck race Jann Horn
2024-08-13 6:19 ` Qi Zheng
2024-08-13 14:57 ` Jann Horn [this message]
2024-08-13 9:28 ` David Hildenbrand
2024-08-12 16:42 ` [PATCH 2/2] userfaultfd: Don't BUG_ON() if khugepaged yanks our page table Jann Horn
2024-08-13 6:24 ` Qi Zheng
2024-08-13 9:27 ` David Hildenbrand
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=CAG48ez3u4tCaP6MSjxwqZQ70teFJHag7z9wRmd8LJXXee3tTTw@mail.gmail.com \
--to=jannh@google.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=hughd@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=stable@vger.kernel.org \
--cc=xemul@virtuozzo.com \
--cc=zhengqi.arch@bytedance.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox