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=1796142326; b=mqTaXdiWhRwnlSo42FZUjTgvH4tmYLpO7JA6Su5UvvKc1ISecnuwXU9jmSL5u99dRddw sfo+CbfPfjw4G8hk6vIWJnhDo5ZrKNHOTFAumeWQRhrpBVipyFk7Y0x2ZEdiQ2UVgnIv1 68hRhhQNX5Q6jLCXpc00njXKaQNOXCTiRDUjMflbQom0SM6NbfekDS9hzNXzpb+6BqYMF v2kOzVQHHcwkyPTMXxKCDxHsKsbfriG+boghlmRroFr96TkPe/pLHPQnJZ1cuXLyALyPk qCmLNIDtdk1aTmxra8eBee7yaOG6uGUrR+HUEAP1g6NZ8Z4eiqfjo+5vIhjDMP+jbxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796142326; h=Received:DKIM-Signature:From:Subject:Date:Message-Id:MIME-Version: Content-Type:Content-Transfer-Encoding:X-B4-Tracking:X-Change-ID:To: Cc:X-Mailer:X-Developer-Signature:X-Developer-Key; bh=ID7Fx7f3ofEPv+To39NUh+GslPvY5y2sqKgqVjB47Y0=; b=HuuWVlknyGyeHndSvgI7dXAVT7xepm0Bk1sdg7+mpFkrRLmaxrqUe+xS41+L5H8YBYsX V9AkhbdFIenldWnQwSKrvXuQ9dQ5oUXMA7AGM6vZOSJlH31Hg5vGaGFxWCIGpO5jy6sF1 X42L4B7G3uxQG8aMn4b5WztCdxK1SCkt5Qc7LLoVqfrMGslOpHcMwY0oA0zXBKfipy+C1 sZof+E2INIywb2wZdCtw9qUGHbL/U+Aw0Ec+WKTtWzuPj9v+ximIp6CaMzI3ad1FYfUBx wfyxdK/Q08rV5B2UiVimF3YMf7+imp0uc50Rq2HWdGo0+qrMJfqXJ5BYrMQmddoWFrQ== 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=PyPDfq2P; 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 92B6A606A33F; Tue, 1 Dec 2026 16:25:26 +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=PyPDfq2P Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94C7F1F00893; Tue, 1 Dec 2026 16:25:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796142326; bh=ID7Fx7f3ofEPv+To39NUh+GslPvY5y2sqKgqVjB47Y0=; h=From:Subject:Date:To:Cc; b=PyPDfq2PkymsYN9qel6xR3o0yOJn21o/ZB3l/VL42IWzge32OmOuWESy9cjzyVTT7 0aZI7O3bCVkmfeXGmcSvdgBUhkovznPG14ZI2YUrLSwP0T2ECXYdZ/wna1TmLfiWs4 p2f2zdkZk+IR0xGVaEsJ1y/M058LGX+gKB0FifQ044nPZHYQloKgtvBm0RfC31ihly k2vep0g+6zQ43NWCfpUiVab60YxicNNTs+GMFQjvRvkOipKoMBE4/b7rcGuoCWvxWk h63VP30jIsZLgYOPEye/1uv1ZD/GTx6agM17YqOT5i7/WbPx8HBn1Pqa7A6NuBMUXb AROFo9oXiD5qw== From: "David Hildenbrand (Arm)" Subject: [PATCH v2 0/2] mm: fixes for stale TLB entries due to delayed/batched TLB flushes Date: Tue, 01 Dec 2026 17:25:04 +0100 Message-Id: <20261201-file_folio_flush-v2-0-994d587aec8e@kernel.org> 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-B4-Tracking: v=1; b=H4sIAAAAAAAA/2XNywqDMBCF4VcJszbFSaVeVn2PIqJmotOGpCQqL eK7Fy1ddXng5zsrRApMESqxQqCFI3sHlVCJgH5s3UCSNVQCVKouiJhJw5Ya4y37xtg5jjLXmGe laTulC0gEPAMZfh3krf7uOHd36qfd2YuR4+TD+/hccO9+fPHPLyhTSSXl55I0ZpReHxQc2ZMPA 9Tbtn0Ay89HCsMAAAA= X-Change-ID: 20261114-file_folio_flush-7d1749fab2d8 To: Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , "Borislav Petkov (AMD)" , =?utf-8?q?Cenk_Y=C4=B1lmaz?= , Zhang Peng , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Andrew Morton , Zi Yan , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Mel Gorman , Sasha Levin , Ingo Molnar , Peter Xu , Hugh Dickins , Axel Rasmussen , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Yuanchu Xie , Wei Xu , Baolin Wang , Thomas Gleixner Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, "David Hildenbrand (Arm)" X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=4614; i=david@kernel.org; h=from:subject:message-id; bh=fSUF64GXdBc8ghFYfH9CbO+5NdGkv2KFCOOrnpy5F/g=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLL5vjyR5g8+eMVgx3aGtxX/Vi+4J39mXlP98wQrj9mzi mudZYI7O0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZhI2yuGf5YHKtWX+jvd7H7+ Vn5LjcyqZ3J1yvyXLy/iWiH109r5Yzkjw90fbL/v/Xg2a9UK6ZXO/m/KU+YvPlfc2bzjgNZNW1n hRWwA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A These are the two remaining fixes from v1, whereby we fix two stale TLB issues with TTU_BATCH_FLUSH during reclaim. Patch #1: Don't defer TLB flushes when unmapping lazyfree folios. Handles mapping a new anon folio after reclaim discarded a lazyfree folio (do_anonymous_page(), migrate_vma_insert_page(), UFFDIO_COPY, UFFDIO_MOVE). Patch #2: Unlock unmapped folios only after the deferred TLB flush. Handles refaulting a new page-cache folio, CoW of a folio that is still in the page-cache, and swapping in a new folio (do_swap_page(), unuse_pte(), UFFDIO_CONTINUE). Also handles invalidation replacing a folio that reclaim kept, and write() modifying a new page-cache folio. There is one remaining problem that I describe in the cover letter of v1 in detail. Testing ------- I threw a bunch of selftests at it and had AI generate some stress tests, in particular for the benchmarks below. I also ran stress-ng to force reclaim. I managed to reproduce both issues, as described in the patch descriptions. Stale reads out of 1024000 checked pages in 2000 iterations: base series lazyfree, MGLRU 16242 0 lazyfree, classic LRU 16274 0 swapcache, MGLRU 24076 0 swapcache, classic LRU 23028 0 Performance Impact ------------------ I ran some benchmarks on v1 in an x86-64 VM with limited CPUs, RAM and zram enabled. Running inside a VM might make TLB flushes more expensive, so the results below might be a bit exaggerated. AI results below. Classic LRU, 4 threads randomly touching a working set twice the size of their memcg for 30 seconds. Pages touched per second: base series anon-rw 681k 1055k anon-ro 1013k 1027k file-ro 417k 419k file-rw 173k 183k lazyfree 1512k 333k Swapping out dirty anon folios ("anon-rw") is about 55% faster, because patch #2 now also batches TLB flushes of stale writable TLB entries. That is what Zhang Peng's series [1] tried to achieve, and his work also inspired how patch #2 locks folios again, but the used mechanism is different. Reclaiming clean anon folios ("anon-ro"), clean page-cache folios ("file-ro") and dirty page-cache folios ("file-rw") is not noticeably affected. Reclaiming lazyfree folios ("lazyfree") is about 4.5x slower, because we stop batching TLB flushes when discarding them entirely, which seems to be the right thing to do for now. We can look into optimizing this separately later. Kairui tested v1 as well on x86-64 under memory pressure (40 GiB in one process with swap, 4 processes concurrently, and 4 memcg-limited cgroups reclaiming concurrently) [2]: TLB shootdown IPIs dropped by 90% and wall time by 30%. Reclaiming clean page-cache folios seemed unchanged, and on arm64 nothing changed either. [1] https://lore.kernel.org/linux-mm/20261031-batch-tlb-flush-v6-0-190539839eec@tencent.com/ [2] https://lore.kernel.org/linux-mm/CAMgjq7DWWCF_iB+GmTciuXr=m=uk7mMmidvY=8yg6MH98X4oPg@mail.gmail.com/ --- Changes in v2: - #1 and #2 are upstream now - Rebase on top of mm-core/for-next - "mm/rmap: don't defer TLB flushes when unmapping lazyfree folios" - Avoid line split - "mm/vmscan: unlock unmapped folios only after the deferred TLB flush" - Label the three phases in the comments - Finish reclaiming a batch before collecting more folios, even if reclaim added folios to folio_list (e.g., splitting a shmem folio) - Activate folios that went under writeback while unlocked - Update the description of (c) and drop the outdated migration example from the patch description now that that stuff is fixed upstream - Collect tags - Link to v1: https://patch.msgid.link/20261118-file_folio_flush-v1-0-e9e739ed14e0@kernel.org --- David Hildenbrand (Arm) (2): mm/rmap: don't defer TLB flushes when unmapping lazyfree folios mm/vmscan: unlock unmapped folios only after the deferred TLB flush mm/internal.h | 5 ++ mm/rmap.c | 19 ++++++- mm/shmem.c | 2 +- mm/swap.h | 5 +- mm/swapfile.c | 7 ++- mm/vmscan.c | 175 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 6 files changed, 197 insertions(+), 16 deletions(-) --- base-commit: 40e0fcedf9c39c3a4a0100323c101506b4206394 change-id: 20261114-file_folio_flush-7d1749fab2d8 Best regards, -- David Hildenbrand (Arm)