Skip to content

Commit 71ae8aa

Browse files
Francesco Fuscodavem330
authored andcommitted
lib: introduce arch optimized hash library
We introduce a new hashing library that is meant to be used in the contexts where speed is more important than uniformity of the hashed values. The hash library leverages architecture specific implementation to achieve high performance and fall backs to jhash() for the generic case. On Intel-based x86 architectures, the library can exploit the crc32l instruction, part of the Intel SSE4.2 instruction set, if the instruction is supported by the processor. This implementation is twice as fast as the jhash() implementation on an i7 processor. Additional architectures, such as Arm64 provide instructions for accelerating the computation of CRC, so they could be added as well in follow-up work. Signed-off-by: Francesco Fusco <ffusco@redhat.com> Signed-off-by: Daniel Borkmann <dborkman@redhat.com> Signed-off-by: Thomas Graf <tgraf@redhat.com> Cc: linux-kernel@vger.kernel.org Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 89e47d3 commit 71ae8aa

File tree

7 files changed

+180
-2
lines changed

7 files changed

+180
-2
lines changed

arch/x86/include/asm/hash.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _ASM_X86_HASH_H
2+
#define _ASM_X86_HASH_H
3+
4+
struct fast_hash_ops;
5+
extern void setup_arch_fast_hash(struct fast_hash_ops *ops);
6+
7+
#endif /* _ASM_X86_HASH_H */

arch/x86/lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ lib-$(CONFIG_SMP) += rwlock.o
2424
lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
2525
lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o
2626

27-
obj-y += msr.o msr-reg.o msr-reg-export.o
27+
obj-y += msr.o msr-reg.o msr-reg-export.o hash.o
2828

2929
ifeq ($(CONFIG_X86_32),y)
3030
obj-y += atomic64_32.o

arch/x86/lib/hash.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
40+
static inline u32 crc32_u32(u32 crc, u32 val)
41+
{
42+
asm ("crc32l %1,%0\n" : "+r" (crc) : "rm" (val));
43+
return crc;
44+
}
45+
46+
static 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+
71+
static 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+
82+
void 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+
}

include/asm-generic/hash.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef __ASM_GENERIC_HASH_H
2+
#define __ASM_GENERIC_HASH_H
3+
4+
struct arch_hash_ops;
5+
static inline void setup_arch_fast_hash(struct arch_hash_ops *ops)
6+
{
7+
}
8+
9+
#endif /* __ASM_GENERIC_HASH_H */

include/linux/hash.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <asm/types.h>
18+
#include <asm/hash.h>
1819
#include <linux/compiler.h>
1920

2021
/* 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)
7879
#endif
7980
return (u32)val;
8081
}
82+
83+
struct 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+
*/
100+
extern 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+
*/
115+
extern u32 arch_fast_hash2(const u32 *data, u32 len, u32 seed);
116+
81117
#endif /* _LINUX_HASH_H */

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
2626
bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \
2727
gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \
2828
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
3030
obj-y += string_helpers.o
3131
obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
3232
obj-y += kstrtox.o

lib/hash.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
16+
static struct fast_hash_ops arch_hash_ops __read_mostly = {
17+
.hash = jhash,
18+
.hash2 = jhash2,
19+
};
20+
21+
u32 arch_fast_hash(const void *data, u32 len, u32 seed)
22+
{
23+
return arch_hash_ops.hash(data, len, seed);
24+
}
25+
EXPORT_SYMBOL_GPL(arch_fast_hash);
26+
27+
u32 arch_fast_hash2(const u32 *data, u32 len, u32 seed)
28+
{
29+
return arch_hash_ops.hash2(data, len, seed);
30+
}
31+
EXPORT_SYMBOL_GPL(arch_fast_hash2);
32+
33+
static int __init hashlib_init(void)
34+
{
35+
setup_arch_fast_hash(&arch_hash_ops);
36+
return 0;
37+
}
38+
early_initcall(hashlib_init);

0 commit comments

Comments
 (0)