test_hash.c 6.3 KB

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