Re: [RFC PATCH v4 09/18] mm/swap: avoid direct reclaim when populating swap cluster metadata
From: Kunwu Chan
Date: Thu Nov 19 2026 - 23:31:48 EST
Reviewed-by: Kunwu Chan <kunwu.chan@xxxxxxxxx>
Thanks,
Kunwu
>
> Swap cluster population is entered from swap allocation and may itself
> need memory for slot metadata. Its fallback drops the device and cluster
> locks before allocating with GFP_KERNEL, which can reenter swap
> allocation through direct reclaim.
>
> Make this allocation boundary explicitly non-reclaiming so callers can
> retain outer synchronization without a recursive swap allocation.
> Clear __GFP_DIRECT_RECLAIM from the fallback request while retaining
> __GFP_HIGH and __GFP_NOMEMALLOC. The retry can still wake kswapd, but
> fails without direct reclaim when memory is unavailable.
>
> This can make metadata allocation fail earlier under memory pressure.
> Keep the existing isolated-cluster rollback and restrict this request
> without adding a task-wide PF_MEMALLOC scope.
>
> Signed-off-by: Lian Wang <lianux.mm@xxxxxxxxx>
> ---
> mm/swapfile.c | 23 +++++++++++++----------
> 1 file changed, 13 insertions(+), 10 deletions(-)
>
> diff --git a/mm/swapfile.c b/mm/swapfile.c
> index c46b43414bf4..fae862fc873c 100644
> --- a/mm/swapfile.c
> +++ b/mm/swapfile.c
> @@ -586,13 +586,14 @@ static void swap_cluster_assert_empty(struct swap_cluster_info *ci,
> }
>
> /*
> - * Allocate swap table for one cluster. Attempt an atomic allocation first,
> - * then fallback to sleeping allocation.
> + * Allocate swap table for one cluster. Attempt allocation under the locks
> + * first, then retry with the locks dropped without entering direct reclaim.
> */
> static struct swap_cluster_info *
> swap_cluster_populate(struct swap_info_struct *si,
> struct swap_cluster_info *ci)
> {
> + gfp_t gfp;
> int ret;
>
> /*
> @@ -610,9 +611,11 @@ swap_cluster_populate(struct swap_info_struct *si,
> return ci;
>
> /*
> - * Try a sleep allocation. Each isolated free cluster may cause
> - * a sleep allocation, but there is a limited number of them, so
> - * the potential recursive allocation is limited.
> + * Drop the device and cluster locks before retrying. Metadata allocation
> + * must not recurse into swap allocation through direct reclaim: callers
> + * can retain outer synchronization across cluster population. Restrict
> + * this request instead of granting the task PF_MEMALLOC, which could
> + * give allocator hooks and failure cleanup reserve access.
> */
> spin_unlock(&ci->lock);
> if (si->flags & SWP_SOLIDSTATE)
> @@ -620,8 +623,9 @@ swap_cluster_populate(struct swap_info_struct *si,
> else
> spin_unlock(&si->global_cluster->lock);
>
> - ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
> - GFP_KERNEL, false);
> + gfp = (GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC) &
> + ~__GFP_DIRECT_RECLAIM;
> + ret = swap_cluster_alloc_table(si, ci, gfp, false);
>
> /*
> * Back to atomic context. We might have migrated to a new CPU with a
> @@ -3160,9 +3164,8 @@ static int swap_device_disable(struct address_space *mapping,
> return err;
>
> /*
> - * The swap allocator doesn't take swapon_rwsem. Looping through every
> - * cluster lock after clearing SWP_WRITEOK ensures that allocators see
> - * the updated flag and that no allocation remains in flight.
> + * Drain cluster-lock users after clearing SWP_WRITEOK, so swapoff
> + * does not race with operations that started before the transition.
> */
> for (offset = 0; offset < end; offset += SWAPFILE_CLUSTER) {
> ci = swap_cluster_lock(si, offset);
>