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=1796381530; b=v/P5h4aR/x83cxIasnOxX8zRklIHkmZBrq1xcloikSkE4q2yNx60faXuIRrjJtth0hl2 6UlexVqe5wzYaBSYsOD9KiYTbZxIhwFrTCPsaPKT+gR8kKaOUUgiUJAyyuc4+n9pYgxVL /wP3ciSy6Ou1PE5pkJ8Oep8cwAAYBaOIaiqFsWOkFScJtBxwzLUHSzJLtus0CIRcqCziP bJtM7rcm8jGzS1WRyrFuhHp3KrCuHJCho+d7qGmShdQ5dhn9uKp+1CQmmlCXPCAy7zOkY Puw1IzxaI7VOOBPDp6MvDGqyFJDotJHWCKdUo0mkdIx9ueJnPCv2Lh9Ko8yYZO4EqRA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381530; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=kJAg1hPb52OyJcHjUCVwlH+ti50tB6CQQr+ITyHWRBo=; b=tETGrnZL3Swi3GGxZ6J2mQICqm6fekNiyJNd/VmClTTMYRmtL9kFnm5PIFm6i9Q9ZPsq +nZn5R+F6bvxDXoweBbcaIunRHxeltb2aORxEytnXeKUYWacvVPHVn83ok1/mJIoHAfLh rRYl/Vw0vGW/XxjJhOjvGGCLWDC5BdRIPONX0ROh/gtzL1P/OmcDajCICvsjUSyRDaygg w2h3e3aqABMqLs5uz5q+Q7uNUNw2P70pEllu0c8YWlGQ+HMhtIM/w9MByEyhjExmHfJnV wyIY7by9JKQEjAJ3s4SbClq48VcEp71JdNZnKqyisDOuf4UfqldUeA+aoPPYvNbpO0w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=qTP1G5JM; 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 E9321606A681; Fri, 4 Dec 2026 10:52:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=qTP1G5JM Received: by smtp.kernel.org (Postfix) with ESMTPSA id 668EB1F000C2; Fri, 4 Dec 2026 10:52:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381529; bh=kJAg1hPb52OyJcHjUCVwlH+ti50tB6CQQr+ITyHWRBo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=qTP1G5JM1uo2O18r7n5GgEOYDrydpW21Riz+rUhMimUQm7XhAkzClM7EP+VJVMJNY eHqDeSa0mOWLLezBsaNdembqmvq0xTNmGdXeNEA4qmzZW2V5TDet8VIUnDWIQyuvhP 71A/J8XPpEJD6aPMsOQpgOXy16S2iS8aRLXqCkm0= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Chris Mason , Mani Tofigh , Nhat Pham , Baolin Wang , "David Hildenbrand (Arm)" Subject: [PATCH 7.3 742/742] mm/shmem: dont drop a folio whose swap write failed Date: Fri, 4 Dec 2026 19:10:06 +0900 Message-ID: <20261204100654.829007062@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100635.034361255@linuxfoundation.org> References: <20261204100635.034361255@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore 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-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mani Tofigh commit e3464357e282d164ce873bfb4e259996cea3a564 upstream. shmem_swapin_folio() drops a large folio that does not fit the mapping from the swap cache if the folio is neither dirty nor under writeback. The folio can belong to another file and be under swap writeback, when the swap entry was freed and reused before the swap cache lookup. Swap write completion does not take the folio lock. On a failed write, swap_write_end() marks the folio dirty and then ends writeback. Since dirty is tested first, the folio can look clean and written: CPU0 (swapin of file Y) CPU1 (swapout of file X) ----------------------- ------------------------ shmem_swapin_folio() folio_test_dirty() == false swap_write_end(), failed write folio_mark_dirty() folio_end_writeback() folio_test_writeback() == false swap_cache_del_folio() /* X's data is lost */ The next swapin of X reads the slot whose write failed. Test writeback first. Both flags are in folio->flags. If the writeback test sees the write ended, the dirty test then sees the dirty bit set before it. With the fix, a folio whose swap write failed stays in the swap cache. Fixes: fc2c19648d68 ("mm/shmem: fix swapin livelock on a mismatched large swap cache folio") Suggested-by: Chris Mason Cc: stable@vger.kernel.org Signed-off-by: Mani Tofigh Reviewed-by: Nhat Pham Reviewed-by: Baolin Wang Link: https://patch.msgid.link/20261123-shmem-swap-write-failed-v1-1-41a0115dfbf1@columbia.edu Signed-off-by: David Hildenbrand (Arm) Signed-off-by: Greg Kroah-Hartman --- mm/shmem.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2457,10 +2457,12 @@ failed: * 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 swap write sets dirty and then ends writeback without the + * folio lock, so test writeback before dirty. */ 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)