The patch titled Subject: mm, swap: avoid BUG_ON in relocate_cluster() has been added to the -mm mm-hotfixes-unstable branch. Its filename is mm-swap-avoid-bug_on-in-relocate_cluster.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-swap-avoid-bug_on-in-relocate_cluster.patch This patch will later appear in the mm-hotfixes-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Kemeng Shi <shikemeng@xxxxxxxxxxxxxxx> Subject: mm, swap: avoid BUG_ON in relocate_cluster() Date: Sun, 23 Feb 2025 00:08:47 +0800 If allocation is racy with swapoff, we may call free_cluster for cluster already in free list and trigger BUG_ON() as following: Allocation Swapoff cluster_alloc_swap_entry ... /* may get a free cluster with offset */ offset = xxx; if (offset) ci = lock_cluster(si, offset); ... del_from_avail_list(p, true); si->flags &= ~SWP_WRITEOK; alloc_swap_scan_cluster(si, ci, ...) ... /* failed to alloc entry from free entry */ if (!cluster_alloc_range(...)) break; ... /* add back a free cluster */ relocate_cluster(si, ci); if (!ci->count) free_cluster(si, ci); VM_BUG_ON(ci->flags == CLUSTER_FLAG_FREE); To prevent the BUG_ON(), call free_cluster() for free cluster to move the cluster to tail of list. Check cluster is not free before calling free_cluster() in relocate_cluster() to avoid BUG_ON(). Link: https://lkml.kernel.org/r/20250222160850.505274-4-shikemeng@xxxxxxxxxxxxxxx Fixes: 3b644773eefda ("mm, swap: reduce contention on device lock") Signed-off-by: Kemeng Shi <shikemeng@xxxxxxxxxxxxxxx> Reviewed-by: Kairui Song <kasong@xxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/swapfile.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) --- a/mm/swapfile.c~mm-swap-avoid-bug_on-in-relocate_cluster +++ a/mm/swapfile.c @@ -653,7 +653,8 @@ static void relocate_cluster(struct swap return; if (!ci->count) { - free_cluster(si, ci); + if (ci->flags != CLUSTER_FLAG_FREE) + free_cluster(si, ci); } else if (ci->count != SWAPFILE_CLUSTER) { if (ci->flags != CLUSTER_FLAG_FRAG) move_cluster(si, ci, &si->frag_clusters[ci->order], _ Patches currently in -mm which might be from shikemeng@xxxxxxxxxxxxxxx are mm-swap-avoid-losting-cluster-in-swap_reclaim_full_clusters.patch mm-swap-use-correct-step-in-loop-to-wait-all-clusters-in-wait_for_allocation.patch mm-swap-avoid-bug_on-in-relocate_cluster.patch mm-swap-remove-setting-swap_map_bad-for-discard-cluster.patch mm-swap-correct-comment-in-swap_usage_sub.patch mm-swap-remove-stale-comment-of-swap_reclaim_full_clusters.patch