]> www.infradead.org Git - users/willy/linux.git/commitdiff
net-memcg: Introduce mem_cgroup_from_sk().
authorKuniyuki Iwashima <kuniyu@google.com>
Fri, 15 Aug 2025 20:16:14 +0000 (20:16 +0000)
committerJakub Kicinski <kuba@kernel.org>
Wed, 20 Aug 2025 02:20:59 +0000 (19:20 -0700)
We will store a flag in the lowest bit of sk->sk_memcg.

Then, directly dereferencing sk->sk_memcg will be illegal, and we
do not want to allow touching the raw sk->sk_memcg in many places.

Let's introduce mem_cgroup_from_sk().

Other places accessing the raw sk->sk_memcg will be converted later.

Note that we cannot define the helper as an inline function in
memcontrol.h as we cannot access any fields of struct sock there
due to circular dependency, so it is placed in sock.h.

Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
Acked-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://patch.msgid.link/20250815201712.1745332-7-kuniyu@google.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/net/sock.h
mm/memcontrol.c
net/ipv4/inet_connection_sock.c

index c8a4b283df6fc4b931270502ddbb5df7ae1e4aa2..811f95ea8d00cee3029432be104a2dad7bec4767 100644 (file)
@@ -2594,6 +2594,18 @@ static inline gfp_t gfp_memcg_charge(void)
        return in_softirq() ? GFP_ATOMIC : GFP_KERNEL;
 }
 
+#ifdef CONFIG_MEMCG
+static inline struct mem_cgroup *mem_cgroup_from_sk(const struct sock *sk)
+{
+       return sk->sk_memcg;
+}
+#else
+static inline struct mem_cgroup *mem_cgroup_from_sk(const struct sock *sk)
+{
+       return NULL;
+}
+#endif
+
 static inline long sock_rcvtimeo(const struct sock *sk, bool noblock)
 {
        return noblock ? 0 : READ_ONCE(sk->sk_rcvtimeo);
index 46713b9ece063870767f838e0e488b7ee5a39d05..d8a52d1d08fa47857bd4e73db21093809694e8be 100644 (file)
@@ -5020,19 +5020,24 @@ out:
 
 void mem_cgroup_sk_free(struct sock *sk)
 {
-       if (sk->sk_memcg)
-               css_put(&sk->sk_memcg->css);
+       struct mem_cgroup *memcg = mem_cgroup_from_sk(sk);
+
+       if (memcg)
+               css_put(&memcg->css);
 }
 
 void mem_cgroup_sk_inherit(const struct sock *sk, struct sock *newsk)
 {
+       struct mem_cgroup *memcg;
+
        if (sk->sk_memcg == newsk->sk_memcg)
                return;
 
        mem_cgroup_sk_free(newsk);
 
-       if (sk->sk_memcg)
-               css_get(&sk->sk_memcg->css);
+       memcg = mem_cgroup_from_sk(sk);
+       if (memcg)
+               css_get(&memcg->css);
 
        newsk->sk_memcg = sk->sk_memcg;
 }
index 724bd9ed6cd48068331cfeb87d0f4e2895bd2923..93569bbe00f442ffbca57ee88c07e189cd936dc9 100644 (file)
@@ -718,7 +718,7 @@ struct sock *inet_csk_accept(struct sock *sk, struct proto_accept_arg *arg)
                lock_sock(newsk);
 
                mem_cgroup_sk_alloc(newsk);
-               if (newsk->sk_memcg) {
+               if (mem_cgroup_from_sk(newsk)) {
                        /* The socket has not been accepted yet, no need
                         * to look at newsk->sk_wmem_queued.
                         */