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=-2.5 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=no 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 9835AC4360C for ; Mon, 30 Sep 2019 09:19:51 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3C36621721 for ; Mon, 30 Sep 2019 09:19:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3C36621721 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 96BAA6B0003; Mon, 30 Sep 2019 05:19:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 91D076B0005; Mon, 30 Sep 2019 05:19:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80BE96B0006; Mon, 30 Sep 2019 05:19:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0197.hostedemail.com [216.40.44.197]) by kanga.kvack.org (Postfix) with ESMTP id 5F4196B0003 for ; Mon, 30 Sep 2019 05:19:50 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id F3D0F1E095 for ; Mon, 30 Sep 2019 09:19:49 +0000 (UTC) X-FDA: 75991039698.18.town01_1b8155bf9e20c X-HE-Tag: town01_1b8155bf9e20c X-Filterd-Recvd-Size: 6533 Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Mon, 30 Sep 2019 09:19:48 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 3845CB15C; Mon, 30 Sep 2019 09:19:46 +0000 (UTC) Received: by quack2.suse.cz (Postfix, from userid 1000) id F21881E3ED9; Mon, 30 Sep 2019 11:20:03 +0200 (CEST) Date: Mon, 30 Sep 2019 11:20:03 +0200 From: Jan Kara To: John Hubbard 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 , =?iso-8859-1?B?Suly9G1l?= Glisse , Andrea Arcangeli , "Aneesh Kumar K . V" , David Rientjes , Matthew Wilcox , Lance Roy , Ralph Campbell , Jason Gunthorpe , Dave Airlie , Thomas Hellstrom , Souptick Joarder , Mel Gorman , Jan Kara , 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" , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH v3 3/4] mm: don't expose non-hugetlb page to fast gup prematurely Message-ID: <20190930092003.GA22118@quack2.suse.cz> References: <20190914070518.112954-1-yuzhao@google.com> <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> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) 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 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: > > > > BTW, have you looked at other levels of page table hierarchy. Do we have > > > > the same issue for PMD/PUD/... pages? > > > > > > > > > > Along the lines of "what other memory barriers might be missing for > > > get_user_pages_fast(), I'm also concerned that the synchronization between > > > get_user_pages_fast() and freeing the page tables might be technically broken, > > > due to missing memory barriers on the get_user_pages_fast() side. Details: > > > > > > gup_fast() disables interrupts, but I think it also needs some sort of > > > memory barrier(s), in order to prevent reads of the page table (gup_pgd_range, > > > etc) from speculatively happening before the interrupts are disabled. > > > > Could you be more specific about the race scenario please? I thought > > that the unmap path will be serialized by the pte lock. > > > > I don't see a pte lock anywhere here. > > This case is really pretty far out there, but without run-time memory barriers > I don't think we can completely rule it out: > > CPU 0 CPU 1 > -------- --------------- > get_user_pages_fast() > > do_unmap() > unmap_region() > free_pgtables() > /* > * speculative reads, re-ordered > * by the CPU at run time, not > * compile time. The function calls > * are not really in this order, but > * the corresponding reads could be. > */ > gup_pgd_range() > gup_p4d_range() > gup_pud_range() > gup_pmd_range() > pmd = READ_ONCE(*pmdp) > /* This is stale */ > > tlb_finish_mmu() > tlb_flush_mmu() > tlb_flush_mmu_tlbonly() > tlb_flush() > flush_tlb_mm > flush_tlb_mm_range > flush_tlb_others > native_flush_tlb_others > smp_call_function_many: IPIs > ...blocks until CPU1 reenables > interrupts > > local_irq_disable() > ...continue the page walk based > on stale/freed data... Yes, but then we have: head = try_get_compound_head(page, 1); which has an atomic operation providing barrier in it and then we have: if (unlikely(pte_val(pte) != pte_val(*ptep))) { put_page(head); goto pte_unmap; } So we reload PTE again and check the value didn't change. Which should prevent the race you explain above. Or do I miss anything? Honza -- Jan Kara SUSE Labs, CR