From nobody Mon Dec 29 00:44:47 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 07A66C10DC1 for ; Mon, 4 Dec 2023 14:23:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345304AbjLDOXC (ORCPT ); Mon, 4 Dec 2023 09:23:02 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235862AbjLDOWZ (ORCPT ); Mon, 4 Dec 2023 09:22:25 -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 DCF871721 for ; Mon, 4 Dec 2023 06:22:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701699738; 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=HK+wCLAanK8icsaUODtbkemdg+UtHwl4vMlZjDb9LtY=; b=LbDNQUwdJBEAHZMfx+lR6r5w8iMrULA6uwC/6tX35g+cBE5972WwWne56a4OG9h1cY96L/ NhntiOHu29TjxTP7KkdcJzgHgeT7oYFFsPxfiNOMK+D9Ut16+I2BqJkEv3VDU8k6dQj7T5 wk0kLdB+n+gpxoS4uXJvSC1vHiGJUSM= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-441-tU-ty63bN-Gpvd547o2MWA-1; Mon, 04 Dec 2023 09:22:13 -0500 X-MC-Unique: tU-ty63bN-Gpvd547o2MWA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (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 E0D0129AA3AE; Mon, 4 Dec 2023 14:22:12 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.195.87]) by smtp.corp.redhat.com (Postfix) with ESMTP id 477C72026D6E; Mon, 4 Dec 2023 14:22:11 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , "Matthew Wilcox (Oracle)" , Hugh Dickins , Ryan Roberts , Yin Fengwei , Mike Kravetz , Muchun Song , Peter Xu Subject: [PATCH RFC 13/39] mm/rmap: factor out adding folio mappings into __folio_add_rmap() Date: Mon, 4 Dec 2023 15:21:20 +0100 Message-ID: <20231204142146.91437-14-david@redhat.com> In-Reply-To: <20231204142146.91437-1-david@redhat.com> References: <20231204142146.91437-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Let's factor it out to prepare for reuse as we convert page_add_anon_rmap() to folio_add_anon_rmap_[pte|ptes|pmd](). Make the compiler always special-case on the granularity by using __always_inline. Signed-off-by: David Hildenbrand Reviewed-by: Yin Fengwei --- mm/rmap.c | 75 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 53e2c653be99a..c09b360402599 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1127,6 +1127,46 @@ int folio_total_mapcount(struct folio *folio) return mapcount; } =20 +static __always_inline unsigned int __folio_add_rmap(struct folio *folio, + struct page *page, unsigned int nr_pages, enum rmap_mode mode, + int *nr_pmdmapped) +{ + atomic_t *mapped =3D &folio->_nr_pages_mapped; + int first, nr =3D 0; + + __folio_rmap_sanity_checks(folio, page, nr_pages, mode); + + /* Is page being mapped by PTE? Is this its first map to be added? */ + if (likely(mode =3D=3D RMAP_MODE_PTE)) { + do { + first =3D atomic_inc_and_test(&page->_mapcount); + if (first && folio_test_large(folio)) { + first =3D atomic_inc_return_relaxed(mapped); + first =3D (first < COMPOUND_MAPPED); + } + + if (first) + nr++; + } while (page++, --nr_pages > 0); + } else if (mode =3D=3D RMAP_MODE_PMD) { + first =3D atomic_inc_and_test(&folio->_entire_mapcount); + if (first) { + nr =3D atomic_add_return_relaxed(COMPOUND_MAPPED, mapped); + if (likely(nr < COMPOUND_MAPPED + COMPOUND_MAPPED)) { + *nr_pmdmapped =3D folio_nr_pages(folio); + nr =3D *nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); + /* Raced ahead of a remove and another add? */ + if (unlikely(nr < 0)) + nr =3D 0; + } else { + /* Raced ahead of a remove of COMPOUND_MAPPED */ + nr =3D 0; + } + } + } + return nr; +} + /** * folio_move_anon_rmap - move a folio to our anon_vma * @folio: The folio to move to our anon_vma @@ -1338,42 +1378,11 @@ static __always_inline void __folio_add_file_rmap(s= truct folio *folio, struct page *page, unsigned int nr_pages, struct vm_area_struct *vma, enum rmap_mode mode) { - atomic_t *mapped =3D &folio->_nr_pages_mapped; - unsigned int nr_pmdmapped =3D 0, first; - int nr =3D 0; + unsigned int nr, nr_pmdmapped =3D 0; =20 VM_WARN_ON_FOLIO(folio_test_anon(folio), folio); - __folio_rmap_sanity_checks(folio, page, nr_pages, mode); - - /* Is page being mapped by PTE? Is this its first map to be added? */ - if (likely(mode =3D=3D RMAP_MODE_PTE)) { - do { - first =3D atomic_inc_and_test(&page->_mapcount); - if (first && folio_test_large(folio)) { - first =3D atomic_inc_return_relaxed(mapped); - first =3D (first < COMPOUND_MAPPED); - } - - if (first) - nr++; - } while (page++, --nr_pages > 0); - } else if (mode =3D=3D RMAP_MODE_PMD) { - first =3D atomic_inc_and_test(&folio->_entire_mapcount); - if (first) { - nr =3D atomic_add_return_relaxed(COMPOUND_MAPPED, mapped); - if (likely(nr < COMPOUND_MAPPED + COMPOUND_MAPPED)) { - nr_pmdmapped =3D folio_nr_pages(folio); - nr =3D nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); - /* Raced ahead of a remove and another add? */ - if (unlikely(nr < 0)) - nr =3D 0; - } else { - /* Raced ahead of a remove of COMPOUND_MAPPED */ - nr =3D 0; - } - } - } =20 + nr =3D __folio_add_rmap(folio, page, nr_pages, mode, &nr_pmdmapped); if (nr_pmdmapped) __lruvec_stat_mod_folio(folio, folio_test_swapbacked(folio) ? NR_SHMEM_PMDMAPPED : NR_FILE_PMDMAPPED, nr_pmdmapped); --=20 2.41.0