rhashtable.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 32UL
  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, 64UL);
  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. tbl->locks = NULL;
  69. #ifdef CONFIG_NUMA
  70. if (size * sizeof(spinlock_t) > PAGE_SIZE &&
  71. gfp == GFP_KERNEL)
  72. tbl->locks = vmalloc(size * sizeof(spinlock_t));
  73. #endif
  74. if (gfp != GFP_KERNEL)
  75. gfp |= __GFP_NOWARN | __GFP_NORETRY;
  76. if (!tbl->locks)
  77. tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
  78. gfp);
  79. if (!tbl->locks)
  80. return -ENOMEM;
  81. for (i = 0; i < size; i++)
  82. spin_lock_init(&tbl->locks[i]);
  83. }
  84. tbl->locks_mask = size - 1;
  85. return 0;
  86. }
  87. static void bucket_table_free(const struct bucket_table *tbl)
  88. {
  89. if (tbl)
  90. kvfree(tbl->locks);
  91. kvfree(tbl);
  92. }
  93. static void bucket_table_free_rcu(struct rcu_head *head)
  94. {
  95. bucket_table_free(container_of(head, struct bucket_table, rcu));
  96. }
  97. static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
  98. size_t nbuckets,
  99. gfp_t gfp)
  100. {
  101. struct bucket_table *tbl = NULL;
  102. size_t size;
  103. int i;
  104. size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
  105. if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER) ||
  106. gfp != GFP_KERNEL)
  107. tbl = kzalloc(size, gfp | __GFP_NOWARN | __GFP_NORETRY);
  108. if (tbl == NULL && gfp == GFP_KERNEL)
  109. tbl = vzalloc(size);
  110. if (tbl == NULL)
  111. return NULL;
  112. tbl->size = nbuckets;
  113. if (alloc_bucket_locks(ht, tbl, gfp) < 0) {
  114. bucket_table_free(tbl);
  115. return NULL;
  116. }
  117. INIT_LIST_HEAD(&tbl->walkers);
  118. get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
  119. for (i = 0; i < nbuckets; i++)
  120. INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
  121. return tbl;
  122. }
  123. static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
  124. struct bucket_table *tbl)
  125. {
  126. struct bucket_table *new_tbl;
  127. do {
  128. new_tbl = tbl;
  129. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  130. } while (tbl);
  131. return new_tbl;
  132. }
  133. static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash)
  134. {
  135. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  136. struct bucket_table *new_tbl = rhashtable_last_table(ht,
  137. rht_dereference_rcu(old_tbl->future_tbl, ht));
  138. struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
  139. int err = -ENOENT;
  140. struct rhash_head *head, *next, *entry;
  141. spinlock_t *new_bucket_lock;
  142. unsigned int new_hash;
  143. rht_for_each(entry, old_tbl, old_hash) {
  144. err = 0;
  145. next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
  146. if (rht_is_a_nulls(next))
  147. break;
  148. pprev = &entry->next;
  149. }
  150. if (err)
  151. goto out;
  152. new_hash = head_hashfn(ht, new_tbl, entry);
  153. new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
  154. spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
  155. head = rht_dereference_bucket(new_tbl->buckets[new_hash],
  156. new_tbl, new_hash);
  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. spin_unlock_bh(old_tbl->locks);
  192. return 0;
  193. }
  194. static int rhashtable_rehash_table(struct rhashtable *ht)
  195. {
  196. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  197. struct bucket_table *new_tbl;
  198. struct rhashtable_walker *walker;
  199. unsigned int old_hash;
  200. new_tbl = rht_dereference(old_tbl->future_tbl, ht);
  201. if (!new_tbl)
  202. return 0;
  203. for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
  204. rhashtable_rehash_chain(ht, old_hash);
  205. /* Publish the new table pointer. */
  206. rcu_assign_pointer(ht->tbl, new_tbl);
  207. spin_lock(&ht->lock);
  208. list_for_each_entry(walker, &old_tbl->walkers, list)
  209. walker->tbl = NULL;
  210. spin_unlock(&ht->lock);
  211. /* Wait for readers. All new readers will see the new
  212. * table, and thus no references to the old table will
  213. * remain.
  214. */
  215. call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
  216. return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
  217. }
  218. /**
  219. * rhashtable_expand - Expand hash table while allowing concurrent lookups
  220. * @ht: the hash table to expand
  221. *
  222. * A secondary bucket array is allocated and the hash entries are migrated.
  223. *
  224. * This function may only be called in a context where it is safe to call
  225. * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
  226. *
  227. * The caller must ensure that no concurrent resizing occurs by holding
  228. * ht->mutex.
  229. *
  230. * It is valid to have concurrent insertions and deletions protected by per
  231. * bucket locks or concurrent RCU protected lookups and traversals.
  232. */
  233. static int rhashtable_expand(struct rhashtable *ht)
  234. {
  235. struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
  236. int err;
  237. ASSERT_RHT_MUTEX(ht);
  238. old_tbl = rhashtable_last_table(ht, old_tbl);
  239. new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, GFP_KERNEL);
  240. if (new_tbl == NULL)
  241. return -ENOMEM;
  242. err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
  243. if (err)
  244. bucket_table_free(new_tbl);
  245. return err;
  246. }
  247. /**
  248. * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
  249. * @ht: the hash table to shrink
  250. *
  251. * This function shrinks the hash table to fit, i.e., the smallest
  252. * size would not cause it to expand right away automatically.
  253. *
  254. * The caller must ensure that no concurrent resizing occurs by holding
  255. * ht->mutex.
  256. *
  257. * The caller must ensure that no concurrent table mutations take place.
  258. * It is however valid to have concurrent lookups if they are RCU protected.
  259. *
  260. * It is valid to have concurrent insertions and deletions protected by per
  261. * bucket locks or concurrent RCU protected lookups and traversals.
  262. */
  263. static int rhashtable_shrink(struct rhashtable *ht)
  264. {
  265. struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
  266. unsigned int nelems = atomic_read(&ht->nelems);
  267. unsigned int size = 0;
  268. int err;
  269. ASSERT_RHT_MUTEX(ht);
  270. if (nelems)
  271. size = roundup_pow_of_two(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 int rhashtable_insert_rehash(struct rhashtable *ht,
  305. struct bucket_table *tbl)
  306. {
  307. struct bucket_table *old_tbl;
  308. struct bucket_table *new_tbl;
  309. unsigned int size;
  310. int err;
  311. old_tbl = rht_dereference_rcu(ht->tbl, ht);
  312. size = tbl->size;
  313. err = -EBUSY;
  314. if (rht_grow_above_75(ht, tbl))
  315. size *= 2;
  316. /* Do not schedule more than one rehash */
  317. else if (old_tbl != tbl)
  318. goto fail;
  319. err = -ENOMEM;
  320. new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC);
  321. if (new_tbl == NULL)
  322. goto fail;
  323. err = rhashtable_rehash_attach(ht, tbl, new_tbl);
  324. if (err) {
  325. bucket_table_free(new_tbl);
  326. if (err == -EEXIST)
  327. err = 0;
  328. } else
  329. schedule_work(&ht->run_work);
  330. return err;
  331. fail:
  332. /* Do not fail the insert if someone else did a rehash. */
  333. if (likely(rcu_dereference_raw(tbl->future_tbl)))
  334. return 0;
  335. /* Schedule async rehash to retry allocation in process context. */
  336. if (err == -ENOMEM)
  337. schedule_work(&ht->run_work);
  338. return err;
  339. }
  340. static void *rhashtable_lookup_one(struct rhashtable *ht,
  341. struct bucket_table *tbl, unsigned int hash,
  342. const void *key, struct rhash_head *obj)
  343. {
  344. struct rhashtable_compare_arg arg = {
  345. .ht = ht,
  346. .key = key,
  347. };
  348. struct rhash_head __rcu **pprev;
  349. struct rhash_head *head;
  350. int elasticity;
  351. elasticity = ht->elasticity;
  352. pprev = &tbl->buckets[hash];
  353. rht_for_each(head, tbl, hash) {
  354. struct rhlist_head *list;
  355. struct rhlist_head *plist;
  356. elasticity--;
  357. if (!key ||
  358. (ht->p.obj_cmpfn ?
  359. ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
  360. rhashtable_compare(&arg, rht_obj(ht, head))))
  361. continue;
  362. if (!ht->rhlist)
  363. return rht_obj(ht, head);
  364. list = container_of(obj, struct rhlist_head, rhead);
  365. plist = container_of(head, struct rhlist_head, rhead);
  366. RCU_INIT_POINTER(list->next, plist);
  367. head = rht_dereference_bucket(head->next, tbl, hash);
  368. RCU_INIT_POINTER(list->rhead.next, head);
  369. rcu_assign_pointer(*pprev, obj);
  370. return NULL;
  371. }
  372. if (elasticity <= 0)
  373. return ERR_PTR(-EAGAIN);
  374. return ERR_PTR(-ENOENT);
  375. }
  376. static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
  377. struct bucket_table *tbl,
  378. unsigned int hash,
  379. struct rhash_head *obj,
  380. void *data)
  381. {
  382. struct bucket_table *new_tbl;
  383. struct rhash_head *head;
  384. if (!IS_ERR_OR_NULL(data))
  385. return ERR_PTR(-EEXIST);
  386. if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
  387. return ERR_CAST(data);
  388. new_tbl = rcu_dereference(tbl->future_tbl);
  389. if (new_tbl)
  390. return new_tbl;
  391. if (PTR_ERR(data) != -ENOENT)
  392. return ERR_CAST(data);
  393. if (unlikely(rht_grow_above_max(ht, tbl)))
  394. return ERR_PTR(-E2BIG);
  395. if (unlikely(rht_grow_above_100(ht, tbl)))
  396. return ERR_PTR(-EAGAIN);
  397. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  398. RCU_INIT_POINTER(obj->next, head);
  399. if (ht->rhlist) {
  400. struct rhlist_head *list;
  401. list = container_of(obj, struct rhlist_head, rhead);
  402. RCU_INIT_POINTER(list->next, NULL);
  403. }
  404. rcu_assign_pointer(tbl->buckets[hash], obj);
  405. atomic_inc(&ht->nelems);
  406. if (rht_grow_above_75(ht, tbl))
  407. schedule_work(&ht->run_work);
  408. return NULL;
  409. }
  410. static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
  411. struct rhash_head *obj)
  412. {
  413. struct bucket_table *new_tbl;
  414. struct bucket_table *tbl;
  415. unsigned int hash;
  416. spinlock_t *lock;
  417. void *data;
  418. tbl = rcu_dereference(ht->tbl);
  419. /* All insertions must grab the oldest table containing
  420. * the hashed bucket that is yet to be rehashed.
  421. */
  422. for (;;) {
  423. hash = rht_head_hashfn(ht, tbl, obj, ht->p);
  424. lock = rht_bucket_lock(tbl, hash);
  425. spin_lock_bh(lock);
  426. if (tbl->rehash <= hash)
  427. break;
  428. spin_unlock_bh(lock);
  429. tbl = rcu_dereference(tbl->future_tbl);
  430. }
  431. data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
  432. new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
  433. if (PTR_ERR(new_tbl) != -EEXIST)
  434. data = ERR_CAST(new_tbl);
  435. while (!IS_ERR_OR_NULL(new_tbl)) {
  436. tbl = new_tbl;
  437. hash = rht_head_hashfn(ht, tbl, obj, ht->p);
  438. spin_lock_nested(rht_bucket_lock(tbl, hash),
  439. SINGLE_DEPTH_NESTING);
  440. data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
  441. new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
  442. if (PTR_ERR(new_tbl) != -EEXIST)
  443. data = ERR_CAST(new_tbl);
  444. spin_unlock(rht_bucket_lock(tbl, hash));
  445. }
  446. spin_unlock_bh(lock);
  447. if (PTR_ERR(data) == -EAGAIN)
  448. data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
  449. -EAGAIN);
  450. return data;
  451. }
  452. void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
  453. struct rhash_head *obj)
  454. {
  455. void *data;
  456. do {
  457. rcu_read_lock();
  458. data = rhashtable_try_insert(ht, key, obj);
  459. rcu_read_unlock();
  460. } while (PTR_ERR(data) == -EAGAIN);
  461. return data;
  462. }
  463. EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
  464. /**
  465. * rhashtable_walk_enter - Initialise an iterator
  466. * @ht: Table to walk over
  467. * @iter: Hash table Iterator
  468. *
  469. * This function prepares a hash table walk.
  470. *
  471. * Note that if you restart a walk after rhashtable_walk_stop you
  472. * may see the same object twice. Also, you may miss objects if
  473. * there are removals in between rhashtable_walk_stop and the next
  474. * call to rhashtable_walk_start.
  475. *
  476. * For a completely stable walk you should construct your own data
  477. * structure outside the hash table.
  478. *
  479. * This function may sleep so you must not call it from interrupt
  480. * context or with spin locks held.
  481. *
  482. * You must call rhashtable_walk_exit after this function returns.
  483. */
  484. void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
  485. {
  486. iter->ht = ht;
  487. iter->p = NULL;
  488. iter->slot = 0;
  489. iter->skip = 0;
  490. spin_lock(&ht->lock);
  491. iter->walker.tbl =
  492. rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
  493. list_add(&iter->walker.list, &iter->walker.tbl->walkers);
  494. spin_unlock(&ht->lock);
  495. }
  496. EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
  497. /**
  498. * rhashtable_walk_exit - Free an iterator
  499. * @iter: Hash table Iterator
  500. *
  501. * This function frees resources allocated by rhashtable_walk_init.
  502. */
  503. void rhashtable_walk_exit(struct rhashtable_iter *iter)
  504. {
  505. spin_lock(&iter->ht->lock);
  506. if (iter->walker.tbl)
  507. list_del(&iter->walker.list);
  508. spin_unlock(&iter->ht->lock);
  509. }
  510. EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
  511. /**
  512. * rhashtable_walk_start - Start a hash table walk
  513. * @iter: Hash table iterator
  514. *
  515. * Start a hash table walk. Note that we take the RCU lock in all
  516. * cases including when we return an error. So you must always call
  517. * rhashtable_walk_stop to clean up.
  518. *
  519. * Returns zero if successful.
  520. *
  521. * Returns -EAGAIN if resize event occured. Note that the iterator
  522. * will rewind back to the beginning and you may use it immediately
  523. * by calling rhashtable_walk_next.
  524. */
  525. int rhashtable_walk_start(struct rhashtable_iter *iter)
  526. __acquires(RCU)
  527. {
  528. struct rhashtable *ht = iter->ht;
  529. rcu_read_lock();
  530. spin_lock(&ht->lock);
  531. if (iter->walker.tbl)
  532. list_del(&iter->walker.list);
  533. spin_unlock(&ht->lock);
  534. if (!iter->walker.tbl) {
  535. iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
  536. return -EAGAIN;
  537. }
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(rhashtable_walk_start);
  541. /**
  542. * rhashtable_walk_next - Return the next object and advance the iterator
  543. * @iter: Hash table iterator
  544. *
  545. * Note that you must call rhashtable_walk_stop when you are finished
  546. * with the walk.
  547. *
  548. * Returns the next object or NULL when the end of the table is reached.
  549. *
  550. * Returns -EAGAIN if resize event occured. Note that the iterator
  551. * will rewind back to the beginning and you may continue to use it.
  552. */
  553. void *rhashtable_walk_next(struct rhashtable_iter *iter)
  554. {
  555. struct bucket_table *tbl = iter->walker.tbl;
  556. struct rhlist_head *list = iter->list;
  557. struct rhashtable *ht = iter->ht;
  558. struct rhash_head *p = iter->p;
  559. bool rhlist = ht->rhlist;
  560. if (p) {
  561. if (!rhlist || !(list = rcu_dereference(list->next))) {
  562. p = rcu_dereference(p->next);
  563. list = container_of(p, struct rhlist_head, rhead);
  564. }
  565. goto next;
  566. }
  567. for (; iter->slot < tbl->size; iter->slot++) {
  568. int skip = iter->skip;
  569. rht_for_each_rcu(p, tbl, iter->slot) {
  570. if (rhlist) {
  571. list = container_of(p, struct rhlist_head,
  572. rhead);
  573. do {
  574. if (!skip)
  575. goto next;
  576. skip--;
  577. list = rcu_dereference(list->next);
  578. } while (list);
  579. continue;
  580. }
  581. if (!skip)
  582. break;
  583. skip--;
  584. }
  585. next:
  586. if (!rht_is_a_nulls(p)) {
  587. iter->skip++;
  588. iter->p = p;
  589. iter->list = list;
  590. return rht_obj(ht, rhlist ? &list->rhead : p);
  591. }
  592. iter->skip = 0;
  593. }
  594. iter->p = NULL;
  595. /* Ensure we see any new tables. */
  596. smp_rmb();
  597. iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  598. if (iter->walker.tbl) {
  599. iter->slot = 0;
  600. iter->skip = 0;
  601. return ERR_PTR(-EAGAIN);
  602. }
  603. return NULL;
  604. }
  605. EXPORT_SYMBOL_GPL(rhashtable_walk_next);
  606. /**
  607. * rhashtable_walk_stop - Finish a hash table walk
  608. * @iter: Hash table iterator
  609. *
  610. * Finish a hash table walk.
  611. */
  612. void rhashtable_walk_stop(struct rhashtable_iter *iter)
  613. __releases(RCU)
  614. {
  615. struct rhashtable *ht;
  616. struct bucket_table *tbl = iter->walker.tbl;
  617. if (!tbl)
  618. goto out;
  619. ht = iter->ht;
  620. spin_lock(&ht->lock);
  621. if (tbl->rehash < tbl->size)
  622. list_add(&iter->walker.list, &tbl->walkers);
  623. else
  624. iter->walker.tbl = NULL;
  625. spin_unlock(&ht->lock);
  626. iter->p = NULL;
  627. out:
  628. rcu_read_unlock();
  629. }
  630. EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
  631. static size_t rounded_hashtable_size(const struct rhashtable_params *params)
  632. {
  633. return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
  634. (unsigned long)params->min_size);
  635. }
  636. static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
  637. {
  638. return jhash2(key, length, seed);
  639. }
  640. /**
  641. * rhashtable_init - initialize a new hash table
  642. * @ht: hash table to be initialized
  643. * @params: configuration parameters
  644. *
  645. * Initializes a new hash table based on the provided configuration
  646. * parameters. A table can be configured either with a variable or
  647. * fixed length key:
  648. *
  649. * Configuration Example 1: Fixed length keys
  650. * struct test_obj {
  651. * int key;
  652. * void * my_member;
  653. * struct rhash_head node;
  654. * };
  655. *
  656. * struct rhashtable_params params = {
  657. * .head_offset = offsetof(struct test_obj, node),
  658. * .key_offset = offsetof(struct test_obj, key),
  659. * .key_len = sizeof(int),
  660. * .hashfn = jhash,
  661. * .nulls_base = (1U << RHT_BASE_SHIFT),
  662. * };
  663. *
  664. * Configuration Example 2: Variable length keys
  665. * struct test_obj {
  666. * [...]
  667. * struct rhash_head node;
  668. * };
  669. *
  670. * u32 my_hash_fn(const void *data, u32 len, u32 seed)
  671. * {
  672. * struct test_obj *obj = data;
  673. *
  674. * return [... hash ...];
  675. * }
  676. *
  677. * struct rhashtable_params params = {
  678. * .head_offset = offsetof(struct test_obj, node),
  679. * .hashfn = jhash,
  680. * .obj_hashfn = my_hash_fn,
  681. * };
  682. */
  683. int rhashtable_init(struct rhashtable *ht,
  684. const struct rhashtable_params *params)
  685. {
  686. struct bucket_table *tbl;
  687. size_t size;
  688. size = HASH_DEFAULT_SIZE;
  689. if ((!params->key_len && !params->obj_hashfn) ||
  690. (params->obj_hashfn && !params->obj_cmpfn))
  691. return -EINVAL;
  692. if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
  693. return -EINVAL;
  694. memset(ht, 0, sizeof(*ht));
  695. mutex_init(&ht->mutex);
  696. spin_lock_init(&ht->lock);
  697. memcpy(&ht->p, params, sizeof(*params));
  698. if (params->min_size)
  699. ht->p.min_size = roundup_pow_of_two(params->min_size);
  700. if (params->max_size)
  701. ht->p.max_size = rounddown_pow_of_two(params->max_size);
  702. if (params->insecure_max_entries)
  703. ht->p.insecure_max_entries =
  704. rounddown_pow_of_two(params->insecure_max_entries);
  705. else
  706. ht->p.insecure_max_entries = ht->p.max_size * 2;
  707. ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
  708. if (params->nelem_hint)
  709. size = rounded_hashtable_size(&ht->p);
  710. /* The maximum (not average) chain length grows with the
  711. * size of the hash table, at a rate of (log N)/(log log N).
  712. * The value of 16 is selected so that even if the hash
  713. * table grew to 2^32 you would not expect the maximum
  714. * chain length to exceed it unless we are under attack
  715. * (or extremely unlucky).
  716. *
  717. * As this limit is only to detect attacks, we don't need
  718. * to set it to a lower value as you'd need the chain
  719. * length to vastly exceed 16 to have any real effect
  720. * on the system.
  721. */
  722. if (!params->insecure_elasticity)
  723. ht->elasticity = 16;
  724. if (params->locks_mul)
  725. ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
  726. else
  727. ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
  728. ht->key_len = ht->p.key_len;
  729. if (!params->hashfn) {
  730. ht->p.hashfn = jhash;
  731. if (!(ht->key_len & (sizeof(u32) - 1))) {
  732. ht->key_len /= sizeof(u32);
  733. ht->p.hashfn = rhashtable_jhash2;
  734. }
  735. }
  736. tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  737. if (tbl == NULL)
  738. return -ENOMEM;
  739. atomic_set(&ht->nelems, 0);
  740. RCU_INIT_POINTER(ht->tbl, tbl);
  741. INIT_WORK(&ht->run_work, rht_deferred_worker);
  742. return 0;
  743. }
  744. EXPORT_SYMBOL_GPL(rhashtable_init);
  745. /**
  746. * rhltable_init - initialize a new hash list table
  747. * @hlt: hash list table to be initialized
  748. * @params: configuration parameters
  749. *
  750. * Initializes a new hash list table.
  751. *
  752. * See documentation for rhashtable_init.
  753. */
  754. int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
  755. {
  756. int err;
  757. /* No rhlist NULLs marking for now. */
  758. if (params->nulls_base)
  759. return -EINVAL;
  760. err = rhashtable_init(&hlt->ht, params);
  761. hlt->ht.rhlist = true;
  762. return err;
  763. }
  764. EXPORT_SYMBOL_GPL(rhltable_init);
  765. static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
  766. void (*free_fn)(void *ptr, void *arg),
  767. void *arg)
  768. {
  769. struct rhlist_head *list;
  770. if (!ht->rhlist) {
  771. free_fn(rht_obj(ht, obj), arg);
  772. return;
  773. }
  774. list = container_of(obj, struct rhlist_head, rhead);
  775. do {
  776. obj = &list->rhead;
  777. list = rht_dereference(list->next, ht);
  778. free_fn(rht_obj(ht, obj), arg);
  779. } while (list);
  780. }
  781. /**
  782. * rhashtable_free_and_destroy - free elements and destroy hash table
  783. * @ht: the hash table to destroy
  784. * @free_fn: callback to release resources of element
  785. * @arg: pointer passed to free_fn
  786. *
  787. * Stops an eventual async resize. If defined, invokes free_fn for each
  788. * element to releasal resources. Please note that RCU protected
  789. * readers may still be accessing the elements. Releasing of resources
  790. * must occur in a compatible manner. Then frees the bucket array.
  791. *
  792. * This function will eventually sleep to wait for an async resize
  793. * to complete. The caller is responsible that no further write operations
  794. * occurs in parallel.
  795. */
  796. void rhashtable_free_and_destroy(struct rhashtable *ht,
  797. void (*free_fn)(void *ptr, void *arg),
  798. void *arg)
  799. {
  800. const struct bucket_table *tbl;
  801. unsigned int i;
  802. cancel_work_sync(&ht->run_work);
  803. mutex_lock(&ht->mutex);
  804. tbl = rht_dereference(ht->tbl, ht);
  805. if (free_fn) {
  806. for (i = 0; i < tbl->size; i++) {
  807. struct rhash_head *pos, *next;
  808. for (pos = rht_dereference(tbl->buckets[i], ht),
  809. next = !rht_is_a_nulls(pos) ?
  810. rht_dereference(pos->next, ht) : NULL;
  811. !rht_is_a_nulls(pos);
  812. pos = next,
  813. next = !rht_is_a_nulls(pos) ?
  814. rht_dereference(pos->next, ht) : NULL)
  815. rhashtable_free_one(ht, pos, free_fn, arg);
  816. }
  817. }
  818. bucket_table_free(tbl);
  819. mutex_unlock(&ht->mutex);
  820. }
  821. EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
  822. void rhashtable_destroy(struct rhashtable *ht)
  823. {
  824. return rhashtable_free_and_destroy(ht, NULL, NULL);
  825. }
  826. EXPORT_SYMBOL_GPL(rhashtable_destroy);