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 75584606A31A for ; Wed, 2 Dec 2026 15:31:49 +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=VW4G3+f2 Received: by kanga.kvack.org (Postfix) id 846386B0662; Wed, 2 Dec 2026 10:31:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 81E266B0663; Wed, 2 Dec 2026 10:31:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 734C36B0664; Wed, 2 Dec 2026 10:31:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 2B0E16B0662 for ; Wed, 2 Dec 2026 10:31:48 -0500 (EST) Received: from smtpin16.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AFC50C03BF for ; Wed, 2 Dec 2026 15:31:47 +0000 (UTC) X-FDA: 85499433054.16.597871F Received: from mail-pj2-f4.google.com (mail-pj2-f4.google.com [74.125.227.132]) by imf30.hostedemail.com (Postfix) with ESMTP id DF0468001C for ; Wed, 2 Dec 2026 15:31:45 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=VW4G3+f2; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of wjl.linux@gmail.com designates 74.125.227.132 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=1796225506; b=gmuVflLP446mZXoPEXs1B5RWDgAtL5HZdj66CAypOkrIz/aIs7q1ufNok6nLiLx36UkYmx Py+9DidOSW0j3wK3jNAeQBGcVj+n2PvCnZDXVIwN/5xZmyGFwqEEbPLwjxnbwer1U81HdD G19lVQA3Ulh9gGaYYpBziTbVGNh/5oA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796225506; 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:in-reply-to:references:references:dkim-signature; bh=7rrPAWP0sgIaHVmJ4hUzkedx7p7lZSb7DEX5RQi0QhE=; b=A88LGJXZ1A6bHAJh4jclomHIQc2gW09jifQ3uz4ZRWu3nR3B1lSVMu7L8cMVnrIFLyJNsK uYEERq5xwgqwgKfRZ8FidEBchyMttBfIxUWiePzg2x9937y+LnU1FG+cWudjX6TWDn2D7v eBOARTfAOP/f1qwmUnY4N7bMTFXWc9s= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=gmail.com header.s=20260707 header.b=VW4G3+f2; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf30.hostedemail.com: domain of wjl.linux@gmail.com designates 74.125.227.132 as permitted sender) smtp.mailfrom=wjl.linux@gmail.com Received: by mail-pj2-f4.google.com with SMTP id d9443c01a7336-2f066a193bbso75971865ad.0 for ; Wed, 02 Dec 2026 07:31:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796225505; x=1796830305; darn=kvack.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=7rrPAWP0sgIaHVmJ4hUzkedx7p7lZSb7DEX5RQi0QhE=; b=VW4G3+f2LSZZabSmNgznfnZrpAxnI0Z9CIDs7EKduT8CyH8qPDpTzpTKmQeLVWWqgd yDfxDXzoSJSioJVfxXr8adRlNEH0J8u94BrLn8VOZb4kESCz1iZeSsG9iN2InDnZuA0w My4h9j1tpleJmkAdRk2BS7tTrxHbqJl4YtkTnywbbmJo9NTiQrcQ67pVtnkXSvNHXzWK SChcQzgag5T7kqUFJ/LE2NAGYfex2vmjoryjYNMjfOyzZVHnyb6I8GwnPR/1FMBaWHmA 584LQExekCA/Ys3ZdFhzTcCZEDJxG3t4aRYOw8ytjI8YtB8EtuibK3noz7eIvrDuxnrB Bx+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796225505; x=1796830305; 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=7rrPAWP0sgIaHVmJ4hUzkedx7p7lZSb7DEX5RQi0QhE=; b=2uhpScYrsNiW/dmlMRyjOnajLs6/uyeVDsx1t7sUZfhmrf3OgMyf6MNIuSdloeznZ9 OO4I8U8n7x6aFXlshrWVlzBQZJYaj5PQGor3GwAATXB5i2Gc1DaNAeEYW8xfS2DqRpAg R9Zc4QVYA+WFdZ5RxE+N3XL6W9FqPSsqGrf9NB/gGXDt6VCoBwcS58MKtlgrRHwh3xee WvBHOnYH2n0zFwtCQ5C8DNu8ffJ09vHyzQsvTTYYc2YDAc/k+iDmcSLLcpR/FgR37Wxk EV7N15jaOQL0eO4yfL9p3ez7XlWbqH0FauVFEEGY7Qp3+QxFF41lJeua3GI6EFzRY2un ktBA== X-Forwarded-Encrypted: i=1; AL+hErG0AUpDLmleI/jeSwHfaRi7SH9aj3ZGOya4Z9abCn8wNyt8JbP4TsMx1yk40fUTdnREQ41pptMH/A==@kvack.org X-Gm-Message-State: APuU4Z9hLX5Ei9U2GMuwici1lZ8tc3g/g4j6MzVBzZaGmj1DjdR2ikax LNaZC0k68ewNMS5WD1oIsV2leTPU+sIILR+0F0LE/1G3c/mryaVO6Gmv X-Gm-Gg: AZFdTZfrJtzvYBD71tiMV89L2SdOo6YGxQ208TzYBRUC+Etd+TtAq4gcnS+Aoc8NpY5 LnxEksg/50Pf7u4Zj9BDKgf1JvTA16Cb9SJDJ94IUZL5HIEgqJWFP9Qy95+AOQU3w1MsG9Yz6WW JvqgZchPOU9GYRUl/nXix1aYtlansLwhE7wt3q0pCg7VIqYXizoUa9LnNMXulabqFgk88D2E3KX m3K0WTqbE6wHwoVBjALG3AhdM4Cu+hVYud+8UPUcwJbbRcXIv9zV8m5i+XK9mjEpOEyqxpikcHD 5Zlz2ImaIIPttiSyjyv54h8INypJ97BCkhFlCYLVC7vwwrauZ4D+9APQn5dtlwd9hpqftrwVcla hb4W7acouVBCb7vod9VWJMpqmPV4HGWTyU5EfCRmGzw4fCdJkTNXLpg44aWOH09BPJf18PMUKIq rISc/wsslTb5qm0bu4XB+NfEeHkebQ02Q5mAttAdUUgNl/9nTlDTbm/SkpWdZn0jaK+TvPKCI4l 82cTE14ozAP6UpC21kreXDBpwaeD/YUqrkYWR8vHsRbALX40jSP6UTiS9V+Z83NjPrnCffPj6US ogHCajaNoUQAr4bO9DJW6NuD X-Received: by 2002:a17:903:98b:b0:2df:aed3:2ac0 with SMTP id d9443c01a7336-2f03f1cf5ccmr463440355ad.10.1796225504461; Wed, 02 Dec 2026 07:31:44 -0800 (PST) Received: from xbp14 ([240e:46c:2000:2eef:b7d7:5fb0:82e0:7c14]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f18fcd93f4sm255925ad.59.2026.12.02.07.31.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 07:31:43 -0800 (PST) From: Jialin Wang To: david@kernel.org Cc: akpm@linux-foundation.org, brendan.jackman@linux.dev, hannes@cmpxchg.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:31:15 +0800 Message-ID: <20261202153115.273095-1-wjl.linux@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <5cda167e-4e2b-4267-b6df-a85242684de7@kernel.org> References: <5cda167e-4e2b-4267-b6df-a85242684de7@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: DF0468001C X-Stat-Signature: 377c3zx9sah1ywi8se6dyu899y1za9d1 X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_PASS, SPF_PASS,USER_AGENT_GIT autolearn=no autolearn_force=no version=4.0.1 X-Rspamd-Server: rspam06 X-HE-Tag: 1796225505-828426 X-HE-Meta: U2FsdGVkX1+v4MUdKRWEjDuA+yyEMGLMLCT9fN6fOROekcW3ECuJXiHv3wIqWRHuoAmcvpOiwYSOeE3myiB8EePFoon9FN9AuURzOA+cZG1S/KC/gIIRXvd93eSIiI5IPQn83td7VmJ2fh2HboxkWjjdP89IjHbwWyMe9dn0040xj+Bmarfy6nwDuNSbDTH/45kQf1Q3QjWjS4IFoW2fm1FBqOqDH0T+bB1LQWQSN+FSNdqPKSzEL2/YBqLr8c5ei/nFTBY4++fcPw/L9VWkJ1/OEu0JUb1ADifc0QlMb1Oc8ZQaOpYKM9koYLCKzA1U+Lt8UgiX6qRI9rp/O0zqQ+njASVP7NAdjNvTk18rMJdQNI933gdPmmsUm9rjm+x/VWgQOLXPCpTrkwsvC1u+XbFn+HSDZAf4JQyOvl217dCTWrTS25C7Bs0wQTkpYSrr/xZkT/5g7xbkM15RV+oSHTkFgvBYMWnocOfrUAIfIeU58fBDiWJwFcYnXAmETXhM9a1GJ8IIVClcXS8Yh7Wquo/wAnDMCGu2U94+U9mf8/5JLlNmD3csUozZyaHdELUfG9S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M2R89oRNgnzK2cX32WEDH3nwScDJQdI0t8B7LoXM8TQtNNz70+JFmtTHY7l 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 On 2026-12-01 18:48 +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? Hi David, Good point. Right now we have no way to tell short-term and long-term pins apart on a folio, since FOLL_LONGTERM is not tracked after pinning. So a short-term pinned folio would also be skipped here. I think skipping a folio that is DMA-pinned at isolation time is still a reasonable trade-off for regular compaction: 1. Regular compaction (direct compaction and kcompactd) is best-effort. It mostly runs in MIGRATE_ASYNC or MIGRATE_SYNC_LIGHT mode with ignore_skip_hint = false, and already skips some migratable folios (such as writeback or dirty folios under ISOLATE_ASYNC_MIGRATE) to avoid wasted work. Also, the existing anonymous folio check right below already skips on any extra refcount, including short-term pins. 2. Unlike writeback (where MIGRATE_SYNC can wait in folio_wait_writeback()), migrate_pages() does not wait for FOLL_PIN; it only retries in a tight loop. Looking at the possible cases for a pinned folio: - If the pin lasts a long time (e.g., long-term pins or slower I/O), skipping it is clearly better than isolating, unmapping, failing all retries, and remapping. - If the pin is short but happens very often on the same folio, skipping it is also reasonable because migration is still likely to fail. - If the pin is short and rare, compaction is unlikely to hit it often. Even if it gets skipped once, the pin will likely be gone on the next scan, and compaction can just move on to other blocks. Your question also made me realize I should probably limit this check to (mode & ISOLATE_ASYNC_MIGRATE) (i.e., cc->mode != MIGRATE_SYNC), just like the writeback check below. That way, direct compaction and kcompactd still get the benefit, while full MIGRATE_SYNC compaction (such as /proc/sys/vm/compact_memory) will still try to migrate short-term pinned folios. What do you think? Would adding (mode & ISOLATE_ASYNC_MIGRATE) (or further limiting this to shmem/swapbacked folios) address your concern? -- Best regards, Jialin