rhashtable.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. void **data);
  311. int rhashtable_insert_rehash(struct rhashtable *ht, struct bucket_table *tbl);
  312. int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter,
  313. gfp_t gfp);
  314. void rhashtable_walk_exit(struct rhashtable_iter *iter);
  315. int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
  316. void *rhashtable_walk_next(struct rhashtable_iter *iter);
  317. void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
  318. void rhashtable_free_and_destroy(struct rhashtable *ht,
  319. void (*free_fn)(void *ptr, void *arg),
  320. void *arg);
  321. void rhashtable_destroy(struct rhashtable *ht);
  322. #define rht_dereference(p, ht) \
  323. rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
  324. #define rht_dereference_rcu(p, ht) \
  325. rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
  326. #define rht_dereference_bucket(p, tbl, hash) \
  327. rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
  328. #define rht_dereference_bucket_rcu(p, tbl, hash) \
  329. rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
  330. #define rht_entry(tpos, pos, member) \
  331. ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
  332. /**
  333. * rht_for_each_continue - continue iterating over hash chain
  334. * @pos: the &struct rhash_head to use as a loop cursor.
  335. * @head: the previous &struct rhash_head to continue from
  336. * @tbl: the &struct bucket_table
  337. * @hash: the hash value / bucket index
  338. */
  339. #define rht_for_each_continue(pos, head, tbl, hash) \
  340. for (pos = rht_dereference_bucket(head, tbl, hash); \
  341. !rht_is_a_nulls(pos); \
  342. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  343. /**
  344. * rht_for_each - iterate over hash chain
  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. */
  349. #define rht_for_each(pos, tbl, hash) \
  350. rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
  351. /**
  352. * rht_for_each_entry_continue - continue iterating over hash chain
  353. * @tpos: the type * to use as a loop cursor.
  354. * @pos: the &struct rhash_head to use as a loop cursor.
  355. * @head: the previous &struct rhash_head to continue from
  356. * @tbl: the &struct bucket_table
  357. * @hash: the hash value / bucket index
  358. * @member: name of the &struct rhash_head within the hashable struct.
  359. */
  360. #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
  361. for (pos = rht_dereference_bucket(head, tbl, hash); \
  362. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  363. pos = rht_dereference_bucket((pos)->next, tbl, hash))
  364. /**
  365. * rht_for_each_entry - iterate over hash chain of given type
  366. * @tpos: the type * to use as a loop cursor.
  367. * @pos: the &struct rhash_head to use as a loop cursor.
  368. * @tbl: the &struct bucket_table
  369. * @hash: the hash value / bucket index
  370. * @member: name of the &struct rhash_head within the hashable struct.
  371. */
  372. #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
  373. rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
  374. tbl, hash, member)
  375. /**
  376. * rht_for_each_entry_safe - safely iterate over hash chain of given type
  377. * @tpos: the type * to use as a loop cursor.
  378. * @pos: the &struct rhash_head to use as a loop cursor.
  379. * @next: the &struct rhash_head to use as next in loop cursor.
  380. * @tbl: the &struct bucket_table
  381. * @hash: the hash value / bucket index
  382. * @member: name of the &struct rhash_head within the hashable struct.
  383. *
  384. * This hash chain list-traversal primitive allows for the looped code to
  385. * remove the loop cursor from the list.
  386. */
  387. #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
  388. for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
  389. next = !rht_is_a_nulls(pos) ? \
  390. rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
  391. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  392. pos = next, \
  393. next = !rht_is_a_nulls(pos) ? \
  394. rht_dereference_bucket(pos->next, tbl, hash) : NULL)
  395. /**
  396. * rht_for_each_rcu_continue - continue iterating over rcu hash chain
  397. * @pos: the &struct rhash_head to use as a loop cursor.
  398. * @head: the previous &struct rhash_head to continue from
  399. * @tbl: the &struct bucket_table
  400. * @hash: the hash value / bucket index
  401. *
  402. * This hash chain list-traversal primitive may safely run concurrently with
  403. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  404. * traversal is guarded by rcu_read_lock().
  405. */
  406. #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
  407. for (({barrier(); }), \
  408. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  409. !rht_is_a_nulls(pos); \
  410. pos = rcu_dereference_raw(pos->next))
  411. /**
  412. * rht_for_each_rcu - iterate over rcu hash chain
  413. * @pos: the &struct rhash_head to use as a loop cursor.
  414. * @tbl: the &struct bucket_table
  415. * @hash: the hash value / bucket index
  416. *
  417. * This hash chain list-traversal primitive may safely run concurrently with
  418. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  419. * traversal is guarded by rcu_read_lock().
  420. */
  421. #define rht_for_each_rcu(pos, tbl, hash) \
  422. rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
  423. /**
  424. * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
  425. * @tpos: the type * to use as a loop cursor.
  426. * @pos: the &struct rhash_head to use as a loop cursor.
  427. * @head: the previous &struct rhash_head to continue from
  428. * @tbl: the &struct bucket_table
  429. * @hash: the hash value / bucket index
  430. * @member: name of the &struct rhash_head within the hashable struct.
  431. *
  432. * This hash chain list-traversal primitive may safely run concurrently with
  433. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  434. * traversal is guarded by rcu_read_lock().
  435. */
  436. #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
  437. for (({barrier(); }), \
  438. pos = rht_dereference_bucket_rcu(head, tbl, hash); \
  439. (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
  440. pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
  441. /**
  442. * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
  443. * @tpos: the type * to use as a loop cursor.
  444. * @pos: the &struct rhash_head to use as a loop cursor.
  445. * @tbl: the &struct bucket_table
  446. * @hash: the hash value / bucket index
  447. * @member: name of the &struct rhash_head within the hashable struct.
  448. *
  449. * This hash chain list-traversal primitive may safely run concurrently with
  450. * the _rcu mutation primitives such as rhashtable_insert() as long as the
  451. * traversal is guarded by rcu_read_lock().
  452. */
  453. #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
  454. rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
  455. tbl, hash, member)
  456. static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
  457. const void *obj)
  458. {
  459. struct rhashtable *ht = arg->ht;
  460. const char *ptr = obj;
  461. return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
  462. }
  463. /**
  464. * rhashtable_lookup_fast - search hash table, inlined version
  465. * @ht: hash table
  466. * @key: the pointer to the key
  467. * @params: hash table parameters
  468. *
  469. * Computes the hash value for the key and traverses the bucket chain looking
  470. * for a entry with an identical key. The first matching entry is returned.
  471. *
  472. * Returns the first entry on which the compare function returned true.
  473. */
  474. static inline void *rhashtable_lookup_fast(
  475. struct rhashtable *ht, const void *key,
  476. const struct rhashtable_params params)
  477. {
  478. struct rhashtable_compare_arg arg = {
  479. .ht = ht,
  480. .key = key,
  481. };
  482. const struct bucket_table *tbl;
  483. struct rhash_head *he;
  484. unsigned int hash;
  485. rcu_read_lock();
  486. tbl = rht_dereference_rcu(ht->tbl, ht);
  487. restart:
  488. hash = rht_key_hashfn(ht, tbl, key, params);
  489. rht_for_each_rcu(he, tbl, hash) {
  490. if (params.obj_cmpfn ?
  491. params.obj_cmpfn(&arg, rht_obj(ht, he)) :
  492. rhashtable_compare(&arg, rht_obj(ht, he)))
  493. continue;
  494. rcu_read_unlock();
  495. return rht_obj(ht, he);
  496. }
  497. /* Ensure we see any new tables. */
  498. smp_rmb();
  499. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  500. if (unlikely(tbl))
  501. goto restart;
  502. rcu_read_unlock();
  503. return NULL;
  504. }
  505. /* Internal function, please use rhashtable_insert_fast() instead. This
  506. * function returns the existing element already in hashes in there is a clash,
  507. * otherwise it returns an error via ERR_PTR().
  508. */
  509. static inline void *__rhashtable_insert_fast(
  510. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  511. const struct rhashtable_params params)
  512. {
  513. struct rhashtable_compare_arg arg = {
  514. .ht = ht,
  515. .key = key,
  516. };
  517. struct bucket_table *tbl, *new_tbl;
  518. struct rhash_head *head;
  519. spinlock_t *lock;
  520. unsigned int elasticity;
  521. unsigned int hash;
  522. void *data = NULL;
  523. int err;
  524. restart:
  525. rcu_read_lock();
  526. tbl = rht_dereference_rcu(ht->tbl, ht);
  527. /* All insertions must grab the oldest table containing
  528. * the hashed bucket that is yet to be rehashed.
  529. */
  530. for (;;) {
  531. hash = rht_head_hashfn(ht, tbl, obj, params);
  532. lock = rht_bucket_lock(tbl, hash);
  533. spin_lock_bh(lock);
  534. if (tbl->rehash <= hash)
  535. break;
  536. spin_unlock_bh(lock);
  537. tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  538. }
  539. new_tbl = rht_dereference_rcu(tbl->future_tbl, ht);
  540. if (unlikely(new_tbl)) {
  541. tbl = rhashtable_insert_slow(ht, key, obj, new_tbl, &data);
  542. if (!IS_ERR_OR_NULL(tbl))
  543. goto slow_path;
  544. err = PTR_ERR(tbl);
  545. if (err == -EEXIST)
  546. err = 0;
  547. goto out;
  548. }
  549. err = -E2BIG;
  550. if (unlikely(rht_grow_above_max(ht, tbl)))
  551. goto out;
  552. if (unlikely(rht_grow_above_100(ht, tbl))) {
  553. slow_path:
  554. spin_unlock_bh(lock);
  555. err = rhashtable_insert_rehash(ht, tbl);
  556. rcu_read_unlock();
  557. if (err)
  558. return ERR_PTR(err);
  559. goto restart;
  560. }
  561. err = 0;
  562. elasticity = ht->elasticity;
  563. rht_for_each(head, tbl, hash) {
  564. if (key &&
  565. unlikely(!(params.obj_cmpfn ?
  566. params.obj_cmpfn(&arg, rht_obj(ht, head)) :
  567. rhashtable_compare(&arg, rht_obj(ht, head))))) {
  568. data = rht_obj(ht, head);
  569. goto out;
  570. }
  571. if (!--elasticity)
  572. goto slow_path;
  573. }
  574. head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
  575. RCU_INIT_POINTER(obj->next, head);
  576. rcu_assign_pointer(tbl->buckets[hash], obj);
  577. atomic_inc(&ht->nelems);
  578. if (rht_grow_above_75(ht, tbl))
  579. schedule_work(&ht->run_work);
  580. out:
  581. spin_unlock_bh(lock);
  582. rcu_read_unlock();
  583. return err ? ERR_PTR(err) : data;
  584. }
  585. /**
  586. * rhashtable_insert_fast - insert object into hash table
  587. * @ht: hash table
  588. * @obj: pointer to hash head inside object
  589. * @params: hash table parameters
  590. *
  591. * Will take a per bucket spinlock to protect against mutual mutations
  592. * on the same bucket. Multiple insertions may occur in parallel unless
  593. * they map to the same bucket lock.
  594. *
  595. * It is safe to call this function from atomic context.
  596. *
  597. * Will trigger an automatic deferred table resizing if the size grows
  598. * beyond the watermark indicated by grow_decision() which can be passed
  599. * to rhashtable_init().
  600. */
  601. static inline int rhashtable_insert_fast(
  602. struct rhashtable *ht, struct rhash_head *obj,
  603. const struct rhashtable_params params)
  604. {
  605. void *ret;
  606. ret = __rhashtable_insert_fast(ht, NULL, obj, params);
  607. if (IS_ERR(ret))
  608. return PTR_ERR(ret);
  609. return ret == NULL ? 0 : -EEXIST;
  610. }
  611. /**
  612. * rhashtable_lookup_insert_fast - lookup and insert object into hash table
  613. * @ht: hash table
  614. * @obj: pointer to hash head inside object
  615. * @params: hash table parameters
  616. *
  617. * Locks down the bucket chain in both the old and new table if a resize
  618. * is in progress to ensure that writers can't remove from the old table
  619. * and can't insert to the new table during the atomic operation of search
  620. * and insertion. Searches for duplicates in both the old and new table if
  621. * a resize is in progress.
  622. *
  623. * This lookup function may only be used for fixed key hash table (key_len
  624. * parameter set). It will BUG() if used inappropriately.
  625. *
  626. * It is safe to call this function from atomic context.
  627. *
  628. * Will trigger an automatic deferred table resizing if the size grows
  629. * beyond the watermark indicated by grow_decision() which can be passed
  630. * to rhashtable_init().
  631. */
  632. static inline int rhashtable_lookup_insert_fast(
  633. struct rhashtable *ht, struct rhash_head *obj,
  634. const struct rhashtable_params params)
  635. {
  636. const char *key = rht_obj(ht, obj);
  637. void *ret;
  638. BUG_ON(ht->p.obj_hashfn);
  639. ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params);
  640. if (IS_ERR(ret))
  641. return PTR_ERR(ret);
  642. return ret == NULL ? 0 : -EEXIST;
  643. }
  644. /**
  645. * rhashtable_lookup_insert_key - search and insert object to hash table
  646. * with explicit key
  647. * @ht: hash table
  648. * @key: key
  649. * @obj: pointer to hash head inside object
  650. * @params: hash table parameters
  651. *
  652. * Locks down the bucket chain in both the old and new table if a resize
  653. * is in progress to ensure that writers can't remove from the old table
  654. * and can't insert to the new table during the atomic operation of search
  655. * and insertion. Searches for duplicates in both the old and new table if
  656. * a resize is in progress.
  657. *
  658. * Lookups may occur in parallel with hashtable mutations and resizing.
  659. *
  660. * Will trigger an automatic deferred table resizing if the size grows
  661. * beyond the watermark indicated by grow_decision() which can be passed
  662. * to rhashtable_init().
  663. *
  664. * Returns zero on success.
  665. */
  666. static inline int rhashtable_lookup_insert_key(
  667. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  668. const struct rhashtable_params params)
  669. {
  670. void *ret;
  671. BUG_ON(!ht->p.obj_hashfn || !key);
  672. ret = __rhashtable_insert_fast(ht, key, obj, params);
  673. if (IS_ERR(ret))
  674. return PTR_ERR(ret);
  675. return ret == NULL ? 0 : -EEXIST;
  676. }
  677. /**
  678. * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
  679. * @ht: hash table
  680. * @obj: pointer to hash head inside object
  681. * @params: hash table parameters
  682. * @data: pointer to element data already in hashes
  683. *
  684. * Just like rhashtable_lookup_insert_key(), but this function returns the
  685. * object if it exists, NULL if it does not and the insertion was successful,
  686. * and an ERR_PTR otherwise.
  687. */
  688. static inline void *rhashtable_lookup_get_insert_key(
  689. struct rhashtable *ht, const void *key, struct rhash_head *obj,
  690. const struct rhashtable_params params)
  691. {
  692. BUG_ON(!ht->p.obj_hashfn || !key);
  693. return __rhashtable_insert_fast(ht, key, obj, params);
  694. }
  695. /* Internal function, please use rhashtable_remove_fast() instead */
  696. static inline int __rhashtable_remove_fast(
  697. struct rhashtable *ht, struct bucket_table *tbl,
  698. struct rhash_head *obj, const struct rhashtable_params params)
  699. {
  700. struct rhash_head __rcu **pprev;
  701. struct rhash_head *he;
  702. spinlock_t * lock;
  703. unsigned int hash;
  704. int err = -ENOENT;
  705. hash = rht_head_hashfn(ht, tbl, obj, params);
  706. lock = rht_bucket_lock(tbl, hash);
  707. spin_lock_bh(lock);
  708. pprev = &tbl->buckets[hash];
  709. rht_for_each(he, tbl, hash) {
  710. if (he != obj) {
  711. pprev = &he->next;
  712. continue;
  713. }
  714. rcu_assign_pointer(*pprev, obj->next);
  715. err = 0;
  716. break;
  717. }
  718. spin_unlock_bh(lock);
  719. return err;
  720. }
  721. /**
  722. * rhashtable_remove_fast - remove object from hash table
  723. * @ht: hash table
  724. * @obj: pointer to hash head inside object
  725. * @params: hash table parameters
  726. *
  727. * Since the hash chain is single linked, the removal operation needs to
  728. * walk the bucket chain upon removal. The removal operation is thus
  729. * considerable slow if the hash table is not correctly sized.
  730. *
  731. * Will automatically shrink the table via rhashtable_expand() if the
  732. * shrink_decision function specified at rhashtable_init() returns true.
  733. *
  734. * Returns zero on success, -ENOENT if the entry could not be found.
  735. */
  736. static inline int rhashtable_remove_fast(
  737. struct rhashtable *ht, struct rhash_head *obj,
  738. const struct rhashtable_params params)
  739. {
  740. struct bucket_table *tbl;
  741. int err;
  742. rcu_read_lock();
  743. tbl = rht_dereference_rcu(ht->tbl, ht);
  744. /* Because we have already taken (and released) the bucket
  745. * lock in old_tbl, if we find that future_tbl is not yet
  746. * visible then that guarantees the entry to still be in
  747. * the old tbl if it exists.
  748. */
  749. while ((err = __rhashtable_remove_fast(ht, tbl, obj, params)) &&
  750. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  751. ;
  752. if (err)
  753. goto out;
  754. atomic_dec(&ht->nelems);
  755. if (unlikely(ht->p.automatic_shrinking &&
  756. rht_shrink_below_30(ht, tbl)))
  757. schedule_work(&ht->run_work);
  758. out:
  759. rcu_read_unlock();
  760. return err;
  761. }
  762. /* Internal function, please use rhashtable_replace_fast() instead */
  763. static inline int __rhashtable_replace_fast(
  764. struct rhashtable *ht, struct bucket_table *tbl,
  765. struct rhash_head *obj_old, struct rhash_head *obj_new,
  766. const struct rhashtable_params params)
  767. {
  768. struct rhash_head __rcu **pprev;
  769. struct rhash_head *he;
  770. spinlock_t *lock;
  771. unsigned int hash;
  772. int err = -ENOENT;
  773. /* Minimally, the old and new objects must have same hash
  774. * (which should mean identifiers are the same).
  775. */
  776. hash = rht_head_hashfn(ht, tbl, obj_old, params);
  777. if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
  778. return -EINVAL;
  779. lock = rht_bucket_lock(tbl, hash);
  780. spin_lock_bh(lock);
  781. pprev = &tbl->buckets[hash];
  782. rht_for_each(he, tbl, hash) {
  783. if (he != obj_old) {
  784. pprev = &he->next;
  785. continue;
  786. }
  787. rcu_assign_pointer(obj_new->next, obj_old->next);
  788. rcu_assign_pointer(*pprev, obj_new);
  789. err = 0;
  790. break;
  791. }
  792. spin_unlock_bh(lock);
  793. return err;
  794. }
  795. /**
  796. * rhashtable_replace_fast - replace an object in hash table
  797. * @ht: hash table
  798. * @obj_old: pointer to hash head inside object being replaced
  799. * @obj_new: pointer to hash head inside object which is new
  800. * @params: hash table parameters
  801. *
  802. * Replacing an object doesn't affect the number of elements in the hash table
  803. * or bucket, so we don't need to worry about shrinking or expanding the
  804. * table here.
  805. *
  806. * Returns zero on success, -ENOENT if the entry could not be found,
  807. * -EINVAL if hash is not the same for the old and new objects.
  808. */
  809. static inline int rhashtable_replace_fast(
  810. struct rhashtable *ht, struct rhash_head *obj_old,
  811. struct rhash_head *obj_new,
  812. const struct rhashtable_params params)
  813. {
  814. struct bucket_table *tbl;
  815. int err;
  816. rcu_read_lock();
  817. tbl = rht_dereference_rcu(ht->tbl, ht);
  818. /* Because we have already taken (and released) the bucket
  819. * lock in old_tbl, if we find that future_tbl is not yet
  820. * visible then that guarantees the entry to still be in
  821. * the old tbl if it exists.
  822. */
  823. while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
  824. obj_new, params)) &&
  825. (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
  826. ;
  827. rcu_read_unlock();
  828. return err;
  829. }
  830. #endif /* _LINUX_RHASHTABLE_H */