blob: 68210cc2bab8e53c35edd1a791378935d83c4808 [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
Herbert Xu9497df82015-03-12 22:07:49 +1100274 /* Ensure the new table is visible to readers. */
275 smp_wmb();
276
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100277 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
278 rhashtable_rehash_chain(ht, old_hash);
279
280 /* Publish the new table pointer. */
281 rcu_assign_pointer(ht->tbl, new_tbl);
282
283 /* Wait for readers. All new readers will see the new
284 * table, and thus no references to the old table will
285 * remain.
286 */
287 synchronize_rcu();
288
289 bucket_table_free(old_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200290}
291
292/**
293 * rhashtable_expand - Expand hash table while allowing concurrent lookups
294 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200295 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100296 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200297 *
298 * This function may only be called in a context where it is safe to call
299 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
300 *
Thomas Graf97defe12015-01-02 23:00:20 +0100301 * The caller must ensure that no concurrent resizing occurs by holding
302 * ht->mutex.
303 *
304 * It is valid to have concurrent insertions and deletions protected by per
305 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200306 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100307int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200308{
309 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200310
311 ASSERT_RHT_MUTEX(ht);
312
Thomas Graf97defe12015-01-02 23:00:20 +0100313 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200314 if (new_tbl == NULL)
315 return -ENOMEM;
316
Herbert Xu988dfbd2015-03-10 09:27:55 +1100317 new_tbl->hash_rnd = old_tbl->hash_rnd;
318
Ying Xuec0c09bf2015-01-07 13:41:56 +0800319 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200320
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100321 rhashtable_rehash(ht, new_tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100322
Thomas Graf7e1e7762014-08-02 11:47:44 +0200323 return 0;
324}
325EXPORT_SYMBOL_GPL(rhashtable_expand);
326
327/**
328 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
329 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200330 *
331 * This function may only be called in a context where it is safe to call
332 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
333 *
Thomas Graf97defe12015-01-02 23:00:20 +0100334 * The caller must ensure that no concurrent resizing occurs by holding
335 * ht->mutex.
336 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200337 * The caller must ensure that no concurrent table mutations take place.
338 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100339 *
340 * It is valid to have concurrent insertions and deletions protected by per
341 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200342 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100343int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200344{
Thomas Graf97defe12015-01-02 23:00:20 +0100345 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200346
347 ASSERT_RHT_MUTEX(ht);
348
Thomas Graf97defe12015-01-02 23:00:20 +0100349 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
350 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200351 return -ENOMEM;
352
Herbert Xu988dfbd2015-03-10 09:27:55 +1100353 new_tbl->hash_rnd = tbl->hash_rnd;
354
Ying Xuec0c09bf2015-01-07 13:41:56 +0800355 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200356
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100357 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200358
359 return 0;
360}
361EXPORT_SYMBOL_GPL(rhashtable_shrink);
362
Thomas Graf97defe12015-01-02 23:00:20 +0100363static void rht_deferred_worker(struct work_struct *work)
364{
365 struct rhashtable *ht;
366 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100367 struct rhashtable_walker *walker;
Thomas Graf97defe12015-01-02 23:00:20 +0100368
Ying Xue57699a42015-01-16 11:13:09 +0800369 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100370 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100371 if (ht->being_destroyed)
372 goto unlock;
373
Thomas Graf97defe12015-01-02 23:00:20 +0100374 tbl = rht_dereference(ht->tbl, ht);
375
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100376 list_for_each_entry(walker, &ht->walkers, list)
377 walker->resize = true;
378
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100379 if (rht_grow_above_75(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100380 rhashtable_expand(ht);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100381 else if (rht_shrink_below_30(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100382 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100383unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100384 mutex_unlock(&ht->mutex);
385}
386
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100387static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
388 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800389{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100390 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000391 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100392 bool no_resize_running;
393 unsigned hash;
394 bool success = true;
395
396 rcu_read_lock();
397
398 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100399 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100400
401 spin_lock_bh(bucket_lock(old_tbl, hash));
402
403 /* Because we have already taken the bucket lock in old_tbl,
404 * if we find that future_tbl is not yet visible then that
405 * guarantees all other insertions of the same entry will
406 * also grab the bucket lock in old_tbl because until the
407 * rehash completes ht->tbl won't be changed.
408 */
409 tbl = rht_dereference_rcu(ht->future_tbl, ht);
410 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100411 hash = head_hashfn(ht, tbl, obj);
Herbert Xu84ed82b2015-03-12 14:47:13 +1100412 spin_lock_nested(bucket_lock(tbl, hash), RHT_LOCK_NESTED);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100413 }
414
415 if (compare &&
416 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
417 compare, arg)) {
418 success = false;
419 goto exit;
420 }
421
422 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000423
Thomas Graf020219a2015-02-06 16:08:43 +0000424 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800425
426 if (rht_is_a_nulls(head))
427 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
428 else
429 RCU_INIT_POINTER(obj->next, head);
430
431 rcu_assign_pointer(tbl->buckets[hash], obj);
432
433 atomic_inc(&ht->nelems);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100434 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
435 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100436
437exit:
438 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100439 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100440 spin_unlock(bucket_lock(tbl, hash));
441 }
442
Herbert Xueca84932015-03-12 14:49:39 +1100443 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100444 spin_unlock_bh(bucket_lock(old_tbl, hash));
445
446 rcu_read_unlock();
447
448 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800449}
450
Thomas Graf7e1e7762014-08-02 11:47:44 +0200451/**
Ying Xuedb304852015-01-07 13:41:54 +0800452 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200453 * @ht: hash table
454 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200455 *
Thomas Graf97defe12015-01-02 23:00:20 +0100456 * Will take a per bucket spinlock to protect against mutual mutations
457 * on the same bucket. Multiple insertions may occur in parallel unless
458 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200459 *
Thomas Graf97defe12015-01-02 23:00:20 +0100460 * It is safe to call this function from atomic context.
461 *
462 * Will trigger an automatic deferred table resizing if the size grows
463 * beyond the watermark indicated by grow_decision() which can be passed
464 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200465 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100466void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200467{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100468 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200469}
470EXPORT_SYMBOL_GPL(rhashtable_insert);
471
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100472static bool __rhashtable_remove(struct rhashtable *ht,
473 struct bucket_table *tbl,
474 struct rhash_head *obj)
475{
476 struct rhash_head __rcu **pprev;
477 struct rhash_head *he;
478 spinlock_t * lock;
479 unsigned hash;
480 bool ret = false;
481
Herbert Xueca84932015-03-12 14:49:39 +1100482 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100483 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100484
485 spin_lock_bh(lock);
486
487 pprev = &tbl->buckets[hash];
488 rht_for_each(he, tbl, hash) {
489 if (he != obj) {
490 pprev = &he->next;
491 continue;
492 }
493
494 rcu_assign_pointer(*pprev, obj->next);
495 ret = true;
496 break;
497 }
498
499 spin_unlock_bh(lock);
500
501 return ret;
502}
503
Thomas Graf7e1e7762014-08-02 11:47:44 +0200504/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200505 * rhashtable_remove - remove object from hash table
506 * @ht: hash table
507 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200508 *
509 * Since the hash chain is single linked, the removal operation needs to
510 * walk the bucket chain upon removal. The removal operation is thus
511 * considerable slow if the hash table is not correctly sized.
512 *
Ying Xuedb304852015-01-07 13:41:54 +0800513 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200514 * shrink_decision function specified at rhashtable_init() returns true.
515 *
516 * The caller must ensure that no concurrent table mutations occur. It is
517 * however valid to have concurrent lookups if they are RCU protected.
518 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100519bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200520{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100521 struct bucket_table *tbl, *old_tbl;
522 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200523
Thomas Graf97defe12015-01-02 23:00:20 +0100524 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100525
Thomas Graf020219a2015-02-06 16:08:43 +0000526 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100527 ret = __rhashtable_remove(ht, old_tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200528
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100529 /* Because we have already taken (and released) the bucket
530 * lock in old_tbl, if we find that future_tbl is not yet
531 * visible then that guarantees the entry to still be in
532 * old_tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000533 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100534 tbl = rht_dereference_rcu(ht->future_tbl, ht);
535 if (!ret && old_tbl != tbl)
536 ret = __rhashtable_remove(ht, tbl, obj);
Thomas Graffe6a0432015-01-21 11:54:01 +0000537
538 if (ret) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100539 bool no_resize_running = tbl == old_tbl;
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100540
Thomas Graffe6a0432015-01-21 11:54:01 +0000541 atomic_dec(&ht->nelems);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100542 if (no_resize_running && rht_shrink_below_30(ht, tbl->size))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100543 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000544 }
545
Thomas Graf97defe12015-01-02 23:00:20 +0100546 rcu_read_unlock();
547
Thomas Graffe6a0432015-01-21 11:54:01 +0000548 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200549}
550EXPORT_SYMBOL_GPL(rhashtable_remove);
551
Ying Xueefb975a62015-01-07 13:41:52 +0800552struct rhashtable_compare_arg {
553 struct rhashtable *ht;
554 const void *key;
555};
556
557static bool rhashtable_compare(void *ptr, void *arg)
558{
559 struct rhashtable_compare_arg *x = arg;
560 struct rhashtable *ht = x->ht;
561
562 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
563}
564
Thomas Graf7e1e7762014-08-02 11:47:44 +0200565/**
566 * rhashtable_lookup - lookup key in hash table
567 * @ht: hash table
568 * @key: pointer to key
569 *
570 * Computes the hash value for the key and traverses the bucket chain looking
571 * for a entry with an identical key. The first matching entry is returned.
572 *
573 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800574 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200575 *
Thomas Graf97defe12015-01-02 23:00:20 +0100576 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200577 */
Thomas Graf97defe12015-01-02 23:00:20 +0100578void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200579{
Ying Xueefb975a62015-01-07 13:41:52 +0800580 struct rhashtable_compare_arg arg = {
581 .ht = ht,
582 .key = key,
583 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200584
585 BUG_ON(!ht->p.key_len);
586
Ying Xueefb975a62015-01-07 13:41:52 +0800587 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200588}
589EXPORT_SYMBOL_GPL(rhashtable_lookup);
590
591/**
592 * rhashtable_lookup_compare - search hash table with compare function
593 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100594 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200595 * @compare: compare function, must return true on match
596 * @arg: argument passed on to compare function
597 *
598 * Traverses the bucket chain behind the provided hash value and calls the
599 * specified compare function for each entry.
600 *
Thomas Graf97defe12015-01-02 23:00:20 +0100601 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200602 *
603 * Returns the first entry on which the compare function returned true.
604 */
Thomas Graf97defe12015-01-02 23:00:20 +0100605void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200606 bool (*compare)(void *, void *), void *arg)
607{
Thomas Graf97defe12015-01-02 23:00:20 +0100608 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200609 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100610 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200611
Thomas Graf97defe12015-01-02 23:00:20 +0100612 rcu_read_lock();
613
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100614 tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xucffaa9c2015-03-12 14:49:40 +1100615 hash = key_hashfn(ht, tbl, key);
Thomas Graf97defe12015-01-02 23:00:20 +0100616restart:
Herbert Xu8d2b1872015-03-12 14:49:38 +1100617 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200618 if (!compare(rht_obj(ht, he), arg))
619 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100620 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100621 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200622 }
623
Herbert Xu9497df82015-03-12 22:07:49 +1100624 /* Ensure we see any new tables. */
625 smp_rmb();
626
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100627 old_tbl = tbl;
628 tbl = rht_dereference_rcu(ht->future_tbl, ht);
629 if (unlikely(tbl != old_tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100630 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100631 rcu_read_unlock();
632
Thomas Graf7e1e7762014-08-02 11:47:44 +0200633 return NULL;
634}
635EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
636
Ying Xuedb304852015-01-07 13:41:54 +0800637/**
638 * rhashtable_lookup_insert - lookup and insert object into hash table
639 * @ht: hash table
640 * @obj: pointer to hash head inside object
641 *
642 * Locks down the bucket chain in both the old and new table if a resize
643 * is in progress to ensure that writers can't remove from the old table
644 * and can't insert to the new table during the atomic operation of search
645 * and insertion. Searches for duplicates in both the old and new table if
646 * a resize is in progress.
647 *
648 * This lookup function may only be used for fixed key hash table (key_len
649 * parameter set). It will BUG() if used inappropriately.
650 *
651 * It is safe to call this function from atomic context.
652 *
653 * Will trigger an automatic deferred table resizing if the size grows
654 * beyond the watermark indicated by grow_decision() which can be passed
655 * to rhashtable_init().
656 */
657bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
658{
Ying Xue7a868d12015-01-12 14:52:22 +0800659 struct rhashtable_compare_arg arg = {
660 .ht = ht,
661 .key = rht_obj(ht, obj) + ht->p.key_offset,
662 };
663
664 BUG_ON(!ht->p.key_len);
665
666 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
667 &arg);
668}
669EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
670
671/**
672 * rhashtable_lookup_compare_insert - search and insert object to hash table
673 * with compare function
674 * @ht: hash table
675 * @obj: pointer to hash head inside object
676 * @compare: compare function, must return true on match
677 * @arg: argument passed on to compare function
678 *
679 * Locks down the bucket chain in both the old and new table if a resize
680 * is in progress to ensure that writers can't remove from the old table
681 * and can't insert to the new table during the atomic operation of search
682 * and insertion. Searches for duplicates in both the old and new table if
683 * a resize is in progress.
684 *
685 * Lookups may occur in parallel with hashtable mutations and resizing.
686 *
687 * Will trigger an automatic deferred table resizing if the size grows
688 * beyond the watermark indicated by grow_decision() which can be passed
689 * to rhashtable_init().
690 */
691bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
692 struct rhash_head *obj,
693 bool (*compare)(void *, void *),
694 void *arg)
695{
Ying Xuedb304852015-01-07 13:41:54 +0800696 BUG_ON(!ht->p.key_len);
697
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100698 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800699}
Ying Xue7a868d12015-01-12 14:52:22 +0800700EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800701
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100702/**
703 * rhashtable_walk_init - Initialise an iterator
704 * @ht: Table to walk over
705 * @iter: Hash table Iterator
706 *
707 * This function prepares a hash table walk.
708 *
709 * Note that if you restart a walk after rhashtable_walk_stop you
710 * may see the same object twice. Also, you may miss objects if
711 * there are removals in between rhashtable_walk_stop and the next
712 * call to rhashtable_walk_start.
713 *
714 * For a completely stable walk you should construct your own data
715 * structure outside the hash table.
716 *
717 * This function may sleep so you must not call it from interrupt
718 * context or with spin locks held.
719 *
720 * You must call rhashtable_walk_exit if this function returns
721 * successfully.
722 */
723int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
724{
725 iter->ht = ht;
726 iter->p = NULL;
727 iter->slot = 0;
728 iter->skip = 0;
729
730 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
731 if (!iter->walker)
732 return -ENOMEM;
733
Sasha Levin71bb0012015-02-23 04:35:06 -0500734 INIT_LIST_HEAD(&iter->walker->list);
735 iter->walker->resize = false;
736
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100737 mutex_lock(&ht->mutex);
738 list_add(&iter->walker->list, &ht->walkers);
739 mutex_unlock(&ht->mutex);
740
741 return 0;
742}
743EXPORT_SYMBOL_GPL(rhashtable_walk_init);
744
745/**
746 * rhashtable_walk_exit - Free an iterator
747 * @iter: Hash table Iterator
748 *
749 * This function frees resources allocated by rhashtable_walk_init.
750 */
751void rhashtable_walk_exit(struct rhashtable_iter *iter)
752{
753 mutex_lock(&iter->ht->mutex);
754 list_del(&iter->walker->list);
755 mutex_unlock(&iter->ht->mutex);
756 kfree(iter->walker);
757}
758EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
759
760/**
761 * rhashtable_walk_start - Start a hash table walk
762 * @iter: Hash table iterator
763 *
764 * Start a hash table walk. Note that we take the RCU lock in all
765 * cases including when we return an error. So you must always call
766 * rhashtable_walk_stop to clean up.
767 *
768 * Returns zero if successful.
769 *
770 * Returns -EAGAIN if resize event occured. Note that the iterator
771 * will rewind back to the beginning and you may use it immediately
772 * by calling rhashtable_walk_next.
773 */
774int rhashtable_walk_start(struct rhashtable_iter *iter)
775{
776 rcu_read_lock();
777
778 if (iter->walker->resize) {
779 iter->slot = 0;
780 iter->skip = 0;
781 iter->walker->resize = false;
782 return -EAGAIN;
783 }
784
785 return 0;
786}
787EXPORT_SYMBOL_GPL(rhashtable_walk_start);
788
789/**
790 * rhashtable_walk_next - Return the next object and advance the iterator
791 * @iter: Hash table iterator
792 *
793 * Note that you must call rhashtable_walk_stop when you are finished
794 * with the walk.
795 *
796 * Returns the next object or NULL when the end of the table is reached.
797 *
798 * Returns -EAGAIN if resize event occured. Note that the iterator
799 * will rewind back to the beginning and you may continue to use it.
800 */
801void *rhashtable_walk_next(struct rhashtable_iter *iter)
802{
803 const struct bucket_table *tbl;
804 struct rhashtable *ht = iter->ht;
805 struct rhash_head *p = iter->p;
806 void *obj = NULL;
807
808 tbl = rht_dereference_rcu(ht->tbl, ht);
809
810 if (p) {
811 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
812 goto next;
813 }
814
815 for (; iter->slot < tbl->size; iter->slot++) {
816 int skip = iter->skip;
817
818 rht_for_each_rcu(p, tbl, iter->slot) {
819 if (!skip)
820 break;
821 skip--;
822 }
823
824next:
825 if (!rht_is_a_nulls(p)) {
826 iter->skip++;
827 iter->p = p;
828 obj = rht_obj(ht, p);
829 goto out;
830 }
831
832 iter->skip = 0;
833 }
834
835 iter->p = NULL;
836
837out:
838 if (iter->walker->resize) {
839 iter->p = NULL;
840 iter->slot = 0;
841 iter->skip = 0;
842 iter->walker->resize = false;
843 return ERR_PTR(-EAGAIN);
844 }
845
846 return obj;
847}
848EXPORT_SYMBOL_GPL(rhashtable_walk_next);
849
850/**
851 * rhashtable_walk_stop - Finish a hash table walk
852 * @iter: Hash table iterator
853 *
854 * Finish a hash table walk.
855 */
856void rhashtable_walk_stop(struct rhashtable_iter *iter)
857{
858 rcu_read_unlock();
859 iter->p = NULL;
860}
861EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
862
Ying Xue94000172014-09-03 09:22:36 +0800863static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200864{
Ying Xue94000172014-09-03 09:22:36 +0800865 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
866 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200867}
868
869/**
870 * rhashtable_init - initialize a new hash table
871 * @ht: hash table to be initialized
872 * @params: configuration parameters
873 *
874 * Initializes a new hash table based on the provided configuration
875 * parameters. A table can be configured either with a variable or
876 * fixed length key:
877 *
878 * Configuration Example 1: Fixed length keys
879 * struct test_obj {
880 * int key;
881 * void * my_member;
882 * struct rhash_head node;
883 * };
884 *
885 * struct rhashtable_params params = {
886 * .head_offset = offsetof(struct test_obj, node),
887 * .key_offset = offsetof(struct test_obj, key),
888 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100889 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100890 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200891 * };
892 *
893 * Configuration Example 2: Variable length keys
894 * struct test_obj {
895 * [...]
896 * struct rhash_head node;
897 * };
898 *
899 * u32 my_hash_fn(const void *data, u32 seed)
900 * {
901 * struct test_obj *obj = data;
902 *
903 * return [... hash ...];
904 * }
905 *
906 * struct rhashtable_params params = {
907 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100908 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200909 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200910 * };
911 */
912int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
913{
914 struct bucket_table *tbl;
915 size_t size;
916
917 size = HASH_DEFAULT_SIZE;
918
919 if ((params->key_len && !params->hashfn) ||
920 (!params->key_len && !params->obj_hashfn))
921 return -EINVAL;
922
Thomas Graff89bd6f2015-01-02 23:00:21 +0100923 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
924 return -EINVAL;
925
Ying Xue94000172014-09-03 09:22:36 +0800926 params->min_shift = max_t(size_t, params->min_shift,
927 ilog2(HASH_MIN_SIZE));
928
Thomas Graf7e1e7762014-08-02 11:47:44 +0200929 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800930 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200931
Thomas Graf97defe12015-01-02 23:00:20 +0100932 memset(ht, 0, sizeof(*ht));
933 mutex_init(&ht->mutex);
934 memcpy(&ht->p, params, sizeof(*params));
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100935 INIT_LIST_HEAD(&ht->walkers);
Thomas Graf97defe12015-01-02 23:00:20 +0100936
937 if (params->locks_mul)
938 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
939 else
940 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
941
942 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200943 if (tbl == NULL)
944 return -ENOMEM;
945
Herbert Xu988dfbd2015-03-10 09:27:55 +1100946 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
947
Ying Xue545a1482015-01-07 13:41:57 +0800948 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800949 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200950 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100951 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200952
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100953 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100954
Thomas Graf7e1e7762014-08-02 11:47:44 +0200955 return 0;
956}
957EXPORT_SYMBOL_GPL(rhashtable_init);
958
959/**
960 * rhashtable_destroy - destroy hash table
961 * @ht: the hash table to destroy
962 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200963 * Frees the bucket array. This function is not rcu safe, therefore the caller
964 * has to make sure that no resizing may happen by unpublishing the hashtable
965 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200966 */
Thomas Graf97defe12015-01-02 23:00:20 +0100967void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200968{
Thomas Graf97defe12015-01-02 23:00:20 +0100969 ht->being_destroyed = true;
970
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100971 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800972
Thomas Graf97defe12015-01-02 23:00:20 +0100973 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100974 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100975 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200976}
977EXPORT_SYMBOL_GPL(rhashtable_destroy);