Skip to contentSkip to conversation

cgroup: memcg: net: do not associate sock with unrelated cgroup

PATCH Superseded Shakeel Butt 3 participants 8 messages Feb 14, 2020 – Feb 15, 2020

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cgroup: memcg: net: do not associate sock with unrelated cgroup

We are testing network memory accounting in our setup and noticed inconsistent network memory usage and often unrelated cgroups network usage correlates with testing workload. On further inspection, it seems like mem_cgroup_sk_alloc() and cgroup_sk_alloc() are broken in irq context specially for cgroup v1.

mem_cgroup_sk_alloc() and cgroup_sk_alloc() can be called in irq context and kind of assumes that this can only happen from sk_clone_lock() and the source sock object has already associated cgroup. However in cgroup v1, where network memory accounting is opt-in, the source sock can be unassociated with any cgroup and the new cloned sock can get associated with unrelated interrupted cgroup.

Cgroup v2 can also suffer if the source sock object was created by process in the root cgroup or if sk_alloc() is called in irq context. The fix is to just do nothing in interrupt.

Fixes 2d7580738345 ("mm: memcontrol: consolidate cgroup socket tracking")
Fixes d979a39d7242 ("cgroup: duplicate cgroup reference when cloning sockets")

---

Changes since v1:
- Fix cgroup_sk_alloc() too.

 kernel/cgroup/cgroup.c | 4 ++++
 mm/memcontrol.c        | 4 ++++
 2 files changed, 8 insertions(+)
kernel/cgroup/cgroup.c +4 -0
index 9a8a5ded3c48..46e5f5518fba 100644
@@ -6449,6 +6449,10 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
 		return;
 	}
 
+	/* Do not associate the sock with unrelated interrupted task's memcg. */
+	if (in_interrupt())
+		return;
+
 	rcu_read_lock();
 
 	while (true) {
mm/memcontrol.c +4 -0
index 63bb6a2aab81..f500da82bfe8 100644
@@ -6697,6 +6697,10 @@ void mem_cgroup_sk_alloc(struct sock *sk)
 		return;
 	}
 
+	/* Do not associate the sock with unrelated interrupted task's memcg. */
+	if (in_interrupt())
+		return;
+
 	rcu_read_lock();
 	memcg = mem_cgroup_from_task(current);
 	if (memcg == root_mem_cgroup)
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
Reviewed-by on v2 1/1
#1 lore raw
Quoted 34 lines · Shakeel Butt

We are testing network memory accounting in our setup and noticed inconsistent network memory usage and often unrelated cgroups network usage correlates with testing workload. On further inspection, it seems like mem_cgroup_sk_alloc() and cgroup_sk_alloc() are broken in irq context specially for cgroup v1.

mem_cgroup_sk_alloc() and cgroup_sk_alloc() can be called in irq context and kind of assumes that this can only happen from sk_clone_lock() and the source sock object has already associated cgroup. However in cgroup v1, where network memory accounting is opt-in, the source sock can be unassociated with any cgroup and the new cloned sock can get associated with unrelated interrupted cgroup.

Cgroup v2 can also suffer if the source sock object was created by process in the root cgroup or if sk_alloc() is called in irq context. The fix is to just do nothing in interrupt.

Fixes 2d7580738345 ("mm: memcontrol: consolidate cgroup socket tracking")
Fixes d979a39d7242 ("cgroup: duplicate cgroup reference when cloning sockets")

---

Changes since v1:
- Fix cgroup_sk_alloc() too.

 kernel/cgroup/cgroup.c | 4 ++++
 mm/memcontrol.c        | 4 ++++
 2 files changed, 8 insertions(+)
kernel/cgroup/cgroup.c +0 -0
index 9a8a5ded3c48..46e5f5518fba 100644

kernel/cgroup/cgroup.c void cgroup_sk_alloc(struct sock_cgroup_data *skcd) · line 6452

Quoted 4 lines
kernel/cgroup/cgroup.c +1 -0
@@ -6449,6 +6449,10 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
 		return;
 	}
 
+	/* Do not associate the sock with unrelated interrupted task's memcg. */
                                                                       ^^^^^
								       cgroup?

mm/memcontrol.c void mem_cgroup_sk_alloc(struct sock *sk) · line 6698

Quoted 13 lines
kernel/cgroup/cgroup.c +3 -0
+	if (in_interrupt())
+		return;
+
 	rcu_read_lock();
 
 	while (true) {
mm/memcontrol.c +0 -0
index 63bb6a2aab81..f500da82bfe8 100644
@@ -6697,6 +6697,10 @@ void mem_cgroup_sk_alloc(struct sock *sk)
 		return;
 	}

