From patchwork Tue Feb 20 18:32:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13564334 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 CB698C5475B for ; Tue, 20 Feb 2024 18:32:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 512D96B0083; Tue, 20 Feb 2024 13:32:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 49D126B0085; Tue, 20 Feb 2024 13:32:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C70B6B0087; Tue, 20 Feb 2024 13:32:48 -0500 (EST) 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 13D116B0083 for ; Tue, 20 Feb 2024 13:32:48 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CB2581A07C2 for ; Tue, 20 Feb 2024 18:32:47 +0000 (UTC) X-FDA: 81813028374.18.860BF31 Received: from wout3-smtp.messagingengine.com (wout3-smtp.messagingengine.com [64.147.123.19]) by imf13.hostedemail.com (Postfix) with ESMTP id 8924720023 for ; Tue, 20 Feb 2024 18:32:45 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=sent.com header.s=fm1 header.b=IBCxnz5k; dkim=pass header.d=messagingengine.com header.s=fm1 header.b=jiSueTP2; spf=pass (imf13.hostedemail.com: domain of zi.yan@sent.com designates 64.147.123.19 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=1708453965; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=VthfB9xHWXamr5LNX5aI7SP9sOuq2T7O1UcOskPjsOI=; b=NJEUAhMOBdsGAU7kxMChYV1ex3VExE1+oYRzpWKlfs80cEqd+10jKlpXhRinR3MkeRrrKx pETa+ZMGKtapJwIzqaY0mY5HX2obxgHeVYLAPZN58XLCPm6HEIYAQ+Rwg7VfkeoNdupBDI j5jUc0XOU7/MBKLf2R5X2LmASLt5LnM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708453965; a=rsa-sha256; cv=none; b=uhGSu0yYFQo9wDAAmv/TVGNs76CH+MIWwFxQCIF80mC2uwvvFoQAuejw+CApS8HBWfn7JF g+v8XRx4hUcE+hjl2KIf50MdBT0ia1WR5Wk2FODslefoBToihj+O/Cae1WDxznQ0ECWllA Hgp+fQvLxrMIS/+s4/CVuqzVeuc5TbQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=sent.com header.s=fm1 header.b=IBCxnz5k; dkim=pass header.d=messagingengine.com header.s=fm1 header.b=jiSueTP2; spf=pass (imf13.hostedemail.com: domain of zi.yan@sent.com designates 64.147.123.19 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 8B5B03200A4D; Tue, 20 Feb 2024 13:32:42 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Tue, 20 Feb 2024 13:32:44 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:reply-to:subject:subject:to:to; s=fm1; t= 1708453962; x=1708540362; bh=VthfB9xHWXamr5LNX5aI7SP9sOuq2T7O1Uc OskPjsOI=; b=IBCxnz5kEzrcnVVh8byadrCYLGeY+gCPJu4KYwlIh6EYBa+DLmh DVgGL5okBSyRGQJM8oS2cHgWVRx3A/VIJsXjlm3/Q8xVAl81p8ZTP/MDsKI2ilKg j1Ao/xGia5AXXNoAg2GqbL8XA2QOpbmWkUYhgoERs1srQZHv3C2sJWikcSOgJwz/ +rJtvtpe0YpSvNLJwH2TfKlclX3dzkRoKZiE4kGoAG4IF8+gMNdMFu8x2Hi8ckn9 gW/FXHoVwtF++TWclK+cV7TKWHLboYQV2MVqYg/G0zDsRVdgwpivGy01VjhcrLvX Bh05vMbRhOlvB5aLeaXKrxXzQ4cKhU/uynQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type: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:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t= 1708453962; x=1708540362; bh=VthfB9xHWXamr5LNX5aI7SP9sOuq2T7O1Uc OskPjsOI=; b=jiSueTP2NFAW16xmDieh0jQv1EMcSklBlVsdVriYGBp7o94Ggcq fl0/Y10zf0QltMFk5nK18Kf7eVAmp5HMBYWlwcMMrbzm2fCsUiP4W6EWAgsjePP1 pugKuaGP0Dg72DLTzagqAwEVofFHzqE8dUqb7Ia413zAYAzGmQ/RmNmNMqu80iK8 dpyfuQmLBOu24LqQBr77Xce4SnEjXhA2bFEF/1r0RToVXdwMDXckGJibLq+U8kYX /vNtbBD9Is8Z+R/sn+DyixhqNPCqW9vOfAJ/u/DEMTKXT0EFLnPVqCUjBp/RfVJ4 2jWosqtyzTbBoRRq+5SVmMelvVS5BbOYhzg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrfedtgdduudehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhhrgggtgfesthekredtredtjeenucfhrhhomhepkghi ucgjrghnuceoiihirdihrghnsehsvghnthdrtghomheqnecuggftrfgrthhtvghrnhepje ekteekffelleekudfftdefvddtjeejuedtuedtteegjefgvedtfedujeekieevnecuvehl uhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepiihirdihrghnse hsvghnthdrtghomh X-ME-Proxy: Feedback-ID: iccd040f4:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 20 Feb 2024 13:32:41 -0500 (EST) From: Zi Yan To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Zi Yan , "Huang, Ying" , Ryan Roberts , Andrew Morton , "Matthew Wilcox (Oracle)" , David Hildenbrand , "Yin, Fengwei" , Yu Zhao , Vlastimil Babka , "Kirill A . Shutemov" , Johannes Weiner , Baolin Wang , Kemeng Shi , Mel Gorman , Rohan Puri , Mcgrof Chamberlain , Adam Manzanares , "Vishal Moola (Oracle)" Subject: [PATCH v7 2/4] mm/compaction: enable compacting >0 order folios. Date: Tue, 20 Feb 2024 13:32:18 -0500 Message-ID: <20240220183220.1451315-3-zi.yan@sent.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240220183220.1451315-1-zi.yan@sent.com> References: <20240220183220.1451315-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Stat-Signature: c4f57pdrb66gdoh7a3mdpmcrpg1xyjbb X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8924720023 X-Rspam-User: X-HE-Tag: 1708453965-409754 X-HE-Meta: U2FsdGVkX19WpO0RJXAGha3ibOQ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/dSyUQWSZHBEznMkxb+anryUmJtjN8kC+7T8pjsIOWrJTQjehXpvXamosdB8Wxq6Y/e8HfaetdX+W7wEsjl0EBRic9CFzEoz2F1Az0NhL92Zv0MGZnXg73SdBjta4iKbD5kYFb9Cyv+RcApfXq1JkoeOXBv1DKtw8BiYwtJFLiBV0p4V2egrF0Im58tdINabBoJOijvL55xixxPmHUtWX+thqfNi8yrobhw5WUXTiTbZ9WiP+rVEyrj62lOxrYLkhFRZ6ukL2fIyqc6TguvOCOUQ+NtAJgBqezHO1nmOY8uQWEnOd4DmC9Q5rf00ien0rV9ddolL5OcyILIz5E6JoeGO+Df2LJV7XRru6Gdk4P+yixWr7ZUyOGZoEECShW1VnTZzs+EuiUZV+JicKsIMeEk5uUhYMHl3Cj39dIhN6S4oYY1vm1UV32ph9yULBtd+F14hVY/dX7XqtdvgLRDrI6d45KRpN1XnKVwutWFbJNF95Jglp2Rui1OLuuilO7yEw+y9Xwhd92TB2l5gDrniFs03DZa2FsK+wEmA1AIOzulFkQo4O5zQnkijO7LA8FsBNxjdoGZMJVa/8m/vemVyFGsJ/Wez3rqjVe/iaMbTqgsgXqVHpR9Jhy8m4Dn2VmpDjhkKC2rbtmlqURizXJWOybPDnPYGa/sR7y2WR/6COygHPtDc3Dau6bq0StXY/K5Q5suZ0cFxgK8M+dOMbz46ax6Lt1J5WGNxqZGQBquXK1xP9SDfUqhZXunwke/VA/jrQfyUrmddO4pguVaO4hAGZ/NUNng9u3XPKUibkRO3WPNofGbLAsMMibZZiVufiQVwH9JWFCOSVFJXwWEvBMH4va0M0d3noTbqE0 05oFMyQw tSmL7JqN3aq7wgs8grTmcCGLyDKnTUJ/mzIz6xLinAA= 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: List-Subscribe: List-Unsubscribe: From: Zi Yan migrate_pages() supports >0 order folio migration and during compaction, even if compaction_alloc() cannot provide >0 order free pages, migrate_pages() can split the source page and try to migrate the base pages from the split. It can be a baseline and start point for adding support for compacting >0 order folios. Signed-off-by: Zi Yan Suggested-by: Huang Ying Reviewed-by: Baolin Wang Reviewed-by: Vlastimil Babka Tested-by: Baolin Wang Tested-by: Yu Zhao Cc: Adam Manzanares Cc: David Hildenbrand Cc: Johannes Weiner Cc: Kemeng Shi Cc: Kirill A. Shutemov Cc: Luis Chamberlain Cc: Matthew Wilcox (Oracle) Cc: Mel Gorman Cc: Ryan Roberts Cc: Vishal Moola (Oracle) Cc: Vlastimil Babka Cc: Yin Fengwei --- mm/compaction.c | 101 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 76 insertions(+), 25 deletions(-) diff --git a/mm/compaction.c b/mm/compaction.c index ccd447282665..6509fab27be9 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -40,9 +40,22 @@ static inline void count_compact_events(enum vm_event_item item, long delta) { count_vm_events(item, delta); } + +/* + * order == -1 is expected when compacting proactively via + * 1. /proc/sys/vm/compact_memory + * 2. /sys/devices/system/node/nodex/compact + * 3. /proc/sys/vm/compaction_proactiveness + */ +static inline bool is_via_compact_memory(int order) +{ + return order == -1; +} + #else #define count_compact_event(item) do { } while (0) #define count_compact_events(item, delta) do { } while (0) +static inline bool is_via_compact_memory(int order) { return false; } #endif #if defined CONFIG_COMPACTION || defined CONFIG_CMA @@ -816,6 +829,32 @@ static bool too_many_isolated(struct compact_control *cc) return too_many; } +/** + * skip_isolation_on_order() - determine when to skip folio isolation based on + * folio order and compaction target order + * @order: to-be-isolated folio order + * @target_order: compaction target order + * + * This avoids unnecessary folio isolations during compaction. + */ +static bool skip_isolation_on_order(int order, int target_order) +{ + /* + * Unless we are performing global compaction (i.e., + * is_via_compact_memory), skip any folios that are larger than the + * target order: we wouldn't be here if we'd have a free folio with + * the desired target_order, so migrating this folio would likely fail + * later. + */ + if (!is_via_compact_memory(target_order) && order >= target_order) + return true; + /* + * We limit memory compaction to pageblocks and won't try + * creating free blocks of memory that are larger than that. + */ + return order >= pageblock_order; +} + /** * isolate_migratepages_block() - isolate all migrate-able pages within * a single pageblock @@ -947,7 +986,22 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, valid_page = page; } - if (PageHuge(page) && cc->alloc_contig) { + if (PageHuge(page)) { + /* + * skip hugetlbfs if we are not compacting for pages + * bigger than its order. THPs and other compound pages + * are handled below. + */ + if (!cc->alloc_contig) { + const unsigned int order = compound_order(page); + + if (order <= MAX_PAGE_ORDER) { + low_pfn += (1UL << order) - 1; + nr_scanned += (1UL << order) - 1; + } + goto isolate_fail; + } + /* for alloc_contig case */ if (locked) { unlock_page_lruvec_irqrestore(locked, flags); locked = NULL; @@ -1008,21 +1062,24 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, } /* - * Regardless of being on LRU, compound pages such as THP and - * hugetlbfs are not to be compacted unless we are attempting - * an allocation much larger than the huge page size (eg CMA). - * We can potentially save a lot of iterations if we skip them - * at once. The check is racy, but we can consider only valid - * values and the only danger is skipping too much. + * Regardless of being on LRU, compound pages such as THP + * (hugetlbfs is handled above) are not to be compacted unless + * we are attempting an allocation larger than the compound + * page size. We can potentially save a lot of iterations if we + * skip them at once. The check is racy, but we can consider + * only valid values and the only danger is skipping too much. */ if (PageCompound(page) && !cc->alloc_contig) { const unsigned int order = compound_order(page); - if (likely(order <= MAX_PAGE_ORDER)) { - low_pfn += (1UL << order) - 1; - nr_scanned += (1UL << order) - 1; + /* Skip based on page order and compaction target order. */ + if (skip_isolation_on_order(order, cc->order)) { + if (order <= MAX_PAGE_ORDER) { + low_pfn += (1UL << order) - 1; + nr_scanned += (1UL << order) - 1; + } + goto isolate_fail; } - goto isolate_fail; } /* @@ -1165,10 +1222,11 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, } /* - * folio become large since the non-locked check, - * and it's on LRU. + * Check LRU folio order under the lock */ - if (unlikely(folio_test_large(folio) && !cc->alloc_contig)) { + if (unlikely(skip_isolation_on_order(folio_order(folio), + cc->order) && + !cc->alloc_contig)) { low_pfn += folio_nr_pages(folio) - 1; nr_scanned += folio_nr_pages(folio) - 1; folio_set_lru(folio); @@ -1788,6 +1846,10 @@ static struct folio *compaction_alloc(struct folio *src, unsigned long data) struct compact_control *cc = (struct compact_control *)data; struct folio *dst; + /* this makes migrate_pages() split the source page and retry */ + if (folio_test_large(src)) + return NULL; + if (list_empty(&cc->freepages)) { isolate_freepages(cc); @@ -2090,17 +2152,6 @@ static isolate_migrate_t isolate_migratepages(struct compact_control *cc) return cc->nr_migratepages ? ISOLATE_SUCCESS : ISOLATE_NONE; } -/* - * order == -1 is expected when compacting proactively via - * 1. /proc/sys/vm/compact_memory - * 2. /sys/devices/system/node/nodex/compact - * 3. /proc/sys/vm/compaction_proactiveness - */ -static inline bool is_via_compact_memory(int order) -{ - return order == -1; -} - /* * Determine whether kswapd is (or recently was!) running on this node. *