Skip to contentSkip to conversation

mm: fix swapcache writeout and cluster table publication

PATCH Superseded Nhat Pham 4 participants 9 messages Nov 6 – Nov 10

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Nhat Pham

author reviewer
#0 lore raw

cover mm: fix swapcache writeout and cluster table publication

Sashiko's review of the vswap series uncovered two bugs in existing swap
code: reclaim skips swapcache folios without anon mappings, and cluster tables can be published while allocator locks are dropped.

Allow swapcache folios through pageout() and keep cluster tables private until the allocator locks are reacquired. The second patch completes Kairui Song's locking sketch.

Nhat Pham (2):
mm: vmscan: allow writeout of swapcache folios without anon mappings mm/swap: publish cluster tables after reacquiring allocator locks

 mm/swapfile.c | 106 +++++++++++++++++++++++++++++++++++---------------
 mm/vmscan.c   |   3 +-
 2 files changed, 76 insertions(+), 33 deletions(-)

base-commit: 32959c9a49d24faffb39413322ef80014f88dd16

author reviewer
#1 lore raw

PATCH 1/2 mm: vmscan: allow writeout of swapcache folios without anon mappings

Unfaulted zswap readahead folios can be dirty and in swapcache without an anonymous mapping. pageout() rejects them before swap_writeout(), so reclaim cannot write them out while their swap entries remain referenced.

Allow swapcache folios through the filesystem folio check. They already have swap entries and can use the existing swap writeout path. Anonymous folios outside swapcache have no mapping and already return PAGE_KEEP.

Fixes 84798514db50 ("mm: Remove swap_writepage() and shmem_writepage()")
---
 mm/vmscan.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/vmscan.c +2 -1
index 7db6be53f3a9..4b014e3c07bd 100644
@@ -714,7 +714,8 @@ static pageout_t pageout(struct swap_io_ctx *ctx, struct address_space *mapping,
 	 */
 	if (folio_ref_count(folio) != 1 + folio_nr_pages(folio) || !mapping)
 		return PAGE_KEEP;
-	if (!shmem_mapping(mapping) && !folio_test_anon(folio))
+	/* Anonymous folios outside swapcache returned PAGE_KEEP above. */
+	if (!shmem_mapping(mapping) && !folio_test_swapcache(folio))
 		return PAGE_ACTIVATE;
 	if (!folio_clear_dirty_for_io(folio))
 		return PAGE_CLEAN;
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] mm: vmscan: allow writeout of swapcache folios without anon mappings

Quoted 9 lines · Nhat Pham

Unfaulted zswap readahead folios can be dirty and in swapcache without an anonymous mapping. pageout() rejects them before swap_writeout(), so reclaim cannot write them out while their swap entries remain referenced.

Allow swapcache folios through the filesystem folio check. They already have swap entries and can use the existing swap writeout path. Anonymous folios outside swapcache have no mapping and already return PAGE_KEEP.

Quoted 6 lines
Fixes 84798514db50 ("mm: Remove swap_writepage() and shmem_writepage()")

---

LGTM.

author reviewer
#3 lore raw

PATCH 2/2 mm/swap: publish cluster tables after reacquiring allocator locks

In reply to Nhat Pham

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
swap_cluster_populate()
drop local/global/ci locks
allocate and publish table
swap_alloc_fast()
consume the table
charge failure cleanup
free the table
reacquire local/global/ci
return ci
alloc_swap_scan_cluster()
read the missing 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.

A stale reclaimer can return the isolated cluster to allocation while its locks are dropped. 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.

---
 mm/swapfile.c | 106 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 74 insertions(+), 32 deletions(-)
mm/swapfile.c +74 -32
index 189f04d8838f..162a73a9e663 100644
@@ -449,15 +449,23 @@ static void swap_cluster_free_table(struct swap_cluster_info *ci)
 	swap_cluster_free_count_table(table);
 }
 
