Skip to content

Commit d4438ce

Browse files
edumazetkuba-moo
authored andcommitted
inet: call inet6_ehashfn() once from inet6_hash_connect()
inet6_ehashfn() being called from __inet6_check_established() has a big impact on performance, as shown in the Tested section. After prior patch, we can compute the hash for port 0 from inet6_hash_connect(), and derive each hash in __inet_hash_connect() from this initial hash: hash(saddr, lport, daddr, dport) == hash(saddr, 0, daddr, dport) + lport Apply the same principle for __inet_check_established(), although inet_ehashfn() has a smaller cost. Tested: Server: ulimit -n 40000; neper/tcp_crr -T 200 -F 30000 -6 --nolog Client: ulimit -n 40000; neper/tcp_crr -T 200 -F 30000 -6 --nolog -c -H server Before this patch: utime_start=0.286131 utime_end=4.378886 stime_start=11.952556 stime_end=1991.655533 num_transactions=1446830 latency_min=0.001061085 latency_max=12.075275028 latency_mean=0.376375302 latency_stddev=1.361969596 num_samples=306383 throughput=151866.56 perf top: 50.01% [kernel] [k] __inet6_check_established 20.65% [kernel] [k] __inet_hash_connect 15.81% [kernel] [k] inet6_ehashfn 2.92% [kernel] [k] rcu_all_qs 2.34% [kernel] [k] __cond_resched 0.50% [kernel] [k] _raw_spin_lock 0.34% [kernel] [k] sched_balance_trigger 0.24% [kernel] [k] queued_spin_lock_slowpath After this patch: utime_start=0.315047 utime_end=9.257617 stime_start=7.041489 stime_end=1923.688387 num_transactions=3057968 latency_min=0.003041375 latency_max=7.056589232 latency_mean=0.141075048 # Better latency metrics latency_stddev=0.526900516 num_samples=312996 throughput=320677.21 # 111 % increase, and 229 % for the series perf top: inet6_ehashfn is no longer seen. 39.67% [kernel] [k] __inet_hash_connect 37.06% [kernel] [k] __inet6_check_established 4.79% [kernel] [k] rcu_all_qs 3.82% [kernel] [k] __cond_resched 1.76% [kernel] [k] sched_balance_domains 0.82% [kernel] [k] _raw_spin_lock 0.81% [kernel] [k] sched_balance_rq 0.81% [kernel] [k] sched_balance_trigger 0.76% [kernel] [k] queued_spin_lock_slowpath Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Tested-by: Jason Xing <[email protected]> Reviewed-by: Jason Xing <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 9544d60 commit d4438ce

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

include/net/inet_hashtables.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,12 @@ static inline void sk_rcv_saddr_set(struct sock *sk, __be32 addr)
527527

528528
int __inet_hash_connect(struct inet_timewait_death_row *death_row,
529529
struct sock *sk, u64 port_offset,
530+
u32 hash_port0,
530531
int (*check_established)(struct inet_timewait_death_row *,
531532
struct sock *, __u16,
532533
struct inet_timewait_sock **,
533-
bool rcu_lookup));
534+
bool rcu_lookup,
535+
u32 hash));
534536

535537
int inet_hash_connect(struct inet_timewait_death_row *death_row,
536538
struct sock *sk);

include/net/ip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static inline void inet_get_local_port_range(const struct net *net, int *low, in
357357
bool inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high);
358358

