From mboxrd@z Thu Jan 1 00:00:00 1970 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 D5E9EC0015E for ; Mon, 17 Jul 2023 14:31:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 725516B0078; Mon, 17 Jul 2023 10:31:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6D5B16B007B; Mon, 17 Jul 2023 10:31:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5C5186B007D; Mon, 17 Jul 2023 10:31:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 4E8936B0078 for ; Mon, 17 Jul 2023 10:31:46 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DDD6D1603C8 for ; Mon, 17 Jul 2023 14:31:29 +0000 (UTC) X-FDA: 81021341898.03.844A19C Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf19.hostedemail.com (Postfix) with ESMTP id 174691A0027 for ; Mon, 17 Jul 2023 14:31:25 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689604286; 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; bh=GdameDER4NHHlwMn42ZID5qyKD9OC9rWI1a9WDRPBIY=; b=BfdtE9ePB+zUHy6zM5u+KqSyDHX4mTeZ31012VT6mvQqvM42ae+OANe61e9jHRFYA7gDtA UsVACA2DnkT5Jtua4OTz4XP57AhHeoR+4zswQnJAAwmkgjOTPP6AJYOi4+TrkbG9PXqlXb PTFQbvAqtSTs4ZOfSTsLY1Fp/1Pr3Ec= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689604286; a=rsa-sha256; cv=none; b=uftuYqTxGQvzQv8O27n1StUMGntID0UkPkuGmmiInrq6f3qm/nfnOHqqWmiULL0sXo68LD N1KXtcGDEsHKw3XFfEAUxsqdVKXx968IFqw41kcpF9Qtb2mphkO4cgYXeUpxR69Bzs+XzL CKBztHwezT1flFQZFrOAZNIc4sDf7/o= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 642BD11FB; Mon, 17 Jul 2023 07:32:08 -0700 (PDT) Received: from e125769.cambridge.arm.com (e125769.cambridge.arm.com [10.1.196.26]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 749663F738; Mon, 17 Jul 2023 07:31:23 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , Matthew Wilcox , Yin Fengwei , David Hildenbrand , Yu Zhao , Yang Shi , "Huang, Ying" , Zi Yan Cc: Ryan Roberts , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range() Date: Mon, 17 Jul 2023 15:31:09 +0100 Message-Id: <20230717143110.260162-3-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230717143110.260162-1-ryan.roberts@arm.com> References: <20230717143110.260162-1-ryan.roberts@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 174691A0027 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: ea9q457zn6sherx7u3whq51nb5bxgg8r X-HE-Tag: 1689604285-668812 X-HE-Meta: U2FsdGVkX1/snUDu3ojKfEexTp5B5XDWO42au9MRBwI/ohw3l7e0BnunPAoaETx2jLlYjQJPdoVr+ZcDTw0Y6RsNRgXpvg3DEQ1bDmyy+4MyIp6osO15UVD1Ha2DsumY1RedN9XTLdLkG4NhjeERQTBo6sEcnKxv2/mPCKeuJDCGvAuT57HtKHEQisp/RDW8LancDn4NAomyrtPIIOXwW6r4CIzS/28qYSTrhJnb1iN8CMPEM64Tv2oPp6xvYNC2YHDR1PbdHEghXpFCCP6bY8i7wZsl+MDuyeCdwiY04VDjG5GVf6+XctL0Kuq/vxTgY6D3z4cJQtVowlew/Htb/a/a78l+q2sLSNAEyK4M2FaNpxUpWwxlkC3bQu/TOlaDTxSofgrNDB4CzsflYmXtTfix2MxKvDGfqWa93qOgcrCcCUA+3e26nBJNDl5r9hu7lE9OpESKjFASlFzqvUAiJFtFCqO0KYAlrh8031s0hw/9OJiV+D6IPIP/zweSxUW5QeaU+GZ36piaQdKk9UaRvXGz+AZ80d9mqsqmdO49QV1bd6W3GpjYwHmKh+MSzyEHq0HPJBb96M1XW6JzTCOKokWpd85TrMQaPm5/uIksgPAhyaB/yR3k40xzQFlkk25i4aE0PK9IgqCSdgG6l97cjUhfKxZesRKRI8vjytls2QrW3TkeBBonzemnRryWY7rjOBd15j96OnUReRrDXUfDc2YWuwKkmdyjAPRoQ1/9kGTGSLrCD+f+jpmWXmh9PW364dsmew9gnC8sf5XnY7qyqPPn7U2seo4vRHrO4QJrLna3mtddmElTMOjzWu9sQwMcIcCa7q4DZ2qjWzKCSYHJ5PWrvedVCIK73iv75miOcpOuza6RI/Ohm9vQKAQedgV3pGdjA8HOgRQU5M2Es9/OCjA9CR3toBDxalC4Y6sWIUmcMJi0ElLVO/m0ZdIt4JG5n/RgI4fr2RFEaTxhhEh dwDmCfVc E/c4CmZNbLRgX5gcNFyFqxmbc8JOgFB+wQHx+fQ2BxMglbHPpjY5EsbxuQegUrJKnC+fXu7T+chw7LOtcrO5XP2OMnIDohxHnXXeRWPbBGhm99oL28W2hZlVghj2k6Zk5cNvm216Ni0VahonZbQXOz+b8IjOeh3sDROwdzeEkI6fZ7/QUZJ4otcMWRB2PUt6VunQxhixGH2e5AcA= 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: Like page_remove_rmap() but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock. All contained pages are accounted using the order-0 folio (or base page) scheme. Signed-off-by: Ryan Roberts --- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/linux/rmap.h b/include/linux/rmap.h index b87d01660412..f578975c12c0 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, 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, + int nr, struct vm_area_struct *vma); void hugepage_add_anon_rmap(struct page *, struct vm_area_struct *, unsigned long address, rmap_t flags); diff --git a/mm/rmap.c b/mm/rmap.c index 2baf57d65c23..1da05aca2bb1 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, mlock_vma_folio(folio, vma, compound); } +/* + * folio_remove_rmap_range - take down pte mappings from a range of pages + * belonging to a folio. All pages are accounted as small pages. + * @folio: folio that all pages belong to + * @page: first page in range to remove mapping from + * @nr: number of pages in range to remove mapping from + * @vma: the vm area from which the mapping is removed + * + * The caller needs to hold the pte lock. + */ +void folio_remove_rmap_range(struct folio *folio, struct page *page, + int nr, struct vm_area_struct *vma) +{ + atomic_t *mapped = &folio->_nr_pages_mapped; + int nr_unmapped = 0; + int nr_mapped; + bool last; + enum node_stat_item idx; + + if (unlikely(folio_test_hugetlb(folio))) { + VM_WARN_ON_FOLIO(1, folio); + return; + } + + if (!folio_test_large(folio)) { + /* Is this the page's last map to be removed? */ + last = atomic_add_negative(-1, &page->_mapcount); + nr_unmapped = last; + } else { + for (; nr != 0; nr--, page++) { + /* Is this the page's last map to be removed? */ + last = atomic_add_negative(-1, &page->_mapcount); + if (last) { + /* Page still mapped if folio mapped entirely */ + nr_mapped = atomic_dec_return_relaxed(mapped); + if (nr_mapped < COMPOUND_MAPPED) + nr_unmapped++; + } + } + } + + if (nr_unmapped) { + idx = folio_test_anon(folio) ? NR_ANON_MAPPED : NR_FILE_MAPPED; + __lruvec_stat_mod_folio(folio, idx, -nr_unmapped); + + /* + * Queue anon THP for deferred split if we have just unmapped at + * least 1 page, while at least 1 page remains mapped. + */ + if (folio_test_large(folio) && folio_test_anon(folio)) + if (nr_mapped) + deferred_split_folio(folio); + } + + /* + * It would be tidy to reset folio_test_anon mapping when fully + * unmapped, but that might overwrite a racing page_add_anon_rmap + * which increments mapcount after us but sets mapping before us: + * so leave the reset to free_pages_prepare, and remember that + * it's only reliable while mapped. + */ + + munlock_vma_folio(folio, vma, false); +} + /** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from -- 2.25.1