From nobody Wed Dec 17 17:22:15 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45997C61D97 for ; Fri, 24 Nov 2023 13:27:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345265AbjKXN1L (ORCPT ); Fri, 24 Nov 2023 08:27:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231494AbjKXN07 (ORCPT ); Fri, 24 Nov 2023 08:26:59 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A438170C for ; Fri, 24 Nov 2023 05:26:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700832409; 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=lJ96IMZVe5oCg8NRltA+Bp5PZmcoSK+MjUKn4pYnke0=; b=FM0OJ75bu4CiGpQIwlsLGqL+b36KzF5OtZ08MDNU3rz6kSWBiKvSVdCBLhiCSAcjcfzJ4x A5xYJeU7dBXk4M4oajgGIYdH7mVYaCmnthhpAIZjcIndrMg4ja61pqYJ3w2y1Ub1qYrZBb qZL72wZIhGyFcmcOP7X1Na1xsRMMu9g= 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-507-j-wvy56dM2qBkwjy1JR5fQ-1; Fri, 24 Nov 2023 08:26:47 -0500 X-MC-Unique: j-wvy56dM2qBkwjy1JR5fQ-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (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 5E5A4811E7B; Fri, 24 Nov 2023 13:26:46 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 46A822166B2A; Fri, 24 Nov 2023 13:26:43 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Linus Torvalds , Ryan Roberts , Matthew Wilcox , Hugh Dickins , Yin Fengwei , Yang Shi , Ying Huang , Zi Yan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , "Paul E. McKenney" Subject: [PATCH WIP v1 04/20] mm/rmap: pass dst_vma to page_try_dup_anon_rmap() and page_dup_file_rmap() Date: Fri, 24 Nov 2023 14:26:09 +0100 Message-ID: <20231124132626.235350-5-david@redhat.com> In-Reply-To: <20231124132626.235350-1-david@redhat.com> References: <20231124132626.235350-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" We'll need access to the destination MM when modifying the total mapcount of a partially-mappable folio next. So pass in the destination VMA for consistency. While at it, change the parameter order for page_try_dup_anon_rmap() such that the "bool compound" parameter is last, to match the other rmap functions. Signed-off-by: David Hildenbrand --- include/linux/rmap.h | 21 +++++++++++++-------- mm/huge_memory.c | 2 +- mm/hugetlb.c | 9 +++++---- mm/memory.c | 6 +++--- mm/migrate.c | 2 +- 5 files changed, 23 insertions(+), 17 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 42e2c74d4d6e..6cb497f6feab 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -208,7 +208,8 @@ void hugepage_add_anon_rmap(struct folio *, struct vm_a= rea_struct *, void hugepage_add_new_anon_rmap(struct folio *, struct vm_area_struct *, unsigned long address); =20 -static inline void __page_dup_rmap(struct page *page, bool compound) +static inline void __page_dup_rmap(struct page *page, + struct vm_area_struct *dst_vma, bool compound) { struct folio *folio =3D page_folio(page); =20 @@ -225,17 +226,19 @@ static inline void __page_dup_rmap(struct page *page,= bool compound) atomic_inc(&folio->_total_mapcount); } =20 -static inline void page_dup_file_rmap(struct page *page, bool compound) +static inline void page_dup_file_rmap(struct page *page, + struct vm_area_struct *dst_vma, bool compound) { - __page_dup_rmap(page, compound); + __page_dup_rmap(page, dst_vma, compound); } =20 /** * page_try_dup_anon_rmap - try duplicating a mapping of an already mapped * anonymous page * @page: the page to duplicate the mapping for + * @dst_vma: the destination vma + * @src_vma: the source vma * @compound: the page is mapped as compound or as a small page - * @vma: the source vma * * The caller needs to hold the PT lock and the vma->vma_mm->write_protect= _seq. * @@ -247,8 +250,10 @@ static inline void page_dup_file_rmap(struct page *pag= e, bool compound) * * Returns 0 if duplicating the mapping succeeded. Returns -EBUSY otherwis= e. */ -static inline int page_try_dup_anon_rmap(struct page *page, bool compound, - struct vm_area_struct *vma) +static inline int page_try_dup_anon_rmap(struct page *page, + struct vm_area_struct *dst_vma, + struct vm_area_struct *src_vma, + bool compound) { VM_BUG_ON_PAGE(!PageAnon(page), page); =20 @@ -267,7 +272,7 @@ static inline int page_try_dup_anon_rmap(struct page *p= age, bool compound, * future on write faults. */ if (likely(!is_device_private_page(page) && - unlikely(page_needs_cow_for_dma(vma, page)))) + unlikely(page_needs_cow_for_dma(src_vma, page)))) return -EBUSY; =20 ClearPageAnonExclusive(page); @@ -276,7 +281,7 @@ static inline int page_try_dup_anon_rmap(struct page *p= age, bool compound, * the page R/O into both processes. */ dup: - __page_dup_rmap(page, compound); + __page_dup_rmap(page, dst_vma, compound); return 0; } =20 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 874eeeb90e0b..51a878efca0e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1166,7 +1166,7 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm= _struct *src_mm, VM_BUG_ON_PAGE(!PageHead(src_page), src_page); =20 get_page(src_page); - if (unlikely(page_try_dup_anon_rmap(src_page, true, src_vma))) { + if (unlikely(page_try_dup_anon_rmap(src_page, dst_vma, src_vma, true))) { /* Page maybe pinned: split and retry the fault on PTEs. */ put_page(src_page); pte_free(dst_mm, pgtable); diff --git a/mm/hugetlb.c b/mm/hugetlb.c index cf84784064c7..1ddef4082cad 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5401,9 +5401,10 @@ int copy_hugetlb_page_range(struct mm_struct *dst, s= truct mm_struct *src, * sleep during the process. */ if (!folio_test_anon(pte_folio)) { - page_dup_file_rmap(&pte_folio->page, true); + page_dup_file_rmap(&pte_folio->page, dst_vma, + true); } else if (page_try_dup_anon_rmap(&pte_folio->page, - true, src_vma)) { + dst_vma, src_vma, true)) { pte_t src_pte_old =3D entry; struct folio *new_folio; =20 @@ -6272,7 +6273,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *m= m, if (anon_rmap) hugepage_add_new_anon_rmap(folio, vma, haddr); else - page_dup_file_rmap(&folio->page, true); + page_dup_file_rmap(&folio->page, vma, true); new_pte =3D make_huge_pte(vma, &folio->page, ((vma->vm_flags & VM_WRITE) && (vma->vm_flags & VM_SHARED))); /* @@ -6723,7 +6724,7 @@ int hugetlb_mfill_atomic_pte(pte_t *dst_pte, goto out_release_unlock; =20 if (folio_in_pagecache) - page_dup_file_rmap(&folio->page, true); + page_dup_file_rmap(&folio->page, dst_vma, true); else hugepage_add_new_anon_rmap(folio, dst_vma, dst_addr); =20 diff --git a/mm/memory.c b/mm/memory.c index 6bcfa763a146..14416d05e1b6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -836,7 +836,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm= _struct *src_mm, get_page(page); rss[mm_counter(page)]++; /* Cannot fail as these pages cannot get pinned. */ - BUG_ON(page_try_dup_anon_rmap(page, false, src_vma)); + BUG_ON(page_try_dup_anon_rmap(page, dst_vma, src_vma, false)); =20 /* * We do not preserve soft-dirty information, because so @@ -950,7 +950,7 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct= vm_area_struct *src_vma, * future. */ folio_get(folio); - if (unlikely(page_try_dup_anon_rmap(page, false, src_vma))) { + if (unlikely(page_try_dup_anon_rmap(page, dst_vma, src_vma, false))) { /* Page may be pinned, we have to copy. */ folio_put(folio); return copy_present_page(dst_vma, src_vma, dst_pte, src_pte, @@ -959,7 +959,7 @@ copy_present_pte(struct vm_area_struct *dst_vma, struct= vm_area_struct *src_vma, rss[MM_ANONPAGES]++; } else if (page) { folio_get(folio); - page_dup_file_rmap(page, false); + page_dup_file_rmap(page, dst_vma, false); rss[mm_counter_file(page)]++; } =20 diff --git a/mm/migrate.c b/mm/migrate.c index fda41bc09903..341a84c3e8e4 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -252,7 +252,7 @@ static bool remove_migration_pte(struct folio *folio, hugepage_add_anon_rmap(folio, vma, pvmw.address, rmap_flags); else - page_dup_file_rmap(new, true); + page_dup_file_rmap(new, vma, true); set_huge_pte_at(vma->vm_mm, pvmw.address, pvmw.pte, pte, psize); } else --=20 2.41.0