This source file includes following definitions.
- xorshift
- mod255
- fill_buf
- test_int_hash
- test_hash_init
- test_hash_exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt "\n"
18
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>
25
26
27 static u32 __init __attribute_const__
28 xorshift(u32 seed)
29 {
30 seed ^= seed << 13;
31 seed ^= seed >> 17;
32 seed ^= seed << 5;
33 return seed;
34 }
35
36
37 static u8 __init __attribute_const__
38 mod255(u32 x)
39 {
40 x = (x & 0xffff) + (x >> 16);
41 x = (x & 0xff) + (x >> 8);
42 x = (x & 0xff) + (x >> 8);
43 x = (x & 0xff) + (x >> 8);
44 return x;
45 }
46
47
48 static void __init
49 fill_buf(char *buf, size_t len, u32 seed)
50 {
51 size_t i;
52
53 for (i = 0; i < len; i++) {
54 seed = xorshift(seed);
55 buf[i] = mod255(seed);
56 }
57 }
58
59
60
61
62
63
64
65
66
67
68 static bool __init
69 test_int_hash(unsigned long long h64, u32 hash_or[2][33])
70 {
71 int k;
72 u32 h0 = (u32)h64, h1, h2;
73
74
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
79 if (h1 != h2) {
80 pr_err("__hash_32(%#x) = %#x != __hash_32_generic() = %#x",
81 h0, h1, h2);
82 return false;
83 }
84 #endif
85 #endif
86
87
88 for (k = 1; k <= 32; k++) {
89 u32 const m = ((u32)2 << (k-1)) - 1;
90
91
92 hash_or[0][k] |= h1 = hash_32(h0, k);
93 if (h1 > m) {
94 pr_err("hash_32(%#x, %d) = %#x > %#x", h0, k, h1, m);
95 return false;
96 }
97 #ifdef HAVE_ARCH_HASH_32
98 h2 = hash_32_generic(h0, k);
99 #if HAVE_ARCH_HASH_32 == 1
100 if (h1 != h2) {
101 pr_err("hash_32(%#x, %d) = %#x != hash_32_generic() "
102 " = %#x", h0, k, h1, h2);
103 return false;
104 }
105 #else
106 if (h2 > m) {
107 pr_err("hash_32_generic(%#x, %d) = %#x > %#x",
108 h0, k, h1, m);
109 return false;
110 }
111 #endif
112 #endif
113
114 hash_or[1][k] |= h1 = hash_64(h64, k);
115 if (h1 > m) {
116 pr_err("hash_64(%#llx, %d) = %#x > %#x", h64, k, h1, m);
117 return false;
118 }
119 #ifdef HAVE_ARCH_HASH_64
120 h2 = hash_64_generic(h64, k);
121 #if HAVE_ARCH_HASH_64 == 1
122 if (h1 != h2) {
123 pr_err("hash_64(%#llx, %d) = %#x != hash_64_generic() "
124 "= %#x", h64, k, h1, h2);
125 return false;
126 }
127 #else
128 if (h2 > m) {
129 pr_err("hash_64_generic(%#llx, %d) = %#x > %#x",
130 h64, k, h1, m);
131 return false;
132 }
133 #endif
134 #endif
135 }
136
137 (void)h2;
138 return true;
139 }
140
141 #define SIZE 256
142
143 static int __init
144 test_hash_init(void)
145 {
146 char buf[SIZE+1];
147 u32 string_or = 0, hash_or[2][33] = { { 0, } };
148 unsigned tests = 0;
149 unsigned long long h64 = 0;
150 int i, j;
151
152 fill_buf(buf, SIZE, 1);
153
154
155 for (j = SIZE; j > 0; --j) {
156 buf[j] = '\0';
157
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);
161
162
163 if (hashlen_len(hashlen) != j-i) {
164 pr_err("hashlen_string(%d..%d) returned length"
165 " %u, expected %d",
166 i, j, hashlen_len(hashlen), j-i);
167 return -EINVAL;
168 }
169
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);
174 return -EINVAL;
175 }
176
177 string_or |= h0;
178 h64 = h64 << 32 | h0;
179 if (!test_int_hash(h64, hash_or))
180 return -EINVAL;
181 tests++;
182 }
183 }
184
185
186 if (~string_or) {
187 pr_err("OR of all string hash results = %#x != %#x",
188 string_or, -1u);
189 return -EINVAL;
190 }
191 if (~hash_or[0][0]) {
192 pr_err("OR of all __hash_32 results = %#x != %#x",
193 hash_or[0][0], -1u);
194 return -EINVAL;
195 }
196 #ifdef HAVE_ARCH__HASH_32
197 #if HAVE_ARCH__HASH_32 != 1
198 if (~hash_or[1][0]) {
199 pr_err("OR of all __hash_32_generic results = %#x != %#x",
200 hash_or[1][0], -1u);
201 return -EINVAL;
202 }
203 #endif
204 #endif
205
206
207 for (i = 1; i <= 32; i++) {
208 u32 const m = ((u32)2 << (i-1)) - 1;
209
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);
213 return -EINVAL;
214 }
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);
218 return -EINVAL;
219 }
220 }
221
222
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.");
226 #endif
227 #else
228 pr_info("__hash_32() has no arch implementation to test.");
229 #endif
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.");
233 #endif
234 #else
235 pr_info("hash_32() has no arch implementation to test.");
236 #endif
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.");
240 #endif
241 #else
242 pr_info("hash_64() has no arch implementation to test.");
243 #endif
244
245 pr_notice("%u tests passed.", tests);
246
247 return 0;
248 }
249
250 static void __exit test_hash_exit(void)
251 {
252 }
253
254 module_init(test_hash_init);
255 module_exit(test_hash_exit);
256
257 MODULE_LICENSE("GPL");