Skip to contentSkip to conversation

inet: Save one atomic op if no memcg to charge

PATCH In review Abel Wu 2 participants 5 messages Jun 19, 2023 – Jun 20, 2023

Subsystems (3)
lore
Share & tools
lore

Abel Wu

author
#0 lore raw

PATCH 1/1 inet: Save one atomic op if no memcg to charge

If there is no net-memcg associated with the sock, don't bother calculating its memory usage for charge.

---
 net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
net/ipv4/inet_connection_sock.c +11 -7
index 65ad4251f6fd..73798282c1ef 100644
@@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
 out:
 	release_sock(sk);
 	if (newsk && mem_cgroup_sockets_enabled) {
-		int amt;
+		int amt = 0;
 
 		/* atomically get the memory usage, set and charge the
 		 * newsk->sk_memcg.
 		 */
 		lock_sock(newsk);
 
-		/* The socket has not been accepted yet, no need to look at
-		 * newsk->sk_wmem_queued.
-		 */
-		amt = sk_mem_pages(newsk->sk_forward_alloc +
-				   atomic_read(&newsk->sk_rmem_alloc));
 		mem_cgroup_sk_alloc(newsk);
-		if (newsk->sk_memcg && amt)
+		if (newsk->sk_memcg) {
+			/* The socket has not been accepted yet, no need
+			 * to look at newsk->sk_wmem_queued.
+			 */
+			amt = sk_mem_pages(newsk->sk_forward_alloc +
+					   atomic_read(&newsk->sk_rmem_alloc));
+
+		}
+
+		if (amt)
 			mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
 						GFP_KERNEL | __GFP_NOFAIL);
 
Signature · 1 line
2.37.3
#1 lore raw
Quoted 34 lines · Abel Wu

If there is no net-memcg associated with the sock, don't bother calculating its memory usage for charge.

---
 net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
net/ipv4/inet_connection_sock.c +3 -7
index 65ad4251f6fd..73798282c1ef 100644
@@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
 out:
        release_sock(sk);
        if (newsk && mem_cgroup_sockets_enabled) {
-               int amt;
+               int amt = 0;
 
                /* atomically get the memory usage, set and charge the
                 * newsk->sk_memcg.
                 */
                lock_sock(newsk);
 
-               /* The socket has not been accepted yet, no need to look at
-                * newsk->sk_wmem_queued.
-                */
-               amt = sk_mem_pages(newsk->sk_forward_alloc +
-                                  atomic_read(&newsk->sk_rmem_alloc));
                mem_cgroup_sk_alloc(newsk);
-               if (newsk->sk_memcg && amt)
+               if (newsk->sk_memcg) {
+                       /* The socket has not been accepted yet, no need

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

Quoted 10 lines
net/ipv4/inet_connection_sock.c +8 -0
+                        * to look at newsk->sk_wmem_queued.
+                        */
+                       amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                          atomic_read(&newsk->sk_rmem_alloc));
+
+               }
+
+               if (amt)
                        mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
                                                GFP_KERNEL | __GFP_NOFAIL);

This looks correct, but claiming reading an atomic_t is an 'atomic op' is a bit exaggerated.

author
#2 lore raw
Quoted 40 lines · Eric Dumazet
Quoted 39 lines · Abel Wu

If there is no net-memcg associated with the sock, don't bother calculating its memory usage for charge.

---
  net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
  1 file changed, 11 insertions(+), 7 deletions(-)
net/ipv4/inet_connection_sock.c +8 -7
index 65ad4251f6fd..73798282c1ef 100644
@@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
  out:
         release_sock(sk);
         if (newsk && mem_cgroup_sockets_enabled) {
-               int amt;
+               int amt = 0;
 
                 /* atomically get the memory usage, set and charge the
                  * newsk->sk_memcg.
                  */
                 lock_sock(newsk);
 
-               /* The socket has not been accepted yet, no need to look at
-                * newsk->sk_wmem_queued.
-                */
-               amt = sk_mem_pages(newsk->sk_forward_alloc +
-                                  atomic_read(&newsk->sk_rmem_alloc));
                 mem_cgroup_sk_alloc(newsk);
-               if (newsk->sk_memcg && amt)
+               if (newsk->sk_memcg) {
+                       /* The socket has not been accepted yet, no need
+                        * to look at newsk->sk_wmem_queued.
+                        */
+                       amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                          atomic_read(&newsk->sk_rmem_alloc));
+
Quoted 8 lines

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

Quoted 5 lines
net/ipv4/inet_connection_sock.c +3 -0
+               }
+
+               if (amt)
                         mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
                                                 GFP_KERNEL | __GFP_NOFAIL);

This looks correct, but claiming reading an atomic_t is an 'atomic op' is a bit exaggerated.

