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=1795951832; b=HgRY+JIIkPNfDK22/f794fCUCwFnLqn88G38eZ0fDHCNSPph4eyd4p4jKJYWKHXWTjLS bLz/73QQah1xIdTG3A5aW+/DDM4YCegUU/lgkn46uZcMSxo6kB9JVwwTI0XSeNGIy+h9o p21Hq6I1EJnZYYmLnijQNRFUmsI5ZdpgSlBgbvnZHKULwl9gFDAZB2kYKI0sqwLcFg71S XaKJwyra0koZYqB8XZm06jpm62PjYVEwqPHKSmd9lSy8350gS3QmMmWMflcCyvt5Sks1u efyNDUpz93cnsDC3sMm3XCcHQGRBpOwF2ex/gg6cROsacBCbkjYiV/zWBOFBh+L/ehA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795951832; h=Received:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:Content-Language:Autocrypt:In-Reply-To: Content-Type:Content-Transfer-Encoding; bh=fjYqS2p8/z2/UdFkYPqBpKz4x/Hj6vCzw/l1wRLIPPM=; b=eiiTR9HkkGyGJhTcLHLnrVNlY6gEiSQXk24Gi7RN9yrnPiLP2TlGmrV1MfMq03wF5VqR OJF5G28u7PSgAUu/fK6lIWRuJ6PwPS/eHjkGTGhYGEniABBz/Kh5tzvtaELmCkv5/xGCi AIKdpKvMfkll2krxEKFo77XDRlpq7vZy28+vSnt14oOYk0DXH9b4UMI73lH7HCN85Mc09 Ibt1lSHsPiJZ22UgKqTIsnC+vJjWs6z0rsi1dgGmXSgXTUJ0/K+aDuPAfvl6dMPQLqQjT 5eAnMZQAA9VY5tALg97mRQDHccE8x71gSXjP/MbzrgTqu/6UyLIEYjSiKhLXCRQjVgw== 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=YVyXyYbd; 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 4B7F0606A327; Sun, 29 Nov 2026 11:30:32 +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=YVyXyYbd Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3F9FA1F00897; Sun, 29 Nov 2026 11:30:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795951832; bh=fjYqS2p8/z2/UdFkYPqBpKz4x/Hj6vCzw/l1wRLIPPM=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=YVyXyYbdtP+DIgJrjpuUkNjlym/yoyOGXdcEZBcyOGlwjxXoYPG3Pq9RviRunmSqX D3bk1ofONNfuNKvIfzPNNcXpgPBYxPik8j8N1pJiaJZ/XRMWaDLU/RRYZgVC5nb3dQ UAqV8XFcWGXTHMsckQMUyDiocJFtNduv2p4ketud5T5HYoWSZYtH11X00Yf4Ak28tz p7Jm1haCVPK//Kno0/j7tWanpTe/UuTbXA6igRfInLLd6sahV7W9lXChw1rEdOwrwG vch3aifQ0DCB2RiJ8qrZitIsmBVvhXWNvkrOBsUa2lAuzFjK3MkXiMhnf8AkHyi6Vo xgnPcT+tc2keA== Message-ID: Date: Sun, 29 Nov 2026 12:30:19 +0100 X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 4/4] RFC: mm/vmscan: unlock unmapped folios only after the deferred TLB flush To: Kairui Song , Zhang Peng Cc: Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , "Borislav Petkov (AMD)" , =?UTF-8?Q?Cenk_Y=C4=B1lmaz?= , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Andrew Morton , Zi Yan , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Mel Gorman , Sasha Levin , Rik van Riel , Ingo Molnar , Peter Xu , Hugh Dickins , Axel Rasmussen , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Yuanchu Xie , Wei Xu , Baolin Wang , Andrea Arcangeli , Thomas Gleixner , linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20261118-file_folio_flush-v1-0-e9e739ed14e0@kernel.org> <20261118-file_folio_flush-v1-4-e9e739ed14e0@kernel.org> From: "David Hildenbrand (Arm)" Content-Language: en-US Autocrypt: addr=david@kernel.org; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4KiQoUQFV4R7y1KMEKoSyy8hQaK1umALTdL QZLQMzNE74ap+GDK0wnacPQFpcG1AE9RMq3aeErY5tujekBS32jfC/7AnH7I0v1v1TbbK3Gp XNeiN4QroO+5qaSr0ID2sz5jtBLRb15RMre27E1ImpaIv2Jw8NJgW0k/D1RyKCwaTsgRdwuK Kx/Y91XuSBdz0uOyU/S8kM1+ag0wvsGlpBVxRR/xw/E8M7TEwuCZQArqqTCmkG6HGcXFT0V9 PXFNNgV5jXMQRwU0O/ztJIQqsE5LsUomE//bLwzj9IVsaQpKDqW6TAPjcdBDPLHvriq7kGjt WhVhdl0qEYB8lkBEU7V2Yb+SYhmhpDrti9Fq1EsmhiHSkxJcGREoMK/63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GXde2ZzzGYl64glmccD8e87OnEgKnH3FbnJnT4iJchtSvx yJNi1+t0+qDti4m88+/9IuPqCKb6Stl+s2dnLtJNrjXBGJtsQG/sRpqsJz5x1/2nPJSRMsx9 5YfqbdrJSOFXDzZ8/r82HgQEtUvlSXNaXCa95ez0UkOG7+bDm2b3s0XahBQeLVCH0mw3RAQg r7xDAYKIrAwfHHmMTnBQDPJwVqxJjVNr7yBic4yfzVWGCGNE4DnOW0vcIeoyhy9vnIa3w1uZ 3iyY2Nsd7JxfKu1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/29/26 12:20, Kairui Song wrote: > On Wed, Nov 18, 2026 at 5:22 PM David Hildenbrand (Arm) > wrote: >> try_to_unmap() with TTU_BATCH_FLUSH zaps the PTEs, transfers the PTE >> dirty bit to the folio and defers the TLB flush. Until the TLB is >> flushed, other CPUs might still access the folio through stale TLB >> entries. > > Thanks for taking care of this! Thanks for the review, I'm planning on sending #3 and #4 out soon. > >> Fix it by reclaiming folios in batches of up to 64 folios: >> >> (a) Lock each folio, check whether we want to reclaim it, and unmap it. >> Keep the folio locked if any deferred TLB flush is pending after >> unmapping it, otherwise unlock it right away. > > I'm not sure locking that many folios at once is safe under pressure. > Our first TLB batch flush series internally kept every folio locked, > but we found that unlock them improved performance further for > some workloads so we switched to the posted version from Peng. Yeah, I don't see a way around not destorying performance: I have further below. "Note that khugepaged and batched (async) folio migration already keep up to HPAGE_PMD_NR (e.g., 512 with 4 KiB pages) small folios locked after unmapping them with a deferred TLB flush; migration even allocates new folios, possibly entering direct reclaim, before flushing the TLB and unlocking them. Both also call MMU notifiers while keeping other folios locked, just like we do now." > But to fix the flush issue we do have to keep them locked though. Yes, unfortunately. > > There is no deadlock, but seems fragile. e.g. shmem_get_folio_gfp() > takes a plain folio_lock(), so a thread faulting a parked folio waits for > the whole batch while holding mmap_lock, and the batch can be > blocked by any non-trylock part of rmap/splitting. The per-folio hold > time is much longer too. Maybe we can optimize other parts later > instead if there is any actual regression. Yes! > >> Allocating swap space can otherwise only sleep when allocating memory >> for the swap table, which cannot enter direct reclaim or compaction, >> as reclaim runs with PF_MEMALLOC. > > I'm trying to make the swap table allocation sleepless, so we no longer > need to worry about that :-D Lian and Chris are pushing on the swap > queue series and it should come after that. That would be nice" :) > >> + if (!may_sync_discard) >> + return -EBUSY; >> if (swap_sync_discard()) >> goto again; > > Hopefully the swap side can get rid of this too. The discard attempt is a > historical thing to avoid OOM on super slow discarding devices, reclaim > throttling (which is being unified for MGLRU) can handle that instead. Ack! > >> +struct staged_folios { >> + /* Locked folios, waiting for the TLB flush. */ >> + struct list_head unflushed; >> + /* Unlocked folios, after the TLB flush (if required). */ >> + struct list_head flushed; >> + /* Number of folios in the current batch. */ >> + unsigned int nr_folios; >> +}; > > Using a list is usually bad for performance IMO, Peng used a folio_batch, > which I'm also a bit concerned about stack usage-wise.. The overhead > could be trivial though, I tested this series and it's working Yeah, if a batch is a better fit (and we can tolerate stack usage) we should ideally optimize it on top of this here. > > Ultimately I think we want just one or two unified batches for the whole > reclaim loop: from the very begiing of the call stack, > SWAP_CLUSTER_MAX is 32 which is the typical starting budget > under pressure, close to the folio_batch limit 31 already, and > we have a few of them already too. Ack. > >> + /* Collect more folios until we have enough or there are no more. */ >> + if (staged->nr_folios < MAX_STAGED_FOLIOS && !list_empty(folio_list)) >> + return NULL; >> + >> + /* Flush the TLB and unlock the folios that waited for the flush. */ >> + staged_folios_flush(staged); > > folio_list can get repopulated from shmem (pageout() passes it to > shmem_writeout()), so the loop can leave N already-flushed folios behind and > get NULL from it. Should be fine, but it blurs the two stages. Yes, I didn't find a simple way to improve that without reshuffling the code massively. > >> + while (!list_empty(folio_list) || !staged_folios_empty(&staged)) { > ... >> + folio = staged_folios_next(&staged, folio_list); >> + if (folio) { >> + nr_pages = folio_nr_pages(folio); >> + goto reclaim_staged; >> + } > > This looks... really weird; it will be much cleaner with Peng's cleanup I > guess. Yes, please do cleanups on top once we have it fixed. This all needs to be separately cleanly into stages. > >> +reclaim_staged: >> + if (!folio_trylock(folio)) >> + goto keep; >> + >> + /* >> + * We failed to unmap the folio, or it was mapped again while it >> + * was unlocked. >> + */ >> + if (folio_mapped(folio)) >> + goto activate_locked; >> + /* >> + * While the folio was unlocked, writeback might have started, or >> + * it might have been poisoned. >> + */ >> + if (folio_test_writeback(folio) || >> + folio_contain_hwpoisoned_page(folio)) >> + goto keep_locked; > > The writeback part may better at least have a folio_set_reclaim() so they > don't miss a rotation? Better if we can reuse the pre-stage checks by > splitting that part out properly. Is that something you want me to change in this patch? > > I tested this on x86 under pressure (40 GiB in one process with > swap, 4 processes concurrently, and 4 memcg-limited cgroups reclaiming > concurrently): TLB shootdown IPIs dropped by 90%, wall time by 30%. > Clean page cache seems unchanged, and on arm64 nothing changes > either. Really nice result! Thanks for the test! That's what I was expecting in the common case. It took a while for me to figure out how to have the patch minimal and yet not harm performance ... and while it even improve performance for the dirty case. > > So it looks good to me, just a few nits and a lot of cleanup to be done. Thanks! Yes, I hope you guys can figure out cleanups on top of this. Is there anything specific you want me to tackle in this patch before I resend? -- Cheers, David