1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Test cases for <linux/hash.h> and <linux/stringhash.h>
4*4882a593Smuzhiyun * This just verifies that various ways of computing a hash
5*4882a593Smuzhiyun * produce the same thing and, for cases where a k-bit hash
6*4882a593Smuzhiyun * value is requested, is of the requested size.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * We fill a buffer with a 255-byte null-terminated string,
9*4882a593Smuzhiyun * and use both full_name_hash() and hashlen_string() to hash the
10*4882a593Smuzhiyun * substrings from i to j, where 0 <= i < j < 256.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * The returned values are used to check that __hash_32() and
13*4882a593Smuzhiyun * __hash_32_generic() compute the same thing. Likewise hash_32()
14*4882a593Smuzhiyun * and hash_64().
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt "\n"
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun #include <linux/compiler.h>
20*4882a593Smuzhiyun #include <linux/types.h>
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/hash.h>
23*4882a593Smuzhiyun #include <linux/stringhash.h>
24*4882a593Smuzhiyun #include <linux/printk.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun /* 32-bit XORSHIFT generator. Seed must not be zero. */
27*4882a593Smuzhiyun static u32 __init __attribute_const__
xorshift(u32 seed)28*4882a593Smuzhiyun xorshift(u32 seed)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun seed ^= seed << 13;
31*4882a593Smuzhiyun seed ^= seed >> 17;
32*4882a593Smuzhiyun seed ^= seed << 5;
33*4882a593Smuzhiyun return seed;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun /* Given a non-zero x, returns a non-zero byte. */
37*4882a593Smuzhiyun static u8 __init __attribute_const__
mod255(u32 x)38*4882a593Smuzhiyun mod255(u32 x)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun x = (x & 0xffff) + (x >> 16); /* 1 <= x <= 0x1fffe */
41*4882a593Smuzhiyun x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x2fd */
42*4882a593Smuzhiyun x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0x100 */
43*4882a593Smuzhiyun x = (x & 0xff) + (x >> 8); /* 1 <= x <= 0xff */
44*4882a593Smuzhiyun return x;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* Fill the buffer with non-zero bytes. */
48*4882a593Smuzhiyun static void __init
fill_buf(char * buf,size_t len,u32 seed)49*4882a593Smuzhiyun fill_buf(char *buf, size_t len, u32 seed)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun size_t i;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun for (i = 0; i < len; i++) {
54*4882a593Smuzhiyun seed = xorshift(seed);
55*4882a593Smuzhiyun buf[i] = mod255(seed);
56*4882a593Smuzhiyun }
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /*
60*4882a593Smuzhiyun * Test the various integer hash functions. h64 (or its low-order bits)
61*4882a593Smuzhiyun * is the integer to hash. hash_or accumulates the OR of the hash values,
62*4882a593Smuzhiyun * which are later checked to see that they cover all the requested bits.
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * Because these functions (as opposed to the string hashes) are all
65*4882a593Smuzhiyun * inline, the code being tested is actually in the module, and you can
66*4882a593Smuzhiyun * recompile and re-test the module without rebooting.
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun static bool __init
test_int_hash(unsigned long long h64,u32 hash_or[2][33])69*4882a593Smuzhiyun test_int_hash(unsigned long long h64, u32 hash_or[2][33])
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun int k;
72*4882a593Smuzhiyun u32 h0 = (u32)h64, h1, h2;
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* Test __hash32 */
75*4882a593Smuzhiyun hash_or[0][0] |= h1 = __hash_32(h0);
76*4882a593Smuzhiyun #ifdef HAVE_ARCH__HASH_32
77*4882a593Smuzhiyun hash_or[1][0] |= h2 = __hash_32_generic(h0);
78*4882a593Smuzhiyun #if HAVE_ARCH__HASH_32 == 1
79*4882a593Smuzhiyun if (h1 != h2) {
80*4882a593Smuzhiyun pr_err("__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
81*4882a593Smuzhiyun h0, h1, h2);
82*4882a593Smuzhiyun return false;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun #endif
85*4882a593Smuzhiyun #endif
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* Test k = 1..32 bits */
88*4882a593Smuzhiyun for (k = 1; k <= 32; k++) {
89*4882a593Smuzhiyun u32 const m = ((u32)2 << (k-1)) - 1; /* Low k bits set */
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* Test hash_32 */
92*4882a593Smuzhiyun hash_or[0][k] |= h1 = hash_32(h0, k);
93*4882a593Smuzhiyun if (h1 > m) {
94*4882a593Smuzhiyun pr_err("hash_32(%#x, %d) = %#x > %#x", h0, k, h1, m);
95*4882a593Smuzhiyun return false;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun #ifdef HAVE_ARCH_HASH_32
98*4882a593Smuzhiyun h2 = hash_32_generic(h0, k);
99*4882a593Smuzhiyun #if HAVE_ARCH_HASH_32 == 1
100*4882a593Smuzhiyun if (h1 != h2) {
101*4882a593Smuzhiyun pr_err("hash_32(%#x, %d) = %#x != hash_32_generic() "
102*4882a593Smuzhiyun " = %#x", h0, k, h1, h2);
103*4882a593Smuzhiyun return false;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun #else
106*4882a593Smuzhiyun if (h2 > m) {
107*4882a593Smuzhiyun pr_err("hash_32_generic(%#x, %d) = %#x > %#x",
108*4882a593Smuzhiyun h0, k, h1, m);
109*4882a593Smuzhiyun return false;
110*4882a593Smuzhiyun }
111*4882a593Smuzhiyun #endif
112*4882a593Smuzhiyun #endif
113*4882a593Smuzhiyun /* Test hash_64 */
114*4882a593Smuzhiyun hash_or[1][k] |= h1 = hash_64(h64, k);
115*4882a593Smuzhiyun if (h1 > m) {
116*4882a593Smuzhiyun pr_err("hash_64(%#llx, %d) = %#x > %#x", h64, k, h1, m);
117*4882a593Smuzhiyun return false;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun #ifdef HAVE_ARCH_HASH_64
120*4882a593Smuzhiyun h2 = hash_64_generic(h64, k);
121*4882a593Smuzhiyun #if HAVE_ARCH_HASH_64 == 1
122*4882a593Smuzhiyun if (h1 != h2) {
123*4882a593Smuzhiyun pr_err("hash_64(%#llx, %d) = %#x != hash_64_generic() "
124*4882a593Smuzhiyun "= %#x", h64, k, h1, h2);
125*4882a593Smuzhiyun return false;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun #else
128*4882a593Smuzhiyun if (h2 > m) {
129*4882a593Smuzhiyun pr_err("hash_64_generic(%#llx, %d) = %#x > %#x",
130*4882a593Smuzhiyun h64, k, h1, m);
131*4882a593Smuzhiyun return false;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun #endif
134*4882a593Smuzhiyun #endif
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun (void)h2; /* Suppress unused variable warning */
138*4882a593Smuzhiyun return true;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun #define SIZE 256 /* Run time is cubic in SIZE */
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun static int __init
test_hash_init(void)144*4882a593Smuzhiyun test_hash_init(void)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun char buf[SIZE+1];
147*4882a593Smuzhiyun u32 string_or = 0, hash_or[2][33] = { { 0, } };
148*4882a593Smuzhiyun unsigned tests = 0;
149*4882a593Smuzhiyun unsigned long long h64 = 0;
150*4882a593Smuzhiyun int i, j;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun fill_buf(buf, SIZE, 1);
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /* Test every possible non-empty substring in the buffer. */
155*4882a593Smuzhiyun for (j = SIZE; j > 0; --j) {
156*4882a593Smuzhiyun buf[j] = '\0';
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun for (i = 0; i <= j; i++) {
159*4882a593Smuzhiyun u64 hashlen = hashlen_string(buf+i, buf+i);
160*4882a593Smuzhiyun u32 h0 = full_name_hash(buf+i, buf+i, j-i);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /* Check that hashlen_string gets the length right */
163*4882a593Smuzhiyun if (hashlen_len(hashlen) != j-i) {
164*4882a593Smuzhiyun pr_err("hashlen_string(%d..%d) returned length"
165*4882a593Smuzhiyun " %u, expected %d",
166*4882a593Smuzhiyun i, j, hashlen_len(hashlen), j-i);
167*4882a593Smuzhiyun return -EINVAL;
168*4882a593Smuzhiyun }
169*4882a593Smuzhiyun /* Check that the hashes match */
170*4882a593Smuzhiyun if (hashlen_hash(hashlen) != h0) {
171*4882a593Smuzhiyun pr_err("hashlen_string(%d..%d) = %08x != "
172*4882a593Smuzhiyun "full_name_hash() = %08x",
173*4882a593Smuzhiyun i, j, hashlen_hash(hashlen), h0);
174*4882a593Smuzhiyun return -EINVAL;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun string_or |= h0;
178*4882a593Smuzhiyun h64 = h64 << 32 | h0; /* For use with hash_64 */
179*4882a593Smuzhiyun if (!test_int_hash(h64, hash_or))
180*4882a593Smuzhiyun return -EINVAL;
181*4882a593Smuzhiyun tests++;
182*4882a593Smuzhiyun } /* i */
183*4882a593Smuzhiyun } /* j */
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun /* The OR of all the hash values should cover all the bits */
186*4882a593Smuzhiyun if (~string_or) {
187*4882a593Smuzhiyun pr_err("OR of all string hash results = %#x != %#x",
188*4882a593Smuzhiyun string_or, -1u);
189*4882a593Smuzhiyun return -EINVAL;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun if (~hash_or[0][0]) {
192*4882a593Smuzhiyun pr_err("OR of all __hash_32 results = %#x != %#x",
193*4882a593Smuzhiyun hash_or[0][0], -1u);
194*4882a593Smuzhiyun return -EINVAL;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun #ifdef HAVE_ARCH__HASH_32
197*4882a593Smuzhiyun #if HAVE_ARCH__HASH_32 != 1 /* Test is pointless if results match */
198*4882a593Smuzhiyun if (~hash_or[1][0]) {
199*4882a593Smuzhiyun pr_err("OR of all __hash_32_generic results = %#x != %#x",
200*4882a593Smuzhiyun hash_or[1][0], -1u);
201*4882a593Smuzhiyun return -EINVAL;
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun #endif
204*4882a593Smuzhiyun #endif
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun /* Likewise for all the i-bit hash values */
207*4882a593Smuzhiyun for (i = 1; i <= 32; i++) {
208*4882a593Smuzhiyun u32 const m = ((u32)2 << (i-1)) - 1; /* Low i bits set */
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun if (hash_or[0][i] != m) {
211*4882a593Smuzhiyun pr_err("OR of all hash_32(%d) results = %#x "
212*4882a593Smuzhiyun "(%#x expected)", i, hash_or[0][i], m);
213*4882a593Smuzhiyun return -EINVAL;
214*4882a593Smuzhiyun }
215*4882a593Smuzhiyun if (hash_or[1][i] != m) {
216*4882a593Smuzhiyun pr_err("OR of all hash_64(%d) results = %#x "
217*4882a593Smuzhiyun "(%#x expected)", i, hash_or[1][i], m);
218*4882a593Smuzhiyun return -EINVAL;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /* Issue notices about skipped tests. */
223*4882a593Smuzhiyun #ifdef HAVE_ARCH__HASH_32
224*4882a593Smuzhiyun #if HAVE_ARCH__HASH_32 != 1
225*4882a593Smuzhiyun pr_info("__hash_32() is arch-specific; not compared to generic.");
226*4882a593Smuzhiyun #endif
227*4882a593Smuzhiyun #else
228*4882a593Smuzhiyun pr_info("__hash_32() has no arch implementation to test.");
229*4882a593Smuzhiyun #endif
230*4882a593Smuzhiyun #ifdef HAVE_ARCH_HASH_32
231*4882a593Smuzhiyun #if HAVE_ARCH_HASH_32 != 1
232*4882a593Smuzhiyun pr_info("hash_32() is arch-specific; not compared to generic.");
233*4882a593Smuzhiyun #endif
234*4882a593Smuzhiyun #else
235*4882a593Smuzhiyun pr_info("hash_32() has no arch implementation to test.");
236*4882a593Smuzhiyun #endif
237*4882a593Smuzhiyun #ifdef HAVE_ARCH_HASH_64
238*4882a593Smuzhiyun #if HAVE_ARCH_HASH_64 != 1
239*4882a593Smuzhiyun pr_info("hash_64() is arch-specific; not compared to generic.");
240*4882a593Smuzhiyun #endif
241*4882a593Smuzhiyun #else
242*4882a593Smuzhiyun pr_info("hash_64() has no arch implementation to test.");
243*4882a593Smuzhiyun #endif
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun pr_notice("%u tests passed.", tests);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun return 0;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
test_hash_exit(void)250*4882a593Smuzhiyun static void __exit test_hash_exit(void)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun module_init(test_hash_init); /* Does everything */
255*4882a593Smuzhiyun module_exit(test_hash_exit); /* Does nothing */
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun MODULE_LICENSE("GPL");
258