From patchwork Wed Aug 2 15:14:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13338368 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 06493C04E69 for ; Wed, 2 Aug 2023 15:15:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8E372801AF; Wed, 2 Aug 2023 11:14:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B3B432801AA; Wed, 2 Aug 2023 11:14:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A03BE2801AF; Wed, 2 Aug 2023 11:14:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 8E0F82801AA for ; Wed, 2 Aug 2023 11:14:32 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 370791C958F for ; Wed, 2 Aug 2023 15:14:32 +0000 (UTC) X-FDA: 81079511184.13.7C34475 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf21.hostedemail.com (Postfix) with ESMTP id 65FE41C000D for ; Wed, 2 Aug 2023 15:14:30 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=SdbjeDHf; spf=none (imf21.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690989270; 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=TdcKo7vjdKwufue4ESIqNQj9yuANHynm6n4NF3jSmIs=; b=FeGdFz+89ORV9yu3df8IbLaAT3e0c/FeQJ0eNj/2Wm+x8TgQc3ZNvIclaPoJ5Bpj9omCN0 9B1bTeYghHKii8z7TzsOR6XpprOObqHeKcSeiiSLwZbNUVGaXL2/IBBpsyqC0EHuzTmqjF ZJn7xicq4aCTRVBvdOzAOQxpnYPE350= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690989270; a=rsa-sha256; cv=none; b=nkGrbXviACV/Vb8SV561TAgAeuxWA/SFoVSDwuSHHuwOsFwNLjg77oCZcGhS1hqOq0ElLe yQwhzNU9Q0rQxoTqRT1AajioAZkZWTVZSRBE5asGFZ7sj46mxmDoz/hswC3MnmCBDtb3Zq /fRNyy9AtEq0DIIOvvmpUrjfHcy5PqQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=SdbjeDHf; spf=none (imf21.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=TdcKo7vjdKwufue4ESIqNQj9yuANHynm6n4NF3jSmIs=; b=SdbjeDHfWjg/0JpcsditjtFvSL +oRaeZWmIDyYutyL9Gdc3vUbtE23PF7hBQPZprJsUfB4z2AMI0hOuJM1R5kNC1fyRVigtWfuFICUf CGC2eJlys7pSUbvJn1JjsJK2zPgl+eBqYR/lyb1NmtWqki9j7JqhUk3jCaUxPft1yXk2b+a4jIxmC 3rzEiLHv4ylPnOD7wrbt2t2oj53l3VawaTFdF/zHL4o/t70NQE6zMXBACFrvYhgBe97tF60xtgZeW +O/Mo6O2TcLONuhW1WmIu4mE+Z3UFgUR7me8a/2rnZG6J+edXKvURdrZ07h5xju2n2/w0ZQdHfpW2 Ix0vV5xw==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qRDYC-00Fflr-1a; Wed, 02 Aug 2023 15:14:12 +0000 From: "Matthew Wilcox (Oracle)" To: Andrew Morton Cc: Yin Fengwei , linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Matthew Wilcox Subject: [PATCH v6 35/38] rmap: add folio_add_file_rmap_range() Date: Wed, 2 Aug 2023 16:14:03 +0100 Message-Id: <20230802151406.3735276-36-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230802151406.3735276-1-willy@infradead.org> References: <20230802151406.3735276-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 65FE41C000D X-Rspam-User: X-Stat-Signature: kdcbwguig44698m8p6uehmgbyh1e73yu X-Rspamd-Server: rspam03 X-HE-Tag: 1690989270-139456 X-HE-Meta: U2FsdGVkX1/JEPtr6ZpWA4kDXlW+oN5BBNX4KRPuJKWHuU38DVyBOOzATmz6Cc//1Fz20rpmmK4s3hLxSsZ/IBZF9X/SpK9gz5bBsWhD0Q1Bbv1UqTwiHmXb2CCO0CCQPqAlLd4G8jxTCbL3gq+x7TQpfhbRoMHjpr5T2+tOTW3IB3HQo8OqUQGUN0+jABVqN9qVq7n58Rucn7AO7/ohnLDARbOnag2SXI4KYHTn8dfGzlMmghshcK+NFwWgLBp3E3DitSYNFezjWFzLDVfl9j4rIyFS5LAOH8HfblXzCRH+0DbAYHp42crlP2sh7xKj3JNbSrVfSWqCTmuGaO8sH6aNzucmAuwvQe+5Z0OjayLSbL/Umc9U5t+z/ZTpFTRSjC9XJBevVSxdggJaue+Ozi5ncALujf4wONoZ38x8C5k5b6TOYBwB0sWZwvhobCLm4Bk4QlOAC6du3E2VgabUkN4Yjg3UJtWePOqgdtn6r7rHh5Jko4B2Y9a0hxjeIqBlc2m7daow+Iqm8iVXNfcp2JFmaR5pe/1T9brIVuM1JUNC+5US+3LCI0M+mOIMReYVPOLabEeq5jRM4xbz/OjUhkS4fVMr8bFrZ4IymqdoyDZoPTyLoMexd6EKE+dN8IJD5B9wZ6TuPeOLg4mvnUqEKtypX0vMrF/gYFhVWaUHccI8dGJVMJPebE5yoInxbMZPnDC3ejwxRr1zwYYbQkCEUBbAdlonWY88CHInq5BOcOzzS37WnVnWGD8heRxNpYYGPgWlnTaXuu5l2ygXLM+QIf8APbBahk3nHpUJRecDmx+u6cxyhavvYFOF8eSg5SsAcMB8goGbMECVkZxyvdpPMdly5KTqDTLjtkM4cuoLDWEGC5wLDLdoMNQ/vI8YLXyuBvf4zeMqNfRzemsg5hFPNcoWIFJ+kkwKbXcbCX4VsgrX2ZjMdSnQQ9YSDtAgAqDM0p3qH3z9qx4xgx22PBc 3KzB1az5 YtL9doLD3TDbwADJOZwdYC20+weLVjDAU8zUKDWojDimlgwCtyPYMD0cqTq2A6dkwFMsrx/QgGyZvAitwmAusQFEqG+JZzVVKrKWSqITBZbtcntdULqmJen4CoLzqpE3nrKX7x9u31vtH/K2sqfMb263sb/hpBBoZCsY+pf925/cNbfvaRcvQKzYpIiIffdUlKWp4ss4/bzQ7kunXtwJBFI/htniE8dtRsiwvOdsIIdb7yzPYtzNZD1vdIw== 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: From: Yin Fengwei 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 Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/rmap.h | 2 ++ mm/rmap.c | 60 +++++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index f578975c12c0..d442d1e5425d 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 *, struct page *, unsigned int nr, + struct vm_area_struct *, bool compound); void page_remove_rmap(struct page *, struct vm_area_struct *, bool compound); void folio_remove_rmap_range(struct folio *folio, struct page *page, diff --git a/mm/rmap.c b/mm/rmap.c index 54124f18e0e4..d82d52ebf3a6 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1294,31 +1294,39 @@ 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 + * @page: The first page to add + * @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, struct page *page, + 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); - } + 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 (folio_test_pmd_mappable(folio)) { /* That test is redundant: it's for safety or to optimize out */ @@ -1347,6 +1355,30 @@ 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, page, nr_pages, vma, compound); +} + /** * __remove_rmap_finish - common operations when taking down a mapping. * @folio: Folio containing all pages taken down.