From patchwork Mon Feb 6 14:06:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13129996 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 7FF2BC636D6 for ; Mon, 6 Feb 2023 14:08:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D5276B0074; Mon, 6 Feb 2023 09:08:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 483616B0075; Mon, 6 Feb 2023 09:08:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2FD366B0078; Mon, 6 Feb 2023 09:08:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 2075B6B0074 for ; Mon, 6 Feb 2023 09:08:39 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DD1501C5EA9 for ; Mon, 6 Feb 2023 14:08:38 +0000 (UTC) X-FDA: 80437047516.03.DD59B3A Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by imf30.hostedemail.com (Postfix) with ESMTP id 7533780002 for ; Mon, 6 Feb 2023 14:08:36 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jEaizIQV; spf=pass (imf30.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1675692516; 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=bSe1dsEI/cRuQnhHM9hcrT2HZtexocFON22OgJp+EvI=; b=nRV8YMNG5Df8DaItSZUOuesve4Nu+rpE5JL0uPom5TQ2sNMtpzL99r46Fud8l6wFlk12pD 5M9jFhrJwSYuc5z9Tw8Kjh5kkCCERX2FHtaw2Xn3j/AlOuua2MLzJFfLDpvnf9B3M7gXo6 5VxsNhf8ADisTcDWLKYZ8P6auqokCwM= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jEaizIQV; spf=pass (imf30.hostedemail.com: domain of fengwei.yin@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=fengwei.yin@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1675692516; a=rsa-sha256; cv=none; b=mGElC2alDIPTgxsnzyPb2WBkZ0786XWIYZW/9QPBlydAxSYzypxyp26R6mem6l02gelXIi lTsyxBQflSrpJELWWLKZE10xZCVT0ab4BGoKw/1BSbCQRYFvU3OWjb17bBT8yQ2yTsEzi0 gBBnjZmD5hVgQ0h1HGhNSMKuOTWA3WI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675692516; x=1707228516; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RGeWKyZa2H852sBbJ7Gc/HeCa7JW/tXb4rXyzwaxNk8=; b=jEaizIQVRak6INr1Roanc4HnTrkRb9o0JeiDKBM6UATVmAxnk8WtSo+z /BXuOFL95W6ZfuA+VCsNe4NhvoHcS57pfotW6GswwGnSVgyJ51NRb61hV 5lz0xEZsPH4g2V+j1WllPg9CMi1eYTZ0vaeaCFzgkZTJJIHwppHREDqH9 DHKNhKcOeuXjA8Xnmbbgrx1OSjl8lStoaKPiORL7uoopicPZlCPOFo0NB upAhaup9QQkhmxt5zwVFsjYuugvFcytYsl8xMCfpoAsZYy71FVXcX+T0+ Gda7B9KwEoVSs7l66xBMsb6xTUepxxWtEYKyM5gKtETNcidiCv9LcmAUo A==; X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="312864275" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="312864275" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 06 Feb 2023 06:04:53 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10612"; a="911937871" X-IronPort-AV: E=Sophos;i="5.97,276,1669104000"; d="scan'208";a="911937871" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga006.fm.intel.com with ESMTP; 06 Feb 2023 06:04:47 -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 v4 2/4] rmap: add folio_add_file_rmap_range() Date: Mon, 6 Feb 2023 22:06:37 +0800 Message-Id: <20230206140639.538867-3-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230206140639.538867-1-fengwei.yin@intel.com> References: <20230206140639.538867-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: hjy6zzsttx1bhdcwap1zxdt9dbouapcn X-Rspamd-Queue-Id: 7533780002 X-HE-Tag: 1675692516-99810 X-HE-Meta: U2FsdGVkX19ntxj2PU1bmv888vJ8DFAD7ODLtRebQ5cZY15bxyzzik2PO0zTeaznvs8CYD7fGtI+7foOZj9tAr6tllSR1i36eAx8dLTVgPTFY4wlJfFlbGTD9UIZeGzaJDXYL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H1jR+7WVTko0xvwyidQx7P1UL7E1f 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: folio_add_file_rmap_range() allows to add pte mapping to a specific range of file folio. Comparing to 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..974124b41fee 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 folio_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 8287f2cc327d..c07c4eef3df2 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 + * folio_add_file_rmap_range - add pte mapping to page range of a folio + * @folio: The folio to add the mapping to + * @start: The first page number in folio + * @nr_pages: The number of pages which will be mapped * @vma: the vm area in which the mapping is added * @compound: charge the page as compound or small page * + * The page range of folio is defined by [first_page, first_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 folio_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_pmdmapped = 0, first; + int nr = 0; - 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); + + folio_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