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=1796381078; b=qmImkw+e3sxA/Y63PSrMhTC0L1efO0pFrUWOb5VcNb68GqIHkuXathFCYjgbqRgIcNac vNSAGSkLOZk3m5SYY/tRr4ukPenJowjLY9F4VkvegPd4kydvEvRlRpnDKNMSZbanYeaKR Nm/fwEZ4rWXHSlhd2JOTCOP0BT5zHkm22QrGKwC+5Dt6JKEA+V/TSreG3GbdiOeIgFP5+ UgtcGsJKsCA5Asuooet8Ms+I/dX1z4DabnOd8u24aq5UK0l7Z5tiicMI5KxNtx93SlgFi 9Zn7TbnzOM+NC6pRTUjArY3iRJfKSJezrn+/mGcLFfxCGK1bZyH0hWHzWhLkbWQA5EQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796381078; 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=VQFQR+JMb/Ou/6vgIADluVRDFvKQ+7y5E0LG7P6QQ5M=; b=IdUeMatt3jxwmVCMDaoz28dQc52jOblAmgekK0LG+wqaECvdZ+DxvRPcadMtYL7u7tuX pANlIn0RlBP7KvZQGNG2Ellu7Wn9ijA6A0KdPx7qQSKdX9uV7zJLXUNpwQS18/TiUbHkY mKwuYfU/AH8x+8XDwk6NjlRpzzNdpF2dkwAzd9TwW+gAIKuXVJsL1cv7QmL+6xVx9j5T7 QilUzfxKiLcNuTCy0pBAjsdxbChEZaZFFxdNzAhc9YZ/L8Tv0i+zbYJ9d1i+7pRwz4CBL Yd2HjCP9YV5c/Bjg5BXzhUrwtSGE1jr16/toOxM4QcDb/jt63LOFXreEusxBrfzOvVA== 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=uvmvzpvf; 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 D0648606A322; Fri, 4 Dec 2026 10:44:38 +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=uvmvzpvf Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4C6FA1F000C2; Fri, 4 Dec 2026 10:44:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796381078; bh=VQFQR+JMb/Ou/6vgIADluVRDFvKQ+7y5E0LG7P6QQ5M=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=uvmvzpvfEZ7fB5Umgpt0BtD6eXeMS2Q9+22JN2/Tzi0hgkv12oS/G6ttMt35xM6zF dYoYh5v20ieRszOK5baToiZsrZWLyhbdyA+iAm66DfhNAoFJ2zzYDSKixg2OQio2SW nTUMhq78krYunMh2tU0LbAV1uXVYszkCYI+XXFtI= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Mani Tofigh , Andrew Morton , Baolin Wang , Hugh Dickins , Kairui Song , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , David Hildenbrand , Tal Zussman Subject: [PATCH 7.3 549/742] mm/shmem: fix swapin livelock on a mismatched large swap cache folio Date: Fri, 4 Dec 2026 19:06:53 +0900 Message-ID: <20261204100649.740811852@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: patches@lists.linux.dev 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 fc2c19648d681cc5fb00efa938b78460c2e5b44d upstream. 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. Link: https://lore.kernel.org/20261027-shmem-swapin-mismatch-v1-1-84e30aa3b9e5@columbia.edu Fixes: c246d236b18b ("mm/shmem: never bypass the swap cache for SWP_SYNCHRONOUS_IO") Signed-off-by: Mani Tofigh Signed-off-by: Andrew Morton Reviewed-by: Baolin Wang Cc: Hugh Dickins Cc: Kairui Song Cc: Chris Li Cc: Kemeng Shi Cc: Nhat Pham Cc: Baoquan He Cc: Barry Song Cc: Youngjun Park Cc: David Hildenbrand Cc: Tal Zussman Cc: Signed-off-by: Greg Kroah-Hartman --- mm/shmem.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2405,11 +2405,14 @@ static int shmem_swapin_folio(struct ino * 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; @@ -2450,6 +2453,15 @@ failed: 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);