From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.134 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796225655; b=MWdd1uvmNCBRXHJerxkidPuX/e+6Ercs/sPFF9mVuj6yYHBuYe1T0W9DhaEBHpBNw5tW FNWKPz2ubx7/1oq6q1V9yWy8jvcvdy2A7SKF45gyH+TMS7g/IsTOT5Rae7tgE4zupl8yt YrpeadBqkXOi8Q4HOXSEHneQxMmw/tNAeGt2WypkU/B0yQASKwzkjWKdq80KaxlHFZomi 4NAUdwrRCzqvICEezKT/Vf2k1mGc9wf22cXagQPy5GO/BXwoIIr6o3xrKIZjEuWOyKHGZ hrx98fQbDXqRsYc3EExtghtaK8d/XsKzpUHRjwRUeGQL3wHq8RttR1rdQLG0xfFsA0g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796225655; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=4ITmIEPHs7IF14usQyMj9G50C201bSkcKPKMx38e+VI=; b=gvlFUdKGLsfs5yO/pDsiqw2OqDBefRKIDlpP/xHVntyM6IWzNnU1TROlU2w6T/1CnpZs DmlbsyIpxMTrWQRmorI1dBJs8Mt58vuaOV0cjLYu2JgmZN4P+nJ3sI2MANTzleO5x2/pv DkC8FRt6Y6xXvHSN5/Od2f65pRh+7VzzrbTo4Cyn6wHzXYf+k96wRVf7hsth4WzGcCHMl rimQMcE/uqNqNRdRxM3XhzXDsIKP/sCoN/3Jd8S8ZGMlXyzQQmY+GzQ4yHHDjnOxYXiuS FMZtw3sjHCplE7hSP42juHDQNU6nm6rNvG4Z62RmYqJC8LjzkIGEIFNgkyWULCA4n3g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=sgY6cA7h; arc=none smtp.remote-ip=74.125.227.134 Received: from mail-pj2-f6.google.com (mail-pj2-f6.google.com [74.125.227.134]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BEFA9606A323 for ; Wed, 2 Dec 2026 15:34:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BEFA9606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=sgY6cA7h Received: by mail-pj2-f6.google.com with SMTP id d9443c01a7336-2f08b80a840so82765335ad.1 for ; Wed, 02 Dec 2026 07:34:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796225654; x=1796830454; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=4ITmIEPHs7IF14usQyMj9G50C201bSkcKPKMx38e+VI=; b=sgY6cA7hXImAXo1jFC2EgZkuHhdsugWWzMPkKLPwPkLjYDoCbLPQ5fGIDhhV4B8/zx iTBbnJdW/s+sCNAUkaexumADnd/XXEuPDGVozLtqivvS+xJIP3PdYWmja/0MBnSFutna unvItPj77iE73CD1Lq2GFgzM34AXF65ZtO9ojnnmTOtA43e5S/OV5QH2KrEfsB6yz1l5 Y2mA+Z6toKJiSIEp6YKwHZkLRSJoqm0foFII2ibkgo3rHviePGN1uFVm6K9zd4CxwNNz o1TZBsV8xRo46QrbAZlkFbHFc07D6BmIeVX8pHicE6dHzL/ggka7wWsXNfrFagmUyrIv F4EQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796225654; x=1796830454; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=4ITmIEPHs7IF14usQyMj9G50C201bSkcKPKMx38e+VI=; b=2PlHmIwEANA4XzUolaJnerWinU3e7sRN44Eyzxb9hsxGU0OqVCGZ84VfP9CxG073rN zJpL5YUZinZF4FFEpA869OZ6GUY6B15sh6jDwyzexOFbOPD3W67+D1BnKy0bEHIRlO9I 32qBGOCwuNMySr3+vB2U5cMhXwTeG8JSQQFiI5WW1ZsKoAY6pvKzWFu92NP6D6iVcEo+ r765S+fyZbAfKS86JB+rtnwx3Bt+E3ZhS6N91Zm8amxjNIVXXUleB7npI7NrQT8cs2bD 2m3KQKxqgZ9Z9t3ywu6NBg4sQr772+8jP+eM1tRMowEQ7i4w9I5f5Nn4R2PIm+ho4MKz gevw== X-Forwarded-Encrypted: i=1; AL+hErGmb6d9VdM9VGcNhFPE+JjSD5h0QWDsHCl7Cej5XrQxnmWZZF3CGSQ2vR2xpgwo7zsxDqPUZddKRrKsnF4=@vger.kernel.org X-Gm-Message-State: APuU4Z9BdOg/VM/KcKMUEFwtzswDJX7Cmdumhmy+lINcNkF1QrQ7JD3G 5XuI7w2nBIO9HGCXHzyf9Dipo4feLEQ4wqnrjdWlwIOSuTL/Hzt7BIRR X-Gm-Gg: AZFdTZex/ttNbn4RSP2ymfBaQu+TAimUwNJQq2+0JI8mDhx0mWaKcUfa2i45PYdKBnJ qS9xJ0wYfiLWs7K+wD2TC9XpuXSvIlRHZJPVcI33I0gf76Hb98yrBTQR4R1YP8ZXjYx68lHddDz jgoVVTmQR12jhAsVoafiwEeYXTwTdWwQP0p0wJVxdKrFYQRKfk3yG9EWSwg6Ju532HF3Dg+cqcT xDBbUC8LtMuGodl0SYPZ5jzv+yDkiCbfNX0Spz6gccGpOYC0l/Pswrqyvy8r/9YGxIaiajhJoSV KV506ypLz171i0YKY0ge3R2cRf3lSw0PCfJY3dq1+h1G+0ERiMQ9ssOQBFGgZQlvor5msNyNj+a 7CUgCnCseTIQfQLpUIFyyEwSKc0TywCQTuvrbc4sy6fXEtUDdpRzPUGfHHdxBTAj434f2NLo1uZ JMRZSbHYJsCv+PckB3GYxqjGYFPdNyURsXJsYWRwEv0Y7jcYWcxRZH72H1gRJqjVtGgMvUlxfx+ WBDiCh9YxoxXwlk0GmCdVBW2Q/TClRpJLN+cZWxDYjR7UnTDcf/OW8UmyPHWWHkQtO2VcswOogC hdPLWKUkCpmetucPMt99tVunjlhYm7+sxPX0o2BCsq853A== X-Received: by 2002:a17:902:db11:b0:2ef:eb06:387 with SMTP id d9443c01a7336-2f03f246dc5mr442174265ad.47.1796225653703; Wed, 02 Dec 2026 07:34:13 -0800 (PST) Received: from xbp14 ([101.82.143.240]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f0bc695f31sm147605695ad.49.2026.12.02.07.34.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 07:34:13 -0800 (PST) From: Jialin Wang To: harry@kernel.org Cc: akpm@linux-foundation.org, brendan.jackman@linux.dev, david@kernel.org, hannes@cmpxchg.org, kas@kernel.org, khalid@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mhocko@suse.com, surenb@google.com, vbabka@kernel.org, wjl.linux@gmail.com, ziy@nvidia.com Subject: Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation Date: Wed, 2 Dec 2026 23:34:00 +0800 Message-ID: <20261202153400.274475-1-wjl.linux@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit On 2026-12-02 02:19 +0000, Harry Yoo wrote: > On Tue, Dec 01, 2026 at 06:48:01PM +0100, David Hildenbrand (Arm) wrote: > > On 12/1/26 16:46, Jialin Wang wrote: > > > Migration fails while a folio is pinned, so isolating it during compaction > > > wastes CPU. Currently, isolate_migratepages_block() only checks for extra > > > references when folio_mapping() is NULL (anonymous folios not in the > > > swapcache). Folios with a mapping are not checked, including: > > > > > > - shmem folios allocated by cudaHostAlloc() via > > > mmap(MAP_SHARED | MAP_ANONYMOUS) and pinned by the NVIDIA driver > > > with FOLL_PIN | FOLL_LONGTERM; > > > - shmem/memfd folios pinned by VFIO with FOLL_PIN | FOLL_LONGTERM [1]; > > > - other folios that can be long-term DMA-pinned, such as swapcache > > > folios, ramfs folios, and file-backed folios pinned read-only > > > (without FOLL_WRITE). > > > > > > During direct compaction and kcompactd runs, compaction repeatedly > > > isolates these pinned folios, unmaps them via rmap, flushes TLBs, fails > > > migration, and restores the mappings. On GPU hosts with lots of pinned > > > memory and heavy high-order allocations, this makes direct compaction > > > very slow and keeps kcompactd at 100% CPU for long periods. > > > > > > Skip these folios early in isolate_migratepages_block(). To avoid > > > hurting other workloads: > > > > > > - Do not change cc->alloc_contig (such as CMA and virtio-mem). > > > - Only target FOLL_PIN, not every extra reference (such as FOLL_GET or > > > short-lived pagecache references). Checking for any extra refcount > > > could skip normal pagecache folios during read(), or folios with extra > > > references that can still migrate (such as AIO ring folios). > > > - Add a helper folio_likely_dma_pinned() instead of calling > > > folio_maybe_dma_pinned() directly. For folios without _pincount, it > > > uses folio_expected_ref_count() so a folio mapped many times (around > > > GUP_PIN_COUNTING_BIAS, i.e., 1024) is not always treated as pinned. > > > - Check folio_is_longterm_pinnable() so we do not skip folios in areas > > > where long-term pinning is not allowed (such as ZONE_MOVABLE and > > > MIGRATE_CMA). Any pin there must be short-term. > > > > > > Also move the !folio_test_lru() check right after > > > folio_get_nontail_page(). It makes sure folio_mapping(), > > > folio_likely_dma_pinned() (which may call folio_expected_ref_count()), > > > and the anonymous refcount check only run on LRU folios. If a page is > > > freed and reused as a non-LRU or typed page, we will not inspect its > > > non-LRU fields or risk hitting folio_expected_ref_count()'s > > > WARN_ON_ONCE(). > > > > > > Benchmark results (8GB VM, 4 vCPUs, 5120MB of 4KB shmem pages pinned with > > > FOLL_PIN | FOLL_LONGTERM, 50 x 2MB GFP_TRANSHUGE allocations per trial, > > > with fragments memory, mean over 3 trials) [2]: > > > > But what about short-term pins that are long gone until you actually try migrating? > > Forcing all long-term pins to migrate pages to an unmovable > pageblock would probably help here? > > Movable allocations won't try to isolate nonmovable pageblocks until > we're really desperate. > > Separately from this issue, long-term pins shouldn't really cause > trouble for compaction even outside ZONE_MOVABLE :) > > (The idea is originally from an independent off-list discussion with > Kiryl a while ago) Hi Harry, Thanks for the idea! Moving long-term pinned pages into MIGRATE_UNMOVABLE pageblocks makes sense for reducing fragmentation. I tested a quick prototype of this idea. Here is what I saw in testing: 1. It does not fix the compaction problem. After pinning 5120MB, folios were moved to MIGRATE_UNMOVABLE. However, compaction still scans MIGRATE_UNMOVABLE pageblocks in common paths because suitable_migration_source() returns true when: - cc->mode != MIGRATE_ASYNC (direct compaction switches to MIGRATE_SYNC_LIGHT right after async fails), or - !cc->direct_compaction (kcompactd, proactive compaction, and /proc/sys/vm/compact_memory). Because pinned shmem folios are still on the LRU, they still get isolated and fail migration over and over: - Direct compaction benchmark (50 x 2MB GFP_TRANSHUGE, 3-trial mean): alloc_ms: 34,846 ms (vs 962 ms with my patch) compact_isolated: 57,447,043 (vs 32,272 with my patch) pgmigrate_fail: 57,343,328 (vs 72 with my patch) - Proactive kcompactd (proactiveness=50 for 3s): compact_isolated: 1,318,427 pgmigrate_fail: 1,309,575 - /proc/sys/vm/compact_memory (MIGRATE_SYNC): time: 842 ms pgmigrate_fail: 1,309,575 2. It adds large pin latency for already-faulted memory. When pages are faulted in before pinning (common for apps that fill a buffer and then register it with GPU/VFIO), they start in MIGRATE_MOVABLE. Forcing migration skips gup_fast and copies every page: - 2048MB pre-faulted shmem (all in MIGRATE_MOVABLE, no CMA hit): baseline: pin_ms = 13.4 ms, pgmigrate_success = 0 unmovable: pin_ms = 698.8 ms (52x slower), pgmigrate_success = 524,288 - 5120MB pre-faulted shmem (fragmented, 844MB CMA enabled): baseline: pin_ms = 598.3 ms, pgmigrate_success = 342,896 unmovable: pin_ms = 2,550.1 ms (4.3x slower), pgmigrate_success = 2,181,701 (1.66x per page) So I think the two ideas work together rather than replace each other. Even if we move long-term pins to MIGRATE_UNMOVABLE pageblocks, skipping DMA-pinned LRU folios in isolate_migratepages_block() is still needed whenever kcompactd or sync compaction scans those blocks. -- Best regards, Jialin