blob: 6ffc793145f3e98eeddee8736db83f8f5b147745 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +01004 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Thomas Graf7e1e7762014-08-02 11:47:44 +02005 * 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>
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
30#define HASH_MIN_SIZE 4UL
Thomas Graf97defe12015-01-02 23:00:20 +010031#define BUCKET_LOCKS_PER_CPU 128UL
32
Thomas Graff89bd6f2015-01-02 23:00:21 +010033/* Base bits plus 1 bit for nulls marker */
34#define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
35
Thomas Graf97defe12015-01-02 23:00:20 +010036enum {
37 RHT_LOCK_NORMAL,
38 RHT_LOCK_NESTED,
Thomas Graf97defe12015-01-02 23:00:20 +010039};
40
41/* The bucket lock is selected based on the hash and protects mutations
42 * on a group of hash buckets.
43 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +010044 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
45 * a single lock always covers both buckets which may both contains
46 * entries which link to the same bucket of the old table during resizing.
47 * This allows to simplify the locking as locking the bucket in both
48 * tables during resize always guarantee protection.
49 *
Thomas Graf97defe12015-01-02 23:00:20 +010050 * IMPORTANT: When holding the bucket lock of both the old and new table
51 * during expansions and shrinking, the old bucket lock must always be
52 * acquired first.
53 */
54static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
55{
56 return &tbl->locks[hash & tbl->locks_mask];
57}
Thomas Graf7e1e7762014-08-02 11:47:44 +020058
Thomas Grafc91eee52014-08-13 16:38:30 +020059static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020060{
61 return (void *) he - ht->p.head_offset;
62}
Thomas Graf7e1e7762014-08-02 11:47:44 +020063
Thomas Graf8d24c0b2015-01-02 23:00:14 +010064static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020065{
Herbert Xuec9f71c2015-03-12 14:49:41 +110066 return (hash >> HASH_RESERVED_SPACE) & (tbl->size - 1);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010067}
68
Herbert Xuaa34a6cb02015-03-11 09:43:48 +110069static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
Herbert Xucffaa9c2015-03-12 14:49:40 +110070 const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +020071{
Herbert Xucffaa9c2015-03-12 14:49:40 +110072 return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len,
Herbert Xuec9f71c2015-03-12 14:49:41 +110073 tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020074}
Thomas Graf7e1e7762014-08-02 11:47:44 +020075
Herbert Xu988dfbd2015-03-10 09:27:55 +110076static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010077 const struct bucket_table *tbl,
78 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020079{
Herbert Xuec9f71c2015-03-12 14:49:41 +110080 const char *ptr = rht_obj(ht, he);
81
82 return likely(ht->p.key_len) ?
83 key_hashfn(ht, tbl, ptr + ht->p.key_offset) :
84 rht_bucket_index(tbl, ht->p.obj_hashfn(ptr, tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020085}
86
Thomas Grafa03eaec2015-02-05 02:03:34 +010087#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010088#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010089
90int lockdep_rht_mutex_is_held(struct rhashtable *ht)
91{
92 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
93}
94EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
95
96int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
97{
98 spinlock_t *lock = bucket_lock(tbl, hash);
99
100 return (debug_locks) ? lockdep_is_held(lock) : 1;
101}
102EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
103#else
104#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +0100105#endif
106
107
Thomas Graf97defe12015-01-02 23:00:20 +0100108static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
109{
110 unsigned int i, size;
111#if defined(CONFIG_PROVE_LOCKING)
112 unsigned int nr_pcpus = 2;
113#else
114 unsigned int nr_pcpus = num_possible_cpus();
115#endif
116
117 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
118 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
119
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100120 /* Never allocate more than 0.5 locks per bucket */
121 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100122
123 if (sizeof(spinlock_t) != 0) {
124#ifdef CONFIG_NUMA
125 if (size * sizeof(spinlock_t) > PAGE_SIZE)
126 tbl->locks = vmalloc(size * sizeof(spinlock_t));
127 else
128#endif
129 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
130 GFP_KERNEL);
131 if (!tbl->locks)
132 return -ENOMEM;
133 for (i = 0; i < size; i++)
134 spin_lock_init(&tbl->locks[i]);
135 }
136 tbl->locks_mask = size - 1;
137
138 return 0;
139}
140
141static void bucket_table_free(const struct bucket_table *tbl)
142{
143 if (tbl)
144 kvfree(tbl->locks);
145
146 kvfree(tbl);
147}
148
149static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
150 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200151{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100152 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200153 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100154 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200155
156 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100157 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
158 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200159 if (tbl == NULL)
160 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200161 if (tbl == NULL)
162 return NULL;
163
164 tbl->size = nbuckets;
165
Thomas Graf97defe12015-01-02 23:00:20 +0100166 if (alloc_bucket_locks(ht, tbl) < 0) {
167 bucket_table_free(tbl);
168 return NULL;
169 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200170
Thomas Graff89bd6f2015-01-02 23:00:21 +0100171 for (i = 0; i < nbuckets; i++)
172 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
173
Thomas Graf97defe12015-01-02 23:00:20 +0100174 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200175}
176
177/**
178 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
179 * @ht: hash table
180 * @new_size: new table size
181 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100182static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200183{
184 /* Expand table when exceeding 75% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800185 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
Daniel Borkmann8331de72015-02-25 16:31:53 +0100186 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200187}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200188
189/**
190 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
191 * @ht: hash table
192 * @new_size: new table size
193 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100194static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200195{
196 /* Shrink table beneath 30% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800197 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
198 (atomic_read(&ht->shift) > ht->p.min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200199}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200200
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100201static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100202{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100203 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
204 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
205 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
206 int err = -ENOENT;
207 struct rhash_head *head, *next, *entry;
208 spinlock_t *new_bucket_lock;
209 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100210
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100211 rht_for_each(entry, old_tbl, old_hash) {
212 err = 0;
213 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100214
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100215 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200216 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100217
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100218 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200219 }
220
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100221 if (err)
222 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100223
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100224 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100225
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100226 new_bucket_lock = bucket_lock(new_tbl, new_hash);
227
Herbert Xu84ed82b2015-03-12 14:47:13 +1100228 spin_lock_nested(new_bucket_lock, RHT_LOCK_NESTED);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100229 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
230 new_tbl, new_hash);
231
232 if (rht_is_a_nulls(head))
233 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
234 else
235 RCU_INIT_POINTER(entry->next, head);
236
237 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
238 spin_unlock(new_bucket_lock);
239
240 rcu_assign_pointer(*pprev, next);
241
242out:
243 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100244}
245
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100246static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100247{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100248 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
249 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100250
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100251 old_bucket_lock = bucket_lock(old_tbl, old_hash);
252
253 spin_lock_bh(old_bucket_lock);
254 while (!rhashtable_rehash_one(ht, old_hash))
255 ;
256 spin_unlock_bh(old_bucket_lock);
257}
258
259static void rhashtable_rehash(struct rhashtable *ht,
260 struct bucket_table *new_tbl)
261{
262 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
263 unsigned old_hash;
264
265 get_random_bytes(&new_tbl->hash_rnd, sizeof(new_tbl->hash_rnd));
266
267 /* Make insertions go into the new, empty table right away. Deletions
268 * and lookups will be attempted in both tables until we synchronize.
269 * The synchronize_rcu() guarantees for the new table to be picked up
270 * so no new additions go into the old table while we relink.
271 */
272 rcu_assign_pointer(ht->future_tbl, new_tbl);
273
274 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
275 rhashtable_rehash_chain(ht, old_hash);
276
277 /* Publish the new table pointer. */
278 rcu_assign_pointer(ht->tbl, new_tbl);
279
280 /* Wait for readers. All new readers will see the new
281 * table, and thus no references to the old table will
282 * remain.
283 */
284 synchronize_rcu();
285
286 bucket_table_free(old_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200287}
288
289/**
290 * rhashtable_expand - Expand hash table while allowing concurrent lookups
291 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200292 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100293 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200294 *
295 * This function may only be called in a context where it is safe to call
296 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
297 *
Thomas Graf97defe12015-01-02 23:00:20 +0100298 * The caller must ensure that no concurrent resizing occurs by holding
299 * ht->mutex.
300 *
301 * It is valid to have concurrent insertions and deletions protected by per
302 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200303 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100304int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200305{
306 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200307
308 ASSERT_RHT_MUTEX(ht);
309
Thomas Graf97defe12015-01-02 23:00:20 +0100310 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200311 if (new_tbl == NULL)
312 return -ENOMEM;
313
Herbert Xu988dfbd2015-03-10 09:27:55 +1100314 new_tbl->hash_rnd = old_tbl->hash_rnd;
315
Ying Xuec0c09bf2015-01-07 13:41:56 +0800316 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200317
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100318 rhashtable_rehash(ht, new_tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100319
Thomas Graf7e1e7762014-08-02 11:47:44 +0200320 return 0;
321}
322EXPORT_SYMBOL_GPL(rhashtable_expand);
323
324/**
325 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
326 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200327 *
328 * This function may only be called in a context where it is safe to call
329 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
330 *
Thomas Graf97defe12015-01-02 23:00:20 +0100331 * The caller must ensure that no concurrent resizing occurs by holding
332 * ht->mutex.
333 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200334 * The caller must ensure that no concurrent table mutations take place.
335 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100336 *
337 * It is valid to have concurrent insertions and deletions protected by per
338 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200339 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100340int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200341{
Thomas Graf97defe12015-01-02 23:00:20 +0100342 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200343
344 ASSERT_RHT_MUTEX(ht);
345
Thomas Graf97defe12015-01-02 23:00:20 +0100346 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
347 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200348 return -ENOMEM;
349
Herbert Xu988dfbd2015-03-10 09:27:55 +1100350 new_tbl->hash_rnd = tbl->hash_rnd;
351
Ying Xuec0c09bf2015-01-07 13:41:56 +0800352 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200353
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100354 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200355
356 return 0;
357}
358EXPORT_SYMBOL_GPL(rhashtable_shrink);
359
Thomas Graf97defe12015-01-02 23:00:20 +0100360static void rht_deferred_worker(struct work_struct *work)
361{
362 struct rhashtable *ht;
363 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100364 struct rhashtable_walker *walker;
Thomas Graf97defe12015-01-02 23:00:20 +0100365
Ying Xue57699a42015-01-16 11:13:09 +0800366 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100367 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100368 if (ht->being_destroyed)
369 goto unlock;
370
Thomas Graf97defe12015-01-02 23:00:20 +0100371 tbl = rht_dereference(ht->tbl, ht);
372
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100373 list_for_each_entry(walker, &ht->walkers, list)
374 walker->resize = true;
375
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100376 if (rht_grow_above_75(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100377 rhashtable_expand(ht);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100378 else if (rht_shrink_below_30(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100379 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100380unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100381 mutex_unlock(&ht->mutex);
382}
383
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100384static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
385 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800386{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100387 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000388 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100389 bool no_resize_running;
390 unsigned hash;
391 bool success = true;
392
393 rcu_read_lock();
394
395 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100396 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100397
398 spin_lock_bh(bucket_lock(old_tbl, hash));
399
400 /* Because we have already taken the bucket lock in old_tbl,
401 * if we find that future_tbl is not yet visible then that
402 * guarantees all other insertions of the same entry will
403 * also grab the bucket lock in old_tbl because until the
404 * rehash completes ht->tbl won't be changed.
405 */
406 tbl = rht_dereference_rcu(ht->future_tbl, ht);
407 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100408 hash = head_hashfn(ht, tbl, obj);
Herbert Xu84ed82b2015-03-12 14:47:13 +1100409 spin_lock_nested(bucket_lock(tbl, hash), RHT_LOCK_NESTED);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100410 }
411
412 if (compare &&
413 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
414 compare, arg)) {
415 success = false;
416 goto exit;
417 }
418
419 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000420
Thomas Graf020219a2015-02-06 16:08:43 +0000421 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800422
423 if (rht_is_a_nulls(head))
424 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
425 else
426 RCU_INIT_POINTER(obj->next, head);
427
428 rcu_assign_pointer(tbl->buckets[hash], obj);
429
430 atomic_inc(&ht->nelems);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100431 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
432 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100433
434exit:
435 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100436 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100437 spin_unlock(bucket_lock(tbl, hash));
438 }
439
Herbert Xueca84932015-03-12 14:49:39 +1100440 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100441 spin_unlock_bh(bucket_lock(old_tbl, hash));
442
443 rcu_read_unlock();
444
445 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800446}
447
Thomas Graf7e1e7762014-08-02 11:47:44 +0200448/**
Ying Xuedb304852015-01-07 13:41:54 +0800449 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200450 * @ht: hash table
451 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200452 *
Thomas Graf97defe12015-01-02 23:00:20 +0100453 * Will take a per bucket spinlock to protect against mutual mutations
454 * on the same bucket. Multiple insertions may occur in parallel unless
455 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200456 *
Thomas Graf97defe12015-01-02 23:00:20 +0100457 * It is safe to call this function from atomic context.
458 *
459 * Will trigger an automatic deferred table resizing if the size grows
460 * beyond the watermark indicated by grow_decision() which can be passed
461 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200462 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100463void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200464{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100465 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200466}
467EXPORT_SYMBOL_GPL(rhashtable_insert);
468
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100469static bool __rhashtable_remove(struct rhashtable *ht,
470 struct bucket_table *tbl,
471 struct rhash_head *obj)
472{
473 struct rhash_head __rcu **pprev;
474 struct rhash_head *he;
475 spinlock_t * lock;
476 unsigned hash;
477 bool ret = false;
478
Herbert Xueca84932015-03-12 14:49:39 +1100479 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100480 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100481
482 spin_lock_bh(lock);
483
484 pprev = &tbl->buckets[hash];
485 rht_for_each(he, tbl, hash) {
486 if (he != obj) {
487 pprev = &he->next;
488 continue;
489 }
490
491 rcu_assign_pointer(*pprev, obj->next);
492 ret = true;
493 break;
494 }
495
496 spin_unlock_bh(lock);
497
498 return ret;
499}
500
Thomas Graf7e1e7762014-08-02 11:47:44 +0200501/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200502 * rhashtable_remove - remove object from hash table
503 * @ht: hash table
504 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200505 *
506 * Since the hash chain is single linked, the removal operation needs to
507 * walk the bucket chain upon removal. The removal operation is thus
508 * considerable slow if the hash table is not correctly sized.
509 *
Ying Xuedb304852015-01-07 13:41:54 +0800510 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200511 * shrink_decision function specified at rhashtable_init() returns true.
512 *
513 * The caller must ensure that no concurrent table mutations occur. It is
514 * however valid to have concurrent lookups if they are RCU protected.
515 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100516bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200517{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100518 struct bucket_table *tbl, *old_tbl;
519 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200520
Thomas Graf97defe12015-01-02 23:00:20 +0100521 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100522
Thomas Graf020219a2015-02-06 16:08:43 +0000523 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100524 ret = __rhashtable_remove(ht, old_tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200525
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100526 /* Because we have already taken (and released) the bucket
527 * lock in old_tbl, if we find that future_tbl is not yet
528 * visible then that guarantees the entry to still be in
529 * old_tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000530 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100531 tbl = rht_dereference_rcu(ht->future_tbl, ht);
532 if (!ret && old_tbl != tbl)
533 ret = __rhashtable_remove(ht, tbl, obj);
Thomas Graffe6a0432015-01-21 11:54:01 +0000534
535 if (ret) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100536 bool no_resize_running = tbl == old_tbl;
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100537
Thomas Graffe6a0432015-01-21 11:54:01 +0000538 atomic_dec(&ht->nelems);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100539 if (no_resize_running && rht_shrink_below_30(ht, tbl->size))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100540 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000541 }
542
Thomas Graf97defe12015-01-02 23:00:20 +0100543 rcu_read_unlock();
544
Thomas Graffe6a0432015-01-21 11:54:01 +0000545 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200546}
547EXPORT_SYMBOL_GPL(rhashtable_remove);
548
Ying Xueefb975a62015-01-07 13:41:52 +0800549struct rhashtable_compare_arg {
550 struct rhashtable *ht;
551 const void *key;
552};
553
554static bool rhashtable_compare(void *ptr, void *arg)
555{
556 struct rhashtable_compare_arg *x = arg;
557 struct rhashtable *ht = x->ht;
558
559 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
560}
561
Thomas Graf7e1e7762014-08-02 11:47:44 +0200562/**
563 * rhashtable_lookup - lookup key in hash table
564 * @ht: hash table
565 * @key: pointer to key
566 *
567 * Computes the hash value for the key and traverses the bucket chain looking
568 * for a entry with an identical key. The first matching entry is returned.
569 *
570 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800571 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200572 *
Thomas Graf97defe12015-01-02 23:00:20 +0100573 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200574 */
Thomas Graf97defe12015-01-02 23:00:20 +0100575void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200576{
Ying Xueefb975a62015-01-07 13:41:52 +0800577 struct rhashtable_compare_arg arg = {
578 .ht = ht,
579 .key = key,
580 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200581
582 BUG_ON(!ht->p.key_len);
583
Ying Xueefb975a62015-01-07 13:41:52 +0800584 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200585}
586EXPORT_SYMBOL_GPL(rhashtable_lookup);
587
588/**
589 * rhashtable_lookup_compare - search hash table with compare function
590 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100591 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200592 * @compare: compare function, must return true on match
593 * @arg: argument passed on to compare function
594 *
595 * Traverses the bucket chain behind the provided hash value and calls the
596 * specified compare function for each entry.
597 *
Thomas Graf97defe12015-01-02 23:00:20 +0100598 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200599 *
600 * Returns the first entry on which the compare function returned true.
601 */
Thomas Graf97defe12015-01-02 23:00:20 +0100602void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200603 bool (*compare)(void *, void *), void *arg)
604{
Thomas Graf97defe12015-01-02 23:00:20 +0100605 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200606 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100607 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200608
Thomas Graf97defe12015-01-02 23:00:20 +0100609 rcu_read_lock();
610
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100611 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xucffaa9c2015-03-12 14:49:40 +1100612 hash = key_hashfn(ht, tbl, key);
Thomas Graf97defe12015-01-02 23:00:20 +0100613restart:
Herbert Xu8d2b1872015-03-12 14:49:38 +1100614 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200615 if (!compare(rht_obj(ht, he), arg))
616 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100617 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100618 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200619 }
620
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100621 old_tbl = tbl;
622 tbl = rht_dereference_rcu(ht->future_tbl, ht);
623 if (unlikely(tbl != old_tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100624 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100625 rcu_read_unlock();
626
Thomas Graf7e1e7762014-08-02 11:47:44 +0200627 return NULL;
628}
629EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
630
Ying Xuedb304852015-01-07 13:41:54 +0800631/**
632 * rhashtable_lookup_insert - lookup and insert object into hash table
633 * @ht: hash table
634 * @obj: pointer to hash head inside object
635 *
636 * Locks down the bucket chain in both the old and new table if a resize
637 * is in progress to ensure that writers can't remove from the old table
638 * and can't insert to the new table during the atomic operation of search
639 * and insertion. Searches for duplicates in both the old and new table if
640 * a resize is in progress.
641 *
642 * This lookup function may only be used for fixed key hash table (key_len
643 * parameter set). It will BUG() if used inappropriately.
644 *
645 * It is safe to call this function from atomic context.
646 *
647 * Will trigger an automatic deferred table resizing if the size grows
648 * beyond the watermark indicated by grow_decision() which can be passed
649 * to rhashtable_init().
650 */
651bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
652{
Ying Xue7a868d12015-01-12 14:52:22 +0800653 struct rhashtable_compare_arg arg = {
654 .ht = ht,
655 .key = rht_obj(ht, obj) + ht->p.key_offset,
656 };
657
658 BUG_ON(!ht->p.key_len);
659
660 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
661 &arg);
662}
663EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
664
665/**
666 * rhashtable_lookup_compare_insert - search and insert object to hash table
667 * with compare function
668 * @ht: hash table
669 * @obj: pointer to hash head inside object
670 * @compare: compare function, must return true on match
671 * @arg: argument passed on to compare function
672 *
673 * Locks down the bucket chain in both the old and new table if a resize
674 * is in progress to ensure that writers can't remove from the old table
675 * and can't insert to the new table during the atomic operation of search
676 * and insertion. Searches for duplicates in both the old and new table if
677 * a resize is in progress.
678 *
679 * Lookups may occur in parallel with hashtable mutations and resizing.
680 *
681 * Will trigger an automatic deferred table resizing if the size grows
682 * beyond the watermark indicated by grow_decision() which can be passed
683 * to rhashtable_init().
684 */
685bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
686 struct rhash_head *obj,
687 bool (*compare)(void *, void *),
688 void *arg)
689{
Ying Xuedb304852015-01-07 13:41:54 +0800690 BUG_ON(!ht->p.key_len);
691
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100692 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800693}
Ying Xue7a868d12015-01-12 14:52:22 +0800694EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800695
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100696/**
697 * rhashtable_walk_init - Initialise an iterator
698 * @ht: Table to walk over
699 * @iter: Hash table Iterator
700 *
701 * This function prepares a hash table walk.
702 *
703 * Note that if you restart a walk after rhashtable_walk_stop you
704 * may see the same object twice. Also, you may miss objects if
705 * there are removals in between rhashtable_walk_stop and the next
706 * call to rhashtable_walk_start.
707 *
708 * For a completely stable walk you should construct your own data
709 * structure outside the hash table.
710 *
711 * This function may sleep so you must not call it from interrupt
712 * context or with spin locks held.
713 *
714 * You must call rhashtable_walk_exit if this function returns
715 * successfully.
716 */
717int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
718{
719 iter->ht = ht;
720 iter->p = NULL;
721 iter->slot = 0;
722 iter->skip = 0;
723
724 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
725 if (!iter->walker)
726 return -ENOMEM;
727
Sasha Levin71bb0012015-02-23 04:35:06 -0500728 INIT_LIST_HEAD(&iter->walker->list);
729 iter->walker->resize = false;
730
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100731 mutex_lock(&ht->mutex);
732 list_add(&iter->walker->list, &ht->walkers);
733 mutex_unlock(&ht->mutex);
734
735 return 0;
736}
737EXPORT_SYMBOL_GPL(rhashtable_walk_init);
738
739/**
740 * rhashtable_walk_exit - Free an iterator
741 * @iter: Hash table Iterator
742 *
743 * This function frees resources allocated by rhashtable_walk_init.
744 */
745void rhashtable_walk_exit(struct rhashtable_iter *iter)
746{
747 mutex_lock(&iter->ht->mutex);
748 list_del(&iter->walker->list);
749 mutex_unlock(&iter->ht->mutex);
750 kfree(iter->walker);
751}
752EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
753
754/**
755 * rhashtable_walk_start - Start a hash table walk
756 * @iter: Hash table iterator
757 *
758 * Start a hash table walk. Note that we take the RCU lock in all
759 * cases including when we return an error. So you must always call
760 * rhashtable_walk_stop to clean up.
761 *
762 * Returns zero if successful.
763 *
764 * Returns -EAGAIN if resize event occured. Note that the iterator
765 * will rewind back to the beginning and you may use it immediately
766 * by calling rhashtable_walk_next.
767 */
768int rhashtable_walk_start(struct rhashtable_iter *iter)
769{
770 rcu_read_lock();
771
772 if (iter->walker->resize) {
773 iter->slot = 0;
774 iter->skip = 0;
775 iter->walker->resize = false;
776 return -EAGAIN;
777 }
778
779 return 0;
780}
781EXPORT_SYMBOL_GPL(rhashtable_walk_start);
782
783/**
784 * rhashtable_walk_next - Return the next object and advance the iterator
785 * @iter: Hash table iterator
786 *
787 * Note that you must call rhashtable_walk_stop when you are finished
788 * with the walk.
789 *
790 * Returns the next object or NULL when the end of the table is reached.
791 *
792 * Returns -EAGAIN if resize event occured. Note that the iterator
793 * will rewind back to the beginning and you may continue to use it.
794 */
795void *rhashtable_walk_next(struct rhashtable_iter *iter)
796{
797 const struct bucket_table *tbl;
798 struct rhashtable *ht = iter->ht;
799 struct rhash_head *p = iter->p;
800 void *obj = NULL;
801
802 tbl = rht_dereference_rcu(ht->tbl, ht);
803
804 if (p) {
805 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
806 goto next;
807 }
808
809 for (; iter->slot < tbl->size; iter->slot++) {
810 int skip = iter->skip;
811
812 rht_for_each_rcu(p, tbl, iter->slot) {
813 if (!skip)
814 break;
815 skip--;
816 }
817
818next:
819 if (!rht_is_a_nulls(p)) {
820 iter->skip++;
821 iter->p = p;
822 obj = rht_obj(ht, p);
823 goto out;
824 }
825
826 iter->skip = 0;
827 }
828
829 iter->p = NULL;
830
831out:
832 if (iter->walker->resize) {
833 iter->p = NULL;
834 iter->slot = 0;
835 iter->skip = 0;
836 iter->walker->resize = false;
837 return ERR_PTR(-EAGAIN);
838 }
839
840 return obj;
841}
842EXPORT_SYMBOL_GPL(rhashtable_walk_next);
843
844/**
845 * rhashtable_walk_stop - Finish a hash table walk
846 * @iter: Hash table iterator
847 *
848 * Finish a hash table walk.
849 */
850void rhashtable_walk_stop(struct rhashtable_iter *iter)
851{
852 rcu_read_unlock();
853 iter->p = NULL;
854}
855EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
856
Ying Xue94000172014-09-03 09:22:36 +0800857static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200858{
Ying Xue94000172014-09-03 09:22:36 +0800859 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
860 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200861}
862
863/**
864 * rhashtable_init - initialize a new hash table
865 * @ht: hash table to be initialized
866 * @params: configuration parameters
867 *
868 * Initializes a new hash table based on the provided configuration
869 * parameters. A table can be configured either with a variable or
870 * fixed length key:
871 *
872 * Configuration Example 1: Fixed length keys
873 * struct test_obj {
874 * int key;
875 * void * my_member;
876 * struct rhash_head node;
877 * };
878 *
879 * struct rhashtable_params params = {
880 * .head_offset = offsetof(struct test_obj, node),
881 * .key_offset = offsetof(struct test_obj, key),
882 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100883 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100884 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200885 * };
886 *
887 * Configuration Example 2: Variable length keys
888 * struct test_obj {
889 * [...]
890 * struct rhash_head node;
891 * };
892 *
893 * u32 my_hash_fn(const void *data, u32 seed)
894 * {
895 * struct test_obj *obj = data;
896 *
897 * return [... hash ...];
898 * }
899 *
900 * struct rhashtable_params params = {
901 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100902 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200903 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200904 * };
905 */
906int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
907{
908 struct bucket_table *tbl;
909 size_t size;
910
911 size = HASH_DEFAULT_SIZE;
912
913 if ((params->key_len && !params->hashfn) ||
914 (!params->key_len && !params->obj_hashfn))
915 return -EINVAL;
916
Thomas Graff89bd6f2015-01-02 23:00:21 +0100917 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
918 return -EINVAL;
919
Ying Xue94000172014-09-03 09:22:36 +0800920 params->min_shift = max_t(size_t, params->min_shift,
921 ilog2(HASH_MIN_SIZE));
922
Thomas Graf7e1e7762014-08-02 11:47:44 +0200923 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800924 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200925
Thomas Graf97defe12015-01-02 23:00:20 +0100926 memset(ht, 0, sizeof(*ht));
927 mutex_init(&ht->mutex);
928 memcpy(&ht->p, params, sizeof(*params));
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100929 INIT_LIST_HEAD(&ht->walkers);
Thomas Graf97defe12015-01-02 23:00:20 +0100930
931 if (params->locks_mul)
932 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
933 else
934 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
935
936 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200937 if (tbl == NULL)
938 return -ENOMEM;
939
Herbert Xu988dfbd2015-03-10 09:27:55 +1100940 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
941
Ying Xue545a1482015-01-07 13:41:57 +0800942 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800943 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200944 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100945 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200946
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100947 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100948
Thomas Graf7e1e7762014-08-02 11:47:44 +0200949 return 0;
950}
951EXPORT_SYMBOL_GPL(rhashtable_init);
952
953/**
954 * rhashtable_destroy - destroy hash table
955 * @ht: the hash table to destroy
956 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200957 * Frees the bucket array. This function is not rcu safe, therefore the caller
958 * has to make sure that no resizing may happen by unpublishing the hashtable
959 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200960 */
Thomas Graf97defe12015-01-02 23:00:20 +0100961void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200962{
Thomas Graf97defe12015-01-02 23:00:20 +0100963 ht->being_destroyed = true;
964
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100965 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800966
Thomas Graf97defe12015-01-02 23:00:20 +0100967 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100968 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100969 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200970}
971EXPORT_SYMBOL_GPL(rhashtable_destroy);