Skip to contentSkip to conversation

mm: vmscan: skip anon folios zswap would refuse when writeback is off

PATCH In review Jiayuan Chen 4 participants 8 messages Oct 30 – Nov 2

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover mm: vmscan: skip anon folios zswap would refuse when writeback is off

memory.zswap.writeback=0 is what we set for latency sensitive services:
their cold anon pages may go to zswap, but never to disk, a swapin from disk is a latency spike they cannot afford. Such a memcg can only swap into zswap. When zswap stops taking its pages, because the pool hit max_pool_percent or the memcg hit memory.zswap.max, reclaim still goes through the motions on its anon LRU: every folio it picks gets a swap slot, is unmapped, is refused by zswap_store() and is activated again. Nothing is freed, the folio now sits in the swap cache holding a slot, and the owner takes a minor fault on the next access.

memory.reclaim below is just the easiest way to trigger it. The same loop runs whenever the memcg is reclaimed: when it hits memory.max, or when kswapd walks it under node pressure. Neither is something the service can opt out of.

Patch 1 is a refactor so the memcg zswap check can be reused from reclaim. Patch 2 adds the check to shrink_folio_list(). It cannot go into can_reclaim_anon_pages(): zero-filled folios never reach zswap and must stay reclaimable.

Numbers from a 16-CPU, 32G QEMU guest on mm-new, zswap with max_pool_percent=2 (~640M), a memcg with memory.zswap.writeback=0 holding 3G of 2:1 compressible anon. The pool fills up after ~300k pages. Then 5 x "echo 512M > memory.reclaim", and the task walks its 3G once:

minor faults on the walk
MGLRU classic LRU

  before             786,454      786,452
  after              301,168      301,158

The ~300k after the patch are the pages that really are in zswap (stored_pages was 301,133 / 301,165), swapped back in once each. The other ~485k before the patch are pages zswap refused: unmapped, left in the swap cache, mapped again on the next touch.

Jiayuan Chen (2):
mm: memcontrol: split mem_cgroup_may_zswap() out of
obj_cgroup_may_zswap()
mm: vmscan: skip anon folios zswap would refuse when writeback is off

 include/linux/memcontrol.h |  5 +++++
 include/linux/zswap.h      |  5 +++++
 mm/memcontrol.c            | 26 ++++++++++++++++++--------
 mm/page_io.c               | 15 ++++++++++++++-
 mm/swap.h                  | 12 ++++++++++++
 mm/vmscan.c                | 17 +++++++++++++++++
 mm/zswap.c                 | 11 +++++++++++
 7 files changed, 82 insertions(+), 9 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/2 mm: memcontrol: split mem_cgroup_may_zswap() out of obj_cgroup_may_zswap()

zswap_store() asks obj_cgroup_may_zswap() whether the memcg is under its zswap limit. The next patch wants to ask the same question from reclaim, where it has a memcg but no objcg.

Move the walk into mem_cgroup_may_zswap() taking a memcg and make obj_cgroup_may_zswap() a wrapper around it. Either way the answer is the one zswap_store() gets.

No functional change.

---
 include/linux/memcontrol.h |  5 +++++
 mm/memcontrol.c            | 26 ++++++++++++++++++--------
 2 files changed, 23 insertions(+), 8 deletions(-)
include/linux/memcontrol.h +5 -0
index 74110a324f9e..dab15db2c61a 100644
@@ -1943,11 +1943,16 @@ static inline void mem_cgroup_calculate_protection_path(struct mem_cgroup *root,
 #endif
 
 #if defined(CONFIG_MEMCG) && defined(CONFIG_ZSWAP)
+bool mem_cgroup_may_zswap(struct mem_cgroup *memcg);
 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg);
 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size);
 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size);
 bool mem_cgroup_zswap_writeback_enabled(const struct mem_cgroup *memcg);
 #else