359359
#ifdef CONFIG_SYSCTL
360-
static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)
360+
static inline bool inet_is_local_reserved_port(const struct net *net, unsigned short port)
361361
{
362362
if (!net->ipv4.sysctl_local_reserved_ports)
363363
return false;

net/ipv4/inet_hashtables.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,8 @@ EXPORT_SYMBOL_GPL(__inet_lookup_established);
538538
static int __inet_check_established(struct inet_timewait_death_row *death_row,
539539
struct sock *sk, __u16 lport,
540540
struct inet_timewait_sock **twp,
541-
bool rcu_lookup)
541+
bool rcu_lookup,
542+
u32 hash)
542543
{
543544
struct inet_hashinfo *hinfo = death_row->hashinfo;
544545
struct inet_sock *inet = inet_sk(sk);
@@ -549,8 +550,6 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
549550
int sdif = l3mdev_master_ifindex_by_index(net, dif);
550551
INET_ADDR_COOKIE(acookie, saddr, daddr);
551552
const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
552-
unsigned int hash = inet_ehashfn(net, daddr, lport,
553-
saddr, inet->inet_dport);
554553
struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
555554
struct inet_timewait_sock *tw = NULL;
556555
const struct hlist_nulls_node *node;
@@ -1007,9 +1006,10 @@ static u32 *table_perturb;
10071006

10081007
int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10091008
struct sock *sk, u64 port_offset,
1009+
u32 hash_port0,
10101010
int (*check_established)(struct inet_timewait_death_row *,
10111011
struct sock *, __u16, struct inet_timewait_sock **,
1012-
bool rcu_lookup))
1012+
bool rcu_lookup, u32 hash))
10131013
{
10141014
struct inet_hashinfo *hinfo = death_row->hashinfo;
10151015
struct inet_bind_hashbucket *head, *head2;
@@ -1027,7 +1027,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10271027

10281028
if (port) {
10291029
local_bh_disable();
1030-
ret = check_established(death_row, sk, port, NULL, false);
1030+
ret = check_established(death_row, sk, port, NULL, false,
1031+
hash_port0 + port);
10311032
local_bh_enable();
10321033
return ret;
10331034
}
@@ -1071,7 +1072,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10711072
rcu_read_unlock();
10721073
goto next_port;
10731074
}
1074-
if (!check_established(death_row, sk, port, &tw, true))
1075+
if (!check_established(death_row, sk, port, &tw, true,
1076+
hash_port0 + port))
10751077
break;
10761078
rcu_read_unlock();
10771079
goto next_port;
@@ -1090,7 +1092,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
10901092
goto next_port_unlock;
10911093
WARN_ON(hlist_empty(&tb->bhash2));
10921094
if (!check_established(death_row, sk,
1093-
port, &tw, false))
1095+
port, &tw, false,
1096+
hash_port0 + port))
10941097
goto ok;
10951098
goto next_port_unlock;
10961099
}
@@ -1197,11 +1200,18 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
11971200
int inet_hash_connect(struct inet_timewait_death_row *death_row,
11981201
struct sock *sk)
11991202
{
1203+
const struct inet_sock *inet = inet_sk(sk);
1204+
const struct net *net = sock_net(sk);
12001205
u64 port_offset = 0;
1206+
u32 hash_port0;
12011207

12021208
if (!inet_sk(sk)->inet_num)
12031209
port_offset = inet_sk_port_offset(sk);
1204-
return __inet_hash_connect(death_row, sk, port_offset,
1210+
1211+
hash_port0 = inet_ehashfn(net, inet->inet_rcv_saddr, 0,
1212+
inet->inet_daddr, inet->inet_dport);
1213+
1214+
return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
12051215
__inet_check_established);
12061216
}
12071217
EXPORT_SYMBOL_GPL(inet_hash_connect);

net/ipv6/inet6_hashtables.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ EXPORT_SYMBOL_GPL(inet6_lookup);
264264
static int __inet6_check_established(struct inet_timewait_death_row *death_row,
265265
struct sock *sk, const __u16 lport,
266266
struct inet_timewait_sock **twp,
267-
bool rcu_lookup)
267+
bool rcu_lookup,
268+
u32 hash)
268269
{
269270
struct inet_hashinfo *hinfo = death_row->hashinfo;
270271
struct inet_sock *inet = inet_sk(sk);
@@ -274,8 +275,6 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
274275
struct net *net = sock_net(sk);
275276
const int sdif = l3mdev_master_ifindex_by_index(net, dif);
276277
const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
277-
const unsigned int hash = inet6_ehashfn(net, daddr, lport, saddr,
278-
inet->inet_dport);
279278
struct inet_ehash_bucket *head = inet_ehash_bucket(hinfo, hash);
280279
struct inet_timewait_sock *tw = NULL;
281280
const struct hlist_nulls_node *node;
@@ -354,11 +353,19 @@ static u64 inet6_sk_port_offset(const struct sock *sk)
354353
int inet6_hash_connect(struct inet_timewait_death_row *death_row,
355354
struct sock *sk)
356355
{
356+
const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
357+
const struct in6_addr *saddr = &sk->sk_v6_daddr;
358+
const struct inet_sock *inet = inet_sk(sk);
359+
const struct net *net = sock_net(sk);
357360
u64 port_offset = 0;
361+
u32 hash_port0;
358362

359363
if (!inet_sk(sk)->inet_num)
360364
port_offset = inet6_sk_port_offset(sk);
361-
return __inet_hash_connect(death_row, sk, port_offset,
365+
366+
hash_port0 = inet6_ehashfn(net, daddr, 0, saddr, inet->inet_dport);
367+
368+
return __inet_hash_connect(death_row, sk, port_offset, hash_port0,
362369
__inet6_check_established);
363370
}
364371
EXPORT_SYMBOL_GPL(inet6_hash_connect);

0 commit comments

Comments
 (0)