1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test cases for <linux/hash.h> and <linux/stringhash.h>
4 * This just verifies that various ways of computing a hash
5 * produce the same thing and, for cases where a k-bit hash
6 * value is requested, is of the requested size.
8 * We fill a buffer with a 255-byte null-terminated string,
9 * and use both full_name_hash() and hashlen_string() to hash the
10 * substrings from i to j, where 0 <= i < j < 256.
12 * The returned values are used to check that __hash_32() and
13 * __hash_32_generic() compute the same thing. Likewise hash_32()
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt "\n"
19 #include <linux/compiler.h>
20 #include <linux/types.h>
21 #include <linux/module.h>
22 #include <linux/hash.h>
23 #include <linux/stringhash.h>
24 #include <linux/printk.h>
26 /* 32-bit XORSHIFT generator. Seed must not be zero. */
27 static u32 __init __attribute_const__
36 /* Given a non-zero x, returns a non-zero byte. */
37 static u8 __init __attribute_const__
40 x = (x & 0xffff) + (x >> 16); /* 1 <= x <= 0x1fffe */
41 x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x2fd */
42 x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x100 */
43 x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0xff */
47 /* Fill the buffer with non-zero bytes. */
49 fill_buf(char *buf, size_t len, u32 seed)
53 for (i = 0; i < len; i++) {
54 seed = xorshift(seed);
55 buf[i] = mod255(seed);
60 * Test the various integer hash functions. h64 (or its low-order bits)
61 * is the integer to hash. hash_or accumulates the OR of the hash values,
62 * which are later checked to see that they cover all the requested bits.
64 * Because these functions (as opposed to the string hashes) are all
65 * inline, the code being tested is actually in the module, and you can
66 * recompile and re-test the module without rebooting.
69 test_int_hash(unsigned long long h64, u32 hash_or[2][33])
72 u32 h0 = (u32)h64, h1, h2;
75 hash_or[0][0] |= h1 = __hash_32(h0);
76 #ifdef HAVE_ARCH__HASH_32
77 hash_or[1][0] |= h2 = __hash_32_generic(h0);
78 #if HAVE_ARCH__HASH_32 == 1
80 pr_err("__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
87 /* Test k = 1..32 bits */
88 for (k = 1; k <= 32; k++) {
89 u32 const m = ((u32)2 << (k-1)) - 1; /* Low k bits set */
92 hash_or[0][k] |= h1 = hash_32(h0, k);
94 pr_err("hash_32(%#x, %d) = %#x > %#x", h0, k, h1, m);
97 #ifdef HAVE_ARCH_HASH_32
98 h2 = hash_32_generic(h0, k);
99 #if HAVE_ARCH_HASH_32 == 1
101 pr_err("hash_32(%#x, %d) = %#x != hash_32_generic() "
102 " = %#x", h0, k, h1, h2);
107 pr_err("hash_32_generic(%#x, %d) = %#x > %#x",
114 hash_or[1][k] |= h1 = hash_64(h64, k);
116 pr_err("hash_64(%#llx, %d) = %#x > %#x", h64, k, h1, m);
119 #ifdef HAVE_ARCH_HASH_64
120 h2 = hash_64_generic(h64, k);
121 #if HAVE_ARCH_HASH_64 == 1
123 pr_err("hash_64(%#llx, %d) = %#x != hash_64_generic() "
124 "= %#x", h64, k, h1, h2);
129 pr_err("hash_64_generic(%#llx, %d) = %#x > %#x",
137 (void)h2; /* Suppress unused variable warning */
141 #define SIZE 256 /* Run time is cubic in SIZE */
147 u32 string_or = 0, hash_or[2][33] = { { 0, } };
149 unsigned long long h64 = 0;
152 fill_buf(buf, SIZE, 1);
154 /* Test every possible non-empty substring in the buffer. */
155 for (j = SIZE; j > 0; --j) {
158 for (i = 0; i <= j; i++) {
159 u64 hashlen = hashlen_string(buf+i, buf+i);
160 u32 h0 = full_name_hash(buf+i, buf+i, j-i);
162 /* Check that hashlen_string gets the length right */
163 if (hashlen_len(hashlen) != j-i) {
164 pr_err("hashlen_string(%d..%d) returned length"
166 i, j, hashlen_len(hashlen), j-i);
169 /* Check that the hashes match */
170 if (hashlen_hash(hashlen) != h0) {
171 pr_err("hashlen_string(%d..%d) = %08x != "
172 "full_name_hash() = %08x",
173 i, j, hashlen_hash(hashlen), h0);
178 h64 = h64 << 32 | h0; /* For use with hash_64 */
179 if (!test_int_hash(h64, hash_or))
185 /* The OR of all the hash values should cover all the bits */
187 pr_err("OR of all string hash results = %#x != %#x",
191 if (~hash_or[0][0]) {
192 pr_err("OR of all __hash_32 results = %#x != %#x",
196 #ifdef HAVE_ARCH__HASH_32
197 #if HAVE_ARCH__HASH_32 != 1 /* Test is pointless if results match */
198 if (~hash_or[1][0]) {
199 pr_err("OR of all __hash_32_generic results = %#x != %#x",
206 /* Likewise for all the i-bit hash values */
207 for (i = 1; i <= 32; i++) {
208 u32 const m = ((u32)2 << (i-1)) - 1; /* Low i bits set */
210 if (hash_or[0][i] != m) {
211 pr_err("OR of all hash_32(%d) results = %#x "
212 "(%#x expected)", i, hash_or[0][i], m);
215 if (hash_or[1][i] != m) {
216 pr_err("OR of all hash_64(%d) results = %#x "
217 "(%#x expected)", i, hash_or[1][i], m);
222 /* Issue notices about skipped tests. */
223 #ifdef HAVE_ARCH__HASH_32
224 #if HAVE_ARCH__HASH_32 != 1
225 pr_info("__hash_32() is arch-specific; not compared to generic.");
228 pr_info("__hash_32() has no arch implementation to test.");
230 #ifdef HAVE_ARCH_HASH_32
231 #if HAVE_ARCH_HASH_32 != 1
232 pr_info("hash_32() is arch-specific; not compared to generic.");
235 pr_info("hash_32() has no arch implementation to test.");
237 #ifdef HAVE_ARCH_HASH_64
238 #if HAVE_ARCH_HASH_64 != 1
239 pr_info("hash_64() is arch-specific; not compared to generic.");
242 pr_info("hash_64() has no arch implementation to test.");
245 pr_notice("%u tests passed.", tests);
250 static void __exit test_hash_exit(void)
254 module_init(test_hash_init); /* Does everything */
255 module_exit(test_hash_exit); /* Does nothing */
257 MODULE_LICENSE("GPL");