+static inline bool mem_cgroup_may_zswap(struct mem_cgroup *memcg)
+{
+	return true;
+}
 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
 {
 	return true;
mm/memcontrol.c +18 -8
index aad0498a7bd6..f32105a08a91 100644
@@ -6261,8 +6261,8 @@ static struct cftype swap_files[] = {
 
 #ifdef CONFIG_ZSWAP
 /**
- * obj_cgroup_may_zswap - check if this cgroup can zswap
- * @objcg: the object cgroup
+ * mem_cgroup_may_zswap - check if this cgroup can zswap
+ * @memcg: the memory cgroup
  *
  * Check if the hierarchical zswap limit has been reached.
  *
@@ -6272,17 +6272,14 @@ static struct cftype swap_files[] = {
  * spending cycles on compression when there is already no room left
  * or zswap is disabled altogether somewhere in the hierarchy.
  */
-bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
+bool mem_cgroup_may_zswap(struct mem_cgroup *memcg)
 {
-	struct mem_cgroup *memcg, *original_memcg;
 	bool ret = true;
 
 	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
 		return true;
 
-	original_memcg = get_mem_cgroup_from_objcg(objcg);
-	for (memcg = original_memcg; !mem_cgroup_is_root(memcg);
-	     memcg = parent_mem_cgroup(memcg)) {
+	for (; !mem_cgroup_is_root(memcg); memcg = parent_mem_cgroup(memcg)) {
 		unsigned long max = READ_ONCE(memcg->zswap_max);
 		unsigned long pages;
 
@@ -6301,7 +6298,20 @@ bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
 		ret = false;
 		break;
 	}
-	mem_cgroup_put(original_memcg);
+	return ret;
+}
+
+bool obj_cgroup_may_zswap(struct obj_cgroup *objcg)
+{
+	struct mem_cgroup *memcg;
+	bool ret;
+
+	if (!cgroup_subsys_on_dfl(memory_cgrp_subsys))
+		return true;
+
+	memcg = get_mem_cgroup_from_objcg(objcg);
+	ret = mem_cgroup_may_zswap(memcg);
+	mem_cgroup_put(memcg);
 	return ret;
 }
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH 2/2 mm: vmscan: skip anon folios zswap would refuse when writeback is off

In reply to Jiayuan Chen

A memcg with memory.zswap.writeback=0 can only swap into zswap. Once zswap stops taking its pages, because the pool is at its limit or the memcg is at memory.zswap.max, swap_writeout() has nowhere to put a dirty folio and returns AOP_WRITEPAGE_ACTIVATE.

shrink_folio_list() only learns that at the very end. Every folio it picks gets a swap slot, is unmapped, fails in swap_writeout() and goes back to the active list, to be aged and tried again. Each round costs a slot, an rmap walk, a TLB flush and a minor fault for the owner, and frees nothing. The folio also stays in the swap cache holding its slot.

The obvious place for a zswap check is can_reclaim_anon_pages(), but it cannot go there. Zero-filled folios never reach zswap:
swap_writeout() marks them in the swap table and returns before zswap_store() is called. They need no pool space, they are common, and a memcg-wide "no anon" decision would keep them in memory for as long as zswap is full. With zswap full they must still be reclaimable.

So decide per folio in shrink_folio_list() instead. When swap_writeout() is bound to return AOP_WRITEPAGE_ACTIVATE for a folio, activate it right there, before it costs a swap slot and an unmap. Zero-filled folios go on as before (that check reads the whole folio, so it comes after the zswap one and only runs when zswap is blocked). A swap cache folio with a clean flag may still be dirty in the page table; it goes on too, unmap settles it and the next round sees the dirty flag. An unmapped swap cache folio goes on as well: nobody holds its slot any more, so swap_writeout() drops it in folio_free_swap() and the folio is freed without a store. Large folios go on too. They may still be split, before the swap allocation or when it fails, and the base folios then come back through this check, zero-filled ones go on, the rest is stopped. Keeping the split path as it is keeps the change small.
zswap_store_blocked() asks zswap_check_limits() itself, the same call zswap_store() makes first, so the pool-full state keeps being updated while vmscan turns folios away and pool_limit_hit counts once per batch instead of once per folio. A refused store would have kicked the zswap shrinker to make room, so zswap_store_blocked() does that too.

The check is made once per batch, at the first folio it could stop, and the whole batch goes by it. A batch of file folios never asks zswap anything. zswap can change state under us while the batch is being worked through, that is fine: at worst the tail of one batch takes the old path and is refused in swap_writeout() as before, or is activated once more. Most folios are still stopped early, and the next batch sees the new state.

Only anon folios are handled. shmem splits large folios and zeroes fallocated ones on its own way to swap, that is not for vmscan to second-guess.

---
 include/linux/zswap.h |  5 +++++
 mm/page_io.c          | 15 ++++++++++++++-
 mm/swap.h             | 12 ++++++++++++
 mm/vmscan.c           | 17 +++++++++++++++++
 mm/zswap.c            | 11 +++++++++++
 5 files changed, 59 insertions(+), 1 deletion(-)
include/linux/zswap.h +5 -0
index df6cafbe95dc..b64fcdeda575 100644
@@ -33,6 +33,7 @@ void zswap_swapoff(int type);
 void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg);
 void zswap_lruvec_state_init(struct lruvec *lruvec);
 void zswap_folio_swapin(struct folio *folio);
+bool zswap_store_blocked(struct mem_cgroup *memcg);
 bool zswap_is_enabled(void);
 bool zswap_never_enabled(void);
 #else
@@ -62,6 +63,10 @@ static inline void zswap_swapoff(int type) {}
 static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {}
 static inline void zswap_lruvec_state_init(struct lruvec *lruvec) {}
 static inline void zswap_folio_swapin(struct folio *folio) {}
+static inline bool zswap_store_blocked(struct mem_cgroup *memcg)
+{
+	return false;
+}
 
 static inline bool zswap_is_enabled(void)
 {
mm/page_io.c +14 -1
index c6824fcd483e..b4a9fe401a3c 100644
@@ -125,7 +125,7 @@ int generic_swapfile_activate(struct swap_info_struct *sis,
 	goto out;
 }
 
-static bool is_folio_zero_filled(struct folio *folio)
+bool is_folio_zero_filled(struct folio *folio)
 {
 	unsigned int pos, last_pos;
 	unsigned long *data;
@@ -197,6 +197,19 @@ static void swap_zeromap_folio_clear(struct folio *folio)
 	swap_cluster_unlock(ci);
 }
 
+/*
+ * True when swap_writeout() would only activate the dirty folios of @memcg:
+ * zswap refuses them and they may not go to the device. Zero-filled folios
+ * are the exception, see swap_writeout().
+ */
+bool swap_writeout_blocked(struct mem_cgroup *memcg)
+{
+	/* The device takes the folio, also when zswap is off */
+	if (mem_cgroup_zswap_writeback_enabled(memcg))
+		return false;
+	return zswap_store_blocked(memcg);
+}
+
 /*
  * We may have stale swap cache pages in memory: notice
  * them here and get rid of the unnecessary final write.
mm/swap.h +12 -0
index d5bf21f517dc..51d00e37870c 100644
@@ -259,6 +259,8 @@ void swap_read_submit(struct swap_io_ctx *ctx);
 void swap_write_submit(struct swap_io_ctx *ctx);
 int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio);
 void __swap_writeout(struct swap_io_ctx *ctx, struct folio *folio);
+bool swap_writeout_blocked(struct mem_cgroup *memcg);
+bool is_folio_zero_filled(struct folio *folio);
 
 /* linux/mm/swap_state.c */
 extern struct address_space swap_space __read_mostly;
@@ -428,6 +430,16 @@ static inline int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)
 	return 0;
 }
 
+static inline bool swap_writeout_blocked(struct mem_cgroup *memcg)
+{
+	return false;
+}
+
+static inline bool is_folio_zero_filled(struct folio *folio)
+{
+	return false;
+}
+
 static inline int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)
 {
 	return -EINVAL;
mm/vmscan.c +17 -0
index daa7e37c2236..d732cd378b55 100644
@@ -1169,6 +1169,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 	unsigned int nr_reclaimed = 0, nr_demoted = 0;
 	unsigned int pgactivate = 0;
 	bool do_demote_pass;
+	int swap_blocked = -1;
 	struct swap_io_ctx ctx = {};
 
 	folio_batch_init(&free_folios);
@@ -1355,6 +1356,22 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 			continue;
 		}
 
