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,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, 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 C6340C43461 for ; Wed, 16 Sep 2020 17:48:09 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 120A1206DC for ; Wed, 16 Sep 2020 17:48:08 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="muOiPYDc" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 120A1206DC Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 34C276B005A; Wed, 16 Sep 2020 13:48:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D5DD6B005C; Wed, 16 Sep 2020 13:48:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 19EF26B005D; Wed, 16 Sep 2020 13:48:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0112.hostedemail.com [216.40.44.112]) by kanga.kvack.org (Postfix) with ESMTP id F311C6B005A for ; Wed, 16 Sep 2020 13:48:07 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id B1A938249980 for ; Wed, 16 Sep 2020 17:48:07 +0000 (UTC) X-FDA: 77269658214.20.shoe77_3f152582711b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin20.hostedemail.com (Postfix) with ESMTP id 82794180C07A3 for ; Wed, 16 Sep 2020 17:48:07 +0000 (UTC) X-HE-Tag: shoe77_3f152582711b X-Filterd-Recvd-Size: 6582 Received: from mail-qv1-f66.google.com (mail-qv1-f66.google.com [209.85.219.66]) by imf48.hostedemail.com (Postfix) with ESMTP for ; Wed, 16 Sep 2020 17:48:06 +0000 (UTC) Received: by mail-qv1-f66.google.com with SMTP id db4so3993253qvb.4 for ; Wed, 16 Sep 2020 10:48:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=+L39r6ejDyySARnpWF+ib/JBQg6eJuBB3G7VcL1C+mo=; b=muOiPYDceaxT7/vx4pl1sTEW1BluFOl76J4QSzOzK+JCRCNmEV2tX/Jiex+0zzFt+a X6vT6P7we60Ly/yxCXCRNUHX71sSPdfQRMAr7aKo0oTn8GzkpR5gprF2VmK4mx0V/oL3 l21Yejte/8+5kAtuRZbvuOQf1mR/FiTFEorB+xh/ev+rXB4/D0QjhpLxbv4qvSgjuAnh Bs1pQGPbfi+8+P6caNvxWrwYlca6+6jDs9Par1gScZeu9sd00RLJiuPJ8FvWsZrVqih2 T7rdmPwefAn3V7AX1jh2YBnKtammzvKn+l7JcCeZdSkRONzApN0KD4rx7gmfxJ7BwV5y Ynrw== 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=+L39r6ejDyySARnpWF+ib/JBQg6eJuBB3G7VcL1C+mo=; b=kdYkWLv17RGjV2JdY+vh3B9wtC2Lol/nmbGJMb47phF65TX6HQdVlzYTyutJIO7Sli D7A+wTjKMb87eHEd0e+fYtO0M2ZEwMzGjUzNtaDzgU96uTZOaXo634AA1E/ygHQxmsOZ pVTFSwU3KLs9UaOSFknWKyq4bMwHxVOIoaDLzj0q2TOJbuxUT+z3/YCUcxxeD5daqMq6 6FSrPT9nT23kZQeoNBwV8VL0bpQ3QrdaC/zu0VUxmPUFmPkH1SnluU/T2LQsvmOr/1Zk PbJTfuMRY89d8J6Td4Z4Ocu3kGYXt5ROgxeYrmdp4jQAClcYmBCOZ1csjLkz4TF1/H/6 DsYg== X-Gm-Message-State: AOAM531N2AJ1gAd8P8DKZCqB097W/kInqCOszf5WRUWWV2SdkvmrV0g7 slyaVtwVm8X+8Ze8DHZ+7YpDNA== X-Google-Smtp-Source: ABdhPJy4BGwzITw0CZlaZiow9SX1MOVFbkSfKL6nYwhmUtNfg9HcPuRPb8UJzZN4QIFQfwvSMNHMvw== X-Received: by 2002:a0c:b396:: with SMTP id t22mr11497898qve.13.1600278486067; Wed, 16 Sep 2020 10:48:06 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-156-34-48-30.dhcp-dynamic.fibreop.ns.bellaliant.net. [156.34.48.30]) by smtp.gmail.com with ESMTPSA id a52sm20058340qtc.22.2020.09.16.10.48.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Sep 2020 10:48:05 -0700 (PDT) Received: from jgg by mlx with local (Exim 4.94) (envelope-from ) id 1kIbXI-000An2-D4; Wed, 16 Sep 2020 14:48:04 -0300 Date: Wed, 16 Sep 2020 14:48:04 -0300 From: Jason Gunthorpe To: John Hubbard Cc: Peter Xu , 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: <20200916174804.GC8409@ziepe.ca> References: <20200914232851.GH1221970@ziepe.ca> <20200915145040.GA2949@xz-x1> <20200915160553.GJ1221970@ziepe.ca> <20200915182933.GM1221970@ziepe.ca> <20200915191346.GD2949@xz-x1> <20200915193838.GN1221970@ziepe.ca> <20200915213330.GE2949@xz-x1> <20200915232238.GO1221970@ziepe.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 82794180C07A3 X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam04 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 06:50:46PM -0700, John Hubbard wrote: > > > > It seems very strange that a physical page exclusively owned by a > > process can become copied if pin_user_pages() is active and the > > process did fork() at some point. > > > > Could the new pin_user_pages() logic help here? eg the > > GUP_PIN_COUNTING_BIAS stuff? > > > > Could the COW code consider a refcount of GUP_PIN_COUNTING_BIAS + 1 as > > being owned by the current mm and not needing COW? The DMA pin would > > be 'invisible' for COW purposes? > > > Please do be careful to use the API, rather than the implementation. The > FOLL_PIN refcounting system results in being able to get a "maybe > DMA-pinned", or a "definitely not DMA-pinned", via this API call: So, what I'm thinking is something like (untested): diff --git a/mm/memory.c b/mm/memory.c index 469af373ae76e1..77f63183667e52 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2889,6 +2889,26 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf) return ret; } +static bool cow_needed(struct vm_fault *vmf) +{ + int total_map_swapcount; + + if (!reuse_swap_page(vmf->page, &total_map_swapcount)) { + unlock_page(vmf->page); + return true; + } + + if (total_map_swapcount == 1) { + /* + * The page is all ours. Move it to our anon_vma so the rmap + * code will not search our parent or siblings. Protected + * against the rmap code by the page lock. + */ + page_move_anon_rmap(vmf->page, vmf->vma); + } + return false; +} + /* * This routine handles present pages, when users try to write * to a shared page. It is done by copying the page to a new address @@ -2947,8 +2967,21 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) if (!trylock_page(page)) goto copy; if (PageKsm(page) || page_mapcount(page) != 1 || page_count(page) != 1) { + bool cow = true; + + /* + * If the page is DMA pinned we can't rely on the + * above to know if there are other CPU references as + * page_count() will be elevated by the + * pin. Needlessly copying the page will cause the DMA + * pin to break, try harder to avoid that. + */ + if (page_maybe_dma_pinned(page)) + cow = cow_needed(vmf); + unlock_page(page); - goto copy; + if (cow) + goto copy; } /* * Ok, we've got the only map reference, and the only What do you think Peter? Is this remotely close? Seems like it preserves the fast path in most cases, the page_count & page_maybe_dma_pinned could be further optimized down to one atomic in non huge page cases. Jason