Skip to contentSkip to conversation

memcontrol: implement swap bypassing

RFC PATCH In review Nhat Pham 2 participants 4 messages Oct 24, 2023 – Oct 30, 2023

Subsystems (8)
lore
Share & tools
lore

Nhat Pham

author maintainer
#0 lore raw

RFC PATCH 1/1 memcontrol: implement swap bypassing

During our experiment with zswap, we sometimes observe swap IOs due to occasional zswap store failures and writebacks. These swapping IOs prevent many users who cannot tolerate swapping from adopting zswap to save memory and improve performance where possible.

This patch adds the option to bypass swap entirely: do not swap when an zswap store attempt fail, and do not write pages in the zswap pool back to swap. The feature is disabled by default (to preserve the existing behavior), and can be enabled on a cgroup-basis via a new cgroup file.

Note that this is subtly different from setting memory.swap.max to 0, as it still allows for pages to be stored in the zswap pool (which itself consumes swap space in its current form).

This is the second attempt (spiritual successor) of the following patch:

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

and should be applied on top of the zswap shrinker series:

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

---
 Documentation/admin-guide/cgroup-v2.rst | 11 +++++
 Documentation/admin-guide/mm/zswap.rst  |  6 +++
 include/linux/memcontrol.h              | 20 ++++++++++
 mm/memcontrol.c                         | 53 +++++++++++++++++++++++++
 mm/page_io.c                            |  6 +++
 mm/shmem.c                              |  8 +++-
 mm/zswap.c                              |  9 +++++
 7 files changed, 111 insertions(+), 2 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +11 -0
index 606b2e0eac4b..34306d70b3f7 100644
@@ -1657,6 +1657,17 @@ PAGE_SIZE multiple when read back.
 	higher than the limit for an extended period of time.  This
 	reduces the impact on the workload and memory management.
 
+  memory.swap.bypass.enabled
+	A read-write single value file which exists on non-root
+	cgroups.  The default value is "0".
+
+	When this is set to 1, all swapping attempts are disabled.
+	Note that this is subtly different from setting memory.swap.max to
+	0, as it still allows for pages to be written to the zswap pool
+	(which also consumes swap space in its current form). However,
+	zswap store failure will not lead to swapping, and zswap writebacks
+	will be disabled altogether.
+
   memory.zswap.current
 	A read-only single value file which exists on non-root
 	cgroups.
Documentation/admin-guide/mm/zswap.rst +6 -0
index 522ae22ccb84..b7bf481a3e25 100644
@@ -153,6 +153,12 @@ attribute, e. g.::
 
 Setting this parameter to 100 will disable the hysteresis.
 
+Some users cannot tolerate the swapping that comes with zswap store failures
+and zswap writebacks. Swapping can be disabled entirely (without disabling
+zswap itself) on a cgroup-basis as follows:
+
+	echo 1 > /sys/fs/cgroup/<cgroup-name>/memory.swap.bypass.enabled
+
 When there is a sizable amount of cold memory residing in the zswap pool, it
 can be advantageous to proactively write these cold pages to swap and reclaim
 the memory for other use cases. By default, the zswap shrinker is disabled.
include/linux/memcontrol.h +20 -0
index c1846e57011b..e481c5c609f2 100644
@@ -221,6 +221,9 @@ struct mem_cgroup {
 	unsigned long zswap_max;
 #endif
 
+	/* bypass swap (on zswap failure and writebacks) */
+	bool swap_bypass_enabled;
+
 	unsigned long soft_limit;
 
 	/* vmpressure notifications */
@@ -1157,6 +1160,13 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
 						gfp_t gfp_mask,
 						unsigned long *total_scanned);
 
+static inline bool mem_cgroup_swap_bypass_enabled(struct mem_cgroup *memcg)
+{
+	return memcg && READ_ONCE(memcg->swap_bypass_enabled);
+}
+
+bool mem_cgroup_swap_bypass_folio(struct folio *folio);
+
 #else /* CONFIG_MEMCG */
 
 #define MEM_CGROUP_ID_SHIFT	0
