icmp_socket namespacing
Subsystems (5)
cover icmp_socket namespacing
This set of patches is targeted to create separate icmp_socket inside each namespace. Both IPv4 and IPv6 codepaths are affected.
Though, in order to do this smoothly, a bit of optimisations are performed. The kernel from now on will use sock rather than socket on the ICMP send path.
PATCH 1/12 : Remove struct net_proto_family* from _init calls.
struct net_proto_family* is not used in icmp[v6]_init, ndisc_init, igmp_init and tcp_v4_init. Remove it.
--- include/linux/icmpv6.h | 2 +- include/net/icmp.h | 2 +- include/net/ndisc.h | 4 ++-- include/net/tcp.h | 2 +- net/ipv4/af_inet.c | 4 ++-- net/ipv4/icmp.c | 2 +- net/ipv4/tcp_ipv4.c | 2 +- net/ipv6/af_inet6.c | 6 +++--- net/ipv6/icmp.c | 2 +- net/ipv6/mcast.c | 2 +- net/ipv6/ndisc.c | 2 +- 11 files changed, 15 insertions(+), 15 deletions(-)
include/linux/icmpv6.h +1 -1
@@ -176,7 +176,7 @@ extern void icmpv6_send(struct sk_buff *skb,__u32 info, struct net_device *dev); -extern int icmpv6_init (struct net_proto_family *ops); +extern int icmpv6_init (void );extern int icmpv6_err_convert (int type,int code,int *err);extern void icmpv6_cleanup (void );
include/net/icmp.h +1 -1
@@ -48,7 +48,7 @@ struct sk_buff;extern void icmp_send (struct sk_buff *skb_in,int type,int code,__be32 info);extern int icmp_rcv (struct sk_buff *skb);extern int icmp_ioctl (struct sock *sk,int cmd,unsigned long arg); -extern void icmp_init (struct net_proto_family *ops); +extern void icmp_init (void );extern void icmp_out_count (unsigned char type);/* Move into dst.h ? */
include/net/ndisc.h +2 -2
@@ -77,7 +77,7 @@ struct nd_opt_hdr { } __attribute__((__packed__)); -extern int ndisc_init (struct net_proto_family *ops); +extern int ndisc_init (void );extern void ndisc_cleanup (void );
@@ -107,7 +107,7 @@ extern int ndisc_mc_map(struct in6_addr *addr, char *buf, struct net_device *d/* * IGMP */ -extern int igmp6_init (struct net_proto_family *ops); +extern int igmp6_init (void );extern void igmp6_cleanup (void );
include/net/tcp.h +1 -1
@@ -1373,7 +1373,7 @@ struct tcp_request_sock_ops { #endif }; -extern void tcp_v4_init (struct net_proto_family *ops); +extern void tcp_v4_init (void );extern void tcp_init (void );#endif /* _TCP_H */
net/ipv4/af_inet.c +2 -2
@@ -1415,7 +1415,7 @@ static int __init inet_init(void)ip_init (); -tcp_v4_init (&inet_family_ops); +tcp_v4_init ();/* Setup TCP slab cache for open requests. */ tcp_init ();
@@ -1430,7 +1430,7 @@ static int __init inet_init(void)* Set the ICMP layer up */ -icmp_init (&inet_family_ops); +icmp_init ();/* * Initialise the multicast router
net/ipv4/icmp.c +1 -1
@@ -1139,7 +1139,7 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = { }, }; -void __init icmp_init (struct net_proto_family *ops) +void __init icmp_init (void ) {struct inet_sock *inet;int i;
net/ipv4/tcp_ipv4.c +1 -1
@@ -2443,7 +2443,7 @@ struct proto tcp_prot = { REF_PROTO_INUSE(tcp) }; -void __init tcp_v4_init (struct net_proto_family *ops) +void __init tcp_v4_init (void ) {if (inet_csk_ctl_sock_create (&tcp_socket,PF_INET ,SOCK_RAW ,IPPROTO_TCP ) <0 )
net/ipv6/af_inet6.c +3 -3
@@ -808,13 +808,13 @@ static int __init inet6_init(void)if (err) goto sysctl_fail;#endif - err =icmpv6_init (&inet6_family_ops); + err =icmpv6_init ();if (err) goto icmp_fail; - err =ndisc_init (&inet6_family_ops); + err =ndisc_init ();if (err) goto ndisc_fail; - err =igmp6_init (&inet6_family_ops); + err =igmp6_init ();if (err) goto igmp_fail; err =ipv6_netfilter_init ();
net/ipv6/icmp.c +1 -1
@@ -780,7 +780,7 @@ drop_no_count:*/ static struct lock_class_key icmpv6_socket_sk_dst_lock_key; -int __init icmpv6_init (struct net_proto_family *ops) +int __init icmpv6_init (void ) {struct sock *sk;int err, i, j;
net/ipv6/mcast.c +1 -1
@@ -2597,7 +2597,7 @@ static const struct file_operations igmp6_mcf_seq_fops = { };#endif -int __init igmp6_init (struct net_proto_family *ops) +int __init igmp6_init (void ) {struct ipv6_pinfo *np;struct sock *sk;
net/ipv6/ndisc.c +1 -1
@@ -1733,7 +1733,7 @@ static int ndisc_ifinfo_sysctl_strategy(ctl_table *ctl, int __user *name,#endif -int __init ndisc_init (struct net_proto_family *ops) +int __init ndisc_init (void ) {struct ipv6_pinfo *np;struct sock *sk;
Signature · 1 line
1.5.3.rc5
PATCH 2/12 : Add return code to icmp_init.
In reply to Denis V. Lunevicmp_init could fail and this is normal for namespace other than initial. So, the panic should be triggered only on init_net initialization path.
Additionally create rollback path for icmp_init as a separate function. It will also be used later during namespace destruction.
--- include/net/icmp.h | 2 +- net/ipv4/af_inet.c | 3 ++- net/ipv4/icmp.c | 26 ++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 6 deletions(-)
include/net/icmp.h +1 -1
@@ -48,7 +48,7 @@ struct sk_buff;extern void icmp_send (struct sk_buff *skb_in,int type,int code,__be32 info);extern int icmp_rcv (struct sk_buff *skb);extern int icmp_ioctl (struct sock *sk,int cmd,unsigned long arg); -extern void icmp_init (void ); +extern int icmp_init (void );extern void icmp_out_count (unsigned char type);/* Move into dst.h ? */
net/ipv4/af_inet.c +2 -1
@@ -1430,7 +1430,8 @@ static int __init inet_init(void)* Set the ICMP layer up */ -icmp_init (); +if (icmp_init () <0 ) +panic ("Failed to create the ICMP control socket.\n" );/* * Initialise the multicast router
net/ipv4/icmp.c +22 -4
@@ -1139,19 +1139,32 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = { }, }; -void __init icmp_init (void ) +static void __exit icmp_exit(void) { -struct inet_sock *inet;int i;for_each_possible_cpu (i) { -int err; +struct socket *sock; + + sock =per_cpu (__icmp_socket, i); +if (sock ==NULL ) +continue ; +per_cpu (__icmp_socket, i) =NULL ; +sock_release (sock); + } +} +int __init icmp_init (void ) +{ +struct inet_sock *inet; +int i, err; + +for_each_possible_cpu (i) { err =sock_create_kern (PF_INET ,SOCK_RAW ,IPPROTO_ICMP , &per_cpu (__icmp_socket, i));if (err <0 ) -panic ("Failed to create the ICMP control socket.\n" ); + goto fail;per_cpu (__icmp_socket, i)->sk->sk_allocation =GFP_ATOMIC ;
@@ -1171,6 +1184,11 @@ void __init icmp_init(void)*/ per_cpu (__icmp_socket, i)->sk->sk_prot->unhash (per_cpu (__icmp_socket, i)->sk); } +return 0 ; + +fail: +icmp_exit (); +return err; }EXPORT_SYMBOL (icmp_err_convert);
Signature · 1 line
1.5.3.rc5
PATCH 3/12 : Optimize icmp_socket usage.
In reply to Denis V. LunevUse this macro only once in a function to save a bit of space.
add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-98 (-98)
function old new delta icmp_reply 562 561 -1 icmp_push_reply 305 258 -47 icmp_init 273 223 -50
--- net/ipv4/icmp.c | 29 +++++++++++++++++------------ 1 files changed, 17 insertions(+), 12 deletions(-)
net/ipv4/icmp.c +17 -12
@@ -346,19 +346,21 @@ static int icmp_glue_bits(void *from, char *to, int offset, int len, int odd,static void icmp_push_reply (struct icmp_bxm *icmp_param,struct ipcm_cookie *ipc,struct rtable *rt) { +struct sock *sk;struct sk_buff *skb; -if (ip_append_data (icmp_socket->sk, icmp_glue_bits, icmp_param, + sk = icmp_socket->sk; +if (ip_append_data (sk, icmp_glue_bits, icmp_param, icmp_param->data_len+icmp_param->head_len, icmp_param->head_len, ipc, rt,MSG_DONTWAIT ) <0 ) -ip_flush_pending_frames (icmp_socket->sk); -else if ((skb =skb_peek (&icmp_socket->sk->sk_write_queue)) !=NULL ) { +ip_flush_pending_frames (sk); +else if ((skb =skb_peek (&sk->sk_write_queue)) !=NULL ) {struct icmphdr *icmph =icmp_hdr (skb);__wsum csum =0 ;struct sk_buff *skb1; - skb_queue_walk(&icmp_socket->sk->sk_write_queue, skb1) { + skb_queue_walk(&sk->sk_write_queue, skb1) { csum =csum_add (csum, skb1->csum); } csum = csum_partial_copy_nocheck((void *)&icmp_param->data,
@@ -366,7 +368,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param, icmp_param->head_len, csum); icmph->checksum =csum_fold (csum); skb->ip_summed =CHECKSUM_NONE ; -ip_push_pending_frames (icmp_socket->sk); +ip_push_pending_frames (sk); } }
@@ -1156,25 +1158,28 @@ static void __exit icmp_exit(void)int __init icmp_init (void ) { -struct inet_sock *inet;int i, err;for_each_possible_cpu (i) { - err =sock_create_kern (PF_INET ,SOCK_RAW ,IPPROTO_ICMP , - &per_cpu (__icmp_socket, i)); +struct sock *sk; +struct socket *sock; +struct inet_sock *inet; + err =sock_create_kern (PF_INET ,SOCK_RAW ,IPPROTO_ICMP , &sock);if (err <0 ) goto fail; -per_cpu (__icmp_socket, i)->sk->sk_allocation =GFP_ATOMIC ; +per_cpu (__icmp_socket, i) = sock; + sk = sock->sk; + sk->sk_allocation =GFP_ATOMIC ;/* Enough space for 2 64K ICMP packets, including * sk_buff struct overhead. */ -per_cpu (__icmp_socket, i)->sk->sk_sndbuf = + sk->sk_sndbuf = (2 * ((64 *1024 ) +sizeof (struct sk_buff ))); - inet =inet_sk (per_cpu (__icmp_socket, i)->sk); + inet =inet_sk (sk); inet->uc_ttl =-1 ; inet->pmtudisc =IP_PMTUDISC_DONT ;
@@ -1182,7 +1187,7 @@ int __init icmp_init(void)* see it, we do not wish this socket to see incoming * packets. */ -per_cpu (__icmp_socket, i)->sk->sk_prot->unhash (per_cpu (__icmp_socket, i)->sk); + sk->sk_prot->unhash (sk); }return 0 ;
Signature · 1 line
1.5.3.rc5
PATCH 4/12 : Store sock rather than socket for ICMP flow control.
In reply to Denis V. LunevBasically, there is no difference, what to store: socket or sock. Though, sock looks better as there will be 1 less dereferrence on the fast path.
--- net/ipv4/icmp.c | 27 +++++++++++++-------------- net/ipv6/icmp.c | 25 +++++++++++++------------ 2 files changed, 26 insertions(+), 26 deletions(-)
net/ipv4/icmp.c +13 -14
@@ -229,14 +229,14 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];* * On SMP we have one ICMP socket per-cpu. */ -static DEFINE_PER_CPU(struct socket *, __icmp_socket) = NULL; -#define icmp_socket __get_cpu_var(__icmp_socket) +static DEFINE_PER_CPU(struct sock *, __icmp_sk) = NULL; +#define icmp_sk __get_cpu_var(__icmp_sk)static inline int icmp_xmit_lock (void ) {local_bh_disable (); -if (unlikely (!spin_trylock (&icmp_socket->sk->sk_lock.slock))) { +if (unlikely (!spin_trylock (&icmp_sk->sk_lock.slock))) {/* This can happen if the output path signals a * dst_link_failure() for an outgoing ICMP packet. */
@@ -248,7 +248,7 @@ static inline int icmp_xmit_lock(void)static inline void icmp_xmit_unlock (void ) { -spin_unlock_bh (&icmp_socket->sk->sk_lock.slock); +spin_unlock_bh (&icmp_sk->sk_lock.slock); }/*
@@ -349,7 +349,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,struct sock *sk;struct sk_buff *skb; - sk = icmp_socket->sk; + sk = icmp_sk; if (ip_append_data(sk, icmp_glue_bits, icmp_param, icmp_param->data_len+icmp_param->head_len, icmp_param->head_len,
@@ -378,7 +378,7 @@ static void icmp_push_reply(struct icmp_bxm *icmp_param,static void icmp_reply (struct icmp_bxm *icmp_param,struct sk_buff *skb) { -struct sock *sk = icmp_socket->sk; +struct sock *sk = icmp_sk;struct inet_sock *inet =inet_sk (sk);struct ipcm_cookie ipc;struct rtable *rt = (struct rtable *)skb->dst;
@@ -546,7 +546,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info) icmp_param.data.icmph.checksum =0 ; icmp_param.skb = skb_in; icmp_param.offset =skb_network_offset (skb_in); -inet_sk (icmp_socket->sk)->tos = tos; +inet_sk (icmp_sk)->tos = tos; ipc.addr = iph->saddr; ipc.opt = &icmp_param.replyopts;
@@ -1146,13 +1146,13 @@ static void __exit icmp_exit(void)int i;for_each_possible_cpu (i) { -struct socket *sock; +struct sock *sk; - sock =per_cpu (__icmp_socket, i); -if (sock ==NULL ) + sk =per_cpu (__icmp_sk, i); +if (sk ==NULL )continue ; -per_cpu (__icmp_socket, i) =NULL ; -sock_release (sock); +per_cpu (__icmp_sk, i) =NULL ; +sock_release (sk->sk_socket); } }
@@ -1169,8 +1169,7 @@ int __init icmp_init(void)if (err <0 ) goto fail; -per_cpu (__icmp_socket, i) = sock; - sk = sock->sk; +per_cpu (__icmp_sk, i) = sk = sock->sk; sk->sk_allocation =GFP_ATOMIC ;/* Enough space for 2 64K ICMP packets, including
net/ipv6/icmp.c +13 -12
@@ -80,8 +80,8 @@ EXPORT_SYMBOL(icmpv6msg_statistics);* * On SMP we have one ICMP socket per-cpu. */ -static DEFINE_PER_CPU(struct socket *, __icmpv6_socket) = NULL; -#define icmpv6_socket __get_cpu_var(__icmpv6_socket) +static DEFINE_PER_CPU(struct sock *, __icmpv6_sk) = NULL; +#define icmpv6_sk __get_cpu_var(__icmpv6_sk)static int icmpv6_rcv (struct sk_buff *skb);
@@ -94,7 +94,7 @@ static __inline__ int icmpv6_xmit_lock(void) {local_bh_disable (); -if (unlikely (!spin_trylock (&icmpv6_socket->sk->sk_lock.slock))) { +if (unlikely (!spin_trylock (&icmpv6_sk->sk_lock.slock))) {/* This can happen if the output path (f.e. SIT or * ip6ip6 tunnel) signals dst_link_failure() for an * outgoing ICMP6 packet.
@@ -107,7 +107,7 @@ static __inline__ int icmpv6_xmit_lock(void)static __inline__void icmpv6_xmit_unlock (void ) { -spin_unlock_bh (&icmpv6_socket->sk->sk_lock.slock); +spin_unlock_bh (&icmpv6_sk->sk_lock.slock); }/*
@@ -392,7 +392,7 @@ void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info,if (icmpv6_xmit_lock ())return ; - sk = icmpv6_socket->sk; + sk = icmpv6_sk; np =inet6_sk (sk);if (!icmpv6_xrlim_allow (sk, type, &fl))
@@ -538,7 +538,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb)if (icmpv6_xmit_lock ())return ; - sk = icmpv6_socket->sk; + sk = icmpv6_sk; np =inet6_sk (sk);if (!fl.oif &&ipv6_addr_is_multicast (&fl.fl6_dst))
@@ -776,7 +776,7 @@ drop_no_count: }/* -* Special lock-class for __icmpv6_socket: +* Special lock-class for __icmpv6_sk: */ static struct lock_class_key icmpv6_socket_sk_dst_lock_key;
@@ -786,8 +786,9 @@ int __init icmpv6_init(void)int err, i, j;for_each_possible_cpu (i) { +struct socket *sock; err =sock_create_kern (PF_INET6 ,SOCK_RAW ,IPPROTO_ICMPV6 , - &per_cpu (__icmpv6_socket, i)); + &sock);if (err <0 ) {printk (KERN_ERR "Failed to initialize the ICMP6 control socket "
@@ -796,12 +797,12 @@ int __init icmpv6_init(void) goto fail; } - sk =per_cpu (__icmpv6_socket, i)->sk; +per_cpu (__icmpv6_sk, i) = sk = sock->sk; sk->sk_allocation =GFP_ATOMIC ;/* * Split off their lock-class, because sk->sk_dst_lock * gets used from softirqs, which is safe for -* __icmpv6_socket (because those never get directly used +* __icmpv6_sk (because those never get directly used * via userspace syscalls), but unsafe for normal sockets. */ lockdep_set_class(&sk->sk_dst_lock,
@@ -829,7 +830,7 @@ int __init icmpv6_init(void)for (j =0 ; j < i; j++) {if (!cpu_possible (j))continue ; -sock_release (per_cpu (__icmpv6_socket, j)); +sock_release (per_cpu (__icmpv6_sk, j)->sk_socket); }return err;
@@ -840,7 +841,7 @@ void icmpv6_cleanup(void)int i;for_each_possible_cpu (i) { -sock_release (per_cpu (__icmpv6_socket, i)); +sock_release (per_cpu (__icmpv6_sk, i)->sk_socket); }inet6_del_protocol (&icmpv6_protocol,IPPROTO_ICMPV6 ); }
Signature · 1 line
1.5.3.rc5
PATCH 5/12 : Pass proper ICMP socket into icmp(v6)_xmit_(un)lock.
In reply to Denis V. LunevWe have to get socket lock inside icmp(v6)_xmit_lock/unlock. The socket is get from global variable now. When this code became namespaces, one should pass a namespace and get socket from it.
Though, above is useless. Socket is available in the caller, just pass it inside. This saves a bit of code now and saves more later.
add/remove: 0/0 grow/shrink: 1/3 up/down: 1/-169 (-168)
function old new delta icmp_rcv 718 719 +1 icmpv6_rcv 2343 2303 -40 icmp_send 1566 1518 -48 icmp_reply 549 468 -81
--- net/ipv4/icmp.c | 19 ++++++++++--------- net/ipv6/icmp.c | 24 ++++++++++++------------ 2 files changed, 22 insertions(+), 21 deletions(-)
net/ipv4/icmp.c +10 -9
@@ -232,11 +232,11 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1]; static DEFINE_PER_CPU(struct sock *, __icmp_sk) = NULL;#define icmp_sk __get_cpu_var(__icmp_sk) -static inline int icmp_xmit_lock (void ) +static inline int icmp_xmit_lock (struct sock *sk) {local_bh_disable (); -if (unlikely (!spin_trylock (&icmp_sk->sk_lock.slock))) { +if (unlikely (!spin_trylock (&sk->sk_lock.slock))) {/* This can happen if the output path signals a * dst_link_failure() for an outgoing ICMP packet. */
@@ -246,9 +246,9 @@ static inline int icmp_xmit_lock(void)return 0 ; } -static inline void icmp_xmit_unlock (void ) +static inline void icmp_xmit_unlock (struct sock *sk) { -spin_unlock_bh (&icmp_sk->sk_lock.slock); +spin_unlock_bh (&sk->sk_lock.slock); }/*
@@ -387,7 +387,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)if (ip_options_echo (&icmp_param->replyopts, skb))return ; -if (icmp_xmit_lock ()) +if (icmp_xmit_lock (sk))return ; icmp_param->data.icmph.checksum =0 ;
@@ -415,7 +415,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)icmp_push_reply (icmp_param, &ipc, rt);ip_rt_put (rt); out_unlock: -icmp_xmit_unlock (); +icmp_xmit_unlock (sk); }
@@ -440,6 +440,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)__be32 saddr;u8 tos;struct net *net; +struct sock *sk = icmp_sk;if (!rt) goto out;
@@ -507,7 +508,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info) } } -if (icmp_xmit_lock ()) +if (icmp_xmit_lock (sk))return ;/*
@@ -546,7 +547,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info) icmp_param.data.icmph.checksum =0 ; icmp_param.skb = skb_in; icmp_param.offset =skb_network_offset (skb_in); -inet_sk (icmp_sk)->tos = tos; +inet_sk (sk)->tos = tos; ipc.addr = iph->saddr; ipc.opt = &icmp_param.replyopts;
@@ -654,7 +655,7 @@ route_done: ende:ip_rt_put (rt); out_unlock: -icmp_xmit_unlock (); +icmp_xmit_unlock (sk); out:; }
net/ipv6/icmp.c +12 -12
@@ -90,11 +90,11 @@ static struct inet6_protocol icmpv6_protocol = { .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, }; -static __inline__int icmpv6_xmit_lock (void ) +static __inline__int icmpv6_xmit_lock (struct sock *sk) {local_bh_disable (); -if (unlikely (!spin_trylock (&icmpv6_sk->sk_lock.slock))) { +if (unlikely (!spin_trylock (&sk->sk_lock.slock))) {/* This can happen if the output path (f.e. SIT or * ip6ip6 tunnel) signals dst_link_failure() for an * outgoing ICMP6 packet.
@@ -105,9 +105,9 @@ static __inline__ int icmpv6_xmit_lock(void)return 0 ; } -static __inline__void icmpv6_xmit_unlock (void ) +static __inline__void icmpv6_xmit_unlock (struct sock *sk) { -spin_unlock_bh (&icmpv6_sk->sk_lock.slock); +spin_unlock_bh (&sk->sk_lock.slock); }/*
@@ -389,12 +389,12 @@ void icmpv6_send(struct sk_buff *skb, int type, int code, __u32 info, fl.fl_icmp_code = code;security_skb_classify_flow (skb, &fl); -if (icmpv6_xmit_lock ()) -return ; - sk = icmpv6_sk; np =inet6_sk (sk); +if (icmpv6_xmit_lock (sk)) +return ; +if (!icmpv6_xrlim_allow (sk, type, &fl)) goto out;
@@ -498,7 +498,7 @@ out_put: out_dst_release:dst_release (dst); out: -icmpv6_xmit_unlock (); +icmpv6_xmit_unlock (sk); }EXPORT_SYMBOL (icmpv6_send);
@@ -535,12 +535,12 @@ static void icmpv6_echo_reply(struct sk_buff *skb) fl.fl_icmp_type =ICMPV6_ECHO_REPLY ;security_skb_classify_flow (skb, &fl); -if (icmpv6_xmit_lock ()) -return ; - sk = icmpv6_sk; np =inet6_sk (sk); +if (icmpv6_xmit_lock (sk)) +return ; +if (!fl.oif &&ipv6_addr_is_multicast (&fl.fl6_dst)) fl.oif = np->mcast_oif;
@@ -584,7 +584,7 @@ out_put:in6_dev_put (idev);dst_release (dst); out: -icmpv6_xmit_unlock (); +icmpv6_xmit_unlock (sk); }static void icmpv6_notify (struct sk_buff *skb,int type,int code,__be32 info)
Signature · 1 line
1.5.3.rc5
PATCH 6/12 : Allocate data for __icmp(v6)_sk dynamically.
In reply to Denis V. LunevOwn __icmp(v6)_sk should be present in each namespace. So, it should be allocated dynamically. Though, alloc_percpu does not fit the case as it implies additional dereferrence for no bonus.
Allocate data for pointers just like __percpu_alloc_mask does and place pointers to struct sock into this array.
--- net/ipv4/icmp.c | 15 ++++++++++----- net/ipv6/icmp.c | 14 +++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-)
net/ipv4/icmp.c +10 -5
@@ -229,8 +229,8 @@ static const struct icmp_control icmp_pointers[NR_ICMP_TYPES+1];* * On SMP we have one ICMP socket per-cpu. */ -static DEFINE_PER_CPU(struct sock *, __icmp_sk) = NULL; -#define icmp_sk __get_cpu_var(__icmp_sk) +static struct sock **__icmp_sk =NULL ; +#define icmp_sk (__icmp_sk[smp_processor_id()])static inline int icmp_xmit_lock (struct sock *sk) {
@@ -1149,18 +1149,23 @@ static void __exit icmp_exit(void)for_each_possible_cpu (i) {struct sock *sk; - sk =per_cpu (__icmp_sk, i); + sk = __icmp_sk[i];if (sk ==NULL )continue ; -per_cpu (__icmp_sk, i) =NULL ;sock_release (sk->sk_socket); } +kfree (__icmp_sk); + __icmp_sk =NULL ; }int __init icmp_init (void ) {int i, err; + __icmp_sk =kzalloc (nr_cpu_ids *sizeof (struct sock *),GFP_KERNEL ); +if (__icmp_sk ==NULL ) +return -ENOMEM ; +for_each_possible_cpu (i) {struct sock *sk;struct socket *sock;
@@ -1170,7 +1175,7 @@ int __init icmp_init(void)if (err <0 ) goto fail; -per_cpu (__icmp_sk, i) = sk = sock->sk; + __icmp_sk[i] = sk = sock->sk; sk->sk_allocation =GFP_ATOMIC ;/* Enough space for 2 64K ICMP packets, including
net/ipv6/icmp.c +9 -5
@@ -80,8 +80,8 @@ EXPORT_SYMBOL(icmpv6msg_statistics);* * On SMP we have one ICMP socket per-cpu. */ -static DEFINE_PER_CPU(struct sock *, __icmpv6_sk) = NULL; -#define icmpv6_sk __get_cpu_var(__icmpv6_sk) +static struct sock **__icmpv6_sk =NULL ; +#define icmpv6_sk (__icmpv6_sk[smp_processor_id()])static int icmpv6_rcv (struct sk_buff *skb);
@@ -785,6 +785,10 @@ int __init icmpv6_init(void)struct sock *sk;int err, i, j; + __icmpv6_sk =kzalloc (nr_cpu_ids *sizeof (struct sock *),GFP_KERNEL ); +if (__icmpv6_sk ==NULL ) +return -ENOMEM ; +for_each_possible_cpu (i) {struct socket *sock; err = sock_create_kern(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6,
@@ -797,7 +801,7 @@ int __init icmpv6_init(void) goto fail; } -per_cpu (__icmpv6_sk, i) = sk = sock->sk; + __icmpv6_sk[i] = sk = sock->sk; sk->sk_allocation =GFP_ATOMIC ;/* * Split off their lock-class, because sk->sk_dst_lock
@@ -830,7 +834,7 @@ int __init icmpv6_init(void)for (j =0 ; j < i; j++) {if (!cpu_possible (j))continue ; -sock_release (per_cpu (__icmpv6_sk, j)->sk_socket); +sock_release (__icmpv6_sk[j]->sk_socket); }return err;
@@ -841,7 +845,7 @@ void icmpv6_cleanup(void)int i;for_each_possible_cpu (i) { -sock_release (per_cpu (__icmpv6_sk, i)->sk_socket); +sock_release (__icmpv6_sk[i]->sk_socket); }inet6_del_protocol (&icmpv6_protocol,IPPROTO_ICMPV6 ); }
Signature · 1 line
1.5.3.rc5
PATCH 7/12 No need for a separate __netlink_release call.
In reply to Denis V. LunevMerge it to netlink_kernel_release.
--- net/netlink/af_netlink.c | 30 ++++++++++++------------------ 1 files changed, 12 insertions(+), 18 deletions(-)
net/netlink/af_netlink.c +12 -18
@@ -1344,22 +1344,6 @@ static void netlink_data_ready(struct sock *sk, int len)* queueing. */ -static void __netlink_release (struct sock *sk) -{ -/* -* Last sock_put should drop referrence to sk->sk_net. It has already -* been dropped in netlink_kernel_create. Taking referrence to stopping -* namespace is not an option. -* Take referrence to a socket to remove it from netlink lookup table -* _alive_ and after that destroy it in the context of init_net. -*/ - -sock_hold (sk); -sock_release (sk->sk_socket); - sk->sk_net =get_net (&init_net); -sock_put (sk); -} -struct sock * netlink_kernel_create(struct net *net, int unit, unsigned int groups, void (*input)(struct sk_buff *skb),
@@ -1424,7 +1408,7 @@ netlink_kernel_create(struct net *net, int unit, unsigned int groups, out_sock_release:kfree (listeners); -__netlink_release (sk); +netlink_kernel_release (sk);return NULL ; out_sock_release_nosk:
@@ -1437,10 +1421,20 @@ EXPORT_SYMBOL(netlink_kernel_create);void netlink_kernel_release (struct sock *sk) { +/* +* Last sock_put should drop referrence to sk->sk_net. It has already +* been dropped in netlink_kernel_create. Taking referrence to stopping +* namespace is not an option. +* Take referrence to a socket to remove it from netlink lookup table +* _alive_ and after that destroy it in the context of init_net. +*/ if (sk ==NULL || sk->sk_socket ==NULL )return ; -__netlink_release (sk); +sock_hold (sk); +sock_release (sk->sk_socket); + sk->sk_net =get_net (&init_net); +sock_put (sk); }EXPORT_SYMBOL (netlink_kernel_release);
Signature · 1 line
1.5.3.rc5
PATCH 8/12 Make netlink_kernel_release publically available as sk_release_kernel.
In reply to Denis V. LunevThis staff will be needed for non-netlink kernel sockets, which should also not pin a namespace like tcp_socket and icmp_socket.
--- include/net/sock.h | 13 +++++++++++++ net/core/sock.c | 18 ++++++++++++++++++ net/netlink/af_netlink.c | 18 ++---------------- 3 files changed, 33 insertions(+), 16 deletions(-)
include/net/sock.h +13 -0
@@ -850,6 +850,7 @@ extern struct sock *sk_alloc(struct net *net, int family,gfp_t priority, struct proto *prot);extern void sk_free (struct sock *sk); +extern void sk_release_kernel (struct sock *sk);extern struct sock *sk_clone (const struct sock *sk,const gfp_t priority);
@@ -1333,6 +1334,18 @@ static inline void sk_eat_skb(struct sock *sk, struct sk_buff *skb, int copied_e }#endif +/* +* Kernel sockets, f.e. rtnl or icmp_socket, are a part of a namespace. +* They should not hold a referrence to a namespace in order to allow +* to stop it. +* Sockets after sk_change_net should be released using sk_release_kernel +*/ +static inline void sk_change_net (struct sock *sk,struct net *net) +{ +put_net (sk->sk_net); + sk->sk_net = net; +} +extern void sock_enable_timestamp (struct sock *sk);extern int sock_get_timestamp (struct sock *,struct timeval __user *);extern int sock_get_timestampns (struct sock *,struct timespec __user *);
net/core/sock.c +18 -0
@@ -987,6 +987,24 @@ void sk_free(struct sock *sk)sk_prot_free (sk->sk_prot_creator, sk); } +/* +* Last sock_put should drop referrence to sk->sk_net. It has already +* been dropped in sk_change_net. Taking referrence to stopping namespace +* is not an option. +* Take referrence to a socket to remove it from hash _alive_ and after that +* destroy it in the context of init_net. +*/ +void sk_release_kernel (struct sock *sk) +{ +if (sk ==NULL || sk->sk_socket ==NULL ) +return ; + +sock_hold (sk); +sock_release (sk->sk_socket); + sk->sk_net =get_net (&init_net); +sock_put (sk); +} +struct sock *sk_clone (const struct sock *sk,const gfp_t priority) {struct sock *newsk;
net/netlink/af_netlink.c +2 -16
@@ -1372,8 +1372,7 @@ netlink_kernel_create(struct net *net, int unit, unsigned int groups, goto out_sock_release_nosk; sk = sock->sk; -put_net (sk->sk_net); - sk->sk_net = net; +sk_change_net (sk, net);if (groups <32 ) groups =32 ;
@@ -1421,20 +1420,7 @@ EXPORT_SYMBOL(netlink_kernel_create);void netlink_kernel_release (struct sock *sk) { -/* -* Last sock_put should drop referrence to sk->sk_net. It has already -* been dropped in netlink_kernel_create. Taking referrence to stopping -* namespace is not an option. -* Take referrence to a socket to remove it from netlink lookup table -* _alive_ and after that destroy it in the context of init_net. -*/ -if (sk ==NULL || sk->sk_socket ==NULL ) -return ; - -sock_hold (sk); -sock_release (sk->sk_socket); - sk->sk_net =get_net (&init_net); -sock_put (sk); +sk_release_kernel (sk); }EXPORT_SYMBOL (netlink_kernel_release);
Signature · 1 line
1.5.3.rc5
PATCH 9/12 : icmp(v6)_sk should not pin a namespace.
In reply to Denis V. LunevSo, change icmp(v6)_sk creation/disposal to the scheme used in the netlink for rtnl, i.e. create a socket in the context of the init_net and assign the namespace without getting a referrence later.
Also use sk_release_kernel instead of sock_release to properly destroy such sockets.
--- net/ipv4/icmp.c | 12 ++++-------- net/ipv6/icmp.c | 11 +++++------ 2 files changed, 9 insertions(+), 14 deletions(-)
net/ipv4/icmp.c +4 -8
@@ -1146,14 +1146,8 @@ static void __exit icmp_exit(void) {int i; -for_each_possible_cpu (i) { -struct sock *sk; - - sk = __icmp_sk[i]; -if (sk ==NULL ) -continue ; -sock_release (sk->sk_socket); - } +for_each_possible_cpu (i) +sk_release_kernel (__icmp_sk[i]);kfree (__icmp_sk); __icmp_sk =NULL ; }
@@ -1176,6 +1170,8 @@ int __init icmp_init(void) goto fail; __icmp_sk[i] = sk = sock->sk; +sk_change_net (sk, &init_net); + sk->sk_allocation =GFP_ATOMIC ;/* Enough space for 2 64K ICMP packets, including
net/ipv6/icmp.c +5 -6
@@ -802,6 +802,8 @@ int __init icmpv6_init(void) } __icmpv6_sk[i] = sk = sock->sk; +sk_change_net (sk, &init_net); + sk->sk_allocation =GFP_ATOMIC ;/* * Split off their lock-class, because sk->sk_dst_lock
@@ -831,11 +833,8 @@ int __init icmpv6_init(void)return 0 ; fail: -for (j =0 ; j < i; j++) { -if (!cpu_possible (j)) -continue ; -sock_release (__icmpv6_sk[j]->sk_socket); - } +for (j =0 ; j < i; j++) +sk_release_kernel (__icmpv6_sk[j]);return err; }
@@ -845,7 +844,7 @@ void icmpv6_cleanup(void)int i;for_each_possible_cpu (i) { -sock_release (__icmpv6_sk[i]->sk_socket); +sk_release_kernel (__icmpv6_sk[i]); }inet6_del_protocol (&icmpv6_protocol,IPPROTO_ICMPV6 ); }
Signature · 1 line
1.5.3.rc5
David Miller
maintainerRe: [PATCH 9/12 net-2.6.26] [NETNS]: icmp(v6)_sk should not pin a namespace.
From: "Denis V. Lunev" <[email protected]>
Date: Fri, 29 Feb 2008 16:40:55 +0300
Quoted 9 lines
So, change icmp(v6)_sk creation/disposal to the scheme used in the netlink for rtnl, i.e. create a socket in the context of the
init_netand assign the namespace without getting a referrence later.Also use
sk_release_kernelinstead ofsock_releaseto properly destroy such sockets.Signed-off-by Denis V. Lunev <[email protected]>Acked-by Daniel Lezcano <[email protected]>
Please validate the build in the future, thanks :-/
I just checked in the following:
commit 45af1754bc09926b5e062bda24f789d7b320939f
Author: David S. Miller <[email protected]> Date: Fri Feb 29 11:33:19 2008 -0800
[NET]: sk_release_kernel needs to be exported to modules
Fixes:
ERROR: "sk_release_kernel" [net/ipv6/ipv6.ko] undefined!
net/core/sock.c +1 -0
@@ -1004,6 +1004,7 @@ void sk_release_kernel(struct sock *sk) sk->sk_net =get_net (&init_net);sock_put (sk); } +EXPORT_SYMBOL (sk_release_kernel);struct sock *sk_clone (const struct sock *sk,const gfp_t priority) {
PATCH 10/12 : Make icmp_sk per namespace.
In reply to Denis V. LunevAll preparations are done. Now just add a hook to perform an initialization on namespace startup and replace icmp_sk macro with proper inline call.
--- include/net/netns/ipv4.h | 2 + net/ipv4/icmp.c | 49 ++++++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 17 deletions(-)
include/net/netns/ipv4.h +2 -0
@@ -26,6 +26,8 @@ struct netns_ipv4 {struct hlist_head *fib_table_hash;struct sock *fibnl; +struct sock **icmp_sk; +struct netns_frags frags;#ifdef CONFIG_NETFILTER struct xt_table *iptable_filter;
net/ipv4/icmp.c +32 -17
115 lines not on this page · raw message
Signature · 1 line
1.5.3.rc5
PATCH 11/12 : Make icmpv6_sk per namespace.
In reply to Denis V. LunevAll preparations are done. Now just add a hook to perform an initialization on namespace startup and replace icmpv6_sk macro with proper inline call. Actual namespace the packet belongs too will be passed later along with the one for the routing.
--- include/net/netns/ipv6.h | 1 + net/ipv6/icmp.c | 68 +++++++++++++++++++++++++++++++-------------- 2 files changed, 48 insertions(+), 21 deletions(-)
include/net/netns/ipv6.h +1 -0
@@ -36,5 +36,6 @@ struct netns_ipv6 {struct xt_table *ip6table_mangle;struct xt_table *ip6table_raw;#endif + struct sock **icmp_sk; };#endif
net/ipv6/icmp.c +47 -21
123 lines not on this page · raw message
Signature · 1 line
1.5.3.rc5
PATCH 12/12 : Consolidate fail path icmpv6_sk_init with icmpv6_sk_exit.
In reply to Denis V. Lunev--- net/ipv6/icmp.c | 29 ++++++++++++++--------------- 1 files changed, 14 insertions(+), 15 deletions(-)
net/ipv6/icmp.c +14 -15
@@ -777,6 +777,17 @@ drop_no_count:return 0 ; } + +static void __net_exit icmpv6_sk_exit(struct net *net) +{ +int i; + +for_each_possible_cpu (i) { +sk_release_kernel (net->ipv6.icmp_sk[i]); + } +kfree (net->ipv6.icmp_sk); +} +/* * Special lock-class for __icmpv6_sk: */
@@ -785,7 +796,7 @@ static struct lock_class_key icmpv6_socket_sk_dst_lock_key;static int __net_init icmpv6_sk_init (struct net *net) {struct sock *sk; -int err, i, j; +int err, i; net->ipv6.icmp_sk =kzalloc (nr_cpu_ids *sizeof (struct sock *),GFP_KERNEL );
@@ -827,23 +838,11 @@ static int __net_init icmpv6_sk_init(struct net *net) }return 0 ; - fail: -for (j =0 ; j < i; j++) -sk_release_kernel (net->ipv6.icmp_sk[j]); -kfree (net->ipv6.icmp_sk); +fail: +icmpv6_sk_exit (net);return err; } -static void __net_exit icmpv6_sk_exit(struct net *net) -{ -int i; - -for_each_possible_cpu (i) { -sk_release_kernel (net->ipv6.icmp_sk[i]); - } -kfree (net->ipv6.icmp_sk); -} -static struct pernet_operations __net_initdata icmpv6_sk_ops = { .init = icmpv6_sk_init, .exit = icmpv6_sk_exit,
Signature · 1 line
1.5.3.rc5
David Miller
maintainerRe: [PATCH 12/12 net-2.6.26] [ICMP6]: Consolidate fail path icmpv6_sk_init with icmpv6_sk_exit.
From: "Denis V. Lunev" <[email protected]>
Date: Fri, 29 Feb 2008 16:40:58 +0300
net/ipv6/icmp.c drop_no_count: · line 782
Quoted 2 lines
net/ipv6/icmp.c +2
-0+static void __net_exit icmpv6_sk_exit(struct net *net) +{
Since you call this from __new_init code, won't this create a section conflict when ipv6 is built statically into the kernel?
Purpose of __net_exit & friends [Was: [ICMP6]: Consolidate fail ...]
Quoted 9 lines · David Miller
From: "Denis V. Lunev" <[email protected]>
Date: Fri, 29 Feb 2008 16:40:58 +0300net/ipv6/icmp.c drop_no_count: · line 782
Quoted 2 lines
net/ipv6/icmp.c +2
-0+static void __net_exit icmpv6_sk_exit(struct net *net) +{Since you call this from
__new_initcode, won't this create a section conflict when ipv6 is built statically into the kernel?
I have noticed this __net_init/__net_exit stuff before but never got around to ask about it.
Whats the actual purpose and are there any strict rules as to what may be called from where?
If there are we should apply the same checks as we do for __devinit/__devexit and friends.
Sam
David Miller
maintainerRe: Purpose of __net_exit & friends
From: Sam Ravnborg <[email protected]>
Date: Fri, 29 Feb 2008 21:12:47 +0100
Quoted 11 lines · David Miller
Quoted 9 lines
From: "Denis V. Lunev" <[email protected]>
Date: Fri, 29 Feb 2008 16:40:58 +0300net/ipv6/icmp.c drop_no_count: · line 782
Quoted 2 lines
net/ipv6/icmp.c +2
-0+static void __net_exit icmpv6_sk_exit(struct net *net) +{Since you call this from
__new_initcode, won't this create a section conflict when ipv6 is built statically into the kernel?
Quoted 7 lines
I have noticed this __net_init/__net_exit stuff before but never got around to ask about it.
Whats the actual purpose and are there any strict rules as to what may be called from where?
If there are we should apply the same checks as we do for __devinit/__devexit and friends.
Basically, if network namespace support isn't enabled, these functions can act like normal __init and __exit functions.
Otherwise we have to keep them around so that they can be called when namespaces are created and destroyed.
Re: [PATCH 12/12 net-2.6.26] [ICMP6]: Consolidate fail path icmpv6_sk_init with icmpv6_sk_exit.
In reply to David MillerQuoted 9 lines · David Miller
From: "Denis V. Lunev" <[email protected]>
Date: Fri, 29 Feb 2008 16:40:58 +0300Review
Quoted 2 lines
(quoted diff) +2
-0+static void __net_exit icmpv6_sk_exit(struct net *net) +{Since you call this from
__new_initcode, won't this create a section conflict when ipv6 is built statically into the kernel?
Dave, you are perfectly correct :) Though, I have made a similar mistake in the IPv4 code. Pls consider the patch attached.
[ICMP]: Section conflict between icmp_sk_init/icmp_sk_exit.
Functions from __exit section should not be called from ones in __init section. Fix this conflict.
---
net/ipv4/icmp.c +3 -1
@@ -1198,7 +1198,9 @@ int __net_init icmp_sk_init(struct net *net)return 0 ; fail: -icmp_sk_exit (net); +for_each_possible_cpu (i) +sk_release_kernel (net->ipv4.icmp_sk[i]); +kfree (net->ipv4.icmp_sk);return err; }
David Miller
maintainerRe: [PATCH 12/12 net-2.6.26] [ICMP6]: Consolidate fail path icmpv6_sk_init with icmpv6_sk_exit.
From: "Denis V. Lunev" <[email protected]>
Date: Sat, 01 Mar 2008 01:05:41 +0300
Quoted 2 lines
Though, I have made a similar mistake in the IPv4 code. Pls consider the patch attached.
Applied and pushed out to net-2.6.26, thanks.
David Miller
maintainerFrom: "Denis V. Lunev" <[email protected]>
Date: Fri, 29 Feb 2008 16:39:20 +0300
Quoted 8 lines
This set of patches is targeted to create separate
icmp_socketinside each namespace. Both IPv4 and IPv6 codepaths are affected.Though, in order to do this smoothly, a bit of optimisations are performed. The kernel from now on will use sock rather than socket on the ICMP send path.
Signed-off-by Denis V. Lunev <[email protected]>
I applied everything except the final patch #12.
I'll reply to that patch posting with my feedback.