Can you, please, include the stacktrace into the commit log? Except a minor typo (see above),

A really good catch.

Thank you!

#2 lore raw
Quoted 58 lines · Roman Gushchin
Quoted 34 lines · Shakeel Butt

We are testing network memory accounting in our setup and noticed inconsistent network memory usage and often unrelated cgroups network usage correlates with testing workload. On further inspection, it seems like mem_cgroup_sk_alloc() and cgroup_sk_alloc() are broken in irq context specially for cgroup v1.

mem_cgroup_sk_alloc() and cgroup_sk_alloc() can be called in irq context and kind of assumes that this can only happen from sk_clone_lock() and the source sock object has already associated cgroup. However in cgroup v1, where network memory accounting is opt-in, the source sock can be unassociated with any cgroup and the new cloned sock can get associated with unrelated interrupted cgroup.

Cgroup v2 can also suffer if the source sock object was created by process in the root cgroup or if sk_alloc() is called in irq context. The fix is to just do nothing in interrupt.

Fixes 2d7580738345 ("mm: memcontrol: consolidate cgroup socket tracking")
Fixes d979a39d7242 ("cgroup: duplicate cgroup reference when cloning sockets")

---

Changes since v1:
- Fix cgroup_sk_alloc() too.

 kernel/cgroup/cgroup.c | 4 ++++
 mm/memcontrol.c        | 4 ++++
 2 files changed, 8 insertions(+)
kernel/cgroup/cgroup.c +0 -0
index 9a8a5ded3c48..46e5f5518fba 100644

kernel/cgroup/cgroup.c void cgroup_sk_alloc(struct sock_cgroup_data *skcd) · line 6452

Quoted 4 lines
kernel/cgroup/cgroup.c +1 -0
@@ -6449,6 +6449,10 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
              return;
      }
 
+     /* Do not associate the sock with unrelated interrupted task's memcg. */
                                                                       ^^^^^
                                                                       cgroup?

mm/memcontrol.c void mem_cgroup_sk_alloc(struct sock *sk) · line 6698

Quoted 13 lines
kernel/cgroup/cgroup.c +3 -0
+     if (in_interrupt())
+             return;
+
      rcu_read_lock();
 
      while (true) {
mm/memcontrol.c +0 -0
index 63bb6a2aab81..f500da82bfe8 100644
@@ -6697,6 +6697,10 @@ void mem_cgroup_sk_alloc(struct sock *sk)
              return;
      }

Can you, please, include the stacktrace into the commit log? Except a minor typo (see above),

Quoted 4 lines

A really good catch.

Thanks, I will add the stack trace and fix the typo.

Shakeel

#3 lore raw
In reply to Shakeel Butt
Quoted 14 lines · Shakeel Butt

We are testing network memory accounting in our setup and noticed inconsistent network memory usage and often unrelated cgroups network usage correlates with testing workload. On further inspection, it seems like mem_cgroup_sk_alloc() and cgroup_sk_alloc() are broken in irq context specially for cgroup v1.

mem_cgroup_sk_alloc() and cgroup_sk_alloc() can be called in irq context and kind of assumes that this can only happen from sk_clone_lock() and the source sock object has already associated cgroup. However in cgroup v1, where network memory accounting is opt-in, the source sock can be unassociated with any cgroup and the new cloned sock can get associated with unrelated interrupted cgroup.

Quoted 3 lines

Cgroup v2 can also suffer if the source sock object was created by process in the root cgroup or if sk_alloc() is called in irq context. The fix is to just do nothing in interrupt.

So, when will the association be done ?
At accept() time ?
Is it done already ?

Thanks

Quoted 46 lines
Fixes 2d7580738345 ("mm: memcontrol: consolidate cgroup socket tracking")
Fixes d979a39d7242 ("cgroup: duplicate cgroup reference when cloning sockets")

---

Changes since v1:
- Fix cgroup_sk_alloc() too.

 kernel/cgroup/cgroup.c | 4 ++++
 mm/memcontrol.c        | 4 ++++
 2 files changed, 8 insertions(+)
kernel/cgroup/cgroup.c +4 -0
index 9a8a5ded3c48..46e5f5518fba 100644
@@ -6449,6 +6449,10 @@ void cgroup_sk_alloc(struct sock_cgroup_data *skcd)
                return;
        }
 