@@ -1615,6 +1625,16 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
 {
 	return 0;
 }
+
+static inline bool mem_cgroup_swap_bypass_enabled(struct mem_cgroup *memcg)
+{
+	return false;
+}
+
+static inline bool mem_cgroup_swap_bypass_folio(struct folio *folio)
+{
+	return false;
+}
 #endif /* CONFIG_MEMCG */
 
 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
mm/memcontrol.c +53 -0

71 lines not on this page · raw message

mm/page_io.c +6 -0
index cb559ae324c6..0c84e1592c39 100644
@@ -201,6 +201,12 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
 		folio_end_writeback(folio);
 		return 0;
 	}
+
+	if (mem_cgroup_swap_bypass_folio(folio)) {
+		folio_mark_dirty(folio);
+		return AOP_WRITEPAGE_ACTIVATE;
+	}
+
 	__swap_writepage(&folio->page, wbc);
 	return 0;
 }
mm/shmem.c +6 -2
index cab053831fea..6ce1d4a7a48b 100644
@@ -1514,8 +1514,12 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 
 		mutex_unlock(&shmem_swaplist_mutex);
 		BUG_ON(folio_mapped(folio));
-		swap_writepage(&folio->page, wbc);
-		return 0;
+		/*
+		 * Seeing AOP_WRITEPAGE_ACTIVATE here indicates swapping is disabled on
+		 * zswap store failure. Note that in that case the folio is already
+		 * re-marked dirty by swap_writepage()
+		 */
+		return swap_writepage(&folio->page, wbc);
 	}
 
 	mutex_unlock(&shmem_swaplist_mutex);
mm/zswap.c +9 -0
index c40697f07ba3..f19e26d647a3 100644
@@ -535,6 +535,9 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker,
 	struct zswap_pool *pool = shrinker->private_data;
 	bool encountered_page_in_swapcache = false;
 
+	if (mem_cgroup_swap_bypass_enabled(sc->memcg))
+		return SHRINK_STOP;
+
 	nr_protected =
 		atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected);
 	lru_size = list_lru_shrink_count(&pool->list_lru, sc);
@@ -565,6 +568,9 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
 	struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid));
 	unsigned long nr_backing, nr_stored, nr_freeable, nr_protected;
 
+	if (mem_cgroup_swap_bypass_enabled(memcg))
+		return 0;
+
 #ifdef CONFIG_MEMCG_KMEM
 	cgroup_rstat_flush(memcg->css.cgroup);
 	nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT;
@@ -890,6 +896,9 @@ static int shrink_memcg(struct mem_cgroup *memcg)
 	struct zswap_pool *pool;
 	int nid, shrunk = 0;
 
