Skip to contentSkip to conversation

net: proc entry showing inodes on sockfs and their types

PATCH In review Masatake YAMATO 2 participants 18 messages Aug 11, 2012 – Aug 12, 2012

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/15 net: proc entry showing inodes on sockfs and their types

I've worked on improving lsof output on linux both lsof and linux sides. Sometimes lsof cannot resolve socket descriptors and as the result it prints them like:

[yamato@localhost]/tmp% sudo lsof | grep dbus | grep iden
dbus-daem 652 dbus 6u sock ... 17812 can't identify protocol
dbus-daem 652 dbus 34u sock ... 24689 can't identify protocol
dbus-daem 652 dbus 42u sock ... 24739 can't identify protocol
dbus-daem 652 dbus 48u sock ... 22329 can't identify protocol
...

lsof refers /proc/net/PROTOCOL files to solve the type of socket for given socket descriptor. lsof looks up inode column of the files.

lsof cannot resolve the type if there is no PROTOCOL file for PROTOCOL for the given socket descriptor nor there is PROTOCOL file with no inode column.

About kernel side, bluetooth protocol families didn't have their own PROTOCOL files. So I added them. netlink protocol had its own PROTOCOL file but no inode column. So I added the column to it.

About lsof side, I added code to refer /proc/net/netlink to lsof. I added code to refer /proc/net/icmp to lsof. I have to added code to refer /proc/net/PROTOCOL files for bluetooth protocol families.

However, it is far from complete.

    $ grep -r '^\(static \)*struct proto ' | wc -l
    60

It is difficult for me to deal such many protocols. During working on improving, newer protocol may be implemented. Further more subsystem mariners don't want to add inode column to /proc/net/PROTOCOL file for avoiding file format incompatibility.

This patch introduces /proc/net/sockfs, which shows most of all inodes on sockfs and their socket type. A socket newly associated with socket descriptor in kernel is stored to a list named `proc_sockfs_list'. /proc/net/sockfs shows the element of the list.

If a protocol has its own lsof friendly proc entry, a socket of the protocol should not be stored to proc_sockfs_list'; lsof can solve the socket type via protocol the proc entry. A protocol can declare it has own proc entry with has_own_proc_entry' field in struct proto. If the field is non-zero, the socket of the protocol is never added to `proc_sockfs_list'.

In v2 patch, unnecessary CONFIG_PROC_FS ifdefs are removed as suggested by Alan Cox. The patches are rebased to net-next.

---
 include/linux/net.h |   8 +++
 include/net/sock.h  |   3 +
 net/socket.c        | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 178 insertions(+), 2 deletions(-)
