rhashtable.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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. #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. /**
  51. * struct bucket_table - Table of hash buckets
  52. * @size: Number of hash buckets
  53. * @rehash: Current bucket being rehashed
  54. * @hash_rnd: Random seed to fold into hash
  55. * @locks_mask: Mask to apply before accessing locks[]
  56. * @locks: Array of spinlocks protecting individual buckets
  57. * @walkers: List of active walkers
  58. * @rcu: RCU structure for freeing the table
  59. * @future_tbl: Table under construction during rehashing
  60. * @buckets: size * hash buckets
  61. */
  62. struct bucket_table {
  63. unsigned int size;
  64. unsigned int rehash;
  65. u32 hash_rnd;
  66. unsigned int locks_mask;
  67. spinlock_t *locks;
  68. struct list_head walkers;
  69. struct rcu_head rcu;
  70. struct bucket_table __rcu *future_tbl;
  71. struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
  72. };
  73. /**
  74. * struct rhashtable_compare_arg - Key for the function rhashtable_compare
  75. * @ht: Hash table
  76. * @key: Key to compare against
  77. */
  78. struct rhashtable_compare_arg {
  79. struct rhashtable *ht;
  80. const void *key;
  81. };
  82. typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
  83. typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
  84. typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
  85. const void *obj);
  86. struct rhashtable;
  87. /**
  88. * struct rhashtable_params - Hash table construction parameters
  89. * @nelem_hint: Hint on number of elements, should be 75% of desired size
  90. * @key_len: Length of key
  91. * @key_offset: Offset of key in struct to be hashed
  92. * @head_offset: Offset of rhash_head in struct to be hashed
  93. * @insecure_max_entries: Maximum number of entries (may be exceeded)
  94. * @max_size: Maximum size while expanding
  95. * @min_size: Minimum size while shrinking
  96. * @nulls_base: Base value to generate nulls marker
  97. * @insecure_elasticity: Set to true to disable chain length checks
  98. * @automatic_shrinking: Enable automatic shrinking of tables
  99. * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
  100. * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
  101. * @obj_hashfn: Function to hash object
  102. * @obj_cmpfn: Function to compare key with object
  103. */
  104. struct rhashtable_params {
  105. size_t nelem_hint;
  106. size_t key_len;
  107. size_t key_offset;
  108. size_t head_offset;
  109. unsigned int insecure_max_entries;
  110. unsigned int max_size;
  111. unsigned int min_size;
  112. u32 nulls_base;
  113. bool insecure_elasticity;
  114. bool automatic_shrinking;
  115. size_t locks_mul;
  116. rht_hashfn_t hashfn;
  117. rht_obj_hashfn_t obj_hashfn;
  118. rht_obj_cmpfn_t obj_cmpfn;
  119. };
  120. /**
  121. * struct rhashtable - Hash table handle
  122. * @tbl: Bucket table
  123. * @key_len: Key length for hashfn
  124. * @elasticity: Maximum chain length before rehash
  125. * @p: Configuration parameters
  126. * @run_work: Deferred worker to expand/shrink asynchronously
  127. * @mutex: Mutex to protect current/future table swapping
  128. * @lock: Spin lock to protect walker list
  129. * @nelems: Number of elements in table
  130. */
  131. struct rhashtable {
  132. struct bucket_table __rcu *tbl;
  133. unsigned int key_len;
  134. unsigned int elasticity;
  135. struct rhashtable_params p;
  136. struct work_struct run_work;
  137. struct mutex mutex;
  138. spinlock_t lock;
  139. atomic_t nelems;
  140. };
  141. /**
  142. * struct rhashtable_walker - Hash table walker
  143. * @list: List entry on list of walkers
  144. * @tbl: The table that we were walking over
  145. */
  146. struct rhashtable_walker {
  147. struct list_head list;
  148. struct bucket_table *tbl;
  149. };
  150. /**
  151. * struct rhashtable_iter - Hash table iterator, fits into netlink cb
  152. * @ht: Table to iterate through
  153. * @p: Current pointer
  154. * @walker: Associated rhashtable walker
  155. * @slot: Current slot
  156. * @skip: Number of entries to skip in slot
  157. */
  158. struct rhashtable_iter {
  159. struct rhashtable *ht;
  160. struct rhash_head *p;
  161. struct rhashtable_walker *walker;
  162. unsigned int slot;
  163. unsigned int skip;
  164. };
  165. static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
  166. {
  167. return NULLS_MARKER(ht->p.nulls_base + hash);
  168. }
  169. #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
  170. ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
  171. static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
  172. {
  173. return ((unsigned long) ptr & 1);
  174. }
  175. static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
  176. {
  177. return ((unsigned long) ptr) >> 1;
  178. }
  179. static inline void *rht_obj(const struct rhashtable *ht,
  180. const struct rhash_head *he)
  181. {
  182. return (char *)he - ht->p.head_offset;
  183. }
  184. static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
  185. unsigned int hash)
  186. {
  187. return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
  188. }
  189. static inline unsigned int rht_key_hashfn(
  190. struct rhashtable *ht, const struct bucket_table *tbl,
  191. const void *key, const struct rhashtable_params params)
  192. {
  193. unsigned int hash;
  194. /* params must be equal to ht->p if it isn't constant. */
  195. if (!__builtin_constant_p(params.key_len))
  196. hash = ht->p.hashfn(key, ht->key_len, tbl->hash_rnd);
  197. else if (params.key_len) {
  198. unsigned int key_len = params.key_len;
  199. if (params.hashfn)
  200. hash = params.hashfn(key, key_len, tbl->hash_rnd);
  201. else if (key_len & (sizeof(u32) - 1))
  202. hash = jhash(key, key_len, tbl->hash_rnd);
  203. else
  204. hash = jhash2(key, key_len / sizeof(u32),
  205. tbl->hash_rnd);
  206. } else {
  207. unsigned int key_len = ht->p.key_len;
  208. if (params.hashfn)
  209. hash = params.hashfn(key, key_len, tbl->hash_rnd);
  210. else
  211. hash = jhash(key, key_len, tbl->hash_rnd);
  212. }
  213. return rht_bucket_index(tbl, hash);
  214. }
  215. static inline unsigned int rht_head_hashfn(
  216. struct rhashtable *ht, const struct bucket_table *tbl,
  217. const struct rhash_head *he, const struct rhashtable_params params)
  218. {
  219. const char *ptr = rht_obj(ht, he);
  220. return likely(params.obj_hashfn) ?
  221. rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
  222. ht->p.key_len,
  223. tbl->hash_rnd)) :
  224. rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
  225. }
  226. /**
  227. * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
  228. * @ht: hash table
  229. * @tbl: current table
  230. */
  231. static inline bool rht_grow_above_75(const struct rhashtable *ht,
  232. const struct bucket_table *tbl)
  233. {
  234. /* Expand table when exceeding 75% load */
  235. return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
  236. (!ht->p.max_size || tbl->size < ht->p.max_size);
  237. }
  238. /**
  239. * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
  240. * @ht: hash table
  241. * @tbl: current table
  242. */
  243. static inline bool rht_shrink_below_30(const struct rhashtable *ht,
  244. const struct bucket_table *tbl)
  245. {
  246. /* Shrink table beneath 30% load */
  247. return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
  248. tbl->size > ht->p.min_size;
  249. }
  250. /**
  251. * rht_grow_above_100 - returns true if nelems > table-size
  252. * @ht: hash table
  253. * @tbl: current table
  254. */
  255. static inline bool rht_grow_above_100(const struct rhashtable *ht,
  256. const struct bucket_table *tbl)
  257. {
  258. return atomic_read(&ht->nelems) > tbl->size &&
  259. (!ht->p.max_size || tbl->size < ht->p.max_size);
  260. }
  261. /**
  262. * rht_grow_above_max - returns true if table is above maximum
  263. * @ht: hash table
  264. * @tbl: current table
  265. */
  266. static inline bool rht_grow_above_max(const struct rhashtable *ht,
  267. const struct bucket_table *tbl)
  268. {
  269. return ht->p.insecure_max_entries &&
  270. atomic_read(&ht->nelems) >= ht->p.insecure_max_entries;
  271. }
  272. /* The bucket lock is selected based on the hash and protects mutations
  273. * on a group of hash buckets.
  274. *
  275. * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
  276. * a single lock always covers both buckets which may both contains
  277. * entries which link to the same bucket of the old table during resizing.
  278. * This allows to simplify the locking as locking the bucket in both
  279. * tables during resize always guarantee protection.
  280. *
  281. * IMPORTANT: When holding the bucket lock of both the old and new table
  282. * during expansions and shrinking, the old bucket lock must always be
  283. * acquired first.
  284. */
  285. static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
  286. unsigned int hash)
  287. {
  288. return &tbl->locks[hash & tbl->locks_mask];
  289. }
  290. #ifdef CONFIG_PROVE_LOCKING
  291. int lockdep_rht_mutex_is_held(struct rhashtable *ht);
  292. int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
  293. #else
  294. static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
  295. {
  296. return 1;
  297. }
  298. static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
  299. u32 hash)
  300. {
  301. return 1;
  302. }
  303. #endif /* CONFIG_PROVE_LOCKING */
  304. int rhashtable_init(struct rhashtable *ht,
  305. const struct rhashtable_params *params);
  306. struct bucket_table *rhashtable_insert_slow(struct rhashtable *ht,
  307. const void *key,
  308. struct rhash_head *obj,
  309. struct bucket_table *old_tbl,
  310. void **data);
  311. int rhashtable_insert_rehash(struct rhashtable *ht, struct bucket_table *tbl);
  312. int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
  313. void rhashtable_walk_exit(struct rhashtable_iter *iter);
  314. int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
  315. void *rhashtable_walk_next(struct rhashtable_iter *iter);
  316. void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
  317. void rhashtable_free_and_destroy(struct rhashtable *ht,
  318. void (*free_fn)(void *ptr, void *arg),
  319. void *arg);
  320. void rhashtable_destroy(struct rhashtable *ht);
  321. #define rht_dereference(p, ht) \
  322. rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
  323. #define rht_dereference_rcu(p, ht) \
  324. rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
  325. #define rht_dereference_bucket(p, tbl, hash) \
  326. rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
  327. #define rht_dereference_bucket_rcu(p, tbl, hash) \
  328. rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
  329. #define rht_entry(tpos, pos, member) \
  330. ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
  331. /**
  332. * rht_for_each_continue - continue iterating over hash chain
  333. * @pos: the &struct rhash_head to use as a loop cursor.
  334. * @head: the previous &struct rhash_head to continue from
  335. * @tbl: the &struct bucket_table
  336. * @hash: the hash value / bucket index
  337. */
  338. #define rht_for_each_continue(pos, head, tbl, hash) \
  339. for (pos = rht_dereference_bucket(head, tbl, hash); \
  340. !rht_is_a_nulls(pos); \
  341. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  342. /**
  343. * rht_for_each - iterate over hash chain
  344. * @pos: the &struct rhash_head to use as a loop cursor.
  345. * @tbl: the &struct bucket_table
  346. * @hash: the hash value / bucket index
  347. */
  348. #define rht_for_each(pos, tbl, hash) \
  349. rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
  350. /**
  351. * rht_for_each_entry_continue - continue iterating over hash chain
  352. * @tpos: the type * to use as a loop cursor.
  353. * @pos: the &struct rhash_head to use as a loop cursor.
  354. * @head: the previous &struct rhash_head to continue from
  355. * @tbl: the &struct bucket_table
  356. * @hash: the hash value / bucket index
  357. * @member: name of the &struct rhash_head within the hashable struct.
  358. */
  359. #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
  360. for (pos = rht_dereference_bucket(head, tbl, hash); \
  361. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  362. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  363. /**
  364. * rht_for_each_entry - iterate over hash chain of given type
  365. * @tpos: the type * to use as a loop cursor.
  366. * @pos: the &struct rhash_head to use as a loop cursor.
  367. * @tbl: the &struct bucket_table
  368. * @hash: the hash value / bucket index
  369. * @member: name of the &struct rhash_head within the hashable struct.
  370. */
  371. #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
  372. rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
  373. tbl, hash, member)
  374. /**
  375. * rht_for_each_entry_safe - safely iterate over hash chain of given type
  376. * @tpos: the type * to use as a loop cursor.
  377. * @pos: the &struct rhash_head to use as a loop cursor.
  378. * @next: the &struct rhash_head to use as next in loop cursor.
  379. * @tbl: the &struct bucket_table
  380. * @hash: the hash value / bucket index
  381. * @member: name of the &struct rhash_head within the hashable struct.
  382. *
  383. * This hash chain list-traversal primitive allows for the looped code to
  384. * remove the loop cursor from the list.
  385. */
  386. #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
  387. for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
  388. next = !rht_is_a_nulls(pos) ? \
  389. rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
  390. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  391. pos = next, \
  392. next = !rht_is_a_nulls(pos) ? \
  393. rht_dereference_bucket(pos->next, tbl, hash) : NULL)
  394. /**
  395. * rht_for_each_rcu_continue - continue iterating over rcu hash chain
  396. * @pos: the &struct rhash_head to use as a loop cursor.
  397. * @head: the previous &struct rhash_head to continue from
  398. * @tbl: the &struct bucket_table
  399. * @hash: the hash value / bucket index
  400. *
  401. * This hash chain list-traversal primitive may safely run concurrently with
  402. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  403. * traversal is guarded by rcu_read_lock().
  404. */
  405. #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
  406. for (({barrier(); }), \
  407. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  408. !rht_is_a_nulls(pos); \
  409. pos = rcu_dereference_raw(pos->next))
  410. /**
  411. * rht_for_each_rcu - iterate over rcu hash chain
  412. * @pos: the &struct rhash_head to use as a loop cursor.
  413. * @tbl: the &struct bucket_table
  414. * @hash: the hash value / bucket index
  415. *
  416. * This hash chain list-traversal primitive may safely run concurrently with
  417. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  418. * traversal is guarded by rcu_read_lock().
  419. */
  420. #define rht_for_each_rcu(pos, tbl, hash) \
  421. rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
  422. /**
  423. * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
  424. * @tpos: the type * to use as a loop cursor.
  425. * @pos: the &struct rhash_head to use as a loop cursor.
  426. * @head: the previous &struct rhash_head to continue from
  427. * @tbl: the &struct bucket_table
  428. * @hash: the hash value / bucket index
  429. * @member: name of the &struct rhash_head within the hashable struct.
  430. *
  431. * This hash chain list-traversal primitive may safely run concurrently with
  432. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  433. * traversal is guarded by rcu_read_lock().
  434. */
  435. #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
  436. for (({barrier(); }), \
  437. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  438. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  439. pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
  440. /**
  441. * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
  442. * @tpos: the type * to use as a loop cursor.
  443. * @pos: the &struct rhash_head to use as a loop cursor.
  444. * @tbl: the &struct bucket_table
  445. * @hash: the hash value / bucket index
  446. * @member: name of the &struct rhash_head within the hashable struct.
  447. *
  448. * This hash chain list-traversal primitive may safely run concurrently with
  449. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  450. * traversal is guarded by rcu_read_lock().
  451. */
  452. #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
  453. rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
  454. tbl, hash, member)
  455. static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
  456. const void *obj)
  457. {
  458. struct rhashtable *ht = arg->ht;
  459. const char *ptr = obj;
  460. return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
  461. }
  462. /* Internal function, do not use. */
  463. static inline struct rhash_head *__rhashtable_lookup(
  464. struct rhashtable *ht, const void *key,
  465. const struct rhashtable_params params)
  466. {
  467. struct rhashtable_compare_arg arg = {
  468. .ht = ht,
  469. .key = key,
  470. };
  471. const struct bucket_table *tbl;
  472. struct rhash_head *he;
  473. unsigned int hash;
  474. tbl = rht_dereference_rcu(ht->tbl, ht);
  475. restart:
  476. hash = rht_key_hashfn(ht, tbl, key, params);
  477. rht_for_each_rcu(he, tbl, hash) {
  478. if (params.obj_cmpfn ?
  479. params.obj_cmpfn(&arg, rht_obj(ht, he)) :
  480. rhashtable_compare(&arg, rht_obj(ht, he)))
  481. continue;
  482. return he;
  483. }
  484. /* Ensure we see any new tables. */
  485. smp_rmb();
  486. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  487. if (unlikely(tbl))
  488. goto restart;
  489. return NULL;
  490. }
  491. /**
  492. * rhashtable_lookup - search hash table
  493. * @ht: hash table
  494. * @key: the pointer to the key
  495. * @params: hash table parameters
  496. *
  497. * Computes the hash value for the key and traverses the bucket chain looking
  498. * for a entry with an identical key. The first matching entry is returned.
  499. *
  500. * This must only be called under the RCU read lock.
  501. *
  502. * Returns the first entry on which the compare function returned true.
  503. */
  504. static inline void *rhashtable_lookup(
  505. struct rhashtable *ht, const void *key,
  506. const struct rhashtable_params params)
  507. {
  508. struct rhash_head *he = __rhashtable_lookup(ht, key, params);
  509. return he ? rht_obj(ht, he) : NULL;
  510. }
  511. /**
  512. * rhashtable_lookup_fast - search hash table, without RCU read lock
  513. * @ht: hash table
  514. * @key: the pointer to the key
  515. * @params: hash table parameters
  516. *
  517. * Computes the hash value for the key and traverses the bucket chain looking
  518. * for a entry with an identical key. The first matching entry is returned.
  519. *
  520. * Only use this function when you have other mechanisms guaranteeing
  521. * that the object won't go away after the RCU read lock is released.
  522. *
  523. * Returns the first entry on which the compare function returned true.
  524. */
  525. static inline void *rhashtable_lookup_fast(
  526. struct rhashtable *ht, const void *key,
  527. const struct rhashtable_params params)
  528. {
  529. void *obj;
  530. rcu_read_lock();
  531. obj = rhashtable_lookup(ht, key, params);
  532. rcu_read_unlock();
  533. return obj;
  534. }
  535. /* Internal function, please use rhashtable_insert_fast() instead. This
  536. * function returns the existing element already in hashes in there is a clash,
  537. * otherwise it returns an error via ERR_PTR().
  538. */
  539. static inline void *__rhashtable_insert_fast(
  540. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  541. const struct rhashtable_params params)
  542. {
  543. struct rhashtable_compare_arg arg = {
  544. .ht = ht,
  545. .key = key,
  546. };
  547. struct bucket_table *tbl, *new_tbl;
  548. struct rhash_head *head;
  549. spinlock_t *lock;
  550. unsigned int elasticity;
  551. unsigned int hash;
  552. void *data = NULL;
  553. int err;
  554. restart:
  555. rcu_read_lock();
  556. tbl = rht_dereference_rcu(ht->tbl, ht);
  557. /* All insertions must grab the oldest table containing
  558. * the hashed bucket that is yet to be rehashed.
  559. */
  560. for (;;) {
  561. hash = rht_head_hashfn(ht, tbl, obj, params);
  562. lock = rht_bucket_lock(tbl, hash);
  563. spin_lock_bh(lock);
  564. if (tbl->rehash <= hash)
  565. break;
  566. spin_unlock_bh(lock);
  567. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  568. }
  569. new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  570. if (unlikely(new_tbl)) {
  571. tbl = rhashtable_insert_slow(ht, key, obj, new_tbl, &data);
  572. if (!IS_ERR_OR_NULL(tbl))
  573. goto slow_path;
  574. err = PTR_ERR(tbl);
  575. if (err == -EEXIST)
  576. err = 0;
  577. goto out;
  578. }
  579. err = -E2BIG;
  580. if (unlikely(rht_grow_above_max(ht, tbl)))
  581. goto out;
  582. if (unlikely(rht_grow_above_100(ht, tbl))) {
  583. slow_path:
  584. spin_unlock_bh(lock);
  585. err = rhashtable_insert_rehash(ht, tbl);
  586. rcu_read_unlock();
  587. if (err)
  588. return ERR_PTR(err);
  589. goto restart;
  590. }
  591. err = 0;
  592. elasticity = ht->elasticity;
  593. rht_for_each(head, tbl, hash) {
  594. if (key &&
  595. unlikely(!(params.obj_cmpfn ?
  596. params.obj_cmpfn(&arg, rht_obj(ht, head)) :
  597. rhashtable_compare(&arg, rht_obj(ht, head))))) {
  598. data = rht_obj(ht, head);
  599. goto out;
  600. }
  601. if (!--elasticity)
  602. goto slow_path;
  603. }
  604. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  605. RCU_INIT_POINTER(obj->next, head);
  606. rcu_assign_pointer(tbl->buckets[hash], obj);
  607. atomic_inc(&ht->nelems);
  608. if (rht_grow_above_75(ht, tbl))
  609. schedule_work(&ht->run_work);
  610. out:
  611. spin_unlock_bh(lock);
  612. rcu_read_unlock();
  613. return err ? ERR_PTR(err) : data;
  614. }
  615. /**
  616. * rhashtable_insert_fast - insert object into hash table
  617. * @ht: hash table
  618. * @obj: pointer to hash head inside object
  619. * @params: hash table parameters
  620. *
  621. * Will take a per bucket spinlock to protect against mutual mutations
  622. * on the same bucket. Multiple insertions may occur in parallel unless
  623. * they map to the same bucket lock.
  624. *
  625. * It is safe to call this function from atomic context.
  626. *
  627. * Will trigger an automatic deferred table resizing if the size grows
  628. * beyond the watermark indicated by grow_decision() which can be passed
  629. * to rhashtable_init().
  630. */
  631. static inline int rhashtable_insert_fast(
  632. struct rhashtable *ht, struct rhash_head *obj,
  633. const struct rhashtable_params params)
  634. {
  635. void *ret;
  636. ret = __rhashtable_insert_fast(ht, NULL, obj, params);
  637. if (IS_ERR(ret))
  638. return PTR_ERR(ret);
  639. return ret == NULL ? 0 : -EEXIST;
  640. }
  641. /**
  642. * rhashtable_lookup_insert_fast - lookup and insert object into hash table
  643. * @ht: hash table
  644. * @obj: pointer to hash head inside object
  645. * @params: hash table parameters
  646. *
  647. * Locks down the bucket chain in both the old and new table if a resize
  648. * is in progress to ensure that writers can't remove from the old table
  649. * and can't insert to the new table during the atomic operation of search
  650. * and insertion. Searches for duplicates in both the old and new table if
  651. * a resize is in progress.
  652. *
  653. * This lookup function may only be used for fixed key hash table (key_len
  654. * parameter set). It will BUG() if used inappropriately.
  655. *
  656. * It is safe to call this function from atomic context.
  657. *
  658. * Will trigger an automatic deferred table resizing if the size grows
  659. * beyond the watermark indicated by grow_decision() which can be passed
  660. * to rhashtable_init().
  661. */
  662. static inline int rhashtable_lookup_insert_fast(
  663. struct rhashtable *ht, struct rhash_head *obj,
  664. const struct rhashtable_params params)
  665. {
  666. const char *key = rht_obj(ht, obj);
  667. void *ret;
  668. BUG_ON(ht->p.obj_hashfn);
  669. ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params);
  670. if (IS_ERR(ret))
  671. return PTR_ERR(ret);
  672. return ret == NULL ? 0 : -EEXIST;
  673. }
  674. /**
  675. * rhashtable_lookup_insert_key - search and insert object to hash table
  676. * with explicit key
  677. * @ht: hash table
  678. * @key: key
  679. * @obj: pointer to hash head inside object
  680. * @params: hash table parameters
  681. *
  682. * Locks down the bucket chain in both the old and new table if a resize
  683. * is in progress to ensure that writers can't remove from the old table
  684. * and can't insert to the new table during the atomic operation of search
  685. * and insertion. Searches for duplicates in both the old and new table if
  686. * a resize is in progress.
  687. *
  688. * Lookups may occur in parallel with hashtable mutations and resizing.
  689. *
  690. * Will trigger an automatic deferred table resizing if the size grows
  691. * beyond the watermark indicated by grow_decision() which can be passed
  692. * to rhashtable_init().
  693. *
  694. * Returns zero on success.
  695. */
  696. static inline int rhashtable_lookup_insert_key(
  697. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  698. const struct rhashtable_params params)
  699. {
  700. void *ret;
  701. BUG_ON(!ht->p.obj_hashfn || !key);
  702. ret = __rhashtable_insert_fast(ht, key, obj, params);
  703. if (IS_ERR(ret))
  704. return PTR_ERR(ret);
  705. return ret == NULL ? 0 : -EEXIST;
  706. }
  707. /**
  708. * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
  709. * @ht: hash table
  710. * @obj: pointer to hash head inside object
  711. * @params: hash table parameters
  712. * @data: pointer to element data already in hashes
  713. *
  714. * Just like rhashtable_lookup_insert_key(), but this function returns the
  715. * object if it exists, NULL if it does not and the insertion was successful,
  716. * and an ERR_PTR otherwise.
  717. */
  718. static inline void *rhashtable_lookup_get_insert_key(
  719. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  720. const struct rhashtable_params params)
  721. {
  722. BUG_ON(!ht->p.obj_hashfn || !key);
  723. return __rhashtable_insert_fast(ht, key, obj, params);
  724. }
  725. /* Internal function, please use rhashtable_remove_fast() instead */
  726. static inline int __rhashtable_remove_fast(
  727. struct rhashtable *ht, struct bucket_table *tbl,
  728. struct rhash_head *obj, const struct rhashtable_params params)
  729. {
  730. struct rhash_head __rcu **pprev;
  731. struct rhash_head *he;
  732. spinlock_t * lock;
  733. unsigned int hash;
  734. int err = -ENOENT;
  735. hash = rht_head_hashfn(ht, tbl, obj, params);
  736. lock = rht_bucket_lock(tbl, hash);
  737. spin_lock_bh(lock);
  738. pprev = &tbl->buckets[hash];
  739. rht_for_each(he, tbl, hash) {
  740. if (he != obj) {
  741. pprev = &he->next;
  742. continue;
  743. }
  744. rcu_assign_pointer(*pprev, obj->next);
  745. err = 0;
  746. break;
  747. }
  748. spin_unlock_bh(lock);
  749. return err;
  750. }
  751. /**
  752. * rhashtable_remove_fast - remove object from hash table
  753. * @ht: hash table
  754. * @obj: pointer to hash head inside object
  755. * @params: hash table parameters
  756. *
  757. * Since the hash chain is single linked, the removal operation needs to
  758. * walk the bucket chain upon removal. The removal operation is thus
  759. * considerable slow if the hash table is not correctly sized.
  760. *
  761. * Will automatically shrink the table via rhashtable_expand() if the
  762. * shrink_decision function specified at rhashtable_init() returns true.
  763. *
  764. * Returns zero on success, -ENOENT if the entry could not be found.
  765. */
  766. static inline int rhashtable_remove_fast(
  767. struct rhashtable *ht, struct rhash_head *obj,
  768. const struct rhashtable_params params)
  769. {
  770. struct bucket_table *tbl;
  771. int err;
  772. rcu_read_lock();
  773. tbl = rht_dereference_rcu(ht->tbl, ht);
  774. /* Because we have already taken (and released) the bucket
  775. * lock in old_tbl, if we find that future_tbl is not yet
  776. * visible then that guarantees the entry to still be in
  777. * the old tbl if it exists.
  778. */
  779. while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
  780. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  781. ;
  782. if (err)
  783. goto out;
  784. atomic_dec(&ht->nelems);
  785. if (unlikely(ht->p.automatic_shrinking &&
  786. rht_shrink_below_30(ht, tbl)))
  787. schedule_work(&ht->run_work);
  788. out:
  789. rcu_read_unlock();
  790. return err;
  791. }
  792. #endif /* _LINUX_RHASHTABLE_H */