1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Resizable, Scalable, Concurrent Hash Table
5 * Copyright (c) 2015-2016 Herbert Xu <herbert@gondor.apana.org.au>
6 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
7 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
9 * Code partially derived from nft_hash
10 * Rewritten with rehash code from br_multicast plus single list
11 * pointer as suggested by Josh Triplett
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #ifndef _LINUX_RHASHTABLE_H
19 #define _LINUX_RHASHTABLE_H
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/jhash.h>
24 #include <linux/list_nulls.h>
25 #include <linux/rcupdate.h>
26 #include <linux/workqueue.h>
27 #include <linux/rculist.h>
28 #include <linux/bit_spinlock.h>
30 #define BIT(nr) (1UL << (nr))
31 #define BIT_ULL(nr) (1ULL << (nr))
33 #include <linux/rhashtable-types.h>
35 * Objects in an rhashtable have an embedded struct rhash_head
36 * which is linked into as hash chain from the hash table - or one
37 * of two or more hash tables when the rhashtable is being resized.
38 * The end of the chain is marked with a special nulls marks which has
39 * the least significant bit set but otherwise stores the address of
40 * the hash bucket. This allows us to be sure we've found the end
42 * The value stored in the hash bucket has BIT(0) used as a lock bit.
43 * This bit must be atomically set before any changes are made to
44 * the chain. To avoid dereferencing this pointer without clearing
45 * the bit first, we use an opaque 'struct rhash_lock_head *' for the
46 * pointer stored in the bucket. This struct needs to be defined so
47 * that rcu_dereference() works on it, but it has no content so a
48 * cast is needed for it to be useful. This ensures it isn't
49 * used by mistake with clearing the lock bit first.
51 struct rhash_lock_head {};
53 /* Maximum chain length before rehash
55 * The maximum (not average) chain length grows with the size of the hash
56 * table, at a rate of (log N)/(log log N).
58 * The value of 16 is selected so that even if the hash table grew to
59 * 2^32 you would not expect the maximum chain length to exceed it
60 * unless we are under attack (or extremely unlucky).
62 * As this limit is only to detect attacks, we don't need to set it to a
63 * lower value as you'd need the chain length to vastly exceed 16 to have
64 * any real effect on the system.
66 #define RHT_ELASTICITY 16u
69 * struct bucket_table - Table of hash buckets
70 * @size: Number of hash buckets
71 * @nest: Number of bits of first-level nested table.
72 * @rehash: Current bucket being rehashed
73 * @hash_rnd: Random seed to fold into hash
74 * @walkers: List of active walkers
75 * @rcu: RCU structure for freeing the table
76 * @future_tbl: Table under construction during rehashing
77 * @ntbl: Nested table used when out of memory.
78 * @buckets: size * hash buckets
84 struct list_head walkers;
87 struct bucket_table __rcu *future_tbl;
89 struct rhash_lock_head __rcu *buckets[] ____cacheline_aligned_in_smp;
93 * NULLS_MARKER() expects a hash value with the low
94 * bits mostly likely to be significant, and it discards
96 * We give it an address, in which the bottom bit is
97 * always 0, and the msb might be significant.
98 * So we shift the address down one bit to align with
99 * expectations and avoid losing a significant bit.
101 * We never store the NULLS_MARKER in the hash table
102 * itself as we need the lsb for locking.
103 * Instead we store a NULL
105 #define RHT_NULLS_MARKER(ptr) \
106 ((void *)NULLS_MARKER(((unsigned long) (ptr)) >> 1))
107 #define INIT_RHT_NULLS_HEAD(ptr) \
110 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
112 return ((unsigned long) ptr & 1);
115 static inline void *rht_obj(const struct rhashtable *ht,
116 const struct rhash_head *he)
118 return (char *)he - ht->p.head_offset;
121 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
124 return hash & (tbl->size - 1);
127 static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
128 const void *key, const struct rhashtable_params params,
129 unsigned int hash_rnd)
133 /* params must be equal to ht->p if it isn't constant. */
134 if (!__builtin_constant_p(params.key_len))
135 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
136 else if (params.key_len) {
137 unsigned int key_len = params.key_len;
140 hash = params.hashfn(key, key_len, hash_rnd);
141 else if (key_len & (sizeof(u32) - 1))
142 hash = jhash(key, key_len, hash_rnd);
144 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
146 unsigned int key_len = ht->p.key_len;
149 hash = params.hashfn(key, key_len, hash_rnd);
151 hash = jhash(key, key_len, hash_rnd);
157 static inline unsigned int rht_key_hashfn(
158 struct rhashtable *ht, const struct bucket_table *tbl,
159 const void *key, const struct rhashtable_params params)
161 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
163 return rht_bucket_index(tbl, hash);
166 static inline unsigned int rht_head_hashfn(
167 struct rhashtable *ht, const struct bucket_table *tbl,
168 const struct rhash_head *he, const struct rhashtable_params params)
170 const char *ptr = rht_obj(ht, he);
172 return likely(params.obj_hashfn) ?
173 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
176 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
180 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
182 * @tbl: current table
184 static inline bool rht_grow_above_75(const struct rhashtable *ht,
185 const struct bucket_table *tbl)
187 /* Expand table when exceeding 75% load */
188 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
189 (!ht->p.max_size || tbl->size < ht->p.max_size);
193 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
195 * @tbl: current table
197 static inline bool rht_shrink_below_30(const struct rhashtable *ht,
198 const struct bucket_table *tbl)
200 /* Shrink table beneath 30% load */
201 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
202 tbl->size > ht->p.min_size;
206 * rht_grow_above_100 - returns true if nelems > table-size
208 * @tbl: current table
210 static inline bool rht_grow_above_100(const struct rhashtable *ht,
211 const struct bucket_table *tbl)
213 return atomic_read(&ht->nelems) > tbl->size &&
214 (!ht->p.max_size || tbl->size < ht->p.max_size);
218 * rht_grow_above_max - returns true if table is above maximum
220 * @tbl: current table
222 static inline bool rht_grow_above_max(const struct rhashtable *ht,
223 const struct bucket_table *tbl)
225 return atomic_read(&ht->nelems) >= ht->max_elems;
228 #ifdef CONFIG_PROVE_LOCKING
229 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
230 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
232 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
237 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
242 #endif /* CONFIG_PROVE_LOCKING */
244 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
245 struct rhash_head *obj);
247 void rhashtable_walk_enter(struct rhashtable *ht,
248 struct rhashtable_iter *iter);
249 void rhashtable_walk_exit(struct rhashtable_iter *iter);
250 int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
252 static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
254 (void)rhashtable_walk_start_check(iter);
257 void *rhashtable_walk_next(struct rhashtable_iter *iter);
258 void *rhashtable_walk_peek(struct rhashtable_iter *iter);
259 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
261 void rhashtable_free_and_destroy(struct rhashtable *ht,
262 void (*free_fn)(void *ptr, void *arg),
264 void rhashtable_destroy(struct rhashtable *ht);
266 struct rhash_lock_head __rcu **rht_bucket_nested(
267 const struct bucket_table *tbl, unsigned int hash);
268 struct rhash_lock_head __rcu **__rht_bucket_nested(
269 const struct bucket_table *tbl, unsigned int hash);
270 struct rhash_lock_head __rcu **rht_bucket_nested_insert(
271 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash);
273 #define rht_dereference(p, ht) \
276 #define rht_dereference_rcu(p, ht) \
279 #define rht_dereference_bucket(p, tbl, hash) \
282 #define rht_dereference_bucket_rcu(p, tbl, hash) \
285 #define rht_entry(tpos, pos, member) \
286 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
288 static inline struct rhash_lock_head __rcu *const *rht_bucket(
289 const struct bucket_table *tbl, unsigned int hash)
291 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
295 static inline struct rhash_lock_head __rcu **rht_bucket_var(
296 struct bucket_table *tbl, unsigned int hash)
298 return unlikely(tbl->nest) ? __rht_bucket_nested(tbl, hash) :
302 static inline struct rhash_lock_head __rcu **rht_bucket_insert(
303 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
305 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
310 * We lock a bucket by setting BIT(0) in the pointer - this is always
311 * zero in real pointers. The NULLS mark is never stored in the bucket,
312 * rather we store NULL if the bucket is empty.
313 * bit_spin_locks do not handle contention well, but the whole point
314 * of the hashtable design is to achieve minimum per-bucket contention.
315 * A nested hash table might not have a bucket pointer. In that case
316 * we cannot get a lock. For remove and replace the bucket cannot be
317 * interesting and doesn't need locking.
318 * For insert we allocate the bucket if this is the last bucket_table,
319 * and then take the lock.
320 * Sometimes we unlock a bucket by writing a new pointer there. In that
321 * case we don't need to unlock, but we do need to reset state such as
322 * local_bh. For that we have rht_assign_unlock(). As rcu_assign_pointer()
323 * provides the same release semantics that bit_spin_unlock() provides,
325 * When we write to a bucket without unlocking, we use rht_assign_locked().
328 static inline void rht_lock(struct bucket_table *tbl,
329 struct rhash_lock_head __rcu **bkt)
331 bit_spin_lock(0, (unsigned long *)bkt);
334 static inline void rht_lock_nested(struct bucket_table *tbl,
335 struct rhash_lock_head __rcu **bucket,
336 unsigned int subclass)
338 bit_spin_lock(0, (unsigned long *)bucket);
341 static inline void rht_unlock(struct bucket_table *tbl,
342 struct rhash_lock_head __rcu **bkt)
344 bit_spin_unlock(0, (unsigned long *)bkt);
347 static inline struct rhash_head *__rht_ptr(
348 struct rhash_lock_head *p, struct rhash_lock_head __rcu *const *bkt)
350 return (struct rhash_head *)
351 ((unsigned long)p & ~BIT(0) ?:
352 (unsigned long)RHT_NULLS_MARKER(bkt));
356 * Where 'bkt' is a bucket and might be locked:
357 * rht_ptr_rcu() dereferences that pointer and clears the lock bit.
358 * rht_ptr() dereferences in a context where the bucket is locked.
359 * rht_ptr_exclusive() dereferences in a context where exclusive
360 * access is guaranteed, such as when destroying the table.
362 static inline struct rhash_head *rht_ptr_rcu(
363 struct rhash_lock_head __rcu *const *bkt)
365 return __rht_ptr(rcu_dereference(*bkt), bkt);
368 static inline struct rhash_head *rht_ptr(
369 struct rhash_lock_head __rcu *const *bkt,
370 struct bucket_table *tbl,
373 return __rht_ptr(rht_dereference_bucket(*bkt, tbl, hash), bkt);
376 static inline struct rhash_head *rht_ptr_exclusive(
377 struct rhash_lock_head __rcu *const *bkt)
379 return __rht_ptr(rcu_dereference(*bkt), bkt);
382 static inline void rht_assign_locked(struct rhash_lock_head __rcu **bkt,
383 struct rhash_head *obj)
385 if (rht_is_a_nulls(obj))
387 rcu_assign_pointer(*bkt, (void *)((unsigned long)obj | BIT(0)));
390 static inline void rht_assign_unlock(struct bucket_table *tbl,
391 struct rhash_lock_head __rcu **bkt,
392 struct rhash_head *obj)
394 if (rht_is_a_nulls(obj))
396 rcu_assign_pointer(*bkt, (void *)obj);
399 bit_spin_wake(0, (unsigned long *) bkt);
403 * rht_for_each_from - iterate over hash chain from given head
404 * @pos: the &struct rhash_head to use as a loop cursor.
405 * @head: the &struct rhash_head to start from
406 * @tbl: the &struct bucket_table
407 * @hash: the hash value / bucket index
409 #define rht_for_each_from(pos, head, tbl, hash) \
411 !rht_is_a_nulls(pos); \
412 pos = rht_dereference_bucket((pos)->next, tbl, hash))
415 * rht_for_each - iterate over hash chain
416 * @pos: the &struct rhash_head to use as a loop cursor.
417 * @tbl: the &struct bucket_table
418 * @hash: the hash value / bucket index
420 #define rht_for_each(pos, tbl, hash) \
421 rht_for_each_from(pos, rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
425 * rht_for_each_entry_from - iterate over hash chain from given head
426 * @tpos: the type * to use as a loop cursor.
427 * @pos: the &struct rhash_head to use as a loop cursor.
428 * @head: the &struct rhash_head to start from
429 * @tbl: the &struct bucket_table
430 * @hash: the hash value / bucket index
431 * @member: name of the &struct rhash_head within the hashable struct.
433 #define rht_for_each_entry_from(tpos, pos, head, tbl, hash, member) \
435 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
436 pos = rht_dereference_bucket((pos)->next, tbl, hash))
439 * rht_for_each_entry - iterate over hash chain of given type
440 * @tpos: the type * to use as a loop cursor.
441 * @pos: the &struct rhash_head to use as a loop cursor.
442 * @tbl: the &struct bucket_table
443 * @hash: the hash value / bucket index
444 * @member: name of the &struct rhash_head within the hashable struct.
446 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
447 rht_for_each_entry_from(tpos, pos, \
448 rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
452 * rht_for_each_entry_safe - safely iterate over hash chain of given type
453 * @tpos: the type * to use as a loop cursor.
454 * @pos: the &struct rhash_head to use as a loop cursor.
455 * @next: the &struct rhash_head to use as next in loop cursor.
456 * @tbl: the &struct bucket_table
457 * @hash: the hash value / bucket index
458 * @member: name of the &struct rhash_head within the hashable struct.
460 * This hash chain list-traversal primitive allows for the looped code to
461 * remove the loop cursor from the list.
463 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
464 for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash), \
465 next = !rht_is_a_nulls(pos) ? \
466 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
467 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
469 next = !rht_is_a_nulls(pos) ? \
470 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
473 * rht_for_each_rcu_from - iterate over rcu hash chain from given head
474 * @pos: the &struct rhash_head to use as a loop cursor.
475 * @head: the &struct rhash_head to start from
476 * @tbl: the &struct bucket_table
477 * @hash: the hash value / bucket index
479 * This hash chain list-traversal primitive may safely run concurrently with
480 * the _rcu mutation primitives such as rhashtable_insert() as long as the
481 * traversal is guarded by rcu_read_lock().
483 #define rht_for_each_rcu_from(pos, head, tbl, hash) \
484 for (({barrier(); }), \
486 !rht_is_a_nulls(pos); \
487 pos = rcu_dereference_raw(pos->next))
490 * rht_for_each_rcu - iterate over rcu hash chain
491 * @pos: the &struct rhash_head to use as a loop cursor.
492 * @tbl: the &struct bucket_table
493 * @hash: the hash value / bucket index
495 * This hash chain list-traversal primitive may safely run concurrently with
496 * the _rcu mutation primitives such as rhashtable_insert() as long as the
497 * traversal is guarded by rcu_read_lock().
499 #define rht_for_each_rcu(pos, tbl, hash) \
500 for (({barrier(); }), \
501 pos = rht_ptr_rcu(rht_bucket(tbl, hash)); \
502 !rht_is_a_nulls(pos); \
503 pos = rcu_dereference_raw(pos->next))
506 * rht_for_each_entry_rcu_from - iterated over rcu hash chain from given head
507 * @tpos: the type * to use as a loop cursor.
508 * @pos: the &struct rhash_head to use as a loop cursor.
509 * @head: the &struct rhash_head to start from
510 * @tbl: the &struct bucket_table
511 * @hash: the hash value / bucket index
512 * @member: name of the &struct rhash_head within the hashable struct.
514 * This hash chain list-traversal primitive may safely run concurrently with
515 * the _rcu mutation primitives such as rhashtable_insert() as long as the
516 * traversal is guarded by rcu_read_lock().
518 #define rht_for_each_entry_rcu_from(tpos, pos, head, tbl, hash, member) \
519 for (({barrier(); }), \
521 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
522 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
525 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
526 * @tpos: the type * to use as a loop cursor.
527 * @pos: the &struct rhash_head to use as a loop cursor.
528 * @tbl: the &struct bucket_table
529 * @hash: the hash value / bucket index
530 * @member: name of the &struct rhash_head within the hashable struct.
532 * This hash chain list-traversal primitive may safely run concurrently with
533 * the _rcu mutation primitives such as rhashtable_insert() as long as the
534 * traversal is guarded by rcu_read_lock().
536 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
537 rht_for_each_entry_rcu_from(tpos, pos, \
538 rht_ptr_rcu(rht_bucket(tbl, hash)), \
542 * rhl_for_each_rcu - iterate over rcu hash table list
543 * @pos: the &struct rlist_head to use as a loop cursor.
544 * @list: the head of the list
546 * This hash chain list-traversal primitive should be used on the
547 * list returned by rhltable_lookup.
549 #define rhl_for_each_rcu(pos, list) \
550 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
553 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
554 * @tpos: the type * to use as a loop cursor.
555 * @pos: the &struct rlist_head to use as a loop cursor.
556 * @list: the head of the list
557 * @member: name of the &struct rlist_head within the hashable struct.
559 * This hash chain list-traversal primitive should be used on the
560 * list returned by rhltable_lookup.
562 #define rhl_for_each_entry_rcu(tpos, pos, list, member) \
563 for (pos = list; pos && rht_entry(tpos, pos, member); \
564 pos = rcu_dereference_raw(pos->next))
566 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
569 struct rhashtable *ht = arg->ht;
570 const char *ptr = obj;
572 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
575 /* Internal function, do not use. */
576 static inline struct rhash_head *__rhashtable_lookup(
577 struct rhashtable *ht, const void *key,
578 const struct rhashtable_params params)
580 struct rhashtable_compare_arg arg = {
584 struct rhash_lock_head __rcu *const *bkt;
585 struct bucket_table *tbl;
586 struct rhash_head *he;
589 tbl = rht_dereference_rcu(ht->tbl, ht);
591 hash = rht_key_hashfn(ht, tbl, key, params);
592 bkt = rht_bucket(tbl, hash);
594 rht_for_each_rcu_from(he, rht_ptr_rcu(bkt), tbl, hash) {
595 if (params.obj_cmpfn ?
596 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
597 rhashtable_compare(&arg, rht_obj(ht, he)))
601 /* An object might have been moved to a different hash chain,
602 * while we walk along it - better check and retry.
604 } while (he != RHT_NULLS_MARKER(bkt));
606 /* Ensure we see any new tables. */
609 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
617 * rhashtable_lookup - search hash table
619 * @key: the pointer to the key
620 * @params: hash table parameters
622 * Computes the hash value for the key and traverses the bucket chain looking
623 * for a entry with an identical key. The first matching entry is returned.
625 * This must only be called under the RCU read lock.
627 * Returns the first entry on which the compare function returned true.
629 static inline void *rhashtable_lookup(
630 struct rhashtable *ht, const void *key,
631 const struct rhashtable_params params)
633 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
635 return he ? rht_obj(ht, he) : NULL;
639 * rhashtable_lookup_fast - search hash table, without RCU read lock
641 * @key: the pointer to the key
642 * @params: hash table parameters
644 * Computes the hash value for the key and traverses the bucket chain looking
645 * for a entry with an identical key. The first matching entry is returned.
647 * Only use this function when you have other mechanisms guaranteeing
648 * that the object won't go away after the RCU read lock is released.
650 * Returns the first entry on which the compare function returned true.
652 static inline void *rhashtable_lookup_fast(
653 struct rhashtable *ht, const void *key,
654 const struct rhashtable_params params)
659 obj = rhashtable_lookup(ht, key, params);
666 * rhltable_lookup - search hash list table
668 * @key: the pointer to the key
669 * @params: hash table parameters
671 * Computes the hash value for the key and traverses the bucket chain looking
672 * for a entry with an identical key. All matching entries are returned
675 * This must only be called under the RCU read lock.
677 * Returns the list of entries that match the given key.
679 static inline struct rhlist_head *rhltable_lookup(
680 struct rhltable *hlt, const void *key,
681 const struct rhashtable_params params)
683 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
685 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
688 /* Internal function, please use rhashtable_insert_fast() instead. This
689 * function returns the existing element already in hashes in there is a clash,
690 * otherwise it returns an error via ERR_PTR().
692 static inline void *__rhashtable_insert_fast(
693 struct rhashtable *ht, const void *key, struct rhash_head *obj,
694 const struct rhashtable_params params, bool rhlist)
696 struct rhashtable_compare_arg arg = {
700 struct rhash_lock_head __rcu **bkt;
701 struct rhash_head __rcu **pprev;
702 struct bucket_table *tbl;
703 struct rhash_head *head;
710 tbl = rht_dereference_rcu(ht->tbl, ht);
711 hash = rht_head_hashfn(ht, tbl, obj, params);
712 elasticity = RHT_ELASTICITY;
713 bkt = rht_bucket_insert(ht, tbl, hash);
714 data = ERR_PTR(-ENOMEM);
720 if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
722 rht_unlock(tbl, bkt);
724 return rhashtable_insert_slow(ht, key, obj);
727 rht_for_each_from(head, rht_ptr(bkt, tbl, hash), tbl, hash) {
728 struct rhlist_head *plist;
729 struct rhlist_head *list;
734 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
735 rhashtable_compare(&arg, rht_obj(ht, head)))) {
740 data = rht_obj(ht, head);
746 list = container_of(obj, struct rhlist_head, rhead);
747 plist = container_of(head, struct rhlist_head, rhead);
749 RCU_INIT_POINTER(list->next, plist);
750 head = rht_dereference_bucket(head->next, tbl, hash);
751 RCU_INIT_POINTER(list->rhead.next, head);
753 rcu_assign_pointer(*pprev, obj);
754 rht_unlock(tbl, bkt);
756 rht_assign_unlock(tbl, bkt, obj);
764 data = ERR_PTR(-E2BIG);
765 if (unlikely(rht_grow_above_max(ht, tbl)))
768 if (unlikely(rht_grow_above_100(ht, tbl)))
771 /* Inserting at head of list makes unlocking free. */
772 head = rht_ptr(bkt, tbl, hash);
774 RCU_INIT_POINTER(obj->next, head);
776 struct rhlist_head *list;
778 list = container_of(obj, struct rhlist_head, rhead);
779 RCU_INIT_POINTER(list->next, NULL);
782 atomic_inc(&ht->nelems);
783 rht_assign_unlock(tbl, bkt, obj);
785 if (rht_grow_above_75(ht, tbl))
786 schedule_work(&ht->run_work);
795 rht_unlock(tbl, bkt);
800 * rhashtable_insert_fast - insert object into hash table
802 * @obj: pointer to hash head inside object
803 * @params: hash table parameters
805 * Will take the per bucket bitlock to protect against mutual mutations
806 * on the same bucket. Multiple insertions may occur in parallel unless
807 * they map to the same bucket.
809 * It is safe to call this function from atomic context.
811 * Will trigger an automatic deferred table resizing if residency in the
812 * table grows beyond 70%.
814 static inline int rhashtable_insert_fast(
815 struct rhashtable *ht, struct rhash_head *obj,
816 const struct rhashtable_params params)
820 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
824 return ret == NULL ? 0 : -EEXIST;
828 * rhltable_insert_key - insert object into hash list table
829 * @hlt: hash list table
830 * @key: the pointer to the key
831 * @list: pointer to hash list head inside object
832 * @params: hash table parameters
834 * Will take the per bucket bitlock to protect against mutual mutations
835 * on the same bucket. Multiple insertions may occur in parallel unless
836 * they map to the same bucket.
838 * It is safe to call this function from atomic context.
840 * Will trigger an automatic deferred table resizing if residency in the
841 * table grows beyond 70%.
843 static inline int rhltable_insert_key(
844 struct rhltable *hlt, const void *key, struct rhlist_head *list,
845 const struct rhashtable_params params)
847 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
852 * rhltable_insert - insert object into hash list table
853 * @hlt: hash list table
854 * @list: pointer to hash list head inside object
855 * @params: hash table parameters
857 * Will take the per bucket bitlock to protect against mutual mutations
858 * on the same bucket. Multiple insertions may occur in parallel unless
859 * they map to the same bucket.
861 * It is safe to call this function from atomic context.
863 * Will trigger an automatic deferred table resizing if residency in the
864 * table grows beyond 70%.
866 static inline int rhltable_insert(
867 struct rhltable *hlt, struct rhlist_head *list,
868 const struct rhashtable_params params)
870 const char *key = rht_obj(&hlt->ht, &list->rhead);
872 key += params.key_offset;
874 return rhltable_insert_key(hlt, key, list, params);
878 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
880 * @obj: pointer to hash head inside object
881 * @params: hash table parameters
883 * This lookup function may only be used for fixed key hash table (key_len
884 * parameter set). It will BUG() if used inappropriately.
886 * It is safe to call this function from atomic context.
888 * Will trigger an automatic deferred table resizing if residency in the
889 * table grows beyond 70%.
891 static inline int rhashtable_lookup_insert_fast(
892 struct rhashtable *ht, struct rhash_head *obj,
893 const struct rhashtable_params params)
895 const char *key = rht_obj(ht, obj);
898 BUG_ON(ht->p.obj_hashfn);
900 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
905 return ret == NULL ? 0 : -EEXIST;
909 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
911 * @obj: pointer to hash head inside object
912 * @params: hash table parameters
914 * Just like rhashtable_lookup_insert_fast(), but this function returns the
915 * object if it exists, NULL if it did not and the insertion was successful,
916 * and an ERR_PTR otherwise.
918 static inline void *rhashtable_lookup_get_insert_fast(
919 struct rhashtable *ht, struct rhash_head *obj,
920 const struct rhashtable_params params)
922 const char *key = rht_obj(ht, obj);
924 BUG_ON(ht->p.obj_hashfn);
926 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
931 * rhashtable_lookup_insert_key - search and insert object to hash table
935 * @obj: pointer to hash head inside object
936 * @params: hash table parameters
938 * Lookups may occur in parallel with hashtable mutations and resizing.
940 * Will trigger an automatic deferred table resizing if residency in the
941 * table grows beyond 70%.
943 * Returns zero on success.
945 static inline int rhashtable_lookup_insert_key(
946 struct rhashtable *ht, const void *key, struct rhash_head *obj,
947 const struct rhashtable_params params)
951 BUG_ON(!ht->p.obj_hashfn || !key);
953 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
957 return ret == NULL ? 0 : -EEXIST;
961 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
964 * @obj: pointer to hash head inside object
965 * @params: hash table parameters
967 * Just like rhashtable_lookup_insert_key(), but this function returns the
968 * object if it exists, NULL if it does not and the insertion was successful,
969 * and an ERR_PTR otherwise.
971 static inline void *rhashtable_lookup_get_insert_key(
972 struct rhashtable *ht, const void *key, struct rhash_head *obj,
973 const struct rhashtable_params params)
975 BUG_ON(!ht->p.obj_hashfn || !key);
977 return __rhashtable_insert_fast(ht, key, obj, params, false);
980 /* Internal function, please use rhashtable_remove_fast() instead */
981 static inline int __rhashtable_remove_fast_one(
982 struct rhashtable *ht, struct bucket_table *tbl,
983 struct rhash_head *obj, const struct rhashtable_params params,
986 struct rhash_lock_head __rcu **bkt;
987 struct rhash_head __rcu **pprev;
988 struct rhash_head *he;
992 hash = rht_head_hashfn(ht, tbl, obj, params);
993 bkt = rht_bucket_var(tbl, hash);
999 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
1000 struct rhlist_head *list;
1002 list = container_of(he, struct rhlist_head, rhead);
1005 struct rhlist_head __rcu **lpprev;
1013 lpprev = &list->next;
1014 list = rht_dereference_bucket(list->next,
1016 } while (list && obj != &list->rhead);
1021 list = rht_dereference_bucket(list->next, tbl, hash);
1022 RCU_INIT_POINTER(*lpprev, list);
1027 obj = rht_dereference_bucket(obj->next, tbl, hash);
1031 list = rht_dereference_bucket(list->next, tbl, hash);
1033 RCU_INIT_POINTER(list->rhead.next, obj);
1040 rcu_assign_pointer(*pprev, obj);
1041 rht_unlock(tbl, bkt);
1043 rht_assign_unlock(tbl, bkt, obj);
1048 rht_unlock(tbl, bkt);
1051 atomic_dec(&ht->nelems);
1052 if (unlikely(ht->p.automatic_shrinking &&
1053 rht_shrink_below_30(ht, tbl)))
1054 schedule_work(&ht->run_work);
1061 /* Internal function, please use rhashtable_remove_fast() instead */
1062 static inline int __rhashtable_remove_fast(
1063 struct rhashtable *ht, struct rhash_head *obj,
1064 const struct rhashtable_params params, bool rhlist)
1066 struct bucket_table *tbl;
1071 tbl = rht_dereference_rcu(ht->tbl, ht);
1073 /* Because we have already taken (and released) the bucket
1074 * lock in old_tbl, if we find that future_tbl is not yet
1075 * visible then that guarantees the entry to still be in
1076 * the old tbl if it exists.
1078 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1080 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1089 * rhashtable_remove_fast - remove object from hash table
1091 * @obj: pointer to hash head inside object
1092 * @params: hash table parameters
1094 * Since the hash chain is single linked, the removal operation needs to
1095 * walk the bucket chain upon removal. The removal operation is thus
1096 * considerable slow if the hash table is not correctly sized.
1098 * Will automatically shrink the table if permitted when residency drops
1101 * Returns zero on success, -ENOENT if the entry could not be found.
1103 static inline int rhashtable_remove_fast(
1104 struct rhashtable *ht, struct rhash_head *obj,
1105 const struct rhashtable_params params)
1107 return __rhashtable_remove_fast(ht, obj, params, false);
1111 * rhltable_remove - remove object from hash list table
1112 * @hlt: hash list table
1113 * @list: pointer to hash list head inside object
1114 * @params: hash table parameters
1116 * Since the hash chain is single linked, the removal operation needs to
1117 * walk the bucket chain upon removal. The removal operation is thus
1118 * considerable slow if the hash table is not correctly sized.
1120 * Will automatically shrink the table if permitted when residency drops
1123 * Returns zero on success, -ENOENT if the entry could not be found.
1125 static inline int rhltable_remove(
1126 struct rhltable *hlt, struct rhlist_head *list,
1127 const struct rhashtable_params params)
1129 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
1132 /* Internal function, please use rhashtable_replace_fast() instead */
1133 static inline int __rhashtable_replace_fast(
1134 struct rhashtable *ht, struct bucket_table *tbl,
1135 struct rhash_head *obj_old, struct rhash_head *obj_new,
1136 const struct rhashtable_params params)
1138 struct rhash_lock_head __rcu **bkt;
1139 struct rhash_head __rcu **pprev;
1140 struct rhash_head *he;
1144 /* Minimally, the old and new objects must have same hash
1145 * (which should mean identifiers are the same).
1147 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1148 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1151 bkt = rht_bucket_var(tbl, hash);
1158 rht_for_each_from(he, rht_ptr(bkt, tbl, hash), tbl, hash) {
1159 if (he != obj_old) {
1164 rcu_assign_pointer(obj_new->next, obj_old->next);
1166 rcu_assign_pointer(*pprev, obj_new);
1167 rht_unlock(tbl, bkt);
1169 rht_assign_unlock(tbl, bkt, obj_new);
1175 rht_unlock(tbl, bkt);
1182 * rhashtable_replace_fast - replace an object in hash table
1184 * @obj_old: pointer to hash head inside object being replaced
1185 * @obj_new: pointer to hash head inside object which is new
1186 * @params: hash table parameters
1188 * Replacing an object doesn't affect the number of elements in the hash table
1189 * or bucket, so we don't need to worry about shrinking or expanding the
1192 * Returns zero on success, -ENOENT if the entry could not be found,
1193 * -EINVAL if hash is not the same for the old and new objects.
1195 static inline int rhashtable_replace_fast(
1196 struct rhashtable *ht, struct rhash_head *obj_old,
1197 struct rhash_head *obj_new,
1198 const struct rhashtable_params params)
1200 struct bucket_table *tbl;
1205 tbl = rht_dereference_rcu(ht->tbl, ht);
1207 /* Because we have already taken (and released) the bucket
1208 * lock in old_tbl, if we find that future_tbl is not yet
1209 * visible then that guarantees the entry to still be in
1210 * the old tbl if it exists.
1212 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1213 obj_new, params)) &&
1214 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1223 * rhltable_walk_enter - Initialise an iterator
1224 * @hlt: Table to walk over
1225 * @iter: Hash table Iterator
1227 * This function prepares a hash table walk.
1229 * Note that if you restart a walk after rhashtable_walk_stop you
1230 * may see the same object twice. Also, you may miss objects if
1231 * there are removals in between rhashtable_walk_stop and the next
1232 * call to rhashtable_walk_start.
1234 * For a completely stable walk you should construct your own data
1235 * structure outside the hash table.
1237 * This function may be called from any process context, including
1238 * non-preemptable context, but cannot be called from softirq or
1241 * You must call rhashtable_walk_exit after this function returns.
1243 static inline void rhltable_walk_enter(struct rhltable *hlt,
1244 struct rhashtable_iter *iter)
1246 return rhashtable_walk_enter(&hlt->ht, iter);
1250 * rhltable_free_and_destroy - free elements and destroy hash list table
1251 * @hlt: the hash list table to destroy
1252 * @free_fn: callback to release resources of element
1253 * @arg: pointer passed to free_fn
1255 * See documentation for rhashtable_free_and_destroy.
1257 static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1258 void (*free_fn)(void *ptr,
1262 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1265 static inline void rhltable_destroy(struct rhltable *hlt)
1267 return rhltable_free_and_destroy(hlt, NULL, NULL);
1270 #endif /* _LINUX_RHASHTABLE_H */