]> git.karo-electronics.de Git - karo-tx-linux.git/blob - include/linux/rhashtable.h
Merge https://git.kernel.org/pub/scm/linux/kernel/git/horms/ipvs
[karo-tx-linux.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  * @locks_mask: Mask to apply before accessing locks[]
53  * @locks: Array of spinlocks protecting individual buckets
54  * @buckets: size * hash buckets
55  */
56 struct bucket_table {
57         size_t                  size;
58         unsigned int            locks_mask;
59         spinlock_t              *locks;
60
61         struct rhash_head __rcu *buckets[] ____cacheline_aligned_in_smp;
62 };
63
64 typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
65 typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
66
67 struct rhashtable;
68
69 /**
70  * struct rhashtable_params - Hash table construction parameters
71  * @nelem_hint: Hint on number of elements, should be 75% of desired size
72  * @key_len: Length of key
73  * @key_offset: Offset of key in struct to be hashed
74  * @head_offset: Offset of rhash_head in struct to be hashed
75  * @hash_rnd: Seed to use while hashing
76  * @max_shift: Maximum number of shifts while expanding
77  * @min_shift: Minimum number of shifts while shrinking
78  * @nulls_base: Base value to generate nulls marker
79  * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
80  * @hashfn: Function to hash key
81  * @obj_hashfn: Function to hash object
82  * @grow_decision: If defined, may return true if table should expand
83  * @shrink_decision: If defined, may return true if table should shrink
84  *
85  * Note: when implementing the grow and shrink decision function, min/max
86  * shift must be enforced, otherwise, resizing watermarks they set may be
87  * useless.
88  */
89 struct rhashtable_params {
90         size_t                  nelem_hint;
91         size_t                  key_len;
92         size_t                  key_offset;
93         size_t                  head_offset;
94         u32                     hash_rnd;
95         size_t                  max_shift;
96         size_t                  min_shift;
97         u32                     nulls_base;
98         size_t                  locks_mul;
99         rht_hashfn_t            hashfn;
100         rht_obj_hashfn_t        obj_hashfn;
101         bool                    (*grow_decision)(const struct rhashtable *ht,
102                                                  size_t new_size);
103         bool                    (*shrink_decision)(const struct rhashtable *ht,
104                                                    size_t new_size);
105 };
106
107 /**
108  * struct rhashtable - Hash table handle
109  * @tbl: Bucket table
110  * @future_tbl: Table under construction during expansion/shrinking
111  * @nelems: Number of elements in table
112  * @shift: Current size (1 << shift)
113  * @p: Configuration parameters
114  * @run_work: Deferred worker to expand/shrink asynchronously
115  * @mutex: Mutex to protect current/future table swapping
116  * @walkers: List of active walkers
117  * @being_destroyed: True if table is set up for destruction
118  */
119 struct rhashtable {
120         struct bucket_table __rcu       *tbl;
121         struct bucket_table __rcu       *future_tbl;
122         atomic_t                        nelems;
123         atomic_t                        shift;
124         struct rhashtable_params        p;
125         struct work_struct              run_work;
126         struct mutex                    mutex;
127         struct list_head                walkers;
128         bool                            being_destroyed;
129 };
130
131 /**
132  * struct rhashtable_walker - Hash table walker
133  * @list: List entry on list of walkers
134  * @resize: Resize event occured
135  */
136 struct rhashtable_walker {
137         struct list_head list;
138         bool resize;
139 };
140
141 /**
142  * struct rhashtable_iter - Hash table iterator, fits into netlink cb
143  * @ht: Table to iterate through
144  * @p: Current pointer
145  * @walker: Associated rhashtable walker
146  * @slot: Current slot
147  * @skip: Number of entries to skip in slot
148  */
149 struct rhashtable_iter {
150         struct rhashtable *ht;
151         struct rhash_head *p;
152         struct rhashtable_walker *walker;
153         unsigned int slot;
154         unsigned int skip;
155 };
156
157 static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
158 {
159         return NULLS_MARKER(ht->p.nulls_base + hash);
160 }
161
162 #define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
163         ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
164
165 static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
166 {
167         return ((unsigned long) ptr & 1);
168 }
169
170 static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
171 {
172         return ((unsigned long) ptr) >> 1;
173 }
174
175 #ifdef CONFIG_PROVE_LOCKING
176 int lockdep_rht_mutex_is_held(struct rhashtable *ht);
177 int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
178 #else
179 static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
180 {
181         return 1;
182 }
183
184 static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
185                                              u32 hash)
186 {
187         return 1;
188 }
189 #endif /* CONFIG_PROVE_LOCKING */
190
191 int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
192
193 void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
194 bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
195
196 bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
197 bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
198
199 int rhashtable_expand(struct rhashtable *ht);
200 int rhashtable_shrink(struct rhashtable *ht);
201
202 void *rhashtable_lookup(struct rhashtable *ht, const void *key);
203 void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
204                                 bool (*compare)(void *, void *), void *arg);
205
206 bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj);
207 bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
208                                       struct rhash_head *obj,
209                                       bool (*compare)(void *, void *),
210                                       void *arg);
211
212 int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter);
213 void rhashtable_walk_exit(struct rhashtable_iter *iter);
214 int rhashtable_walk_start(struct rhashtable_iter *iter) __acquires(RCU);
215 void *rhashtable_walk_next(struct rhashtable_iter *iter);
216 void rhashtable_walk_stop(struct rhashtable_iter *iter) __releases(RCU);
217
218 void rhashtable_destroy(struct rhashtable *ht);
219
220 #define rht_dereference(p, ht) \
221         rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
222
223 #define rht_dereference_rcu(p, ht) \
224         rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
225
226 #define rht_dereference_bucket(p, tbl, hash) \
227         rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
228
229 #define rht_dereference_bucket_rcu(p, tbl, hash) \
230         rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
231
232 #define rht_entry(tpos, pos, member) \
233         ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
234
235 /**
236  * rht_for_each_continue - continue iterating over hash chain
237  * @pos:        the &struct rhash_head to use as a loop cursor.
238  * @head:       the previous &struct rhash_head to continue from
239  * @tbl:        the &struct bucket_table
240  * @hash:       the hash value / bucket index
241  */
242 #define rht_for_each_continue(pos, head, tbl, hash) \
243         for (pos = rht_dereference_bucket(head, tbl, hash); \
244              !rht_is_a_nulls(pos); \
245              pos = rht_dereference_bucket((pos)->next, tbl, hash))
246
247 /**
248  * rht_for_each - iterate over hash chain
249  * @pos:        the &struct rhash_head to use as a loop cursor.
250  * @tbl:        the &struct bucket_table
251  * @hash:       the hash value / bucket index
252  */
253 #define rht_for_each(pos, tbl, hash) \
254         rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
255
256 /**
257  * rht_for_each_entry_continue - continue iterating over hash chain
258  * @tpos:       the type * to use as a loop cursor.
259  * @pos:        the &struct rhash_head to use as a loop cursor.
260  * @head:       the previous &struct rhash_head to continue from
261  * @tbl:        the &struct bucket_table
262  * @hash:       the hash value / bucket index
263  * @member:     name of the &struct rhash_head within the hashable struct.
264  */
265 #define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
266         for (pos = rht_dereference_bucket(head, tbl, hash);             \
267              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);    \
268              pos = rht_dereference_bucket((pos)->next, tbl, hash))
269
270 /**
271  * rht_for_each_entry - iterate over hash chain of given type
272  * @tpos:       the type * to use as a loop cursor.
273  * @pos:        the &struct rhash_head to use as a loop cursor.
274  * @tbl:        the &struct bucket_table
275  * @hash:       the hash value / bucket index
276  * @member:     name of the &struct rhash_head within the hashable struct.
277  */
278 #define rht_for_each_entry(tpos, pos, tbl, hash, member)                \
279         rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash],    \
280                                     tbl, hash, member)
281
282 /**
283  * rht_for_each_entry_safe - safely iterate over hash chain of given type
284  * @tpos:       the type * to use as a loop cursor.
285  * @pos:        the &struct rhash_head to use as a loop cursor.
286  * @next:       the &struct rhash_head to use as next in loop cursor.
287  * @tbl:        the &struct bucket_table
288  * @hash:       the hash value / bucket index
289  * @member:     name of the &struct rhash_head within the hashable struct.
290  *
291  * This hash chain list-traversal primitive allows for the looped code to
292  * remove the loop cursor from the list.
293  */
294 #define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member)         \
295         for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
296              next = !rht_is_a_nulls(pos) ?                                  \
297                        rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
298              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);        \
299              pos = next,                                                    \
300              next = !rht_is_a_nulls(pos) ?                                  \
301                        rht_dereference_bucket(pos->next, tbl, hash) : NULL)
302
303 /**
304  * rht_for_each_rcu_continue - continue iterating over rcu hash chain
305  * @pos:        the &struct rhash_head to use as a loop cursor.
306  * @head:       the previous &struct rhash_head to continue from
307  * @tbl:        the &struct bucket_table
308  * @hash:       the hash value / bucket index
309  *
310  * This hash chain list-traversal primitive may safely run concurrently with
311  * the _rcu mutation primitives such as rhashtable_insert() as long as the
312  * traversal is guarded by rcu_read_lock().
313  */
314 #define rht_for_each_rcu_continue(pos, head, tbl, hash)                 \
315         for (({barrier(); }),                                           \
316              pos = rht_dereference_bucket_rcu(head, tbl, hash);         \
317              !rht_is_a_nulls(pos);                                      \
318              pos = rcu_dereference_raw(pos->next))
319
320 /**
321  * rht_for_each_rcu - iterate over rcu hash chain
322  * @pos:        the &struct rhash_head to use as a loop cursor.
323  * @tbl:        the &struct bucket_table
324  * @hash:       the hash value / bucket index
325  *
326  * This hash chain list-traversal primitive may safely run concurrently with
327  * the _rcu mutation primitives such as rhashtable_insert() as long as the
328  * traversal is guarded by rcu_read_lock().
329  */
330 #define rht_for_each_rcu(pos, tbl, hash)                                \
331         rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
332
333 /**
334  * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
335  * @tpos:       the type * to use as a loop cursor.
336  * @pos:        the &struct rhash_head to use as a loop cursor.
337  * @head:       the previous &struct rhash_head to continue from
338  * @tbl:        the &struct bucket_table
339  * @hash:       the hash value / bucket index
340  * @member:     name of the &struct rhash_head within the hashable struct.
341  *
342  * This hash chain list-traversal primitive may safely run concurrently with
343  * the _rcu mutation primitives such as rhashtable_insert() as long as the
344  * traversal is guarded by rcu_read_lock().
345  */
346 #define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
347         for (({barrier(); }),                                               \
348              pos = rht_dereference_bucket_rcu(head, tbl, hash);             \
349              (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);        \
350              pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
351
352 /**
353  * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
354  * @tpos:       the type * to use as a loop cursor.
355  * @pos:        the &struct rhash_head to use as a loop cursor.
356  * @tbl:        the &struct bucket_table
357  * @hash:       the hash value / bucket index
358  * @member:     name of the &struct rhash_head within the hashable struct.
359  *
360  * This hash chain list-traversal primitive may safely run concurrently with
361  * the _rcu mutation primitives such as rhashtable_insert() as long as the
362  * traversal is guarded by rcu_read_lock().
363  */
364 #define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member)            \
365         rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
366                                         tbl, hash, member)
367
368 #endif /* _LINUX_RHASHTABLE_H */