From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.138 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796139979; b=Ng86YyFmKiz4xdCwqjxK2J5QYBTd+wPBo7Ev6MRqVzWs3bbSzq9xZP0sNZZYMtuMdGzF c3gMMc3kKP7kq8z0Hd3/3n3X8E20xnkD8nXpNmiZIt0I3unDJlOF6KtBJ+hA7EkCTA+Vi 52W165AHEgzM5KQ0UrfSk4nWgm3DADPFM79V7zoQ0fQ2cBCAxvxNvZ4sf1tZROZWKlJL2 UXkPMmnhLP0avKtijG7HudXBldxQEnV/SJFbk9T43lN8asTJ8U6YFvbcc535nhvwh7gn3 IYnq6gbkWLNkfo35dZz6WfrNHnH9YiVTggwV8+p8h416p6MYa+L8kb20zzm8i6Pwycw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796139979; 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:MIME-Version: Content-Transfer-Encoding; bh=IaFJOazx9GCDtxXnSyUoxveg0VQqBqoT4PdC2SdIubo=; b=HwQnF5ylVRVG6TOKGBTuN89Z6TtRNxFv+NdSLZbQdjXUqY2PG3Md4efyC1lrtYKahf5T s6Vg7NvY88qnVMKjWY4I/lWD/Fzo6M6ZWLhcPv91CBPTJUtKltKeEvhf4pkEiUq06IQ3V tRGk8U+kNeFMF02O0P0dQHBRDDhdLgd+bcwSoirTzSoz3JTUUews0FRXSm3l5iaWZPKaJ ry+MmyOPZHSLsh3AIan8KAqpCK9ejWCWJDPLORanp3oahicUT4Ic0KrQ4kuLLRwyT3RNz ag5sk6tOyzxaYAhbQxc/sc7RwTtqOxwe4itQRsOxTS2HIPYK+7lN7kebIpCbUy86/TA== 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="qUD/sjYf"; arc=none smtp.remote-ip=74.125.227.138 Received: from mail-pj2-f10.google.com (mail-pj2-f10.google.com [74.125.227.138]) (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 0016A606A31A for ; Tue, 1 Dec 2026 15:46:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0016A606A31A 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=qUD/sjYf Received: by mail-pj2-f10.google.com with SMTP id 98e67ed59e1d1-3b40a2025b0so842512a91.0 for ; Tue, 01 Dec 2026 07:46:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796139978; x=1796744778; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=IaFJOazx9GCDtxXnSyUoxveg0VQqBqoT4PdC2SdIubo=; b=qUD/sjYfxef11MwcvYABhQWhmW6IY9R78IXAkQrUQbUr28okPnFvGMZu6cHVCVJnqD 9VLlDZSSK1GKu8kAhxFy2/wSBh8VbFcAcqWz5nwhV9wOxQkcY/Foa9DBn7j1GiuagFhH 5wY65mU+HY0F20Qnog6T8pVqoXZrHD8WCCwYGppNcvhULlcIA4HebeIJe+YeI/zSozcr JHcih9CgN7lWEJJdeUoCuXTNcG/2TSF0NYwl4Nxy6Yv8aP+Qvu9LvG6QDXY7j4XqvU4k kl2fh15dzAoUpIHQQZZMaNS52EQBK2NdDwWJ7YDIEs8nNXRe26OWp4tfjrIJaHM65lBr ZXVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796139978; x=1796744778; h=content-transfer-encoding:mime-version: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=IaFJOazx9GCDtxXnSyUoxveg0VQqBqoT4PdC2SdIubo=; b=mcdyCPXzNve/nLZojYpdunaSCWMjHPmEkhtH/5+jpnpQ8Dgh2hL3N6a9F8105pXdec CiSPv7FS/LL31c40erIYIcc5Smdp4j5AOmio7wV7eSPTLC7XY7nnj225sPG8qxs1kvAy h+otyspLbb2kE8aHox5HKMfATjuW901VAkqOTN7e7kwBA58b0D6gJ2wnVY851oAaOFaV zYR1AheE6omnLmyP0NoiOtjpO80NR8tJEmHutF70bVAG1thYZh6IZI75ecj8BFxTNA/T BwwvN5Y71LlxEnfPyzJ/JkawvE6XGgwiqSiiETO6D9m/FcSJMiKRr+iZp7ldQo0O9h/m nS+A== X-Forwarded-Encrypted: i=1; AL+hErHGe5XXZCxn4mNxb6VDqsTAiuYXMkFnnEfTUgPK6yVo+XI4MFilaiVOIteERwLxgkT2Fz+57XRKCiidMg0=@vger.kernel.org X-Gm-Message-State: APuU4Z//jFr8tPnXGw4/GN1GHvZVkXsP2W7M64IBPfJ0BTuEWymBSHKF 1lnz6lfG10hLvkF144b+FfRfAK3384cWT3iCHKFSBvcrzLqQsEj81LlkItjod5s+ X-Gm-Gg: AZFdTZdON0i5ZdB7uyvACHbSbifNHvH2M2hZu8gwJJpG6WoPhHGvP7Tbv+7v7lIjPBe wWdnYJsySC+WudyT78oSZV+wa8NYP3GCrz26gA5ldyu1sn3UGZS6Kv5PizNTdye5SDiMRVS7bAc 6W7fcyPaJpaKtPr110cOkWKI2hY+F7zTaU3ba9L8o5EwyXwNlPFzdrx7Ot3/gNZFL6JKQmGDoqx oQlS8cn9zxfcqHDYBEXIBUYv7LVFTEIAQXQD37NPqB6RWG4yMkx0tvmoBejEWOO2WJbjRJCFBg4 jg+6db27oOJ2qooYlrcuv0EIzEkv9/FwTO3JbUnBqllDBCD4eoKBzhGJvupGhqDukav/gl6Tc2j YeP7500reeuyn9YbAV/+fTxoXAHL/9tYcnv+lN4m6qCcFVozdfSDpInhrE2/X0He0q4pQD0l00V MSrdHNL2bzGuERxFcLmW9RjVvRtCMuQAW0iyDPFChO1cbSPMjJ6OiUq5Yf/YPheo1S/0nq7GRGq vcm56zpROqwrlylMm6X5jlB5mmtwHyAFeWPg2IGv8VjEp76hSi1ehFrQy3sLrh+eCsKuHrtOuFb gnbtV1RJKhKfqhpmECDHf6fZ X-Received: by 2002:a17:90b:57c6:b0:3a8:786a:cfed with SMTP id 98e67ed59e1d1-3b41c9617ffmr28201433a91.37.1796139977968; Tue, 01 Dec 2026 07:46:17 -0800 (PST) Received: from xbp14 ([101.82.170.189]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b41aab4729sm57949321a91.0.2026.12.01.07.46.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 07:46:17 -0800 (PST) From: Jialin Wang To: Andrew Morton , Vlastimil Babka , Johannes Weiner Cc: Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Zi Yan , David Hildenbrand , Khalid Aziz , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Jialin Wang Subject: [PATCH] mm: compaction: skip DMA-pinned folios during isolation Date: Tue, 1 Dec 2026 23:46:06 +0800 Message-ID: <20261201154606.548937-1-wjl.linux@gmail.com> X-Mailer: git-send-email 2.56.0 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 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]: Metric Before After Delta alloc_ms 38134.772 ms 961.924 ms -97.48% (39.64x) compact_migrate_scanned 175,671,741 130,391,479 -25.78% compact_free_scanned 1,004,877 343,528 -65.81% compact_isolated 61,953,531 32,272 -99.95% pgmigrate_success 47,742 13,435 -71.86% pgmigrate_fail 61,784,613 72 -99.99% [1] https://lore.kernel.org/all/20230525191507.160076-1-khalid.aziz@oracle.com/ [2] https://github.com/hyuuko1/compact_dma_pin_bench Signed-off-by: Jialin Wang --- mm/compaction.c | 83 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 16 deletions(-) diff --git a/mm/compaction.c b/mm/compaction.c index 1ceee7a6e46e..9afed3f25b9c 100644 --- a/mm/compaction.c +++ b/mm/compaction.c @@ -834,6 +834,44 @@ static bool skip_isolation_on_order(int order, int target_order) return order >= pageblock_order; } +/** + * folio_likely_dma_pinned() - Check if a folio is likely DMA-pinned + * @folio: The folio + * + * Compared to folio_maybe_dma_pinned(): + * + * - For folios without _pincount, this accounts for expected references to + * avoid permanent false positives when a folio is mapped + * >= GUP_PIN_COUNTING_BIAS times. + * - Because it reads refcount and mapcount without locks, concurrent page + * table mappings between the two reads can cause transient false negatives. + * Do not use it where zero false negatives are required for correctness. + * + * Context: Caller must hold a folio reference and ensure the folio is not + * a typed non-hugetlb page, or folio_expected_ref_count() will warn. + * Return: True if the folio is likely DMA-pinned, false otherwise. + */ +static bool folio_likely_dma_pinned(const struct folio *folio) +{ + unsigned int ref_count; + + if (folio_has_pincount(folio)) + return atomic_read(&folio->_pincount) > 0; + + /* Fast check: definitely not DMA-pinned if refcount is below bias. */ + ref_count = folio_ref_count(folio); + if (ref_count < GUP_PIN_COUNTING_BIAS) + return false; + + /* + * Use half the bias as the threshold. This leaves room for the + * caller's own reference, short extra references, and concurrent + * map/unmap between the two reads. + */ + return ref_count >= + folio_expected_ref_count(folio) + GUP_PIN_COUNTING_BIAS / 2; +} + /** * isolate_migratepages_block() - isolate all migrate-able pages within * a single pageblock @@ -1093,26 +1131,39 @@ isolate_migratepages_block(struct compact_control *cc, unsigned long low_pfn, if (unlikely(!folio)) goto isolate_fail; - /* - * Migration will fail if an anonymous page is pinned in memory, - * so avoid taking lru_lock and isolating it unnecessarily in an - * admittedly racy check. - */ - mapping = folio_mapping(folio); - if (!mapping && (folio_ref_count(folio) - 1) > folio_mapcount(folio)) - goto isolate_fail_put; - - /* - * Only allow to migrate anonymous pages in GFP_NOFS context - * because those do not depend on fs locks. - */ - if (!(cc->gfp_mask & __GFP_FS) && mapping) - goto isolate_fail_put; - /* Only take pages on LRU: a check now makes later tests safe */ if (!folio_test_lru(folio)) goto isolate_fail_put; + mapping = folio_mapping(folio); + if (mapping) { + /* + * Only allow to migrate anonymous pages in GFP_NOFS + * context because those do not depend on fs locks. + */ + if (!(cc->gfp_mask & __GFP_FS)) + goto isolate_fail_put; + + /* + * Skip DMA-pinned folios (such as VFIO or GPU-pinned + * shmem) because migration will fail. Leave + * alloc_contig unchanged, and do not skip folios in + * areas where long-term pinning is not allowed (any + * pin there must be short-term). + */ + if (!cc->alloc_contig && + folio_likely_dma_pinned(folio) && + folio_is_longterm_pinnable(folio)) + goto isolate_fail_put; + } else if ((folio_ref_count(folio) - 1) > folio_mapcount(folio)) { + /* + * Migration will fail if an anonymous page is pinned in + * memory, so avoid taking lru_lock and isolating it + * unnecessarily in an admittedly racy check. + */ + goto isolate_fail_put; + } + is_unevictable = folio_test_unevictable(folio); /* Compaction might skip unevictable pages but CMA takes them */ -- 2.56.0