rhashtable.h 36 KB

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