chash.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. * Copyright 2017 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. */
  23. #include <linux/types.h>
  24. #include <linux/hash.h>
  25. #include <linux/bug.h>
  26. #include <linux/slab.h>
  27. #include <linux/module.h>
  28. #include <linux/sched/clock.h>
  29. #include <asm/div64.h>
  30. #include <linux/chash.h>
  31. /**
  32. * chash_table_alloc - Allocate closed hash table
  33. * @table: Pointer to the table structure
  34. * @bits: Table size will be 2^bits entries
  35. * @key_size: Size of hash keys in bytes, 4 or 8
  36. * @value_size: Size of data values in bytes, can be 0
  37. */
  38. int chash_table_alloc(struct chash_table *table, u8 bits, u8 key_size,
  39. unsigned int value_size, gfp_t gfp_mask)
  40. {
  41. if (bits > 31)
  42. return -EINVAL;
  43. if (key_size != 4 && key_size != 8)
  44. return -EINVAL;
  45. table->data = kcalloc(__CHASH_DATA_SIZE(bits, key_size, value_size),
  46. sizeof(long), gfp_mask);
  47. if (!table->data)
  48. return -ENOMEM;
  49. __CHASH_TABLE_INIT(table->table, table->data,
  50. bits, key_size, value_size);
  51. return 0;
  52. }
  53. EXPORT_SYMBOL(chash_table_alloc);
  54. /**
  55. * chash_table_free - Free closed hash table
  56. * @table: Pointer to the table structure
  57. */
  58. void chash_table_free(struct chash_table *table)
  59. {
  60. kfree(table->data);
  61. }
  62. EXPORT_SYMBOL(chash_table_free);
  63. #ifdef CONFIG_CHASH_STATS
  64. #define DIV_FRAC(nom, denom, quot, frac, frac_digits) do { \
  65. u64 __nom = (nom); \
  66. u64 __denom = (denom); \
  67. u64 __quot, __frac; \
  68. u32 __rem; \
  69. \
  70. while (__denom >> 32) { \
  71. __nom >>= 1; \
  72. __denom >>= 1; \
  73. } \
  74. __quot = __nom; \
  75. __rem = do_div(__quot, __denom); \
  76. __frac = __rem * (frac_digits) + (__denom >> 1); \
  77. do_div(__frac, __denom); \
  78. (quot) = __quot; \
  79. (frac) = __frac; \
  80. } while (0)
  81. void __chash_table_dump_stats(struct __chash_table *table)
  82. {
  83. struct chash_iter iter = CHASH_ITER_INIT(table, 0);
  84. u32 filled = 0, empty = 0, tombstones = 0;
  85. u64 quot1, quot2;
  86. u32 frac1, frac2;
  87. do {
  88. if (chash_iter_is_valid(iter))
  89. filled++;
  90. else if (chash_iter_is_empty(iter))
  91. empty++;
  92. else
  93. tombstones++;
  94. CHASH_ITER_INC(iter);
  95. } while (iter.slot);
  96. pr_debug("chash: key size %u, value size %u\n",
  97. table->key_size, table->value_size);
  98. pr_debug(" Slots total/filled/empty/tombstones: %u / %u / %u / %u\n",
  99. 1 << table->bits, filled, empty, tombstones);
  100. if (table->hits > 0) {
  101. DIV_FRAC(table->hits_steps, table->hits, quot1, frac1, 1000);
  102. DIV_FRAC(table->hits * 1000, table->hits_time_ns,
  103. quot2, frac2, 1000);
  104. } else {
  105. quot1 = quot2 = 0;
  106. frac1 = frac2 = 0;
  107. }
  108. pr_debug(" Hits (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
  109. table->hits, quot1, frac1, quot2, frac2);
  110. if (table->miss > 0) {
  111. DIV_FRAC(table->miss_steps, table->miss, quot1, frac1, 1000);
  112. DIV_FRAC(table->miss * 1000, table->miss_time_ns,
  113. quot2, frac2, 1000);
  114. } else {
  115. quot1 = quot2 = 0;
  116. frac1 = frac2 = 0;
  117. }
  118. pr_debug(" Misses (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
  119. table->miss, quot1, frac1, quot2, frac2);
  120. if (table->hits + table->miss > 0) {
  121. DIV_FRAC(table->hits_steps + table->miss_steps,
  122. table->hits + table->miss, quot1, frac1, 1000);
  123. DIV_FRAC((table->hits + table->miss) * 1000,
  124. (table->hits_time_ns + table->miss_time_ns),
  125. quot2, frac2, 1000);
  126. } else {
  127. quot1 = quot2 = 0;
  128. frac1 = frac2 = 0;
  129. }
  130. pr_debug(" Total (avg.cost, rate): %llu (%llu.%03u, %llu.%03u M/s)\n",
  131. table->hits + table->miss, quot1, frac1, quot2, frac2);
  132. if (table->relocs > 0) {
  133. DIV_FRAC(table->hits + table->miss, table->relocs,
  134. quot1, frac1, 1000);
  135. DIV_FRAC(table->reloc_dist, table->relocs, quot2, frac2, 1000);
  136. pr_debug(" Relocations (freq, avg.dist): %llu (1:%llu.%03u, %llu.%03u)\n",
  137. table->relocs, quot1, frac1, quot2, frac2);
  138. } else {
  139. pr_debug(" No relocations\n");
  140. }
  141. }
  142. EXPORT_SYMBOL(__chash_table_dump_stats);
  143. #undef DIV_FRAC
  144. #endif
  145. #define CHASH_INC(table, a) ((a) = ((a) + 1) & (table)->size_mask)
  146. #define CHASH_ADD(table, a, b) (((a) + (b)) & (table)->size_mask)
  147. #define CHASH_SUB(table, a, b) (((a) - (b)) & (table)->size_mask)
  148. #define CHASH_IN_RANGE(table, slot, first, last) \
  149. (CHASH_SUB(table, slot, first) <= CHASH_SUB(table, last, first))
  150. /*#define CHASH_DEBUG Uncomment this to enable verbose debug output*/
  151. #ifdef CHASH_DEBUG
  152. static void chash_table_dump(struct __chash_table *table)
  153. {
  154. struct chash_iter iter = CHASH_ITER_INIT(table, 0);
  155. do {
  156. if ((iter.slot & 3) == 0)
  157. pr_debug("%04x: ", iter.slot);
  158. if (chash_iter_is_valid(iter))
  159. pr_debug("[%016llx] ", chash_iter_key(iter));
  160. else if (chash_iter_is_empty(iter))
  161. pr_debug("[ <empty> ] ");
  162. else
  163. pr_debug("[ <tombstone> ] ");
  164. if ((iter.slot & 3) == 3)
  165. pr_debug("\n");
  166. CHASH_ITER_INC(iter);
  167. } while (iter.slot);
  168. if ((iter.slot & 3) != 0)
  169. pr_debug("\n");
  170. }
  171. static int chash_table_check(struct __chash_table *table)
  172. {
  173. u32 hash;
  174. struct chash_iter iter = CHASH_ITER_INIT(table, 0);
  175. struct chash_iter cur = CHASH_ITER_INIT(table, 0);
  176. do {
  177. if (!chash_iter_is_valid(iter)) {
  178. CHASH_ITER_INC(iter);
  179. continue;
  180. }
  181. hash = chash_iter_hash(iter);
  182. CHASH_ITER_SET(cur, hash);
  183. while (cur.slot != iter.slot) {
  184. if (chash_iter_is_empty(cur)) {
  185. pr_err("Path to element at %x with hash %x broken at slot %x\n",
  186. iter.slot, hash, cur.slot);
  187. chash_table_dump(table);
  188. return -EINVAL;
  189. }
  190. CHASH_ITER_INC(cur);
  191. }
  192. CHASH_ITER_INC(iter);
  193. } while (iter.slot);
  194. return 0;
  195. }
  196. #endif
  197. static void chash_iter_relocate(struct chash_iter dst, struct chash_iter src)
  198. {
  199. BUG_ON(src.table == dst.table && src.slot == dst.slot);
  200. BUG_ON(src.table->key_size != dst.table->key_size);
  201. BUG_ON(src.table->value_size != dst.table->value_size);
  202. if (dst.table->key_size == 4)
  203. dst.table->keys32[dst.slot] = src.table->keys32[src.slot];
  204. else
  205. dst.table->keys64[dst.slot] = src.table->keys64[src.slot];
  206. if (dst.table->value_size)
  207. memcpy(chash_iter_value(dst), chash_iter_value(src),
  208. dst.table->value_size);
  209. chash_iter_set_valid(dst);
  210. chash_iter_set_invalid(src);
  211. #ifdef CONFIG_CHASH_STATS
  212. if (src.table == dst.table) {
  213. dst.table->relocs++;
  214. dst.table->reloc_dist +=
  215. CHASH_SUB(dst.table, src.slot, dst.slot);
  216. }
  217. #endif
  218. }
  219. /**
  220. * __chash_table_find - Helper for looking up a hash table entry
  221. * @iter: Pointer to hash table iterator
  222. * @key: Key of the entry to find
  223. * @for_removal: set to true if the element will be removed soon
  224. *
  225. * Searches for an entry in the hash table with a given key. iter must
  226. * be initialized by the caller to point to the home position of the
  227. * hypothetical entry, i.e. it must be initialized with the hash table
  228. * and the key's hash as the initial slot for the search.
  229. *
  230. * This function also does some local clean-up to speed up future
  231. * look-ups by relocating entries to better slots and removing
  232. * tombstones that are no longer needed.
  233. *
  234. * If @for_removal is true, the function avoids relocating the entry
  235. * that is being returned.
  236. *
  237. * Returns 0 if the search is successful. In this case iter is updated
  238. * to point to the found entry. Otherwise %-EINVAL is returned and the
  239. * iter is updated to point to the first available slot for the given
  240. * key. If the table is full, the slot is set to -1.
  241. */
  242. static int chash_table_find(struct chash_iter *iter, u64 key,
  243. bool for_removal)
  244. {
  245. #ifdef CONFIG_CHASH_STATS
  246. u64 ts1 = local_clock();
  247. #endif
  248. u32 hash = iter->slot;
  249. struct chash_iter first_redundant = CHASH_ITER_INIT(iter->table, -1);
  250. int first_avail = (for_removal ? -2 : -1);
  251. while (!chash_iter_is_valid(*iter) || chash_iter_key(*iter) != key) {
  252. if (chash_iter_is_empty(*iter)) {
  253. /* Found an empty slot, which ends the
  254. * search. Clean up any preceding tombstones
  255. * that are no longer needed because they lead
  256. * to no-where
  257. */
  258. if ((int)first_redundant.slot < 0)
  259. goto not_found;
  260. while (first_redundant.slot != iter->slot) {
  261. if (!chash_iter_is_valid(first_redundant))
  262. chash_iter_set_empty(first_redundant);
  263. CHASH_ITER_INC(first_redundant);
  264. }
  265. #ifdef CHASH_DEBUG
  266. chash_table_check(iter->table);
  267. #endif
  268. goto not_found;
  269. } else if (!chash_iter_is_valid(*iter)) {
  270. /* Found a tombstone. Remember it as candidate
  271. * for relocating the entry we're looking for
  272. * or for adding a new entry with the given key
  273. */
  274. if (first_avail == -1)
  275. first_avail = iter->slot;
  276. /* Or mark it as the start of a series of
  277. * potentially redundant tombstones
  278. */
  279. else if (first_redundant.slot == -1)
  280. CHASH_ITER_SET(first_redundant, iter->slot);
  281. } else if (first_redundant.slot >= 0) {
  282. /* Found a valid, occupied slot with a
  283. * preceding series of tombstones. Relocate it
  284. * to a better position that no longer depends
  285. * on those tombstones
  286. */
  287. u32 cur_hash = chash_iter_hash(*iter);
  288. if (!CHASH_IN_RANGE(iter->table, cur_hash,
  289. first_redundant.slot + 1,
  290. iter->slot)) {
  291. /* This entry has a hash at or before
  292. * the first tombstone we found. We
  293. * can relocate it to that tombstone
  294. * and advance to the next tombstone
  295. */
  296. chash_iter_relocate(first_redundant, *iter);
  297. do {
  298. CHASH_ITER_INC(first_redundant);
  299. } while (chash_iter_is_valid(first_redundant));
  300. } else if (cur_hash != iter->slot) {
  301. /* Relocate entry to its home position
  302. * or as close as possible so it no
  303. * longer depends on any preceding
  304. * tombstones
  305. */
  306. struct chash_iter new_iter =
  307. CHASH_ITER_INIT(iter->table, cur_hash);
  308. while (new_iter.slot != iter->slot &&
  309. chash_iter_is_valid(new_iter))
  310. CHASH_ITER_INC(new_iter);
  311. if (new_iter.slot != iter->slot)
  312. chash_iter_relocate(new_iter, *iter);
  313. }
  314. }
  315. CHASH_ITER_INC(*iter);
  316. if (iter->slot == hash) {
  317. iter->slot = -1;
  318. goto not_found;
  319. }
  320. }
  321. #ifdef CONFIG_CHASH_STATS
  322. iter->table->hits++;
  323. iter->table->hits_steps += CHASH_SUB(iter->table, iter->slot, hash) + 1;
  324. #endif
  325. if (first_avail >= 0) {
  326. CHASH_ITER_SET(first_redundant, first_avail);
  327. chash_iter_relocate(first_redundant, *iter);
  328. iter->slot = first_redundant.slot;
  329. iter->mask = first_redundant.mask;
  330. }
  331. #ifdef CONFIG_CHASH_STATS
  332. iter->table->hits_time_ns += local_clock() - ts1;
  333. #endif
  334. return 0;
  335. not_found:
  336. #ifdef CONFIG_CHASH_STATS
  337. iter->table->miss++;
  338. iter->table->miss_steps += (iter->slot < 0) ?
  339. (1 << iter->table->bits) :
  340. CHASH_SUB(iter->table, iter->slot, hash) + 1;
  341. #endif
  342. if (first_avail >= 0)
  343. CHASH_ITER_SET(*iter, first_avail);
  344. #ifdef CONFIG_CHASH_STATS
  345. iter->table->miss_time_ns += local_clock() - ts1;
  346. #endif
  347. return -EINVAL;
  348. }
  349. int __chash_table_copy_in(struct __chash_table *table, u64 key,
  350. const void *value)
  351. {
  352. u32 hash = (table->key_size == 4) ?
  353. hash_32(key, table->bits) : hash_64(key, table->bits);
  354. struct chash_iter iter = CHASH_ITER_INIT(table, hash);
  355. int r = chash_table_find(&iter, key, false);
  356. /* Found an existing entry */
  357. if (!r) {
  358. if (value && table->value_size)
  359. memcpy(chash_iter_value(iter), value,
  360. table->value_size);
  361. return 1;
  362. }
  363. /* Is there a place to add a new entry? */
  364. if (iter.slot < 0) {
  365. pr_err("Hash table overflow\n");
  366. return -ENOMEM;
  367. }
  368. chash_iter_set_valid(iter);
  369. if (table->key_size == 4)
  370. table->keys32[iter.slot] = key;
  371. else
  372. table->keys64[iter.slot] = key;
  373. if (value && table->value_size)
  374. memcpy(chash_iter_value(iter), value, table->value_size);
  375. return 0;
  376. }
  377. EXPORT_SYMBOL(__chash_table_copy_in);
  378. int __chash_table_copy_out(struct __chash_table *table, u64 key,
  379. void *value, bool remove)
  380. {
  381. u32 hash = (table->key_size == 4) ?
  382. hash_32(key, table->bits) : hash_64(key, table->bits);
  383. struct chash_iter iter = CHASH_ITER_INIT(table, hash);
  384. int r = chash_table_find(&iter, key, remove);
  385. if (r < 0)
  386. return r;
  387. if (value && table->value_size)
  388. memcpy(value, chash_iter_value(iter), table->value_size);
  389. if (remove)
  390. chash_iter_set_invalid(iter);
  391. return iter.slot;
  392. }
  393. EXPORT_SYMBOL(__chash_table_copy_out);
  394. #ifdef CONFIG_CHASH_SELFTEST
  395. /**
  396. * chash_self_test - Run a self-test of the hash table implementation
  397. * @bits: Table size will be 2^bits entries
  398. * @key_size: Size of hash keys in bytes, 4 or 8
  399. * @min_fill: Minimum fill level during the test
  400. * @max_fill: Maximum fill level during the test
  401. * @iterations: Number of test iterations
  402. *
  403. * The test adds and removes entries from a hash table, cycling the
  404. * fill level between min_fill and max_fill entries. Also tests lookup
  405. * and value retrieval.
  406. */
  407. static int __init chash_self_test(u8 bits, u8 key_size,
  408. int min_fill, int max_fill,
  409. u64 iterations)
  410. {
  411. struct chash_table table;
  412. int ret;
  413. u64 add_count, rmv_count;
  414. u64 value;
  415. if (key_size == 4 && iterations > 0xffffffff)
  416. return -EINVAL;
  417. if (min_fill >= max_fill)
  418. return -EINVAL;
  419. ret = chash_table_alloc(&table, bits, key_size, sizeof(u64),
  420. GFP_KERNEL);
  421. if (ret) {
  422. pr_err("chash_table_alloc failed: %d\n", ret);
  423. return ret;
  424. }
  425. for (add_count = 0, rmv_count = 0; add_count < iterations;
  426. add_count++) {
  427. /* When we hit the max_fill level, remove entries down
  428. * to min_fill
  429. */
  430. if (add_count - rmv_count == max_fill) {
  431. u64 find_count = rmv_count;
  432. /* First try to find all entries that we're
  433. * about to remove, confirm their value, test
  434. * writing them back a second time.
  435. */
  436. for (; add_count - find_count > min_fill;
  437. find_count++) {
  438. ret = chash_table_copy_out(&table, find_count,
  439. &value);
  440. if (ret < 0) {
  441. pr_err("chash_table_copy_out failed: %d\n",
  442. ret);
  443. goto out;
  444. }
  445. if (value != ~find_count) {
  446. pr_err("Wrong value retrieved for key 0x%llx, expected 0x%llx got 0x%llx\n",
  447. find_count, ~find_count, value);
  448. #ifdef CHASH_DEBUG
  449. chash_table_dump(&table.table);
  450. #endif
  451. ret = -EFAULT;
  452. goto out;
  453. }
  454. ret = chash_table_copy_in(&table, find_count,
  455. &value);
  456. if (ret != 1) {
  457. pr_err("copy_in second time returned %d, expected 1\n",
  458. ret);
  459. ret = -EFAULT;
  460. goto out;
  461. }
  462. }
  463. /* Remove them until we hit min_fill level */
  464. for (; add_count - rmv_count > min_fill; rmv_count++) {
  465. ret = chash_table_remove(&table, rmv_count,
  466. NULL);
  467. if (ret < 0) {
  468. pr_err("chash_table_remove failed: %d\n",
  469. ret);
  470. goto out;
  471. }
  472. }
  473. }
  474. /* Add a new value */
  475. value = ~add_count;
  476. ret = chash_table_copy_in(&table, add_count, &value);
  477. if (ret != 0) {
  478. pr_err("copy_in first time returned %d, expected 0\n",
  479. ret);
  480. ret = -EFAULT;
  481. goto out;
  482. }
  483. }
  484. chash_table_dump_stats(&table);
  485. chash_table_reset_stats(&table);
  486. out:
  487. chash_table_free(&table);
  488. return ret;
  489. }
  490. static unsigned int chash_test_bits = 10;
  491. MODULE_PARM_DESC(test_bits,
  492. "Selftest number of hash bits ([4..20], default=10)");
  493. module_param_named(test_bits, chash_test_bits, uint, 0444);
  494. static unsigned int chash_test_keysize = 8;
  495. MODULE_PARM_DESC(test_keysize, "Selftest keysize (4 or 8, default=8)");
  496. module_param_named(test_keysize, chash_test_keysize, uint, 0444);
  497. static unsigned int chash_test_minfill;
  498. MODULE_PARM_DESC(test_minfill, "Selftest minimum #entries (default=50%)");
  499. module_param_named(test_minfill, chash_test_minfill, uint, 0444);
  500. static unsigned int chash_test_maxfill;
  501. MODULE_PARM_DESC(test_maxfill, "Selftest maximum #entries (default=80%)");
  502. module_param_named(test_maxfill, chash_test_maxfill, uint, 0444);
  503. static unsigned long chash_test_iters;
  504. MODULE_PARM_DESC(test_iters, "Selftest iterations (default=1000 x #entries)");
  505. module_param_named(test_iters, chash_test_iters, ulong, 0444);
  506. static int __init chash_init(void)
  507. {
  508. int ret;
  509. u64 ts1_ns;
  510. /* Skip self test on user errors */
  511. if (chash_test_bits < 4 || chash_test_bits > 20) {
  512. pr_err("chash: test_bits out of range [4..20].\n");
  513. return 0;
  514. }
  515. if (chash_test_keysize != 4 && chash_test_keysize != 8) {
  516. pr_err("chash: test_keysize invalid. Must be 4 or 8.\n");
  517. return 0;
  518. }
  519. if (!chash_test_minfill)
  520. chash_test_minfill = (1 << chash_test_bits) / 2;
  521. if (!chash_test_maxfill)
  522. chash_test_maxfill = (1 << chash_test_bits) * 4 / 5;
  523. if (!chash_test_iters)
  524. chash_test_iters = (1 << chash_test_bits) * 1000;
  525. if (chash_test_minfill >= (1 << chash_test_bits)) {
  526. pr_err("chash: test_minfill too big. Must be < table size.\n");
  527. return 0;
  528. }
  529. if (chash_test_maxfill >= (1 << chash_test_bits)) {
  530. pr_err("chash: test_maxfill too big. Must be < table size.\n");
  531. return 0;
  532. }
  533. if (chash_test_minfill >= chash_test_maxfill) {
  534. pr_err("chash: test_minfill must be < test_maxfill.\n");
  535. return 0;
  536. }
  537. if (chash_test_keysize == 4 && chash_test_iters > 0xffffffff) {
  538. pr_err("chash: test_iters must be < 4G for 4 byte keys.\n");
  539. return 0;
  540. }
  541. ts1_ns = local_clock();
  542. ret = chash_self_test(chash_test_bits, chash_test_keysize,
  543. chash_test_minfill, chash_test_maxfill,
  544. chash_test_iters);
  545. if (!ret) {
  546. u64 ts_delta_us = local_clock() - ts1_ns;
  547. u64 iters_per_second = (u64)chash_test_iters * 1000000;
  548. do_div(ts_delta_us, 1000);
  549. do_div(iters_per_second, ts_delta_us);
  550. pr_info("chash: self test took %llu us, %llu iterations/s\n",
  551. ts_delta_us, iters_per_second);
  552. } else {
  553. pr_err("chash: self test failed: %d\n", ret);
  554. }
  555. return ret;
  556. }
  557. module_init(chash_init);
  558. #endif /* CONFIG_CHASH_SELFTEST */
  559. MODULE_DESCRIPTION("Closed hash table");
  560. MODULE_LICENSE("GPL and additional rights");