aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--arch/x86/include/asm/hash.h7
-rw-r--r--arch/x86/lib/Makefile2
-rw-r--r--arch/x86/lib/hash.c88
-rw-r--r--include/asm-generic/hash.h9
-rw-r--r--include/linux/hash.h36
-rw-r--r--lib/Makefile2
-rw-r--r--lib/hash.c38
7 files changed, 180 insertions, 2 deletions
diff --git a/arch/x86/include/asm/hash.h b/arch/x86/include/asm/hash.h
new file mode 100644
index 000000000000..e8c58f88b1d4
--- /dev/null
+++ b/arch/x86/include/asm/hash.h
@@ -0,0 +1,7 @@
1#ifndef _ASM_X86_HASH_H
2#define _ASM_X86_HASH_H
3
4struct fast_hash_ops;
5extern void setup_arch_fast_hash(struct fast_hash_ops *ops);
6
7#endif /* _ASM_X86_HASH_H */
diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
index 992d63bb154f..eabcb6e6a900 100644
--- a/arch/x86/lib/Makefile
+++ b/arch/x86/lib/Makefile
@@ -24,7 +24,7 @@ lib-$(CONFIG_SMP) += rwlock.o
24lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o 24lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
25lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o 25lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o
26 26
27obj-y += msr.o msr-reg.o msr-reg-export.o 27obj-y += msr.o msr-reg.o msr-reg-export.o hash.o
28 28
29ifeq ($(CONFIG_X86_32),y) 29ifeq ($(CONFIG_X86_32),y)
30 obj-y += atomic64_32.o 30 obj-y += atomic64_32.o
diff --git a/arch/x86/lib/hash.c b/arch/x86/lib/hash.c
new file mode 100644
index 000000000000..3056702e81fb
--- /dev/null
+++ b/arch/x86/lib/hash.c
@@ -0,0 +1,88 @@
1/*
2 * Some portions derived from code covered by the following notice:
3 *
4 * Copyright (c) 2010-2013 Intel Corporation. All rights reserved.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <linux/hash.h>
35
36#include <asm/processor.h>
37#include <asm/cpufeature.h>
38#include <asm/hash.h>
39
40static inline u32 crc32_u32(u32 crc, u32 val)
41{
42 asm ("crc32l %1,%0\n" : "+r" (crc) : "rm" (val));
43 return crc;
44}
45
46static u32 intel_crc4_2_hash(const void *data, u32 len, u32 seed)
47{
48 const u32 *p32 = (const u32 *) data;
49 u32 i, tmp = 0;
50
51 for (i = 0; i < len / 4; i++)
52 seed = crc32_u32(*p32++, seed);
53
54 switch (3 - (len & 0x03)) {
55 case 0:
56 tmp |= *((const u8 *) p32 + 2) << 16;
57 /* fallthrough */
58 case 1:
59 tmp |= *((const u8 *) p32 + 1) << 8;
60 /* fallthrough */
61 case 2:
62 tmp |= *((const u8 *) p32);
63 seed = crc32_u32(tmp, seed);
64 default:
65 break;
66 }
67
68 return seed;
69}
70
71static u32 intel_crc4_2_hash2(const u32 *data, u32 len, u32 seed)
72{
73 const u32 *p32 = (const u32 *) data;
74 u32 i;
75
76 for (i = 0; i < len; i++)
77 seed = crc32_u32(*p32++, seed);
78
79 return seed;
80}
81
82void setup_arch_fast_hash(struct fast_hash_ops *ops)
83{
84 if (cpu_has_xmm4_2) {
85 ops->hash = intel_crc4_2_hash;
86 ops->hash2 = intel_crc4_2_hash2;
87 }
88}
diff --git a/include/asm-generic/hash.h b/include/asm-generic/hash.h
new file mode 100644
index 000000000000..05cb3421ee7e
--- /dev/null
+++ b/include/asm-generic/hash.h
@@ -0,0 +1,9 @@
1#ifndef __ASM_GENERIC_HASH_H
2#define __ASM_GENERIC_HASH_H
3
4struct arch_hash_ops;
5static inline void setup_arch_fast_hash(struct arch_hash_ops *ops)
6{
7}
8
9#endif /* __ASM_GENERIC_HASH_H */
diff --git a/include/linux/hash.h b/include/linux/hash.h
index f09a0ae4d858..bd1754c7ecef 100644
--- a/include/linux/hash.h
+++ b/include/linux/hash.h
@@ -15,6 +15,7 @@
15 */ 15 */
16 16
17#include <asm/types.h> 17#include <asm/types.h>
18#include <asm/hash.h>
18#include <linux/compiler.h> 19#include <linux/compiler.h>
19 20
20/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ 21/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
@@ -78,4 +79,39 @@ static inline u32 hash32_ptr(const void *ptr)
78#endif 79#endif
79 return (u32)val; 80 return (u32)val;
80} 81}
82
83struct fast_hash_ops {
84 u32 (*hash)(const void *data, u32 len, u32 seed);
85 u32 (*hash2)(const u32 *data, u32 len, u32 seed);
86};
87
88/**
89 * arch_fast_hash - Caclulates a hash over a given buffer that can have
90 * arbitrary size. This function will eventually use an
91 * architecture-optimized hashing implementation if
92 * available, and trades off distribution for speed.
93 *
94 * @data: buffer to hash
95 * @len: length of buffer in bytes
96 * @seed: start seed
97 *
98 * Returns 32bit hash.
99 */
100extern u32 arch_fast_hash(const void *data, u32 len, u32 seed);
101
102/**
103 * arch_fast_hash2 - Caclulates a hash over a given buffer that has a
104 * size that is of a multiple of 32bit words. This
105 * function will eventually use an architecture-
106 * optimized hashing implementation if available,
107 * and trades off distribution for speed.
108 *
109 * @data: buffer to hash (must be 32bit padded)
110 * @len: number of 32bit words
111 * @seed: start seed
112 *
113 * Returns 32bit hash.
114 */
115extern u32 arch_fast_hash2(const u32 *data, u32 len, u32 seed);
116
81#endif /* _LINUX_HASH_H */ 117#endif /* _LINUX_HASH_H */
diff --git a/lib/Makefile b/lib/Makefile
index a459c31e8c6b..d0f79c547d97 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -26,7 +26,7 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
26 bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ 26 bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \
27 gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \ 27 gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \
28 bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \ 28 bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \
29 percpu-refcount.o percpu_ida.o 29 percpu-refcount.o percpu_ida.o hash.o
30obj-y += string_helpers.o 30obj-y += string_helpers.o
31obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o 31obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
32obj-y += kstrtox.o 32obj-y += kstrtox.o
diff --git a/lib/hash.c b/lib/hash.c
new file mode 100644
index 000000000000..b89f06a2d606
--- /dev/null
+++ b/lib/hash.c
@@ -0,0 +1,38 @@
1/* General purpose hashing library
2 *
3 * That's a start of a kernel hashing library, which can be extended
4 * with further algorithms in future. arch_fast_hash{2,}() will
5 * eventually resolve to an architecture optimized implementation.
6 *
7 * Copyright 2013 Francesco Fusco <ffusco@redhat.com>
8 * Copyright 2013 Daniel Borkmann <dborkman@redhat.com>
9 * Copyright 2013 Thomas Graf <tgraf@redhat.com>
10 * Licensed under the GNU General Public License, version 2.0 (GPLv2)
11 */
12
13#include <linux/jhash.h>
14#include <linux/hash.h>
15
16static struct fast_hash_ops arch_hash_ops __read_mostly = {
17 .hash = jhash,
18 .hash2 = jhash2,
19};
20
21u32 arch_fast_hash(const void *data, u32 len, u32 seed)
22{
23 return arch_hash_ops.hash(data, len, seed);
24}
25EXPORT_SYMBOL_GPL(arch_fast_hash);
26
27u32 arch_fast_hash2(const u32 *data, u32 len, u32 seed)
28{
29 return arch_hash_ops.hash2(data, len, seed);
30}
31EXPORT_SYMBOL_GPL(arch_fast_hash2);
32
33static int __init hashlib_init(void)
34{
35 setup_arch_fast_hash(&arch_hash_ops);
36 return 0;
37}
38early_initcall(hashlib_init);