+       /* Do not associate the sock with unrelated interrupted task's memcg. */
+       if (in_interrupt())
+               return;
+
        rcu_read_lock();
 
        while (true) {
mm/memcontrol.c +4 -1
index 63bb6a2aab81..f500da82bfe8 100644
@@ -6697,6 +6697,10 @@ void mem_cgroup_sk_alloc(struct sock *sk)
                return;
        }
 
+       /* Do not associate the sock with unrelated interrupted task's memcg. */
+       if (in_interrupt())
+               return;
+
        rcu_read_lock();
        memcg = mem_cgroup_from_task(current);
        if (memcg == root_mem_cgroup)
--

2.25.0.265.gbab2e86ba0-goog

#4 lore raw
Quoted 16 lines · Eric Dumazet
Quoted 14 lines · Shakeel Butt

We are testing network memory accounting in our setup and noticed inconsistent network memory usage and often unrelated cgroups network usage correlates with testing workload. On further inspection, it seems like mem_cgroup_sk_alloc() and cgroup_sk_alloc() are broken in irq context specially for cgroup v1.

mem_cgroup_sk_alloc() and cgroup_sk_alloc() can be called in irq context and kind of assumes that this can only happen from sk_clone_lock() and the source sock object has already associated cgroup. However in cgroup v1, where network memory accounting is opt-in, the source sock can be unassociated with any cgroup and the new cloned sock can get associated with unrelated interrupted cgroup.

Quoted 8 lines
Quoted 3 lines

Cgroup v2 can also suffer if the source sock object was created by process in the root cgroup or if sk_alloc() is called in irq context. The fix is to just do nothing in interrupt.

So, when will the association be done ?
At accept() time ?
Is it done already ?

I think in the current code if the association is skipped at allocation time then the sock will remain unassociated for its lifetime.

Maybe we can add the association in the later stages but it seems like it is not a simple task i.e. edbe69ef2c90f ("Revert "defer call to mem_cgroup_sk_alloc()"").

Shakeel

#5 lore raw
Quoted 8 lines · Shakeel Butt

I think in the current code if the association is skipped at allocation time then the sock will remain unassociated for its lifetime.

Maybe we can add the association in the later stages but it seems like it is not a simple task i.e. edbe69ef2c90f ("Revert "defer call to mem_cgroup_sk_alloc()"").

Half TCP sockets are passive, so this means that 50% of TCP sockets won't be charged.
(the socket cloning always happens from BH context)

I think this deserves a comment in the changelog or documentation, otherwise some people might think
using memcg will make them safe.

#6 lore raw
Quoted 12 lines · Eric Dumazet
Quoted 8 lines · Shakeel Butt

I think in the current code if the association is skipped at allocation time then the sock will remain unassociated for its lifetime.

Maybe we can add the association in the later stages but it seems like it is not a simple task i.e. edbe69ef2c90f ("Revert "defer call to mem_cgroup_sk_alloc()"").

Quoted 7 lines

Half TCP sockets are passive, so this means that 50% of TCP sockets won't be charged.
(the socket cloning always happens from BH context)

I think this deserves a comment in the changelog or documentation, otherwise some people might think
using memcg will make them safe.

Thanks I will update the changelog. Also is inet_csk_accept() the right place for delayed cgroup/memcg binding (if we decide to do that). I am wondering if we can force charge the memcg during late binding to cater the issue fixed in edbe69ef2c90f.

Shakeel

#7 lore raw
Quoted 18 lines · Shakeel Butt
Quoted 16 lines · Eric Dumazet
Quoted 8 lines · Shakeel Butt

I think in the current code if the association is skipped at allocation time then the sock will remain unassociated for its lifetime.

Maybe we can add the association in the later stages but it seems like it is not a simple task i.e. edbe69ef2c90f ("Revert "defer call to mem_cgroup_sk_alloc()"").

Half TCP sockets are passive, so this means that 50% of TCP sockets won't be charged.
(the socket cloning always happens from BH context)

Quoted 9 lines
Quoted 3 lines

I think this deserves a comment in the changelog or documentation, otherwise some people might think
using memcg will make them safe.

Thanks I will update the changelog. Also is inet_csk_accept() the right place for delayed cgroup/memcg binding (if we decide to do that). I am wondering if we can force charge the memcg during late binding to cater the issue fixed in edbe69ef2c90f.

Yes, this is exactly why accept() would be the natural choice.

You do not want to test/change the binding at sendmsg()/recvmsg() time, right ?