netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@amazon.com>
To: "David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>
Cc: Kuniyuki Iwashima <kuniyu@amazon.com>,
	Kuniyuki Iwashima <kuni1840@gmail.com>, <netdev@vger.kernel.org>
Subject: [PATCH v2 net-next 10/15] socket: Don't count kernel sockets in /proc/net/sockstat.
Date: Tue, 10 Dec 2024 16:38:24 +0900	[thread overview]
Message-ID: <20241210073829.62520-11-kuniyu@amazon.com> (raw)
In-Reply-To: <20241210073829.62520-1-kuniyu@amazon.com>

The first line in /proc/net/sockstat shows the number of
sockets counted by sock_inuse_add().

  $ cat /proc/net/sockstat
  sockets: used 169

The count initially showed the number of userspace sockets,
but now it includes some kernel sockets, which is confusing.

This is because __sk_free() decrements the count based on
sk->sk_net_refcnt, which should be sk->sk_kern_sock.

Let's call sock_inuse_add() based on sk->sk_kern_sock.

Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com>
---
 fs/smb/client/connect.c |  1 -
 net/core/sock.c         | 17 ++++++++++-------
 net/mptcp/subflow.c     |  2 +-
 net/rds/tcp.c           |  1 -
 net/smc/af_smc.c        |  1 -
 net/sunrpc/svcsock.c    |  2 +-
 net/sunrpc/xprtsock.c   |  1 -
 7 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/fs/smb/client/connect.c b/fs/smb/client/connect.c
index 1efef860d20c..9f6daa32c083 100644
--- a/fs/smb/client/connect.c
+++ b/fs/smb/client/connect.c
@@ -3146,7 +3146,6 @@ generic_ip_connect(struct TCP_Server_Info *server)
 		__netns_tracker_free(net, &sk->ns_tracker, false);
 		sk->sk_net_refcnt = 1;
 		get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
-		sock_inuse_add(net, 1);
 
 		/* BB other socket options to set KEEPALIVE, NODELAY? */
 		cifs_dbg(FYI, "Socket created\n");
