test_rhashtable.c 20 KB

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