Skip to contentSkip to conversation

net: memcg: late association of sock to memcg

PATCH Superseded Shakeel Butt 2 participants 5 messages Mar 4, 2020 – Mar 5, 2020

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: memcg: late association of sock to memcg

If a TCP socket is allocated in IRQ context or cloned from unassociated (i.e. not associated to a memcg) in IRQ context then it will remain unassociated for its whole life. Almost half of the TCPs created on the system are created in IRQ context, so, memory used by such sockets will not be accounted by the memcg.

This issue is more widespread in cgroup v1 where network memory accounting is opt-in but it can happen in cgroup v2 if the source socket for the cloning was created in root memcg.

To fix the issue, just do the late association of the unassociated sockets at accept() time in the process context and then force charge the memory buffer already reserved by the socket.

---
Changes since v1:
- added sk->sk_rmem_alloc to initial charging.
- added synchronization to get memory usage and set sk_memcg race-free.

 net/ipv4/inet_connection_sock.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/ipv4/inet_connection_sock.c +19 -0
index a4db79b1b643..7bcd657cd45e 100644
@@ -482,6 +482,25 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
 		}
 		spin_unlock_bh(&queue->fastopenq.lock);
 	}
+
+	if (mem_cgroup_sockets_enabled && !newsk->sk_memcg) {
+		int amt;
+
+		/* atomically get the memory usage and set sk->sk_memcg. */
+		lock_sock(newsk);
+
+		/* The sk has not been accepted yet, no need to look at
+		 * sk->sk_wmem_queued.
+		 */
+		amt = sk_mem_pages(newsk->sk_forward_alloc +
+				   atomic_read(&sk->sk_rmem_alloc));
+		mem_cgroup_sk_alloc(newsk);
+
+		release_sock(newsk);
+
+		if (newsk->sk_memcg)
+			mem_cgroup_charge_skmem(newsk->sk_memcg, amt);
+	}
 out:
 	release_sock(sk);
 	if (req)
Signature · 1 line
2.25.0.265.gbab2e86ba0-goog
#1 lore raw
Quoted 39 lines · Shakeel Butt

If a TCP socket is allocated in IRQ context or cloned from unassociated (i.e. not associated to a memcg) in IRQ context then it will remain unassociated for its whole life. Almost half of the TCPs created on the system are created in IRQ context, so, memory used by such sockets will not be accounted by the memcg.

This issue is more widespread in cgroup v1 where network memory accounting is opt-in but it can happen in cgroup v2 if the source socket for the cloning was created in root memcg.

To fix the issue, just do the late association of the unassociated sockets at accept() time in the process context and then force charge the memory buffer already reserved by the socket.

---
Changes since v1:
- added sk->sk_rmem_alloc to initial charging.
- added synchronization to get memory usage and set sk_memcg race-free.

 net/ipv4/inet_connection_sock.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/ipv4/inet_connection_sock.c +7 -0
index a4db79b1b643..7bcd657cd45e 100644
@@ -482,6 +482,25 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
                }
                spin_unlock_bh(&queue->fastopenq.lock);
        }
+
+       if (mem_cgroup_sockets_enabled && !newsk->sk_memcg) {
+               int amt;
+
+               /* atomically get the memory usage and set sk->sk_memcg. */
+               lock_sock(newsk);
+

net/ipv4/inet_connection_sock.c struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern) · line 501

Quoted 10 lines
net/ipv4/inet_connection_sock.c +10 -0
+               /* The sk has not been accepted yet, no need to look at
+                * sk->sk_wmem_queued.
+                */
+               amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                  atomic_read(&sk->sk_rmem_alloc));
+               mem_cgroup_sk_alloc(newsk);
+
+               release_sock(newsk);
+
+               if (newsk->sk_memcg)

Most sockets in accept queue should have amt == 0, so maybe avoid calling this thing only when amt == 0 ?

Also I would release_sock(newsk) after this, otherwise incoming packets could mess with newsk->sk_forward_alloc

if (amt && newsk->sk_memcg)

      mem_cgroup_charge_skmem(newsk->sk_memcg, amt);

release_sock(newsk);

Also, I wonder if mem_cgroup_charge_skmem() has been used at all
these last four years
on arches with PAGE_SIZE != 4096

( SK_MEM_QUANTUM is not anymore PAGE_SIZE, but 4096)

Quoted 8 lines
net/ipv4/inet_connection_sock.c +2 -1
+                       mem_cgroup_charge_skmem(newsk->sk_memcg, amt);
+       }
 out:
        release_sock(sk);
        if (req)
--

2.25.0.265.gbab2e86ba0-goog

#2 lore raw
Quoted 46 lines · Eric Dumazet
Quoted 44 lines · Shakeel Butt

