From patchwork Tue Mar 21 00:48:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13182072 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 1C0DAC761A6 for ; Tue, 21 Mar 2023 00:49:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB2BB6B007E; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E14896B0080; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0D1D6B0081; Mon, 20 Mar 2023 20:48:55 -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 B81FA6B0080 for ; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C56B216020D for ; Tue, 21 Mar 2023 00:48:54 +0000 (UTC) X-FDA: 80591070588.12.BCEC0DF Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by imf18.hostedemail.com (Postfix) with ESMTP id C396F1C0005 for ; Tue, 21 Mar 2023 00:48:52 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=TGOw0K2a; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=VLrp8OER; spf=pass (imf18.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679359732; h=from:from:sender:reply-to: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=n0I3rKbVmW11Ko5MdyrWnLEXy5CTcBBnWUTtQZPt4TU=; b=43Ek+j9yUJ2gYK2IRHXs3LqX1+OAzTmh0sBvd0peJNstXvSuE4NltuBegi79Gi4be5ZZ2M cvVBziar4rKHsG4LhTPhdLyo6RYudivRwIvuiNOxQ/ZmeciRUb4WdkqwVe7/YKuaMzi7gV 3exlR8mUBjVJmA3a/Qfmi9N5kpqC9ng= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=TGOw0K2a; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=VLrp8OER; spf=pass (imf18.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679359732; a=rsa-sha256; cv=none; b=IPFup0r/qgS08NLSSJkDdS2dEtEro3XfVjYyv2BAZiItgOraWCPk3KkuSQe+5pHbBOinwB jRCsPZ8vlRKgZomUQsN/E1t/W5MvRxzpn9IaQPkALwiA8tSSU/F7p77qr7Z7yGm+pmRxDc 8YwlDgmzmGAFzOpRqQtkysrWekqIZX0= Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 454555C005B; Mon, 20 Mar 2023 20:48:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 20 Mar 2023 20:48:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to; s=fm2; t= 1679359732; x=1679446132; bh=n0I3rKbVmW11Ko5MdyrWnLEXy5CTcBBnWUT tQZPt4TU=; b=TGOw0K2al2eY7sLD74hzDSD2ANtWUXGox+zhPeiE6P/ZNRvUeRK 4Q0E1OSDty/ycWijhLtITpkdOGOarjXiu5fjbrQ3nWDAN+QJGLTONr+uF1Q2Uc3l Q48w6zo1cQIsT1e/iFjfwnL9rPqppkTFZZ8DR7FAaqythtnQqFWf28twSJ818+pW MxoA8/Q1YQGKfE+sRpYG00S3FtH6s2dn1TImcA2bdwUMcy6TSJlcyjawq5pX/rXG byesOejoHcwfGEod9ItRRpSOFSr+WvxW7oMQjUxOh8TFlU0NGfbDID/+AwGhAJyS kP7Shwv+niUFBCz3C7lngIgEvcg9nOfO/FQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1679359732; x=1679446132; bh=n0I3rKbVmW11Ko5MdyrWnLEXy5CTcBBnWUT tQZPt4TU=; b=VLrp8OERc6ago/IJ0cIfqE5ueb4bd17BUSOXX+/PiCWaLJcJjyd Su89Z+er3PRdhytvTZUnnTTXLq2pxmHZaKWGlhXwDW/j8avGXV1RqvqPsCbJA5LR XaSrQJ/kltnPp88OMBoTfnEq4ZIDTD4Uk3aHbzEs18a8DGB4ky8tc5tH+QiWV8ks fUSY5GIBk0XfQhIXNIqUOfjkUN+uNz58xYJ4r/9dJha7JCRfeHJ4UP1FQnRI4U86 Tq+yJY7w7c05XSu+4+cpgPEXo9L9Kn2oCE15QcC/RtukdTvDwenI6RmJuxwC80/c zrT0gjqbKoA6/aoyfrG4NIkliT0oM4AxUgQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdefledgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcu jggrnhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeegge ehudfgudduvdelheehteegledtteeiveeuhfffveekhfevueefieeijeegvdenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeiiihdrhigrnhessh gvnhhtrdgtohhm X-ME-Proxy: Feedback-ID: iccd040f4:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 20 Mar 2023 20:48:51 -0400 (EDT) From: Zi Yan To: "Matthew Wilcox (Oracle)" , Yang Shi , Yu Zhao , linux-mm@kvack.org Cc: Zi Yan , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 4/5] mm: truncate: split huge page cache page to a non-zero order if possible. Date: Mon, 20 Mar 2023 20:48:28 -0400 Message-Id: <20230321004829.2012847-5-zi.yan@sent.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230321004829.2012847-1-zi.yan@sent.com> References: <20230321004829.2012847-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 9mcws5z857ackk8tuksjczoe43kwfzk9 X-Rspamd-Queue-Id: C396F1C0005 X-HE-Tag: 1679359732-22843 X-HE-Meta: U2FsdGVkX19uZuU7Yr6yQwsTi3tzyQBkhU5y2kj4VuOWQ9MocHVQxfR6bdPS/kSIqMvytMxFC1QPuOZV44u2ZR7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WVt4U+qIvJP4tVm7vuJiKX2E+AQTiLnwGQd6k/CdYkG5vD9BdOKZL7TxagPOV5dbHm8KQ1HDk+QEHbYQGkzlEfA+WHLBZkq+vxyECqjRf1c1de0dr1OaO7BzJNbAMY6WbheiJIdfv2tcM1XeN8DSizuWTS4N/B1F8zqI6M7s48e5w90m65SOA5NlgnBHKbF4mFRFPHDJgSgG3U9JjfgBb2IJqdeS3o/6pceWUJRt4qA4SGwKcOgoTSw/kwHbWpHTwWc 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: Zi Yan To minimize the number of pages after a huge page truncation, we do not need to split it all the way down to order-0. The huge page has at most three parts, the part before offset, the part to be truncated, the part remaining at the end. Find the greatest common divisor of them to calculate the new page order from it, so we can split the huge page to this order and keep the remaining pages as large and as few as possible. Signed-off-by: Zi Yan --- mm/truncate.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/mm/truncate.c b/mm/truncate.c index 86de31ed4d32..817efd5e94b4 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -22,6 +22,7 @@ #include /* grr. try_to_release_page */ #include #include +#include #include "internal.h" /* @@ -211,7 +212,8 @@ int truncate_inode_folio(struct address_space *mapping, struct folio *folio) bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) { loff_t pos = folio_pos(folio); - unsigned int offset, length; + unsigned int offset, length, remaining; + unsigned int new_order = folio_order(folio); if (pos < start) offset = start - pos; @@ -222,6 +224,7 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) length = length - offset; else length = end + 1 - pos - offset; + remaining = folio_size(folio) - offset - length; folio_wait_writeback(folio); if (length == folio_size(folio)) { @@ -236,11 +239,25 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) */ folio_zero_range(folio, offset, length); + /* + * Use the greatest common divisor of offset, length, and remaining + * as the smallest page size and compute the new order from it. So we + * can truncate a subpage as large as possible. Round up gcd to + * PAGE_SIZE, otherwise ilog2 can give -1 when gcd/PAGE_SIZE is 0. + */ + new_order = ilog2(round_up(gcd(gcd(offset, length), remaining), + PAGE_SIZE) / PAGE_SIZE); + + /* order-1 THP not supported, downgrade to order-0 */ + if (new_order == 1) + new_order = 0; + + if (folio_has_private(folio)) folio_invalidate(folio, offset, length); if (!folio_test_large(folio)) return true; - if (split_folio(folio) == 0) + if (split_huge_page_to_list_to_order(&folio->page, NULL, new_order) == 0) return true; if (folio_test_dirty(folio)) return false;