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=1796388664; b=BkvyyJyV1dKb/N2+xsvItpnWprv416wRbN+9uaqu3uJ/dzntHj5SzM6huWHkg9cHyTPl qAEVaIdbAthyUHgFAuZYJ3HvkZz28x1np3AicXpQfUhACYlSS4MJWmw9G7lKweNUvdvbK RS/Yo6bjoqyeOrSMXqhsUAh7oN0zocElMPMk/+Bh0AcszmqhC7RS3wrlxuMjrDxGqL85K nBH7gDUqqZaFCYaCETlZGcDQyefDS+WK4HZFyQu3pd0MVO4yv4MDNIR/Qaom+fLfgH14E JtMarzDJ9omPW2iSzLR81VeSvtwmvLjjMuwdYpPiWvY2hGaBjSEIeBbUoAtzyCGts0g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796388664; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=v2FDswEbVgjHTa/6MfLasYjj2ch++s3Ii3zl8sd2B00=; b=fby8FvbNacztvqC2JQHdxiF136K4rKGd4DVuaH007joVMszUR4pGRlinXm+mgzBS6csB 03/qJoKZgLSfPd2vQBARwmlGgHlCGnOl4eBViJcNzsRROhnPWo39cntxZM4cDNft6RFT2 jlyHC7od2bnSzKfjrAYBkM3x3dYiB4xSJiuKSug9CR+ZmqZUU20jl/4Fh0T8dzYj9hRGO 1ZWYSLnSWdLQ75x5CIotjJaSq+Mll5H+WpNtXtla25zsgR5D0DoPCesaQ/3ndzVQm6I3D vlGzyXISvFapcu7kQtgVS49Y3E3lci9EDFmkolgRhmR3xKTpzZVHNN+cpkLOJeWvIaA== 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=AUCpVIfr; 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 255F8606A32C; Fri, 4 Dec 2026 12:51:04 +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=AUCpVIfr Received: by smtp.kernel.org (Postfix) with ESMTPSA id D742E1F00A0C; Fri, 4 Dec 2026 12:50:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796388663; bh=v2FDswEbVgjHTa/6MfLasYjj2ch++s3Ii3zl8sd2B00=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=AUCpVIfryNgoS9lsSNx8O1fHj7Qam0tGxaMCKGirp01Z6BYUMA79fC86PASzm5rP/ Q06OKaAh7AedHWWRf7uqxcVxb18t9B4r2nSkJRhSL6foVdKWoi60OsQYrcnyyIDbxe 2Ugrod9ZZPXWm9AC5RQlPAjOJDsOmCaIeeBhQas+dRlJrh6PQfwyBBx0ttI9yknK1w ESSLf5AvpqMg1bmdQVQd/fzRPYXndaMcpQB0WknVTv5r5tTyNL3YtOaVQgtRdt7RKM 6hAoZaqnGo7QCkH51ym5PCGq4K+DQXZ5gQLnDgGdB8IQxJ/fIFypzUQabI27Dw8nph FXg6azrRcMRUA== From: "David Hildenbrand (Arm)" Date: Fri, 04 Dec 2026 13:49:53 +0100 Subject: [PATCH v2 02/31] mm/shmem: don't skip parts of a split swap entry when punching holes 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 Message-Id: <20261204-fixes_core_3-v2-2-9aad65727632@kernel.org> References: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> In-Reply-To: <20261204-fixes_core_3-v2-0-9aad65727632@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , stable@vger.kernel.org, Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3901; i=david@kernel.org; h=from:subject:message-id; bh=ypeuHNPjCaLjKGtz1i+10bzBN3GvNLeBOz5zgbIE4R4=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLKFtv377dvHs/1LCPeOJTLXWU7Y2Cp5CgucidB72DSl9 fATeWmfjlIWBjEOBlkxRZag4PsGL1+uq61242OGmcPKBDKEgYtTACaSdJvhf9WqN6/7PgebTptZ J8G907sx9di0GysbJf6En7woHxe+9zsjw9ymxUdyP+0qPZNkFOB6nvO5y/UJt9zcbslytM8KW9n WzAMA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A The final loop in shmem_undo_range() looks up a batch of entries and calls shmem_free_swap() for each swap entry. Concurrently swapping in a folio that is smaller than the swap entry splits the entry. The first piece keeps the value of the original entry, so shmem_free_swap() only frees that piece and reports success. The loop then continues with the next entry of the batch, skipping the remaining pieces: hole punch read() ========== ====== find_get_entries() index 192: swap, order 6 index 256: swap, order 6 shmem_swapin_folio(), index 200 shmem_split_large_entry() shmem_free_swap(), index 192 order 0 by now: frees 1 page shmem_free_swap(), index 256 /* 193 to 255 are left behind */ While truncate scans again until nothing is left, a hole punch only makes a single pass and does not revisit these entries. Consequently, punching a hole into a shmem/tmpfs file with large swap entries can leave old data behind if it races with a read of that range: fallocate() succeeds, but later reads return the old data instead of zeroes, and the swap space stays in use. Let's fix it by letting shmem_free_swap() return the number of pages the swap entry covered, and looking up the remaining entries again starting at the first index that was not freed. As the first loop in shmem_undo_range() already frees all swap entries within the range, these additional lookups are rare. This has low urgency, as the race is hard to hit. Fixes: 809bc86517cc ("mm: shmem: support large folio swap out") Cc: stable@vger.kernel.org Reported-by: Chris Mason Reviewed-by: Baolin Wang Tested-by: Gregory Price (Meta) Reviewed-by: Gregory Price (Meta) Signed-off-by: David Hildenbrand (Arm) --- mm/shmem.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 57eaa867028ef..a302ec07b36fd 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1207,9 +1207,11 @@ static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) /* * Remove a swap entry or swapin-error marker wholly within [index, end]. - * Return whether it was removed, counting freed swap pages in nr_swaps_freed. + * Return the number of pages the entry covered, or 0 if it was not removed. + * Only a swap entry holds swap slots: their number is added to + * nr_swaps_freed. */ -static bool shmem_free_swap(struct address_space *mapping, +static unsigned int shmem_free_swap(struct address_space *mapping, pgoff_t index, pgoff_t end, void *radswap, long *nr_swaps_freed) { XA_STATE(xas, &mapping->i_pages, index); @@ -1474,12 +1476,15 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend, folio = fbatch.folios[i]; if (xa_is_value(folio)) { + unsigned int nr_pages; int order; if (unfalloc) continue; - if (!shmem_free_swap(mapping, indices[i], end - 1, - folio, &nr_swaps_freed)) { + nr_pages = shmem_free_swap(mapping, indices[i], + end - 1, folio, + &nr_swaps_freed); + if (!nr_pages) { pgoff_t base = indices[i]; order = shmem_confirm_swap(mapping, indices[i], @@ -1500,7 +1505,13 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, uoff_t lend, index = base; break; } - continue; + /* + * A swapin may have split the entry after the + * lookup, so only a part of it might have been + * removed. Look up the rest. + */ + index = indices[i] + nr_pages; + break; } folio_lock(folio); -- 2.56.1