From patchwork Mon Aug 21 20:44:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthew Wilcox X-Patchwork-Id: 13359803 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 AF12FEE4996 for ; Mon, 21 Aug 2023 20:44:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A97E3940015; Mon, 21 Aug 2023 16:44:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A4702940013; Mon, 21 Aug 2023 16:44:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 87237940015; Mon, 21 Aug 2023 16:44:43 -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 7013F940013 for ; Mon, 21 Aug 2023 16:44:43 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 504A2C07D7 for ; Mon, 21 Aug 2023 20:44:43 +0000 (UTC) X-FDA: 81149290446.14.5240C6E Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf17.hostedemail.com (Postfix) with ESMTP id 9CB054000B for ; Mon, 21 Aug 2023 20:44:41 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=rzIBTvDK; spf=none (imf17.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=1692650681; 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=iQ6N/LjQx+6o+U2nCa8DzMMTkBWW+AcF6vFCml2CAOc=; b=CMbg9Rs4DnLTzz/N/yplIln66rMWcVgK+Al9KCjieNU8rmlClrLBkTcGUomrb8g+bj0oJk ynkSogq+hxV0M7//XrU7OG3Ze89HV9vSGjq/3jusNC+MlXYYOzZGD/Qs+LH95Zo+puudHF D2KAMr4SlGmzt9NA/F5ABVUqJ45L9EQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=infradead.org header.s=casper.20170209 header.b=rzIBTvDK; spf=none (imf17.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1692650681; a=rsa-sha256; cv=none; b=jF99Aqu343ypGkb95GkNsUEFYCWQwWZEePNZBuspDPNo3gL5SN8EGhBLtPZt8sE5Z/YMAu qC7V5DVA8gmrKhmqiBUwwyvyyK8Vs850z4k/upquKt98MT88mZG1a+pZFdsrjkhT6cmfx5 Zelapm5u1s02HPOgds8S6KLECwTNUSM= 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=iQ6N/LjQx+6o+U2nCa8DzMMTkBWW+AcF6vFCml2CAOc=; b=rzIBTvDK8Pk0RQceRRAl76K1f9 NGnTg9B59ZUz3WjY2SEsQN+m8Ucg8weTeTcBwjS+CUceeWIDduR1Uv06uxGl4vMDlBCMPZYE7ttcK I8/EnoTTyv2Yecui/H62M7nREHC7USK/BGSjwK1g0OmGOC8sY6BDa/DZDN/FXIRm2kWdH/2REfwlb 1GpgRBuCB96Y6mXA8kGAsI3rKNCe3ypQKbyqd/Hwnak8ALNLQLemvTNd0E89YYvNo2mI9KVVuI5AA wMp4XqKQc8qbpgEKwqe92Zi3pQCOXXB0/KOHbnviyd69XPXsDkpom+BUzOJW4t+5HrAk11gyRQE3m T1bYbKjg==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1qYBlE-00CL04-22; Mon, 21 Aug 2023 20:44:28 +0000 From: "Matthew Wilcox (Oracle)" To: Mateusz Guzik , linux-kernel@vger.kernel.org, dennis@kernel.org, tj@kernel.org, cl@linux.com, akpm@linux-foundation.org, shakeelb@google.com, linux-mm@kvack.org Cc: "Matthew Wilcox (Oracle)" Subject: [PATCH 1/7] mm: Make folios_put() the basis of release_pages() Date: Mon, 21 Aug 2023 21:44:19 +0100 Message-Id: <20230821204425.2940496-1-willy@infradead.org> X-Mailer: git-send-email 2.37.1 In-Reply-To: References: MIME-Version: 1.0 X-Rspamd-Queue-Id: 9CB054000B X-Rspam-User: X-Stat-Signature: niyok1bt8p6prb94ox8us7f6h67km6uk X-Rspamd-Server: rspam01 X-HE-Tag: 1692650681-743770 X-HE-Meta: U2FsdGVkX18Vyg/0Ox1QkyZWhgLP0ctZs26lBRbv1C/gedTGCL9lON9TVDhSysJ/H42+39JXpjt9R+deyy/K0QT5D2I13kH0Uracfb0QU4iyH7Xnrs7CJWs0MyVNk1BYNijlfEBYkU8esXVwpiJ5uR2gxIgBgVoPE/i5WHeeFPaHRKnW3MYo8BuS3k5C/dMbFUxKVIca5/zzfU/OW+nF0A7sCDbRxK0Fh6j5YpBiOR5v50rLlakbsDYuJ+cxgXgUl5fuFW3WXdJMm5MSl2TN4bkYaInBAoj7dGQUq0feLvdw7UMihh6B6M2ZEo7lBz8xEq0cN8v+Tttj4XpZYHl9Jay/Biv5tZck2qsIjkclbSDvEMoS/B8HzyZyMZb3Evki6zSW7PQ3JRrXNnPI0NH7LQyRtoqe5I2c0NxY0XMeULfHUewib4EuN2sYzO5jX0vsBTeyDqOzIHH94E+4p0tJzKXzS3z+gHaLuIMbFonqe8rjL6zC/H271xwO31nAtHmL6TzEbbAvNbYqfP4W84UYbbnhs3PpS5hlJUs+oyGkFoC5c/0TK33Uqu1PA8Lgfn804UtF3J6aZx37vfTOIEfXkzJK9Ifpbi4Zz1Ak4uI9RDz3qnd45Tipe5mGxN0Gw4PUVRrS+1SbKzQEsXsYxmnXvJ6KACjZycDF3YSh3SJllaeXqDOJ9+FffngFJapf4wERUMkYyW4+muXMPCqqQusCiu7p+SjZyJCJXuQb+N/QG+hodK6wmxj3/qUTY4K/E38rK9mChuY9Wr7HbxNUQ1kh1BNPRPkDy3f9JntIX0QIrmI+cQ4vxeib0tj/saC6IYLmWfMJj3DVLy0rizLBEOJT9cNwEUVN8SYfoKIDyZjvfTNiCx+onYVJ8qoxqqdW9vMuxYvNW7ruWhMy7A5uC/arsDu0lszZqCVHeLtxdDGO6tNNhJeZtqT7kbiWeV98fXkVpL6zli7paX1GXvRKMGR b3MPfz4B iAVMSUJRfpg5bT0mJVDcJUsXBQW15Z7PiMOoIRc/awog9fqCl2IaYtDXJKWuc9Gyt1kFeG3WTPlPIt1lRC+sjybMv2M0gHNvoIWtaY6qMcI7xbgdHYSfUp3UesMkTcWhhQO/ffs6XeTskOB1wFJxa77BwP1LTvvjT5d5jI8bpvfb5L0zzwLr4J879ISVO+RKfwMREEZI9gLBDKl38GfQLYGlkkX9+p5fAugRW 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: By making release_pages() call folios_put(), we can get rid of the calls to compound_head() for the callers that already know they have folios. We can also get rid of the lock_batch tracking as we know the size of the batch is limited by folio_batch. Signed-off-by: Matthew Wilcox (Oracle) --- include/linux/mm.h | 19 ++---------- mm/mlock.c | 2 +- mm/swap.c | 77 +++++++++++++++++++++++++++------------------- 3 files changed, 48 insertions(+), 50 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 85568e2b2556..c1fc81da8dff 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -36,6 +36,7 @@ struct anon_vma; struct anon_vma_chain; struct user_struct; struct pt_regs; +struct folio_batch; extern int sysctl_page_lock_unfairness; @@ -1497,23 +1498,7 @@ typedef union { } release_pages_arg __attribute__ ((__transparent_union__)); void release_pages(release_pages_arg, int nr); - -/** - * folios_put - Decrement the reference count on an array of folios. - * @folios: The folios. - * @nr: How many folios there are. - * - * Like folio_put(), but for an array of folios. This is more efficient - * than writing the loop yourself as it will optimise the locks which - * need to be taken if the folios are freed. - * - * Context: May be called in process or interrupt context, but not in NMI - * context. May be called while holding a spinlock. - */ -static inline void folios_put(struct folio **folios, unsigned int nr) -{ - release_pages(folios, nr); -} +void folios_put(struct folio_batch *folios); static inline void put_page(struct page *page) { diff --git a/mm/mlock.c b/mm/mlock.c index 0a0c996c5c21..67ec840cf5f1 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -206,7 +206,7 @@ static void mlock_folio_batch(struct folio_batch *fbatch) if (lruvec) unlock_page_lruvec_irq(lruvec); - folios_put(fbatch->folios, folio_batch_count(fbatch)); + folios_put(fbatch); folio_batch_reinit(fbatch); } diff --git a/mm/swap.c b/mm/swap.c index cd8f0150ba3a..11ca25d4843f 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -221,7 +221,7 @@ static void folio_batch_move_lru(struct folio_batch *fbatch, move_fn_t move_fn) if (lruvec) unlock_page_lruvec_irqrestore(lruvec, flags); - folios_put(fbatch->folios, folio_batch_count(fbatch)); + folios_put(fbatch); folio_batch_reinit(fbatch); } @@ -946,41 +946,25 @@ void lru_cache_disable(void) } /** - * release_pages - batched put_page() - * @arg: array of pages to release - * @nr: number of pages + * folios_put - Decrement the reference count on a batch of folios. + * @folios: The folios. * - * Decrement the reference count on all the pages in @arg. If it - * fell to zero, remove the page from the LRU and free it. + * Like folio_put(), but for a batch of folios. This is more efficient + * than writing the loop yourself as it will optimise the locks which + * need to be taken if the folios are freed. * - * Note that the argument can be an array of pages, encoded pages, - * or folio pointers. We ignore any encoded bits, and turn any of - * them into just a folio that gets free'd. + * Context: May be called in process or interrupt context, but not in NMI + * context. May be called while holding a spinlock. */ -void release_pages(release_pages_arg arg, int nr) +void folios_put(struct folio_batch *folios) { int i; - struct encoded_page **encoded = arg.encoded_pages; LIST_HEAD(pages_to_free); struct lruvec *lruvec = NULL; unsigned long flags = 0; - unsigned int lock_batch; - for (i = 0; i < nr; i++) { - struct folio *folio; - - /* Turn any of the argument types into a folio */ - folio = page_folio(encoded_page_ptr(encoded[i])); - - /* - * Make sure the IRQ-safe lock-holding time does not get - * excessive with a continuous string of pages from the - * same lruvec. The lock is held only if lruvec != NULL. - */ - if (lruvec && ++lock_batch == SWAP_CLUSTER_MAX) { - unlock_page_lruvec_irqrestore(lruvec, flags); - lruvec = NULL; - } + for (i = 0; i < folios->nr; i++) { + struct folio *folio = folios->folios[i]; if (is_huge_zero_page(&folio->page)) continue; @@ -1010,13 +994,8 @@ void release_pages(release_pages_arg arg, int nr) } if (folio_test_lru(folio)) { - struct lruvec *prev_lruvec = lruvec; - lruvec = folio_lruvec_relock_irqsave(folio, lruvec, &flags); - if (prev_lruvec != lruvec) - lock_batch = 0; - lruvec_del_folio(lruvec, folio); __folio_clear_lru_flags(folio); } @@ -1041,6 +1020,40 @@ void release_pages(release_pages_arg arg, int nr) mem_cgroup_uncharge_list(&pages_to_free); free_unref_page_list(&pages_to_free); } +EXPORT_SYMBOL(folios_put); + +/** + * release_pages - batched put_page() + * @arg: array of pages to release + * @nr: number of pages + * + * Decrement the reference count on all the pages in @arg. If it + * fell to zero, remove the page from the LRU and free it. + * + * Note that the argument can be an array of pages, encoded pages, + * or folio pointers. We ignore any encoded bits, and turn any of + * them into just a folio that gets free'd. + */ +void release_pages(release_pages_arg arg, int nr) +{ + struct folio_batch fbatch; + struct encoded_page **encoded = arg.encoded_pages; + int i; + + folio_batch_init(&fbatch); + for (i = 0; i < nr; i++) { + /* Turn any of the argument types into a folio */ + struct folio *folio = page_folio(encoded_page_ptr(encoded[i])); + + if (folio_batch_add(&fbatch, folio) > 0) + continue; + folios_put(&fbatch); + fbatch.nr = 0; + } + + if (fbatch.nr) + folios_put(&fbatch); +} EXPORT_SYMBOL(release_pages); /*