Skip to content

Commit

Permalink
[NET]: inet_ehash_secret should be __read_mostly and set only once
Browse files Browse the repository at this point in the history
There is a very tiny probability that build_ehash_secret() is called
at the same time by different CPUS.

Also, using __read_mostly is a must for inet_ehash_secret

Signed-off-by: Eric Dumazet <dada1@cosmosbay.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Eric Dumazet authored and David S. Miller committed Apr 26, 2007
1 parent 35fc92a commit be77628
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions net/ipv4/af_inet.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,23 @@ int inet_listen(struct socket *sock, int backlog)
return err;
}

u32 inet_ehash_secret;
u32 inet_ehash_secret __read_mostly;
EXPORT_SYMBOL(inet_ehash_secret);

/*
* inet_ehash_secret must be set exactly once
* Instead of using a dedicated spinlock, we (ab)use inetsw_lock
*/
void build_ehash_secret(void)
{
while (!inet_ehash_secret)
get_random_bytes(&inet_ehash_secret, 4);
u32 rnd;
do {
get_random_bytes(&rnd, sizeof(rnd));
} while (rnd == 0);
spin_lock_bh(&inetsw_lock);
if (!inet_ehash_secret)
inet_ehash_secret = rnd;
spin_unlock_bh(&inetsw_lock);
}
EXPORT_SYMBOL(build_ehash_secret);

Expand Down

0 comments on commit be77628

Please sign in to comment.