rhashtable.c 20 KB

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