]>
Commit | Line | Data |
---|---|---|
7e1e7763 TG |
1 | /* |
2 | * Resizable, Scalable, Concurrent Hash Table | |
3 | * | |
4 | * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch> | |
5 | * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net> | |
6 | * | |
7 | * Based on the following paper: | |
8 | * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf | |
9 | * | |
10 | * Code partially derived from nft_hash | |
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> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/vmalloc.h> | |
22 | #include <linux/mm.h> | |
87545899 | 23 | #include <linux/jhash.h> |
7e1e7763 TG |
24 | #include <linux/random.h> |
25 | #include <linux/rhashtable.h> | |
7e1e7763 TG |
26 | |
27 | #define HASH_DEFAULT_SIZE 64UL | |
28 | #define HASH_MIN_SIZE 4UL | |
97defe1e TG |
29 | #define BUCKET_LOCKS_PER_CPU 128UL |
30 | ||
f89bd6f8 TG |
31 | /* Base bits plus 1 bit for nulls marker */ |
32 | #define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1) | |
33 | ||
97defe1e TG |
34 | enum { |
35 | RHT_LOCK_NORMAL, | |
36 | RHT_LOCK_NESTED, | |
37 | RHT_LOCK_NESTED2, | |
38 | }; | |
39 | ||
40 | /* The bucket lock is selected based on the hash and protects mutations | |
41 | * on a group of hash buckets. | |
42 | * | |
43 | * IMPORTANT: When holding the bucket lock of both the old and new table | |
44 | * during expansions and shrinking, the old bucket lock must always be | |
45 | * acquired first. | |
46 | */ | |
47 | static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash) | |
48 | { | |
49 | return &tbl->locks[hash & tbl->locks_mask]; | |
50 | } | |
7e1e7763 TG |
51 | |
52 | #define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT)) | |
97defe1e TG |
53 | #define ASSERT_BUCKET_LOCK(TBL, HASH) \ |
54 | BUG_ON(!lockdep_rht_bucket_is_held(TBL, HASH)) | |
7e1e7763 TG |
55 | |
56 | #ifdef CONFIG_PROVE_LOCKING | |
97defe1e | 57 | int lockdep_rht_mutex_is_held(struct rhashtable *ht) |
7e1e7763 | 58 | { |
97defe1e | 59 | return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1; |
7e1e7763 TG |
60 | } |
61 | EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held); | |
88d6ed15 TG |
62 | |
63 | int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash) | |
64 | { | |
97defe1e TG |
65 | spinlock_t *lock = bucket_lock(tbl, hash); |
66 | ||
67 | return (debug_locks) ? lockdep_is_held(lock) : 1; | |
88d6ed15 TG |
68 | } |
69 | EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held); | |
7e1e7763 TG |
70 | #endif |
71 | ||
c91eee56 | 72 | static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he) |
7e1e7763 TG |
73 | { |
74 | return (void *) he - ht->p.head_offset; | |
75 | } | |
7e1e7763 | 76 | |
8d24c0b4 | 77 | static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash) |
7e1e7763 | 78 | { |
8d24c0b4 | 79 | return hash & (tbl->size - 1); |
7e1e7763 | 80 | } |
7e1e7763 | 81 | |
8d24c0b4 | 82 | static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr) |
7e1e7763 | 83 | { |
8d24c0b4 | 84 | u32 hash; |
7e1e7763 | 85 | |
8d24c0b4 TG |
86 | if (unlikely(!ht->p.key_len)) |
87 | hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd); | |
88 | else | |
89 | hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len, | |
90 | ht->p.hash_rnd); | |
7e1e7763 | 91 | |
f89bd6f8 | 92 | return hash >> HASH_RESERVED_SPACE; |
7e1e7763 TG |
93 | } |
94 | ||
97defe1e | 95 | static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len) |
7e1e7763 TG |
96 | { |
97 | struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); | |
8d24c0b4 TG |
98 | u32 hash; |
99 | ||
100 | hash = ht->p.hashfn(key, len, ht->p.hash_rnd); | |
f89bd6f8 | 101 | hash >>= HASH_RESERVED_SPACE; |
7e1e7763 | 102 | |
8d24c0b4 | 103 | return rht_bucket_index(tbl, hash); |
7e1e7763 | 104 | } |
7e1e7763 TG |
105 | |
106 | static u32 head_hashfn(const struct rhashtable *ht, | |
8d24c0b4 TG |
107 | const struct bucket_table *tbl, |
108 | const struct rhash_head *he) | |
7e1e7763 | 109 | { |
8d24c0b4 | 110 | return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he))); |
7e1e7763 TG |
111 | } |
112 | ||
b8e1943e TG |
113 | static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n) |
114 | { | |
115 | struct rhash_head __rcu **pprev; | |
116 | ||
117 | for (pprev = &tbl->buckets[n]; | |
f89bd6f8 | 118 | !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n)); |
b8e1943e TG |
119 | pprev = &rht_dereference_bucket(*pprev, tbl, n)->next) |
120 | ; | |
121 | ||
122 | return pprev; | |
123 | } | |
124 | ||
97defe1e TG |
125 | static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl) |
126 | { | |
127 | unsigned int i, size; | |
128 | #if defined(CONFIG_PROVE_LOCKING) | |
129 | unsigned int nr_pcpus = 2; | |
130 | #else | |
131 | unsigned int nr_pcpus = num_possible_cpus(); | |
132 | #endif | |
133 | ||
134 | nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL); | |
135 | size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul); | |
136 | ||
137 | /* Never allocate more than one lock per bucket */ | |
138 | size = min_t(unsigned int, size, tbl->size); | |
139 | ||
140 | if (sizeof(spinlock_t) != 0) { | |
141 | #ifdef CONFIG_NUMA | |
142 | if (size * sizeof(spinlock_t) > PAGE_SIZE) | |
143 | tbl->locks = vmalloc(size * sizeof(spinlock_t)); | |
144 | else | |
145 | #endif | |
146 | tbl->locks = kmalloc_array(size, sizeof(spinlock_t), | |
147 | GFP_KERNEL); | |
148 | if (!tbl->locks) | |
149 | return -ENOMEM; | |
150 | for (i = 0; i < size; i++) | |
151 | spin_lock_init(&tbl->locks[i]); | |
152 | } | |
153 | tbl->locks_mask = size - 1; | |
154 | ||
155 | return 0; | |
156 | } | |
157 | ||
158 | static void bucket_table_free(const struct bucket_table *tbl) | |
159 | { | |
160 | if (tbl) | |
161 | kvfree(tbl->locks); | |
162 | ||
163 | kvfree(tbl); | |
164 | } | |
165 | ||
166 | static struct bucket_table *bucket_table_alloc(struct rhashtable *ht, | |
167 | size_t nbuckets) | |
7e1e7763 TG |
168 | { |
169 | struct bucket_table *tbl; | |
170 | size_t size; | |
f89bd6f8 | 171 | int i; |
7e1e7763 TG |
172 | |
173 | size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]); | |
6eba8224 | 174 | tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); |
7e1e7763 TG |
175 | if (tbl == NULL) |
176 | tbl = vzalloc(size); | |
177 | ||
178 | if (tbl == NULL) | |
179 | return NULL; | |
180 | ||
181 | tbl->size = nbuckets; | |
182 | ||
97defe1e TG |
183 | if (alloc_bucket_locks(ht, tbl) < 0) { |
184 | bucket_table_free(tbl); | |
185 | return NULL; | |
186 | } | |
7e1e7763 | 187 | |
f89bd6f8 TG |
188 | for (i = 0; i < nbuckets; i++) |
189 | INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i); | |
190 | ||
97defe1e | 191 | return tbl; |
7e1e7763 TG |
192 | } |
193 | ||
194 | /** | |
195 | * rht_grow_above_75 - returns true if nelems > 0.75 * table-size | |
196 | * @ht: hash table | |
197 | * @new_size: new table size | |
198 | */ | |
199 | bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size) | |
200 | { | |
201 | /* Expand table when exceeding 75% load */ | |
c0c09bfd YX |
202 | return atomic_read(&ht->nelems) > (new_size / 4 * 3) && |
203 | (ht->p.max_shift && atomic_read(&ht->shift) < ht->p.max_shift); | |
7e1e7763 TG |
204 | } |
205 | EXPORT_SYMBOL_GPL(rht_grow_above_75); | |
206 | ||
207 | /** | |
208 | * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size | |
209 | * @ht: hash table | |
210 | * @new_size: new table size | |
211 | */ | |
212 | bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size) | |
213 | { | |
214 | /* Shrink table beneath 30% load */ | |
c0c09bfd YX |
215 | return atomic_read(&ht->nelems) < (new_size * 3 / 10) && |
216 | (atomic_read(&ht->shift) > ht->p.min_shift); | |
7e1e7763 TG |
217 | } |
218 | EXPORT_SYMBOL_GPL(rht_shrink_below_30); | |
219 | ||
220 | static void hashtable_chain_unzip(const struct rhashtable *ht, | |
221 | const struct bucket_table *new_tbl, | |
97defe1e TG |
222 | struct bucket_table *old_tbl, |
223 | size_t old_hash) | |
7e1e7763 TG |
224 | { |
225 | struct rhash_head *he, *p, *next; | |
97defe1e TG |
226 | spinlock_t *new_bucket_lock, *new_bucket_lock2 = NULL; |
227 | unsigned int new_hash, new_hash2; | |
228 | ||
229 | ASSERT_BUCKET_LOCK(old_tbl, old_hash); | |
7e1e7763 TG |
230 | |
231 | /* Old bucket empty, no work needed. */ | |
97defe1e TG |
232 | p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl, |
233 | old_hash); | |
f89bd6f8 | 234 | if (rht_is_a_nulls(p)) |
7e1e7763 TG |
235 | return; |
236 | ||
97defe1e TG |
237 | new_hash = new_hash2 = head_hashfn(ht, new_tbl, p); |
238 | new_bucket_lock = bucket_lock(new_tbl, new_hash); | |
239 | ||
7e1e7763 TG |
240 | /* Advance the old bucket pointer one or more times until it |
241 | * reaches a node that doesn't hash to the same bucket as the | |
242 | * previous node p. Call the previous node p; | |
243 | */ | |
97defe1e TG |
244 | rht_for_each_continue(he, p->next, old_tbl, old_hash) { |
245 | new_hash2 = head_hashfn(ht, new_tbl, he); | |
246 | if (new_hash != new_hash2) | |
7e1e7763 TG |
247 | break; |
248 | p = he; | |
249 | } | |
97defe1e TG |
250 | rcu_assign_pointer(old_tbl->buckets[old_hash], p->next); |
251 | ||
252 | spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); | |
253 | ||
254 | /* If we have encountered an entry that maps to a different bucket in | |
255 | * the new table, lock down that bucket as well as we might cut off | |
256 | * the end of the chain. | |
257 | */ | |
258 | new_bucket_lock2 = bucket_lock(new_tbl, new_hash); | |
259 | if (new_bucket_lock != new_bucket_lock2) | |
260 | spin_lock_bh_nested(new_bucket_lock2, RHT_LOCK_NESTED2); | |
7e1e7763 TG |
261 | |
262 | /* Find the subsequent node which does hash to the same | |
263 | * bucket as node P, or NULL if no such node exists. | |
264 | */ | |
f89bd6f8 TG |
265 | INIT_RHT_NULLS_HEAD(next, ht, old_hash); |
266 | if (!rht_is_a_nulls(he)) { | |
97defe1e TG |
267 | rht_for_each_continue(he, he->next, old_tbl, old_hash) { |
268 | if (head_hashfn(ht, new_tbl, he) == new_hash) { | |
7e1e7763 TG |
269 | next = he; |
270 | break; | |
271 | } | |
272 | } | |
273 | } | |
274 | ||
275 | /* Set p's next pointer to that subsequent node pointer, | |
276 | * bypassing the nodes which do not hash to p's bucket | |
277 | */ | |
97defe1e TG |
278 | rcu_assign_pointer(p->next, next); |
279 | ||
280 | if (new_bucket_lock != new_bucket_lock2) | |
281 | spin_unlock_bh(new_bucket_lock2); | |
282 | spin_unlock_bh(new_bucket_lock); | |
283 | } | |
284 | ||
285 | static void link_old_to_new(struct bucket_table *new_tbl, | |
286 | unsigned int new_hash, struct rhash_head *entry) | |
287 | { | |
288 | spinlock_t *new_bucket_lock; | |
289 | ||
290 | new_bucket_lock = bucket_lock(new_tbl, new_hash); | |
291 | ||
292 | spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); | |
293 | rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry); | |
294 | spin_unlock_bh(new_bucket_lock); | |
7e1e7763 TG |
295 | } |
296 | ||
297 | /** | |
298 | * rhashtable_expand - Expand hash table while allowing concurrent lookups | |
299 | * @ht: the hash table to expand | |
7e1e7763 TG |
300 | * |
301 | * A secondary bucket array is allocated and the hash entries are migrated | |
302 | * while keeping them on both lists until the end of the RCU grace period. | |
303 | * | |
304 | * This function may only be called in a context where it is safe to call | |
305 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. | |
306 | * | |
97defe1e TG |
307 | * The caller must ensure that no concurrent resizing occurs by holding |
308 | * ht->mutex. | |
309 | * | |
310 | * It is valid to have concurrent insertions and deletions protected by per | |
311 | * bucket locks or concurrent RCU protected lookups and traversals. | |
7e1e7763 | 312 | */ |
6eba8224 | 313 | int rhashtable_expand(struct rhashtable *ht) |
7e1e7763 TG |
314 | { |
315 | struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht); | |
316 | struct rhash_head *he; | |
97defe1e TG |
317 | spinlock_t *old_bucket_lock; |
318 | unsigned int new_hash, old_hash; | |
319 | bool complete = false; | |
7e1e7763 TG |
320 | |
321 | ASSERT_RHT_MUTEX(ht); | |
322 | ||
97defe1e | 323 | new_tbl = bucket_table_alloc(ht, old_tbl->size * 2); |
7e1e7763 TG |
324 | if (new_tbl == NULL) |
325 | return -ENOMEM; | |
326 | ||
c0c09bfd | 327 | atomic_inc(&ht->shift); |
7e1e7763 | 328 | |
97defe1e TG |
329 | /* Make insertions go into the new, empty table right away. Deletions |
330 | * and lookups will be attempted in both tables until we synchronize. | |
331 | * The synchronize_rcu() guarantees for the new table to be picked up | |
332 | * so no new additions go into the old table while we relink. | |
333 | */ | |
334 | rcu_assign_pointer(ht->future_tbl, new_tbl); | |
335 | synchronize_rcu(); | |
336 | ||
337 | /* For each new bucket, search the corresponding old bucket for the | |
338 | * first entry that hashes to the new bucket, and link the end of | |
339 | * newly formed bucket chain (containing entries added to future | |
340 | * table) to that entry. Since all the entries which will end up in | |
341 | * the new bucket appear in the same old bucket, this constructs an | |
342 | * entirely valid new hash table, but with multiple buckets | |
343 | * "zipped" together into a single imprecise chain. | |
7e1e7763 | 344 | */ |
97defe1e TG |
345 | for (new_hash = 0; new_hash < new_tbl->size; new_hash++) { |
346 | old_hash = rht_bucket_index(old_tbl, new_hash); | |
347 | old_bucket_lock = bucket_lock(old_tbl, old_hash); | |
348 | ||
349 | spin_lock_bh(old_bucket_lock); | |
350 | rht_for_each(he, old_tbl, old_hash) { | |
351 | if (head_hashfn(ht, new_tbl, he) == new_hash) { | |
352 | link_old_to_new(new_tbl, new_hash, he); | |
7e1e7763 TG |
353 | break; |
354 | } | |
355 | } | |
97defe1e | 356 | spin_unlock_bh(old_bucket_lock); |
7e1e7763 TG |
357 | } |
358 | ||
359 | /* Publish the new table pointer. Lookups may now traverse | |
0c828f2f HX |
360 | * the new table, but they will not benefit from any |
361 | * additional efficiency until later steps unzip the buckets. | |
7e1e7763 TG |
362 | */ |
363 | rcu_assign_pointer(ht->tbl, new_tbl); | |
364 | ||
365 | /* Unzip interleaved hash chains */ | |
97defe1e | 366 | while (!complete && !ht->being_destroyed) { |
7e1e7763 TG |
367 | /* Wait for readers. All new readers will see the new |
368 | * table, and thus no references to the old table will | |
369 | * remain. | |
370 | */ | |
371 | synchronize_rcu(); | |
372 | ||
373 | /* For each bucket in the old table (each of which | |
374 | * contains items from multiple buckets of the new | |
375 | * table): ... | |
376 | */ | |
377 | complete = true; | |
97defe1e | 378 | for (old_hash = 0; old_hash < old_tbl->size; old_hash++) { |
f89bd6f8 TG |
379 | struct rhash_head *head; |
380 | ||
97defe1e TG |
381 | old_bucket_lock = bucket_lock(old_tbl, old_hash); |
382 | spin_lock_bh(old_bucket_lock); | |
383 | ||
384 | hashtable_chain_unzip(ht, new_tbl, old_tbl, old_hash); | |
f89bd6f8 TG |
385 | head = rht_dereference_bucket(old_tbl->buckets[old_hash], |
386 | old_tbl, old_hash); | |
387 | if (!rht_is_a_nulls(head)) | |
7e1e7763 | 388 | complete = false; |
97defe1e TG |
389 | |
390 | spin_unlock_bh(old_bucket_lock); | |
7e1e7763 | 391 | } |
97defe1e | 392 | } |
7e1e7763 TG |
393 | |
394 | bucket_table_free(old_tbl); | |
395 | return 0; | |
396 | } | |
397 | EXPORT_SYMBOL_GPL(rhashtable_expand); | |
398 | ||
399 | /** | |
400 | * rhashtable_shrink - Shrink hash table while allowing concurrent lookups | |
401 | * @ht: the hash table to shrink | |
7e1e7763 TG |
402 | * |
403 | * This function may only be called in a context where it is safe to call | |
404 | * synchronize_rcu(), e.g. not within a rcu_read_lock() section. | |
405 | * | |
97defe1e TG |
406 | * The caller must ensure that no concurrent resizing occurs by holding |
407 | * ht->mutex. | |
408 | * | |
7e1e7763 TG |
409 | * The caller must ensure that no concurrent table mutations take place. |
410 | * It is however valid to have concurrent lookups if they are RCU protected. | |
97defe1e TG |
411 | * |
412 | * It is valid to have concurrent insertions and deletions protected by per | |
413 | * bucket locks or concurrent RCU protected lookups and traversals. | |
7e1e7763 | 414 | */ |
6eba8224 | 415 | int rhashtable_shrink(struct rhashtable *ht) |
7e1e7763 | 416 | { |
97defe1e TG |
417 | struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht); |
418 | spinlock_t *new_bucket_lock, *old_bucket_lock1, *old_bucket_lock2; | |
419 | unsigned int new_hash; | |
7e1e7763 TG |
420 | |
421 | ASSERT_RHT_MUTEX(ht); | |
422 | ||
97defe1e TG |
423 | new_tbl = bucket_table_alloc(ht, tbl->size / 2); |
424 | if (new_tbl == NULL) | |
7e1e7763 TG |
425 | return -ENOMEM; |
426 | ||
97defe1e TG |
427 | rcu_assign_pointer(ht->future_tbl, new_tbl); |
428 | synchronize_rcu(); | |
7e1e7763 | 429 | |
97defe1e TG |
430 | /* Link the first entry in the old bucket to the end of the |
431 | * bucket in the new table. As entries are concurrently being | |
432 | * added to the new table, lock down the new bucket. As we | |
433 | * always divide the size in half when shrinking, each bucket | |
434 | * in the new table maps to exactly two buckets in the old | |
435 | * table. | |
436 | * | |
437 | * As removals can occur concurrently on the old table, we need | |
438 | * to lock down both matching buckets in the old table. | |
7e1e7763 | 439 | */ |
97defe1e TG |
440 | for (new_hash = 0; new_hash < new_tbl->size; new_hash++) { |
441 | old_bucket_lock1 = bucket_lock(tbl, new_hash); | |
442 | old_bucket_lock2 = bucket_lock(tbl, new_hash + new_tbl->size); | |
443 | new_bucket_lock = bucket_lock(new_tbl, new_hash); | |
444 | ||
445 | spin_lock_bh(old_bucket_lock1); | |
80ca8c3a TG |
446 | |
447 | /* Depending on the lock per buckets mapping, the bucket in | |
448 | * the lower and upper region may map to the same lock. | |
449 | */ | |
450 | if (old_bucket_lock1 != old_bucket_lock2) { | |
451 | spin_lock_bh_nested(old_bucket_lock2, RHT_LOCK_NESTED); | |
452 | spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED2); | |
453 | } else { | |
454 | spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); | |
455 | } | |
97defe1e TG |
456 | |
457 | rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), | |
458 | tbl->buckets[new_hash]); | |
459 | rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), | |
460 | tbl->buckets[new_hash + new_tbl->size]); | |
461 | ||
462 | spin_unlock_bh(new_bucket_lock); | |
80ca8c3a TG |
463 | if (old_bucket_lock1 != old_bucket_lock2) |
464 | spin_unlock_bh(old_bucket_lock2); | |
97defe1e | 465 | spin_unlock_bh(old_bucket_lock1); |
7e1e7763 TG |
466 | } |
467 | ||
468 | /* Publish the new, valid hash table */ | |
97defe1e | 469 | rcu_assign_pointer(ht->tbl, new_tbl); |
c0c09bfd | 470 | atomic_dec(&ht->shift); |
7e1e7763 TG |
471 | |
472 | /* Wait for readers. No new readers will have references to the | |
473 | * old hash table. | |
474 | */ | |
475 | synchronize_rcu(); | |
476 | ||
477 | bucket_table_free(tbl); | |
478 | ||
479 | return 0; | |
480 | } | |
481 | EXPORT_SYMBOL_GPL(rhashtable_shrink); | |
482 | ||
97defe1e TG |
483 | static void rht_deferred_worker(struct work_struct *work) |
484 | { | |
485 | struct rhashtable *ht; | |
486 | struct bucket_table *tbl; | |
487 | ||
57699a40 | 488 | ht = container_of(work, struct rhashtable, run_work); |
97defe1e | 489 | mutex_lock(&ht->mutex); |
28134a53 HX |
490 | if (ht->being_destroyed) |
491 | goto unlock; | |
492 | ||
97defe1e TG |
493 | tbl = rht_dereference(ht->tbl, ht); |
494 | ||
495 | if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size)) | |
496 | rhashtable_expand(ht); | |
497 | else if (ht->p.shrink_decision && ht->p.shrink_decision(ht, tbl->size)) | |
498 | rhashtable_shrink(ht); | |
499 | ||
28134a53 | 500 | unlock: |
97defe1e TG |
501 | mutex_unlock(&ht->mutex); |
502 | } | |
503 | ||
54c5b7d3 YX |
504 | static void rhashtable_wakeup_worker(struct rhashtable *ht) |
505 | { | |
506 | struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht); | |
507 | struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht); | |
508 | size_t size = tbl->size; | |
509 | ||
510 | /* Only adjust the table if no resizing is currently in progress. */ | |
511 | if (tbl == new_tbl && | |
512 | ((ht->p.grow_decision && ht->p.grow_decision(ht, size)) || | |
513 | (ht->p.shrink_decision && ht->p.shrink_decision(ht, size)))) | |
57699a40 | 514 | schedule_work(&ht->run_work); |
54c5b7d3 YX |
515 | } |
516 | ||
db304854 YX |
517 | static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj, |
518 | struct bucket_table *tbl, u32 hash) | |
519 | { | |
520 | struct rhash_head *head = rht_dereference_bucket(tbl->buckets[hash], | |
521 | tbl, hash); | |
522 | ||
523 | if (rht_is_a_nulls(head)) | |
524 | INIT_RHT_NULLS_HEAD(obj->next, ht, hash); | |
525 | else | |
526 | RCU_INIT_POINTER(obj->next, head); | |
527 | ||
528 | rcu_assign_pointer(tbl->buckets[hash], obj); | |
529 | ||
530 | atomic_inc(&ht->nelems); | |
531 | ||
532 | rhashtable_wakeup_worker(ht); | |
533 | } | |
534 | ||
7e1e7763 | 535 | /** |
db304854 | 536 | * rhashtable_insert - insert object into hash table |
7e1e7763 TG |
537 | * @ht: hash table |
538 | * @obj: pointer to hash head inside object | |
7e1e7763 | 539 | * |
97defe1e TG |
540 | * Will take a per bucket spinlock to protect against mutual mutations |
541 | * on the same bucket. Multiple insertions may occur in parallel unless | |
542 | * they map to the same bucket lock. | |
7e1e7763 | 543 | * |
97defe1e TG |
544 | * It is safe to call this function from atomic context. |
545 | * | |
546 | * Will trigger an automatic deferred table resizing if the size grows | |
547 | * beyond the watermark indicated by grow_decision() which can be passed | |
548 | * to rhashtable_init(). | |
7e1e7763 | 549 | */ |
6eba8224 | 550 | void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj) |
7e1e7763 | 551 | { |
97defe1e TG |
552 | struct bucket_table *tbl; |
553 | spinlock_t *lock; | |
554 | unsigned hash; | |
7e1e7763 | 555 | |
97defe1e | 556 | rcu_read_lock(); |
7e1e7763 | 557 | |
97defe1e | 558 | tbl = rht_dereference_rcu(ht->future_tbl, ht); |
8d24c0b4 | 559 | hash = head_hashfn(ht, tbl, obj); |
97defe1e TG |
560 | lock = bucket_lock(tbl, hash); |
561 | ||
562 | spin_lock_bh(lock); | |
db304854 | 563 | __rhashtable_insert(ht, obj, tbl, hash); |
97defe1e | 564 | spin_unlock_bh(lock); |
7e1e7763 | 565 | |
97defe1e | 566 | rcu_read_unlock(); |
7e1e7763 TG |
567 | } |
568 | EXPORT_SYMBOL_GPL(rhashtable_insert); | |
569 | ||
7e1e7763 TG |
570 | /** |
571 | * rhashtable_remove - remove object from hash table | |
572 | * @ht: hash table | |
573 | * @obj: pointer to hash head inside object | |
7e1e7763 TG |
574 | * |
575 | * Since the hash chain is single linked, the removal operation needs to | |
576 | * walk the bucket chain upon removal. The removal operation is thus | |
577 | * considerable slow if the hash table is not correctly sized. | |
578 | * | |
db304854 | 579 | * Will automatically shrink the table via rhashtable_expand() if the |
7e1e7763 TG |
580 | * shrink_decision function specified at rhashtable_init() returns true. |
581 | * | |
582 | * The caller must ensure that no concurrent table mutations occur. It is | |
583 | * however valid to have concurrent lookups if they are RCU protected. | |
584 | */ | |
6eba8224 | 585 | bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj) |
7e1e7763 | 586 | { |
97defe1e | 587 | struct bucket_table *tbl; |
7e1e7763 TG |
588 | struct rhash_head __rcu **pprev; |
589 | struct rhash_head *he; | |
97defe1e TG |
590 | spinlock_t *lock; |
591 | unsigned int hash; | |
fe6a043c | 592 | bool ret = false; |
7e1e7763 | 593 | |
97defe1e TG |
594 | rcu_read_lock(); |
595 | tbl = rht_dereference_rcu(ht->tbl, ht); | |
596 | hash = head_hashfn(ht, tbl, obj); | |
7e1e7763 | 597 | |
97defe1e TG |
598 | lock = bucket_lock(tbl, hash); |
599 | spin_lock_bh(lock); | |
7e1e7763 | 600 | |
97defe1e TG |
601 | restart: |
602 | pprev = &tbl->buckets[hash]; | |
603 | rht_for_each(he, tbl, hash) { | |
7e1e7763 TG |
604 | if (he != obj) { |
605 | pprev = &he->next; | |
606 | continue; | |
607 | } | |
608 | ||
97defe1e | 609 | rcu_assign_pointer(*pprev, obj->next); |
897362e4 | 610 | |
fe6a043c TG |
611 | ret = true; |
612 | break; | |
7e1e7763 TG |
613 | } |
614 | ||
fe6a043c TG |
615 | /* The entry may be linked in either 'tbl', 'future_tbl', or both. |
616 | * 'future_tbl' only exists for a short period of time during | |
617 | * resizing. Thus traversing both is fine and the added cost is | |
618 | * very rare. | |
619 | */ | |
bd6d4db5 | 620 | if (tbl != rht_dereference_rcu(ht->future_tbl, ht)) { |
97defe1e TG |
621 | spin_unlock_bh(lock); |
622 | ||
bd6d4db5 | 623 | tbl = rht_dereference_rcu(ht->future_tbl, ht); |
97defe1e TG |
624 | hash = head_hashfn(ht, tbl, obj); |
625 | ||
626 | lock = bucket_lock(tbl, hash); | |
627 | spin_lock_bh(lock); | |
628 | goto restart; | |
629 | } | |
630 | ||
631 | spin_unlock_bh(lock); | |
fe6a043c TG |
632 | |
633 | if (ret) { | |
634 | atomic_dec(&ht->nelems); | |
635 | rhashtable_wakeup_worker(ht); | |
636 | } | |
637 | ||
97defe1e TG |
638 | rcu_read_unlock(); |
639 | ||
fe6a043c | 640 | return ret; |
7e1e7763 TG |
641 | } |
642 | EXPORT_SYMBOL_GPL(rhashtable_remove); | |
643 | ||
efb975a6 YX |
644 | struct rhashtable_compare_arg { |
645 | struct rhashtable *ht; | |
646 | const void *key; | |
647 | }; | |
648 | ||
649 | static bool rhashtable_compare(void *ptr, void *arg) | |
650 | { | |
651 | struct rhashtable_compare_arg *x = arg; | |
652 | struct rhashtable *ht = x->ht; | |
653 | ||
654 | return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len); | |
655 | } | |
656 | ||
7e1e7763 TG |
657 | /** |
658 | * rhashtable_lookup - lookup key in hash table | |
659 | * @ht: hash table | |
660 | * @key: pointer to key | |
661 | * | |
662 | * Computes the hash value for the key and traverses the bucket chain looking | |
663 | * for a entry with an identical key. The first matching entry is returned. | |
664 | * | |
665 | * This lookup function may only be used for fixed key hash table (key_len | |
db304854 | 666 | * parameter set). It will BUG() if used inappropriately. |
7e1e7763 | 667 | * |
97defe1e | 668 | * Lookups may occur in parallel with hashtable mutations and resizing. |
7e1e7763 | 669 | */ |
97defe1e | 670 | void *rhashtable_lookup(struct rhashtable *ht, const void *key) |
7e1e7763 | 671 | { |
efb975a6 YX |
672 | struct rhashtable_compare_arg arg = { |
673 | .ht = ht, | |
674 | .key = key, | |
675 | }; | |
7e1e7763 TG |
676 | |
677 | BUG_ON(!ht->p.key_len); | |
678 | ||
efb975a6 | 679 | return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg); |
7e1e7763 TG |
680 | } |
681 | EXPORT_SYMBOL_GPL(rhashtable_lookup); | |
682 | ||
683 | /** | |
684 | * rhashtable_lookup_compare - search hash table with compare function | |
685 | * @ht: hash table | |
8d24c0b4 | 686 | * @key: the pointer to the key |
7e1e7763 TG |
687 | * @compare: compare function, must return true on match |
688 | * @arg: argument passed on to compare function | |
689 | * | |
690 | * Traverses the bucket chain behind the provided hash value and calls the | |
691 | * specified compare function for each entry. | |
692 | * | |
97defe1e | 693 | * Lookups may occur in parallel with hashtable mutations and resizing. |
7e1e7763 TG |
694 | * |
695 | * Returns the first entry on which the compare function returned true. | |
696 | */ | |
97defe1e | 697 | void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key, |
7e1e7763 TG |
698 | bool (*compare)(void *, void *), void *arg) |
699 | { | |
97defe1e | 700 | const struct bucket_table *tbl, *old_tbl; |
7e1e7763 | 701 | struct rhash_head *he; |
8d24c0b4 | 702 | u32 hash; |
7e1e7763 | 703 | |
97defe1e TG |
704 | rcu_read_lock(); |
705 | ||
706 | old_tbl = rht_dereference_rcu(ht->tbl, ht); | |
707 | tbl = rht_dereference_rcu(ht->future_tbl, ht); | |
8d24c0b4 | 708 | hash = key_hashfn(ht, key, ht->p.key_len); |
97defe1e TG |
709 | restart: |
710 | rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) { | |
7e1e7763 TG |
711 | if (!compare(rht_obj(ht, he), arg)) |
712 | continue; | |
97defe1e | 713 | rcu_read_unlock(); |
a4b18cda | 714 | return rht_obj(ht, he); |
7e1e7763 TG |
715 | } |
716 | ||
97defe1e TG |
717 | if (unlikely(tbl != old_tbl)) { |
718 | tbl = old_tbl; | |
719 | goto restart; | |
720 | } | |
721 | rcu_read_unlock(); | |
722 | ||
7e1e7763 TG |
723 | return NULL; |
724 | } | |
725 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare); | |
726 | ||
db304854 YX |
727 | /** |
728 | * rhashtable_lookup_insert - lookup and insert object into hash table | |
729 | * @ht: hash table | |
730 | * @obj: pointer to hash head inside object | |
731 | * | |
732 | * Locks down the bucket chain in both the old and new table if a resize | |
733 | * is in progress to ensure that writers can't remove from the old table | |
734 | * and can't insert to the new table during the atomic operation of search | |
735 | * and insertion. Searches for duplicates in both the old and new table if | |
736 | * a resize is in progress. | |
737 | * | |
738 | * This lookup function may only be used for fixed key hash table (key_len | |
739 | * parameter set). It will BUG() if used inappropriately. | |
740 | * | |
741 | * It is safe to call this function from atomic context. | |
742 | * | |
743 | * Will trigger an automatic deferred table resizing if the size grows | |
744 | * beyond the watermark indicated by grow_decision() which can be passed | |
745 | * to rhashtable_init(). | |
746 | */ | |
747 | bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj) | |
7a868d1e YX |
748 | { |
749 | struct rhashtable_compare_arg arg = { | |
750 | .ht = ht, | |
751 | .key = rht_obj(ht, obj) + ht->p.key_offset, | |
752 | }; | |
753 | ||
754 | BUG_ON(!ht->p.key_len); | |
755 | ||
756 | return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare, | |
757 | &arg); | |
758 | } | |
759 | EXPORT_SYMBOL_GPL(rhashtable_lookup_insert); | |
760 | ||
761 | /** | |
762 | * rhashtable_lookup_compare_insert - search and insert object to hash table | |
763 | * with compare function | |
764 | * @ht: hash table | |
765 | * @obj: pointer to hash head inside object | |
766 | * @compare: compare function, must return true on match | |
767 | * @arg: argument passed on to compare function | |
768 | * | |
769 | * Locks down the bucket chain in both the old and new table if a resize | |
770 | * is in progress to ensure that writers can't remove from the old table | |
771 | * and can't insert to the new table during the atomic operation of search | |
772 | * and insertion. Searches for duplicates in both the old and new table if | |
773 | * a resize is in progress. | |
774 | * | |
775 | * Lookups may occur in parallel with hashtable mutations and resizing. | |
776 | * | |
777 | * Will trigger an automatic deferred table resizing if the size grows | |
778 | * beyond the watermark indicated by grow_decision() which can be passed | |
779 | * to rhashtable_init(). | |
780 | */ | |
781 | bool rhashtable_lookup_compare_insert(struct rhashtable *ht, | |
782 | struct rhash_head *obj, | |
783 | bool (*compare)(void *, void *), | |
784 | void *arg) | |
db304854 YX |
785 | { |
786 | struct bucket_table *new_tbl, *old_tbl; | |
787 | spinlock_t *new_bucket_lock, *old_bucket_lock; | |
788 | u32 new_hash, old_hash; | |
789 | bool success = true; | |
790 | ||
791 | BUG_ON(!ht->p.key_len); | |
792 | ||
793 | rcu_read_lock(); | |
794 | ||
795 | old_tbl = rht_dereference_rcu(ht->tbl, ht); | |
796 | old_hash = head_hashfn(ht, old_tbl, obj); | |
797 | old_bucket_lock = bucket_lock(old_tbl, old_hash); | |
798 | spin_lock_bh(old_bucket_lock); | |
799 | ||
800 | new_tbl = rht_dereference_rcu(ht->future_tbl, ht); | |
801 | new_hash = head_hashfn(ht, new_tbl, obj); | |
802 | new_bucket_lock = bucket_lock(new_tbl, new_hash); | |
803 | if (unlikely(old_tbl != new_tbl)) | |
804 | spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED); | |
805 | ||
7a868d1e YX |
806 | if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset, |
807 | compare, arg)) { | |
db304854 YX |
808 | success = false; |
809 | goto exit; | |
810 | } | |
811 | ||
812 | __rhashtable_insert(ht, obj, new_tbl, new_hash); | |
813 | ||
814 | exit: | |
815 | if (unlikely(old_tbl != new_tbl)) | |
816 | spin_unlock_bh(new_bucket_lock); | |
817 | spin_unlock_bh(old_bucket_lock); | |
818 | ||
819 | rcu_read_unlock(); | |
820 | ||
821 | return success; | |
822 | } | |
7a868d1e | 823 | EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert); |
db304854 | 824 | |
94000176 | 825 | static size_t rounded_hashtable_size(struct rhashtable_params *params) |
7e1e7763 | 826 | { |
94000176 YX |
827 | return max(roundup_pow_of_two(params->nelem_hint * 4 / 3), |
828 | 1UL << params->min_shift); | |
7e1e7763 TG |
829 | } |
830 | ||
831 | /** | |
832 | * rhashtable_init - initialize a new hash table | |
833 | * @ht: hash table to be initialized | |
834 | * @params: configuration parameters | |
835 | * | |
836 | * Initializes a new hash table based on the provided configuration | |
837 | * parameters. A table can be configured either with a variable or | |
838 | * fixed length key: | |
839 | * | |
840 | * Configuration Example 1: Fixed length keys | |
841 | * struct test_obj { | |
842 | * int key; | |
843 | * void * my_member; | |
844 | * struct rhash_head node; | |
845 | * }; | |
846 | * | |
847 | * struct rhashtable_params params = { | |
848 | * .head_offset = offsetof(struct test_obj, node), | |
849 | * .key_offset = offsetof(struct test_obj, key), | |
850 | * .key_len = sizeof(int), | |
87545899 | 851 | * .hashfn = jhash, |
f89bd6f8 | 852 | * .nulls_base = (1U << RHT_BASE_SHIFT), |
7e1e7763 TG |
853 | * }; |
854 | * | |
855 | * Configuration Example 2: Variable length keys | |
856 | * struct test_obj { | |
857 | * [...] | |
858 | * struct rhash_head node; | |
859 | * }; | |
860 | * | |
861 | * u32 my_hash_fn(const void *data, u32 seed) | |
862 | * { | |
863 | * struct test_obj *obj = data; | |
864 | * | |
865 | * return [... hash ...]; | |
866 | * } | |
867 | * | |
868 | * struct rhashtable_params params = { | |
869 | * .head_offset = offsetof(struct test_obj, node), | |
87545899 | 870 | * .hashfn = jhash, |
7e1e7763 | 871 | * .obj_hashfn = my_hash_fn, |
7e1e7763 TG |
872 | * }; |
873 | */ | |
874 | int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params) | |
875 | { | |
876 | struct bucket_table *tbl; | |
877 | size_t size; | |
878 | ||
879 | size = HASH_DEFAULT_SIZE; | |
880 | ||
881 | if ((params->key_len && !params->hashfn) || | |
882 | (!params->key_len && !params->obj_hashfn)) | |
883 | return -EINVAL; | |
884 | ||
f89bd6f8 TG |
885 | if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT)) |
886 | return -EINVAL; | |
887 | ||
94000176 YX |
888 | params->min_shift = max_t(size_t, params->min_shift, |
889 | ilog2(HASH_MIN_SIZE)); | |
890 | ||
7e1e7763 | 891 | if (params->nelem_hint) |
94000176 | 892 | size = rounded_hashtable_size(params); |
7e1e7763 | 893 | |
97defe1e TG |
894 | memset(ht, 0, sizeof(*ht)); |
895 | mutex_init(&ht->mutex); | |
896 | memcpy(&ht->p, params, sizeof(*params)); | |
897 | ||
898 | if (params->locks_mul) | |
899 | ht->p.locks_mul = roundup_pow_of_two(params->locks_mul); | |
900 | else | |
901 | ht->p.locks_mul = BUCKET_LOCKS_PER_CPU; | |
902 | ||
903 | tbl = bucket_table_alloc(ht, size); | |
7e1e7763 TG |
904 | if (tbl == NULL) |
905 | return -ENOMEM; | |
906 | ||
545a148e | 907 | atomic_set(&ht->nelems, 0); |
c0c09bfd | 908 | atomic_set(&ht->shift, ilog2(tbl->size)); |
7e1e7763 | 909 | RCU_INIT_POINTER(ht->tbl, tbl); |
97defe1e | 910 | RCU_INIT_POINTER(ht->future_tbl, tbl); |
7e1e7763 TG |
911 | |
912 | if (!ht->p.hash_rnd) | |
913 | get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd)); | |
914 | ||
97defe1e | 915 | if (ht->p.grow_decision || ht->p.shrink_decision) |
57699a40 | 916 | INIT_WORK(&ht->run_work, rht_deferred_worker); |
97defe1e | 917 | |
7e1e7763 TG |
918 | return 0; |
919 | } | |
920 | EXPORT_SYMBOL_GPL(rhashtable_init); | |
921 | ||
922 | /** | |
923 | * rhashtable_destroy - destroy hash table | |
924 | * @ht: the hash table to destroy | |
925 | * | |
ae82ddcf PNA |
926 | * Frees the bucket array. This function is not rcu safe, therefore the caller |
927 | * has to make sure that no resizing may happen by unpublishing the hashtable | |
928 | * and waiting for the quiescent cycle before releasing the bucket array. | |
7e1e7763 | 929 | */ |
97defe1e | 930 | void rhashtable_destroy(struct rhashtable *ht) |
7e1e7763 | 931 | { |
97defe1e TG |
932 | ht->being_destroyed = true; |
933 | ||
57699a40 YX |
934 | if (ht->p.grow_decision || ht->p.shrink_decision) |
935 | cancel_work_sync(&ht->run_work); | |
97defe1e | 936 | |
57699a40 | 937 | mutex_lock(&ht->mutex); |
97defe1e | 938 | bucket_table_free(rht_dereference(ht->tbl, ht)); |
97defe1e | 939 | mutex_unlock(&ht->mutex); |
7e1e7763 TG |
940 | } |
941 | EXPORT_SYMBOL_GPL(rhashtable_destroy); |