From patchwork Wed Jan 8 11:30:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ge Yang X-Patchwork-Id: 13930598 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 AF12AE77188 for ; Wed, 8 Jan 2025 11:31:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EACD6B0095; Wed, 8 Jan 2025 06:31:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19A386B0096; Wed, 8 Jan 2025 06:31:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 060A36B0098; Wed, 8 Jan 2025 06:31:09 -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 DC6496B0095 for ; Wed, 8 Jan 2025 06:31:09 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 6867EC10AA for ; Wed, 8 Jan 2025 11:31:09 +0000 (UTC) X-FDA: 82984068258.01.0877451 Received: from m16.mail.126.com (m16.mail.126.com [220.197.31.9]) by imf29.hostedemail.com (Postfix) with ESMTP id D02EF120007 for ; Wed, 8 Jan 2025 11:31:06 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=Esll1q0y; spf=pass (imf29.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.9 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736335867; 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=eM7pL9XaBJnkbVmBdztVEP4QzvnS9NzTO6UxarK4cOw=; b=tTdttui+oKB2BRVAJxW84DJzku05CBr+VoPBvUrU8ZixRB+rH3uxyCe5402wFeLQvqX14U 7Q5JhpPmokEWcMGyapatZLc3wY70ZKb3zT4MY+al0CVXluF3PE7RCvkMG+O7wb83xs/Fgl qbWumf+bCLMAO9ngXIcgrHFhl+oZ6yM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736335867; a=rsa-sha256; cv=none; b=b1MwyFiE0AzKCOfepFLKH/zwpqJiSQ6Ygxrj99DADkTy27TpMWwAq07EGyUEBRHqHn2+uw +4mdH05A3rQOelSJbhkz26mmNm5/3mz+pNFv4uI+qUztxsaDxO5eYFPHE7JUD/w2DV86F6 +olXcQRBIhwPNvQPIRUX7ceT+B8491g= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=126.com header.s=s110527 header.b=Esll1q0y; spf=pass (imf29.hostedemail.com: domain of yangge1116@126.com designates 220.197.31.9 as permitted sender) smtp.mailfrom=yangge1116@126.com; dmarc=pass (policy=none) header.from=126.com 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-Originating-IP: [112.64.138.194] X-CM-SenderInfo: 51dqwwjhrrila6rslhhfrp/1tbiOgfOG2d+U2bS3QAAsL X-Rspamd-Queue-Id: D02EF120007 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: g4ft63k3m5wpxbwr9n4619m8dbhhgnrq X-HE-Tag: 1736335866-623287 X-HE-Meta: U2FsdGVkX1+TMCvmjQG8DfTJu0w//zT01tm2u3YDnhNZct7aH2fqhXEbgmK8trZEYlnpxbr1o0Wp8aNy2TfFmOQoMxPkuo7Z5TKwYd/lKuMp0b4zc8moSwWiYdtouHrm2fPw4oJ1bhaehi80JgaWeJZ41Ctg3YiJf4tEXBB7A0wZvBcbB/rLA5z2lWlovZJ/odbQJx9TLNC3UY3N4h5blSLb0P3hsayoF3qy7Rmu/1tvUF3Xpvh2Fxv/4OVWPiLN+bIvdlLPIJdSGDQswIeRIbFDQHNRsQDiViHn11ObEdVd5JoZrTtKZOvkYIURWwj2U0T979A30tNjMgKr2JV/E7m5jJsaX+V873H4LgLYU3K09mwnWXNHRMIdzS0HZlwdQE/CVJ6IeHOIMKZk8J8noynZn+CWArm6UeqDAu2PewFC7tKnxEBVb1lFHxTj0gQStPdiyIyrTkt9IWlk0+x+PkHOufOdQgJYtWaJCinrBr8cWVXHroaYBIXyX27r+mdqoSRdk3UOW49iwyfguqdf2MyVb5lbzAMGzp5RbgXx/GfPMD2SWvUC+HxJ0eWJGs3ycYaVVOn9ZXTYXecU+XLWtxcSN2pt+4nvWtUDoMM8QqQYRcnWegTofKmgu1XNqM2sns8HIcDHIW2KHGro7C6YuLcO/w/OmxqVnuEcW/GtIMJktH+q6aV2NoiIwpjqBUXZMuP+dQoUGMQCm/dw4RumexG1i0nhsvKOm72f2FXSgHEBC5fzwbjzq8E0QgpeoTrDYrf+er8U7GB1AV6gEsvbjhyJRfb41zLWM15ZnVk6xzxlS6dOjhI90TMLL1iB+UvR+YLpL4FxecoLE8xxZZt81d7xUbzPvlg9iYmZ2dCEN51wGtlUsvFYv0nxDz49q5pYZux8sG6FcLfZwnTwjQOwERkExa09G2Rl77rX9lvypMUhiM73cQ7Sq5MPztkScOBMKPA9g5jD1ymavbj+r65 CmF2S588 wLm/lOEWnPQqT001HF61p1CzhV1nLBi1B626dm96GgLZqhxgP/CJdJkfLsJE0JOvWpzj/9gJQKkRORPIbL9GyV9fLT0FzfB4PjjaYW3xul8DRv0nWG63gktIcx4fVcT+Qbduhf9mvdXUahaD/1gc5z2nfEPHxsWCVICD15o2T0rso4BqhNnKN3ruJVRMgnZoW0qiSXccXHAPK3qbwzdXcr54wFkwLy7WICMuNo12jdKL+YUS9U4WdR8wsVrh4j6YJrHtz9LYIJee66/LhpkAG+EHOTw== 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 --- 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 = 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) + + node_page_state(pgdat, NR_UNEVICTABLE); + + 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