From patchwork Sat Jan 4 08:58:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ge Yang X-Patchwork-Id: 13926172 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 76830E77188 for ; Sat, 4 Jan 2025 08:59:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9BF2F6B0082; Sat, 4 Jan 2025 03:59:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 948836B0088; Sat, 4 Jan 2025 03:59:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EA3F6B0089; Sat, 4 Jan 2025 03:59:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 593EE6B0082 for ; Sat, 4 Jan 2025 03:59:05 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C071E120D79 for ; Sat, 4 Jan 2025 08:59:04 +0000 (UTC) X-FDA: 82969169808.30.1678C2E Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.9]) by imf25.hostedemail.com (Postfix) with ESMTP id 483C9A0005 for ; Sat, 4 Jan 2025 08:59:01 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=FE3PT1NF; dmarc=pass (policy=none) header.from=126.com; spf=pass (imf25.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.9 as permitted sender) smtp.mailfrom=yangge1116@126.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735981143; a=rsa-sha256; cv=none; b=yGe0hyCdlV7aYeHazewWtuHHz401G9Jlk4/eXa3DqeFWEIzRZXgzEfmAswJREj0rdtPcGC fy3SYPl+JEQOcWZx2+LewDMMZPRcJUmjgPaypNcU0kZL5X+WYeYU2ZeqSQf1uyWmNEXoCw Rz2o8ZanYakYWhLx5n6aGBqKUj9vAjY= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=FE3PT1NF; dmarc=pass (policy=none) header.from=126.com; spf=pass (imf25.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.9 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=1735981143; 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=xKC8wxHEhzJrtMlPhH7R+/FendVSFosY/WD8c1jrup0=; b=j7dOJoFcNP+0Fwv8DyV0zpztU/+VxRpwRn5FMEkIwCSOsyj4Q23Ydf107y5C9sfoazZypH B93HzJUNB/L2WaXnbtZRZo9qsilcDpu7w6rGAw8yWawNHCiwmZIWmh+dwr6CkKLs3hRyG4 meuRj3P1vddqd3g9IrVRS1o02mnnfIo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=xKC8wxHEhzJrtMlPhH 7R+/FendVSFosY/WD8c1jrup0=; b=FE3PT1NFxo0xm4AfHWbYsys6r0wwWXrnvR 6+FINoZrsV9hp/UOwloigrGnfCD+qTx702IWZYYZoaaSfKi3XAbWTImsU4JyL+yJ I8vA1qQXgxifCVexb5UTzW45WToan4Y0WJ/e05hpBUlBpuTIjmS9iio2garp+UOe nMrn1+TCU= Received: from hg-OptiPlex-7040.hygon.cn (unknown []) by gzga-smtp-mtada-g1-2 (Coremail) with SMTP id _____wBXrsdE+HhnV5FfAw--.4953S2; Sat, 04 Jan 2025 16:58:46 +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] mm: compaction: skip memory compaction when there are not enough migratable pages Date: Sat, 4 Jan 2025 16:58:42 +0800 Message-Id: <1735981122-2085-1-git-send-email-yangge1116@126.com> X-Mailer: git-send-email 2.7.4 X-CM-TRANSID: _____wBXrsdE+HhnV5FfAw--.4953S2 X-Coremail-Antispam: 1Uf129KBjvJXoWxZrW3CFyxAw15GFW5uryxuFg_yoW5AFW8pr 18GrnxWw4kXa9Ikw1Iv3WvyFyFya1fGFWUJF9FyF93u3ZI9FySvwnrt34jya15Zryjqr4Y vFWDuw1DCan8Za7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRoCJQUUUUU= X-Originating-IP: [112.64.138.194] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiigrKG2d49PclaAAAsA X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 483C9A0005 X-Stat-Signature: y3746ndfcbj7dmfrhm94agjdpy68mebb X-HE-Tag: 1735981141-308655 X-HE-Meta: U2FsdGVkX19SZi6F+i3vMVV491ZUsS0vwGq8jDWZZs9R5m8k+Vk3Ge9ajyQXbcEacJGcd42KyEH6gHzrlw4kmeVXv4zfuUL5plMfuUKugZ5MB5MxzKykGB3Pt7KXc2/GAetHbfUSdaXNrelxMGzNN4b1ZEWXQBPAkZB98KGghu3DZ6adnGk2kopPtXAvxJhRlV4Zer2/CVW2GIIWPkyzQZm8YZuCvRSYZ3sKeKVDucV/f5ciwSNHwPblgJA4FkNfCkH7I35wJRF7h2QA+nOV/QQXpo31+j0Pq6whF+aBHLszCylg5fHn6+fYS76S6lnxTwBZNsst2P/lKW9MoKzMM5+dxi2XTbwyRExPSkR30ue+AzeHX5t6/Cgj8CiHaQLEbZxHLi5gTe10yZlNPcvvw7NkSaXLzKW68D0WO5oD7SxKjvJ9UBJJPdlywyungkqzTQVtxWIHR9las/DWDw4OkyFum8tIy2cJGg3E56O7gMNV6EugHOSYCugtzQ3yXs/inmhj6/88qqaMUJrpnPh9yX72sUXR39hLWQpnQZV9htn6FyDhnEnQXXAXs5AfkREkRzpcMy8nFL5V+Y9hzaMxZSjt98vkNRoAC69tcgG3Ld/WG4MgW4uq5gEyiBomkqwO12rWv/9HrDJHxHU6tbpoMqEASFoBwrWj+4TYiGP66O5+AQZVrYuFvN5c9aiJ9/Sf95Aziry0Gi1yBSSvq+duY41Sdn8i/M5FlEcxNmzwlW8fQOwWziHmvmJ05u+vW25NIsDa4MYe10+P+rVA86F7IYsHOsiLkntOXYRgQ42PAjUilKUj+uzTfjlZ76zoIfGCfv9Sstd0Rw6sZbzxT2h+21FNfoc1sc2VzqKGt0IAL6sgrZDI/1YGsuY7B80yJ48/uwTwfD4rT3FmjMkc3u3NlCzTASkLQIdoaG+IXIFoG7cfF2RLRgjzfvhhL9dIOM4nmINyFrpie972TBiyxWF DxZS3urp bHvtleZ0xJF1N/LTR+wEUpCIDKDiICp/rN5m+ZvASq27/5NLkyQ+w8cFrPhz2XYINVtUGq+jITAQQXJtjJ11YbAChcNfb45Bwlkou6/w2jIyylBsIH+c+ONtHgzGpXn+5NGIIF6wEgsIBw809/vJ/SytIS+onbGPsN7eWZVUVGX+A0UYQhHulFR6/KqOz/pJ9NxlCuMuDL+bkcDf/FsyRt7ENXhhqOCc7Rb7l2lNPTAfBnSUJZr0Rh4o+GhIZ+DOQ4pOmni4AEm3Pf34aRHH3eahdjQ== 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: 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 == COMPACT_SKIPPED || compact_result == 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 --- mm/compaction.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mm/compaction.c b/mm/compaction.c index 07bd227..1c469b3 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -2383,7 +2383,26 @@ static bool __compaction_suitable(struct zone *zone, int order, int highest_zoneidx, unsigned long wmark_target) { + pg_data_t *pgdat = zone->zone_pgdat; + unsigned long sum, nr_pinned; unsigned long watermark; + + sum = 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); + + nr_pinned = 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