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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C046CC433F5 for ; Fri, 25 Feb 2022 08:01:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D85A8D0002; Fri, 25 Feb 2022 03:01:26 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 286D08D0001; Fri, 25 Feb 2022 03:01:26 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 176E08D0002; Fri, 25 Feb 2022 03:01:26 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.25]) by kanga.kvack.org (Postfix) with ESMTP id 09CAD8D0001 for ; Fri, 25 Feb 2022 03:01:26 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id C760E81847 for ; Fri, 25 Feb 2022 08:01:25 +0000 (UTC) X-FDA: 79180557330.07.AF2CD16 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf09.hostedemail.com (Postfix) with ESMTP id 34E0C14001F for ; Fri, 25 Feb 2022 08:01:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1645776083; 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=kxEkgFHWlRHgNokt14zVKg7qW6czfGlbSzwkwZw75A0=; b=FA53RrpZWJbuuaSndEfJUIrsgoQ6b9Jxdl08OxIHYCfDfpkD8/yRprEO1ZuCdbS1woPTIM diCkJ6vCz+7WPgsmPrbw2qSXIs1u2m1VtWJfP1XPx1ec52qvI67ihR41SvzTK37qj5ck/e KP/q7l6N2W+xEprPzezjiTWRpBKOK0M= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-428-GfoA4Zg9P12ofa8i7ccN4w-1; Fri, 25 Feb 2022 03:01:22 -0500 X-MC-Unique: GfoA4Zg9P12ofa8i7ccN4w-1 Received: by mail-wr1-f71.google.com with SMTP id v17-20020adfa1d1000000b001ed9d151569so572070wrv.21 for ; Fri, 25 Feb 2022 00:01:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent :content-language:to:cc:references:from:organization:subject :in-reply-to:content-transfer-encoding; bh=kxEkgFHWlRHgNokt14zVKg7qW6czfGlbSzwkwZw75A0=; b=Z0aTYBwvbz5qmzObc9WQU3rLL1gm43LoBAbzdPdnMTbisTfDZ4XveyXmppm70qHZ2S w6HoJnKOp/fEMUvIgzf002kGlz5xsx0icbG/bme9GiF/8qptfd5MZClHLg+mDC4cfkcO W/o0XPHP3ZhIkI3cqIV+4Q9IzCZwpqRdHxne33/HOR+AJRHshS3Hal+TqJbDrmPKuIol es2lEny65ZH318bpAsXF/y1ZHQk+AnkpEPSH7JIlGgCDCqPyvlr+W0Qk+HtuO572IJJM 67tWVDZCdzI+P6Kg66yCGRhyEbYFyrw9r94PED80RazxFUmDrGMkOm0tLCSPG46lSujj pjUg== X-Gm-Message-State: AOAM5312gk+A/9oRS6aiMuEPCEg/KmYZDhyzqzWERykv7rXfy2H6W0MX YuzkhrXBXAEjiVTsLHl9ViVwtbivStYeIZY05ugQXubT+tpZVLTIYjhMyuzQInAncfJUYjvPkq8 FQpzfWR2r+dQ= X-Received: by 2002:a1c:7518:0:b0:37c:7eb:f255 with SMTP id o24-20020a1c7518000000b0037c07ebf255mr1577999wmc.29.1645776080818; Fri, 25 Feb 2022 00:01:20 -0800 (PST) X-Google-Smtp-Source: ABdhPJyN87poyQBw/1i3JAFgNDtFHBe+rKf4EKP/70Us8z5RNeOxRIiN3s9a/T3STfYSEMuqEoSweA== X-Received: by 2002:a1c:7518:0:b0:37c:7eb:f255 with SMTP id o24-20020a1c7518000000b0037c07ebf255mr1577979wmc.29.1645776080505; Fri, 25 Feb 2022 00:01:20 -0800 (PST) Received: from ?IPV6:2003:cb:c706:1900:f2f7:d2ad:80d9:218f? (p200300cbc7061900f2f7d2ad80d9218f.dip0.t-ipconnect.de. [2003:cb:c706:1900:f2f7:d2ad:80d9:218f]) by smtp.gmail.com with ESMTPSA id k4-20020adfe8c4000000b001e68c92af35sm1523883wrn.30.2022.02.25.00.01.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 25 Feb 2022 00:01:20 -0800 (PST) Message-ID: <2df1e602-4182-b1e9-62f1-a8e29153d8d1@redhat.com> Date: Fri, 25 Feb 2022 09:01:19 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 To: Hillf Danton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Linus Torvalds , John Hubbard , Oded Gabbay References: <20220224122614.94921-1-david@redhat.com> <20220225051511.2383-1-hdanton@sina.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH RFC 03/13] mm/memory: slightly simplify copy_present_pte() In-Reply-To: <20220225051511.2383-1-hdanton@sina.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FA53RrpZ; spf=none (imf09.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 34E0C14001F X-Stat-Signature: msdp5wkp5e7s6q1p5tu7o87bfsx7bn1t X-HE-Tag: 1645776084-925904 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 25.02.22 06:15, Hillf Danton wrote: > On Thu, 24 Feb 2022 13:26:04 +0100 David Hildenbrand wrote: >> Let's move the pinning check into the caller, to simplify return code >> logic and prepare for further changes: relocating the >> page_needs_cow_for_dma() into rmap handling code. >> >> While at it, remove the unused pte parameter and simplify the comments a >> bit. >> >> No functional change intended. >> >> Signed-off-by: David Hildenbrand >> --- >> mm/memory.c | 53 ++++++++++++++++------------------------------------- >> 1 file changed, 16 insertions(+), 37 deletions(-) >> >> diff --git a/mm/memory.c b/mm/memory.c >> index c6177d897964..accb72a3343d 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -865,19 +865,11 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, >> } >> >> /* >> - * Copy a present and normal page if necessary. >> + * Copy a present and normal page. >> * >> - * NOTE! The usual case is that this doesn't need to do >> - * anything, and can just return a positive value. That >> - * will let the caller know that it can just increase >> - * the page refcount and re-use the pte the traditional >> - * way. >> - * >> - * But _if_ we need to copy it because it needs to be >> - * pinned in the parent (and the child should get its own >> - * copy rather than just a reference to the same page), >> - * we'll do that here and return zero to let the caller >> - * know we're done. >> + * NOTE! The usual case is that this isn't required; >> + * instead, the caller can just increase the page refcount >> + * and re-use the pte the traditional way. >> * >> * And if we need a pre-allocated page but don't yet have >> * one, return a negative error to let the preallocation >> @@ -887,25 +879,10 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, >> static inline int >> copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, >> pte_t *dst_pte, pte_t *src_pte, unsigned long addr, int *rss, >> - struct page **prealloc, pte_t pte, struct page *page) >> + struct page **prealloc, struct page *page) >> { >> struct page *new_page; >> - >> - /* >> - * What we want to do is to check whether this page may >> - * have been pinned by the parent process. If so, >> - * instead of wrprotect the pte on both sides, we copy >> - * the page immediately so that we'll always guarantee >> - * the pinned page won't be randomly replaced in the >> - * future. >> - * >> - * The page pinning checks are just "has this mm ever >> - * seen pinning", along with the (inexact) check of >> - * the page count. That might give false positives for >> - * for pinning, but it will work correctly. >> - */ >> - if (likely(!page_needs_cow_for_dma(src_vma, page))) >> - return 1; >> + pte_t pte; >> >> new_page = *prealloc; >> if (!new_page) >> @@ -947,14 +924,16 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, >> struct page *page; >> >> page = vm_normal_page(src_vma, addr, pte); >> - if (page) { >> - int retval; >> - >> - retval = copy_present_page(dst_vma, src_vma, dst_pte, src_pte, >> - addr, rss, prealloc, pte, page); >> - if (retval <= 0) >> - return retval; >> - >> + if (page && unlikely(page_needs_cow_for_dma(src_vma, page))) { >> + /* >> + * If this page may have been pinned by the parent process, >> + * copy the page immediately for the child so that we'll always >> + * guarantee the pinned page won't be randomly replaced in the >> + * future. >> + */ >> + return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, >> + addr, rss, prealloc, page); > > Off topic question, is it likely that the DMA tranfer from device in parallel > to copying page ends up with different data between parent and kid? If the parent has a GUP pin on the page before fork(), the parent will keep that page mapped exclusively and the child will receive a copy. It's pretty much undefined which content that copy will have if there is concurrent DMA via that GUP pin: we'll snapshot that page at some point in time. It's fully under the parent process control when to start/stop I/O via a GUP pin and when to call fork(). So yes, if there is fork() with concurrent DMA via a GUP pin modifying the page, the page content isn't well defined: could be the content before DMA, mid DMA or post DMA. -- Thanks, David / dhildenb