From patchwork Fri Feb 9 22:15:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13551940 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 2A37BC4829E for ; Fri, 9 Feb 2024 22:15:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 98F576B0081; Fri, 9 Feb 2024 17:15:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9184C6B0082; Fri, 9 Feb 2024 17:15:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 792996B0085; Fri, 9 Feb 2024 17:15:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 6773A6B0081 for ; Fri, 9 Feb 2024 17:15:28 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 09234160122 for ; Fri, 9 Feb 2024 22:15:28 +0000 (UTC) X-FDA: 81773672736.07.FE44159 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf15.hostedemail.com (Postfix) with ESMTP id 5C20DA000D for ; Fri, 9 Feb 2024 22:15:26 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eO0Do0z9; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707516926; 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:dkim-signature; bh=GUXSwV0Ba8onVJaRwxmEJ/5t7CtvdY0dXq3jtt3HQ88=; b=PrYHuYcoZ7dRmqWQrBuptvOWANyIb+BUpBBqoCJSInor4RzfLucdWf/SAneq573tyGYPpV wM+lvil1sN0FWo0pUqYuexjpiVneWXqJH8h5wkNUP+ksWTNYxX1jqIrESeo5dSr4ylk0tt VrtULVfXfM5m+8jvTPTGyVYf8IDDXos= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eO0Do0z9; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf15.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707516926; a=rsa-sha256; cv=none; b=z8M4Q1jJz+gE+b1L3fvc9s2lc6y6kK3TlpZMDKev4Rizb6cmIGL0iKPVDpX7gFhG7DZatZ Wb97q3s6Of+JF6lKtWIAr4bWnH4pq7YLNVUqSq7F6hzZVLjXKVIrEJDdRqFVbSg5xj6a0z yP3xDwaAufOu+DeVZyQxpQQrPWy3BlQ= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707516925; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=GUXSwV0Ba8onVJaRwxmEJ/5t7CtvdY0dXq3jtt3HQ88=; b=eO0Do0z9Lx9rSmCheBzWkfmkXHRK0CqSnA3iOqB39SzWSuUoU4Zdzo69J7Cn6VwkJQDj2J WDlejUjNkqRpVM9JswND24gAfDqzjhCTba3XtjszGKVyH6OF7ypZfktmR08zXFDoyEJf92 pqmQEbXQ/XNyqdOdGWZizuc6mnmMeDQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-42-CTYzAu7yOQSNMpmHOZXTyA-1; Fri, 09 Feb 2024 17:15:21 -0500 X-MC-Unique: CTYzAu7yOQSNMpmHOZXTyA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 85BC185A589; Fri, 9 Feb 2024 22:15:20 +0000 (UTC) Received: from t14s.redhat.com (unknown [10.39.194.59]) by smtp.corp.redhat.com (Postfix) with ESMTP id 74DCF1C14B04; Fri, 9 Feb 2024 22:15:16 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Matthew Wilcox , Ryan Roberts , Catalin Marinas , Yin Fengwei , Michal Hocko , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Peter Zijlstra , Michael Ellerman , Christophe Leroy , "Naveen N. Rao" , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Arnd Bergmann , linux-arch@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org Subject: [PATCH v2 01/10] mm/memory: factor out zapping of present pte into zap_present_pte() Date: Fri, 9 Feb 2024 23:15:00 +0100 Message-ID: <20240209221509.585251-2-david@redhat.com> In-Reply-To: <20240209221509.585251-1-david@redhat.com> References: <20240209221509.585251-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Rspamd-Queue-Id: 5C20DA000D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: jzuy59dogk3ych9wbrgcthyxjm1ezr91 X-HE-Tag: 1707516926-583878 X-HE-Meta: U2FsdGVkX19e3fBKObnlu2OT0QoR9xtCCtV2FK6sjLu8pe7VtQGblFKm9Jn2Tk0vN7VczH4I4XD5xHdaXVIpXTlp16hYEhCm1XoyItUVgPPQ4mAXbr4Y1WEY0AfKyvoFl00JNZvIdPHBHohOl+4MkjvaF7e3ORniYmRkb2t/bebJ5fyHJRSQMtbUN+VMtGHmczNcvcv/XXehko/JuC2S71RlrbuqvrqH26C5m1b5eHofSQJq0W7bCJ4LXlG81eVEEaKCT7RzA+iACVrQNsG4o3WKSlp/q7szGYna6HSkTB9NOhpwhb9tSjXduu3r9NI/2UFottUG3+j6to8dAPs8lQfdsedBKXBo5GVNub1PfUMJiB9vTQdrC5w7an8986PXs/Sjnoag/UpdKCWa0h9UyQRBIXPdW6nZMYldD3IXstN0KitqX5zfK96FcpwSShzWtyiEVsoWRP/3A3KSR5rpY7AgMcp+vX/1PUFzn/7vC9bE2gNHa0tN1XiZgsEGeelya47G6vYiCzQdiyq97rXVJ1GXUrkhB2+G5wExBxqjBAX1xhi7fPjKYqxr2d3i/86RMcvL2v/hI+66UftvpH+nq/kRmV9QmN/WnxF8Vck4b+FcgDuYlamCuGkbmtbElhJqrtwnx4ZA4RREiEm423XEvnAkvnuwjLcCckTW2Wlkz1MOWa5UfpI9kWRKCn82su+2KpSadWLIYdkpro7d2aXFZcrMkuBnZXLAay51DbmIZXruHAuykdXvt/KfQXk/Vng9OtQQbAIiAfu/0WAmRfjn4KMjNkokSMJCL5ESv/CKTP8X6CAmZjiVh1j0ExbGNJvL4CJN2O+k9ILU0LtM0UxZWB5NkN77K4lHwUoWd/luGDO6FcDaS62toAhFhMu/oLs4uL78+m2WNObmmr+CJIwHvpo7L7jLlSh5K53S6WLNSXPDbDxqglgKL8qFQGY9iavNkHW/iEbZpMH3b3LWp+5 JEFGQkGx 3cYljUzxH59X2ejG0Izm5FcfBodkR2Lo2GuASkUjj1l13JBDsy/y61DrYao3BCZdeRqWdAHCv3NHdMqF1lrjAQobsKJaIYAGdM2BbtfjknMbkxAoy15RG61NuuV3G6t1OhQS25qp/LlOqw0yA0/5AFBJ+b5nwRVWLJYQh6f7wBlHFyEPglfSpgtCme1HThKMCiisSf55+GhX235Jqr0IImgUL3pwskCP6/IS+ 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: List-Subscribe: List-Unsubscribe: Let's prepare for further changes by factoring out processing of present PTEs. Signed-off-by: David Hildenbrand Reviewed-by: Ryan Roberts --- mm/memory.c | 94 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 41 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 7c3ca41a7610..5b0dc33133a6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1532,13 +1532,61 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); } +static inline void zap_present_pte(struct mmu_gather *tlb, + struct vm_area_struct *vma, pte_t *pte, pte_t ptent, + unsigned long addr, struct zap_details *details, + int *rss, bool *force_flush, bool *force_break) +{ + struct mm_struct *mm = tlb->mm; + struct folio *folio = NULL; + bool delay_rmap = false; + struct page *page; + + page = vm_normal_page(vma, addr, ptent); + if (page) + folio = page_folio(page); + + if (unlikely(!should_zap_folio(details, folio))) + return; + ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + arch_check_zapped_pte(vma, ptent); + tlb_remove_tlb_entry(tlb, pte, addr); + zap_install_uffd_wp_if_needed(vma, addr, pte, details, ptent); + if (unlikely(!page)) { + ksm_might_unmap_zero_page(mm, ptent); + return; + } + + if (!folio_test_anon(folio)) { + if (pte_dirty(ptent)) { + folio_mark_dirty(folio); + if (tlb_delay_rmap(tlb)) { + delay_rmap = true; + *force_flush = true; + } + } + if (pte_young(ptent) && likely(vma_has_recency(vma))) + folio_mark_accessed(folio); + } + rss[mm_counter(folio)]--; + if (!delay_rmap) { + folio_remove_rmap_pte(folio, page, vma); + if (unlikely(page_mapcount(page) < 0)) + print_bad_pte(vma, addr, ptent, page); + } + if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { + *force_flush = true; + *force_break = true; + } +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end, struct zap_details *details) { + bool force_flush = false, force_break = false; struct mm_struct *mm = tlb->mm; - int force_flush = 0; int rss[NR_MM_COUNTERS]; spinlock_t *ptl; pte_t *start_pte; @@ -1555,7 +1603,7 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, arch_enter_lazy_mmu_mode(); do { pte_t ptent = ptep_get(pte); - struct folio *folio = NULL; + struct folio *folio; struct page *page; if (pte_none(ptent)) @@ -1565,45 +1613,9 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, break; if (pte_present(ptent)) { - unsigned int delay_rmap; - - page = vm_normal_page(vma, addr, ptent); - if (page) - folio = page_folio(page); - - if (unlikely(!should_zap_folio(details, folio))) - continue; - ptent = ptep_get_and_clear_full(mm, addr, pte, - tlb->fullmm); - arch_check_zapped_pte(vma, ptent); - tlb_remove_tlb_entry(tlb, pte, addr); - zap_install_uffd_wp_if_needed(vma, addr, pte, details, - ptent); - if (unlikely(!page)) { - ksm_might_unmap_zero_page(mm, ptent); - continue; - } - - delay_rmap = 0; - if (!folio_test_anon(folio)) { - if (pte_dirty(ptent)) { - folio_mark_dirty(folio); - if (tlb_delay_rmap(tlb)) { - delay_rmap = 1; - force_flush = 1; - } - } - if (pte_young(ptent) && likely(vma_has_recency(vma))) - folio_mark_accessed(folio); - } - rss[mm_counter(folio)]--; - if (!delay_rmap) { - folio_remove_rmap_pte(folio, page, vma); - if (unlikely(page_mapcount(page) < 0)) - print_bad_pte(vma, addr, ptent, page); - } - if (unlikely(__tlb_remove_page(tlb, page, delay_rmap))) { - force_flush = 1; + zap_present_pte(tlb, vma, pte, ptent, addr, details, + rss, &force_flush, &force_break); + if (unlikely(force_break)) { addr += PAGE_SIZE; break; }