mm: restore per-memcg reclaim for NONSLAB shrinkers under nokmem
Subsystems (6)
v5 cover mm: restore per-memcg reclaim for NONSLAB shrinkers under nokmem
With cgroup.memory=nokmem, the THP deferred split shrinker and the zswap shrinker lose their memcg awareness because neither sets SHRINKER_NONSLAB. When a cgroup hits its memory limit, these shrinkers do not split its partially unmapped THPs or write back its zswap entries.
Under nokmem, __list_lru_init() also disables per-memcg lists. Setting SHRINKER_NONSLAB alone would leave all memcgs sharing one list per node for each shrinker. Adding the first object to an empty list sets the shrinker bit for its memcg. A second memcg can add objects without setting its own bit, so reclaim in that memcg may skip the shrinker. A scan for the first memcg can then reach objects belonging to both.
Before commit fafaeceb89a5 ("mm: switch deferred split shrinker to list_lru"), THP had fully per-memcg deferred split queues embedded in struct mem_cgroup, working independently of kmem accounting. nokmem only opts out of kernel slab accounting; THPs and zswapped pages are user memory and remain charged to their cgroups, so per-memcg reclaim is still what these shrinkers want.
Patch 1 drops kmemcg_id and indexes non-root lists by the memory CSS ID. The root memcg keeps index zero and uses the per-node list directly. The lists are reparented when their memcg goes offline, including under nokmem.
Patch 2 keeps a list_lru memcg aware under nokmem when its backing shrinker is registered SHRINKER_NONSLAB.
Patch 3 restores SHRINKER_NONSLAB on the THP deferred split shrinker.
Patch 4 sets the same flag on the zswap shrinker.
Slab-backed lrus (e.g. the superblock dentry/inode lrus) still fall back to per-node lists under nokmem. The per-memcg lists for the two shrinkers are allocated on demand.
With nokmem enabled, the test_memcontrol and test_zswap kselftests showed no new failures compared with the unpatched kernel. When one of two sibling cgroups exceeded memory.high, the patched kernel split THPs and wrote back zswap entries only in that cgroup.
Changes in v5:
- Rebase on mm-unstable (7d1a16abe4cc) as discussed with Andrew [7].
- Fold v4 patches 1 and 2 [6] into patch 1. Use the memory CSS ID so that two generations of the memory controller on the same cgroup have distinct indices while both are alive.
- Keep the memcg argument to the internal walk for the owner check on restart. Hold RCU from the XArray lookup until the walker takes a reference to an online CSS.
Changes in v4:
- Fix the boot hang reported by Kairui against v3 [3]: the walker no longer reverse-resolves the xarray keys; the index is passed directly through the internal walk path (new patch 1, split out as a preparation so patch 2 remains a pure key switch).
- Drop the kmemcg_id member from memcg_struct_check() along with the field removal, as pointed out by Gregory Price [4].
- Otherwise unchanged from v3.
Changes in v3:
- Index the list_lru xarray with mem_cgroup_id() (the cgroup ID) instead of the private memcg ID, whose lifetime only covers online groups, and pass memcg pointers through the list_lru plumbing; inline memcg_online_kmem()/memcg_offline_kmem() into the css online/offline hooks (patch 1 as numbered in v3, per Johannes's review of v2 [5]).
- Describe in the THP patch's changelog why the list_lru conversion dropped SHRINKER_NONSLAB knowingly, per Johannes's pointer to the original discussion [5].
- Collected review tags: Johannes's Reviewed-by on patches 2-4, Nhat's and Yosry's Acked-by on patch 4 (numbering as in v3).
Changes in v2:
- Keep the list_lrus behind the two shrinkers per-memcg under nokmem, so the shrinker bits are set for the right memcgs and scans stay scoped to the target cgroup's objects (patches 1-2 as numbered in v2, new; addresses Sashiko's review of v1 [2]).
- Patches 3-4 are unchanged from v1 [1] apart from review tags.
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://sashiko.dev/#/patchset/[email protected]
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/arNK64PxTXbVEhis@gourry-fedora-PF4VCD3F/
[5] https://lore.kernel.org/lkml/[email protected]/
[6] https://lore.kernel.org/lkml/[email protected]/
[7] https://lore.kernel.org/lkml/[email protected]/
Qinyun Tan (4):
mm: list_lru: index per-memcg lists by memory CSS ID
mm: list_lru: keep per-memcg lists with nokmem for NONSLAB-backed lrus
mm: thp: restore SHRINKER_NONSLAB on the deferred split shrinker
mm: zswap: mark the zswap shrinker SHRINKER_NONSLAB
include/linux/memcontrol.h | 16 ------------- mm/huge_memory.c | 3 ++- mm/list_lru.c | 49 +++++++++++++++++++++++++------------- mm/memcontrol.c | 45 +++++++--------------------------- mm/zswap.c | 4 ++-- 5 files changed, 45 insertions(+), 72 deletions(-)
Signature · 1 line
2.55.0
PATCH v5 1/4 mm: list_lru: index per-memcg lists by memory CSS ID
list_lru uses kmemcg_id, a copy of the private memcg ID, to index its per-memcg lists. kmemcg_id is only set when kmem accounting is enabled. Non-slab shrinkers need an index that also works with cgroup.memory=nokmem.
Use memcg->css.id for non-root lists. The ID stays allocated until the CSS is freed, so disabling and re-enabling the memory controller on the same cgroup gives the new CSS a distinct index while the old CSS is still referenced. Reserve index zero for the root, which uses the per-node list directly.
In list_lru_walk_node(), hold the RCU read lock across both lookups so the CSS ID cannot be reused between them. css_from_id() can see a CSS before its online callback succeeds, so check that the CSS is online before taking a reference. Drop the RCU read lock for the walk and keep the reference across restarts to recheck the same owner.
Remove kmemcg_id and reparent the lists before the objcgs on memcg offline, including when kmem accounting is disabled.
--- include/linux/memcontrol.h | 16 -------------- mm/list_lru.c | 45 +++++++++++++++++++++++++------------- mm/memcontrol.c | 45 +++++++------------------------------- 3 files changed, 38 insertions(+), 68 deletions(-)
include/linux/memcontrol.h +0 -16
@@ -335,8 +335,6 @@ struct mem_cgroup {/* Private memcg ID. Used to ID objects that outlive the cgroup */ int private_id; -int kmemcg_id; -/* * Should the OOM killer kill all belonging tasks, had it kill one? */
@@ -1796,15 +1794,6 @@ static inline void memcg_kmem_uncharge_page(struct page *page, int order)__memcg_kmem_uncharge_page (page, order); } -/* -* A helper for accessing memcg's kmem_id, used for getting -* corresponding LRU lists. -*/ -static inline int memcg_kmem_id (const struct mem_cgroup *memcg) -{ -return memcg ? memcg->kmemcg_id :-1 ; -} -struct mem_cgroup *mem_cgroup_from_virt (void *p); static inline void count_objcg_events(struct obj_cgroup *objcg,
@@ -1872,11 +1861,6 @@ static inline bool memcg_kmem_online(void)return false; } -static inline int memcg_kmem_id (const struct mem_cgroup *memcg) -{ -return -1 ; -} -static inline struct mem_cgroup *mem_cgroup_from_virt (void *p) {return NULL ;
mm/list_lru.c +30 -15
108 lines not on this page · raw message
mm/memcontrol.c +8 -37
89 lines not on this page · raw message
Signature · 1 line
2.55.0
Re: [PATCH v5 1/4] mm: list_lru: index per-memcg lists by memory CSS ID
Quoted 20 lines · Qinyun Tan
list_lruuseskmemcg_id, a copy of the private memcg ID, to index its per-memcg lists.kmemcg_idis only set when kmem accounting is enabled. Non-slab shrinkers need an index that also works with cgroup.memory=nokmem.Use
memcg->css.idfor non-root lists. The ID stays allocated until the CSS is freed, so disabling and re-enabling the memory controller on the same cgroup gives the new CSS a distinct index while the old CSS is still referenced. Reserve index zero for the root, which uses the per-node list directly.In
list_lru_walk_node(), hold the RCU read lock across both lookups so the CSS ID cannot be reused between them.css_from_id()can see a CSS before its online callback succeeds, so check that the CSS is online before taking a reference. Drop the RCU read lock for the walk and keep the reference across restarts to recheck the same owner.
Quoted 4 lines
Remove
kmemcg_idand reparent the lists before the objcgs on memcg offline, including when kmem accounting is disabled.Signed-off-by Qinyun Tan <[email protected]>
Nice cleanup.
Thanks.
PATCH v5 2/4 mm: list_lru: keep per-memcg lists with nokmem for NONSLAB-backed lrus
In reply to Qinyun TanWith cgroup.memory=nokmem, __list_lru_init() collapses every list_lru into per-node lists. That is fine for slab objects, which are not charged per memcg without kmem accounting. But a lru backed by a SHRINKER_NONSLAB shrinker, e.g. the THP deferred split queue or the zswap entries, holds user memory, which is charged regardless of nokmem. Collapsing such an lru loses the per-memcg view of its objects. Reclaim can no longer target just the cgroup under pressure.
Keep such lrus memcg aware under nokmem when their backing shrinker is registered SHRINKER_NONSLAB.
This leaves the savings of nokmem intact: slab-backed lrus (e.g. the superblock dentry/inode lrus) still fall back to the per-node lists, and the per-memcg lists are allocated only when a memcg actually holds such objects.
--- mm/list_lru.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/list_lru.c +3 -1
@@ -712,7 +712,9 @@ int __list_lru_init(struct list_lru *lru, bool memcg_aware, struct shrinker *shr else lru->shrinker_id = -1; -if (mem_cgroup_disabled () ||mem_cgroup_kmem_disabled ()) +if (mem_cgroup_disabled () || + (mem_cgroup_kmem_disabled () && + (!shrinker || !(shrinker->flags &SHRINKER_NONSLAB )))) memcg_aware = false;#endif
Signature · 1 line
2.55.0
Re: [PATCH v5 2/4] mm: list_lru: keep per-memcg lists with nokmem for NONSLAB-backed lrus
Quoted 15 lines · Qinyun Tan
With cgroup.memory=nokmem,
__list_lru_init()collapses everylist_lruinto per-node lists. That is fine for slab objects, which are not charged per memcg without kmem accounting. But a lru backed by aSHRINKER_NONSLABshrinker, e.g. the THP deferred split queue or the zswap entries, holds user memory, which is charged regardless of nokmem. Collapsing such an lru loses the per-memcg view of its objects. Reclaim can no longer target just the cgroup under pressure.Keep such lrus memcg aware under nokmem when their backing shrinker is registered
SHRINKER_NONSLAB.
Quoted 8 lines
This leaves the savings of nokmem intact: slab-backed lrus (e.g. the superblock dentry/inode lrus) still fall back to the per-node lists, and the per-memcg lists are allocated only when a memcg actually holds such objects.
Fixes fafaeceb89a5 ("mm: switch deferred split shrinker to list_lru")Signed-off-by Qinyun Tan <[email protected]>Reviewed-by Johannes Weiner <[email protected]>
PATCH v5 3/4 mm: thp: restore SHRINKER_NONSLAB on the deferred split shrinker
In reply to Qinyun TanOn a system booted with cgroup.memory=nokmem, the deferred split shrinker is demoted to a non-memcg-aware one. As a result, partially unmapped THPs are only split under global reclaim; memcg (limit-induced) reclaim never splits them, so a cgroup under memory pressure keeps its underused THPs intact.
The shrinker has carried SHRINKER_NONSLAB since commit 0a432dcbeb32 ("mm: shrinker: make shrinker not depend on memcg kmem") precisely so it would keep working with kmem accounting disabled. Commit fafaeceb89a5 ("mm: switch deferred split shrinker to list_lru") dropped it knowingly: under nokmem the list_lru collapses into a shared per-node list while __list_lru_add() still sets the shrinker bit on whichever memcg happens to add the first item, and with the flag set the shrinker would run twice per reclaim cycle.
NONSLAB-backed lrus keep their per-memcg lists under nokmem now, so the shrinker bit identifies the owning memcg again. Restore the flag.
This was noticed by Michal during review of the patch "mm/list_lru:
don't copy stale shrinker id from non-memcg-aware shrinkers".
--- mm/huge_memory.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
mm/huge_memory.c +2 -1
@@ -1058,7 +1058,8 @@ int folio_memcg_alloc_deferred(struct folio *folio)static int __init thp_shrinker_init (void ) { deferred_split_shrinker =shrinker_alloc (SHRINKER_NUMA_AWARE | -SHRINKER_MEMCG_AWARE , +SHRINKER_MEMCG_AWARE | +SHRINKER_NONSLAB ,"thp-deferred_split" );if (!deferred_split_shrinker)return -ENOMEM ;
Signature · 1 line
2.55.0
Re: [PATCH v5 3/4] mm: thp: restore SHRINKER_NONSLAB on the deferred split shrinker
Quoted 26 lines · Qinyun Tan
On a system booted with cgroup.memory=nokmem, the deferred split shrinker is demoted to a non-memcg-aware one. As a result, partially unmapped THPs are only split under global reclaim; memcg (limit-induced) reclaim never splits them, so a cgroup under memory pressure keeps its underused THPs intact.
The shrinker has carried
SHRINKER_NONSLABsince commit0a432dcbeb32("mm: shrinker: make shrinker not depend on memcg kmem") precisely so it would keep working with kmem accounting disabled. Commitfafaeceb89a5("mm: switch deferred split shrinker tolist_lru") dropped it knowingly: under nokmem thelist_lrucollapses into a shared per-node list while__list_lru_add()still sets the shrinker bit on whichever memcg happens to add the first item, and with the flag set the shrinker would run twice per reclaim cycle.NONSLAB-backed lrus keep their per-memcg lists under nokmem now, so the shrinker bit identifies the owning memcg again. Restore the flag.
This was noticed by Michal during review of the patch "
mm/list_lru:
don't copy stale shrinker id from non-memcg-aware shrinkers".
Quoted 10 lines
Fixes fafaeceb89a5 ("mm: switch deferred split shrinker to list_lru")Suggested-by Michal Koutný <[email protected]>Signed-off-by Qinyun Tan <[email protected]>Acked-by Zi Yan <[email protected]>Acked-by Usama Arif <[email protected]>Reviewed-by Baolin Wang <[email protected]>Reviewed-by Johannes Weiner <[email protected]>
PATCH v5 4/4 mm: zswap: mark the zswap shrinker SHRINKER_NONSLAB
In reply to Qinyun TanWith kmem accounting disabled (cgroup.memory=nokmem), memcg reclaim never invokes the zswap shrinker: a cgroup under memory pressure does not write back its own zswapped pages to make room; they are only written back by global reclaim or once the global pool limit kicks in.
The zswap shrinker is registered memcg-aware but without SHRINKER_NONSLAB, so under nokmem it is treated as a slab shrinker:
before commit 03375203e1da ("mm: do not allocate shrinker info with cgroup.memory=nokmem") it was skipped by the !memcg_kmem_online() check in shrink_slab_memcg(); since that commit it is demoted to non-memcg-aware at registration. But the zswap shrinker is not a slab
shrinker: it tracks zswap entries and resolves the owning memcg from the folio's objcg, independent of kmem accounting.
This was noticed by Michal during review of the patch "mm/list_lru:
don't copy stale shrinker id from non-memcg-aware shrinkers".
Mark it SHRINKER_NONSLAB so it keeps its memcg awareness and runs under memcg reclaim with nokmem.
--- mm/zswap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/zswap.c +2 -2
@@ -1379,8 +1379,8 @@ static struct shrinker *zswap_alloc_shrinker(void) {struct shrinker *shrinker; - shrinker = -shrinker_alloc (SHRINKER_NUMA_AWARE |SHRINKER_MEMCG_AWARE ,"mm-zswap" ); + shrinker =shrinker_alloc (SHRINKER_NUMA_AWARE |SHRINKER_MEMCG_AWARE | +SHRINKER_NONSLAB ,"mm-zswap" );if (!shrinker)return NULL ;
Signature · 1 line
2.55.0
Re: [PATCH v5 4/4] mm: zswap: mark the zswap shrinker SHRINKER_NONSLAB
Quoted 24 lines · Qinyun Tan
With kmem accounting disabled (cgroup.memory=nokmem), memcg reclaim never invokes the zswap shrinker: a cgroup under memory pressure does not write back its own zswapped pages to make room; they are only written back by global reclaim or once the global pool limit kicks in.
The zswap shrinker is registered memcg-aware but without
SHRINKER_NONSLAB, so under nokmem it is treated as a slab shrinker:
before commit03375203e1da("mm: do not allocate shrinker info with cgroup.memory=nokmem") it was skipped by the !memcg_kmem_online() check inshrink_slab_memcg(); since that commit it is demoted to non-memcg-aware at registration. But the zswap shrinker is not a slab
shrinker: it tracks zswap entries and resolves the owning memcg from the folio's objcg, independent of kmem accounting.This was noticed by Michal during review of the patch "
mm/list_lru:
don't copy stale shrinker id from non-memcg-aware shrinkers".Mark it
SHRINKER_NONSLABso it keeps its memcg awareness and runs under memcg reclaim with nokmem.
Quoted 8 lines
Fixes b5ba474f3f51 ("zswap: shrink zswap pool based on memory pressure")Suggested-by Michal Koutný <[email protected]>Signed-off-by Qinyun Tan <[email protected]>Acked-by Usama Arif <[email protected]>Acked-by Nhat Pham <[email protected]>Acked-by Yosry Ahmed <[email protected]>Reviewed-by Johannes Weiner <[email protected]>