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=-8.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 AED58C4363D for ; Thu, 24 Sep 2020 14:35:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 23C90238E3 for ; Thu, 24 Sep 2020 14:35:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="H9hXmr4A" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 23C90238E3 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 789A28E0001; Thu, 24 Sep 2020 10:35:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7618D6B005D; Thu, 24 Sep 2020 10:35:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 650B38E0001; Thu, 24 Sep 2020 10:35:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0225.hostedemail.com [216.40.44.225]) by kanga.kvack.org (Postfix) with ESMTP id 4E1C86B005C for ; Thu, 24 Sep 2020 10:35:25 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id A52683623 for ; Thu, 24 Sep 2020 14:35:24 +0000 (UTC) X-FDA: 77298202968.09.bird16_4904d7f2715f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 7E4AD180AD807 for ; Thu, 24 Sep 2020 14:35:24 +0000 (UTC) X-HE-Tag: bird16_4904d7f2715f X-Filterd-Recvd-Size: 9808 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf34.hostedemail.com (Postfix) with ESMTP for ; Thu, 24 Sep 2020 14:35:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600958123; 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=zvhmXdurMqfIrlufAAPJfQcazZ/8TT7PFFG3tysOQUc=; b=H9hXmr4AZAeDPeIhNSYWtR2rqFtspOtdtx0gZxd2/5QlDorswpLwzdwr7P0bV1zCbjQmx1 mBqy4XJ8RKZOQjPQ9caFahkzHCbZWEztALL1TZ4ZLh2vUfCsYcPh33IIzIkEJIzujaEV+Y Di0JpD1W2pOYSFGnXwvOfTheIsox4hc= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-159-Rg6A86ifMjyWseCO-rPnKg-1; Thu, 24 Sep 2020 10:35:21 -0400 X-MC-Unique: Rg6A86ifMjyWseCO-rPnKg-1 Received: by mail-qv1-f72.google.com with SMTP id f21so2248978qve.9 for ; Thu, 24 Sep 2020 07:35:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=zvhmXdurMqfIrlufAAPJfQcazZ/8TT7PFFG3tysOQUc=; b=V2YDW2o2wNxQYUrIsyhwFeRD3M1xAjy1HIHyfGOtB/6iBC3FK9Uda6rJxEdzZDDEyD oE7pzS42ZOEOKi/fEQT3ts8Pg5bCbIbih3pEUijUHA925c0QZB9kCEMN3/LQsfR2SgMW hYEgj5e2Yk2aV+4Q6oVPhjdVIdtOQUPj6HsW8tNejlm2KbNSM2UA1ohQ8MaJOSC47HB5 nHGwvLt8FuTrQEsAW/5Ce2XesnlLnJM/Ae1QqbzJHIr7nDb2Yg43R/RZc0+9tMI+qUCd lhClWyXrB0jdkwVJYHqhipUXH4Lk5v/Q+kPmqfYL3o8DDr0txELRUxIctuWXsAmX9i6T urNg== X-Gm-Message-State: AOAM533kAG/LUw19usORKQr+SJukt27UUvU7udHYlDSbK9sLwqd7jr5I 8CLTUsPvBo6t6NWj7es2AnUHu1I7KwwNUSM2mPlXmCKieJIzB79wGOVHWmEFDU1dM713dhG5K8i hX6kNljlDxzs= X-Received: by 2002:ae9:f701:: with SMTP id s1mr5256078qkg.446.1600958120816; Thu, 24 Sep 2020 07:35:20 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz/n7SJnWJZzZBSNTparOQ6wMGNiV1kVJQwLJonregx+J8ZQSUFVAhRN6bKAXZ3EEGQYF8Vew== X-Received: by 2002:ae9:f701:: with SMTP id s1mr5256036qkg.446.1600958120399; Thu, 24 Sep 2020 07:35:20 -0700 (PDT) Received: from xz-x1 (bras-vprn-toroon474qw-lp130-11-70-53-122-15.dsl.bell.ca. [70.53.122.15]) by smtp.gmail.com with ESMTPSA id m6sm2108265qkh.106.2020.09.24.07.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Sep 2020 07:35:19 -0700 (PDT) Date: Thu, 24 Sep 2020 10:35:17 -0400 From: Peter Xu To: Jason Gunthorpe Cc: John Hubbard , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , Jan Kara , Michal Hocko , Kirill Tkhai , Kirill Shutemov , Hugh Dickins , Christoph Hellwig , Andrea Arcangeli , Oleg Nesterov , Leon Romanovsky , Linus Torvalds , Jann Horn Subject: Re: [PATCH 1/5] mm: Introduce mm_struct.has_pinned Message-ID: <20200924143517.GD79898@xz-x1> References: <20200921211744.24758-1-peterx@redhat.com> <20200921211744.24758-2-peterx@redhat.com> <224908c1-5d0f-8e01-baa9-94ec2374971f@nvidia.com> <20200922151736.GD19098@xz-x1> <20200922161046.GB731578@ziepe.ca> <20200922175415.GI19098@xz-x1> <20200922191116.GK8409@ziepe.ca> <20200923002735.GN19098@xz-x1> <20200923170759.GA9916@ziepe.ca> MIME-Version: 1.0 In-Reply-To: <20200923170759.GA9916@ziepe.ca> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline 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 Wed, Sep 23, 2020 at 02:07:59PM -0300, Jason Gunthorpe wrote: > On Tue, Sep 22, 2020 at 08:27:35PM -0400, Peter Xu wrote: > > On Tue, Sep 22, 2020 at 04:11:16PM -0300, Jason Gunthorpe wrote: > > > On Tue, Sep 22, 2020 at 01:54:15PM -0400, Peter Xu wrote: > > > > diff --git a/mm/memory.c b/mm/memory.c > > > > index 8f3521be80ca..6591f3f33299 100644 > > > > +++ b/mm/memory.c > > > > @@ -888,8 +888,8 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, > > > > * Because we'll need to release the locks before doing cow, > > > > * pass this work to upper layer. > > > > */ > > > > - if (READ_ONCE(src_mm->has_pinned) && wp && > > > > - page_maybe_dma_pinned(page)) { > > > > + if (wp && page_maybe_dma_pinned(page) && > > > > + READ_ONCE(src_mm->has_pinned)) { > > > > /* We've got the page already; we're safe */ > > > > data->cow_old_page = page; > > > > data->cow_oldpte = *src_pte; > > > > > > > > I can also add some more comment to emphasize this. > > > > > > It is not just that, but the ptep_set_wrprotect() has to be done > > > earlier. > > > > Now I understand your point, I think.. So I guess it's not only about > > has_pinned, but it should be a race between the fast-gup and the fork() code, > > even if has_pinned is always set. > > Yes > > > > The best algorithm I've thought of is something like: > > > > > > pte_map_lock() > > > if (page) { > > > if (wp) { > > > ptep_set_wrprotect() > > > /* Order with try_grab_compound_head(), either we see > > > * page_maybe_dma_pinned(), or they see the wrprotect */ > > > get_page(); > > > > Is this get_page() a must to be after ptep_set_wrprotect() > > explicitly? > > No, just before page_maybe_dma_pinned() > > > IIUC what we need is to order ptep_set_wrprotect() and > > page_maybe_dma_pinned() here. E.g., would a "mb()" work? > > mb() is not needed because page_maybe_dma_pinned() has an atomic > barrier too. I like to see get_page() followed immediately by > page_maybe_dma_pinned() since they are accessing the same atomic and > could be fused together someday If so, I'd hope you won't disagree that I still move the get_page() out of the "if (wp)". Not only it's a shared operation no matter whether "if (wp)" or not, but I'm afraid it would confuse future readers on a special ordering on the get_page() and the wrprotect(), especially with the comment above. > > > Another thing is, do we need similar thing for e.g. gup_pte_range(), so that > > to guarantee ordering of try_grab_compound_head() and the pte change > > check? > > gup_pte_range() is as I quoted? The gup slow path ends up in > follow_page_pte() which uses the pte lock so is OK. > > > > Another question is, how about read fast-gup for pinning? Because we can't use > > the write-protect mechanism to block a read gup. I remember we've discussed > > similar things and iirc your point is "pinned pages should always be with > > WRITE". However now I still doubt it... Because I feel like read gup is still > > legal (as I mentioned previously - when device purely writes to the page and > > the processor only reads from it). > > We need a definition for what FOLL_PIN means. After this work on fork > I propose that FOLL_PIN means: > > The page is in-use for DMA and the CPU PTE should not be changed > without explicit involvement of the application (eg via mmap/munmap) > > If GUP encounters a read-only page during FOLL_PIN the behavior should > depend on what the fault handler would do. If the fault handler would > trigger COW and replace the PTE then it violates the above. GUP should > do the COW before pinning. > > If the fault handler would SIGSEGV then GUP can keep the read-only > page and allow !FOLL_WRITE access. The PTE should not be replaced for > other reasons (though I think there is work there too). > > For COW related issues the idea is the mm_struct doing the pin will > never trigger a COW. When other processes hit the COW they copy the > page into their mm and don't touch the source MM's PTE. > > Today we do this roughly with FOLL_FORCE and FOLL_WRITE in the users, > but a more nuanced version and documentation would be much clearer. > > Unfortunately just doing simple read GUP potentially exposes things to > various COW related data corruption races. > > This is a discussion beyond this series though.. Yes. It's kind of related here on whether we can still use wrprotect() to guard against fast-gup, though. So my understanding is that we should still at least need the other patch [1] that I proposed in the other thread to force break-cow for read-only gups (that patch is not only for fast-gup, of course). But I agree that should be another bigger topic. I hope we don't need to pick that patch up someday by another dma report on read-only pinned pages... Regarding the solution here, I think we can also cover read-only fast-gup too in the future - IIUC what we need to do is to make it pte_protnone() instead of pte_wrprotect(), then in the fault handler we should identify this special pte_protnone() against numa balancing (change_prot_numa()). I think it should work fine too, iiuc, because I don't think we should migrate a page at all if it's pinned for any reason... So I think I'll focus on the wrprotect() solution for now. Thanks! [1] https://lore.kernel.org/lkml/20200915151746.GB2949@xz-x1/ -- Peter Xu