From patchwork Mon Jul 17 14:31:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13315834 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 9E002EB64DC for ; Mon, 17 Jul 2023 14:31:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0E4076B0072; Mon, 17 Jul 2023 10:31:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 093FA6B0074; Mon, 17 Jul 2023 10:31:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC4728D0001; Mon, 17 Jul 2023 10:31:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E10416B0072 for ; Mon, 17 Jul 2023 10:31:29 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A0F11120395 for ; Mon, 17 Jul 2023 14:31:29 +0000 (UTC) X-FDA: 81021341898.03.EE8148D Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf20.hostedemail.com (Postfix) with ESMTP id 442E61C0033 for ; Mon, 17 Jul 2023 14:31:23 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1689604284; 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=BP+7rStUqlpLuRykLENwCQmjiIT3a8ADMh8sshpsKms=; b=IrUK1hEJPFGyYBoEFb343ormwFP+iXz/BzMs6mJkOiyJy1MmDwTzUdQXyP5EB3H2/qxyD1 28s/ZIbZ7hcKF8YLslhrWynuaoa8eBULXq+4jPqokuhc3P/wS6TRSNZ/3Da/a+lORfhecb d1WKOYnvn44rKNkX54zA3/jOkyMqy6Y= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com; dmarc=pass (policy=none) header.from=arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1689604284; a=rsa-sha256; cv=none; b=3x+EypsW1fkYdIF0aH05oi+x0J49ajgRlLXZwI8DyBCh9AhH7om0VqgLamvWDFXnkCHVe9 r/Jno4wM8wnSgkEVEsfaa3CRP+udD0WGiKxYm4YeqhO+VbUfiuwHUjwrsCwu+T6Y34pRbG WUoeQ2DJmeWiXmHUcbiQi14X8WnI68E= 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 8D926D75; Mon, 17 Jul 2023 07:32:06 -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 9E0313F738; Mon, 17 Jul 2023 07:31:21 -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 1/3] mm: Allow deferred splitting of arbitrary large anon folios Date: Mon, 17 Jul 2023 15:31:08 +0100 Message-Id: <20230717143110.260162-2-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 X-Rspamd-Queue-Id: 442E61C0033 X-Rspam-User: X-Stat-Signature: umunetsyzjmunwnoqtmx9uabu5gqrmws X-Rspamd-Server: rspam01 X-HE-Tag: 1689604283-107920 X-HE-Meta: U2FsdGVkX1+HuT4GAqXEOQz5fPN/ITYkV3tRIbtzHo2jt+hgJZpqa+XNDkLlViAM/sLM6z2IJ91bY7DtvIoxuNX2zJwqdjplUr/8Or/GkcX/zt8ghjtCk4+L1UZUPrb2CBKSt03+GfgLgAHJiF2upucF28PwqJBCqzecE/L5xVyDp3cY1ZHYUazIpYMbjK/O9hdJjzDMikdWWiTR1gWA/YOR7hWcpC+QKG07ZmGargFoQqpuQu1FeERbYfVHMHrAtCQA3ePI5fRO11fHsO4NUK/5YkiIX1jsFSg/NWssZBzqm2dL9vD561OaEPGCKma7U5ZDJ850DjMxziFOfpc7YWV7GeTEhWkEx/ro0lB70huKpNBK8hN6csTWHvBLq7rCzrYJSGca3PsOWFR62Irmvc0HoBZ/yOtpLpF50w1RddyM2tamf4/yl+GSj4VLKq2fxx1CUztr/Ot3hAoGSNW7gSCk/miIcx5TnnUImqmSklw0LtLBoaHFO2vKp9xExPq3ax/nqvIX9/zUNBF6cMTjLcaiAawkxD77YSJF73bWQ94ufvGEJ5qDaYepmAeoXqs9jEBIhpDDoMAzcec+PBc4s3EeCWHnFc2bb2Ae7DlrJRjabqlvPrK/ZlgH5QP1iE1Fxe+beZTKSB//5MjTcWjEDN6qxY0Kr73g76sp56B/hA+aLbYHDS5vCArJuPTuJmiK60ia3w69DpBqYl4KQIzpV5af2klM+HRqrenzxghJXFq2Gdo8aJsPN/6xWI+yVbPa7vm2K3AgoBNfVGnEAcHwDwe23S3KAyodAgJU3CZmUqoT4V9nfis8jmH6FPjT7WjXqpgk2aktIm+AZXXe7PhI5r5HmhGm4biLM89yyOoQhsEze6nb1i5UEd+GPWhc2jmUoo8VafTkZAClO4ApyeWHliM0/PB0eV3aa+c3uug4yDNj8P2CFvavrJw+MFw19AbI1mBybXyolhPxCcUGoh0 Io8tfiLn vfrxbzMjE5CdKnxA2VLixhCC2GlWnGrMFHy/TKR/NrnSrIHns8jot2QTlRWkdr6FCilD+eACYPbtnmWZROrUSR5JYdZqfxLhUO/sRvVndfbSr7eibgWmWFPJCMQGiRb41r+ZNcDUhr1Dis4EhMFNOwpx3/RLRoAGJrk/n1EGAmnMuxNDlj8FwRWx9p6+aPzKTfZr3tlZazHENMUc= 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: In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized. Signed-off-by: Ryan Roberts Reviewed-by: Yu Zhao Reviewed-by: Yin Fengwei Reviewed-by: Matthew Wilcox (Oracle) Reviewed-by: David Hildenbrand --- mm/rmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/rmap.c b/mm/rmap.c index 0c0d8857dfce..2baf57d65c23 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, struct vm_area_struct *vma, * page of the folio is unmapped and at least one page * is still mapped. */ - if (folio_test_pmd_mappable(folio) && folio_test_anon(folio)) + if (folio_test_large(folio) && folio_test_anon(folio)) if (!compound || nr < nr_pmdmapped) deferred_split_folio(folio); } From patchwork Mon Jul 17 14:31:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13315837 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 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 Reviewed-by: Yin Fengwei --- 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 From patchwork Mon Jul 17 14:31:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13315835 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 B0B19EB64DC for ; Mon, 17 Jul 2023 14:31:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1CE156B0074; Mon, 17 Jul 2023 10:31:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 17E8F6B0075; Mon, 17 Jul 2023 10:31:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 046828D0001; Mon, 17 Jul 2023 10:31:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EA18A6B0074 for ; Mon, 17 Jul 2023 10:31:33 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B19C6B0BE4 for ; Mon, 17 Jul 2023 14:31:33 +0000 (UTC) X-FDA: 81021342066.05.2B4ACC9 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf08.hostedemail.com (Postfix) with ESMTP id E2FF916003B for ; Mon, 17 Jul 2023 14:31:27 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf08.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=1689604288; 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=Z9tyZhKLEUiT4Cu9fZro8vsV1H+EfHsCasdo1zxwtLo=; b=mg47KjXQ1SU/TtJuPJRlim+w66JwSaS/HUqtvU47QmDSs9Rttw4awZ641V0emwzLT6F+JM do6xSbUFgpkP/gEf8xIGi+t0NTqdqJ7o+zS4P1rUmL8fGITwy+VjavH3GTi3seKy8skeLc VhoDXBz6z+7SLeXqcjxo3wK0XIL1LCM= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf08.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=1689604288; a=rsa-sha256; cv=none; b=w//DtUIooTm3oef/mC1UpV1eMsP+fjsalMsZR+2AQ8mn3KOGUH+H7HLVS5thoSMYI3p3vV +0RNarM9n5O3Tz79OBDCAf+qrSclF8axhEebsKIYL0zH715NOWYJ6TeaoYSHN9X5GqW/xu tM11ZWM/8tEpwdAZpUY6RAEfi1OPFJ8= 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 3A3BB1424; Mon, 17 Jul 2023 07:32:10 -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 4B2FC3F738; Mon, 17 Jul 2023 07:31:25 -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 3/3] mm: Batch-zap large anonymous folio PTE mappings Date: Mon, 17 Jul 2023 15:31:10 +0100 Message-Id: <20230717143110.260162-4-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 X-Rspamd-Queue-Id: E2FF916003B X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 1x6ioxs1eakyesuue85yowk3rpp4eg4e X-HE-Tag: 1689604287-910088 X-HE-Meta: U2FsdGVkX1+Tr7Y/TNOZo8DVPM9RcnGTdeJpfZovL37RGOmAj72RATn+cyXDG/oUxB/8XtBJH8+fB3NuN8BiXjYaZV6zfROSSJmXeoFyB4IngkIRMVJkpfWTu4fvSV+Gu6q3xRrFbwAS/OYCSs5Z0UBfmvoz2g04KOdXP3HSsET42PI09U6q8X+lrafSW8M96O5gVM5YaNzPgMV/RZMAkjgvQ+7HuJQfNdk3sO8VluUnGUHqXRpXJKOTTCEzX6/7aGF+KUmH7R9P8KH/hDrxCVS6X2dpxOzZNpDZ4MrSzx+bYEb+dpBkO/cY5xYR/jpxj+nz4rbW9dixpUB94x1bjpzTO4xaZgli5KAyDeiZ8SDor4Q2QoiK8w3BuQUZ4bgMw+GvF2ArXVABmfe+UzznZ/uh8VezX0XiACiG7Zq3/8GUZW7hOAmMIl8kQRZwdDxCPZoXmmipAkfv1vtnF501Z64c00hdnAYGKdrAAaOVKlNjZR0ViwqlibCHL+EGQHDxGejJLmdb/CGjEw9rZ51FyIHnxkaIsCG4qKmXUtViM0XfPUIpEpxSRyMto5mh6mxw8CV4f+RoOkpHcmFOg47ZDRZ1IxEOifaTX1uuzy9Q65CaJDGA2uT1mdWNQhQQC9AsraKuadTheMPma4QvaxOrC3ztyK0Nt82LzqMSVwcENRAd51+tLRlXI4w3zmysvp5/i5wKsWeSDVAI9watFgpxeGr2V/K8c8Uh8uKiXxdVMPOar9cT/qCaCrxDaoCdF01OkTZpUKXtehDXyac8ngGqtJNVwCQLL/ZDIHOWcpUDqdl+zx+bjUWMJYKnbXTxGjad1D3O/yfoF+WkW4WH1ljy5oa/oexoIB/4wphLnkBP8T8toa20h9Nee5yp9zC9LNkNXUsRXwGdXsE1mmuxVMs+fmuoSl5XrncsGENSzD5mNK1sOpn/29I6bsfpFvXk5rEPN8EyeFf1m0ZbS0virMZ 62Q5vMwk LPquoEC2mtMXCoGHPSNDHV6GIdcyJNakgDGYRKvKhPfQELHuRvoLpYC4rCpy9yvv2yWOwRKOXfjgjaGz0yVu0qF1i7yvd2+1wSymfSUNKyUjL5Mtnzc/Gi3cdWpUpwgN+WL6MhHBApFnmiB2MZ8kXWbmpcA== 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: This allows batching the rmap removal with folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention. Previously each page was removed from the rmap individually with page_remove_rmap(). If the first page belonged to a large folio, this would cause page_remove_rmap() to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue. Signed-off-by: Ryan Roberts --- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index 01f39e8144ef..6facb8c8807a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, pte_install_uffd_wp_if_needed(vma, addr, pte, pteval); } +static inline unsigned long page_addr(struct page *page, + struct page *anchor, unsigned long anchor_addr) +{ + unsigned long offset; + unsigned long addr; + + offset = (page_to_pfn(page) - page_to_pfn(anchor)) << PAGE_SHIFT; + addr = anchor_addr + offset; + + if (anchor > page) { + if (addr > anchor_addr) + return 0; + } else { + if (addr < anchor_addr) + return ULONG_MAX; + } + + return addr; +} + +static int calc_anon_folio_map_pgcount(struct folio *folio, + struct page *page, pte_t *pte, + unsigned long addr, unsigned long end) +{ + pte_t ptent; + int floops; + int i; + unsigned long pfn; + + end = min(page_addr(&folio->page + folio_nr_pages(folio), page, addr), + end); + floops = (end - addr) >> PAGE_SHIFT; + pfn = page_to_pfn(page); + pfn++; + pte++; + + for (i = 1; i < floops; i++) { + ptent = ptep_get(pte); + + if (!pte_present(ptent) || + pte_pfn(ptent) != pfn) { + return i; + } + + pfn++; + pte++; + } + + return floops; +} + +static unsigned long zap_anon_pte_range(struct mmu_gather *tlb, + struct vm_area_struct *vma, + struct page *page, pte_t *pte, + unsigned long addr, unsigned long end, + bool *full_out) +{ + struct folio *folio = page_folio(page); + struct mm_struct *mm = tlb->mm; + pte_t ptent; + int pgcount; + int i; + bool full; + + pgcount = calc_anon_folio_map_pgcount(folio, page, pte, addr, end); + + for (i = 0; i < pgcount;) { + ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); + tlb_remove_tlb_entry(tlb, pte, addr); + full = __tlb_remove_page(tlb, page, 0); + + if (unlikely(page_mapcount(page) < 1)) + print_bad_pte(vma, addr, ptent, page); + + i++; + page++; + pte++; + addr += PAGE_SIZE; + + if (unlikely(full)) + break; + } + + folio_remove_rmap_range(folio, page - i, i, vma); + + *full_out = full; + return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end, @@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page = vm_normal_page(vma, addr, ptent); if (unlikely(!should_zap_page(details, page))) continue; + + /* + * Batch zap large anonymous folio mappings. This allows + * batching the rmap removal, which means we avoid + * spuriously adding a partially unmapped folio to the + * deferrred split queue in the common case, which + * reduces split queue lock contention. Require the VMA + * to be anonymous to ensure that none of the PTEs in + * the range require zap_install_uffd_wp_if_needed(). + */ + if (page && PageAnon(page) && vma_is_anonymous(vma)) { + bool full; + int pgcount; + + pgcount = zap_anon_pte_range(tlb, vma, + page, pte, addr, end, &full); + + rss[mm_counter(page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount << PAGE_SHIFT; + + if (unlikely(full)) { + force_flush = 1; + addr += PAGE_SIZE; + break; + } + continue; + } + ptent = ptep_get_and_clear_full(mm, addr, pte, tlb->fullmm); tlb_remove_tlb_entry(tlb, pte, addr);