If a TCP socket is allocated in IRQ context or cloned from unassociated (i.e. not associated to a memcg) in IRQ context then it will remain unassociated for its whole life. Almost half of the TCPs created on the system are created in IRQ context, so, memory used by such sockets will not be accounted by the memcg.

This issue is more widespread in cgroup v1 where network memory accounting is opt-in but it can happen in cgroup v2 if the source socket for the cloning was created in root memcg.

To fix the issue, just do the late association of the unassociated sockets at accept() time in the process context and then force charge the memory buffer already reserved by the socket.

---
Changes since v1:
- added sk->sk_rmem_alloc to initial charging.
- added synchronization to get memory usage and set sk_memcg race-free.

 net/ipv4/inet_connection_sock.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/ipv4/inet_connection_sock.c +12 -0
index a4db79b1b643..7bcd657cd45e 100644
@@ -482,6 +482,25 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
                }
                spin_unlock_bh(&queue->fastopenq.lock);
        }
+
+       if (mem_cgroup_sockets_enabled && !newsk->sk_memcg) {
+               int amt;
+
+               /* atomically get the memory usage and set sk->sk_memcg. */
+               lock_sock(newsk);
+
+               /* The sk has not been accepted yet, no need to look at
+                * sk->sk_wmem_queued.
+                */
+               amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                  atomic_read(&sk->sk_rmem_alloc));
Quoted 9 lines

net/ipv4/inet_connection_sock.c struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern) · line 501

Quoted 5 lines
net/ipv4/inet_connection_sock.c +5 -0
+               mem_cgroup_sk_alloc(newsk);
+
+               release_sock(newsk);
+
+               if (newsk->sk_memcg)

Most sockets in accept queue should have amt == 0, so maybe avoid calling this thing only when amt == 0 ?

Thanks, will do in the next version. BTW I have tested with adding mdelay() here and running iperf3 and I did see non-zero amt.

Quoted 3 lines

Also I would release_sock(newsk) after this, otherwise incoming packets could mess with newsk->sk_forward_alloc

I think that is fine. Once sk->sk_memcg is set then mem_cgroup_charge_skmem() will be called for new incoming packets. Here we just need to call mem_cgroup_charge_skmem() with amt before sk->sk_memcg was set.

Quoted 10 lines

if (amt && newsk->sk_memcg)

      mem_cgroup_charge_skmem(newsk->sk_memcg, amt);

release_sock(newsk);

Also, I wonder if mem_cgroup_charge_skmem() has been used at all
these last four years
on arches with PAGE_SIZE != 4096

( SK_MEM_QUANTUM is not anymore PAGE_SIZE, but 4096)

Oh so sk_mem_pages() does not really give the number of pages. Yeah this needs a fix for non-4906 page size architectures. Though I can understand why this has not been caught yet. Network memory accounting is opt-in in cgroup v1 and most of the users still use v1. In cgroup v2, it is enabled and there is no way to opt-out. Facebook is a well-known v2 user and it seems like they don't have non-4096 page size arch systems.

#3 lore raw
Quoted 61 lines · Shakeel Butt
Quoted 46 lines · Eric Dumazet
Quoted 44 lines · Shakeel Butt

If a TCP socket is allocated in IRQ context or cloned from unassociated (i.e. not associated to a memcg) in IRQ context then it will remain unassociated for its whole life. Almost half of the TCPs created on the system are created in IRQ context, so, memory used by such sockets will not be accounted by the memcg.

This issue is more widespread in cgroup v1 where network memory accounting is opt-in but it can happen in cgroup v2 if the source socket for the cloning was created in root memcg.

To fix the issue, just do the late association of the unassociated sockets at accept() time in the process context and then force charge the memory buffer already reserved by the socket.

---
Changes since v1:
- added sk->sk_rmem_alloc to initial charging.
- added synchronization to get memory usage and set sk_memcg race-free.

 net/ipv4/inet_connection_sock.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/ipv4/inet_connection_sock.c +12 -0
index a4db79b1b643..7bcd657cd45e 100644
@@ -482,6 +482,25 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
                }
                spin_unlock_bh(&queue->fastopenq.lock);
        }
+
+       if (mem_cgroup_sockets_enabled && !newsk->sk_memcg) {
+               int amt;
+
+               /* atomically get the memory usage and set sk->sk_memcg. */
+               lock_sock(newsk);
+
+               /* The sk has not been accepted yet, no need to look at
+                * sk->sk_wmem_queued.
+                */
+               amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                  atomic_read(&sk->sk_rmem_alloc));
Quoted 9 lines

net/ipv4/inet_connection_sock.c struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern) · line 501

Quoted 5 lines
net/ipv4/inet_connection_sock.c +5 -0
+               mem_cgroup_sk_alloc(newsk);
+
+               release_sock(newsk);
+
+               if (newsk->sk_memcg)

