From patchwork Fri Apr 14 13:02:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ryan Roberts X-Patchwork-Id: 13211465 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 2E728C77B72 for ; Fri, 14 Apr 2023 13:03:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C610280004; Fri, 14 Apr 2023 09:03:38 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 44F08280001; Fri, 14 Apr 2023 09:03:38 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A4A4280004; Fri, 14 Apr 2023 09:03:38 -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 11857280001 for ; Fri, 14 Apr 2023 09:03:38 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D300B120241 for ; Fri, 14 Apr 2023 13:03:37 +0000 (UTC) X-FDA: 80680013274.23.B9A8BD2 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf21.hostedemail.com (Postfix) with ESMTP id 3141E1C000B for ; Fri, 14 Apr 2023 13:03:35 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; spf=pass (imf21.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=1681477415; 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=7ZDJ9cjTlxL0uCmbHQ5z7qLc8C1ClBv5+RC+K8b5xO4=; b=H+3e6ApQZaAGlBJ3g1jAgae7qRw6QgzTO9feRndXyM+AjgQy/sKCryzyK0PpQOQF8kd18u AuUfhKcj98kORdfgEdBOQtX6PZDPcxI2vH0H9rxfvNcL/2YnWro4z3dLoel+2a2AMR2oBq 6PwcTM9UfrVDWi+Z91mKzBhpiC4F6qU= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; spf=pass (imf21.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=1681477415; a=rsa-sha256; cv=none; b=Xp0zzANHjsZMjO+FziiHWP4M7u8sHgvUKOyLjQyZjptZd87GvaEVCXmm/xGpXmjlvKV6M1 v+VtWEh7x6xi+YYp5AjWgukUeDmwjyZPIijXM7sLfFffMdWwK6hvl3mQQno7IHw8KP8/b/ xSnEwXVaDXtDN12GALhkc+baRstNQoo= 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 4E2F02F4; Fri, 14 Apr 2023 06:04:19 -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 C9E343F6C4; Fri, 14 Apr 2023 06:03:33 -0700 (PDT) From: Ryan Roberts To: Andrew Morton , "Matthew Wilcox (Oracle)" , Yu Zhao , "Yin, Fengwei" Cc: Ryan Roberts , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org Subject: [RFC v2 PATCH 13/17] mm: Implement folio_remove_rmap_range() Date: Fri, 14 Apr 2023 14:02:59 +0100 Message-Id: <20230414130303.2345383-14-ryan.roberts@arm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230414130303.2345383-1-ryan.roberts@arm.com> References: <20230414130303.2345383-1-ryan.roberts@arm.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 787b98pee56agn7gayfcmte4yrs8i7yu X-Rspamd-Queue-Id: 3141E1C000B X-HE-Tag: 1681477415-635766 X-HE-Meta: U2FsdGVkX18/qXghwyKCEhKA7Z359KhbREZ/ACtDBMZY5b67khui8PZliuoYvjDUJ6ru8hsjFGK2498EcHKekdkh11zQGCYGGNhaffAZeB4QZGZunJOia4yTIECQNrF075kWne+oZzGa22CvzXM0V4HX+9+4wZWajnhl1QBLikF/nvSUyozqIMsY24mzyCQdABq2pU6/TKtB+heTloNf64lVAm/MVK2EzUyw21fbwnY6hYa+srJ3+FD+0/AeVRFKP1nsGQ8IqWBcozmThGtGDzLjhj9Bh74Xf20qo4Oy31q6eS9Cn4Q/HlEmOFj0vFOTLdL8uUgiXLt8tMTqxkNHL279i9HqdLYi6OdMSHnRzp8Ye954LSRY1L0JoPET7lAmAf3c5AsXfKH33oCwWWxU+2y4D27JXCSFshEAJ5aRzLht4gwsh1D/rh20lS56jLUe31OaZiuDl7oiraZN3w0g8gZPaRiQKYJvbsXkPYVkK5ATW3pI32VMiJvDw2YWgfGSLMF5iNTyfE7WCBGKo9SV2kqILxikF1/Ix+d5QPe84bEjZNK0zE+prE1ur9P/Bvw3E6oK5JH41Sa7NnSG1s9GQIGOi0tcVeMuBoxnrjkVrdpXux3aVHX+0rMCsfF/ZUBTMSnZZ9Tdfz5GiAk9bssKGWzncOaZc0p7Vj6UKSBMd9E/MjLKBv6rolvUCfGAal7TJ6xPJ0u1fZXVXtvHuFdo2cT9yIrpFkItozVj5z6nBjIcGWSNHr0ldr6qY+wCo0z5Z0Ds54Dx98PbRXAnfUNZhLPHvg001tpG5XAQr0bZUFNZ111EIVwXw2BqN723RDyOLH7ovrEl+gHrEaDlB+tV819JWeGYMBHyXglvZX8XBLQKLQ8A0NK8RxvfA9/ZwYefsKyjNa35XzFcstHvY379xR2uQaNR5eVyl7SQyMfgBhsZyom2X0x3d7Vf6S/tnmbt0VhGiecc4aeuRpEOayA ukbjvXOH q+TtcrUo8QpMCzCD/TD91RrMdzxwWs9uLfDnELyDFB/KLyKcgrtInafUpxW+GSyE1gqZOJkS/HdKELZVFF7pZnLcbBDePh6Tji7CqLVa130GtpV03DNRIcCX1NcDUYexS51vb++BfJA2Xik/6CQ0rdWv7rCI6iZTYEcn/j1EEk2tVE8sMwW/d3heMkdv0XA9bR/hFrkf4ZILu/PJ9PIFfuSjY6QElbsYYJNsy 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, for effciency savings. All pages are accounted as small pages. Signed-off-by: Ryan Roberts --- include/linux/rmap.h | 2 ++ mm/rmap.c | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) -- 2.25.1 diff --git a/include/linux/rmap.h b/include/linux/rmap.h index 8cb0ba48d58f..7daf25887049 100644 --- a/include/linux/rmap.h +++ b/include/linux/rmap.h @@ -204,6 +204,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 1cd8fb0b929f..954e44054d5c 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1419,6 +1419,68 @@ 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; + + VM_BUG_ON_FOLIO(folio_test_hugetlb(folio), folio); + + 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