netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Dumazet <eric.dumazet@gmail.com>
To: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>,
	netdev@vger.kernel.org
Subject: Re: [PATCH] sock: fix /proc/net/sockstat underflow in sk_clone_lock()
Date: Sun, 14 Nov 2021 12:01:27 -0800	[thread overview]
Message-ID: <ee46d850-7dcb-b9d5-b61c-56638fa2f9ae@gmail.com> (raw)
In-Reply-To: <20211114060222.3370-1-penguin-kernel@I-love.SAKURA.ne.jp>



On 11/13/21 10:02 PM, Tetsuo Handa wrote:
> sk_clone_lock() needs to call get_net() and sock_inuse_inc() together, or
> 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,
I suspect this should be fixed with the following patch.

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 <xiangxia.m.yue@gmail.com>
Date:   Thu Dec 14 05:51:58 2017 -0800

    sock: Move the socket inuse to namespace.


> Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
> ---
>  net/core/sock.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/net/core/sock.c b/net/core/sock.c
> index 8f2b2f2c0e7b..41e91d0f7061 100644
> --- a/net/core/sock.c
> +++ b/net/core/sock.c
> @@ -2124,8 +2124,10 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
>  	newsk->sk_prot_creator = prot;
>  
>  	/* SANITY */
> -	if (likely(newsk->sk_net_refcnt))
> +	if (likely(newsk->sk_net_refcnt)) {
>  		get_net(sock_net(newsk));
> +		sock_inuse_add(sock_net(newsk), 1);
> +	}
>  	sk_node_init(&newsk->sk_node);
>  	sock_lock_init(newsk);
>  	bh_lock_sock(newsk);
> @@ -2197,8 +2199,6 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
>  	newsk->sk_err_soft = 0;
>  	newsk->sk_priority = 0;
>  	newsk->sk_incoming_cpu = raw_smp_processor_id();
> -	if (likely(newsk->sk_net_refcnt))
> -		sock_inuse_add(sock_net(newsk), 1);
>  
>  	/* Before updating sk_refcnt, we must commit prior changes to memory
>  	 * (Documentation/RCU/rculist_nulls.rst for details)
> 

  reply	other threads:[~2021-11-14 20:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-14  6:02 [PATCH] sock: fix /proc/net/sockstat underflow in sk_clone_lock() Tetsuo Handa
2021-11-14 20:01 ` Eric Dumazet [this message]
2021-11-14 22:43   ` Tetsuo Handa
2021-11-14 23:54     ` Eric Dumazet
2021-11-15 10:16       ` [PATCH v2] " Tetsuo Handa
2021-11-15 14:50         ` patchwork-bot+netdevbpf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ee46d850-7dcb-b9d5-b61c-56638fa2f9ae@gmail.com \
    --to=eric.dumazet@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=penguin-kernel@I-love.SAKURA.ne.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).