From patchwork Mon Jul 17 14:31:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13315835 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 B0B19EB64DC for ; Mon, 17 Jul 2023 14:31:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CE156B0074; Mon, 17 Jul 2023 10:31:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 17E8F6B0075; Mon, 17 Jul 2023 10:31:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 046828D0001; Mon, 17 Jul 2023 10:31:34 -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 EA18A6B0074 for ; Mon, 17 Jul 2023 10:31:33 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B19C6B0BE4 for ; Mon, 17 Jul 2023 14:31:33 +0000 (UTC) X-FDA: 81021342066.05.2B4ACC9 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf08.hostedemail.com (Postfix) with ESMTP id E2FF916003B for ; Mon, 17 Jul 2023 14:31:27 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf08.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689604288; 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=Z9tyZhKLEUiT4Cu9fZro8vsV1H+EfHsCasdo1zxwtLo=; b=mg47KjXQ1SU/TtJuPJRlim+w66JwSaS/HUqtvU47QmDSs9Rttw4awZ641V0emwzLT6F+JM do6xSbUFgpkP/gEf8xIGi+t0NTqdqJ7o+zS4P1rUmL8fGITwy+VjavH3GTi3seKy8skeLc VhoDXBz6z+7SLeXqcjxo3wK0XIL1LCM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf08.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689604288; a=rsa-sha256; cv=none; b=w//DtUIooTm3oef/mC1UpV1eMsP+fjsalMsZR+2AQ8mn3KOGUH+H7HLVS5thoSMYI3p3vV +0RNarM9n5O3Tz79OBDCAf+qrSclF8axhEebsKIYL0zH715NOWYJ6TeaoYSHN9X5GqW/xu tM11ZWM/8tEpwdAZpUY6RAEfi1OPFJ8= 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 3A3BB1424; Mon, 17 Jul 2023 07:32:10 -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 4B2FC3F738; Mon, 17 Jul 2023 07:31:25 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , Matthew Wilcox , Yin Fengwei , David Hildenbrand , Yu Zhao , Yang Shi , "Huang, Ying" , Zi Yan Cc: Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings Date: Mon, 17 Jul 2023 15:31:10 +0100 Message-Id: <20230717143110.260162-4-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230717143110.260162-1-ryan.roberts@arm.com> References: <20230717143110.260162-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: E2FF916003B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 1x6ioxs1eakyesuue85yowk3rpp4eg4e X-HE-Tag: 1689604287-910088 X-HE-Meta: U2FsdGVkX1+Tr7Y/TNOZo8DVPM9RcnGTdeJpfZovL37RGOmAj72RATn+cyXDG/oUxB/8XtBJH8+fB3NuN8BiXjYaZV6zfROSSJmXeoFyB4IngkIRMVJkpfWTu4fvSV+Gu6q3xRrFbwAS/OYCSs5Z0UBfmvoz2g04KOdXP3HSsET42PI09U6q8X+lrafSW8M96O5gVM5YaNzPgMV/RZMAkjgvQ+7HuJQfNdk3sO8VluUnGUHqXRpXJKOTTCEzX6/7aGF+KUmH7R9P8KH/hDrxCVS6X2dpxOzZNpDZ4MrSzx+bYEb+dpBkO/cY5xYR/jpxj+nz4rbW9dixpUB94x1bjpzTO4xaZgli5KAyDeiZ8SDor4Q2QoiK8w3BuQUZ4bgMw+GvF2ArXVABmfe+UzznZ/uh8VezX0XiACiG7Zq3/8GUZW7hOAmMIl8kQRZwdDxCPZoXmmipAkfv1vtnF501Z64c00hdnAYGKdrAAaOVKlNjZR0ViwqlibCHL+EGQHDxGejJLmdb/CGjEw9rZ51FyIHnxkaIsCG4qKmXUtViM0XfPUIpEpxSRyMto5mh6mxw8CV4f+RoOkpHcmFOg47ZDRZ1IxEOifaTX1uuzy9Q65CaJDGA2uT1mdWNQhQQC9AsraKuadTheMPma4QvaxOrC3ztyK0Nt82LzqMSVwcENRAd51+tLRlXI4w3zmysvp5/i5wKsWeSDVAI9watFgpxeGr2V/K8c8Uh8uKiXxdVMPOar9cT/qCaCrxDaoCdF01OkTZpUKXtehDXyac8ngGqtJNVwCQLL/ZDIHOWcpUDqdl+zx+bjUWMJYKnbXTxGjad1D3O/yfoF+WkW4WH1ljy5oa/oexoIB/4wphLnkBP8T8toa20h9Nee5yp9zC9LNkNXUsRXwGdXsE1mmuxVMs+fmuoSl5XrncsGENSzD5mNK1sOpn/29I6bsfpFvXk5rEPN8EyeFf1m0ZbS0virMZ 62Q5vMwk LPquoEC2mtMXCoGHPSNDHV6GIdcyJNakgDGYRKvKhPfQELHuRvoLpYC4rCpy9yvv2yWOwRKOXfjgjaGz0yVu0qF1i7yvd2+1wSymfSUNKyUjL5Mtnzc/Gi3cdWpUpwgN+WL6MhHBApFnmiB2MZ8kXWbmpcA== 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 | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index 01f39e8144ef..6facb8c8807a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1391,6 +1391,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 = ptep_get(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, @@ -1428,6 +1517,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);