-static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
+/*
+ * Return with ci locked. An unlocked allocator also needs si's outer locks;
+ * initial bad-slot setup passes NULL because the device is not published.
+ */
+static int swap_cluster_alloc_table(struct swap_info_struct *si,
+				    struct swap_cluster_info *ci,
+				    gfp_t gfp, bool locked)
 {
 	struct swap_table *table = NULL;
+#ifdef CONFIG_MEMCG
+	struct swap_memcg_table *memcg_table = NULL;
+#endif
+#if !SWAP_TABLE_HAS_ZEROFLAG
+	unsigned long *zero_bitmap = NULL;
+#endif
 	struct folio *folio;
-
-	/* The cluster must be empty and not on any list during allocation. */
-	VM_WARN_ON_ONCE(ci->flags || !cluster_is_empty(ci));
-	if (rcu_access_pointer(ci->table))
-		return 0;
+	int ret = -ENOMEM;
 
 	if (SWP_TABLE_USE_PAGE) {
 		folio = folio_alloc(gfp | __GFP_ZERO, 0);
@@ -467,27 +475,49 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
 		table = kmem_cache_zalloc(swap_table_cachep, gfp);
 	}
 	if (!table)
-		return -ENOMEM;
+		goto lock;
 
 #ifdef CONFIG_MEMCG
 	if (!mem_cgroup_disabled()) {
-		VM_WARN_ON_ONCE(ci->memcg_table);
-		ci->memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
-		if (!ci->memcg_table) {
-			swap_cluster_free_count_table(table);
-			return -ENOMEM;
-		}
+		memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
+		if (!memcg_table)
+			goto lock;
 	}
 #endif
 
 #if !SWAP_TABLE_HAS_ZEROFLAG
-	VM_WARN_ON_ONCE(ci->zero_bitmap);
-	ci->zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
-	if (!ci->zero_bitmap) {
-		swap_cluster_free_table(ci);
-		swap_cluster_free_count_table(table);
-		return -ENOMEM;
+	zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
+	if (!zero_bitmap)
+		goto lock;
+#endif
+	ret = 0;
+
+lock:
+	if (!locked) {
+		if (si) {
+			local_lock(&percpu_swap_cluster.lock);
+			if (!(si->flags & SWP_SOLIDSTATE))
+				spin_lock(&si->global_cluster_lock);
+		}
+		spin_lock(&ci->lock);
 	}
+	lockdep_assert_held(&ci->lock);
+
+	/* A stale reclaimer may have returned the cluster to allocation. */
+	if (ci->flags != CLUSTER_FLAG_NONE || cluster_table_is_alloced(ci))
+		ret = -EAGAIN;
+	if (ret)
+		goto free;
+
+	/* The cluster must be empty and not on any list during allocation. */
+	VM_WARN_ON_ONCE(!cluster_is_empty(ci));
+#ifdef CONFIG_MEMCG
+	VM_WARN_ON_ONCE(ci->memcg_table);
+	ci->memcg_table = memcg_table;
+#endif
+#if !SWAP_TABLE_HAS_ZEROFLAG
+	VM_WARN_ON_ONCE(ci->zero_bitmap);
+	ci->zero_bitmap = zero_bitmap;
 #endif
 
 	/*
@@ -496,6 +526,17 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
 	 */
 	rcu_assign_pointer(ci->table, table);
 	return 0;
+
+free:
+#ifdef CONFIG_MEMCG
+	kfree(memcg_table);
+#endif
+#if !SWAP_TABLE_HAS_ZEROFLAG
+	bitmap_free(zero_bitmap);
+#endif
+	if (table)
+		swap_cluster_free_count_table(table);
+	return ret;
 }
 
 /*
@@ -546,8 +587,8 @@ swap_cluster_populate(struct swap_info_struct *si,
 		lockdep_assert_held(&si->global_cluster_lock);
 	lockdep_assert_held(&ci->lock);
 
-	if (!swap_cluster_alloc_table(ci, __GFP_HIGH | __GFP_NOMEMALLOC |
-					  __GFP_NOWARN))
+	if (!swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+					      __GFP_NOWARN, true))
 		return ci;
 
 	/*
@@ -560,8 +601,8 @@ swap_cluster_populate(struct swap_info_struct *si,
 		spin_unlock(&si->global_cluster_lock);
 	local_unlock(&percpu_swap_cluster.lock);
 
-	ret = swap_cluster_alloc_table(ci, __GFP_HIGH | __GFP_NOMEMALLOC |
-					   GFP_KERNEL);
+	ret = swap_cluster_alloc_table(si, ci, __GFP_HIGH | __GFP_NOMEMALLOC |
+					     GFP_KERNEL, false);
 
 	/*
 	 * Back to atomic context. We might have migrated to a new CPU with a
@@ -571,13 +612,9 @@ swap_cluster_populate(struct swap_info_struct *si,
 	 * could happen with ignoring the percpu cluster is fragmentation,
 	 * which is acceptable since this fallback and race is rare.
 	 */
-	local_lock(&percpu_swap_cluster.lock);
-	if (!(si->flags & SWP_SOLIDSTATE))
-		spin_lock(&si->global_cluster_lock);
-	spin_lock(&ci->lock);
-
 	if (ret) {
-		move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
+		if (ret != -EAGAIN)
+			move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
 		spin_unlock(&ci->lock);
 		return NULL;
 	}
@@ -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);
author reviewer
#4 lore raw

