From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC3B4ECAAD2 for ; Thu, 1 Sep 2022 23:27:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8534280083; Thu, 1 Sep 2022 19:27:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DBEC8000D; Thu, 1 Sep 2022 19:27:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 655F680083; Thu, 1 Sep 2022 19:27:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 50FC58000D for ; Thu, 1 Sep 2022 19:27:00 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 22F9A40874 for ; Thu, 1 Sep 2022 23:27:00 +0000 (UTC) X-FDA: 79865104200.16.E63979A Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf28.hostedemail.com (Postfix) with ESMTP id 5D1D3C006F for ; Thu, 1 Sep 2022 23:26:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662074818; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=4zhQGyUQdaZfg/v5RuJfK81JYWToWC40+GtvI6Qq18w=; b=iHV28TC774j54FPqkccV9y/0O4CbLJMFX+ErH5lpjKZ/pfsCdhyF60BK/w7amzX+lDQzp7 VwldjhUdnivvOwX1sibMMvgjkoq0mv5BcAnqQeu35MBOIJvRmbYQi0c2IZn7rX8IERPxi5 F16g3KZ1KWU/+FDTfKiCbfYBNCm5u8I= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-522-gMZrkmrhNkiomIE3eZRfCQ-1; Thu, 01 Sep 2022 19:26:57 -0400 X-MC-Unique: gMZrkmrhNkiomIE3eZRfCQ-1 Received: by mail-qv1-f71.google.com with SMTP id mr4-20020a056214348400b00498fa61c57bso269016qvb.0 for ; Thu, 01 Sep 2022 16:26:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=4zhQGyUQdaZfg/v5RuJfK81JYWToWC40+GtvI6Qq18w=; b=h+NOC7HFARf/gb3h9hksqrwrut9ng4gHm3akMecRYSpZVI5rF7Hmhq0FOQBRdVVdJK NxkzT8ByeUu7cYmFaQf7/7+l3UJTY7Z3RWOeUwl/6g9OD0dWvCVsuFKyroy2I00r6s67 Fc2+nd92SobS6d0jNPykcRLvzDfcDgie2EzearjHjSKHj8pU8Xg1xDEYX9Vp9RbiQYPX RgxNTlI8V37qoQAE/GHg2d5fVuYVCYp1A820GvfnMJAkeadpnWTo1SoHcyIimwW9fLHW ivRU7shL6MDiw/lVXqH5r71xZrne2nzhbdMGl7tqgCeMyuGk8PH8XXiUNPNr9j742v94 cQKA== X-Gm-Message-State: ACgBeo0LuljNed2Rc4zz63gYdWkZkDu4cfNLBFXW/YYVju6RB+nwM4X1 v9dgrRaCaCt+Q7Ru6fQ/WAXq8cQ3v0bmjJU8CJuN+Q9mymZA1hRtI/Dj8zczz1a4DTqYhNK5K/i V+IexF3qMQXA= X-Received: by 2002:a05:6214:238b:b0:48a:f607:c4e0 with SMTP id fw11-20020a056214238b00b0048af607c4e0mr27083585qvb.44.1662074817146; Thu, 01 Sep 2022 16:26:57 -0700 (PDT) X-Google-Smtp-Source: AA6agR7PbY3MIHgmSUi6mRoFOFLyzTJoF2sshpE2r5ybOvJGbOWappmAtfv6+P5IvtHPGmBYk24LkQ== X-Received: by 2002:a05:6214:238b:b0:48a:f607:c4e0 with SMTP id fw11-20020a056214238b00b0048af607c4e0mr27083571qvb.44.1662074816889; Thu, 01 Sep 2022 16:26:56 -0700 (PDT) Received: from xz-m1.local (bras-base-aurron9127w-grc-35-70-27-3-10.dsl.bell.ca. [70.27.3.10]) by smtp.gmail.com with ESMTPSA id d3-20020a05620a166300b006b5c061844fsm203236qko.49.2022.09.01.16.26.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 16:26:56 -0700 (PDT) Date: Thu, 1 Sep 2022 19:26:54 -0400 From: Peter Xu To: Yang Shi Cc: david@redhat.com, kirill.shutemov@linux.intel.com, jhubbard@nvidia.com, jgg@nvidia.com, hughd@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm: gup: fix the fast GUP race against THP collapse Message-ID: References: <20220901222707.477402-1-shy828301@gmail.com> MIME-Version: 1.0 In-Reply-To: <20220901222707.477402-1-shy828301@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662074819; a=rsa-sha256; cv=none; b=S7EpY6iZbV2yJlRIi2nVKLvfPtnmqyhhQ3sLBPKWUipTeZk643cS5jgIUBxDc38SEsYaVF UD3kLfiFp4gjFvikNHrWOZZx8KzxGCPeEjHUsCR4cwgpOXeAJBKEmmupj3MetZ5sfGTR4q tMMhR/e/Hol1lHf/fZ16MY97hThRNe8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iHV28TC7; spf=pass (imf28.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662074819; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4zhQGyUQdaZfg/v5RuJfK81JYWToWC40+GtvI6Qq18w=; b=BpKjL3N8hKHo1Ltir6TvCduQ9hrsN6bDMwALeEVJgPHWT6xW0eNiaCWfp3m27d82wd1nMt 9j0ueQKcWBJlLVUFRv9D05Mj2Tp+zMT95T8zF97jn5+KbFx9eGFTPigMEOA+Sn3iMjXIPC ySoqGgWbtXK7z4e0xVpHVfUYTOyJSCE= Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iHV28TC7; spf=pass (imf28.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Stat-Signature: srhjzxf7kshr8rmrf5kwnthr8kb6iiks X-Rspamd-Queue-Id: 5D1D3C006F X-Rspamd-Server: rspam04 X-Rspam-User: X-HE-Tag: 1662074819-318440 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hi, Yang, On Thu, Sep 01, 2022 at 03:27:07PM -0700, Yang Shi 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. If TLB flush (or, IPI broadcasts) used to work to protect against gup-fast, I'm kind of confused why it's not sufficient even if with RCU gup? Isn't that'll keep working as long as interrupt disabled (which current fast-gup will still do)? IIUC the issue is you suspect not all archs correctly implemented pmdp_collapse_flush(), or am I wrong? > 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. Could the race also be fixed by impl pmdp_collapse_flush() correctly for the archs that are missing? Do you know which arch(s) is broken with it? It's just not clear to me whether this patch is an optimization or a fix, if it's a fix whether the IPI broadcast in ppc pmdp_collapse_flush() would still be needed. Thanks, > > Also update the stale comment about serializing against fast GUP in > khugepaged. > > Fixes: 2667f50e8b81 ("mm: introduce a general RCU get_user_pages_fast()") > Signed-off-by: Yang Shi > --- > mm/gup.c | 30 ++++++++++++++++++++++++------ > mm/khugepaged.c | 10 ++++++---- > 2 files changed, 30 insertions(+), 10 deletions(-) > > diff --git a/mm/gup.c b/mm/gup.c > index f3fc1f08d90c..4365b2811269 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -2380,8 +2380,9 @@ static void __maybe_unused undo_dev_pagemap(int *nr, int nr_start, > } > > #ifdef CONFIG_ARCH_HAS_PTE_SPECIAL > -static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > - unsigned int flags, struct page **pages, int *nr) > +static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, > + unsigned long end, unsigned int flags, > + struct page **pages, int *nr) > { > struct dev_pagemap *pgmap = NULL; > int nr_start = *nr, ret = 0; > @@ -2423,7 +2424,23 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > goto pte_unmap; > } > > - if (unlikely(pte_val(pte) != pte_val(*ptep))) { > + /* > + * THP collapse conceptually does: > + * 1. Clear and flush PMD > + * 2. Check the base page refcount > + * 3. Copy data to huge page > + * 4. Clear PTE > + * 5. Discard the base page > + * > + * So fast GUP may race with THP collapse then pin and > + * return an old page since TLB flush is no longer sufficient > + * to serialize against fast GUP. > + * > + * Check PMD, if it is changed just back off since it > + * means there may be parallel THP collapse. > + */ > + if (unlikely(pmd_val(pmd) != pmd_val(*pmdp)) || > + unlikely(pte_val(pte) != pte_val(*ptep))) { > gup_put_folio(folio, 1, flags); > goto pte_unmap; > } > @@ -2470,8 +2487,9 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > * get_user_pages_fast_only implementation that can pin pages. Thus it's still > * useful to have gup_huge_pmd even if we can't operate on ptes. > */ > -static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, > - unsigned int flags, struct page **pages, int *nr) > +static int gup_pte_range(pmd_t pmd, pmd_t *pmdp, unsigned long addr, > + unsigned long end, unsigned int flags, > + struct page **pages, int *nr) > { > return 0; > } > @@ -2791,7 +2809,7 @@ static int gup_pmd_range(pud_t *pudp, pud_t pud, unsigned long addr, unsigned lo > if (!gup_huge_pd(__hugepd(pmd_val(pmd)), addr, > PMD_SHIFT, next, flags, pages, nr)) > return 0; > - } else if (!gup_pte_range(pmd, addr, next, flags, pages, nr)) > + } else if (!gup_pte_range(pmd, pmdp, addr, next, flags, pages, nr)) > return 0; > } while (pmdp++, addr = next, addr != end); > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index 2d74cf01f694..518b49095db3 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -1049,10 +1049,12 @@ static int collapse_huge_page(struct mm_struct *mm, unsigned long address, > > pmd_ptl = pmd_lock(mm, pmd); /* probably unnecessary */ > /* > - * After this gup_fast can't run anymore. This also removes > - * any huge TLB entry from the CPU so we won't allow > - * huge and small TLB entries for the same virtual address > - * to avoid the risk of CPU bugs in that area. > + * This removes any huge TLB entry from the CPU so we won't allow > + * huge and small TLB entries for the same virtual address to > + * avoid the risk of CPU bugs in that area. > + * > + * Parallel fast GUP is fine since fast GUP will back off when > + * it detects PMD is changed. > */ > _pmd = pmdp_collapse_flush(vma, address, pmd); > spin_unlock(pmd_ptl); > -- > 2.26.3 > -- Peter Xu