diff --git a/net/Kconfig b/net/Kconfig
index d79219fa53437cbb4433605fcd0eb292a26f577a..e2a52376d1ea95e09e67c96f910bf50c4f2023da 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -98,6 +98,9 @@ source "net/netlabel/Kconfig"
 
 endif # if INET
 
+config SOCK_DIAG
+	bool
+
 config NETWORK_SECMARK
 	bool "Security Marking"
 	help
diff --git a/net/core/Makefile b/net/core/Makefile
index 80175e6a2eb871087985cfcb6f8c0ee94aef979f..8aa31e739ada6ba8aee8d37d780d5a3e67801f8e 100644
--- a/net/core/Makefile
+++ b/net/core/Makefile
@@ -10,9 +10,10 @@ obj-$(CONFIG_SYSCTL) += sysctl_net_core.o
 
 obj-y		     += dev.o ethtool.o dev_addr_lists.o dst.o netevent.o \
 			neighbour.o rtnetlink.o utils.o link_watch.o filter.o \
-			sock_diag.o dev_ioctl.o tso.o sock_reuseport.o \
+ 			dev_ioctl.o tso.o sock_reuseport.o \
 			fib_notifier.o xdp.o
 
+obj-$(CONFIG_SOCK_DIAG) += sock_diag.o
 obj-y += net-sysfs.o
 obj-$(CONFIG_PAGE_POOL) += page_pool.o
 obj-$(CONFIG_PROC_FS) += net-procfs.o
diff --git a/net/core/sock.c b/net/core/sock.c
index e6cbe137cb6fc949d089c0ae0f0fb03135b9cfbf..787dfbb446de398aadcb37ef5a5c78b9644c6a69 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -490,6 +490,18 @@ int __sk_receive_skb(struct sock *sk, struct sk_buff *skb,
 }
 EXPORT_SYMBOL(__sk_receive_skb);
 
+u64 sock_gen_cookie(struct sock *sk)
+{
+	while (1) {
+		u64 res = atomic64_read(&sk->sk_cookie);
+
+		if (res)
+			return res;
+		res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
+		atomic64_cmpxchg(&sk->sk_cookie, 0, res);
+	}
+}
+
 struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
 {
 	struct dst_entry *dst = __sk_dst_get(sk);
@@ -1604,9 +1616,11 @@ static void __sk_free(struct sock *sk)
 	if (likely(sk->sk_net_refcnt))
 		sock_inuse_add(sock_net(sk), -1);
 
+#ifdef CONFIG_SOCK_DIAG
 	if (unlikely(sk->sk_net_refcnt && sock_diag_has_destroy_listeners(sk)))
 		sock_diag_broadcast_destroy(sk);
 	else
+#endif
 		sk_destruct(sk);
 }
 
diff --git a/net/core/sock_diag.c b/net/core/sock_diag.c
index 3312a5849a974e372a49164abca8f7067a2ed7e7..8cd45bc053f24895b79b1bca9fb0a723c6cf5325 100644
--- a/net/core/sock_diag.c
+++ b/net/core/sock_diag.c
@@ -20,18 +20,6 @@ static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
 static DEFINE_MUTEX(sock_diag_table_mutex);
 static struct workqueue_struct *broadcast_wq;
 
-u64 sock_gen_cookie(struct sock *sk)
-{
-	while (1) {
-		u64 res = atomic64_read(&sk->sk_cookie);
-
-		if (res)
-			return res;
-		res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
-		atomic64_cmpxchg(&sk->sk_cookie, 0, res);
-	}
-}
-
 int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
 {
 	u64 res;
diff --git a/net/ipv4/Kconfig b/net/ipv4/Kconfig
index 2e12f848203a4638c4718a0ba9e1ab541240b8e5..ed980212406729a59446345dd69519792f46dabb 100644
--- a/net/ipv4/Kconfig
+++ b/net/ipv4/Kconfig
@@ -426,6 +426,7 @@ config INET_XFRM_MODE_BEET
 
 config INET_DIAG
 	tristate "INET: socket monitoring interface"
+	select SOCK_DIAG
 	default y
 	---help---
 	  Support for INET (TCP, DCCP, etc) socket monitoring interface used by
diff --git a/net/netlink/Kconfig b/net/netlink/Kconfig
index 5d6e8c05b3d48adb1201fad2b69c8cda2efcc03e..8a2696d66c203c55530d21281a52057cfb861330 100644
--- a/net/netlink/Kconfig
+++ b/net/netlink/Kconfig
@@ -4,6 +4,7 @@
 
 config NETLINK_DIAG
 	tristate "NETLINK: socket monitoring interface"
+	select SOCK_DIAG
 	default n
 	---help---
 	  Support for NETLINK socket monitoring interface used by the ss tool.
diff --git a/net/packet/Kconfig b/net/packet/Kconfig
index cc55b35f80e5acd045c37fc20bb37d98ba3d258c..9a83c86f5480c166ae2f2098f8b6efbb10f9b449 100644
--- a/net/packet/Kconfig
+++ b/net/packet/Kconfig
@@ -18,6 +18,7 @@ config PACKET
 config PACKET_DIAG
 	tristate "Packet: sockets monitoring interface"
 	depends on PACKET
+	select SOCK_DIAG
 	default n
 	---help---
 	  Support for PF_PACKET sockets monitoring interface used by the ss tool.
diff --git a/net/unix/Kconfig b/net/unix/Kconfig
index 8b31ab85d050f7b5930d53db5d69b6949865df4e..6548cf3ea7f11f9a20e0abc275482317f48c8652 100644
--- a/net/unix/Kconfig
+++ b/net/unix/Kconfig
@@ -22,6 +22,7 @@ config UNIX
 config UNIX_DIAG
 	tristate "UNIX: socket monitoring interface"
 	depends on UNIX
+	select SOCK_DIAG
 	default n
 	---help---
 	  Support for UNIX socket monitoring interface used by the ss tool.