aboutsummaryrefslogtreecommitdiffstats
path: root/net/ipv6/inet6_hashtables.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/ipv6/inet6_hashtables.c')
-rw-r--r--net/ipv6/inet6_hashtables.c89
1 files changed, 57 insertions, 32 deletions
diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c
index 1646a5658255..21544b9be259 100644
--- a/net/ipv6/inet6_hashtables.c
+++ b/net/ipv6/inet6_hashtables.c
@@ -25,26 +25,30 @@
25void __inet6_hash(struct sock *sk) 25void __inet6_hash(struct sock *sk)
26{ 26{
27 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; 27 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
28 struct hlist_head *list;
29 rwlock_t *lock;
30 28
31 WARN_ON(!sk_unhashed(sk)); 29 WARN_ON(!sk_unhashed(sk));
32 30
33 if (sk->sk_state == TCP_LISTEN) { 31 if (sk->sk_state == TCP_LISTEN) {
34 list = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)]; 32 struct inet_listen_hashbucket *ilb;
35 lock = &hashinfo->lhash_lock; 33
36 inet_listen_wlock(hashinfo); 34 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
35 spin_lock(&ilb->lock);
36 __sk_add_node(sk, &ilb->head);
37 spin_unlock(&ilb->lock);
37 } else { 38 } else {
38 unsigned int hash; 39 unsigned int hash;
40 struct hlist_nulls_head *list;
41 rwlock_t *lock;
42
39 sk->sk_hash = hash = inet6_sk_ehashfn(sk); 43 sk->sk_hash = hash = inet6_sk_ehashfn(sk);
40 list = &inet_ehash_bucket(hashinfo, hash)->chain; 44 list = &inet_ehash_bucket(hashinfo, hash)->chain;
41 lock = inet_ehash_lockp(hashinfo, hash); 45 lock = inet_ehash_lockp(hashinfo, hash);
42 write_lock(lock); 46 write_lock(lock);
47 __sk_nulls_add_node_rcu(sk, list);
48 write_unlock(lock);
43 } 49 }
44 50
45 __sk_add_node(sk, list);
46 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 51 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
47 write_unlock(lock);
48} 52}
49EXPORT_SYMBOL(__inet6_hash); 53EXPORT_SYMBOL(__inet6_hash);
50 54
@@ -63,33 +67,53 @@ struct sock *__inet6_lookup_established(struct net *net,
63 const int dif) 67 const int dif)
64{ 68{
65 struct sock *sk; 69 struct sock *sk;
66 const struct hlist_node *node; 70 const struct hlist_nulls_node *node;
67 const __portpair ports = INET_COMBINED_PORTS(sport, hnum); 71 const __portpair ports = INET_COMBINED_PORTS(sport, hnum);
68 /* Optimize here for direct hit, only listening connections can 72 /* Optimize here for direct hit, only listening connections can
69 * have wildcards anyways. 73 * have wildcards anyways.
70 */ 74 */
71 unsigned int hash = inet6_ehashfn(net, daddr, hnum, saddr, sport); 75 unsigned int hash = inet6_ehashfn(net, daddr, hnum, saddr, sport);
72 struct inet_ehash_bucket *head = inet_ehash_bucket(hashinfo, hash); 76 unsigned int slot = hash & (hashinfo->ehash_size - 1);
73 rwlock_t *lock = inet_ehash_lockp(hashinfo, hash); 77 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
74 78
75 prefetch(head->chain.first); 79
76 read_lock(lock); 80 rcu_read_lock();
77 sk_for_each(sk, node, &head->chain) { 81begin:
82 sk_nulls_for_each_rcu(sk, node, &head->chain) {
78 /* For IPV6 do the cheaper port and family tests first. */ 83 /* For IPV6 do the cheaper port and family tests first. */
79 if (INET6_MATCH(sk, net, hash, saddr, daddr, ports, dif)) 84 if (INET6_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
80 goto hit; /* You sunk my battleship! */ 85 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
86 goto begintw;
87 if (!INET6_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
88 sock_put(sk);
89 goto begin;
90 }
91 goto out;
92 }
81 } 93 }
94 if (get_nulls_value(node) != slot)
95 goto begin;
96
97begintw:
82 /* Must check for a TIME_WAIT'er before going to listener hash. */ 98 /* Must check for a TIME_WAIT'er before going to listener hash. */
83 sk_for_each(sk, node, &head->twchain) { 99 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
84 if (INET6_TW_MATCH(sk, net, hash, saddr, daddr, ports, dif)) 100 if (INET6_TW_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
85 goto hit; 101 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) {
102 sk = NULL;
103 goto out;
104 }
105 if (!INET6_TW_MATCH(sk, net, hash, saddr, daddr, ports, dif)) {
106 sock_put(sk);
107 goto begintw;
108 }
109 goto out;
110 }
86 } 111 }
87 read_unlock(lock); 112 if (get_nulls_value(node) != slot)
88 return NULL; 113 goto begintw;
89 114 sk = NULL;
90hit: 115out:
91 sock_hold(sk); 116 rcu_read_unlock();
92 read_unlock(lock);
93 return sk; 117 return sk;
94} 118}
95EXPORT_SYMBOL(__inet6_lookup_established); 119EXPORT_SYMBOL(__inet6_lookup_established);
@@ -102,10 +126,11 @@ struct sock *inet6_lookup_listener(struct net *net,
102 const struct hlist_node *node; 126 const struct hlist_node *node;
103 struct sock *result = NULL; 127 struct sock *result = NULL;
104 int score, hiscore = 0; 128 int score, hiscore = 0;
129 struct inet_listen_hashbucket *ilb;
105 130
106 read_lock(&hashinfo->lhash_lock); 131 ilb = &hashinfo->listening_hash[inet_lhashfn(net, hnum)];
107 sk_for_each(sk, node, 132 spin_lock(&ilb->lock);
108 &hashinfo->listening_hash[inet_lhashfn(net, hnum)]) { 133 sk_for_each(sk, node, &ilb->head) {
109 if (net_eq(sock_net(sk), net) && inet_sk(sk)->num == hnum && 134 if (net_eq(sock_net(sk), net) && inet_sk(sk)->num == hnum &&
110 sk->sk_family == PF_INET6) { 135 sk->sk_family == PF_INET6) {
111 const struct ipv6_pinfo *np = inet6_sk(sk); 136 const struct ipv6_pinfo *np = inet6_sk(sk);
@@ -133,7 +158,7 @@ struct sock *inet6_lookup_listener(struct net *net,
133 } 158 }
134 if (result) 159 if (result)
135 sock_hold(result); 160 sock_hold(result);
136 read_unlock(&hashinfo->lhash_lock); 161 spin_unlock(&ilb->lock);
137 return result; 162 return result;
138} 163}
139 164
@@ -172,14 +197,14 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
172 struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash); 197 struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
173 rwlock_t *lock = inet_ehash_lockp(hinfo, hash); 198 rwlock_t *lock = inet_ehash_lockp(hinfo, hash);
174 struct sock *sk2; 199 struct sock *sk2;
175 const struct hlist_node *node; 200 const struct hlist_nulls_node *node;
176 struct inet_timewait_sock *tw; 201 struct inet_timewait_sock *tw;
177 202
178 prefetch(head->chain.first); 203 prefetch(head->chain.first);
179 write_lock(lock); 204 write_lock(lock);
180 205
181 /* Check TIME-WAIT sockets first. */ 206 /* Check TIME-WAIT sockets first. */
182 sk_for_each(sk2, node, &head->twchain) { 207 sk_nulls_for_each(sk2, node, &head->twchain) {
183 tw = inet_twsk(sk2); 208 tw = inet_twsk(sk2);
184 209
185 if (INET6_TW_MATCH(sk2, net, hash, saddr, daddr, ports, dif)) { 210 if (INET6_TW_MATCH(sk2, net, hash, saddr, daddr, ports, dif)) {
@@ -192,7 +217,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
192 tw = NULL; 217 tw = NULL;
193 218
194 /* And established part... */ 219 /* And established part... */
195 sk_for_each(sk2, node, &head->chain) { 220 sk_nulls_for_each(sk2, node, &head->chain) {
196 if (INET6_MATCH(sk2, net, hash, saddr, daddr, ports, dif)) 221 if (INET6_MATCH(sk2, net, hash, saddr, daddr, ports, dif))
197 goto not_unique; 222 goto not_unique;
198 } 223 }
@@ -203,7 +228,7 @@ unique:
203 inet->num = lport; 228 inet->num = lport;
204 inet->sport = htons(lport); 229 inet->sport = htons(lport);
205 WARN_ON(!sk_unhashed(sk)); 230 WARN_ON(!sk_unhashed(sk));
206 __sk_add_node(sk, &head->chain); 231 __sk_nulls_add_node_rcu(sk, &head->chain);
207 sk->sk_hash = hash; 232 sk->sk_hash = hash;
208 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 233 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
209 write_unlock(lock); 234 write_unlock(lock);