diff --git a/net/core/sock.c b/net/core/sock.c
index 11aa6d8c0cdd..4041152c7024 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -2227,16 +2227,17 @@ struct sock *sk_alloc(struct net *net, int family, gfp_t priority,
 
 		DEBUG_NET_WARN_ON_ONCE(!kern && !hold_net);
 		sk->sk_kern_sock = kern;
+		if (likely(!kern))
+			sock_inuse_add(net, 1);
+
 		sock_lock_init(sk);
 
 		sk->sk_net_refcnt = hold_net;
-		if (likely(sk->sk_net_refcnt)) {
+		if (likely(sk->sk_net_refcnt))
 			get_net_track(net, &sk->ns_tracker, priority);
-			sock_inuse_add(net, 1);
-		} else {
+		else
 			__netns_tracker_alloc(net, &sk->ns_tracker,
 					      false, priority);
-		}
 
 		sock_net_set(sk, net);
 		refcount_set(&sk->sk_wmem_alloc, 1);
@@ -2314,7 +2315,7 @@ void sk_destruct(struct sock *sk)
 
 static void __sk_free(struct sock *sk)
 {
-	if (likely(sk->sk_net_refcnt))
+	if (likely(!sk->sk_kern_sock))
 		sock_inuse_add(sock_net(sk), -1);
 
 	if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
@@ -2383,10 +2384,11 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
 
 	newsk->sk_prot_creator = prot;
 
-	/* SANITY */
+	if (likely(!sk->sk_kern_sock))
+		sock_inuse_add(sock_net(newsk), 1);
+
 	if (likely(newsk->sk_net_refcnt)) {
 		get_net_track(sock_net(newsk), &newsk->ns_tracker, priority);
-		sock_inuse_add(sock_net(newsk), 1);
 	} else {
 		/* Kernel sockets are not elevating the struct net refcount.
 		 * Instead, use a tracker to more easily detect if a layer
@@ -2396,6 +2398,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
 		__netns_tracker_alloc(sock_net(newsk), &newsk->ns_tracker,
 				      false, priority);
 	}
+
 	sk_node_init(&newsk->sk_node);
 	sock_lock_init(newsk);
 	bh_lock_sock(newsk);
diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
index fd021cf8286e..fc534290f119 100644
--- a/net/mptcp/subflow.c
+++ b/net/mptcp/subflow.c
@@ -1775,7 +1775,7 @@ int mptcp_subflow_create_socket(struct sock *sk, unsigned short family,
 	__netns_tracker_free(net, &sf->sk->ns_tracker, false);
 	sf->sk->sk_net_refcnt = 1;
 	get_net_track(net, &sf->sk->ns_tracker, GFP_KERNEL);
-	sock_inuse_add(net, 1);
+
 	err = tcp_set_ulp(sf->sk, "mptcp");
 	if (err)
 		goto err_free;
diff --git a/net/rds/tcp.c b/net/rds/tcp.c
index 351ac1747224..f7e8a309f678 100644
--- a/net/rds/tcp.c
+++ b/net/rds/tcp.c
@@ -507,7 +507,6 @@ bool rds_tcp_tune(struct socket *sock)
 
 		sk->sk_net_refcnt = 1;
 		netns_tracker_alloc(net, &sk->ns_tracker, GFP_KERNEL);
-		sock_inuse_add(net, 1);
 	}
 	rtn = net_generic(net, rds_tcp_netid);
 	if (rtn->sndbuf_size > 0) {
diff --git a/net/smc/af_smc.c b/net/smc/af_smc.c
index 9b5738a55dde..10f9968f87b1 100644
--- a/net/smc/af_smc.c
+++ b/net/smc/af_smc.c
@@ -3324,7 +3324,6 @@ int smc_create_clcsk(struct net *net, struct sock *sk, int family)
 	__netns_tracker_free(net, &sk->ns_tracker, false);
 	sk->sk_net_refcnt = 1;
 	get_net_track(net, &sk->ns_tracker, GFP_KERNEL);
-	sock_inuse_add(net, 1);
 	return 0;
 }
 
diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index 9583bad3d150..bdea406308a8 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -1554,7 +1554,7 @@ static struct svc_xprt *svc_create_socket(struct svc_serv *serv,
 		__netns_tracker_free(net, &sock->sk->ns_tracker, false);
 		sock->sk->sk_net_refcnt = 1;
 		get_net_track(net, &sock->sk->ns_tracker, GFP_KERNEL);
-		sock_inuse_add(net, 1);
+
 		if ((error = kernel_listen(sock, 64)) < 0)
 			goto bummer;
 	}
diff --git a/net/sunrpc/xprtsock.c b/net/sunrpc/xprtsock.c
index feb1768e8a57..1bc3a480d919 100644
--- a/net/sunrpc/xprtsock.c
+++ b/net/sunrpc/xprtsock.c
@@ -1945,7 +1945,6 @@ static struct socket *xs_create_sock(struct rpc_xprt *xprt,
 		__netns_tracker_free(xprt->xprt_net, &sock->sk->ns_tracker, false);
 		sock->sk->sk_net_refcnt = 1;
 		get_net_track(xprt->xprt_net, &sock->sk->ns_tracker, GFP_KERNEL);
-		sock_inuse_add(xprt->xprt_net, 1);
 	}
 
 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
-- 
2.39.5 (Apple Git-154)


  parent reply	other threads:[~2024-12-10  7:42 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-10  7:38 [PATCH v2 net-next 00/15] treewide: socket: Clean up sock_create() and friends Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 01/15] socket: Un-export __sock_create() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 02/15] socket: Pass hold_net flag to __sock_create() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 03/15] smc: Pass kern to smc_sock_alloc() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 04/15] socket: Pass hold_net to struct net_proto_family.create() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 05/15] ppp: Pass hold_net to struct pppox_proto.create() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 06/15] nfc: Pass hold_net to struct nfc_protocol.create() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 07/15] socket: Add hold_net flag to struct proto_accept_arg Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 08/15] socket: Pass hold_net to sk_alloc() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 09/15] socket: Respect hold_net in sk_alloc() Kuniyuki Iwashima
2024-12-10  7:38 ` Kuniyuki Iwashima [this message]
2024-12-10  7:38 ` [PATCH v2 net-next 11/15] socket: Introduce sock_create_net() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 12/15] socket: Remove kernel socket conversion Kuniyuki Iwashima
2024-12-11  2:20   ` Jakub Kicinski
2024-12-12 17:35     ` Allison Henderson
2024-12-13  8:28       ` Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 13/15] socket: Use sock_create_net() instead of sock_create() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 14/15] socket: Rename sock_create() to sock_create_user() Kuniyuki Iwashima
2024-12-10  7:38 ` [PATCH v2 net-next 15/15] socket: Rename sock_create_kern() to sock_create_net_noref() Kuniyuki Iwashima
2024-12-10  8:46 ` [PATCH v2 net-next 00/15] treewide: socket: Clean up sock_create() and friends Eric Dumazet
2024-12-10  9:47   ` Kuniyuki Iwashima

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=20241210073829.62520-11-kuniyu@amazon.com \
    --to=kuniyu@amazon.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /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).