From patchwork Fri Apr 14 13:03:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13211469 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 DBB8FC77B72 for ; Fri, 14 Apr 2023 13:04:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5131F280008; Fri, 14 Apr 2023 09:03:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 49CAA280001; Fri, 14 Apr 2023 09:03:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A0D5280008; Fri, 14 Apr 2023 09:03:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 113FE280001 for ; Fri, 14 Apr 2023 09:03:42 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id D0CBE160207 for ; Fri, 14 Apr 2023 13:03:41 +0000 (UTC) X-FDA: 80680013442.25.E286147 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf21.hostedemail.com (Postfix) with ESMTP id 0519C1C0006 for ; Fri, 14 Apr 2023 13:03:39 +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=1681477420; 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=iux+4ckBwMpXp6sm/36rK893xRH3ArY8ns7SoNZg89w=; b=Gf9hAPqRpwMIffDZg037EhsEWqmiDCDCbyiR/R2peYr1kUXt80Koz8PvmZWA3iumQNzApS A2QvMV5D6parjpOVT73+Kx6JqXZ3g0w+e6EMh7GNJK88faRHHkSgP/vmd4+yM39axHSgQM zUJIjiMvx3nr4WS004YKrLYX1HFbKXU= 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=1681477420; a=rsa-sha256; cv=none; b=FxmXm2uMFwowtusukPY01Txxt+E8xsOBaJ+Gsj7xCOhgKD7WrwGFV8VabSrbV6CmaI2VHM +mxm5ExCpe8jdHyHuXrK2eF8SEtzZi+MGep92Qfl9iEBnIdq92Fs5aR5OKATrsIjaMpszY YZ6lxCxtNrVysuVtgiqojNAwnPX7ixk= 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 3D53C176A; Fri, 14 Apr 2023 06:04:24 -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 B902E3F6C4; Fri, 14 Apr 2023 06:03:38 -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 17/17] mm: Batch-zap large anonymous folio PTE mappings Date: Fri, 14 Apr 2023 14:03:03 +0100 Message-Id: <20230414130303.2345383-18-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: eia4aa4m5uepwwuzsmr7jsyqhfkkca7b X-Rspamd-Queue-Id: 0519C1C0006 X-HE-Tag: 1681477419-562803 X-HE-Meta: U2FsdGVkX19j5hG5yOwt1ucqmM3wLZKV/BJ58/1MFTYVK/b6secZNbTQCnO0x7ix0zGU30uM/51HOxD4+PckfEYd43GdPmzbKBcE/PWQ6PDk4prkzs7Gt164HNB6O+cQ/emtLVtn0dfGYaU743ab2w4+dQ99/v1xZ36Qv46zvvpf/FvpmT9m24oGKnVOu3B3V+oaSZwgxmobtJmhrnYs+3PXVJvC1VGWX5VDrk1mH3gVa4elgWlJxwG1uHYB7bg3gS4W4VL8N5w8fqqZDwdknd+vtb4Jxn9hOySFPnLxBxqETtq84eIIbFIX641/lGOpvZjoMLghJbpYtlxXRkvnbHm1Zth4Ha91U5oqmbYzWbaeW+nidbsBx/v2Mrdp2UdUCasg5tZrdLkmPEGyhnIqBqnXLZ9Bg0HF0EcvLCKmDPYm2zA2DnQK1k/juEtB2xTCwpbIYwMKMB3A9V7cFqblMJL8YCkCfuVH3lqK/HdprPcilMiich6+ubp9i9+4hxB4n4MF08YsLM8NFqKxAuAZxtsPp6KpCWELITxxpuEGK37bVWiPUxVf4ojTlt7SBvgZ1E/1oHrp2jekyIcywo2nFiUbVEi4Ct9jK1Uwn9yo0rjjz6Du5ugl6RE8fAznGyMfaOsWJbtK5jRreCehwqr8XzYqvcnt1c0o3FCvXXdTYg5itOZF8J1sbl4EkR0sqPYNVgvG8iVuEuJ36Nn89OeYmuCPF6Q/2eDh4Mbc/5iE2HgrwbkxFS61qg/YGMyibhZiph6UY2SIZ4rHYR9z3QGzWhyRkcNmfNICfb3l7K1uh1o7gkUYWok6Dl4ARx7P+T/uJNod3/Ryuyc4K83CLALvK0o2p36HIINBy0ZaXhjYKvzVVvmxVrKtbiqSntfIwufewi3FqYPF/Y5m2IYlPMWXqrr6vhA0f6HJ3Ex3cybqlzLLFQ9kmkT0r3R0oW8RGyBhKDeRC58Ip/oo8avPt5c Dxid0+9D e40UIChEDDmRYOs5xWCYYKLv8QpiCYBys2iaEZmk2uupCCO9jzBj5IgI5uDjrVAOuccTZr/Y/HX8WehFLUowdyPmdGR7Zzd4h5hu371gmfwlIORNcWupMEGApdjvMXwZEGIQKXpjbyxiBOMfiMJbfzlP2qo+YBm8M7KXFhivXFrZj4B4= 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: This allows batching the rmap removal with folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention. Previously each page was removed from the rmap individually with page_remove_rmap(). If the first page belonged to a large folio, this would cause page_remove_rmap() to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue. Signed-off-by: Ryan Roberts --- mm/memory.c | 139 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 119 insertions(+), 20 deletions(-) -- 2.25.1 diff --git a/mm/memory.c b/mm/memory.c index fac686e9f895..e1cb4bf6fd5d 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1351,6 +1351,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); } +static inline unsigned long page_addr(struct page *page, + struct page *anchor, unsigned long anchor_addr) +{ + unsigned long offset; + unsigned long addr; + + offset = (page_to_pfn(page) - page_to_pfn(anchor)) << PAGE_SHIFT; + addr = anchor_addr + offset; + + if (anchor > page) { + if (addr > anchor_addr) + return 0; + } else { + if (addr < anchor_addr) + return ULONG_MAX; + } + + return addr; +} + +static int calc_anon_folio_map_pgcount(struct folio *folio, + struct page *page, pte_t *pte, + unsigned long addr, unsigned long end) +{ + pte_t ptent; + int floops; + int i; + unsigned long pfn; + + end = min(page_addr(&folio->page + folio_nr_pages(folio), page, addr), + end); + floops = (end - addr) >> PAGE_SHIFT; + pfn = page_to_pfn(page); + pfn++; + pte++; + + for (i = 1; i < floops; i++) { + ptent = *pte; + + if (!pte_present(ptent) || + pte_pfn(ptent) != pfn) { + return i; + } + + pfn++; + pte++; + } + + return floops; +} + +static unsigned long zap_anon_pte_range(struct mmu_gather *tlb, + struct vm_area_struct *vma, + struct page *page, pte_t *pte, + unsigned long addr, unsigned long end, + bool *full_out) +{ + struct folio *folio = page_folio(page); + struct mm_struct *mm = tlb->mm; + pte_t ptent; + int pgcount; + int i; + bool full; + + pgcount = calc_anon_folio_map_pgcount(folio, page, pte, addr, end); + + for (i = 0; i < pgcount;) { + ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + tlb_remove_tlb_entry(tlb, pte, addr); + full = __tlb_remove_page(tlb, page, 0); + + if (unlikely(page_mapcount(page) < 1)) + print_bad_pte(vma, addr, ptent, page); + + i++; + page++; + pte++; + addr += PAGE_SIZE; + + if (unlikely(full)) + break; + } + + folio_remove_rmap_range(folio, page - i, i, vma); + + *full_out = full; + return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end, @@ -1387,6 +1476,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page = vm_normal_page(vma, addr, ptent); if (unlikely(!should_zap_page(details, page))) continue; + + /* + * Batch zap large anonymous folio mappings. This allows + * batching the rmap removal, which means we avoid + * spuriously adding a partially unmapped folio to the + * deferrred split queue in the common case, which + * reduces split queue lock contention. Require the VMA + * to be anonymous to ensure that none of the PTEs in + * the range require zap_install_uffd_wp_if_needed(). + */ + if (page && PageAnon(page) && vma_is_anonymous(vma)) { + bool full; + int pgcount; + + pgcount = zap_anon_pte_range(tlb, vma, + page, pte, addr, end, &full); + + rss[mm_counter(page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount << PAGE_SHIFT; + + if (unlikely(full)) { + force_flush = 1; + addr += PAGE_SIZE; + break; + } + continue; + } + ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); tlb_remove_tlb_entry(tlb, pte, addr); @@ -3051,26 +3170,6 @@ struct anon_folio_range { bool exclusive; }; -static inline unsigned long page_addr(struct page *page, - struct page *anchor, unsigned long anchor_addr) -{ - unsigned long offset; - unsigned long addr; - - offset = (page_to_pfn(page) - page_to_pfn(anchor)) << PAGE_SHIFT; - addr = anchor_addr + offset; - - if (anchor > page) { - if (addr > anchor_addr) - return 0; - } else { - if (addr < anchor_addr) - return ULONG_MAX; - } - - return addr; -} - /* * Returns index of first pte that is not none, or nr if all are none. */