Re: [PATCH 2/2] mm/swap: publish cluster tables after reacquiring allocator locks

Quoted 30 lines · Nhat Pham

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
swap_cluster_populate()
drop local/global/ci locks
allocate and publish table
swap_alloc_fast()
consume the table
charge failure cleanup
free the table
reacquire local/global/ci
return ci
alloc_swap_scan_cluster()
read the missing 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.

A stale reclaimer can return the isolated cluster to allocation while its locks are dropped. 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.

Quoted 4 lines

I think I need a

here as well...

(Kairui - up to you if you feel like Co-developed-by + Signed-off-by or Suggested-by + Acked-by is more appropriate here. I just filled in the sketch you gave me.)

#5 lore raw

Re: [PATCH 2/2] mm/swap: publish cluster tables after reacquiring allocator locks

Quoted 39 lines · Nhat Pham
Quoted 30 lines · Nhat Pham

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
swap_cluster_populate()
drop local/global/ci locks
allocate and publish table
swap_alloc_fast()
consume the table
charge failure cleanup
free the table
reacquire local/global/ci
return ci
alloc_swap_scan_cluster()
read the missing 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.

A stale reclaimer can return the isolated cluster to allocation while its locks are dropped. 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.

Quoted 4 lines

I think I need a

Quoted 7 lines

here as well...

(Kairui - up to you if you feel like Co-developed-by + Signed-off-by or Suggested-by + Acked-by is more appropriate here. I just filled in the sketch you gave me.)

Let's go with Suggested-by then :), I didn't even check if that sketch actually works, so thanks for taking care of that!

author reviewer
#6 lore raw

Re: [PATCH 2/2] mm/swap: publish cluster tables after reacquiring allocator locks

Quoted 43 lines · Kairui Song
Quoted 41 lines · Nhat Pham
Quoted 30 lines · Nhat Pham

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
swap_cluster_populate()
drop local/global/ci locks
allocate and publish table
swap_alloc_fast()
consume the table
charge failure cleanup
free the table
reacquire local/global/ci
return ci
alloc_swap_scan_cluster()
read the missing 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.

A stale reclaimer can return the isolated cluster to allocation while its locks are dropped. 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.

Quoted 4 lines

I think I need a

Quoted 8 lines
Quoted 5 lines

here as well...

(Kairui - up to you if you feel like Co-developed-by + Signed-off-by or Suggested-by + Acked-by is more appropriate here. I just filled in the sketch you gave me.)

Let's go with Suggested-by then :), I didn't even check if that sketch actually works, so thanks for taking care of that!

Okie dokie - will update that in v2. Sashiko picked out another issue, so I'll use this opportunity to fix it too :)

If something sticks out as obviously wrong to you though please let me know!

#7 lore raw

Re: [PATCH 2/2] mm/swap: publish cluster tables after reacquiring allocator locks

In reply to Nhat Pham
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] mm/swap: NULL deref in swap_cluster_assert_empty() on stale reclaimer race - [Medium] mm/swap: ABA…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm/swap: NULL deref in swap_cluster_assert_empty() on stale reclaimer race
- [Medium] mm/swap: ABA race on cluster state check in swap_cluster_alloc_table()
--

commit 22e220b18bd05df45a828a3170169b92c0c14f76
Author: Nhat Pham <[email protected]>

mm/swap: publish cluster tables after reacquiring allocator locks

