[RFC PATCH v3 09/18] mm/swap: avoid direct reclaim when populating swap cluster metadata

From: Lian Wang

Date: Tue Oct 27 2026 - 12:27:22 EST


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 2035513d3c61..68bed5023299 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -545,13 +545,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;

/*
@@ -569,9 +570,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)
@@ -579,8 +582,9 @@ swap_cluster_populate(struct swap_info_struct *si,
else
spin_unlock(&si->global_cluster->lock);

- ret = swap_cluster_alloc_table(ci, __GFP_HIGH | __GFP_NOMEMALLOC |
- GFP_KERNEL);
+ gfp = (GFP_KERNEL | __GFP_HIGH | __GFP_NOMEMALLOC) &
+ ~__GFP_DIRECT_RECLAIM;
+ ret = swap_cluster_alloc_table(ci, gfp);

/*
* Back to atomic context. We might have migrated to a new CPU with a
@@ -3095,9 +3099,8 @@ unlock:
del_from_avail_list(si, true);

/*
- * 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);