blob: d1d23fb5852570e9c8422e0dd04f7153c15c8713 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Herbert Xu02fd97c2015-03-20 21:57:00 +11004 * Copyright (c) 2015 Herbert Xu <herbert@gondor.apana.org.au>
Thomas Grafa5ec68e2015-02-05 02:03:32 +01005 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02006 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 *
Thomas Graf7e1e7762014-08-02 11:47:44 +02008 * Code partially derived from nft_hash
Herbert Xu02fd97c2015-03-20 21:57:00 +11009 * Rewritten with rehash code from br_multicast plus single list
10 * pointer as suggested by Josh Triplett
Thomas Graf7e1e7762014-08-02 11:47:44 +020011 *
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>
Eric Dumazet5beb5c92015-02-26 07:20:34 -080020#include <linux/sched.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020021#include <linux/slab.h>
22#include <linux/vmalloc.h>
23#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010024#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020025#include <linux/random.h>
26#include <linux/rhashtable.h>
Stephen Rothwell61d7b092015-02-09 14:04:03 +110027#include <linux/err.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020028
29#define HASH_DEFAULT_SIZE 64UL
Herbert Xuc2e213c2015-03-18 20:01:16 +110030#define HASH_MIN_SIZE 4U
Thomas Graf97defe12015-01-02 23:00:20 +010031#define BUCKET_LOCKS_PER_CPU 128UL
32
Herbert Xu988dfbd2015-03-10 09:27:55 +110033static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010034 const struct bucket_table *tbl,
35 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020036{
Herbert Xu02fd97c2015-03-20 21:57:00 +110037 return rht_head_hashfn(ht, tbl, he, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +020038}
39
Thomas Grafa03eaec2015-02-05 02:03:34 +010040#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010041#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010042
43int lockdep_rht_mutex_is_held(struct rhashtable *ht)
44{
45 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
46}
47EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
48
49int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
50{
Herbert Xu02fd97c2015-03-20 21:57:00 +110051 spinlock_t *lock = rht_bucket_lock(tbl, hash);
Thomas Grafa03eaec2015-02-05 02:03:34 +010052
53 return (debug_locks) ? lockdep_is_held(lock) : 1;
54}
55EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
56#else
57#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +010058#endif
59
60
Thomas Graf97defe12015-01-02 23:00:20 +010061static 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
Thomas Grafa5ec68e2015-02-05 02:03:32 +010073 /* Never allocate more than 0.5 locks per bucket */
74 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +010075
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
94static void bucket_table_free(const struct bucket_table *tbl)
95{
96 if (tbl)
97 kvfree(tbl->locks);
98
99 kvfree(tbl);
100}
101
Herbert Xu9d901bc2015-03-14 13:57:23 +1100102static void bucket_table_free_rcu(struct rcu_head *head)
103{
104 bucket_table_free(container_of(head, struct bucket_table, rcu));
105}
106
Thomas Graf97defe12015-01-02 23:00:20 +0100107static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Herbert Xu5269b532015-03-14 13:57:22 +1100108 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200109{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100110 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200111 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100112 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200113
114 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100115 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
116 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200117 if (tbl == NULL)
118 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200119 if (tbl == NULL)
120 return NULL;
121
122 tbl->size = nbuckets;
123
Thomas Graf97defe12015-01-02 23:00:20 +0100124 if (alloc_bucket_locks(ht, tbl) < 0) {
125 bucket_table_free(tbl);
126 return NULL;
127 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200128
Herbert Xueddee5ba2015-03-14 13:57:20 +1100129 INIT_LIST_HEAD(&tbl->walkers);
130
Herbert Xu5269b532015-03-14 13:57:22 +1100131 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
132
Thomas Graff89bd6f2015-01-02 23:00:21 +0100133 for (i = 0; i < nbuckets; i++)
134 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
135
Thomas Graf97defe12015-01-02 23:00:20 +0100136 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200137}
138
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100139static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100140{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100141 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xuc4db8842015-03-14 13:57:25 +1100142 struct bucket_table *new_tbl =
143 rht_dereference(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100144 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;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100149
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100150 rht_for_each(entry, old_tbl, old_hash) {
151 err = 0;
152 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100153
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100154 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200155 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100156
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100157 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200158 }
159
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100160 if (err)
161 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100162
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100163 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100164
Herbert Xu02fd97c2015-03-20 21:57:00 +1100165 new_bucket_lock = rht_bucket_lock(new_tbl, new_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100166
Herbert Xu8f2484b2015-03-14 13:57:21 +1100167 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100168 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
169 new_tbl, new_hash);
170
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);
175
176 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
177 spin_unlock(new_bucket_lock);
178
179 rcu_assign_pointer(*pprev, next);
180
181out:
182 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100183}
184
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100185static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100186{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100187 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
188 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100189
Herbert Xu02fd97c2015-03-20 21:57:00 +1100190 old_bucket_lock = rht_bucket_lock(old_tbl, old_hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100191
192 spin_lock_bh(old_bucket_lock);
193 while (!rhashtable_rehash_one(ht, old_hash))
194 ;
Herbert Xu63d512d2015-03-14 13:57:24 +1100195 old_tbl->rehash++;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100196 spin_unlock_bh(old_bucket_lock);
197}
198
199static void rhashtable_rehash(struct rhashtable *ht,
200 struct bucket_table *new_tbl)
201{
202 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100203 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100204 unsigned old_hash;
205
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100206 /* Make insertions go into the new, empty table right away. Deletions
207 * and lookups will be attempted in both tables until we synchronize.
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100208 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100209 rcu_assign_pointer(old_tbl->future_tbl, new_tbl);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100210
Herbert Xu9497df82015-03-12 22:07:49 +1100211 /* Ensure the new table is visible to readers. */
212 smp_wmb();
213
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100214 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
Herbert Xueddee5ba2015-03-14 13:57:20 +1100220 list_for_each_entry(walker, &old_tbl->walkers, list)
221 walker->tbl = NULL;
222
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100223 /* Wait for readers. All new readers will see the new
224 * table, and thus no references to the old table will
225 * remain.
226 */
Herbert Xu9d901bc2015-03-14 13:57:23 +1100227 call_rcu(&old_tbl->rcu, bucket_table_free_rcu);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200228}
229
230/**
231 * rhashtable_expand - Expand hash table while allowing concurrent lookups
232 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200233 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100234 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200235 *
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 *
Thomas Graf97defe12015-01-02 23:00:20 +0100239 * 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.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200244 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100245int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200246{
247 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200248
249 ASSERT_RHT_MUTEX(ht);
250
Herbert Xu5269b532015-03-14 13:57:22 +1100251 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200252 if (new_tbl == NULL)
253 return -ENOMEM;
254
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100255 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200256 return 0;
257}
258EXPORT_SYMBOL_GPL(rhashtable_expand);
259
260/**
261 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
262 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200263 *
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 *
Thomas Graf97defe12015-01-02 23:00:20 +0100267 * The caller must ensure that no concurrent resizing occurs by holding
268 * ht->mutex.
269 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200270 * 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.
Thomas Graf97defe12015-01-02 23:00:20 +0100272 *
273 * It is valid to have concurrent insertions and deletions protected by per
274 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200275 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100276int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200277{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100278 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200279
280 ASSERT_RHT_MUTEX(ht);
281
Herbert Xu5269b532015-03-14 13:57:22 +1100282 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2);
Thomas Graf97defe12015-01-02 23:00:20 +0100283 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200284 return -ENOMEM;
285
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100286 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200287 return 0;
288}
289EXPORT_SYMBOL_GPL(rhashtable_shrink);
290
Thomas Graf97defe12015-01-02 23:00:20 +0100291static void rht_deferred_worker(struct work_struct *work)
292{
293 struct rhashtable *ht;
294 struct bucket_table *tbl;
295
Ying Xue57699a42015-01-16 11:13:09 +0800296 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100297 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100298 if (ht->being_destroyed)
299 goto unlock;
300
Thomas Graf97defe12015-01-02 23:00:20 +0100301 tbl = rht_dereference(ht->tbl, ht);
302
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100303 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100304 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100305 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100306 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100307unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100308 mutex_unlock(&ht->mutex);
309}
310
Herbert Xu02fd97c2015-03-20 21:57:00 +1100311int 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
335exit:
336 spin_unlock(rht_bucket_lock(tbl, hash));
337
338 return err;
339}
340EXPORT_SYMBOL_GPL(rhashtable_insert_slow);
341
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100342static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
343 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800344{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100345 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000346 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100347 bool no_resize_running;
348 unsigned hash;
Thomas Graf617011e2015-03-16 10:42:26 +0100349 spinlock_t *old_lock;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100350 bool success = true;
351
352 rcu_read_lock();
353
354 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100355 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100356 old_lock = rht_bucket_lock(old_tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100357
Thomas Graf617011e2015-03-16 10:42:26 +0100358 spin_lock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100359
360 /* Because we have already taken the bucket lock in old_tbl,
361 * if we find that future_tbl is not yet visible then that
362 * guarantees all other insertions of the same entry will
363 * also grab the bucket lock in old_tbl because until the
364 * rehash completes ht->tbl won't be changed.
365 */
Herbert Xuc4db8842015-03-14 13:57:25 +1100366 tbl = rht_dereference_rcu(old_tbl->future_tbl, ht) ?: old_tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100367 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100368 hash = head_hashfn(ht, tbl, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100369 spin_lock_nested(rht_bucket_lock(tbl, hash),
370 SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100371 }
372
373 if (compare &&
374 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
375 compare, arg)) {
376 success = false;
377 goto exit;
378 }
379
380 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000381
Thomas Graf020219a2015-02-06 16:08:43 +0000382 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800383
384 if (rht_is_a_nulls(head))
385 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
386 else
387 RCU_INIT_POINTER(obj->next, head);
388
389 rcu_assign_pointer(tbl->buckets[hash], obj);
390
391 atomic_inc(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100392 if (no_resize_running && rht_grow_above_75(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100393 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100394
395exit:
Thomas Graf617011e2015-03-16 10:42:26 +0100396 if (tbl != old_tbl)
Herbert Xu02fd97c2015-03-20 21:57:00 +1100397 spin_unlock(rht_bucket_lock(tbl, hash));
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100398
Thomas Graf617011e2015-03-16 10:42:26 +0100399 spin_unlock_bh(old_lock);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100400
401 rcu_read_unlock();
402
403 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800404}
405
Thomas Graf7e1e7762014-08-02 11:47:44 +0200406/**
Ying Xuedb304852015-01-07 13:41:54 +0800407 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200408 * @ht: hash table
409 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200410 *
Thomas Graf97defe12015-01-02 23:00:20 +0100411 * Will take a per bucket spinlock to protect against mutual mutations
412 * on the same bucket. Multiple insertions may occur in parallel unless
413 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200414 *
Thomas Graf97defe12015-01-02 23:00:20 +0100415 * It is safe to call this function from atomic context.
416 *
417 * Will trigger an automatic deferred table resizing if the size grows
418 * beyond the watermark indicated by grow_decision() which can be passed
419 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200420 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100421void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200422{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100423 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200424}
425EXPORT_SYMBOL_GPL(rhashtable_insert);
426
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100427static bool __rhashtable_remove(struct rhashtable *ht,
428 struct bucket_table *tbl,
429 struct rhash_head *obj)
430{
431 struct rhash_head __rcu **pprev;
432 struct rhash_head *he;
433 spinlock_t * lock;
434 unsigned hash;
435 bool ret = false;
436
Herbert Xueca84932015-03-12 14:49:39 +1100437 hash = head_hashfn(ht, tbl, obj);
Herbert Xu02fd97c2015-03-20 21:57:00 +1100438 lock = rht_bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100439
440 spin_lock_bh(lock);
441
442 pprev = &tbl->buckets[hash];
443 rht_for_each(he, tbl, hash) {
444 if (he != obj) {
445 pprev = &he->next;
446 continue;
447 }
448
449 rcu_assign_pointer(*pprev, obj->next);
450 ret = true;
451 break;
452 }
453
454 spin_unlock_bh(lock);
455
456 return ret;
457}
458
Thomas Graf7e1e7762014-08-02 11:47:44 +0200459/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460 * rhashtable_remove - remove object from hash table
461 * @ht: hash table
462 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200463 *
464 * Since the hash chain is single linked, the removal operation needs to
465 * walk the bucket chain upon removal. The removal operation is thus
466 * considerable slow if the hash table is not correctly sized.
467 *
Ying Xuedb304852015-01-07 13:41:54 +0800468 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200469 * shrink_decision function specified at rhashtable_init() returns true.
470 *
471 * The caller must ensure that no concurrent table mutations occur. It is
472 * however valid to have concurrent lookups if they are RCU protected.
473 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100474bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200475{
Herbert Xu565e8642015-03-15 21:12:05 +1100476 struct bucket_table *tbl;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100477 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200478
Thomas Graf97defe12015-01-02 23:00:20 +0100479 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100480
Herbert Xu565e8642015-03-15 21:12:05 +1100481 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200482
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100483 /* Because we have already taken (and released) the bucket
484 * lock in old_tbl, if we find that future_tbl is not yet
485 * visible then that guarantees the entry to still be in
Herbert Xu565e8642015-03-15 21:12:05 +1100486 * the old tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000487 */
Herbert Xu565e8642015-03-15 21:12:05 +1100488 while (!(ret = __rhashtable_remove(ht, tbl, obj)) &&
489 (tbl = rht_dereference_rcu(tbl->future_tbl, ht)))
490 ;
Thomas Graffe6a0432015-01-21 11:54:01 +0000491
492 if (ret) {
493 atomic_dec(&ht->nelems);
Herbert Xu565e8642015-03-15 21:12:05 +1100494 if (rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100495 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000496 }
497
Thomas Graf97defe12015-01-02 23:00:20 +0100498 rcu_read_unlock();
499
Thomas Graffe6a0432015-01-21 11:54:01 +0000500 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200501}
502EXPORT_SYMBOL_GPL(rhashtable_remove);
503
504/**
505 * rhashtable_lookup - lookup key in hash table
506 * @ht: hash table
507 * @key: pointer to key
508 *
509 * Computes the hash value for the key and traverses the bucket chain looking
510 * for a entry with an identical key. The first matching entry is returned.
511 *
512 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800513 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200514 *
Thomas Graf97defe12015-01-02 23:00:20 +0100515 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200516 */
Thomas Graf97defe12015-01-02 23:00:20 +0100517void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200518{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100519 return rhashtable_lookup_fast(ht, key, ht->p);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200520}
521EXPORT_SYMBOL_GPL(rhashtable_lookup);
522
523/**
524 * rhashtable_lookup_compare - search hash table with compare function
525 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100526 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200527 * @compare: compare function, must return true on match
528 * @arg: argument passed on to compare function
529 *
530 * Traverses the bucket chain behind the provided hash value and calls the
531 * specified compare function for each entry.
532 *
Thomas Graf97defe12015-01-02 23:00:20 +0100533 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200534 *
535 * Returns the first entry on which the compare function returned true.
536 */
Thomas Graf97defe12015-01-02 23:00:20 +0100537void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Herbert Xu02fd97c2015-03-20 21:57:00 +1100538 bool (*compare)(void *, void *),
539 void *arg)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200540{
Herbert Xuc4db8842015-03-14 13:57:25 +1100541 const struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200542 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100543 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200544
Thomas Graf97defe12015-01-02 23:00:20 +0100545 rcu_read_lock();
546
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100547 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100548restart:
Herbert Xu02fd97c2015-03-20 21:57:00 +1100549 hash = rht_key_hashfn(ht, tbl, key, ht->p);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100550 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200551 if (!compare(rht_obj(ht, he), arg))
552 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100553 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100554 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200555 }
556
Herbert Xu9497df82015-03-12 22:07:49 +1100557 /* Ensure we see any new tables. */
558 smp_rmb();
559
Herbert Xuc4db8842015-03-14 13:57:25 +1100560 tbl = rht_dereference_rcu(tbl->future_tbl, ht);
561 if (unlikely(tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100562 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100563 rcu_read_unlock();
564
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565 return NULL;
566}
567EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
568
Ying Xuedb304852015-01-07 13:41:54 +0800569/**
570 * rhashtable_lookup_insert - lookup and insert object into hash table
571 * @ht: hash table
572 * @obj: pointer to hash head inside object
573 *
574 * Locks down the bucket chain in both the old and new table if a resize
575 * is in progress to ensure that writers can't remove from the old table
576 * and can't insert to the new table during the atomic operation of search
577 * and insertion. Searches for duplicates in both the old and new table if
578 * a resize is in progress.
579 *
580 * This lookup function may only be used for fixed key hash table (key_len
581 * parameter set). It will BUG() if used inappropriately.
582 *
583 * It is safe to call this function from atomic context.
584 *
585 * Will trigger an automatic deferred table resizing if the size grows
586 * beyond the watermark indicated by grow_decision() which can be passed
587 * to rhashtable_init().
588 */
589bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
590{
Herbert Xu02fd97c2015-03-20 21:57:00 +1100591 return rhashtable_lookup_insert_fast(ht, obj, ht->p);
Ying Xue7a868d12015-01-12 14:52:22 +0800592}
593EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
594
595/**
596 * rhashtable_lookup_compare_insert - search and insert object to hash table
597 * with compare function
598 * @ht: hash table
599 * @obj: pointer to hash head inside object
600 * @compare: compare function, must return true on match
601 * @arg: argument passed on to compare function
602 *
603 * Locks down the bucket chain in both the old and new table if a resize
604 * is in progress to ensure that writers can't remove from the old table
605 * and can't insert to the new table during the atomic operation of search
606 * and insertion. Searches for duplicates in both the old and new table if
607 * a resize is in progress.
608 *
609 * Lookups may occur in parallel with hashtable mutations and resizing.
610 *
611 * Will trigger an automatic deferred table resizing if the size grows
612 * beyond the watermark indicated by grow_decision() which can be passed
613 * to rhashtable_init().
614 */
615bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
616 struct rhash_head *obj,
617 bool (*compare)(void *, void *),
618 void *arg)
619{
Ying Xuedb304852015-01-07 13:41:54 +0800620 BUG_ON(!ht->p.key_len);
621
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100622 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800623}
Ying Xue7a868d12015-01-12 14:52:22 +0800624EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800625
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100626/**
627 * rhashtable_walk_init - Initialise an iterator
628 * @ht: Table to walk over
629 * @iter: Hash table Iterator
630 *
631 * This function prepares a hash table walk.
632 *
633 * Note that if you restart a walk after rhashtable_walk_stop you
634 * may see the same object twice. Also, you may miss objects if
635 * there are removals in between rhashtable_walk_stop and the next
636 * call to rhashtable_walk_start.
637 *
638 * For a completely stable walk you should construct your own data
639 * structure outside the hash table.
640 *
641 * This function may sleep so you must not call it from interrupt
642 * context or with spin locks held.
643 *
644 * You must call rhashtable_walk_exit if this function returns
645 * successfully.
646 */
647int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
648{
649 iter->ht = ht;
650 iter->p = NULL;
651 iter->slot = 0;
652 iter->skip = 0;
653
654 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
655 if (!iter->walker)
656 return -ENOMEM;
657
658 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100659 iter->walker->tbl = rht_dereference(ht->tbl, ht);
660 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100661 mutex_unlock(&ht->mutex);
662
663 return 0;
664}
665EXPORT_SYMBOL_GPL(rhashtable_walk_init);
666
667/**
668 * rhashtable_walk_exit - Free an iterator
669 * @iter: Hash table Iterator
670 *
671 * This function frees resources allocated by rhashtable_walk_init.
672 */
673void rhashtable_walk_exit(struct rhashtable_iter *iter)
674{
675 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100676 if (iter->walker->tbl)
677 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100678 mutex_unlock(&iter->ht->mutex);
679 kfree(iter->walker);
680}
681EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
682
683/**
684 * rhashtable_walk_start - Start a hash table walk
685 * @iter: Hash table iterator
686 *
687 * Start a hash table walk. Note that we take the RCU lock in all
688 * cases including when we return an error. So you must always call
689 * rhashtable_walk_stop to clean up.
690 *
691 * Returns zero if successful.
692 *
693 * Returns -EAGAIN if resize event occured. Note that the iterator
694 * will rewind back to the beginning and you may use it immediately
695 * by calling rhashtable_walk_next.
696 */
697int rhashtable_walk_start(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100698 __acquires(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100699{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100700 struct rhashtable *ht = iter->ht;
701
702 mutex_lock(&ht->mutex);
703
704 if (iter->walker->tbl)
705 list_del(&iter->walker->list);
706
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100707 rcu_read_lock();
708
Herbert Xueddee5ba2015-03-14 13:57:20 +1100709 mutex_unlock(&ht->mutex);
710
711 if (!iter->walker->tbl) {
712 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100713 return -EAGAIN;
714 }
715
716 return 0;
717}
718EXPORT_SYMBOL_GPL(rhashtable_walk_start);
719
720/**
721 * rhashtable_walk_next - Return the next object and advance the iterator
722 * @iter: Hash table iterator
723 *
724 * Note that you must call rhashtable_walk_stop when you are finished
725 * with the walk.
726 *
727 * Returns the next object or NULL when the end of the table is reached.
728 *
729 * Returns -EAGAIN if resize event occured. Note that the iterator
730 * will rewind back to the beginning and you may continue to use it.
731 */
732void *rhashtable_walk_next(struct rhashtable_iter *iter)
733{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100734 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100735 struct rhashtable *ht = iter->ht;
736 struct rhash_head *p = iter->p;
737 void *obj = NULL;
738
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100739 if (p) {
740 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
741 goto next;
742 }
743
744 for (; iter->slot < tbl->size; iter->slot++) {
745 int skip = iter->skip;
746
747 rht_for_each_rcu(p, tbl, iter->slot) {
748 if (!skip)
749 break;
750 skip--;
751 }
752
753next:
754 if (!rht_is_a_nulls(p)) {
755 iter->skip++;
756 iter->p = p;
757 obj = rht_obj(ht, p);
758 goto out;
759 }
760
761 iter->skip = 0;
762 }
763
Herbert Xuc4db8842015-03-14 13:57:25 +1100764 iter->walker->tbl = rht_dereference_rcu(tbl->future_tbl, ht);
765 if (iter->walker->tbl) {
Herbert Xueddee5ba2015-03-14 13:57:20 +1100766 iter->slot = 0;
767 iter->skip = 0;
768 return ERR_PTR(-EAGAIN);
769 }
770
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100771 iter->p = NULL;
772
773out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100774
775 return obj;
776}
777EXPORT_SYMBOL_GPL(rhashtable_walk_next);
778
779/**
780 * rhashtable_walk_stop - Finish a hash table walk
781 * @iter: Hash table iterator
782 *
783 * Finish a hash table walk.
784 */
785void rhashtable_walk_stop(struct rhashtable_iter *iter)
Thomas Grafdb4374f2015-03-16 10:42:27 +0100786 __releases(RCU)
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100787{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100788 struct rhashtable *ht;
789 struct bucket_table *tbl = iter->walker->tbl;
790
Herbert Xueddee5ba2015-03-14 13:57:20 +1100791 if (!tbl)
Herbert Xu963ecbd2015-03-15 21:12:04 +1100792 goto out;
Herbert Xueddee5ba2015-03-14 13:57:20 +1100793
794 ht = iter->ht;
795
796 mutex_lock(&ht->mutex);
Herbert Xuc4db8842015-03-14 13:57:25 +1100797 if (tbl->rehash < tbl->size)
Herbert Xueddee5ba2015-03-14 13:57:20 +1100798 list_add(&iter->walker->list, &tbl->walkers);
799 else
800 iter->walker->tbl = NULL;
801 mutex_unlock(&ht->mutex);
802
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100803 iter->p = NULL;
Herbert Xu963ecbd2015-03-15 21:12:04 +1100804
805out:
806 rcu_read_unlock();
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100807}
808EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
809
Herbert Xu488fb86e2015-03-20 21:56:59 +1100810static size_t rounded_hashtable_size(const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200811{
Ying Xue94000172014-09-03 09:22:36 +0800812 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
Herbert Xue2e21c12015-03-18 20:01:21 +1100813 (unsigned long)params->min_size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200814}
815
816/**
817 * rhashtable_init - initialize a new hash table
818 * @ht: hash table to be initialized
819 * @params: configuration parameters
820 *
821 * Initializes a new hash table based on the provided configuration
822 * parameters. A table can be configured either with a variable or
823 * fixed length key:
824 *
825 * Configuration Example 1: Fixed length keys
826 * struct test_obj {
827 * int key;
828 * void * my_member;
829 * struct rhash_head node;
830 * };
831 *
832 * struct rhashtable_params params = {
833 * .head_offset = offsetof(struct test_obj, node),
834 * .key_offset = offsetof(struct test_obj, key),
835 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100836 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100837 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200838 * };
839 *
840 * Configuration Example 2: Variable length keys
841 * struct test_obj {
842 * [...]
843 * struct rhash_head node;
844 * };
845 *
846 * u32 my_hash_fn(const void *data, u32 seed)
847 * {
848 * struct test_obj *obj = data;
849 *
850 * return [... hash ...];
851 * }
852 *
853 * struct rhashtable_params params = {
854 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100855 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200856 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200857 * };
858 */
Herbert Xu488fb86e2015-03-20 21:56:59 +1100859int rhashtable_init(struct rhashtable *ht,
860 const struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200861{
862 struct bucket_table *tbl;
863 size_t size;
864
865 size = HASH_DEFAULT_SIZE;
866
Herbert Xu02fd97c2015-03-20 21:57:00 +1100867 if ((!(params->key_len && params->hashfn) && !params->obj_hashfn) ||
868 (params->obj_hashfn && !params->obj_cmpfn))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200869 return -EINVAL;
870
Thomas Graff89bd6f2015-01-02 23:00:21 +0100871 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
872 return -EINVAL;
873
Thomas Graf7e1e7762014-08-02 11:47:44 +0200874 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800875 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200876
Thomas Graf97defe12015-01-02 23:00:20 +0100877 memset(ht, 0, sizeof(*ht));
878 mutex_init(&ht->mutex);
879 memcpy(&ht->p, params, sizeof(*params));
880
Thomas Grafa998f712015-03-19 22:31:13 +0000881 if (params->min_size)
882 ht->p.min_size = roundup_pow_of_two(params->min_size);
883
884 if (params->max_size)
885 ht->p.max_size = rounddown_pow_of_two(params->max_size);
886
Herbert Xu488fb86e2015-03-20 21:56:59 +1100887 ht->p.min_size = max(ht->p.min_size, HASH_MIN_SIZE);
Thomas Grafa998f712015-03-19 22:31:13 +0000888
Thomas Graf97defe12015-01-02 23:00:20 +0100889 if (params->locks_mul)
890 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
891 else
892 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
893
Herbert Xu5269b532015-03-14 13:57:22 +1100894 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200895 if (tbl == NULL)
896 return -ENOMEM;
897
Ying Xue545a1482015-01-07 13:41:57 +0800898 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100899
Thomas Graf7e1e7762014-08-02 11:47:44 +0200900 RCU_INIT_POINTER(ht->tbl, tbl);
901
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100902 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100903
Thomas Graf7e1e7762014-08-02 11:47:44 +0200904 return 0;
905}
906EXPORT_SYMBOL_GPL(rhashtable_init);
907
908/**
909 * rhashtable_destroy - destroy hash table
910 * @ht: the hash table to destroy
911 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200912 * Frees the bucket array. This function is not rcu safe, therefore the caller
913 * has to make sure that no resizing may happen by unpublishing the hashtable
914 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200915 */
Thomas Graf97defe12015-01-02 23:00:20 +0100916void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200917{
Thomas Graf97defe12015-01-02 23:00:20 +0100918 ht->being_destroyed = true;
919
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100920 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800921
Thomas Graf97defe12015-01-02 23:00:20 +0100922 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100923 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100924 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200925}
926EXPORT_SYMBOL_GPL(rhashtable_destroy);