rhashtable.h 24 KB

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