Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp384673rdb; Tue, 23 Jan 2024 02:46:50 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbl+uYFEPeBOhy1hS7oXuoXu0DyJVgtyPHKFJGR9x/GInm8pGeS7tqGO0uzeaW0m3gRvJj X-Received: by 2002:a0d:c183:0:b0:5ff:5d20:ed7e with SMTP id c125-20020a0dc183000000b005ff5d20ed7emr4355060ywd.79.1706006809939; Tue, 23 Jan 2024 02:46:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706006809; cv=pass; d=google.com; s=arc-20160816; b=ujuGB8qPmubtqzJNiztC60Di/GHLtw2b1DbR+Szl0iiYIH6BNc6uEOJ1PiRx2+04r4 1F2a2LM0hIM6cDRAozruQYSgnW8neILFsiC2aHVnzoFNAhR6LCmPW0H0HMNGrnQ7SszG aRXHKQe8EKBiFgUIKAinMWAlLeF7esxtKcAq7T62piVyqf0BoMbtTNF8q5bLoZxg8H6s cFSt1N8yqB9UOuYnGu6yPX34pFnBAAg58R/bWyWJdyxHcDMLWEcTStSFfAjm022Gtioh r4dUXXItf6802W2zn6g2Ou5LyyOxieOIXyJkJ+yCf1R6bulLYL8KqNdzhbYaeySABJYa rYIQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=ACaNjszqqG73K8wZ4v+Oa+8vkOTvSUGbh0yQ1lFJByk=; fh=cLjkAanmB8aobrGIK/HTftjty0FQAaQ+aRvIv8PHcYU=; b=oqReLo22mx2yaSz9zyhRnxT44qwNEfgSbee2FOZOG04Zwvf3bbaO7J2uwArUHP6mdn ZjlfeP226JiteUOzGCLga8tt7ViueWpdmK5Ohig8ygCJKKjZfHmEMQ6u6/L/+EBsl/sO fo4vn+qEmu8t9KCZtoHgPBo8Cn3G83nW9hg+tBYoLEO+wr+U1qtYrOUZAG/EZjXSAN95 RgPB7HIDd1jIVyLcwe6cIZBzC9+D8I1hSLYK2L1mVAFbuUDp7tGBOV8BHjjP3VuWa8kz RJ/LTetvqeks6A2QYVR2aIz4WUunPAmdrSK977WozFZpXY/CcN2Xy63qpzGlod/w37zl JOKg== ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-35119-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35119-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id p15-20020a0cf54f000000b006819bda2532si7624040qvm.234.2024.01.23.02.46.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 02:46:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35119-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-35119-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35119-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id AD4EE1C22C58 for ; Tue, 23 Jan 2024 10:46:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1FC3A5D8F4; Tue, 23 Jan 2024 10:45:27 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7E6075D725; Tue, 23 Jan 2024 10:45:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706006726; cv=none; b=OOEhna3Th67aKTWyX++KE9gE26W8wdUNjEGVzf178DOASYRsDCSS4KAeNzQ6eBdyKmzXrKGvS+GbKvenRZgo9RSEpPqokfzWG3GDpAkBAfcVndYKUMZn6OFN4tF9WhYnE/oPR/boHXEaGTFXqt3E+qaLY6K+rIkCAW99RNuq/gQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706006726; c=relaxed/simple; bh=Bv9tpEfskUzoIjJvF99P42BdqqUjJxhrXzxON1qU9Y0=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=iZK2JyGIIpnRmfrwT66PCzyCUb60d2s8X3gbMFQueN9FvC89Kldl3nnyEvBwdESPZmmLBy4BIBZb/qy7SMIm1bAz0IZEMU66Ds1dvel/vDD1/I0eXuPhjnvB+ZxnRfjQ6U1Bi/MKCMX7zlxOYf+hzUNsKCG6vV+jt3QE3o9wjuE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 689251FB; Tue, 23 Jan 2024 02:46:08 -0800 (PST) Received: from [10.57.77.165] (unknown [10.57.77.165]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 4FFE73F5A1; Tue, 23 Jan 2024 02:45:19 -0800 (PST) Message-ID: <3d6a5076-48d3-4790-a507-d3b1584e8587@arm.com> Date: Tue, 23 Jan 2024 10:45:17 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v1 07/11] mm/memory: factor out copying the actual PTE in copy_present_pte() Content-Language: en-GB To: David Hildenbrand , linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, Andrew Morton , Matthew Wilcox , Russell King , Catalin Marinas , Will Deacon , Dinh Nguyen , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Gordeev , Gerald Schaefer , Heiko Carstens , Vasily Gorbik , Christian Borntraeger , Sven Schnelle , "David S. Miller" , linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org References: <20240122194200.381241-1-david@redhat.com> <20240122194200.381241-8-david@redhat.com> From: Ryan Roberts In-Reply-To: <20240122194200.381241-8-david@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 22/01/2024 19:41, David Hildenbrand wrote: > Let's prepare for further changes. > > Signed-off-by: David Hildenbrand Reviewed-by: Ryan Roberts > --- > mm/memory.c | 60 ++++++++++++++++++++++++++++------------------------- > 1 file changed, 32 insertions(+), 28 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index 7e1f4849463aa..2aa2051ee51d3 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -930,6 +930,29 @@ copy_present_page(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma > return 0; > } > > +static inline void __copy_present_pte(struct vm_area_struct *dst_vma, > + struct vm_area_struct *src_vma, pte_t *dst_pte, pte_t *src_pte, > + pte_t pte, unsigned long addr) > +{ > + struct mm_struct *src_mm = src_vma->vm_mm; > + > + /* If it's a COW mapping, write protect it both processes. */ > + if (is_cow_mapping(src_vma->vm_flags) && pte_write(pte)) { > + ptep_set_wrprotect(src_mm, addr, src_pte); > + pte = pte_wrprotect(pte); > + } > + > + /* If it's a shared mapping, mark it clean in the child. */ > + if (src_vma->vm_flags & VM_SHARED) > + pte = pte_mkclean(pte); > + pte = pte_mkold(pte); > + > + if (!userfaultfd_wp(dst_vma)) > + pte = pte_clear_uffd_wp(pte); > + > + set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); > +} > + > /* > * Copy one pte. Returns 0 if succeeded, or -EAGAIN if one preallocated page > * is required to copy this pte. > @@ -939,16 +962,16 @@ copy_present_pte(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 folio **prealloc) > { > - struct mm_struct *src_mm = src_vma->vm_mm; > - unsigned long vm_flags = src_vma->vm_flags; > pte_t pte = ptep_get(src_pte); > struct page *page; > struct folio *folio; > > page = vm_normal_page(src_vma, addr, pte); > - if (page) > - folio = page_folio(page); > - if (page && folio_test_anon(folio)) { > + if (unlikely(!page)) > + goto copy_pte; > + > + folio = page_folio(page); > + if (folio_test_anon(folio)) { > /* > * If this page may have been pinned by the parent process, > * copy the page immediately for the child so that we'll always > @@ -963,34 +986,15 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma, > addr, rss, prealloc, page); > } > rss[MM_ANONPAGES]++; > - } else if (page) { > + VM_WARN_ON_FOLIO(PageAnonExclusive(page), folio); > + } else { > folio_get(folio); > folio_dup_file_rmap_pte(folio, page); > rss[mm_counter_file(page)]++; > } > > - /* > - * If it's a COW mapping, write protect it both > - * in the parent and the child > - */ > - if (is_cow_mapping(vm_flags) && pte_write(pte)) { > - ptep_set_wrprotect(src_mm, addr, src_pte); > - pte = pte_wrprotect(pte); > - } > - VM_BUG_ON(page && folio_test_anon(folio) && PageAnonExclusive(page)); > - > - /* > - * If it's a shared mapping, mark it clean in > - * the child > - */ > - if (vm_flags & VM_SHARED) > - pte = pte_mkclean(pte); > - pte = pte_mkold(pte); > - > - if (!userfaultfd_wp(dst_vma)) > - pte = pte_clear_uffd_wp(pte); > - > - set_pte_at(dst_vma->vm_mm, addr, dst_pte, pte); > +copy_pte: > + __copy_present_pte(dst_vma, src_vma, dst_pte, src_pte, pte, addr); > return 0; > } >