+		/*
+		 * swap_writeout() would only activate this folio. Do that now,
+		 * before it costs a swap slot and an unmap. Zero-filled folios
+		 * are marked in the swap table without a store, an unmapped one
+		 * may only need its stale slot dropped and a large one may still
+		 * be split, let them go on.
+		 */
+		if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+		    folio_mapped(folio) && !folio_test_large(folio) &&
+		    (!folio_test_swapcache(folio) || folio_test_dirty(folio))) {
+			if (swap_blocked < 0)
+				swap_blocked = swap_writeout_blocked(memcg);
+			if (swap_blocked && !is_folio_zero_filled(folio))
+				goto activate_locked;
+		}
+
 		/*
 		 * Anonymous process memory has backing store?
 		 * Try to allocate it some swap space here.
mm/zswap.c +11 -0
index 572075bb1c1c..468bdec6931a 100644
@@ -566,6 +566,17 @@ static bool zswap_check_limits(void)
 	return zswap_pool_reached_full;
 }
 
+/* True when zswap_store() would refuse every folio of @memcg right now */
+bool zswap_store_blocked(struct mem_cgroup *memcg)
+{
+	if (zswap_check_limits()) {
+		/* A refused store would kick the shrinker, do the same */
+		queue_work(shrink_wq, &zswap_shrink_work);
+		return true;
+	}
+	return !mem_cgroup_may_zswap(memcg);
+}
+
 /*********************************
 * param callbacks
 **********************************/
