rhashtable.c 24 KB

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