Re: [PATCH v2 2/3] mm/swap: publish cluster tables after reacquiring allocator locks
From: Kairui Song
Date: Sun Nov 15 2026 - 07:56:52 EST
On Tue, Nov 10, 2026 at 11:14 PM Nhat Pham <nphamcs@xxxxxxxxx> wrote:
>
> Under memory pressure, swap_cluster_populate() can publish a table
> while its locks are dropped for a sleeping allocation. A cached
> allocator can consume and free that table before the populating
> allocator resumes, leaving the resumed scan with a NULL table.
>
> CPU0 CPU1
> alloc_swap_scan_list()
> isolate_lock_cluster()
> swap_cluster_populate()
> drop local/global/ci locks
> swap_cluster_alloc_table()
> rcu_assign_pointer(ci->table, table)
> folio_alloc_swap()
> swap_alloc_fast()
> alloc_swap_scan_cluster()
> __swap_cluster_alloc_entries()
> mem_cgroup_try_charge_swap()
> return -ENOMEM
> swap_cache_del_folio()
> __swap_cache_del_folio()
> __swap_cache_do_del_folio()
> __swap_cluster_free_entries()
> free_cluster()
> __free_cluster()
> swap_cluster_free_table()
> reacquire local/global/ci locks
> return ci
> alloc_swap_scan_cluster()
> cluster_scan_range()
> __swap_table_get() /* NULL table */
>
> Keep the count table, cgroup table and zero bitmap private until
> swap_cluster_alloc_table() restores all allocator locks. Install them
> under the cluster lock and retain it through the scan. Initial bad-slot
> setup takes only the cluster lock because the device is unpublished.
>
> The allocator releases its locks before the sleeping allocation. A
> reclaimer that dropped ci->lock earlier can then resume and put the
> cluster back on the free list. Abandon installation if the list state
> changes or a table appears, freeing only private allocations. Restore
> locks on allocation failure too, so the caller can return a still-isolated
> cluster to its free list.
>
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Link: https://lore.kernel.org/all/sashiko-outbox-181084@xxxxxxxxxx/
> Link: https://lore.kernel.org/all/sashiko-outbox-181086@xxxxxxxxxx/
> Suggested-by: Kairui Song <ryncsn@xxxxxxxxx>
> Fixes: cdd77f84d966 ("mm, swap: consolidate cluster allocation helpers")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Nhat Pham <nphamcs@xxxxxxxxx>
> ---
> mm/swapfile.c | 106 +++++++++++++++++++++++++++++++++++---------------
> 1 file changed, 74 insertions(+), 32 deletions(-)
>
Thanks for the fix!
> @@ -825,9 +862,13 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
>
> ci = cluster_info + idx;
> /* Need to allocate swap table first for initial bad slot marking. */
> - if (!ci->count && swap_cluster_alloc_table(ci, GFP_KERNEL))
> - return -ENOMEM;
> - spin_lock(&ci->lock);
> + if (!ci->count) {
> + ret = swap_cluster_alloc_table(NULL, ci, GFP_KERNEL, false);
> + if (ret)
> + goto out;
> + } else {
> + spin_lock(&ci->lock);
> + }
> /* Check for duplicated bad swap slots. */
> if (__swap_table_xchg(ci, ci_off, SWP_TB_BAD) != SWP_TB_NULL) {
> pr_warn("Duplicated bad slot offset %d\n", offset);
> @@ -835,6 +876,7 @@ static int swap_cluster_setup_bad_slot(struct swap_info_struct *si,
> } else {
> ci->count++;
> }
> +out:
> spin_unlock(&ci->lock);
>
> WARN_ON(ci->count > SWAPFILE_CLUSTER);
> --
> 2.53.0-Meta
Acked-by: Kairui Song <kasong@xxxxxxxxxxx>