Rework inet_csk_get_port
Subsystems (5)
v2 cover Rework inet_csk_get_port
V1->V2:
-Added a new patch 'inet: collapse ipv4/v6 rcv_saddr_equal functions into one'
at Hannes' suggestion.
-Dropped ->bind_conflict and just use the new helper.
-Fixed a compile bug from the original ->bind_conflict patch.
The original description of the series follows
=========================================================
At some point recently the guys working on our load balancer added the ability to use SO_REUSEPORT. When they restarted their app with this option enabled they immediately hit a softlockup on what appeared to be the inet_bind_bucket->lock. Eventually what all of our debugging and discussion led us to was the fact that the application comes up without SO_REUSEPORT, shuts down which creates around 100k twsk's, and then comes up and tries to open a bunch of sockets using SO_REUSEPORT, which meant traversing the inet_bind_bucket owners list under the lock. Since this lock is needed for dealing with the twsk's and basically anything else related to connections we would softlockup, and sometimes not ever recover.
To solve this problem I did what you see in Path 5/5. Once we have a SO_REUSEPORT socket on the tb->owners list we know that the socket has no conflicts with any of the other sockets on that list. So we can add a copy of the sock_common (really all we need is the recv_saddr but it seemed ugly to copy just the ipv6, ipv4, and flag to indicate if we were ipv6 only in there so I've copied the whole common) in order to check subsequent SO_REUSEPORT sockets. If they match the previous one then we can skip the expensive inet_csk_bind_conflict check. This is what eliminated the soft lockup that we were seeing.
Patches 1-4 are cleanups and re-workings. For instance when we specify port == 0 we need to find an open port, but we would do two passes through inet_csk_bind_conflict every time we found a possible port. We would also keep track of the smallest_port value in order to try and use it if we found no port our first run through. This however made no sense as it would have had to fail the first pass through inet_csk_bind_conflict, so would not actually pass the second pass through either. Finally I split the function into two functions in order to make it easier to read and to distinguish between the two behaviors.
I have tested this on one of our load balancing boxes during peak traffic and it hasn't fallen over. But this is not my area, so obviously feel free to point out where I'm being stupid and I'll get it fixed up and retested. Thanks,
Josef
PATCH v2 1/6 inet: collapse ipv4/v6 rcv_saddr_equal functions into one
We pass these per-protocol equal functions around in various places, but we can just have one function that checks the sk->sk_family and then do the right comparison function. I've also changed the ipv4 version to not cast to inet_sock since it is unneeded.
--- include/net/addrconf.h | 4 +-- include/net/inet_hashtables.h | 5 +-- include/net/udp.h | 1 - net/ipv4/inet_connection_sock.c | 72 ++++++++++++++++++++++++++++++++++++++++ net/ipv4/inet_hashtables.c | 16 +++------ net/ipv4/udp.c | 58 +++++++------------------------- net/ipv6/inet6_connection_sock.c | 4 +-- net/ipv6/inet6_hashtables.c | 46 +------------------------ net/ipv6/udp.c | 2 +- 9 files changed, 95 insertions(+), 113 deletions(-)
include/net/addrconf.h +1 -3
@@ -88,9 +88,7 @@ int __ipv6_get_lladdr(struct inet6_dev *idev, struct in6_addr *addr, u32 banned_flags);int ipv6_get_lladdr (struct net_device *dev,struct in6_addr *addr,u32 banned_flags); -int ipv4_rcv_saddr_equal (const struct sock *sk,const struct sock *sk2, -bool match_wildcard); -int ipv6_rcv_saddr_equal (const struct sock *sk,const struct sock *sk2, +int inet_rcv_saddr_equal (const struct sock *sk,const struct sock *sk2,bool match_wildcard);void addrconf_join_solict (struct net_device *dev,const struct in6_addr *addr);void addrconf_leave_solict (struct inet6_dev *idev,const struct in6_addr *addr);
include/net/inet_hashtables.h +1 -4
@@ -203,10 +203,7 @@ void inet_hashinfo_init(struct inet_hashinfo *h);bool inet_ehash_insert (struct sock *sk,struct sock *osk);bool inet_ehash_nolisten (struct sock *sk,struct sock *osk); -int __inet_hash (struct sock *sk,struct sock *osk, -int (*saddr_same)(const struct sock *sk1, -const struct sock *sk2, -bool match_wildcard)); +int __inet_hash (struct sock *sk,struct sock *osk);int inet_hash (struct sock *sk);void inet_unhash (struct sock *sk);
include/net/udp.h +0 -1
@@ -204,7 +204,6 @@ static inline void udp_lib_close(struct sock *sk, long timeout) }int udp_lib_get_port (struct sock *sk,unsigned short snum, -int (*)(const struct sock *,const struct sock *,bool ),unsigned int hash2_nulladdr);u32 udp_flow_hashrnd (void );
net/ipv4/inet_connection_sock.c +72 -0
78 lines not on this page · raw message
net/ipv4/inet_hashtables.c +5 -11
@@ -435,10 +435,7 @@ bool inet_ehash_nolisten(struct sock *sk, struct sock *osk)EXPORT_SYMBOL_GPL (inet_ehash_nolisten);static int inet_reuseport_add_sock (struct sock *sk, -struct inet_listen_hashbucket *ilb, -int (*saddr_same)(const struct sock *sk1, -const struct sock *sk2, -bool match_wildcard)) +struct inet_listen_hashbucket *ilb) {struct inet_bind_bucket *tb =inet_csk (sk)->icsk_bind_hash;struct sock *sk2;
@@ -451,7 +448,7 @@ static int inet_reuseport_add_sock(struct sock *sk, sk2->sk_bound_dev_if == sk->sk_bound_dev_if && inet_csk(sk2)->icsk_bind_hash == tb && sk2->sk_reuseport && uid_eq(uid, sock_i_uid(sk2)) && - saddr_same(sk, sk2, false)) + inet_rcv_saddr_equal(sk, sk2, false))return reuseport_add_sock (sk, sk2); }
@@ -461,10 +458,7 @@ static int inet_reuseport_add_sock(struct sock *sk,return 0 ; } -int __inet_hash (struct sock *sk,struct sock *osk, -int (*saddr_same)(const struct sock *sk1, -const struct sock *sk2, -bool match_wildcard)) +int __inet_hash (struct sock *sk,struct sock *osk) {struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;struct inet_listen_hashbucket *ilb;
@@ -479,7 +473,7 @@ int __inet_hash(struct sock *sk, struct sock *osk,spin_lock (&ilb->lock);if (sk->sk_reuseport) { - err =inet_reuseport_add_sock (sk, ilb, saddr_same); + err =inet_reuseport_add_sock (sk, ilb);if (err) goto unlock; }
@@ -503,7 +497,7 @@ int inet_hash(struct sock *sk)if (sk->sk_state !=TCP_CLOSE ) {local_bh_disable (); - err =__inet_hash (sk,NULL , ipv4_rcv_saddr_equal); + err =__inet_hash (sk,NULL );local_bh_enable (); }
net/ipv4/udp.c +12 -46
143 lines not on this page · raw message
net/ipv6/inet6_connection_sock.c +2 -2
@@ -54,12 +54,12 @@ int inet6_csk_bind_conflict(const struct sock *sk, (sk2->sk_state != TCP_TIME_WAIT && !uid_eq(uid, sock_i_uid((struct sock *)sk2))))) { -if (ipv6_rcv_saddr_equal (sk, sk2, true)) +if (inet_rcv_saddr_equal (sk, sk2, true))break ; }if (!relax && reuse && sk2->sk_reuse && sk2->sk_state !=TCP_LISTEN && -ipv6_rcv_saddr_equal (sk, sk2, true)) +inet_rcv_saddr_equal (sk, sk2, true))break ; } }
net/ipv6/inet6_hashtables.c +1 -45
55 lines not on this page · raw message
net/ipv6/udp.c +1 -1
@@ -103,7 +103,7 @@ int udp_v6_get_port(struct sock *sk, unsigned short snum)/* precompute partial secondary hash */ udp_sk (sk)->udp_portaddr_hash = hash2_partial; -return udp_lib_get_port (sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr); +return udp_lib_get_port (sk, snum, hash2_nulladdr); }static void udp_v6_rehash (struct sock *sk)
Signature · 1 line
2.5.5
PATCH v2 2/6 inet: drop ->bind_conflict
In reply to Josef BacikThe only difference between inet6_csk_bind_conflict and inet_csk_bind_conflict is how they check the rcv_saddr, so delete this call back and simply change inet_csk_bind_conflict to call inet_rcv_saddr_equal.
--- include/net/inet6_connection_sock.h | 5 ----- include/net/inet_connection_sock.h | 6 ------ net/dccp/ipv4.c | 2 +- net/dccp/ipv6.c | 2 -- net/ipv4/inet_connection_sock.c | 22 +++++++------------- net/ipv4/tcp_ipv4.c | 2 +- net/ipv6/inet6_connection_sock.c | 40 ------------------------------------- net/ipv6/tcp_ipv6.c | 2 -- 8 files changed, 9 insertions(+), 72 deletions(-)
include/net/inet6_connection_sock.h +0 -5
@@ -15,16 +15,11 @@#include <linux/types.h> -struct inet_bind_bucket ;struct request_sock ;struct sk_buff ;struct sock ;struct sockaddr ; -int inet6_csk_bind_conflict (const struct sock *sk, -const struct inet_bind_bucket *tb,bool relax, -bool soreuseport_ok); -struct dst_entry *inet6_csk_route_req (const struct sock *sk,struct flowi6 *fl6,const struct request_sock *req,u8 proto);
include/net/inet_connection_sock.h +0 -6
@@ -62,9 +62,6 @@ struct inet_connection_sock_af_ops { char __user *optval, int __user *optlen);#endif void (*addr2sockaddr)(struct sock *sk,struct sockaddr *); -int (*bind_conflict)(const struct sock *sk, -const struct inet_bind_bucket *tb, -bool relax,bool soreuseport_ok);void (*mtu_reduced)(struct sock *sk); };
@@ -261,9 +258,6 @@ inet_csk_rto_backoff(const struct inet_connection_sock *icsk,struct sock *inet_csk_accept (struct sock *sk,int flags,int *err); -int inet_csk_bind_conflict (const struct sock *sk, -const struct inet_bind_bucket *tb,bool relax, -bool soreuseport_ok);int inet_csk_get_port (struct sock *sk,unsigned short snum); struct dst_entry *inet_csk_route_req(const struct sock *sk, struct flowi4 *fl4,
net/dccp/ipv4.c +1 -1
@@ -17,6 +17,7 @@#include <linux/skbuff.h> #include <linux/random.h> +#include <net/addrconf.h> #include <net/icmp.h> #include <net/inet_common.h> #include <net/inet_hashtables.h>
@@ -904,7 +905,6 @@ static const struct inet_connection_sock_af_ops dccp_ipv4_af_ops = { .getsockopt = ip_getsockopt, .addr2sockaddr = inet_csk_addr2sockaddr, .sockaddr_len =sizeof (struct sockaddr_in ), - .bind_conflict = inet_csk_bind_conflict, #ifdef CONFIG_COMPAT .compat_setsockopt = compat_ip_setsockopt, .compat_getsockopt = compat_ip_getsockopt,
net/dccp/ipv6.c +0 -2
@@ -937,7 +937,6 @@ static const struct inet_connection_sock_af_ops dccp_ipv6_af_ops = { .getsockopt = ipv6_getsockopt, .addr2sockaddr = inet6_csk_addr2sockaddr, .sockaddr_len =sizeof (struct sockaddr_in6 ), - .bind_conflict = inet6_csk_bind_conflict, #ifdef CONFIG_COMPAT .compat_setsockopt = compat_ipv6_setsockopt, .compat_getsockopt = compat_ipv6_getsockopt,
@@ -958,7 +957,6 @@ static const struct inet_connection_sock_af_ops dccp_ipv6_mapped = { .getsockopt = ipv6_getsockopt, .addr2sockaddr = inet6_csk_addr2sockaddr, .sockaddr_len =sizeof (struct sockaddr_in6 ), - .bind_conflict = inet6_csk_bind_conflict, #ifdef CONFIG_COMPAT .compat_setsockopt = compat_ipv6_setsockopt, .compat_getsockopt = compat_ipv6_getsockopt,
net/ipv4/inet_connection_sock.c +7 -15
@@ -116,9 +116,9 @@ void inet_get_local_port_range(struct net *net, int *low, int *high) }EXPORT_SYMBOL (inet_get_local_port_range); -int inet_csk_bind_conflict (const struct sock *sk, -const struct inet_bind_bucket *tb,bool relax, -bool reuseport_ok) +static int inet_csk_bind_conflict (const struct sock *sk, +const struct inet_bind_bucket *tb, +bool relax,bool reuseport_ok) {struct sock *sk2;bool reuse = sk->sk_reuse;
@@ -134,7 +134,6 @@ int inet_csk_bind_conflict(const struct sock *sk,sk_for_each_bound (sk2, &tb->owners) {if (sk != sk2 && - !inet_v6_ipv6only (sk2) && (!sk->sk_bound_dev_if || !sk2->sk_bound_dev_if || sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) {
@@ -144,23 +143,18 @@ int inet_csk_bind_conflict(const struct sock *sk, rcu_access_pointer(sk->sk_reuseport_cb) || (sk2->sk_state != TCP_TIME_WAIT && !uid_eq(uid, sock_i_uid(sk2))))) { - -if (!sk2->sk_rcv_saddr || !sk->sk_rcv_saddr || - sk2->sk_rcv_saddr == sk->sk_rcv_saddr) +if (inet_rcv_saddr_equal (sk, sk2, true))break ; }if (!relax && reuse && sk2->sk_reuse && sk2->sk_state !=TCP_LISTEN ) { - -if (!sk2->sk_rcv_saddr || !sk->sk_rcv_saddr || - sk2->sk_rcv_saddr == sk->sk_rcv_saddr) +if (inet_rcv_saddr_equal (sk, sk2, true))break ; } } }return sk2 !=NULL ; } -EXPORT_SYMBOL_GPL (inet_csk_bind_conflict);/* Obtain a reference to a local port for the given sock, * if snum is zero it means select any available local port.
@@ -239,8 +233,7 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) smallest_size = tb->num_owners; smallest_port = port; } -if (!inet_csk (sk)->icsk_af_ops->bind_conflict (sk, tb, false, - reuseport_ok)) +if (!inet_csk_bind_conflict (sk, tb, false, reuseport_ok)) goto tb_found; goto next_port; }
@@ -281,8 +274,7 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) sk->sk_reuseport && uid_eq(tb->fastuid, uid))) && smallest_size == -1) goto success; -if (inet_csk (sk)->icsk_af_ops->bind_conflict (sk, tb, true, - reuseport_ok)) { +if (inet_csk_bind_conflict (sk, tb, true, reuseport_ok)) { if ((reuse || (tb->fastreuseport > 0 && sk->sk_reuseport &&
net/ipv4/tcp_ipv4.c +1 -1
@@ -63,6 +63,7 @@#include <linux/times.h> #include <linux/slab.h> +#include <net/addrconf.h> #include <net/net_namespace.h> #include <net/icmp.h> #include <net/inet_hashtables.h>
@@ -1817,7 +1818,6 @@ const struct inet_connection_sock_af_ops ipv4_specific = { .getsockopt = ip_getsockopt, .addr2sockaddr = inet_csk_addr2sockaddr, .sockaddr_len =sizeof (struct sockaddr_in ), - .bind_conflict = inet_csk_bind_conflict, #ifdef CONFIG_COMPAT .compat_setsockopt = compat_ip_setsockopt, .compat_getsockopt = compat_ip_getsockopt,
net/ipv6/inet6_connection_sock.c +0 -40
@@ -28,46 +28,6 @@#include <net/inet6_connection_sock.h> #include <net/sock_reuseport.h> -int inet6_csk_bind_conflict (const struct sock *sk, -const struct inet_bind_bucket *tb,bool relax, -bool reuseport_ok) -{ -const struct sock *sk2; -bool reuse = !!sk->sk_reuse; -bool reuseport = !!sk->sk_reuseport && reuseport_ok; -kuid_t uid =sock_i_uid ((struct sock *)sk); - -/* We must walk the whole port owner list in this case. -DaveM */ -/* -* See comment in inet_csk_bind_conflict about sock lookup -* vs net namespaces issues. -*/ -sk_for_each_bound (sk2, &tb->owners) { -if (sk != sk2 && - (!sk->sk_bound_dev_if || - !sk2->sk_bound_dev_if || - sk->sk_bound_dev_if == sk2->sk_bound_dev_if)) { -if ((!reuse || !sk2->sk_reuse || - sk2->sk_state ==TCP_LISTEN ) && - (!reuseport || !sk2->sk_reuseport || -rcu_access_pointer (sk->sk_reuseport_cb) || - (sk2->sk_state !=TCP_TIME_WAIT && - !uid_eq (uid, -sock_i_uid ((struct sock *)sk2))))) { -if (inet_rcv_saddr_equal (sk, sk2, true)) -break ; - } -if (!relax && reuse && sk2->sk_reuse && - sk2->sk_state !=TCP_LISTEN && -inet_rcv_saddr_equal (sk, sk2, true)) -break ; - } - } - -return sk2 !=NULL ; -} -EXPORT_SYMBOL_GPL (inet6_csk_bind_conflict); - struct dst_entry *inet6_csk_route_req(const struct sock *sk, struct flowi6 *fl6, const struct request_sock *req,
net/ipv6/tcp_ipv6.c +0 -2
@@ -1620,7 +1620,6 @@ static const struct inet_connection_sock_af_ops ipv6_specific = { .getsockopt = ipv6_getsockopt, .addr2sockaddr = inet6_csk_addr2sockaddr, .sockaddr_len =sizeof (struct sockaddr_in6 ), - .bind_conflict = inet6_csk_bind_conflict, #ifdef CONFIG_COMPAT .compat_setsockopt = compat_ipv6_setsockopt, .compat_getsockopt = compat_ipv6_getsockopt,
@@ -1651,7 +1650,6 @@ static const struct inet_connection_sock_af_ops ipv6_mapped = { .getsockopt = ipv6_getsockopt, .addr2sockaddr = inet6_csk_addr2sockaddr, .sockaddr_len =sizeof (struct sockaddr_in6 ), - .bind_conflict = inet6_csk_bind_conflict, #ifdef CONFIG_COMPAT .compat_setsockopt = compat_ipv6_setsockopt, .compat_getsockopt = compat_ipv6_getsockopt,
Signature · 1 line
2.5.5
PATCH v2 4/6 inet: don't check for bind conflicts twice when searching for a port
In reply to Josef BacikThis is just wasted time, we've already found a tb that doesn't have a bind conflict, and we don't drop the head lock so scanning again isn't going to give us a different answer. Instead move the tb->reuse setting logic outside of the found_tb path and put it in the success: path. Then make it so that we don't goto again if we find a bind conflict in the found_tb path as we won't reach this anymore when we are scanning for an ephemeral port.
--- net/ipv4/inet_connection_sock.c | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-)
net/ipv4/inet_connection_sock.c +13 -20
@@ -164,7 +164,7 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) {bool reuse = sk->sk_reuse && sk->sk_state !=TCP_LISTEN ;struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo; -int ret =1 , attempts =5 , port = snum; +int ret =1 , port = snum;struct inet_bind_hashbucket *head;struct net *net =sock_net (sk);int i, low, high, attempt_half;
@@ -183,7 +183,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) goto tb_not_found; } -again: attempt_half = (sk->sk_reuse ==SK_CAN_REUSE ) ?1 :0 ; other_half_scan:inet_get_local_port_range (net, &low, &high);
@@ -220,8 +219,10 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)spin_lock_bh (&head->lock);inet_bind_bucket_for_each (tb, &head->chain)if (net_eq (ib_net (tb), net) && tb->port == port) { +if (hlist_empty (&tb->owners)) + goto success;if (!inet_csk_bind_conflict (sk, tb, false, reuseport_ok)) - goto tb_found; + goto success; goto next_port; } goto tb_not_found;
@@ -256,23 +257,11 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) !rcu_access_pointer(sk->sk_reuseport_cb) && sk->sk_reuseport && uid_eq(tb->fastuid, uid))) goto success; -if (inet_csk_bind_conflict (sk, tb, true, reuseport_ok)) { -if ((reuse || - (tb->fastreuseport >0 && - sk->sk_reuseport && - !rcu_access_pointer (sk->sk_reuseport_cb) && -uid_eq (tb->fastuid, uid))) && !snum && - --attempts >=0 ) { -spin_unlock_bh (&head->lock); - goto again; - } +if (inet_csk_bind_conflict (sk, tb, true, reuseport_ok)) goto fail_unlock; - } -if (!reuse) - tb->fastreuse =0 ; -if (!sk->sk_reuseport || !uid_eq (tb->fastuid, uid)) - tb->fastreuseport =0 ; - }else { + } +success: +if (!hlist_empty (&tb->owners)) { tb->fastreuse = reuse;if (sk->sk_reuseport) { tb->fastreuseport =1 ;
@@ -280,8 +269,12 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) }else { tb->fastreuseport =0 ; } + }else { +if (!reuse) + tb->fastreuse =0 ; +if (!sk->sk_reuseport || !uid_eq (tb->fastuid, uid)) + tb->fastreuseport =0 ; } -success:if (!inet_csk (sk)->icsk_bind_hash)inet_bind_hash (sk, tb, port);WARN_ON (inet_csk (sk)->icsk_bind_hash != tb);
Signature · 1 line
2.5.5
PATCH v2 5/6 inet: split inet_csk_get_port into two functions
In reply to Josef Bacikinet_csk_get_port does two different things, it either scans for an open port, or it tries to see if the specified port is available for use. Since these two operations have different rules and are basically independent lets split them into two different functions to make them both more readable.
--- net/ipv4/inet_connection_sock.c | 66 +++++++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 22 deletions(-)
net/ipv4/inet_connection_sock.c +44 -22
@@ -156,33 +156,21 @@ static int inet_csk_bind_conflict(const struct sock *sk,return sk2 !=NULL ; } -/* Obtain a reference to a local port for the given sock, -* if snum is zero it means select any available local port. -* We try to allocate an odd port (and leave even ports for connect()) +/* +* Find an open port number for the socket. Returns with the +* inet_bind_hashbucket lock held. */ -int inet_csk_get_port (struct sock *sk,unsigned short snum) +static struct inet_bind_hashbucket * +inet_csk_find_open_port (struct sock *sk,struct inet_bind_bucket **tb_ret,int *port_ret) { -bool reuse = sk->sk_reuse && sk->sk_state !=TCP_LISTEN ;struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo; -int ret =1 , port = snum; +int port =0 ;struct inet_bind_hashbucket *head;struct net *net =sock_net (sk);int i, low, high, attempt_half;struct inet_bind_bucket *tb; -kuid_t uid =sock_i_uid (sk);u32 remaining, offset; -bool reuseport_ok = !!snum; -if (port) { - head = &hinfo->bhash[inet_bhashfn (net, port, - hinfo->bhash_size)]; -spin_lock_bh (&head->lock); -inet_bind_bucket_for_each (tb, &head->chain) -if (net_eq (ib_net (tb), net) && tb->port == port) - goto tb_found; - - goto tb_not_found; - } attempt_half = (sk->sk_reuse ==SK_CAN_REUSE ) ?1 :0 ; other_half_scan:inet_get_local_port_range (net, &low, &high);
@@ -221,11 +209,12 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)if (net_eq (ib_net (tb), net) && tb->port == port) {if (hlist_empty (&tb->owners)) goto success; -if (!inet_csk_bind_conflict (sk, tb, false, reuseport_ok)) +if (!inet_csk_bind_conflict (sk, tb, false, false)) goto success; goto next_port; } - goto tb_not_found; + tb =NULL ; + goto success; next_port:spin_unlock_bh (&head->lock);cond_resched ();
@@ -240,8 +229,41 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) attempt_half =2 ; goto other_half_scan; } -return ret; +return NULL ; +success: + *port_ret = port; + *tb_ret = tb; +return head; +} + +/* Obtain a reference to a local port for the given sock, +* if snum is zero it means select any available local port. +* We try to allocate an odd port (and leave even ports for connect()) +*/ +int inet_csk_get_port (struct sock *sk,unsigned short snum) +{ +bool reuse = sk->sk_reuse && sk->sk_state !=TCP_LISTEN ; +struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo; +int ret =1 , port = snum; +struct inet_bind_hashbucket *head; +struct net *net =sock_net (sk); +struct inet_bind_bucket *tb =NULL ; +kuid_t uid =sock_i_uid (sk); +if (!port) { + head =inet_csk_find_open_port (sk, &tb, &port); +if (!head) +return ret; +if (!tb) + goto tb_not_found; + goto success; + } + head = &hinfo->bhash[inet_bhashfn (net, port, + hinfo->bhash_size)]; +spin_lock_bh (&head->lock); +inet_bind_bucket_for_each (tb, &head->chain) +if (net_eq (ib_net (tb), net) && tb->port == port) + goto tb_found; tb_not_found: tb =inet_bind_bucket_create (hinfo->bind_bucket_cachep, net, head, port);
@@ -257,7 +279,7 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum) !rcu_access_pointer(sk->sk_reuseport_cb) && sk->sk_reuseport && uid_eq(tb->fastuid, uid))) goto success; -if (inet_csk_bind_conflict (sk, tb, true, reuseport_ok)) +if (inet_csk_bind_conflict (sk, tb, true, true)) goto fail_unlock; } success:
Signature · 1 line
2.5.5
PATCH v2 3/6 inet: kill smallest_size and smallest_port
In reply to Josef BacikIn inet_csk_get_port we seem to be using smallest_port to figure out where the best place to look for a SO_REUSEPORT sk that matches with an existing set of SO_REUSEPORT's. However if we get to the logic
if (smallest_size != -1) {
port = smallest_port;
goto have_port;
}we will do a useless search, because we would have already done the inet_csk_bind_conflict for that port and it would have returned 1, otherwise we would have gone to found_tb and succeeded. Since this logic makes us do yet another trip through inet_csk_bind_conflict for a port we know won't work just delete this code and save us the time.
--- net/ipv4/inet_connection_sock.c | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-)
net/ipv4/inet_connection_sock.c +4 -22
@@ -165,7 +165,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)bool reuse = sk->sk_reuse && sk->sk_state !=TCP_LISTEN ;struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo;int ret =1 , attempts =5 , port = snum; -int smallest_size =-1 , smallest_port;struct inet_bind_hashbucket *head;struct net *net =sock_net (sk);int i, low, high, attempt_half;
@@ -175,7 +174,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)bool reuseport_ok = !!snum;if (port) { -have_port: head = &hinfo->bhash[inet_bhashfn (net, port, hinfo->bhash_size)];spin_lock_bh (&head->lock);
@@ -209,8 +207,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)* We do the opposite to not pollute connect() users. */ offset |=1U ; - smallest_size =-1 ; - smallest_port = low;/* avoid compiler warning */ other_parity_scan: port = low + offset;
@@ -224,15 +220,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)spin_lock_bh (&head->lock);inet_bind_bucket_for_each (tb, &head->chain)if (net_eq (ib_net (tb), net) && tb->port == port) { -if (((tb->fastreuse >0 && reuse) || - (tb->fastreuseport >0 && - sk->sk_reuseport && - !rcu_access_pointer (sk->sk_reuseport_cb) && -uid_eq (tb->fastuid, uid))) && - (tb->num_owners < smallest_size || smallest_size ==-1 )) { - smallest_size = tb->num_owners; - smallest_port = port; - }if (!inet_csk_bind_conflict (sk, tb, false, reuseport_ok)) goto tb_found; goto next_port;
@@ -243,10 +230,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)cond_resched (); } -if (smallest_size !=-1 ) { - port = smallest_port; - goto have_port; - } offset--;if (!(offset &1 )) goto other_parity_scan;
@@ -268,19 +251,18 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)if (sk->sk_reuse ==SK_FORCE_REUSE ) goto success; -if (((tb->fastreuse >0 && reuse) || +if ((tb->fastreuse >0 && reuse) || (tb->fastreuseport >0 && !rcu_access_pointer (sk->sk_reuseport_cb) && - sk->sk_reuseport &&uid_eq (tb->fastuid, uid))) && - smallest_size ==-1 ) + sk->sk_reuseport &&uid_eq (tb->fastuid, uid))) goto success;if (inet_csk_bind_conflict (sk, tb, true, reuseport_ok)) {if ((reuse || (tb->fastreuseport >0 && sk->sk_reuseport && !rcu_access_pointer (sk->sk_reuseport_cb) && -uid_eq (tb->fastuid, uid))) && - !snum && smallest_size !=-1 && --attempts >=0 ) { +uid_eq (tb->fastuid, uid))) && !snum && + --attempts >=0 ) {spin_unlock_bh (&head->lock); goto again; }
Signature · 1 line
2.5.5
PATCH v2 6/6 inet: reset tb->fastreuseport when adding a reuseport sk
In reply to Josef BacikIf we have non reuseport sockets on a tb we will set tb->fastreuseport to 0 and never set it again. Which means that in the future if we end up adding a bunch of reuseport sk's to that tb we'll have to do the expensive scan every time. Instead add a sock_common to the tb so we know what reuseport sk succeeded last. Once one sk has made it onto the list we know that there are no potential bind conflicts on the owners list that match that sk's rcv_addr. So copy the sk's common into our tb->fastsock and set tb->fastruseport to FASTREUSESOCK_STRICT so we know we have to do an extra check for subsequent reuseport sockets and skip the expensive bind conflict check.
--- include/net/inet_hashtables.h | 4 ++++ net/ipv4/inet_connection_sock.c | 53 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-)
include/net/inet_hashtables.h +4 -0
@@ -74,12 +74,16 @@ struct inet_ehash_bucket {* users logged onto your box, isn't it nice to know that new data * ports are created in O(1) time? I thought so. ;-) -DaveM */ +#define FASTREUSEPORT_ANY 1 +#define FASTREUSEPORT_STRICT 2 +struct inet_bind_bucket {possible_net_t ib_net;unsigned short port;signed char fastreuse;signed char fastreuseport;kuid_t fastuid; +struct sock_common fastsock;int num_owners;struct hlist_node node;struct hlist_head owners;
net/ipv4/inet_connection_sock.c +49 -4
78 lines not on this page · raw message
Signature · 1 line
2.5.5
Re: [PATCH 3/6 net-next] inet: kill smallest_size and smallest_port
In reply to Josef BacikQuoted 9 lines · Josef Bacik
In
inet_csk_get_portwe seem to be usingsmallest_portto figure out where the best place to look for aSO_REUSEPORTsk that matches with an existing set of SO_REUSEPORT's. However if we get to the logicif (smallest_size != -1) { port = smallest_port; goto have_port; }
Quoted 7 lines
we will do a useless search, because we would have already done the
inet_csk_bind_conflictfor that port and it would have returned 1, otherwise we would have gone tofound_tband succeeded. Since this logic makes us do yet another trip throughinet_csk_bind_conflictfor a port we know won't work just delete this code and save us the time.Signed-off-by Josef Bacik <[email protected]>
Please remove tb->need_owners ;)
Re: [PATCH 6/6 net-next] inet: reset tb->fastreuseport when adding a reuseport sk
In reply to Josef BacikQuoted 27 lines · Josef Bacik
If we have non reuseport sockets on a tb we will set
tb->fastreuseportto 0 and never set it again. Which means that in the future if we end up adding a bunch of reuseport sk's to that tb we'll have to do the expensive scan every time. Instead add asock_commonto the tb so we know what reuseport sk succeeded last. Once one sk has made it onto the list we know that there are no potential bind conflicts on the owners list that match that sk'srcv_addr. So copy the sk's common into ourtb->fastsockand settb->fastruseporttoFASTREUSESOCK_STRICTso we know we have to do an extra check for subsequent reuseport sockets and skip the expensive bind conflict check.Signed-off-by Josef Bacik <[email protected]>--- include/net/inet_hashtables.h | 4 ++++ net/ipv4/inet_connection_sock.c | 53 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 4 deletions(-)include/net/inet_hashtables.h +3
-0@@ -74,12 +74,16 @@ struct inet_ehash_bucket {* users logged onto your box, isn't it nice to know that new data * ports are created in O(1) time? I thought so. ;-) -DaveM */ +#define FASTREUSEPORT_ANY 1 +#define FASTREUSEPORT_STRICT 2 +
include/net/inet_hashtables.h struct inet_ehash_bucket { · line 89
Quoted 10 lines
include/net/inet_hashtables.h +1
-0struct inet_bind_bucket {possible_net_t ib_net;unsigned short port;signed char fastreuse;signed char fastreuseport;kuid_t fastuid; +struct sock_common fastsock;int num_owners;struct hlist_node node;struct hlist_head owners;
Please place this fat field at the end of inet_bind_bucket
Many sockets do not use SO_REUSEPORT and should not use this field, while tb->owners need to be touched.
David Miller
maintainerRe: [PATCH 4/6 net-next] inet: don't check for bind conflicts twice when searching for a port
In reply to Josef BacikFrom: Josef Bacik <[email protected]>
Date: Thu, 22 Dec 2016 16:26:36 -0500
Quoted 8 lines
This is just wasted time, we've already found a tb that doesn't have a bind conflict, and we don't drop the head lock so scanning again isn't going to give us a different answer. Instead move the
tb->reusesetting logic outside of thefound_tbpath and put it in the success: path. Then make it so that we don't goto again if we find a bind conflict in thefound_tbpath as we won't reach this anymore when we are scanning for an ephemeral port.Signed-off-by Josef Bacik <[email protected]>
...
net/ipv4/inet_connection_sock.c int inet_csk_get_port(struct sock *sk, unsigned short snum) · line 223
Quoted 6 lines
net/ipv4/inet_connection_sock.c +2
-0@@ -220,8 +219,10 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)spin_lock_bh (&head->lock);inet_bind_bucket_for_each (tb, &head->chain)if (net_eq (ib_net (tb), net) && tb->port == port) { +if (hlist_empty (&tb->owners)) + goto success;
I am not sure that this condition can ever trigger.
The only time we will see an alive 'tb' with an empty owners list is when we allocate one in this function. And when that allocation succeeds, we go straight through the rest of this function without ever jumping out for errors or anything like that.
So we should never drop the allocated fresh 'tb', leave it with an empty owners list, and branch back up to this part of the function.
David Miller
maintainerRe: [PATCH 6/6 net-next] inet: reset tb->fastreuseport when adding a reuseport sk
In reply to Josef BacikFrom: Josef Bacik <[email protected]>
Date: Thu, 22 Dec 2016 16:26:38 -0500
Quoted 7 lines
include/net/inet_hashtables.h +3
-0@@ -74,12 +74,16 @@ struct inet_ehash_bucket {* users logged onto your box, isn't it nice to know that new data * ports are created in O(1) time? I thought so. ;-) -DaveM */ +#define FASTREUSEPORT_ANY 1 +#define FASTREUSEPORT_STRICT 2 +
include/net/inet_hashtables.h struct inet_ehash_bucket { · line 89
Quoted 10 lines
include/net/inet_hashtables.h +1
-0struct inet_bind_bucket {possible_net_t ib_net;unsigned short port;signed char fastreuse;signed char fastreuseport;kuid_t fastuid; +struct sock_common fastsock;int num_owners;struct hlist_node node;struct hlist_head owners;
I understand why you did this fastsock thing, it's the easiest way out.
But it is so wasteful, but in space and time. You have to store an entire fake socket here, but there is not need for anything other than the recv saddr member(s).
And you have to copy the whole thing to store the state, which again is almost entirely unnecessary.
You really just need to store only the required state, and abstract the AF specific bits properly.