]>
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 | ||
07ee0722 | 17 | #include <linux/atomic.h> |
7e1e7763 TG |
18 | #include <linux/kernel.h> |
19 | #include <linux/init.h> | |
20 | #include <linux/log2.h> | |
5beb5c90 | 21 | #include <linux/sched.h> |
b2d09103 | 22 | #include <linux/rculist.h> |
7e1e7763 TG |
23 | #include <linux/slab.h> |
24 | #include <linux/vmalloc.h> | |
25 | #include <linux/mm.h> | |
87545899 | 26 | #include <linux/jhash.h> |
7e1e7763 TG |
27 | #include <linux/random.h> |
28 | #include <linux/rhashtable.h> | |
61d7b097 | 29 | #include <linux/err.h> |
6d795413 | 30 | #include <linux/export.h> |
7e1e7763 TG |
31 | |
32 | #define HASH_DEFAULT_SIZE 64UL | |
c2e213cf | 33 | #define HASH_MIN_SIZE 4U |
4cf0b354 | 34 | #define BUCKET_LOCKS_PER_CPU 32UL |
97defe1e | 35 | |
da20420f HX |
36 | union nested_table { |
37 | union nested_table __rcu *table; | |
38 | struct rhash_head __rcu *bucket; | |
39 | }; | |
40 | ||
988dfbd7 | 41 | static u32 head_hashfn(struct rhashtable *ht, |
8d24c0b4 TG |
42 | const struct bucket_table *tbl, |
43 | const struct rhash_head *he) | |
7e1e7763 | 44 | { |
02fd97c3 | 45 | return rht_head_hashfn(ht, tbl, he, ht->p); |
7e1e7763 TG |
46 | } |
47 | ||
a03eaec0 | 48 | #ifdef CONFIG_PROVE_LOCKING |
a03eaec0 | 49 | #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) |
a03eaec0 TG |
50 | |
51 | int lockdep_rht_mutex_is_held(struct rhashtable *ht) | |
52 | { | |
53 | return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; | |
54 | } | |
55 | EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); | |
56 | ||
57 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) | |
58 | { | |
02fd97c3 | 59 | spinlock_t *lock = rht_bucket_lock(tbl, hash); |
a03eaec0 TG |
60 | |
61 | return (debug_locks) ? lockdep_is_held(lock) : 1; | |
62 | } | |
63 | EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); | |
64 | #else | |
65 | #define ASSERT_RHT_MUTEX(HT) | |
a03eaec0 TG |
66 | #endif |
67 | ||
da20420f HX |
68 | static void nested_table_free(union nested_table *ntbl, unsigned int size) |
69 | { | |
70 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); | |
71 | const unsigned int len = 1 << shift; | |
72 | unsigned int i; | |
73 | ||
74 | ntbl = rcu_dereference_raw(ntbl->table); | |
75 | if (!ntbl) | |
76 | return; | |
77 | ||
78 | if (size > len) { | |
79 | size >>= shift; | |
80 | for (i = 0; i < len; i++) | |
81 | nested_table_free(ntbl + i, size); | |
82 | } | |
83 | ||
84 | kfree(ntbl); | |
85 | } | |
86 | ||
87 | static void nested_bucket_table_free(const struct bucket_table *tbl) | |
88 | { | |
89 | unsigned int size = tbl->size >> tbl->nest; | |
90 | unsigned int len = 1 << tbl->nest; | |
91 | union nested_table *ntbl; | |
92 | unsigned int i; | |
93 | ||
94 | ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); | |
95 | ||
96 | for (i = 0; i < len; i++) | |
97 | nested_table_free(ntbl + i, size); | |
98 | ||
99 | kfree(ntbl); | |
100 | } | |
101 | ||
97defe1e TG |
102 | static void bucket_table_free(const struct bucket_table *tbl) |
103 | { | |
da20420f HX |
104 | if (tbl->nest) |
105 | nested_bucket_table_free(tbl); | |
106 | ||
64e0cd0d | 107 | free_bucket_spinlocks(tbl->locks); |
97defe1e TG |
108 | kvfree(tbl); |
109 | } | |
110 | ||
9d901bc0 HX |
111 | static void bucket_table_free_rcu(struct rcu_head *head) |
112 | { | |
113 | bucket_table_free(container_of(head, struct bucket_table, rcu)); | |
114 | } | |
115 | ||
da20420f HX |
116 | static union nested_table *nested_table_alloc(struct rhashtable *ht, |
117 | union nested_table __rcu **prev, | |
5af68ef7 | 118 | bool leaf) |
da20420f HX |
119 | { |
120 | union nested_table *ntbl; | |
121 | int i; | |
122 | ||
123 | ntbl = rcu_dereference(*prev); | |
124 | if (ntbl) | |
125 | return ntbl; | |
126 | ||
127 | ntbl = kzalloc(PAGE_SIZE, GFP_ATOMIC); | |
128 | ||
5af68ef7 N |
129 | if (ntbl && leaf) { |
130 | for (i = 0; i < PAGE_SIZE / sizeof(ntbl[0]); i++) | |
9b4f64a2 | 131 | INIT_RHT_NULLS_HEAD(ntbl[i].bucket); |
da20420f HX |
132 | } |
133 | ||
134 | rcu_assign_pointer(*prev, ntbl); | |
135 | ||
136 | return ntbl; | |
137 | } | |
138 | ||
139 | static struct bucket_table *nested_bucket_table_alloc(struct rhashtable *ht, | |
140 | size_t nbuckets, | |
141 | gfp_t gfp) | |
142 | { | |
143 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); | |
144 | struct bucket_table *tbl; | |
145 | size_t size; | |
146 | ||
147 | if (nbuckets < (1 << (shift + 1))) | |
148 | return NULL; | |
149 | ||
150 | size = sizeof(*tbl) + sizeof(tbl->buckets[0]); | |
151 | ||
152 | tbl = kzalloc(size, gfp); | |
153 | if (!tbl) | |
154 | return NULL; | |
155 | ||
156 | if (!nested_table_alloc(ht, (union nested_table __rcu **)tbl->buckets, | |
5af68ef7 | 157 | false)) { |
da20420f HX |
158 | kfree(tbl); |
159 | return NULL; | |
160 | } | |
161 | ||
162 | tbl->nest = (ilog2(nbuckets) - 1) % shift + 1; | |
163 | ||
164 | return tbl; | |
165 | } | |
166 | ||
97defe1e | 167 | static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, |
b9ecfdaa HX |
168 | size_t nbuckets, |
169 | gfp_t gfp) | |
7e1e7763 | 170 | { |
eb6d1abf | 171 | struct bucket_table *tbl = NULL; |
64e0cd0d | 172 | size_t size, max_locks; |
f89bd6f8 | 173 | int i; |
7e1e7763 TG |
174 | |
175 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); | |
93f976b5 | 176 | tbl = kvzalloc(size, gfp); |
da20420f HX |
177 | |
178 | size = nbuckets; | |
179 | ||
2d22ecf6 | 180 | if (tbl == NULL && (gfp & ~__GFP_NOFAIL) != GFP_KERNEL) { |
da20420f HX |
181 | tbl = nested_bucket_table_alloc(ht, nbuckets, gfp); |
182 | nbuckets = 0; | |
183 | } | |
2d22ecf6 | 184 | |
7e1e7763 TG |
185 | if (tbl == NULL) |
186 | return NULL; | |
187 | ||
da20420f | 188 | tbl->size = size; |
7e1e7763 | 189 | |
64e0cd0d TH |
190 | max_locks = size >> 1; |
191 | if (tbl->nest) | |
192 | max_locks = min_t(size_t, max_locks, 1U << tbl->nest); | |
193 | ||
194 | if (alloc_bucket_spinlocks(&tbl->locks, &tbl->locks_mask, max_locks, | |
195 | ht->p.locks_mul, gfp) < 0) { | |
97defe1e TG |
196 | bucket_table_free(tbl); |
197 | return NULL; | |
198 | } | |
7e1e7763 | 199 | |
eddee5ba HX |
200 | INIT_LIST_HEAD(&tbl->walkers); |
201 | ||
d48ad080 | 202 | tbl->hash_rnd = get_random_u32(); |
5269b53d | 203 | |
f89bd6f8 | 204 | for (i = 0; i < nbuckets; i++) |
9b4f64a2 | 205 | INIT_RHT_NULLS_HEAD(tbl->buckets[i]); |
f89bd6f8 | 206 | |
97defe1e | 207 | return tbl; |
7e1e7763 TG |
208 | } |
209 | ||
b824478b HX |
210 | static struct bucket_table *rhashtable_last_table(struct rhashtable *ht, |
211 | struct bucket_table *tbl) | |
212 | { | |
213 | struct bucket_table *new_tbl; | |
214 | ||
215 | do { | |
216 | new_tbl = tbl; | |
217 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); | |
218 | } while (tbl); | |
219 | ||
220 | return new_tbl; | |
221 | } | |
222 | ||
299e5c32 | 223 | static int rhashtable_rehash_one(struct rhashtable *ht, unsigned int old_hash) |
a5ec68e3 | 224 | { |
aa34a6cb | 225 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
c0690016 | 226 | struct bucket_table *new_tbl = rhashtable_last_table(ht, old_tbl); |
da20420f HX |
227 | struct rhash_head __rcu **pprev = rht_bucket_var(old_tbl, old_hash); |
228 | int err = -EAGAIN; | |
aa34a6cb HX |
229 | struct rhash_head *head, *next, *entry; |
230 | spinlock_t *new_bucket_lock; | |
299e5c32 | 231 | unsigned int new_hash; |
aa34a6cb | 232 | |
da20420f HX |
233 | if (new_tbl->nest) |
234 | goto out; | |
235 | ||
236 | err = -ENOENT; | |
237 | ||
aa34a6cb HX |
238 | rht_for_each(entry, old_tbl, old_hash) { |
239 | err = 0; | |
240 | next = rht_dereference_bucket(entry->next, old_tbl, old_hash); | |
241 | ||
242 | if (rht_is_a_nulls(next)) | |
243 | break; | |
a5ec68e3 | 244 | |
aa34a6cb HX |
245 | pprev = &entry->next; |
246 | } | |
a5ec68e3 | 247 | |
aa34a6cb HX |
248 | if (err) |
249 | goto out; | |
97defe1e | 250 | |
aa34a6cb | 251 | new_hash = head_hashfn(ht, new_tbl, entry); |
7e1e7763 | 252 | |
02fd97c3 | 253 | new_bucket_lock = rht_bucket_lock(new_tbl, new_hash); |
7e1e7763 | 254 | |
8f2484bd | 255 | spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING); |
aa34a6cb HX |
256 | head = rht_dereference_bucket(new_tbl->buckets[new_hash], |
257 | new_tbl, new_hash); | |
97defe1e | 258 | |
7def0f95 | 259 | RCU_INIT_POINTER(entry->next, head); |
a5ec68e3 | 260 | |
aa34a6cb HX |
261 | rcu_assign_pointer(new_tbl->buckets[new_hash], entry); |
262 | spin_unlock(new_bucket_lock); | |
97defe1e | 263 | |
aa34a6cb | 264 | rcu_assign_pointer(*pprev, next); |
7e1e7763 | 265 | |
aa34a6cb HX |
266 | out: |
267 | return err; | |
268 | } | |
97defe1e | 269 | |
da20420f | 270 | static int rhashtable_rehash_chain(struct rhashtable *ht, |
299e5c32 | 271 | unsigned int old_hash) |
aa34a6cb HX |
272 | { |
273 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); | |
274 | spinlock_t *old_bucket_lock; | |
da20420f | 275 | int err; |
aa34a6cb | 276 | |
02fd97c3 | 277 | old_bucket_lock = rht_bucket_lock(old_tbl, old_hash); |
a5ec68e3 | 278 | |
aa34a6cb | 279 | spin_lock_bh(old_bucket_lock); |
da20420f | 280 | while (!(err = rhashtable_rehash_one(ht, old_hash))) |
aa34a6cb | 281 | ; |
da20420f HX |
282 | |
283 | if (err == -ENOENT) { | |
284 | old_tbl->rehash++; | |
285 | err = 0; | |
286 | } | |
aa34a6cb | 287 | spin_unlock_bh(old_bucket_lock); |
da20420f HX |
288 | |
289 | return err; | |
97defe1e TG |
290 | } |
291 | ||
b824478b HX |
292 | static int rhashtable_rehash_attach(struct rhashtable *ht, |
293 | struct bucket_table *old_tbl, | |
294 | struct bucket_table *new_tbl) | |
97defe1e | 295 | { |
aa34a6cb HX |
296 | /* Make insertions go into the new, empty table right away. Deletions |
297 | * and lookups will be attempted in both tables until we synchronize. | |
0ad66449 N |
298 | * As cmpxchg() provides strong barriers, we do not need |
299 | * rcu_assign_pointer(). | |
aa34a6cb | 300 | */ |
aa34a6cb | 301 | |
0ad66449 N |
302 | if (cmpxchg(&old_tbl->future_tbl, NULL, new_tbl) != NULL) |
303 | return -EEXIST; | |
b824478b HX |
304 | |
305 | return 0; | |
306 | } | |
307 | ||
308 | static int rhashtable_rehash_table(struct rhashtable *ht) | |
309 | { | |
310 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); | |
311 | struct bucket_table *new_tbl; | |
312 | struct rhashtable_walker *walker; | |
299e5c32 | 313 | unsigned int old_hash; |
da20420f | 314 | int err; |
b824478b HX |
315 | |
316 | new_tbl = rht_dereference(old_tbl->future_tbl, ht); | |
317 | if (!new_tbl) | |
318 | return 0; | |
319 | ||
da20420f HX |
320 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) { |
321 | err = rhashtable_rehash_chain(ht, old_hash); | |
322 | if (err) | |
323 | return err; | |
ae6da1f5 | 324 | cond_resched(); |
da20420f | 325 | } |
aa34a6cb HX |
326 | |
327 | /* Publish the new table pointer. */ | |
328 | rcu_assign_pointer(ht->tbl, new_tbl); | |
329 | ||
ba7c95ea | 330 | spin_lock(&ht->lock); |
eddee5ba HX |
331 | list_for_each_entry(walker, &old_tbl->walkers, list) |
332 | walker->tbl = NULL; | |
ba7c95ea | 333 | spin_unlock(&ht->lock); |
eddee5ba | 334 | |
aa34a6cb HX |
335 | /* Wait for readers. All new readers will see the new |
336 | * table, and thus no references to the old table will | |
337 | * remain. | |
338 | */ | |
9d901bc0 | 339 | call_rcu(&old_tbl->rcu, bucket_table_free_rcu); |
b824478b HX |
340 | |
341 | return rht_dereference(new_tbl->future_tbl, ht) ? -EAGAIN : 0; | |
7e1e7763 TG |
342 | } |
343 | ||
da20420f HX |
344 | static int rhashtable_rehash_alloc(struct rhashtable *ht, |
345 | struct bucket_table *old_tbl, | |
346 | unsigned int size) | |
7e1e7763 | 347 | { |
da20420f | 348 | struct bucket_table *new_tbl; |
b824478b | 349 | int err; |
7e1e7763 TG |
350 | |
351 | ASSERT_RHT_MUTEX(ht); | |
352 | ||
da20420f | 353 | new_tbl = bucket_table_alloc(ht, size, GFP_KERNEL); |
7e1e7763 TG |
354 | if (new_tbl == NULL) |
355 | return -ENOMEM; | |
356 | ||
b824478b HX |
357 | err = rhashtable_rehash_attach(ht, old_tbl, new_tbl); |
358 | if (err) | |
359 | bucket_table_free(new_tbl); | |
360 | ||
361 | return err; | |
7e1e7763 | 362 | } |
7e1e7763 TG |
363 | |
364 | /** | |
365 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups | |
366 | * @ht: the hash table to shrink | |
7e1e7763 | 367 | * |
18093d1c HX |
368 | * This function shrinks the hash table to fit, i.e., the smallest |
369 | * size would not cause it to expand right away automatically. | |
7e1e7763 | 370 | * |
97defe1e TG |
371 | * The caller must ensure that no concurrent resizing occurs by holding |
372 | * ht->mutex. | |
373 | * | |
7e1e7763 TG |
374 | * The caller must ensure that no concurrent table mutations take place. |
375 | * It is however valid to have concurrent lookups if they are RCU protected. | |
97defe1e TG |
376 | * |
377 | * It is valid to have concurrent insertions and deletions protected by per | |
378 | * bucket locks or concurrent RCU protected lookups and traversals. | |
7e1e7763 | 379 | */ |
b824478b | 380 | static int rhashtable_shrink(struct rhashtable *ht) |
7e1e7763 | 381 | { |
da20420f | 382 | struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht); |
12311959 VN |
383 | unsigned int nelems = atomic_read(&ht->nelems); |
384 | unsigned int size = 0; | |
7e1e7763 | 385 | |
12311959 VN |
386 | if (nelems) |
387 | size = roundup_pow_of_two(nelems * 3 / 2); | |
18093d1c HX |
388 | if (size < ht->p.min_size) |
389 | size = ht->p.min_size; | |
390 | ||
391 | if (old_tbl->size <= size) | |
392 | return 0; | |
393 | ||
b824478b HX |
394 | if (rht_dereference(old_tbl->future_tbl, ht)) |
395 | return -EEXIST; | |
396 | ||
da20420f | 397 | return rhashtable_rehash_alloc(ht, old_tbl, size); |
7e1e7763 | 398 | } |
7e1e7763 | 399 | |
97defe1e TG |
400 | static void rht_deferred_worker(struct work_struct *work) |
401 | { | |
402 | struct rhashtable *ht; | |
403 | struct bucket_table *tbl; | |
b824478b | 404 | int err = 0; |
97defe1e | 405 | |
57699a40 | 406 | ht = container_of(work, struct rhashtable, run_work); |
97defe1e | 407 | mutex_lock(&ht->mutex); |
28134a53 | 408 | |
97defe1e | 409 | tbl = rht_dereference(ht->tbl, ht); |
b824478b | 410 | tbl = rhashtable_last_table(ht, tbl); |
97defe1e | 411 | |
a5b6846f | 412 | if (rht_grow_above_75(ht, tbl)) |
da20420f | 413 | err = rhashtable_rehash_alloc(ht, tbl, tbl->size * 2); |
b5e2c150 | 414 | else if (ht->p.automatic_shrinking && rht_shrink_below_30(ht, tbl)) |
da20420f HX |
415 | err = rhashtable_shrink(ht); |
416 | else if (tbl->nest) | |
417 | err = rhashtable_rehash_alloc(ht, tbl, tbl->size); | |
b824478b | 418 | |
408f13ef HX |
419 | if (!err || err == -EEXIST) { |
420 | int nerr; | |
421 | ||
422 | nerr = rhashtable_rehash_table(ht); | |
423 | err = err ?: nerr; | |
424 | } | |
b824478b | 425 | |
97defe1e | 426 | mutex_unlock(&ht->mutex); |
b824478b HX |
427 | |
428 | if (err) | |
429 | schedule_work(&ht->run_work); | |
97defe1e TG |
430 | } |
431 | ||
ca26893f HX |
432 | static int rhashtable_insert_rehash(struct rhashtable *ht, |
433 | struct bucket_table *tbl) | |
ccd57b1b HX |
434 | { |
435 | struct bucket_table *old_tbl; | |
436 | struct bucket_table *new_tbl; | |
ccd57b1b HX |
437 | unsigned int size; |
438 | int err; | |
439 | ||
440 | old_tbl = rht_dereference_rcu(ht->tbl, ht); | |
ccd57b1b HX |
441 | |
442 | size = tbl->size; | |
443 | ||
3cf92222 HX |
444 | err = -EBUSY; |
445 | ||
ccd57b1b HX |
446 | if (rht_grow_above_75(ht, tbl)) |
447 | size *= 2; | |
a87b9ebf TG |
448 | /* Do not schedule more than one rehash */ |
449 | else if (old_tbl != tbl) | |
3cf92222 HX |
450 | goto fail; |
451 | ||
452 | err = -ENOMEM; | |
ccd57b1b | 453 | |
93f976b5 | 454 | new_tbl = bucket_table_alloc(ht, size, GFP_ATOMIC | __GFP_NOWARN); |
3cf92222 HX |
455 | if (new_tbl == NULL) |
456 | goto fail; | |
ccd57b1b HX |
457 | |
458 | err = rhashtable_rehash_attach(ht, tbl, new_tbl); | |
459 | if (err) { | |
460 | bucket_table_free(new_tbl); | |
461 | if (err == -EEXIST) | |
462 | err = 0; | |
463 | } else | |
464 | schedule_work(&ht->run_work); | |
465 | ||
466 | return err; | |
3cf92222 HX |
467 | |
468 | fail: | |
469 | /* Do not fail the insert if someone else did a rehash. */ | |
c0690016 | 470 | if (likely(rcu_access_pointer(tbl->future_tbl))) |
3cf92222 HX |
471 | return 0; |
472 | ||
473 | /* Schedule async rehash to retry allocation in process context. */ | |
474 | if (err == -ENOMEM) | |
475 | schedule_work(&ht->run_work); | |
476 | ||
477 | return err; | |
ccd57b1b | 478 | } |
ccd57b1b | 479 | |
ca26893f HX |
480 | static void *rhashtable_lookup_one(struct rhashtable *ht, |
481 | struct bucket_table *tbl, unsigned int hash, | |
482 | const void *key, struct rhash_head *obj) | |
02fd97c3 | 483 | { |
ca26893f HX |
484 | struct rhashtable_compare_arg arg = { |
485 | .ht = ht, | |
486 | .key = key, | |
487 | }; | |
488 | struct rhash_head __rcu **pprev; | |
02fd97c3 | 489 | struct rhash_head *head; |
ca26893f | 490 | int elasticity; |
02fd97c3 | 491 | |
5f8ddeab | 492 | elasticity = RHT_ELASTICITY; |
da20420f HX |
493 | pprev = rht_bucket_var(tbl, hash); |
494 | rht_for_each_continue(head, *pprev, tbl, hash) { | |
ca26893f HX |
495 | struct rhlist_head *list; |
496 | struct rhlist_head *plist; | |
497 | ||
498 | elasticity--; | |
499 | if (!key || | |
500 | (ht->p.obj_cmpfn ? | |
501 | ht->p.obj_cmpfn(&arg, rht_obj(ht, head)) : | |
d3dcf8eb PB |
502 | rhashtable_compare(&arg, rht_obj(ht, head)))) { |
503 | pprev = &head->next; | |
ca26893f | 504 | continue; |
d3dcf8eb | 505 | } |
ca26893f HX |
506 | |
507 | if (!ht->rhlist) | |
508 | return rht_obj(ht, head); | |
509 | ||
510 | list = container_of(obj, struct rhlist_head, rhead); | |
511 | plist = container_of(head, struct rhlist_head, rhead); | |
512 | ||
513 | RCU_INIT_POINTER(list->next, plist); | |
514 | head = rht_dereference_bucket(head->next, tbl, hash); | |
515 | RCU_INIT_POINTER(list->rhead.next, head); | |
516 | rcu_assign_pointer(*pprev, obj); | |
517 | ||
518 | return NULL; | |
5ca8cc5b | 519 | } |
02fd97c3 | 520 | |
ca26893f HX |
521 | if (elasticity <= 0) |
522 | return ERR_PTR(-EAGAIN); | |
523 | ||
524 | return ERR_PTR(-ENOENT); | |
525 | } | |
526 | ||
527 | static struct bucket_table *rhashtable_insert_one(struct rhashtable *ht, | |
528 | struct bucket_table *tbl, | |
529 | unsigned int hash, | |
530 | struct rhash_head *obj, | |
531 | void *data) | |
532 | { | |
da20420f | 533 | struct rhash_head __rcu **pprev; |
ca26893f HX |
534 | struct bucket_table *new_tbl; |
535 | struct rhash_head *head; | |
536 | ||
537 | if (!IS_ERR_OR_NULL(data)) | |
538 | return ERR_PTR(-EEXIST); | |
07ee0722 | 539 | |
ca26893f HX |
540 | if (PTR_ERR(data) != -EAGAIN && PTR_ERR(data) != -ENOENT) |
541 | return ERR_CAST(data); | |
ccd57b1b | 542 | |
c0690016 | 543 | new_tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
ca26893f HX |
544 | if (new_tbl) |
545 | return new_tbl; | |
546 | ||
547 | if (PTR_ERR(data) != -ENOENT) | |
548 | return ERR_CAST(data); | |
549 | ||
550 | if (unlikely(rht_grow_above_max(ht, tbl))) | |
551 | return ERR_PTR(-E2BIG); | |
552 | ||
553 | if (unlikely(rht_grow_above_100(ht, tbl))) | |
554 | return ERR_PTR(-EAGAIN); | |
02fd97c3 | 555 | |
da20420f HX |
556 | pprev = rht_bucket_insert(ht, tbl, hash); |
557 | if (!pprev) | |
558 | return ERR_PTR(-ENOMEM); | |
559 | ||
560 | head = rht_dereference_bucket(*pprev, tbl, hash); | |
02fd97c3 HX |
561 | |
562 | RCU_INIT_POINTER(obj->next, head); | |
ca26893f HX |
563 | if (ht->rhlist) { |
564 | struct rhlist_head *list; | |
565 | ||
566 | list = container_of(obj, struct rhlist_head, rhead); | |
567 | RCU_INIT_POINTER(list->next, NULL); | |
568 | } | |
02fd97c3 | 569 | |
da20420f | 570 | rcu_assign_pointer(*pprev, obj); |
02fd97c3 HX |
571 | |
572 | atomic_inc(&ht->nelems); | |
ca26893f HX |
573 | if (rht_grow_above_75(ht, tbl)) |
574 | schedule_work(&ht->run_work); | |
02fd97c3 | 575 | |
ca26893f HX |
576 | return NULL; |
577 | } | |
02fd97c3 | 578 | |
ca26893f HX |
579 | static void *rhashtable_try_insert(struct rhashtable *ht, const void *key, |
580 | struct rhash_head *obj) | |
581 | { | |
582 | struct bucket_table *new_tbl; | |
583 | struct bucket_table *tbl; | |
584 | unsigned int hash; | |
585 | spinlock_t *lock; | |
586 | void *data; | |
587 | ||
588 | tbl = rcu_dereference(ht->tbl); | |
589 | ||
590 | /* All insertions must grab the oldest table containing | |
591 | * the hashed bucket that is yet to be rehashed. | |
592 | */ | |
593 | for (;;) { | |
594 | hash = rht_head_hashfn(ht, tbl, obj, ht->p); | |
595 | lock = rht_bucket_lock(tbl, hash); | |
596 | spin_lock_bh(lock); | |
597 | ||
598 | if (tbl->rehash <= hash) | |
599 | break; | |
600 | ||
601 | spin_unlock_bh(lock); | |
c0690016 | 602 | tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
ca26893f HX |
603 | } |
604 | ||
605 | data = rhashtable_lookup_one(ht, tbl, hash, key, obj); | |
606 | new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data); | |
607 | if (PTR_ERR(new_tbl) != -EEXIST) | |
608 | data = ERR_CAST(new_tbl); | |
609 | ||
610 | while (!IS_ERR_OR_NULL(new_tbl)) { | |
611 | tbl = new_tbl; | |
612 | hash = rht_head_hashfn(ht, tbl, obj, ht->p); | |
613 | spin_lock_nested(rht_bucket_lock(tbl, hash), | |
614 | SINGLE_DEPTH_NESTING); | |
615 | ||
616 | data = rhashtable_lookup_one(ht, tbl, hash, key, obj); | |
617 | new_tbl = rhashtable_insert_one(ht, tbl, hash, obj, data); | |
618 | if (PTR_ERR(new_tbl) != -EEXIST) | |
619 | data = ERR_CAST(new_tbl); | |
620 | ||
621 | spin_unlock(rht_bucket_lock(tbl, hash)); | |
622 | } | |
623 | ||
624 | spin_unlock_bh(lock); | |
625 | ||
626 | if (PTR_ERR(data) == -EAGAIN) | |
627 | data = ERR_PTR(rhashtable_insert_rehash(ht, tbl) ?: | |
628 | -EAGAIN); | |
629 | ||
630 | return data; | |
631 | } | |
632 | ||
633 | void *rhashtable_insert_slow(struct rhashtable *ht, const void *key, | |
634 | struct rhash_head *obj) | |
635 | { | |
636 | void *data; | |
637 | ||
638 | do { | |
639 | rcu_read_lock(); | |
640 | data = rhashtable_try_insert(ht, key, obj); | |
641 | rcu_read_unlock(); | |
642 | } while (PTR_ERR(data) == -EAGAIN); | |
643 | ||
644 | return data; | |
02fd97c3 HX |
645 | } |
646 | EXPORT_SYMBOL_GPL(rhashtable_insert_slow); | |
647 | ||
f2dba9c6 | 648 | /** |
246779dd | 649 | * rhashtable_walk_enter - Initialise an iterator |
f2dba9c6 HX |
650 | * @ht: Table to walk over |
651 | * @iter: Hash table Iterator | |
652 | * | |
653 | * This function prepares a hash table walk. | |
654 | * | |
655 | * Note that if you restart a walk after rhashtable_walk_stop you | |
656 | * may see the same object twice. Also, you may miss objects if | |
657 | * there are removals in between rhashtable_walk_stop and the next | |
658 | * call to rhashtable_walk_start. | |
659 | * | |
660 | * For a completely stable walk you should construct your own data | |
661 | * structure outside the hash table. | |
662 | * | |
82266e98 N |
663 | * This function may be called from any process context, including |
664 | * non-preemptable context, but cannot be called from softirq or | |
665 | * hardirq context. | |
f2dba9c6 | 666 | * |
246779dd | 667 | * You must call rhashtable_walk_exit after this function returns. |
f2dba9c6 | 668 | */ |
246779dd | 669 | void rhashtable_walk_enter(struct rhashtable *ht, struct rhashtable_iter *iter) |
f2dba9c6 HX |
670 | { |
671 | iter->ht = ht; | |
672 | iter->p = NULL; | |
673 | iter->slot = 0; | |
674 | iter->skip = 0; | |
2db54b47 | 675 | iter->end_of_table = 0; |
f2dba9c6 | 676 | |
c6ff5268 | 677 | spin_lock(&ht->lock); |
246779dd | 678 | iter->walker.tbl = |
179ccc0a | 679 | rcu_dereference_protected(ht->tbl, lockdep_is_held(&ht->lock)); |
246779dd | 680 | list_add(&iter->walker.list, &iter->walker.tbl->walkers); |
c6ff5268 | 681 | spin_unlock(&ht->lock); |
f2dba9c6 | 682 | } |
246779dd | 683 | EXPORT_SYMBOL_GPL(rhashtable_walk_enter); |
f2dba9c6 HX |
684 | |
685 | /** | |
686 | * rhashtable_walk_exit - Free an iterator | |
687 | * @iter: Hash table Iterator | |
688 | * | |
6c4128f6 | 689 | * This function frees resources allocated by rhashtable_walk_enter. |
f2dba9c6 HX |
690 | */ |
691 | void rhashtable_walk_exit(struct rhashtable_iter *iter) | |
692 | { | |
c6ff5268 | 693 | spin_lock(&iter->ht->lock); |
246779dd HX |
694 | if (iter->walker.tbl) |
695 | list_del(&iter->walker.list); | |
c6ff5268 | 696 | spin_unlock(&iter->ht->lock); |
f2dba9c6 HX |
697 | } |
698 | EXPORT_SYMBOL_GPL(rhashtable_walk_exit); | |
699 | ||
700 | /** | |
97a6ec4a | 701 | * rhashtable_walk_start_check - Start a hash table walk |
f2dba9c6 HX |
702 | * @iter: Hash table iterator |
703 | * | |
0647169c AG |
704 | * Start a hash table walk at the current iterator position. Note that we take |
705 | * the RCU lock in all cases including when we return an error. So you must | |
706 | * always call rhashtable_walk_stop to clean up. | |
f2dba9c6 HX |
707 | * |
708 | * Returns zero if successful. | |
709 | * | |
710 | * Returns -EAGAIN if resize event occured. Note that the iterator | |
711 | * will rewind back to the beginning and you may use it immediately | |
712 | * by calling rhashtable_walk_next. | |
97a6ec4a TH |
713 | * |
714 | * rhashtable_walk_start is defined as an inline variant that returns | |
715 | * void. This is preferred in cases where the caller would ignore | |
716 | * resize events and always continue. | |
f2dba9c6 | 717 | */ |
97a6ec4a | 718 | int rhashtable_walk_start_check(struct rhashtable_iter *iter) |
db4374f4 | 719 | __acquires(RCU) |
f2dba9c6 | 720 | { |
eddee5ba | 721 | struct rhashtable *ht = iter->ht; |
5d240a89 | 722 | bool rhlist = ht->rhlist; |
eddee5ba | 723 | |
c6ff5268 | 724 | rcu_read_lock(); |
eddee5ba | 725 | |
c6ff5268 | 726 | spin_lock(&ht->lock); |
246779dd HX |
727 | if (iter->walker.tbl) |
728 | list_del(&iter->walker.list); | |
c6ff5268 | 729 | spin_unlock(&ht->lock); |
eddee5ba | 730 | |
5d240a89 N |
731 | if (iter->end_of_table) |
732 | return 0; | |
733 | if (!iter->walker.tbl) { | |
246779dd | 734 | iter->walker.tbl = rht_dereference_rcu(ht->tbl, ht); |
b41cc04b N |
735 | iter->slot = 0; |
736 | iter->skip = 0; | |
f2dba9c6 HX |
737 | return -EAGAIN; |
738 | } | |
739 | ||
5d240a89 N |
740 | if (iter->p && !rhlist) { |
741 | /* | |
742 | * We need to validate that 'p' is still in the table, and | |
743 | * if so, update 'skip' | |
744 | */ | |
745 | struct rhash_head *p; | |
746 | int skip = 0; | |
747 | rht_for_each_rcu(p, iter->walker.tbl, iter->slot) { | |
748 | skip++; | |
749 | if (p == iter->p) { | |
750 | iter->skip = skip; | |
751 | goto found; | |
752 | } | |
753 | } | |
754 | iter->p = NULL; | |
755 | } else if (iter->p && rhlist) { | |
756 | /* Need to validate that 'list' is still in the table, and | |
757 | * if so, update 'skip' and 'p'. | |
758 | */ | |
759 | struct rhash_head *p; | |
760 | struct rhlist_head *list; | |
761 | int skip = 0; | |
762 | rht_for_each_rcu(p, iter->walker.tbl, iter->slot) { | |
763 | for (list = container_of(p, struct rhlist_head, rhead); | |
764 | list; | |
765 | list = rcu_dereference(list->next)) { | |
766 | skip++; | |
767 | if (list == iter->list) { | |
768 | iter->p = p; | |
c643ecf3 | 769 | iter->skip = skip; |
5d240a89 N |
770 | goto found; |
771 | } | |
772 | } | |
773 | } | |
774 | iter->p = NULL; | |
775 | } | |
776 | found: | |
f2dba9c6 HX |
777 | return 0; |
778 | } | |
97a6ec4a | 779 | EXPORT_SYMBOL_GPL(rhashtable_walk_start_check); |
f2dba9c6 HX |
780 | |
781 | /** | |
2db54b47 TH |
782 | * __rhashtable_walk_find_next - Find the next element in a table (or the first |
783 | * one in case of a new walk). | |
784 | * | |
f2dba9c6 HX |
785 | * @iter: Hash table iterator |
786 | * | |
2db54b47 | 787 | * Returns the found object or NULL when the end of the table is reached. |
f2dba9c6 | 788 | * |
2db54b47 | 789 | * Returns -EAGAIN if resize event occurred. |
f2dba9c6 | 790 | */ |
2db54b47 | 791 | static void *__rhashtable_walk_find_next(struct rhashtable_iter *iter) |
f2dba9c6 | 792 | { |
246779dd | 793 | struct bucket_table *tbl = iter->walker.tbl; |
ca26893f | 794 | struct rhlist_head *list = iter->list; |
f2dba9c6 HX |
795 | struct rhashtable *ht = iter->ht; |
796 | struct rhash_head *p = iter->p; | |
ca26893f | 797 | bool rhlist = ht->rhlist; |
f2dba9c6 | 798 | |
2db54b47 TH |
799 | if (!tbl) |
800 | return NULL; | |
f2dba9c6 HX |
801 | |
802 | for (; iter->slot < tbl->size; iter->slot++) { | |
803 | int skip = iter->skip; | |
804 | ||
805 | rht_for_each_rcu(p, tbl, iter->slot) { | |
ca26893f HX |
806 | if (rhlist) { |
807 | list = container_of(p, struct rhlist_head, | |
808 | rhead); | |
809 | do { | |
810 | if (!skip) | |
811 | goto next; | |
812 | skip--; | |
813 | list = rcu_dereference(list->next); | |
814 | } while (list); | |
815 | ||
816 | continue; | |
817 | } | |
f2dba9c6 HX |
818 | if (!skip) |
819 | break; | |
820 | skip--; | |
821 | } | |
822 | ||
823 | next: | |
824 | if (!rht_is_a_nulls(p)) { | |
825 | iter->skip++; | |
826 | iter->p = p; | |
ca26893f HX |
827 | iter->list = list; |
828 | return rht_obj(ht, rhlist ? &list->rhead : p); | |
f2dba9c6 HX |
829 | } |
830 | ||
831 | iter->skip = 0; | |
832 | } | |
833 | ||
142b942a PS |
834 | iter->p = NULL; |
835 | ||
d88252f9 HX |
836 | /* Ensure we see any new tables. */ |
837 | smp_rmb(); | |
838 | ||
246779dd HX |
839 | iter->walker.tbl = rht_dereference_rcu(tbl->future_tbl, ht); |
840 | if (iter->walker.tbl) { | |
f2dba9c6 HX |
841 | iter->slot = 0; |
842 | iter->skip = 0; | |
f2dba9c6 | 843 | return ERR_PTR(-EAGAIN); |
2db54b47 TH |
844 | } else { |
845 | iter->end_of_table = true; | |
f2dba9c6 HX |
846 | } |
847 | ||
c936a79f | 848 | return NULL; |
f2dba9c6 | 849 | } |
2db54b47 TH |
850 | |
851 | /** | |
852 | * rhashtable_walk_next - Return the next object and advance the iterator | |
853 | * @iter: Hash table iterator | |
854 | * | |
855 | * Note that you must call rhashtable_walk_stop when you are finished | |
856 | * with the walk. | |
857 | * | |
858 | * Returns the next object or NULL when the end of the table is reached. | |
859 | * | |
860 | * Returns -EAGAIN if resize event occurred. Note that the iterator | |
861 | * will rewind back to the beginning and you may continue to use it. | |
862 | */ | |
863 | void *rhashtable_walk_next(struct rhashtable_iter *iter) | |
864 | { | |
865 | struct rhlist_head *list = iter->list; | |
866 | struct rhashtable *ht = iter->ht; | |
867 | struct rhash_head *p = iter->p; | |
868 | bool rhlist = ht->rhlist; | |
869 | ||
870 | if (p) { | |
871 | if (!rhlist || !(list = rcu_dereference(list->next))) { | |
872 | p = rcu_dereference(p->next); | |
873 | list = container_of(p, struct rhlist_head, rhead); | |
874 | } | |
875 | if (!rht_is_a_nulls(p)) { | |
876 | iter->skip++; | |
877 | iter->p = p; | |
878 | iter->list = list; | |
879 | return rht_obj(ht, rhlist ? &list->rhead : p); | |
880 | } | |
881 | ||
882 | /* At the end of this slot, switch to next one and then find | |
883 | * next entry from that point. | |
884 | */ | |
885 | iter->skip = 0; | |
886 | iter->slot++; | |
887 | } | |
888 | ||
889 | return __rhashtable_walk_find_next(iter); | |
890 | } | |
f2dba9c6 HX |
891 | EXPORT_SYMBOL_GPL(rhashtable_walk_next); |
892 | ||
2db54b47 TH |
893 | /** |
894 | * rhashtable_walk_peek - Return the next object but don't advance the iterator | |
895 | * @iter: Hash table iterator | |
896 | * | |
897 | * Returns the next object or NULL when the end of the table is reached. | |
898 | * | |
899 | * Returns -EAGAIN if resize event occurred. Note that the iterator | |
900 | * will rewind back to the beginning and you may continue to use it. | |
901 | */ | |
902 | void *rhashtable_walk_peek(struct rhashtable_iter *iter) | |
903 | { | |
904 | struct rhlist_head *list = iter->list; | |
905 | struct rhashtable *ht = iter->ht; | |
906 | struct rhash_head *p = iter->p; | |
907 | ||
908 | if (p) | |
909 | return rht_obj(ht, ht->rhlist ? &list->rhead : p); | |
910 | ||
911 | /* No object found in current iter, find next one in the table. */ | |
912 | ||
913 | if (iter->skip) { | |
914 | /* A nonzero skip value points to the next entry in the table | |
915 | * beyond that last one that was found. Decrement skip so | |
916 | * we find the current value. __rhashtable_walk_find_next | |
917 | * will restore the original value of skip assuming that | |
918 | * the table hasn't changed. | |
919 | */ | |
920 | iter->skip--; | |
921 | } | |
922 | ||
923 | return __rhashtable_walk_find_next(iter); | |
924 | } | |
925 | EXPORT_SYMBOL_GPL(rhashtable_walk_peek); | |
926 | ||
f2dba9c6 HX |
927 | /** |
928 | * rhashtable_walk_stop - Finish a hash table walk | |
929 | * @iter: Hash table iterator | |
930 | * | |
0647169c AG |
931 | * Finish a hash table walk. Does not reset the iterator to the start of the |
932 | * hash table. | |
f2dba9c6 HX |
933 | */ |
934 | void rhashtable_walk_stop(struct rhashtable_iter *iter) | |
db4374f4 | 935 | __releases(RCU) |
f2dba9c6 | 936 | { |
eddee5ba | 937 | struct rhashtable *ht; |
246779dd | 938 | struct bucket_table *tbl = iter->walker.tbl; |
eddee5ba | 939 | |
eddee5ba | 940 | if (!tbl) |
963ecbd4 | 941 | goto out; |
eddee5ba HX |
942 | |
943 | ht = iter->ht; | |
944 | ||
ba7c95ea | 945 | spin_lock(&ht->lock); |
c4db8848 | 946 | if (tbl->rehash < tbl->size) |
246779dd | 947 | list_add(&iter->walker.list, &tbl->walkers); |
eddee5ba | 948 | else |
246779dd | 949 | iter->walker.tbl = NULL; |
ba7c95ea | 950 | spin_unlock(&ht->lock); |
eddee5ba | 951 | |
963ecbd4 HX |
952 | out: |
953 | rcu_read_unlock(); | |
f2dba9c6 HX |
954 | } |
955 | EXPORT_SYMBOL_GPL(rhashtable_walk_stop); | |
956 | ||
488fb86e | 957 | static size_t rounded_hashtable_size(const struct rhashtable_params *params) |
7e1e7763 | 958 | { |
107d01f5 DB |
959 | size_t retsize; |
960 | ||
961 | if (params->nelem_hint) | |
962 | retsize = max(roundup_pow_of_two(params->nelem_hint * 4 / 3), | |
963 | (unsigned long)params->min_size); | |
964 | else | |
965 | retsize = max(HASH_DEFAULT_SIZE, | |
966 | (unsigned long)params->min_size); | |
967 | ||
968 | return retsize; | |
7e1e7763 TG |
969 | } |
970 | ||
31ccde2d HX |
971 | static u32 rhashtable_jhash2(const void *key, u32 length, u32 seed) |
972 | { | |
973 | return jhash2(key, length, seed); | |
974 | } | |
975 | ||
7e1e7763 TG |
976 | /** |
977 | * rhashtable_init - initialize a new hash table | |
978 | * @ht: hash table to be initialized | |
979 | * @params: configuration parameters | |
980 | * | |
981 | * Initializes a new hash table based on the provided configuration | |
982 | * parameters. A table can be configured either with a variable or | |
983 | * fixed length key: | |
984 | * | |
985 | * Configuration Example 1: Fixed length keys | |
986 | * struct test_obj { | |
987 | * int key; | |
988 | * void * my_member; | |
989 | * struct rhash_head node; | |
990 | * }; | |
991 | * | |
992 | * struct rhashtable_params params = { | |
993 | * .head_offset = offsetof(struct test_obj, node), | |
994 | * .key_offset = offsetof(struct test_obj, key), | |
995 | * .key_len = sizeof(int), | |
87545899 | 996 | * .hashfn = jhash, |
7e1e7763 TG |
997 | * }; |
998 | * | |
999 | * Configuration Example 2: Variable length keys | |
1000 | * struct test_obj { | |
1001 | * [...] | |
1002 | * struct rhash_head node; | |
1003 | * }; | |
1004 | * | |
49f7b33e | 1005 | * u32 my_hash_fn(const void *data, u32 len, u32 seed) |
7e1e7763 TG |
1006 | * { |
1007 | * struct test_obj *obj = data; | |
1008 | * | |
1009 | * return [... hash ...]; | |
1010 | * } | |
1011 | * | |
1012 | * struct rhashtable_params params = { | |
1013 | * .head_offset = offsetof(struct test_obj, node), | |
87545899 | 1014 | * .hashfn = jhash, |
7e1e7763 | 1015 | * .obj_hashfn = my_hash_fn, |
7e1e7763 TG |
1016 | * }; |
1017 | */ | |
488fb86e HX |
1018 | int rhashtable_init(struct rhashtable *ht, |
1019 | const struct rhashtable_params *params) | |
7e1e7763 TG |
1020 | { |
1021 | struct bucket_table *tbl; | |
1022 | size_t size; | |
1023 | ||
31ccde2d | 1024 | if ((!params->key_len && !params->obj_hashfn) || |
02fd97c3 | 1025 | (params->obj_hashfn && !params->obj_cmpfn)) |
7e1e7763 TG |
1026 | return -EINVAL; |
1027 | ||
97defe1e TG |
1028 | memset(ht, 0, sizeof(*ht)); |
1029 | mutex_init(&ht->mutex); | |
ba7c95ea | 1030 | spin_lock_init(&ht->lock); |
97defe1e TG |
1031 | memcpy(&ht->p, params, sizeof(*params)); |
1032 | ||
a998f712 TG |
1033 | if (params->min_size) |
1034 | ht->p.min_size = roundup_pow_of_two(params->min_size); | |
1035 | ||
6d684e54 HX |
1036 | /* Cap total entries at 2^31 to avoid nelems overflow. */ |
1037 | ht->max_elems = 1u << 31; | |
2d2ab658 HX |
1038 | |
1039 | if (params->max_size) { | |
1040 | ht->p.max_size = rounddown_pow_of_two(params->max_size); | |
1041 | if (ht->p.max_size < ht->max_elems / 2) | |
1042 | ht->max_elems = ht->p.max_size * 2; | |
1043 | } | |
6d684e54 | 1044 | |
48e75b43 | 1045 | ht->p.min_size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE); |
a998f712 | 1046 | |
107d01f5 | 1047 | size = rounded_hashtable_size(&ht->p); |
3a324606 | 1048 | |
97defe1e TG |
1049 | if (params->locks_mul) |
1050 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); | |
1051 | else | |
1052 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; | |
1053 | ||
31ccde2d HX |
1054 | ht->key_len = ht->p.key_len; |
1055 | if (!params->hashfn) { | |
1056 | ht->p.hashfn = jhash; | |
1057 | ||
1058 | if (!(ht->key_len & (sizeof(u32) - 1))) { | |
1059 | ht->key_len /= sizeof(u32); | |
1060 | ht->p.hashfn = rhashtable_jhash2; | |
1061 | } | |
1062 | } | |
1063 | ||
2d22ecf6 DB |
1064 | /* |
1065 | * This is api initialization and thus we need to guarantee the | |
1066 | * initial rhashtable allocation. Upon failure, retry with the | |
1067 | * smallest possible size with __GFP_NOFAIL semantics. | |
1068 | */ | |
b9ecfdaa | 1069 | tbl = bucket_table_alloc(ht, size, GFP_KERNEL); |
2d22ecf6 DB |
1070 | if (unlikely(tbl == NULL)) { |
1071 | size = max_t(u16, ht->p.min_size, HASH_MIN_SIZE); | |
1072 | tbl = bucket_table_alloc(ht, size, GFP_KERNEL | __GFP_NOFAIL); | |
1073 | } | |
7e1e7763 | 1074 | |
545a148e | 1075 | atomic_set(&ht->nelems, 0); |
a5b6846f | 1076 | |
7e1e7763 TG |
1077 | RCU_INIT_POINTER(ht->tbl, tbl); |
1078 | ||
4c4b52d9 | 1079 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
97defe1e | 1080 | |
7e1e7763 TG |
1081 | return 0; |
1082 | } | |
1083 | EXPORT_SYMBOL_GPL(rhashtable_init); | |
1084 | ||
ca26893f HX |
1085 | /** |
1086 | * rhltable_init - initialize a new hash list table | |
1087 | * @hlt: hash list table to be initialized | |
1088 | * @params: configuration parameters | |
1089 | * | |
1090 | * Initializes a new hash list table. | |
1091 | * | |
1092 | * See documentation for rhashtable_init. | |
1093 | */ | |
1094 | int rhltable_init(struct rhltable *hlt, const struct rhashtable_params *params) | |
1095 | { | |
1096 | int err; | |
1097 | ||
ca26893f HX |
1098 | err = rhashtable_init(&hlt->ht, params); |
1099 | hlt->ht.rhlist = true; | |
1100 | return err; | |
1101 | } | |
1102 | EXPORT_SYMBOL_GPL(rhltable_init); | |
1103 | ||
1104 | static void rhashtable_free_one(struct rhashtable *ht, struct rhash_head *obj, | |
1105 | void (*free_fn)(void *ptr, void *arg), | |
1106 | void *arg) | |
1107 | { | |
1108 | struct rhlist_head *list; | |
1109 | ||
1110 | if (!ht->rhlist) { | |
1111 | free_fn(rht_obj(ht, obj), arg); | |
1112 | return; | |
1113 | } | |
1114 | ||
1115 | list = container_of(obj, struct rhlist_head, rhead); | |
1116 | do { | |
1117 | obj = &list->rhead; | |
1118 | list = rht_dereference(list->next, ht); | |
1119 | free_fn(rht_obj(ht, obj), arg); | |
1120 | } while (list); | |
1121 | } | |
1122 | ||
7e1e7763 | 1123 | /** |
6b6f302c | 1124 | * rhashtable_free_and_destroy - free elements and destroy hash table |
7e1e7763 | 1125 | * @ht: the hash table to destroy |
6b6f302c TG |
1126 | * @free_fn: callback to release resources of element |
1127 | * @arg: pointer passed to free_fn | |
7e1e7763 | 1128 | * |
6b6f302c TG |
1129 | * Stops an eventual async resize. If defined, invokes free_fn for each |
1130 | * element to releasal resources. Please note that RCU protected | |
1131 | * readers may still be accessing the elements. Releasing of resources | |
1132 | * must occur in a compatible manner. Then frees the bucket array. | |
1133 | * | |
1134 | * This function will eventually sleep to wait for an async resize | |
1135 | * to complete. The caller is responsible that no further write operations | |
1136 | * occurs in parallel. | |
7e1e7763 | 1137 | */ |
6b6f302c TG |
1138 | void rhashtable_free_and_destroy(struct rhashtable *ht, |
1139 | void (*free_fn)(void *ptr, void *arg), | |
1140 | void *arg) | |
7e1e7763 | 1141 | { |
0026129c | 1142 | struct bucket_table *tbl, *next_tbl; |
6b6f302c | 1143 | unsigned int i; |
97defe1e | 1144 | |
4c4b52d9 | 1145 | cancel_work_sync(&ht->run_work); |
97defe1e | 1146 | |
57699a40 | 1147 | mutex_lock(&ht->mutex); |
6b6f302c | 1148 | tbl = rht_dereference(ht->tbl, ht); |
0026129c | 1149 | restart: |
6b6f302c TG |
1150 | if (free_fn) { |
1151 | for (i = 0; i < tbl->size; i++) { | |
1152 | struct rhash_head *pos, *next; | |
1153 | ||
ae6da1f5 | 1154 | cond_resched(); |
da20420f | 1155 | for (pos = rht_dereference(*rht_bucket(tbl, i), ht), |
6b6f302c TG |
1156 | next = !rht_is_a_nulls(pos) ? |
1157 | rht_dereference(pos->next, ht) : NULL; | |
1158 | !rht_is_a_nulls(pos); | |
1159 | pos = next, | |
1160 | next = !rht_is_a_nulls(pos) ? | |
1161 | rht_dereference(pos->next, ht) : NULL) | |
ca26893f | 1162 | rhashtable_free_one(ht, pos, free_fn, arg); |
6b6f302c TG |
1163 | } |
1164 | } | |
1165 | ||
0026129c | 1166 | next_tbl = rht_dereference(tbl->future_tbl, ht); |
6b6f302c | 1167 | bucket_table_free(tbl); |
0026129c TY |
1168 | if (next_tbl) { |
1169 | tbl = next_tbl; | |
1170 | goto restart; | |
1171 | } | |
97defe1e | 1172 | mutex_unlock(&ht->mutex); |
7e1e7763 | 1173 | } |
6b6f302c TG |
1174 | EXPORT_SYMBOL_GPL(rhashtable_free_and_destroy); |
1175 | ||
1176 | void rhashtable_destroy(struct rhashtable *ht) | |
1177 | { | |
1178 | return rhashtable_free_and_destroy(ht, NULL, NULL); | |
1179 | } | |
7e1e7763 | 1180 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |
da20420f HX |
1181 | |
1182 | struct rhash_head __rcu **rht_bucket_nested(const struct bucket_table *tbl, | |
1183 | unsigned int hash) | |
1184 | { | |
1185 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); | |
82208d0d | 1186 | static struct rhash_head __rcu *rhnull; |
da20420f HX |
1187 | unsigned int index = hash & ((1 << tbl->nest) - 1); |
1188 | unsigned int size = tbl->size >> tbl->nest; | |
1189 | unsigned int subhash = hash; | |
1190 | union nested_table *ntbl; | |
1191 | ||
1192 | ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); | |
c4d2603d | 1193 | ntbl = rht_dereference_bucket_rcu(ntbl[index].table, tbl, hash); |
da20420f HX |
1194 | subhash >>= tbl->nest; |
1195 | ||
1196 | while (ntbl && size > (1 << shift)) { | |
1197 | index = subhash & ((1 << shift) - 1); | |
c4d2603d HX |
1198 | ntbl = rht_dereference_bucket_rcu(ntbl[index].table, |
1199 | tbl, hash); | |
da20420f HX |
1200 | size >>= shift; |
1201 | subhash >>= shift; | |
1202 | } | |
1203 | ||
82208d0d N |
1204 | if (!ntbl) { |
1205 | if (!rhnull) | |
1206 | INIT_RHT_NULLS_HEAD(rhnull); | |
da20420f | 1207 | return &rhnull; |
82208d0d | 1208 | } |
da20420f HX |
1209 | |
1210 | return &ntbl[subhash].bucket; | |
1211 | ||
1212 | } | |
1213 | EXPORT_SYMBOL_GPL(rht_bucket_nested); | |
1214 | ||
1215 | struct rhash_head __rcu **rht_bucket_nested_insert(struct rhashtable *ht, | |
1216 | struct bucket_table *tbl, | |
1217 | unsigned int hash) | |
1218 | { | |
1219 | const unsigned int shift = PAGE_SHIFT - ilog2(sizeof(void *)); | |
1220 | unsigned int index = hash & ((1 << tbl->nest) - 1); | |
1221 | unsigned int size = tbl->size >> tbl->nest; | |
1222 | union nested_table *ntbl; | |
da20420f HX |
1223 | |
1224 | ntbl = (union nested_table *)rcu_dereference_raw(tbl->buckets[0]); | |
1225 | hash >>= tbl->nest; | |
da20420f | 1226 | ntbl = nested_table_alloc(ht, &ntbl[index].table, |
5af68ef7 | 1227 | size <= (1 << shift)); |
da20420f HX |
1228 | |
1229 | while (ntbl && size > (1 << shift)) { | |
1230 | index = hash & ((1 << shift) - 1); | |
1231 | size >>= shift; | |
1232 | hash >>= shift; | |
da20420f | 1233 | ntbl = nested_table_alloc(ht, &ntbl[index].table, |
5af68ef7 | 1234 | size <= (1 << shift)); |
da20420f HX |
1235 | } |
1236 | ||
1237 | if (!ntbl) | |
1238 | return NULL; | |
1239 | ||
1240 | return &ntbl[hash].bucket; | |
1241 | ||
1242 | } | |
1243 | EXPORT_SYMBOL_GPL(rht_bucket_nested_insert); |