include/linux/net.h +8 -0
index 99276c3..c87acff 100644
@@ -61,6 +61,9 @@ typedef enum {
 #include <linux/fcntl.h>	/* For O_CLOEXEC and O_NONBLOCK */
 #include <linux/kmemcheck.h>
 #include <linux/rcupdate.h>
+#ifdef CONFIG_PROC_FS
+#include <linux/list.h>
+#endif
 
 struct poll_table_struct;
 struct pipe_inode_info;
@@ -135,6 +138,7 @@ struct socket_wq {
  *  @file: File back pointer for gc
  *  @sk: internal networking protocol agnostic socket representation
  *  @wq: wait queue for several uses
+ *  @proc_sockfs_list: list head to link this socket to /proc/net/sockfs
  */
 struct socket {
 	socket_state		state;
@@ -150,6 +154,10 @@ struct socket {
 	struct file		*file;
 	struct sock		*sk;
 	const struct proto_ops	*ops;
+
+#ifdef CONFIG_PROC_FS
+	struct list_head        proc_sockfs_list;
+#endif
 };
 
 struct vm_area_struct;
include/net/sock.h +3 -0
index 72132ae..af37a1e 100644
@@ -955,6 +955,9 @@ struct proto {
 	void			(*destroy_cgroup)(struct mem_cgroup *memcg);
 	struct cg_proto		*(*proto_cgroup)(struct mem_cgroup *memcg);
 #endif
+	/* Set non-zero value if this protocol manages its
+	   own /proc/net/PROTOCOL entry and the entry has inode column. */
+        int has_own_proc_entry;
 };
 
 /*
net/socket.c +167 -2

237 lines not on this page · raw message

Signature · 1 line
1.7.11.2
#2 lore raw

PATCH v2 3/15 ipv4: declaring icmp protocols has its own proc entry

Declaring icmp protocols has its own proc entry.

---
 net/ipv4/ping.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv4/ping.c +1 -0
index 6232d47..c4bb504 100644
@@ -748,6 +748,7 @@ struct proto ping_prot = {
 	.unhash =	ping_v4_unhash,
 	.get_port =	ping_v4_get_port,
 	.obj_size =	sizeof(struct inet_sock),
+	.has_own_proc_entry= 1,
 };
 EXPORT_SYMBOL(ping_prot);
 
Signature · 1 line
1.7.11.2
#4 lore raw

PATCH v2 5/15 ipv4: declaring raw protocols has its own proc entry

In reply to Masatake YAMATO

Declaring raw protocols has its own proc entry.

---
 net/ipv4/raw.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv4/raw.c +1 -0
index ff0f071..3980a4a 100644
@@ -901,6 +901,7 @@ struct proto raw_prot = {
 	.compat_getsockopt = compat_raw_getsockopt,
 	.compat_ioctl	   = compat_raw_ioctl,
 #endif
+	.has_own_proc_entry= 1,
 };
 
 #ifdef CONFIG_PROC_FS
Signature · 1 line
1.7.11.2
#5 lore raw

PATCH v2 6/15 ipv4: declaring tcp protocols has its own proc entry

In reply to Masatake YAMATO

Declaring tcp protocols has its own proc entry.

---
 net/ipv4/tcp_ipv4.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv4/tcp_ipv4.c +1 -0
index c660d2c..6f36929 100644
@@ -2635,6 +2635,7 @@ struct proto tcp_prot = {
 	.destroy_cgroup		= tcp_destroy_cgroup,
 	.proto_cgroup		= tcp_proto_cgroup,
 #endif
+	.has_own_proc_entry= 1,
 };
 EXPORT_SYMBOL(tcp_prot);
 
Signature · 1 line
1.7.11.2
#6 lore raw

PATCH v2 7/15 packet: declaring packet protocols has its own proc entry

In reply to Masatake YAMATO

Declaring packet protocols has its own proc entry.

---
 net/packet/af_packet.c | 1 +
 1 file changed, 1 insertion(+)
net/packet/af_packet.c +1 -0
index ceaca7c..6a0e47c 100644
@@ -2564,6 +2564,7 @@ static struct proto packet_proto = {
 	.name	  = "PACKET",
 	.owner	  = THIS_MODULE,
 	.obj_size = sizeof(struct packet_sock),
+	.has_own_proc_entry= 1,
 };
 
 /*
Signature · 1 line
1.7.11.2
#7 lore raw

PATCH v2 8/15 unix: declaring unix protocols has its own proc entry

In reply to Masatake YAMATO

Declaring unix protocols has its own proc entry.

---
 net/unix/af_unix.c | 1 +
 1 file changed, 1 insertion(+)
net/unix/af_unix.c +1 -0
index e4768c1..7f109e6 100644
@@ -616,6 +616,7 @@ static struct proto unix_proto = {
 	.name			= "UNIX",
 	.owner			= THIS_MODULE,
 	.obj_size		= sizeof(struct unix_sock),
+	.has_own_proc_entry= 1,
 };
 
 /*
Signature · 1 line
1.7.11.2
#8 lore raw

PATCH v2 9/15 netlink: declaring netlink protocols has its own proc entry

In reply to Masatake YAMATO

Declaring netlink protocols has its own proc entry.

---
 net/netlink/af_netlink.c | 1 +
 1 file changed, 1 insertion(+)
net/netlink/af_netlink.c +1 -0
index 5463969..9094be5 100644
@@ -410,6 +410,7 @@ static struct proto netlink_proto = {
 	.name	  = "NETLINK",
 	.owner	  = THIS_MODULE,
 	.obj_size = sizeof(struct netlink_sock),
+	.has_own_proc_entry= 1,
 };
 
 static int __netlink_create(struct net *net, struct socket *sock,
Signature · 1 line
1.7.11.2
#9 lore raw

PATCH v2 10/15 ipv6: declaring tcpv6 protocols has its own proc entry

In reply to Masatake YAMATO

Declaring tcpv6 protocols has its own proc entry.

---
 net/ipv6/tcp_ipv6.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv6/tcp_ipv6.c +1 -0
index aa41b0e..741cb3b 100644
@@ -2017,6 +2017,7 @@ struct proto tcpv6_prot = {
 #ifdef CONFIG_MEMCG_KMEM
 	.proto_cgroup		= tcp_proto_cgroup,
 #endif
+	.has_own_proc_entry= 1,
 };
 
 static const struct inet6_protocol tcpv6_protocol = {
Signature · 1 line
1.7.11.2
#11 lore raw

PATCH v2 12/15 ipv6: declaring udplitev6 protocols has its own proc entry

In reply to Masatake YAMATO

Declaring udplitev6 protocols has its own proc entry.

---
 net/ipv6/udplite.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv6/udplite.c +1 -0
index 1d08e21..9013897 100644
@@ -57,6 +57,7 @@ struct proto udplitev6_prot = {
 	.compat_getsockopt = compat_udpv6_getsockopt,
 #endif
 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
+	.has_own_proc_entry= 1,
 };
 
 static struct inet_protosw udplite6_protosw = {
Signature · 1 line
1.7.11.2
#12 lore raw

PATCH v2 13/15 ipv6: declaring rawv6 protocols has its own proc entry

In reply to Masatake YAMATO

Declaring rawv6 protocols has its own proc entry.

---
 net/ipv6/raw.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv6/raw.c +1 -0
index ef0579d..62ac59f 100644
@@ -1226,6 +1226,7 @@ struct proto rawv6_prot = {
 	.compat_getsockopt = compat_rawv6_getsockopt,
 	.compat_ioctl	   = compat_rawv6_ioctl,
 #endif
+	.has_own_proc_entry= 1,
 };
 
 #ifdef CONFIG_PROC_FS
Signature · 1 line
1.7.11.2
#13 lore raw

PATCH v2 14/15 ax25: declaring ax25 protocols has its own proc entry

In reply to Masatake YAMATO

Declaring ax25 protocols has its own proc entry.

---
 net/ax25/af_ax25.c | 1 +
 1 file changed, 1 insertion(+)
net/ax25/af_ax25.c +1 -0
index 779095d..ff23c8a 100644
@@ -803,6 +803,7 @@ static struct proto ax25_proto = {
 	.name	  = "AX25",
 	.owner	  = THIS_MODULE,
 	.obj_size = sizeof(struct sock),
+	.has_own_proc_entry= 1,
 };
 
 static int ax25_create(struct net *net, struct socket *sock, int protocol,
Signature · 1 line
1.7.11.2
#14 lore raw

PATCH v2 15/15 ipx: declaring ipx protocols has its own proc entry

In reply to Masatake YAMATO

Declaring ipx protocols has its own proc entry.

---
 net/ipx/af_ipx.c | 1 +
 1 file changed, 1 insertion(+)
net/ipx/af_ipx.c +1 -0
index dfd6faa..14be087 100644
@@ -1345,6 +1345,7 @@ static struct proto ipx_proto = {
 	.name	  = "IPX",
 	.owner	  = THIS_MODULE,
 	.obj_size = sizeof(struct ipx_sock),
+	.has_own_proc_entry= 1,
 };
 
 static int ipx_create(struct net *net, struct socket *sock, int protocol,
Signature · 1 line
1.7.11.2
#1 lore raw

PATCH v2 2/15 ipv4: declaring udp protocols has its own proc entry

In reply to Masatake YAMATO

Declaring udp protocols has its own proc entry.

---
 net/ipv4/udp.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv4/udp.c +1 -0
index b4c3582..a16c4fa 100644
@@ -1963,6 +1963,7 @@ struct proto udp_prot = {
 	.compat_getsockopt = compat_udp_getsockopt,
 #endif
 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
+	.has_own_proc_entry= 1,
 };
 EXPORT_SYMBOL(udp_prot);
 
Signature · 1 line
1.7.11.2
#10 lore raw

PATCH v2 11/15 ipv6: declaring udpv6 protocols has its own proc entry

In reply to Masatake YAMATO

Declaring udpv6 protocols has its own proc entry.

---
 net/ipv6/udp.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv6/udp.c +1 -0
index 99d0077..1b28eaf 100644
@@ -1537,6 +1537,7 @@ struct proto udpv6_prot = {
 	.compat_getsockopt = compat_udpv6_getsockopt,
 #endif
 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
+	.has_own_proc_entry= 1,
 };
 
 static struct inet_protosw udpv6_protosw = {
Signature · 1 line
1.7.11.2
#3 lore raw

PATCH v2 4/15 ipv4: declaring udplite protocols has its own proc entry

In reply to Masatake YAMATO

Declaring udplite protocols has its own proc entry.

---
 net/ipv4/udplite.c | 1 +
 1 file changed, 1 insertion(+)
net/ipv4/udplite.c +1 -0
index 2c46acd..14c9dec 100644
@@ -62,6 +62,7 @@ struct proto 	udplite_prot = {
 	.compat_getsockopt = compat_udp_getsockopt,
 #endif
 	.clear_sk	   = sk_prot_clear_portaddr_nulls,
+	.has_own_proc_entry= 1,
 };
 EXPORT_SYMBOL(udplite_prot);
 
Signature · 1 line
1.7.11.2
maintainer
#15 lore raw
In reply to Masatake YAMATO

Sorry, you cannot do this.

You are adding a new lock and insert into a global list for pretty much every socket created, that will destroy performance.

You also cannnot add new fields to socket listing procfs files, it will break existing application which depend upon the existing exact layout of those fields.

maintainer
#17 lore raw
In reply to Masatake YAMATO

I also want to mention that I absolutely do not consider better lsof support important at all.

So if you want to add this, it had to be exactly zero overhead and it must not break anything that exists already. Your patch set violates this on both counts.

#16 lore raw
In reply to David Miller
Quoted 5 lines

Sorry, you cannot do this.

You are adding a new lock and insert into a global list for pretty much every socket created, that will destroy performance.

I think there are no serious performance penalty in generally use. Most frequently used types of sockets like tcp, udp, and unix are not stored to the global list and don't touch the new lock at all. Please, look at has_own_proc_entry in struct proto.

Quoted 3 lines

You also cannnot add new fields to socket listing procfs files, it will break existing application which depend upon the existing exact layout of those fields.

Is there any strcut which can be extended?
Extending struct sock is o.k.?

Masatake YAMATO