rhashtable.c 29 KB

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