Skip to contentSkip to conversation

mm/memcg: Free percpu stats memory of dying memcg's

PATCH In review Waiman Long 3 participants 10 messages Apr 21, 2022 – Apr 25, 2022

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

PATCH 1/1 mm/memcg: Free percpu stats memory of dying memcg's

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Unpatched kernel:

  Run    Percpu Memory Increase    nr_dying_descendants Increase
  ---    ----------------------    -----------------------------
   1           76032 kB                       209
   2           74112 kB                       208
   3           74112 kB                       207

Patched kernel:

  Run    Percpu Memory Increase    nr_dying_descendants Increase
  ---    ----------------------    -----------------------------
   1            4224 kB                       207
   2            1536 kB                       209
   3               0 kB                       208

The x86-64 test system has 2 sockets and 96 threads. For the compiled kernel, memcg_vmstats_percpu and lruvec_stats_percpu have a size of 2424 and 656 bytes respectively. The mem_cgroup structure is just a bit over 4k. That translates to a percpu stats data size of 350.25 kB per memcg. That means 72852 kB of percpu data for 208 memcg's. This matches pretty well with the unpatched kernel figures listed above.

---
 include/linux/memcontrol.h |  13 ++++
 mm/memcontrol.c            | 122 +++++++++++++++++++++++++++++++------
 2 files changed, 115 insertions(+), 20 deletions(-)
include/linux/memcontrol.h +13 -0
index 89b14729d59f..3389b705e242 100644
@@ -51,6 +51,13 @@ enum memcg_memory_event {
 	MEMCG_NR_MEMORY_EVENTS,
 };
 
