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=1796382502; b=NuJsDQ8iBOQSBwF7VtnjU6h6Bqt4RSjgR7iYifRSDYtBCcCJpmHqpZHqBjvK7j9BSQqR lQGIdO1ZmrDY6pxJ+oNKgaOXm/Y6Fi4BBeffinm/RDU42fCeoN+H1+N1e9mVZb/BWI5Nq WCKANR+pam+4VdXWtcE+6o2vE6oWg4MUroROgFsPOs/jG1J9pTbTz1xRQR98SIyqPduO5 BpUNWaMr33ElkltjTWJ0uLG0FZ2y3U56R59RXk4gB3cSGXwFFrwOR7J1hgiWACwaEvd+q 0ofJutWZp9w7KHVnWYGGdkllRfs8JxXHtOmX4kYsRGY9GC8UJSjuqQojYWQ6lAmAB1g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796382502; 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=OyELIJm28mVPKp+qs3BAES+risQ0mFWWsZ+e4iRE6Cs=; b=kjRN2esSTENujT+Zsx5dxgyUmLYkt47NT24XxXZtGsqsINDY/mbOgp72AE9sjjZUtfzJ VMqDdJV9Y6Pl8GWWKUdmvVW12rZCqjp1sFZT+xkWrFD04IKmR1cH74IQHEZa6gYWtq2MR E+nXcNjYNfBLpoHxEXwQtAgPNFTEsQSrw+nWwF2/EAl7aRRJiYYuO7UmKg563NGONQ09M 8uQ6iaXWoijvhd0O3CvixsxDGwX5u+hVEwiT3INUMQHdnJDGemK6dy9N02AF/pipTLP+x oCawulSIMgd12ZrFSv7dN0fbrvopAITPu+7Lt0fUoeeQ7Txs9908FnQKmDcQFmJn38Q== 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=o0l0CaiN; 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 E36B4606A337; Fri, 4 Dec 2026 11:08:21 +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=o0l0CaiN Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57FE81F000CE; Fri, 4 Dec 2026 11:08:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796382501; bh=OyELIJm28mVPKp+qs3BAES+risQ0mFWWsZ+e4iRE6Cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=o0l0CaiNwznACZckWPfX+59huraxgBUO+iZNp3NEvu95cufrW+xR3jsvi1JVXRbgs prRcYvKSrx7qyZvf3Q8tvtavwcY7tCDkeH1oytLXQ5xqjZt9vNaxr8YZ4Cnjm9oCgA 3Z3ScAoNfvrJsvlmIW2VIYvGvTf201oVNYJTSaPk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Mani Tofigh , Andrew Morton , Kairui Song , Chris Li , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Youngjun Park , Tal Zussman , Kairui Song Subject: [PATCH 6.18 481/703] mm, swap: stop cluster list scans once swapoff clears SWP_WRITEOK Date: Fri, 4 Dec 2026 19:06:20 +0900 Message-ID: <20261204100640.597907801@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100627.522266145@linuxfoundation.org> References: <20261204100627.522266145@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 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Mani Tofigh commit 53a6f71c147a445a6c305513acaa25f2e01a44e9 upstream. SWP_WRITEOK is cleared under si->lock when swapoff starts so that no new swap entries are allocated from the device, and try_to_unuse() then waits for writeback of the folios that still use the device. However, an order-0 allocation that chose the device can race with swapoff clearing the flag and then loop forever. It takes a cluster from the fragment cluster list of a higher order with isolate_lock_cluster(), the allocation from that cluster fails because SWP_WRITEOK is clear, and relocate_cluster() puts the cluster back on the same list, so the loop keeps finding clusters on it. The looping task is in reclaim, and it may have set writeback on a folio whose write has been queued but not yet submitted. swapoff then waits for that folio, while the reclaiming task, stuck in the loop, never reaches the point where it submits the write: CPU0 (reclaim) CPU1 (swapoff) -------------- -------------- shrink_folio_list() pageout(F) /* F under writeback, write not submitted */ folio_alloc_swap(G) /* order 0 */ swap_alloc_slow() del_from_avail_list(si, true) si->flags &= ~SWP_WRITEOK try_to_unuse() folio_wait_writeback(F) cluster_alloc_swap_entry() isolate_lock_cluster() /* allocation fails: !SWP_WRITEOK */ relocate_cluster() /* repeats forever */ swap_write_submit() /* never reached */ This results in a soft lockup on CPU0 and a swapoff that never returns. isolate_lock_cluster() used to check SWP_WRITEOK under si->lock until commit 07adc4cf1ecd ("mm, swap: implement dynamic allocation of swap table") removed that check. Restore the check. Since swapoff clears the flag under the same lock, isolate_lock_cluster() then returns NULL, which ends the scan; the swap slot allocation for G fails, and reclaim goes on to submit the write of F. Given that this is a race, its reproducibility is non-deterministic. We reproduce it by using a non-rotational swap device that is not synchronous, with 64K mTHP enabled, where one CPU repeatedly pages out two 64K folios and a 4K page with madvise(MADV_PAGEOUT), while other CPUs run swapoff and swapon on the same device. In my tests it triggers within 20s in each run, and with the fix applied, it runs for 300s without any hang/kernel report. This bug was found and reproduced deterministically using a deterministic hypervisor that is currently under development. Link: https://lore.kernel.org/20261026-swapoff-writeok-v1-1-4f4cd420aa33@columbia.edu Fixes: 07adc4cf1ecd ("mm, swap: implement dynamic allocation of swap table") Signed-off-by: Mani Tofigh Signed-off-by: Andrew Morton Acked-by: Kairui Song Cc: Chris Li Cc: Kemeng Shi Cc: Nhat Pham Cc: Baoquan He Cc: Barry Song Cc: Youngjun Park Cc: Tal Zussman Cc: Kairui Song Cc: Signed-off-by: Greg Kroah-Hartman --- mm/swapfile.c | 5 +++++ 1 file changed, 5 insertions(+) --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -599,6 +599,10 @@ static struct swap_cluster_info *isolate struct swap_cluster_info *ci, *found = NULL; spin_lock(&si->lock); + + if (unlikely(!(si->flags & SWP_WRITEOK))) + goto out; + list_for_each_entry(ci, list, list) { if (!spin_trylock(&ci->lock)) continue; @@ -613,6 +617,7 @@ static struct swap_cluster_info *isolate found = ci; break; } +out: spin_unlock(&si->lock); if (found && !cluster_table_is_alloced(found)) {