This commit keeps newly allocated swap cluster tables private until swap_cluster_alloc_table() reacquires the allocator and cluster locks, and abandons installation if a stale reclaimer returned the cluster while the locks were dropped.

Quoted 4 lines
mm/swapfile.c +0 -0
index 189f04d8838f3..162a73a9e6638 100644

[ ... ]

Quoted 38 lines
mm/swapfile.c +14 -13
@@ -467,27 +475,49 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
 		table = kmem_cache_zalloc(swap_table_cachep, gfp);
 	}
 	if (!table)
-		return -ENOMEM;
+		goto lock;
 
 #ifdef CONFIG_MEMCG
 	if (!mem_cgroup_disabled()) {
-		VM_WARN_ON_ONCE(ci->memcg_table);
-		ci->memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
-		if (!ci->memcg_table) {
-			swap_cluster_free_count_table(table);
-			return -ENOMEM;
-		}
+		memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
+		if (!memcg_table)
+			goto lock;
 	}
 #endif
 
 #if !SWAP_TABLE_HAS_ZEROFLAG
-	VM_WARN_ON_ONCE(ci->zero_bitmap);
-	ci->zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
-	if (!ci->zero_bitmap) {
-		swap_cluster_free_table(ci);
-		swap_cluster_free_count_table(table);
-		return -ENOMEM;
+	zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
+	if (!zero_bitmap)
+		goto lock;
+#endif
+	ret = 0;
+
+lock:
+	if (!locked) {
+		if (si) {
+			local_lock(&percpu_swap_cluster.lock);

mm/swapfile.c static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp) · line 508

Quoted 10 lines
mm/swapfile.c +9 -0
+			if (!(si->flags & SWP_SOLIDSTATE))
+				spin_lock(&si->global_cluster_lock);
+		}
+		spin_lock(&ci->lock);
 	}
+	lockdep_assert_held(&ci->lock);
+
+	/* A stale reclaimer may have returned the cluster to allocation. */
+	if (ci->flags != CLUSTER_FLAG_NONE || cluster_table_is_alloced(ci))
+		ret = -EAGAIN;

[Severity: Medium]
Can a stale reclaimer trigger a NULL pointer dereference in swap_cluster_assert_empty() while ci->lock is dropped here?

Because ci->table now remains NULL across the sleeping allocation in swap_cluster_populate(), a stale reclaimer that dropped ci->lock before the cluster was freed and re-isolated can reacquire ci->lock while ci->count is 0, ci->flags is CLUSTER_FLAG_NONE, and ci->table is NULL:

CPU0 CPU1

                                  cluster_reclaim_range()
                                    spin_unlock(&ci->lock)
                                    /* ci is freed and table is freed */

isolate_lock_cluster()
ci->flags = CLUSTER_FLAG_NONE
swap_cluster_populate()
/* atomic alloc fails */
spin_unlock(&ci->lock)
swap_cluster_alloc_table()

                                    spin_lock(&ci->lock)
                                  ...
                                  relocate_cluster()
                                    free_cluster()
                                      __free_cluster()
                                        swap_cluster_assert_empty()
                                          __swap_table_get()
                                            <-- NULL pointer dereference

In relocate_cluster(), !ci->count and ci->flags != CLUSTER_FLAG_FREE are both true, so it calls free_cluster() -> __free_cluster() without checking cluster_table_is_alloced(ci). With CONFIG_DEBUG_VM enabled, swap_cluster_assert_empty() calls __swap_table_get(), which dereferences the NULL ci->table. With SWP_PAGE_DISCARD enabled, free_cluster() also schedules a redundant discard before swap_do_scheduled_discard() calls __free_cluster() on the tableless cluster.

Should relocate_cluster() avoid calling free_cluster() when !cluster_table_is_alloced(ci)?

[ ... ]

