rhashtable.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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. * @nelems: Number of elements in table
  124. * @key_len: Key length for hashfn
  125. * @elasticity: Maximum chain length before rehash
  126. * @p: Configuration parameters
  127. * @run_work: Deferred worker to expand/shrink asynchronously
  128. * @mutex: Mutex to protect current/future table swapping
  129. * @lock: Spin lock to protect walker list
  130. */
  131. struct rhashtable {
  132. struct bucket_table __rcu *tbl;
  133. atomic_t nelems;
  134. unsigned int key_len;
  135. unsigned int elasticity;
  136. struct rhashtable_params p;
  137. struct work_struct run_work;
  138. struct mutex mutex;
  139. spinlock_t lock;
  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. int rhashtable_insert_rehash(struct rhashtable *ht, struct bucket_table *tbl);
  311. int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
  312. void rhashtable_walk_exit(struct rhashtable_iter *iter);
  313. int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
  314. void *rhashtable_walk_next(struct rhashtable_iter *iter);
  315. void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
  316. void rhashtable_free_and_destroy(struct rhashtable *ht,
  317. void (*free_fn)(void *ptr, void *arg),
  318. void *arg);
  319. void rhashtable_destroy(struct rhashtable *ht);
  320. #define rht_dereference(p, ht) \
  321. rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
  322. #define rht_dereference_rcu(p, ht) \
  323. rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
  324. #define rht_dereference_bucket(p, tbl, hash) \
  325. rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
  326. #define rht_dereference_bucket_rcu(p, tbl, hash) \
  327. rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
  328. #define rht_entry(tpos, pos, member) \
  329. ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
  330. /**
  331. * rht_for_each_continue - continue iterating over hash chain
  332. * @pos: the &struct rhash_head to use as a loop cursor.
  333. * @head: the previous &struct rhash_head to continue from
  334. * @tbl: the &struct bucket_table
  335. * @hash: the hash value / bucket index
  336. */
  337. #define rht_for_each_continue(pos, head, tbl, hash) \
  338. for (pos = rht_dereference_bucket(head, tbl, hash); \
  339. !rht_is_a_nulls(pos); \
  340. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  341. /**
  342. * rht_for_each - iterate over hash chain
  343. * @pos: the &struct rhash_head to use as a loop cursor.
  344. * @tbl: the &struct bucket_table
  345. * @hash: the hash value / bucket index
  346. */
  347. #define rht_for_each(pos, tbl, hash) \
  348. rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
  349. /**
  350. * rht_for_each_entry_continue - continue iterating over hash chain
  351. * @tpos: the type * to use as a loop cursor.
  352. * @pos: the &struct rhash_head to use as a loop cursor.
  353. * @head: the previous &struct rhash_head to continue from
  354. * @tbl: the &struct bucket_table
  355. * @hash: the hash value / bucket index
  356. * @member: name of the &struct rhash_head within the hashable struct.
  357. */
  358. #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
  359. for (pos = rht_dereference_bucket(head, tbl, hash); \
  360. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  361. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  362. /**
  363. * rht_for_each_entry - iterate over hash chain of given type
  364. * @tpos: the type * to use as a loop cursor.
  365. * @pos: the &struct rhash_head to use as a loop cursor.
  366. * @tbl: the &struct bucket_table
  367. * @hash: the hash value / bucket index
  368. * @member: name of the &struct rhash_head within the hashable struct.
  369. */
  370. #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
  371. rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
  372. tbl, hash, member)
  373. /**
  374. * rht_for_each_entry_safe - safely iterate over hash chain of given type
  375. * @tpos: the type * to use as a loop cursor.
  376. * @pos: the &struct rhash_head to use as a loop cursor.
  377. * @next: the &struct rhash_head to use as next in loop cursor.
  378. * @tbl: the &struct bucket_table
  379. * @hash: the hash value / bucket index
  380. * @member: name of the &struct rhash_head within the hashable struct.
  381. *
  382. * This hash chain list-traversal primitive allows for the looped code to
  383. * remove the loop cursor from the list.
  384. */
  385. #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
  386. for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
  387. next = !rht_is_a_nulls(pos) ? \
  388. rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
  389. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  390. pos = next, \
  391. next = !rht_is_a_nulls(pos) ? \
  392. rht_dereference_bucket(pos->next, tbl, hash) : NULL)
  393. /**
  394. * rht_for_each_rcu_continue - continue iterating over rcu hash chain
  395. * @pos: the &struct rhash_head to use as a loop cursor.
  396. * @head: the previous &struct rhash_head to continue from
  397. * @tbl: the &struct bucket_table
  398. * @hash: the hash value / bucket index
  399. *
  400. * This hash chain list-traversal primitive may safely run concurrently with
  401. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  402. * traversal is guarded by rcu_read_lock().
  403. */
  404. #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
  405. for (({barrier(); }), \
  406. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  407. !rht_is_a_nulls(pos); \
  408. pos = rcu_dereference_raw(pos->next))
  409. /**
  410. * rht_for_each_rcu - iterate over rcu hash chain
  411. * @pos: the &struct rhash_head to use as a loop cursor.
  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(pos, tbl, hash) \
  420. rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
  421. /**
  422. * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
  423. * @tpos: the type * to use as a loop cursor.
  424. * @pos: the &struct rhash_head to use as a loop cursor.
  425. * @head: the previous &struct rhash_head to continue from
  426. * @tbl: the &struct bucket_table
  427. * @hash: the hash value / bucket index
  428. * @member: name of the &struct rhash_head within the hashable struct.
  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_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
  435. for (({barrier(); }), \
  436. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  437. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  438. pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
  439. /**
  440. * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
  441. * @tpos: the type * to use as a loop cursor.
  442. * @pos: the &struct rhash_head to use as a loop cursor.
  443. * @tbl: the &struct bucket_table
  444. * @hash: the hash value / bucket index
  445. * @member: name of the &struct rhash_head within the hashable struct.
  446. *
  447. * This hash chain list-traversal primitive may safely run concurrently with
  448. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  449. * traversal is guarded by rcu_read_lock().
  450. */
  451. #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
  452. rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
  453. tbl, hash, member)
  454. static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
  455. const void *obj)
  456. {
  457. struct rhashtable *ht = arg->ht;
  458. const char *ptr = obj;
  459. return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
  460. }
  461. /**
  462. * rhashtable_lookup_fast - search hash table, inlined version
  463. * @ht: hash table
  464. * @key: the pointer to the key
  465. * @params: hash table parameters
  466. *
  467. * Computes the hash value for the key and traverses the bucket chain looking
  468. * for a entry with an identical key. The first matching entry is returned.
  469. *
  470. * Returns the first entry on which the compare function returned true.
  471. */
  472. static inline void *rhashtable_lookup_fast(
  473. struct rhashtable *ht, const void *key,
  474. const struct rhashtable_params params)
  475. {
  476. struct rhashtable_compare_arg arg = {
  477. .ht = ht,
  478. .key = key,
  479. };
  480. const struct bucket_table *tbl;
  481. struct rhash_head *he;
  482. unsigned int hash;
  483. rcu_read_lock();
  484. tbl = rht_dereference_rcu(ht->tbl, ht);
  485. restart:
  486. hash = rht_key_hashfn(ht, tbl, key, params);
  487. rht_for_each_rcu(he, tbl, hash) {
  488. if (params.obj_cmpfn ?
  489. params.obj_cmpfn(&arg, rht_obj(ht, he)) :
  490. rhashtable_compare(&arg, rht_obj(ht, he)))
  491. continue;
  492. rcu_read_unlock();
  493. return rht_obj(ht, he);
  494. }
  495. /* Ensure we see any new tables. */
  496. smp_rmb();
  497. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  498. if (unlikely(tbl))
  499. goto restart;
  500. rcu_read_unlock();
  501. return NULL;
  502. }
  503. /* Internal function, please use rhashtable_insert_fast() instead */
  504. static inline int __rhashtable_insert_fast(
  505. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  506. const struct rhashtable_params params)
  507. {
  508. struct rhashtable_compare_arg arg = {
  509. .ht = ht,
  510. .key = key,
  511. };
  512. struct bucket_table *tbl, *new_tbl;
  513. struct rhash_head *head;
  514. spinlock_t *lock;
  515. unsigned int elasticity;
  516. unsigned int hash;
  517. int err;
  518. restart:
  519. rcu_read_lock();
  520. tbl = rht_dereference_rcu(ht->tbl, ht);
  521. /* All insertions must grab the oldest table containing
  522. * the hashed bucket that is yet to be rehashed.
  523. */
  524. for (;;) {
  525. hash = rht_head_hashfn(ht, tbl, obj, params);
  526. lock = rht_bucket_lock(tbl, hash);
  527. spin_lock_bh(lock);
  528. if (tbl->rehash <= hash)
  529. break;
  530. spin_unlock_bh(lock);
  531. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  532. }
  533. new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  534. if (unlikely(new_tbl)) {
  535. tbl = rhashtable_insert_slow(ht, key, obj, new_tbl);
  536. if (!IS_ERR_OR_NULL(tbl))
  537. goto slow_path;
  538. err = PTR_ERR(tbl);
  539. goto out;
  540. }
  541. err = -E2BIG;
  542. if (unlikely(rht_grow_above_max(ht, tbl)))
  543. goto out;
  544. if (unlikely(rht_grow_above_100(ht, tbl))) {
  545. slow_path:
  546. spin_unlock_bh(lock);
  547. err = rhashtable_insert_rehash(ht, tbl);
  548. rcu_read_unlock();
  549. if (err)
  550. return err;
  551. goto restart;
  552. }
  553. err = -EEXIST;
  554. elasticity = ht->elasticity;
  555. rht_for_each(head, tbl, hash) {
  556. if (key &&
  557. unlikely(!(params.obj_cmpfn ?
  558. params.obj_cmpfn(&arg, rht_obj(ht, head)) :
  559. rhashtable_compare(&arg, rht_obj(ht, head)))))
  560. goto out;
  561. if (!--elasticity)
  562. goto slow_path;
  563. }
  564. err = 0;
  565. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  566. RCU_INIT_POINTER(obj->next, head);
  567. rcu_assign_pointer(tbl->buckets[hash], obj);
  568. atomic_inc(&ht->nelems);
  569. if (rht_grow_above_75(ht, tbl))
  570. schedule_work(&ht->run_work);
  571. out:
  572. spin_unlock_bh(lock);
  573. rcu_read_unlock();
  574. return err;
  575. }
  576. /**
  577. * rhashtable_insert_fast - insert object into hash table
  578. * @ht: hash table
  579. * @obj: pointer to hash head inside object
  580. * @params: hash table parameters
  581. *
  582. * Will take a per bucket spinlock to protect against mutual mutations
  583. * on the same bucket. Multiple insertions may occur in parallel unless
  584. * they map to the same bucket lock.
  585. *
  586. * It is safe to call this function from atomic context.
  587. *
  588. * Will trigger an automatic deferred table resizing if the size grows
  589. * beyond the watermark indicated by grow_decision() which can be passed
  590. * to rhashtable_init().
  591. */
  592. static inline int rhashtable_insert_fast(
  593. struct rhashtable *ht, struct rhash_head *obj,
  594. const struct rhashtable_params params)
  595. {
  596. return __rhashtable_insert_fast(ht, NULL, obj, params);
  597. }
  598. /**
  599. * rhashtable_lookup_insert_fast - lookup and insert object into hash table
  600. * @ht: hash table
  601. * @obj: pointer to hash head inside object
  602. * @params: hash table parameters
  603. *
  604. * Locks down the bucket chain in both the old and new table if a resize
  605. * is in progress to ensure that writers can't remove from the old table
  606. * and can't insert to the new table during the atomic operation of search
  607. * and insertion. Searches for duplicates in both the old and new table if
  608. * a resize is in progress.
  609. *
  610. * This lookup function may only be used for fixed key hash table (key_len
  611. * parameter set). It will BUG() if used inappropriately.
  612. *
  613. * It is safe to call this function from atomic context.
  614. *
  615. * Will trigger an automatic deferred table resizing if the size grows
  616. * beyond the watermark indicated by grow_decision() which can be passed
  617. * to rhashtable_init().
  618. */
  619. static inline int rhashtable_lookup_insert_fast(
  620. struct rhashtable *ht, struct rhash_head *obj,
  621. const struct rhashtable_params params)
  622. {
  623. const char *key = rht_obj(ht, obj);
  624. BUG_ON(ht->p.obj_hashfn);
  625. return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj,
  626. params);
  627. }
  628. /**
  629. * rhashtable_lookup_insert_key - search and insert object to hash table
  630. * with explicit key
  631. * @ht: hash table
  632. * @key: key
  633. * @obj: pointer to hash head inside object
  634. * @params: hash table parameters
  635. *
  636. * Locks down the bucket chain in both the old and new table if a resize
  637. * is in progress to ensure that writers can't remove from the old table
  638. * and can't insert to the new table during the atomic operation of search
  639. * and insertion. Searches for duplicates in both the old and new table if
  640. * a resize is in progress.
  641. *
  642. * Lookups may occur in parallel with hashtable mutations and resizing.
  643. *
  644. * Will trigger an automatic deferred table resizing if the size grows
  645. * beyond the watermark indicated by grow_decision() which can be passed
  646. * to rhashtable_init().
  647. *
  648. * Returns zero on success.
  649. */
  650. static inline int rhashtable_lookup_insert_key(
  651. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  652. const struct rhashtable_params params)
  653. {
  654. BUG_ON(!ht->p.obj_hashfn || !key);
  655. return __rhashtable_insert_fast(ht, key, obj, params);
  656. }
  657. /* Internal function, please use rhashtable_remove_fast() instead */
  658. static inline int __rhashtable_remove_fast(
  659. struct rhashtable *ht, struct bucket_table *tbl,
  660. struct rhash_head *obj, const struct rhashtable_params params)
  661. {
  662. struct rhash_head __rcu **pprev;
  663. struct rhash_head *he;
  664. spinlock_t * lock;
  665. unsigned int hash;
  666. int err = -ENOENT;
  667. hash = rht_head_hashfn(ht, tbl, obj, params);
  668. lock = rht_bucket_lock(tbl, hash);
  669. spin_lock_bh(lock);
  670. pprev = &tbl->buckets[hash];
  671. rht_for_each(he, tbl, hash) {
  672. if (he != obj) {
  673. pprev = &he->next;
  674. continue;
  675. }
  676. rcu_assign_pointer(*pprev, obj->next);
  677. err = 0;
  678. break;
  679. }
  680. spin_unlock_bh(lock);
  681. return err;
  682. }
  683. /**
  684. * rhashtable_remove_fast - remove object from hash table
  685. * @ht: hash table
  686. * @obj: pointer to hash head inside object
  687. * @params: hash table parameters
  688. *
  689. * Since the hash chain is single linked, the removal operation needs to
  690. * walk the bucket chain upon removal. The removal operation is thus
  691. * considerable slow if the hash table is not correctly sized.
  692. *
  693. * Will automatically shrink the table via rhashtable_expand() if the
  694. * shrink_decision function specified at rhashtable_init() returns true.
  695. *
  696. * Returns zero on success, -ENOENT if the entry could not be found.
  697. */
  698. static inline int rhashtable_remove_fast(
  699. struct rhashtable *ht, struct rhash_head *obj,
  700. const struct rhashtable_params params)
  701. {
  702. struct bucket_table *tbl;
  703. int err;
  704. rcu_read_lock();
  705. tbl = rht_dereference_rcu(ht->tbl, ht);
  706. /* Because we have already taken (and released) the bucket
  707. * lock in old_tbl, if we find that future_tbl is not yet
  708. * visible then that guarantees the entry to still be in
  709. * the old tbl if it exists.
  710. */
  711. while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
  712. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  713. ;
  714. if (err)
  715. goto out;
  716. atomic_dec(&ht->nelems);
  717. if (unlikely(ht->p.automatic_shrinking &&
  718. rht_shrink_below_30(ht, tbl)))
  719. schedule_work(&ht->run_work);
  720. out:
  721. rcu_read_unlock();
  722. return err;
  723. }
  724. #endif /* _LINUX_RHASHTABLE_H */