rhashtable.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Resizable, Scalable, Concurrent Hash Table
  4. *
  5. * Copyright (c) 2015-2016 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. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #ifndef _LINUX_RHASHTABLE_H
  18. #define _LINUX_RHASHTABLE_H
  19. #include <linux/err.h>
  20. #include <linux/errno.h>
  21. #include <linux/jhash.h>
  22. #include <linux/list_nulls.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/rculist.h>
  25. #include <linux/rhashtable-types.h>
  26. /*
  27. * The end of the chain is marked with a special nulls marks which has
  28. * the least significant bit set.
  29. */
  30. /* Maximum chain length before rehash
  31. *
  32. * The maximum (not average) chain length grows with the size of the hash
  33. * table, at a rate of (log N)/(log log N).
  34. *
  35. * The value of 16 is selected so that even if the hash table grew to
  36. * 2^32 you would not expect the maximum chain length to exceed it
  37. * unless we are under attack (or extremely unlucky).
  38. *
  39. * As this limit is only to detect attacks, we don't need to set it to a
  40. * lower value as you'd need the chain length to vastly exceed 16 to have
  41. * any real effect on the system.
  42. */
  43. #define RHT_ELASTICITY 16u
  44. /**
  45. * struct bucket_table - Table of hash buckets
  46. * @size: Number of hash buckets
  47. * @nest: Number of bits of first-level nested table.
  48. * @rehash: Current bucket being rehashed
  49. * @hash_rnd: Random seed to fold into hash
  50. * @locks_mask: Mask to apply before accessing locks[]
  51. * @locks: Array of spinlocks protecting individual buckets
  52. * @walkers: List of active walkers
  53. * @rcu: RCU structure for freeing the table
  54. * @future_tbl: Table under construction during rehashing
  55. * @ntbl: Nested table used when out of memory.
  56. * @buckets: size * hash buckets
  57. */
  58. struct bucket_table {
  59. unsigned int size;
  60. unsigned int nest;
  61. unsigned int rehash;
  62. u32 hash_rnd;
  63. unsigned int locks_mask;
  64. spinlock_t *locks;
  65. struct list_head walkers;
  66. struct rcu_head rcu;
  67. struct bucket_table __rcu *future_tbl;
  68. struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
  69. };
  70. #define INIT_RHT_NULLS_HEAD(ptr) \
  71. ((ptr) = (typeof(ptr)) NULLS_MARKER(0))
  72. static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
  73. {
  74. return ((unsigned long) ptr & 1);
  75. }
  76. static inline void *rht_obj(const struct rhashtable *ht,
  77. const struct rhash_head *he)
  78. {
  79. return (char *)he - ht->p.head_offset;
  80. }
  81. static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
  82. unsigned int hash)
  83. {
  84. return hash & (tbl->size - 1);
  85. }
  86. static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
  87. const void *key, const struct rhashtable_params params,
  88. unsigned int hash_rnd)
  89. {
  90. unsigned int hash;
  91. /* params must be equal to ht->p if it isn't constant. */
  92. if (!__builtin_constant_p(params.key_len))
  93. hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
  94. else if (params.key_len) {
  95. unsigned int key_len = params.key_len;
  96. if (params.hashfn)
  97. hash = params.hashfn(key, key_len, hash_rnd);
  98. else if (key_len & (sizeof(u32) - 1))
  99. hash = jhash(key, key_len, hash_rnd);
  100. else
  101. hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
  102. } else {
  103. unsigned int key_len = ht->p.key_len;
  104. if (params.hashfn)
  105. hash = params.hashfn(key, key_len, hash_rnd);
  106. else
  107. hash = jhash(key, key_len, hash_rnd);
  108. }
  109. return hash;
  110. }
  111. static inline unsigned int rht_key_hashfn(
  112. struct rhashtable *ht, const struct bucket_table *tbl,
  113. const void *key, const struct rhashtable_params params)
  114. {
  115. unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
  116. return rht_bucket_index(tbl, hash);
  117. }
  118. static inline unsigned int rht_head_hashfn(
  119. struct rhashtable *ht, const struct bucket_table *tbl,
  120. const struct rhash_head *he, const struct rhashtable_params params)
  121. {
  122. const char *ptr = rht_obj(ht, he);
  123. return likely(params.obj_hashfn) ?
  124. rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
  125. ht->p.key_len,
  126. tbl->hash_rnd)) :
  127. rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
  128. }
  129. /**
  130. * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
  131. * @ht: hash table
  132. * @tbl: current table
  133. */
  134. static inline bool rht_grow_above_75(const struct rhashtable *ht,
  135. const struct bucket_table *tbl)
  136. {
  137. /* Expand table when exceeding 75% load */
  138. return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
  139. (!ht->p.max_size || tbl->size < ht->p.max_size);
  140. }
  141. /**
  142. * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
  143. * @ht: hash table
  144. * @tbl: current table
  145. */
  146. static inline bool rht_shrink_below_30(const struct rhashtable *ht,
  147. const struct bucket_table *tbl)
  148. {
  149. /* Shrink table beneath 30% load */
  150. return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
  151. tbl->size > ht->p.min_size;
  152. }
  153. /**
  154. * rht_grow_above_100 - returns true if nelems > table-size
  155. * @ht: hash table
  156. * @tbl: current table
  157. */
  158. static inline bool rht_grow_above_100(const struct rhashtable *ht,
  159. const struct bucket_table *tbl)
  160. {
  161. return atomic_read(&ht->nelems) > tbl->size &&
  162. (!ht->p.max_size || tbl->size < ht->p.max_size);
  163. }
  164. /**
  165. * rht_grow_above_max - returns true if table is above maximum
  166. * @ht: hash table
  167. * @tbl: current table
  168. */
  169. static inline bool rht_grow_above_max(const struct rhashtable *ht,
  170. const struct bucket_table *tbl)
  171. {
  172. return atomic_read(&ht->nelems) >= ht->max_elems;
  173. }
  174. /* The bucket lock is selected based on the hash and protects mutations
  175. * on a group of hash buckets.
  176. *
  177. * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
  178. * a single lock always covers both buckets which may both contains
  179. * entries which link to the same bucket of the old table during resizing.
  180. * This allows to simplify the locking as locking the bucket in both
  181. * tables during resize always guarantee protection.
  182. *
  183. * IMPORTANT: When holding the bucket lock of both the old and new table
  184. * during expansions and shrinking, the old bucket lock must always be
  185. * acquired first.
  186. */
  187. static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
  188. unsigned int hash)
  189. {
  190. return &tbl->locks[hash & tbl->locks_mask];
  191. }
  192. #ifdef CONFIG_PROVE_LOCKING
  193. int lockdep_rht_mutex_is_held(struct rhashtable *ht);
  194. int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
  195. #else
  196. static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
  197. {
  198. return 1;
  199. }
  200. static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
  201. u32 hash)
  202. {
  203. return 1;
  204. }
  205. #endif /* CONFIG_PROVE_LOCKING */
  206. void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
  207. struct rhash_head *obj);
  208. void rhashtable_walk_enter(struct rhashtable *ht,
  209. struct rhashtable_iter *iter);
  210. void rhashtable_walk_exit(struct rhashtable_iter *iter);
  211. int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
  212. static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
  213. {
  214. (void)rhashtable_walk_start_check(iter);
  215. }
  216. void *rhashtable_walk_next(struct rhashtable_iter *iter);
  217. void *rhashtable_walk_peek(struct rhashtable_iter *iter);
  218. void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
  219. void rhashtable_free_and_destroy(struct rhashtable *ht,
  220. void (*free_fn)(void *ptr, void *arg),
  221. void *arg);
  222. void rhashtable_destroy(struct rhashtable *ht);
  223. struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
  224. unsigned int hash);
  225. struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
  226. struct bucket_table *tbl,
  227. unsigned int hash);
  228. #define rht_dereference(p, ht) \
  229. rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
  230. #define rht_dereference_rcu(p, ht) \
  231. rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
  232. #define rht_dereference_bucket(p, tbl, hash) \
  233. rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
  234. #define rht_dereference_bucket_rcu(p, tbl, hash) \
  235. rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
  236. #define rht_entry(tpos, pos, member) \
  237. ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
  238. static inline struct rhash_head __rcu *const *rht_bucket(
  239. const struct bucket_table *tbl, unsigned int hash)
  240. {
  241. return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
  242. &tbl->buckets[hash];
  243. }
  244. static inline struct rhash_head __rcu **rht_bucket_var(
  245. struct bucket_table *tbl, unsigned int hash)
  246. {
  247. return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
  248. &tbl->buckets[hash];
  249. }
  250. static inline struct rhash_head __rcu **rht_bucket_insert(
  251. struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
  252. {
  253. return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
  254. &tbl->buckets[hash];
  255. }
  256. /**
  257. * rht_for_each_continue - continue iterating over hash chain
  258. * @pos: the &struct rhash_head to use as a loop cursor.
  259. * @head: the previous &struct rhash_head to continue from
  260. * @tbl: the &struct bucket_table
  261. * @hash: the hash value / bucket index
  262. */
  263. #define rht_for_each_continue(pos, head, tbl, hash) \
  264. for (pos = rht_dereference_bucket(head, tbl, hash); \
  265. !rht_is_a_nulls(pos); \
  266. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  267. /**
  268. * rht_for_each - iterate over hash chain
  269. * @pos: the &struct rhash_head to use as a loop cursor.
  270. * @tbl: the &struct bucket_table
  271. * @hash: the hash value / bucket index
  272. */
  273. #define rht_for_each(pos, tbl, hash) \
  274. rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
  275. /**
  276. * rht_for_each_entry_continue - continue iterating over hash chain
  277. * @tpos: the type * to use as a loop cursor.
  278. * @pos: the &struct rhash_head to use as a loop cursor.
  279. * @head: the previous &struct rhash_head to continue from
  280. * @tbl: the &struct bucket_table
  281. * @hash: the hash value / bucket index
  282. * @member: name of the &struct rhash_head within the hashable struct.
  283. */
  284. #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
  285. for (pos = rht_dereference_bucket(head, tbl, hash); \
  286. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  287. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  288. /**
  289. * rht_for_each_entry - iterate over hash chain of given type
  290. * @tpos: the type * to use as a loop cursor.
  291. * @pos: the &struct rhash_head to use as a loop cursor.
  292. * @tbl: the &struct bucket_table
  293. * @hash: the hash value / bucket index
  294. * @member: name of the &struct rhash_head within the hashable struct.
  295. */
  296. #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
  297. rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
  298. tbl, hash, member)
  299. /**
  300. * rht_for_each_entry_safe - safely iterate over hash chain of given type
  301. * @tpos: the type * to use as a loop cursor.
  302. * @pos: the &struct rhash_head to use as a loop cursor.
  303. * @next: the &struct rhash_head to use as next in loop cursor.
  304. * @tbl: the &struct bucket_table
  305. * @hash: the hash value / bucket index
  306. * @member: name of the &struct rhash_head within the hashable struct.
  307. *
  308. * This hash chain list-traversal primitive allows for the looped code to
  309. * remove the loop cursor from the list.
  310. */
  311. #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
  312. for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
  313. next = !rht_is_a_nulls(pos) ? \
  314. rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
  315. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  316. pos = next, \
  317. next = !rht_is_a_nulls(pos) ? \
  318. rht_dereference_bucket(pos->next, tbl, hash) : NULL)
  319. /**
  320. * rht_for_each_rcu_continue - continue iterating over rcu hash chain
  321. * @pos: the &struct rhash_head to use as a loop cursor.
  322. * @head: the previous &struct rhash_head to continue from
  323. * @tbl: the &struct bucket_table
  324. * @hash: the hash value / bucket index
  325. *
  326. * This hash chain list-traversal primitive may safely run concurrently with
  327. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  328. * traversal is guarded by rcu_read_lock().
  329. */
  330. #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
  331. for (({barrier(); }), \
  332. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  333. !rht_is_a_nulls(pos); \
  334. pos = rcu_dereference_raw(pos->next))
  335. /**
  336. * rht_for_each_rcu - iterate over rcu hash chain
  337. * @pos: the &struct rhash_head to use as a loop cursor.
  338. * @tbl: the &struct bucket_table
  339. * @hash: the hash value / bucket index
  340. *
  341. * This hash chain list-traversal primitive may safely run concurrently with
  342. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  343. * traversal is guarded by rcu_read_lock().
  344. */
  345. #define rht_for_each_rcu(pos, tbl, hash) \
  346. rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
  347. /**
  348. * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
  349. * @tpos: the type * to use as a loop cursor.
  350. * @pos: the &struct rhash_head to use as a loop cursor.
  351. * @head: the previous &struct rhash_head to continue from
  352. * @tbl: the &struct bucket_table
  353. * @hash: the hash value / bucket index
  354. * @member: name of the &struct rhash_head within the hashable struct.
  355. *
  356. * This hash chain list-traversal primitive may safely run concurrently with
  357. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  358. * traversal is guarded by rcu_read_lock().
  359. */
  360. #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
  361. for (({barrier(); }), \
  362. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  363. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  364. pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
  365. /**
  366. * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
  367. * @tpos: the type * to use as a loop cursor.
  368. * @pos: the &struct rhash_head to use as a loop cursor.
  369. * @tbl: the &struct bucket_table
  370. * @hash: the hash value / bucket index
  371. * @member: name of the &struct rhash_head within the hashable struct.
  372. *
  373. * This hash chain list-traversal primitive may safely run concurrently with
  374. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  375. * traversal is guarded by rcu_read_lock().
  376. */
  377. #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
  378. rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
  379. tbl, hash, member)
  380. /**
  381. * rhl_for_each_rcu - iterate over rcu hash table list
  382. * @pos: the &struct rlist_head to use as a loop cursor.
  383. * @list: the head of the list
  384. *
  385. * This hash chain list-traversal primitive should be used on the
  386. * list returned by rhltable_lookup.
  387. */
  388. #define rhl_for_each_rcu(pos, list) \
  389. for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
  390. /**
  391. * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
  392. * @tpos: the type * to use as a loop cursor.
  393. * @pos: the &struct rlist_head to use as a loop cursor.
  394. * @list: the head of the list
  395. * @member: name of the &struct rlist_head within the hashable struct.
  396. *
  397. * This hash chain list-traversal primitive should be used on the
  398. * list returned by rhltable_lookup.
  399. */
  400. #define rhl_for_each_entry_rcu(tpos, pos, list, member) \
  401. for (pos = list; pos && rht_entry(tpos, pos, member); \
  402. pos = rcu_dereference_raw(pos->next))
  403. static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
  404. const void *obj)
  405. {
  406. struct rhashtable *ht = arg->ht;
  407. const char *ptr = obj;
  408. return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
  409. }
  410. /* Internal function, do not use. */
  411. static inline struct rhash_head *__rhashtable_lookup(
  412. struct rhashtable *ht, const void *key,
  413. const struct rhashtable_params params)
  414. {
  415. struct rhashtable_compare_arg arg = {
  416. .ht = ht,
  417. .key = key,
  418. };
  419. struct bucket_table *tbl;
  420. struct rhash_head *he;
  421. unsigned int hash;
  422. tbl = rht_dereference_rcu(ht->tbl, ht);
  423. restart:
  424. hash = rht_key_hashfn(ht, tbl, key, params);
  425. rht_for_each_rcu(he, tbl, hash) {
  426. if (params.obj_cmpfn ?
  427. params.obj_cmpfn(&arg, rht_obj(ht, he)) :
  428. rhashtable_compare(&arg, rht_obj(ht, he)))
  429. continue;
  430. return he;
  431. }
  432. /* Ensure we see any new tables. */
  433. smp_rmb();
  434. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  435. if (unlikely(tbl))
  436. goto restart;
  437. return NULL;
  438. }
  439. /**
  440. * rhashtable_lookup - search hash table
  441. * @ht: hash table
  442. * @key: the pointer to the key
  443. * @params: hash table parameters
  444. *
  445. * Computes the hash value for the key and traverses the bucket chain looking
  446. * for a entry with an identical key. The first matching entry is returned.
  447. *
  448. * This must only be called under the RCU read lock.
  449. *
  450. * Returns the first entry on which the compare function returned true.
  451. */
  452. static inline void *rhashtable_lookup(
  453. struct rhashtable *ht, const void *key,
  454. const struct rhashtable_params params)
  455. {
  456. struct rhash_head *he = __rhashtable_lookup(ht, key, params);
  457. return he ? rht_obj(ht, he) : NULL;
  458. }
  459. /**
  460. * rhashtable_lookup_fast - search hash table, without RCU read lock
  461. * @ht: hash table
  462. * @key: the pointer to the key
  463. * @params: hash table parameters
  464. *
  465. * Computes the hash value for the key and traverses the bucket chain looking
  466. * for a entry with an identical key. The first matching entry is returned.
  467. *
  468. * Only use this function when you have other mechanisms guaranteeing
  469. * that the object won't go away after the RCU read lock is released.
  470. *
  471. * Returns the first entry on which the compare function returned true.
  472. */
  473. static inline void *rhashtable_lookup_fast(
  474. struct rhashtable *ht, const void *key,
  475. const struct rhashtable_params params)
  476. {
  477. void *obj;
  478. rcu_read_lock();
  479. obj = rhashtable_lookup(ht, key, params);
  480. rcu_read_unlock();
  481. return obj;
  482. }
  483. /**
  484. * rhltable_lookup - search hash list table
  485. * @hlt: hash table
  486. * @key: the pointer to the key
  487. * @params: hash table parameters
  488. *
  489. * Computes the hash value for the key and traverses the bucket chain looking
  490. * for a entry with an identical key. All matching entries are returned
  491. * in a list.
  492. *
  493. * This must only be called under the RCU read lock.
  494. *
  495. * Returns the list of entries that match the given key.
  496. */
  497. static inline struct rhlist_head *rhltable_lookup(
  498. struct rhltable *hlt, const void *key,
  499. const struct rhashtable_params params)
  500. {
  501. struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
  502. return he ? container_of(he, struct rhlist_head, rhead) : NULL;
  503. }
  504. /* Internal function, please use rhashtable_insert_fast() instead. This
  505. * function returns the existing element already in hashes in there is a clash,
  506. * otherwise it returns an error via ERR_PTR().
  507. */
  508. static inline void *__rhashtable_insert_fast(
  509. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  510. const struct rhashtable_params params, bool rhlist)
  511. {
  512. struct rhashtable_compare_arg arg = {
  513. .ht = ht,
  514. .key = key,
  515. };
  516. struct rhash_head __rcu **pprev;
  517. struct bucket_table *tbl;
  518. struct rhash_head *head;
  519. spinlock_t *lock;
  520. unsigned int hash;
  521. int elasticity;
  522. void *data;
  523. rcu_read_lock();
  524. tbl = rht_dereference_rcu(ht->tbl, ht);
  525. hash = rht_head_hashfn(ht, tbl, obj, params);
  526. lock = rht_bucket_lock(tbl, hash);
  527. spin_lock_bh(lock);
  528. if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
  529. slow_path:
  530. spin_unlock_bh(lock);
  531. rcu_read_unlock();
  532. return rhashtable_insert_slow(ht, key, obj);
  533. }
  534. elasticity = RHT_ELASTICITY;
  535. pprev = rht_bucket_insert(ht, tbl, hash);
  536. data = ERR_PTR(-ENOMEM);
  537. if (!pprev)
  538. goto out;
  539. rht_for_each_continue(head, *pprev, tbl, hash) {
  540. struct rhlist_head *plist;
  541. struct rhlist_head *list;
  542. elasticity--;
  543. if (!key ||
  544. (params.obj_cmpfn ?
  545. params.obj_cmpfn(&arg, rht_obj(ht, head)) :
  546. rhashtable_compare(&arg, rht_obj(ht, head)))) {
  547. pprev = &head->next;
  548. continue;
  549. }
  550. data = rht_obj(ht, head);
  551. if (!rhlist)
  552. goto out;
  553. list = container_of(obj, struct rhlist_head, rhead);
  554. plist = container_of(head, struct rhlist_head, rhead);
  555. RCU_INIT_POINTER(list->next, plist);
  556. head = rht_dereference_bucket(head->next, tbl, hash);
  557. RCU_INIT_POINTER(list->rhead.next, head);
  558. rcu_assign_pointer(*pprev, obj);
  559. goto good;
  560. }
  561. if (elasticity <= 0)
  562. goto slow_path;
  563. data = ERR_PTR(-E2BIG);
  564. if (unlikely(rht_grow_above_max(ht, tbl)))
  565. goto out;
  566. if (unlikely(rht_grow_above_100(ht, tbl)))
  567. goto slow_path;
  568. head = rht_dereference_bucket(*pprev, tbl, hash);
  569. RCU_INIT_POINTER(obj->next, head);
  570. if (rhlist) {
  571. struct rhlist_head *list;
  572. list = container_of(obj, struct rhlist_head, rhead);
  573. RCU_INIT_POINTER(list->next, NULL);
  574. }
  575. rcu_assign_pointer(*pprev, obj);
  576. atomic_inc(&ht->nelems);
  577. if (rht_grow_above_75(ht, tbl))
  578. schedule_work(&ht->run_work);
  579. good:
  580. data = NULL;
  581. out:
  582. spin_unlock_bh(lock);
  583. rcu_read_unlock();
  584. return data;
  585. }
  586. /**
  587. * rhashtable_insert_fast - insert object into hash table
  588. * @ht: hash table
  589. * @obj: pointer to hash head inside object
  590. * @params: hash table parameters
  591. *
  592. * Will take a per bucket spinlock to protect against mutual mutations
  593. * on the same bucket. Multiple insertions may occur in parallel unless
  594. * they map to the same bucket lock.
  595. *
  596. * It is safe to call this function from atomic context.
  597. *
  598. * Will trigger an automatic deferred table resizing if residency in the
  599. * table grows beyond 70%.
  600. */
  601. static inline int rhashtable_insert_fast(
  602. struct rhashtable *ht, struct rhash_head *obj,
  603. const struct rhashtable_params params)
  604. {
  605. void *ret;
  606. ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
  607. if (IS_ERR(ret))
  608. return PTR_ERR(ret);
  609. return ret == NULL ? 0 : -EEXIST;
  610. }
  611. /**
  612. * rhltable_insert_key - insert object into hash list table
  613. * @hlt: hash list table
  614. * @key: the pointer to the key
  615. * @list: pointer to hash list head inside object
  616. * @params: hash table parameters
  617. *
  618. * Will take a per bucket spinlock to protect against mutual mutations
  619. * on the same bucket. Multiple insertions may occur in parallel unless
  620. * they map to the same bucket lock.
  621. *
  622. * It is safe to call this function from atomic context.
  623. *
  624. * Will trigger an automatic deferred table resizing if residency in the
  625. * table grows beyond 70%.
  626. */
  627. static inline int rhltable_insert_key(
  628. struct rhltable *hlt, const void *key, struct rhlist_head *list,
  629. const struct rhashtable_params params)
  630. {
  631. return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
  632. params, true));
  633. }
  634. /**
  635. * rhltable_insert - insert object into hash list table
  636. * @hlt: hash list table
  637. * @list: pointer to hash list head inside object
  638. * @params: hash table parameters
  639. *
  640. * Will take a per bucket spinlock to protect against mutual mutations
  641. * on the same bucket. Multiple insertions may occur in parallel unless
  642. * they map to the same bucket lock.
  643. *
  644. * It is safe to call this function from atomic context.
  645. *
  646. * Will trigger an automatic deferred table resizing if residency in the
  647. * table grows beyond 70%.
  648. */
  649. static inline int rhltable_insert(
  650. struct rhltable *hlt, struct rhlist_head *list,
  651. const struct rhashtable_params params)
  652. {
  653. const char *key = rht_obj(&hlt->ht, &list->rhead);
  654. key += params.key_offset;
  655. return rhltable_insert_key(hlt, key, list, params);
  656. }
  657. /**
  658. * rhashtable_lookup_insert_fast - lookup and insert object into hash table
  659. * @ht: hash table
  660. * @obj: pointer to hash head inside object
  661. * @params: hash table parameters
  662. *
  663. * Locks down the bucket chain in both the old and new table if a resize
  664. * is in progress to ensure that writers can't remove from the old table
  665. * and can't insert to the new table during the atomic operation of search
  666. * and insertion. Searches for duplicates in both the old and new table if
  667. * a resize is in progress.
  668. *
  669. * This lookup function may only be used for fixed key hash table (key_len
  670. * parameter set). It will BUG() if used inappropriately.
  671. *
  672. * It is safe to call this function from atomic context.
  673. *
  674. * Will trigger an automatic deferred table resizing if residency in the
  675. * table grows beyond 70%.
  676. */
  677. static inline int rhashtable_lookup_insert_fast(
  678. struct rhashtable *ht, struct rhash_head *obj,
  679. const struct rhashtable_params params)
  680. {
  681. const char *key = rht_obj(ht, obj);
  682. void *ret;
  683. BUG_ON(ht->p.obj_hashfn);
  684. ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
  685. false);
  686. if (IS_ERR(ret))
  687. return PTR_ERR(ret);
  688. return ret == NULL ? 0 : -EEXIST;
  689. }
  690. /**
  691. * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
  692. * @ht: hash table
  693. * @obj: pointer to hash head inside object
  694. * @params: hash table parameters
  695. *
  696. * Just like rhashtable_lookup_insert_fast(), but this function returns the
  697. * object if it exists, NULL if it did not and the insertion was successful,
  698. * and an ERR_PTR otherwise.
  699. */
  700. static inline void *rhashtable_lookup_get_insert_fast(
  701. struct rhashtable *ht, struct rhash_head *obj,
  702. const struct rhashtable_params params)
  703. {
  704. const char *key = rht_obj(ht, obj);
  705. BUG_ON(ht->p.obj_hashfn);
  706. return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
  707. false);
  708. }
  709. /**
  710. * rhashtable_lookup_insert_key - search and insert object to hash table
  711. * with explicit key
  712. * @ht: hash table
  713. * @key: key
  714. * @obj: pointer to hash head inside object
  715. * @params: hash table parameters
  716. *
  717. * Locks down the bucket chain in both the old and new table if a resize
  718. * is in progress to ensure that writers can't remove from the old table
  719. * and can't insert to the new table during the atomic operation of search
  720. * and insertion. Searches for duplicates in both the old and new table if
  721. * a resize is in progress.
  722. *
  723. * Lookups may occur in parallel with hashtable mutations and resizing.
  724. *
  725. * Will trigger an automatic deferred table resizing if residency in the
  726. * table grows beyond 70%.
  727. *
  728. * Returns zero on success.
  729. */
  730. static inline int rhashtable_lookup_insert_key(
  731. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  732. const struct rhashtable_params params)
  733. {
  734. void *ret;
  735. BUG_ON(!ht->p.obj_hashfn || !key);
  736. ret = __rhashtable_insert_fast(ht, key, obj, params, false);
  737. if (IS_ERR(ret))
  738. return PTR_ERR(ret);
  739. return ret == NULL ? 0 : -EEXIST;
  740. }
  741. /**
  742. * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
  743. * @ht: hash table
  744. * @obj: pointer to hash head inside object
  745. * @params: hash table parameters
  746. * @data: pointer to element data already in hashes
  747. *
  748. * Just like rhashtable_lookup_insert_key(), but this function returns the
  749. * object if it exists, NULL if it does not and the insertion was successful,
  750. * and an ERR_PTR otherwise.
  751. */
  752. static inline void *rhashtable_lookup_get_insert_key(
  753. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  754. const struct rhashtable_params params)
  755. {
  756. BUG_ON(!ht->p.obj_hashfn || !key);
  757. return __rhashtable_insert_fast(ht, key, obj, params, false);
  758. }
  759. /* Internal function, please use rhashtable_remove_fast() instead */
  760. static inline int __rhashtable_remove_fast_one(
  761. struct rhashtable *ht, struct bucket_table *tbl,
  762. struct rhash_head *obj, const struct rhashtable_params params,
  763. bool rhlist)
  764. {
  765. struct rhash_head __rcu **pprev;
  766. struct rhash_head *he;
  767. spinlock_t * lock;
  768. unsigned int hash;
  769. int err = -ENOENT;
  770. hash = rht_head_hashfn(ht, tbl, obj, params);
  771. lock = rht_bucket_lock(tbl, hash);
  772. spin_lock_bh(lock);
  773. pprev = rht_bucket_var(tbl, hash);
  774. rht_for_each_continue(he, *pprev, tbl, hash) {
  775. struct rhlist_head *list;
  776. list = container_of(he, struct rhlist_head, rhead);
  777. if (he != obj) {
  778. struct rhlist_head __rcu **lpprev;
  779. pprev = &he->next;
  780. if (!rhlist)
  781. continue;
  782. do {
  783. lpprev = &list->next;
  784. list = rht_dereference_bucket(list->next,
  785. tbl, hash);
  786. } while (list && obj != &list->rhead);
  787. if (!list)
  788. continue;
  789. list = rht_dereference_bucket(list->next, tbl, hash);
  790. RCU_INIT_POINTER(*lpprev, list);
  791. err = 0;
  792. break;
  793. }
  794. obj = rht_dereference_bucket(obj->next, tbl, hash);
  795. err = 1;
  796. if (rhlist) {
  797. list = rht_dereference_bucket(list->next, tbl, hash);
  798. if (list) {
  799. RCU_INIT_POINTER(list->rhead.next, obj);
  800. obj = &list->rhead;
  801. err = 0;
  802. }
  803. }
  804. rcu_assign_pointer(*pprev, obj);
  805. break;
  806. }
  807. spin_unlock_bh(lock);
  808. if (err > 0) {
  809. atomic_dec(&ht->nelems);
  810. if (unlikely(ht->p.automatic_shrinking &&
  811. rht_shrink_below_30(ht, tbl)))
  812. schedule_work(&ht->run_work);
  813. err = 0;
  814. }
  815. return err;
  816. }
  817. /* Internal function, please use rhashtable_remove_fast() instead */
  818. static inline int __rhashtable_remove_fast(
  819. struct rhashtable *ht, struct rhash_head *obj,
  820. const struct rhashtable_params params, bool rhlist)
  821. {
  822. struct bucket_table *tbl;
  823. int err;
  824. rcu_read_lock();
  825. tbl = rht_dereference_rcu(ht->tbl, ht);
  826. /* Because we have already taken (and released) the bucket
  827. * lock in old_tbl, if we find that future_tbl is not yet
  828. * visible then that guarantees the entry to still be in
  829. * the old tbl if it exists.
  830. */
  831. while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
  832. rhlist)) &&
  833. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  834. ;
  835. rcu_read_unlock();
  836. return err;
  837. }
  838. /**
  839. * rhashtable_remove_fast - remove object from hash table
  840. * @ht: hash table
  841. * @obj: pointer to hash head inside object
  842. * @params: hash table parameters
  843. *
  844. * Since the hash chain is single linked, the removal operation needs to
  845. * walk the bucket chain upon removal. The removal operation is thus
  846. * considerable slow if the hash table is not correctly sized.
  847. *
  848. * Will automatically shrink the table if permitted when residency drops
  849. * below 30%.
  850. *
  851. * Returns zero on success, -ENOENT if the entry could not be found.
  852. */
  853. static inline int rhashtable_remove_fast(
  854. struct rhashtable *ht, struct rhash_head *obj,
  855. const struct rhashtable_params params)
  856. {
  857. return __rhashtable_remove_fast(ht, obj, params, false);
  858. }
  859. /**
  860. * rhltable_remove - remove object from hash list table
  861. * @hlt: hash list table
  862. * @list: pointer to hash list head inside object
  863. * @params: hash table parameters
  864. *
  865. * Since the hash chain is single linked, the removal operation needs to
  866. * walk the bucket chain upon removal. The removal operation is thus
  867. * considerable slow if the hash table is not correctly sized.
  868. *
  869. * Will automatically shrink the table if permitted when residency drops
  870. * below 30%
  871. *
  872. * Returns zero on success, -ENOENT if the entry could not be found.
  873. */
  874. static inline int rhltable_remove(
  875. struct rhltable *hlt, struct rhlist_head *list,
  876. const struct rhashtable_params params)
  877. {
  878. return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
  879. }
  880. /* Internal function, please use rhashtable_replace_fast() instead */
  881. static inline int __rhashtable_replace_fast(
  882. struct rhashtable *ht, struct bucket_table *tbl,
  883. struct rhash_head *obj_old, struct rhash_head *obj_new,
  884. const struct rhashtable_params params)
  885. {
  886. struct rhash_head __rcu **pprev;
  887. struct rhash_head *he;
  888. spinlock_t *lock;
  889. unsigned int hash;
  890. int err = -ENOENT;
  891. /* Minimally, the old and new objects must have same hash
  892. * (which should mean identifiers are the same).
  893. */
  894. hash = rht_head_hashfn(ht, tbl, obj_old, params);
  895. if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
  896. return -EINVAL;
  897. lock = rht_bucket_lock(tbl, hash);
  898. spin_lock_bh(lock);
  899. pprev = rht_bucket_var(tbl, hash);
  900. rht_for_each_continue(he, *pprev, tbl, hash) {
  901. if (he != obj_old) {
  902. pprev = &he->next;
  903. continue;
  904. }
  905. rcu_assign_pointer(obj_new->next, obj_old->next);
  906. rcu_assign_pointer(*pprev, obj_new);
  907. err = 0;
  908. break;
  909. }
  910. spin_unlock_bh(lock);
  911. return err;
  912. }
  913. /**
  914. * rhashtable_replace_fast - replace an object in hash table
  915. * @ht: hash table
  916. * @obj_old: pointer to hash head inside object being replaced
  917. * @obj_new: pointer to hash head inside object which is new
  918. * @params: hash table parameters
  919. *
  920. * Replacing an object doesn't affect the number of elements in the hash table
  921. * or bucket, so we don't need to worry about shrinking or expanding the
  922. * table here.
  923. *
  924. * Returns zero on success, -ENOENT if the entry could not be found,
  925. * -EINVAL if hash is not the same for the old and new objects.
  926. */
  927. static inline int rhashtable_replace_fast(
  928. struct rhashtable *ht, struct rhash_head *obj_old,
  929. struct rhash_head *obj_new,
  930. const struct rhashtable_params params)
  931. {
  932. struct bucket_table *tbl;
  933. int err;
  934. rcu_read_lock();
  935. tbl = rht_dereference_rcu(ht->tbl, ht);
  936. /* Because we have already taken (and released) the bucket
  937. * lock in old_tbl, if we find that future_tbl is not yet
  938. * visible then that guarantees the entry to still be in
  939. * the old tbl if it exists.
  940. */
  941. while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
  942. obj_new, params)) &&
  943. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  944. ;
  945. rcu_read_unlock();
  946. return err;
  947. }
  948. /* Obsolete function, do not use in new code. */
  949. static inline int rhashtable_walk_init(struct rhashtable *ht,
  950. struct rhashtable_iter *iter, gfp_t gfp)
  951. {
  952. rhashtable_walk_enter(ht, iter);
  953. return 0;
  954. }
  955. /**
  956. * rhltable_walk_enter - Initialise an iterator
  957. * @hlt: Table to walk over
  958. * @iter: Hash table Iterator
  959. *
  960. * This function prepares a hash table walk.
  961. *
  962. * Note that if you restart a walk after rhashtable_walk_stop you
  963. * may see the same object twice. Also, you may miss objects if
  964. * there are removals in between rhashtable_walk_stop and the next
  965. * call to rhashtable_walk_start.
  966. *
  967. * For a completely stable walk you should construct your own data
  968. * structure outside the hash table.
  969. *
  970. * This function may be called from any process context, including
  971. * non-preemptable context, but cannot be called from softirq or
  972. * hardirq context.
  973. *
  974. * You must call rhashtable_walk_exit after this function returns.
  975. */
  976. static inline void rhltable_walk_enter(struct rhltable *hlt,
  977. struct rhashtable_iter *iter)
  978. {
  979. return rhashtable_walk_enter(&hlt->ht, iter);
  980. }
  981. /**
  982. * rhltable_free_and_destroy - free elements and destroy hash list table
  983. * @hlt: the hash list table to destroy
  984. * @free_fn: callback to release resources of element
  985. * @arg: pointer passed to free_fn
  986. *
  987. * See documentation for rhashtable_free_and_destroy.
  988. */
  989. static inline void rhltable_free_and_destroy(struct rhltable *hlt,
  990. void (*free_fn)(void *ptr,
  991. void *arg),
  992. void *arg)
  993. {
  994. return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
  995. }
  996. static inline void rhltable_destroy(struct rhltable *hlt)
  997. {
  998. return rhltable_free_and_destroy(hlt, NULL, NULL);
  999. }
  1000. #endif /* _LINUX_RHASHTABLE_H */