Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

zswap: memcontrol: implement zswap writeback disabling

During our experiment with zswap, we sometimes observe swap IOs due to
occasional zswap store failures and writebacks-to-swap. 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 disable this behavior entirely: do not
writeback to backing swapping device when a zswap store attempt fail, and
do not write pages in the zswap pool back to the backing swap device (both
when the pool is full, and when the new zswap shrinker is called).

This new behavior can be opted-in/out on a per-cgroup basis via a new
cgroup file. By default, writebacks to swap device is enabled, which is
the previous behavior. Initially, writeback is enabled for the root
cgroup, and a newly created cgroup will inherit the current setting of its
parent.

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 patch should be applied on top of the zswap shrinker series:

https://lore.kernel.org/linux-mm/20231130194023.4102148-1-nphamcs@gmail.com/

as it also disables the zswap shrinker, a major source of zswap
writebacks.

For the most part, this feature is motivated by internal parties who
have already established their opinions regarding swapping - the
workloads that are highly sensitive to IO, and especially those who are
using servers with really slow disk performance (for instance, massive
but slow HDDs). For these folks, it's impossible to convince them to
even entertain zswap if swapping also comes as a packaged deal.
Writeback disabling is quite a useful feature in these situations - on
a mixed workloads deployment, they can disable writeback for the more
IO-sensitive workloads, and enable writeback for other background
workloads.

