From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 96CA5606A31A for ; Tue, 1 Dec 2026 15:46:23 +0000 (UTC) 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=zYdgEYGx Received: by kanga.kvack.org (Postfix) id A918A6B0604; Tue, 1 Dec 2026 10:46:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A68F36B0605; Tue, 1 Dec 2026 10:46:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 957976B0606; Tue, 1 Dec 2026 10:46:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 6E96E6B0604 for ; Tue, 1 Dec 2026 10:46:22 -0500 (EST) Received: from smtpin15.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 56812A0F17 for ; Tue, 1 Dec 2026 15:46:21 +0000 (UTC) X-FDA: 85495840962.15.7DA3F07 Received: from mail-pj2-f3.google.com (mail-pj2-f3.google.com [74.125.227.131]) by imf31.hostedemail.com (Postfix) with ESMTP id 8E6482000A for ; Tue, 1 Dec 2026 15:46:19 +0000 (UTC) Authentication-Results: imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=zYdgEYGx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf31.hostedemail.com: domain of wjl.linux@gmail.com designates 74.125.227.131 as permitted sender) smtp.mailfrom=wjl.linux@gmail.com ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796139979; b=2FOCDLepRQJTff4e4z74L472PTyAQltS+WbeMCaO6f32wMp481O8B5GxNUXF0HjBnoQSRs U/rvI1WlrWRMHIDGAjJgwBvzxgBjRDJmgab8JFh4oSSfiGKeE6kk5lq82s0c2qHu4iDOOF jGtrVN1D5nhun/7G3jBdCafSxWY3LMM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796139979; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=IaFJOazx9GCDtxXnSyUoxveg0VQqBqoT4PdC2SdIubo=; b=gEJkcxOC2iv5/b3hMH4sc6qDtEYHVI5+zqF7HlY7es0JLZ/DuPzlJT814Fdru+bXGj7M8Y anZYbaWURM3h0c0V4TUw3BFYXAa3AMrRLGO7THcS62DUnKsHnOUBRsq1WMc1OWnMg46KBN Wg+ktm6TkkZNIP4+/OuFX0h6+ftDNUo= ARC-Authentication-Results: i=1; imf31.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=zYdgEYGx; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf31.hostedemail.com: domain of wjl.linux@gmail.com designates 74.125.227.131 as permitted sender) smtp.mailfrom=wjl.linux@gmail.com Received: by mail-pj2-f3.google.com with SMTP id 98e67ed59e1d1-3b497279bf2so962015a91.1 for ; Tue, 01 Dec 2026 07:46:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796139978; x=1796744778; darn=kvack.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=zYdgEYGxIRk6MSU/vnKknT8EVn3vS1+X0L59OOKxcwE5jErjZgP+4XVO6306Exsvtq gWT1qK4BamNIubuP0vJWLqZ7bgm0GmExAbQcy0rExIw/2dte/lpJh2gTnH5BG4aJCyNX FlgB9mzSGkaXU6yyH6QZWLkprpX5ciY+YrFAigS2DfgYqwjMsLHf7j4OmjWCNZb7vzty 6GIb7BammIBjOp62vXUyqOPFR0aHPqVt/pAHMdsWSFPPSZuxPlvDVemMgCEuCjOe/7oz NpJ+WhK+zPD6g9bEuN7Tv9hFBe9ny/TkuSx/hogcOXW8wKJ5FpkPyC0kbgHvXWglSVcm tkuA== 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=dFZiFSVUhoQFQ5byO+K7hr8duJL84pdL4tgqQpvVdXfvp6QdnLsrG7/ewe+c3IQqXW 8qgZrpQVIIIGOI9/Gqf3QHHcoPsLfnSaA9Bngn94QJE81jeBtW7HdadE24y/BjMNwpry 5Arh/LXFY2EDR3BZbCdA8dCpdcdzZ7dVDWo7L3dDOea+Cffnmgz/EiraNK6bRC/SZJWT NsDSjo50KyLPaDqxBXBs3nh5k3BsGP3csaXl8LauvX5sMGKSoBb4XRxgEMhEWosh6CLb BAwJN8ijqvS8aRUAKOxd1AvsvREJiWgfe64f9Q9USH/aeno1nWXe7km21YYwQoBKm+hR 4k+g== X-Forwarded-Encrypted: i=1; AL+hErF+rstvEF+aXmQaAqhRFwC20aWvx6UHi1UZvFylsZ8DNYn0J0/BzHkZ5HLvzGMd2eG0u3wrTqF8XA==@kvack.org X-Gm-Message-State: APuU4Z/EV/Wny1efnZ5c9xRZ/KLZ1L2zZFcQLrmOPKjAYhInHjDI7fNy jcqEmMwK3gqZujgv7BKpzmi0UAe6qMSDnimyyeLd2f3WuRRKachKEQ1M X-Gm-Gg: AZFdTZdW2SU1zAWj7XXkDh/IS3Iv78uNpQ+WIDaBFZGpVw98J2pA0QRvLeIyOZo4pF8 Dq4J5owsrzUSA8smJVSabtIDi9SbUdIVQ6f8uK07DBlEN9OScO+HrQIxTsJSClpkT8verCSt5/f sP936Wv0iOW0mv4HG+a5O0Z8ooz7vTr96eR1vPOaeWC1nFw4HiUS+vXr7pcaLUrIQTgzjN2BQCT xfmxmPtseGbklIw6G14JSI0ZmSYW7WppxqQwSdTFmePSbkmeMlNmjwxIEhtMm6I09pjxmpKe/8G 2iawGFXGfK48kREVxToCFMhCerp+EMqwS8YGM/0AwzIKoK3x8nhyOkG1JjCx4SDw7T91FmDOQ45 fcEXXYOEZ+w5CS88pvmy+LCqGBPc80+epJBRGjlprbUnEBbXM0j/YGqgigR7hPc3Ht92z1R2FoY WNJFLT3DHigXYV7/U2zXjOUSAU2DjV1q5esAAPfapd1IMEJRDJ4xdNw7nUquplVYQdu7a1T5OjS 3OfoFCxejdRQFrFf1z28GuQD29n004PVDBuxcY/VXdfrNOdyB64D6hEYHY2nFTE8KMCFgFnA7gl 2EB0ddxFoOST8+ZeXNkMKSgb 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-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: z59ysy8gak3t6oi6sp6i1sdu75isw78e X-Rspam-User: X-Rspamd-Queue-Id: 8E6482000A X-Rspamd-Server: rspam12 X-Spam-Status: No, score=-20.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MENTIONS_GIT_HOSTING,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796139979-123992 X-HE-Meta: U2FsdGVkX18UXp3EUjZaBPeyzGhtby5zuS2F+4RCArHGqbZ4g6a7NBxghu6kVMt3B3/E/vxZ1Oj8eRNabOuuYz576L5NTSRH1+pzMtBMcHCTQZgga330kdk0IVMz54+iJYcNrjUp2kR6PkQQoAQxr3xQETc/szej7auWzgr9m4m68urCVvPb5u5sAL3Kg1nfwPqDZ55XE1Xz2UBzSCKrc4Skcz67s+5CN1fOA9SatOAfhrauMii344A+eZpna6FtZXBEG4PRVtlGCu7OWpiFLxW4oLi5Egy3bP/7XAqSKRfith7yFvPScfz6X8jQTFWUi+s2O5IFbTcmmyQyWT/1VvP6a5fQQGVwQODJEF5c7ZYGAKjo5n1ucYJiFGYs3D8X5JBjN5IItGzkg0a0Dytz92SKQuQI75mhLPZnW/6hcIrRajFMLAxukmQjTUgZ++/TUP8xq2L3+wAof2hRMe5w0KtYdMARUczKhZ5u4iu9VDWhvozyYPtBZb8fpfhjoX8bEHrIIzWmineN2RZDkZCeaFPahqLhmCF0cvSHbj+33Dw3o7i5ZT7VuTDm3pbJljXgXzmbluy+LL3puzEnC+e+BKKHXEwpKg7Y8Bv2CAp+NdXz7LVPOUXPhDBAL2UulpxoyzRW6BQVX9xVz8FlVMbcWMGAQXqySqsMqTyznzmxcL+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4CAkcZLMmg== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 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