2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 * Based on the following paper by Josh Triplett, Paul E. McKenney
8 * and Jonathan Walpole:
9 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
11 * Code partially derived from nft_hash
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/rculist.h>
24 struct rhash_head __rcu *next;
27 #define INIT_HASH_HEAD(ptr) ((ptr)->next = NULL)
31 struct rhash_head __rcu *buckets[];
34 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
35 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
40 * struct rhashtable_params - Hash table construction parameters
41 * @nelem_hint: Hint on number of elements, should be 75% of desired size
42 * @key_len: Length of key
43 * @key_offset: Offset of key in struct to be hashed
44 * @head_offset: Offset of rhash_head in struct to be hashed
45 * @hash_rnd: Seed to use while hashing
46 * @max_shift: Maximum number of shifts while expanding
47 * @min_shift: Minimum number of shifts while shrinking
48 * @hashfn: Function to hash key
49 * @obj_hashfn: Function to hash object
50 * @grow_decision: If defined, may return true if table should expand
51 * @shrink_decision: If defined, may return true if table should shrink
52 * @mutex_is_held: Must return true if protecting mutex is held
54 struct rhashtable_params {
63 rht_obj_hashfn_t obj_hashfn;
64 bool (*grow_decision)(const struct rhashtable *ht,
66 bool (*shrink_decision)(const struct rhashtable *ht,
68 #ifdef CONFIG_PROVE_LOCKING
69 int (*mutex_is_held)(void);
74 * struct rhashtable - Hash table handle
76 * @nelems: Number of elements in table
77 * @shift: Current size (1 << shift)
78 * @p: Configuration parameters
81 struct bucket_table __rcu *tbl;
84 struct rhashtable_params p;
87 #ifdef CONFIG_PROVE_LOCKING
88 int lockdep_rht_mutex_is_held(const struct rhashtable *ht);
90 static inline int lockdep_rht_mutex_is_held(const struct rhashtable *ht)
94 #endif /* CONFIG_PROVE_LOCKING */
96 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
98 u32 rhashtable_hashfn(const struct rhashtable *ht, const void *key, u32 len);
99 u32 rhashtable_obj_hashfn(const struct rhashtable *ht, void *ptr);
101 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node, gfp_t);
102 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node, gfp_t);
103 void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj,
104 struct rhash_head __rcu **pprev, gfp_t flags);
106 bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
107 bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
109 int rhashtable_expand(struct rhashtable *ht, gfp_t flags);
110 int rhashtable_shrink(struct rhashtable *ht, gfp_t flags);
112 void *rhashtable_lookup(const struct rhashtable *ht, const void *key);
113 void *rhashtable_lookup_compare(const struct rhashtable *ht, u32 hash,
114 bool (*compare)(void *, void *), void *arg);
116 void rhashtable_destroy(const struct rhashtable *ht);
118 #define rht_dereference(p, ht) \
119 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
121 #define rht_dereference_rcu(p, ht) \
122 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
124 #define rht_entry(ptr, type, member) container_of(ptr, type, member)
125 #define rht_entry_safe(ptr, type, member) \
127 typeof(ptr) __ptr = (ptr); \
128 __ptr ? rht_entry(__ptr, type, member) : NULL; \
131 #define rht_next_entry_safe(pos, ht, member) \
133 pos ? rht_entry_safe(rht_dereference((pos)->member.next, ht), \
134 typeof(*(pos)), member) : NULL; \
138 * rht_for_each - iterate over hash chain
139 * @pos: &struct rhash_head to use as a loop cursor.
140 * @head: head of the hash chain (struct rhash_head *)
141 * @ht: pointer to your struct rhashtable
143 #define rht_for_each(pos, head, ht) \
144 for (pos = rht_dereference(head, ht); \
146 pos = rht_dereference((pos)->next, ht))
149 * rht_for_each_entry - iterate over hash chain of given type
150 * @pos: type * to use as a loop cursor.
151 * @head: head of the hash chain (struct rhash_head *)
152 * @ht: pointer to your struct rhashtable
153 * @member: name of the rhash_head within the hashable struct.
155 #define rht_for_each_entry(pos, head, ht, member) \
156 for (pos = rht_entry_safe(rht_dereference(head, ht), \
157 typeof(*(pos)), member); \
159 pos = rht_next_entry_safe(pos, ht, member))
162 * rht_for_each_entry_safe - safely iterate over hash chain of given type
163 * @pos: type * to use as a loop cursor.
164 * @n: type * to use for temporary next object storage
165 * @head: head of the hash chain (struct rhash_head *)
166 * @ht: pointer to your struct rhashtable
167 * @member: name of the rhash_head within the hashable struct.
169 * This hash chain list-traversal primitive allows for the looped code to
170 * remove the loop cursor from the list.
172 #define rht_for_each_entry_safe(pos, n, head, ht, member) \
173 for (pos = rht_entry_safe(rht_dereference(head, ht), \
174 typeof(*(pos)), member), \
175 n = rht_next_entry_safe(pos, ht, member); \
178 n = rht_next_entry_safe(pos, ht, member))
181 * rht_for_each_rcu - iterate over rcu hash chain
182 * @pos: &struct rhash_head to use as a loop cursor.
183 * @head: head of the hash chain (struct rhash_head *)
184 * @ht: pointer to your struct rhashtable
186 * This hash chain list-traversal primitive may safely run concurrently with
187 * the _rcu fkht mutation primitives such as rht_insert() as long as the
188 * traversal is guarded by rcu_read_lock().
190 #define rht_for_each_rcu(pos, head, ht) \
191 for (pos = rht_dereference_rcu(head, ht); \
193 pos = rht_dereference_rcu((pos)->next, ht))
196 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
197 * @pos: type * to use as a loop cursor.
198 * @head: head of the hash chain (struct rhash_head *)
199 * @member: name of the rhash_head within the hashable struct.
201 * This hash chain list-traversal primitive may safely run concurrently with
202 * the _rcu fkht mutation primitives such as rht_insert() as long as the
203 * traversal is guarded by rcu_read_lock().
205 #define rht_for_each_entry_rcu(pos, head, member) \
206 for (pos = rht_entry_safe(rcu_dereference_raw(head), \
207 typeof(*(pos)), member); \
209 pos = rht_entry_safe(rcu_dereference_raw((pos)->member.next), \
210 typeof(*(pos)), member))
212 #endif /* _LINUX_RHASHTABLE_H */