From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.227.132 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796225505; b=emQOcjJMyoX6brnWLczJf0hISk02OId6wo7HTaG35NgYBdWLb5/oEFDe3++73MMHGOPg hcXBWSAxZ35GQ9WG8UfPBd5YB7w46x71dIJ3tOKm6STufiG6Lv2vV4lfESHEf0L0KN+CT 3bOkFy0Isb0pDO/ya+J/4nnwy8pgWrf7fTz6JVK6MAfBnaxydodPUJs99pySWiilCHnsX qosmZbJ3F6gHrsCEeLG29XajVAdx3VYyVHVEoFOWX2UFF5xaNhDRykeQbmP9GNVzc0c3G 3gvModKPBV3RXPT4SonLGrE/9npSkxtHCeqMkK26dlqGciwhZXyWa91xGXIeSzFTUqQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796225505; 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=7rrPAWP0sgIaHVmJ4hUzkedx7p7lZSb7DEX5RQi0QhE=; b=ct2YUIp/EWGvE1Jj2N1gY74+KLsFPu7j9EdZwH5W/EUzfbfi5cj1ZUbkVtpG3qM/Sf0w ncF5EDOKI6+IUzWqHnv/sf6Z0w16850z6Ei2rxuSZAiwTLpFDhkopTv9AZX6rzf8LlsPN Jxgq5+Y3GD8RWunE1NNWFM3Dd9BNZRzYAWyUzefsZD+oK0WO/sc/2RcI6Xvb1zkFCfA0U sx3fcKCswbxYPAYlUN7Kze1dKB9aonTLsVplyw2txGe1k+Uq3EMXiQFWRcn7tXt8jmw9w dJB6QlLfG4j5Kj1C4e9ioxjss0Fe3L1+P6esYfZ09oJxBzTANTHmHDsfxwpI4+AOvhA== 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=pZQVIcJT; arc=none smtp.remote-ip=74.125.227.132 Received: from mail-pj2-f4.google.com (mail-pj2-f4.google.com [74.125.227.132]) (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 64FA9606A325 for ; Wed, 2 Dec 2026 15:31:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 64FA9606A325 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=pZQVIcJT Received: by mail-pj2-f4.google.com with SMTP id d9443c01a7336-2f066a193bbso75971885ad.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=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=7rrPAWP0sgIaHVmJ4hUzkedx7p7lZSb7DEX5RQi0QhE=; b=pZQVIcJTrK2UWyJj1yeILFbbJ7TlN59eglVYJZcES/lKV65oc8fqSicNh/awM6+MYW W2VBLEpMnyoz6X7rtI1uz1MHtXt2htDiw6N7I/VsHvBwbXDcWm/dLCJTbyT0QFmAcgZ9 glgTHlbNOxyR0xstfKSSh9RJjfCCLIk3Paixv3SLPVOb0j7meWx4tgq07L0ujcKTWUh3 3vehIg3nOmJUMOpfH7M3xinG4VwxbinFOX8uLxet1FGvHrZ3YlqaIW1i1xo+KP+NuGSi nizksCy4Dxf6Cu0pDNA1015Xmd7D6opkPAAvrw9hpVPRwfeo5jDXIywU3ZtfGYsxqCvg XpfQ== 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=LIwORh8ZIwTTl7BP7OmFkTRcO6Kfpn0Ts7s/RKYDSRbF98oq4plbKI5MzJ1bNtuxWj PHm+FgA3qC1ltPK+Grn3AHRh+QJG/q4G0iqPCa2WC03+BWp2PfCGuJ3SSofhRJw7vZxm DcUTryBffx11KB4T3t12g149cULQWupGW6NjF0QT3v2+qCiR0LR9h2JN3xEhGoLdEK+g 4YTuhJNS75CruaZ+2zPR/SN7EOOBn8jszGYUs2cZnEEjUeyR2jF9THJ1am3z+LrbyXUa msAXV8dxT4BWcgAPK02CSb+ExIWp0PqYlCRBvAPFhphG3xc++QvOcBRktvtaBcOAbQXd +gtw== X-Forwarded-Encrypted: i=1; AL+hErHMCU+821Lz3ZsjB0KTuts7wNSLe0/gRyJq8UxrtIUV24qX6LdZeS6+6Z0bkwrby3l3/XfdB3NNb1dohVk=@vger.kernel.org X-Gm-Message-State: APuU4Z+5v/cLmUe/L/BPbADrLwlJLlIK1mUcJG1v9QZ2t+Y+tXSLqK8i gG2jVZj8wydmdSc5xoTzAeUc3EW2IuGn77GY7ty5PsihOJb/mtobvbdY X-Gm-Gg: AZFdTZciSxZMruwx9O55PKVETglGumd19e3LOH7yQ97aXJczC1rAzy8npUzZndc9u4o LDLRju02++gfucWgDj12NhjwUkfrukyFkXLSGmv8w8tJA8VLcmIEtxTIcsXdTf0kIETiGt/Hc3o j5bILe7/cPddYMprXJO4dSC6UPYK6EJR4SzqDKtkKul7xskgpWig4ca5eP57p+ZAF5B+72FbVgF AeZvzJe654d6/1TBFOJf+lDSv4vd16+XC/K8MIlC2TEJ05BDME57C1P1FQPwVDlNH9GBXWppsGY SoZqABopW+J6KthUtouCZrxfqmffrpr80fV7yLsHE7P7XJUrwulEZRwwsHhBBtxn7i5/GZTCiou SbTS9ZnDjLo24dOnms5caW/1kBBKsVmHlnPJ8du5McNY1ybRxb59fSFbzBnEjShzAfI/N047f+6 tr0yOL9S6YKjf4DElc58t4ETaWEwkiqxMt0RM4DwQrswLCqUsEISP6q3UfF/QXKpuyeLxhf8qWL RZZx9lxRDtyqkdsN+zwVebh/cjWKypd6qOOUwSkIzBi9fAdn53f52KAkw7SBBSgWCy17X6v/yRW xQtvyja3uAPh3q9BABwWsohL 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-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-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