The patch titled Subject: mm: swap: use correct step in loop to wait all clusters in wait_for_allocation() has been added to the -mm mm-hotfixes-unstable branch. Its filename is mm-swap-use-correct-step-in-loop-to-wait-all-clusters-in-wait_for_allocation.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-swap-use-correct-step-in-loop-to-wait-all-clusters-in-wait_for_allocation.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: use correct step in loop to wait all clusters in wait_for_allocation() Date: Sun, 23 Feb 2025 00:08:46 +0800 Use correct step in loop to wait all clusters in wait_for_allocation(). If we miss some cluster in wait_for_allocation(), use after free may occur as follows: shmem_writepage swapoff folio_alloc_swap get_swap_pages scan_swap_map_slots cluster_alloc_swap_entry alloc_swap_scan_cluster cluster_alloc_range /* SWP_WRITEOK is valid */ if (!(si->flags & SWP_WRITEOK)) ... del_from_avail_list(p, true); ... /* miss the cluster in shmem_writepage */ wait_for_allocation() ... try_to_unuse() memset(si->swap_map + start, usage, nr_pages); swap_range_alloc(si, nr_pages); ci->count += nr_pages; /* return a valid entry */ ... exit_swap_address_space(p->type); ... ... add_to_swap_cache /* dereference swap_address_space(entry) which is NULL */ xas_lock_irq(&xas); Link: https://lkml.kernel.org/r/20250222160850.505274-3-shikemeng@xxxxxxxxxxxxxxx Fixes: 9a0ddeb79880 ("mm, swap: hold a reference during scan and cleanup flag usage") Signed-off-by: Kemeng Shi <shikemeng@xxxxxxxxxxxxxxx> Reviewed-by: Kairui Song <kasong@xxxxxxxxxxx> Signed-off-by: Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> --- mm/swapfile.c | 1 - 1 file changed, 1 deletion(-) --- a/mm/swapfile.c~mm-swap-use-correct-step-in-loop-to-wait-all-clusters-in-wait_for_allocation +++ a/mm/swapfile.c @@ -2645,7 +2645,6 @@ static void wait_for_allocation(struct s for (offset = 0; offset < end; offset += SWAPFILE_CLUSTER) { ci = lock_cluster(si, offset); unlock_cluster(ci); - offset += SWAPFILE_CLUSTER; } } _ 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