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=1795946857; b=lcJajz/ntOX+JgK3cs7XYYsnSt6irhpYXZd0BxJIxOYGYE2ytDFICsH8uHNpprPyJ8fz nNtiq4deEcIof966hT8I95PasLZxy6cfG3RWNbTZLu/UDlUyeEn3Z2lAc5k/3FRBh2yJN 48PzWQMFJQ0Ff4f1xaKHr4CvNi3ypARpwWgN1i8/KX7wRP2rNnYftYusVR8eDidTb2JDB AHvZXIM8WvHCQFF++N1AbNT/LdapTbXiZJFm3e+EMgWm6rbSJfsOX0eRu52mZynEtSHFT bWxFZn8qZijIADNttXNNrf75cgCbNBxBjYOVB2qUNYauUrw700lIy6Qc+idxOSz40oQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795946857; 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=Jm+Ol1bBa++QrSLg19j5zaV3MHS4aeLMJ17A+c+JM1I=; b=y1yUTZENOvhk52ZCaH6TRqWiciIDVTXQaitR+07S2649snilIP2evGar1ohcyz/rnBOC hQ+Fh2mqfGrunnbxoOP1O6rCzDT1CNDew3wrhOc8iDSBBP3Rtvr6NSS1ZiDub2cubZLJ1 JbN7YmFhPMb0z+jy6o0JHivqg82w51b+6cvnZOHn8ngPoNOz+y4fXGxJOv38QfriP9ACJ GDKX8MM89Rl+dsZhhaVmL783s/mXat6gxstOhJ5v/4DahI6f9pYopzr+1f1Xct3+WMC0d HOD2aBnsZBynFqAjSYZh+JNCegf120DBslXHTbS/ykQ4kMXJQHVliq7Ccf3D4vGec0w== 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=XUxNqiBz; 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 3DB5A606A31C; Sun, 29 Nov 2026 10:07:36 +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=XUxNqiBz Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A75C1F00899; Sun, 29 Nov 2026 10:07:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795946856; bh=Jm+Ol1bBa++QrSLg19j5zaV3MHS4aeLMJ17A+c+JM1I=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=XUxNqiBzBpx2U0hVL71vusvdKHun5LgM4sR5yRhu5O6b+mTZvcm66RUpeSl5PsQtq 9090M182QBLcwZnTWK+qtcrCHGvhTORe8NhHuZhOfnu91dnsZefw8B2rg4FeMeE0yK 5K3DRYAAZdcCQZ4KNG/HwelCczyX3M1tK22bhsGD7S/BgQJZ1SsNZWelTXdf3vktB2 IXVmsq0osI414pK0Xo4imqwapztI0ViTcl3leGm3I2LrXIxaBBuKOXJoBcLtuGHprt aen0sOCY+es2QZx4uHykLZrKKuJYo/8ZK2fUSv5DGgjwK6mR9rCThbMrEX4rYoSMEi fmuEgRs4fBzgw== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:33 +0100 Subject: [PATCH 02/32] mm/shmem: don't skip parts of a split swap entry when punching holes 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="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-fixes_core_3-v1-2-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@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=3735; i=david@kernel.org; h=from:subject:message-id; bh=bL0sU3NVfylGHblWAIivnvSTQG1e6ulxfwxrLvrgp5Y=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f2pPW7JiU+KhR81zhPqv15yVm7NlgVDytEzNA1w/j KITQgXyO0pZGMQ4GGTFFFmCgu8bvHy5rrbajY8ZZg4rE8gQBi5OAZjIV0NGhtd2prfzDl2J4zwY nMt+xrdtmtBTRRsWq4ObuS9Kz1IVfsfw3/fn/WPbfn1K61jRclC0uOWAR2TdKdPDPMsLTDVVWzZ qsgMA 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 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 fca7c1f6c8574..afe6c80b6f48c 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1206,9 +1206,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); @@ -1473,12 +1475,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], @@ -1499,7 +1504,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