]>
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 | ||
aa34a6cb | 139 | static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash) |
a5ec68e3 | 140 | { |
aa34a6cb | 141 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
c4db8848 HX |
142 | struct bucket_table *new_tbl = |
143 | rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl; | |
aa34a6cb HX |
144 | struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash]; |
145 | int err = -ENOENT; | |
146 | struct rhash_head *head, *next, *entry; | |
147 | spinlock_t *new_bucket_lock; | |
148 | unsigned new_hash; | |
149 | ||
150 | rht_for_each(entry, old_tbl, old_hash) { | |
151 | err = 0; | |
152 | next = rht_dereference_bucket(entry->next, old_tbl, old_hash); | |
153 | ||
154 | if (rht_is_a_nulls(next)) | |
155 | break; | |
a5ec68e3 | 156 | |
aa34a6cb HX |
157 | pprev = &entry->next; |
158 | } | |
a5ec68e3 | 159 | |
aa34a6cb HX |
160 | if (err) |
161 | goto out; | |
97defe1e | 162 | |
aa34a6cb | 163 | new_hash = head_hashfn(ht, new_tbl, entry); |
7e1e7763 | 164 | |
02fd97c3 | 165 | new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); |
7e1e7763 | 166 | |
8f2484bd | 167 | spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); |
aa34a6cb HX |
168 | head = rht_dereference_bucket(new_tbl->buckets[new_hash], |
169 | new_tbl, new_hash); | |
97defe1e | 170 | |
aa34a6cb HX |
171 | if (rht_is_a_nulls(head)) |
172 | INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash); | |
173 | else | |
174 | RCU_INIT_POINTER(entry->next, head); | |
a5ec68e3 | 175 | |
aa34a6cb HX |
176 | rcu_assign_pointer(new_tbl->buckets[new_hash], entry); |
177 | spin_unlock(new_bucket_lock); | |
97defe1e | 178 | |
aa34a6cb | 179 | rcu_assign_pointer(*pprev, next); |
7e1e7763 | 180 | |
aa34a6cb HX |
181 | out: |
182 | return err; | |
183 | } | |
97defe1e | 184 | |
aa34a6cb HX |
185 | static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash) |
186 | { | |
187 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); | |
188 | spinlock_t *old_bucket_lock; | |
189 | ||
02fd97c3 | 190 | old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); |
a5ec68e3 | 191 | |
aa34a6cb HX |
192 | spin_lock_bh(old_bucket_lock); |
193 | while (!rhashtable_rehash_one(ht, old_hash)) | |
194 | ; | |
63d512d0 | 195 | old_tbl->rehash++; |
aa34a6cb | 196 | spin_unlock_bh(old_bucket_lock); |
97defe1e TG |
197 | } |
198 | ||
aa34a6cb HX |
199 | static void rhashtable_rehash(struct rhashtable *ht, |
200 | struct bucket_table *new_tbl) | |
97defe1e | 201 | { |
aa34a6cb | 202 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
eddee5ba | 203 | struct rhashtable_walker *walker; |
aa34a6cb | 204 | unsigned old_hash; |
7cd10db8 | 205 | |
aa34a6cb HX |
206 | /* Make insertions go into the new, empty table right away. Deletions |
207 | * and lookups will be attempted in both tables until we synchronize. | |
aa34a6cb | 208 | */ |
c4db8848 | 209 | rcu_assign_pointer(old_tbl->future_tbl, new_tbl); |
aa34a6cb | 210 | |
9497df88 HX |
211 | /* Ensure the new table is visible to readers. */ |
212 | smp_wmb(); | |
213 | ||
aa34a6cb HX |
214 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) |
215 | rhashtable_rehash_chain(ht, old_hash); | |
216 | ||
217 | /* Publish the new table pointer. */ | |
218 | rcu_assign_pointer(ht->tbl, new_tbl); | |
219 | ||
eddee5ba HX |
220 | list_for_each_entry(walker, &old_tbl->walkers, list) |
221 | walker->tbl = NULL; | |
222 | ||
aa34a6cb HX |
223 | /* Wait for readers. All new readers will see the new |
224 | * table, and thus no references to the old table will | |
225 | * remain. | |
226 | */ | |
9d901bc0 | 227 | call_rcu(&old_tbl->rcu, bucket_table_free_rcu); |
7e1e7763 TG |
228 | } |
229 | ||
230 | /** | |
231 | * rhashtable_expand - Expand hash table while allowing concurrent lookups | |
232 | * @ht: the hash table to expand | |
7e1e7763 | 233 | * |
aa34a6cb | 234 | * A secondary bucket array is allocated and the hash entries are migrated. |
7e1e7763 TG |
235 | * |
236 | * This function may only be called in a context where it is safe to call | |
237 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. | |
238 | * | |
97defe1e TG |
239 | * The caller must ensure that no concurrent resizing occurs by holding |
240 | * ht->mutex. | |
241 | * | |
242 | * It is valid to have concurrent insertions and deletions protected by per | |
243 | * bucket locks or concurrent RCU protected lookups and traversals. | |
7e1e7763 | 244 | */ |
6eba8224 | 245 | int rhashtable_expand(struct rhashtable *ht) |
7e1e7763 TG |
246 | { |
247 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); | |
7e1e7763 TG |
248 | |
249 | ASSERT_RHT_MUTEX(ht); | |
250 | ||
5269b53d | 251 | new_tbl = bucket_table_alloc(ht, old_tbl->size * 2); |
7e1e7763 TG |
252 | if (new_tbl == NULL) |
253 | return -ENOMEM; | |
254 | ||
aa34a6cb | 255 | rhashtable_rehash(ht, new_tbl); |
7e1e7763 TG |
256 | return 0; |
257 | } | |
258 | EXPORT_SYMBOL_GPL(rhashtable_expand); | |
259 | ||
260 | /** | |
261 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups | |
262 | * @ht: the hash table to shrink | |
7e1e7763 TG |
263 | * |
264 | * This function may only be called in a context where it is safe to call | |
265 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. | |
266 | * | |
97defe1e TG |
267 | * The caller must ensure that no concurrent resizing occurs by holding |
268 | * ht->mutex. | |
269 | * | |
7e1e7763 TG |
270 | * The caller must ensure that no concurrent table mutations take place. |
271 | * It is however valid to have concurrent lookups if they are RCU protected. | |
97defe1e TG |
272 | * |
273 | * It is valid to have concurrent insertions and deletions protected by per | |
274 | * bucket locks or concurrent RCU protected lookups and traversals. | |
7e1e7763 | 275 | */ |
6eba8224 | 276 | int rhashtable_shrink(struct rhashtable *ht) |
7e1e7763 | 277 | { |
a5b6846f | 278 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); |
7e1e7763 TG |
279 | |
280 | ASSERT_RHT_MUTEX(ht); | |
281 | ||
5269b53d | 282 | new_tbl = bucket_table_alloc(ht, old_tbl->size / 2); |
97defe1e | 283 | if (new_tbl == NULL) |
7e1e7763 TG |
284 | return -ENOMEM; |
285 | ||
aa34a6cb | 286 | rhashtable_rehash(ht, new_tbl); |
7e1e7763 TG |
287 | return 0; |
288 | } | |
289 | EXPORT_SYMBOL_GPL(rhashtable_shrink); | |
290 | ||
97defe1e TG |
291 | static void rht_deferred_worker(struct work_struct *work) |
292 | { | |
293 | struct rhashtable *ht; | |
294 | struct bucket_table *tbl; | |
295 | ||
57699a40 | 296 | ht = container_of(work, struct rhashtable, run_work); |
97defe1e | 297 | mutex_lock(&ht->mutex); |
28134a53 HX |
298 | if (ht->being_destroyed) |
299 | goto unlock; | |
300 | ||
97defe1e TG |
301 | tbl = rht_dereference(ht->tbl, ht); |
302 | ||
a5b6846f | 303 | if (rht_grow_above_75(ht, tbl)) |
97defe1e | 304 | rhashtable_expand(ht); |
a5b6846f | 305 | else if (rht_shrink_below_30(ht, tbl)) |
97defe1e | 306 | rhashtable_shrink(ht); |
28134a53 | 307 | unlock: |
97defe1e TG |
308 | mutex_unlock(&ht->mutex); |
309 | } | |
310 | ||
02fd97c3 HX |
311 | int rhashtable_insert_slow(struct rhashtable *ht, const void *key, |
312 | struct rhash_head *obj, | |
313 | struct bucket_table *tbl) | |
314 | { | |
315 | struct rhash_head *head; | |
316 | unsigned hash; | |
317 | int err = -EEXIST; | |
318 | ||
319 | hash = head_hashfn(ht, tbl, obj); | |
320 | spin_lock_nested(rht_bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING); | |
321 | ||
322 | if (key && rhashtable_lookup_fast(ht, key, ht->p)) | |
323 | goto exit; | |
324 | ||
325 | err = 0; | |
326 | ||
327 | head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash); | |
328 | ||
329 | RCU_INIT_POINTER(obj->next, head); | |
330 | ||
331 | rcu_assign_pointer(tbl->buckets[hash], obj); | |
332 | ||
333 | atomic_inc(&ht->nelems); | |
334 | ||
335 | exit: | |
336 | spin_unlock(rht_bucket_lock(tbl, hash)); | |
337 | ||
338 | return err; | |
339 | } | |
340 | EXPORT_SYMBOL_GPL(rhashtable_insert_slow); | |
341 | ||
f2dba9c6 HX |
342 | /** |
343 | * rhashtable_walk_init - Initialise an iterator | |
344 | * @ht: Table to walk over | |
345 | * @iter: Hash table Iterator | |
346 | * | |
347 | * This function prepares a hash table walk. | |
348 | * | |
349 | * Note that if you restart a walk after rhashtable_walk_stop you | |
350 | * may see the same object twice. Also, you may miss objects if | |
351 | * there are removals in between rhashtable_walk_stop and the next | |
352 | * call to rhashtable_walk_start. | |
353 | * | |
354 | * For a completely stable walk you should construct your own data | |
355 | * structure outside the hash table. | |
356 | * | |
357 | * This function may sleep so you must not call it from interrupt | |
358 | * context or with spin locks held. | |
359 | * | |
360 | * You must call rhashtable_walk_exit if this function returns | |
361 | * successfully. | |
362 | */ | |
363 | int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter) | |
364 | { | |
365 | iter->ht = ht; | |
366 | iter->p = NULL; | |
367 | iter->slot = 0; | |
368 | iter->skip = 0; | |
369 | ||
370 | iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL); | |
371 | if (!iter->walker) | |
372 | return -ENOMEM; | |
373 | ||
374 | mutex_lock(&ht->mutex); | |
eddee5ba HX |
375 | iter->walker->tbl = rht_dereference(ht->tbl, ht); |
376 | list_add(&iter->walker->list, &iter->walker->tbl->walkers); | |
f2dba9c6 HX |
377 | mutex_unlock(&ht->mutex); |
378 | ||
379 | return 0; | |
380 | } | |
381 | EXPORT_SYMBOL_GPL(rhashtable_walk_init); | |
382 | ||
383 | /** | |
384 | * rhashtable_walk_exit - Free an iterator | |
385 | * @iter: Hash table Iterator | |
386 | * | |
387 | * This function frees resources allocated by rhashtable_walk_init. | |
388 | */ | |
389 | void rhashtable_walk_exit(struct rhashtable_iter *iter) | |
390 | { | |
391 | mutex_lock(&iter->ht->mutex); | |
eddee5ba HX |
392 | if (iter->walker->tbl) |
393 | list_del(&iter->walker->list); | |
f2dba9c6 HX |
394 | mutex_unlock(&iter->ht->mutex); |
395 | kfree(iter->walker); | |
396 | } | |
397 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); | |
398 | ||
399 | /** | |
400 | * rhashtable_walk_start - Start a hash table walk | |
401 | * @iter: Hash table iterator | |
402 | * | |
403 | * Start a hash table walk. Note that we take the RCU lock in all | |
404 | * cases including when we return an error. So you must always call | |
405 | * rhashtable_walk_stop to clean up. | |
406 | * | |
407 | * Returns zero if successful. | |
408 | * | |
409 | * Returns -EAGAIN if resize event occured. Note that the iterator | |
410 | * will rewind back to the beginning and you may use it immediately | |
411 | * by calling rhashtable_walk_next. | |
412 | */ | |
413 | int rhashtable_walk_start(struct rhashtable_iter *iter) | |
db4374f4 | 414 | __acquires(RCU) |
f2dba9c6 | 415 | { |
eddee5ba HX |
416 | struct rhashtable *ht = iter->ht; |
417 | ||
418 | mutex_lock(&ht->mutex); | |
419 | ||
420 | if (iter->walker->tbl) | |
421 | list_del(&iter->walker->list); | |
422 | ||
f2dba9c6 HX |
423 | rcu_read_lock(); |
424 | ||
eddee5ba HX |
425 | mutex_unlock(&ht->mutex); |
426 | ||
427 | if (!iter->walker->tbl) { | |
428 | iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht); | |
f2dba9c6 HX |
429 | return -EAGAIN; |
430 | } | |
431 | ||
432 | return 0; | |
433 | } | |
434 | EXPORT_SYMBOL_GPL(rhashtable_walk_start); | |
435 | ||
436 | /** | |
437 | * rhashtable_walk_next - Return the next object and advance the iterator | |
438 | * @iter: Hash table iterator | |
439 | * | |
440 | * Note that you must call rhashtable_walk_stop when you are finished | |
441 | * with the walk. | |
442 | * | |
443 | * Returns the next object or NULL when the end of the table is reached. | |
444 | * | |
445 | * Returns -EAGAIN if resize event occured. Note that the iterator | |
446 | * will rewind back to the beginning and you may continue to use it. | |
447 | */ | |
448 | void *rhashtable_walk_next(struct rhashtable_iter *iter) | |
449 | { | |
eddee5ba | 450 | struct bucket_table *tbl = iter->walker->tbl; |
f2dba9c6 HX |
451 | struct rhashtable *ht = iter->ht; |
452 | struct rhash_head *p = iter->p; | |
453 | void *obj = NULL; | |
454 | ||
f2dba9c6 HX |
455 | if (p) { |
456 | p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot); | |
457 | goto next; | |
458 | } | |
459 | ||
460 | for (; iter->slot < tbl->size; iter->slot++) { | |
461 | int skip = iter->skip; | |
462 | ||
463 | rht_for_each_rcu(p, tbl, iter->slot) { | |
464 | if (!skip) | |
465 | break; | |
466 | skip--; | |
467 | } | |
468 | ||
469 | next: | |
470 | if (!rht_is_a_nulls(p)) { | |
471 | iter->skip++; | |
472 | iter->p = p; | |
473 | obj = rht_obj(ht, p); | |
474 | goto out; | |
475 | } | |
476 | ||
477 | iter->skip = 0; | |
478 | } | |
479 | ||
c4db8848 HX |
480 | iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
481 | if (iter->walker->tbl) { | |
f2dba9c6 HX |
482 | iter->slot = 0; |
483 | iter->skip = 0; | |
f2dba9c6 HX |
484 | return ERR_PTR(-EAGAIN); |
485 | } | |
486 | ||
eddee5ba HX |
487 | iter->p = NULL; |
488 | ||
489 | out: | |
490 | ||
f2dba9c6 HX |
491 | return obj; |
492 | } | |
493 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); | |
494 | ||
495 | /** | |
496 | * rhashtable_walk_stop - Finish a hash table walk | |
497 | * @iter: Hash table iterator | |
498 | * | |
499 | * Finish a hash table walk. | |
500 | */ | |
501 | void rhashtable_walk_stop(struct rhashtable_iter *iter) | |
db4374f4 | 502 | __releases(RCU) |
f2dba9c6 | 503 | { |
eddee5ba HX |
504 | struct rhashtable *ht; |
505 | struct bucket_table *tbl = iter->walker->tbl; | |
506 | ||
eddee5ba | 507 | if (!tbl) |
963ecbd4 | 508 | goto out; |
eddee5ba HX |
509 | |
510 | ht = iter->ht; | |
511 | ||
512 | mutex_lock(&ht->mutex); | |
c4db8848 | 513 | if (tbl->rehash < tbl->size) |
eddee5ba HX |
514 | list_add(&iter->walker->list, &tbl->walkers); |
515 | else | |
516 | iter->walker->tbl = NULL; | |
517 | mutex_unlock(&ht->mutex); | |
518 | ||
f2dba9c6 | 519 | iter->p = NULL; |
963ecbd4 HX |
520 | |
521 | out: | |
522 | rcu_read_unlock(); | |
f2dba9c6 HX |
523 | } |
524 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); | |
525 | ||
488fb86e | 526 | static size_t rounded_hashtable_size(const struct rhashtable_params *params) |
7e1e7763 | 527 | { |
94000176 | 528 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
e2e21c1c | 529 | (unsigned long)params->min_size); |
7e1e7763 TG |
530 | } |
531 | ||
532 | /** | |
533 | * rhashtable_init - initialize a new hash table | |
534 | * @ht: hash table to be initialized | |
535 | * @params: configuration parameters | |
536 | * | |
537 | * Initializes a new hash table based on the provided configuration | |
538 | * parameters. A table can be configured either with a variable or | |
539 | * fixed length key: | |
540 | * | |
541 | * Configuration Example 1: Fixed length keys | |
542 | * struct test_obj { | |
543 | * int key; | |
544 | * void * my_member; | |
545 | * struct rhash_head node; | |
546 | * }; | |
547 | * | |
548 | * struct rhashtable_params params = { | |
549 | * .head_offset = offsetof(struct test_obj, node), | |
550 | * .key_offset = offsetof(struct test_obj, key), | |
551 | * .key_len = sizeof(int), | |
87545899 | 552 | * .hashfn = jhash, |
f89bd6f8 | 553 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
7e1e7763 TG |
554 | * }; |
555 | * | |
556 | * Configuration Example 2: Variable length keys | |
557 | * struct test_obj { | |
558 | * [...] | |
559 | * struct rhash_head node; | |
560 | * }; | |
561 | * | |
562 | * u32 my_hash_fn(const void *data, u32 seed) | |
563 | * { | |
564 | * struct test_obj *obj = data; | |
565 | * | |
566 | * return [... hash ...]; | |
567 | * } | |
568 | * | |
569 | * struct rhashtable_params params = { | |
570 | * .head_offset = offsetof(struct test_obj, node), | |
87545899 | 571 | * .hashfn = jhash, |
7e1e7763 | 572 | * .obj_hashfn = my_hash_fn, |
7e1e7763 TG |
573 | * }; |
574 | */ | |
488fb86e HX |
575 | int rhashtable_init(struct rhashtable *ht, |
576 | const struct rhashtable_params *params) | |
7e1e7763 TG |
577 | { |
578 | struct bucket_table *tbl; | |
579 | size_t size; | |
580 | ||
581 | size = HASH_DEFAULT_SIZE; | |
582 | ||
02fd97c3 HX |
583 | if ((!(params->key_len && params->hashfn) && !params->obj_hashfn) || |
584 | (params->obj_hashfn && !params->obj_cmpfn)) | |
7e1e7763 TG |
585 | return -EINVAL; |
586 | ||
f89bd6f8 TG |
587 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
588 | return -EINVAL; | |
589 | ||
7e1e7763 | 590 | if (params->nelem_hint) |
94000176 | 591 | size = rounded_hashtable_size(params); |
7e1e7763 | 592 | |
97defe1e TG |
593 | memset(ht, 0, sizeof(*ht)); |
594 | mutex_init(&ht->mutex); | |
595 | memcpy(&ht->p, params, sizeof(*params)); | |
596 | ||
a998f712 TG |
597 | if (params->min_size) |
598 | ht->p.min_size = roundup_pow_of_two(params->min_size); | |
599 | ||
600 | if (params->max_size) | |
601 | ht->p.max_size = rounddown_pow_of_two(params->max_size); | |
602 | ||
488fb86e | 603 | ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE); |
a998f712 | 604 | |
97defe1e TG |
605 | if (params->locks_mul) |
606 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); | |
607 | else | |
608 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; | |
609 | ||
5269b53d | 610 | tbl = bucket_table_alloc(ht, size); |
7e1e7763 TG |
611 | if (tbl == NULL) |
612 | return -ENOMEM; | |
613 | ||
545a148e | 614 | atomic_set(&ht->nelems, 0); |
a5b6846f | 615 | |
7e1e7763 TG |
616 | RCU_INIT_POINTER(ht->tbl, tbl); |
617 | ||
4c4b52d9 | 618 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
97defe1e | 619 | |
7e1e7763 TG |
620 | return 0; |
621 | } | |
622 | EXPORT_SYMBOL_GPL(rhashtable_init); | |
623 | ||
624 | /** | |
625 | * rhashtable_destroy - destroy hash table | |
626 | * @ht: the hash table to destroy | |
627 | * | |
ae82ddcf PNA |
628 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
629 | * has to make sure that no resizing may happen by unpublishing the hashtable | |
630 | * and waiting for the quiescent cycle before releasing the bucket array. | |
7e1e7763 | 631 | */ |
97defe1e | 632 | void rhashtable_destroy(struct rhashtable *ht) |
7e1e7763 | 633 | { |
97defe1e TG |
634 | ht->being_destroyed = true; |
635 | ||
4c4b52d9 | 636 | cancel_work_sync(&ht->run_work); |
97defe1e | 637 | |
57699a40 | 638 | mutex_lock(&ht->mutex); |
97defe1e | 639 | bucket_table_free(rht_dereference(ht->tbl, ht)); |
97defe1e | 640 | mutex_unlock(&ht->mutex); |
7e1e7763 TG |
641 | } |
642 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |