rhashtable.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. /* Publish the new table pointer. */
  205. rcu_assign_pointer(ht->tbl, new_tbl);
  206. spin_lock(&ht->lock);
  207. list_for_each_entry(walker, &old_tbl->walkers, list)
  208. walker->tbl = NULL;
  209. spin_unlock(&ht->lock);
  210. /* Wait for readers. All new readers will see the new
  211. * table, and thus no references to the old table will
  212. * remain.
  213. */
  214. call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
  215. return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
  216. }
  217. /**
  218. * rhashtable_expand - Expand hash table while allowing concurrent lookups
  219. * @ht: the hash table to expand
  220. *
  221. * A secondary bucket array is allocated and the hash entries are migrated.
  222. *
  223. * This function may only be called in a context where it is safe to call
  224. * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
  225. *
  226. * The caller must ensure that no concurrent resizing occurs by holding
  227. * ht->mutex.
  228. *
  229. * It is valid to have concurrent insertions and deletions protected by per
  230. * bucket locks or concurrent RCU protected lookups and traversals.
  231. */
  232. static int rhashtable_expand(struct rhashtable *ht)
  233. {
  234. struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
  235. int err;
  236. ASSERT_RHT_MUTEX(ht);
  237. old_tbl = rhashtable_last_table(ht, old_tbl);
  238. new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, GFP_KERNEL);
  239. if (new_tbl == NULL)
  240. return -ENOMEM;
  241. err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
  242. if (err)
  243. bucket_table_free(new_tbl);
  244. return err;
  245. }
  246. /**
  247. * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
  248. * @ht: the hash table to shrink
  249. *
  250. * This function shrinks the hash table to fit, i.e., the smallest
  251. * size would not cause it to expand right away automatically.
  252. *
  253. * The caller must ensure that no concurrent resizing occurs by holding
  254. * ht->mutex.
  255. *
  256. * The caller must ensure that no concurrent table mutations take place.
  257. * It is however valid to have concurrent lookups if they are RCU protected.
  258. *
  259. * It is valid to have concurrent insertions and deletions protected by per
  260. * bucket locks or concurrent RCU protected lookups and traversals.
  261. */
  262. static int rhashtable_shrink(struct rhashtable *ht)
  263. {
  264. struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
  265. unsigned int size;
  266. int err;
  267. ASSERT_RHT_MUTEX(ht);
  268. size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2);
  269. if (size < ht->p.min_size)
  270. size = ht->p.min_size;
  271. if (old_tbl->size <= size)
  272. return 0;
  273. if (rht_dereference(old_tbl->future_tbl, ht))
  274. return -EEXIST;
  275. new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  276. if (new_tbl == NULL)
  277. return -ENOMEM;
  278. err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
  279. if (err)
  280. bucket_table_free(new_tbl);
  281. return err;
  282. }
  283. static void rht_deferred_worker(struct work_struct *work)
  284. {
  285. struct rhashtable *ht;
  286. struct bucket_table *tbl;
  287. int err = 0;
  288. ht = container_of(work, struct rhashtable, run_work);
  289. mutex_lock(&ht->mutex);
  290. tbl = rht_dereference(ht->tbl, ht);
  291. tbl = rhashtable_last_table(ht, tbl);
  292. if (rht_grow_above_75(ht, tbl))
  293. rhashtable_expand(ht);
  294. else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
  295. rhashtable_shrink(ht);
  296. err = rhashtable_rehash_table(ht);
  297. mutex_unlock(&ht->mutex);
  298. if (err)
  299. schedule_work(&ht->run_work);
  300. }
  301. static bool rhashtable_check_elasticity(struct rhashtable *ht,
  302. struct bucket_table *tbl,
  303. unsigned int hash)
  304. {
  305. unsigned int elasticity = ht->elasticity;
  306. struct rhash_head *head;
  307. rht_for_each(head, tbl, hash)
  308. if (!--elasticity)
  309. return true;
  310. return false;
  311. }
  312. int rhashtable_insert_rehash(struct rhashtable *ht)
  313. {
  314. struct bucket_table *old_tbl;
  315. struct bucket_table *new_tbl;
  316. struct bucket_table *tbl;
  317. unsigned int size;
  318. int err;
  319. old_tbl = rht_dereference_rcu(ht->tbl, ht);
  320. tbl = rhashtable_last_table(ht, old_tbl);
  321. size = tbl->size;
  322. if (rht_grow_above_75(ht, tbl))
  323. size *= 2;
  324. /* Do not schedule more than one rehash */
  325. else if (old_tbl != tbl)
  326. return -EBUSY;
  327. new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
  328. if (new_tbl == NULL) {
  329. /* Schedule async resize/rehash to try allocation
  330. * non-atomic context.
  331. */
  332. schedule_work(&ht->run_work);
  333. return -ENOMEM;
  334. }
  335. err = rhashtable_rehash_attach(ht, tbl, new_tbl);
  336. if (err) {
  337. bucket_table_free(new_tbl);
  338. if (err == -EEXIST)
  339. err = 0;
  340. } else
  341. schedule_work(&ht->run_work);
  342. return err;
  343. }
  344. EXPORT_SYMBOL_GPL(rhashtable_insert_rehash);
  345. int rhashtable_insert_slow(struct rhashtable *ht, const void *key,
  346. struct rhash_head *obj,
  347. struct bucket_table *tbl)
  348. {
  349. struct rhash_head *head;
  350. unsigned int hash;
  351. int err;
  352. tbl = rhashtable_last_table(ht, tbl);
  353. hash = head_hashfn(ht, tbl, obj);
  354. spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
  355. err = -EEXIST;
  356. if (key && rhashtable_lookup_fast(ht, key, ht->p))
  357. goto exit;
  358. err = -E2BIG;
  359. if (unlikely(rht_grow_above_max(ht, tbl)))
  360. goto exit;
  361. err = -EAGAIN;
  362. if (rhashtable_check_elasticity(ht, tbl, hash) ||
  363. rht_grow_above_100(ht, tbl))
  364. goto exit;
  365. err = 0;
  366. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  367. RCU_INIT_POINTER(obj->next, head);
  368. rcu_assign_pointer(tbl->buckets[hash], obj);
  369. atomic_inc(&ht->nelems);
  370. exit:
  371. spin_unlock(rht_bucket_lock(tbl, hash));
  372. return err;
  373. }
  374. EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
  375. /**
  376. * rhashtable_walk_init - Initialise an iterator
  377. * @ht: Table to walk over
  378. * @iter: Hash table Iterator
  379. *
  380. * This function prepares a hash table walk.
  381. *
  382. * Note that if you restart a walk after rhashtable_walk_stop you
  383. * may see the same object twice. Also, you may miss objects if
  384. * there are removals in between rhashtable_walk_stop and the next
  385. * call to rhashtable_walk_start.
  386. *
  387. * For a completely stable walk you should construct your own data
  388. * structure outside the hash table.
  389. *
  390. * This function may sleep so you must not call it from interrupt
  391. * context or with spin locks held.
  392. *
  393. * You must call rhashtable_walk_exit if this function returns
  394. * successfully.
  395. */
  396. int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
  397. {
  398. iter->ht = ht;
  399. iter->p = NULL;
  400. iter->slot = 0;
  401. iter->skip = 0;
  402. iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
  403. if (!iter->walker)
  404. return -ENOMEM;
  405. mutex_lock(&ht->mutex);
  406. iter->walker->tbl = rht_dereference(ht->tbl, ht);
  407. list_add(&iter->walker->list, &iter->walker->tbl->walkers);
  408. mutex_unlock(&ht->mutex);
  409. return 0;
  410. }
  411. EXPORT_SYMBOL_GPL(rhashtable_walk_init);
  412. /**
  413. * rhashtable_walk_exit - Free an iterator
  414. * @iter: Hash table Iterator
  415. *
  416. * This function frees resources allocated by rhashtable_walk_init.
  417. */
  418. void rhashtable_walk_exit(struct rhashtable_iter *iter)
  419. {
  420. mutex_lock(&iter->ht->mutex);
  421. if (iter->walker->tbl)
  422. list_del(&iter->walker->list);
  423. mutex_unlock(&iter->ht->mutex);
  424. kfree(iter->walker);
  425. }
  426. EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
  427. /**
  428. * rhashtable_walk_start - Start a hash table walk
  429. * @iter: Hash table iterator
  430. *
  431. * Start a hash table walk. Note that we take the RCU lock in all
  432. * cases including when we return an error. So you must always call
  433. * rhashtable_walk_stop to clean up.
  434. *
  435. * Returns zero if successful.
  436. *
  437. * Returns -EAGAIN if resize event occured. Note that the iterator
  438. * will rewind back to the beginning and you may use it immediately
  439. * by calling rhashtable_walk_next.
  440. */
  441. int rhashtable_walk_start(struct rhashtable_iter *iter)
  442. __acquires(RCU)
  443. {
  444. struct rhashtable *ht = iter->ht;
  445. mutex_lock(&ht->mutex);
  446. if (iter->walker->tbl)
  447. list_del(&iter->walker->list);
  448. rcu_read_lock();
  449. mutex_unlock(&ht->mutex);
  450. if (!iter->walker->tbl) {
  451. iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
  452. return -EAGAIN;
  453. }
  454. return 0;
  455. }
  456. EXPORT_SYMBOL_GPL(rhashtable_walk_start);
  457. /**
  458. * rhashtable_walk_next - Return the next object and advance the iterator
  459. * @iter: Hash table iterator
  460. *
  461. * Note that you must call rhashtable_walk_stop when you are finished
  462. * with the walk.
  463. *
  464. * Returns the next object or NULL when the end of the table is reached.
  465. *
  466. * Returns -EAGAIN if resize event occured. Note that the iterator
  467. * will rewind back to the beginning and you may continue to use it.
  468. */
  469. void *rhashtable_walk_next(struct rhashtable_iter *iter)
  470. {
  471. struct bucket_table *tbl = iter->walker->tbl;
  472. struct rhashtable *ht = iter->ht;
  473. struct rhash_head *p = iter->p;
  474. if (p) {
  475. p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
  476. goto next;
  477. }
  478. for (; iter->slot < tbl->size; iter->slot++) {
  479. int skip = iter->skip;
  480. rht_for_each_rcu(p, tbl, iter->slot) {
  481. if (!skip)
  482. break;
  483. skip--;
  484. }
  485. next:
  486. if (!rht_is_a_nulls(p)) {
  487. iter->skip++;
  488. iter->p = p;
  489. return rht_obj(ht, p);
  490. }
  491. iter->skip = 0;
  492. }
  493. iter->p = NULL;
  494. /* Ensure we see any new tables. */
  495. smp_rmb();
  496. iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  497. if (iter->walker->tbl) {
  498. iter->slot = 0;
  499. iter->skip = 0;
  500. return ERR_PTR(-EAGAIN);
  501. }
  502. return NULL;
  503. }
  504. EXPORT_SYMBOL_GPL(rhashtable_walk_next);
  505. /**
  506. * rhashtable_walk_stop - Finish a hash table walk
  507. * @iter: Hash table iterator
  508. *
  509. * Finish a hash table walk.
  510. */
  511. void rhashtable_walk_stop(struct rhashtable_iter *iter)
  512. __releases(RCU)
  513. {
  514. struct rhashtable *ht;
  515. struct bucket_table *tbl = iter->walker->tbl;
  516. if (!tbl)
  517. goto out;
  518. ht = iter->ht;
  519. spin_lock(&ht->lock);
  520. if (tbl->rehash < tbl->size)
  521. list_add(&iter->walker->list, &tbl->walkers);
  522. else
  523. iter->walker->tbl = NULL;
  524. spin_unlock(&ht->lock);
  525. iter->p = NULL;
  526. out:
  527. rcu_read_unlock();
  528. }
  529. EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
  530. static size_t rounded_hashtable_size(const struct rhashtable_params *params)
  531. {
  532. return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
  533. (unsigned long)params->min_size);
  534. }
  535. static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
  536. {
  537. return jhash2(key, length, seed);
  538. }
  539. /**
  540. * rhashtable_init - initialize a new hash table
  541. * @ht: hash table to be initialized
  542. * @params: configuration parameters
  543. *
  544. * Initializes a new hash table based on the provided configuration
  545. * parameters. A table can be configured either with a variable or
  546. * fixed length key:
  547. *
  548. * Configuration Example 1: Fixed length keys
  549. * struct test_obj {
  550. * int key;
  551. * void * my_member;
  552. * struct rhash_head node;
  553. * };
  554. *
  555. * struct rhashtable_params params = {
  556. * .head_offset = offsetof(struct test_obj, node),
  557. * .key_offset = offsetof(struct test_obj, key),
  558. * .key_len = sizeof(int),
  559. * .hashfn = jhash,
  560. * .nulls_base = (1U << RHT_BASE_SHIFT),
  561. * };
  562. *
  563. * Configuration Example 2: Variable length keys
  564. * struct test_obj {
  565. * [...]
  566. * struct rhash_head node;
  567. * };
  568. *
  569. * u32 my_hash_fn(const void *data, u32 len, u32 seed)
  570. * {
  571. * struct test_obj *obj = data;
  572. *
  573. * return [... hash ...];
  574. * }
  575. *
  576. * struct rhashtable_params params = {
  577. * .head_offset = offsetof(struct test_obj, node),
  578. * .hashfn = jhash,
  579. * .obj_hashfn = my_hash_fn,
  580. * };
  581. */
  582. int rhashtable_init(struct rhashtable *ht,
  583. const struct rhashtable_params *params)
  584. {
  585. struct bucket_table *tbl;
  586. size_t size;
  587. size = HASH_DEFAULT_SIZE;
  588. if ((!params->key_len && !params->obj_hashfn) ||
  589. (params->obj_hashfn && !params->obj_cmpfn))
  590. return -EINVAL;
  591. if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
  592. return -EINVAL;
  593. if (params->nelem_hint)
  594. size = rounded_hashtable_size(params);
  595. memset(ht, 0, sizeof(*ht));
  596. mutex_init(&ht->mutex);
  597. spin_lock_init(&ht->lock);
  598. memcpy(&ht->p, params, sizeof(*params));
  599. if (params->min_size)
  600. ht->p.min_size = roundup_pow_of_two(params->min_size);
  601. if (params->max_size)
  602. ht->p.max_size = rounddown_pow_of_two(params->max_size);
  603. if (params->insecure_max_entries)
  604. ht->p.insecure_max_entries =
  605. rounddown_pow_of_two(params->insecure_max_entries);
  606. else
  607. ht->p.insecure_max_entries = ht->p.max_size * 2;
  608. ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
  609. /* The maximum (not average) chain length grows with the
  610. * size of the hash table, at a rate of (log N)/(log log N).
  611. * The value of 16 is selected so that even if the hash
  612. * table grew to 2^32 you would not expect the maximum
  613. * chain length to exceed it unless we are under attack
  614. * (or extremely unlucky).
  615. *
  616. * As this limit is only to detect attacks, we don't need
  617. * to set it to a lower value as you'd need the chain
  618. * length to vastly exceed 16 to have any real effect
  619. * on the system.
  620. */
  621. if (!params->insecure_elasticity)
  622. ht->elasticity = 16;
  623. if (params->locks_mul)
  624. ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
  625. else
  626. ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
  627. ht->key_len = ht->p.key_len;
  628. if (!params->hashfn) {
  629. ht->p.hashfn = jhash;
  630. if (!(ht->key_len & (sizeof(u32) - 1))) {
  631. ht->key_len /= sizeof(u32);
  632. ht->p.hashfn = rhashtable_jhash2;
  633. }
  634. }
  635. tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  636. if (tbl == NULL)
  637. return -ENOMEM;
  638. atomic_set(&ht->nelems, 0);
  639. RCU_INIT_POINTER(ht->tbl, tbl);
  640. INIT_WORK(&ht->run_work, rht_deferred_worker);
  641. return 0;
  642. }
  643. EXPORT_SYMBOL_GPL(rhashtable_init);
  644. /**
  645. * rhashtable_free_and_destroy - free elements and destroy hash table
  646. * @ht: the hash table to destroy
  647. * @free_fn: callback to release resources of element
  648. * @arg: pointer passed to free_fn
  649. *
  650. * Stops an eventual async resize. If defined, invokes free_fn for each
  651. * element to releasal resources. Please note that RCU protected
  652. * readers may still be accessing the elements. Releasing of resources
  653. * must occur in a compatible manner. Then frees the bucket array.
  654. *
  655. * This function will eventually sleep to wait for an async resize
  656. * to complete. The caller is responsible that no further write operations
  657. * occurs in parallel.
  658. */
  659. void rhashtable_free_and_destroy(struct rhashtable *ht,
  660. void (*free_fn)(void *ptr, void *arg),
  661. void *arg)
  662. {
  663. const struct bucket_table *tbl;
  664. unsigned int i;
  665. cancel_work_sync(&ht->run_work);
  666. mutex_lock(&ht->mutex);
  667. tbl = rht_dereference(ht->tbl, ht);
  668. if (free_fn) {
  669. for (i = 0; i < tbl->size; i++) {
  670. struct rhash_head *pos, *next;
  671. for (pos = rht_dereference(tbl->buckets[i], ht),
  672. next = !rht_is_a_nulls(pos) ?
  673. rht_dereference(pos->next, ht) : NULL;
  674. !rht_is_a_nulls(pos);
  675. pos = next,
  676. next = !rht_is_a_nulls(pos) ?
  677. rht_dereference(pos->next, ht) : NULL)
  678. free_fn(rht_obj(ht, pos), arg);
  679. }
  680. }
  681. bucket_table_free(tbl);
  682. mutex_unlock(&ht->mutex);
  683. }
  684. EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
  685. void rhashtable_destroy(struct rhashtable *ht)
  686. {
  687. return rhashtable_free_and_destroy(ht, NULL, NULL);
  688. }
  689. EXPORT_SYMBOL_GPL(rhashtable_destroy);