rhashtable.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Resizable, Scalable, Concurrent Hash Table
  3. *
  4. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  5. * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
  6. * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
  7. *
  8. * Code partially derived from nft_hash
  9. * Rewritten with rehash code from br_multicast plus single list
  10. * pointer as suggested by Josh Triplett
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. */
  16. #include <linux/atomic.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/log2.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/mm.h>
  24. #include <linux/jhash.h>
  25. #include <linux/random.h>
  26. #include <linux/rhashtable.h>
  27. #include <linux/err.h>
  28. #include <linux/export.h>
  29. #define HASH_DEFAULT_SIZE 64UL
  30. #define HASH_MIN_SIZE 4U
  31. #define BUCKET_LOCKS_PER_CPU 128UL
  32. static u32 head_hashfn(struct rhashtable *ht,
  33. const struct bucket_table *tbl,
  34. const struct rhash_head *he)
  35. {
  36. return rht_head_hashfn(ht, tbl, he, ht->p);
  37. }
  38. #ifdef CONFIG_PROVE_LOCKING
  39. #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
  40. int lockdep_rht_mutex_is_held(struct rhashtable *ht)
  41. {
  42. return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
  43. }
  44. EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
  45. int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
  46. {
  47. spinlock_t *lock = rht_bucket_lock(tbl, hash);
  48. return (debug_locks) ? lockdep_is_held(lock) : 1;
  49. }
  50. EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
  51. #else
  52. #define ASSERT_RHT_MUTEX(HT)
  53. #endif
  54. static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl,
  55. gfp_t gfp)
  56. {
  57. unsigned int i, size;
  58. #if defined(CONFIG_PROVE_LOCKING)
  59. unsigned int nr_pcpus = 2;
  60. #else
  61. unsigned int nr_pcpus = num_possible_cpus();
  62. #endif
  63. nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
  64. size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
  65. /* Never allocate more than 0.5 locks per bucket */
  66. size = min_t(unsigned int, size, tbl->size >> 1);
  67. if (sizeof(spinlock_t) != 0) {
  68. #ifdef CONFIG_NUMA
  69. if (size * sizeof(spinlock_t) > PAGE_SIZE &&
  70. gfp == GFP_KERNEL)
  71. tbl->locks = vmalloc(size * sizeof(spinlock_t));
  72. else
  73. #endif
  74. tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
  75. gfp);
  76. if (!tbl->locks)
  77. return -ENOMEM;
  78. for (i = 0; i < size; i++)
  79. spin_lock_init(&tbl->locks[i]);
  80. }
  81. tbl->locks_mask = size - 1;
  82. return 0;
  83. }
  84. static void bucket_table_free(const struct bucket_table *tbl)
  85. {
  86. if (tbl)
  87. kvfree(tbl->locks);
  88. kvfree(tbl);
  89. }
  90. static void bucket_table_free_rcu(struct rcu_head *head)
  91. {
  92. bucket_table_free(container_of(head, struct bucket_table, rcu));
  93. }
  94. static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
  95. size_t nbuckets,
  96. gfp_t gfp)
  97. {
  98. struct bucket_table *tbl = NULL;
  99. size_t size;
  100. int i;
  101. size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
  102. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) ||
  103. gfp != GFP_KERNEL)
  104. tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
  105. if (tbl == NULL && gfp == GFP_KERNEL)
  106. tbl = vzalloc(size);
  107. if (tbl == NULL)
  108. return NULL;
  109. tbl->size = nbuckets;
  110. if (alloc_bucket_locks(ht, tbl, gfp) < 0) {
  111. bucket_table_free(tbl);
  112. return NULL;
  113. }
  114. INIT_LIST_HEAD(&tbl->walkers);
  115. get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
  116. for (i = 0; i < nbuckets; i++)
  117. INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
  118. return tbl;
  119. }
  120. static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
  121. struct bucket_table *tbl)
  122. {
  123. struct bucket_table *new_tbl;
  124. do {
  125. new_tbl = tbl;
  126. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  127. } while (tbl);
  128. return new_tbl;
  129. }
  130. static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
  131. {
  132. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  133. struct bucket_table *new_tbl = rhashtable_last_table(ht,
  134. rht_dereference_rcu(old_tbl->future_tbl, ht));
  135. struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
  136. int err = -ENOENT;
  137. struct rhash_head *head, *next, *entry;
  138. spinlock_t *new_bucket_lock;
  139. unsigned int new_hash;
  140. rht_for_each(entry, old_tbl, old_hash) {
  141. err = 0;
  142. next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
  143. if (rht_is_a_nulls(next))
  144. break;
  145. pprev = &entry->next;
  146. }
  147. if (err)
  148. goto out;
  149. new_hash = head_hashfn(ht, new_tbl, entry);
  150. new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
  151. spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
  152. head = rht_dereference_bucket(new_tbl->buckets[new_hash],
  153. new_tbl, new_hash);
  154. RCU_INIT_POINTER(entry->next, head);
  155. rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
  156. spin_unlock(new_bucket_lock);
  157. rcu_assign_pointer(*pprev, next);
  158. out:
  159. return err;
  160. }
  161. static void rhashtable_rehash_chain(struct rhashtable *ht,
  162. unsigned int old_hash)
  163. {
  164. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  165. spinlock_t *old_bucket_lock;
  166. old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
  167. spin_lock_bh(old_bucket_lock);
  168. while (!rhashtable_rehash_one(ht, old_hash))
  169. ;
  170. old_tbl->rehash++;
  171. spin_unlock_bh(old_bucket_lock);
  172. }
  173. static int rhashtable_rehash_attach(struct rhashtable *ht,
  174. struct bucket_table *old_tbl,
  175. struct bucket_table *new_tbl)
  176. {
  177. /* Protect future_tbl using the first bucket lock. */
  178. spin_lock_bh(old_tbl->locks);
  179. /* Did somebody beat us to it? */
  180. if (rcu_access_pointer(old_tbl->future_tbl)) {
  181. spin_unlock_bh(old_tbl->locks);
  182. return -EEXIST;
  183. }
  184. /* Make insertions go into the new, empty table right away. Deletions
  185. * and lookups will be attempted in both tables until we synchronize.
  186. */
  187. rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
  188. /* Ensure the new table is visible to readers. */
  189. smp_wmb();
  190. spin_unlock_bh(old_tbl->locks);
  191. return 0;
  192. }
  193. static int rhashtable_rehash_table(struct rhashtable *ht)
  194. {
  195. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  196. struct bucket_table *new_tbl;
  197. struct rhashtable_walker *walker;
  198. unsigned int old_hash;
  199. new_tbl = rht_dereference(old_tbl->future_tbl, ht);
  200. if (!new_tbl)
  201. return 0;
  202. for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
  203. rhashtable_rehash_chain(ht, old_hash);
  204. cond_resched();
  205. }
  206. /* Publish the new table pointer. */
  207. rcu_assign_pointer(ht->tbl, new_tbl);
  208. spin_lock(&ht->lock);
  209. list_for_each_entry(walker, &old_tbl->walkers, list)
  210. walker->tbl = NULL;
  211. spin_unlock(&ht->lock);
  212. /* Wait for readers. All new readers will see the new
  213. * table, and thus no references to the old table will
  214. * remain.
  215. */
  216. call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
  217. return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
  218. }
  219. /**
  220. * rhashtable_expand - Expand hash table while allowing concurrent lookups
  221. * @ht: the hash table to expand
  222. *
  223. * A secondary bucket array is allocated and the hash entries are migrated.
  224. *
  225. * This function may only be called in a context where it is safe to call
  226. * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
  227. *
  228. * The caller must ensure that no concurrent resizing occurs by holding
  229. * ht->mutex.
  230. *
  231. * It is valid to have concurrent insertions and deletions protected by per
  232. * bucket locks or concurrent RCU protected lookups and traversals.
  233. */
  234. static int rhashtable_expand(struct rhashtable *ht)
  235. {
  236. struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
  237. int err;
  238. ASSERT_RHT_MUTEX(ht);
  239. old_tbl = rhashtable_last_table(ht, old_tbl);
  240. new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, GFP_KERNEL);
  241. if (new_tbl == NULL)
  242. return -ENOMEM;
  243. err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
  244. if (err)
  245. bucket_table_free(new_tbl);
  246. return err;
  247. }
  248. /**
  249. * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
  250. * @ht: the hash table to shrink
  251. *
  252. * This function shrinks the hash table to fit, i.e., the smallest
  253. * size would not cause it to expand right away automatically.
  254. *
  255. * The caller must ensure that no concurrent resizing occurs by holding
  256. * ht->mutex.
  257. *
  258. * The caller must ensure that no concurrent table mutations take place.
  259. * It is however valid to have concurrent lookups if they are RCU protected.
  260. *
  261. * It is valid to have concurrent insertions and deletions protected by per
  262. * bucket locks or concurrent RCU protected lookups and traversals.
  263. */
  264. static int rhashtable_shrink(struct rhashtable *ht)
  265. {
  266. struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
  267. unsigned int size;
  268. int err;
  269. ASSERT_RHT_MUTEX(ht);
  270. size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2);
  271. if (size < ht->p.min_size)
  272. size = ht->p.min_size;
  273. if (old_tbl->size <= size)
  274. return 0;
  275. if (rht_dereference(old_tbl->future_tbl, ht))
  276. return -EEXIST;
  277. new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  278. if (new_tbl == NULL)
  279. return -ENOMEM;
  280. err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
  281. if (err)
  282. bucket_table_free(new_tbl);
  283. return err;
  284. }
  285. static void rht_deferred_worker(struct work_struct *work)
  286. {
  287. struct rhashtable *ht;
  288. struct bucket_table *tbl;
  289. int err = 0;
  290. ht = container_of(work, struct rhashtable, run_work);
  291. mutex_lock(&ht->mutex);
  292. tbl = rht_dereference(ht->tbl, ht);
  293. tbl = rhashtable_last_table(ht, tbl);
  294. if (rht_grow_above_75(ht, tbl))
  295. rhashtable_expand(ht);
  296. else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
  297. rhashtable_shrink(ht);
  298. err = rhashtable_rehash_table(ht);
  299. mutex_unlock(&ht->mutex);
  300. if (err)
  301. schedule_work(&ht->run_work);
  302. }
  303. static bool rhashtable_check_elasticity(struct rhashtable *ht,
  304. struct bucket_table *tbl,
  305. unsigned int hash)
  306. {
  307. unsigned int elasticity = ht->elasticity;
  308. struct rhash_head *head;
  309. rht_for_each(head, tbl, hash)
  310. if (!--elasticity)
  311. return true;
  312. return false;
  313. }
  314. int rhashtable_insert_rehash(struct rhashtable *ht,
  315. struct bucket_table *tbl)
  316. {
  317. struct bucket_table *old_tbl;
  318. struct bucket_table *new_tbl;
  319. unsigned int size;
  320. int err;
  321. old_tbl = rht_dereference_rcu(ht->tbl, ht);
  322. size = tbl->size;
  323. err = -EBUSY;
  324. if (rht_grow_above_75(ht, tbl))
  325. size *= 2;
  326. /* Do not schedule more than one rehash */
  327. else if (old_tbl != tbl)
  328. goto fail;
  329. err = -ENOMEM;
  330. new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
  331. if (new_tbl == NULL)
  332. goto fail;
  333. err = rhashtable_rehash_attach(ht, tbl, new_tbl);
  334. if (err) {
  335. bucket_table_free(new_tbl);
  336. if (err == -EEXIST)
  337. err = 0;
  338. } else
  339. schedule_work(&ht->run_work);
  340. return err;
  341. fail:
  342. /* Do not fail the insert if someone else did a rehash. */
  343. if (likely(rcu_dereference_raw(tbl->future_tbl)))
  344. return 0;
  345. /* Schedule async rehash to retry allocation in process context. */
  346. if (err == -ENOMEM)
  347. schedule_work(&ht->run_work);
  348. return err;
  349. }
  350. EXPORT_SYMBOL_GPL(rhashtable_insert_rehash);
  351. struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht,
  352. const void *key,
  353. struct rhash_head *obj,
  354. struct bucket_table *tbl,
  355. void **data)
  356. {
  357. struct rhash_head *head;
  358. unsigned int hash;
  359. int err;
  360. tbl = rhashtable_last_table(ht, tbl);
  361. hash = head_hashfn(ht, tbl, obj);
  362. spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
  363. err = -EEXIST;
  364. if (key) {
  365. *data = rhashtable_lookup_fast(ht, key, ht->p);
  366. if (*data)
  367. goto exit;
  368. }
  369. err = -E2BIG;
  370. if (unlikely(rht_grow_above_max(ht, tbl)))
  371. goto exit;
  372. err = -EAGAIN;
  373. if (rhashtable_check_elasticity(ht, tbl, hash) ||
  374. rht_grow_above_100(ht, tbl))
  375. goto exit;
  376. err = 0;
  377. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  378. RCU_INIT_POINTER(obj->next, head);
  379. rcu_assign_pointer(tbl->buckets[hash], obj);
  380. atomic_inc(&ht->nelems);
  381. exit:
  382. spin_unlock(rht_bucket_lock(tbl, hash));
  383. if (err == 0)
  384. return NULL;
  385. else if (err == -EAGAIN)
  386. return tbl;
  387. else
  388. return ERR_PTR(err);
  389. }
  390. EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
  391. /**
  392. * rhashtable_walk_init - Initialise an iterator
  393. * @ht: Table to walk over
  394. * @iter: Hash table Iterator
  395. *
  396. * This function prepares a hash table walk.
  397. *
  398. * Note that if you restart a walk after rhashtable_walk_stop you
  399. * may see the same object twice. Also, you may miss objects if
  400. * there are removals in between rhashtable_walk_stop and the next
  401. * call to rhashtable_walk_start.
  402. *
  403. * For a completely stable walk you should construct your own data
  404. * structure outside the hash table.
  405. *
  406. * This function may sleep so you must not call it from interrupt
  407. * context or with spin locks held.
  408. *
  409. * You must call rhashtable_walk_exit if this function returns
  410. * successfully.
  411. */
  412. int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
  413. {
  414. iter->ht = ht;
  415. iter->p = NULL;
  416. iter->slot = 0;
  417. iter->skip = 0;
  418. iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
  419. if (!iter->walker)
  420. return -ENOMEM;
  421. spin_lock(&ht->lock);
  422. iter->walker->tbl =
  423. rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
  424. list_add(&iter->walker->list, &iter->walker->tbl->walkers);
  425. spin_unlock(&ht->lock);
  426. return 0;
  427. }
  428. EXPORT_SYMBOL_GPL(rhashtable_walk_init);
  429. /**
  430. * rhashtable_walk_exit - Free an iterator
  431. * @iter: Hash table Iterator
  432. *
  433. * This function frees resources allocated by rhashtable_walk_init.
  434. */
  435. void rhashtable_walk_exit(struct rhashtable_iter *iter)
  436. {
  437. spin_lock(&iter->ht->lock);
  438. if (iter->walker->tbl)
  439. list_del(&iter->walker->list);
  440. spin_unlock(&iter->ht->lock);
  441. kfree(iter->walker);
  442. }
  443. EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
  444. /**
  445. * rhashtable_walk_start - Start a hash table walk
  446. * @iter: Hash table iterator
  447. *
  448. * Start a hash table walk. Note that we take the RCU lock in all
  449. * cases including when we return an error. So you must always call
  450. * rhashtable_walk_stop to clean up.
  451. *
  452. * Returns zero if successful.
  453. *
  454. * Returns -EAGAIN if resize event occured. Note that the iterator
  455. * will rewind back to the beginning and you may use it immediately
  456. * by calling rhashtable_walk_next.
  457. */
  458. int rhashtable_walk_start(struct rhashtable_iter *iter)
  459. __acquires(RCU)
  460. {
  461. struct rhashtable *ht = iter->ht;
  462. rcu_read_lock();
  463. spin_lock(&ht->lock);
  464. if (iter->walker->tbl)
  465. list_del(&iter->walker->list);
  466. spin_unlock(&ht->lock);
  467. if (!iter->walker->tbl) {
  468. iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
  469. return -EAGAIN;
  470. }
  471. return 0;
  472. }
  473. EXPORT_SYMBOL_GPL(rhashtable_walk_start);
  474. /**
  475. * rhashtable_walk_next - Return the next object and advance the iterator
  476. * @iter: Hash table iterator
  477. *
  478. * Note that you must call rhashtable_walk_stop when you are finished
  479. * with the walk.
  480. *
  481. * Returns the next object or NULL when the end of the table is reached.
  482. *
  483. * Returns -EAGAIN if resize event occured. Note that the iterator
  484. * will rewind back to the beginning and you may continue to use it.
  485. */
  486. void *rhashtable_walk_next(struct rhashtable_iter *iter)
  487. {
  488. struct bucket_table *tbl = iter->walker->tbl;
  489. struct rhashtable *ht = iter->ht;
  490. struct rhash_head *p = iter->p;
  491. if (p) {
  492. p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
  493. goto next;
  494. }
  495. for (; iter->slot < tbl->size; iter->slot++) {
  496. int skip = iter->skip;
  497. rht_for_each_rcu(p, tbl, iter->slot) {
  498. if (!skip)
  499. break;
  500. skip--;
  501. }
  502. next:
  503. if (!rht_is_a_nulls(p)) {
  504. iter->skip++;
  505. iter->p = p;
  506. return rht_obj(ht, p);
  507. }
  508. iter->skip = 0;
  509. }
  510. iter->p = NULL;
  511. /* Ensure we see any new tables. */
  512. smp_rmb();
  513. iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  514. if (iter->walker->tbl) {
  515. iter->slot = 0;
  516. iter->skip = 0;
  517. return ERR_PTR(-EAGAIN);
  518. }
  519. return NULL;
  520. }
  521. EXPORT_SYMBOL_GPL(rhashtable_walk_next);
  522. /**
  523. * rhashtable_walk_stop - Finish a hash table walk
  524. * @iter: Hash table iterator
  525. *
  526. * Finish a hash table walk.
  527. */
  528. void rhashtable_walk_stop(struct rhashtable_iter *iter)
  529. __releases(RCU)
  530. {
  531. struct rhashtable *ht;
  532. struct bucket_table *tbl = iter->walker->tbl;
  533. if (!tbl)
  534. goto out;
  535. ht = iter->ht;
  536. spin_lock(&ht->lock);
  537. if (tbl->rehash < tbl->size)
  538. list_add(&iter->walker->list, &tbl->walkers);
  539. else
  540. iter->walker->tbl = NULL;
  541. spin_unlock(&ht->lock);
  542. iter->p = NULL;
  543. out:
  544. rcu_read_unlock();
  545. }
  546. EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
  547. static size_t rounded_hashtable_size(const struct rhashtable_params *params)
  548. {
  549. size_t retsize;
  550. if (params->nelem_hint)
  551. retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
  552. (unsigned long)params->min_size);
  553. else
  554. retsize = max(HASH_DEFAULT_SIZE,
  555. (unsigned long)params->min_size);
  556. return retsize;
  557. }
  558. static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
  559. {
  560. return jhash2(key, length, seed);
  561. }
  562. /**
  563. * rhashtable_init - initialize a new hash table
  564. * @ht: hash table to be initialized
  565. * @params: configuration parameters
  566. *
  567. * Initializes a new hash table based on the provided configuration
  568. * parameters. A table can be configured either with a variable or
  569. * fixed length key:
  570. *
  571. * Configuration Example 1: Fixed length keys
  572. * struct test_obj {
  573. * int key;
  574. * void * my_member;
  575. * struct rhash_head node;
  576. * };
  577. *
  578. * struct rhashtable_params params = {
  579. * .head_offset = offsetof(struct test_obj, node),
  580. * .key_offset = offsetof(struct test_obj, key),
  581. * .key_len = sizeof(int),
  582. * .hashfn = jhash,
  583. * .nulls_base = (1U << RHT_BASE_SHIFT),
  584. * };
  585. *
  586. * Configuration Example 2: Variable length keys
  587. * struct test_obj {
  588. * [...]
  589. * struct rhash_head node;
  590. * };
  591. *
  592. * u32 my_hash_fn(const void *data, u32 len, u32 seed)
  593. * {
  594. * struct test_obj *obj = data;
  595. *
  596. * return [... hash ...];
  597. * }
  598. *
  599. * struct rhashtable_params params = {
  600. * .head_offset = offsetof(struct test_obj, node),
  601. * .hashfn = jhash,
  602. * .obj_hashfn = my_hash_fn,
  603. * };
  604. */
  605. int rhashtable_init(struct rhashtable *ht,
  606. const struct rhashtable_params *params)
  607. {
  608. struct bucket_table *tbl;
  609. size_t size;
  610. if ((!params->key_len && !params->obj_hashfn) ||
  611. (params->obj_hashfn && !params->obj_cmpfn))
  612. return -EINVAL;
  613. if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
  614. return -EINVAL;
  615. memset(ht, 0, sizeof(*ht));
  616. mutex_init(&ht->mutex);
  617. spin_lock_init(&ht->lock);
  618. memcpy(&ht->p, params, sizeof(*params));
  619. if (params->min_size)
  620. ht->p.min_size = roundup_pow_of_two(params->min_size);
  621. if (params->max_size)
  622. ht->p.max_size = rounddown_pow_of_two(params->max_size);
  623. if (params->insecure_max_entries)
  624. ht->p.insecure_max_entries =
  625. rounddown_pow_of_two(params->insecure_max_entries);
  626. else
  627. ht->p.insecure_max_entries = ht->p.max_size * 2;
  628. ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
  629. size = rounded_hashtable_size(&ht->p);
  630. /* The maximum (not average) chain length grows with the
  631. * size of the hash table, at a rate of (log N)/(log log N).
  632. * The value of 16 is selected so that even if the hash
  633. * table grew to 2^32 you would not expect the maximum
  634. * chain length to exceed it unless we are under attack
  635. * (or extremely unlucky).
  636. *
  637. * As this limit is only to detect attacks, we don't need
  638. * to set it to a lower value as you'd need the chain
  639. * length to vastly exceed 16 to have any real effect
  640. * on the system.
  641. */
  642. if (!params->insecure_elasticity)
  643. ht->elasticity = 16;
  644. if (params->locks_mul)
  645. ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
  646. else
  647. ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
  648. ht->key_len = ht->p.key_len;
  649. if (!params->hashfn) {
  650. ht->p.hashfn = jhash;
  651. if (!(ht->key_len & (sizeof(u32) - 1))) {
  652. ht->key_len /= sizeof(u32);
  653. ht->p.hashfn = rhashtable_jhash2;
  654. }
  655. }
  656. tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  657. if (tbl == NULL)
  658. return -ENOMEM;
  659. atomic_set(&ht->nelems, 0);
  660. RCU_INIT_POINTER(ht->tbl, tbl);
  661. INIT_WORK(&ht->run_work, rht_deferred_worker);
  662. return 0;
  663. }
  664. EXPORT_SYMBOL_GPL(rhashtable_init);
  665. /**
  666. * rhashtable_free_and_destroy - free elements and destroy hash table
  667. * @ht: the hash table to destroy
  668. * @free_fn: callback to release resources of element
  669. * @arg: pointer passed to free_fn
  670. *
  671. * Stops an eventual async resize. If defined, invokes free_fn for each
  672. * element to releasal resources. Please note that RCU protected
  673. * readers may still be accessing the elements. Releasing of resources
  674. * must occur in a compatible manner. Then frees the bucket array.
  675. *
  676. * This function will eventually sleep to wait for an async resize
  677. * to complete. The caller is responsible that no further write operations
  678. * occurs in parallel.
  679. */
  680. void rhashtable_free_and_destroy(struct rhashtable *ht,
  681. void (*free_fn)(void *ptr, void *arg),
  682. void *arg)
  683. {
  684. const struct bucket_table *tbl;
  685. unsigned int i;
  686. cancel_work_sync(&ht->run_work);
  687. mutex_lock(&ht->mutex);
  688. tbl = rht_dereference(ht->tbl, ht);
  689. if (free_fn) {
  690. for (i = 0; i < tbl->size; i++) {
  691. struct rhash_head *pos, *next;
  692. cond_resched();
  693. for (pos = rht_dereference(tbl->buckets[i], ht),
  694. next = !rht_is_a_nulls(pos) ?
  695. rht_dereference(pos->next, ht) : NULL;
  696. !rht_is_a_nulls(pos);
  697. pos = next,
  698. next = !rht_is_a_nulls(pos) ?
  699. rht_dereference(pos->next, ht) : NULL)
  700. free_fn(rht_obj(ht, pos), arg);
  701. }
  702. }
  703. bucket_table_free(tbl);
  704. mutex_unlock(&ht->mutex);
  705. }
  706. EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
  707. void rhashtable_destroy(struct rhashtable *ht)
  708. {
  709. return rhashtable_free_and_destroy(ht, NULL, NULL);
  710. }
  711. EXPORT_SYMBOL_GPL(rhashtable_destroy);