Signature · 1 line
2.43.0
maintainer
#3 lore raw
Quoted 12 lines · Jiayuan Chen

A memcg with memory.zswap.writeback=0 can only swap into zswap. Once zswap stops taking its pages, because the pool is at its limit or the memcg is at memory.zswap.max, swap_writeout() has nowhere to put a dirty folio and returns AOP_WRITEPAGE_ACTIVATE.

shrink_folio_list() only learns that at the very end. Every folio it picks gets a swap slot, is unmapped, fails in swap_writeout() and goes back to the active list, to be aged and tried again. Each round costs a slot, an rmap walk, a TLB flush and a minor fault for the owner, and frees nothing. The folio also stays in the swap cache holding its slot.

Quoted 6 lines

The obvious place for a zswap check is can_reclaim_anon_pages(), but it cannot go there. Zero-filled folios never reach zswap:
swap_writeout() marks them in the swap table and returns before zswap_store() is called. They need no pool space, they are common, and a memcg-wide "no anon" decision would keep them in memory for as long as zswap is full. With zswap full they must still be reclaimable.

I really wonder if we should revisit not swapping zero-filled pages at all, and instead mapping them to the zero page read-only (and whatever the equivalent for shmem could be), because yes, the right place is can_reclaim_anon_pages(). I don't remember what happened to that proposal, Usama do you remember?

reviewer
#4 lore raw
Quoted 21 lines · Yosry Ahmed
Quoted 12 lines · Jiayuan Chen

A memcg with memory.zswap.writeback=0 can only swap into zswap. Once zswap stops taking its pages, because the pool is at its limit or the memcg is at memory.zswap.max, swap_writeout() has nowhere to put a dirty folio and returns AOP_WRITEPAGE_ACTIVATE.

shrink_folio_list() only learns that at the very end. Every folio it picks gets a swap slot, is unmapped, fails in swap_writeout() and goes back to the active list, to be aged and tried again. Each round costs a slot, an rmap walk, a TLB flush and a minor fault for the owner, and frees nothing. The folio also stays in the swap cache holding its slot.

Quoted 6 lines

The obvious place for a zswap check is can_reclaim_anon_pages(), but it cannot go there. Zero-filled folios never reach zswap:
swap_writeout() marks them in the swap table and returns before zswap_store() is called. They need no pool space, they are common, and a memcg-wide "no anon" decision would keep them in memory for as long as zswap is full. With zswap full they must still be reclaimable.

Quoted 5 lines

I really wonder if we should revisit not swapping zero-filled pages at all, and instead mapping them to the zero page read-only (and whatever the equivalent for shmem could be), because yes, the right place is can_reclaim_anon_pages(). I don't remember what happened to that proposal, Usama do you remember?

Hi Yosry,

FYI, I recalled that we discussed something similar in this thread too:
https://lore.kernel.org/linux-mm/[email protected]/

It would be nice to see an implementation that maps zero-filled pages to the shared zero page.

Best,
Barry

maintainer
#5 lore raw
Quoted 28 lines · Barry Song
Quoted 21 lines · Yosry Ahmed
Quoted 12 lines · Jiayuan Chen

A memcg with memory.zswap.writeback=0 can only swap into zswap. Once zswap stops taking its pages, because the pool is at its limit or the memcg is at memory.zswap.max, swap_writeout() has nowhere to put a dirty folio and returns AOP_WRITEPAGE_ACTIVATE.

