1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Resizable, Scalable, Concurrent Hash Table
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/workqueue.h>
26 #include <linux/rculist.h>
28 #include <linux/rhashtable-types.h>
30 * The end of the chain is marked with a special nulls marks which has
31 * the least significant bit set.
34 /* Maximum chain length before rehash
36 * The maximum (not average) chain length grows with the size of the hash
37 * table, at a rate of (log N)/(log log N).
39 * The value of 16 is selected so that even if the hash table grew to
40 * 2^32 you would not expect the maximum chain length to exceed it
41 * unless we are under attack (or extremely unlucky).
43 * As this limit is only to detect attacks, we don't need to set it to a
44 * lower value as you'd need the chain length to vastly exceed 16 to have
45 * any real effect on the system.
47 #define RHT_ELASTICITY 16u
50 * struct bucket_table - Table of hash buckets
51 * @size: Number of hash buckets
52 * @nest: Number of bits of first-level nested table.
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 * @ntbl: Nested table used when out of memory.
61 * @buckets: size * hash buckets
68 unsigned int locks_mask;
70 struct list_head walkers;
73 struct bucket_table __rcu *future_tbl;
75 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
78 #define INIT_RHT_NULLS_HEAD(ptr) \
79 ((ptr) = (typeof(ptr)) NULLS_MARKER(0))
81 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
83 return ((unsigned long) ptr & 1);
86 static inline void *rht_obj(const struct rhashtable *ht,
87 const struct rhash_head *he)
89 return (char *)he - ht->p.head_offset;
92 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
95 return hash & (tbl->size - 1);
98 static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
99 const void *key, const struct rhashtable_params params,
100 unsigned int hash_rnd)
104 /* params must be equal to ht->p if it isn't constant. */
105 if (!__builtin_constant_p(params.key_len))
106 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
107 else if (params.key_len) {
108 unsigned int key_len = params.key_len;
111 hash = params.hashfn(key, key_len, hash_rnd);
112 else if (key_len & (sizeof(u32) - 1))
113 hash = jhash(key, key_len, hash_rnd);
115 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
117 unsigned int key_len = ht->p.key_len;
120 hash = params.hashfn(key, key_len, hash_rnd);
122 hash = jhash(key, key_len, hash_rnd);
128 static inline unsigned int rht_key_hashfn(
129 struct rhashtable *ht, const struct bucket_table *tbl,
130 const void *key, const struct rhashtable_params params)
132 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
134 return rht_bucket_index(tbl, hash);
137 static inline unsigned int rht_head_hashfn(
138 struct rhashtable *ht, const struct bucket_table *tbl,
139 const struct rhash_head *he, const struct rhashtable_params params)
141 const char *ptr = rht_obj(ht, he);
143 return likely(params.obj_hashfn) ?
144 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
147 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
151 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
153 * @tbl: current table
155 static inline bool rht_grow_above_75(const struct rhashtable *ht,
156 const struct bucket_table *tbl)
158 /* Expand table when exceeding 75% load */
159 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
160 (!ht->p.max_size || tbl->size < ht->p.max_size);
164 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
166 * @tbl: current table
168 static inline bool rht_shrink_below_30(const struct rhashtable *ht,
169 const struct bucket_table *tbl)
171 /* Shrink table beneath 30% load */
172 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
173 tbl->size > ht->p.min_size;
177 * rht_grow_above_100 - returns true if nelems > table-size
179 * @tbl: current table
181 static inline bool rht_grow_above_100(const struct rhashtable *ht,
182 const struct bucket_table *tbl)
184 return atomic_read(&ht->nelems) > tbl->size &&
185 (!ht->p.max_size || tbl->size < ht->p.max_size);
189 * rht_grow_above_max - returns true if table is above maximum
191 * @tbl: current table
193 static inline bool rht_grow_above_max(const struct rhashtable *ht,
194 const struct bucket_table *tbl)
196 return atomic_read(&ht->nelems) >= ht->max_elems;
199 /* The bucket lock is selected based on the hash and protects mutations
200 * on a group of hash buckets.
202 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
203 * a single lock always covers both buckets which may both contains
204 * entries which link to the same bucket of the old table during resizing.
205 * This allows to simplify the locking as locking the bucket in both
206 * tables during resize always guarantee protection.
208 * IMPORTANT: When holding the bucket lock of both the old and new table
209 * during expansions and shrinking, the old bucket lock must always be
212 static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
215 return &tbl->locks[hash & tbl->locks_mask];
218 #ifdef CONFIG_PROVE_LOCKING
219 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
220 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
222 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
227 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
232 #endif /* CONFIG_PROVE_LOCKING */
234 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
235 struct rhash_head *obj);
237 void rhashtable_walk_enter(struct rhashtable *ht,
238 struct rhashtable_iter *iter);
239 void rhashtable_walk_exit(struct rhashtable_iter *iter);
240 int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
242 static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
244 (void)rhashtable_walk_start_check(iter);
247 void *rhashtable_walk_next(struct rhashtable_iter *iter);
248 void *rhashtable_walk_peek(struct rhashtable_iter *iter);
249 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
251 void rhashtable_free_and_destroy(struct rhashtable *ht,
252 void (*free_fn)(void *ptr, void *arg),
254 void rhashtable_destroy(struct rhashtable *ht);
256 struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
258 struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
259 struct bucket_table *tbl,
262 #define rht_dereference(p, ht) \
263 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
265 #define rht_dereference_rcu(p, ht) \
266 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
268 #define rht_dereference_bucket(p, tbl, hash) \
269 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
271 #define rht_dereference_bucket_rcu(p, tbl, hash) \
272 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
274 #define rht_entry(tpos, pos, member) \
275 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
277 static inline struct rhash_head __rcu *const *rht_bucket(
278 const struct bucket_table *tbl, unsigned int hash)
280 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
284 static inline struct rhash_head __rcu **rht_bucket_var(
285 struct bucket_table *tbl, unsigned int hash)
287 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
291 static inline struct rhash_head __rcu **rht_bucket_insert(
292 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
294 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
299 * rht_for_each_continue - continue iterating over hash chain
300 * @pos: the &struct rhash_head to use as a loop cursor.
301 * @head: the previous &struct rhash_head to continue from
302 * @tbl: the &struct bucket_table
303 * @hash: the hash value / bucket index
305 #define rht_for_each_continue(pos, head, tbl, hash) \
306 for (pos = rht_dereference_bucket(head, tbl, hash); \
307 !rht_is_a_nulls(pos); \
308 pos = rht_dereference_bucket((pos)->next, tbl, hash))
311 * rht_for_each - iterate over hash chain
312 * @pos: the &struct rhash_head to use as a loop cursor.
313 * @tbl: the &struct bucket_table
314 * @hash: the hash value / bucket index
316 #define rht_for_each(pos, tbl, hash) \
317 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
320 * rht_for_each_entry_continue - continue iterating over hash chain
321 * @tpos: the type * to use as a loop cursor.
322 * @pos: the &struct rhash_head to use as a loop cursor.
323 * @head: the previous &struct rhash_head to continue from
324 * @tbl: the &struct bucket_table
325 * @hash: the hash value / bucket index
326 * @member: name of the &struct rhash_head within the hashable struct.
328 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
329 for (pos = rht_dereference_bucket(head, tbl, hash); \
330 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
331 pos = rht_dereference_bucket((pos)->next, tbl, hash))
334 * rht_for_each_entry - iterate over hash chain of given type
335 * @tpos: the type * to use as a loop cursor.
336 * @pos: the &struct rhash_head to use as a loop cursor.
337 * @tbl: the &struct bucket_table
338 * @hash: the hash value / bucket index
339 * @member: name of the &struct rhash_head within the hashable struct.
341 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
342 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
346 * rht_for_each_entry_safe - safely iterate over hash chain of given type
347 * @tpos: the type * to use as a loop cursor.
348 * @pos: the &struct rhash_head to use as a loop cursor.
349 * @next: the &struct rhash_head to use as next in loop cursor.
350 * @tbl: the &struct bucket_table
351 * @hash: the hash value / bucket index
352 * @member: name of the &struct rhash_head within the hashable struct.
354 * This hash chain list-traversal primitive allows for the looped code to
355 * remove the loop cursor from the list.
357 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
358 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
359 next = !rht_is_a_nulls(pos) ? \
360 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
361 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
363 next = !rht_is_a_nulls(pos) ? \
364 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
367 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
368 * @pos: the &struct rhash_head to use as a loop cursor.
369 * @head: the previous &struct rhash_head to continue from
370 * @tbl: the &struct bucket_table
371 * @hash: the hash value / bucket index
373 * This hash chain list-traversal primitive may safely run concurrently with
374 * the _rcu mutation primitives such as rhashtable_insert() as long as the
375 * traversal is guarded by rcu_read_lock().
377 #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
378 for (({barrier(); }), \
379 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
380 !rht_is_a_nulls(pos); \
381 pos = rcu_dereference_raw(pos->next))
384 * rht_for_each_rcu - iterate over rcu hash chain
385 * @pos: the &struct rhash_head to use as a loop cursor.
386 * @tbl: the &struct bucket_table
387 * @hash: the hash value / bucket index
389 * This hash chain list-traversal primitive may safely run concurrently with
390 * the _rcu mutation primitives such as rhashtable_insert() as long as the
391 * traversal is guarded by rcu_read_lock().
393 #define rht_for_each_rcu(pos, tbl, hash) \
394 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
397 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
398 * @tpos: the type * to use as a loop cursor.
399 * @pos: the &struct rhash_head to use as a loop cursor.
400 * @head: the previous &struct rhash_head to continue from
401 * @tbl: the &struct bucket_table
402 * @hash: the hash value / bucket index
403 * @member: name of the &struct rhash_head within the hashable struct.
405 * This hash chain list-traversal primitive may safely run concurrently with
406 * the _rcu mutation primitives such as rhashtable_insert() as long as the
407 * traversal is guarded by rcu_read_lock().
409 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
410 for (({barrier(); }), \
411 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
412 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
413 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
416 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
417 * @tpos: the type * to use as a loop cursor.
418 * @pos: the &struct rhash_head to use as a loop cursor.
419 * @tbl: the &struct bucket_table
420 * @hash: the hash value / bucket index
421 * @member: name of the &struct rhash_head within the hashable struct.
423 * This hash chain list-traversal primitive may safely run concurrently with
424 * the _rcu mutation primitives such as rhashtable_insert() as long as the
425 * traversal is guarded by rcu_read_lock().
427 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
428 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
432 * rhl_for_each_rcu - iterate over rcu hash table list
433 * @pos: the &struct rlist_head to use as a loop cursor.
434 * @list: the head of the list
436 * This hash chain list-traversal primitive should be used on the
437 * list returned by rhltable_lookup.
439 #define rhl_for_each_rcu(pos, list) \
440 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
443 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
444 * @tpos: the type * to use as a loop cursor.
445 * @pos: the &struct rlist_head to use as a loop cursor.
446 * @list: the head of the list
447 * @member: name of the &struct rlist_head within the hashable struct.
449 * This hash chain list-traversal primitive should be used on the
450 * list returned by rhltable_lookup.
452 #define rhl_for_each_entry_rcu(tpos, pos, list, member) \
453 for (pos = list; pos && rht_entry(tpos, pos, member); \
454 pos = rcu_dereference_raw(pos->next))
456 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
459 struct rhashtable *ht = arg->ht;
460 const char *ptr = obj;
462 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
465 /* Internal function, do not use. */
466 static inline struct rhash_head *__rhashtable_lookup(
467 struct rhashtable *ht, const void *key,
468 const struct rhashtable_params params)
470 struct rhashtable_compare_arg arg = {
474 struct bucket_table *tbl;
475 struct rhash_head *he;
478 tbl = rht_dereference_rcu(ht->tbl, ht);
480 hash = rht_key_hashfn(ht, tbl, key, params);
481 rht_for_each_rcu(he, tbl, hash) {
482 if (params.obj_cmpfn ?
483 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
484 rhashtable_compare(&arg, rht_obj(ht, he)))
489 /* Ensure we see any new tables. */
492 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
500 * rhashtable_lookup - search hash table
502 * @key: the pointer to the key
503 * @params: hash table parameters
505 * Computes the hash value for the key and traverses the bucket chain looking
506 * for a entry with an identical key. The first matching entry is returned.
508 * This must only be called under the RCU read lock.
510 * Returns the first entry on which the compare function returned true.
512 static inline void *rhashtable_lookup(
513 struct rhashtable *ht, const void *key,
514 const struct rhashtable_params params)
516 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
518 return he ? rht_obj(ht, he) : NULL;
522 * rhashtable_lookup_fast - search hash table, without RCU read lock
524 * @key: the pointer to the key
525 * @params: hash table parameters
527 * Computes the hash value for the key and traverses the bucket chain looking
528 * for a entry with an identical key. The first matching entry is returned.
530 * Only use this function when you have other mechanisms guaranteeing
531 * that the object won't go away after the RCU read lock is released.
533 * Returns the first entry on which the compare function returned true.
535 static inline void *rhashtable_lookup_fast(
536 struct rhashtable *ht, const void *key,
537 const struct rhashtable_params params)
542 obj = rhashtable_lookup(ht, key, params);
549 * rhltable_lookup - search hash list table
551 * @key: the pointer to the key
552 * @params: hash table parameters
554 * Computes the hash value for the key and traverses the bucket chain looking
555 * for a entry with an identical key. All matching entries are returned
558 * This must only be called under the RCU read lock.
560 * Returns the list of entries that match the given key.
562 static inline struct rhlist_head *rhltable_lookup(
563 struct rhltable *hlt, const void *key,
564 const struct rhashtable_params params)
566 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
568 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
571 /* Internal function, please use rhashtable_insert_fast() instead. This
572 * function returns the existing element already in hashes in there is a clash,
573 * otherwise it returns an error via ERR_PTR().
575 static inline void *__rhashtable_insert_fast(
576 struct rhashtable *ht, const void *key, struct rhash_head *obj,
577 const struct rhashtable_params params, bool rhlist)
579 struct rhashtable_compare_arg arg = {
583 struct rhash_head __rcu **pprev;
584 struct bucket_table *tbl;
585 struct rhash_head *head;
593 tbl = rht_dereference_rcu(ht->tbl, ht);
594 hash = rht_head_hashfn(ht, tbl, obj, params);
595 lock = rht_bucket_lock(tbl, hash);
598 if (unlikely(rcu_access_pointer(tbl->future_tbl))) {
600 spin_unlock_bh(lock);
602 return rhashtable_insert_slow(ht, key, obj);
605 elasticity = RHT_ELASTICITY;
606 pprev = rht_bucket_insert(ht, tbl, hash);
607 data = ERR_PTR(-ENOMEM);
611 rht_for_each_continue(head, *pprev, tbl, hash) {
612 struct rhlist_head *plist;
613 struct rhlist_head *list;
618 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
619 rhashtable_compare(&arg, rht_obj(ht, head)))) {
624 data = rht_obj(ht, head);
630 list = container_of(obj, struct rhlist_head, rhead);
631 plist = container_of(head, struct rhlist_head, rhead);
633 RCU_INIT_POINTER(list->next, plist);
634 head = rht_dereference_bucket(head->next, tbl, hash);
635 RCU_INIT_POINTER(list->rhead.next, head);
636 rcu_assign_pointer(*pprev, obj);
644 data = ERR_PTR(-E2BIG);
645 if (unlikely(rht_grow_above_max(ht, tbl)))
648 if (unlikely(rht_grow_above_100(ht, tbl)))
651 head = rht_dereference_bucket(*pprev, tbl, hash);
653 RCU_INIT_POINTER(obj->next, head);
655 struct rhlist_head *list;
657 list = container_of(obj, struct rhlist_head, rhead);
658 RCU_INIT_POINTER(list->next, NULL);
661 rcu_assign_pointer(*pprev, obj);
663 atomic_inc(&ht->nelems);
664 if (rht_grow_above_75(ht, tbl))
665 schedule_work(&ht->run_work);
671 spin_unlock_bh(lock);
678 * rhashtable_insert_fast - insert object into hash table
680 * @obj: pointer to hash head inside object
681 * @params: hash table parameters
683 * Will take a per bucket spinlock to protect against mutual mutations
684 * on the same bucket. Multiple insertions may occur in parallel unless
685 * they map to the same bucket lock.
687 * It is safe to call this function from atomic context.
689 * Will trigger an automatic deferred table resizing if residency in the
690 * table grows beyond 70%.
692 static inline int rhashtable_insert_fast(
693 struct rhashtable *ht, struct rhash_head *obj,
694 const struct rhashtable_params params)
698 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
702 return ret == NULL ? 0 : -EEXIST;
706 * rhltable_insert_key - insert object into hash list table
707 * @hlt: hash list table
708 * @key: the pointer to the key
709 * @list: pointer to hash list head inside object
710 * @params: hash table parameters
712 * Will take a per bucket spinlock to protect against mutual mutations
713 * on the same bucket. Multiple insertions may occur in parallel unless
714 * they map to the same bucket lock.
716 * It is safe to call this function from atomic context.
718 * Will trigger an automatic deferred table resizing if residency in the
719 * table grows beyond 70%.
721 static inline int rhltable_insert_key(
722 struct rhltable *hlt, const void *key, struct rhlist_head *list,
723 const struct rhashtable_params params)
725 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
730 * rhltable_insert - insert object into hash list table
731 * @hlt: hash list table
732 * @list: pointer to hash list head inside object
733 * @params: hash table parameters
735 * Will take a per bucket spinlock to protect against mutual mutations
736 * on the same bucket. Multiple insertions may occur in parallel unless
737 * they map to the same bucket lock.
739 * It is safe to call this function from atomic context.
741 * Will trigger an automatic deferred table resizing if residency in the
742 * table grows beyond 70%.
744 static inline int rhltable_insert(
745 struct rhltable *hlt, struct rhlist_head *list,
746 const struct rhashtable_params params)
748 const char *key = rht_obj(&hlt->ht, &list->rhead);
750 key += params.key_offset;
752 return rhltable_insert_key(hlt, key, list, params);
756 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
758 * @obj: pointer to hash head inside object
759 * @params: hash table parameters
761 * Locks down the bucket chain in both the old and new table if a resize
762 * is in progress to ensure that writers can't remove from the old table
763 * and can't insert to the new table during the atomic operation of search
764 * and insertion. Searches for duplicates in both the old and new table if
765 * a resize is in progress.
767 * This lookup function may only be used for fixed key hash table (key_len
768 * parameter set). It will BUG() if used inappropriately.
770 * It is safe to call this function from atomic context.
772 * Will trigger an automatic deferred table resizing if residency in the
773 * table grows beyond 70%.
775 static inline int rhashtable_lookup_insert_fast(
776 struct rhashtable *ht, struct rhash_head *obj,
777 const struct rhashtable_params params)
779 const char *key = rht_obj(ht, obj);
782 BUG_ON(ht->p.obj_hashfn);
784 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
789 return ret == NULL ? 0 : -EEXIST;
793 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
795 * @obj: pointer to hash head inside object
796 * @params: hash table parameters
798 * Just like rhashtable_lookup_insert_fast(), but this function returns the
799 * object if it exists, NULL if it did not and the insertion was successful,
800 * and an ERR_PTR otherwise.
802 static inline void *rhashtable_lookup_get_insert_fast(
803 struct rhashtable *ht, struct rhash_head *obj,
804 const struct rhashtable_params params)
806 const char *key = rht_obj(ht, obj);
808 BUG_ON(ht->p.obj_hashfn);
810 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
815 * rhashtable_lookup_insert_key - search and insert object to hash table
819 * @obj: pointer to hash head inside object
820 * @params: hash table parameters
822 * Locks down the bucket chain in both the old and new table if a resize
823 * is in progress to ensure that writers can't remove from the old table
824 * and can't insert to the new table during the atomic operation of search
825 * and insertion. Searches for duplicates in both the old and new table if
826 * a resize is in progress.
828 * Lookups may occur in parallel with hashtable mutations and resizing.
830 * Will trigger an automatic deferred table resizing if residency in the
831 * table grows beyond 70%.
833 * Returns zero on success.
835 static inline int rhashtable_lookup_insert_key(
836 struct rhashtable *ht, const void *key, struct rhash_head *obj,
837 const struct rhashtable_params params)
841 BUG_ON(!ht->p.obj_hashfn || !key);
843 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
847 return ret == NULL ? 0 : -EEXIST;
851 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
853 * @obj: pointer to hash head inside object
854 * @params: hash table parameters
855 * @data: pointer to element data already in hashes
857 * Just like rhashtable_lookup_insert_key(), but this function returns the
858 * object if it exists, NULL if it does not and the insertion was successful,
859 * and an ERR_PTR otherwise.
861 static inline void *rhashtable_lookup_get_insert_key(
862 struct rhashtable *ht, const void *key, struct rhash_head *obj,
863 const struct rhashtable_params params)
865 BUG_ON(!ht->p.obj_hashfn || !key);
867 return __rhashtable_insert_fast(ht, key, obj, params, false);
870 /* Internal function, please use rhashtable_remove_fast() instead */
871 static inline int __rhashtable_remove_fast_one(
872 struct rhashtable *ht, struct bucket_table *tbl,
873 struct rhash_head *obj, const struct rhashtable_params params,
876 struct rhash_head __rcu **pprev;
877 struct rhash_head *he;
882 hash = rht_head_hashfn(ht, tbl, obj, params);
883 lock = rht_bucket_lock(tbl, hash);
887 pprev = rht_bucket_var(tbl, hash);
888 rht_for_each_continue(he, *pprev, tbl, hash) {
889 struct rhlist_head *list;
891 list = container_of(he, struct rhlist_head, rhead);
894 struct rhlist_head __rcu **lpprev;
902 lpprev = &list->next;
903 list = rht_dereference_bucket(list->next,
905 } while (list && obj != &list->rhead);
910 list = rht_dereference_bucket(list->next, tbl, hash);
911 RCU_INIT_POINTER(*lpprev, list);
916 obj = rht_dereference_bucket(obj->next, tbl, hash);
920 list = rht_dereference_bucket(list->next, tbl, hash);
922 RCU_INIT_POINTER(list->rhead.next, obj);
928 rcu_assign_pointer(*pprev, obj);
932 spin_unlock_bh(lock);
935 atomic_dec(&ht->nelems);
936 if (unlikely(ht->p.automatic_shrinking &&
937 rht_shrink_below_30(ht, tbl)))
938 schedule_work(&ht->run_work);
945 /* Internal function, please use rhashtable_remove_fast() instead */
946 static inline int __rhashtable_remove_fast(
947 struct rhashtable *ht, struct rhash_head *obj,
948 const struct rhashtable_params params, bool rhlist)
950 struct bucket_table *tbl;
955 tbl = rht_dereference_rcu(ht->tbl, ht);
957 /* Because we have already taken (and released) the bucket
958 * lock in old_tbl, if we find that future_tbl is not yet
959 * visible then that guarantees the entry to still be in
960 * the old tbl if it exists.
962 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
964 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
973 * rhashtable_remove_fast - remove object from hash table
975 * @obj: pointer to hash head inside object
976 * @params: hash table parameters
978 * Since the hash chain is single linked, the removal operation needs to
979 * walk the bucket chain upon removal. The removal operation is thus
980 * considerable slow if the hash table is not correctly sized.
982 * Will automatically shrink the table if permitted when residency drops
985 * Returns zero on success, -ENOENT if the entry could not be found.
987 static inline int rhashtable_remove_fast(
988 struct rhashtable *ht, struct rhash_head *obj,
989 const struct rhashtable_params params)
991 return __rhashtable_remove_fast(ht, obj, params, false);
995 * rhltable_remove - remove object from hash list table
996 * @hlt: hash list table
997 * @list: pointer to hash list head inside object
998 * @params: hash table parameters
1000 * Since the hash chain is single linked, the removal operation needs to
1001 * walk the bucket chain upon removal. The removal operation is thus
1002 * considerable slow if the hash table is not correctly sized.
1004 * Will automatically shrink the table if permitted when residency drops
1007 * Returns zero on success, -ENOENT if the entry could not be found.
1009 static inline int rhltable_remove(
1010 struct rhltable *hlt, struct rhlist_head *list,
1011 const struct rhashtable_params params)
1013 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
1016 /* Internal function, please use rhashtable_replace_fast() instead */
1017 static inline int __rhashtable_replace_fast(
1018 struct rhashtable *ht, struct bucket_table *tbl,
1019 struct rhash_head *obj_old, struct rhash_head *obj_new,
1020 const struct rhashtable_params params)
1022 struct rhash_head __rcu **pprev;
1023 struct rhash_head *he;
1028 /* Minimally, the old and new objects must have same hash
1029 * (which should mean identifiers are the same).
1031 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1032 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1035 lock = rht_bucket_lock(tbl, hash);
1039 pprev = rht_bucket_var(tbl, hash);
1040 rht_for_each_continue(he, *pprev, tbl, hash) {
1041 if (he != obj_old) {
1046 rcu_assign_pointer(obj_new->next, obj_old->next);
1047 rcu_assign_pointer(*pprev, obj_new);
1052 spin_unlock_bh(lock);
1058 * rhashtable_replace_fast - replace an object in hash table
1060 * @obj_old: pointer to hash head inside object being replaced
1061 * @obj_new: pointer to hash head inside object which is new
1062 * @params: hash table parameters
1064 * Replacing an object doesn't affect the number of elements in the hash table
1065 * or bucket, so we don't need to worry about shrinking or expanding the
1068 * Returns zero on success, -ENOENT if the entry could not be found,
1069 * -EINVAL if hash is not the same for the old and new objects.
1071 static inline int rhashtable_replace_fast(
1072 struct rhashtable *ht, struct rhash_head *obj_old,
1073 struct rhash_head *obj_new,
1074 const struct rhashtable_params params)
1076 struct bucket_table *tbl;
1081 tbl = rht_dereference_rcu(ht->tbl, ht);
1083 /* Because we have already taken (and released) the bucket
1084 * lock in old_tbl, if we find that future_tbl is not yet
1085 * visible then that guarantees the entry to still be in
1086 * the old tbl if it exists.
1088 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1089 obj_new, params)) &&
1090 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1098 /* Obsolete function, do not use in new code. */
1099 static inline int rhashtable_walk_init(struct rhashtable *ht,
1100 struct rhashtable_iter *iter, gfp_t gfp)
1102 rhashtable_walk_enter(ht, iter);
1107 * rhltable_walk_enter - Initialise an iterator
1108 * @hlt: Table to walk over
1109 * @iter: Hash table Iterator
1111 * This function prepares a hash table walk.
1113 * Note that if you restart a walk after rhashtable_walk_stop you
1114 * may see the same object twice. Also, you may miss objects if
1115 * there are removals in between rhashtable_walk_stop and the next
1116 * call to rhashtable_walk_start.
1118 * For a completely stable walk you should construct your own data
1119 * structure outside the hash table.
1121 * This function may be called from any process context, including
1122 * non-preemptable context, but cannot be called from softirq or
1125 * You must call rhashtable_walk_exit after this function returns.
1127 static inline void rhltable_walk_enter(struct rhltable *hlt,
1128 struct rhashtable_iter *iter)
1130 return rhashtable_walk_enter(&hlt->ht, iter);
1134 * rhltable_free_and_destroy - free elements and destroy hash list table
1135 * @hlt: the hash list table to destroy
1136 * @free_fn: callback to release resources of element
1137 * @arg: pointer passed to free_fn
1139 * See documentation for rhashtable_free_and_destroy.
1141 static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1142 void (*free_fn)(void *ptr,
1146 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1149 static inline void rhltable_destroy(struct rhltable *hlt)
1151 return rhltable_free_and_destroy(hlt, NULL, NULL);
1154 #endif /* _LINUX_RHASHTABLE_H */