+	if (mem_cgroup_swap_bypass_enabled(memcg))
+		return -EINVAL;
+
 	/*
 	 * Skip zombies because their LRUs are reparented and we would be
 	 * reclaiming from the parent instead of the dead memcg.
Signature · 1 line
2.34.1
#1 lore raw
Quoted 10 lines · Nhat Pham

During our experiment with zswap, we sometimes observe swap IOs due to occasional zswap store failures and writebacks. These swapping IOs prevent many users who cannot tolerate swapping from adopting zswap to save memory and improve performance where possible.

This patch adds the option to bypass swap entirely: do not swap when an zswap store attempt fail, and do not write pages in the zswap pool back to swap. The feature is disabled by default (to preserve the existing behavior), and can be enabled on a cgroup-basis via a new cgroup file.

I think the word "bypass" here is fairly confusing because zswap is considered as swap for all purposes, so we are not really bypassing swap. I think it should be something like memory.swap.disk or memory.swap.writeback or whatever the correct terminology is for non-zswap swap (which I honestly don't know). Writing 0 to such an interface would give the desired effect.

Same goes for the usage of the term in the code.

Quoted 114 lines

Note that this is subtly different from setting memory.swap.max to 0, as it still allows for pages to be stored in the zswap pool (which itself consumes swap space in its current form).

This is the second attempt (spiritual successor) of the following patch:

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

and should be applied on top of the zswap shrinker series:

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

---
 Documentation/admin-guide/cgroup-v2.rst | 11 +++++
 Documentation/admin-guide/mm/zswap.rst  |  6 +++
 include/linux/memcontrol.h              | 20 ++++++++++
 mm/memcontrol.c                         | 53 +++++++++++++++++++++++++
 mm/page_io.c                            |  6 +++
 mm/shmem.c                              |  8 +++-
 mm/zswap.c                              |  9 +++++
 7 files changed, 111 insertions(+), 2 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +11 -0
index 606b2e0eac4b..34306d70b3f7 100644
@@ -1657,6 +1657,17 @@ PAGE_SIZE multiple when read back.
        higher than the limit for an extended period of time.  This
        reduces the impact on the workload and memory management.
 
+  memory.swap.bypass.enabled
+       A read-write single value file which exists on non-root
+       cgroups.  The default value is "0".
+
+       When this is set to 1, all swapping attempts are disabled.
+       Note that this is subtly different from setting memory.swap.max to
+       0, as it still allows for pages to be written to the zswap pool
+       (which also consumes swap space in its current form). However,
+       zswap store failure will not lead to swapping, and zswap writebacks
+       will be disabled altogether.
+
   memory.zswap.current
        A read-only single value file which exists on non-root
        cgroups.
Documentation/admin-guide/mm/zswap.rst +6 -0
index 522ae22ccb84..b7bf481a3e25 100644
@@ -153,6 +153,12 @@ attribute, e. g.::
 
 Setting this parameter to 100 will disable the hysteresis.
 
+Some users cannot tolerate the swapping that comes with zswap store failures
+and zswap writebacks. Swapping can be disabled entirely (without disabling
+zswap itself) on a cgroup-basis as follows:
+
+       echo 1 > /sys/fs/cgroup/<cgroup-name>/memory.swap.bypass.enabled
+
 When there is a sizable amount of cold memory residing in the zswap pool, it
 can be advantageous to proactively write these cold pages to swap and reclaim
 the memory for other use cases. By default, the zswap shrinker is disabled.
include/linux/memcontrol.h +20 -0
index c1846e57011b..e481c5c609f2 100644
@@ -221,6 +221,9 @@ struct mem_cgroup {
        unsigned long zswap_max;
 #endif
 
+       /* bypass swap (on zswap failure and writebacks) */
+       bool swap_bypass_enabled;
+
        unsigned long soft_limit;
 
        /* vmpressure notifications */
@@ -1157,6 +1160,13 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
                                                gfp_t gfp_mask,
                                                unsigned long *total_scanned);
 
+static inline bool mem_cgroup_swap_bypass_enabled(struct mem_cgroup *memcg)
+{
+       return memcg && READ_ONCE(memcg->swap_bypass_enabled);
+}
+
+bool mem_cgroup_swap_bypass_folio(struct folio *folio);
+
 #else /* CONFIG_MEMCG */
 
 #define MEM_CGROUP_ID_SHIFT    0
@@ -1615,6 +1625,16 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
 {
        return 0;
 }
+
+static inline bool mem_cgroup_swap_bypass_enabled(struct mem_cgroup *memcg)
+{
+       return false;
+}
+
+static inline bool mem_cgroup_swap_bypass_folio(struct folio *folio)
+{
+       return false;
+}
 #endif /* CONFIG_MEMCG */
 
 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
mm/memcontrol.c +0 -0
index 568d9d037a59..f231cf2f745b 100644

mm/memcontrol.c bool mem_cgroup_swap_full(struct folio *folio) · line 7933

Quoted 6 lines
mm/memcontrol.c +3 -0
@@ -7928,6 +7928,28 @@ bool mem_cgroup_swap_full(struct folio *folio)
        return false;
 }
 
