All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] NET : inet_ehash_secret should be __read_mostly and set only once
@ 2007-03-27 10:13 Eric Dumazet
  2007-03-27 20:53 ` David Miller
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Dumazet @ 2007-03-27 10:13 UTC (permalink / raw)
  To: David Miller; +Cc: netdev

Hi David

Please find this patch against net-2.6.22

Thank you

[PATCH] NET : inet_ehash_secret should be __read_mostly and set only once

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>

diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index b7b7278..597646b 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -218,13 +218,23 @@ out:
 	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);
 
@@ -684,7 +694,8 @@ int inet_sendmsg(struct kiocb *iocb, str
 }
 
 
-static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset, size_t size, int flags)
+static ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
+	size_t size, int flags)
 {
 	struct sock *sk = sock->sk;
 

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-03-27 21:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-27 10:13 [PATCH] NET : inet_ehash_secret should be __read_mostly and set only once Eric Dumazet
2007-03-27 20:53 ` David Miller
2007-03-27 21:03   ` Eric Dumazet

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.