Yeah, shall I change subject to 'inet: Skip usage calculation if no memcg to charge'? Or do you have any suggestions?

Thanks,
Abel

#3 lore raw
Quoted 51 lines · Abel Wu
Quoted 40 lines · Eric Dumazet
Quoted 39 lines · Abel Wu

If there is no net-memcg associated with the sock, don't bother calculating its memory usage for charge.

---
  net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
  1 file changed, 11 insertions(+), 7 deletions(-)
net/ipv4/inet_connection_sock.c +8 -7
index 65ad4251f6fd..73798282c1ef 100644
@@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
  out:
         release_sock(sk);
         if (newsk && mem_cgroup_sockets_enabled) {
-               int amt;
+               int amt = 0;
 
                 /* atomically get the memory usage, set and charge the
                  * newsk->sk_memcg.
                  */
                 lock_sock(newsk);
 
-               /* The socket has not been accepted yet, no need to look at
-                * newsk->sk_wmem_queued.
-                */
-               amt = sk_mem_pages(newsk->sk_forward_alloc +
-                                  atomic_read(&newsk->sk_rmem_alloc));
                 mem_cgroup_sk_alloc(newsk);
-               if (newsk->sk_memcg && amt)
+               if (newsk->sk_memcg) {
+                       /* The socket has not been accepted yet, no need
+                        * to look at newsk->sk_wmem_queued.
+                        */
+                       amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                          atomic_read(&newsk->sk_rmem_alloc));
+
Quoted 8 lines

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

Quoted 5 lines
net/ipv4/inet_connection_sock.c +3 -0
+               }
+
+               if (amt)
                         mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
                                                 GFP_KERNEL | __GFP_NOFAIL);

This looks correct, but claiming reading an atomic_t is an 'atomic op' is a bit exaggerated.

Quoted 2 lines

Yeah, shall I change subject to 'inet: Skip usage calculation if no memcg to charge'? Or do you have any suggestions?

I would call this a cleanup or refactoring, maybe...

author
#4 lore raw
Quoted 52 lines · Eric Dumazet
Quoted 51 lines · Abel Wu
Quoted 48 lines · Eric Dumazet
Quoted 34 lines · Abel Wu

If there is no net-memcg associated with the sock, don't bother calculating its memory usage for charge.

---
   net/ipv4/inet_connection_sock.c | 18 +++++++++++-------
   1 file changed, 11 insertions(+), 7 deletions(-)
net/ipv4/inet_connection_sock.c +3 -7
index 65ad4251f6fd..73798282c1ef 100644
@@ -706,20 +706,24 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err, bool kern)
   out:
          release_sock(sk);
          if (newsk && mem_cgroup_sockets_enabled) {
-               int amt;
+               int amt = 0;
 
                  /* atomically get the memory usage, set and charge the
                   * newsk->sk_memcg.
                   */
                  lock_sock(newsk);
 
-               /* The socket has not been accepted yet, no need to look at
-                * newsk->sk_wmem_queued.
-                */
-               amt = sk_mem_pages(newsk->sk_forward_alloc +
-                                  atomic_read(&newsk->sk_rmem_alloc));
                  mem_cgroup_sk_alloc(newsk);
-               if (newsk->sk_memcg && amt)
+               if (newsk->sk_memcg) {
+                       /* The socket has not been accepted yet, no need

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

Quoted 10 lines
net/ipv4/inet_connection_sock.c +8 -0
+                        * to look at newsk->sk_wmem_queued.
+                        */
+                       amt = sk_mem_pages(newsk->sk_forward_alloc +
+                                          atomic_read(&newsk->sk_rmem_alloc));
+
+               }
+
+               if (amt)
                          mem_cgroup_charge_skmem(newsk->sk_memcg, amt,
                                                  GFP_KERNEL | __GFP_NOFAIL);

This looks correct, but claiming reading an atomic_t is an 'atomic op' is a bit exaggerated.

Quoted 4 lines
Quoted 2 lines

Yeah, shall I change subject to 'inet: Skip usage calculation if no memcg to charge'? Or do you have any suggestions?

I would call this a cleanup or refactoring, maybe...

Alright, I have changed to 'cleanup', please take a look at v2.

Yet I have another question about this condition:

	'if (newsk && mem_cgroup_sockets_enabled)'

IMHO in the scope of cgroup v1, 'mem_cgroup_sockets_enabled' doesn't imply socket accounting enabled for current's memcg. As the listening socket and the newly accepted socket are processing same traffic, can we make this condition more specific like this:

	'if (newsk && mem_cgroup_sockets_enabled && sk->sk_memcg)'

would you mind shedding some light please?

Thanks!
Abel