test_rhashtable.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Resizable, Scalable, Concurrent Hash Table
  4. *
  5. * Copyright (c) 2014-2015 Thomas Graf <[email protected]>
  6. * Copyright (c) 2008-2014 Patrick McHardy <[email protected]>
  7. */
  8. /**************************************************************************
  9. * Self Test
  10. **************************************************************************/
  11. #include <linux/init.h>
  12. #include <linux/jhash.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kthread.h>
  15. #include <linux/module.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/rhashtable.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/random.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/wait.h>
  23. #define MAX_ENTRIES 1000000
  24. #define TEST_INSERT_FAIL INT_MAX
  25. static int parm_entries = 50000;
  26. module_param(parm_entries, int, 0);
  27. MODULE_PARM_DESC(parm_entries, "Number of entries to add (default: 50000)");
  28. static int runs = 4;
  29. module_param(runs, int, 0);
  30. MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
  31. static int max_size = 0;
  32. module_param(max_size, int, 0);
  33. MODULE_PARM_DESC(max_size, "Maximum table size (default: calculated)");
  34. static bool shrinking = false;
  35. module_param(shrinking, bool, 0);
  36. MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
  37. static int size = 8;
  38. module_param(size, int, 0);
  39. MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
  40. static int tcount = 10;
  41. module_param(tcount, int, 0);
  42. MODULE_PARM_DESC(tcount, "Number of threads to spawn (default: 10)");
  43. static bool enomem_retry = false;
  44. module_param(enomem_retry, bool, 0);
  45. MODULE_PARM_DESC(enomem_retry, "Retry insert even if -ENOMEM was returned (default: off)");
  46. struct test_obj_val {
  47. int id;
  48. int tid;
  49. };
  50. struct test_obj {
  51. struct test_obj_val value;
  52. struct rhash_head node;
  53. };
  54. struct test_obj_rhl {
  55. struct test_obj_val value;
  56. struct rhlist_head list_node;
  57. };
  58. struct thread_data {
  59. unsigned int entries;
  60. int id;
  61. struct task_struct *task;
  62. struct test_obj *objs;
  63. };
  64. static u32 my_hashfn(const void *data, u32 len, u32 seed)
  65. {
  66. const struct test_obj_rhl *obj = data;
  67. return (obj->value.id % 10);
  68. }
  69. static int my_cmpfn(struct rhashtable_compare_arg *arg, const void *obj)
  70. {
  71. const struct test_obj_rhl *test_obj = obj;
  72. const struct test_obj_val *val = arg->key;
  73. return test_obj->value.id - val->id;
  74. }
  75. static struct rhashtable_params test_rht_params = {
  76. .head_offset = offsetof(struct test_obj, node),
  77. .key_offset = offsetof(struct test_obj, value),
  78. .key_len = sizeof(struct test_obj_val),
  79. .hashfn = jhash,
  80. };
  81. static struct rhashtable_params test_rht_params_dup = {
  82. .head_offset = offsetof(struct test_obj_rhl, list_node),
  83. .key_offset = offsetof(struct test_obj_rhl, value),
  84. .key_len = sizeof(struct test_obj_val),
  85. .hashfn = jhash,
  86. .obj_hashfn = my_hashfn,
  87. .obj_cmpfn = my_cmpfn,
  88. .nelem_hint = 128,
  89. .automatic_shrinking = false,
  90. };
  91. static atomic_t startup_count;
  92. static DECLARE_WAIT_QUEUE_HEAD(startup_wait);
  93. static int insert_retry(struct rhashtable *ht, struct test_obj *obj,
  94. const struct rhashtable_params params)
  95. {
  96. int err, retries = -1, enomem_retries = 0;
  97. do {
  98. retries++;
  99. cond_resched();
  100. err = rhashtable_insert_fast(ht, &obj->node, params);
  101. if (err == -ENOMEM && enomem_retry) {
  102. enomem_retries++;
  103. err = -EBUSY;
  104. }
  105. } while (err == -EBUSY);
  106. if (enomem_retries)
  107. pr_info(" %u insertions retried after -ENOMEM\n",
  108. enomem_retries);
  109. return err ? : retries;
  110. }
  111. static int __init test_rht_lookup(struct rhashtable *ht, struct test_obj *array,
  112. unsigned int entries)
  113. {
  114. unsigned int i;
  115. for (i = 0; i < entries; i++) {
  116. struct test_obj *obj;
  117. bool expected = !(i % 2);
  118. struct test_obj_val key = {
  119. .id = i,
  120. };
  121. if (array[i / 2].value.id == TEST_INSERT_FAIL)
  122. expected = false;
  123. obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
  124. if (expected && !obj) {
  125. pr_warn("Test failed: Could not find key %u\n", key.id);
  126. return -ENOENT;
  127. } else if (!expected && obj) {
  128. pr_warn("Test failed: Unexpected entry found for key %u\n",
  129. key.id);
  130. return -EEXIST;
  131. } else if (expected && obj) {
  132. if (obj->value.id != i) {
  133. pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
  134. obj->value.id, i);
  135. return -EINVAL;
  136. }
  137. }
  138. cond_resched_rcu();
  139. }
  140. return 0;
  141. }
  142. static void test_bucket_stats(struct rhashtable *ht, unsigned int entries)
  143. {
  144. unsigned int total = 0, chain_len = 0;
  145. struct rhashtable_iter hti;
  146. struct rhash_head *pos;
  147. rhashtable_walk_enter(ht, &hti);
  148. rhashtable_walk_start(&hti);
  149. while ((pos = rhashtable_walk_next(&hti))) {
  150. if (PTR_ERR(pos) == -EAGAIN) {
  151. pr_info("Info: encountered resize\n");
  152. chain_len++;
  153. continue;
  154. } else if (IS_ERR(pos)) {
  155. pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
  156. PTR_ERR(pos));
  157. break;
  158. }
  159. total++;
  160. }
  161. rhashtable_walk_stop(&hti);
  162. rhashtable_walk_exit(&hti);
  163. pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
  164. total, atomic_read(&ht->nelems), entries, chain_len);
  165. if (total != atomic_read(&ht->nelems) || total != entries)
  166. pr_warn("Test failed: Total count mismatch ^^^");
  167. }
  168. static s64 __init test_rhashtable(struct rhashtable *ht, struct test_obj *array,
  169. unsigned int entries)
  170. {
  171. struct test_obj *obj;
  172. int err;
  173. unsigned int i, insert_retries = 0;
  174. s64 start, end;
  175. /*
  176. * Insertion Test:
  177. * Insert entries into table with all keys even numbers
  178. */
  179. pr_info(" Adding %d keys\n", entries);
  180. start = ktime_get_ns();
  181. for (i = 0; i < entries; i++) {
  182. struct test_obj *obj = &array[i];
  183. obj->value.id = i * 2;
  184. err = insert_retry(ht, obj, test_rht_params);
  185. if (err > 0)
  186. insert_retries += err;
  187. else if (err)
  188. return err;
  189. }
  190. if (insert_retries)
  191. pr_info(" %u insertions retried due to memory pressure\n",
  192. insert_retries);
  193. test_bucket_stats(ht, entries);
  194. rcu_read_lock();
  195. test_rht_lookup(ht, array, entries);
  196. rcu_read_unlock();
  197. test_bucket_stats(ht, entries);
  198. pr_info(" Deleting %d keys\n", entries);
  199. for (i = 0; i < entries; i++) {
  200. struct test_obj_val key = {
  201. .id = i * 2,
  202. };
  203. if (array[i].value.id != TEST_INSERT_FAIL) {
  204. obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
  205. BUG_ON(!obj);
  206. rhashtable_remove_fast(ht, &obj->node, test_rht_params);
  207. }
  208. cond_resched();
  209. }
  210. end = ktime_get_ns();
  211. pr_info(" Duration of test: %lld ns\n", end - start);
  212. return end - start;
  213. }
  214. static struct rhashtable ht;
  215. static struct rhltable rhlt;
  216. static int __init test_rhltable(unsigned int entries)
  217. {
  218. struct test_obj_rhl *rhl_test_objects;
  219. unsigned long *obj_in_table;
  220. unsigned int i, j, k;
  221. int ret, err;
  222. if (entries == 0)
  223. entries = 1;
  224. rhl_test_objects = vzalloc(array_size(entries,
  225. sizeof(*rhl_test_objects)));
  226. if (!rhl_test_objects)
  227. return -ENOMEM;
  228. ret = -ENOMEM;
  229. obj_in_table = vzalloc(array_size(sizeof(unsigned long),
  230. BITS_TO_LONGS(entries)));
  231. if (!obj_in_table)
  232. goto out_free;
  233. err = rhltable_init(&rhlt, &test_rht_params);
  234. if (WARN_ON(err))
  235. goto out_free;
  236. k = get_random_u32();
  237. ret = 0;
  238. for (i = 0; i < entries; i++) {
  239. rhl_test_objects[i].value.id = k;
  240. err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node,
  241. test_rht_params);
  242. if (WARN(err, "error %d on element %d\n", err, i))
  243. break;
  244. if (err == 0)
  245. set_bit(i, obj_in_table);
  246. }
  247. if (err)
  248. ret = err;
  249. pr_info("test %d add/delete pairs into rhlist\n", entries);
  250. for (i = 0; i < entries; i++) {
  251. struct rhlist_head *h, *pos;
  252. struct test_obj_rhl *obj;
  253. struct test_obj_val key = {
  254. .id = k,
  255. };
  256. bool found;
  257. rcu_read_lock();
  258. h = rhltable_lookup(&rhlt, &key, test_rht_params);
  259. if (WARN(!h, "key not found during iteration %d of %d", i, entries)) {
  260. rcu_read_unlock();
  261. break;
  262. }
  263. if (i) {
  264. j = i - 1;
  265. rhl_for_each_entry_rcu(obj, pos, h, list_node) {
  266. if (WARN(pos == &rhl_test_objects[j].list_node, "old element found, should be gone"))
  267. break;
  268. }
  269. }
  270. cond_resched_rcu();
  271. found = false;
  272. rhl_for_each_entry_rcu(obj, pos, h, list_node) {
  273. if (pos == &rhl_test_objects[i].list_node) {
  274. found = true;
  275. break;
  276. }
  277. }
  278. rcu_read_unlock();
  279. if (WARN(!found, "element %d not found", i))
  280. break;
  281. err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
  282. WARN(err, "rhltable_remove: err %d for iteration %d\n", err, i);
  283. if (err == 0)
  284. clear_bit(i, obj_in_table);
  285. }
  286. if (ret == 0 && err)
  287. ret = err;
  288. for (i = 0; i < entries; i++) {
  289. WARN(test_bit(i, obj_in_table), "elem %d allegedly still present", i);
  290. err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node,
  291. test_rht_params);
  292. if (WARN(err, "error %d on element %d\n", err, i))
  293. break;
  294. if (err == 0)
  295. set_bit(i, obj_in_table);
  296. }
  297. pr_info("test %d random rhlist add/delete operations\n", entries);
  298. for (j = 0; j < entries; j++) {
  299. u32 i = prandom_u32_max(entries);
  300. u32 prand = prandom_u32_max(4);
  301. cond_resched();
  302. err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
  303. if (test_bit(i, obj_in_table)) {
  304. clear_bit(i, obj_in_table);
  305. if (WARN(err, "cannot remove element at slot %d", i))
  306. continue;
  307. } else {
  308. if (WARN(err != -ENOENT, "removed non-existent element %d, error %d not %d",
  309. i, err, -ENOENT))
  310. continue;
  311. }
  312. if (prand & 1) {
  313. err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
  314. if (err == 0) {
  315. if (WARN(test_and_set_bit(i, obj_in_table), "succeeded to insert same object %d", i))
  316. continue;
  317. } else {
  318. if (WARN(!test_bit(i, obj_in_table), "failed to insert object %d", i))
  319. continue;
  320. }
  321. }
  322. if (prand & 2) {
  323. i = prandom_u32_max(entries);
  324. if (test_bit(i, obj_in_table)) {
  325. err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
  326. WARN(err, "cannot remove element at slot %d", i);
  327. if (err == 0)
  328. clear_bit(i, obj_in_table);
  329. } else {
  330. err = rhltable_insert(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
  331. WARN(err, "failed to insert object %d", i);
  332. if (err == 0)
  333. set_bit(i, obj_in_table);
  334. }
  335. }
  336. }
  337. for (i = 0; i < entries; i++) {
  338. cond_resched();
  339. err = rhltable_remove(&rhlt, &rhl_test_objects[i].list_node, test_rht_params);
  340. if (test_bit(i, obj_in_table)) {
  341. if (WARN(err, "cannot remove element at slot %d", i))
  342. continue;
  343. } else {
  344. if (WARN(err != -ENOENT, "removed non-existent element, error %d not %d",
  345. err, -ENOENT))
  346. continue;
  347. }
  348. }
  349. rhltable_destroy(&rhlt);
  350. out_free:
  351. vfree(rhl_test_objects);
  352. vfree(obj_in_table);
  353. return ret;
  354. }
  355. static int __init test_rhashtable_max(struct test_obj *array,
  356. unsigned int entries)
  357. {
  358. unsigned int i, insert_retries = 0;
  359. int err;
  360. test_rht_params.max_size = roundup_pow_of_two(entries / 8);
  361. err = rhashtable_init(&ht, &test_rht_params);
  362. if (err)
  363. return err;
  364. for (i = 0; i < ht.max_elems; i++) {
  365. struct test_obj *obj = &array[i];
  366. obj->value.id = i * 2;
  367. err = insert_retry(&ht, obj, test_rht_params);
  368. if (err > 0)
  369. insert_retries += err;
  370. else if (err)
  371. return err;
  372. }
  373. err = insert_retry(&ht, &array[ht.max_elems], test_rht_params);
  374. if (err == -E2BIG) {
  375. err = 0;
  376. } else {
  377. pr_info("insert element %u should have failed with %d, got %d\n",
  378. ht.max_elems, -E2BIG, err);
  379. if (err == 0)
  380. err = -1;
  381. }
  382. rhashtable_destroy(&ht);
  383. return err;
  384. }
  385. static unsigned int __init print_ht(struct rhltable *rhlt)
  386. {
  387. struct rhashtable *ht;
  388. const struct bucket_table *tbl;
  389. char buff[512] = "";
  390. int offset = 0;
  391. unsigned int i, cnt = 0;
  392. ht = &rhlt->ht;
  393. /* Take the mutex to avoid RCU warning */
  394. mutex_lock(&ht->mutex);
  395. tbl = rht_dereference(ht->tbl, ht);
  396. for (i = 0; i < tbl->size; i++) {
  397. struct rhash_head *pos, *next;
  398. struct test_obj_rhl *p;
  399. pos = rht_ptr_exclusive(tbl->buckets + i);
  400. next = !rht_is_a_nulls(pos) ? rht_dereference(pos->next, ht) : NULL;
  401. if (!rht_is_a_nulls(pos)) {
  402. offset += sprintf(buff + offset, "\nbucket[%d] -> ", i);
  403. }
  404. while (!rht_is_a_nulls(pos)) {
  405. struct rhlist_head *list = container_of(pos, struct rhlist_head, rhead);
  406. offset += sprintf(buff + offset, "[[");
  407. do {
  408. pos = &list->rhead;
  409. list = rht_dereference(list->next, ht);
  410. p = rht_obj(ht, pos);
  411. offset += sprintf(buff + offset, " val %d (tid=%d)%s", p->value.id, p->value.tid,
  412. list? ", " : " ");
  413. cnt++;
  414. } while (list);
  415. pos = next,
  416. next = !rht_is_a_nulls(pos) ?
  417. rht_dereference(pos->next, ht) : NULL;
  418. offset += sprintf(buff + offset, "]]%s", !rht_is_a_nulls(pos) ? " -> " : "");
  419. }
  420. }
  421. printk(KERN_ERR "\n---- ht: ----%s\n-------------\n", buff);
  422. mutex_unlock(&ht->mutex);
  423. return cnt;
  424. }
  425. static int __init test_insert_dup(struct test_obj_rhl *rhl_test_objects,
  426. int cnt, bool slow)
  427. {
  428. struct rhltable *rhlt;
  429. unsigned int i, ret;
  430. const char *key;
  431. int err = 0;
  432. rhlt = kmalloc(sizeof(*rhlt), GFP_KERNEL);
  433. if (WARN_ON(!rhlt))
  434. return -EINVAL;
  435. err = rhltable_init(rhlt, &test_rht_params_dup);
  436. if (WARN_ON(err)) {
  437. kfree(rhlt);
  438. return err;
  439. }
  440. for (i = 0; i < cnt; i++) {
  441. rhl_test_objects[i].value.tid = i;
  442. key = rht_obj(&rhlt->ht, &rhl_test_objects[i].list_node.rhead);
  443. key += test_rht_params_dup.key_offset;
  444. if (slow) {
  445. err = PTR_ERR(rhashtable_insert_slow(&rhlt->ht, key,
  446. &rhl_test_objects[i].list_node.rhead));
  447. if (err == -EAGAIN)
  448. err = 0;
  449. } else
  450. err = rhltable_insert(rhlt,
  451. &rhl_test_objects[i].list_node,
  452. test_rht_params_dup);
  453. if (WARN(err, "error %d on element %d/%d (%s)\n", err, i, cnt, slow? "slow" : "fast"))
  454. goto skip_print;
  455. }
  456. ret = print_ht(rhlt);
  457. WARN(ret != cnt, "missing rhltable elements (%d != %d, %s)\n", ret, cnt, slow? "slow" : "fast");
  458. skip_print:
  459. rhltable_destroy(rhlt);
  460. kfree(rhlt);
  461. return 0;
  462. }
  463. static int __init test_insert_duplicates_run(void)
  464. {
  465. struct test_obj_rhl rhl_test_objects[3] = {};
  466. pr_info("test inserting duplicates\n");
  467. /* two different values that map to same bucket */
  468. rhl_test_objects[0].value.id = 1;
  469. rhl_test_objects[1].value.id = 21;
  470. /* and another duplicate with same as [0] value
  471. * which will be second on the bucket list */
  472. rhl_test_objects[2].value.id = rhl_test_objects[0].value.id;
  473. test_insert_dup(rhl_test_objects, 2, false);
  474. test_insert_dup(rhl_test_objects, 3, false);
  475. test_insert_dup(rhl_test_objects, 2, true);
  476. test_insert_dup(rhl_test_objects, 3, true);
  477. return 0;
  478. }
  479. static int thread_lookup_test(struct thread_data *tdata)
  480. {
  481. unsigned int entries = tdata->entries;
  482. int i, err = 0;
  483. for (i = 0; i < entries; i++) {
  484. struct test_obj *obj;
  485. struct test_obj_val key = {
  486. .id = i,
  487. .tid = tdata->id,
  488. };
  489. obj = rhashtable_lookup_fast(&ht, &key, test_rht_params);
  490. if (obj && (tdata->objs[i].value.id == TEST_INSERT_FAIL)) {
  491. pr_err(" found unexpected object %d-%d\n", key.tid, key.id);
  492. err++;
  493. } else if (!obj && (tdata->objs[i].value.id != TEST_INSERT_FAIL)) {
  494. pr_err(" object %d-%d not found!\n", key.tid, key.id);
  495. err++;
  496. } else if (obj && memcmp(&obj->value, &key, sizeof(key))) {
  497. pr_err(" wrong object returned (got %d-%d, expected %d-%d)\n",
  498. obj->value.tid, obj->value.id, key.tid, key.id);
  499. err++;
  500. }
  501. cond_resched();
  502. }
  503. return err;
  504. }
  505. static int threadfunc(void *data)
  506. {
  507. int i, step, err = 0, insert_retries = 0;
  508. struct thread_data *tdata = data;
  509. if (atomic_dec_and_test(&startup_count))
  510. wake_up(&startup_wait);
  511. if (wait_event_interruptible(startup_wait, atomic_read(&startup_count) == -1)) {
  512. pr_err(" thread[%d]: interrupted\n", tdata->id);
  513. goto out;
  514. }
  515. for (i = 0; i < tdata->entries; i++) {
  516. tdata->objs[i].value.id = i;
  517. tdata->objs[i].value.tid = tdata->id;
  518. err = insert_retry(&ht, &tdata->objs[i], test_rht_params);
  519. if (err > 0) {
  520. insert_retries += err;
  521. } else if (err) {
  522. pr_err(" thread[%d]: rhashtable_insert_fast failed\n",
  523. tdata->id);
  524. goto out;
  525. }
  526. }
  527. if (insert_retries)
  528. pr_info(" thread[%d]: %u insertions retried due to memory pressure\n",
  529. tdata->id, insert_retries);
  530. err = thread_lookup_test(tdata);
  531. if (err) {
  532. pr_err(" thread[%d]: rhashtable_lookup_test failed\n",
  533. tdata->id);
  534. goto out;
  535. }
  536. for (step = 10; step > 0; step--) {
  537. for (i = 0; i < tdata->entries; i += step) {
  538. if (tdata->objs[i].value.id == TEST_INSERT_FAIL)
  539. continue;
  540. err = rhashtable_remove_fast(&ht, &tdata->objs[i].node,
  541. test_rht_params);
  542. if (err) {
  543. pr_err(" thread[%d]: rhashtable_remove_fast failed\n",
  544. tdata->id);
  545. goto out;
  546. }
  547. tdata->objs[i].value.id = TEST_INSERT_FAIL;
  548. cond_resched();
  549. }
  550. err = thread_lookup_test(tdata);
  551. if (err) {
  552. pr_err(" thread[%d]: rhashtable_lookup_test (2) failed\n",
  553. tdata->id);
  554. goto out;
  555. }
  556. }
  557. out:
  558. while (!kthread_should_stop()) {
  559. set_current_state(TASK_INTERRUPTIBLE);
  560. schedule();
  561. }
  562. return err;
  563. }
  564. static int __init test_rht_init(void)
  565. {
  566. unsigned int entries;
  567. int i, err, started_threads = 0, failed_threads = 0;
  568. u64 total_time = 0;
  569. struct thread_data *tdata;
  570. struct test_obj *objs;
  571. if (parm_entries < 0)
  572. parm_entries = 1;
  573. entries = min(parm_entries, MAX_ENTRIES);
  574. test_rht_params.automatic_shrinking = shrinking;
  575. test_rht_params.max_size = max_size ? : roundup_pow_of_two(entries);
  576. test_rht_params.nelem_hint = size;
  577. objs = vzalloc(array_size(sizeof(struct test_obj),
  578. test_rht_params.max_size + 1));
  579. if (!objs)
  580. return -ENOMEM;
  581. pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
  582. size, max_size, shrinking);
  583. for (i = 0; i < runs; i++) {
  584. s64 time;
  585. pr_info("Test %02d:\n", i);
  586. memset(objs, 0, test_rht_params.max_size * sizeof(struct test_obj));
  587. err = rhashtable_init(&ht, &test_rht_params);
  588. if (err < 0) {
  589. pr_warn("Test failed: Unable to initialize hashtable: %d\n",
  590. err);
  591. continue;
  592. }
  593. time = test_rhashtable(&ht, objs, entries);
  594. rhashtable_destroy(&ht);
  595. if (time < 0) {
  596. vfree(objs);
  597. pr_warn("Test failed: return code %lld\n", time);
  598. return -EINVAL;
  599. }
  600. total_time += time;
  601. }
  602. pr_info("test if its possible to exceed max_size %d: %s\n",
  603. test_rht_params.max_size, test_rhashtable_max(objs, entries) == 0 ?
  604. "no, ok" : "YES, failed");
  605. vfree(objs);
  606. do_div(total_time, runs);
  607. pr_info("Average test time: %llu\n", total_time);
  608. test_insert_duplicates_run();
  609. if (!tcount)
  610. return 0;
  611. pr_info("Testing concurrent rhashtable access from %d threads\n",
  612. tcount);
  613. atomic_set(&startup_count, tcount);
  614. tdata = vzalloc(array_size(tcount, sizeof(struct thread_data)));
  615. if (!tdata)
  616. return -ENOMEM;
  617. objs = vzalloc(array3_size(sizeof(struct test_obj), tcount, entries));
  618. if (!objs) {
  619. vfree(tdata);
  620. return -ENOMEM;
  621. }
  622. test_rht_params.max_size = max_size ? :
  623. roundup_pow_of_two(tcount * entries);
  624. err = rhashtable_init(&ht, &test_rht_params);
  625. if (err < 0) {
  626. pr_warn("Test failed: Unable to initialize hashtable: %d\n",
  627. err);
  628. vfree(tdata);
  629. vfree(objs);
  630. return -EINVAL;
  631. }
  632. for (i = 0; i < tcount; i++) {
  633. tdata[i].id = i;
  634. tdata[i].entries = entries;
  635. tdata[i].objs = objs + i * entries;
  636. tdata[i].task = kthread_run(threadfunc, &tdata[i],
  637. "rhashtable_thrad[%d]", i);
  638. if (IS_ERR(tdata[i].task)) {
  639. pr_err(" kthread_run failed for thread %d\n", i);
  640. atomic_dec(&startup_count);
  641. } else {
  642. started_threads++;
  643. }
  644. }
  645. if (wait_event_interruptible(startup_wait, atomic_read(&startup_count) == 0))
  646. pr_err(" wait_event interruptible failed\n");
  647. /* count is 0 now, set it to -1 and wake up all threads together */
  648. atomic_dec(&startup_count);
  649. wake_up_all(&startup_wait);
  650. for (i = 0; i < tcount; i++) {
  651. if (IS_ERR(tdata[i].task))
  652. continue;
  653. if ((err = kthread_stop(tdata[i].task))) {
  654. pr_warn("Test failed: thread %d returned: %d\n",
  655. i, err);
  656. failed_threads++;
  657. }
  658. }
  659. rhashtable_destroy(&ht);
  660. vfree(tdata);
  661. vfree(objs);
  662. /*
  663. * rhltable_remove is very expensive, default values can cause test
  664. * to run for 2 minutes or more, use a smaller number instead.
  665. */
  666. err = test_rhltable(entries / 16);
  667. pr_info("Started %d threads, %d failed, rhltable test returns %d\n",
  668. started_threads, failed_threads, err);
  669. return 0;
  670. }
  671. static void __exit test_rht_exit(void)
  672. {
  673. }
  674. module_init(test_rht_init);
  675. module_exit(test_rht_exit);
  676. MODULE_LICENSE("GPL v2");