rhashtable.c 30 KB

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