rhashtable: Introduce max_size/min_size
[linux-2.6-block.git] / include / linux / rhashtable.h
1 /*
2  * Resizable, Scalable, Concurrent Hash Table
3  *
4  * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5  * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
6  *
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
10  *
11  * Code partially derived from nft_hash
12  *
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.
16  */
17
18 #ifndef _LINUX_RHASHTABLE_H
19 #define _LINUX_RHASHTABLE_H
20
21 #include <linux/compiler.h>
22 #include <linux/list_nulls.h>
23 #include <linux/workqueue.h>
24 #include <linux/mutex.h>
25
26 /*
27  * The end of the chain is marked with a special nulls marks which has
28  * the following format:
29  *
30  * +-------+-----------------------------------------------------+-+
31  * | Base  |                      Hash                           |1|
32  * +-------+-----------------------------------------------------+-+
33  *
34  * Base (4 bits) : Reserved to distinguish between multiple tables.
35  *                 Specified via &struct rhashtable_params.nulls_base.
36  * Hash (27 bits): Full hash (unmasked) of first element added to bucket
37  * 1 (1 bit)     : Nulls marker (always set)
38  *
39  * The remaining bits of the next pointer remain unused for now.
40  */
41 #define RHT_BASE_BITS           4
42 #define RHT_HASH_BITS           27
43 #define RHT_BASE_SHIFT          RHT_HASH_BITS
44
45 struct rhash_head {
46         struct rhash_head __rcu         *next;
47 };
48
49 /**
50  * struct bucket_table - Table of hash buckets
51  * @size: Number of hash buckets
52  * @rehash: Current bucket being rehashed
53  * @hash_rnd: Random seed to fold into hash
54  * @locks_mask: Mask to apply before accessing locks[]
55  * @locks: Array of spinlocks protecting individual buckets
56  * @walkers: List of active walkers
57  * @rcu: RCU structure for freeing the table
58  * @future_tbl: Table under construction during rehashing
59  * @buckets: size * hash buckets
60  */
61 struct bucket_table {
62         unsigned int            size;
63         unsigned int            rehash;
64         u32                     hash_rnd;
65         unsigned int            locks_mask;
66         spinlock_t              *locks;
67         struct list_head        walkers;
68         struct rcu_head         rcu;
69
70         struct bucket_table __rcu *future_tbl;
71
72         struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
73 };
74
75 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
76 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
77
78 struct rhashtable;
79
80 /**
81  * struct rhashtable_params - Hash table construction parameters
82  * @nelem_hint: Hint on number of elements, should be 75% of desired size
83  * @key_len: Length of key
84  * @key_offset: Offset of key in struct to be hashed
85  * @head_offset: Offset of rhash_head in struct to be hashed
86  * @max_shift: Maximum number of shifts while expanding
87  * @min_shift: Minimum number of shifts while shrinking
88  * @max_size: Maximum size while expanding
89  * @min_size: Minimum size while shrinking
90  * @nulls_base: Base value to generate nulls marker
91  * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
92  * @hashfn: Function to hash key
93  * @obj_hashfn: Function to hash object
94  */
95 struct rhashtable_params {
96         size_t                  nelem_hint;
97         size_t                  key_len;
98         size_t                  key_offset;
99         size_t                  head_offset;
100         size_t                  max_shift;
101         size_t                  min_shift;
102         unsigned int            max_size;
103         unsigned int            min_size;
104         u32                     nulls_base;
105         size_t                  locks_mul;
106         rht_hashfn_t            hashfn;
107         rht_obj_hashfn_t        obj_hashfn;
108 };
109
110 /**
111  * struct rhashtable - Hash table handle
112  * @tbl: Bucket table
113  * @nelems: Number of elements in table
114  * @p: Configuration parameters
115  * @run_work: Deferred worker to expand/shrink asynchronously
116  * @mutex: Mutex to protect current/future table swapping
117  * @being_destroyed: True if table is set up for destruction
118  */
119 struct rhashtable {
120         struct bucket_table __rcu       *tbl;
121         atomic_t                        nelems;
122         bool                            being_destroyed;
123         struct rhashtable_params        p;
124         struct work_struct              run_work;
125         struct mutex                    mutex;
126 };
127
128 /**
129  * struct rhashtable_walker - Hash table walker
130  * @list: List entry on list of walkers
131  * @tbl: The table that we were walking over
132  */
133 struct rhashtable_walker {
134         struct list_head list;
135         struct bucket_table *tbl;
136 };
137
138 /**
139  * struct rhashtable_iter - Hash table iterator, fits into netlink cb
140  * @ht: Table to iterate through
141  * @p: Current pointer
142  * @walker: Associated rhashtable walker
143  * @slot: Current slot
144  * @skip: Number of entries to skip in slot
145  */
146 struct rhashtable_iter {
147         struct rhashtable *ht;
148         struct rhash_head *p;
149         struct rhashtable_walker *walker;
150         unsigned int slot;
151         unsigned int skip;
152 };
153
154 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
155 {
156         return NULLS_MARKER(ht->p.nulls_base + hash);
157 }
158
159 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
160         ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
161
162 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
163 {
164         return ((unsigned long) ptr & 1);
165 }
166
167 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
168 {
169         return ((unsigned long) ptr) >> 1;
170 }
171
172 #ifdef CONFIG_PROVE_LOCKING
173 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
174 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
175 #else
176 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
177 {
178         return 1;
179 }
180
181 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
182                                              u32 hash)
183 {
184         return 1;
185 }
186 #endif /* CONFIG_PROVE_LOCKING */
187
188 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
189
190 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
191 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
192
193 int rhashtable_expand(struct rhashtable *ht);
194 int rhashtable_shrink(struct rhashtable *ht);
195
196 void *rhashtable_lookup(struct rhashtable *ht, const void *key);
197 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
198                                 bool (*compare)(void *, void *), void *arg);
199
200 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
201 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
202                                       struct rhash_head *obj,
203                                       bool (*compare)(void *, void *),
204                                       void *arg);
205
206 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
207 void rhashtable_walk_exit(struct rhashtable_iter *iter);
208 int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
209 void *rhashtable_walk_next(struct rhashtable_iter *iter);
210 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
211
212 void rhashtable_destroy(struct rhashtable *ht);
213
214 #define rht_dereference(p, ht) \
215         rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
216
217 #define rht_dereference_rcu(p, ht) \
218         rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
219
220 #define rht_dereference_bucket(p, tbl, hash) \
221         rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
222
223 #define rht_dereference_bucket_rcu(p, tbl, hash) \
224         rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
225
226 #define rht_entry(tpos, pos, member) \
227         ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
228
229 /**
230  * rht_for_each_continue - continue iterating over hash chain
231  * @pos:        the &struct rhash_head to use as a loop cursor.
232  * @head:       the previous &struct rhash_head to continue from
233  * @tbl:        the &struct bucket_table
234  * @hash:       the hash value / bucket index
235  */
236 #define rht_for_each_continue(pos, head, tbl, hash) \
237         for (pos = rht_dereference_bucket(head, tbl, hash); \
238              !rht_is_a_nulls(pos); \
239              pos = rht_dereference_bucket((pos)->next, tbl, hash))
240
241 /**
242  * rht_for_each - iterate over hash chain
243  * @pos:        the &struct rhash_head to use as a loop cursor.
244  * @tbl:        the &struct bucket_table
245  * @hash:       the hash value / bucket index
246  */
247 #define rht_for_each(pos, tbl, hash) \
248         rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
249
250 /**
251  * rht_for_each_entry_continue - continue iterating over hash chain
252  * @tpos:       the type * to use as a loop cursor.
253  * @pos:        the &struct rhash_head to use as a loop cursor.
254  * @head:       the previous &struct rhash_head to continue from
255  * @tbl:        the &struct bucket_table
256  * @hash:       the hash value / bucket index
257  * @member:     name of the &struct rhash_head within the hashable struct.
258  */
259 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
260         for (pos = rht_dereference_bucket(head, tbl, hash);             \
261              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);    \
262              pos = rht_dereference_bucket((pos)->next, tbl, hash))
263
264 /**
265  * rht_for_each_entry - iterate over hash chain of given type
266  * @tpos:       the type * to use as a loop cursor.
267  * @pos:        the &struct rhash_head to use as a loop cursor.
268  * @tbl:        the &struct bucket_table
269  * @hash:       the hash value / bucket index
270  * @member:     name of the &struct rhash_head within the hashable struct.
271  */
272 #define rht_for_each_entry(tpos, pos, tbl, hash, member)                \
273         rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash],    \
274                                     tbl, hash, member)
275
276 /**
277  * rht_for_each_entry_safe - safely iterate over hash chain of given type
278  * @tpos:       the type * to use as a loop cursor.
279  * @pos:        the &struct rhash_head to use as a loop cursor.
280  * @next:       the &struct rhash_head to use as next in loop cursor.
281  * @tbl:        the &struct bucket_table
282  * @hash:       the hash value / bucket index
283  * @member:     name of the &struct rhash_head within the hashable struct.
284  *
285  * This hash chain list-traversal primitive allows for the looped code to
286  * remove the loop cursor from the list.
287  */
288 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member)         \
289         for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
290              next = !rht_is_a_nulls(pos) ?                                  \
291                        rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
292              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);        \
293              pos = next,                                                    \
294              next = !rht_is_a_nulls(pos) ?                                  \
295                        rht_dereference_bucket(pos->next, tbl, hash) : NULL)
296
297 /**
298  * rht_for_each_rcu_continue - continue iterating over rcu hash chain
299  * @pos:        the &struct rhash_head to use as a loop cursor.
300  * @head:       the previous &struct rhash_head to continue from
301  * @tbl:        the &struct bucket_table
302  * @hash:       the hash value / bucket index
303  *
304  * This hash chain list-traversal primitive may safely run concurrently with
305  * the _rcu mutation primitives such as rhashtable_insert() as long as the
306  * traversal is guarded by rcu_read_lock().
307  */
308 #define rht_for_each_rcu_continue(pos, head, tbl, hash)                 \
309         for (({barrier(); }),                                           \
310              pos = rht_dereference_bucket_rcu(head, tbl, hash);         \
311              !rht_is_a_nulls(pos);                                      \
312              pos = rcu_dereference_raw(pos->next))
313
314 /**
315  * rht_for_each_rcu - iterate over rcu hash chain
316  * @pos:        the &struct rhash_head to use as a loop cursor.
317  * @tbl:        the &struct bucket_table
318  * @hash:       the hash value / bucket index
319  *
320  * This hash chain list-traversal primitive may safely run concurrently with
321  * the _rcu mutation primitives such as rhashtable_insert() as long as the
322  * traversal is guarded by rcu_read_lock().
323  */
324 #define rht_for_each_rcu(pos, tbl, hash)                                \
325         rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
326
327 /**
328  * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
329  * @tpos:       the type * to use as a loop cursor.
330  * @pos:        the &struct rhash_head to use as a loop cursor.
331  * @head:       the previous &struct rhash_head to continue from
332  * @tbl:        the &struct bucket_table
333  * @hash:       the hash value / bucket index
334  * @member:     name of the &struct rhash_head within the hashable struct.
335  *
336  * This hash chain list-traversal primitive may safely run concurrently with
337  * the _rcu mutation primitives such as rhashtable_insert() as long as the
338  * traversal is guarded by rcu_read_lock().
339  */
340 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
341         for (({barrier(); }),                                               \
342              pos = rht_dereference_bucket_rcu(head, tbl, hash);             \
343              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);        \
344              pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
345
346 /**
347  * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
348  * @tpos:       the type * to use as a loop cursor.
349  * @pos:        the &struct rhash_head to use as a 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.
353  *
354  * This hash chain list-traversal primitive may safely run concurrently with
355  * the _rcu mutation primitives such as rhashtable_insert() as long as the
356  * traversal is guarded by rcu_read_lock().
357  */
358 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member)            \
359         rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
360                                         tbl, hash, member)
361
362 #endif /* _LINUX_RHASHTABLE_H */