workload-specific and memory pressure-driven zswap writeback
Subsystems (18)
Nhat Pham
maintainerv7 cover workload-specific and memory pressure-driven zswap writeback
Changelog:
v7:
* Added the mem_cgroup_iter_online() function to the API for the new behavior (suggested by Andrew Morton) (patch 2)
* Fixed a missing list_lru_del -> list_lru_del_obj (patch 1)
v6:
* Rebase on top of latest mm-unstable.
* Fix/improve the in-code documentation of the new list_lru manipulation functions (patch 1)
v5:
* Replace reference getting with an rcu_read_lock() section for zswap lru modifications (suggested by Yosry)
* Add a new prep patch that allows mem_cgroup_iter() to return online cgroup.
* Add a callback that updates pool->next_shrink when the cgroup is offlined (suggested by Yosry Ahmed, Johannes Weiner)
v4:
* Rename list_lru_add to list_lru_add_obj and __list_lru_add to list_lru_add (patch 1) (suggested by Johannes Weiner and Yosry Ahmed)
* Some cleanups on the memcg aware LRU patch (patch 2) (suggested by Yosry Ahmed)
* Use event interface for the new per-cgroup writeback counters. (patch 3) (suggested by Yosry Ahmed)
* Abstract zswap's lruvec states and handling into zswap_lruvec_state (patch 5) (suggested by Yosry Ahmed)
v3:
* Add a patch to export per-cgroup zswap writeback counters
* Add a patch to update zswap's kselftest
* Separate the new list_lru functions into its own prep patch
* Do not start from the top of the hierarchy when encounter a memcg that is not online for the global limit zswap writeback (patch 2) (suggested by Yosry Ahmed)
* Do not remove the swap entry from list_lru in __read_swapcache_async() (patch 2) (suggested by Yosry Ahmed)
* Removed a redundant zswap pool getting (patch 2) (reported by Ryan Roberts)
* Use atomic for the nr_zswap_protected (instead of lruvec's lock) (patch 5) (suggested by Yosry Ahmed)
* Remove the per-cgroup zswap shrinker knob (patch 5) (suggested by Yosry Ahmed)
v2:
* Fix loongarch compiler errors
* Use pool stats instead of memcg stats when !CONFIG_MEMCG_KEM
There are currently several issues with zswap writeback:
1. There is only a single global LRU for zswap, making it impossible to perform worload-specific shrinking - an memcg under memory pressure cannot determine which pages in the pool it owns, and often ends up writing pages from other memcgs. This issue has been previously observed in practice and mitigated by simply disabling memcg-initiated shrinking:
https://lore.kernel.org/all/[email protected]/T/#u
But this solution leaves a lot to be desired, as we still do not have an avenue for an memcg to free up its own memory locked up in the zswap pool.
2. We only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages).
This patch series solves these issues by separating the global zswap LRU into per-memcg and per-NUMA LRUs, and performs workload-specific (i.e memcg- and NUMA-aware) zswap writeback under memory pressure. The new shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis.
As a proof of concept, we ran the following synthetic benchmark:
build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds.
Domenico Cerasuolo (3):
zswap: make shrinking memcg-aware
mm: memcg: add per-memcg zswap writeback stat
selftests: cgroup: update per-memcg zswap writeback selftest
Nhat Pham (3):
list_lru: allows explicit memcg and NUMA node selection
memcontrol: add a new function to traverse online-only memcg hierarchy
zswap: shrinks zswap pool based on memory pressure
Documentation/admin-guide/mm/zswap.rst | 7 + drivers/android/binder_alloc.c | 7 +- fs/dcache.c | 8 +- fs/gfs2/quota.c | 6 +- fs/inode.c | 4 +- fs/nfs/nfs42xattr.c | 8 +- fs/nfsd/filecache.c | 4 +- fs/xfs/xfs_buf.c | 6 +- fs/xfs/xfs_dquot.c | 2 +- fs/xfs/xfs_qm.c | 2 +- include/linux/list_lru.h | 54 ++- include/linux/memcontrol.h | 18 + include/linux/mmzone.h | 2 + include/linux/vm_event_item.h | 1 + include/linux/zswap.h | 27 +- mm/list_lru.c | 48 ++- mm/memcontrol.c | 32 +- mm/mmzone.c | 1 + mm/swap.h | 3 +- mm/swap_state.c | 26 +- mm/vmstat.c | 1 + mm/workingset.c | 4 +- mm/zswap.c | 426 +++++++++++++++++--- tools/testing/selftests/cgroup/test_zswap.c | 74 ++-- 24 files changed, 641 insertions(+), 130 deletions(-)
base-commit: 5cdba94229e58a39ca389ad99763af29e6b0c5a5
Signature · 1 line
2.34.1
Nhat Pham
maintainerPATCH v7 1/6 list_lru: allows explicit memcg and NUMA node selection
The interface of list_lru is based on the assumption that the list node and the data it represents belong to the same allocated on the correct node/memcg. While this assumption is valid for existing slab objects LRU such as dentries and inodes, it is undocumented, and rather inflexible for certain potential list_lru users (such as the upcoming zswap shrinker and the THP shrinker). It has caused us a lot of issues during our development.
This patch changes list_lru interface so that the caller must explicitly specify numa node and memcg when adding and removing objects. The old list_lru_add() and list_lru_del() are renamed to list_lru_add_obj() and list_lru_del_obj(), respectively.
It also extends the list_lru API with a new function, list_lru_putback, which undoes a previous list_lru_isolate call. Unlike list_lru_add, it does not increment the LRU node count (as list_lru_isolate does not decrement the node count). list_lru_putback also allows for explicit memcg and NUMA node selection.
--- drivers/android/binder_alloc.c | 7 ++--- fs/dcache.c | 8 +++-- fs/gfs2/quota.c | 6 ++-- fs/inode.c | 4 +-- fs/nfs/nfs42xattr.c | 8 ++--- fs/nfsd/filecache.c | 4 +-- fs/xfs/xfs_buf.c | 6 ++-- fs/xfs/xfs_dquot.c | 2 +- fs/xfs/xfs_qm.c | 2 +- include/linux/list_lru.h | 54 ++++++++++++++++++++++++++++++++-- mm/list_lru.c | 48 +++++++++++++++++++++++++----- mm/workingset.c | 4 +-- 12 files changed, 117 insertions(+), 36 deletions(-)
drivers/android/binder_alloc.c +3 -4
@@ -234,7 +234,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,if (page->page_ptr) {trace_binder_alloc_lru_start (alloc, index); - on_lru =list_lru_del (&binder_alloc_lru, &page->lru); + on_lru =list_lru_del_obj (&binder_alloc_lru, &page->lru);WARN_ON (!on_lru);trace_binder_alloc_lru_end (alloc, index);
@@ -285,7 +285,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,trace_binder_free_lru_start (alloc, index); - ret =list_lru_add (&binder_alloc_lru, &page->lru); + ret =list_lru_add_obj (&binder_alloc_lru, &page->lru);WARN_ON (!ret);trace_binder_free_lru_end (alloc, index);
@@ -848,7 +848,7 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc)if (!alloc->pages[i].page_ptr)continue ; - on_lru =list_lru_del (&binder_alloc_lru, + on_lru =list_lru_del_obj (&binder_alloc_lru, &alloc->pages[i].lru); page_addr = alloc->buffer + i *PAGE_SIZE ; binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
@@ -1287,4 +1287,3 @@ int binder_alloc_copy_from_buffer(struct binder_alloc *alloc,return binder_alloc_do_buffer_copy (alloc, false, buffer, buffer_offset, dest, bytes); } -
fs/dcache.c +5 -3
@@ -428,7 +428,8 @@ static void d_lru_add(struct dentry *dentry)this_cpu_inc (nr_dentry_unused);if (d_is_negative (dentry))this_cpu_inc (nr_dentry_negative); -WARN_ON_ONCE (!list_lru_add (&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); +WARN_ON_ONCE (!list_lru_add_obj ( + &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); }static void d_lru_del (struct dentry *dentry)
@@ -438,7 +439,8 @@ static void d_lru_del(struct dentry *dentry)this_cpu_dec (nr_dentry_unused);if (d_is_negative (dentry))this_cpu_dec (nr_dentry_negative); -WARN_ON_ONCE (!list_lru_del (&dentry->d_sb->s_dentry_lru, &dentry->d_lru)); +WARN_ON_ONCE (!list_lru_del_obj ( + &dentry->d_sb->s_dentry_lru, &dentry->d_lru)); }static void d_shrink_del (struct dentry *dentry)
@@ -1240,7 +1242,7 @@ static enum lru_status dentry_lru_isolate(struct list_head *item,* * This is guaranteed by the fact that all LRU management * functions are intermediated by the LRU API calls like -* list_lru_add and list_lru_del. List movement in this file +* list_lru_add_obj and list_lru_del_obj. List movement in this file * only ever occur through this functions or through callbacks * like this one, that are called from the LRU API. *
fs/gfs2/quota.c +3 -3
@@ -271,7 +271,7 @@ static struct gfs2_quota_data *gfs2_qd_search_bucket(unsigned int hash,if (qd->qd_sbd != sdp)continue ;if (lockref_get_not_dead (&qd->qd_lockref)) { -list_lru_del (&gfs2_qd_lru, &qd->qd_lru); +list_lru_del_obj (&gfs2_qd_lru, &qd->qd_lru);return qd; } }
@@ -344,7 +344,7 @@ static void qd_put(struct gfs2_quota_data *qd) } qd->qd_lockref.count =0 ; -list_lru_add (&gfs2_qd_lru, &qd->qd_lru); +list_lru_add_obj (&gfs2_qd_lru, &qd->qd_lru);spin_unlock (&qd->qd_lockref.lock); }
@@ -1517,7 +1517,7 @@ void gfs2_quota_cleanup(struct gfs2_sbd *sdp)lockref_mark_dead (&qd->qd_lockref);spin_unlock (&qd->qd_lockref.lock); -list_lru_del (&gfs2_qd_lru, &qd->qd_lru); +list_lru_del_obj (&gfs2_qd_lru, &qd->qd_lru);list_add (&qd->qd_lru, &dispose); }spin_unlock (&qd_lock);
fs/inode.c +2 -2
@@ -464,7 +464,7 @@ static void __inode_add_lru(struct inode *inode, bool rotate)if (!mapping_shrinkable (&inode->i_data))return ; -if (list_lru_add (&inode->i_sb->s_inode_lru, &inode->i_lru)) +if (list_lru_add_obj (&inode->i_sb->s_inode_lru, &inode->i_lru))this_cpu_inc (nr_unused);else if (rotate) inode->i_state |=I_REFERENCED ;
@@ -482,7 +482,7 @@ void inode_add_lru(struct inode *inode)static void inode_lru_list_del (struct inode *inode) { -if (list_lru_del (&inode->i_sb->s_inode_lru, &inode->i_lru)) +if (list_lru_del_obj (&inode->i_sb->s_inode_lru, &inode->i_lru))this_cpu_dec (nr_unused); }
fs/nfs/nfs42xattr.c +4 -4
@@ -132,7 +132,7 @@ nfs4_xattr_entry_lru_add(struct nfs4_xattr_entry *entry) lru = (entry->flags &NFS4_XATTR_ENTRY_EXTVAL ) ? &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru; -return list_lru_add (lru, &entry->lru); +return list_lru_add_obj (lru, &entry->lru); } static bool
@@ -143,7 +143,7 @@ nfs4_xattr_entry_lru_del(struct nfs4_xattr_entry *entry) lru = (entry->flags &NFS4_XATTR_ENTRY_EXTVAL ) ? &nfs4_xattr_large_entry_lru : &nfs4_xattr_entry_lru; -return list_lru_del (lru, &entry->lru); +return list_lru_del_obj (lru, &entry->lru); }/*
@@ -349,7 +349,7 @@ nfs4_xattr_cache_unlink(struct inode *inode) oldcache = nfsi->xattr_cache;if (oldcache !=NULL ) { -list_lru_del (&nfs4_xattr_cache_lru, &oldcache->lru); +list_lru_del_obj (&nfs4_xattr_cache_lru, &oldcache->lru); oldcache->inode =NULL ; } nfsi->xattr_cache =NULL ;
@@ -474,7 +474,7 @@ nfs4_xattr_get_cache(struct inode *inode, int add)kref_get (&cache->ref); nfsi->xattr_cache = cache; cache->inode = inode; -list_lru_add (&nfs4_xattr_cache_lru, &cache->lru); +list_lru_add_obj (&nfs4_xattr_cache_lru, &cache->lru); }spin_unlock (&inode->i_lock);
fs/nfsd/filecache.c +2 -2
@@ -322,7 +322,7 @@ nfsd_file_check_writeback(struct nfsd_file *nf)static bool nfsd_file_lru_add (struct nfsd_file *nf) {set_bit (NFSD_FILE_REFERENCED , &nf->nf_flags); -if (list_lru_add (&nfsd_file_lru, &nf->nf_lru)) { +if (list_lru_add_obj (&nfsd_file_lru, &nf->nf_lru)) {trace_nfsd_file_lru_add (nf);return true; }
@@ -331,7 +331,7 @@ static bool nfsd_file_lru_add(struct nfsd_file *nf)static bool nfsd_file_lru_remove (struct nfsd_file *nf) { -if (list_lru_del (&nfsd_file_lru, &nf->nf_lru)) { +if (list_lru_del_obj (&nfsd_file_lru, &nf->nf_lru)) {trace_nfsd_file_lru_del (nf);return true; }
fs/xfs/xfs_buf.c +3 -3
@@ -169,7 +169,7 @@ xfs_buf_stale(atomic_set (&bp->b_lru_ref,0 );if (!(bp->b_state &XFS_BSTATE_DISPOSE ) && - (list_lru_del (&bp->b_target->bt_lru, &bp->b_lru))) + (list_lru_del_obj (&bp->b_target->bt_lru, &bp->b_lru)))atomic_dec (&bp->b_hold);ASSERT (atomic_read (&bp->b_hold) >=1 );
@@ -1047,7 +1047,7 @@ xfs_buf_rele(* buffer for the LRU and clear the (now stale) dispose list * state flag */ -if (list_lru_add (&bp->b_target->bt_lru, &bp->b_lru)) { +if (list_lru_add_obj (&bp->b_target->bt_lru, &bp->b_lru)) { bp->b_state &= ~XFS_BSTATE_DISPOSE ;atomic_inc (&bp->b_hold); }
@@ -1060,7 +1060,7 @@ xfs_buf_rele(* was on was the disposal list */ if (!(bp->b_state &XFS_BSTATE_DISPOSE )) { -list_lru_del (&bp->b_target->bt_lru, &bp->b_lru); +list_lru_del_obj (&bp->b_target->bt_lru, &bp->b_lru); }else {ASSERT (list_empty (&bp->b_lru)); }
fs/xfs/xfs_dquot.c +1 -1
@@ -1064,7 +1064,7 @@ xfs_qm_dqput(struct xfs_quotainfo *qi = dqp->q_mount->m_quotainfo;trace_xfs_dqput_free (dqp); -if (list_lru_add (&qi->qi_lru, &dqp->q_lru)) +if (list_lru_add_obj (&qi->qi_lru, &dqp->q_lru))XFS_STATS_INC (dqp->q_mount, xs_qm_dquot_unused); }xfs_dqunlock (dqp);
fs/xfs/xfs_qm.c +1 -1
@@ -171,7 +171,7 @@ xfs_qm_dqpurge(* hits zero, so it really should be on the freelist here. */ ASSERT (!list_empty (&dqp->q_lru)); -list_lru_del (&qi->qi_lru, &dqp->q_lru); +list_lru_del_obj (&qi->qi_lru, &dqp->q_lru);XFS_STATS_DEC (dqp->q_mount, xs_qm_dquot_unused);xfs_qm_dqdestroy (dqp);
include/linux/list_lru.h +51 -3
82 lines not on this page · raw message
mm/list_lru.c +40 -8
88 lines not on this page · raw message
mm/workingset.c +2 -2
@@ -615,12 +615,12 @@ void workingset_update_node(struct xa_node *node)if (node->count && node->count == node->nr_values) {if (list_empty (&node->private_list)) { -list_lru_add (&shadow_nodes, &node->private_list); +list_lru_add_obj (&shadow_nodes, &node->private_list);__inc_lruvec_kmem_state (node,WORKINGSET_NODES ); } }else {if (!list_empty (&node->private_list)) { -list_lru_del (&shadow_nodes, &node->private_list); +list_lru_del_obj (&shadow_nodes, &node->private_list);__dec_lruvec_kmem_state (node,WORKINGSET_NODES ); } }
Signature · 1 line
2.34.1
Re: [PATCH v7 1/6] list_lru: allows explicit memcg and NUMA node selection
Quoted 13 lines · Nhat Pham
The interface of
list_lruis based on the assumption that the list node and the data it represents belong to the same allocated on the correct node/memcg. While this assumption is valid for existing slab objects LRU such as dentries and inodes, it is undocumented, and rather inflexible for certain potentiallist_lruusers (such as the upcoming zswap shrinker and the THP shrinker). It has caused us a lot of issues during our development.This patch changes
list_lruinterface so that the caller must explicitly specify numa node and memcg when adding and removing objects. The oldlist_lru_add()andlist_lru_del()are renamed tolist_lru_add_obj()andlist_lru_del_obj(), respectively.
Quoted 8 lines
It also extends the
list_lruAPI with a new function,list_lru_putback, which undoes a previouslist_lru_isolatecall. Unlikelist_lru_add, it does not increment the LRU node count (aslist_lru_isolatedoes not decrement the node count).list_lru_putbackalso allows for explicit memcg and NUMA node selection.Suggested-by Johannes Weiner <[email protected]>Signed-off-by Nhat Pham <[email protected]>
Nhat Pham
maintainerPATCH v7 2/6 memcontrol: add a new function to traverse online-only memcg hierarchy
In reply to Nhat PhamThe new zswap writeback scheme requires an online-only memcg hierarchy traversal. Add this functionality via the new mem_cgroup_iter_online() function - the old mem_cgroup_iter() is a special case of this new function.
--- include/linux/memcontrol.h | 13 +++++++++++++ mm/memcontrol.c | 29 +++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-)
include/linux/memcontrol.h +13 -0
@@ -833,6 +833,10 @@ static inline void mem_cgroup_put(struct mem_cgroup *memcg)struct mem_cgroup *mem_cgroup_iter (struct mem_cgroup *,struct mem_cgroup *,struct mem_cgroup_reclaim_cookie *); +struct mem_cgroup *mem_cgroup_iter_online (struct mem_cgroup *root, +struct mem_cgroup *prev, +struct mem_cgroup_reclaim_cookie *reclaim, +bool online);void mem_cgroup_iter_break (struct mem_cgroup *,struct mem_cgroup *);void mem_cgroup_scan_tasks (struct mem_cgroup *memcg,int (*)(struct task_struct *,void *),void *arg);
@@ -1386,6 +1390,15 @@ mem_cgroup_iter(struct mem_cgroup *root,return NULL ; } +static inline struct mem_cgroup * +mem_cgroup_iter_online (struct mem_cgroup *root, +struct mem_cgroup *prev, +struct mem_cgroup_reclaim_cookie *reclaim, +bool online) +{ +return NULL ; +} +static inline void mem_cgroup_iter_break (struct mem_cgroup *root,struct mem_cgroup *prev) {
mm/memcontrol.c +25 -4
@@ -1111,10 +1111,11 @@ struct mem_cgroup *get_mem_cgroup_from_current(void) }/** -* mem_cgroup_iter - iterate over memory cgroup hierarchy +* mem_cgroup_iter_online - iterate over memory cgroup hierarchy * @root: hierarchy root * @prev: previously returned memcg, NULL on first invocation * @reclaim: cookie for shared reclaim walks, NULL for full walks +* @online: whether to skip offline memcgs * * Returns references to children of the hierarchy below @root, or * @root itself, or %NULL after a full round-trip.
@@ -1123,13 +1124,16 @@ struct mem_cgroup *get_mem_cgroup_from_current(void)* invocations for reference counting, or use mem_cgroup_iter_break() * to cancel a hierarchy walk before the round-trip is complete. * +* Caller can skip offline memcgs by passing true for @online. +* * Reclaimers can specify a node in @reclaim to divide up the memcgs * in the hierarchy among all concurrent reclaimers operating on the * same node. */ -struct mem_cgroup *mem_cgroup_iter (struct mem_cgroup *root, +struct mem_cgroup *mem_cgroup_iter_online (struct mem_cgroup *root,struct mem_cgroup *prev, -struct mem_cgroup_reclaim_cookie *reclaim) +struct mem_cgroup_reclaim_cookie *reclaim, +bool online) {struct mem_cgroup_reclaim_iter *iter;struct cgroup_subsys_state *css =NULL ;
@@ -1199,7 +1203,8 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,* is provided by the caller, so we know it's alive * and kicking, and don't take an extra reference. */ -if (css == &root->css ||css_tryget (css)) { +if (css == &root->css || (!online &&css_tryget (css)) || +css_tryget_online (css)) { memcg =mem_cgroup_from_css (css);break ; }
@@ -1228,6 +1233,22 @@ struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,return memcg; } +/** +* mem_cgroup_iter - iterate over memory cgroup hierarchy +* @root: hierarchy root +* @prev: previously returned memcg, NULL on first invocation +* @reclaim: cookie for shared reclaim walks, NULL for full walks +* +* Perform an iteration on the memory cgroup hierarchy without skipping +* offline memcgs. +*/ +struct mem_cgroup *mem_cgroup_iter (struct mem_cgroup *root, +struct mem_cgroup *prev, +struct mem_cgroup_reclaim_cookie *reclaim) +{ +return mem_cgroup_iter_online (root, prev, reclaim, false); +} +/** * mem_cgroup_iter_break - abort a hierarchy walk prematurely * @root: hierarchy root
Signature · 1 line
2.34.1
Re: [PATCH v7 2/6] memcontrol: add a new function to traverse online-only memcg hierarchy
Quoted 7 lines · Nhat Pham
The new zswap writeback scheme requires an online-only memcg hierarchy traversal. Add this functionality via the new
mem_cgroup_iter_online()function - the oldmem_cgroup_iter()is a special case of this new function.Suggested-by Andrew Morton <[email protected]>Signed-off-by Nhat Pham <[email protected]>
Johannes Weiner
maintainerRe: [PATCH v7 2/6] memcontrol: add a new function to traverse online-only memcg hierarchy
Quoted 10 lines · Johannes Weiner
Quoted 7 lines · Nhat Pham
The new zswap writeback scheme requires an online-only memcg hierarchy traversal. Add this functionality via the new
mem_cgroup_iter_online()function - the oldmem_cgroup_iter()is a special case of this new function.Suggested-by Andrew Morton <[email protected]>Signed-off-by Nhat Pham <[email protected]>Acked-by Johannes Weiner <[email protected]>
I saw Michal's reply on the other thread only after I sent this.
I agree with him it would be better to just check mem_cgroup_online() in the shinker callsite and leave mem_cgroup_iter() as-is. If it's offline, just continue to the next mem_cgroup_iter() invocation.
Nhat Pham
maintainerPATCH v7 3/6 zswap: make shrinking memcg-aware
In reply to Nhat PhamFrom: Domenico Cerasuolo <[email protected]>
Currently, we only have a single global LRU for zswap. This makes it impossible to perform worload-specific shrinking - an memcg cannot determine which pages in the pool it owns, and often ends up writing pages from other memcgs. This issue has been previously observed in practice and mitigated by simply disabling memcg-initiated shrinking:
https://lore.kernel.org/all/[email protected]/T/#u
This patch fully resolves the issue by replacing the global zswap LRU with memcg- and NUMA-specific LRUs, and modify the reclaim logic:
a) When a store attempt hits an memcg limit, it now triggers a synchronous reclaim attempt that, if successful, allows the new hotter page to be accepted by zswap.
b) If the store attempt instead hits the global zswap limit, it will trigger an asynchronous reclaim attempt, in which an memcg is selected for reclaim in a round-robin-like fashion.
--- include/linux/memcontrol.h | 5 + include/linux/zswap.h | 2 + mm/memcontrol.c | 2 + mm/swap.h | 3 +- mm/swap_state.c | 24 +++- mm/zswap.c | 248 +++++++++++++++++++++++++++++-------- 6 files changed, 223 insertions(+), 61 deletions(-)
include/linux/memcontrol.h +5 -0
@@ -1191,6 +1191,11 @@ static inline struct mem_cgroup *page_memcg_check(struct page *page)return NULL ; } +static inline struct mem_cgroup *get_mem_cgroup_from_objcg (struct obj_cgroup *objcg) +{ +return NULL ; +} +static inline bool folio_memcg_kmem (struct folio *folio) {return false;
include/linux/zswap.h +2 -0
@@ -15,6 +15,7 @@ bool zswap_load(struct folio *folio);void zswap_invalidate (int type,pgoff_t offset);void zswap_swapon (int type);void zswap_swapoff (int type); +void zswap_memcg_offline_cleanup (struct mem_cgroup *memcg);#else
@@ -31,6 +32,7 @@ static inline bool zswap_load(struct folio *folio)static inline void zswap_invalidate (int type,pgoff_t offset) {}static inline void zswap_swapon (int type) {}static inline void zswap_swapoff (int type) {} +static inline void zswap_memcg_offline_cleanup (struct mem_cgroup *memcg) {}#endif
mm/memcontrol.c +2 -0
@@ -5635,6 +5635,8 @@ static void mem_cgroup_css_offline(struct cgroup_subsys_state *css)page_counter_set_min (&memcg->memory,0 );page_counter_set_low (&memcg->memory,0 ); +zswap_memcg_offline_cleanup (memcg); +memcg_offline_kmem (memcg);reparent_shrinker_deferred (memcg);wb_memcg_offline (memcg);
mm/swap.h +2 -1
@@ -51,7 +51,8 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct swap_iocb **plug);struct page *__read_swap_cache_async (swp_entry_t entry,gfp_t gfp_mask,struct mempolicy *mpol,pgoff_t ilx, -bool *new_page_allocated); +bool *new_page_allocated, +bool skip_if_exists);struct page *swap_cluster_readahead (swp_entry_t entry,gfp_t flag,struct mempolicy *mpol,pgoff_t ilx); struct page *swapin_readahead(swp_entry_t entry, gfp_t flag,
mm/swap_state.c +18 -6
@@ -412,7 +412,8 @@ struct folio *filemap_get_incore_folio(struct address_space *mapping,struct page *__read_swap_cache_async (swp_entry_t entry,gfp_t gfp_mask,struct mempolicy *mpol,pgoff_t ilx, -bool *new_page_allocated) +bool *new_page_allocated, +bool skip_if_exists) {struct swap_info_struct *si;struct folio *folio;
@@ -470,6 +471,17 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,if (err != -EEXIST ) goto fail_put_swap; +/* +* Protect against a recursive call to __read_swap_cache_async() +* on the same entry waiting forever here because SWAP_HAS_CACHE +* is set but the folio is not the swap cache yet. This can +* happen today if mem_cgroup_swapin_charge_folio() below +* triggers reclaim through zswap, which may call +* __read_swap_cache_async() in the writeback path. +*/ +if (skip_if_exists) + goto fail_put_swap; +/* * We might race against __delete_from_swap_cache(), and * stumble across a swap_map entry whose SWAP_HAS_CACHE
@@ -537,7 +549,7 @@ struct page *read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, mpol =get_vma_policy (vma, addr,0 , &ilx); page =__read_swap_cache_async (entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false);mpol_cond_put (mpol);if (page_allocated)
@@ -654,7 +666,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,/* Ok, do the async read-ahead now */ page =__read_swap_cache_async (swp_entry (swp_type (entry), offset), - gfp_mask, mpol, ilx, &page_allocated); + gfp_mask, mpol, ilx, &page_allocated, false);if (!page)continue ;if (page_allocated) {
@@ -672,7 +684,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, skip:/* The page was likely read above, so no need for plugging here */ page =__read_swap_cache_async (entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false);if (unlikely (page_allocated))swap_readpage (page, false,NULL );return page;
@@ -827,7 +839,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask,pte_unmap (pte); pte =NULL ; page =__read_swap_cache_async (entry, gfp_mask, mpol, ilx, - &page_allocated); + &page_allocated, false);if (!page)continue ;if (page_allocated) {
@@ -847,7 +859,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, skip:/* The page was likely read above, so no need for plugging here */ page =__read_swap_cache_async (targ_entry, gfp_mask, mpol, targ_ilx, - &page_allocated); + &page_allocated, false);if (unlikely (page_allocated))swap_readpage (page, false,NULL );return page;
mm/zswap.c +194 -54
400 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v7 3/6] zswap: make shrinking memcg-aware
Quoted 22 lines · Nhat Pham
static void
shrink_worker(struct work_struct *w)
{mm/zswap.c +9
-5struct zswap_pool *pool =container_of (w,typeof (*pool), shrink_work); +struct mem_cgroup *memcg;int ret, failures =0 ; +/* global reclaim will select cgroup in a round-robin fashion. */ do { - ret = zswap_reclaim_entry(pool); - if (ret) { - zswap_reject_reclaim_fail++; - if (ret != -EAGAIN) - break; + spin_lock(&zswap_pools_lock); + memcg = pool->next_shrink = + mem_cgroup_iter_online(NULL, pool->next_shrink, NULL, true); + + /* full round trip */ + if (!memcg) { + spin_unlock(&zswap_pools_lock); if (++failures == MAX_RECLAIM_RETRIES)
mm/zswap.c static int zswap_reclaim_entry(struct zswap_pool *pool) · line 825
Quoted 10 lines
mm/zswap.c +8
-0break ; + + goto resched; } + +/* +* Acquire an extra reference to the iterated memcg in case the +* original reference is dropped by the zswap offlining callback. +*/ +css_get (&memcg->css);
struct mem_cgroup isn't defined when !CONFIG_MEMCG. This needs a mem_cgroup_get() wrapper and a dummy function for no-memcg builds.
With that fixed, though, everything else looks good to me:
Nhat Pham
maintainerRe: [PATCH v7 3/6] zswap: make shrinking memcg-aware
Quoted 29 lines · Johannes Weiner
Quoted 27 lines · Nhat Pham
static void
shrink_worker(struct work_struct *w)
{
structzswap_pool*pool = container_of(w,typeof(*pool),
shrink_work);mm/zswap.c +12
-5+struct mem_cgroup *memcg;int ret, failures =0 ; +/* global reclaim will select cgroup in a round-robin fashion. */ do { - ret = zswap_reclaim_entry(pool); - if (ret) { - zswap_reject_reclaim_fail++; - if (ret != -EAGAIN) - break; + spin_lock(&zswap_pools_lock); + memcg = pool->next_shrink = + mem_cgroup_iter_online(NULL, pool->next_shrink, NULL, true); + + /* full round trip */ + if (!memcg) { + spin_unlock(&zswap_pools_lock); if (++failures == MAX_RECLAIM_RETRIES) break; + + goto resched; } +
Quoted 8 lines
mm/zswap.c static int zswap_reclaim_entry(struct zswap_pool *pool) · line 825
Quoted 5 lines
mm/zswap.c +5
-0+/* +* Acquire an extra reference to the iterated memcg in case the +* original reference is dropped by the zswap offlining callback. +*/ +css_get (&memcg->css);struct
mem_cgroupisn't defined when !CONFIG_MEMCG. This needs amem_cgroup_get()wrapper and a dummy function for no-memcg builds.
I got this exact same issue a couple of versions ago, but it was hidden behind another helper function which can be implemented as a no-op in the case of !CONFIG_MEMCG, so I forgot about it until now. It always strikes me a bit weird that we have mem_cgroup_put() but not an equivalent get - let me correct that.
Quoted 4 lines
With that fixed, though, everything else looks good to me:
Acked-by Johannes Weiner <[email protected]>
Thanks for the review, Johannes!
Nhat Pham
maintainerRe: [PATCH v7 3/6] zswap: make shrinking memcg-aware
Quoted 40 lines · Nhat Pham
Quoted 29 lines · Johannes Weiner
Quoted 27 lines · Nhat Pham
static void
shrink_worker(struct work_struct *w)
{
structzswap_pool*pool = container_of(w,typeof(*pool),
shrink_work);mm/zswap.c +12
-5+struct mem_cgroup *memcg;int ret, failures =0 ; +/* global reclaim will select cgroup in a round-robin fashion. */ do { - ret = zswap_reclaim_entry(pool); - if (ret) { - zswap_reject_reclaim_fail++; - if (ret != -EAGAIN) - break; + spin_lock(&zswap_pools_lock); + memcg = pool->next_shrink = + mem_cgroup_iter_online(NULL, pool->next_shrink, NULL, true); + + /* full round trip */ + if (!memcg) { + spin_unlock(&zswap_pools_lock); if (++failures == MAX_RECLAIM_RETRIES) break; + + goto resched; } +Quoted 8 lines
mm/zswap.c static int zswap_reclaim_entry(struct zswap_pool *pool) · line 825
Quoted 5 lines
mm/zswap.c +5
-0+/* +* Acquire an extra reference to the iterated memcg in case the +* original reference is dropped by the zswap offlining callback. +*/ +css_get (&memcg->css);struct
mem_cgroupisn't defined when !CONFIG_MEMCG. This needs amem_cgroup_get()wrapper and a dummy function for no-memcg builds.
Quoted 5 lines
I got this exact same issue a couple of versions ago, but it was hidden behind another helper function which can be implemented as a no-op in the case of !CONFIG_MEMCG, so I forgot about it until now. It always strikes me a bit weird that we have
mem_cgroup_put()but not an equivalent get - let me correct that.
Actually, I'll instead implement mem_cgroup_tryget_online(), as we have to check for the cgroup's onlineness as well anyway! If it's online, then keep the extra reference - all good. If it's not, then drop the original reference before releasing the lock.
Quoted 7 lines
Quoted 5 lines
With that fixed, though, everything else looks good to me:
Acked-by Johannes Weiner <[email protected]>Thanks for the review, Johannes!
Nhat Pham
maintainerPATCH v7 4/6 mm: memcg: add per-memcg zswap writeback stat
In reply to Nhat PhamFrom: Domenico Cerasuolo <[email protected]>
Since zswap now writes back pages from memcg-specific LRUs, we now need a new stat to show writebacks count for each memcg.
--- include/linux/vm_event_item.h | 1 + mm/memcontrol.c | 1 + mm/vmstat.c | 1 + mm/zswap.c | 3 +++ 4 files changed, 6 insertions(+)
include/linux/vm_event_item.h +1 -0
@@ -142,6 +142,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,#ifdef CONFIG_ZSWAP ZSWPIN ,ZSWPOUT , +ZSWP_WB , #endif #ifdef CONFIG_X86 DIRECT_MAP_LEVEL2_SPLIT,
mm/memcontrol.c +1 -0
@@ -703,6 +703,7 @@ static const unsigned int memcg_vm_event_stat[] = {#if defined(CONFIG_MEMCG_KMEM ) && defined(CONFIG_ZSWAP )ZSWPIN ,ZSWPOUT , +ZSWP_WB , #endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE THP_FAULT_ALLOC,
mm/vmstat.c +1 -0
@@ -1401,6 +1401,7 @@ const char * const vmstat_text[] = {#ifdef CONFIG_ZSWAP "zswpin" ,"zswpout" , +"zswp_wb" , #endif #ifdef CONFIG_X86 "direct_map_level2_splits",
mm/zswap.c +3 -0
@@ -754,6 +754,9 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o } zswap_written_back_pages++; +if (entry->objcg) +count_objcg_event (entry->objcg,ZSWP_WB ); +/* * Writeback started successfully, the page now belongs to the * swapcache. Drop the entry from zswap - unless invalidate already
Signature · 1 line
2.34.1
Johannes Weiner
maintainerRe: [PATCH v7 4/6] mm: memcg: add per-memcg zswap writeback stat
Quoted 44 lines · Nhat Pham
From: Domenico Cerasuolo <[email protected]>
Since zswap now writes back pages from memcg-specific LRUs, we now need a new stat to show writebacks count for each memcg.
Suggested-by Nhat Pham <[email protected]>Signed-off-by Domenico Cerasuolo <[email protected]>Signed-off-by Nhat Pham <[email protected]>--- include/linux/vm_event_item.h | 1 + mm/memcontrol.c | 1 + mm/vmstat.c | 1 + mm/zswap.c | 3 +++ 4 files changed, 6 insertions(+)include/linux/vm_event_item.h +1
-0@@ -142,6 +142,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,#ifdef CONFIG_ZSWAP ZSWPIN ,ZSWPOUT , +ZSWP_WB , #endif #ifdef CONFIG_X86 DIRECT_MAP_LEVEL2_SPLIT,mm/memcontrol.c +1
-0@@ -703,6 +703,7 @@ static const unsigned int memcg_vm_event_stat[] = {#if defined(CONFIG_MEMCG_KMEM ) && defined(CONFIG_ZSWAP )ZSWPIN ,ZSWPOUT , +ZSWP_WB , #endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE THP_FAULT_ALLOC,mm/vmstat.c +0
-0
mm/vmstat.c const char * const vmstat_text[] = { · line 1404
Quoted 4 lines
mm/vmstat.c +1
-0@@ -1401,6 +1401,7 @@ const char * const vmstat_text[] = {#ifdef CONFIG_ZSWAP "zswpin" ,"zswpout" , + "zswp_wb",
ZSWPWB and "zswpwb" would match the existing naming scheme a bit better.
Quoted 8 lines
mm/vmstat.c +0
-0#endif #ifdef CONFIG_X86 "direct_map_level2_splits",mm/zswap.c +0
-0
mm/zswap.c static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o · line 759
Quoted 6 lines
mm/zswap.c +3
-0@@ -754,6 +754,9 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o } zswap_written_back_pages++; +if (entry->objcg) +count_objcg_event (entry->objcg,ZSWP_WB ); +
You need to call count_vm_event() as well, otherwise the /proc/vmstat counter will stay zero.
Nhat Pham
maintainerRe: [PATCH v7 4/6] mm: memcg: add per-memcg zswap writeback stat
Quoted 65 lines · Johannes Weiner
Quoted 44 lines · Nhat Pham
From: Domenico Cerasuolo <[email protected]>
Since zswap now writes back pages from memcg-specific LRUs, we now need a new stat to show writebacks count for each memcg.
Suggested-by Nhat Pham <[email protected]>Signed-off-by Domenico Cerasuolo <[email protected]>Signed-off-by Nhat Pham <[email protected]>--- include/linux/vm_event_item.h | 1 + mm/memcontrol.c | 1 + mm/vmstat.c | 1 + mm/zswap.c | 3 +++ 4 files changed, 6 insertions(+)include/linux/vm_event_item.h +1
-0@@ -142,6 +142,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,#ifdef CONFIG_ZSWAP ZSWPIN ,ZSWPOUT , +ZSWP_WB , #endif #ifdef CONFIG_X86 DIRECT_MAP_LEVEL2_SPLIT,mm/memcontrol.c +1
-0@@ -703,6 +703,7 @@ static const unsigned int memcg_vm_event_stat[] = {#if defined(CONFIG_MEMCG_KMEM ) && defined(CONFIG_ZSWAP )ZSWPIN ,ZSWPOUT , +ZSWP_WB , #endif #ifdef CONFIG_TRANSPARENT_HUGEPAGE THP_FAULT_ALLOC,mm/vmstat.c +0
-0mm/vmstat.c const char * const vmstat_text[] = { · line 1404
Quoted 4 lines
mm/vmstat.c +1
-0@@ -1401,6 +1401,7 @@ const char * const vmstat_text[] = {#ifdef CONFIG_ZSWAP "zswpin" ,"zswpout" , + "zswp_wb",ZSWPWB and "zswpwb" would match the existing naming scheme a bit better.
Quoted 11 lines
mm/vmstat.c +0
-0#endif #ifdef CONFIG_X86 "direct_map_level2_splits",mm/zswap.c +0
-0@@ -754,6 +754,9 @@ static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o } zswap_written_back_pages++;
Quoted 6 lines
mm/zswap.c static enum lru_status shrink_memcg_cb(struct list_head *item, struct list_lru_o · line 759
Quoted 3 lines
mm/zswap.c +3
-0+if (entry->objcg) +count_objcg_event (entry->objcg,ZSWP_WB ); +You need to call
count_vm_event()as well, otherwise the/proc/vmstatcounter will stay zero.
Ahh I didn't check that. I only looked at the cgroup's memory.stat counter. I'll fix this for v8!
Nhat Pham
maintainerPATCH v7 5/6 selftests: cgroup: update per-memcg zswap writeback selftest
In reply to Nhat PhamFrom: Domenico Cerasuolo <[email protected]>
The memcg-zswap self test is updated to adjust to the behavior change implemented by commit 87730b165089 ("zswap: make shrinking memcg-aware"), where zswap performs writeback for specific memcg.
--- tools/testing/selftests/cgroup/test_zswap.c | 74 ++++++++++++++------- 1 file changed, 50 insertions(+), 24 deletions(-)
tools/testing/selftests/cgroup/test_zswap.c +50 -24
@@ -50,9 +50,9 @@ static int get_zswap_stored_pages(size_t *value)return read_int ("/sys/kernel/debug/zswap/stored_pages" , value); } -static int get_zswap_written_back_pages (size_t *value) +static int get_cg_wb_count (const char *cg) { -return read_int ("/sys/kernel/debug/zswap/written_back_pages" , value); +return cg_read_key_long (cg,"memory.stat" ,"zswp_wb" ); }static long get_zswpout (const char *cgroup)
@@ -73,6 +73,24 @@ static int allocate_bytes(const char *cgroup, void *arg)return 0 ; } +static char *setup_test_group_1M (const char *root,const char *name) +{ +char *group_name =cg_name (root, name); + +if (!group_name) +return NULL ; +if (cg_create (group_name)) + goto fail; +if (cg_write (group_name,"memory.max" ,"1M" )) { +cg_destroy (group_name); + goto fail; + } +return group_name; +fail: +free (group_name); +return NULL ; +} +/* * Sanity test to check that pages are written into zswap. */
@@ -117,43 +135,51 @@ static int test_zswap_usage(const char *root)/* * When trying to store a memcg page in zswap, if the memcg hits its memory -* limit in zswap, writeback should not be triggered. -* -* This was fixed with commit 0bdf0efa180a("zswap: do not shrink if cgroup may -* not zswap"). Needs to be revised when a per memcg writeback mechanism is -* implemented. +* limit in zswap, writeback should affect only the zswapped pages of that +* memcg. */ static int test_no_invasive_cgroup_shrink (const char *root) { -size_t written_back_before, written_back_after;int ret =KSFT_FAIL ; -char *test_group; +size_t control_allocation_size =MB (10 ); +char *control_allocation, *wb_group =NULL , *control_group =NULL ;/* Set up */ - test_group =cg_name (root,"no_shrink_test" ); -if (!test_group) - goto out; -if (cg_create (test_group)) + wb_group =setup_test_group_1M (root,"per_memcg_wb_test1" ); +if (!wb_group) +return KSFT_FAIL ; +if (cg_write (wb_group,"memory.zswap.max" ,"10K" )) goto out; -if (cg_write (test_group,"memory.max" ,"1M" )) + control_group =setup_test_group_1M (root,"per_memcg_wb_test2" ); +if (!control_group) goto out; -if (cg_write (test_group,"memory.zswap.max" ,"10K" )) + +/* Push some test_group2 memory into zswap */ +if (cg_enter_current (control_group)) goto out; -if (get_zswap_written_back_pages (&written_back_before)) + control_allocation =malloc (control_allocation_size); +for (int i =0 ; i < control_allocation_size; i +=4095 ) + control_allocation[i] ='a' ; +if (cg_read_key_long (control_group,"memory.stat" ,"zswapped" ) <1 ) goto out; -/* Allocate 10x memory.max to push memory into zswap */ -if (cg_run (test_group, allocate_bytes, (void *)MB (10 ))) +/* Allocate 10x memory.max to push wb_group memory into zswap and trigger wb */ +if (cg_run (wb_group, allocate_bytes, (void *)MB (10 ))) goto out; -/* Verify that no writeback happened because of the memcg allocation */ -if (get_zswap_written_back_pages (&written_back_after)) - goto out; -if (written_back_after == written_back_before) +/* Verify that only zswapped memory from gwb_group has been written back */ +if (get_cg_wb_count (wb_group) >0 &&get_cg_wb_count (control_group) ==0 ) ret =KSFT_PASS ; out: -cg_destroy (test_group); -free (test_group); +cg_enter_current (root); +if (control_group) { +cg_destroy (control_group); +free (control_group); + } +cg_destroy (wb_group); +free (wb_group); +if (control_allocation) +free (control_allocation);return ret; }
Signature · 1 line
2.34.1
Nhat Pham
maintainerPATCH v7 6/6 zswap: shrinks zswap pool based on memory pressure
In reply to Nhat PhamCurrently, we only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages).
This patch implements a memcg- and NUMA-aware shrinker for zswap, that is initiated when there is memory pressure. The shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis.
Furthermore, to make it more robust for many workloads and prevent overshrinking (i.e evicting warm pages that might be refaulted into memory), we build in the following heuristics:
* Estimate the number of warm pages residing in zswap, and attempt to protect this region of the zswap LRU.
* Scale the number of freeable objects by an estimate of the memory saving factor. The better zswap compresses the data, the fewer pages we will evict to swap (as we will otherwise incur IO for relatively small memory saving).
* During reclaim, if the shrinker encounters a page that is also being brought into memory, the shrinker will cautiously terminate its shrinking action, as this is a sign that it is touching the warmer region of the zswap LRU.
As a proof of concept, we ran the following synthetic benchmark:
build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds.
--- Documentation/admin-guide/mm/zswap.rst | 7 + include/linux/mmzone.h | 2 + include/linux/zswap.h | 25 +++- mm/mmzone.c | 1 + mm/swap_state.c | 2 + mm/zswap.c | 177 ++++++++++++++++++++++++- 6 files changed, 208 insertions(+), 6 deletions(-)
Documentation/admin-guide/mm/zswap.rst +7 -0
@@ -153,6 +153,13 @@ attribute, e. g.:: Setting this parameter to 100 will disable the hysteresis. +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. +User can enable it as follows: + + echo Y > /sys/module/zswap/parameters/shrinker_enabled + A debugfs interface is provided for various statistic about pool size, number of pages stored, same-value filled pages and various counters for the reasons pages are rejected.
include/linux/mmzone.h +2 -0
@@ -22,6 +22,7 @@#include <linux/mm_types.h> #include <linux/page-flags.h> #include <linux/local_lock.h> +#include <linux/zswap.h> #include <asm/page.h> /* Free memory management - zoned buddy allocator. */
@@ -641,6 +642,7 @@ struct lruvec {#ifdef CONFIG_MEMCG struct pglist_data *pgdat;#endif +struct zswap_lruvec_state zswap_lruvec_state; };/* Isolate for asynchronous migration */
include/linux/zswap.h +23 -2
@@ -5,20 +5,40 @@#include <linux/types.h> #include <linux/mm_types.h> +struct lruvec ; +extern u64 zswap_pool_total_size;extern atomic_t zswap_stored_pages;#ifdef CONFIG_ZSWAP +struct zswap_lruvec_state { +/* +* Number of pages in zswap that should be protected from the shrinker. +* This number is an estimate of the following counts: +* +* a) Recent page faults. +* b) Recent insertion to the zswap LRU. This includes new zswap stores, +* as well as recent zswap LRU rotations. +* +* These pages are likely to be warm, and might incur IO if the are written +* to swap. +*/ +atomic_long_t nr_zswap_protected; +}; +bool zswap_store (struct folio *folio);bool zswap_load (struct folio *folio);void zswap_invalidate (int type,pgoff_t offset);void zswap_swapon (int type);void zswap_swapoff (int type);void zswap_memcg_offline_cleanup (struct mem_cgroup *memcg); - +void zswap_lruvec_state_init (struct lruvec *lruvec); +void zswap_lruvec_swapin (struct page *page);#else +struct zswap_lruvec_state {}; +static inline bool zswap_store (struct folio *folio) {return false;
@@ -33,7 +53,8 @@ static inline void zswap_invalidate(int type, pgoff_t offset) {}static inline void zswap_swapon (int type) {}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_lruvec_swapin (struct page *page) {}#endif #endif /* _LINUX_ZSWAP_H */
mm/mmzone.c +1 -0
@@ -78,6 +78,7 @@ void lruvec_init(struct lruvec *lruvec)memset (lruvec,0 ,sizeof (struct lruvec ));spin_lock_init (&lruvec->lru_lock); +zswap_lruvec_state_init (lruvec);for_each_lru (lru)INIT_LIST_HEAD (&lruvec->lists[lru]);
mm/swap_state.c +2 -0
@@ -687,6 +687,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, &page_allocated, false);if (unlikely (page_allocated))swap_readpage (page, false,NULL ); +zswap_lruvec_swapin (page);return page; }
@@ -862,6 +863,7 @@ static struct page *swap_vma_readahead(swp_entry_t targ_entry, gfp_t gfp_mask, &page_allocated, false);if (unlikely (page_allocated))swap_readpage (page, false,NULL ); +zswap_lruvec_swapin (page);return page; }
mm/zswap.c +173 -4
288 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v7 6/6] zswap: shrinks zswap pool based on memory pressure
Quoted 27 lines · Nhat Pham
Currently, we only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages).
This patch implements a memcg- and NUMA-aware shrinker for zswap, that is initiated when there is memory pressure. The shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis.
Furthermore, to make it more robust for many workloads and prevent overshrinking (i.e evicting warm pages that might be refaulted into memory), we build in the following heuristics:
* Estimate the number of warm pages residing in zswap, and attempt to protect this region of the zswap LRU.
* Scale the number of freeable objects by an estimate of the memory saving factor. The better zswap compresses the data, the fewer pages we will evict to swap (as we will otherwise incur IO for relatively small memory saving).
* During reclaim, if the shrinker encounters a page that is also being brought into memory, the shrinker will cautiously terminate its shrinking action, as this is a sign that it is touching the warmer region of the zswap LRU.
Quoted 6 lines
As a proof of concept, we ran the following synthetic benchmark:
build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds.
I think this is a really important piece of functionality for zswap.
Memory compression has been around and in use for a long time, but the question of zswap vs swap sizing has been in the room since the hybrid mode was first proposed. Because depending on the reuse patterns, putting zswap with a static size limit in front of an existing swap file can be a net negative for performance as it consumes more memory.
It's great to finally see a solution to this which makes zswap *much* more general purpose. And something that distributions might want to turn on per default when swap is configured.
Actually to the point where I think there should be a config option to enable the shrinker per default. Maybe not right away, but in a few releases when this feature has racked up some more production time.
mm/swap_state.c struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 690
Quoted 5 lines
mm/swap_state.c +1
-0@@ -687,6 +687,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, &page_allocated, false);if (unlikely (page_allocated))swap_readpage (page, false,NULL ); +zswap_lruvec_swapin (page);
The "lruvec" in the name vs the page parameter is a bit odd. zswap_page_swapin() would be slightly better, but it still also sounds like it would cause an actual swapin of some sort.
zswap_record_swapin()?
Quoted 20 lines
mm/zswap.c +16
-0@@ -520,6 +575,95 @@ static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,return entry; } +/********************************* +* shrinker functions +**********************************/ +static enum lru_status shrink_memcg_cb (struct list_head *item,struct list_lru_one *l, +spinlock_t *lock,void *arg); + +static unsigned long zswap_shrinker_scan (struct shrinker *shrinker, +struct shrink_control *sc) +{ +struct lruvec *lruvec =mem_cgroup_lruvec (sc->memcg,NODE_DATA (sc->nid)); +unsigned long shrink_ret, nr_protected, lru_size; +struct zswap_pool *pool = shrinker->private_data; +bool encountered_page_in_swapcache = false; + + nr_protected = +atomic_long_read (&lruvec->zswap_lruvec_state.nr_zswap_protected);
mm/zswap.c static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, · line 603
Quoted 10 lines
mm/zswap.c +10
-0+ lru_size =list_lru_shrink_count (&pool->list_lru, sc); + +/* +* Abort if the shrinker is disabled or if we are shrinking into the +* protected region. +*/ +if (!zswap_shrinker_enabled || nr_protected >= lru_size - sc->nr_to_scan) { + sc->nr_scanned =0 ; +return SHRINK_STOP ; + }
I'm scratching my head at the protection check. zswap_shrinker_count() already factors protection into account, so sc->nr_to_scan should only be what is left on the list after excluding the protected area.
Do we even get here if the whole list is protected? Is this to protect against concurrent shrinking of the list through multiple shrinkers or swapins? If so, a comment would be nice :)
Otherwise, this looks great to me!
Just nitpicks, no show stoppers:
Nhat Pham
maintainerRe: [PATCH v7 6/6] zswap: shrinks zswap pool based on memory pressure
Quoted 44 lines · Johannes Weiner
Quoted 27 lines · Nhat Pham
Currently, we only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages).
This patch implements a memcg- and NUMA-aware shrinker for zswap, that is initiated when there is memory pressure. The shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis.
Furthermore, to make it more robust for many workloads and prevent overshrinking (i.e evicting warm pages that might be refaulted into memory), we build in the following heuristics:
* Estimate the number of warm pages residing in zswap, and attempt to protect this region of the zswap LRU.
* Scale the number of freeable objects by an estimate of the memory saving factor. The better zswap compresses the data, the fewer pages we will evict to swap (as we will otherwise incur IO for relatively small memory saving).
* During reclaim, if the shrinker encounters a page that is also being brought into memory, the shrinker will cautiously terminate its shrinking action, as this is a sign that it is touching the warmer region of the zswap LRU.Quoted 6 lines
As a proof of concept, we ran the following synthetic benchmark:
build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds.I think this is a really important piece of functionality for zswap.
Memory compression has been around and in use for a long time, but the question of zswap vs swap sizing has been in the room since the hybrid mode was first proposed. Because depending on the reuse patterns, putting zswap with a static size limit in front of an existing swap file can be a net negative for performance as it consumes more memory.
Quoted 7 lines
It's great to finally see a solution to this which makes zswap *much* more general purpose. And something that distributions might want to turn on per default when swap is configured.
Actually to the point where I think there should be a config option to enable the shrinker per default. Maybe not right away, but in a few releases when this feature has racked up some more production time.
Sure thingy - how does everyone feel about this?
Quoted 11 lines
mm/swap_state.c struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 690
Quoted 6 lines
mm/swap_state.c +1
-0@@ -687,6 +687,7 @@ struct page *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask, &page_allocated, false);if (unlikely (page_allocated))swap_readpage (page, false,NULL ); +zswap_lruvec_swapin (page);The "lruvec" in the name vs the page parameter is a bit odd.
zswap_page_swapin()would be slightly better, but it still also sounds like it would cause an actual swapin of some sort.
Quoted 1 line
zswap_record_swapin()?
Hmm that sounds good to me. I'm not very good with naming, if that's not already evident :)
Quoted 36 lines
Quoted 21 lines
mm/zswap.c +16
-0@@ -520,6 +575,95 @@ static struct zswap_entry *zswap_entry_find_get(struct rb_root *root,return entry; } +/********************************* +* shrinker functions +**********************************/ +static enum lru_status shrink_memcg_cb (struct list_head *item,struct list_lru_one *l, +spinlock_t *lock,void *arg); + +static unsigned long zswap_shrinker_scan (struct shrinker *shrinker, +struct shrink_control *sc) +{ +struct lruvec *lruvec =mem_cgroup_lruvec (sc->memcg,NODE_DATA (sc->nid)); +unsigned long shrink_ret, nr_protected, lru_size; +struct zswap_pool *pool = shrinker->private_data; +bool encountered_page_in_swapcache = false; + + nr_protected = +atomic_long_read (&lruvec->zswap_lruvec_state.nr_zswap_protected);mm/zswap.c static struct zswap_entry *zswap_entry_find_get(struct rb_root *root, · line 603
Quoted 10 lines
mm/zswap.c +10
-0+ lru_size =list_lru_shrink_count (&pool->list_lru, sc); + +/* +* Abort if the shrinker is disabled or if we are shrinking into the +* protected region. +*/ +if (!zswap_shrinker_enabled || nr_protected >= lru_size - sc->nr_to_scan) { + sc->nr_scanned =0 ; +return SHRINK_STOP ; + }I'm scratching my head at the protection check.
zswap_shrinker_count()already factors protection into account, sosc->nr_to_scanshould only be what is left on the list after excluding the protected area.
Quoted 3 lines
Do we even get here if the whole list is protected? Is this to protect against concurrent shrinking of the list through multiple shrinkers or swapins? If so, a comment would be nice :)
Yep, this is mostly for concurrent shrinkers. Please fact-check me, but IIUC if we have too many reclaimers all calling upon the zswap shrinker (before any of them can make substantial progress), we can have a situation where the total number of objects freed by the reclaimers will eat into the protection area of the zswap LRU (even if the number of freeable objects is scaled down by the compression ratio, and further scaled down internally in the shrinker/vmscan code). I've observed this tendency when there is a) a lot of worker threads in my benchmark and b) memory pressure. This is a crude/racy way to alleviate the issue.
I think this is actually a wider problem than just zswap and zswap shrinker - we need better reclaimer throttling logic IMO. Perhaps this check should be done higher up the stack - something along the lines of having each reclaimer "register" its intention (number of objects it wants to reclaim) to a particular shrinker, allowing the shrinker to deny a reclaimer if there is already a strong reclaim driving force. Or some other throttling heuristics based on the number of freeable objects and the reclaimer registration data.
Quoted 6 lines
Otherwise, this looks great to me!
Just nitpicks, no show stoppers:
Acked-by Johannes Weiner <[email protected]>
Quoted 118 lines · Nhat Pham
Changelog:
v7:
* Added themem_cgroup_iter_online()function to the API for the new behavior (suggested by Andrew Morton) (patch 2)
* Fixed a missinglist_lru_del->list_lru_del_obj(patch 1)
v6:
* Rebase on top of latest mm-unstable.
* Fix/improve the in-code documentation of the newlist_lrumanipulation functions (patch 1)
v5:
* Replace reference getting with anrcu_read_lock()section for zswap lru modifications (suggested by Yosry)
* Add a new prep patch that allowsmem_cgroup_iter()to return online cgroup.
* Add a callback that updatespool->next_shrinkwhen the cgroup is offlined (suggested by Yosry Ahmed, Johannes Weiner)
v4:
* Renamelist_lru_addtolist_lru_add_objand__list_lru_addtolist_lru_add(patch 1) (suggested by Johannes Weiner and Yosry Ahmed)
* Some cleanups on the memcg aware LRU patch (patch 2) (suggested by Yosry Ahmed)
* Use event interface for the new per-cgroup writeback counters. (patch 3) (suggested by Yosry Ahmed)
* Abstract zswap's lruvec states and handling intozswap_lruvec_state(patch 5) (suggested by Yosry Ahmed)
v3:
* Add a patch to export per-cgroup zswap writeback counters
* Add a patch to update zswap's kselftest
* Separate the newlist_lrufunctions into its own prep patch
* Do not start from the top of the hierarchy when encounter a memcg that is not online for the global limit zswap writeback (patch 2) (suggested by Yosry Ahmed)
* Do not remove the swap entry fromlist_lruin__read_swapcache_async()(patch 2) (suggested by Yosry Ahmed)
* Removed a redundant zswap pool getting (patch 2) (reported by Ryan Roberts)
* Use atomic for thenr_zswap_protected(instead of lruvec's lock) (patch 5) (suggested by Yosry Ahmed)
* Remove the per-cgroup zswap shrinker knob (patch 5) (suggested by Yosry Ahmed)
v2:
* Fix loongarch compiler errors
* Use pool stats instead of memcg stats when !CONFIG_MEMCG_KEMThere are currently several issues with zswap writeback:
1. There is only a single global LRU for zswap, making it impossible to perform worload-specific shrinking - an memcg under memory pressure cannot determine which pages in the pool it owns, and often ends up writing pages from other memcgs. This issue has been previously observed in practice and mitigated by simply disabling memcg-initiated shrinking:
https://lore.kernel.org/all/[email protected]/T/#u
But this solution leaves a lot to be desired, as we still do not have an avenue for an memcg to free up its own memory locked up in the zswap pool.
2. We only shrink the zswap pool when the user-defined limit is hit. This means that if we set the limit too high, cold data that are unlikely to be used again will reside in the pool, wasting precious memory. It is hard to predict how much zswap space will be needed ahead of time, as this depends on the workload (specifically, on factors such as memory access patterns and compressibility of the memory pages).
This patch series solves these issues by separating the global zswap LRU into per-memcg and per-NUMA LRUs, and performs workload-specific (i.e memcg- and NUMA-aware) zswap writeback under memory pressure. The new shrinker does not have any parameter that must be tuned by the user, and can be opted in or out on a per-memcg basis.
As a proof of concept, we ran the following synthetic benchmark:
build the linux kernel in a memory-limited cgroup, and allocate some cold data in tmpfs to see if the shrinker could write them out and improved the overall performance. Depending on the amount of cold data generated, we observe from 14% to 35% reduction in kernel CPU time used in the kernel builds.Domenico Cerasuolo (3):
zswap: make shrinking memcg-aware
mm: memcg: add per-memcg zswap writeback stat
selftests: cgroup: update per-memcg zswap writeback selftestNhat Pham (3):
list_lru: allows explicit memcg and NUMA node selection
memcontrol: add a new function to traverse online-only memcg hierarchy
zswap: shrinks zswap pool based on memory pressureDocumentation/admin-guide/mm/zswap.rst | 7 + drivers/android/binder_alloc.c | 7 +- fs/dcache.c | 8 +- fs/gfs2/quota.c | 6 +- fs/inode.c | 4 +- fs/nfs/nfs42xattr.c | 8 +- fs/nfsd/filecache.c | 4 +- fs/xfs/xfs_buf.c | 6 +- fs/xfs/xfs_dquot.c | 2 +- fs/xfs/xfs_qm.c | 2 +- include/linux/list_lru.h | 54 ++- include/linux/memcontrol.h | 18 + include/linux/mmzone.h | 2 + include/linux/vm_event_item.h | 1 + include/linux/zswap.h | 27 +- mm/list_lru.c | 48 ++- mm/memcontrol.c | 32 +- mm/mmzone.c | 1 + mm/swap.h | 3 +- mm/swap_state.c | 26 +- mm/vmstat.c | 1 + mm/workingset.c | 4 +- mm/zswap.c | 426 +++++++++++++++++--- tools/testing/selftests/cgroup/test_zswap.c | 74 ++-- 24 files changed, 641 insertions(+), 130 deletions(-)
Quoted 1 line
base-commit:
5cdba94229e58a39ca389ad99763af29e6b0c5a5
No regressions when booting kernel with series applied.
Signature · 1 line
An old man doll... just what I always wanted! - Clara