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