From patchwork Fri Apr 14 13:02:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13211461 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 A05F8C77B6E for ; Fri, 14 Apr 2023 13:03:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D146900005; Fri, 14 Apr 2023 09:03:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BBF3900002; Fri, 14 Apr 2023 09:03:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E821900005; Fri, 14 Apr 2023 09:03:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 5C670900002 for ; Fri, 14 Apr 2023 09:03:32 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 2D160AAF52 for ; Fri, 14 Apr 2023 13:03:32 +0000 (UTC) X-FDA: 80680013064.15.8882DE6 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf21.hostedemail.com (Postfix) with ESMTP id 5BE001C0011 for ; Fri, 14 Apr 2023 13:03:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681477410; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gVuGG+EGC3oQJFIhWJXlenajYxJw7IAheJ47HnxAHkY=; b=h7Lt8nAaQs4NEE/IyYALi+9NIuq/jJuFtq+M6HzxI+0oI5PAY2TUQ+ss/KhP0ldChjAIrO HKK7mAC81X48K0NCv5vgfmW3RmiO0C6kUhF1brnNokGbmO79tHmHqg6zrBlEB9nobsDFmg YViC0u3oOhM9vlphi3HWQdoLobRvYQQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681477410; a=rsa-sha256; cv=none; b=EWRwJfuDyt6qTo/p43p2PhIV0eJCq+sZNiLkZUwvWpRYNvCUoZzh7ykMQXx+oyG6Uof7hj WuG5uLX5Xw6mG3rKqkOCeCv4hMc3tjYwo5oRMdoK4mJbU1Koh918BWpleRlNAdtWpU7rJo Eo2hHnjUAfAzBzoJIcJ33oRnpem/9oY= 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 7A1EA1763; Fri, 14 Apr 2023 06:04:14 -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 0171B3F6C4; Fri, 14 Apr 2023 06:03:28 -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 09/17] mm: Update wp_page_reuse() to operate on range of pages Date: Fri, 14 Apr 2023 14:02:55 +0100 Message-Id: <20230414130303.2345383-10-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-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: hpcg498asd55ryupuctcb5dxetidyi6s X-Rspamd-Queue-Id: 5BE001C0011 X-HE-Tag: 1681477410-593355 X-HE-Meta: U2FsdGVkX1+Mj/YRpn3Ce4ivCDxfI/oZDRke6YLxESCaOFzmagQ5cyGmTBMfIwmIKkws5f55ov7KcBRJjftkFZ1/Ap/73+ZnCMXOwVcUv+ijxxHE4FwBm/O3MOA/opNCGrqt+0f8Vzmo2eIGFzuQSqCguLej6/kjVQmQomksMWKF2XIhEcpOyrVq2LSdAw42V0Ft0qYEHRq1GG+RrUyfzeBlRWkhPtRVkures9YKkOf5F2Fm/qFoZ9tsWWIDPwiODhab19vuuteBsl7z8O4qvCYJHuISkf4AdI2ly3OUcz9gQH2zinxBjCktzvS1b9N3530fbipd6bHtK6TY2cTQBu1b0tUxRAtZoDiqrW1hW2i9whxl8feK3uu0+nOxifImpGc6ifVESOm5wL+kTajJtLBsUMN/EafnrePuJCarOUEmqjlol1La3eAq3gfdh0G2Wfq5uahW7xv3W1VnvtU8mnCPRawYAVd+iAQWimUouY3aTmlT7y3LMxAolDiMXQnoBOu1YFRBJDC3t9zgyRYTCc5gUr2ge7IHiN2af+LkWyKXKLfUwXACqF223CtdcO7x4x7oxsITHdgKK75mMWuAthYIEq+vSP3DWRTGDGKSr+P8agyi5mdUSjjBvyjEu6ez/8HOKJbROXuUdW9KPxb5l0mUH7Okdg6M3rMg1w/RXwa4z/K7eEZFr8ngbUy4hIZaw9JbQxcKdaysdLAswk1Tj7mWbUvL4rbLGub0EKt/mnVtoWQtrVUuIp0vzwAFOvPdsBtOvuyD41C73cOnosgocM2w/RzMf1Kd/J0kaCR9sZV1IdV4/acCuhK7W90/fv6EFlsGaJuMZkgIWi6hTgf8UBABkm+QC9lfbxYcJ3fvO7GJ+EX4L9EThYMd0NzHG0TRq+2oQ9L8XikNxCGz5J4MDV1bSW7bTh8VLsCW/aZ7naioL4w7SthEjCGZlLS4MN8ghQl2JvbhF0208ZqcG5k JPamRXGo +P2Rub/hAjOd/QU8+KKMmvGhxEldBLT9s88afydtENAh5a73xE3KoDY+2JOg0k4eVFn74vV4MNVsy7dQLf/UXdzWLAn8eDCF5C27P7EL/wpH0ggBPQSyDlU//2rr3HxCpjdcYNxBXhXY0w2T564RZm9+LhP0IaljBdbkm+AooE82QUeEnhJ+bSGjrMdSX0pKVrp0OXsv4YMpmlL0xhWSlBiPj1q/UBikZbeJM 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 will shortly be updating do_wp_page() to be able to reuse a range of pages from a large anon folio. As an enabling step, modify wp_page_reuse() to operate on a range of pages, if a struct anon_folio_range is passed in. Batching in this way allows us to batch up the cache maintenance and event counting for small performance improvements. Currently all callsites pass range=NULL, so no functional changes intended. Signed-off-by: Ryan Roberts --- mm/memory.c | 80 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 60 insertions(+), 20 deletions(-) -- 2.25.1 diff --git a/mm/memory.c b/mm/memory.c index f92a28064596..83835ff5a818 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3030,6 +3030,14 @@ static inline int max_anon_folio_order(struct vm_area_struct *vma) return ANON_FOLIO_ORDER_MAX; } +struct anon_folio_range { + unsigned long va_start; + pte_t *pte_start; + struct page *pg_start; + int nr; + bool exclusive; +}; + /* * Returns index of first pte that is not none, or nr if all are none. */ @@ -3122,31 +3130,63 @@ static int calc_anon_folio_order_alloc(struct vm_fault *vmf, int order) * case, all we need to do here is to mark the page as writable and update * any related book-keeping. */ -static inline void wp_page_reuse(struct vm_fault *vmf) +static inline void wp_page_reuse(struct vm_fault *vmf, + struct anon_folio_range *range) __releases(vmf->ptl) { struct vm_area_struct *vma = vmf->vma; - struct page *page = vmf->page; + unsigned long addr; + pte_t *pte; + struct page *page; + int nr; pte_t entry; + int change = 0; + int i; VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE)); - VM_BUG_ON(page && PageAnon(page) && !PageAnonExclusive(page)); - /* - * Clear the pages cpupid information as the existing - * information potentially belongs to a now completely - * unrelated process. - */ - if (page) - page_cpupid_xchg_last(page, (1 << LAST_CPUPID_SHIFT) - 1); + if (range) { + addr = range->va_start; + pte = range->pte_start; + page = range->pg_start; + nr = range->nr; + } else { + addr = vmf->address; + pte = vmf->pte; + page = vmf->page; + nr = 1; + } + + if (page) { + for (i = 0; i < nr; i++, page++) { + VM_BUG_ON(PageAnon(page) && !PageAnonExclusive(page)); + + /* + * Clear the pages cpupid information as the existing + * information potentially belongs to a now completely + * unrelated process. + */ + page_cpupid_xchg_last(page, + (1 << LAST_CPUPID_SHIFT) - 1); + } + } + + flush_cache_range(vma, addr, addr + (nr << PAGE_SHIFT)); + + for (i = 0; i < nr; i++) { + entry = pte_mkyoung(pte[i]); + entry = maybe_mkwrite(pte_mkdirty(entry), vma); + change |= ptep_set_access_flags(vma, + addr + (i << PAGE_SHIFT), + pte + i, + entry, 1); + } + + if (change) + update_mmu_cache_range(vma, addr, pte, nr); - flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); - entry = pte_mkyoung(vmf->orig_pte); - entry = maybe_mkwrite(pte_mkdirty(entry), vma); - if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) - update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); - count_vm_event(PGREUSE); + count_vm_events(PGREUSE, nr); } /* @@ -3359,7 +3399,7 @@ vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf) pte_unmap_unlock(vmf->pte, vmf->ptl); return VM_FAULT_NOPAGE; } - wp_page_reuse(vmf); + wp_page_reuse(vmf, NULL); return 0; } @@ -3381,7 +3421,7 @@ static vm_fault_t wp_pfn_shared(struct vm_fault *vmf) return ret; return finish_mkwrite_fault(vmf); } - wp_page_reuse(vmf); + wp_page_reuse(vmf, NULL); return 0; } @@ -3410,7 +3450,7 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf) return tmp; } } else { - wp_page_reuse(vmf); + wp_page_reuse(vmf, NULL); lock_page(vmf->page); } ret |= fault_dirty_shared_page(vmf); @@ -3534,7 +3574,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf) pte_unmap_unlock(vmf->pte, vmf->ptl); return 0; } - wp_page_reuse(vmf); + wp_page_reuse(vmf, NULL); return 0; } copy: