From patchwork Fri Apr 14 13:02:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13211478 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D91DEC77B76 for ; Fri, 14 Apr 2023 13:04:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=a4AYw8QOLLD4dhUrLmIyKk6IEM161rMTZLOInlHNLW8=; b=nIzC1UAQ09EFkc OW8xnc0JdcwFVSq3zkIv7LupHA4SkS8zIQDIZDA5ZC4qcx4L5mLlawmB1RBkk7YCkXGjN8cZXkUBz Vn6jmgJY5MjCE7Wu3Sg/nBdqRokj+U/1hxf7CaZAiHaY154+kcqD+PH+Mzt9WHsrXhbn+lvTqN05n DkoHdh14gcFtabnjxS6F0JMI/DdANM3z9QPicwBzUj/324lR8PkRpMaBLIENRgnySNUzTECqSP9Aq YD1cWjeEWIurCLbOZShicjmqdd2S69mdq5KUyYVy1cTAbMfy+P7lhexovLTGex31p8vA8FSWR3DR3 zMyqrXvFQbVFI41/z92w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnJ5u-009biC-2n; Fri, 14 Apr 2023 13:04:02 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pnJ5R-009bSz-24 for linux-arm-kernel@lists.infradead.org; Fri, 14 Apr 2023 13:03:39 +0000 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 DE8F04B3; Fri, 14 Apr 2023 06:04:16 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 661D03F6C4; Fri, 14 Apr 2023 06:03:31 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , "Matthew Wilcox (Oracle)" , Yu Zhao , "Yin, Fengwei" Cc: Ryan Roberts , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org Subject: [RFC v2 PATCH 11/17] mm: Split __wp_page_copy_user() into 2 variants Date: Fri, 14 Apr 2023 14:02:57 +0100 Message-Id: <20230414130303.2345383-12-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230414130303.2345383-1-ryan.roberts@arm.com> References: <20230414130303.2345383-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230414_060333_799686_F26B835F X-CRM114-Status: GOOD ( 17.42 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org We will soon support CoWing large folios, so will need support for copying a contiguous range of pages in the case where there is a source folio. Therefore, split __wp_page_copy_user() into 2 variants: __wp_page_copy_user_pfn() copies a single pfn to a destination page. This is used when CoWing from a source without a folio, and is always only a single page copy. __wp_page_copy_user_range() copies a range of pages from source to destination and is used when the source has an underlying folio. For now it is only used to copy a single page, but this will change in a future commit. In both cases, kmsan_copy_page_meta() is moved into these helper functions so that the caller does not need to be concerned with calling it multiple times for the range case. No functional changes intended. Signed-off-by: Ryan Roberts --- mm/memory.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) -- 2.25.1 diff --git a/mm/memory.c b/mm/memory.c index 7e2af54fe2e0..f2b7cfb2efc0 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2786,14 +2786,34 @@ static inline int pte_unmap_same(struct vm_fault *vmf) return same; } +/* + * Return: + * 0: copied succeeded + * -EHWPOISON: copy failed due to hwpoison in source page + */ +static inline int __wp_page_copy_user_range(struct page *dst, struct page *src, + int nr, unsigned long addr, + struct vm_area_struct *vma) +{ + for (; nr != 0; nr--, dst++, src++, addr += PAGE_SIZE) { + if (copy_mc_user_highpage(dst, src, addr, vma)) { + memory_failure_queue(page_to_pfn(src), 0); + return -EHWPOISON; + } + kmsan_copy_page_meta(dst, src); + } + + return 0; +} + /* * Return: * 0: copied succeeded * -EHWPOISON: copy failed due to hwpoison in source page * -EAGAIN: copied failed (some other reason) */ -static inline int __wp_page_copy_user(struct page *dst, struct page *src, - struct vm_fault *vmf) +static inline int __wp_page_copy_user_pfn(struct page *dst, + struct vm_fault *vmf) { int ret; void *kaddr; @@ -2803,14 +2823,6 @@ static inline int __wp_page_copy_user(struct page *dst, struct page *src, struct mm_struct *mm = vma->vm_mm; unsigned long addr = vmf->address; - if (likely(src)) { - if (copy_mc_user_highpage(dst, src, addr, vma)) { - memory_failure_queue(page_to_pfn(src), 0); - return -EHWPOISON; - } - return 0; - } - /* * If the source page was a PFN mapping, we don't have * a "struct page" for it. We do a best-effort copy by @@ -2879,6 +2891,7 @@ static inline int __wp_page_copy_user(struct page *dst, struct page *src, } } + kmsan_copy_page_meta(dst, NULL); ret = 0; pte_unlock: @@ -3372,7 +3385,12 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) if (!new_folio) goto oom; - ret = __wp_page_copy_user(&new_folio->page, vmf->page, vmf); + if (likely(old_folio)) + ret = __wp_page_copy_user_range(&new_folio->page, + vmf->page, + 1, vmf->address, vma); + else + ret = __wp_page_copy_user_pfn(&new_folio->page, vmf); if (ret) { /* * COW failed, if the fault was solved by other, @@ -3388,7 +3406,6 @@ static vm_fault_t wp_page_copy(struct vm_fault *vmf) delayacct_wpcopy_end(); return ret == -EHWPOISON ? VM_FAULT_HWPOISON : 0; } - kmsan_copy_page_meta(&new_folio->page, vmf->page); } if (mem_cgroup_charge(new_folio, mm, GFP_KERNEL))