Subject: [PATCH 1/6] hash: Introduce ptr_hash_mix routine

From: Pavel Emelyanov <[email-masked]>
Date: Mon, 06 Aug 2012 18:13:47 +0400

This one is used to make a salt out of a pointer to be mixed to some
hash function later. Idea and implementation are proposed by Eric Dumazet.

Signed-off-by: Pavel Emelyanov <[email-masked]>
---
 include/linux/hash.h     |   10 ++++++++++
 include/net/netns/hash.h |    9 ++-------
 2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/include/linux/hash.h b/include/linux/hash.h
index b80506b..1bd0ab1 100644
--- a/include/linux/hash.h
+++ b/include/linux/hash.h
@@ -14,6 +14,7 @@
  * machines where multiplications are slow.
  */
 
+#include <linux/cache.h>
 #include <asm/types.h>
 
 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
@@ -67,4 +68,13 @@ static inline unsigned long hash_ptr(const void *ptr, unsigned int bits)
 {
 	return hash_long((unsigned long)ptr, bits);
 }
+
+static inline u32 ptr_hash_mix(const void *ptr)
+{
+#if BITS_PER_LONG == 32
+	return (u32)(unsigned long)ptr;
+#else
+	return (u32)((unsigned long)ptr >> L1_CACHE_SHIFT);
+#endif
+}
 #endif /* _LINUX_HASH_H */
diff --git a/include/net/netns/hash.h b/include/net/netns/hash.h
index c06ac58..bcdabe0 100644
--- a/include/net/netns/hash.h
+++ b/include/net/netns/hash.h
@@ -1,19 +1,14 @@
 #ifndef __NET_NS_HASH_H__
 #define __NET_NS_HASH_H__
 
-#include <asm/cache.h>
+#include <linux/hash.h>
 
 struct net;
 
 static inline unsigned int net_hash_mix(struct net *net)
 {
 #ifdef CONFIG_NET_NS
-	/*
-	 * shift this right to eliminate bits, that are
-	 * always zeroed
-	 */
-
-	return (unsigned)(((unsigned long)net) >> L1_CACHE_SHIFT);
+	return ptr_hash_mix(net);
 #else
 	return 0;
 #endif
-- 
1.7.6.5