Skip to content

Commit 5815d5e

Browse files
edumazetdavem330
authored andcommitted
tcp: use hash_32() in tcp_metrics
Fix a missing roundup_pow_of_two(), since tcpmhash_entries is not guaranteed to be a power of two. Uses hash_32() instead of custom hash. tcpmhash_entries should be an unsigned int. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 67b95bd commit 5815d5e

2 files changed

Lines changed: 11 additions & 16 deletions

File tree

‎include/net/netns/ipv4.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ struct netns_ipv4 {
4040
struct sock **icmp_sk;
4141
struct inet_peer_base *peers;
4242
struct tcpm_hash_bucket *tcp_metrics_hash;
43-
unsigned int tcp_metrics_hash_mask;
43+
unsigned int tcp_metrics_hash_log;
4444
struct netns_frags frags;
4545
#ifdef CONFIG_NETFILTER
4646
struct xt_table *iptable_filter;

‎net/ipv4/tcp_metrics.c‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <linux/slab.h>
88
#include <linux/init.h>
99
#include <linux/tcp.h>
10+
#include <linux/hash.h>
1011

1112
#include <net/inet_connection_sock.h>
1213
#include <net/net_namespace.h>
@@ -228,10 +229,8 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
228229
return NULL;
229230
}
230231

231-
hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
232-
233232
net = dev_net(dst->dev);
234-
hash &= net->ipv4.tcp_metrics_hash_mask;
233+
hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
235234

236235
for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
237236
tm = rcu_dereference(tm->tcpm_next)) {
@@ -265,10 +264,8 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
265264
return NULL;
266265
}
267266

268-
hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
269-
270267
net = twsk_net(tw);
271-
hash &= net->ipv4.tcp_metrics_hash_mask;
268+
hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
272269

273270
for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm;
274271
tm = rcu_dereference(tm->tcpm_next)) {
@@ -302,10 +299,8 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
302299
return NULL;
303300
}
304301

305-
hash ^= (hash >> 24) ^ (hash >> 16) ^ (hash >> 8);
306-
307302
net = dev_net(dst->dev);
308-
hash &= net->ipv4.tcp_metrics_hash_mask;
303+
hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
309304

310305
tm = __tcp_get_metrics(&addr, net, hash);
311306
reclaim = false;
@@ -694,15 +689,15 @@ void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
694689
rcu_read_unlock();
695690
}
696691

697-
static unsigned long tcpmhash_entries;
692+
static unsigned int tcpmhash_entries;
698693
static int __init set_tcpmhash_entries(char *str)
699694
{
700695
ssize_t ret;
701696

702697
if (!str)
703698
return 0;
704699

705-
ret = kstrtoul(str, 0, &tcpmhash_entries);
700+
ret = kstrtouint(str, 0, &tcpmhash_entries);
706701
if (ret)
707702
return 0;
708703

@@ -712,7 +707,8 @@ __setup("tcpmhash_entries=", set_tcpmhash_entries);
712707

713708
static int __net_init tcp_net_metrics_init(struct net *net)
714709
{
715-
int slots, size;
710+
size_t size;
711+
unsigned int slots;
716712

717713
slots = tcpmhash_entries;
718714
if (!slots) {
@@ -722,14 +718,13 @@ static int __net_init tcp_net_metrics_init(struct net *net)
722718
slots = 8 * 1024;
723719
}
724720

725-
size = slots * sizeof(struct tcpm_hash_bucket);
721+
net->ipv4.tcp_metrics_hash_log = order_base_2(slots);
722+
size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log;
726723

727724
net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL);
728725
if (!net->ipv4.tcp_metrics_hash)
729726
return -ENOMEM;
730727

731-
net->ipv4.tcp_metrics_hash_mask = (slots - 1);
732-
733728
return 0;
734729
}
735730

0 commit comments

Comments
 (0)