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=-6.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS 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 87C81C43461 for ; Tue, 15 Sep 2020 21:33:42 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F079D2078E for ; Tue, 15 Sep 2020 21:33:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="eDtfYi9g" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F079D2078E 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 23424900072; Tue, 15 Sep 2020 17:33:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E25C90006B; Tue, 15 Sep 2020 17:33:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AA33900072; Tue, 15 Sep 2020 17:33:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0057.hostedemail.com [216.40.44.57]) by kanga.kvack.org (Postfix) with ESMTP id E7B4E90006B for ; Tue, 15 Sep 2020 17:33:40 -0400 (EDT) Received: from smtpin18.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id AC2ED180AD81F for ; Tue, 15 Sep 2020 21:33:40 +0000 (UTC) X-FDA: 77266597800.18.face33_20014bc27114 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin18.hostedemail.com (Postfix) with ESMTP id 0CCAE100EC683 for ; Tue, 15 Sep 2020 21:33:40 +0000 (UTC) X-HE-Tag: face33_20014bc27114 X-Filterd-Recvd-Size: 9627 Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by imf08.hostedemail.com (Postfix) with ESMTP for ; Tue, 15 Sep 2020 21:33:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1600205618; 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=vUtjGsXiO4aMuU1NJWlF9eBDNUXAFp6DiLlJHWpBbCA=; b=eDtfYi9gtF0bs1K9lQMGEpRSyCWTYb0EknjYd/CcXlEo/yWwbh+a76aXk2QVrg9UWzkIPo copBdR7VbhkjZ/7QG8FSU/8/syQBteEyGxftNxxhpeGLWOpsBv3eARo9wGrwFq3VKyPM5C z4JcnkUrTFa537oaaHHa48XgUgHdHuA= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-102-QE4rpUC2M5CgikDmkOP6gQ-1; Tue, 15 Sep 2020 17:33:34 -0400 X-MC-Unique: QE4rpUC2M5CgikDmkOP6gQ-1 Received: by mail-qt1-f200.google.com with SMTP id f5so4040209qtk.11 for ; Tue, 15 Sep 2020 14:33:34 -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=vUtjGsXiO4aMuU1NJWlF9eBDNUXAFp6DiLlJHWpBbCA=; b=k6cm7YUVsSLU3pq3kxBsHUPQhan3FdXHbJrv0Ac75G2HqZyF+d2PX4hJKxZAr9rFkH Vly/H4dBN38TMIiOpUwDwR3LTbw/erGrP1AGtstpZz/X3zs90aU0IqRSAt13Z7rCDo2d LSNogR/dTSqigSdGpen0ePE++CxmfDkJCUzUyHkpBiOLDiBz3PqDPhy9s+PRvhI+KSoX 56rYESwSz5DELi+A/8VLIW+Qr7o8/RH9WtiiPpx81z2X3n6ItZMzxR4UgyQGITyvywH/ 9Sl+DouBVT1lfiYEwaBAniCUSfC6BCRikygtswb0XXCVweLIQfr05Uo63o4YCUAZPyqi VCwA== X-Gm-Message-State: AOAM531rxVGTE1l6jz5M/7naUZaSiTBvisuZP8iSgG8ZFmjyDJgGXnRL HKYAuPc6kLtIK4Xz82i9SNn7IsqU+WRtmYl/5kba9HBCpjnlTgkZkusU5JikDB4O5vW2ZsZZhnR yJaGUnK4mHXY= X-Received: by 2002:aed:3b95:: with SMTP id r21mr7869629qte.368.1600205613638; Tue, 15 Sep 2020 14:33:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy9uMbKvP58or9hYfkJing+c5CMvQJnLVzB+V6nbmgQvpvx49e/y4q2wMf8TGQvrL8iG1wbAg== X-Received: by 2002:aed:3b95:: with SMTP id r21mr7869602qte.368.1600205613303; Tue, 15 Sep 2020 14:33:33 -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 j6sm16985435qtn.97.2020.09.15.14.33.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Sep 2020 14:33:32 -0700 (PDT) Date: Tue, 15 Sep 2020 17:33:30 -0400 From: Peter Xu To: Jason Gunthorpe Cc: Linus Torvalds , Leon Romanovsky , Linux-MM , Linux Kernel Mailing List , "Maya B . Gokhale" , Yang Shi , Marty Mcfadden , Kirill Shutemov , Oleg Nesterov , Jann Horn , Jan Kara , Kirill Tkhai , Andrea Arcangeli , Christoph Hellwig , Andrew Morton Subject: Re: [PATCH 1/4] mm: Trial do_wp_page() simplification Message-ID: <20200915213330.GE2949@xz-x1> References: <20200914183436.GD30881@xz-x1> <20200914211515.GA5901@xz-x1> <20200914225542.GO904879@nvidia.com> <20200914232851.GH1221970@ziepe.ca> <20200915145040.GA2949@xz-x1> <20200915160553.GJ1221970@ziepe.ca> <20200915182933.GM1221970@ziepe.ca> <20200915191346.GD2949@xz-x1> <20200915193838.GN1221970@ziepe.ca> MIME-Version: 1.0 In-Reply-To: <20200915193838.GN1221970@ziepe.ca> Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=peterx@redhat.com X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Disposition: inline X-Rspamd-Queue-Id: 0CCAE100EC683 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam01 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 Tue, Sep 15, 2020 at 04:38:38PM -0300, Jason Gunthorpe wrote: > On Tue, Sep 15, 2020 at 03:13:46PM -0400, Peter Xu wrote: > > On Tue, Sep 15, 2020 at 03:29:33PM -0300, Jason Gunthorpe wrote: > > > On Tue, Sep 15, 2020 at 01:05:53PM -0300, Jason Gunthorpe wrote: > > > > On Tue, Sep 15, 2020 at 10:50:40AM -0400, Peter Xu wrote: > > > > > On Mon, Sep 14, 2020 at 08:28:51PM -0300, Jason Gunthorpe wrote: > > > > > > Yes, this stuff does pin_user_pages_fast() and MADV_DONTFORK > > > > > > together. It sets FOLL_FORCE and FOLL_WRITE to get an exclusive copy > > > > > > of the page and MADV_DONTFORK was needed to ensure that a future fork > > > > > > doesn't establish a COW that would break the DMA by moving the > > > > > > physical page over to the fork. DMA should stay with the process that > > > > > > called pin_user_pages_fast() (Is MADV_DONTFORK still needed with > > > > > > recent years work to GUP/etc? It is a pretty terrible ancient thing) > > > > > > > > > > ... Now I'm more confused on what has happened. > > > > > > > > I'm going to try to confirm that the MADV_DONTFORK is actually being > > > > done by userspace properly, more later. > > > > > > It turns out the test is broken and does not call MADV_DONTFORK when > > > doing forks - it is an opt-in it didn't do. > > > > > > It looks to me like this patch makes it much more likely that the COW > > > break after page pinning will end up moving the pinned physical page > > > to the fork while before it was not very common. Does that make sense? > > > > My understanding is that the fix should not matter much with current failing > > test case, as long as it's with FOLL_FORCE & FOLL_WRITE. However what I'm not > > sure is what if the RDMA/DMA buffers are designed for pure read from userspace. > > No, they are write. Always FOLL_WRITE. > > > E.g. for vfio I'm looking at vaddr_get_pfn() where I believe such pure read > > buffers will be a GUP with FOLL_PIN and !FOLL_WRITE which will finally pass to > > pin_user_pages_remote(). So what I'm worrying is something like this: > > I think the !(prot & IOMMU_WRITE) case is probably very rare for > VFIO. I'm also not sure it will work reliably, in RDMA we had this as > a more common case and long ago found bugs. The COW had to be broken > for the pin anyhow. If I'm not wrong.. QEMU/KVM (assuming there's one vIOMMU in the guest) will try to do VFIO maps in this read-only way if the IOVA mapped in the guest points to read only buffers (say, allocated with PCI_DMA_FROMDEVICE). > > > 1. Proc A gets a private anon page X for DMA, mapcount==refcount==1. > > > > 2. Proc A fork()s and gives birth to proc B, page X will now have > > mapcount==refcount==2, write-protected. proc B quits. Page X goes back > > to mapcount==refcount==1 (note! without WRITE bits set in the PTE). > > > 3. pin_user_pages(write=false) for page X. Since it's with !FORCE & !WRITE, > > no COW needed. Refcount==2 after that. > > > > 4. Pass these pages to device. We either setup IOMMU page table or just use > > the PFNs, which is not important imho - the most important thing is the > > device will DMA into page X no matter what. > > > > 5. Some thread of proc A writes to page X, trigger COW since it's > > write-protected with mapcount==1 && refcount==2. The HVA that pointing to > > page X will be changed to point to another page Y after the COW. > > > > 6. Device DMA happens, data resides on X. Proc A can never get the data, > > though, because it's looking at page Y now. > > RDMA doesn't ever use !WRITE > > I'm guessing #5 is the issue, just with a different ordering. If the > #3 pin_user_pages() preceeds the #2 fork, don't we get to the same #5? Right, but only if without MADV_DONTFORK? When without MADV_DONTFORK I'll probably still see that as an userspace bug instead of a kernel one when the userspace decided to fork() after step #3. > > > If this is a problem, we may still need the fix patch (maybe not as urgent as > > before at least). But I'd like to double confirm, just in case I miss some > > obvious facts above. > > I'm worred that the sudden need to have MAD_DONTFORK is going to be a > turn into a huge regression. It already blew up our first level of > synthetic test cases. I'm worried what we will see when the > application suite is run in a few months :\ For my own preference I'll consider changing kernel behavior if the impact is still under control (the performance report of 30%+ boost is also attractive after the simplify-cow patch). The other way is to maintain the old reuse logic forever, that'll be another kind of burden. Seems no easy way on either side... > > > > Given that the tests are wrong it seems like broken userspace, > > > however, it also worked reliably for a fairly long time. > > > > IMHO it worked because the page to do RDMA has mapcount==1, so it was reused > > previously just as-is even after the fork without MADV_DONTFORK and after the > > child quits. > > That would match the results we see.. So this patch changes things so > it is not re-used as-is, but replaced with Y? Yes. The patch lets "replaced with Y" (cow) happen earlier at step #3. Then with MADV_DONTFORK, reuse should not happen any more. Thanks, -- Peter Xu