Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Francesco Fusco and committed by
David S. Miller
71ae8aac 89e47d3b

+180 -2
+7
arch/x86/include/asm/hash.h
··· 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 */
+1 -1
arch/x86/lib/Makefile
··· 24 24 lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o 25 25 lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o 26 26 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 28 28 29 29 ifeq ($(CONFIG_X86_32),y) 30 30 obj-y += atomic64_32.o
+88
arch/x86/lib/hash.c
··· 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 + }
+9
include/asm-generic/hash.h
··· 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 */
+36
include/linux/hash.h
··· 15 15 */ 16 16 17 17 #include <asm/types.h> 18 + #include <asm/hash.h> 18 19 #include <linux/compiler.h> 19 20 20 21 /* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ ··· 79 78 #endif 80 79 return (u32)val; 81 80 } 81 + 82 + struct fast_hash_ops { 83 + u32 (*hash)(const void *data, u32 len, u32 seed); 84 + u32 (*hash2)(const u32 *data, u32 len, u32 seed); 85 + }; 86 + 87 + /** 88 + * arch_fast_hash - Caclulates a hash over a given buffer that can have 89 + * arbitrary size. This function will eventually use an 90 + * architecture-optimized hashing implementation if 91 + * available, and trades off distribution for speed. 92 + * 93 + * @data: buffer to hash 94 + * @len: length of buffer in bytes 95 + * @seed: start seed 96 + * 97 + * Returns 32bit hash. 98 + */ 99 + extern u32 arch_fast_hash(const void *data, u32 len, u32 seed); 100 + 101 + /** 102 + * arch_fast_hash2 - Caclulates a hash over a given buffer that has a 103 + * size that is of a multiple of 32bit words. This 104 + * function will eventually use an architecture- 105 + * optimized hashing implementation if available, 106 + * and trades off distribution for speed. 107 + * 108 + * @data: buffer to hash (must be 32bit padded) 109 + * @len: number of 32bit words 110 + * @seed: start seed 111 + * 112 + * Returns 32bit hash. 113 + */ 114 + extern u32 arch_fast_hash2(const u32 *data, u32 len, u32 seed); 115 + 82 116 #endif /* _LINUX_HASH_H */
+1 -1
lib/Makefile
··· 26 26 bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ 27 27 gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \ 28 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 30 30 obj-y += string_helpers.o 31 31 obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o 32 32 obj-y += kstrtox.o
+38
lib/hash.c
··· 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);