From patchwork Wed Jan 15 08:31:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ge Yang X-Patchwork-Id: 13940043 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 1B663C02180 for ; Wed, 15 Jan 2025 08:33:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 635356B007B; Wed, 15 Jan 2025 03:33:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5BE666B0082; Wed, 15 Jan 2025 03:33:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 45EDB6B0083; Wed, 15 Jan 2025 03:33:35 -0500 (EST) 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 23FE86B007B for ; Wed, 15 Jan 2025 03:33:35 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id C2109B04AA for ; Wed, 15 Jan 2025 08:33:34 +0000 (UTC) X-FDA: 83009022348.03.333A9C9 Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.6]) by imf02.hostedemail.com (Postfix) with ESMTP id 836D780007 for ; Wed, 15 Jan 2025 08:33:30 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b="VBnP/DJe"; dmarc=pass (policy=none) header.from=126.com; spf=pass (imf02.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.6 as permitted sender) smtp.mailfrom=yangge1116@126.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736930013; a=rsa-sha256; cv=none; b=wKR2GS2xwVJj0yaQCKwURfmxkY8+nZ+BZynOdWrrmIGzcG1Ejwt7SJHk1/tgfFmQiUX7lF knxnJIisGp2L2KGtGipE7PkFvkQKn5/65lSmfWCKblQGqbJhSvpM9ZKZbIGsg30/nx51E8 leG7flS0rwWscKo7hnKcrtlH1KPtJtk= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b="VBnP/DJe"; dmarc=pass (policy=none) header.from=126.com; spf=pass (imf02.hostedemail.com: domain of yangge1116@126.com designates 117.135.210.6 as permitted sender) smtp.mailfrom=yangge1116@126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736930013; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:references:dkim-signature; bh=jM/ZHyxm7/9s6JcnJbu2F8nRJEyutEKf1kwWyMqKU88=; b=LEf9CZSvRwrYsT/f0oxfxU+nCWTPmahorvEHNdif3I+CGZ3UkudTE3NtcoQ3BX+aozNKA9 1trUvG6puo1L431nNiYD5wsSPxUAcBeZs+los1ls6s0G7CJOPc0Ivam9AMvxunecnHDvID FDMaIKNTjZQVfs/ltIdv+PNBZCBA/5g= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=jM/ZHyxm7/9s6JcnJb u2F8nRJEyutEKf1kwWyMqKU88=; b=VBnP/DJerseMJNXQiD9EnHL/SbiyKzUpKU LRJ9LMkf9FxEuOVPyoov1XgG3KM4ZNh2wsRDc6yVAZb0urbhAUSVsfd6OxszY721 gN5UMkQqlLqhJFtEbO7zOUaeFee1Cz6SSZA1KmnLsoqCC+F8LpJnibS7EdCIFAbc rEGyXIZ9g= Received: from hg-OptiPlex-7040.hygon.cn (unknown []) by gzga-smtp-mtada-g1-2 (Coremail) with SMTP id _____wD3D69ncodnjcF3BQ--.47546S2; Wed, 15 Jan 2025 16:31:36 +0800 (CST) From: yangge1116@126.com To: akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, 21cnbao@gmail.com, david@redhat.com, baolin.wang@linux.alibaba.com, hannes@cmpxchg.org, vbabka@suse.cz, liuzixing@hygon.cn, yangge Subject: [PATCH] mm: compaction: use the actual allocation context to determine the watermarks for costly order during async memory compaction Date: Wed, 15 Jan 2025 16:31:34 +0800 Message-Id: <1736929894-19228-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: _____wD3D69ncodnjcF3BQ--.47546S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZrW3CFyxAFy5Cr13Kr1DGFg_yoWrKFWrpF 48Wr13C395XF47CF4xta1kWF15Cw4xJF1UJFnFv34xZw4akFn2v3WDta45AF4UXry3JF4j vFWqgFnrCanxZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRoKZXUUUUU= X-Originating-IP: [112.64.138.194] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOgXVG2eHcYkO2wAAs8 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 836D780007 X-Stat-Signature: uty1jdkyx31jf1j4an3zx1h3x8knsomw X-HE-Tag: 1736930010-893235 X-HE-Meta: U2FsdGVkX1+hk/bLsE+vkHHOgPOCH4kYykY8k/uhhZ00zu1v37w+PBJaMVdqwdyz9xECbnw/hvJ7bwhj/bs85Uh2AHKrK4XWN/dv8PpnNcBdW+GrYdJGaYHRtQgVs0nV/6CB3u1H6J8n35fYRZReRBoK2f0OWMB8lKIAeiLTWBYXJDFRHHTAQWDObK6f1T4I0DF6UQyrYazjjJ29EPtSGuNrF+LcdQ81Qf/jopRsUlVUwRvzeUoJC5E88TpZft4UZGx3BD9TEgdZEkC47Mrc9SeJiAiBDOOChCi5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ner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: yangge There are 4 NUMA nodes on my machine, and each NUMA node has 32GB of memory. I have configured 16GB of CMA memory on each NUMA node, and starting a 32GB virtual machine with device passthrough is extremely slow, taking almost an hour. Long term GUP cannot allocate memory from CMA area, so a maximum of 16 GB of no-CMA memory on a NUMA node can be used as virtual machine memory. There is 16GB of free CMA memory on a NUMA node, which is sufficient to pass the order-0 watermark check, causing the __compaction_suitable() function to consistently return true. For costly allocations, if the __compaction_suitable() function always returns true, it causes the __alloc_pages_slowpath() function to fail to exit at the appropriate point. This prevents timely fallback to allocating memory on other nodes, ultimately resulting in excessively long virtual machine startup times. Call trace: __alloc_pages_slowpath if (compact_result == COMPACT_SKIPPED || compact_result == COMPACT_DEFERRED) goto nopage; // should exit __alloc_pages_slowpath() from here We could use the real unmovable allocation context to have __zone_watermark_unusable_free() subtract CMA pages, and thus we won't pass the order-0 check anymore once the non-CMA part is exhausted. There is some risk that in some different scenario the compaction could in fact migrate pages from the exhausted non-CMA part of the zone to the CMA part and succeed, and we'll skip it instead. But only __GFP_NORETRY allocations should be affected in the immediate "goto nopage" when compaction is skipped, others will attempt with DEF_COMPACT_PRIORITY anyway and won't fail without trying to compact-migrate the non-CMA pageblocks into CMA pageblocks first, so it should be fine. After this fix, it only takes a few tens of seconds to start a 32GB virtual machine with device passthrough functionality. Link: https://lore.kernel.org/lkml/1736335854-548-1-git-send-email-yangge1116@126.com/ Signed-off-by: yangge Acked-by: Vlastimil Babka --- mm/compaction.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/mm/compaction.c b/mm/compaction.c index 07bd227..9032bb6 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -2490,7 +2490,8 @@ bool compaction_zonelist_suitable(struct alloc_context *ac, int order, */ static enum compact_result compaction_suit_allocation_order(struct zone *zone, unsigned int order, - int highest_zoneidx, unsigned int alloc_flags) + int highest_zoneidx, unsigned int alloc_flags, + bool async) { unsigned long watermark; @@ -2499,6 +2500,25 @@ compaction_suit_allocation_order(struct zone *zone, unsigned int order, alloc_flags)) return COMPACT_SUCCESS; + /* + * For costly orders, during the async memory compaction process, use the + * actual allocation context to determine the watermarks. There's some risk + * that in some different scenario the compaction could in fact migrate + * pages from the exhausted non-CMA part of the zone to the CMA part and + * succeed, and we'll skip it instead. But only __GFP_NORETRY allocations + * should be affected in the immediate "goto nopage" when compaction is + * skipped, others will attempt with DEF_COMPACT_PRIORITY anyway and won't + * fail without trying to compact-migrate the non-CMA pageblocks into CMA + * pageblocks first, so it should be fine. + */ + if (order > PAGE_ALLOC_COSTLY_ORDER && async) { + watermark = low_wmark_pages(zone) + compact_gap(order); + if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx, + alloc_flags & ALLOC_CMA, + zone_page_state(zone, NR_FREE_PAGES))) + return COMPACT_SKIPPED; + } + if (!compaction_suitable(zone, order, highest_zoneidx)) return COMPACT_SKIPPED; @@ -2534,7 +2554,8 @@ compact_zone(struct compact_control *cc, struct capture_control *capc) if (!is_via_compact_memory(cc->order)) { ret = compaction_suit_allocation_order(cc->zone, cc->order, cc->highest_zoneidx, - cc->alloc_flags); + cc->alloc_flags, + cc->mode == MIGRATE_ASYNC); if (ret != COMPACT_CONTINUE) return ret; } @@ -3037,7 +3058,8 @@ static bool kcompactd_node_suitable(pg_data_t *pgdat) ret = compaction_suit_allocation_order(zone, pgdat->kcompactd_max_order, - highest_zoneidx, ALLOC_WMARK_MIN); + highest_zoneidx, ALLOC_WMARK_MIN, + 0); if (ret == COMPACT_CONTINUE) return true; } @@ -3078,7 +3100,8 @@ static void kcompactd_do_work(pg_data_t *pgdat) continue; ret = compaction_suit_allocation_order(zone, - cc.order, zoneid, ALLOC_WMARK_MIN); + cc.order, zoneid, ALLOC_WMARK_MIN, + cc.mode == MIGRATE_ASYNC); if (ret != COMPACT_CONTINUE) continue;