+enum percpu_stats_state {
+	MEMCG_PERCPU_STATS_ACTIVE = 0,
+	MEMCG_PERCPU_STATS_DISABLED,
+	MEMCG_PERCPU_STATS_FLUSHED,
+	MEMCG_PERCPU_STATS_FREED
+};
+
 struct mem_cgroup_reclaim_cookie {
 	pg_data_t *pgdat;
 	unsigned int generation;
@@ -299,6 +306,12 @@ struct mem_cgroup {
 	unsigned long		move_lock_flags;
 
 	MEMCG_PADDING(_pad1_);
+	/*
+	 * Disable percpu stats when offline, flush and free them after one
+	 * grace period.
+	 */
+	struct rcu_work		percpu_stats_rwork;
+	enum percpu_stats_state percpu_stats_disabled;
 
 	/* memory.stat */
 	struct memcg_vmstats	vmstats;
mm/memcontrol.c +102 -20

225 lines not on this page · raw message

Signature · 1 line
2.27.0
maintainer
#1 lore raw
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Thanks!

#2 lore raw
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

In term of overhead, it is mostly one more memory read from the mem_cgroup structure in the update path. I don't expect there will be that many updates when the memcg is in an offline state as updates will be slower in this case. Freeing the dying memcg will take a bit longer though, but its impact on the overall system performance should still be negligible.

I am also thinking about using a static_key for turning it on only for systems with more than, say, 20 cpus as the percpu memory overhead increases linearly with the number of possible cpus.

Any other suggestions and improvements are welcome.

Cheers,
Longman

maintainer
#3 lore raw
Quoted 48 lines · Waiman Long
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Quoted 7 lines

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

The big question is whether there is a memory pressure on those systems. If yes, and the number of dying cgroups is growing, it's worth investigating. It might be due to the sharing of pagecache pages and this will be ultimately fixed with implementing of the pagecache reparenting. But it also might be due to other bugs, which are fixable, so it would be great to understand.

So if there is a memory pressure and dying cgroups are still accumulating, we need to investigate and fix it.

If there is (almost) no memory pressure, it's a proactive reclaim question. There are several discussions and projects going on in this area.

Releasing percpu memory is more a workaround of the problem rather than fix. In the end, if we're accumulating dying cgroups, we're still leaking memory, just at a smaller pace (which is good, of course).

Thanks!

#4 lore raw
Quoted 54 lines · Roman Gushchin
Quoted 46 lines · Waiman Long
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Quoted 7 lines

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

Quoted 5 lines

The big question is whether there is a memory pressure on those systems. If yes, and the number of dying cgroups is growing, it's worth investigating. It might be due to the sharing of pagecache pages and this will be ultimately fixed with implementing of the pagecache reparenting. But it also might be due to other bugs, which are fixable, so it would be great to understand.

Pagecache reparenting will probably fix the problem that I have seen. Is someone working on this?

Quoted 5 lines

So if there is a memory pressure and dying cgroups are still accumulating, we need to investigate and fix it.

If there is (almost) no memory pressure, it's a proactive reclaim question. There are several discussions and projects going on in this area.

As more and more memory are pinned down by dying memcg's, there is just less memory available for other useful works. So it is an issue from the user point of view. I am not sure how much memory pressure the customers have, but they certainly are not happy about that.

Quoted 3 lines

Releasing percpu memory is more a workaround of the problem rather than fix. In the end, if we're accumulating dying cgroups, we're still leaking memory, just at a smaller pace (which is good, of course).

I agree that it is a workaround. However, without pagecache reparenting, userspace applications may need to be modified to minimize the chance of leaving behind dying memcg's. It is not easy either.

Cheers,
Longman

#5 lore raw
Quoted 62 lines · Waiman Long
Quoted 54 lines · Roman Gushchin
Quoted 46 lines · Waiman Long
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Quoted 7 lines

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

Quoted 5 lines

The big question is whether there is a memory pressure on those systems. If yes, and the number of dying cgroups is growing, it's worth investigating. It might be due to the sharing of pagecache pages and this will be ultimately fixed with implementing of the pagecache reparenting. But it also might be due to other bugs, which are fixable, so it would be great to understand.

Quoted 3 lines

Pagecache reparenting will probably fix the problem that I have seen. Is someone working on this?

We also encountered dying cgroup issue on our servers for a long time. I have worked on this for a while and proposed a resolution [1] based on obj_cgroup APIs to charge the LRU pages.

[1] https://lore.kernel.org/all/20220216115132.52602-1-songmuchun-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org/

Thanks.

#6 lore raw
Quoted 62 lines · Muchun Song
Quoted 61 lines · Waiman Long
Quoted 59 lines · Roman Gushchin
Quoted 46 lines · Waiman Long
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Quoted 7 lines

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

The big question is whether there is a memory pressure on those systems. If yes, and the number of dying cgroups is growing, it's worth investigating. It might be due to the sharing of pagecache pages and this will be ultimately fixed with implementing of the pagecache reparenting. But it also might be due to other bugs, which are fixable, so it would be great to understand.

Quoted 8 lines
Quoted 3 lines

Pagecache reparenting will probably fix the problem that I have seen. Is someone working on this?

We also encountered dying cgroup issue on our servers for a long time. I have worked on this for a while and proposed a resolution [1] based on obj_cgroup APIs to charge the LRU pages.

[1] https://lore.kernel.org/all/20220216115132.52602-1-songmuchun-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org/

Thanks for the pointer. I am interested in this patch series. Please cc me if you need to generate a new revision.

Cheers,
Longman

maintainer
#7 lore raw
In reply to Waiman Long
Quoted 62 lines · Waiman Long
Quoted 54 lines · Roman Gushchin
Quoted 46 lines · Waiman Long
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Quoted 7 lines

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

Quoted 5 lines

The big question is whether there is a memory pressure on those systems. If yes, and the number of dying cgroups is growing, it's worth investigating. It might be due to the sharing of pagecache pages and this will be ultimately fixed with implementing of the pagecache reparenting. But it also might be due to other bugs, which are fixable, so it would be great to understand.

Quoted 2 lines

Pagecache reparenting will probably fix the problem that I have seen. Is someone working on this?

Some time ago Muchun posted patches based on the reusing of the obj_cgroup API.

I'm not strictly against this approach, but in my opinion it's not the best. I suggested to use lru vectors as an intermediate objects. In theory, it might allow to avoid bumping reference counters for all charged pages at all: live cgroups will be protected by being live, dying cgroups will only need a temporarily protection while lru vectors and associated pages are reparenting.

There are pros and cons:
+ cgroup reference counting becomes simpler and more debuggable
+ potential perf wins from fewer operations with live cgroups css refcounters = I hope to see code simplifications (but not guaranteed)
- deleting cgroups becomes more expensive, but the cost can be spread to asynchronous workers

Idk if Muchun tried to implement it. If not, I might try myself.

Thanks!

#8 lore raw
Quoted 63 lines · Roman Gushchin
Quoted 62 lines · Waiman Long
Quoted 59 lines · Roman Gushchin
Quoted 46 lines · Waiman Long
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Quoted 7 lines

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

The big question is whether there is a memory pressure on those systems. If yes, and the number of dying cgroups is growing, it's worth investigating. It might be due to the sharing of pagecache pages and this will be ultimately fixed with implementing of the pagecache reparenting. But it also might be due to other bugs, which are fixable, so it would be great to understand.

Quoted 5 lines
Quoted 2 lines

Pagecache reparenting will probably fix the problem that I have seen. Is someone working on this?

Some time ago Muchun posted patches based on the reusing of the obj_cgroup API.

Yep. It is here:

https://lore.kernel.org/all/[email protected]/.

Quoted 2 lines

I'm not strictly against this approach, but in my opinion it's not the best. I suggested to use lru vectors as an intermediate objects. In theory, it might

I remember this.

Quoted 4 lines

allow to avoid bumping reference counters for all charged pages at all: live cgroups will be protected by being live, dying cgroups will only need a temporarily protection while lru vectors and associated pages are reparenting.

Quoted 9 lines

There are pros and cons:
+ cgroup reference counting becomes simpler and more debuggable
+ potential perf wins from fewer operations with live cgroups css refcounters = I hope to see code simplifications (but not guaranteed)
- deleting cgroups becomes more expensive, but the cost can be spread to asynchronous workers

Idk if Muchun tried to implement it. If not, I might try myself.

Yep. I have implemented a initial version recently. I'll do some stability tests and send it out ASAP.

Thanks Roman.

maintainer
#9 lore raw
Quoted 90 lines · Muchun Song
Quoted 63 lines · Roman Gushchin
Quoted 62 lines · Waiman Long
Quoted 59 lines · Roman Gushchin
Quoted 46 lines · Waiman Long
Quoted 38 lines · Roman Gushchin
Quoted 22 lines · Waiman Long

For systems with large number of CPUs, the majority of the memory consumed by the mem_cgroup structure is actually the percpu stats memory. When a large number of memory cgroups are continuously created and destroyed (like in a container host), it is possible that more and more mem_cgroup structures remained in the dying state holding up increasing amount of percpu memory.

We can't free up the memory of the dying mem_cgroup structure due to active references in some other places. However, the percpu stats memory allocated to that mem_cgroup is a different story.

This patch adds a new percpu_stats_disabled variable to keep track of the state of the percpu stats memory. If the variable is set, percpu stats update will be disabled for that particular memcg. All the stats update will be forward to its parent instead. Reading of the its percpu stats will return 0.

The flushing and freeing of the percpu stats memory is a multi-step process. The percpu_stats_disabled variable is set when the memcg is being set to offline state. After a grace period with the help of RCU, the percpu stats data are flushed and then freed.

Quoted 6 lines

This will greatly reduce the amount of memory held up by dying memory cgroups.

By running a simple management tool for container 2000 times per test run, below are the results of increases of percpu memory (as reported in /proc/meminfo) and nr_dying_descendants in root's cgroup.stat.

Hi Waiman!

I've been proposing the same idea some time ago:
https://lore.kernel.org/all/[email protected]/T/ .

However I dropped it with the thinking that with many other fixes preventing the accumulation of the dying cgroups it's not worth the added complexity and a potential cpu overhead.

Quoted 7 lines

I think it ultimately comes to the number of dying cgroups. If it's low, memory savings are not worth the cpu overhead. If it's high, they are. I hope long-term to drive it down significantly (with lru-pages reparenting being the first major milestone), but it might take a while.

I don't have a strong opinion either way, just want to dump my thoughts on this.

Quoted 7 lines

I have quite a number of customer cases complaining about increasing percpu memory usages. The number of dying memcg's can go to tens of thousands. From my own investigation, I believe that those dying memcg's are not freed because they are pinned down by references in the page structure. I am aware that we support the use of objcg in the page structure which will allow easy reparenting, but most pages don't do that and it is not easy to do this conversion and it may take quite a while to do that.

The big question is whether there is a memory pressure on those systems. If yes, and the number of dying cgroups is growing, it's worth investigating. It might be due to the sharing of pagecache pages and this will be ultimately fixed with implementing of the pagecache reparenting. But it also might be due to other bugs, which are fixable, so it would be great to understand.

Quoted 5 lines
Quoted 2 lines

Pagecache reparenting will probably fix the problem that I have seen. Is someone working on this?

Some time ago Muchun posted patches based on the reusing of the obj_cgroup API.

Yep. It is here:

https://lore.kernel.org/all/[email protected]/.

Quoted 2 lines

I'm not strictly against this approach, but in my opinion it's not the best. I suggested to use lru vectors as an intermediate objects. In theory, it might

I remember this.

Quoted 11 lines

allow to avoid bumping reference counters for all charged pages at all: live cgroups will be protected by being live, dying cgroups will only need a temporarily protection while lru vectors and associated pages are reparenting.

There are pros and cons:
+ cgroup reference counting becomes simpler and more debuggable
+ potential perf wins from fewer operations with live cgroups css refcounters = I hope to see code simplifications (but not guaranteed)
- deleting cgroups becomes more expensive, but the cost can be spread to asynchronous workers

Quoted 5 lines
Quoted 2 lines

Idk if Muchun tried to implement it. If not, I might try myself.

Yep. I have implemented a initial version recently. I'll do some stability tests and send it out ASAP.

Great! Looking forward to see it! And thank you for working on it!