Quoted 6 lines
mm/swapfile.c +0 -2
@@ -571,13 +612,9 @@ swap_cluster_populate(struct swap_info_struct *si,
 	 * could happen with ignoring the percpu cluster is fragmentation,
 	 * which is acceptable since this fallback and race is rare.
 	 */
-	local_lock(&percpu_swap_cluster.lock);
-	if (!(si->flags & SWP_SOLIDSTATE))

mm/swapfile.c swap_cluster_populate(struct swap_info_struct *si, · line 620

Quoted 10 lines
mm/swapfile.c +2 -4
-		spin_lock(&si->global_cluster_lock);
-	spin_lock(&ci->lock);
-
 	if (ret) {
-		move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
+		if (ret != -EAGAIN)
+			move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
 		spin_unlock(&ci->lock);
 		return NULL;
 	}

[Severity: Medium]
Can an ABA race on ci->flags and ci->table cause a failed allocator to put a cluster back onto si->free_clusters while a second allocator still has it isolated?

When CONFIG_DEBUG_VM and SWP_PAGE_DISCARD are disabled, a stale reclaimer calling relocate_cluster() -> free_cluster() -> __free_cluster() moves the tableless cluster back to si->free_clusters. A second allocator can then re-isolate it and also drop ci->lock for a sleeping table allocation:

CPU0 CPU1 CPU2
swap_cluster_populate()
spin_unlock(&ci->lock)
swap_cluster_alloc_table()

    /* alloc fails */
                           relocate_cluster()
                             ...
                             __free_cluster()
                               ci->flags =
                                 CLUSTER_FLAG_FREE
                                                      isolate_lock_cluster()
                                                        ci->flags =
                                                          CLUSTER_FLAG_NONE
                                                      swap_cluster_populate()
                                                        spin_unlock(&ci->lock)
    spin_lock(&ci->lock)
    /* flags == NONE,
     * table == NULL */
    return -ENOMEM

move_cluster(..., FREE) <-- puts ci on free_clusters while CPU2 owns it

When CPU0 reacquires ci->lock, ci->flags is back to CLUSTER_FLAG_NONE and cluster_table_is_alloced(ci) is still false, so swap_cluster_alloc_table() returns -ENOMEM instead of -EAGAIN and CPU0 moves the cluster back onto si->free_clusters while CPU2 is still sleeping in swap_cluster_alloc_table().

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author reviewer
#8 lore raw

Re: [PATCH 2/2] mm/swap: publish cluster tables after reacquiring allocator locks

Quoted 106 lines · On Sat, Nov 7, 2026 at 9:05 AM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] mm/swap: NULL deref in swap_cluster_assert_empty() on stale reclaimer race
- [Medium] mm/swap: ABA race on cluster state check in swap_cluster_alloc_table()
--

commit 22e220b18bd05df45a828a3170169b92c0c14f76
Author: Nhat Pham <[email protected]>

mm/swap: publish cluster tables after reacquiring allocator locks

This commit keeps newly allocated swap cluster tables private until swap_cluster_alloc_table() reacquires the allocator and cluster locks, and abandons installation if a stale reclaimer returned the cluster while the locks were dropped.

Quoted 4 lines
mm/swapfile.c +0 -0
index 189f04d8838f3..162a73a9e6638 100644

[ ... ]

Quoted 38 lines
mm/swapfile.c +14 -13
@@ -467,27 +475,49 @@ static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp)
              table = kmem_cache_zalloc(swap_table_cachep, gfp);
      }
      if (!table)
-             return -ENOMEM;
+             goto lock;
 
 #ifdef CONFIG_MEMCG
      if (!mem_cgroup_disabled()) {
-             VM_WARN_ON_ONCE(ci->memcg_table);
-             ci->memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
-             if (!ci->memcg_table) {
-                     swap_cluster_free_count_table(table);
-                     return -ENOMEM;
-             }
+             memcg_table = kzalloc_obj(*ci->memcg_table, gfp);
+             if (!memcg_table)
+                     goto lock;
      }
 #endif
 
 #if !SWAP_TABLE_HAS_ZEROFLAG
