Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[cris-mirror.git] / include / linux / rhashtable.h
blobc9df2527e0cdd60cef38e7d614dd72f5df21a4fc
1 /*
2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2015-2016 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>
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
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.
17 #ifndef _LINUX_RHASHTABLE_H
18 #define _LINUX_RHASHTABLE_H
20 #include <linux/atomic.h>
21 #include <linux/compiler.h>
22 #include <linux/err.h>
23 #include <linux/errno.h>
24 #include <linux/jhash.h>
25 #include <linux/list_nulls.h>
26 #include <linux/workqueue.h>
27 #include <linux/mutex.h>
28 #include <linux/rculist.h>
31 * The end of the chain is marked with a special nulls marks which has
32 * the following format:
34 * +-------+-----------------------------------------------------+-+
35 * | Base | Hash |1|
36 * +-------+-----------------------------------------------------+-+
38 * Base (4 bits) : Reserved to distinguish between multiple tables.
39 * Specified via &struct rhashtable_params.nulls_base.
40 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
41 * 1 (1 bit) : Nulls marker (always set)
43 * The remaining bits of the next pointer remain unused for now.
45 #define RHT_BASE_BITS 4
46 #define RHT_HASH_BITS 27
47 #define RHT_BASE_SHIFT RHT_HASH_BITS
49 /* Base bits plus 1 bit for nulls marker */
50 #define RHT_HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
52 /* Maximum chain length before rehash
54 * The maximum (not average) chain length grows with the size of the hash
55 * table, at a rate of (log N)/(log log N).
57 * The value of 16 is selected so that even if the hash table grew to
58 * 2^32 you would not expect the maximum chain length to exceed it
59 * unless we are under attack (or extremely unlucky).
61 * As this limit is only to detect attacks, we don't need to set it to a
62 * lower value as you'd need the chain length to vastly exceed 16 to have
63 * any real effect on the system.
65 #define RHT_ELASTICITY 16u
67 struct rhash_head {
68 struct rhash_head __rcu *next;
71 struct rhlist_head {
72 struct rhash_head rhead;
73 struct rhlist_head __rcu *next;
76 /**
77 * struct bucket_table - Table of hash buckets
78 * @size: Number of hash buckets
79 * @nest: Number of bits of first-level nested table.
80 * @rehash: Current bucket being rehashed
81 * @hash_rnd: Random seed to fold into hash
82 * @locks_mask: Mask to apply before accessing locks[]
83 * @locks: Array of spinlocks protecting individual buckets
84 * @walkers: List of active walkers
85 * @rcu: RCU structure for freeing the table
86 * @future_tbl: Table under construction during rehashing
87 * @ntbl: Nested table used when out of memory.
88 * @buckets: size * hash buckets
90 struct bucket_table {
91 unsigned int size;
92 unsigned int nest;
93 unsigned int rehash;
94 u32 hash_rnd;
95 unsigned int locks_mask;
96 spinlock_t *locks;
97 struct list_head walkers;
98 struct rcu_head rcu;
100 struct bucket_table __rcu *future_tbl;
102 struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
106 * struct rhashtable_compare_arg - Key for the function rhashtable_compare
107 * @ht: Hash table
108 * @key: Key to compare against
110 struct rhashtable_compare_arg {
111 struct rhashtable *ht;
112 const void *key;
115 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
116 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 len, u32 seed);
117 typedef int (*rht_obj_cmpfn_t)(struct rhashtable_compare_arg *arg,
118 const void *obj);
120 struct rhashtable;
123 * struct rhashtable_params - Hash table construction parameters
124 * @nelem_hint: Hint on number of elements, should be 75% of desired size
125 * @key_len: Length of key
126 * @key_offset: Offset of key in struct to be hashed
127 * @head_offset: Offset of rhash_head in struct to be hashed
128 * @max_size: Maximum size while expanding
129 * @min_size: Minimum size while shrinking
130 * @locks_mul: Number of bucket locks to allocate per cpu (default: 32)
131 * @automatic_shrinking: Enable automatic shrinking of tables
132 * @nulls_base: Base value to generate nulls marker
133 * @hashfn: Hash function (default: jhash2 if !(key_len % 4), or jhash)
134 * @obj_hashfn: Function to hash object
135 * @obj_cmpfn: Function to compare key with object
137 struct rhashtable_params {
138 u16 nelem_hint;
139 u16 key_len;
140 u16 key_offset;
141 u16 head_offset;
142 unsigned int max_size;
143 u16 min_size;
144 bool automatic_shrinking;
145 u8 locks_mul;
146 u32 nulls_base;
147 rht_hashfn_t hashfn;
148 rht_obj_hashfn_t obj_hashfn;
149 rht_obj_cmpfn_t obj_cmpfn;
153 * struct rhashtable - Hash table handle
154 * @tbl: Bucket table
155 * @nelems: Number of elements in table
156 * @key_len: Key length for hashfn
157 * @p: Configuration parameters
158 * @max_elems: Maximum number of elements in table
159 * @rhlist: True if this is an rhltable
160 * @run_work: Deferred worker to expand/shrink asynchronously
161 * @mutex: Mutex to protect current/future table swapping
162 * @lock: Spin lock to protect walker list
164 struct rhashtable {
165 struct bucket_table __rcu *tbl;
166 atomic_t nelems;
167 unsigned int key_len;
168 struct rhashtable_params p;
169 unsigned int max_elems;
170 bool rhlist;
171 struct work_struct run_work;
172 struct mutex mutex;
173 spinlock_t lock;
177 * struct rhltable - Hash table with duplicate objects in a list
178 * @ht: Underlying rhtable
180 struct rhltable {
181 struct rhashtable ht;
185 * struct rhashtable_walker - Hash table walker
186 * @list: List entry on list of walkers
187 * @tbl: The table that we were walking over
189 struct rhashtable_walker {
190 struct list_head list;
191 struct bucket_table *tbl;
195 * struct rhashtable_iter - Hash table iterator
196 * @ht: Table to iterate through
197 * @p: Current pointer
198 * @list: Current hash list pointer
199 * @walker: Associated rhashtable walker
200 * @slot: Current slot
201 * @skip: Number of entries to skip in slot
203 struct rhashtable_iter {
204 struct rhashtable *ht;
205 struct rhash_head *p;
206 struct rhlist_head *list;
207 struct rhashtable_walker walker;
208 unsigned int slot;
209 unsigned int skip;
210 bool end_of_table;
213 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
215 return NULLS_MARKER(ht->p.nulls_base + hash);
218 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
219 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
221 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
223 return ((unsigned long) ptr & 1);
226 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
228 return ((unsigned long) ptr) >> 1;
231 static inline void *rht_obj(const struct rhashtable *ht,
232 const struct rhash_head *he)
234 return (char *)he - ht->p.head_offset;
237 static inline unsigned int rht_bucket_index(const struct bucket_table *tbl,
238 unsigned int hash)
240 return (hash >> RHT_HASH_RESERVED_SPACE) & (tbl->size - 1);
243 static inline unsigned int rht_key_get_hash(struct rhashtable *ht,
244 const void *key, const struct rhashtable_params params,
245 unsigned int hash_rnd)
247 unsigned int hash;
249 /* params must be equal to ht->p if it isn't constant. */
250 if (!__builtin_constant_p(params.key_len))
251 hash = ht->p.hashfn(key, ht->key_len, hash_rnd);
252 else if (params.key_len) {
253 unsigned int key_len = params.key_len;
255 if (params.hashfn)
256 hash = params.hashfn(key, key_len, hash_rnd);
257 else if (key_len & (sizeof(u32) - 1))
258 hash = jhash(key, key_len, hash_rnd);
259 else
260 hash = jhash2(key, key_len / sizeof(u32), hash_rnd);
261 } else {
262 unsigned int key_len = ht->p.key_len;
264 if (params.hashfn)
265 hash = params.hashfn(key, key_len, hash_rnd);
266 else
267 hash = jhash(key, key_len, hash_rnd);
270 return hash;
273 static inline unsigned int rht_key_hashfn(
274 struct rhashtable *ht, const struct bucket_table *tbl,
275 const void *key, const struct rhashtable_params params)
277 unsigned int hash = rht_key_get_hash(ht, key, params, tbl->hash_rnd);
279 return rht_bucket_index(tbl, hash);
282 static inline unsigned int rht_head_hashfn(
283 struct rhashtable *ht, const struct bucket_table *tbl,
284 const struct rhash_head *he, const struct rhashtable_params params)
286 const char *ptr = rht_obj(ht, he);
288 return likely(params.obj_hashfn) ?
289 rht_bucket_index(tbl, params.obj_hashfn(ptr, params.key_len ?:
290 ht->p.key_len,
291 tbl->hash_rnd)) :
292 rht_key_hashfn(ht, tbl, ptr + params.key_offset, params);
296 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
297 * @ht: hash table
298 * @tbl: current table
300 static inline bool rht_grow_above_75(const struct rhashtable *ht,
301 const struct bucket_table *tbl)
303 /* Expand table when exceeding 75% load */
304 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
305 (!ht->p.max_size || tbl->size < ht->p.max_size);
309 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
310 * @ht: hash table
311 * @tbl: current table
313 static inline bool rht_shrink_below_30(const struct rhashtable *ht,
314 const struct bucket_table *tbl)
316 /* Shrink table beneath 30% load */
317 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
318 tbl->size > ht->p.min_size;
322 * rht_grow_above_100 - returns true if nelems > table-size
323 * @ht: hash table
324 * @tbl: current table
326 static inline bool rht_grow_above_100(const struct rhashtable *ht,
327 const struct bucket_table *tbl)
329 return atomic_read(&ht->nelems) > tbl->size &&
330 (!ht->p.max_size || tbl->size < ht->p.max_size);
334 * rht_grow_above_max - returns true if table is above maximum
335 * @ht: hash table
336 * @tbl: current table
338 static inline bool rht_grow_above_max(const struct rhashtable *ht,
339 const struct bucket_table *tbl)
341 return atomic_read(&ht->nelems) >= ht->max_elems;
344 /* The bucket lock is selected based on the hash and protects mutations
345 * on a group of hash buckets.
347 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
348 * a single lock always covers both buckets which may both contains
349 * entries which link to the same bucket of the old table during resizing.
350 * This allows to simplify the locking as locking the bucket in both
351 * tables during resize always guarantee protection.
353 * IMPORTANT: When holding the bucket lock of both the old and new table
354 * during expansions and shrinking, the old bucket lock must always be
355 * acquired first.
357 static inline spinlock_t *rht_bucket_lock(const struct bucket_table *tbl,
358 unsigned int hash)
360 return &tbl->locks[hash & tbl->locks_mask];
363 #ifdef CONFIG_PROVE_LOCKING
364 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
365 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
366 #else
367 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
369 return 1;
372 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
373 u32 hash)
375 return 1;
377 #endif /* CONFIG_PROVE_LOCKING */
379 int rhashtable_init(struct rhashtable *ht,
380 const struct rhashtable_params *params);
381 int rhltable_init(struct rhltable *hlt,
382 const struct rhashtable_params *params);
384 void *rhashtable_insert_slow(struct rhashtable *ht, const void *key,
385 struct rhash_head *obj);
387 void rhashtable_walk_enter(struct rhashtable *ht,
388 struct rhashtable_iter *iter);
389 void rhashtable_walk_exit(struct rhashtable_iter *iter);
390 int rhashtable_walk_start_check(struct rhashtable_iter *iter) __acquires(RCU);
392 static inline void rhashtable_walk_start(struct rhashtable_iter *iter)
394 (void)rhashtable_walk_start_check(iter);
397 void *rhashtable_walk_next(struct rhashtable_iter *iter);
398 void *rhashtable_walk_peek(struct rhashtable_iter *iter);
399 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
401 void rhashtable_free_and_destroy(struct rhashtable *ht,
402 void (*free_fn)(void *ptr, void *arg),
403 void *arg);
404 void rhashtable_destroy(struct rhashtable *ht);
406 struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl,
407 unsigned int hash);
408 struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht,
409 struct bucket_table *tbl,
410 unsigned int hash);
412 #define rht_dereference(p, ht) \
413 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
415 #define rht_dereference_rcu(p, ht) \
416 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
418 #define rht_dereference_bucket(p, tbl, hash) \
419 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
421 #define rht_dereference_bucket_rcu(p, tbl, hash) \
422 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
424 #define rht_entry(tpos, pos, member) \
425 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
427 static inline struct rhash_head __rcu *const *rht_bucket(
428 const struct bucket_table *tbl, unsigned int hash)
430 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
431 &tbl->buckets[hash];
434 static inline struct rhash_head __rcu **rht_bucket_var(
435 struct bucket_table *tbl, unsigned int hash)
437 return unlikely(tbl->nest) ? rht_bucket_nested(tbl, hash) :
438 &tbl->buckets[hash];
441 static inline struct rhash_head __rcu **rht_bucket_insert(
442 struct rhashtable *ht, struct bucket_table *tbl, unsigned int hash)
444 return unlikely(tbl->nest) ? rht_bucket_nested_insert(ht, tbl, hash) :
445 &tbl->buckets[hash];
449 * rht_for_each_continue - continue iterating over hash chain
450 * @pos: the &struct rhash_head to use as a loop cursor.
451 * @head: the previous &struct rhash_head to continue from
452 * @tbl: the &struct bucket_table
453 * @hash: the hash value / bucket index
455 #define rht_for_each_continue(pos, head, tbl, hash) \
456 for (pos = rht_dereference_bucket(head, tbl, hash); \
457 !rht_is_a_nulls(pos); \
458 pos = rht_dereference_bucket((pos)->next, tbl, hash))
461 * rht_for_each - iterate over hash chain
462 * @pos: the &struct rhash_head to use as a loop cursor.
463 * @tbl: the &struct bucket_table
464 * @hash: the hash value / bucket index
466 #define rht_for_each(pos, tbl, hash) \
467 rht_for_each_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
470 * rht_for_each_entry_continue - continue iterating over hash chain
471 * @tpos: the type * to use as a loop cursor.
472 * @pos: the &struct rhash_head to use as a loop cursor.
473 * @head: the previous &struct rhash_head to continue from
474 * @tbl: the &struct bucket_table
475 * @hash: the hash value / bucket index
476 * @member: name of the &struct rhash_head within the hashable struct.
478 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
479 for (pos = rht_dereference_bucket(head, tbl, hash); \
480 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
481 pos = rht_dereference_bucket((pos)->next, tbl, hash))
484 * rht_for_each_entry - iterate over hash chain of given type
485 * @tpos: the type * to use as a loop cursor.
486 * @pos: the &struct rhash_head to use as a loop cursor.
487 * @tbl: the &struct bucket_table
488 * @hash: the hash value / bucket index
489 * @member: name of the &struct rhash_head within the hashable struct.
491 #define rht_for_each_entry(tpos, pos, tbl, hash, member) \
492 rht_for_each_entry_continue(tpos, pos, *rht_bucket(tbl, hash), \
493 tbl, hash, member)
496 * rht_for_each_entry_safe - safely iterate over hash chain of given type
497 * @tpos: the type * to use as a loop cursor.
498 * @pos: the &struct rhash_head to use as a loop cursor.
499 * @next: the &struct rhash_head to use as next in loop cursor.
500 * @tbl: the &struct bucket_table
501 * @hash: the hash value / bucket index
502 * @member: name of the &struct rhash_head within the hashable struct.
504 * This hash chain list-traversal primitive allows for the looped code to
505 * remove the loop cursor from the list.
507 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
508 for (pos = rht_dereference_bucket(*rht_bucket(tbl, hash), tbl, hash), \
509 next = !rht_is_a_nulls(pos) ? \
510 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
511 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
512 pos = next, \
513 next = !rht_is_a_nulls(pos) ? \
514 rht_dereference_bucket(pos->next, tbl, hash) : NULL)
517 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
518 * @pos: the &struct rhash_head to use as a loop cursor.
519 * @head: the previous &struct rhash_head to continue from
520 * @tbl: the &struct bucket_table
521 * @hash: the hash value / bucket index
523 * This hash chain list-traversal primitive may safely run concurrently with
524 * the _rcu mutation primitives such as rhashtable_insert() as long as the
525 * traversal is guarded by rcu_read_lock().
527 #define rht_for_each_rcu_continue(pos, head, tbl, hash) \
528 for (({barrier(); }), \
529 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
530 !rht_is_a_nulls(pos); \
531 pos = rcu_dereference_raw(pos->next))
534 * rht_for_each_rcu - iterate over rcu hash chain
535 * @pos: the &struct rhash_head to use as a loop cursor.
536 * @tbl: the &struct bucket_table
537 * @hash: the hash value / bucket index
539 * This hash chain list-traversal primitive may safely run concurrently with
540 * the _rcu mutation primitives such as rhashtable_insert() as long as the
541 * traversal is guarded by rcu_read_lock().
543 #define rht_for_each_rcu(pos, tbl, hash) \
544 rht_for_each_rcu_continue(pos, *rht_bucket(tbl, hash), tbl, hash)
547 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
548 * @tpos: the type * to use as a loop cursor.
549 * @pos: the &struct rhash_head to use as a loop cursor.
550 * @head: the previous &struct rhash_head to continue from
551 * @tbl: the &struct bucket_table
552 * @hash: the hash value / bucket index
553 * @member: name of the &struct rhash_head within the hashable struct.
555 * This hash chain list-traversal primitive may safely run concurrently with
556 * the _rcu mutation primitives such as rhashtable_insert() as long as the
557 * traversal is guarded by rcu_read_lock().
559 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
560 for (({barrier(); }), \
561 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
562 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
563 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
566 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
567 * @tpos: the type * to use as a loop cursor.
568 * @pos: the &struct rhash_head to use as a loop cursor.
569 * @tbl: the &struct bucket_table
570 * @hash: the hash value / bucket index
571 * @member: name of the &struct rhash_head within the hashable struct.
573 * This hash chain list-traversal primitive may safely run concurrently with
574 * the _rcu mutation primitives such as rhashtable_insert() as long as the
575 * traversal is guarded by rcu_read_lock().
577 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
578 rht_for_each_entry_rcu_continue(tpos, pos, *rht_bucket(tbl, hash), \
579 tbl, hash, member)
582 * rhl_for_each_rcu - iterate over rcu hash table list
583 * @pos: the &struct rlist_head to use as a loop cursor.
584 * @list: the head of the list
586 * This hash chain list-traversal primitive should be used on the
587 * list returned by rhltable_lookup.
589 #define rhl_for_each_rcu(pos, list) \
590 for (pos = list; pos; pos = rcu_dereference_raw(pos->next))
593 * rhl_for_each_entry_rcu - iterate over rcu hash table list of given type
594 * @tpos: the type * to use as a loop cursor.
595 * @pos: the &struct rlist_head to use as a loop cursor.
596 * @list: the head of the list
597 * @member: name of the &struct rlist_head within the hashable struct.
599 * This hash chain list-traversal primitive should be used on the
600 * list returned by rhltable_lookup.
602 #define rhl_for_each_entry_rcu(tpos, pos, list, member) \
603 for (pos = list; pos && rht_entry(tpos, pos, member); \
604 pos = rcu_dereference_raw(pos->next))
606 static inline int rhashtable_compare(struct rhashtable_compare_arg *arg,
607 const void *obj)
609 struct rhashtable *ht = arg->ht;
610 const char *ptr = obj;
612 return memcmp(ptr + ht->p.key_offset, arg->key, ht->p.key_len);
615 /* Internal function, do not use. */
616 static inline struct rhash_head *__rhashtable_lookup(
617 struct rhashtable *ht, const void *key,
618 const struct rhashtable_params params)
620 struct rhashtable_compare_arg arg = {
621 .ht = ht,
622 .key = key,
624 struct bucket_table *tbl;
625 struct rhash_head *he;
626 unsigned int hash;
628 tbl = rht_dereference_rcu(ht->tbl, ht);
629 restart:
630 hash = rht_key_hashfn(ht, tbl, key, params);
631 rht_for_each_rcu(he, tbl, hash) {
632 if (params.obj_cmpfn ?
633 params.obj_cmpfn(&arg, rht_obj(ht, he)) :
634 rhashtable_compare(&arg, rht_obj(ht, he)))
635 continue;
636 return he;
639 /* Ensure we see any new tables. */
640 smp_rmb();
642 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
643 if (unlikely(tbl))
644 goto restart;
646 return NULL;
650 * rhashtable_lookup - search hash table
651 * @ht: hash table
652 * @key: the pointer to the key
653 * @params: hash table parameters
655 * Computes the hash value for the key and traverses the bucket chain looking
656 * for a entry with an identical key. The first matching entry is returned.
658 * This must only be called under the RCU read lock.
660 * Returns the first entry on which the compare function returned true.
662 static inline void *rhashtable_lookup(
663 struct rhashtable *ht, const void *key,
664 const struct rhashtable_params params)
666 struct rhash_head *he = __rhashtable_lookup(ht, key, params);
668 return he ? rht_obj(ht, he) : NULL;
672 * rhashtable_lookup_fast - search hash table, without RCU read lock
673 * @ht: hash table
674 * @key: the pointer to the key
675 * @params: hash table parameters
677 * Computes the hash value for the key and traverses the bucket chain looking
678 * for a entry with an identical key. The first matching entry is returned.
680 * Only use this function when you have other mechanisms guaranteeing
681 * that the object won't go away after the RCU read lock is released.
683 * Returns the first entry on which the compare function returned true.
685 static inline void *rhashtable_lookup_fast(
686 struct rhashtable *ht, const void *key,
687 const struct rhashtable_params params)
689 void *obj;
691 rcu_read_lock();
692 obj = rhashtable_lookup(ht, key, params);
693 rcu_read_unlock();
695 return obj;
699 * rhltable_lookup - search hash list table
700 * @hlt: hash table
701 * @key: the pointer to the key
702 * @params: hash table parameters
704 * Computes the hash value for the key and traverses the bucket chain looking
705 * for a entry with an identical key. All matching entries are returned
706 * in a list.
708 * This must only be called under the RCU read lock.
710 * Returns the list of entries that match the given key.
712 static inline struct rhlist_head *rhltable_lookup(
713 struct rhltable *hlt, const void *key,
714 const struct rhashtable_params params)
716 struct rhash_head *he = __rhashtable_lookup(&hlt->ht, key, params);
718 return he ? container_of(he, struct rhlist_head, rhead) : NULL;
721 /* Internal function, please use rhashtable_insert_fast() instead. This
722 * function returns the existing element already in hashes in there is a clash,
723 * otherwise it returns an error via ERR_PTR().
725 static inline void *__rhashtable_insert_fast(
726 struct rhashtable *ht, const void *key, struct rhash_head *obj,
727 const struct rhashtable_params params, bool rhlist)
729 struct rhashtable_compare_arg arg = {
730 .ht = ht,
731 .key = key,
733 struct rhash_head __rcu **pprev;
734 struct bucket_table *tbl;
735 struct rhash_head *head;
736 spinlock_t *lock;
737 unsigned int hash;
738 int elasticity;
739 void *data;
741 rcu_read_lock();
743 tbl = rht_dereference_rcu(ht->tbl, ht);
744 hash = rht_head_hashfn(ht, tbl, obj, params);
745 lock = rht_bucket_lock(tbl, hash);
746 spin_lock_bh(lock);
748 if (unlikely(rht_dereference_bucket(tbl->future_tbl, tbl, hash))) {
749 slow_path:
750 spin_unlock_bh(lock);
751 rcu_read_unlock();
752 return rhashtable_insert_slow(ht, key, obj);
755 elasticity = RHT_ELASTICITY;
756 pprev = rht_bucket_insert(ht, tbl, hash);
757 data = ERR_PTR(-ENOMEM);
758 if (!pprev)
759 goto out;
761 rht_for_each_continue(head, *pprev, tbl, hash) {
762 struct rhlist_head *plist;
763 struct rhlist_head *list;
765 elasticity--;
766 if (!key ||
767 (params.obj_cmpfn ?
768 params.obj_cmpfn(&arg, rht_obj(ht, head)) :
769 rhashtable_compare(&arg, rht_obj(ht, head))))
770 continue;
772 data = rht_obj(ht, head);
774 if (!rhlist)
775 goto out;
778 list = container_of(obj, struct rhlist_head, rhead);
779 plist = container_of(head, struct rhlist_head, rhead);
781 RCU_INIT_POINTER(list->next, plist);
782 head = rht_dereference_bucket(head->next, tbl, hash);
783 RCU_INIT_POINTER(list->rhead.next, head);
784 rcu_assign_pointer(*pprev, obj);
786 goto good;
789 if (elasticity <= 0)
790 goto slow_path;
792 data = ERR_PTR(-E2BIG);
793 if (unlikely(rht_grow_above_max(ht, tbl)))
794 goto out;
796 if (unlikely(rht_grow_above_100(ht, tbl)))
797 goto slow_path;
799 head = rht_dereference_bucket(*pprev, tbl, hash);
801 RCU_INIT_POINTER(obj->next, head);
802 if (rhlist) {
803 struct rhlist_head *list;
805 list = container_of(obj, struct rhlist_head, rhead);
806 RCU_INIT_POINTER(list->next, NULL);
809 rcu_assign_pointer(*pprev, obj);
811 atomic_inc(&ht->nelems);
812 if (rht_grow_above_75(ht, tbl))
813 schedule_work(&ht->run_work);
815 good:
816 data = NULL;
818 out:
819 spin_unlock_bh(lock);
820 rcu_read_unlock();
822 return data;
826 * rhashtable_insert_fast - insert object into hash table
827 * @ht: hash table
828 * @obj: pointer to hash head inside object
829 * @params: hash table parameters
831 * Will take a per bucket spinlock to protect against mutual mutations
832 * on the same bucket. Multiple insertions may occur in parallel unless
833 * they map to the same bucket lock.
835 * It is safe to call this function from atomic context.
837 * Will trigger an automatic deferred table resizing if the size grows
838 * beyond the watermark indicated by grow_decision() which can be passed
839 * to rhashtable_init().
841 static inline int rhashtable_insert_fast(
842 struct rhashtable *ht, struct rhash_head *obj,
843 const struct rhashtable_params params)
845 void *ret;
847 ret = __rhashtable_insert_fast(ht, NULL, obj, params, false);
848 if (IS_ERR(ret))
849 return PTR_ERR(ret);
851 return ret == NULL ? 0 : -EEXIST;
855 * rhltable_insert_key - insert object into hash list table
856 * @hlt: hash list table
857 * @key: the pointer to the key
858 * @list: pointer to hash list head inside object
859 * @params: hash table parameters
861 * Will take a per bucket spinlock to protect against mutual mutations
862 * on the same bucket. Multiple insertions may occur in parallel unless
863 * they map to the same bucket lock.
865 * It is safe to call this function from atomic context.
867 * Will trigger an automatic deferred table resizing if the size grows
868 * beyond the watermark indicated by grow_decision() which can be passed
869 * to rhashtable_init().
871 static inline int rhltable_insert_key(
872 struct rhltable *hlt, const void *key, struct rhlist_head *list,
873 const struct rhashtable_params params)
875 return PTR_ERR(__rhashtable_insert_fast(&hlt->ht, key, &list->rhead,
876 params, true));
880 * rhltable_insert - insert object into hash list table
881 * @hlt: hash list table
882 * @list: pointer to hash list head inside object
883 * @params: hash table parameters
885 * Will take a per bucket spinlock to protect against mutual mutations
886 * on the same bucket. Multiple insertions may occur in parallel unless
887 * they map to the same bucket lock.
889 * It is safe to call this function from atomic context.
891 * Will trigger an automatic deferred table resizing if the size grows
892 * beyond the watermark indicated by grow_decision() which can be passed
893 * to rhashtable_init().
895 static inline int rhltable_insert(
896 struct rhltable *hlt, struct rhlist_head *list,
897 const struct rhashtable_params params)
899 const char *key = rht_obj(&hlt->ht, &list->rhead);
901 key += params.key_offset;
903 return rhltable_insert_key(hlt, key, list, params);
907 * rhashtable_lookup_insert_fast - lookup and insert object into hash table
908 * @ht: hash table
909 * @obj: pointer to hash head inside object
910 * @params: hash table parameters
912 * Locks down the bucket chain in both the old and new table if a resize
913 * is in progress to ensure that writers can't remove from the old table
914 * and can't insert to the new table during the atomic operation of search
915 * and insertion. Searches for duplicates in both the old and new table if
916 * a resize is in progress.
918 * This lookup function may only be used for fixed key hash table (key_len
919 * parameter set). It will BUG() if used inappropriately.
921 * It is safe to call this function from atomic context.
923 * Will trigger an automatic deferred table resizing if the size grows
924 * beyond the watermark indicated by grow_decision() which can be passed
925 * to rhashtable_init().
927 static inline int rhashtable_lookup_insert_fast(
928 struct rhashtable *ht, struct rhash_head *obj,
929 const struct rhashtable_params params)
931 const char *key = rht_obj(ht, obj);
932 void *ret;
934 BUG_ON(ht->p.obj_hashfn);
936 ret = __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
937 false);
938 if (IS_ERR(ret))
939 return PTR_ERR(ret);
941 return ret == NULL ? 0 : -EEXIST;
945 * rhashtable_lookup_get_insert_fast - lookup and insert object into hash table
946 * @ht: hash table
947 * @obj: pointer to hash head inside object
948 * @params: hash table parameters
950 * Just like rhashtable_lookup_insert_fast(), but this function returns the
951 * object if it exists, NULL if it did not and the insertion was successful,
952 * and an ERR_PTR otherwise.
954 static inline void *rhashtable_lookup_get_insert_fast(
955 struct rhashtable *ht, struct rhash_head *obj,
956 const struct rhashtable_params params)
958 const char *key = rht_obj(ht, obj);
960 BUG_ON(ht->p.obj_hashfn);
962 return __rhashtable_insert_fast(ht, key + ht->p.key_offset, obj, params,
963 false);
967 * rhashtable_lookup_insert_key - search and insert object to hash table
968 * with explicit key
969 * @ht: hash table
970 * @key: key
971 * @obj: pointer to hash head inside object
972 * @params: hash table parameters
974 * Locks down the bucket chain in both the old and new table if a resize
975 * is in progress to ensure that writers can't remove from the old table
976 * and can't insert to the new table during the atomic operation of search
977 * and insertion. Searches for duplicates in both the old and new table if
978 * a resize is in progress.
980 * Lookups may occur in parallel with hashtable mutations and resizing.
982 * Will trigger an automatic deferred table resizing if the size grows
983 * beyond the watermark indicated by grow_decision() which can be passed
984 * to rhashtable_init().
986 * Returns zero on success.
988 static inline int rhashtable_lookup_insert_key(
989 struct rhashtable *ht, const void *key, struct rhash_head *obj,
990 const struct rhashtable_params params)
992 void *ret;
994 BUG_ON(!ht->p.obj_hashfn || !key);
996 ret = __rhashtable_insert_fast(ht, key, obj, params, false);
997 if (IS_ERR(ret))
998 return PTR_ERR(ret);
1000 return ret == NULL ? 0 : -EEXIST;
1004 * rhashtable_lookup_get_insert_key - lookup and insert object into hash table
1005 * @ht: hash table
1006 * @obj: pointer to hash head inside object
1007 * @params: hash table parameters
1008 * @data: pointer to element data already in hashes
1010 * Just like rhashtable_lookup_insert_key(), but this function returns the
1011 * object if it exists, NULL if it does not and the insertion was successful,
1012 * and an ERR_PTR otherwise.
1014 static inline void *rhashtable_lookup_get_insert_key(
1015 struct rhashtable *ht, const void *key, struct rhash_head *obj,
1016 const struct rhashtable_params params)
1018 BUG_ON(!ht->p.obj_hashfn || !key);
1020 return __rhashtable_insert_fast(ht, key, obj, params, false);
1023 /* Internal function, please use rhashtable_remove_fast() instead */
1024 static inline int __rhashtable_remove_fast_one(
1025 struct rhashtable *ht, struct bucket_table *tbl,
1026 struct rhash_head *obj, const struct rhashtable_params params,
1027 bool rhlist)
1029 struct rhash_head __rcu **pprev;
1030 struct rhash_head *he;
1031 spinlock_t * lock;
1032 unsigned int hash;
1033 int err = -ENOENT;
1035 hash = rht_head_hashfn(ht, tbl, obj, params);
1036 lock = rht_bucket_lock(tbl, hash);
1038 spin_lock_bh(lock);
1040 pprev = rht_bucket_var(tbl, hash);
1041 rht_for_each_continue(he, *pprev, tbl, hash) {
1042 struct rhlist_head *list;
1044 list = container_of(he, struct rhlist_head, rhead);
1046 if (he != obj) {
1047 struct rhlist_head __rcu **lpprev;
1049 pprev = &he->next;
1051 if (!rhlist)
1052 continue;
1054 do {
1055 lpprev = &list->next;
1056 list = rht_dereference_bucket(list->next,
1057 tbl, hash);
1058 } while (list && obj != &list->rhead);
1060 if (!list)
1061 continue;
1063 list = rht_dereference_bucket(list->next, tbl, hash);
1064 RCU_INIT_POINTER(*lpprev, list);
1065 err = 0;
1066 break;
1069 obj = rht_dereference_bucket(obj->next, tbl, hash);
1070 err = 1;
1072 if (rhlist) {
1073 list = rht_dereference_bucket(list->next, tbl, hash);
1074 if (list) {
1075 RCU_INIT_POINTER(list->rhead.next, obj);
1076 obj = &list->rhead;
1077 err = 0;
1081 rcu_assign_pointer(*pprev, obj);
1082 break;
1085 spin_unlock_bh(lock);
1087 if (err > 0) {
1088 atomic_dec(&ht->nelems);
1089 if (unlikely(ht->p.automatic_shrinking &&
1090 rht_shrink_below_30(ht, tbl)))
1091 schedule_work(&ht->run_work);
1092 err = 0;
1095 return err;
1098 /* Internal function, please use rhashtable_remove_fast() instead */
1099 static inline int __rhashtable_remove_fast(
1100 struct rhashtable *ht, struct rhash_head *obj,
1101 const struct rhashtable_params params, bool rhlist)
1103 struct bucket_table *tbl;
1104 int err;
1106 rcu_read_lock();
1108 tbl = rht_dereference_rcu(ht->tbl, ht);
1110 /* Because we have already taken (and released) the bucket
1111 * lock in old_tbl, if we find that future_tbl is not yet
1112 * visible then that guarantees the entry to still be in
1113 * the old tbl if it exists.
1115 while ((err = __rhashtable_remove_fast_one(ht, tbl, obj, params,
1116 rhlist)) &&
1117 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1120 rcu_read_unlock();
1122 return err;
1126 * rhashtable_remove_fast - remove object from hash table
1127 * @ht: hash table
1128 * @obj: pointer to hash head inside object
1129 * @params: hash table parameters
1131 * Since the hash chain is single linked, the removal operation needs to
1132 * walk the bucket chain upon removal. The removal operation is thus
1133 * considerable slow if the hash table is not correctly sized.
1135 * Will automatically shrink the table via rhashtable_expand() if the
1136 * shrink_decision function specified at rhashtable_init() returns true.
1138 * Returns zero on success, -ENOENT if the entry could not be found.
1140 static inline int rhashtable_remove_fast(
1141 struct rhashtable *ht, struct rhash_head *obj,
1142 const struct rhashtable_params params)
1144 return __rhashtable_remove_fast(ht, obj, params, false);
1148 * rhltable_remove - remove object from hash list table
1149 * @hlt: hash list table
1150 * @list: pointer to hash list head inside object
1151 * @params: hash table parameters
1153 * Since the hash chain is single linked, the removal operation needs to
1154 * walk the bucket chain upon removal. The removal operation is thus
1155 * considerable slow if the hash table is not correctly sized.
1157 * Will automatically shrink the table via rhashtable_expand() if the
1158 * shrink_decision function specified at rhashtable_init() returns true.
1160 * Returns zero on success, -ENOENT if the entry could not be found.
1162 static inline int rhltable_remove(
1163 struct rhltable *hlt, struct rhlist_head *list,
1164 const struct rhashtable_params params)
1166 return __rhashtable_remove_fast(&hlt->ht, &list->rhead, params, true);
1169 /* Internal function, please use rhashtable_replace_fast() instead */
1170 static inline int __rhashtable_replace_fast(
1171 struct rhashtable *ht, struct bucket_table *tbl,
1172 struct rhash_head *obj_old, struct rhash_head *obj_new,
1173 const struct rhashtable_params params)
1175 struct rhash_head __rcu **pprev;
1176 struct rhash_head *he;
1177 spinlock_t *lock;
1178 unsigned int hash;
1179 int err = -ENOENT;
1181 /* Minimally, the old and new objects must have same hash
1182 * (which should mean identifiers are the same).
1184 hash = rht_head_hashfn(ht, tbl, obj_old, params);
1185 if (hash != rht_head_hashfn(ht, tbl, obj_new, params))
1186 return -EINVAL;
1188 lock = rht_bucket_lock(tbl, hash);
1190 spin_lock_bh(lock);
1192 pprev = rht_bucket_var(tbl, hash);
1193 rht_for_each_continue(he, *pprev, tbl, hash) {
1194 if (he != obj_old) {
1195 pprev = &he->next;
1196 continue;
1199 rcu_assign_pointer(obj_new->next, obj_old->next);
1200 rcu_assign_pointer(*pprev, obj_new);
1201 err = 0;
1202 break;
1205 spin_unlock_bh(lock);
1207 return err;
1211 * rhashtable_replace_fast - replace an object in hash table
1212 * @ht: hash table
1213 * @obj_old: pointer to hash head inside object being replaced
1214 * @obj_new: pointer to hash head inside object which is new
1215 * @params: hash table parameters
1217 * Replacing an object doesn't affect the number of elements in the hash table
1218 * or bucket, so we don't need to worry about shrinking or expanding the
1219 * table here.
1221 * Returns zero on success, -ENOENT if the entry could not be found,
1222 * -EINVAL if hash is not the same for the old and new objects.
1224 static inline int rhashtable_replace_fast(
1225 struct rhashtable *ht, struct rhash_head *obj_old,
1226 struct rhash_head *obj_new,
1227 const struct rhashtable_params params)
1229 struct bucket_table *tbl;
1230 int err;
1232 rcu_read_lock();
1234 tbl = rht_dereference_rcu(ht->tbl, ht);
1236 /* Because we have already taken (and released) the bucket
1237 * lock in old_tbl, if we find that future_tbl is not yet
1238 * visible then that guarantees the entry to still be in
1239 * the old tbl if it exists.
1241 while ((err = __rhashtable_replace_fast(ht, tbl, obj_old,
1242 obj_new, params)) &&
1243 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
1246 rcu_read_unlock();
1248 return err;
1251 /* Obsolete function, do not use in new code. */
1252 static inline int rhashtable_walk_init(struct rhashtable *ht,
1253 struct rhashtable_iter *iter, gfp_t gfp)
1255 rhashtable_walk_enter(ht, iter);
1256 return 0;
1260 * rhltable_walk_enter - Initialise an iterator
1261 * @hlt: Table to walk over
1262 * @iter: Hash table Iterator
1264 * This function prepares a hash table walk.
1266 * Note that if you restart a walk after rhashtable_walk_stop you
1267 * may see the same object twice. Also, you may miss objects if
1268 * there are removals in between rhashtable_walk_stop and the next
1269 * call to rhashtable_walk_start.
1271 * For a completely stable walk you should construct your own data
1272 * structure outside the hash table.
1274 * This function may sleep so you must not call it from interrupt
1275 * context or with spin locks held.
1277 * You must call rhashtable_walk_exit after this function returns.
1279 static inline void rhltable_walk_enter(struct rhltable *hlt,
1280 struct rhashtable_iter *iter)
1282 return rhashtable_walk_enter(&hlt->ht, iter);
1286 * rhltable_free_and_destroy - free elements and destroy hash list table
1287 * @hlt: the hash list table to destroy
1288 * @free_fn: callback to release resources of element
1289 * @arg: pointer passed to free_fn
1291 * See documentation for rhashtable_free_and_destroy.
1293 static inline void rhltable_free_and_destroy(struct rhltable *hlt,
1294 void (*free_fn)(void *ptr,
1295 void *arg),
1296 void *arg)
1298 return rhashtable_free_and_destroy(&hlt->ht, free_fn, arg);
1301 static inline void rhltable_destroy(struct rhltable *hlt)
1303 return rhltable_free_and_destroy(hlt, NULL, NULL);
1306 #endif /* _LINUX_RHASHTABLE_H */