+bool mem_cgroup_swap_bypass_folio(struct folio *folio)
+{
+       struct obj_cgroup *objcg = get_obj_cgroup_from_folio(folio);

This is a swapbacked LRU folio, which means that it will have a memcg, not an objcg, in folio->memcg_data (unless this changed recently and I missed it).

This function will get the memcg from the folio, then get its objcg, then get its memcg again. I think this can be much simpler and no refs need to be acquired. I think the folio will always be isolated and locked here (but please do check). We can have a VM_BUG_ON() to ensure that. The comment above folio_memcg() states that isolated or locked folios should have stable memcgs.

We might as well inline a call to folio_memcg() and use the memcg version directly, or keep a separate helper to put the VM_BUG_ON()'s. I don't feel strongly.

Quoted 36 lines
mm/memcontrol.c +29 -0
+       struct mem_cgroup *memcg;
+       bool ret;
+
+       if (!objcg)
+               return false;
+
+       if (mem_cgroup_disabled()) {
+               obj_cgroup_put(objcg);
+               return false;
+       }
+
+       memcg = get_mem_cgroup_from_objcg(objcg);
+       ret = mem_cgroup_swap_bypass_enabled(memcg);
+
+       mem_cgroup_put(memcg);
+       obj_cgroup_put(objcg);
+       return ret;
+}
+
 static int __init setup_swap_account(char *s)
 {
        pr_warn_once("The swapaccount= commandline option is deprecated. "
@@ -8013,6 +8035,31 @@ static int swap_events_show(struct seq_file *m, void *v)
        return 0;
 }
 
+static int swap_bypass_enabled_show(struct seq_file *m, void *v)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
+
+       seq_printf(m, "%d\n", READ_ONCE(memcg->swap_bypass_enabled));
+       return 0;
+}
+
+static ssize_t swap_bypass_enabled_write(struct kernfs_open_file *of,
+                               char *buf, size_t nbytes, loff_t off)

mm/memcontrol.c static int swap_events_show(struct seq_file *m, void *v) · line 8057

Quoted 10 lines
mm/memcontrol.c +10 -0
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
+       int swap_bypass_enabled;
+       ssize_t parse_ret = kstrtoint(strstrip(buf), 0, &swap_bypass_enabled);
+
+       if (parse_ret)
+               return parse_ret;
+
+       if (swap_bypass_enabled != 0 && swap_bypass_enabled != 1)
+               return -ERANGE;

I don't think ERANGE is appropriate here. EINVAL should be used here AFACIT (and looking at other write handlers for memcg interfaces).

Quoted 43 lines
mm/memcontrol.c +11 -0
+
+       WRITE_ONCE(memcg->swap_bypass_enabled, swap_bypass_enabled);
+       return nbytes;
+}
+
 static struct cftype swap_files[] = {
        {
                .name = "swap.current",
@@ -8042,6 +8089,12 @@ static struct cftype swap_files[] = {
                .file_offset = offsetof(struct mem_cgroup, swap_events_file),
                .seq_show = swap_events_show,
        },
+       {
+               .name = "swap.bypass.enabled",
+               .flags = CFTYPE_NOT_ON_ROOT,
+               .seq_show = swap_bypass_enabled_show,
+               .write = swap_bypass_enabled_write,
+       },
        { }     /* terminate */
 };
 
mm/page_io.c +6 -0
index cb559ae324c6..0c84e1592c39 100644
@@ -201,6 +201,12 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
                folio_end_writeback(folio);
                return 0;
        }
+
+       if (mem_cgroup_swap_bypass_folio(folio)) {
+               folio_mark_dirty(folio);
+               return AOP_WRITEPAGE_ACTIVATE;
+       }
+
        __swap_writepage(&folio->page, wbc);
        return 0;
 }
mm/shmem.c +0 -0
index cab053831fea..6ce1d4a7a48b 100644

mm/shmem.c static int shmem_writepage(struct page *page, struct writeback_control *wbc) · line 1519

Quoted 8 lines
mm/shmem.c +3 -2
@@ -1514,8 +1514,12 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 
                mutex_unlock(&shmem_swaplist_mutex);
                BUG_ON(folio_mapped(folio));
