rhashtable.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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. void rhashtable_walk_enter(struct rhashtable *ht,
  312. 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. /**
  463. * rhashtable_lookup_fast - search hash table, inlined version
  464. * @ht: hash table
  465. * @key: the pointer to the key
  466. * @params: hash table parameters
  467. *
  468. * Computes the hash value for the key and traverses the bucket chain looking
  469. * for a entry with an identical key. The first matching entry is returned.
  470. *
  471. * Returns the first entry on which the compare function returned true.
  472. */
  473. static inline void *rhashtable_lookup_fast(
  474. struct rhashtable *ht, const void *key,
  475. const struct rhashtable_params params)
  476. {
  477. struct rhashtable_compare_arg arg = {
  478. .ht = ht,
  479. .key = key,
  480. };
  481. const struct bucket_table *tbl;
  482. struct rhash_head *he;
  483. unsigned int hash;
  484. rcu_read_lock();
  485. tbl = rht_dereference_rcu(ht->tbl, ht);
  486. restart:
  487. hash = rht_key_hashfn(ht, tbl, key, params);
  488. rht_for_each_rcu(he, tbl, hash) {
  489. if (params.obj_cmpfn ?
  490. params.obj_cmpfn(&arg, rht_obj(ht, he)) :
  491. rhashtable_compare(&arg, rht_obj(ht, he)))
  492. continue;
  493. rcu_read_unlock();
  494. return rht_obj(ht, he);
  495. }
  496. /* Ensure we see any new tables. */
  497. smp_rmb();
  498. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  499. if (unlikely(tbl))
  500. goto restart;
  501. rcu_read_unlock();
  502. return NULL;
  503. }
  504. /* Internal function, please use rhashtable_insert_fast() instead */
  505. static inline int __rhashtable_insert_fast(
  506. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  507. const struct rhashtable_params params)
  508. {
  509. struct rhashtable_compare_arg arg = {
  510. .ht = ht,
  511. .key = key,
  512. };
  513. struct bucket_table *tbl, *new_tbl;
  514. struct rhash_head *head;
  515. spinlock_t *lock;
  516. unsigned int elasticity;
  517. unsigned int hash;
  518. int err;
  519. restart:
  520. rcu_read_lock();
  521. tbl = rht_dereference_rcu(ht->tbl, ht);
  522. /* All insertions must grab the oldest table containing
  523. * the hashed bucket that is yet to be rehashed.
  524. */
  525. for (;;) {
  526. hash = rht_head_hashfn(ht, tbl, obj, params);
  527. lock = rht_bucket_lock(tbl, hash);
  528. spin_lock_bh(lock);
  529. if (tbl->rehash <= hash)
  530. break;
  531. spin_unlock_bh(lock);
  532. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  533. }
  534. new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  535. if (unlikely(new_tbl)) {
  536. tbl = rhashtable_insert_slow(ht, key, obj, new_tbl);
  537. if (!IS_ERR_OR_NULL(tbl))
  538. goto slow_path;
  539. err = PTR_ERR(tbl);
  540. goto out;
  541. }
  542. err = -E2BIG;
  543. if (unlikely(rht_grow_above_max(ht, tbl)))
  544. goto out;
  545. if (unlikely(rht_grow_above_100(ht, tbl))) {
  546. slow_path:
  547. spin_unlock_bh(lock);
  548. err = rhashtable_insert_rehash(ht, tbl);
  549. rcu_read_unlock();
  550. if (err)
  551. return err;
  552. goto restart;
  553. }
  554. err = -EEXIST;
  555. elasticity = ht->elasticity;
  556. rht_for_each(head, tbl, hash) {
  557. if (key &&
  558. unlikely(!(params.obj_cmpfn ?
  559. params.obj_cmpfn(&arg, rht_obj(ht, head)) :
  560. rhashtable_compare(&arg, rht_obj(ht, head)))))
  561. goto out;
  562. if (!--elasticity)
  563. goto slow_path;
  564. }
  565. err = 0;
  566. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  567. RCU_INIT_POINTER(obj->next, head);
  568. rcu_assign_pointer(tbl->buckets[hash], obj);
  569. atomic_inc(&ht->nelems);
  570. if (rht_grow_above_75(ht, tbl))
  571. schedule_work(&ht->run_work);
  572. out:
  573. spin_unlock_bh(lock);
  574. rcu_read_unlock();
  575. return err;
  576. }
  577. /**
  578. * rhashtable_insert_fast - insert object into hash table
  579. * @ht: hash table
  580. * @obj: pointer to hash head inside object
  581. * @params: hash table parameters
  582. *
  583. * Will take a per bucket spinlock to protect against mutual mutations
  584. * on the same bucket. Multiple insertions may occur in parallel unless
  585. * they map to the same bucket lock.
  586. *
  587. * It is safe to call this function from atomic context.
  588. *
  589. * Will trigger an automatic deferred table resizing if the size grows
  590. * beyond the watermark indicated by grow_decision() which can be passed
  591. * to rhashtable_init().
  592. */
  593. static inline int rhashtable_insert_fast(
  594. struct rhashtable *ht, struct rhash_head *obj,
  595. const struct rhashtable_params params)
  596. {
  597. return __rhashtable_insert_fast(ht, NULL, obj, params);
  598. }
  599. /**
  600. * rhashtable_lookup_insert_fast - lookup and insert object into hash table
  601. * @ht: hash table
  602. * @obj: pointer to hash head inside object
  603. * @params: hash table parameters
  604. *
  605. * Locks down the bucket chain in both the old and new table if a resize
  606. * is in progress to ensure that writers can't remove from the old table
  607. * and can't insert to the new table during the atomic operation of search
  608. * and insertion. Searches for duplicates in both the old and new table if
  609. * a resize is in progress.
  610. *
  611. * This lookup function may only be used for fixed key hash table (key_len
  612. * parameter set). It will BUG() if used inappropriately.
  613. *
  614. * It is safe to call this function from atomic context.
  615. *
  616. * Will trigger an automatic deferred table resizing if the size grows
  617. * beyond the watermark indicated by grow_decision() which can be passed
  618. * to rhashtable_init().
  619. */
  620. static inline int rhashtable_lookup_insert_fast(
  621. struct rhashtable *ht, struct rhash_head *obj,
  622. const struct rhashtable_params params)
  623. {
  624. const char *key = rht_obj(ht, obj);
  625. BUG_ON(ht->p.obj_hashfn);
  626. return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj,
  627. params);
  628. }
  629. /**
  630. * rhashtable_lookup_insert_key - search and insert object to hash table
  631. * with explicit key
  632. * @ht: hash table
  633. * @key: key
  634. * @obj: pointer to hash head inside object
  635. * @params: hash table parameters
  636. *
  637. * Locks down the bucket chain in both the old and new table if a resize
  638. * is in progress to ensure that writers can't remove from the old table
  639. * and can't insert to the new table during the atomic operation of search
  640. * and insertion. Searches for duplicates in both the old and new table if
  641. * a resize is in progress.
  642. *
  643. * Lookups may occur in parallel with hashtable mutations and resizing.
  644. *
  645. * Will trigger an automatic deferred table resizing if the size grows
  646. * beyond the watermark indicated by grow_decision() which can be passed
  647. * to rhashtable_init().
  648. *
  649. * Returns zero on success.
  650. */
  651. static inline int rhashtable_lookup_insert_key(
  652. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  653. const struct rhashtable_params params)
  654. {
  655. BUG_ON(!ht->p.obj_hashfn || !key);
  656. return __rhashtable_insert_fast(ht, key, obj, params);
  657. }
  658. /* Internal function, please use rhashtable_remove_fast() instead */
  659. static inline int __rhashtable_remove_fast(
  660. struct rhashtable *ht, struct bucket_table *tbl,
  661. struct rhash_head *obj, const struct rhashtable_params params)
  662. {
  663. struct rhash_head __rcu **pprev;
  664. struct rhash_head *he;
  665. spinlock_t * lock;
  666. unsigned int hash;
  667. int err = -ENOENT;
  668. hash = rht_head_hashfn(ht, tbl, obj, params);
  669. lock = rht_bucket_lock(tbl, hash);
  670. spin_lock_bh(lock);
  671. pprev = &tbl->buckets[hash];
  672. rht_for_each(he, tbl, hash) {
  673. if (he != obj) {
  674. pprev = &he->next;
  675. continue;
  676. }
  677. rcu_assign_pointer(*pprev, obj->next);
  678. err = 0;
  679. break;
  680. }
  681. spin_unlock_bh(lock);
  682. return err;
  683. }
  684. /**
  685. * rhashtable_remove_fast - remove object from hash table
  686. * @ht: hash table
  687. * @obj: pointer to hash head inside object
  688. * @params: hash table parameters
  689. *
  690. * Since the hash chain is single linked, the removal operation needs to
  691. * walk the bucket chain upon removal. The removal operation is thus
  692. * considerable slow if the hash table is not correctly sized.
  693. *
  694. * Will automatically shrink the table via rhashtable_expand() if the
  695. * shrink_decision function specified at rhashtable_init() returns true.
  696. *
  697. * Returns zero on success, -ENOENT if the entry could not be found.
  698. */
  699. static inline int rhashtable_remove_fast(
  700. struct rhashtable *ht, struct rhash_head *obj,
  701. const struct rhashtable_params params)
  702. {
  703. struct bucket_table *tbl;
  704. int err;
  705. rcu_read_lock();
  706. tbl = rht_dereference_rcu(ht->tbl, ht);
  707. /* Because we have already taken (and released) the bucket
  708. * lock in old_tbl, if we find that future_tbl is not yet
  709. * visible then that guarantees the entry to still be in
  710. * the old tbl if it exists.
  711. */
  712. while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
  713. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  714. ;
  715. if (err)
  716. goto out;
  717. atomic_dec(&ht->nelems);
  718. if (unlikely(ht->p.automatic_shrinking &&
  719. rht_shrink_below_30(ht, tbl)))
  720. schedule_work(&ht->run_work);
  721. out:
  722. rcu_read_unlock();
  723. return err;
  724. }
  725. /* Internal function, please use rhashtable_replace_fast() instead */
  726. static inline int __rhashtable_replace_fast(
  727. struct rhashtable *ht, struct bucket_table *tbl,
  728. struct rhash_head *obj_old, struct rhash_head *obj_new,
  729. const struct rhashtable_params params)
  730. {
  731. struct rhash_head __rcu **pprev;
  732. struct rhash_head *he;
  733. spinlock_t *lock;
  734. unsigned int hash;
  735. int err = -ENOENT;
  736. /* Minimally, the old and new objects must have same hash
  737. * (which should mean identifiers are the same).
  738. */
  739. hash = rht_head_hashfn(ht, tbl, obj_old, params);
  740. if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
  741. return -EINVAL;
  742. lock = rht_bucket_lock(tbl, hash);
  743. spin_lock_bh(lock);
  744. pprev = &tbl->buckets[hash];
  745. rht_for_each(he, tbl, hash) {
  746. if (he != obj_old) {
  747. pprev = &he->next;
  748. continue;
  749. }
  750. rcu_assign_pointer(obj_new->next, obj_old->next);
  751. rcu_assign_pointer(*pprev, obj_new);
  752. err = 0;
  753. break;
  754. }
  755. spin_unlock_bh(lock);
  756. return err;
  757. }
  758. /**
  759. * rhashtable_replace_fast - replace an object in hash table
  760. * @ht: hash table
  761. * @obj_old: pointer to hash head inside object being replaced
  762. * @obj_new: pointer to hash head inside object which is new
  763. * @params: hash table parameters
  764. *
  765. * Replacing an object doesn't affect the number of elements in the hash table
  766. * or bucket, so we don't need to worry about shrinking or expanding the
  767. * table here.
  768. *
  769. * Returns zero on success, -ENOENT if the entry could not be found,
  770. * -EINVAL if hash is not the same for the old and new objects.
  771. */
  772. static inline int rhashtable_replace_fast(
  773. struct rhashtable *ht, struct rhash_head *obj_old,
  774. struct rhash_head *obj_new,
  775. const struct rhashtable_params params)
  776. {
  777. struct bucket_table *tbl;
  778. int err;
  779. rcu_read_lock();
  780. tbl = rht_dereference_rcu(ht->tbl, ht);
  781. /* Because we have already taken (and released) the bucket
  782. * lock in old_tbl, if we find that future_tbl is not yet
  783. * visible then that guarantees the entry to still be in
  784. * the old tbl if it exists.
  785. */
  786. while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
  787. obj_new, params)) &&
  788. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  789. ;
  790. rcu_read_unlock();
  791. return err;
  792. }
  793. /* Obsolete function, do not use in new code. */
  794. static inline int rhashtable_walk_init(struct rhashtable *ht,
  795. struct rhashtable_iter *iter, gfp_t gfp)
  796. {
  797. rhashtable_walk_enter(ht, iter);
  798. return 0;
  799. }
  800. #endif /* _LINUX_RHASHTABLE_H */