From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 565A9125B2; Mon, 9 Feb 2026 14:35:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770647750; cv=none; b=fUgQSxWNVzL2GJsxq1CNYGTOzaHXZbZIHuRiAUUYDPgq8tLbmFKT+k+XCRjAKOU2zbb2/Hn9Br5JrY303XkCUHZ/tg6i0lcUuGDDbysLVuHI0mgHdKr1icHmNrxBkNMorxgEuZFBYiHM6X6QGQp9SP3QRSlTHf194Uijw2Q1jsM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770647750; c=relaxed/simple; bh=yULTwE7bi7PNQi7F9bNEQL1/yFi/qMJIGXVVnjpzXuM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gS+/CWyHUGr3IOdBVSIJ125tX/9FrKrQQvaGOj6G35Cmi+VR+BkgZcup4LJJfKC2s54FbBrFYpO81SC9IcPLy9cHmLysK/i48EzbL9APl3waPIIl394KeRc3E9z+T8r7YZ99CxjUoiDLiakPf4ZlTONUfEa92/sfojeRsZJYglQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=C6hNvEpX; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="C6hNvEpX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D2DD4C16AAE; Mon, 9 Feb 2026 14:35:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1770647750; bh=yULTwE7bi7PNQi7F9bNEQL1/yFi/qMJIGXVVnjpzXuM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=C6hNvEpXt67zM955xZj0D1/+jr8EWOPZQghQj1uVsDbcfPclXNY3FZ/Ypov1ZwLYF oRj0V3WRfbUYDfuIMvUgHXyQsc74bADRXk4N7MShtu5kLCTleRLPXHYzeb+CAdBEx/ P7me4L8rn0UALtLbfUARYgoPROhRjmiKRV6DPd9o= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Kairui Song , Chris Mason , Baolin Wang , Baoquan He , Barry Song , Chris Li , Hugh Dickins , Kemeng Shi , Nhat Pham , Andrew Morton Subject: [PATCH 6.12 018/113] mm, shmem: prevent infinite loop on truncate race Date: Mon, 9 Feb 2026 15:22:47 +0100 Message-ID: <20260209142310.865603787@linuxfoundation.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260209142310.204833231@linuxfoundation.org> References: <20260209142310.204833231@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.12-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kairui Song commit 2030dddf95451b4e7a389f052091e7c4b7b274c6 upstream. When truncating a large swap entry, shmem_free_swap() returns 0 when the entry's index doesn't match the given index due to lookup alignment. The failure fallback path checks if the entry crosses the end border and aborts when it happens, so truncate won't erase an unexpected entry or range. But one scenario was ignored. When `index` points to the middle of a large swap entry, and the large swap entry doesn't go across the end border, find_get_entries() will return that large swap entry as the first item in the batch with `indices[0]` equal to `index`. The entry's base index will be smaller than `indices[0]`, so shmem_free_swap() will fail and return 0 due to the "base < index" check. The code will then call shmem_confirm_swap(), get the order, check if it crosses the END boundary (which it doesn't), and retry with the same index. The next iteration will find the same entry again at the same index with same indices, leading to an infinite loop. Fix this by retrying with a round-down index, and abort if the index is smaller than the truncate range. Link: https://lkml.kernel.org/r/aXo6ltB5iqAKJzY8@KASONG-MC4 Fixes: 809bc86517cc ("mm: shmem: support large folio swap out") Fixes: 8a1968bd997f ("mm/shmem, swap: fix race of truncate and swap entry split") Signed-off-by: Kairui Song Reported-by: Chris Mason Closes: https://lore.kernel.org/linux-mm/20260128130336.727049-1-clm@meta.com/ Reviewed-by: Baolin Wang Cc: Baoquan He Cc: Barry Song Cc: Chris Li Cc: Hugh Dickins Cc: Kemeng Shi Cc: Nhat Pham Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- mm/shmem.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) --- a/mm/shmem.c +++ b/mm/shmem.c @@ -1109,17 +1109,22 @@ whole_folios: swaps_freed = shmem_free_swap(mapping, indices[i], end - 1, folio); if (!swaps_freed) { - /* - * If found a large swap entry cross the end border, - * skip it as the truncate_inode_partial_folio above - * should have at least zerod its content once. - */ + pgoff_t base = indices[i]; + order = shmem_confirm_swap(mapping, indices[i], radix_to_swp_entry(folio)); - if (order > 0 && indices[i] + (1 << order) > end) - continue; - /* Swap was replaced by page: retry */ - index = indices[i]; + /* + * If found a large swap entry cross the end or start + * border, skip it as the truncate_inode_partial_folio + * above should have at least zerod its content once. + */ + if (order > 0) { + base = round_down(base, 1 << order); + if (base < start || base + (1 << order) > end) + continue; + } + /* Swap was replaced by page or extended, retry */ + index = base; break; } nr_swaps_freed += swaps_freed;