Skip to content

Commit b9470c2

Browse files
Josef Bacikdavem330
authored andcommitted
inet: kill smallest_size and smallest_port
In 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. Signed-off-by: Josef Bacik <jbacik@fb.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent aa07884 commit b9470c2

File tree

3 files changed

+4
-26
lines changed

3 files changed

+4
-26
lines changed

include/net/inet_hashtables.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ struct inet_bind_bucket {
8080
signed char fastreuse;
8181
signed char fastreuseport;
8282
kuid_t fastuid;
83-
int num_owners;
8483
struct hlist_node node;
8584
struct hlist_head owners;
8685
};

net/ipv4/inet_connection_sock.c

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
165165
bool reuse = sk->sk_reuse && sk->sk_state != TCP_LISTEN;
166166
struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo;
167167
int ret = 1, attempts = 5, port = snum;
168-
int smallest_size = -1, smallest_port;
169168
struct inet_bind_hashbucket *head;
170169
struct net *net = sock_net(sk);
171170
int i, low, high, attempt_half;
@@ -175,7 +174,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
175174
bool reuseport_ok = !!snum;
176175

177176
if (port) {
178-
have_port:
179177
head = &hinfo->bhash[inet_bhashfn(net, port,
180178
hinfo->bhash_size)];
181179
spin_lock_bh(&head->lock);
@@ -209,8 +207,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
209207
* We do the opposite to not pollute connect() users.
210208
*/
211209
offset |= 1U;
212-
smallest_size = -1;
213-
smallest_port = low; /* avoid compiler warning */
214210

215211
other_parity_scan:
216212
port = low + offset;
@@ -224,15 +220,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
224220
spin_lock_bh(&head->lock);
225221
inet_bind_bucket_for_each(tb, &head->chain)
226222
if (net_eq(ib_net(tb), net) && tb->port == port) {
227-
if (((tb->fastreuse > 0 && reuse) ||
228-
(tb->fastreuseport > 0 &&
229-
sk->sk_reuseport &&
230-
!rcu_access_pointer(sk->sk_reuseport_cb) &&
231-
uid_eq(tb->fastuid, uid))) &&
232-
(tb->num_owners < smallest_size || smallest_size == -1)) {
233-
smallest_size = tb->num_owners;
234-
smallest_port = port;
235-
}
236223
if (!inet_csk_bind_conflict(sk, tb, false, reuseport_ok))
237224
goto tb_found;
238225
goto next_port;
@@ -243,10 +230,6 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
243230
cond_resched();
244231
}
245232

246-
if (smallest_size != -1) {
247-
port = smallest_port;
248-
goto have_port;
249-
}
250233
offset--;
251234
if (!(offset & 1))
252235
goto other_parity_scan;
@@ -268,19 +251,18 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
268251
if (sk->sk_reuse == SK_FORCE_REUSE)
269252
goto success;
270253

271-
if (((tb->fastreuse > 0 && reuse) ||
254+
if ((tb->fastreuse > 0 && reuse) ||
272255
(tb->fastreuseport > 0 &&
273256
!rcu_access_pointer(sk->sk_reuseport_cb) &&
274-
sk->sk_reuseport && uid_eq(tb->fastuid, uid))) &&
275-
smallest_size == -1)
257+
sk->sk_reuseport && uid_eq(tb->fastuid, uid)))
276258
goto success;
277259
if (inet_csk_bind_conflict(sk, tb, true, reuseport_ok)) {
278260
if ((reuse ||
279261
(tb->fastreuseport > 0 &&
280262
sk->sk_reuseport &&
281263
!rcu_access_pointer(sk->sk_reuseport_cb) &&
282-
uid_eq(tb->fastuid, uid))) &&
283-
!snum && smallest_size != -1 && --attempts >= 0) {
264+
uid_eq(tb->fastuid, uid))) && !snum &&
265+
--attempts >= 0) {
284266
spin_unlock_bh(&head->lock);
285267
goto again;
286268
}

net/ipv4/inet_hashtables.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
7373
tb->port = snum;
7474
tb->fastreuse = 0;
7575
tb->fastreuseport = 0;
76-
tb->num_owners = 0;
7776
INIT_HLIST_HEAD(&tb->owners);
7877
hlist_add_head(&tb->node, &head->chain);
7978
}
@@ -96,7 +95,6 @@ void inet_bind_hash(struct sock *sk, struct inet_bind_bucket *tb,
9695
{
9796
inet_sk(sk)->inet_num = snum;
9897
sk_add_bind_node(sk, &tb->owners);
99-
tb->num_owners++;
10098
inet_csk(sk)->icsk_bind_hash = tb;
10199
}
102100

@@ -114,7 +112,6 @@ static void __inet_put_port(struct sock *sk)
114112
spin_lock(&head->lock);
115113
tb = inet_csk(sk)->icsk_bind_hash;
116114
__sk_del_bind_node(sk);
117-
tb->num_owners--;
118115
inet_csk(sk)->icsk_bind_hash = NULL;
119116
inet_sk(sk)->inet_num = 0;
120117
inet_bind_bucket_destroy(hashinfo->bind_bucket_cachep, tb);

0 commit comments

Comments
 (0)