-     VM_WARN_ON_ONCE(ci->zero_bitmap);
-     ci->zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
-     if (!ci->zero_bitmap) {
-             swap_cluster_free_table(ci);
-             swap_cluster_free_count_table(table);
-             return -ENOMEM;
+     zero_bitmap = bitmap_zalloc(SWAPFILE_CLUSTER, gfp);
+     if (!zero_bitmap)
+             goto lock;
+#endif
+     ret = 0;
+
+lock:
+     if (!locked) {
+             if (si) {
+                     local_lock(&percpu_swap_cluster.lock);

mm/swapfile.c static int swap_cluster_alloc_table(struct swap_cluster_info *ci, gfp_t gfp) · line 508

Quoted 10 lines
mm/swapfile.c +9 -0
+                     if (!(si->flags & SWP_SOLIDSTATE))
+                             spin_lock(&si->global_cluster_lock);
+             }
+             spin_lock(&ci->lock);
      }
+     lockdep_assert_held(&ci->lock);
+
+     /* A stale reclaimer may have returned the cluster to allocation. */
+     if (ci->flags != CLUSTER_FLAG_NONE || cluster_table_is_alloced(ci))
+             ret = -EAGAIN;

[Severity: Medium]
Can a stale reclaimer trigger a NULL pointer dereference in swap_cluster_assert_empty() while ci->lock is dropped here?

Because ci->table now remains NULL across the sleeping allocation in swap_cluster_populate(), a stale reclaimer that dropped ci->lock before the cluster was freed and re-isolated can reacquire ci->lock while ci->count is 0, ci->flags is CLUSTER_FLAG_NONE, and ci->table is NULL:

CPU0 CPU1

                                  cluster_reclaim_range()
                                    spin_unlock(&ci->lock)
                                    /* ci is freed and table is freed */

isolate_lock_cluster()
ci->flags = CLUSTER_FLAG_NONE
swap_cluster_populate()
/* atomic alloc fails */
spin_unlock(&ci->lock)
swap_cluster_alloc_table()

                                    spin_lock(&ci->lock)
                                  ...
                                  relocate_cluster()
                                    free_cluster()
                                      __free_cluster()
                                        swap_cluster_assert_empty()
                                          __swap_table_get()
                                            <-- NULL pointer dereference
Quoted 7 lines

In relocate_cluster(), !ci->count and ci->flags != CLUSTER_FLAG_FREE are both true, so it calls free_cluster() -> __free_cluster() without checking cluster_table_is_alloced(ci). With CONFIG_DEBUG_VM enabled, swap_cluster_assert_empty() calls __swap_table_get(), which dereferences the NULL ci->table. With SWP_PAGE_DISCARD enabled, free_cluster() also schedules a redundant discard before swap_do_scheduled_discard() calls __free_cluster() on the tableless cluster.

Hmm this sounds like another pre-existing issue to me... Right now stale reclaimer can get into this exact same situation?

The only thing this patch does is extending the table-less window (i.e push it til after lock acquisition), but the race window exists either way.

Quoted 3 lines

Should relocate_cluster() avoid calling free_cluster() when !cluster_table_is_alloced(ci)?

But this proposal sounds good to me. I'll add this fix to v2.

Quoted 25 lines

[ ... ]

Quoted 6 lines
mm/swapfile.c +0 -2
@@ -571,13 +612,9 @@ swap_cluster_populate(struct swap_info_struct *si,
       * could happen with ignoring the percpu cluster is fragmentation,
       * which is acceptable since this fallback and race is rare.
       */
-     local_lock(&percpu_swap_cluster.lock);
-     if (!(si->flags & SWP_SOLIDSTATE))

mm/swapfile.c swap_cluster_populate(struct swap_info_struct *si, · line 620

Quoted 10 lines
mm/swapfile.c +2 -4
-             spin_lock(&si->global_cluster_lock);
-     spin_lock(&ci->lock);
-
      if (ret) {
-             move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
+             if (ret != -EAGAIN)
+                     move_cluster(si, ci, &si->free_clusters, CLUSTER_FLAG_FREE);
              spin_unlock(&ci->lock);
              return NULL;
      }

[Severity: Medium]
Can an ABA race on ci->flags and ci->table cause a failed allocator to put a cluster back onto si->free_clusters while a second allocator still has it isolated?

Quoted 4 lines

When CONFIG_DEBUG_VM and SWP_PAGE_DISCARD are disabled, a stale reclaimer calling relocate_cluster() -> free_cluster() -> __free_cluster() moves the tableless cluster back to si->free_clusters. A second allocator can then re-isolate it and also drop ci->lock for a sleeping table allocation:

I think if we fix the above issue (i.e not letting relocate_cluster() put the tableless cluster back to free list), we will fix this issue too.

Quoted 3 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2