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