For instance, on a server with HDD, I allocate memories and populate
them with random values (so that zswap store will always fail), and
specify memory.high low enough to trigger reclaim. The time it takes
to allocate the memories and just read through it a couple of times
(doing silly things like computing the values' average etc.):

zswap.writeback disabled:
real 0m30.537s
user 0m23.687s
sys 0m6.637s
0 pages swapped in
0 pages swapped out

zswap.writeback enabled:
real 0m45.061s
user 0m24.310s
sys 0m8.892s
712686 pages swapped in
461093 pages swapped out

(the last two lines are from vmstat -s).

[nphamcs@gmail.com: add a comment about recurring zswap store failures leading to reclaim inefficiency]
Link: https://lkml.kernel.org/r/20231221005725.3446672-1-nphamcs@gmail.com
Link: https://lkml.kernel.org/r/20231207192406.3809579-1-nphamcs@gmail.com
Signed-off-by: Nhat Pham <nphamcs@gmail.com>
Suggested-by: Johannes Weiner <hannes@cmpxchg.org>
Reviewed-by: Yosry Ahmed <yosryahmed@google.com>
Acked-by: Chris Li <chrisl@kernel.org>
Cc: Dan Streetman <ddstreet@ieee.org>
Cc: David Heidelberg <david@ixit.cz>
Cc: Domenico Cerasuolo <cerasuolodomenico@gmail.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Mike Rapoport (IBM) <rppt@kernel.org>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Roman Gushchin <roman.gushchin@linux.dev>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>
Cc: Seth Jennings <sjenning@redhat.com>
Cc: Shakeel Butt <shakeelb@google.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: Vitaly Wool <vitaly.wool@konsulko.com>
Cc: Zefan Li <lizefan.x@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Nhat Pham and committed by
Andrew Morton
501a06fe 1ae41dff

+99 -4
+15
Documentation/admin-guide/cgroup-v2.rst
··· 1679 1679 limit, it will refuse to take any more stores before existing 1680 1680 entries fault back in or are written out to disk. 1681 1681 1682 + memory.zswap.writeback 1683 + A read-write single value file. The default value is "1". The 1684 + initial value of the root cgroup is 1, and when a new cgroup is 1685 + created, it inherits the current value of its parent. 1686 + 1687 + When this is set to 0, all swapping attempts to swapping devices 1688 + are disabled. This included both zswap writebacks, and swapping due 1689 + to zswap store failures. If the zswap store failures are recurring 1690 + (for e.g if the pages are incompressible), users can observe 1691 + reclaim inefficiency after disabling writeback (because the same 1692 + pages might be rejected again and again). 1693 + 1694 + Note that this is subtly different from setting memory.swap.max to 1695 + 0, as it still allows for pages to be written to the zswap pool. 1696 + 1682 1697 memory.pressure 1683 1698 A read-only nested-keyed file. 1684 1699
+10
Documentation/admin-guide/mm/zswap.rst
··· 153 153 154 154 Setting this parameter to 100 will disable the hysteresis. 155 155 156 + Some users cannot tolerate the swapping that comes with zswap store failures 157 + and zswap writebacks. Swapping can be disabled entirely (without disabling 158 + zswap itself) on a cgroup-basis as follows: 159 + 160 + echo 0 > /sys/fs/cgroup/<cgroup-name>/memory.zswap.writeback 161 + 162 + Note that if the store failures are recurring (for e.g if the pages are 163 + incompressible), users can observe reclaim inefficiency after disabling 164 + writeback (because the same pages might be rejected again and again). 165 + 156 166 When there is a sizable amount of cold memory residing in the zswap pool, it 157 167 can be advantageous to proactively write these cold pages to swap and reclaim 158 168 the memory for other use cases. By default, the zswap shrinker is disabled.
+12
include/linux/memcontrol.h
··· 219 219 220 220 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) 221 221 unsigned long zswap_max; 222 + 223 + /* 224 + * Prevent pages from this memcg from being written back from zswap to 225 + * swap, and from being swapped out on zswap store failures. 226 + */ 227 + bool zswap_writeback; 222 228 #endif 223 229 224 230 unsigned long soft_limit; ··· 1947 1941 bool obj_cgroup_may_zswap(struct obj_cgroup *objcg); 1948 1942 void obj_cgroup_charge_zswap(struct obj_cgroup *objcg, size_t size); 1949 1943 void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, size_t size); 1944 + bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg); 1950 1945 #else 1951 1946 static inline bool obj_cgroup_may_zswap(struct obj_cgroup *objcg) 1952 1947 { ··· 1960 1953 static inline void obj_cgroup_uncharge_zswap(struct obj_cgroup *objcg, 1961 1954 size_t size) 1962 1955 { 1956 + } 1957 + static inline bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg) 1958 + { 1959 + /* if zswap is disabled, do not block pages going to the swapping device */ 1960 + return true; 1963 1961 } 1964 1962 #endif 1965 1963
+7
include/linux/zswap.h
··· 35 35 void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg); 36 36 void zswap_lruvec_state_init(struct lruvec *lruvec); 37 37 void zswap_folio_swapin(struct folio *folio); 38 + bool is_zswap_enabled(void); 38 39 #else 39 40 40 41 struct zswap_lruvec_state {}; ··· 56 55 static inline void zswap_memcg_offline_cleanup(struct mem_cgroup *memcg) {} 57 56 static inline void zswap_lruvec_state_init(struct lruvec *lruvec) {} 58 57 static inline void zswap_folio_swapin(struct folio *folio) {} 58 + 59 + static inline bool is_zswap_enabled(void) 60 + { 61 + return false; 62 + } 63 + 59 64 #endif 60 65 61 66 #endif /* _LINUX_ZSWAP_H */
+38
mm/memcontrol.c
··· 5538 5538 WRITE_ONCE(memcg->soft_limit, PAGE_COUNTER_MAX); 5539 5539 #if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_ZSWAP) 5540 5540 memcg->zswap_max = PAGE_COUNTER_MAX; 5541 + WRITE_ONCE(memcg->zswap_writeback, 5542 + !parent || READ_ONCE(parent->zswap_writeback)); 5541 5543 #endif 5542 5544 page_counter_set_high(&memcg->swap, PAGE_COUNTER_MAX); 5543 5545 if (parent) { ··· 8168 8166 rcu_read_unlock(); 8169 8167 } 8170 8168 8169 + bool mem_cgroup_zswap_writeback_enabled(struct mem_cgroup *memcg) 8170 + { 8171 + /* if zswap is disabled, do not block pages going to the swapping device */ 8172 + return !is_zswap_enabled() || !memcg || READ_ONCE(memcg->zswap_writeback); 8173 + } 8174 + 8171 8175 static u64 zswap_current_read(struct cgroup_subsys_state *css, 8172 8176 struct cftype *cft) 8173 8177 { ··· 8206 8198 return nbytes; 8207 8199 } 8208 8200 8201 + static int zswap_writeback_show(struct seq_file *m, void *v) 8202 + { 8203 + struct mem_cgroup *memcg = mem_cgroup_from_seq(m); 8204 + 8205 + seq_printf(m, "%d\n", READ_ONCE(memcg->zswap_writeback)); 8206 + return 0; 8207 + } 8208 + 8209 + static ssize_t zswap_writeback_write(struct kernfs_open_file *of, 8210 + char *buf, size_t nbytes, loff_t off) 8211 + { 8212 + struct mem_cgroup *memcg = mem_cgroup_from_css(of_css(of)); 8213 + int zswap_writeback; 8214 + ssize_t parse_ret = kstrtoint(strstrip(buf), 0, &zswap_writeback); 8215 + 8216 + if (parse_ret) 8217 + return parse_ret; 8218 + 8219 + if (zswap_writeback != 0 && zswap_writeback != 1) 8220 + return -EINVAL; 8221 + 8222 + WRITE_ONCE(memcg->zswap_writeback, zswap_writeback); 8223 + return nbytes; 8224 + } 8225 + 8209 8226 static struct cftype zswap_files[] = { 8210 8227 { 8211 8228 .name = "zswap.current", ··· 8242 8209 .flags = CFTYPE_NOT_ON_ROOT, 8243 8210 .seq_show = zswap_max_show, 8244 8211 .write = zswap_max_write, 8212 + }, 8213 + { 8214 + .name = "zswap.writeback", 8215 + .seq_show = zswap_writeback_show, 8216 + .write = zswap_writeback_write, 8245 8217 }, 8246 8218 { } /* terminate */ 8247 8219 };
+5
mm/page_io.c
··· 201 201 folio_end_writeback(folio); 202 202 return 0; 203 203 } 204 + if (!mem_cgroup_zswap_writeback_enabled(folio_memcg(folio))) { 205 + folio_mark_dirty(folio); 206 + return AOP_WRITEPAGE_ACTIVATE; 207 + } 208 + 204 209 __swap_writepage(folio, wbc); 205 210 return 0; 206 211 }
+1 -2
mm/shmem.c
··· 1514 1514 1515 1515 mutex_unlock(&shmem_swaplist_mutex); 1516 1516 BUG_ON(folio_mapped(folio)); 1517 - swap_writepage(&folio->page, wbc); 1518 - return 0; 1517 + return swap_writepage(&folio->page, wbc); 1519 1518 } 1520 1519 1521 1520 mutex_unlock(&shmem_swaplist_mutex);
+11 -2
mm/zswap.c
··· 153 153 CONFIG_ZSWAP_SHRINKER_DEFAULT_ON); 154 154 module_param_named(shrinker_enabled, zswap_shrinker_enabled, bool, 0644); 155 155 156 + bool is_zswap_enabled(void) 157 + { 158 + return zswap_enabled; 159 + } 160 + 156 161 /********************************* 157 162 * data structures 158 163 **********************************/ ··· 601 596 struct zswap_pool *pool = shrinker->private_data; 602 597 bool encountered_page_in_swapcache = false; 603 598 604 - if (!zswap_shrinker_enabled) { 599 + if (!zswap_shrinker_enabled || 600 + !mem_cgroup_zswap_writeback_enabled(sc->memcg)) { 605 601 sc->nr_scanned = 0; 606 602 return SHRINK_STOP; 607 603 } ··· 643 637 struct lruvec *lruvec = mem_cgroup_lruvec(memcg, NODE_DATA(sc->nid)); 644 638 unsigned long nr_backing, nr_stored, nr_freeable, nr_protected; 645 639 646 - if (!zswap_shrinker_enabled) 640 + if (!zswap_shrinker_enabled || !mem_cgroup_zswap_writeback_enabled(memcg)) 647 641 return 0; 648 642 649 643 #ifdef CONFIG_MEMCG_KMEM ··· 928 922 { 929 923 struct zswap_pool *pool; 930 924 int nid, shrunk = 0; 925 + 926 + if (!mem_cgroup_zswap_writeback_enabled(memcg)) 927 + return -EINVAL; 931 928 932 929 /* 933 930 * Skip zombies because their LRUs are reparented and we would be