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=1795043182; b=xd4klVzsS+luMeZSMsd3tpVoS+XvMZc6ggkRC6hc6Q2P1ldLHquSmvN5AvElAwtUwemV 6QAVjP6kxI5aGt1uIpjie0mZKF8szZygwIgCvm/cv5y8QDmkzGsKrBRP4opo2emtZDk4q RlIN+9NgsvolgKrz/pkelkm31j8oKbXnRqkjUwvYWw6TVZ9+d4yUbWun4sCsmnj2sKtdY FUQNyBdBM8Unn2USjWwDVq3N6Z1Lmg8xzjr08pZkVABPNdvuM9ymfnsM+sTr3XXAUap1t xyiowbHNAuA6cjJIRNOhw+WFWHLC0nA576r7w5BgNrn5dtrUWW793uOm1THLImcVXrg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795043182; 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=d2Hfc7KPFw5DIs6U1CzLiXGJSakBlFr3RRwCIqZUq30=; b=dCVUGP/bDYKYBalP9DG8tbJTdyZ5m6tbIvCBV9IGNYMyBllTqXuMEAnx3H84qD/aHGLN y9NSFPesY4VQTjSB16hSmN1oyiB0563iktKxn8cna3X8KiE7z+QwZ2gpnpv1BT+rzs7kn kLcZxUe+HB1QAxiWkeezODGU1D05ziZhTxxUsfCCBonx/8c2rVUZ0jJTFvWsrCkYEGGHC OZkLPlAPNBN6pxmHOi1hCl9TFYNHhrIQyqamTmgPE4z8jGiBAQQNLWQ3MZv9lpyRTIJS3 GGnQ/z+/DD68XrNf1VZz5FsIct7Qqy3X4Q9gR+Jb/r5DiWsE9D/xAv9EisDkHo+3jEg== 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=Rg4q7NTA; 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 BFADD606A31C; Wed, 18 Nov 2026 23:06:22 +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=Rg4q7NTA Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3A30E1F000C4; Wed, 18 Nov 2026 23:06:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795043182; bh=d2Hfc7KPFw5DIs6U1CzLiXGJSakBlFr3RRwCIqZUq30=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=Rg4q7NTAUqV/9yuq3GSHxO8lC470OmPvn2+1MpQ73rIJri3+E+i2MyLhM5wkbhtQp roGY16V45fVOUYtzXmb/qmSiiQol3j3qXVgPnPJY3qT8cVfz2vsFvvgFnxHpZog7gt QetzGzjVj4fSAIthWnQldKmx6v54IPUTIBpbm3ldR1Wj5wy0aghIJJmQHLdUYVfQie E/+rKIIUZuQmIhOa50c/ZzFm9UWup+rHQ8ekkZPtF0Sq5fYZfaW7vgmooPp0627N22 BZ1koCMMTgy4KuTGWaju1IBQ0fg+llx4DoNUz2Ak5T6mTIi+D/ZtAVySaRc82i6vC8 DwrMt1aoQHdXA== Message-ID: <3c61a49f-6a0d-49eb-8682-aece4e7e7226@kernel.org> Date: Thu, 19 Nov 2026 00:06:15 +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] mm/shmem: fix swapin livelock on a mismatched large swap cache folio To: mt4022@columbia.edu, Hugh Dickins , Baolin Wang , Andrew Morton , Kairui Song Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, tz2294@columbia.edu, Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , stable@vger.kernel.org References: <20261027-shmem-swapin-mismatch-v1-1-84e30aa3b9e5@columbia.edu> 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/63r9WLZYI3+4W2rAc UucZa4OT27U5ZISjNg3Ev0rxU5UH2/pT4wJCfxwocmqaRr6UYmrtZmND89X0KigoFD/XSeVv jwBRNjPAubK9/k5NoRrYqztM9W6sJqrH8+UWZ1Idd/DdmogJh0gNC0+N42Za9yBRURfIdKSb B3JfpUqcWwE7vUaYrHG1nw54pLUoPG6sAA7Mehl3nd4pZUALHwARAQABzS5EYXZpZCBIaWxk ZW5icmFuZCAoQ3VycmVudCkgPGRhdmlkQGtlcm5lbC5vcmc+wsG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Ku1PRhCGwXzRw5TlfEsoRI7V9A8isUCoqE2Dzh3FvYHVeX4Us+bRL/oqareJ CIFqgYMyvHj7Q06kTKmauOe4Nf0l0qEkIuIzfoLJ3qr5UyXc2hLtWyT9Ir+lYlX9efqh7mOY qIws/H2t In-Reply-To: <20261027-shmem-swapin-mismatch-v1-1-84e30aa3b9e5@columbia.edu> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 10/27/26 21:33, Mani Tofigh via B4 Relay wrote: > From: Mani Tofigh > > On a SWP_SYNCHRONOUS_IO swap device, a large shmem folio can be left in > the swap cache over swap slots that another file now uses. Reading > that other file then loops forever in shmem_get_folio_gfp(): > shmem_swapin_folio() finds the large folio, rounds the index and the > swap entry down to the folio's size, finds no matching entry in the > file and returns -EEXIST, and the retry finds the same folio again. > Since the task keeps the mapping locked, a munmap() of the same mapping > waits forever. > > The folio is left behind when the slots are freed and reused while the > swapin is still allocating it: > > Task A (reads X) Task B > ---------------- ------ > shmem_swapin_folio() > allocate a large folio > for X's slots > punch a hole in X > /* X's slots freed */ > swap out file Y > /* Y reuses those slots */ > add the folio to the swap > cache over those slots > shmem_confirm_swap() fails > /* -EEXIST, the folio stays > in the swap cache */ > > Task C (reads Y) > ---------------- > shmem_swapin_folio() > finds the large folio > rounds down to its start, > where Y has no entry > /* -EEXIST, retried forever */ > > Before commit c246d236b18b ("mm/shmem: never bypass the swap cache for > SWP_SYNCHRONOUS_IO"), a direct swapin that failed this way freed its > folio instead of leaving it in the swap cache. > > do_swap_page() already drops a large swap cache folio that does not > fit for anonymous memory. Do the same in shmem_swapin_folio() and on > a -EEXIST, remove a large folio that is not > anonymous/dirty/under-writeback from the swap cache so that the retry > swaps in a folio that fits. > > The reproducer below fills a small brd swap device so that a 64K shmem > folio and the 4K pages of another file land in the same slots, and races > a read of the first file with a hole punch and a swapout of the second. > This bug was identified and confirmed with the help of a deterministic > hypervisor currently under development, but the non-deterministic C > reproducer can typically trigger it in the 2-4s time range. With the > fix, nothing is triggered after 600 seconds. > > Fixes: c246d236b18b ("mm/shmem: never bypass the swap cache for SWP_SYNCHRONOUS_IO") > Cc: stable@vger.kernel.org > Signed-off-by: Mani Tofigh > --- > Two patches in David Hildenbrand's v2 shmem fixes series are related > to this one. "mm/shmem: fix truncate hang in large swap entry" [1] > makes truncation retry -EEXIST directly, so it can also loop on such a > folio. "mm/shmem: preserve allocation errors when swapping in large > entries" [2] fixes a different -EEXIST loop, where a failed allocation > is reported as -EEXIST. > > [1] https://lore.kernel.org/all/20261027-fixes_shmem-v2-10-86a00952acb9@kernel.org/ > [2] https://lore.kernel.org/all/20261027-fixes_shmem-v2-11-86a00952acb9@kernel.org/ > > Reproducer: https://github.com/manitofigh/files/blob/main/linux-repros/shmemloop.c > --- > mm/shmem.c | 16 ++++++++++++++-- > 1 file changed, 14 insertions(+), 2 deletions(-) > > diff --git a/mm/shmem.c b/mm/shmem.c > index 13e0786436cd..f1da8432d55c 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -2568,11 +2568,14 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, > * and swap cache folios are never partially freed. > */ > folio_lock(folio); > - if (!folio_matches_swap_entry(folio, swap) || > - shmem_confirm_swap(mapping, index, swap) < 0) { > + if (!folio_matches_swap_entry(folio, swap)) { > error = -EEXIST; > goto unlock; > } > + if (shmem_confirm_swap(mapping, index, swap) < 0) { > + error = -EEXIST; > + goto failed; > + } > if (!folio_test_uptodate(folio)) { > error = -EIO; > goto failed; > @@ -2613,6 +2616,15 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, > error = -EEXIST; > if (error == -EIO) > shmem_set_folio_swapin_error(inode, index, folio, swap); > + /* > + * A large folio that does not fit the mapping, e.g. because its swap > + * entries were freed and reused, would be found again on every retry. > + * Drop it from the swap cache so the retry can swap in a fitting one. > + */ > + if (error == -EEXIST && folio && folio_test_large(folio) && > + !folio_test_anon(folio) && !folio_test_dirty(folio) && > + !folio_test_writeback(folio)) > + swap_cache_del_folio(folio); > unlock: > if (folio) > folio_unlock(folio); > > --- I have an AI fix here: Author: Chris Mason Date: Wed Nov 11 18:44:55 2026 +0000 mm/shmem: don't drop a folio whose swap write failed If the swap write of a large shmem folio fails while a swap-in of another file finds that folio in the swap cache, its data can be silently lost. A read of the file then returns whatever the swap device holds. "mm/shmem: fix swapin livelock on a mismatched large swap cache folio", earlier in this series, lets shmem_swapin_folio() drop a large folio from the swap cache, if the folio does not fit the file that is swapped in and is neither dirty nor under writeback. A folio that does not fit can belong to another file, with its swap write still in flight. shmem_swapin_folio() holds the folio lock, but the code that completes a write does not take that lock. And a failed write sets dirty before it ends writeback. So the folio can look clean and written, although its write has failed: shmem_swapin_folio() swap_write_end(), failed write ==================== ============================== folio_test_dirty() == false folio_mark_dirty() folio_end_writeback() folio_test_writeback() == false swap_cache_del_folio() /* the only copy of the data */ The fix used here is to test writeback first, and dirty second. Signed-off-by: Chris Mason diff --git a/mm/shmem.c b/mm/shmem.c index 915b312e14175..457a502353a9a 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2809,10 +2809,13 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, * A large folio that does not fit the mapping, e.g. because its swap * entries were freed and reused, would be found again on every retry. * Drop it from the swap cache so the retry can swap in a fitting one. + * A failed write sets dirty and then ends writeback, with no folio + * lock, so test writeback first. Both flags share folio->flags, so the + * order of the two loads holds. */ if (error == -EEXIST && folio && folio_test_large(folio) && - !folio_test_anon(folio) && !folio_test_dirty(folio) && - !folio_test_writeback(folio)) + !folio_test_anon(folio) && !folio_test_writeback(folio) && + !folio_test_dirty(folio)) swap_cache_del_folio(folio); unlock: if (folio) @Mani, can you double check if that makes sense (can we actually trigger that), and if so, send it out officially, with Fixes tag ? Feel free to rework the patch as you please. -- Cheers, David