Skip to contentSkip to conversation

Fix vmstat_percpu incorrect subtraction after reparent

PATCH In review Cai Xinchen 2 participants 5 messages Mar 20, 2023 – Mar 27, 2023

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

cover Fix vmstat_percpu incorrect subtraction after reparent

Hello, I see the patch-series (Use obj_cgroup APIs to charge the LRU pages).

There are two problems left:

     root
     /  \
    A    B

/ \ \
C E D

1. In some case of reparent, some page cache may be used by other memcg D but it charges to the parent memcg A of dying memcg E. D is getting away with using the page for free while A is taxed.

For this problem, the page may be shared by many memcgs. Which memcg should be recharged to? It is hard to select. And for recharge method, for example, the user rmdir E. If we recharge the page to D, some pages of process attached to D may be reclaimed. The user may feel confused about the phenomenon that I rmdir E but the processes attached to D are reclaiming their pages and running slower.

And for cgroup v2, the page is charged to the memcg when it alloc and the stats is counted to its parent. The method of reparent seems to follow the rule.

2. The stats problem of vmstats_percpu. When memcg C is offllined, its pages are reparented to memcg P, so far P->vmstats (hierarchical) have those pages, and P->vmstats_percpu (non-hierarchical) don't. When those pages get uncharged, P->vmstats (hierachical) decreases, which is correct,
but P->vmstats_percpu (non-hierarchical) also decreases, which is wrong, as those stats were never added to P->vmstats_percpu to begin with. If the reparented memory exceeds the original non-hierarchical memory in P, some arg such as cache which is show in memory.stat will be zero (if x < 0, it shows 0)

I think propagate vmstats_percpu stats of dying memcg to its parent can solve this problem. If we do not propagate, the reparented memory exceeds the original non-hierarchical memory in P, (hierarchical_usage - non-hierarchical_usage(shows 0, but exactly negative number) - children_hierarchical_usage) may be meaningless.

And I want to ask for your opinions about problem 1, how to define the actions of charging pages to memcg when the memcg is died.

Cai Xinchen (1):
mm: memcontrol: fix vmstats_percpu state incorrect subtraction after
reparent

 kernel/cgroup/cgroup.c |  5 +++++
 mm/memcontrol.c        | 43 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/1 mm: memcontrol: fix vmstats_percpu state incorrect subtraction after reparent

When memcg C is offllined, its pages are reparented to memcg P, so far P->vmstats (hierarchical) have those pages, and P->vmstats_percpu (non-hierarchical) don't. When those pages get uncharged, P->vmstats (hierachical) decreases, which is correct, but P->vmstats_percpu (non-hierarchical) also decreases, which is wrong, as those stats were never added to P->vmstats_percpu to begin with. If the reparented memory exceeds the original non-hierarchical memory in P, some arg such as cache which is shown in memory.stat will be zero (if x < 0, it shows 0)

To solve this problem, after reparent memcg, we should use cgroup_rstat_updated to add cgrp to rstat updated tree, and then after css refcount == 0, cgroup_rstat_flush is called by css_release_work_fn and css->flags is set as CSS_RELEASED. We propagate state in vmstat_percpu of struct mem_cgroup to its parent if css->flags is CSS_RELEASED. We use reparent_state to record those state and reparent_tag to ensure propagate vmstats_percpu of dying memcg only once. When mem_cgroup_css_rstat_flush is called, we
add reparent_state to parent_memcg->vmstats_percpu->state and memcg->vmstgats_percpu->state_prev in locked context. The delta between state and state_prev would not change. So some accumulative
value of vmstats_percpu (such as cache in memory.stat) will be
shown correctly. And this patch does not produce delta between state and state_prev in vmstats_percpu which will be added to vmstats.

---
 kernel/cgroup/cgroup.c |  5 +++++
 mm/memcontrol.c        | 43 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)
kernel/cgroup/cgroup.c +5 -0
index 935e8121b21e..11138ee09a61 100644
@@ -5384,12 +5384,17 @@ static void css_release_work_fn(struct work_struct *work)
 		container_of(work, struct cgroup_subsys_state, destroy_work);
 	struct cgroup_subsys *ss = css->ss;
 	struct cgroup *cgrp = css->cgroup;
+	int cpu;
 
 	mutex_lock(&cgroup_mutex);
 
 	css->flags |= CSS_RELEASED;
 	list_del_rcu(&css->sibling);
 
+	/* update all cgrp rstat because reparent rstat flush */
+	for_each_possible_cpu(cpu)
+		cgroup_rstat_updated(cgrp, cpu);
+
 	if (ss) {
 		/* css release path */
 		if (!list_empty(&css->rstat_css_node)) {
mm/memcontrol.c +42 -1

92 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH 1/1] mm: memcontrol: fix vmstats_percpu state incorrect subtraction after reparent

Hello.

Quoted 7 lines · Cai Xinchen

When memcg C is offllined, its pages are reparented to memcg P, so far P->vmstats (hierarchical) have those pages, and P->vmstats_percpu (non-hierarchical) don't. When those pages get uncharged, P->vmstats (hierachical) decreases, which is correct, but P->vmstats_percpu (non-hierarchical) also decreases, which is wrong, as those stats were never added to P->vmstats_percpu to begin with.

I was wondering why ->vmstats_percpu matters (in the end all is summed in ->vmstats) -- do you mean this is a cgroup v1 only issue? As only that exposes the non-hieararchical stats.

Thanks,
Michal

#3 lore raw

Re: [PATCH 1/1] mm: memcontrol: fix vmstats_percpu state incorrect subtraction after reparent

Yes, only cgroup v1.

Quoted 16 lines · Michal Koutný

Hello.

Quoted 7 lines · Cai Xinchen

When memcg C is offllined, its pages are reparented to memcg P, so far P->vmstats (hierarchical) have those pages, and P->vmstats_percpu (non-hierarchical) don't. When those pages get uncharged, P->vmstats (hierachical) decreases, which is correct, but P->vmstats_percpu (non-hierarchical) also decreases, which is wrong, as those stats were never added to P->vmstats_percpu to begin with.

I was wondering why ->vmstats_percpu matters (in the end all is summed in ->vmstats) -- do you mean this is a cgroup v1 only issue? As only that exposes the non-hieararchical stats.

Thanks,
Michal

maintainer
#4 lore raw
In reply to Cai Xinchen
Quoted 11 lines · Cai Xinchen

There are two problems left:

     root
     /  \
    A    B

/ \ \
C E D

1. In some case of reparent, some page cache may be used by other memcg D but it charges to the parent memcg A of dying memcg E. D is getting away with using the page for free while A is taxed.

Note that A is (effectively) taxed even before E is removed due to hierarchical nature of charging. Then what you describe transforms into "well-known" problem of shared charging (with not well-known solution :-/).

HTH,
Michal