From nobody Fri Dec 19 19:16:00 2025 Received: from m16.mail.126.com (m16.mail.126.com [117.135.210.9]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 644891F8F0C for ; Wed, 8 Jan 2025 11:31:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.9 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736335894; cv=none; b=QOFSBTTIhqSmqoOqB1ifViQJtTQLTayzJMjlFgjRdMSmkdprLiCYllYqMKrzSiuPkZvovnm7cRPeFK2738wMJWh3mO+5dLbu9bCy+UhEvMVEkFaM+ghmDSjEP0z88yygJmZl5tXmq2av0sAgbxUmu+KICx92/7uPcNo2fiZhqbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736335894; c=relaxed/simple; bh=k/MN6QtTWo6bNlTSAEs2Ckgn8JmVHIAUDSKTQIsDCv8=; h=From:To:Cc:Subject:Date:Message-Id; b=c9srAfyn6ACT4MYtMW9BgId9Ap6nvyxbByWx3eoS5tF9/nrWoIAvAvVugTjw1vsS3pUgJSKFppBkAEVntfkPGG5N2A1LsFb2VKz487RoHXq+sy5haqP7QMrqkyHbnwywNf/Xz8/BnZuQcYRd0VGWFszzu6MdZXJ7ZZjnI+hAOjk= 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=Esll1q0y; arc=none smtp.client-ip=117.135.210.9 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="Esll1q0y" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=eM7pL9XaBJnkbVmBdz tVEP4QzvnS9NzTO6UxarK4cOw=; b=Esll1q0y36nJvKajjjFt6H2X2xq9T/kzd7 6Aq9YzhAoI+uFhjtkTaSw7/PpVjM0KhErqCz77fFc4ndjT9m2MRmYLUaPdWNGW+9 6iOYgWN0FY3Kwmjvz3YQEi1Xw6lgmeXbKgBG+4GXYsbBYealQYSZIXdWCvHbRe2E 97F/kFLbE= Received: from hg-OptiPlex-7040.hygon.cn (unknown []) by gzga-smtp-mtada-g1-0 (Coremail) with SMTP id _____wDnb1jvYX5njTaIAw--.17804S2; Wed, 08 Jan 2025 19:30:56 +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, liuzixing@hygon.cn, yangge Subject: [PATCH V3] mm: compaction: skip memory compaction when there are not enough migratable pages Date: Wed, 8 Jan 2025 19:30:54 +0800 Message-Id: <1736335854-548-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: _____wDnb1jvYX5njTaIAw--.17804S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZrW3CFyxAw15GFW5uryxuFg_yoW5CFW8pr 18GrnxWw4DXFZIkw1Iv3Wvyryfta1fGF4UJF9FyF93u3ZI9FySvw17t34jya1UZry0qr4Y v3yDuw1DGan8Z3JanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRolk3UUUUU= X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOgfOG2d+U2bS3QAAsL 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. During the start-up of the virtual machine, it will call pin_user_pages_remote(..., FOLL_LONGTERM, ...) to allocate memory. 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. However, if there aren't enough migratable pages available, performing memory compaction is also meaningless. Besides checking whether the order-0 watermark is met, __compaction_suitable() also needs to determine whether there are sufficient migratable pages available for memory compaction. For costly allocations, because __compaction_suitable() always returns true, __alloc_pages_slowpath() can't exit at the appropriate place, 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 When the 16G of non-CMA memory on a single node is exhausted, we will fallback to allocating memory on other nodes. In order to quickly fallback to remote nodes, we should skip memory compaction when migratable pages are insufficient. After this fix, it only takes a few tens of seconds to start a 32GB virtual machine with device passthrough functionality. Signed-off-by: yangge --- V3: - fix build error V2: - consider unevictable folios mm/compaction.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/mm/compaction.c b/mm/compaction.c index 07bd227..a9f1261 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -2383,7 +2383,27 @@ static bool __compaction_suitable(struct zone *zone,= int order, int highest_zoneidx, unsigned long wmark_target) { + pg_data_t __maybe_unused *pgdat =3D zone->zone_pgdat; + unsigned long sum, nr_pinned; unsigned long watermark; + + sum =3D node_page_state(pgdat, NR_INACTIVE_FILE) + + node_page_state(pgdat, NR_INACTIVE_ANON) + + node_page_state(pgdat, NR_ACTIVE_FILE) + + node_page_state(pgdat, NR_ACTIVE_ANON) + + node_page_state(pgdat, NR_UNEVICTABLE); + + nr_pinned =3D node_page_state(pgdat, NR_FOLL_PIN_ACQUIRED) - + node_page_state(pgdat, NR_FOLL_PIN_RELEASED); + + /* + * Gup-pinned pages are non-migratable. After subtracting these pages, + * we need to check if the remaining pages are sufficient for memory + * compaction. + */ + if ((sum - nr_pinned) < (1 << order)) + return false; + /* * Watermarks for order-0 must be met for compaction to be able to * isolate free pages for migration targets. This means that the --=20 2.7.4