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 X-Spam-Level: X-Spam-Status: No, score=-5.4 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id EC787C352AA for ; Tue, 1 Oct 2019 18:43:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A302F21855 for ; Tue, 1 Oct 2019 18:43:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=nvidia.com header.i=@nvidia.com header.b="ZnpoLTOS" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A302F21855 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nvidia.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 539318E0001; Tue, 1 Oct 2019 14:43:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4C4616B0006; Tue, 1 Oct 2019 14:43:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 363428E0001; Tue, 1 Oct 2019 14:43:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0233.hostedemail.com [216.40.44.233]) by kanga.kvack.org (Postfix) with ESMTP id 1065F6B0003 for ; Tue, 1 Oct 2019 14:43:36 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id A8A1F82437CF for ; Tue, 1 Oct 2019 18:43:35 +0000 (UTC) X-FDA: 75996089190.16.walk26_3c615ff4cb04a X-HE-Tag: walk26_3c615ff4cb04a X-Filterd-Recvd-Size: 8126 Received: from hqemgate15.nvidia.com (hqemgate15.nvidia.com [216.228.121.64]) by imf13.hostedemail.com (Postfix) with ESMTP for ; Tue, 1 Oct 2019 18:43:34 +0000 (UTC) Received: from hqpgpgate101.nvidia.com (Not Verified[216.228.121.13]) by hqemgate15.nvidia.com (using TLS: TLSv1.2, DES-CBC3-SHA) id ; Tue, 01 Oct 2019 11:43:40 -0700 Received: from hqmail.nvidia.com ([172.20.161.6]) by hqpgpgate101.nvidia.com (PGP Universal service); Tue, 01 Oct 2019 11:43:32 -0700 X-PGP-Universal: processed; by hqpgpgate101.nvidia.com on Tue, 01 Oct 2019 11:43:32 -0700 Received: from DRHQMAIL107.nvidia.com (10.27.9.16) by HQMAIL105.nvidia.com (172.20.187.12) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 1 Oct 2019 18:43:32 +0000 Received: from [10.110.48.28] (10.124.1.5) by DRHQMAIL107.nvidia.com (10.27.9.16) with Microsoft SMTP Server (TLS) id 15.0.1473.3; Tue, 1 Oct 2019 18:43:30 +0000 Subject: Re: [PATCH v3 3/4] mm: don't expose non-hugetlb page to fast gup prematurely To: Jan Kara CC: Michal Hocko , "Kirill A. Shutemov" , Yu Zhao , Peter Zijlstra , Andrew Morton , "Kirill A . Shutemov" , Ingo Molnar , Arnaldo Carvalho de Melo , "Alexander Shishkin" , Jiri Olsa , Namhyung Kim , Vlastimil Babka , "Hugh Dickins" , =?UTF-8?B?SsOpcsO0bWUgR2xpc3Nl?= , Andrea Arcangeli , "Aneesh Kumar K . V" , David Rientjes , Matthew Wilcox , Lance Roy , "Ralph Campbell" , Jason Gunthorpe , Dave Airlie , Thomas Hellstrom , "Souptick Joarder" , Mel Gorman , Mike Kravetz , Huang Ying , Aaron Lu , Omar Sandoval , Thomas Gleixner , Vineeth Remanan Pillai , Daniel Jordan , Mike Rapoport , Joel Fernandes , Mark Rutland , Alexander Duyck , Pavel Tatashin , David Hildenbrand , Juergen Gross , Anthony Yznaga , Johannes Weiner , "Darrick J . Wong" , , References: <20190924232459.214097-1-yuzhao@google.com> <20190924232459.214097-3-yuzhao@google.com> <20190925082530.GD4536@hirez.programming.kicks-ass.net> <20190925222654.GA180125@google.com> <20190926102036.od2wamdx2s7uznvq@box> <9465df76-0229-1b44-5646-5cced1bc1718@nvidia.com> <20190927123056.GE26848@dhcp22.suse.cz> <20190930092003.GA22118@quack2.suse.cz> <6bba357a-1706-7cdb-8a11-359157a21ae8@nvidia.com> <20191001071008.GA25062@quack2.suse.cz> From: John Hubbard X-Nvconfidentiality: public Message-ID: Date: Tue, 1 Oct 2019 11:43:30 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20191001071008.GA25062@quack2.suse.cz> X-Originating-IP: [10.124.1.5] X-ClientProxiedBy: HQMAIL111.nvidia.com (172.20.187.18) To DRHQMAIL107.nvidia.com (10.27.9.16) Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1569955420; bh=5jVNwDlRxMyRRxUik65M4egDbbokUWEUwpvAAuTn7zs=; h=X-PGP-Universal:Subject:To:CC:References:From:X-Nvconfidentiality: Message-ID:Date:User-Agent:MIME-Version:In-Reply-To: X-Originating-IP:X-ClientProxiedBy:Content-Type:Content-Language: Content-Transfer-Encoding; b=ZnpoLTOS9HuRKexJq6JOSKu4fo5jjotme9PvFgZ+nKv0GHHwkQu/rboD6HWsJls24 3wuhn0KXfW4FCa7mpZZHcWkHrnJ0C2MF2jVXx/DqfHk8tm/ZNwuyuM7cLOUmU7I1DC TFvqLu4xPzC5DC/LRJuxSh8tACrWbi5sbkHhTeostFK/w9FYIm3YC7mRK4FJk0sHRJ p8AwPD6KGDD351uCgJ+l7DPsksAEMlDIh1xXVfnU0soveb9FsV654GoQpTrmR/SIK1 uvltbCGI3tX7pHyJOruMXja6oGoAA48bq9mhksVajk5uwH8Vce/oWtQaTIVDGsPbEX 0oIXuT+MnUZvw== 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 10/1/19 12:10 AM, Jan Kara wrote: > On Mon 30-09-19 10:57:08, John Hubbard wrote: >> On 9/30/19 2:20 AM, Jan Kara wrote: >>> On Fri 27-09-19 12:31:41, John Hubbard wrote: >>>> On 9/27/19 5:33 AM, Michal Hocko wrote: >>>>> On Thu 26-09-19 20:26:46, John Hubbard wrote: >>>>>> On 9/26/19 3:20 AM, Kirill A. Shutemov wrote: >> ... >> 2. Your code above is after the "pmd = READ_ONCE(*pmdp)" line, so by then, >> it's already found the pte based on reading a stale pmd. So checking the >> pte seems like it's checking the wrong thing--it's too late, for this case, >> right? > > Well, if PMD is getting freed, all PTEs in it should be cleared by that > time, shouldn't they? So although we read from stale PMD, either we already > see cleared PTE or the check pte_val(pte) != pte_val(*ptep) will fail and > so we never actually succeed in getting stale PTE entry (at least unless > the page table page that used to be PMD can get freed and reused > - which is not the case in the example you've shown above). > Right, that's not what the example shows, but there is nothing here to prevent the page table pages from being freed and re-used. > So I still don't see a problem. That being said I don't feel being expert > in this particular area. I just always thought GUP prevents races like this > by the scheme I describe so I'd like to see what I'm missing :). > I'm very much still "in training" here, so I hope I'm not wasting everyone's time. But I feel confident in stating at least this much: There are two distinct lockless synchronization mechanisms here, each protecting against a different issue, and it's important not to conflate them and think that one protects against the other. I still see a hole in (2) below. The mechanisms are: 1) Protection against a page (not the page table itself) getting freed while get_user_pages*() is trying to take a reference to it. This is avoided by the try-get and the re-checking of pte values that you mention above. It's an elegant little thing, too. :) 2) Protection against page tables getting freed while a get_user_pages_fast() call is in progress. This relies on disabling interrupts in gup_fast(), while firing interrupts in the freeing path (via tlb flushing, IPIs). And on memory barriers (doh--missing!) to avoid leaking memory references outside of the irq disabling. This one has a problem, because Documentation/memory-barriers.txt points out: INTERRUPT DISABLING FUNCTIONS ----------------------------- Functions that disable interrupts (ACQUIRE equivalent) and enable interrupts (RELEASE equivalent) will act as compiler barriers only. So if memory or I/O barriers are required in such a situation, they must be provided from some other means. ...and so I'm suggesting that we need something approximately like this: diff --git a/mm/gup.c b/mm/gup.c index 23a9f9c9d377..1678d50a2d8b 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -2415,7 +2415,9 @@ int get_user_pages_fast(unsigned long start, int nr_pages, if (IS_ENABLED(CONFIG_HAVE_FAST_GUP) && gup_fast_permitted(start, end)) { local_irq_disable(); + smp_mb(); gup_pgd_range(addr, end, gup_flags, pages, &nr); + smp_mb(); local_irq_enable(); ret = nr; thanks, -- John Hubbard NVIDIA