Remove refcounting from unix domain sockets
Subsystems (4)
RFC PATCH 1/1 Remove refcounting from unix domain sockets
While thinking about things it occurred to me that by fixing what is arguably in a bug in the unix domain socket implementation I can remove the performance penality for credential passing.
That bug is the fact that the cmsg returned on a unix domain socket is different depending on who calls recvmsg. Passing file descriptor should not changes how it operates (oops).
There is a second bug this also fixes, pids are now translated as they traverse netlink sockets.
By grabbing a reference to the user and pid namespaces on each unix domain socket, and each netlink socket I can translate the uids and pids when they are placed on the destination socket and avoid the need for a reference count.
This patch is still a little rough and needs to be split up and some of the netlink callers need to be tweaked to pass NULL for the new scm parameter, but it shows to me at least that the my brainstorm is workable.
What I really haven't done yet is benchmark anything. I just know in principle that the benchmark numbers should be better, as even the pid refcount goes away (except when we are faking the senders pid).
Does anyone have ideas about benchmarks that would be useful to run? Does this sound like something that will make unix domain and netlink sockets faster?
Does refcounting the userns and the pidns from each unix domain or netlink socket look like it will add problematic overhead?
Eric
---
include/linux/netlink.h | 13 ++++--- include/net/af_unix.h | 9 +++-- include/net/netlink.h | 4 +- include/net/scm.h | 43 +++++++-------------- net/core/scm.c | 13 ++++--- net/netlink/af_netlink.c | 82 ++++++++++++++++++++++++++-------------- net/netlink/af_netlink.h | 5 +++ net/unix/af_unix.c | 98 ++++++++++++++++++++++++++---------------------- 8 files changed, 149 insertions(+), 118 deletions(-)
include/linux/netlink.h +7 -6
@@ -23,7 +23,7 @@ enum netlink_skb_flags { };struct netlink_skb_parms { -struct scm_creds creds;/* Skb credentials */ +struct ucred creds;/* Skb credentials */ __u32 portid;__u32 dst_group;__u32 flags;
@@ -31,7 +31,6 @@ struct netlink_skb_parms { };#define NETLINK_CB (skb) (*(struct netlink_skb_parms*)&((skb)->cb)) -#define NETLINK_CREDS (skb) (&NETLINK_CB((skb)).creds)extern void netlink_table_grab (void );
@@ -68,11 +67,13 @@ extern void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err);extern int netlink_has_listeners (struct sock *sk,unsigned int group);extern struct sk_buff *netlink_alloc_skb (struct sock *ssk,unsigned int size,u32 dst_portid,gfp_t gfp_mask); -extern int netlink_unicast (struct sock *ssk,struct sk_buff *skb,__u32 portid,int nonblock); +extern int netlink_unicast (struct sock *ssk,struct sk_buff *skb,__u32 portid, +struct scm_cookie *scm,int nonblock);extern int netlink_broadcast (struct sock *ssk,struct sk_buff *skb,__u32 portid, -__u32 group,gfp_t allocation); +__u32 group,struct scm_cookie *scm, +gfp_t allocation);extern int netlink_broadcast_filtered (struct sock *ssk,struct sk_buff *skb, -__u32 portid,__u32 group,gfp_t allocation, +__u32 portid,__u32 group,struct scm_cookie *scm,gfp_t allocation,int (*filter)(struct sock *dsk,struct sk_buff *skb,void *data),void *filter_data);extern int netlink_set_err (struct sock *ssk,__u32 portid,__u32 group,int code);
@@ -82,7 +83,7 @@ extern int netlink_unregister_notifier(struct notifier_block *nb);/* finegrained unicast helpers: */ struct sock *netlink_getsockbyfilp (struct file *filp);int netlink_attachskb (struct sock *sk,struct sk_buff *skb, -long *timeo,struct sock *ssk); +long *timeo,struct sock *ssk,struct scm_cookie *scm);void netlink_detachskb (struct sock *sk,struct sk_buff *skb);int netlink_sendskb (struct sock *sk,struct sk_buff *skb);
include/net/af_unix.h +6 -3
@@ -6,6 +6,9 @@#include <linux/mutex.h> #include <net/sock.h> +struct pid_namespace ; +struct user_namespace ; +void unix_inflight (struct file *fp);void unix_notinflight (struct file *fp);void unix_gc (void );
@@ -28,10 +31,8 @@ struct unix_address { };struct unix_skb_parms { -struct pid *pid;/* Skb credentials */ -kuid_t uid; -kgid_t gid;struct scm_fp_list *fp;/* Passed files */ +struct ucred creds;/* Skb credentials */ #ifdef CONFIG_SECURITY_NETWORK u32 secid;/* Security ID */ #endif
@@ -63,6 +64,8 @@ struct unix_sock {#define UNIX_GC_CANDIDATE 0#define UNIX_GC_MAYBE_CYCLE 1struct socket_wq peer_wq; +struct pid_namespace *pid_ns; +struct user_namespace *user_ns; };#define unix_sk (__sk) ((struct unix_sock *)__sk)
include/net/netlink.h +2 -2
@@ -562,7 +562,7 @@ static inline int nlmsg_multicast(struct sock *sk, struct sk_buff *skb,NETLINK_CB (skb).dst_group = group; - err =netlink_broadcast (sk, skb, portid, group, flags); + err =netlink_broadcast (sk, skb, portid, group,NULL , flags);if (err >0 ) err =0 ;
@@ -579,7 +579,7 @@ static inline int nlmsg_unicast(struct sock *sk, struct sk_buff *skb, u32 portid {int err; - err =netlink_unicast (sk, skb, portid,MSG_DONTWAIT ); + err =netlink_unicast (sk, skb, portid,NULL ,MSG_DONTWAIT );if (err >0 ) err =0 ;
include/net/scm.h +14 -29
76 lines not on this page · raw message
net/core/scm.c +7 -6
@@ -167,15 +167,16 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)if (err) goto error; - p->creds.pid = creds.pid; -if (!p->pid ||pid_vnr (p->pid) != creds.pid) { +if (!p->icreds.pid || +pid_vnr (p->icreds.pid) != creds.pid) {struct pid *pid; err = -ESRCH ; pid =find_get_pid (creds.pid);if (!pid) goto error; -put_pid (p->pid); - p->pid = pid; +if (p->icreds.pid !=task_tgid (current)) +put_pid (p->icreds.pid); + p->icreds.pid = pid; } err = -EINVAL ;
@@ -184,8 +185,8 @@ int __scm_send(struct socket *sock, struct msghdr *msg, struct scm_cookie *p)if (!uid_valid (uid) || !gid_valid (gid)) goto error; - p->creds.uid = uid; - p->creds.gid = gid; + p->icreds.uid = uid; + p->icreds.gid = gid;break ; }default :
net/netlink/af_netlink.c +54 -28
248 lines not on this page · raw message
net/netlink/af_netlink.h +5 -0
@@ -5,6 +5,9 @@#include <linux/atomic.h> #include <net/sock.h> +struct pid_namespace ; +struct user_namespace ; +#define NLGRPSZ (x) (ALIGN(x, sizeof(unsigned long) * 8) / 8)#define NLGRPLONGS (x) (NLGRPSZ(x)/sizeof(unsigned long))
@@ -43,6 +46,8 @@ struct netlink_sock {int (*netlink_bind)(struct net *net,int group);void (*netlink_unbind)(struct net *net,int group);struct module *module; +struct pid_namespace *pid_ns; +struct user_namespace *user_ns;#ifdef CONFIG_NETLINK_MMAP struct mutex pg_vec_lock;struct netlink_ring rx_ring;
net/unix/af_unix.c +54 -44
187 lines not on this page · raw message
Quoted 8 lines · Eric W. Biederman
While thinking about things it occurred to me that by fixing what is arguably in a bug in the unix domain socket implementation I can remove the performance penality for credential passing.
That bug is the fact that the cmsg returned on a unix domain socket is different depending on who calls recvmsg. Passing file descriptor should not changes how it operates (oops).
I'm having some trouble following what's going on with icred and ocred. That being said, I think that you're changing the semantics of socketpair() followed by unshare(), and I wouldn't be that surprised if something relies on the current behavior.
Quoted 3 lines
There is a second bug this also fixes, pids are now translated as they traverse netlink sockets.
Can you describe the bug more clearly?
--Andy