-               swap_writepage(&folio->page, wbc);
-               return 0;
+               /*
+                * Seeing AOP_WRITEPAGE_ACTIVATE here indicates swapping is disabled on
+                * zswap store failure. Note that in that case the folio is already

The interface semantics is "bypassing" swap, or not writing to disk for swap. Let's keep it consistent and not mention zswap here.

Quoted 42 lines
mm/shmem.c +3 -0
+                * re-marked dirty by swap_writepage()
+                */
+               return swap_writepage(&folio->page, wbc);
        }
 
        mutex_unlock(&shmem_swaplist_mutex);
mm/zswap.c +9 -1
index c40697f07ba3..f19e26d647a3 100644
@@ -535,6 +535,9 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker,
        struct zswap_pool *pool = shrinker->private_data;
        bool encountered_page_in_swapcache = false;
 
+       if (mem_cgroup_swap_bypass_enabled(sc->memcg))
+               return SHRINK_STOP;
+
        nr_protected =
                atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected);
        lru_size = list_lru_shrink_count(&pool->list_lru, sc);
@@ -565,6 +568,9 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
        struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid));
        unsigned long nr_backing, nr_stored, nr_freeable, nr_protected;
 
+       if (mem_cgroup_swap_bypass_enabled(memcg))
+               return 0;
+
 #ifdef CONFIG_MEMCG_KMEM
        cgroup_rstat_flush(memcg->css.cgroup);
        nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT;
@@ -890,6 +896,9 @@ static int shrink_memcg(struct mem_cgroup *memcg)
        struct zswap_pool *pool;
        int nid, shrunk = 0;
 
+       if (mem_cgroup_swap_bypass_enabled(memcg))
+               return -EINVAL;
+
        /*
         * Skip zombies because their LRUs are reparented and we would be
         * reclaiming from the parent instead of the dead memcg.
--

2.34.1

author maintainer
#2 lore raw
Quoted 13 lines · Yosry Ahmed
Quoted 10 lines · Nhat Pham

During our experiment with zswap, we sometimes observe swap IOs due to occasional zswap store failures and writebacks. These swapping IOs prevent many users who cannot tolerate swapping from adopting zswap to save memory and improve performance where possible.

This patch adds the option to bypass swap entirely: do not swap when an zswap store attempt fail, and do not write pages in the zswap pool back to swap. The feature is disabled by default (to preserve the existing behavior), and can be enabled on a cgroup-basis via a new cgroup file.

Quoted 6 lines

I think the word "bypass" here is fairly confusing because zswap is considered as swap for all purposes, so we are not really bypassing swap. I think it should be something like memory.swap.disk or memory.swap.writeback or whatever the correct terminology is for non-zswap swap (which I honestly don't know). Writing 0 to such an interface would give the desired effect.

I'm honestly not very good at naming things... especially a novel name that I cannot steal from a precedent...

I want something that means "going to zswap is acceptable, but please don't go to slow swap". Hence, swap bypassing (swap in my mental model is the slow thing that is under zswap).

(but I imagined memory.please.don't.go.to.slow.swap won't be very popular either...)

memory.swap.disk is... acceptable, I guess? It technically could still be wrong, as there's still zram, but we can sort of ignore that special case. I still prefer memory.swap.bypass, but I wouldn't be opposed to this renaming.

Anyone with better naming suggestions should absolutely speak up! Otherwise, I'll send a v2 renaming this to "memory.swap.disk" or "memory.swap.disk_used"

Quoted 135 lines

Same goes for the usage of the term in the code.

Quoted 114 lines

Note that this is subtly different from setting memory.swap.max to 0, as it still allows for pages to be stored in the zswap pool (which itself consumes swap space in its current form).

This is the second attempt (spiritual successor) of the following patch:

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

and should be applied on top of the zswap shrinker series:

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

---
 Documentation/admin-guide/cgroup-v2.rst | 11 +++++
 Documentation/admin-guide/mm/zswap.rst  |  6 +++
 include/linux/memcontrol.h              | 20 ++++++++++
 mm/memcontrol.c                         | 53 +++++++++++++++++++++++++
 mm/page_io.c                            |  6 +++
 mm/shmem.c                              |  8 +++-
 mm/zswap.c                              |  9 +++++
 7 files changed, 111 insertions(+), 2 deletions(-)
Documentation/admin-guide/cgroup-v2.rst +11 -0
index 606b2e0eac4b..34306d70b3f7 100644
@@ -1657,6 +1657,17 @@ PAGE_SIZE multiple when read back.
        higher than the limit for an extended period of time.  This
        reduces the impact on the workload and memory management.
 
+  memory.swap.bypass.enabled
+       A read-write single value file which exists on non-root
+       cgroups.  The default value is "0".
+
+       When this is set to 1, all swapping attempts are disabled.
+       Note that this is subtly different from setting memory.swap.max to
+       0, as it still allows for pages to be written to the zswap pool
+       (which also consumes swap space in its current form). However,
+       zswap store failure will not lead to swapping, and zswap writebacks
+       will be disabled altogether.
+
   memory.zswap.current
        A read-only single value file which exists on non-root
        cgroups.
Documentation/admin-guide/mm/zswap.rst +6 -0
index 522ae22ccb84..b7bf481a3e25 100644
@@ -153,6 +153,12 @@ attribute, e. g.::
 
 Setting this parameter to 100 will disable the hysteresis.
 
+Some users cannot tolerate the swapping that comes with zswap store failures
+and zswap writebacks. Swapping can be disabled entirely (without disabling
+zswap itself) on a cgroup-basis as follows:
+
+       echo 1 > /sys/fs/cgroup/<cgroup-name>/memory.swap.bypass.enabled
+
 When there is a sizable amount of cold memory residing in the zswap pool, it
 can be advantageous to proactively write these cold pages to swap and reclaim
 the memory for other use cases. By default, the zswap shrinker is disabled.
include/linux/memcontrol.h +20 -0
index c1846e57011b..e481c5c609f2 100644
@@ -221,6 +221,9 @@ struct mem_cgroup {
        unsigned long zswap_max;
 #endif
 
+       /* bypass swap (on zswap failure and writebacks) */
+       bool swap_bypass_enabled;
+
        unsigned long soft_limit;
 
        /* vmpressure notifications */
