Re: [PATCH v4 2/2] net: memcg: late association of sock to memcg

From: Qian Cai
Date: Thu Mar 12 2020 - 10:03:40 EST


On Mon, 2020-03-09 at 22:16 -0700, Shakeel Butt wrote:
> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
> index a4db79b1b643..65a3b2565102 100644
> --- a/net/ipv4/inet_connection_sock.c
> +++ b/net/ipv4/inet_connection_sock.c
> @@ -482,6 +482,26 @@ 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) {
> + int amt;
> +
> + /* atomically get the memory usage, set and charge the
> + * sk->sk_memcg.
> + */
> + lock_sock(newsk);

Here we have a deadlock,

[ÂÂ362.620977][ T4106] WARNING: possible recursive locking detected
[ÂÂ362.626983][ T4106] 5.6.0-rc5-next-20200312+ #5 Tainted: GÂÂÂÂÂÂÂÂÂÂÂÂÂLÂÂÂ
[ÂÂ362.633941][ T4106] --------------------------------------------
[ÂÂ362.639944][ T4106] sshd/4106 is trying to acquire lock:
[ÂÂ362.645251][ T4106] 7bff008a2eae6330 (sk_lock-AF_INET){+.+.}, at:
inet_csk_accept+0x370/0x45c
inet_csk_accept at net/ipv4/inet_connection_sock.c:497
[ÂÂ362.653791][ T4106]Â
[ÂÂ362.653791][ T4106] but task is already holding lock:
[ÂÂ362.661007][ T4106] c0ff008a2eae9430 (sk_lock-AF_INET){+.+.}, at:
inet_csk_accept+0x48/0x45c
inet_csk_accept at net/ipv4/inet_connection_sock.c:451
[ÂÂ362.669452][ T4106]Â
[ÂÂ362.669452][ T4106] other info that might help us debug this:
[ÂÂ362.677364][ T4106]ÂÂPossible unsafe locking scenario:
[ÂÂ362.677364][ T4106]Â
[ÂÂ362.684666][ T4106]ÂÂÂÂÂÂÂÂCPU0
[ÂÂ362.687801][ T4106]ÂÂÂÂÂÂÂÂ----
[ÂÂ362.690937][ T4106]ÂÂÂlock(sk_lock-AF_INET);
[ÂÂ362.695204][ T4106]ÂÂÂlock(sk_lock-AF_INET);
[ÂÂ362.699472][ T4106]Â
[ÂÂ362.699472][ T4106]ÂÂ*** DEADLOCK ***
[ÂÂ362.699472][ T4106]Â
[ÂÂ362.707469][ T4106]ÂÂMay be due to missing lock nesting notation
[ÂÂ362.707469][ T4106]Â
[ÂÂ362.715643][ T4106] 1 lock held by sshd/4106:
[ÂÂ362.719993][ T4106]ÂÂ#0: c0ff008a2eae9430 (sk_lock-AF_INET){+.+.}, at:
inet_csk_accept+0x48/0x45c
[ÂÂ362.728874][ T4106]Â
[ÂÂ362.728874][ T4106] stack backtrace:
[ÂÂ362.734622][ T4106] CPU: 22 PID: 4106 Comm: sshd Tainted:
GÂÂÂÂÂÂÂÂÂÂÂÂÂLÂÂÂÂ5.6.0-rc5-next-20200312+ #5
[ÂÂ362.744096][ T4106] Hardware name: HPE Apollo
70ÂÂÂÂÂÂÂÂÂÂÂÂÂ/C01_APACHE_MBÂÂÂÂÂÂÂÂÂ, BIOS L50_5.13_1.11 06/18/2019
[ÂÂ362.754525][ T4106] Call trace:
[ÂÂ362.757667][ T4106]ÂÂdump_backtrace+0x0/0x2c8
[ÂÂ362.762022][ T4106]ÂÂshow_stack+0x20/0x2c
[ÂÂ362.766032][ T4106]ÂÂdump_stack+0xe8/0x150
[ÂÂ362.770128][ T4106]ÂÂvalidate_chain+0x2f08/0x35e0
[ÂÂ362.774830][ T4106]ÂÂ__lock_acquire+0x868/0xc2c
[ÂÂ362.779358][ T4106]ÂÂlock_acquire+0x320/0x360
[ÂÂ362.783715][ T4106]ÂÂlock_sock_nested+0x9c/0xd8
[ÂÂ362.788243][ T4106]ÂÂinet_csk_accept+0x370/0x45c
[ÂÂ362.792861][ T4106]ÂÂinet_accept+0x80/0x1cc
[ÂÂ362.797045][ T4106]ÂÂ__sys_accept4_file+0x1b0/0x2bc
[ÂÂ362.801921][ T4106]ÂÂ__arm64_sys_accept+0x74/0xc8
[ÂÂ362.806625][ T4106]ÂÂdo_el0_svc+0x170/0x240
[ÂÂ362.810807][ T4106]ÂÂel0_sync_handler+0x150/0x250
[ÂÂ362.815509][ T4106]ÂÂel0_sync+0x164/0x180


> +
> + /* 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);
> + if (newsk->sk_memcg && amt)
> + mem_cgroup_charge_skmem(newsk->sk_memcg, amt);
> +
> + release_sock(newsk);
> + }
> out:
> release_sock(sk);
> if (req)