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 D8BC9ECAAA1 for ; Fri, 2 Sep 2022 06:42:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B518800C0; Fri, 2 Sep 2022 02:42:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 263E48008D; Fri, 2 Sep 2022 02:42:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 104F4800C0; Fri, 2 Sep 2022 02:42:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id F39228008D for ; Fri, 2 Sep 2022 02:42:32 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C705280266 for ; Fri, 2 Sep 2022 06:42:32 +0000 (UTC) X-FDA: 79866201744.10.FEAC071 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf22.hostedemail.com (Postfix) with ESMTP id 6ADA0C0060 for ; Fri, 2 Sep 2022 06:42:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662100951; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zd920y5kuntN3Fe3mBbgf2SnydeE5HI0Dhy/WeAHKqc=; b=BfaQq5ldHam3ZEVaHJ+7JV4EQOOJd6lcY8Zjgrx5a2d8tES09npuTs0Jun7jKUlyZEqSxJ KHLOjYRYTFVymImQDcCH0DYyXwJLk2DIn826R9zQj1Ol2f6GG2oQe5pICPiZU+SSG6Dfcw +5c7FBTWP5b7FWYGFQZqGdTSmxhtUCc= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-527-eVI2TSAVOoWRwJSV_5Kmfg-1; Fri, 02 Sep 2022 02:42:30 -0400 X-MC-Unique: eVI2TSAVOoWRwJSV_5Kmfg-1 Received: by mail-wm1-f72.google.com with SMTP id n1-20020a7bc5c1000000b003a682987306so278847wmk.1 for ; Thu, 01 Sep 2022 23:42:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date; bh=zd920y5kuntN3Fe3mBbgf2SnydeE5HI0Dhy/WeAHKqc=; b=N2270RGO8E2KEzQOKOoo8jRG/Ebt7eZOBwwDS2M5IRDfsmJKIlN2aRT4SLfVf4L3ld oCefGxJKW9kzZeCuRBQcnvMrPdFJ/gvcnBle2YJRSxXwyEwi/JUHzxOVnyVpW5Zkj5rq fJBA3kM0sZIV46XTHjMaEw8Dwg1224EQKZLtJrUFm6ethP8YEcVupv1uHMLOgKohKhNx +kdZVDALNRQ32q42xyAkXMXH4vXmYDGG2DoGMvOWq4mlDFCYO/eYfruHz0P0HhvcWuRV yx3ALNEqKyeCt/frOinJSCIgzHiv+Wgz9QV86k5l5R36fdfVApQpU5JJvZ/KgmLlUY7p Ep4Q== X-Gm-Message-State: ACgBeo1Nc1kp56yXelMGVxSf69BkEJCh5sWCHyfFCwBtSW0aNkVx+IrS KQxi4mTCqo2TcDw/07WHVKJGa9hhSHApqj+qRLfYXgR527cXILwlSXO/h2Urf58lLKzc48UrxnO uEo+R9MOyaz4= X-Received: by 2002:a05:600c:4ec9:b0:3a5:a567:137f with SMTP id g9-20020a05600c4ec900b003a5a567137fmr1673994wmq.46.1662100949460; Thu, 01 Sep 2022 23:42:29 -0700 (PDT) X-Google-Smtp-Source: AA6agR7NOLu61wFcCZ0OBX41IN7InJbnGYlfsBwf1Tz6mQhGc6CVw6ILI/UTYKBvLA+1ocvRUh+E6w== X-Received: by 2002:a05:600c:4ec9:b0:3a5:a567:137f with SMTP id g9-20020a05600c4ec900b003a5a567137fmr1673979wmq.46.1662100949183; Thu, 01 Sep 2022 23:42:29 -0700 (PDT) Received: from ?IPV6:2003:cb:c714:4800:2077:1bf6:40e7:2833? (p200300cbc714480020771bf640e72833.dip0.t-ipconnect.de. [2003:cb:c714:4800:2077:1bf6:40e7:2833]) by smtp.gmail.com with ESMTPSA id v3-20020a5d6103000000b002252cb35184sm747022wrt.25.2022.09.01.23.42.28 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 01 Sep 2022 23:42:28 -0700 (PDT) Message-ID: Date: Fri, 2 Sep 2022 08:42:28 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.12.0 Subject: Re: [PATCH] mm: gup: fix the fast GUP race against THP collapse To: Yang Shi , peterx@redhat.com, kirill.shutemov@linux.intel.com, jhubbard@nvidia.com, jgg@nvidia.com, hughd@google.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20220901222707.477402-1-shy828301@gmail.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20220901222707.477402-1-shy828301@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1662100952; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zd920y5kuntN3Fe3mBbgf2SnydeE5HI0Dhy/WeAHKqc=; b=3LKgGKga9ph169H794/1dNfSOG0IBj1FYtRkxe1NMIw2J95PZGmY6enREgOIew23VNGfQ+ NboUftRNJXKp17cTR0i6l5L3FLSzIyCUWmgFl4B1T1kJtaowAr8CMtpsgYH9LCNqLfwzSY /pjl0zZ8Zuw4BnmUPf3bkePhd0mjdrw= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BfaQq5ld; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf22.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1662100952; a=rsa-sha256; cv=none; b=hzXFYmA5lo+SpbO6y2bTFtYLkNWpfnNZmvqoBIZrtUTjO3ozfMJLym0IgINInN2ABvg45S G1zP3o6d7YGmRGIMXkALaZyGIheQTbdDouGxDzjTiPuwaURlhxF9ieisoSjkdE4wjhjJfX 5gEMFYEQoCg2tE0cjez3Vemqam91ZaM= Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BfaQq5ld; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf22.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com X-Rspamd-Queue-Id: 6ADA0C0060 X-Stat-Signature: qs4hybast4rabixpisgtgfpjunytfe9c X-Rspam-User: X-Rspamd-Server: rspam02 X-HE-Tag: 1662100952-438061 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: On 02.09.22 00:27, 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. 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()") > 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); As long as pmdp_collapse_flush() implies a full memory barrier (which I think it does), this should work as expected. Hopefully someone with experience on RCU GUP-fast (Jason, John? :) ) can double-check. To me this sound sane. Acked-by: David Hildenbrand -- Thanks, David / dhildenb