rhashtable.c 21 KB

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