Commit | Line | Data |
---|---|---|
7e1e7763 TG |
1 | /* |
2 | * Resizable, Scalable, Concurrent Hash Table | |
3 | * | |
02fd97c3 | 4 | * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au> |
a5ec68e3 | 5 | * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch> |
7e1e7763 TG |
6 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> |
7 | * | |
7e1e7763 | 8 | * Code partially derived from nft_hash |
02fd97c3 HX |
9 | * Rewritten with rehash code from br_multicast plus single list |
10 | * pointer as suggested by Josh Triplett | |
7e1e7763 TG |
11 | * |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License version 2 as | |
14 | * published by the Free Software Foundation. | |
15 | */ | |
16 | ||
17 | #include <linux/kernel.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/log2.h> | |
5beb5c90 | 20 | #include <linux/sched.h> |
7e1e7763 TG |
21 | #include <linux/slab.h> |
22 | #include <linux/vmalloc.h> | |
23 | #include <linux/mm.h> | |
87545899 | 24 | #include <linux/jhash.h> |
7e1e7763 TG |
25 | #include <linux/random.h> |
26 | #include <linux/rhashtable.h> | |
61d7b097 | 27 | #include <linux/err.h> |
7e1e7763 TG |
28 | |
29 | #define HASH_DEFAULT_SIZE 64UL | |
c2e213cf | 30 | #define HASH_MIN_SIZE 4U |
97defe1e TG |
31 | #define BUCKET_LOCKS_PER_CPU 128UL |
32 | ||
988dfbd7 | 33 | static u32 head_hashfn(struct rhashtable *ht, |
8d24c0b4 TG |
34 | const struct bucket_table *tbl, |
35 | const struct rhash_head *he) | |
7e1e7763 | 36 | { |
02fd97c3 | 37 | return rht_head_hashfn(ht, tbl, he, ht->p); |
7e1e7763 TG |
38 | } |
39 | ||
a03eaec0 | 40 | #ifdef CONFIG_PROVE_LOCKING |
a03eaec0 | 41 | #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) |
a03eaec0 TG |
42 | |
43 | int lockdep_rht_mutex_is_held(struct rhashtable *ht) | |
44 | { | |
45 | return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; | |
46 | } | |
47 | EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); | |
48 | ||
49 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) | |
50 | { | |
02fd97c3 | 51 | spinlock_t *lock = rht_bucket_lock(tbl, hash); |
a03eaec0 TG |
52 | |
53 | return (debug_locks) ? lockdep_is_held(lock) : 1; | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); | |
56 | #else | |
57 | #define ASSERT_RHT_MUTEX(HT) | |
a03eaec0 TG |
58 | #endif |
59 | ||
60 | ||
97defe1e TG |
61 | static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl) |
62 | { | |
63 | unsigned int i, size; | |
64 | #if defined(CONFIG_PROVE_LOCKING) | |
65 | unsigned int nr_pcpus = 2; | |
66 | #else | |
67 | unsigned int nr_pcpus = num_possible_cpus(); | |
68 | #endif | |
69 | ||
70 | nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); | |
71 | size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); | |
72 | ||
a5ec68e3 TG |
73 | /* Never allocate more than 0.5 locks per bucket */ |
74 | size = min_t(unsigned int, size, tbl->size >> 1); | |
97defe1e TG |
75 | |
76 | if (sizeof(spinlock_t) != 0) { | |
77 | #ifdef CONFIG_NUMA | |
78 | if (size * sizeof(spinlock_t) > PAGE_SIZE) | |
79 | tbl->locks = vmalloc(size * sizeof(spinlock_t)); | |
80 | else | |
81 | #endif | |
82 | tbl->locks = kmalloc_array(size, sizeof(spinlock_t), | |
83 | GFP_KERNEL); | |
84 | if (!tbl->locks) | |
85 | return -ENOMEM; | |
86 | for (i = 0; i < size; i++) | |
87 | spin_lock_init(&tbl->locks[i]); | |
88 | } | |
89 | tbl->locks_mask = size - 1; | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
94 | static void bucket_table_free(const struct bucket_table *tbl) | |
95 | { | |
96 | if (tbl) | |
97 | kvfree(tbl->locks); | |
98 | ||
99 | kvfree(tbl); | |
100 | } | |
101 | ||
9d901bc0 HX |
102 | static void bucket_table_free_rcu(struct rcu_head *head) |
103 | { | |
104 | bucket_table_free(container_of(head, struct bucket_table, rcu)); | |
105 | } | |
106 | ||
97defe1e | 107 | static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, |
5269b53d | 108 | size_t nbuckets) |
7e1e7763 | 109 | { |
eb6d1abf | 110 | struct bucket_table *tbl = NULL; |
7e1e7763 | 111 | size_t size; |
f89bd6f8 | 112 | int i; |
7e1e7763 TG |
113 | |
114 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); | |
eb6d1abf DB |
115 | if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) |
116 | tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY); | |
7e1e7763 TG |
117 | if (tbl == NULL) |
118 | tbl = vzalloc(size); | |
7e1e7763 TG |
119 | if (tbl == NULL) |
120 | return NULL; | |
121 | ||
122 | tbl->size = nbuckets; | |
123 | ||
97defe1e TG |
124 | if (alloc_bucket_locks(ht, tbl) < 0) { |
125 | bucket_table_free(tbl); | |
126 | return NULL; | |
127 | } | |
7e1e7763 | 128 | |
eddee5ba HX |
129 | INIT_LIST_HEAD(&tbl->walkers); |
130 | ||
5269b53d HX |
131 | get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd)); |
132 | ||
f89bd6f8 TG |
133 | for (i = 0; i < nbuckets; i++) |
134 | INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); | |
135 | ||
97defe1e | 136 | return tbl; |
7e1e7763 TG |
137 | } |
138 | ||
b824478b HX |
139 | static struct bucket_table *rhashtable_last_table(struct rhashtable *ht, |
140 | struct bucket_table *tbl) | |
141 | { | |
142 | struct bucket_table *new_tbl; | |
143 | ||
144 | do { | |
145 | new_tbl = tbl; | |
146 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); | |
147 | } while (tbl); | |
148 | ||
149 | return new_tbl; | |
150 | } | |
151 | ||
aa34a6cb | 152 | static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash) |
a5ec68e3 | 153 | { |
aa34a6cb | 154 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
b824478b HX |
155 | struct bucket_table *new_tbl = rhashtable_last_table(ht, |
156 | rht_dereference_rcu(old_tbl->future_tbl, ht)); | |
aa34a6cb HX |
157 | struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash]; |
158 | int err = -ENOENT; | |
159 | struct rhash_head *head, *next, *entry; | |
160 | spinlock_t *new_bucket_lock; | |
161 | unsigned new_hash; | |
162 | ||
163 | rht_for_each(entry, old_tbl, old_hash) { | |
164 | err = 0; | |
165 | next = rht_dereference_bucket(entry->next, old_tbl, old_hash); | |
166 | ||
167 | if (rht_is_a_nulls(next)) | |
168 | break; | |
a5ec68e3 | 169 | |
aa34a6cb HX |
170 | pprev = &entry->next; |
171 | } | |
a5ec68e3 | 172 | |
aa34a6cb HX |
173 | if (err) |
174 | goto out; | |
97defe1e | 175 | |
aa34a6cb | 176 | new_hash = head_hashfn(ht, new_tbl, entry); |
7e1e7763 | 177 | |
02fd97c3 | 178 | new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); |
7e1e7763 | 179 | |
8f2484bd | 180 | spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); |
aa34a6cb HX |
181 | head = rht_dereference_bucket(new_tbl->buckets[new_hash], |
182 | new_tbl, new_hash); | |
97defe1e | 183 | |
aa34a6cb HX |
184 | if (rht_is_a_nulls(head)) |
185 | INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash); | |
186 | else | |
187 | RCU_INIT_POINTER(entry->next, head); | |
a5ec68e3 | 188 | |
aa34a6cb HX |
189 | rcu_assign_pointer(new_tbl->buckets[new_hash], entry); |
190 | spin_unlock(new_bucket_lock); | |
97defe1e | 191 | |
aa34a6cb | 192 | rcu_assign_pointer(*pprev, next); |
7e1e7763 | 193 | |
aa34a6cb HX |
194 | out: |
195 | return err; | |
196 | } | |
97defe1e | 197 | |
aa34a6cb HX |
198 | static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash) |
199 | { | |
200 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); | |
201 | spinlock_t *old_bucket_lock; | |
202 | ||
02fd97c3 | 203 | old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); |
a5ec68e3 | 204 | |
aa34a6cb HX |
205 | spin_lock_bh(old_bucket_lock); |
206 | while (!rhashtable_rehash_one(ht, old_hash)) | |
207 | ; | |
63d512d0 | 208 | old_tbl->rehash++; |
aa34a6cb | 209 | spin_unlock_bh(old_bucket_lock); |
97defe1e TG |
210 | } |
211 | ||
b824478b HX |
212 | static int rhashtable_rehash_attach(struct rhashtable *ht, |
213 | struct bucket_table *old_tbl, | |
214 | struct bucket_table *new_tbl) | |
97defe1e | 215 | { |
b824478b HX |
216 | /* Protect future_tbl using the first bucket lock. */ |
217 | spin_lock_bh(old_tbl->locks); | |
218 | ||
219 | /* Did somebody beat us to it? */ | |
220 | if (rcu_access_pointer(old_tbl->future_tbl)) { | |
221 | spin_unlock_bh(old_tbl->locks); | |
222 | return -EEXIST; | |
223 | } | |
7cd10db8 | 224 | |
aa34a6cb HX |
225 | /* Make insertions go into the new, empty table right away. Deletions |
226 | * and lookups will be attempted in both tables until we synchronize. | |
aa34a6cb | 227 | */ |
c4db8848 | 228 | rcu_assign_pointer(old_tbl->future_tbl, new_tbl); |
aa34a6cb | 229 | |
9497df88 HX |
230 | /* Ensure the new table is visible to readers. */ |
231 | smp_wmb(); | |
232 | ||
b824478b HX |
233 | spin_unlock_bh(old_tbl->locks); |
234 | ||
235 | return 0; | |
236 | } | |
237 | ||
238 | static int rhashtable_rehash_table(struct rhashtable *ht) | |
239 | { | |
240 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); | |
241 | struct bucket_table *new_tbl; | |
242 | struct rhashtable_walker *walker; | |
243 | unsigned old_hash; | |
244 | ||
245 | new_tbl = rht_dereference(old_tbl->future_tbl, ht); | |
246 | if (!new_tbl) | |
247 | return 0; | |
248 | ||
aa34a6cb HX |
249 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) |
250 | rhashtable_rehash_chain(ht, old_hash); | |
251 | ||
252 | /* Publish the new table pointer. */ | |
253 | rcu_assign_pointer(ht->tbl, new_tbl); | |
254 | ||
eddee5ba HX |
255 | list_for_each_entry(walker, &old_tbl->walkers, list) |
256 | walker->tbl = NULL; | |
257 | ||
aa34a6cb HX |
258 | /* Wait for readers. All new readers will see the new |
259 | * table, and thus no references to the old table will | |
260 | * remain. | |
261 | */ | |
9d901bc0 | 262 | call_rcu(&old_tbl->rcu, bucket_table_free_rcu); |
b824478b HX |
263 | |
264 | return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0; | |
7e1e7763 TG |
265 | } |
266 | ||
267 | /** | |
268 | * rhashtable_expand - Expand hash table while allowing concurrent lookups | |
269 | * @ht: the hash table to expand | |
7e1e7763 | 270 | * |
aa34a6cb | 271 | * A secondary bucket array is allocated and the hash entries are migrated. |
7e1e7763 TG |
272 | * |
273 | * This function may only be called in a context where it is safe to call | |
274 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. | |
275 | * | |
97defe1e TG |
276 | * The caller must ensure that no concurrent resizing occurs by holding |
277 | * ht->mutex. | |
278 | * | |
279 | * It is valid to have concurrent insertions and deletions protected by per | |
280 | * bucket locks or concurrent RCU protected lookups and traversals. | |
7e1e7763 | 281 | */ |
b824478b | 282 | static int rhashtable_expand(struct rhashtable *ht) |
7e1e7763 TG |
283 | { |
284 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); | |
b824478b | 285 | int err; |
7e1e7763 TG |
286 | |
287 | ASSERT_RHT_MUTEX(ht); | |
288 | ||
b824478b HX |
289 | old_tbl = rhashtable_last_table(ht, old_tbl); |
290 | ||
5269b53d | 291 | new_tbl = bucket_table_alloc(ht, old_tbl->size * 2); |
7e1e7763 TG |
292 | if (new_tbl == NULL) |
293 | return -ENOMEM; | |
294 | ||
b824478b HX |
295 | err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); |
296 | if (err) | |
297 | bucket_table_free(new_tbl); | |
298 | ||
299 | return err; | |
7e1e7763 | 300 | } |
7e1e7763 TG |
301 | |
302 | /** | |
303 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups | |
304 | * @ht: the hash table to shrink | |
7e1e7763 | 305 | * |
18093d1c HX |
306 | * This function shrinks the hash table to fit, i.e., the smallest |
307 | * size would not cause it to expand right away automatically. | |
7e1e7763 | 308 | * |
97defe1e TG |
309 | * The caller must ensure that no concurrent resizing occurs by holding |
310 | * ht->mutex. | |
311 | * | |
7e1e7763 TG |
312 | * The caller must ensure that no concurrent table mutations take place. |
313 | * It is however valid to have concurrent lookups if they are RCU protected. | |
97defe1e TG |
314 | * |
315 | * It is valid to have concurrent insertions and deletions protected by per | |
316 | * bucket locks or concurrent RCU protected lookups and traversals. | |
7e1e7763 | 317 | */ |
b824478b | 318 | static int rhashtable_shrink(struct rhashtable *ht) |
7e1e7763 | 319 | { |
a5b6846f | 320 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
18093d1c | 321 | unsigned size = roundup_pow_of_two(atomic_read(&ht->nelems) * 3 / 2); |
b824478b | 322 | int err; |
7e1e7763 TG |
323 | |
324 | ASSERT_RHT_MUTEX(ht); | |
325 | ||
18093d1c HX |
326 | if (size < ht->p.min_size) |
327 | size = ht->p.min_size; | |
328 | ||
329 | if (old_tbl->size <= size) | |
330 | return 0; | |
331 | ||
b824478b HX |
332 | if (rht_dereference(old_tbl->future_tbl, ht)) |
333 | return -EEXIST; | |
334 | ||
18093d1c | 335 | new_tbl = bucket_table_alloc(ht, size); |
97defe1e | 336 | if (new_tbl == NULL) |
7e1e7763 TG |
337 | return -ENOMEM; |
338 | ||
b824478b HX |
339 | err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); |
340 | if (err) | |
341 | bucket_table_free(new_tbl); | |
342 | ||
343 | return err; | |
7e1e7763 | 344 | } |
7e1e7763 | 345 | |
97defe1e TG |
346 | static void rht_deferred_worker(struct work_struct *work) |
347 | { | |
348 | struct rhashtable *ht; | |
349 | struct bucket_table *tbl; | |
b824478b | 350 | int err = 0; |
97defe1e | 351 | |
57699a40 | 352 | ht = container_of(work, struct rhashtable, run_work); |
97defe1e | 353 | mutex_lock(&ht->mutex); |
28134a53 HX |
354 | if (ht->being_destroyed) |
355 | goto unlock; | |
356 | ||
97defe1e | 357 | tbl = rht_dereference(ht->tbl, ht); |
b824478b | 358 | tbl = rhashtable_last_table(ht, tbl); |
97defe1e | 359 | |
a5b6846f | 360 | if (rht_grow_above_75(ht, tbl)) |
97defe1e | 361 | rhashtable_expand(ht); |
a5b6846f | 362 | else if (rht_shrink_below_30(ht, tbl)) |
97defe1e | 363 | rhashtable_shrink(ht); |
b824478b HX |
364 | |
365 | err = rhashtable_rehash_table(ht); | |
366 | ||
28134a53 | 367 | unlock: |
97defe1e | 368 | mutex_unlock(&ht->mutex); |
b824478b HX |
369 | |
370 | if (err) | |
371 | schedule_work(&ht->run_work); | |
97defe1e TG |
372 | } |
373 | ||
02fd97c3 HX |
374 | int rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
375 | struct rhash_head *obj, | |
376 | struct bucket_table *tbl) | |
377 | { | |
378 | struct rhash_head *head; | |
379 | unsigned hash; | |
380 | int err = -EEXIST; | |
381 | ||
b824478b | 382 | tbl = rhashtable_last_table(ht, tbl); |
02fd97c3 HX |
383 | hash = head_hashfn(ht, tbl, obj); |
384 | spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); | |
385 | ||
386 | if (key && rhashtable_lookup_fast(ht, key, ht->p)) | |
387 | goto exit; | |
388 | ||
389 | err = 0; | |
390 | ||
391 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); | |
392 | ||
393 | RCU_INIT_POINTER(obj->next, head); | |
394 | ||
395 | rcu_assign_pointer(tbl->buckets[hash], obj); | |
396 | ||
397 | atomic_inc(&ht->nelems); | |
398 | ||
399 | exit: | |
400 | spin_unlock(rht_bucket_lock(tbl, hash)); | |
401 | ||
402 | return err; | |
403 | } | |
404 | EXPORT_SYMBOL_GPL(rhashtable_insert_slow); | |
405 | ||
f2dba9c6 HX |
406 | /** |
407 | * rhashtable_walk_init - Initialise an iterator | |
408 | * @ht: Table to walk over | |
409 | * @iter: Hash table Iterator | |
410 | * | |
411 | * This function prepares a hash table walk. | |
412 | * | |
413 | * Note that if you restart a walk after rhashtable_walk_stop you | |
414 | * may see the same object twice. Also, you may miss objects if | |
415 | * there are removals in between rhashtable_walk_stop and the next | |
416 | * call to rhashtable_walk_start. | |
417 | * | |
418 | * For a completely stable walk you should construct your own data | |
419 | * structure outside the hash table. | |
420 | * | |
421 | * This function may sleep so you must not call it from interrupt | |
422 | * context or with spin locks held. | |
423 | * | |
424 | * You must call rhashtable_walk_exit if this function returns | |
425 | * successfully. | |
426 | */ | |
427 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) | |
428 | { | |
429 | iter->ht = ht; | |
430 | iter->p = NULL; | |
431 | iter->slot = 0; | |
432 | iter->skip = 0; | |
433 | ||
434 | iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); | |
435 | if (!iter->walker) | |
436 | return -ENOMEM; | |
437 | ||
438 | mutex_lock(&ht->mutex); | |
eddee5ba HX |
439 | iter->walker->tbl = rht_dereference(ht->tbl, ht); |
440 | list_add(&iter->walker->list, &iter->walker->tbl->walkers); | |
f2dba9c6 HX |
441 | mutex_unlock(&ht->mutex); |
442 | ||
443 | return 0; | |
444 | } | |
445 | EXPORT_SYMBOL_GPL(rhashtable_walk_init); | |
446 | ||
447 | /** | |
448 | * rhashtable_walk_exit - Free an iterator | |
449 | * @iter: Hash table Iterator | |
450 | * | |
451 | * This function frees resources allocated by rhashtable_walk_init. | |
452 | */ | |
453 | void rhashtable_walk_exit(struct rhashtable_iter *iter) | |
454 | { | |
455 | mutex_lock(&iter->ht->mutex); | |
eddee5ba HX |
456 | if (iter->walker->tbl) |
457 | list_del(&iter->walker->list); | |
f2dba9c6 HX |
458 | mutex_unlock(&iter->ht->mutex); |
459 | kfree(iter->walker); | |
460 | } | |
461 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); | |
462 | ||
463 | /** | |
464 | * rhashtable_walk_start - Start a hash table walk | |
465 | * @iter: Hash table iterator | |
466 | * | |
467 | * Start a hash table walk. Note that we take the RCU lock in all | |
468 | * cases including when we return an error. So you must always call | |
469 | * rhashtable_walk_stop to clean up. | |
470 | * | |
471 | * Returns zero if successful. | |
472 | * | |
473 | * Returns -EAGAIN if resize event occured. Note that the iterator | |
474 | * will rewind back to the beginning and you may use it immediately | |
475 | * by calling rhashtable_walk_next. | |
476 | */ | |
477 | int rhashtable_walk_start(struct rhashtable_iter *iter) | |
db4374f4 | 478 | __acquires(RCU) |
f2dba9c6 | 479 | { |
eddee5ba HX |
480 | struct rhashtable *ht = iter->ht; |
481 | ||
482 | mutex_lock(&ht->mutex); | |
483 | ||
484 | if (iter->walker->tbl) | |
485 | list_del(&iter->walker->list); | |
486 | ||
f2dba9c6 HX |
487 | rcu_read_lock(); |
488 | ||
eddee5ba HX |
489 | mutex_unlock(&ht->mutex); |
490 | ||
491 | if (!iter->walker->tbl) { | |
492 | iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); | |
f2dba9c6 HX |
493 | return -EAGAIN; |
494 | } | |
495 | ||
496 | return 0; | |
497 | } | |
498 | EXPORT_SYMBOL_GPL(rhashtable_walk_start); | |
499 | ||
500 | /** | |
501 | * rhashtable_walk_next - Return the next object and advance the iterator | |
502 | * @iter: Hash table iterator | |
503 | * | |
504 | * Note that you must call rhashtable_walk_stop when you are finished | |
505 | * with the walk. | |
506 | * | |
507 | * Returns the next object or NULL when the end of the table is reached. | |
508 | * | |
509 | * Returns -EAGAIN if resize event occured. Note that the iterator | |
510 | * will rewind back to the beginning and you may continue to use it. | |
511 | */ | |
512 | void *rhashtable_walk_next(struct rhashtable_iter *iter) | |
513 | { | |
eddee5ba | 514 | struct bucket_table *tbl = iter->walker->tbl; |
f2dba9c6 HX |
515 | struct rhashtable *ht = iter->ht; |
516 | struct rhash_head *p = iter->p; | |
517 | void *obj = NULL; | |
518 | ||
f2dba9c6 HX |
519 | if (p) { |
520 | p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); | |
521 | goto next; | |
522 | } | |
523 | ||
524 | for (; iter->slot < tbl->size; iter->slot++) { | |
525 | int skip = iter->skip; | |
526 | ||
527 | rht_for_each_rcu(p, tbl, iter->slot) { | |
528 | if (!skip) | |
529 | break; | |
530 | skip--; | |
531 | } | |
532 | ||
533 | next: | |
534 | if (!rht_is_a_nulls(p)) { | |
535 | iter->skip++; | |
536 | iter->p = p; | |
537 | obj = rht_obj(ht, p); | |
538 | goto out; | |
539 | } | |
540 | ||
541 | iter->skip = 0; | |
542 | } | |
543 | ||
d88252f9 HX |
544 | /* Ensure we see any new tables. */ |
545 | smp_rmb(); | |
546 | ||
c4db8848 HX |
547 | iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
548 | if (iter->walker->tbl) { | |
f2dba9c6 HX |
549 | iter->slot = 0; |
550 | iter->skip = 0; | |
f2dba9c6 HX |
551 | return ERR_PTR(-EAGAIN); |
552 | } | |
553 | ||
eddee5ba HX |
554 | iter->p = NULL; |
555 | ||
556 | out: | |
557 | ||
f2dba9c6 HX |
558 | return obj; |
559 | } | |
560 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); | |
561 | ||
562 | /** | |
563 | * rhashtable_walk_stop - Finish a hash table walk | |
564 | * @iter: Hash table iterator | |
565 | * | |
566 | * Finish a hash table walk. | |
567 | */ | |
568 | void rhashtable_walk_stop(struct rhashtable_iter *iter) | |
db4374f4 | 569 | __releases(RCU) |
f2dba9c6 | 570 | { |
eddee5ba HX |
571 | struct rhashtable *ht; |
572 | struct bucket_table *tbl = iter->walker->tbl; | |
573 | ||
eddee5ba | 574 | if (!tbl) |
963ecbd4 | 575 | goto out; |
eddee5ba HX |
576 | |
577 | ht = iter->ht; | |
578 | ||
579 | mutex_lock(&ht->mutex); | |
c4db8848 | 580 | if (tbl->rehash < tbl->size) |
eddee5ba HX |
581 | list_add(&iter->walker->list, &tbl->walkers); |
582 | else | |
583 | iter->walker->tbl = NULL; | |
584 | mutex_unlock(&ht->mutex); | |
585 | ||
f2dba9c6 | 586 | iter->p = NULL; |
963ecbd4 HX |
587 | |
588 | out: | |
589 | rcu_read_unlock(); | |
f2dba9c6 HX |
590 | } |
591 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); | |
592 | ||
488fb86e | 593 | static size_t rounded_hashtable_size(const struct rhashtable_params *params) |
7e1e7763 | 594 | { |
94000176 | 595 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
e2e21c1c | 596 | (unsigned long)params->min_size); |
7e1e7763 TG |
597 | } |
598 | ||
31ccde2d HX |
599 | static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) |
600 | { | |
601 | return jhash2(key, length, seed); | |
602 | } | |
603 | ||
7e1e7763 TG |
604 | /** |
605 | * rhashtable_init - initialize a new hash table | |
606 | * @ht: hash table to be initialized | |
607 | * @params: configuration parameters | |
608 | * | |
609 | * Initializes a new hash table based on the provided configuration | |
610 | * parameters. A table can be configured either with a variable or | |
611 | * fixed length key: | |
612 | * | |
613 | * Configuration Example 1: Fixed length keys | |
614 | * struct test_obj { | |
615 | * int key; | |
616 | * void * my_member; | |
617 | * struct rhash_head node; | |
618 | * }; | |
619 | * | |
620 | * struct rhashtable_params params = { | |
621 | * .head_offset = offsetof(struct test_obj, node), | |
622 | * .key_offset = offsetof(struct test_obj, key), | |
623 | * .key_len = sizeof(int), | |
87545899 | 624 | * .hashfn = jhash, |
f89bd6f8 | 625 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
7e1e7763 TG |
626 | * }; |
627 | * | |
628 | * Configuration Example 2: Variable length keys | |
629 | * struct test_obj { | |
630 | * [...] | |
631 | * struct rhash_head node; | |
632 | * }; | |
633 | * | |
634 | * u32 my_hash_fn(const void *data, u32 seed) | |
635 | * { | |
636 | * struct test_obj *obj = data; | |
637 | * | |
638 | * return [... hash ...]; | |
639 | * } | |
640 | * | |
641 | * struct rhashtable_params params = { | |
642 | * .head_offset = offsetof(struct test_obj, node), | |
87545899 | 643 | * .hashfn = jhash, |
7e1e7763 | 644 | * .obj_hashfn = my_hash_fn, |
7e1e7763 TG |
645 | * }; |
646 | */ | |
488fb86e HX |
647 | int rhashtable_init(struct rhashtable *ht, |
648 | const struct rhashtable_params *params) | |
7e1e7763 TG |
649 | { |
650 | struct bucket_table *tbl; | |
651 | size_t size; | |
652 | ||
653 | size = HASH_DEFAULT_SIZE; | |
654 | ||
31ccde2d | 655 | if ((!params->key_len && !params->obj_hashfn) || |
02fd97c3 | 656 | (params->obj_hashfn && !params->obj_cmpfn)) |
7e1e7763 TG |
657 | return -EINVAL; |
658 | ||
f89bd6f8 TG |
659 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
660 | return -EINVAL; | |
661 | ||
7e1e7763 | 662 | if (params->nelem_hint) |
94000176 | 663 | size = rounded_hashtable_size(params); |
7e1e7763 | 664 | |
97defe1e TG |
665 | memset(ht, 0, sizeof(*ht)); |
666 | mutex_init(&ht->mutex); | |
667 | memcpy(&ht->p, params, sizeof(*params)); | |
668 | ||
a998f712 TG |
669 | if (params->min_size) |
670 | ht->p.min_size = roundup_pow_of_two(params->min_size); | |
671 | ||
672 | if (params->max_size) | |
673 | ht->p.max_size = rounddown_pow_of_two(params->max_size); | |
674 | ||
488fb86e | 675 | ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); |
a998f712 | 676 | |
97defe1e TG |
677 | if (params->locks_mul) |
678 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); | |
679 | else | |
680 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; | |
681 | ||
31ccde2d HX |
682 | ht->key_len = ht->p.key_len; |
683 | if (!params->hashfn) { | |
684 | ht->p.hashfn = jhash; | |
685 | ||
686 | if (!(ht->key_len & (sizeof(u32) - 1))) { | |
687 | ht->key_len /= sizeof(u32); | |
688 | ht->p.hashfn = rhashtable_jhash2; | |
689 | } | |
690 | } | |
691 | ||
5269b53d | 692 | tbl = bucket_table_alloc(ht, size); |
7e1e7763 TG |
693 | if (tbl == NULL) |
694 | return -ENOMEM; | |
695 | ||
545a148e | 696 | atomic_set(&ht->nelems, 0); |
a5b6846f | 697 | |
7e1e7763 TG |
698 | RCU_INIT_POINTER(ht->tbl, tbl); |
699 | ||
4c4b52d9 | 700 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
97defe1e | 701 | |
7e1e7763 TG |
702 | return 0; |
703 | } | |
704 | EXPORT_SYMBOL_GPL(rhashtable_init); | |
705 | ||
706 | /** | |
707 | * rhashtable_destroy - destroy hash table | |
708 | * @ht: the hash table to destroy | |
709 | * | |
ae82ddcf PNA |
710 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
711 | * has to make sure that no resizing may happen by unpublishing the hashtable | |
712 | * and waiting for the quiescent cycle before releasing the bucket array. | |
7e1e7763 | 713 | */ |
97defe1e | 714 | void rhashtable_destroy(struct rhashtable *ht) |
7e1e7763 | 715 | { |
97defe1e TG |
716 | ht->being_destroyed = true; |
717 | ||
4c4b52d9 | 718 | cancel_work_sync(&ht->run_work); |
97defe1e | 719 | |
57699a40 | 720 | mutex_lock(&ht->mutex); |
97defe1e | 721 | bucket_table_free(rht_dereference(ht->tbl, ht)); |
97defe1e | 722 | mutex_unlock(&ht->mutex); |
7e1e7763 TG |
723 | } |
724 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |