rhashtable: introduce rhashtable_wakeup_worker helper function
[deliverable/linux.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
f89bd6f8 21#include <linux/list_nulls.h>
97defe1e 22#include <linux/workqueue.h>
86b35b64 23#include <linux/mutex.h>
7e1e7763 24
f89bd6f8
TG
25/*
26 * The end of the chain is marked with a special nulls marks which has
27 * the following format:
28 *
29 * +-------+-----------------------------------------------------+-+
30 * | Base | Hash |1|
31 * +-------+-----------------------------------------------------+-+
32 *
33 * Base (4 bits) : Reserved to distinguish between multiple tables.
34 * Specified via &struct rhashtable_params.nulls_base.
35 * Hash (27 bits): Full hash (unmasked) of first element added to bucket
36 * 1 (1 bit) : Nulls marker (always set)
37 *
38 * The remaining bits of the next pointer remain unused for now.
39 */
40#define RHT_BASE_BITS 4
41#define RHT_HASH_BITS 27
42#define RHT_BASE_SHIFT RHT_HASH_BITS
43
7e1e7763 44struct rhash_head {
5300fdcb 45 struct rhash_head __rcu *next;
7e1e7763
TG
46};
47
97defe1e
TG
48/**
49 * struct bucket_table - Table of hash buckets
50 * @size: Number of hash buckets
51 * @locks_mask: Mask to apply before accessing locks[]
52 * @locks: Array of spinlocks protecting individual buckets
53 * @buckets: size * hash buckets
54 */
7e1e7763
TG
55struct bucket_table {
56 size_t size;
97defe1e
TG
57 unsigned int locks_mask;
58 spinlock_t *locks;
7e1e7763
TG
59 struct rhash_head __rcu *buckets[];
60};
61
62typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
63typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
64
65struct rhashtable;
66
67/**
68 * struct rhashtable_params - Hash table construction parameters
69 * @nelem_hint: Hint on number of elements, should be 75% of desired size
70 * @key_len: Length of key
71 * @key_offset: Offset of key in struct to be hashed
72 * @head_offset: Offset of rhash_head in struct to be hashed
73 * @hash_rnd: Seed to use while hashing
74 * @max_shift: Maximum number of shifts while expanding
94000176 75 * @min_shift: Minimum number of shifts while shrinking
f89bd6f8 76 * @nulls_base: Base value to generate nulls marker
97defe1e 77 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
7e1e7763
TG
78 * @hashfn: Function to hash key
79 * @obj_hashfn: Function to hash object
80 * @grow_decision: If defined, may return true if table should expand
81 * @shrink_decision: If defined, may return true if table should shrink
7e1e7763
TG
82 */
83struct rhashtable_params {
84 size_t nelem_hint;
85 size_t key_len;
86 size_t key_offset;
87 size_t head_offset;
88 u32 hash_rnd;
89 size_t max_shift;
94000176 90 size_t min_shift;
f89bd6f8 91 u32 nulls_base;
97defe1e 92 size_t locks_mul;
7e1e7763
TG
93 rht_hashfn_t hashfn;
94 rht_obj_hashfn_t obj_hashfn;
95 bool (*grow_decision)(const struct rhashtable *ht,
96 size_t new_size);
97 bool (*shrink_decision)(const struct rhashtable *ht,
98 size_t new_size);
7e1e7763
TG
99};
100
101/**
102 * struct rhashtable - Hash table handle
103 * @tbl: Bucket table
97defe1e 104 * @future_tbl: Table under construction during expansion/shrinking
7e1e7763
TG
105 * @nelems: Number of elements in table
106 * @shift: Current size (1 << shift)
107 * @p: Configuration parameters
97defe1e
TG
108 * @run_work: Deferred worker to expand/shrink asynchronously
109 * @mutex: Mutex to protect current/future table swapping
110 * @being_destroyed: True if table is set up for destruction
7e1e7763
TG
111 */
112struct rhashtable {
113 struct bucket_table __rcu *tbl;
97defe1e
TG
114 struct bucket_table __rcu *future_tbl;
115 atomic_t nelems;
7e1e7763
TG
116 size_t shift;
117 struct rhashtable_params p;
97defe1e
TG
118 struct delayed_work run_work;
119 struct mutex mutex;
120 bool being_destroyed;
7e1e7763
TG
121};
122
f89bd6f8
TG
123static inline unsigned long rht_marker(const struct rhashtable *ht, u32 hash)
124{
125 return NULLS_MARKER(ht->p.nulls_base + hash);
126}
127
128#define INIT_RHT_NULLS_HEAD(ptr, ht, hash) \
129 ((ptr) = (typeof(ptr)) rht_marker(ht, hash))
130
131static inline bool rht_is_a_nulls(const struct rhash_head *ptr)
132{
133 return ((unsigned long) ptr & 1);
134}
135
136static inline unsigned long rht_get_nulls_value(const struct rhash_head *ptr)
137{
138 return ((unsigned long) ptr) >> 1;
139}
140
7e1e7763 141#ifdef CONFIG_PROVE_LOCKING
97defe1e 142int lockdep_rht_mutex_is_held(struct rhashtable *ht);
88d6ed15 143int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
7e1e7763 144#else
97defe1e 145static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
7e1e7763
TG
146{
147 return 1;
148}
88d6ed15
TG
149
150static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
151 u32 hash)
152{
153 return 1;
154}
7e1e7763
TG
155#endif /* CONFIG_PROVE_LOCKING */
156
157int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
158
6eba8224
TG
159void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
160bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
7e1e7763
TG
161
162bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
163bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
164
6eba8224
TG
165int rhashtable_expand(struct rhashtable *ht);
166int rhashtable_shrink(struct rhashtable *ht);
7e1e7763 167
97defe1e
TG
168void *rhashtable_lookup(struct rhashtable *ht, const void *key);
169void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
7e1e7763
TG
170 bool (*compare)(void *, void *), void *arg);
171
97defe1e 172void rhashtable_destroy(struct rhashtable *ht);
7e1e7763
TG
173
174#define rht_dereference(p, ht) \
175 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
176
177#define rht_dereference_rcu(p, ht) \
178 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
179
88d6ed15
TG
180#define rht_dereference_bucket(p, tbl, hash) \
181 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
7e1e7763 182
88d6ed15
TG
183#define rht_dereference_bucket_rcu(p, tbl, hash) \
184 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
185
186#define rht_entry(tpos, pos, member) \
187 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
7e1e7763
TG
188
189/**
88d6ed15
TG
190 * rht_for_each_continue - continue iterating over hash chain
191 * @pos: the &struct rhash_head to use as a loop cursor.
192 * @head: the previous &struct rhash_head to continue from
193 * @tbl: the &struct bucket_table
194 * @hash: the hash value / bucket index
7e1e7763 195 */
88d6ed15
TG
196#define rht_for_each_continue(pos, head, tbl, hash) \
197 for (pos = rht_dereference_bucket(head, tbl, hash); \
f89bd6f8 198 !rht_is_a_nulls(pos); \
88d6ed15
TG
199 pos = rht_dereference_bucket((pos)->next, tbl, hash))
200
201/**
202 * rht_for_each - iterate over hash chain
203 * @pos: the &struct rhash_head to use as a loop cursor.
204 * @tbl: the &struct bucket_table
205 * @hash: the hash value / bucket index
206 */
207#define rht_for_each(pos, tbl, hash) \
208 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
209
210/**
211 * rht_for_each_entry_continue - continue iterating over hash chain
212 * @tpos: the type * to use as a loop cursor.
213 * @pos: the &struct rhash_head to use as a loop cursor.
214 * @head: the previous &struct rhash_head to continue from
215 * @tbl: the &struct bucket_table
216 * @hash: the hash value / bucket index
217 * @member: name of the &struct rhash_head within the hashable struct.
218 */
219#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
220 for (pos = rht_dereference_bucket(head, tbl, hash); \
f89bd6f8 221 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
88d6ed15 222 pos = rht_dereference_bucket((pos)->next, tbl, hash))
7e1e7763
TG
223
224/**
225 * rht_for_each_entry - iterate over hash chain of given type
88d6ed15
TG
226 * @tpos: the type * to use as a loop cursor.
227 * @pos: the &struct rhash_head to use as a loop cursor.
228 * @tbl: the &struct bucket_table
229 * @hash: the hash value / bucket index
230 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763 231 */
88d6ed15
TG
232#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
233 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
234 tbl, hash, member)
7e1e7763
TG
235
236/**
237 * rht_for_each_entry_safe - safely iterate over hash chain of given type
88d6ed15
TG
238 * @tpos: the type * to use as a loop cursor.
239 * @pos: the &struct rhash_head to use as a loop cursor.
240 * @next: the &struct rhash_head to use as next in loop cursor.
241 * @tbl: the &struct bucket_table
242 * @hash: the hash value / bucket index
243 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763
TG
244 *
245 * This hash chain list-traversal primitive allows for the looped code to
246 * remove the loop cursor from the list.
247 */
88d6ed15
TG
248#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
249 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
f89bd6f8
TG
250 next = !rht_is_a_nulls(pos) ? \
251 rht_dereference_bucket(pos->next, tbl, hash) : NULL; \
252 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
88d6ed15
TG
253 pos = next)
254
255/**
256 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
257 * @pos: the &struct rhash_head to use as a loop cursor.
258 * @head: the previous &struct rhash_head to continue from
259 * @tbl: the &struct bucket_table
260 * @hash: the hash value / bucket index
261 *
262 * This hash chain list-traversal primitive may safely run concurrently with
263 * the _rcu mutation primitives such as rhashtable_insert() as long as the
264 * traversal is guarded by rcu_read_lock().
265 */
266#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
267 for (({barrier(); }), \
268 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
f89bd6f8 269 !rht_is_a_nulls(pos); \
88d6ed15 270 pos = rcu_dereference_raw(pos->next))
7e1e7763
TG
271
272/**
273 * rht_for_each_rcu - iterate over rcu hash chain
88d6ed15
TG
274 * @pos: the &struct rhash_head to use as a loop cursor.
275 * @tbl: the &struct bucket_table
276 * @hash: the hash value / bucket index
7e1e7763
TG
277 *
278 * This hash chain list-traversal primitive may safely run concurrently with
88d6ed15 279 * the _rcu mutation primitives such as rhashtable_insert() as long as the
7e1e7763
TG
280 * traversal is guarded by rcu_read_lock().
281 */
88d6ed15
TG
282#define rht_for_each_rcu(pos, tbl, hash) \
283 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
284
285/**
286 * rht_for_each_entry_rcu_continue - continue iterating over rcu hash chain
287 * @tpos: the type * to use as a loop cursor.
288 * @pos: the &struct rhash_head to use as a loop cursor.
289 * @head: the previous &struct rhash_head to continue from
290 * @tbl: the &struct bucket_table
291 * @hash: the hash value / bucket index
292 * @member: name of the &struct rhash_head within the hashable struct.
293 *
294 * This hash chain list-traversal primitive may safely run concurrently with
295 * the _rcu mutation primitives such as rhashtable_insert() as long as the
296 * traversal is guarded by rcu_read_lock().
297 */
298#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
299 for (({barrier(); }), \
300 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
f89bd6f8 301 (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member); \
88d6ed15 302 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
7e1e7763
TG
303
304/**
305 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
88d6ed15
TG
306 * @tpos: the type * to use as a loop cursor.
307 * @pos: the &struct rhash_head to use as a loop cursor.
308 * @tbl: the &struct bucket_table
309 * @hash: the hash value / bucket index
310 * @member: name of the &struct rhash_head within the hashable struct.
7e1e7763
TG
311 *
312 * This hash chain list-traversal primitive may safely run concurrently with
88d6ed15 313 * the _rcu mutation primitives such as rhashtable_insert() as long as the
7e1e7763
TG
314 * traversal is guarded by rcu_read_lock().
315 */
88d6ed15
TG
316#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
317 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
318 tbl, hash, member)
7e1e7763
TG
319
320#endif /* _LINUX_RHASHTABLE_H */
This page took 0.093972 seconds and 5 git commands to generate.