rhashtable.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. pprev = &head->next;
  362. continue;
  363. }
  364. if (!ht->rhlist)
  365. return rht_obj(ht, head);
  366. list = container_of(obj, struct rhlist_head, rhead);
  367. plist = container_of(head, struct rhlist_head, rhead);
  368. RCU_INIT_POINTER(list->next, plist);
  369. head = rht_dereference_bucket(head->next, tbl, hash);
  370. RCU_INIT_POINTER(list->rhead.next, head);
  371. rcu_assign_pointer(*pprev, obj);
  372. return NULL;
  373. }
  374. if (elasticity <= 0)
  375. return ERR_PTR(-EAGAIN);
  376. return ERR_PTR(-ENOENT);
  377. }
  378. static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht,
  379. struct bucket_table *tbl,
  380. unsigned int hash,
  381. struct rhash_head *obj,
  382. void *data)
  383. {
  384. struct bucket_table *new_tbl;
  385. struct rhash_head *head;
  386. if (!IS_ERR_OR_NULL(data))
  387. return ERR_PTR(-EEXIST);
  388. if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
  389. return ERR_CAST(data);
  390. new_tbl = rcu_dereference(tbl->future_tbl);
  391. if (new_tbl)
  392. return new_tbl;
  393. if (PTR_ERR(data) != -ENOENT)
  394. return ERR_CAST(data);
  395. if (unlikely(rht_grow_above_max(ht, tbl)))
  396. return ERR_PTR(-E2BIG);
  397. if (unlikely(rht_grow_above_100(ht, tbl)))
  398. return ERR_PTR(-EAGAIN);
  399. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  400. RCU_INIT_POINTER(obj->next, head);
  401. if (ht->rhlist) {
  402. struct rhlist_head *list;
  403. list = container_of(obj, struct rhlist_head, rhead);
  404. RCU_INIT_POINTER(list->next, NULL);
  405. }
  406. rcu_assign_pointer(tbl->buckets[hash], obj);
  407. atomic_inc(&ht->nelems);
  408. if (rht_grow_above_75(ht, tbl))
  409. schedule_work(&ht->run_work);
  410. return NULL;
  411. }
  412. static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
  413. struct rhash_head *obj)
  414. {
  415. struct bucket_table *new_tbl;
  416. struct bucket_table *tbl;
  417. unsigned int hash;
  418. spinlock_t *lock;
  419. void *data;
  420. tbl = rcu_dereference(ht->tbl);
  421. /* All insertions must grab the oldest table containing
  422. * the hashed bucket that is yet to be rehashed.
  423. */
  424. for (;;) {
  425. hash = rht_head_hashfn(ht, tbl, obj, ht->p);
  426. lock = rht_bucket_lock(tbl, hash);
  427. spin_lock_bh(lock);
  428. if (tbl->rehash <= hash)
  429. break;
  430. spin_unlock_bh(lock);
  431. tbl = rcu_dereference(tbl->future_tbl);
  432. }
  433. data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
  434. new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
  435. if (PTR_ERR(new_tbl) != -EEXIST)
  436. data = ERR_CAST(new_tbl);
  437. while (!IS_ERR_OR_NULL(new_tbl)) {
  438. tbl = new_tbl;
  439. hash = rht_head_hashfn(ht, tbl, obj, ht->p);
  440. spin_lock_nested(rht_bucket_lock(tbl, hash),
  441. SINGLE_DEPTH_NESTING);
  442. data = rhashtable_lookup_one(ht, tbl, hash, key, obj);
  443. new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data);
  444. if (PTR_ERR(new_tbl) != -EEXIST)
  445. data = ERR_CAST(new_tbl);
  446. spin_unlock(rht_bucket_lock(tbl, hash));
  447. }
  448. spin_unlock_bh(lock);
  449. if (PTR_ERR(data) == -EAGAIN)
  450. data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
  451. -EAGAIN);
  452. return data;
  453. }
  454. void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
  455. struct rhash_head *obj)
  456. {
  457. void *data;
  458. do {
  459. rcu_read_lock();
  460. data = rhashtable_try_insert(ht, key, obj);
  461. rcu_read_unlock();
  462. } while (PTR_ERR(data) == -EAGAIN);
  463. return data;
  464. }
  465. EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
  466. /**
  467. * rhashtable_walk_enter - Initialise an iterator
  468. * @ht: Table to walk over
  469. * @iter: Hash table Iterator
  470. *
  471. * This function prepares a hash table walk.
  472. *
  473. * Note that if you restart a walk after rhashtable_walk_stop you
  474. * may see the same object twice. Also, you may miss objects if
  475. * there are removals in between rhashtable_walk_stop and the next
  476. * call to rhashtable_walk_start.
  477. *
  478. * For a completely stable walk you should construct your own data
  479. * structure outside the hash table.
  480. *
  481. * This function may sleep so you must not call it from interrupt
  482. * context or with spin locks held.
  483. *
  484. * You must call rhashtable_walk_exit after this function returns.
  485. */
  486. void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
  487. {
  488. iter->ht = ht;
  489. iter->p = NULL;
  490. iter->slot = 0;
  491. iter->skip = 0;
  492. spin_lock(&ht->lock);
  493. iter->walker.tbl =
  494. rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
  495. list_add(&iter->walker.list, &iter->walker.tbl->walkers);
  496. spin_unlock(&ht->lock);
  497. }
  498. EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
  499. /**
  500. * rhashtable_walk_exit - Free an iterator
  501. * @iter: Hash table Iterator
  502. *
  503. * This function frees resources allocated by rhashtable_walk_init.
  504. */
  505. void rhashtable_walk_exit(struct rhashtable_iter *iter)
  506. {
  507. spin_lock(&iter->ht->lock);
  508. if (iter->walker.tbl)
  509. list_del(&iter->walker.list);
  510. spin_unlock(&iter->ht->lock);
  511. }
  512. EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
  513. /**
  514. * rhashtable_walk_start - Start a hash table walk
  515. * @iter: Hash table iterator
  516. *
  517. * Start a hash table walk. Note that we take the RCU lock in all
  518. * cases including when we return an error. So you must always call
  519. * rhashtable_walk_stop to clean up.
  520. *
  521. * Returns zero if successful.
  522. *
  523. * Returns -EAGAIN if resize event occured. Note that the iterator
  524. * will rewind back to the beginning and you may use it immediately
  525. * by calling rhashtable_walk_next.
  526. */
  527. int rhashtable_walk_start(struct rhashtable_iter *iter)
  528. __acquires(RCU)
  529. {
  530. struct rhashtable *ht = iter->ht;
  531. rcu_read_lock();
  532. spin_lock(&ht->lock);
  533. if (iter->walker.tbl)
  534. list_del(&iter->walker.list);
  535. spin_unlock(&ht->lock);
  536. if (!iter->walker.tbl) {
  537. iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
  538. return -EAGAIN;
  539. }
  540. return 0;
  541. }
  542. EXPORT_SYMBOL_GPL(rhashtable_walk_start);
  543. /**
  544. * rhashtable_walk_next - Return the next object and advance the iterator
  545. * @iter: Hash table iterator
  546. *
  547. * Note that you must call rhashtable_walk_stop when you are finished
  548. * with the walk.
  549. *
  550. * Returns the next object or NULL when the end of the table is reached.
  551. *
  552. * Returns -EAGAIN if resize event occured. Note that the iterator
  553. * will rewind back to the beginning and you may continue to use it.
  554. */
  555. void *rhashtable_walk_next(struct rhashtable_iter *iter)
  556. {
  557. struct bucket_table *tbl = iter->walker.tbl;
  558. struct rhlist_head *list = iter->list;
  559. struct rhashtable *ht = iter->ht;
  560. struct rhash_head *p = iter->p;
  561. bool rhlist = ht->rhlist;
  562. if (p) {
  563. if (!rhlist || !(list = rcu_dereference(list->next))) {
  564. p = rcu_dereference(p->next);
  565. list = container_of(p, struct rhlist_head, rhead);
  566. }
  567. goto next;
  568. }
  569. for (; iter->slot < tbl->size; iter->slot++) {
  570. int skip = iter->skip;
  571. rht_for_each_rcu(p, tbl, iter->slot) {
  572. if (rhlist) {
  573. list = container_of(p, struct rhlist_head,
  574. rhead);
  575. do {
  576. if (!skip)
  577. goto next;
  578. skip--;
  579. list = rcu_dereference(list->next);
  580. } while (list);
  581. continue;
  582. }
  583. if (!skip)
  584. break;
  585. skip--;
  586. }
  587. next:
  588. if (!rht_is_a_nulls(p)) {
  589. iter->skip++;
  590. iter->p = p;
  591. iter->list = list;
  592. return rht_obj(ht, rhlist ? &list->rhead : p);
  593. }
  594. iter->skip = 0;
  595. }
  596. iter->p = NULL;
  597. /* Ensure we see any new tables. */
  598. smp_rmb();
  599. iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  600. if (iter->walker.tbl) {
  601. iter->slot = 0;
  602. iter->skip = 0;
  603. return ERR_PTR(-EAGAIN);
  604. }
  605. return NULL;
  606. }
  607. EXPORT_SYMBOL_GPL(rhashtable_walk_next);
  608. /**
  609. * rhashtable_walk_stop - Finish a hash table walk
  610. * @iter: Hash table iterator
  611. *
  612. * Finish a hash table walk.
  613. */
  614. void rhashtable_walk_stop(struct rhashtable_iter *iter)
  615. __releases(RCU)
  616. {
  617. struct rhashtable *ht;
  618. struct bucket_table *tbl = iter->walker.tbl;
  619. if (!tbl)
  620. goto out;
  621. ht = iter->ht;
  622. spin_lock(&ht->lock);
  623. if (tbl->rehash < tbl->size)
  624. list_add(&iter->walker.list, &tbl->walkers);
  625. else
  626. iter->walker.tbl = NULL;
  627. spin_unlock(&ht->lock);
  628. iter->p = NULL;
  629. out:
  630. rcu_read_unlock();
  631. }
  632. EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
  633. static size_t rounded_hashtable_size(const struct rhashtable_params *params)
  634. {
  635. size_t retsize;
  636. if (params->nelem_hint)
  637. retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
  638. (unsigned long)params->min_size);
  639. else
  640. retsize = max(HASH_DEFAULT_SIZE,
  641. (unsigned long)params->min_size);
  642. return retsize;
  643. }
  644. static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
  645. {
  646. return jhash2(key, length, seed);
  647. }
  648. /**
  649. * rhashtable_init - initialize a new hash table
  650. * @ht: hash table to be initialized
  651. * @params: configuration parameters
  652. *
  653. * Initializes a new hash table based on the provided configuration
  654. * parameters. A table can be configured either with a variable or
  655. * fixed length key:
  656. *
  657. * Configuration Example 1: Fixed length keys
  658. * struct test_obj {
  659. * int key;
  660. * void * my_member;
  661. * struct rhash_head node;
  662. * };
  663. *
  664. * struct rhashtable_params params = {
  665. * .head_offset = offsetof(struct test_obj, node),
  666. * .key_offset = offsetof(struct test_obj, key),
  667. * .key_len = sizeof(int),
  668. * .hashfn = jhash,
  669. * .nulls_base = (1U << RHT_BASE_SHIFT),
  670. * };
  671. *
  672. * Configuration Example 2: Variable length keys
  673. * struct test_obj {
  674. * [...]
  675. * struct rhash_head node;
  676. * };
  677. *
  678. * u32 my_hash_fn(const void *data, u32 len, u32 seed)
  679. * {
  680. * struct test_obj *obj = data;
  681. *
  682. * return [... hash ...];
  683. * }
  684. *
  685. * struct rhashtable_params params = {
  686. * .head_offset = offsetof(struct test_obj, node),
  687. * .hashfn = jhash,
  688. * .obj_hashfn = my_hash_fn,
  689. * };
  690. */
  691. int rhashtable_init(struct rhashtable *ht,
  692. const struct rhashtable_params *params)
  693. {
  694. struct bucket_table *tbl;
  695. size_t size;
  696. if ((!params->key_len && !params->obj_hashfn) ||
  697. (params->obj_hashfn && !params->obj_cmpfn))
  698. return -EINVAL;
  699. if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
  700. return -EINVAL;
  701. memset(ht, 0, sizeof(*ht));
  702. mutex_init(&ht->mutex);
  703. spin_lock_init(&ht->lock);
  704. memcpy(&ht->p, params, sizeof(*params));
  705. if (params->min_size)
  706. ht->p.min_size = roundup_pow_of_two(params->min_size);
  707. if (params->max_size)
  708. ht->p.max_size = rounddown_pow_of_two(params->max_size);
  709. if (params->insecure_max_entries)
  710. ht->p.insecure_max_entries =
  711. rounddown_pow_of_two(params->insecure_max_entries);
  712. else
  713. ht->p.insecure_max_entries = ht->p.max_size * 2;
  714. ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
  715. size = rounded_hashtable_size(&ht->p);
  716. /* The maximum (not average) chain length grows with the
  717. * size of the hash table, at a rate of (log N)/(log log N).
  718. * The value of 16 is selected so that even if the hash
  719. * table grew to 2^32 you would not expect the maximum
  720. * chain length to exceed it unless we are under attack
  721. * (or extremely unlucky).
  722. *
  723. * As this limit is only to detect attacks, we don't need
  724. * to set it to a lower value as you'd need the chain
  725. * length to vastly exceed 16 to have any real effect
  726. * on the system.
  727. */
  728. if (!params->insecure_elasticity)
  729. ht->elasticity = 16;
  730. if (params->locks_mul)
  731. ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
  732. else
  733. ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
  734. ht->key_len = ht->p.key_len;
  735. if (!params->hashfn) {
  736. ht->p.hashfn = jhash;
  737. if (!(ht->key_len & (sizeof(u32) - 1))) {
  738. ht->key_len /= sizeof(u32);
  739. ht->p.hashfn = rhashtable_jhash2;
  740. }
  741. }
  742. tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  743. if (tbl == NULL)
  744. return -ENOMEM;
  745. atomic_set(&ht->nelems, 0);
  746. RCU_INIT_POINTER(ht->tbl, tbl);
  747. INIT_WORK(&ht->run_work, rht_deferred_worker);
  748. return 0;
  749. }
  750. EXPORT_SYMBOL_GPL(rhashtable_init);
  751. /**
  752. * rhltable_init - initialize a new hash list table
  753. * @hlt: hash list table to be initialized
  754. * @params: configuration parameters
  755. *
  756. * Initializes a new hash list table.
  757. *
  758. * See documentation for rhashtable_init.
  759. */
  760. int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
  761. {
  762. int err;
  763. /* No rhlist NULLs marking for now. */
  764. if (params->nulls_base)
  765. return -EINVAL;
  766. err = rhashtable_init(&hlt->ht, params);
  767. hlt->ht.rhlist = true;
  768. return err;
  769. }
  770. EXPORT_SYMBOL_GPL(rhltable_init);
  771. static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
  772. void (*free_fn)(void *ptr, void *arg),
  773. void *arg)
  774. {
  775. struct rhlist_head *list;
  776. if (!ht->rhlist) {
  777. free_fn(rht_obj(ht, obj), arg);
  778. return;
  779. }
  780. list = container_of(obj, struct rhlist_head, rhead);
  781. do {
  782. obj = &list->rhead;
  783. list = rht_dereference(list->next, ht);
  784. free_fn(rht_obj(ht, obj), arg);
  785. } while (list);
  786. }
  787. /**
  788. * rhashtable_free_and_destroy - free elements and destroy hash table
  789. * @ht: the hash table to destroy
  790. * @free_fn: callback to release resources of element
  791. * @arg: pointer passed to free_fn
  792. *
  793. * Stops an eventual async resize. If defined, invokes free_fn for each
  794. * element to releasal resources. Please note that RCU protected
  795. * readers may still be accessing the elements. Releasing of resources
  796. * must occur in a compatible manner. Then frees the bucket array.
  797. *
  798. * This function will eventually sleep to wait for an async resize
  799. * to complete. The caller is responsible that no further write operations
  800. * occurs in parallel.
  801. */
  802. void rhashtable_free_and_destroy(struct rhashtable *ht,
  803. void (*free_fn)(void *ptr, void *arg),
  804. void *arg)
  805. {
  806. const struct bucket_table *tbl;
  807. unsigned int i;
  808. cancel_work_sync(&ht->run_work);
  809. mutex_lock(&ht->mutex);
  810. tbl = rht_dereference(ht->tbl, ht);
  811. if (free_fn) {
  812. for (i = 0; i < tbl->size; i++) {
  813. struct rhash_head *pos, *next;
  814. for (pos = rht_dereference(tbl->buckets[i], ht),
  815. next = !rht_is_a_nulls(pos) ?
  816. rht_dereference(pos->next, ht) : NULL;
  817. !rht_is_a_nulls(pos);
  818. pos = next,
  819. next = !rht_is_a_nulls(pos) ?
  820. rht_dereference(pos->next, ht) : NULL)
  821. rhashtable_free_one(ht, pos, free_fn, arg);
  822. }
  823. }
  824. bucket_table_free(tbl);
  825. mutex_unlock(&ht->mutex);
  826. }
  827. EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
  828. void rhashtable_destroy(struct rhashtable *ht)
  829. {
  830. return rhashtable_free_and_destroy(ht, NULL, NULL);
  831. }
  832. EXPORT_SYMBOL_GPL(rhashtable_destroy);