blob: a1688f0a61937a3a8145ce854869d8edc9545e65 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
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/rculist.h>
Thomas Graf97defe12015-01-02 23:00:20 +010022#include <linux/workqueue.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020023
24struct rhash_head {
Thomas Graf5300fdc2014-08-13 16:38:29 +020025 struct rhash_head __rcu *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +020026};
27
28#define INIT_HASH_HEAD(ptr) ((ptr)->next = NULL)
29
Thomas Graf97defe12015-01-02 23:00:20 +010030/**
31 * struct bucket_table - Table of hash buckets
32 * @size: Number of hash buckets
33 * @locks_mask: Mask to apply before accessing locks[]
34 * @locks: Array of spinlocks protecting individual buckets
35 * @buckets: size * hash buckets
36 */
Thomas Graf7e1e7762014-08-02 11:47:44 +020037struct bucket_table {
38 size_t size;
Thomas Graf97defe12015-01-02 23:00:20 +010039 unsigned int locks_mask;
40 spinlock_t *locks;
Thomas Graf7e1e7762014-08-02 11:47:44 +020041 struct rhash_head __rcu *buckets[];
42};
43
44typedef u32 (*rht_hashfn_t)(const void *data, u32 len, u32 seed);
45typedef u32 (*rht_obj_hashfn_t)(const void *data, u32 seed);
46
47struct rhashtable;
48
49/**
50 * struct rhashtable_params - Hash table construction parameters
51 * @nelem_hint: Hint on number of elements, should be 75% of desired size
52 * @key_len: Length of key
53 * @key_offset: Offset of key in struct to be hashed
54 * @head_offset: Offset of rhash_head in struct to be hashed
55 * @hash_rnd: Seed to use while hashing
56 * @max_shift: Maximum number of shifts while expanding
Ying Xue94000172014-09-03 09:22:36 +080057 * @min_shift: Minimum number of shifts while shrinking
Thomas Graf97defe12015-01-02 23:00:20 +010058 * @locks_mul: Number of bucket locks to allocate per cpu (default: 128)
Thomas Graf7e1e7762014-08-02 11:47:44 +020059 * @hashfn: Function to hash key
60 * @obj_hashfn: Function to hash object
61 * @grow_decision: If defined, may return true if table should expand
62 * @shrink_decision: If defined, may return true if table should shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +020063 */
64struct rhashtable_params {
65 size_t nelem_hint;
66 size_t key_len;
67 size_t key_offset;
68 size_t head_offset;
69 u32 hash_rnd;
70 size_t max_shift;
Ying Xue94000172014-09-03 09:22:36 +080071 size_t min_shift;
Thomas Graf97defe12015-01-02 23:00:20 +010072 size_t locks_mul;
Thomas Graf7e1e7762014-08-02 11:47:44 +020073 rht_hashfn_t hashfn;
74 rht_obj_hashfn_t obj_hashfn;
75 bool (*grow_decision)(const struct rhashtable *ht,
76 size_t new_size);
77 bool (*shrink_decision)(const struct rhashtable *ht,
78 size_t new_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +020079};
80
81/**
82 * struct rhashtable - Hash table handle
83 * @tbl: Bucket table
Thomas Graf97defe12015-01-02 23:00:20 +010084 * @future_tbl: Table under construction during expansion/shrinking
Thomas Graf7e1e7762014-08-02 11:47:44 +020085 * @nelems: Number of elements in table
86 * @shift: Current size (1 << shift)
87 * @p: Configuration parameters
Thomas Graf97defe12015-01-02 23:00:20 +010088 * @run_work: Deferred worker to expand/shrink asynchronously
89 * @mutex: Mutex to protect current/future table swapping
90 * @being_destroyed: True if table is set up for destruction
Thomas Graf7e1e7762014-08-02 11:47:44 +020091 */
92struct rhashtable {
93 struct bucket_table __rcu *tbl;
Thomas Graf97defe12015-01-02 23:00:20 +010094 struct bucket_table __rcu *future_tbl;
95 atomic_t nelems;
Thomas Graf7e1e7762014-08-02 11:47:44 +020096 size_t shift;
97 struct rhashtable_params p;
Thomas Graf97defe12015-01-02 23:00:20 +010098 struct delayed_work run_work;
99 struct mutex mutex;
100 bool being_destroyed;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200101};
102
103#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +0100104int lockdep_rht_mutex_is_held(struct rhashtable *ht);
Thomas Graf88d6ed12015-01-02 23:00:16 +0100105int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200106#else
Thomas Graf97defe12015-01-02 23:00:20 +0100107static inline int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200108{
109 return 1;
110}
Thomas Graf88d6ed12015-01-02 23:00:16 +0100111
112static inline int lockdep_rht_bucket_is_held(const struct bucket_table *tbl,
113 u32 hash)
114{
115 return 1;
116}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200117#endif /* CONFIG_PROVE_LOCKING */
118
119int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params);
120
Thomas Graf6eba8222014-11-13 13:45:46 +0100121void rhashtable_insert(struct rhashtable *ht, struct rhash_head *node);
122bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *node);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200123
124bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size);
125bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size);
126
Thomas Graf6eba8222014-11-13 13:45:46 +0100127int rhashtable_expand(struct rhashtable *ht);
128int rhashtable_shrink(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200129
Thomas Graf97defe12015-01-02 23:00:20 +0100130void *rhashtable_lookup(struct rhashtable *ht, const void *key);
131void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200132 bool (*compare)(void *, void *), void *arg);
133
Thomas Graf97defe12015-01-02 23:00:20 +0100134void rhashtable_destroy(struct rhashtable *ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200135
136#define rht_dereference(p, ht) \
137 rcu_dereference_protected(p, lockdep_rht_mutex_is_held(ht))
138
139#define rht_dereference_rcu(p, ht) \
140 rcu_dereference_check(p, lockdep_rht_mutex_is_held(ht))
141
Thomas Graf88d6ed12015-01-02 23:00:16 +0100142#define rht_dereference_bucket(p, tbl, hash) \
143 rcu_dereference_protected(p, lockdep_rht_bucket_is_held(tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200144
Thomas Graf88d6ed12015-01-02 23:00:16 +0100145#define rht_dereference_bucket_rcu(p, tbl, hash) \
146 rcu_dereference_check(p, lockdep_rht_bucket_is_held(tbl, hash))
147
148#define rht_entry(tpos, pos, member) \
149 ({ tpos = container_of(pos, typeof(*tpos), member); 1; })
150
151/**
152 * rht_for_each_continue - continue iterating over hash chain
153 * @pos: the &struct rhash_head to use as a loop cursor.
154 * @head: the previous &struct rhash_head to continue from
155 * @tbl: the &struct bucket_table
156 * @hash: the hash value / bucket index
157 */
158#define rht_for_each_continue(pos, head, tbl, hash) \
159 for (pos = rht_dereference_bucket(head, tbl, hash); \
160 pos; \
161 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200162
163/**
164 * rht_for_each - iterate over hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100165 * @pos: the &struct rhash_head to use as a loop cursor.
166 * @tbl: the &struct bucket_table
167 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200168 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100169#define rht_for_each(pos, tbl, hash) \
170 rht_for_each_continue(pos, (tbl)->buckets[hash], tbl, hash)
171
172/**
173 * rht_for_each_entry_continue - continue iterating over hash chain
174 * @tpos: the type * to use as a loop cursor.
175 * @pos: the &struct rhash_head to use as a loop cursor.
176 * @head: the previous &struct rhash_head to continue from
177 * @tbl: the &struct bucket_table
178 * @hash: the hash value / bucket index
179 * @member: name of the &struct rhash_head within the hashable struct.
180 */
181#define rht_for_each_entry_continue(tpos, pos, head, tbl, hash, member) \
182 for (pos = rht_dereference_bucket(head, tbl, hash); \
183 pos && rht_entry(tpos, pos, member); \
184 pos = rht_dereference_bucket((pos)->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200185
186/**
187 * rht_for_each_entry - iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100188 * @tpos: the type * to use as a loop cursor.
189 * @pos: the &struct rhash_head to use as a loop cursor.
190 * @tbl: the &struct bucket_table
191 * @hash: the hash value / bucket index
192 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100194#define rht_for_each_entry(tpos, pos, tbl, hash, member) \
195 rht_for_each_entry_continue(tpos, pos, (tbl)->buckets[hash], \
196 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200197
198/**
199 * rht_for_each_entry_safe - safely iterate over hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100200 * @tpos: the type * to use as a loop cursor.
201 * @pos: the &struct rhash_head to use as a loop cursor.
202 * @next: the &struct rhash_head to use as next in loop cursor.
203 * @tbl: the &struct bucket_table
204 * @hash: the hash value / bucket index
205 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200206 *
207 * This hash chain list-traversal primitive allows for the looped code to
208 * remove the loop cursor from the list.
209 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100210#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member) \
211 for (pos = rht_dereference_bucket((tbl)->buckets[hash], tbl, hash), \
212 next = pos ? rht_dereference_bucket(pos->next, tbl, hash) \
213 : NULL; \
214 pos && rht_entry(tpos, pos, member); \
215 pos = next)
216
217/**
218 * rht_for_each_rcu_continue - continue iterating over rcu hash chain
219 * @pos: the &struct rhash_head to use as a loop cursor.
220 * @head: the previous &struct rhash_head to continue from
221 * @tbl: the &struct bucket_table
222 * @hash: the hash value / bucket index
223 *
224 * This hash chain list-traversal primitive may safely run concurrently with
225 * the _rcu mutation primitives such as rhashtable_insert() as long as the
226 * traversal is guarded by rcu_read_lock().
227 */
228#define rht_for_each_rcu_continue(pos, head, tbl, hash) \
229 for (({barrier(); }), \
230 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
231 pos; \
232 pos = rcu_dereference_raw(pos->next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200233
234/**
235 * rht_for_each_rcu - iterate over rcu hash chain
Thomas Graf88d6ed12015-01-02 23:00:16 +0100236 * @pos: the &struct rhash_head to use as a loop cursor.
237 * @tbl: the &struct bucket_table
238 * @hash: the hash value / bucket index
Thomas Graf7e1e7762014-08-02 11:47:44 +0200239 *
240 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100241 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200242 * traversal is guarded by rcu_read_lock().
243 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100244#define rht_for_each_rcu(pos, tbl, hash) \
245 rht_for_each_rcu_continue(pos, (tbl)->buckets[hash], tbl, hash)
246
247/**
248 * rht_for_each_entry_rcu_continue - continue iterating over rcu 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 * This hash chain list-traversal primitive may safely run concurrently with
257 * the _rcu mutation primitives such as rhashtable_insert() as long as the
258 * traversal is guarded by rcu_read_lock().
259 */
260#define rht_for_each_entry_rcu_continue(tpos, pos, head, tbl, hash, member) \
261 for (({barrier(); }), \
262 pos = rht_dereference_bucket_rcu(head, tbl, hash); \
263 pos && rht_entry(tpos, pos, member); \
264 pos = rht_dereference_bucket_rcu(pos->next, tbl, hash))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200265
266/**
267 * rht_for_each_entry_rcu - iterate over rcu hash chain of given type
Thomas Graf88d6ed12015-01-02 23:00:16 +0100268 * @tpos: the type * to use as a loop cursor.
269 * @pos: the &struct rhash_head to use as a loop cursor.
270 * @tbl: the &struct bucket_table
271 * @hash: the hash value / bucket index
272 * @member: name of the &struct rhash_head within the hashable struct.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200273 *
274 * This hash chain list-traversal primitive may safely run concurrently with
Thomas Graf88d6ed12015-01-02 23:00:16 +0100275 * the _rcu mutation primitives such as rhashtable_insert() as long as the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200276 * traversal is guarded by rcu_read_lock().
277 */
Thomas Graf88d6ed12015-01-02 23:00:16 +0100278#define rht_for_each_entry_rcu(tpos, pos, tbl, hash, member) \
279 rht_for_each_entry_rcu_continue(tpos, pos, (tbl)->buckets[hash],\
280 tbl, hash, member)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200281
282#endif /* _LINUX_RHASHTABLE_H */