@@ -1157,6 +1160,13 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
                                                gfp_t gfp_mask,
                                                unsigned long *total_scanned);
 
+static inline bool mem_cgroup_swap_bypass_enabled(struct mem_cgroup *memcg)
+{
+       return memcg && READ_ONCE(memcg->swap_bypass_enabled);
+}
+
+bool mem_cgroup_swap_bypass_folio(struct folio *folio);
+
 #else /* CONFIG_MEMCG */
 
 #define MEM_CGROUP_ID_SHIFT    0
@@ -1615,6 +1625,16 @@ unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order,
 {
        return 0;
 }
+
+static inline bool mem_cgroup_swap_bypass_enabled(struct mem_cgroup *memcg)
+{
+       return false;
+}
+
+static inline bool mem_cgroup_swap_bypass_folio(struct folio *folio)
+{
+       return false;
+}
 #endif /* CONFIG_MEMCG */
 
 static inline void __inc_lruvec_kmem_state(void *p, enum node_stat_item idx)
mm/memcontrol.c +0 -0
index 568d9d037a59..f231cf2f745b 100644

mm/memcontrol.c bool mem_cgroup_swap_full(struct folio *folio) · line 7933

Quoted 6 lines
mm/memcontrol.c +3 -0
@@ -7928,6 +7928,28 @@ bool mem_cgroup_swap_full(struct folio *folio)
        return false;
 }
 
