From nobody Wed Dec 17 17:23:09 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 2F7CBC61D97 for ; Fri, 24 Nov 2023 13:29:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345248AbjKXN30 (ORCPT ); Fri, 24 Nov 2023 08:29:26 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235293AbjKXN2h (ORCPT ); Fri, 24 Nov 2023 08:28:37 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81CF72686 for ; Fri, 24 Nov 2023 05:27:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700832462; 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=BnEHwgivpdbaZgh54Ih7L1AobW4tDWwZEN96Ch5d2sI=; b=Hizn47stOY0bjcCAapanjWMo1DctKX6iH+q4hCiCWA0XhmT2hRBOUWfHfCXBdsEZQQsJiU PwgKXtiLA4TJss8Dutnh2w5T8tDYEclkU4NKPAOWGGLBOp0UWz2wZUCxxhYje3Xa4IQL8/ TGJ7vu7hxWsuWfYCnpXM3IRr5scvSJ8= 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-542-EU4DmTN2O1ar0sjJ8gqpbQ-1; Fri, 24 Nov 2023 08:27:41 -0500 X-MC-Unique: EU4DmTN2O1ar0sjJ8gqpbQ-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 3AD05185A783; Fri, 24 Nov 2023 13:27:40 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 550AE2166B2A; Fri, 24 Nov 2023 13:27:37 +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 19/20] mm/rmap: factor out removing folio range into __folio_remove_rmap_range() Date: Fri, 24 Nov 2023 14:26:24 +0100 Message-ID: <20231124132626.235350-20-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" Let's factor it out, optimize for small folios, and compact it a bit. Well, we're adding the range part, but that will surely come in handy soon -- and it's now wasier to compare it with __folio_add_rmap_range(). Signed-off-by: David Hildenbrand --- mm/rmap.c | 90 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 55 insertions(+), 35 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index da7fa46a18fc..80ac53633332 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1155,6 +1155,57 @@ static unsigned int __folio_add_rmap_range(struct fo= lio *folio, return nr; } =20 +static unsigned int __folio_remove_rmap_range(struct folio *folio, + struct page *page, unsigned int nr_pages, + struct vm_area_struct *vma, bool compound, int *nr_pmdmapped) +{ + atomic_t *mapped =3D &folio->_nr_pages_mapped; + int last, count, nr =3D 0; + + VM_WARN_ON_FOLIO(compound && page !=3D &folio->page, folio); + VM_WARN_ON_FOLIO(compound && !folio_test_pmd_mappable(folio), folio); + VM_WARN_ON_FOLIO(compound && nr_pages !=3D folio_nr_pages(folio), folio); + VM_WARN_ON_FOLIO(!folio_test_large(folio) && nr_pages !=3D 1, folio); + + if (likely(!folio_test_large(folio))) + return atomic_add_negative(-1, &page->_mapcount); + + /* Is page being unmapped by PTE? Is this its last map to be removed? */ + if (!compound) { + folio_add_large_mapcount(folio, -nr_pages, vma); + count =3D nr_pages; + do { + last =3D atomic_add_negative(-1, &page->_mapcount); + if (last) { + last =3D atomic_dec_return_relaxed(mapped); + if (last < COMPOUND_MAPPED) + nr++; + } + } while (page++, --count > 0); + } else if (folio_test_pmd_mappable(folio)) { + /* That test is redundant: it's for safety or to optimize out */ + + folio_dec_large_mapcount(folio, vma); + last =3D atomic_add_negative(-1, &folio->_entire_mapcount); + if (last) { + nr =3D atomic_sub_return_relaxed(COMPOUND_MAPPED, mapped); + if (likely(nr < COMPOUND_MAPPED)) { + *nr_pmdmapped =3D folio_nr_pages(folio); + nr =3D *nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); + /* Raced ahead of another remove and an add? */ + if (unlikely(nr < 0)) + nr =3D 0; + } else { + /* An add of COMPOUND_MAPPED raced ahead */ + nr =3D 0; + } + } + } else { + VM_WARN_ON_ONCE_FOLIO(true, folio); + } + return nr; +} + /** * folio_move_anon_rmap - move a folio to our anon_vma * @folio: The folio to move to our anon_vma @@ -1439,13 +1490,10 @@ void page_remove_rmap(struct page *page, struct vm_= area_struct *vma, bool compound) { struct folio *folio =3D page_folio(page); - atomic_t *mapped =3D &folio->_nr_pages_mapped; - int nr =3D 0, nr_pmdmapped =3D 0; - bool last; + unsigned long nr_pages =3D compound ? folio_nr_pages(folio) : 1; + unsigned int nr, nr_pmdmapped =3D 0; enum node_stat_item idx; =20 - VM_BUG_ON_PAGE(compound && !PageHead(page), page); - /* Hugetlb pages are not counted in NR_*MAPPED */ if (unlikely(folio_test_hugetlb(folio))) { /* hugetlb pages are always mapped with pmds */ @@ -1454,36 +1502,8 @@ void page_remove_rmap(struct page *page, struct vm_a= rea_struct *vma, return; } =20 - if (folio_test_large(folio)) - folio_dec_large_mapcount(folio, vma); - - /* Is page being unmapped by PTE? Is this its last map to be removed? */ - if (likely(!compound)) { - last =3D atomic_add_negative(-1, &page->_mapcount); - nr =3D last; - if (last && folio_test_large(folio)) { - nr =3D atomic_dec_return_relaxed(mapped); - nr =3D (nr < COMPOUND_MAPPED); - } - } else if (folio_test_pmd_mappable(folio)) { - /* That test is redundant: it's for safety or to optimize out */ - - last =3D atomic_add_negative(-1, &folio->_entire_mapcount); - if (last) { - nr =3D atomic_sub_return_relaxed(COMPOUND_MAPPED, mapped); - if (likely(nr < COMPOUND_MAPPED)) { - nr_pmdmapped =3D folio_nr_pages(folio); - nr =3D nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); - /* Raced ahead of another remove and an add? */ - if (unlikely(nr < 0)) - nr =3D 0; - } else { - /* An add of COMPOUND_MAPPED raced ahead */ - nr =3D 0; - } - } - } - + nr =3D __folio_remove_rmap_range(folio, page, nr_pages, vma, compound, + &nr_pmdmapped); if (nr_pmdmapped) { if (folio_test_anon(folio)) idx =3D NR_ANON_THPS; --=20 2.41.0