shrink_folio_list() only learns that at the very end. Every folio it picks gets a swap slot, is unmapped, fails in swap_writeout() and goes back to the active list, to be aged and tried again. Each round costs a slot, an rmap walk, a TLB flush and a minor fault for the owner, and frees nothing. The folio also stays in the swap cache holding its slot.

Quoted 6 lines

The obvious place for a zswap check is can_reclaim_anon_pages(), but it cannot go there. Zero-filled folios never reach zswap:
swap_writeout() marks them in the swap table and returns before zswap_store() is called. They need no pool space, they are common, and a memcg-wide "no anon" decision would keep them in memory for as long as zswap is full. With zswap full they must still be reclaimable.

Quoted 5 lines

I really wonder if we should revisit not swapping zero-filled pages at all, and instead mapping them to the zero page read-only (and whatever the equivalent for shmem could be), because yes, the right place is can_reclaim_anon_pages(). I don't remember what happened to that proposal, Usama do you remember?

Quoted 8 lines

Hi Yosry,

FYI, I recalled that we discussed something similar in this thread too:
https://lore.kernel.org/linux-mm/[email protected]/

It would be nice to see an implementation that maps zero-filled pages to the shared zero page.

Yeah I also found Usama's attempt and the complication that were found, unfortunately it's not simple:

https://lore.kernel.org/linux-mm/[email protected]/.

#6 lore raw
Quoted 31 lines · Yosry Ahmed
Quoted 30 lines · Barry Song
Quoted 18 lines · Yosry Ahmed
Quoted 11 lines · Jiayuan Chen

A memcg with memory.zswap.writeback=0 can only swap into zswap. Once zswap stops taking its pages, because the pool is at its limit or the memcg is at memory.zswap.max, swap_writeout() has nowhere to put a dirty folio and returns AOP_WRITEPAGE_ACTIVATE.

shrink_folio_list() only learns that at the very end. Every folio it picks gets a swap slot, is unmapped, fails in swap_writeout() and goes back to the active list, to be aged and tried again. Each round costs a slot, an rmap walk, a TLB flush and a minor fault for the owner, and frees nothing. The folio also stays in the swap cache holding its slot.

Quoted 6 lines

The obvious place for a zswap check is can_reclaim_anon_pages(), but it cannot go there. Zero-filled folios never reach zswap:
swap_writeout() marks them in the swap table and returns before zswap_store() is called. They need no pool space, they are common, and a memcg-wide "no anon" decision would keep them in memory for as long as zswap is full. With zswap full they must still be reclaimable.

Quoted 5 lines

I really wonder if we should revisit not swapping zero-filled pages at all, and instead mapping them to the zero page read-only (and whatever the equivalent for shmem could be), because yes, the right place is can_reclaim_anon_pages(). I don't remember what happened to that proposal, Usama do you remember?

Hi Yosry,

FYI, I recalled that we discussed something similar in this thread too:
https://lore.kernel.org/linux-mm/[email protected]/

Quoted 6 lines
Quoted 2 lines

It would be nice to see an implementation that maps zero-filled pages to the shared zero page.

Yeah I also found Usama's attempt and the complication that were found, unfortunately it's not simple:

https://lore.kernel.org/linux-mm/[email protected]/.

Exactly, it is complicated.
Taking zero-filled pages out of the swap path doesn't look like an easy thing to do.

#7 lore raw
In reply to Jiayuan Chen
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] mm: vmscan: unmapped swapcache folios bypass swap_writeout_blocked() - [Medium] mm: vmscan…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] mm: vmscan: unmapped swapcache folios bypass swap_writeout_blocked()
- [Medium] mm: vmscan: large and split head folios bypass swap_writeout_blocked() --

commit 6d6f21b971f8e3a837e970c969d5f1fdbeb97cbe
Author: Jiayuan Chen <[email protected]>

mm: vmscan: skip anon folios zswap would refuse when writeback is off

This commit adds swap_writeout_blocked() and zswap_store_blocked() to detect when a memcg with memory.zswap.writeback=0 cannot store pages in zswap. In shrink_folio_list(), non-zero-filled anonymous folios are activated early before allocating swap slots and unmapping them when swap writeout is blocked.

Quoted 21 lines
mm/vmscan.c +6 -0
index 7ddef3b488b3d..591a98cd9ef59 100644
@@ -1169,6 +1169,7 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 	unsigned int nr_reclaimed = 0, nr_demoted = 0;
 	unsigned int pgactivate = 0;
 	bool do_demote_pass;
