From patchwork Fri Feb 3 13:16:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yin Fengwei X-Patchwork-Id: 13127503 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 C557AC636D4 for ; Fri, 3 Feb 2023 13:14:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0206D6B0078; Fri, 3 Feb 2023 08:14:52 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F129E6B007B; Fri, 3 Feb 2023 08:14:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB2716B007D; Fri, 3 Feb 2023 08:14:51 -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 CA4766B0078 for ; Fri, 3 Feb 2023 08:14:51 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 97C18AB7D8 for ; Fri, 3 Feb 2023 13:14:51 +0000 (UTC) X-FDA: 80426025582.13.9E576FA Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by imf23.hostedemail.com (Postfix) with ESMTP id 64D9F14001D for ; Fri, 3 Feb 2023 13:14:49 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GA45Jrjm; spf=pass (imf23.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 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=1675430089; 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=NEgBocWJZ6EgKIGVcRxRsqUmjXNgdcctNcLq4HpzGtA=; b=Hw9BlQBqKGVF83t0FnIkUOiYTS95gGUpkvwq0SdGUTHPypF6Qr0nJbJ1P1/kHkaWMwBvVf F5M1T/M2bt9b0BEEXCZhP89+uS7FSXWAszVFt6PtRooEMdigwjdWXPHkIoXG9KzbP8/c/d ixdjIr/DJkQPyiBXx7acToaZSoHt3mo= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=GA45Jrjm; spf=pass (imf23.hostedemail.com: domain of fengwei.yin@intel.com designates 192.55.52.93 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=1675430089; a=rsa-sha256; cv=none; b=PxdM0JOZ5SFHw+nb5eTWzPqgp0EYaPYM7A4ubn4S+Wj1nlHhcGI7zpGzIgAVYv4/aVg4Jx jGMu4a/uWwbzmckmdelFqKxCCs8HOqlZDWwT2yO/aIai2HnMYm8HjrjnajYR+wMJB0aDeb MWarbF7UFKilYR+BPV+KJljmNz6UAyc= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675430089; x=1706966089; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=d9C+zDfVUIiVtgWbOtTmri9i/MfcV8BTMrFKykwN2Wo=; b=GA45JrjmEisvzRzXMGOJVAI6IOJM6rhhxE5jy6jrXme/FXYvlJe3IY9f LT4MBbAW2OWw5lpF/cD219Sy4HGiy0N8dcgl9nagP7W7smi++gCYjkfDc FcSMoJJi0M6CkS2+HWxLLHUCaqrAOuf+NWbnYWLJiduysIMHbjsNHhyNd 1QM4HOL+XAU+Br9deIZhgS2pL4IdHBiOo7890h4+t63QyvZl6vQm9Z1o9 uykXVn+4nmX31voGQLlVCken5y18+8qZl36iZBnI7nMxHMGqT1ANgKeZp 8due8Bq+MHwCyndEgXRBSjVlX/+Hm+egKwtqHX9KPcfaWt2fsTpAWjxWG w==; X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="326441888" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="326441888" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Feb 2023 05:14:48 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10609"; a="667641946" X-IronPort-AV: E=Sophos;i="5.97,270,1669104000"; d="scan'208";a="667641946" Received: from fyin-dev.sh.intel.com ([10.239.159.32]) by fmsmga007.fm.intel.com with ESMTP; 03 Feb 2023 05:14:46 -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 v3 2/4] rmap: add folio_add_file_rmap_range() Date: Fri, 3 Feb 2023 21:16:34 +0800 Message-Id: <20230203131636.1648662-3-fengwei.yin@intel.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230203131636.1648662-1-fengwei.yin@intel.com> References: <20230203131636.1648662-1-fengwei.yin@intel.com> MIME-Version: 1.0 X-Stat-Signature: s9o9wsgbotbmhdsmcxnw8p3tqiunk8zd X-Rspam-User: X-Rspamd-Queue-Id: 64D9F14001D X-Rspamd-Server: rspam06 X-HE-Tag: 1675430089-502871 X-HE-Meta: U2FsdGVkX19UDRCJRppFn/clwXqAoKP/HYOamS4bov0nzTpsH/5wvVlrtirud07EW30maOlyFOP2jP38DtcZot430w6yEvThvRQOQFahWNEkTn/7htUCBbXaga0NMXXA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rIZGUr32qLPKVP/HOkB+2OuUtP9Gl/3zL7zbDCQ+fpABcbxnKEynNPfgJRG93U 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..f8861d832468 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 + * flio_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 page index in folio + * @nr_pages: The number of pages for pte mapping adding * @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