Skip to contentSkip to conversation

mm: memcontrol: fix memsw uncharge for root_mem_cgroup

PATCH In review Muchun Song 2 participants 6 messages Mar 23, 2021 – Mar 24, 2021

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

PATCH 1/1 mm: memcontrol: fix memsw uncharge for root_mem_cgroup

The pages aren't accounted at the root level, so we cannot uncharge the page to the memsw counter for the root memcg. Fix this.

Fixes 1f47b61fb407 ("mm: memcontrol: fix swap counter leak on swapout from offline cgroup")
---
 mm/memcontrol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol.c +2 -1
index 533b4b31b464..7d765a106684 100644
@@ -7155,7 +7155,8 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	if (!cgroup_memory_noswap && memcg != swap_memcg) {
 		if (!mem_cgroup_is_root(swap_memcg))
 			page_counter_charge(&swap_memcg->memsw, nr_entries);
-		page_counter_uncharge(&memcg->memsw, nr_entries);
+		if (!mem_cgroup_is_root(memcg))
+			page_counter_uncharge(&memcg->memsw, nr_entries);
 	}
 
 	/*
Signature · 1 line
2.11.0
Acked-by on v1 1/1
#1 lore raw
Quoted 2 lines · Muchun Song

The pages aren't accounted at the root level, so we cannot uncharge the page to the memsw counter for the root memcg. Fix this.

The patch is correct but I do wonder whether this matters much in the end. We shouldn't really rely on a correct page counter for the root memcg AFAICS in the kernel. We do not display the value (mem_cgroup_usage) so there shouldn't be any actual problem. Unless I am missing something make sure to spell that out in the changelog.

Quoted 2 lines
Fixes 1f47b61fb407 ("mm: memcontrol: fix swap counter leak on swapout from offline cgroup")
Quoted 20 lines
---
 mm/memcontrol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol.c +2 -1
index 533b4b31b464..7d765a106684 100644
@@ -7155,7 +7155,8 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
 	if (!cgroup_memory_noswap && memcg != swap_memcg) {
 		if (!mem_cgroup_is_root(swap_memcg))
 			page_counter_charge(&swap_memcg->memsw, nr_entries);
-		page_counter_uncharge(&memcg->memsw, nr_entries);
+		if (!mem_cgroup_is_root(memcg))
+			page_counter_uncharge(&memcg->memsw, nr_entries);
 	}
 
 	/*
Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
In reply to Muchun Song
Quoted 6 lines · Muchun Song

The pages aren't accounted at the root level, so we cannot uncharge the page to the memsw counter for the root memcg. Fix this.

Fixes 1f47b61fb407 ("mm: memcontrol: fix swap counter leak on swapout from offline cgroup")

I am very sorry. I should repent. I suddenly realise the fix is totally wrong. Because the @memcg cannot be root memcg when
@memcg != @swap_memcg. Please ignore this patch. I am very sorry for the noise. And sorry to Michal.

Quoted 21 lines
---
 mm/memcontrol.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/memcontrol.c +2 -2
index 533b4b31b464..7d765a106684 100644
@@ -7155,7 +7155,8 @@ void mem_cgroup_swapout(struct page *page, swp_entry_t entry)
        if (!cgroup_memory_noswap && memcg != swap_memcg) {
                if (!mem_cgroup_is_root(swap_memcg))
                        page_counter_charge(&swap_memcg->memsw, nr_entries);
-               page_counter_uncharge(&memcg->memsw, nr_entries);
+               if (!mem_cgroup_is_root(memcg))
+                       page_counter_uncharge(&memcg->memsw, nr_entries);
        }
 
        /*
--

2.11.0

#3 lore raw
Quoted 11 lines · Muchun Song
Quoted 6 lines · Muchun Song

The pages aren't accounted at the root level, so we cannot uncharge the page to the memsw counter for the root memcg. Fix this.

Fixes 1f47b61fb407 ("mm: memcontrol: fix swap counter leak on swapout from offline cgroup")

I am very sorry. I should repent. I suddenly realise the fix is totally wrong. Because the @memcg cannot be root memcg when
@memcg != @swap_memcg.

I am probably blind but I do not see why this would be the case. We have memcg != swap_memcg in this branch but we do not know the neither of the two is root_mem_cgroup, no? If we did knot that we wouldn't have to check for swap_memcg != root_mem_cgroup. Or do I miss something?

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 14 lines · Michal Hocko
Quoted 11 lines · Muchun Song
Quoted 6 lines · Muchun Song

The pages aren't accounted at the root level, so we cannot uncharge the page to the memsw counter for the root memcg. Fix this.

Fixes 1f47b61fb407 ("mm: memcontrol: fix swap counter leak on swapout from offline cgroup")

I am very sorry. I should repent. I suddenly realise the fix is totally wrong. Because the @memcg cannot be root memcg when
@memcg != @swap_memcg.

Quoted 5 lines

I am probably blind but I do not see why this would be the case. We have memcg != swap_memcg in this branch but we do not know the neither of the two is root_mem_cgroup, no? If we did knot that we wouldn't have to check for swap_memcg != root_mem_cgroup. Or do I miss something?

I look at the mem_cgroup_id_get_online() closely. If memcg is root, this function always returns root memcg. So memcg will equal swap_memcg. I apologize for not carefully reviewing the code myself.

Thanks.

Quoted 3 lines

--
Michal Hocko
SUSE Labs

#5 lore raw
Quoted 15 lines · Muchun Song
Quoted 14 lines · Michal Hocko
Quoted 11 lines · Muchun Song
Quoted 6 lines · Muchun Song

The pages aren't accounted at the root level, so we cannot uncharge the page to the memsw counter for the root memcg. Fix this.

Fixes 1f47b61fb407 ("mm: memcontrol: fix swap counter leak on swapout from offline cgroup")

I am very sorry. I should repent. I suddenly realise the fix is totally wrong. Because the @memcg cannot be root memcg when
@memcg != @swap_memcg.

Quoted 8 lines
Quoted 5 lines

I am probably blind but I do not see why this would be the case. We have memcg != swap_memcg in this branch but we do not know the neither of the two is root_mem_cgroup, no? If we did knot that we wouldn't have to check for swap_memcg != root_mem_cgroup. Or do I miss something?

I look at the mem_cgroup_id_get_online() closely. If memcg is root, this function always returns root memcg. So memcg will equal swap_memcg.

Ahh, I can see it now. I have completely missed that the swap_memcg is a parent of an offline memcg. I should have looked more closely.

Signature · 2 lines
Michal Hocko
SUSE Labs