+	int swap_blocked = -1;
 	struct swap_io_ctx ctx = {};
 
 	folio_batch_init(&free_folios);
@@ -1355,6 +1356,22 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,
 			continue;
 		}
 
+		/*
+		 * swap_writeout() would only activate this folio. Do that now,
+		 * before it costs a swap slot and an unmap. Zero-filled folios
+		 * are marked in the swap table without a store, an unmapped one
+		 * may only need its stale slot dropped and a large one may still

mm/vmscan.c static unsigned int shrink_folio_list(struct list_head *folio_list, · line 1373

Quoted 10 lines
mm/vmscan.c +10 -0
+		 * be split, let them go on.
+		 */
+		if (folio_test_anon(folio) && folio_test_swapbacked(folio) &&
+		    folio_mapped(folio) && !folio_test_large(folio) &&
+		    (!folio_test_swapcache(folio) || folio_test_dirty(folio))) {
+			if (swap_blocked < 0)
+				swap_blocked = swap_writeout_blocked(memcg);
+			if (swap_blocked && !is_folio_zero_filled(folio))
+				goto activate_locked;
+		}

[Severity: Medium]
Does requiring folio_mapped(folio) allow unmapped dirty swap-cache folios with active swap PTEs to bypass this check on subsequent reclaim passes?

When try_to_unmap() unmaps an anonymous folio during reclaim, try_to_unmap_one() installs non-present swap PTEs via folio_dup_swap() and removes the present PTEs. Once swap_writeout() rejects the folio with AOP_WRITEPAGE_ACTIVATE (for example, when a clean swap-cache folio had a dirty PTE settled by unmap, or when a folio was in the batch as zswap became full), the folio stays in the swap cache marked dirty with folio_mapped(folio) == false and folio_maybe_swapped(folio) == true.

If userspace does not fault the cold folio back in, folio_mapped(folio) remains false on every subsequent pass through shrink_folio_list(), so the folio skips this check and enters pageout() -> swap_writeout():

mm/page_io.c:swap_writeout() {
    if (folio_free_swap(folio))
        goto out_unlock;
    if (is_folio_zero_filled(folio)) {
        ...
    }
    swap_zeromap_folio_clear(folio);
    if (zswap_store(folio)) {
        ...
    }
    if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio)))
        return AOP_WRITEPAGE_ACTIVATE;
}

Because folio_free_swap() checks folio_maybe_swapped(folio), it returns false when swap PTEs still reference the slot:

mm/swapfile.c:folio_free_swap() {
    if (!folio_swapcache_freeable(folio))
        return false;
    if (folio_maybe_swapped(folio))
        return false;
    ...
}

Wouldn't those unmapped swap-cache folios repeatedly run is_folio_zero_filled(), swap_zeromap_folio_clear(), and zswap_store() on every reclaim cycle instead of being activated early?

[Severity: Medium]
How does !folio_test_large(folio) interact with CONFIG_THP_SWAP and with the head folio left behind by split_folio_to_list()?

First, when CONFIG_THP_SWAP is enabled, a normal fully-mapped large anonymous folio is not split before folio_alloc_swap():

mm/vmscan.c:shrink_folio_list() {
    if (folio_test_large(folio)) {
        if (folio_expected_ref_count(folio) !=
            folio_ref_count(folio) - 1)
            goto activate_locked;
        if (data_race(!list_empty(&folio->_deferred_list) &&
            folio_test_partially_mapped(folio)) &&
            split_folio_to_list(folio, folio_list))
            goto activate_locked;
    }
    ret = folio_alloc_swap(folio);
}

When folio_alloc_swap() succeeds for the large folio without returning -E2BIG, the entire large folio is unmapped by try_to_unmap() and rejected in swap_writeout() with AOP_WRITEPAGE_ACTIVATE, and remains a large folio on subsequent passes.

Second, even when split_folio_to_list(folio, folio_list) splits a large folio (either for a partially-mapped folio on _deferred_list or when folio_alloc_swap() returns -E2BIG), only the tail pages are queued onto folio_list to re-enter the loop. The head folio (now order-0) continues directly to folio_alloc_swap(folio) and try_to_unmap() in the current iteration without passing through the swap_writeout_blocked() check.

Should large folios and split head folios also be checked against swap_writeout_blocked() before allocating swap and unmapping?

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