blob: 03fdaf869c4d725384cc3bc304ccc145c89f892f [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{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010066 return hash & (tbl->size - 1);
Thomas Graf7e1e7762014-08-02 11:47:44 +020067}
68
Herbert Xuaa34a6cb02015-03-11 09:43:48 +110069static u32 obj_raw_hashfn(struct rhashtable *ht,
70 const struct bucket_table *tbl, const void *ptr)
Thomas Graf8d24c0b2015-01-02 23:00:14 +010071{
72 u32 hash;
73
74 if (unlikely(!ht->p.key_len))
Herbert Xu988dfbd2015-03-10 09:27:55 +110075 hash = ht->p.obj_hashfn(ptr, tbl->hash_rnd);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010076 else
77 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
Herbert Xu988dfbd2015-03-10 09:27:55 +110078 tbl->hash_rnd);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010079
Thomas Graff89bd6f2015-01-02 23:00:21 +010080 return hash >> HASH_RESERVED_SPACE;
Thomas Graf8d24c0b2015-01-02 23:00:14 +010081}
82
Herbert Xuaa34a6cb02015-03-11 09:43:48 +110083static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
84 const void *key, u32 len)
Thomas Graf7e1e7762014-08-02 11:47:44 +020085{
Herbert Xu8d2b1872015-03-12 14:49:38 +110086 return rht_bucket_index(tbl, ht->p.hashfn(key, len, tbl->hash_rnd) >>
87 HASH_RESERVED_SPACE);
Thomas Graf7e1e7762014-08-02 11:47:44 +020088}
Thomas Graf7e1e7762014-08-02 11:47:44 +020089
Herbert Xu988dfbd2015-03-10 09:27:55 +110090static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010091 const struct bucket_table *tbl,
92 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020093{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +110094 return rht_bucket_index(tbl, obj_raw_hashfn(ht, tbl, rht_obj(ht, he)));
Thomas Graf7e1e7762014-08-02 11:47:44 +020095}
96
Thomas Grafa03eaec2015-02-05 02:03:34 +010097#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010098#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010099
100int lockdep_rht_mutex_is_held(struct rhashtable *ht)
101{
102 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
103}
104EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
105
106int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
107{
108 spinlock_t *lock = bucket_lock(tbl, hash);
109
110 return (debug_locks) ? lockdep_is_held(lock) : 1;
111}
112EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
113#else
114#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +0100115#endif
116
117
Thomas Graf97defe12015-01-02 23:00:20 +0100118static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
119{
120 unsigned int i, size;
121#if defined(CONFIG_PROVE_LOCKING)
122 unsigned int nr_pcpus = 2;
123#else
124 unsigned int nr_pcpus = num_possible_cpus();
125#endif
126
127 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
128 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
129
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100130 /* Never allocate more than 0.5 locks per bucket */
131 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100132
133 if (sizeof(spinlock_t) != 0) {
134#ifdef CONFIG_NUMA
135 if (size * sizeof(spinlock_t) > PAGE_SIZE)
136 tbl->locks = vmalloc(size * sizeof(spinlock_t));
137 else
138#endif
139 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
140 GFP_KERNEL);
141 if (!tbl->locks)
142 return -ENOMEM;
143 for (i = 0; i < size; i++)
144 spin_lock_init(&tbl->locks[i]);
145 }
146 tbl->locks_mask = size - 1;
147
148 return 0;
149}
150
151static void bucket_table_free(const struct bucket_table *tbl)
152{
153 if (tbl)
154 kvfree(tbl->locks);
155
156 kvfree(tbl);
157}
158
159static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
160 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200161{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100162 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200163 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100164 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200165
166 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100167 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
168 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200169 if (tbl == NULL)
170 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200171 if (tbl == NULL)
172 return NULL;
173
174 tbl->size = nbuckets;
175
Thomas Graf97defe12015-01-02 23:00:20 +0100176 if (alloc_bucket_locks(ht, tbl) < 0) {
177 bucket_table_free(tbl);
178 return NULL;
179 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200180
Thomas Graff89bd6f2015-01-02 23:00:21 +0100181 for (i = 0; i < nbuckets; i++)
182 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
183
Thomas Graf97defe12015-01-02 23:00:20 +0100184 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200185}
186
187/**
188 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
189 * @ht: hash table
190 * @new_size: new table size
191 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100192static bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193{
194 /* Expand table when exceeding 75% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800195 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
Daniel Borkmann8331de72015-02-25 16:31:53 +0100196 (!ht->p.max_shift || atomic_read(&ht->shift) < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200197}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200198
199/**
200 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
201 * @ht: hash table
202 * @new_size: new table size
203 */
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100204static bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200205{
206 /* Shrink table beneath 30% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800207 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
208 (atomic_read(&ht->shift) > ht->p.min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200209}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200210
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100211static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100212{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100213 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
214 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
215 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
216 int err = -ENOENT;
217 struct rhash_head *head, *next, *entry;
218 spinlock_t *new_bucket_lock;
219 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100220
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100221 rht_for_each(entry, old_tbl, old_hash) {
222 err = 0;
223 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100224
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100225 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200226 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100227
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100228 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200229 }
230
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100231 if (err)
232 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100233
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100234 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100235
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100236 new_bucket_lock = bucket_lock(new_tbl, new_hash);
237
Herbert Xu84ed82b2015-03-12 14:47:13 +1100238 spin_lock_nested(new_bucket_lock, RHT_LOCK_NESTED);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100239 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
240 new_tbl, new_hash);
241
242 if (rht_is_a_nulls(head))
243 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
244 else
245 RCU_INIT_POINTER(entry->next, head);
246
247 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
248 spin_unlock(new_bucket_lock);
249
250 rcu_assign_pointer(*pprev, next);
251
252out:
253 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100254}
255
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100256static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100257{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100258 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
259 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100260
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100261 old_bucket_lock = bucket_lock(old_tbl, old_hash);
262
263 spin_lock_bh(old_bucket_lock);
264 while (!rhashtable_rehash_one(ht, old_hash))
265 ;
266 spin_unlock_bh(old_bucket_lock);
267}
268
269static void rhashtable_rehash(struct rhashtable *ht,
270 struct bucket_table *new_tbl)
271{
272 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
273 unsigned old_hash;
274
275 get_random_bytes(&new_tbl->hash_rnd, sizeof(new_tbl->hash_rnd));
276
277 /* Make insertions go into the new, empty table right away. Deletions
278 * and lookups will be attempted in both tables until we synchronize.
279 * The synchronize_rcu() guarantees for the new table to be picked up
280 * so no new additions go into the old table while we relink.
281 */
282 rcu_assign_pointer(ht->future_tbl, new_tbl);
283
284 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
285 rhashtable_rehash_chain(ht, old_hash);
286
287 /* Publish the new table pointer. */
288 rcu_assign_pointer(ht->tbl, new_tbl);
289
290 /* Wait for readers. All new readers will see the new
291 * table, and thus no references to the old table will
292 * remain.
293 */
294 synchronize_rcu();
295
296 bucket_table_free(old_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200297}
298
299/**
300 * rhashtable_expand - Expand hash table while allowing concurrent lookups
301 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200302 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100303 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200304 *
305 * This function may only be called in a context where it is safe to call
306 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
307 *
Thomas Graf97defe12015-01-02 23:00:20 +0100308 * The caller must ensure that no concurrent resizing occurs by holding
309 * ht->mutex.
310 *
311 * It is valid to have concurrent insertions and deletions protected by per
312 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200313 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100314int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200315{
316 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200317
318 ASSERT_RHT_MUTEX(ht);
319
Thomas Graf97defe12015-01-02 23:00:20 +0100320 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200321 if (new_tbl == NULL)
322 return -ENOMEM;
323
Herbert Xu988dfbd2015-03-10 09:27:55 +1100324 new_tbl->hash_rnd = old_tbl->hash_rnd;
325
Ying Xuec0c09bf2015-01-07 13:41:56 +0800326 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200327
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100328 rhashtable_rehash(ht, new_tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100329
Thomas Graf7e1e7762014-08-02 11:47:44 +0200330 return 0;
331}
332EXPORT_SYMBOL_GPL(rhashtable_expand);
333
334/**
335 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
336 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200337 *
338 * This function may only be called in a context where it is safe to call
339 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
340 *
Thomas Graf97defe12015-01-02 23:00:20 +0100341 * The caller must ensure that no concurrent resizing occurs by holding
342 * ht->mutex.
343 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200344 * The caller must ensure that no concurrent table mutations take place.
345 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100346 *
347 * It is valid to have concurrent insertions and deletions protected by per
348 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200349 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100350int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200351{
Thomas Graf97defe12015-01-02 23:00:20 +0100352 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200353
354 ASSERT_RHT_MUTEX(ht);
355
Thomas Graf97defe12015-01-02 23:00:20 +0100356 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
357 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200358 return -ENOMEM;
359
Herbert Xu988dfbd2015-03-10 09:27:55 +1100360 new_tbl->hash_rnd = tbl->hash_rnd;
361
Ying Xuec0c09bf2015-01-07 13:41:56 +0800362 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200363
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100364 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200365
366 return 0;
367}
368EXPORT_SYMBOL_GPL(rhashtable_shrink);
369
Thomas Graf97defe12015-01-02 23:00:20 +0100370static void rht_deferred_worker(struct work_struct *work)
371{
372 struct rhashtable *ht;
373 struct bucket_table *tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100374 struct rhashtable_walker *walker;
Thomas Graf97defe12015-01-02 23:00:20 +0100375
Ying Xue57699a42015-01-16 11:13:09 +0800376 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100377 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100378 if (ht->being_destroyed)
379 goto unlock;
380
Thomas Graf97defe12015-01-02 23:00:20 +0100381 tbl = rht_dereference(ht->tbl, ht);
382
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100383 list_for_each_entry(walker, &ht->walkers, list)
384 walker->resize = true;
385
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100386 if (rht_grow_above_75(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100387 rhashtable_expand(ht);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100388 else if (rht_shrink_below_30(ht, tbl->size))
Thomas Graf97defe12015-01-02 23:00:20 +0100389 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100390unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100391 mutex_unlock(&ht->mutex);
392}
393
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100394static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
395 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800396{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100397 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000398 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100399 bool no_resize_running;
400 unsigned hash;
401 bool success = true;
402
403 rcu_read_lock();
404
405 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100406 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100407
408 spin_lock_bh(bucket_lock(old_tbl, hash));
409
410 /* Because we have already taken the bucket lock in old_tbl,
411 * if we find that future_tbl is not yet visible then that
412 * guarantees all other insertions of the same entry will
413 * also grab the bucket lock in old_tbl because until the
414 * rehash completes ht->tbl won't be changed.
415 */
416 tbl = rht_dereference_rcu(ht->future_tbl, ht);
417 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100418 hash = head_hashfn(ht, tbl, obj);
Herbert Xu84ed82b2015-03-12 14:47:13 +1100419 spin_lock_nested(bucket_lock(tbl, hash), RHT_LOCK_NESTED);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100420 }
421
422 if (compare &&
423 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
424 compare, arg)) {
425 success = false;
426 goto exit;
427 }
428
429 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000430
Thomas Graf020219a2015-02-06 16:08:43 +0000431 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800432
433 if (rht_is_a_nulls(head))
434 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
435 else
436 RCU_INIT_POINTER(obj->next, head);
437
438 rcu_assign_pointer(tbl->buckets[hash], obj);
439
440 atomic_inc(&ht->nelems);
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100441 if (no_resize_running && rht_grow_above_75(ht, tbl->size))
442 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100443
444exit:
445 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100446 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100447 spin_unlock(bucket_lock(tbl, hash));
448 }
449
Herbert Xueca84932015-03-12 14:49:39 +1100450 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100451 spin_unlock_bh(bucket_lock(old_tbl, hash));
452
453 rcu_read_unlock();
454
455 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800456}
457
Thomas Graf7e1e7762014-08-02 11:47:44 +0200458/**
Ying Xuedb304852015-01-07 13:41:54 +0800459 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460 * @ht: hash table
461 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200462 *
Thomas Graf97defe12015-01-02 23:00:20 +0100463 * Will take a per bucket spinlock to protect against mutual mutations
464 * on the same bucket. Multiple insertions may occur in parallel unless
465 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200466 *
Thomas Graf97defe12015-01-02 23:00:20 +0100467 * It is safe to call this function from atomic context.
468 *
469 * Will trigger an automatic deferred table resizing if the size grows
470 * beyond the watermark indicated by grow_decision() which can be passed
471 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200472 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100473void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200474{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100475 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200476}
477EXPORT_SYMBOL_GPL(rhashtable_insert);
478
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100479static bool __rhashtable_remove(struct rhashtable *ht,
480 struct bucket_table *tbl,
481 struct rhash_head *obj)
482{
483 struct rhash_head __rcu **pprev;
484 struct rhash_head *he;
485 spinlock_t * lock;
486 unsigned hash;
487 bool ret = false;
488
Herbert Xueca84932015-03-12 14:49:39 +1100489 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100490 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100491
492 spin_lock_bh(lock);
493
494 pprev = &tbl->buckets[hash];
495 rht_for_each(he, tbl, hash) {
496 if (he != obj) {
497 pprev = &he->next;
498 continue;
499 }
500
501 rcu_assign_pointer(*pprev, obj->next);
502 ret = true;
503 break;
504 }
505
506 spin_unlock_bh(lock);
507
508 return ret;
509}
510
Thomas Graf7e1e7762014-08-02 11:47:44 +0200511/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200512 * rhashtable_remove - remove object from hash table
513 * @ht: hash table
514 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200515 *
516 * Since the hash chain is single linked, the removal operation needs to
517 * walk the bucket chain upon removal. The removal operation is thus
518 * considerable slow if the hash table is not correctly sized.
519 *
Ying Xuedb304852015-01-07 13:41:54 +0800520 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200521 * shrink_decision function specified at rhashtable_init() returns true.
522 *
523 * The caller must ensure that no concurrent table mutations occur. It is
524 * however valid to have concurrent lookups if they are RCU protected.
525 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100526bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200527{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100528 struct bucket_table *tbl, *old_tbl;
529 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200530
Thomas Graf97defe12015-01-02 23:00:20 +0100531 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100532
Thomas Graf020219a2015-02-06 16:08:43 +0000533 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100534 ret = __rhashtable_remove(ht, old_tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200535
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100536 /* Because we have already taken (and released) the bucket
537 * lock in old_tbl, if we find that future_tbl is not yet
538 * visible then that guarantees the entry to still be in
539 * old_tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000540 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100541 tbl = rht_dereference_rcu(ht->future_tbl, ht);
542 if (!ret && old_tbl != tbl)
543 ret = __rhashtable_remove(ht, tbl, obj);
Thomas Graffe6a0432015-01-21 11:54:01 +0000544
545 if (ret) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100546 bool no_resize_running = tbl == old_tbl;
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100547
Thomas Graffe6a0432015-01-21 11:54:01 +0000548 atomic_dec(&ht->nelems);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100549 if (no_resize_running && rht_shrink_below_30(ht, tbl->size))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100550 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000551 }
552
Thomas Graf97defe12015-01-02 23:00:20 +0100553 rcu_read_unlock();
554
Thomas Graffe6a0432015-01-21 11:54:01 +0000555 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200556}
557EXPORT_SYMBOL_GPL(rhashtable_remove);
558
Ying Xueefb975a62015-01-07 13:41:52 +0800559struct rhashtable_compare_arg {
560 struct rhashtable *ht;
561 const void *key;
562};
563
564static bool rhashtable_compare(void *ptr, void *arg)
565{
566 struct rhashtable_compare_arg *x = arg;
567 struct rhashtable *ht = x->ht;
568
569 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
570}
571
Thomas Graf7e1e7762014-08-02 11:47:44 +0200572/**
573 * rhashtable_lookup - lookup key in hash table
574 * @ht: hash table
575 * @key: pointer to key
576 *
577 * Computes the hash value for the key and traverses the bucket chain looking
578 * for a entry with an identical key. The first matching entry is returned.
579 *
580 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800581 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200582 *
Thomas Graf97defe12015-01-02 23:00:20 +0100583 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200584 */
Thomas Graf97defe12015-01-02 23:00:20 +0100585void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200586{
Ying Xueefb975a62015-01-07 13:41:52 +0800587 struct rhashtable_compare_arg arg = {
588 .ht = ht,
589 .key = key,
590 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200591
592 BUG_ON(!ht->p.key_len);
593
Ying Xueefb975a62015-01-07 13:41:52 +0800594 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200595}
596EXPORT_SYMBOL_GPL(rhashtable_lookup);
597
598/**
599 * rhashtable_lookup_compare - search hash table with compare function
600 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100601 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200602 * @compare: compare function, must return true on match
603 * @arg: argument passed on to compare function
604 *
605 * Traverses the bucket chain behind the provided hash value and calls the
606 * specified compare function for each entry.
607 *
Thomas Graf97defe12015-01-02 23:00:20 +0100608 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200609 *
610 * Returns the first entry on which the compare function returned true.
611 */
Thomas Graf97defe12015-01-02 23:00:20 +0100612void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200613 bool (*compare)(void *, void *), void *arg)
614{
Thomas Graf97defe12015-01-02 23:00:20 +0100615 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200616 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100617 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200618
Thomas Graf97defe12015-01-02 23:00:20 +0100619 rcu_read_lock();
620
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100621 tbl = rht_dereference_rcu(ht->tbl, ht);
622 hash = key_hashfn(ht, tbl, key, ht->p.key_len);
Thomas Graf97defe12015-01-02 23:00:20 +0100623restart:
Herbert Xu8d2b1872015-03-12 14:49:38 +1100624 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200625 if (!compare(rht_obj(ht, he), arg))
626 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100627 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100628 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200629 }
630
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100631 old_tbl = tbl;
632 tbl = rht_dereference_rcu(ht->future_tbl, ht);
633 if (unlikely(tbl != old_tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100634 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100635 rcu_read_unlock();
636
Thomas Graf7e1e7762014-08-02 11:47:44 +0200637 return NULL;
638}
639EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
640
Ying Xuedb304852015-01-07 13:41:54 +0800641/**
642 * rhashtable_lookup_insert - lookup and insert object into hash table
643 * @ht: hash table
644 * @obj: pointer to hash head inside object
645 *
646 * Locks down the bucket chain in both the old and new table if a resize
647 * is in progress to ensure that writers can't remove from the old table
648 * and can't insert to the new table during the atomic operation of search
649 * and insertion. Searches for duplicates in both the old and new table if
650 * a resize is in progress.
651 *
652 * This lookup function may only be used for fixed key hash table (key_len
653 * parameter set). It will BUG() if used inappropriately.
654 *
655 * It is safe to call this function from atomic context.
656 *
657 * Will trigger an automatic deferred table resizing if the size grows
658 * beyond the watermark indicated by grow_decision() which can be passed
659 * to rhashtable_init().
660 */
661bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
662{
Ying Xue7a868d12015-01-12 14:52:22 +0800663 struct rhashtable_compare_arg arg = {
664 .ht = ht,
665 .key = rht_obj(ht, obj) + ht->p.key_offset,
666 };
667
668 BUG_ON(!ht->p.key_len);
669
670 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
671 &arg);
672}
673EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
674
675/**
676 * rhashtable_lookup_compare_insert - search and insert object to hash table
677 * with compare function
678 * @ht: hash table
679 * @obj: pointer to hash head inside object
680 * @compare: compare function, must return true on match
681 * @arg: argument passed on to compare function
682 *
683 * Locks down the bucket chain in both the old and new table if a resize
684 * is in progress to ensure that writers can't remove from the old table
685 * and can't insert to the new table during the atomic operation of search
686 * and insertion. Searches for duplicates in both the old and new table if
687 * a resize is in progress.
688 *
689 * Lookups may occur in parallel with hashtable mutations and resizing.
690 *
691 * Will trigger an automatic deferred table resizing if the size grows
692 * beyond the watermark indicated by grow_decision() which can be passed
693 * to rhashtable_init().
694 */
695bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
696 struct rhash_head *obj,
697 bool (*compare)(void *, void *),
698 void *arg)
699{
Ying Xuedb304852015-01-07 13:41:54 +0800700 BUG_ON(!ht->p.key_len);
701
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100702 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800703}
Ying Xue7a868d12015-01-12 14:52:22 +0800704EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800705
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100706/**
707 * rhashtable_walk_init - Initialise an iterator
708 * @ht: Table to walk over
709 * @iter: Hash table Iterator
710 *
711 * This function prepares a hash table walk.
712 *
713 * Note that if you restart a walk after rhashtable_walk_stop you
714 * may see the same object twice. Also, you may miss objects if
715 * there are removals in between rhashtable_walk_stop and the next
716 * call to rhashtable_walk_start.
717 *
718 * For a completely stable walk you should construct your own data
719 * structure outside the hash table.
720 *
721 * This function may sleep so you must not call it from interrupt
722 * context or with spin locks held.
723 *
724 * You must call rhashtable_walk_exit if this function returns
725 * successfully.
726 */
727int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
728{
729 iter->ht = ht;
730 iter->p = NULL;
731 iter->slot = 0;
732 iter->skip = 0;
733
734 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
735 if (!iter->walker)
736 return -ENOMEM;
737
Sasha Levin71bb0012015-02-23 04:35:06 -0500738 INIT_LIST_HEAD(&iter->walker->list);
739 iter->walker->resize = false;
740
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100741 mutex_lock(&ht->mutex);
742 list_add(&iter->walker->list, &ht->walkers);
743 mutex_unlock(&ht->mutex);
744
745 return 0;
746}
747EXPORT_SYMBOL_GPL(rhashtable_walk_init);
748
749/**
750 * rhashtable_walk_exit - Free an iterator
751 * @iter: Hash table Iterator
752 *
753 * This function frees resources allocated by rhashtable_walk_init.
754 */
755void rhashtable_walk_exit(struct rhashtable_iter *iter)
756{
757 mutex_lock(&iter->ht->mutex);
758 list_del(&iter->walker->list);
759 mutex_unlock(&iter->ht->mutex);
760 kfree(iter->walker);
761}
762EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
763
764/**
765 * rhashtable_walk_start - Start a hash table walk
766 * @iter: Hash table iterator
767 *
768 * Start a hash table walk. Note that we take the RCU lock in all
769 * cases including when we return an error. So you must always call
770 * rhashtable_walk_stop to clean up.
771 *
772 * Returns zero if successful.
773 *
774 * Returns -EAGAIN if resize event occured. Note that the iterator
775 * will rewind back to the beginning and you may use it immediately
776 * by calling rhashtable_walk_next.
777 */
778int rhashtable_walk_start(struct rhashtable_iter *iter)
779{
780 rcu_read_lock();
781
782 if (iter->walker->resize) {
783 iter->slot = 0;
784 iter->skip = 0;
785 iter->walker->resize = false;
786 return -EAGAIN;
787 }
788
789 return 0;
790}
791EXPORT_SYMBOL_GPL(rhashtable_walk_start);
792
793/**
794 * rhashtable_walk_next - Return the next object and advance the iterator
795 * @iter: Hash table iterator
796 *
797 * Note that you must call rhashtable_walk_stop when you are finished
798 * with the walk.
799 *
800 * Returns the next object or NULL when the end of the table is reached.
801 *
802 * Returns -EAGAIN if resize event occured. Note that the iterator
803 * will rewind back to the beginning and you may continue to use it.
804 */
805void *rhashtable_walk_next(struct rhashtable_iter *iter)
806{
807 const struct bucket_table *tbl;
808 struct rhashtable *ht = iter->ht;
809 struct rhash_head *p = iter->p;
810 void *obj = NULL;
811
812 tbl = rht_dereference_rcu(ht->tbl, ht);
813
814 if (p) {
815 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
816 goto next;
817 }
818
819 for (; iter->slot < tbl->size; iter->slot++) {
820 int skip = iter->skip;
821
822 rht_for_each_rcu(p, tbl, iter->slot) {
823 if (!skip)
824 break;
825 skip--;
826 }
827
828next:
829 if (!rht_is_a_nulls(p)) {
830 iter->skip++;
831 iter->p = p;
832 obj = rht_obj(ht, p);
833 goto out;
834 }
835
836 iter->skip = 0;
837 }
838
839 iter->p = NULL;
840
841out:
842 if (iter->walker->resize) {
843 iter->p = NULL;
844 iter->slot = 0;
845 iter->skip = 0;
846 iter->walker->resize = false;
847 return ERR_PTR(-EAGAIN);
848 }
849
850 return obj;
851}
852EXPORT_SYMBOL_GPL(rhashtable_walk_next);
853
854/**
855 * rhashtable_walk_stop - Finish a hash table walk
856 * @iter: Hash table iterator
857 *
858 * Finish a hash table walk.
859 */
860void rhashtable_walk_stop(struct rhashtable_iter *iter)
861{
862 rcu_read_unlock();
863 iter->p = NULL;
864}
865EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
866
Ying Xue94000172014-09-03 09:22:36 +0800867static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200868{
Ying Xue94000172014-09-03 09:22:36 +0800869 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
870 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200871}
872
873/**
874 * rhashtable_init - initialize a new hash table
875 * @ht: hash table to be initialized
876 * @params: configuration parameters
877 *
878 * Initializes a new hash table based on the provided configuration
879 * parameters. A table can be configured either with a variable or
880 * fixed length key:
881 *
882 * Configuration Example 1: Fixed length keys
883 * struct test_obj {
884 * int key;
885 * void * my_member;
886 * struct rhash_head node;
887 * };
888 *
889 * struct rhashtable_params params = {
890 * .head_offset = offsetof(struct test_obj, node),
891 * .key_offset = offsetof(struct test_obj, key),
892 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100893 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100894 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200895 * };
896 *
897 * Configuration Example 2: Variable length keys
898 * struct test_obj {
899 * [...]
900 * struct rhash_head node;
901 * };
902 *
903 * u32 my_hash_fn(const void *data, u32 seed)
904 * {
905 * struct test_obj *obj = data;
906 *
907 * return [... hash ...];
908 * }
909 *
910 * struct rhashtable_params params = {
911 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100912 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200913 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200914 * };
915 */
916int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
917{
918 struct bucket_table *tbl;
919 size_t size;
920
921 size = HASH_DEFAULT_SIZE;
922
923 if ((params->key_len && !params->hashfn) ||
924 (!params->key_len && !params->obj_hashfn))
925 return -EINVAL;
926
Thomas Graff89bd6f2015-01-02 23:00:21 +0100927 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
928 return -EINVAL;
929
Ying Xue94000172014-09-03 09:22:36 +0800930 params->min_shift = max_t(size_t, params->min_shift,
931 ilog2(HASH_MIN_SIZE));
932
Thomas Graf7e1e7762014-08-02 11:47:44 +0200933 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800934 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200935
Thomas Graf97defe12015-01-02 23:00:20 +0100936 memset(ht, 0, sizeof(*ht));
937 mutex_init(&ht->mutex);
938 memcpy(&ht->p, params, sizeof(*params));
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100939 INIT_LIST_HEAD(&ht->walkers);
Thomas Graf97defe12015-01-02 23:00:20 +0100940
941 if (params->locks_mul)
942 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
943 else
944 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
945
946 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200947 if (tbl == NULL)
948 return -ENOMEM;
949
Herbert Xu988dfbd2015-03-10 09:27:55 +1100950 get_random_bytes(&tbl->hash_rnd, sizeof(tbl->hash_rnd));
951
Ying Xue545a1482015-01-07 13:41:57 +0800952 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800953 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200954 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100955 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200956
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100957 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100958
Thomas Graf7e1e7762014-08-02 11:47:44 +0200959 return 0;
960}
961EXPORT_SYMBOL_GPL(rhashtable_init);
962
963/**
964 * rhashtable_destroy - destroy hash table
965 * @ht: the hash table to destroy
966 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200967 * Frees the bucket array. This function is not rcu safe, therefore the caller
968 * has to make sure that no resizing may happen by unpublishing the hashtable
969 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200970 */
Thomas Graf97defe12015-01-02 23:00:20 +0100971void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200972{
Thomas Graf97defe12015-01-02 23:00:20 +0100973 ht->being_destroyed = true;
974
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100975 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800976
Thomas Graf97defe12015-01-02 23:00:20 +0100977 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100978 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100979 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200980}
981EXPORT_SYMBOL_GPL(rhashtable_destroy);