From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796178001; b=0D8+wJ2E2C80IUvoK+c+fmHkAdd/DcoDHaVXBFbi8ioX9naWNlv6am3Jqz1Rm4yR0WJk tbXFuCVyMbBQRRAooUbSBnf8pXg7LQVB8cvOe5ZKBlvqMAuqK86mRAaJE40lPdv/qhD/A NT1A5W553+H2z5tvJujYen5H1OlwcHcKj8yT0cRmPaqmB9SXgn0jzSf6gtr7WuIF5KWAh d/cV3Bib41efSl+L8WDmeXW8Qv11RLVr/poeFHXRYWkIGM76RzX2L20Uo2Z0iB14GzW63 MbUxBLQrfa2Yjfl9SONIbZ/om5f6KHAWSd1g5Ujvp7UrZRpx28WsMTiYwZKri03yt4g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796178001; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=wwt9TYMvre0iTiGTi1qxkHS7JnLkBzBG5YdwoLHipnk=; b=w+2YQGJkRFYshDOLTq9xJJyjMTOkS9xOF3kaiuAt5yeqHNTx7bi/dyMqoEJDay8R0V7z 1hJ7adPBSWPXZ4QklAwBMAs+i4Djs/bGGXM2GIv6G1Z9wkDXgdDY1eTSpjozrE0/B7k9L CulXcS/JMm+YZj71U3HpkPMS3jc+JT4IlCLCvA/TEHIFalhL4+SGbuf3S3lk4oJYFXH6f uhpeXs2PTUiDUgC34iLRx3XhyCNzcVA87w0r6SD1PK35SP6aCWrTMr2oPyNHeSGn1ulBK ZxIA5pB+jE9ATphLwp5X51iFoxF7FgkVi2T9r/TOD3F7cfGYlLgkuduTWHNFgaITknQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QRsoba3v; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64D9E606A31A for ; Wed, 2 Dec 2026 02:20:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=QRsoba3v Received: by smtp.kernel.org (Postfix) with ESMTPSA id A13641F00893; Wed, 2 Dec 2026 02:20:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796178001; bh=wwt9TYMvre0iTiGTi1qxkHS7JnLkBzBG5YdwoLHipnk=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=QRsoba3vqTKokt6SjuRUdjI0TeNP+Wyn2wapJsgB2sZv3z740KmzsfKdB8Vk4JzBh 578MmHOQxMlZNlA9F0WQxwwbtv+k89VXrJoiPFuhA713fY/Lq94Cnoc9Pi0jiM1JSn +CU03z6IRooYSbsyrnnNo0M4BBDIsitqHCDUvg6FD/iq4FmRT2qejQEkFi75/TQ3bg ZT2/NerlY6R1J6GyVZfjVcbz3UBTvm9ZiukXIhgFPK47VU4IYjHi1kk5ZBdZXaMUJ4 DJngTyW0eGgcWSGbgbQuI+fgWK0jI0wbGhj4Ae7TR2EDlHf5cwnD1OSs3axMKYCEKM w7n+FWJ0RkaeA== Date: Wed, 2 Dec 2026 02:19:58 +0000 From: Harry Yoo To: "David Hildenbrand (Arm)" Cc: Jialin Wang , Andrew Morton , Vlastimil Babka , Johannes Weiner , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Zi Yan , Khalid Aziz , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kas@kernel.org Subject: Re: [PATCH] mm: compaction: skip DMA-pinned folios during isolation Message-ID: References: <20261201154606.548937-1-wjl.linux@gmail.com> <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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <5cda167e-4e2b-4267-b6df-a85242684de7@kernel.org> 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) -- Cheers, Harry / Hyeonggon