From patchwork Wed Feb 1 08:17:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13123911 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 BD04BC636D6 for ; Wed, 1 Feb 2023 08:16:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2FE0A6B007B; Wed, 1 Feb 2023 03:16:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2619F6B007D; Wed, 1 Feb 2023 03:16:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B2D76B007E; Wed, 1 Feb 2023 03:16:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id EAD586B007B for ; Wed, 1 Feb 2023 03:15:59 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id C1C051C6511 for ; Wed, 1 Feb 2023 08:15:59 +0000 (UTC) X-FDA: 80418014838.25.C9CA551 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by imf02.hostedemail.com (Postfix) with ESMTP id B8F2A80003 for ; Wed, 1 Feb 2023 08:15:57 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EBm14QwP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675239357; 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=3v020W80207K5p7bwR9NFT8ToRSjhAW25L7AUqUD7gs=; b=Y4+t5nTZxLZEltRcDPp0W/OFzFtwbCXjirf2uN5Xh28+tOC80sMgNakvoJFnE6qEgdxVHJ nu1RJ8I56nqoUPaY+DOU/LgjS505iFTwDBOMsUpWgoUUHYc3nQyQZLc+9o4pFuBD3neuSj h8gkFw2guPfXx5h+pd7cqWPoJlvcyP8= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=EBm14QwP; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf02.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.31 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675239358; a=rsa-sha256; cv=none; b=6LUH/GdAYWFC+OtpDvasnCqiEjKqeyjMCde54CnexEQWc7TdXeIszz7PgmTqFH5GJZ19lR KndisxHwgYGD0i5TtnVVbD5c0RX7XNKMrCUEcEVzKsOdzqmhNnb14HDU8YZgE1I5AF926w jYn2gOH+IIRCMbzngsYSsDyUoREN/n4= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675239357; x=1706775357; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/JXq38FVFetnXKwqqjKvNYEq6660noLrsCCrhSHJtHk=; b=EBm14QwPeBud+j/4VWoxB3TrW8b5NDEAlHckHrPNFJZmQuBNSG/G4une C9L/o24XCaX8kAH2u/5vo8gz/aJCjNTsphOPp8DNuATbRR/YVb4ZPz7FT 0PZEdVgxLzzyT9Vaf4YWZCX/4Rb4mBxStHmwcjEsdpjDSlgbkTAy8Ru3a 4QdvSqnEXcdy2SZtDk0RQ7JBZ8vUmiPjXHZ7L7sxrJy2heC82lj4pTUZK tuO4XcLYtM+LtqqMiSdXGWpoXZzeD7coJOq923ESicuqR6BeGaOcU64K+ ch2mZvPoFAYA6NBHFPPCsgmcIKjNmeeugEN2QFeZdW4cSAxj5mM3iXMrV g==; X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="390471694" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="390471694" Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Feb 2023 00:15:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10607"; a="753580339" X-IronPort-AV: E=Sophos;i="5.97,263,1669104000"; d="scan'208";a="753580339" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by FMSMGA003.fm.intel.com with ESMTP; 01 Feb 2023 00:15:52 -0800 From: Yin Fengwei To: willy@infradead.org, david@redhat.com, linux-mm@kvack.org Cc: dave.hansen@intel.com, tim.c.chen@intel.com, ying.huang@intel.com, fengwei.yin@intel.com Subject: [RFC PATCH v2 3/5] rmap: add page_add_file_rmap_range() Date: Wed, 1 Feb 2023 16:17:35 +0800 Message-Id: <20230201081737.2330141-4-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230201081737.2330141-1-fengwei.yin@intel.com> References: <20230201081737.2330141-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B8F2A80003 X-Stat-Signature: jjk8qeeacftjtf1yttzbcejutwmgfta8 X-HE-Tag: 1675239357-252113 X-HE-Meta: U2FsdGVkX19D456xSM2dRGWfLkf34IoydD53CLDNiQ9S4JZhsgNwyJKZdUwRqvHYTbv1sxetFNFkNm7PQccSXlkyMOrbw9Q2CcE1c3JDjuOtw1G/yn6uuqaWtFb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JczdTRCRTxnfL9Ab150lzlWTf5NMmFEy1rQb4dNaRy5u5Aj+5wyCJYr2jjlGBy4K 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: page_add_file_rmap_range() allows to add pte mapping to a specific range of file folio. Comparing to original page_add_file_rmap(), it batched updates __lruvec_stat for large folio. Signed-off-by: Yin Fengwei --- include/linux/rmap.h | 2 ++ mm/rmap.c | 66 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index a4570da03e58..9631a3701504 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -198,6 +198,8 @@ void folio_add_new_anon_rmap(struct folio *, struct vm_area_struct *, unsigned long address); void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound); +void page_add_file_rmap_range(struct folio *, unsigned long start, + unsigned int nr_pages, struct vm_area_struct *, bool compound); void page_remove_rmap(struct page *, struct vm_area_struct *, bool compound); diff --git a/mm/rmap.c b/mm/rmap.c index 15ae24585fc4..090de52e1a9a 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1303,31 +1303,44 @@ void folio_add_new_anon_rmap(struct folio *folio, struct vm_area_struct *vma, } /** - * page_add_file_rmap - add pte mapping to a file page - * @page: the page to add the mapping to + * page_add_file_rmap_range - add pte mapping to a sub page range of a folio + * @folio: The filio to add the mapping to + * @start: The first sub page index in folio + * @nr_pages: The number of sub pages from the first page * @vma: the vm area in which the mapping is added * @compound: charge the page as compound or small page * + * The sub page range of folio is defined by + * [first_sub_page, first_sub_page + nr_pages) + * * The caller needs to hold the pte lock. */ -void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, - bool compound) +void page_add_file_rmap_range(struct folio *folio, unsigned long start, + unsigned int nr_pages, struct vm_area_struct *vma, + bool compound) { - struct folio *folio = page_folio(page); atomic_t *mapped = &folio->_nr_pages_mapped; - int nr = 0, nr_pmdmapped = 0; - bool first; + unsigned int nr = 0, nr_pmdmapped = 0, first; - VM_BUG_ON_PAGE(compound && !PageTransHuge(page), page); + VM_WARN_ON_FOLIO(compound && !folio_test_pmd_mappable(folio), folio); /* Is page being mapped by PTE? Is this its first map to be added? */ if (likely(!compound)) { - first = atomic_inc_and_test(&page->_mapcount); - nr = first; - if (first && folio_test_large(folio)) { - nr = atomic_inc_return_relaxed(mapped); - nr = (nr < COMPOUND_MAPPED); - } + struct page *page = folio_page(folio, start); + + nr_pages = min_t(unsigned int, nr_pages, + folio_nr_pages(folio) - start); + + do { + first = atomic_inc_and_test(&page->_mapcount); + if (first && folio_test_large(folio)) { + first = atomic_inc_return_relaxed(mapped); + first = (nr < COMPOUND_MAPPED); + } + + if (first) + nr++; + } while (page++, --nr_pages > 0); } else if (folio_test_pmd_mappable(folio)) { /* That test is redundant: it's for safety or to optimize out */ @@ -1356,6 +1369,31 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, mlock_vma_folio(folio, vma, compound); } +/** + * page_add_file_rmap - add pte mapping to a file page + * @page: the page to add the mapping to + * @vma: the vm area in which the mapping is added + * @compound: charge the page as compound or small page + * + * The caller needs to hold the pte lock. + */ +void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, + bool compound) +{ + struct folio *folio = page_folio(page); + unsigned int nr_pages; + + VM_WARN_ON_ONCE_PAGE(compound && !PageTransHuge(page), page); + + if (likely(!compound)) + nr_pages = 1; + else + nr_pages = folio_nr_pages(folio); + + page_add_file_rmap_range(folio, folio_page_idx(folio, page), + nr_pages, vma, compound); +} + /** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from