From patchwork Mon Jul 10 20:43:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13307633 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 EFECAEB64D9 for ; Mon, 10 Jul 2023 20:44:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C6678E0023; Mon, 10 Jul 2023 16:44:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 077AE8E001B; Mon, 10 Jul 2023 16:44:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E80AA8E0023; Mon, 10 Jul 2023 16:44:07 -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 CA00F8E001B for ; Mon, 10 Jul 2023 16:44:07 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A0ADA1A017E for ; Mon, 10 Jul 2023 20:44:07 +0000 (UTC) X-FDA: 80996879334.08.030C017 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf09.hostedemail.com (Postfix) with ESMTP id DE53614000F for ; Mon, 10 Jul 2023 20:44:05 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=ByTAb8QL; dmarc=none; spf=none (imf09.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689021846; 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=tse6c+PyGQiNNj587rPbXI3VeCMysTQLdbxkBMTFw2U=; b=RrryZoFxizFhTptBKE/afLEoPYzy/0lGXAgNMf2UW6BpD/oq+IVnTuBWqWeV+VA17mixv6 mQ8ThNJIycDvgsHgD2wCcQuOeaQS7FOa0fGqAcyH/ltSzMeZP0s70xv6MBk1EsUXgloWuc pt98HUqWaWrRl5oSmvwVe5gcdY+3K7E= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=ByTAb8QL; dmarc=none; spf=none (imf09.hostedemail.com: domain of willy@infradead.org has no SPF policy when checking 90.155.50.34) smtp.mailfrom=willy@infradead.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689021846; a=rsa-sha256; cv=none; b=l4xIZ4Ci9pbqbeZlobG9FRXdSxNtHHql47eh7SrOvuulCyMElatI8263bFX0uTcWQwaYG3 164T+mTJuevNsQCf7BGyj1ZNHZLQ023W21sjH+PmFowTnx6HNYyY+ReQ5wibRYuAp8xFi7 BUQx085LN5yQlhOL4WXC+yE6LAycOLw= 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=tse6c+PyGQiNNj587rPbXI3VeCMysTQLdbxkBMTFw2U=; b=ByTAb8QLCEfj5LUw8NRweblA0g IWcJ9mcvelPbcUNEc/gRgpW6n4ttTTktmyu3V7EmrnW0an+1JIqhmeCeyRQBWmDPJuFHUwjzFDWpo XAB1S/E03sxX4yFKmTZaX+54W5iyrUP3Co8NFiKEQfVqMdDBwutG450DZAYzgF4pCRF6XxL4ZmNwA N9WKG1uQmnB8U8rygg5WlNYdy5feYokVBgqd+1DtKuYcoWmWh+pL3pdz30uR+RgEeTguP6cjlmefw 71UTFiAkQLzksnwO8aPUFBJy4ZdRrZcfBsOz5IlsZVF5NRmRO55SF3NP3Vg1E93vlS+J6o3/PcAqu a09WvaTg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qIxjV-00EurO-7T; Mon, 10 Jul 2023 20:43:45 +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 v5 35/38] rmap: add folio_add_file_rmap_range() Date: Mon, 10 Jul 2023 21:43:36 +0100 Message-Id: <20230710204339.3554919-36-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20230710204339.3554919-1-willy@infradead.org> References: <20230710204339.3554919-1-willy@infradead.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: DE53614000F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: pynsmr3usbe34yjobq3q3uzcze8sakc8 X-HE-Tag: 1689021845-638320 X-HE-Meta: U2FsdGVkX1+sDxzPFbSjMP1kZENSUPo4VlQLAXKKfGmZQIfWAyALIV23DN9eDstGvhE8c1659ZNddJ7TGihNW+Ofy1zlj/tzdoCpFAwsamuWjPLhPfzZnA1NhM5BwKzFhyR+n0dsCtjy8MaxRkGdFsY9M7sB9uyHUs8diWq2LNWAB8dtkAXArKEYdmemI8txIJZJxt42F3J3Y4as+UgjtpWd/mbeI4UpyLTM0oM+4iZPfvl/9pohsxeMNpkHsXpGYYidTLFafu4KFqysLYhGagerJI8Iwzz2yTGTValjSxXltq2sgQcg/WjKz7T/IcKvDV6F5R0yver8UEiTuQ1Sb8KFFSPzaiwgwf2BNVAy4BPfYORoO686IixU4+pzHSl2uEGcDO6erMoFBs+3WiDWcQlQAChVWHPzFCQRM0n6Y6Aq5wQu/bsnQlIk45y4wzmTzx0fWwo9nVjIYgBs5nJ2zmeqEc/z65a+D85UmNnfZ3DFnZIIEW9HOE4a27o4ERwL5/28ASTn5Cz6BxKGzHGJfMp48s1JRmSXnk8Wz3krqUV2n0FNFHSABbrCMArvowSTY12sqBcPOt5LKzjUmFlX/OlWjMh6yRJYsaBtJ/kzCKyDi+HSlguC9eeB0biVr492YuDlT7ONXo9KDrFLEZEN56DnyjB1Mz6PfVIPARwNDwutwkO7WngAzxPulgNbxNSenULnsLgRG3EtNCwAY9Y9mqpTlCcx3oxU37jffDNZlU+JOGly1er3VCU3nbttmARCkx/6niTegPFm+iLikEhOO5lD4GfvS4ADF/qD4E+/byiE/9YvkHagv18bLWMdxjfctU1XPf95+ri5hgpzUuv6ybfnWCJK221JnQTnS/j83uOoj1efVnPl599+vNNl5Oajb8rrEKreQt0nS10fDr3XyXXkrdQEtbuxZWy5s0RAvAWypbSwFB6yORdHEWp9pyLHgv27snXoPRYSV6dKK+i Q/ZFYPul Hy3afNb40QdZxY2c4sTRHZTfxShKnNFsveaPHBVpeuFMRZCrfja73vnnb9WFfPoN6ckPghmQ8WgvPVPF9MHYzYQHDlqb/dO7F2ILiHTxxOtVeHmATnMVt10hpmnz3ZLczgbsSHv8xyRaj3a12FAU8FNlBf7/3Cn9Rt7VeRVJ0KNWDpK5axF8N8otMEF64X2ojHES+Oo2Ldm59bctZLdppx8TiJXw1Ra+EX5rhDBFGjKe4jmbuNp9ml4mnUg== 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 b87d01660412..a3825ce81102 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); diff --git a/mm/rmap.c b/mm/rmap.c index 2668f5ea3534..642b3ad8bb1a 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1306,31 +1306,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 */ @@ -1359,6 +1367,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); +} + /** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from