# Re: \[PATCH\] sock: fix /proc/net/sockstat underflow in sk\_clone\_lock()

- From: Tetsuo Handa \<penguin-kernel@i-love.sakura.ne.jp\>
- Date: 2021-11-14 22:48:45 
- List: netdev
- Page: https://ratatoskr.run/netdev/2021/11/11216024
- Thread: https://ratatoskr.run/netdev/2021/11/11216013/t.md

```text
On 2021/11/15 5:01, Eric Dumazet wrote:
> On 11/13/21 10:02 PM, Tetsuo Handa wrote:
>> sk_clone_lock() needs to call get_net() and sock_inuse_inc() together, or

s/sock_inuse_inc/sock_inuse_add/

>> socket_seq_show() will underflow when __sk_free() from sk_free() from
>> sk_free_unlock_clone() is called.
>>
> 
> IMO, a "sock_inuse_get() underflow" is a very different problem,

Yes, a different problem. I found this problem while trying to examine
https://syzkaller.appspot.com/bug?extid=694120e1002c117747ed where
somebody might be failing to call get_net() or we might be failing to
make sure that all timers are synchronously stopped before put_net().

> I suspect this should be fixed with the following patch.

My patch addresses a permanent underflow problem which remains as long as
that namespace exists. Your patch addresses a temporal underflow problem
which happens due to calculating the sum without locks. Therefore, we can
apply both patches if we want.

> 
> diff --git a/net/core/sock.c b/net/core/sock.c
> index c57d9883f62c75f522b7f6bc68451aaf8429dc83..bac8e2b62521301ce897728fff9622c4c05419a3 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -3573,7 +3573,7 @@ int sock_inuse_get(struct net *net)
>         for_each_possible_cpu(cpu)
>                 res += *per_cpu_ptr(net->core.sock_inuse, cpu);
>  
> -       return res;
> +       return max(res, 0);
>  }
>  
>  EXPORT_SYMBOL_GPL(sock_inuse_get);
> 
> 
> Bug added in commit 648845ab7e200993dccd3948c719c858368c91e7
> Author: Tonghao Zhang [ref]
> Date:   Thu Dec 14 05:51:58 2017 -0800
> 
>     sock: Move the socket inuse to namespace.
> 
> 

```
