From nobody Sun Dec 14 13:41:12 2025 Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.8]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4F63F1DB122 for ; Sat, 25 Jan 2025 07:05:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.8 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737788747; cv=none; b=BzAWj0f6IOQnedKW7FDVD3EB0Q7SHQruxBuMyaL+YDI9X0AHJGseBfwdur6X0RkaK4Xx9qGalm+pqHQTbq8GWQPxFEst+eYZdyEzjUodJRIhssmdgpioc3aUr/Z1KHdqbMaGozF/KWf8KyKegPubpIObfiEJh44aF0hEdobAPqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737788747; c=relaxed/simple; bh=bGCmqMqvmgZc+WzsFp2YitSJfCt8P0KGsoof+sWggQ4=; h=From:To:Cc:Subject:Date:Message-Id; b=uiuyJGUmlwmryQWY1BoxiW15IhOm44P5MStdhR01s9zVFPZZCZC0U2a0J45PlJuIetZDJflyniZ70BNCA8NGjSBnUOug5+EiVq0ss8yXjWyHoLPWltag8MOGW27hG+u2JyPyBdaNCrUuzbB9y+bMHS51Yzx1YF6Y5x/TsyNsI54= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=126.com; spf=pass smtp.mailfrom=126.com; dkim=pass (1024-bit key) header.d=126.com header.i=@126.com header.b=LJ3oRveP; arc=none smtp.client-ip=220.197.31.8 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=126.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=126.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=126.com header.i=@126.com header.b="LJ3oRveP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=N0K64Frkc5azdmOFrR 3OtjB88tg3/Za3nPywPrHcrEY=; b=LJ3oRvePutzFEbAn7BrwdVamCucJnDLbHC 2LJDV1KZQ5AquQmXspj7XtS6s1gUaHmMwJuCUr8b1bwx4WJy3a5q8fyeR/ZQ72PY wRoZcvvucUIaIBCux2XBngnndSHOg8wzIS6aksmGU11phChSQTj5TSFIioKruonZ reLLSLFE8= Received: from hg-OptiPlex-7040.hygon.cn (unknown []) by gzsmtp5 (Coremail) with SMTP id QCkvCgDnzxaEgpRn662xAA--.6031S2; Sat, 25 Jan 2025 14:19:49 +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 V3] mm: compaction: use the actual allocation context to determine the watermarks for costly order during async memory compaction Date: Sat, 25 Jan 2025 14:19:47 +0800 Message-Id: <1737785987-7883-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: QCkvCgDnzxaEgpRn662xAA--.6031S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZrW3CFyxAFy5Cr13Kr1DGFg_yoW7JF1fpa y8uw13C395XF43uF4xta1kuF1Ygw4xJF1UJFyIyw18Zw4akFn2v3WDta45AF4UXry3JFWj vFZ09F1DCanxAaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRoKZXUUUUU= X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbifhTeG2eTdfUPBAABsR Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 =3D=3D COMPACT_SKIPPED || compact_result =3D=3D 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-yangge11= 16@126.com/ Signed-off-by: yangge Acked-by: Vlastimil Babka Reviewed-by: Baolin Wang Acked-by: Johannes Weiner --- V3: - collect Baolin's reviewed-by and Johannes's acked-by, thanks! V2: - update code and message suggested by Vlastimil mm/compaction.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/mm/compaction.c b/mm/compaction.c index 07bd227..3de7b67 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -2490,7 +2490,8 @@ bool compaction_zonelist_suitable(struct alloc_contex= t *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; =20 @@ -2499,6 +2500,23 @@ compaction_suit_allocation_order(struct zone *zone, = unsigned int order, alloc_flags)) return COMPACT_SUCCESS; =20 + /* + * For unmovable allocations (without ALLOC_CMA), check if there is enough + * free memory in the non-CMA pageblocks. Otherwise compaction could form + * the high-order page in CMA pageblocks, which would not help the + * allocation to succeed. However, limit the check to costly order async + * compaction (such as opportunistic THP attempts) because there is the + * possibility that compaction would migrate pages from non-CMA to CMA + * pageblock. + */ + if (order > PAGE_ALLOC_COSTLY_ORDER && async && + !(alloc_flags & ALLOC_CMA)) { + watermark =3D low_wmark_pages(zone) + compact_gap(order); + if (!__zone_watermark_ok(zone, 0, watermark, highest_zoneidx, + 0, zone_page_state(zone, NR_FREE_PAGES))) + return COMPACT_SKIPPED; + } + if (!compaction_suitable(zone, order, highest_zoneidx)) return COMPACT_SKIPPED; =20 @@ -2534,7 +2552,8 @@ compact_zone(struct compact_control *cc, struct captu= re_control *capc) if (!is_via_compact_memory(cc->order)) { ret =3D compaction_suit_allocation_order(cc->zone, cc->order, cc->highest_zoneidx, - cc->alloc_flags); + cc->alloc_flags, + cc->mode =3D=3D MIGRATE_ASYNC); if (ret !=3D COMPACT_CONTINUE) return ret; } @@ -3037,7 +3056,8 @@ static bool kcompactd_node_suitable(pg_data_t *pgdat) =20 ret =3D compaction_suit_allocation_order(zone, pgdat->kcompactd_max_order, - highest_zoneidx, ALLOC_WMARK_MIN); + highest_zoneidx, ALLOC_WMARK_MIN, + false); if (ret =3D=3D COMPACT_CONTINUE) return true; } @@ -3078,7 +3098,8 @@ static void kcompactd_do_work(pg_data_t *pgdat) continue; =20 ret =3D compaction_suit_allocation_order(zone, - cc.order, zoneid, ALLOC_WMARK_MIN); + cc.order, zoneid, ALLOC_WMARK_MIN, + false); if (ret !=3D COMPACT_CONTINUE) continue; =20 --=20 2.7.4