Most sockets in accept queue should have amt == 0, so maybe avoid calling this thing only when amt == 0 ?

Thanks, will do in the next version. BTW I have tested with adding mdelay() here and running iperf3 and I did see non-zero amt.

Quoted 8 lines
Quoted 3 lines

Also I would release_sock(newsk) after this, otherwise incoming packets could mess with newsk->sk_forward_alloc

I think that is fine. Once sk->sk_memcg is set then mem_cgroup_charge_skmem() will be called for new incoming packets. Here we just need to call mem_cgroup_charge_skmem() with amt before sk->sk_memcg was set.

Unfortunately, as soon as release_sock(newsk) is done, incoming packets can be fed to the socket,
and completely change memory usage of the socket.

For example, the whole queue might have been zapped, or collapsed, if we receive a RST packet,
or if memory pressure asks us to prune the out of order queue.

So you might charge something, then never uncharge it, since at close() time the socket will have zero bytes to uncharge.

Quoted 19 lines
Quoted 11 lines

if (amt && newsk->sk_memcg)

      mem_cgroup_charge_skmem(newsk->sk_memcg, amt);

release_sock(newsk);

Also, I wonder if mem_cgroup_charge_skmem() has been used at all
these last four years
on arches with PAGE_SIZE != 4096

( SK_MEM_QUANTUM is not anymore PAGE_SIZE, but 4096)

Oh so sk_mem_pages() does not really give the number of pages. Yeah this needs a fix for non-4906 page size architectures. Though I can understand why this has not been caught yet. Network memory accounting is opt-in in cgroup v1 and most of the users still use v1. In cgroup v2, it is enabled and there is no way to opt-out. Facebook is a well-known v2 user and it seems like they don't have non-4096 page size arch systems.

#4 lore raw
Quoted 77 lines · Eric Dumazet
Quoted 61 lines · Shakeel Butt
Quoted 46 lines · Eric Dumazet
Quoted 44 lines · Shakeel Butt

If a TCP socket is allocated in IRQ context or cloned from unassociated (i.e. not associated to a memcg) in IRQ context then it will remain unassociated for its whole life. Almost half of the TCPs created on the system are created in IRQ context, so, memory used by such sockets will not be accounted by the memcg.

This issue is more widespread in cgroup v1 where network memory accounting is opt-in but it can happen in cgroup v2 if the source socket for the cloning was created in root memcg.

To fix the issue, just do the late association of the unassociated sockets at accept() time in the process context and then force charge the memory buffer already reserved by the socket.

---
Changes since v1:
- added sk->sk_rmem_alloc to initial charging.
- added synchronization to get memory usage and set sk_memcg race-free.

 net/ipv4/inet_connection_sock.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
net/ipv4/inet_connection_sock.c +12 -0
index a4db79b1b643..7bcd657cd45e 100644
@@ -482,6 +482,25 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
                }
                spin_unlock_bh(&queue->fastopenq.lock);
        }
+
+       if (mem_cgroup_sockets_enabled && !newsk->sk_memcg) {
+               int amt;
+
+               /* atomically get the memory usage and set sk->sk_memcg. */
+               lock_sock(newsk);
+
+               /* The sk has not been accepted yet, no need to look at
+                * sk->sk_wmem_queued.
+                */
+               amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                  atomic_read(&sk->sk_rmem_alloc));
Quoted 9 lines

net/ipv4/inet_connection_sock.c struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern) · line 501

Quoted 5 lines
net/ipv4/inet_connection_sock.c +5 -0
+               mem_cgroup_sk_alloc(newsk);
+
+               release_sock(newsk);
+
+               if (newsk->sk_memcg)

Most sockets in accept queue should have amt == 0, so maybe avoid calling this thing only when amt == 0 ?

Thanks, will do in the next version. BTW I have tested with adding mdelay() here and running iperf3 and I did see non-zero amt.

Quoted 8 lines
Quoted 3 lines

Also I would release_sock(newsk) after this, otherwise incoming packets could mess with newsk->sk_forward_alloc

I think that is fine. Once sk->sk_memcg is set then mem_cgroup_charge_skmem() will be called for new incoming packets. Here we just need to call mem_cgroup_charge_skmem() with amt before sk->sk_memcg was set.

Unfortunately, as soon as release_sock(newsk) is done, incoming packets can be fed to the socket,
and completely change memory usage of the socket.

Quoted 7 lines

For example, the whole queue might have been zapped, or collapsed, if we receive a RST packet,
or if memory pressure asks us to prune the out of order queue.

So you might charge something, then never uncharge it, since at close() time the socket will have zero bytes to uncharge.

Ok, thanks for the explanation. I will fix this in the next version.