From patchwork Mon Dec 4 14:21:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 13478503 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 8461DC4167B for ; Mon, 4 Dec 2023 14:22:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 819756B0188; Mon, 4 Dec 2023 09:22:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CDCC6B018C; Mon, 4 Dec 2023 09:22:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F6616B018D; Mon, 4 Dec 2023 09:22:20 -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 447E16B0188 for ; Mon, 4 Dec 2023 09:22:20 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 19EA7C012E for ; Mon, 4 Dec 2023 14:22:20 +0000 (UTC) X-FDA: 81529350840.13.B1750B3 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf23.hostedemail.com (Postfix) with ESMTP id 65245140003 for ; Mon, 4 Dec 2023 14:22:18 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cV4zFsAt; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1701699738; 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=HK+wCLAanK8icsaUODtbkemdg+UtHwl4vMlZjDb9LtY=; b=IAQ/I4q1M2YRdsznf598o1DlIeOYao9M5x6IUTuwl2jOjyRxtX9YD8fwARaiDPPqwHWAkb ih1jATaJrfxYzcj23pBdhYuUPD4TnH0ZC1DeuEmpxCg/yfyjU8CRqonK/Ij6ML4MxVL3m9 PDk6jMM3Z2Q8Yvq9UgLJftje/IsyZl0= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=cV4zFsAt; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf23.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701699738; a=rsa-sha256; cv=none; b=GvZobcG5+cHUMVNk8HoBP23tiqPVIfXiDH0mmHT/OmCYFA5I+Ym4A23Kxk5QgAbjDBWL+m qZxGLlj7NCsArFgMqm95cpHuMnZrmWbA3feWTALuD6HeDkIJOR0fRU3VrQwDdeyLYc9YRG LUPfKT6YjjRNx2uAgT232oUgesUeUTI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701699737; 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=cV4zFsAtsScxcDDOttLWHSPeaYcjxejUP6jJk20gSBa//4kgKFagntGzja718DD0tP7A0Y 5NgRgQqZOrqXPct3kHZNbYmNnYbS9xEPnlg5aqasND7ky5EsWfEx0dXTxpb93ct2ildDcR pKvA6Q2u6fADregmQJetz05GH0dVMAU= 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 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 65245140003 X-Stat-Signature: ip3jq6iftrnyf45p4ac9qzwz6ez5585x X-HE-Tag: 1701699738-733212 X-HE-Meta: U2FsdGVkX1+hzlQYv8Ml/+QamMYC7szSI55Baa45uZrU9SgD8B8A5k4CNCMlORw6wS2GqV8a5T8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84t1VDBTqNKGfWlxX1msDc65vdZFWh0f3gSO3w== 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 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; } +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 = &folio->_nr_pages_mapped; + int first, nr = 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 == RMAP_MODE_PTE)) { + do { + first = atomic_inc_and_test(&page->_mapcount); + if (first && folio_test_large(folio)) { + first = atomic_inc_return_relaxed(mapped); + first = (first < COMPOUND_MAPPED); + } + + if (first) + nr++; + } while (page++, --nr_pages > 0); + } else if (mode == RMAP_MODE_PMD) { + first = atomic_inc_and_test(&folio->_entire_mapcount); + if (first) { + nr = atomic_add_return_relaxed(COMPOUND_MAPPED, mapped); + if (likely(nr < COMPOUND_MAPPED + COMPOUND_MAPPED)) { + *nr_pmdmapped = folio_nr_pages(folio); + nr = *nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); + /* Raced ahead of a remove and another add? */ + if (unlikely(nr < 0)) + nr = 0; + } else { + /* Raced ahead of a remove of COMPOUND_MAPPED */ + nr = 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(struct folio *folio, struct page *page, unsigned int nr_pages, struct vm_area_struct *vma, enum rmap_mode mode) { - atomic_t *mapped = &folio->_nr_pages_mapped; - unsigned int nr_pmdmapped = 0, first; - int nr = 0; + unsigned int nr, nr_pmdmapped = 0; 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 == RMAP_MODE_PTE)) { - do { - first = atomic_inc_and_test(&page->_mapcount); - if (first && folio_test_large(folio)) { - first = atomic_inc_return_relaxed(mapped); - first = (first < COMPOUND_MAPPED); - } - - if (first) - nr++; - } while (page++, --nr_pages > 0); - } else if (mode == RMAP_MODE_PMD) { - first = atomic_inc_and_test(&folio->_entire_mapcount); - if (first) { - nr = atomic_add_return_relaxed(COMPOUND_MAPPED, mapped); - if (likely(nr < COMPOUND_MAPPED + COMPOUND_MAPPED)) { - nr_pmdmapped = folio_nr_pages(folio); - nr = nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED); - /* Raced ahead of a remove and another add? */ - if (unlikely(nr < 0)) - nr = 0; - } else { - /* Raced ahead of a remove of COMPOUND_MAPPED */ - nr = 0; - } - } - } + nr = __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);