Re: [PATCH v4 bpf-next/net 1/5] tcp: Save lock_sock() for memcg in inet_csk_accept().
From: Kuniyuki Iwashima <kuniyu@google.com>
Date: 2025-09-02 19:32:18
Also in:
bpf
On Tue, Sep 2, 2025 at 11:55 AM Martin KaFai Lau [off-list ref] wrote:
On 8/28/25 6:00 PM, Kuniyuki Iwashima wrote:quoted
mem_cgroup_sk_alloc() is called for SCTP before __inet_accept(), so I added the protocol check in __inet_accept(), but this can be removed once SCTP uses sk_clone_lock().quoted
void __inet_accept(struct socket *sock, struct socket *newsock, struct sock *newsk) { + /* TODO: use sk_clone_lock() in SCTP and remove protocol checks */ + if (mem_cgroup_sockets_enabled && + (!IS_ENABLED(CONFIG_IP_SCTP) || + sk_is_tcp(newsk) || sk_is_mptcp(newsk))) {Instead of protocol check, is it the same as checking "if (mem_cgroup_sockets_enabled && !mem_cgroup_from_sk(newsk))"
sk_memcg could be NULL when the socket is belongs to the root memcg, and then we shouldn't call kmem_cache_charge() for SCTP. Also, SCTP's child sockets are not supported until we convert them to sk_clone_lock(), and I plan to do so after this series lands on net-next. https://github.com/q2ven/linux/commits/522_sctp_sk_clone_lock/
quoted
+ gfp_t gfp = GFP_KERNEL | __GFP_NOFAIL; + + mem_cgroup_sk_alloc(newsk); + + if (mem_cgroup_from_sk(newsk)) { + int amt; + + /* 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_sk_charge(newsk, amt, gfp); + } + + kmem_cache_charge(newsk, gfp); + } +