net: Clean up __sk_mem_raise_allocated().
authorKuniyuki Iwashima <kuniyu@google.com>
Fri, 15 Aug 2025 20:16:13 +0000 (20:16 +0000)
committerJakub Kicinski <kuba@kernel.org>
Wed, 20 Aug 2025 02:20:59 +0000 (19:20 -0700)
In __sk_mem_raise_allocated(), charged is initialised as true due
to the weird condition removed in the previous patch.

It makes the variable unreliable by itself, so we have to check
another variable, memcg, in advance.

Also, we will factorise the common check below for memcg later.

    if (mem_cgroup_sockets_enabled && sk->sk_memcg)

As a prep, let's initialise charged as false and memcg as NULL.

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20250815201712.1745332-6-kuniyu@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/core/sock.c

index 380bc1a..000940e 100644 (file)
@@ -3263,15 +3263,16 @@ EXPORT_SYMBOL(sk_wait_data);
  */
 int __sk_mem_raise_allocated(struct sock *sk, int size, int amt, int kind)
 {
-       struct mem_cgroup *memcg = mem_cgroup_sockets_enabled ? sk->sk_memcg : NULL;
        struct proto *prot = sk->sk_prot;
-       bool charged = true;
+       struct mem_cgroup *memcg = NULL;
+       bool charged = false;
        long allocated;
 
        sk_memory_allocated_add(sk, amt);
        allocated = sk_memory_allocated(sk);
 
-       if (memcg) {
+       if (mem_cgroup_sockets_enabled && sk->sk_memcg) {
+               memcg = sk->sk_memcg;
                charged = mem_cgroup_charge_skmem(memcg, amt, gfp_memcg_charge());
                if (!charged)
                        goto suppress_allocation;
@@ -3358,7 +3359,7 @@ suppress_allocation:
 
        sk_memory_allocated_sub(sk, amt);
 
-       if (memcg && charged)
+       if (charged)
                mem_cgroup_uncharge_skmem(memcg, amt);
 
        return 0;