From patchwork Wed Feb 3 21:08:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 12065377 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=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT 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 CEC77C433E9 for ; Wed, 3 Feb 2021 21:08:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5B42E64F92 for ; Wed, 3 Feb 2021 21:08:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5B42E64F92 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 D210E6B006E; Wed, 3 Feb 2021 16:08:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CCAF36B0070; Wed, 3 Feb 2021 16:08:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B23446B0071; Wed, 3 Feb 2021 16:08:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0103.hostedemail.com [216.40.44.103]) by kanga.kvack.org (Postfix) with ESMTP id 947236B006E for ; Wed, 3 Feb 2021 16:08:44 -0500 (EST) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 5D8598249980 for ; Wed, 3 Feb 2021 21:08:44 +0000 (UTC) X-FDA: 77778195768.01.verse12_190f8ea275d6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin01.hostedemail.com (Postfix) with ESMTP id 36D6610046469 for ; Wed, 3 Feb 2021 21:08:44 +0000 (UTC) X-HE-Tag: verse12_190f8ea275d6 X-Filterd-Recvd-Size: 8338 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Feb 2021 21:08:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1612386522; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Re19mNdJTnr2eyYv/JALCHR5raIIkk7skM0STXXGd4k=; b=Tq4TIV/RMG0tEsNDmt/BFFNFbqf+0B+944kupEaRaResYHYrONNMkpVheP2uA+I+YuVpNJ cpyAaoxVE+vGrrNRr5wPJDw9oSC8soejvJrfaUbX9mrnAH498OgCzKnYMMQLqJwHSuJokk eyH0xqzqQ9atwAW+4cj/xrhyo3Q14Nc= Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-597-CWz156oqOQSprPYrzayvYQ-1; Wed, 03 Feb 2021 16:08:41 -0500 X-MC-Unique: CWz156oqOQSprPYrzayvYQ-1 Received: by mail-qk1-f200.google.com with SMTP id i11so654488qkn.21 for ; Wed, 03 Feb 2021 13:08:41 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Re19mNdJTnr2eyYv/JALCHR5raIIkk7skM0STXXGd4k=; b=N2JmpRHXVVHoLUMHaSLoQE3M9XD7uAhQ81KrMXT1yBHiQDumIkxH38tpFtORkRaymT XqoXzZAip0Oj0CO2K8yhVGhT9mREQmLlcgsIRr/7n4/6+cvyuoTESq8tqD/8kQIakvOc NCTscUvVquoCDtZfYdIHxw5hmsdqsgksZ3U17xpYSqAxzKbuwQn7kdZSBMgOfjhnPMTd sKjT/cJYt7c3N29FgM5+T1u3xpeJTRJdGyXFbhed4IpanocRqgkEVZpa3PIgQOGyzJLM J0/IC5qphLkV1QKKZ8HeISL3t7vytlGjMwjz273mXh1PqHsmNS21ix0Gsrhr0wtQo1f2 OJ9w== X-Gm-Message-State: AOAM532+aiQmldXc8wUZxQ1uAZsBa4OFpzkSQX4C7aLIwfm9tF71tW90 DjcXzo6cDCkFlCXBE30Infv32U9CyXxxf+IVtkE43nYiiX+6jhaqrGwTNRDiryEFByeHKvjCsDM 2lYG78Bv38kY= X-Received: by 2002:ac8:7757:: with SMTP id g23mr4249253qtu.305.1612386520835; Wed, 03 Feb 2021 13:08:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJw38komgAUUDsxRCb/zg9p3zM2x1SPpALss4dFzMM+hvkykqlrxEZLE1vHGsdJt5hpRlnezlw== X-Received: by 2002:ac8:7757:: with SMTP id g23mr4249234qtu.305.1612386520623; Wed, 03 Feb 2021 13:08:40 -0800 (PST) Received: from xz-x1.redhat.com (bras-vprn-toroon474qw-lp130-20-174-93-89-182.dsl.bell.ca. [174.93.89.182]) by smtp.gmail.com with ESMTPSA id t6sm2507659qti.2.2021.02.03.13.08.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 13:08:39 -0800 (PST) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Wei Zhang , Matthew Wilcox , Linus Torvalds , Jason Gunthorpe , Gal Pressman , peterx@redhat.com, Christoph Hellwig , Andrea Arcangeli , Jan Kara , Kirill Shutemov , David Gibson , Mike Rapoport , Mike Kravetz , Kirill Tkhai , Jann Horn , Andrew Morton Subject: [PATCH 3/4] mm: Introduce page_needs_cow_for_dma() for deciding whether cow Date: Wed, 3 Feb 2021 16:08:31 -0500 Message-Id: <20210203210832.113685-4-peterx@redhat.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210203210832.113685-1-peterx@redhat.com> References: <20210203210832.113685-1-peterx@redhat.com> MIME-Version: 1.0 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 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: We've got quite a few places (pte, pmd, pud) that explicitly checked against whether we should break the cow right now during fork(). It's easier to provide a helper, especially before we work the same thing on hugetlbfs. Since we'll reference is_cow_mapping() in mm.h, move it there too. Actually it suites mm.h more since internal.h is mm/ only, but mm.h is exported to the whole kernel. With that we should expect another patch to use is_cow_mapping() whenever we can across the kernel since we do use it quite a lot but it's always done with raw code against VM_* flags. Signed-off-by: Peter Xu --- include/linux/mm.h | 21 +++++++++++++++++++++ mm/huge_memory.c | 8 ++------ mm/internal.h | 5 ----- mm/memory.c | 7 +------ 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index ecdf8a8cd6ae..6ea20721d349 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1291,6 +1291,27 @@ static inline bool page_maybe_dma_pinned(struct page *page) GUP_PIN_COUNTING_BIAS; } +static inline bool is_cow_mapping(vm_flags_t flags) +{ + return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; +} + +/* + * This should most likely only be called during fork() to see whether we + * should break the cow immediately for a page on the src mm. + */ +static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma, + struct page *page) +{ + if (!is_cow_mapping(vma->vm_flags)) + return false; + + if (!atomic_read(&vma->vm_mm->has_pinned)) + return false; + + return page_maybe_dma_pinned(page); +} + #if defined(CONFIG_SPARSEMEM) && !defined(CONFIG_SPARSEMEM_VMEMMAP) #define SECTION_IN_PAGE_FLAGS #endif diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 9237976abe72..dbff6c7eda67 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1095,9 +1095,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, * best effort that the pinned pages won't be replaced by another * random page during the coming copy-on-write. */ - if (unlikely(is_cow_mapping(vma->vm_flags) && - atomic_read(&src_mm->has_pinned) && - page_maybe_dma_pinned(src_page))) { + if (unlikely(page_needs_cow_for_dma(vma, src_page))) { pte_free(dst_mm, pgtable); spin_unlock(src_ptl); spin_unlock(dst_ptl); @@ -1209,9 +1207,7 @@ int copy_huge_pud(struct mm_struct *dst_mm, struct mm_struct *src_mm, } /* Please refer to comments in copy_huge_pmd() */ - if (unlikely(is_cow_mapping(vma->vm_flags) && - atomic_read(&src_mm->has_pinned) && - page_maybe_dma_pinned(pud_page(pud)))) { + if (unlikely(page_needs_cow_for_dma(vma, pud_page(pud)))) { spin_unlock(src_ptl); spin_unlock(dst_ptl); __split_huge_pud(vma, src_pud, addr); diff --git a/mm/internal.h b/mm/internal.h index 25d2b2439f19..24eec93d0dac 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -296,11 +296,6 @@ static inline unsigned int buddy_order(struct page *page) */ #define buddy_order_unsafe(page) READ_ONCE(page_private(page)) -static inline bool is_cow_mapping(vm_flags_t flags) -{ - return (flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE; -} - /* * These three helpers classifies VMAs for virtual memory accounting. */ diff --git a/mm/memory.c b/mm/memory.c index feff48e1465a..b2849e1d4aab 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -800,9 +800,6 @@ copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma struct mm_struct *src_mm = src_vma->vm_mm; struct page *new_page; - if (!is_cow_mapping(src_vma->vm_flags)) - return 1; - /* * What we want to do is to check whether this page may * have been pinned by the parent process. If so, @@ -816,9 +813,7 @@ copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma * the page count. That might give false positives for * for pinning, but it will work correctly. */ - if (likely(!atomic_read(&src_mm->has_pinned))) - return 1; - if (likely(!page_maybe_dma_pinned(page))) + if (likely(!page_needs_cow_for_dma(src_vma, page))) return 1; new_page = *prealloc;