rhashtable.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Resizable, Scalable, Concurrent Hash Table
  4. *
  5. * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
  6. * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
  7. * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
  8. *
  9. * Code partially derived from nft_hash
  10. * Rewritten with rehash code from br_multicast plus single list
  11. * pointer as suggested by Josh Triplett
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/log2.h>
  17. #include <linux/sched.h>
  18. #include <linux/rculist.h>
  19. #include <linux/slab.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/mm.h>
  22. #include <linux/jhash.h>
  23. #include <linux/random.h>
  24. #include <linux/rhashtable.h>
  25. #include <linux/err.h>
  26. #include <linux/export.h>
  27. #define HASH_DEFAULT_SIZE 64UL
  28. #define HASH_MIN_SIZE 4U
  29. union nested_table {
  30. union nested_table __rcu *table;
  31. struct rhash_lock_head __rcu *bucket;
  32. };
  33. static u32 head_hashfn(struct rhashtable *ht,
  34. const struct bucket_table *tbl,
  35. const struct rhash_head *he)
  36. {
  37. return rht_head_hashfn(ht, tbl, he, ht->p);
  38. }
  39. #ifdef CONFIG_PROVE_LOCKING
  40. #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
  41. int lockdep_rht_mutex_is_held(struct rhashtable *ht)
  42. {
  43. return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
  44. }
  45. EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
  46. int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
  47. {
  48. if (!debug_locks)
  49. return 1;
  50. if (unlikely(tbl->nest))
  51. return 1;
  52. return bit_spin_is_locked(0, (unsigned long *)&tbl->buckets[hash]);
  53. }
  54. EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
  55. #else
  56. #define ASSERT_RHT_MUTEX(HT)
  57. #endif
  58. static inline union nested_table *nested_table_top(
  59. const struct bucket_table *tbl)
  60. {
  61. /* The top-level bucket entry does not need RCU protection
  62. * because it's set at the same time as tbl->nest.
  63. */
  64. return (void *)rcu_dereference_protected(tbl->buckets[0], 1);
  65. }
  66. static void nested_table_free(union nested_table *ntbl, unsigned int size)
  67. {
  68. const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
  69. const unsigned int len = 1 << shift;
  70. unsigned int i;
  71. ntbl = rcu_dereference_protected(ntbl->table, 1);
  72. if (!ntbl)
  73. return;
  74. if (size > len) {
  75. size >>= shift;
  76. for (i = 0; i < len; i++)
  77. nested_table_free(ntbl + i, size);
  78. }
  79. kfree(ntbl);
  80. }
  81. static void nested_bucket_table_free(const struct bucket_table *tbl)
  82. {
  83. unsigned int size = tbl->size >> tbl->nest;
  84. unsigned int len = 1 << tbl->nest;
  85. union nested_table *ntbl;
  86. unsigned int i;
  87. ntbl = nested_table_top(tbl);
  88. for (i = 0; i < len; i++)
  89. nested_table_free(ntbl + i, size);
  90. kfree(ntbl);
  91. }
  92. static void bucket_table_free(const struct bucket_table *tbl)
  93. {
  94. if (tbl->nest)
  95. nested_bucket_table_free(tbl);
  96. kvfree(tbl);
  97. }
  98. static void bucket_table_free_rcu(struct rcu_head *head)
  99. {
  100. bucket_table_free(container_of(head, struct bucket_table, rcu));
  101. }
  102. static union nested_table *nested_table_alloc(struct rhashtable *ht,
  103. union nested_table __rcu **prev,
  104. bool leaf)
  105. {
  106. union nested_table *ntbl;
  107. int i;
  108. ntbl = rcu_dereference(*prev);
  109. if (ntbl)
  110. return ntbl;
  111. ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC);
  112. if (ntbl && leaf) {
  113. for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++)
  114. INIT_RHT_NULLS_HEAD(ntbl[i].bucket);
  115. }
  116. if (cmpxchg((union nested_table **)prev, NULL, ntbl) == NULL)
  117. return ntbl;
  118. /* Raced with another thread. */
  119. kfree(ntbl);
  120. return rcu_dereference(*prev);
  121. }
  122. static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht,
  123. size_t nbuckets,
  124. gfp_t gfp)
  125. {
  126. const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
  127. struct bucket_table *tbl;
  128. size_t size;
  129. if (nbuckets < (1 << (shift + 1)))
  130. return NULL;
  131. size = sizeof(*tbl) + sizeof(tbl->buckets[0]);
  132. tbl = kzalloc(size, gfp);
  133. if (!tbl)
  134. return NULL;
  135. if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets,
  136. false)) {
  137. kfree(tbl);
  138. return NULL;
  139. }
  140. tbl->nest = (ilog2(nbuckets) - 1) % shift + 1;
  141. return tbl;
  142. }
  143. static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
  144. size_t nbuckets,
  145. gfp_t gfp)
  146. {
  147. struct bucket_table *tbl = NULL;
  148. size_t size;
  149. int i;
  150. static struct lock_class_key __key;
  151. tbl = kvzalloc(struct_size(tbl, buckets, nbuckets), gfp);
  152. size = nbuckets;
  153. if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) {
  154. tbl = nested_bucket_table_alloc(ht, nbuckets, gfp);
  155. nbuckets = 0;
  156. }
  157. if (tbl == NULL)
  158. return NULL;
  159. lockdep_init_map(&tbl->dep_map, "rhashtable_bucket", &__key, 0);
  160. tbl->size = size;
  161. rcu_head_init(&tbl->rcu);
  162. INIT_LIST_HEAD(&tbl->walkers);
  163. tbl->hash_rnd = get_random_u32();
  164. for (i = 0; i < nbuckets; i++)
  165. INIT_RHT_NULLS_HEAD(tbl->buckets[i]);
  166. return tbl;
  167. }
  168. static struct bucket_table *rhashtable_last_table(struct rhashtable *ht,
  169. struct bucket_table *tbl)
  170. {
  171. struct bucket_table *new_tbl;
  172. do {
  173. new_tbl = tbl;
  174. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  175. } while (tbl);
  176. return new_tbl;
  177. }
  178. static int rhashtable_rehash_one(struct rhashtable *ht,
  179. struct rhash_lock_head __rcu **bkt,
  180. unsigned int old_hash)
  181. {
  182. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  183. struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl);
  184. int err = -EAGAIN;
  185. struct rhash_head *head, *next, *entry;
  186. struct rhash_head __rcu **pprev = NULL;
  187. unsigned int new_hash;
  188. if (new_tbl->nest)
  189. goto out;
  190. err = -ENOENT;
  191. rht_for_each_from(entry, rht_ptr(bkt, old_tbl, old_hash),
  192. old_tbl, old_hash) {
  193. err = 0;
  194. next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
  195. if (rht_is_a_nulls(next))
  196. break;
  197. pprev = &entry->next;
  198. }
  199. if (err)
  200. goto out;
  201. new_hash = head_hashfn(ht, new_tbl, entry);
  202. rht_lock_nested(new_tbl, &new_tbl->buckets[new_hash], SINGLE_DEPTH_NESTING);
  203. head = rht_ptr(new_tbl->buckets + new_hash, new_tbl, new_hash);
  204. RCU_INIT_POINTER(entry->next, head);
  205. rht_assign_unlock(new_tbl, &new_tbl->buckets[new_hash], entry);
  206. if (pprev)
  207. rcu_assign_pointer(*pprev, next);
  208. else
  209. /* Need to preserved the bit lock. */
  210. rht_assign_locked(bkt, next);
  211. out:
  212. return err;
  213. }
  214. static int rhashtable_rehash_chain(struct rhashtable *ht,
  215. unsigned int old_hash)
  216. {
  217. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  218. struct rhash_lock_head __rcu **bkt = rht_bucket_var(old_tbl, old_hash);
  219. int err;
  220. if (!bkt)
  221. return 0;
  222. rht_lock(old_tbl, bkt);
  223. while (!(err = rhashtable_rehash_one(ht, bkt, old_hash)))
  224. ;
  225. if (err == -ENOENT)
  226. err = 0;
  227. rht_unlock(old_tbl, bkt);
  228. return err;
  229. }
  230. static int rhashtable_rehash_attach(struct rhashtable *ht,
  231. struct bucket_table *old_tbl,
  232. struct bucket_table *new_tbl)
  233. {
  234. /* Make insertions go into the new, empty table right away. Deletions
  235. * and lookups will be attempted in both tables until we synchronize.
  236. * As cmpxchg() provides strong barriers, we do not need
  237. * rcu_assign_pointer().
  238. */
  239. if (cmpxchg((struct bucket_table **)&old_tbl->future_tbl, NULL,
  240. new_tbl) != NULL)
  241. return -EEXIST;
  242. return 0;
  243. }
  244. static int rhashtable_rehash_table(struct rhashtable *ht)
  245. {
  246. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  247. struct bucket_table *new_tbl;
  248. struct rhashtable_walker *walker;
  249. unsigned int old_hash;
  250. int err;
  251. new_tbl = rht_dereference(old_tbl->future_tbl, ht);
  252. if (!new_tbl)
  253. return 0;
  254. for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
  255. err = rhashtable_rehash_chain(ht, old_hash);
  256. if (err)
  257. return err;
  258. cond_resched();
  259. }
  260. /* Publish the new table pointer. */
  261. rcu_assign_pointer(ht->tbl, new_tbl);
  262. spin_lock(&ht->lock);
  263. list_for_each_entry(walker, &old_tbl->walkers, list)
  264. walker->tbl = NULL;
  265. /* Wait for readers. All new readers will see the new
  266. * table, and thus no references to the old table will
  267. * remain.
  268. * We do this inside the locked region so that
  269. * rhashtable_walk_stop() can use rcu_head_after_call_rcu()
  270. * to check if it should not re-link the table.
  271. */
  272. call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
  273. spin_unlock(&ht->lock);
  274. return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0;
  275. }
  276. static int rhashtable_rehash_alloc(struct rhashtable *ht,
  277. struct bucket_table *old_tbl,
  278. unsigned int size)
  279. {
  280. struct bucket_table *new_tbl;
  281. int err;
  282. ASSERT_RHT_MUTEX(ht);
  283. new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  284. if (new_tbl == NULL)
  285. return -ENOMEM;
  286. err = rhashtable_rehash_attach(ht, old_tbl, new_tbl);
  287. if (err)
  288. bucket_table_free(new_tbl);
  289. return err;
  290. }
  291. /**
  292. * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
  293. * @ht: the hash table to shrink
  294. *
  295. * This function shrinks the hash table to fit, i.e., the smallest
  296. * size would not cause it to expand right away automatically.
  297. *
  298. * The caller must ensure that no concurrent resizing occurs by holding
  299. * ht->mutex.
  300. *
  301. * The caller must ensure that no concurrent table mutations take place.
  302. * It is however valid to have concurrent lookups if they are RCU protected.
  303. *
  304. * It is valid to have concurrent insertions and deletions protected by per
  305. * bucket locks or concurrent RCU protected lookups and traversals.
  306. */
  307. static int rhashtable_shrink(struct rhashtable *ht)
  308. {
  309. struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
  310. unsigned int nelems = atomic_read(&ht->nelems);
  311. unsigned int size = 0;
  312. if (nelems)
  313. size = roundup_pow_of_two(nelems * 3 / 2);
  314. if (size < ht->p.min_size)
  315. size = ht->p.min_size;
  316. if (old_tbl->size <= size)
  317. return 0;
  318. if (rht_dereference(old_tbl->future_tbl, ht))
  319. return -EEXIST;
  320. return rhashtable_rehash_alloc(ht, old_tbl, size);
  321. }
  322. static void rht_deferred_worker(struct work_struct *work)
  323. {
  324. struct rhashtable *ht;
  325. struct bucket_table *tbl;
  326. int err = 0;
  327. ht = container_of(work, struct rhashtable, run_work);
  328. mutex_lock(&ht->mutex);
  329. tbl = rht_dereference(ht->tbl, ht);
  330. tbl = rhashtable_last_table(ht, tbl);
  331. if (rht_grow_above_75(ht, tbl))
  332. err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2);
  333. else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl))
  334. err = rhashtable_shrink(ht);
  335. else if (tbl->nest)
  336. err = rhashtable_rehash_alloc(ht, tbl, tbl->size);
  337. if (!err || err == -EEXIST) {
  338. int nerr;
  339. nerr = rhashtable_rehash_table(ht);
  340. err = err ?: nerr;
  341. }
  342. mutex_unlock(&ht->mutex);
  343. if (err)
  344. schedule_work(&ht->run_work);
  345. }
  346. static int rhashtable_insert_rehash(struct rhashtable *ht,
  347. struct bucket_table *tbl)
  348. {
  349. struct bucket_table *old_tbl;
  350. struct bucket_table *new_tbl;
  351. unsigned int size;
  352. int err;
  353. old_tbl = rht_dereference_rcu(ht->tbl, ht);
  354. size = tbl->size;
  355. err = -EBUSY;
  356. if (rht_grow_above_75(ht, tbl))
  357. size *= 2;
  358. /* Do not schedule more than one rehash */
  359. else if (old_tbl != tbl)
  360. goto fail;
  361. err = -ENOMEM;
  362. new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN);
  363. if (new_tbl == NULL)
  364. goto fail;
  365. err = rhashtable_rehash_attach(ht, tbl, new_tbl);
  366. if (err) {
  367. bucket_table_free(new_tbl);
  368. if (err == -EEXIST)
  369. err = 0;
  370. } else
  371. schedule_work(&ht->run_work);
  372. return err;
  373. fail:
  374. /* Do not fail the insert if someone else did a rehash. */
  375. if (likely(rcu_access_pointer(tbl->future_tbl)))
  376. return 0;
  377. /* Schedule async rehash to retry allocation in process context. */
  378. if (err == -ENOMEM)
  379. schedule_work(&ht->run_work);
  380. return err;
  381. }
  382. static void *rhashtable_lookup_one(struct rhashtable *ht,
  383. struct rhash_lock_head __rcu **bkt,
  384. struct bucket_table *tbl, unsigned int hash,
  385. const void *key, struct rhash_head *obj)
  386. {
  387. struct rhashtable_compare_arg arg = {
  388. .ht = ht,
  389. .key = key,
  390. };
  391. struct rhash_head __rcu **pprev = NULL;
  392. struct rhash_head *head;
  393. int elasticity;
  394. elasticity = RHT_ELASTICITY;
  395. rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
  396. struct rhlist_head *list;
  397. struct rhlist_head *plist;
  398. elasticity--;
  399. if (!key ||
  400. (ht->p.obj_cmpfn ?
  401. ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) :
  402. rhashtable_compare(&arg, rht_obj(ht, head)))) {
  403. pprev = &head->next;
  404. continue;
  405. }
  406. if (!ht->rhlist)
  407. return rht_obj(ht, head);
  408. list = container_of(obj, struct rhlist_head, rhead);
  409. plist = container_of(head, struct rhlist_head, rhead);
  410. RCU_INIT_POINTER(list->next, plist);
  411. head = rht_dereference_bucket(head->next, tbl, hash);
  412. RCU_INIT_POINTER(list->rhead.next, head);
  413. if (pprev)
  414. rcu_assign_pointer(*pprev, obj);
  415. else
  416. /* Need to preserve the bit lock */
  417. rht_assign_locked(bkt, obj);
  418. return NULL;
  419. }
  420. if (elasticity <= 0)
  421. return ERR_PTR(-EAGAIN);
  422. return ERR_PTR(-ENOENT);
  423. }
  424. static struct bucket_table *rhashtable_insert_one(
  425. struct rhashtable *ht, struct rhash_lock_head __rcu **bkt,
  426. struct bucket_table *tbl, unsigned int hash, struct rhash_head *obj,
  427. void *data)
  428. {
  429. struct bucket_table *new_tbl;
  430. struct rhash_head *head;
  431. if (!IS_ERR_OR_NULL(data))
  432. return ERR_PTR(-EEXIST);
  433. if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT)
  434. return ERR_CAST(data);
  435. new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  436. if (new_tbl)
  437. return new_tbl;
  438. if (PTR_ERR(data) != -ENOENT)
  439. return ERR_CAST(data);
  440. if (unlikely(rht_grow_above_max(ht, tbl)))
  441. return ERR_PTR(-E2BIG);
  442. if (unlikely(rht_grow_above_100(ht, tbl)))
  443. return ERR_PTR(-EAGAIN);
  444. head = rht_ptr(bkt, tbl, hash);
  445. RCU_INIT_POINTER(obj->next, head);
  446. if (ht->rhlist) {
  447. struct rhlist_head *list;
  448. list = container_of(obj, struct rhlist_head, rhead);
  449. RCU_INIT_POINTER(list->next, NULL);
  450. }
  451. /* bkt is always the head of the list, so it holds
  452. * the lock, which we need to preserve
  453. */
  454. rht_assign_locked(bkt, obj);
  455. atomic_inc(&ht->nelems);
  456. if (rht_grow_above_75(ht, tbl))
  457. schedule_work(&ht->run_work);
  458. return NULL;
  459. }
  460. static void *rhashtable_try_insert(struct rhashtable *ht, const void *key,
  461. struct rhash_head *obj)
  462. {
  463. struct bucket_table *new_tbl;
  464. struct bucket_table *tbl;
  465. struct rhash_lock_head __rcu **bkt;
  466. unsigned int hash;
  467. void *data;
  468. new_tbl = rcu_dereference(ht->tbl);
  469. do {
  470. tbl = new_tbl;
  471. hash = rht_head_hashfn(ht, tbl, obj, ht->p);
  472. if (rcu_access_pointer(tbl->future_tbl))
  473. /* Failure is OK */
  474. bkt = rht_bucket_var(tbl, hash);
  475. else
  476. bkt = rht_bucket_insert(ht, tbl, hash);
  477. if (bkt == NULL) {
  478. new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  479. data = ERR_PTR(-EAGAIN);
  480. } else {
  481. rht_lock(tbl, bkt);
  482. data = rhashtable_lookup_one(ht, bkt, tbl,
  483. hash, key, obj);
  484. new_tbl = rhashtable_insert_one(ht, bkt, tbl,
  485. hash, obj, data);
  486. if (PTR_ERR(new_tbl) != -EEXIST)
  487. data = ERR_CAST(new_tbl);
  488. rht_unlock(tbl, bkt);
  489. }
  490. } while (!IS_ERR_OR_NULL(new_tbl));
  491. if (PTR_ERR(data) == -EAGAIN)
  492. data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?:
  493. -EAGAIN);
  494. return data;
  495. }
  496. void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
  497. struct rhash_head *obj)
  498. {
  499. void *data;
  500. do {
  501. rcu_read_lock();
  502. data = rhashtable_try_insert(ht, key, obj);
  503. rcu_read_unlock();
  504. } while (PTR_ERR(data) == -EAGAIN);
  505. return data;
  506. }
  507. EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
  508. /**
  509. * rhashtable_walk_enter - Initialise an iterator
  510. * @ht: Table to walk over
  511. * @iter: Hash table Iterator
  512. *
  513. * This function prepares a hash table walk.
  514. *
  515. * Note that if you restart a walk after rhashtable_walk_stop you
  516. * may see the same object twice. Also, you may miss objects if
  517. * there are removals in between rhashtable_walk_stop and the next
  518. * call to rhashtable_walk_start.
  519. *
  520. * For a completely stable walk you should construct your own data
  521. * structure outside the hash table.
  522. *
  523. * This function may be called from any process context, including
  524. * non-preemptable context, but cannot be called from softirq or
  525. * hardirq context.
  526. *
  527. * You must call rhashtable_walk_exit after this function returns.
  528. */
  529. void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter)
  530. {
  531. iter->ht = ht;
  532. iter->p = NULL;
  533. iter->slot = 0;
  534. iter->skip = 0;
  535. iter->end_of_table = 0;
  536. spin_lock(&ht->lock);
  537. iter->walker.tbl =
  538. rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock));
  539. list_add(&iter->walker.list, &iter->walker.tbl->walkers);
  540. spin_unlock(&ht->lock);
  541. }
  542. EXPORT_SYMBOL_GPL(rhashtable_walk_enter);
  543. /**
  544. * rhashtable_walk_exit - Free an iterator
  545. * @iter: Hash table Iterator
  546. *
  547. * This function frees resources allocated by rhashtable_walk_enter.
  548. */
  549. void rhashtable_walk_exit(struct rhashtable_iter *iter)
  550. {
  551. spin_lock(&iter->ht->lock);
  552. if (iter->walker.tbl)
  553. list_del(&iter->walker.list);
  554. spin_unlock(&iter->ht->lock);
  555. }
  556. EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
  557. /**
  558. * rhashtable_walk_start_check - Start a hash table walk
  559. * @iter: Hash table iterator
  560. *
  561. * Start a hash table walk at the current iterator position. Note that we take
  562. * the RCU lock in all cases including when we return an error. So you must
  563. * always call rhashtable_walk_stop to clean up.
  564. *
  565. * Returns zero if successful.
  566. *
  567. * Returns -EAGAIN if resize event occured. Note that the iterator
  568. * will rewind back to the beginning and you may use it immediately
  569. * by calling rhashtable_walk_next.
  570. *
  571. * rhashtable_walk_start is defined as an inline variant that returns
  572. * void. This is preferred in cases where the caller would ignore
  573. * resize events and always continue.
  574. */
  575. int rhashtable_walk_start_check(struct rhashtable_iter *iter)
  576. __acquires(RCU)
  577. {
  578. struct rhashtable *ht = iter->ht;
  579. bool rhlist = ht->rhlist;
  580. rcu_read_lock();
  581. spin_lock(&ht->lock);
  582. if (iter->walker.tbl)
  583. list_del(&iter->walker.list);
  584. spin_unlock(&ht->lock);
  585. if (iter->end_of_table)
  586. return 0;
  587. if (!iter->walker.tbl) {
  588. iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht);
  589. iter->slot = 0;
  590. iter->skip = 0;
  591. return -EAGAIN;
  592. }
  593. if (iter->p && !rhlist) {
  594. /*
  595. * We need to validate that 'p' is still in the table, and
  596. * if so, update 'skip'
  597. */
  598. struct rhash_head *p;
  599. int skip = 0;
  600. rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
  601. skip++;
  602. if (p == iter->p) {
  603. iter->skip = skip;
  604. goto found;
  605. }
  606. }
  607. iter->p = NULL;
  608. } else if (iter->p && rhlist) {
  609. /* Need to validate that 'list' is still in the table, and
  610. * if so, update 'skip' and 'p'.
  611. */
  612. struct rhash_head *p;
  613. struct rhlist_head *list;
  614. int skip = 0;
  615. rht_for_each_rcu(p, iter->walker.tbl, iter->slot) {
  616. for (list = container_of(p, struct rhlist_head, rhead);
  617. list;
  618. list = rcu_dereference(list->next)) {
  619. skip++;
  620. if (list == iter->list) {
  621. iter->p = p;
  622. iter->skip = skip;
  623. goto found;
  624. }
  625. }
  626. }
  627. iter->p = NULL;
  628. }
  629. found:
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_GPL(rhashtable_walk_start_check);
  633. /**
  634. * __rhashtable_walk_find_next - Find the next element in a table (or the first
  635. * one in case of a new walk).
  636. *
  637. * @iter: Hash table iterator
  638. *
  639. * Returns the found object or NULL when the end of the table is reached.
  640. *
  641. * Returns -EAGAIN if resize event occurred.
  642. */
  643. static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter)
  644. {
  645. struct bucket_table *tbl = iter->walker.tbl;
  646. struct rhlist_head *list = iter->list;
  647. struct rhashtable *ht = iter->ht;
  648. struct rhash_head *p = iter->p;
  649. bool rhlist = ht->rhlist;
  650. if (!tbl)
  651. return NULL;
  652. for (; iter->slot < tbl->size; iter->slot++) {
  653. int skip = iter->skip;
  654. rht_for_each_rcu(p, tbl, iter->slot) {
  655. if (rhlist) {
  656. list = container_of(p, struct rhlist_head,
  657. rhead);
  658. do {
  659. if (!skip)
  660. goto next;
  661. skip--;
  662. list = rcu_dereference(list->next);
  663. } while (list);
  664. continue;
  665. }
  666. if (!skip)
  667. break;
  668. skip--;
  669. }
  670. next:
  671. if (!rht_is_a_nulls(p)) {
  672. iter->skip++;
  673. iter->p = p;
  674. iter->list = list;
  675. return rht_obj(ht, rhlist ? &list->rhead : p);
  676. }
  677. iter->skip = 0;
  678. }
  679. iter->p = NULL;
  680. /* Ensure we see any new tables. */
  681. smp_rmb();
  682. iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  683. if (iter->walker.tbl) {
  684. iter->slot = 0;
  685. iter->skip = 0;
  686. return ERR_PTR(-EAGAIN);
  687. } else {
  688. iter->end_of_table = true;
  689. }
  690. return NULL;
  691. }
  692. /**
  693. * rhashtable_walk_next - Return the next object and advance the iterator
  694. * @iter: Hash table iterator
  695. *
  696. * Note that you must call rhashtable_walk_stop when you are finished
  697. * with the walk.
  698. *
  699. * Returns the next object or NULL when the end of the table is reached.
  700. *
  701. * Returns -EAGAIN if resize event occurred. Note that the iterator
  702. * will rewind back to the beginning and you may continue to use it.
  703. */
  704. void *rhashtable_walk_next(struct rhashtable_iter *iter)
  705. {
  706. struct rhlist_head *list = iter->list;
  707. struct rhashtable *ht = iter->ht;
  708. struct rhash_head *p = iter->p;
  709. bool rhlist = ht->rhlist;
  710. if (p) {
  711. if (!rhlist || !(list = rcu_dereference(list->next))) {
  712. p = rcu_dereference(p->next);
  713. list = container_of(p, struct rhlist_head, rhead);
  714. }
  715. if (!rht_is_a_nulls(p)) {
  716. iter->skip++;
  717. iter->p = p;
  718. iter->list = list;
  719. return rht_obj(ht, rhlist ? &list->rhead : p);
  720. }
  721. /* At the end of this slot, switch to next one and then find
  722. * next entry from that point.
  723. */
  724. iter->skip = 0;
  725. iter->slot++;
  726. }
  727. return __rhashtable_walk_find_next(iter);
  728. }
  729. EXPORT_SYMBOL_GPL(rhashtable_walk_next);
  730. /**
  731. * rhashtable_walk_peek - Return the next object but don't advance the iterator
  732. * @iter: Hash table iterator
  733. *
  734. * Returns the next object or NULL when the end of the table is reached.
  735. *
  736. * Returns -EAGAIN if resize event occurred. Note that the iterator
  737. * will rewind back to the beginning and you may continue to use it.
  738. */
  739. void *rhashtable_walk_peek(struct rhashtable_iter *iter)
  740. {
  741. struct rhlist_head *list = iter->list;
  742. struct rhashtable *ht = iter->ht;
  743. struct rhash_head *p = iter->p;
  744. if (p)
  745. return rht_obj(ht, ht->rhlist ? &list->rhead : p);
  746. /* No object found in current iter, find next one in the table. */
  747. if (iter->skip) {
  748. /* A nonzero skip value points to the next entry in the table
  749. * beyond that last one that was found. Decrement skip so
  750. * we find the current value. __rhashtable_walk_find_next
  751. * will restore the original value of skip assuming that
  752. * the table hasn't changed.
  753. */
  754. iter->skip--;
  755. }
  756. return __rhashtable_walk_find_next(iter);
  757. }
  758. EXPORT_SYMBOL_GPL(rhashtable_walk_peek);
  759. /**
  760. * rhashtable_walk_stop - Finish a hash table walk
  761. * @iter: Hash table iterator
  762. *
  763. * Finish a hash table walk. Does not reset the iterator to the start of the
  764. * hash table.
  765. */
  766. void rhashtable_walk_stop(struct rhashtable_iter *iter)
  767. __releases(RCU)
  768. {
  769. struct rhashtable *ht;
  770. struct bucket_table *tbl = iter->walker.tbl;
  771. if (!tbl)
  772. goto out;
  773. ht = iter->ht;
  774. spin_lock(&ht->lock);
  775. if (rcu_head_after_call_rcu(&tbl->rcu, bucket_table_free_rcu))
  776. /* This bucket table is being freed, don't re-link it. */
  777. iter->walker.tbl = NULL;
  778. else
  779. list_add(&iter->walker.list, &tbl->walkers);
  780. spin_unlock(&ht->lock);
  781. out:
  782. rcu_read_unlock();
  783. }
  784. EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
  785. static size_t rounded_hashtable_size(const struct rhashtable_params *params)
  786. {
  787. size_t retsize;
  788. if (params->nelem_hint)
  789. retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
  790. (unsigned long)params->min_size);
  791. else
  792. retsize = max(HASH_DEFAULT_SIZE,
  793. (unsigned long)params->min_size);
  794. return retsize;
  795. }
  796. static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed)
  797. {
  798. return jhash2(key, length, seed);
  799. }
  800. /**
  801. * rhashtable_init - initialize a new hash table
  802. * @ht: hash table to be initialized
  803. * @params: configuration parameters
  804. *
  805. * Initializes a new hash table based on the provided configuration
  806. * parameters. A table can be configured either with a variable or
  807. * fixed length key:
  808. *
  809. * Configuration Example 1: Fixed length keys
  810. * struct test_obj {
  811. * int key;
  812. * void * my_member;
  813. * struct rhash_head node;
  814. * };
  815. *
  816. * struct rhashtable_params params = {
  817. * .head_offset = offsetof(struct test_obj, node),
  818. * .key_offset = offsetof(struct test_obj, key),
  819. * .key_len = sizeof(int),
  820. * .hashfn = jhash,
  821. * };
  822. *
  823. * Configuration Example 2: Variable length keys
  824. * struct test_obj {
  825. * [...]
  826. * struct rhash_head node;
  827. * };
  828. *
  829. * u32 my_hash_fn(const void *data, u32 len, u32 seed)
  830. * {
  831. * struct test_obj *obj = data;
  832. *
  833. * return [... hash ...];
  834. * }
  835. *
  836. * struct rhashtable_params params = {
  837. * .head_offset = offsetof(struct test_obj, node),
  838. * .hashfn = jhash,
  839. * .obj_hashfn = my_hash_fn,
  840. * };
  841. */
  842. int rhashtable_init(struct rhashtable *ht,
  843. const struct rhashtable_params *params)
  844. {
  845. struct bucket_table *tbl;
  846. size_t size;
  847. if ((!params->key_len && !params->obj_hashfn) ||
  848. (params->obj_hashfn && !params->obj_cmpfn))
  849. return -EINVAL;
  850. memset(ht, 0, sizeof(*ht));
  851. mutex_init(&ht->mutex);
  852. spin_lock_init(&ht->lock);
  853. memcpy(&ht->p, params, sizeof(*params));
  854. if (params->min_size)
  855. ht->p.min_size = roundup_pow_of_two(params->min_size);
  856. /* Cap total entries at 2^31 to avoid nelems overflow. */
  857. ht->max_elems = 1u << 31;
  858. if (params->max_size) {
  859. ht->p.max_size = rounddown_pow_of_two(params->max_size);
  860. if (ht->p.max_size < ht->max_elems / 2)
  861. ht->max_elems = ht->p.max_size * 2;
  862. }
  863. ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
  864. size = rounded_hashtable_size(&ht->p);
  865. ht->key_len = ht->p.key_len;
  866. if (!params->hashfn) {
  867. ht->p.hashfn = jhash;
  868. if (!(ht->key_len & (sizeof(u32) - 1))) {
  869. ht->key_len /= sizeof(u32);
  870. ht->p.hashfn = rhashtable_jhash2;
  871. }
  872. }
  873. /*
  874. * This is api initialization and thus we need to guarantee the
  875. * initial rhashtable allocation. Upon failure, retry with the
  876. * smallest possible size with __GFP_NOFAIL semantics.
  877. */
  878. tbl = bucket_table_alloc(ht, size, GFP_KERNEL);
  879. if (unlikely(tbl == NULL)) {
  880. size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE);
  881. tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL);
  882. }
  883. atomic_set(&ht->nelems, 0);
  884. RCU_INIT_POINTER(ht->tbl, tbl);
  885. INIT_WORK(&ht->run_work, rht_deferred_worker);
  886. return 0;
  887. }
  888. EXPORT_SYMBOL_GPL(rhashtable_init);
  889. /**
  890. * rhltable_init - initialize a new hash list table
  891. * @hlt: hash list table to be initialized
  892. * @params: configuration parameters
  893. *
  894. * Initializes a new hash list table.
  895. *
  896. * See documentation for rhashtable_init.
  897. */
  898. int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params)
  899. {
  900. int err;
  901. err = rhashtable_init(&hlt->ht, params);
  902. hlt->ht.rhlist = true;
  903. return err;
  904. }
  905. EXPORT_SYMBOL_GPL(rhltable_init);
  906. static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj,
  907. void (*free_fn)(void *ptr, void *arg),
  908. void *arg)
  909. {
  910. struct rhlist_head *list;
  911. if (!ht->rhlist) {
  912. free_fn(rht_obj(ht, obj), arg);
  913. return;
  914. }
  915. list = container_of(obj, struct rhlist_head, rhead);
  916. do {
  917. obj = &list->rhead;
  918. list = rht_dereference(list->next, ht);
  919. free_fn(rht_obj(ht, obj), arg);
  920. } while (list);
  921. }
  922. /**
  923. * rhashtable_free_and_destroy - free elements and destroy hash table
  924. * @ht: the hash table to destroy
  925. * @free_fn: callback to release resources of element
  926. * @arg: pointer passed to free_fn
  927. *
  928. * Stops an eventual async resize. If defined, invokes free_fn for each
  929. * element to releasal resources. Please note that RCU protected
  930. * readers may still be accessing the elements. Releasing of resources
  931. * must occur in a compatible manner. Then frees the bucket array.
  932. *
  933. * This function will eventually sleep to wait for an async resize
  934. * to complete. The caller is responsible that no further write operations
  935. * occurs in parallel.
  936. */
  937. void rhashtable_free_and_destroy(struct rhashtable *ht,
  938. void (*free_fn)(void *ptr, void *arg),
  939. void *arg)
  940. {
  941. struct bucket_table *tbl, *next_tbl;
  942. unsigned int i;
  943. cancel_work_sync(&ht->run_work);
  944. mutex_lock(&ht->mutex);
  945. tbl = rht_dereference(ht->tbl, ht);
  946. restart:
  947. if (free_fn) {
  948. for (i = 0; i < tbl->size; i++) {
  949. struct rhash_head *pos, *next;
  950. cond_resched();
  951. for (pos = rht_ptr_exclusive(rht_bucket(tbl, i)),
  952. next = !rht_is_a_nulls(pos) ?
  953. rht_dereference(pos->next, ht) : NULL;
  954. !rht_is_a_nulls(pos);
  955. pos = next,
  956. next = !rht_is_a_nulls(pos) ?
  957. rht_dereference(pos->next, ht) : NULL)
  958. rhashtable_free_one(ht, pos, free_fn, arg);
  959. }
  960. }
  961. next_tbl = rht_dereference(tbl->future_tbl, ht);
  962. bucket_table_free(tbl);
  963. if (next_tbl) {
  964. tbl = next_tbl;
  965. goto restart;
  966. }
  967. mutex_unlock(&ht->mutex);
  968. }
  969. EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy);
  970. void rhashtable_destroy(struct rhashtable *ht)
  971. {
  972. return rhashtable_free_and_destroy(ht, NULL, NULL);
  973. }
  974. EXPORT_SYMBOL_GPL(rhashtable_destroy);
  975. struct rhash_lock_head __rcu **__rht_bucket_nested(
  976. const struct bucket_table *tbl, unsigned int hash)
  977. {
  978. const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
  979. unsigned int index = hash & ((1 << tbl->nest) - 1);
  980. unsigned int size = tbl->size >> tbl->nest;
  981. unsigned int subhash = hash;
  982. union nested_table *ntbl;
  983. ntbl = nested_table_top(tbl);
  984. ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash);
  985. subhash >>= tbl->nest;
  986. while (ntbl && size > (1 << shift)) {
  987. index = subhash & ((1 << shift) - 1);
  988. ntbl = rht_dereference_bucket_rcu(ntbl[index].table,
  989. tbl, hash);
  990. size >>= shift;
  991. subhash >>= shift;
  992. }
  993. if (!ntbl)
  994. return NULL;
  995. return &ntbl[subhash].bucket;
  996. }
  997. EXPORT_SYMBOL_GPL(__rht_bucket_nested);
  998. struct rhash_lock_head __rcu **rht_bucket_nested(
  999. const struct bucket_table *tbl, unsigned int hash)
  1000. {
  1001. static struct rhash_lock_head __rcu *rhnull;
  1002. if (!rhnull)
  1003. INIT_RHT_NULLS_HEAD(rhnull);
  1004. return __rht_bucket_nested(tbl, hash) ?: &rhnull;
  1005. }
  1006. EXPORT_SYMBOL_GPL(rht_bucket_nested);
  1007. struct rhash_lock_head __rcu **rht_bucket_nested_insert(
  1008. struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
  1009. {
  1010. const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *));
  1011. unsigned int index = hash & ((1 << tbl->nest) - 1);
  1012. unsigned int size = tbl->size >> tbl->nest;
  1013. union nested_table *ntbl;
  1014. ntbl = nested_table_top(tbl);
  1015. hash >>= tbl->nest;
  1016. ntbl = nested_table_alloc(ht, &ntbl[index].table,
  1017. size <= (1 << shift));
  1018. while (ntbl && size > (1 << shift)) {
  1019. index = hash & ((1 << shift) - 1);
  1020. size >>= shift;
  1021. hash >>= shift;
  1022. ntbl = nested_table_alloc(ht, &ntbl[index].table,
  1023. size <= (1 << shift));
  1024. }
  1025. if (!ntbl)
  1026. return NULL;
  1027. return &ntbl[hash].bucket;
  1028. }
  1029. EXPORT_SYMBOL_GPL(rht_bucket_nested_insert);