+bool mem_cgroup_swap_bypass_folio(struct folio *folio)
+{
+       struct obj_cgroup *objcg = get_obj_cgroup_from_folio(folio);

This is a swapbacked LRU folio, which means that it will have a memcg, not an objcg, in folio->memcg_data (unless this changed recently and I missed it).

This function will get the memcg from the folio, then get its objcg, then get its memcg again. I think this can be much simpler and no refs need to be acquired. I think the folio will always be isolated and locked here (but please do check). We can have a VM_BUG_ON() to ensure that. The comment above folio_memcg() states that isolated or locked folios should have stable memcgs.

Quoted 3 lines

We might as well inline a call to folio_memcg() and use the memcg version directly, or keep a separate helper to put the VM_BUG_ON()'s. I don't feel strongly.

Oh right I forgot that there's a direct link from the folio to its memcg. I'll fix this in the next version.

Quoted 42 lines
Quoted 42 lines
mm/memcontrol.c +34 -0
+       struct mem_cgroup *memcg;
+       bool ret;
+
+       if (!objcg)
+               return false;
+
+       if (mem_cgroup_disabled()) {
+               obj_cgroup_put(objcg);
+               return false;
+       }
+
+       memcg = get_mem_cgroup_from_objcg(objcg);
+       ret = mem_cgroup_swap_bypass_enabled(memcg);
+
+       mem_cgroup_put(memcg);
+       obj_cgroup_put(objcg);
+       return ret;
+}
+
 static int __init setup_swap_account(char *s)
 {
        pr_warn_once("The swapaccount= commandline option is deprecated. "
@@ -8013,6 +8035,31 @@ static int swap_events_show(struct seq_file *m, void *v)
        return 0;
 }
 
+static int swap_bypass_enabled_show(struct seq_file *m, void *v)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_seq(m);
+
+       seq_printf(m, "%d\n", READ_ONCE(memcg->swap_bypass_enabled));
+       return 0;
+}
+
+static ssize_t swap_bypass_enabled_write(struct kernfs_open_file *of,
+                               char *buf, size_t nbytes, loff_t off)
+{
+       struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of));
+       int swap_bypass_enabled;
+       ssize_t parse_ret = kstrtoint(strstrip(buf), 0, &swap_bypass_enabled);
+
Quoted 8 lines

mm/memcontrol.c static int swap_events_show(struct seq_file *m, void *v) · line 8057

Quoted 5 lines
mm/memcontrol.c +5 -0
+       if (parse_ret)
+               return parse_ret;
+
+       if (swap_bypass_enabled != 0 && swap_bypass_enabled != 1)
+               return -ERANGE;

I don't think ERANGE is appropriate here. EINVAL should be used here AFACIT (and looking at other write handlers for memcg interfaces).

Oh I think I just copied this from another cgroup file. The idea was that the range
of acceptable values is [0, 1]. But yeah, -EINVAL fits better here.

Will be fixed in v2.

Quoted 45 lines
Quoted 45 lines
mm/memcontrol.c +11 -0
+
+       WRITE_ONCE(memcg->swap_bypass_enabled, swap_bypass_enabled);
+       return nbytes;
+}
+
 static struct cftype swap_files[] = {
        {
                .name = "swap.current",
@@ -8042,6 +8089,12 @@ static struct cftype swap_files[] = {
                .file_offset = offsetof(struct mem_cgroup, swap_events_file),
                .seq_show = swap_events_show,
        },
+       {
+               .name = "swap.bypass.enabled",
+               .flags = CFTYPE_NOT_ON_ROOT,
+               .seq_show = swap_bypass_enabled_show,
+               .write = swap_bypass_enabled_write,
+       },
        { }     /* terminate */
 };
 
mm/page_io.c +6 -0
index cb559ae324c6..0c84e1592c39 100644
@@ -201,6 +201,12 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
                folio_end_writeback(folio);
                return 0;
        }
+
+       if (mem_cgroup_swap_bypass_folio(folio)) {
+               folio_mark_dirty(folio);
+               return AOP_WRITEPAGE_ACTIVATE;
+       }
+
        __swap_writepage(&folio->page, wbc);
        return 0;
 }
mm/shmem.c +0 -0
index cab053831fea..6ce1d4a7a48b 100644
@@ -1514,8 +1514,12 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)
 
Quoted 10 lines

mm/shmem.c static int shmem_writepage(struct page *page, struct writeback_control *wbc) · line 1519

Quoted 7 lines
mm/shmem.c +3 -2
                mutex_unlock(&shmem_swaplist_mutex);
                BUG_ON(folio_mapped(folio));
-               swap_writepage(&folio->page, wbc);
-               return 0;
+               /*
+                * Seeing AOP_WRITEPAGE_ACTIVATE here indicates swapping is disabled on
+                * zswap store failure. Note that in that case the folio is already

The interface semantics is "bypassing" swap, or not writing to disk for swap. Let's keep it consistent and not mention zswap here.

This is leftover from an older iteration of the patch I think. Will be fixed.

Quoted 43 lines
Quoted 43 lines
mm/shmem.c +3 -0
+                * re-marked dirty by swap_writepage()
+                */
+               return swap_writepage(&folio->page, wbc);
        }
 
        mutex_unlock(&shmem_swaplist_mutex);
mm/zswap.c +9 -1
index c40697f07ba3..f19e26d647a3 100644
@@ -535,6 +535,9 @@ static unsigned long zswap_shrinker_scan(struct shrinker *shrinker,
        struct zswap_pool *pool = shrinker->private_data;
        bool encountered_page_in_swapcache = false;
 
+       if (mem_cgroup_swap_bypass_enabled(sc->memcg))
+               return SHRINK_STOP;
+
        nr_protected =
                atomic_long_read(&lruvec->zswap_lruvec_state.nr_zswap_protected);
        lru_size = list_lru_shrink_count(&pool->list_lru, sc);
@@ -565,6 +568,9 @@ static unsigned long zswap_shrinker_count(struct shrinker *shrinker,
        struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid));
        unsigned long nr_backing, nr_stored, nr_freeable, nr_protected;
 
+       if (mem_cgroup_swap_bypass_enabled(memcg))
+               return 0;
+
 #ifdef CONFIG_MEMCG_KMEM
        cgroup_rstat_flush(memcg->css.cgroup);
        nr_backing = memcg_page_state(memcg, MEMCG_ZSWAP_B) >> PAGE_SHIFT;
@@ -890,6 +896,9 @@ static int shrink_memcg(struct mem_cgroup *memcg)
        struct zswap_pool *pool;
        int nid, shrunk = 0;
 
+       if (mem_cgroup_swap_bypass_enabled(memcg))
+               return -EINVAL;
+
        /*
         * Skip zombies because their LRUs are reparented and we would be
         * reclaiming from the parent instead of the dead memcg.
--

2.34.1

#3 lore raw
Quoted 22 lines · Nhat Pham
Quoted 13 lines · Yosry Ahmed
Quoted 10 lines · Nhat Pham

During our experiment with zswap, we sometimes observe swap IOs due to occasional zswap store failures and writebacks. These swapping IOs prevent many users who cannot tolerate swapping from adopting zswap to save memory and improve performance where possible.

This patch adds the option to bypass swap entirely: do not swap when an zswap store attempt fail, and do not write pages in the zswap pool back to swap. The feature is disabled by default (to preserve the existing behavior), and can be enabled on a cgroup-basis via a new cgroup file.

Quoted 6 lines

I think the word "bypass" here is fairly confusing because zswap is considered as swap for all purposes, so we are not really bypassing swap. I think it should be something like memory.swap.disk or memory.swap.writeback or whatever the correct terminology is for non-zswap swap (which I honestly don't know). Writing 0 to such an interface would give the desired effect.

Quoted 2 lines

I'm honestly not very good at naming things... especially a novel name that I cannot steal from a precedent...

Neither am I :)

Quoted 8 lines

I want something that means "going to zswap is acceptable, but please don't go to slow swap". Hence, swap bypassing (swap in my mental model is the slow thing that is under zswap).

(but I imagined memory.please.don't.go.to.slow.swap won't be very popular either...)

Quoted 9 lines

memory.swap.disk is... acceptable, I guess? It technically could still be wrong, as there's still zram, but we can sort of ignore that special case. I still prefer memory.swap.bypass, but I wouldn't be opposed to this renaming.

Anyone with better naming suggestions should absolutely speak up! Otherwise, I'll send a v2 renaming this to "memory.swap.disk" or "memory.swap.disk_used"

Hoping someone else chimes in for the naming here, this is not going to be changeable anytime soon.