blob: 5d06cc2b1e4a5ea38280a4676b8922e07b8f6e66 [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 +010036/* The bucket lock is selected based on the hash and protects mutations
37 * on a group of hash buckets.
38 *
Thomas Grafa5ec68e2015-02-05 02:03:32 +010039 * A maximum of tbl->size/2 bucket locks is allocated. This ensures that
40 * a single lock always covers both buckets which may both contains
41 * entries which link to the same bucket of the old table during resizing.
42 * This allows to simplify the locking as locking the bucket in both
43 * tables during resize always guarantee protection.
44 *
Thomas Graf97defe12015-01-02 23:00:20 +010045 * IMPORTANT: When holding the bucket lock of both the old and new table
46 * during expansions and shrinking, the old bucket lock must always be
47 * acquired first.
48 */
49static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
50{
51 return &tbl->locks[hash & tbl->locks_mask];
52}
Thomas Graf7e1e7762014-08-02 11:47:44 +020053
Thomas Grafc91eee52014-08-13 16:38:30 +020054static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020055{
56 return (void *) he - ht->p.head_offset;
57}
Thomas Graf7e1e7762014-08-02 11:47:44 +020058
Thomas Graf8d24c0b2015-01-02 23:00:14 +010059static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020060{
Herbert Xuec9f71c2015-03-12 14:49:41 +110061 return (hash >> HASH_RESERVED_SPACE) & (tbl->size - 1);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010062}
63
Herbert Xuaa34a6cb02015-03-11 09:43:48 +110064static u32 key_hashfn(struct rhashtable *ht, const struct bucket_table *tbl,
Herbert Xucffaa9c2015-03-12 14:49:40 +110065 const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +020066{
Herbert Xucffaa9c2015-03-12 14:49:40 +110067 return rht_bucket_index(tbl, ht->p.hashfn(key, ht->p.key_len,
Herbert Xuec9f71c2015-03-12 14:49:41 +110068 tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020069}
Thomas Graf7e1e7762014-08-02 11:47:44 +020070
Herbert Xu988dfbd2015-03-10 09:27:55 +110071static u32 head_hashfn(struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010072 const struct bucket_table *tbl,
73 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020074{
Herbert Xuec9f71c2015-03-12 14:49:41 +110075 const char *ptr = rht_obj(ht, he);
76
77 return likely(ht->p.key_len) ?
78 key_hashfn(ht, tbl, ptr + ht->p.key_offset) :
79 rht_bucket_index(tbl, ht->p.obj_hashfn(ptr, tbl->hash_rnd));
Thomas Graf7e1e7762014-08-02 11:47:44 +020080}
81
Thomas Grafa03eaec2015-02-05 02:03:34 +010082#ifdef CONFIG_PROVE_LOCKING
Thomas Grafa03eaec2015-02-05 02:03:34 +010083#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Grafa03eaec2015-02-05 02:03:34 +010084
85int lockdep_rht_mutex_is_held(struct rhashtable *ht)
86{
87 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
88}
89EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
90
91int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
92{
93 spinlock_t *lock = bucket_lock(tbl, hash);
94
95 return (debug_locks) ? lockdep_is_held(lock) : 1;
96}
97EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
98#else
99#define ASSERT_RHT_MUTEX(HT)
Thomas Grafa03eaec2015-02-05 02:03:34 +0100100#endif
101
102
Thomas Graf97defe12015-01-02 23:00:20 +0100103static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
104{
105 unsigned int i, size;
106#if defined(CONFIG_PROVE_LOCKING)
107 unsigned int nr_pcpus = 2;
108#else
109 unsigned int nr_pcpus = num_possible_cpus();
110#endif
111
112 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
113 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
114
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100115 /* Never allocate more than 0.5 locks per bucket */
116 size = min_t(unsigned int, size, tbl->size >> 1);
Thomas Graf97defe12015-01-02 23:00:20 +0100117
118 if (sizeof(spinlock_t) != 0) {
119#ifdef CONFIG_NUMA
120 if (size * sizeof(spinlock_t) > PAGE_SIZE)
121 tbl->locks = vmalloc(size * sizeof(spinlock_t));
122 else
123#endif
124 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
125 GFP_KERNEL);
126 if (!tbl->locks)
127 return -ENOMEM;
128 for (i = 0; i < size; i++)
129 spin_lock_init(&tbl->locks[i]);
130 }
131 tbl->locks_mask = size - 1;
132
133 return 0;
134}
135
136static void bucket_table_free(const struct bucket_table *tbl)
137{
138 if (tbl)
139 kvfree(tbl->locks);
140
141 kvfree(tbl);
142}
143
144static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100145 size_t nbuckets, u32 hash_rnd)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200146{
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100147 struct bucket_table *tbl = NULL;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200148 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100149 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200150
151 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Daniel Borkmanneb6d1ab2015-02-20 00:53:38 +0100152 if (size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
153 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200154 if (tbl == NULL)
155 tbl = vzalloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200156 if (tbl == NULL)
157 return NULL;
158
159 tbl->size = nbuckets;
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100160 tbl->shift = ilog2(nbuckets);
161 tbl->hash_rnd = hash_rnd;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200162
Thomas Graf97defe12015-01-02 23:00:20 +0100163 if (alloc_bucket_locks(ht, tbl) < 0) {
164 bucket_table_free(tbl);
165 return NULL;
166 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200167
Herbert Xueddee5ba2015-03-14 13:57:20 +1100168 INIT_LIST_HEAD(&tbl->walkers);
169
Thomas Graff89bd6f2015-01-02 23:00:21 +0100170 for (i = 0; i < nbuckets; i++)
171 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
172
Thomas Graf97defe12015-01-02 23:00:20 +0100173 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200174}
175
176/**
177 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
178 * @ht: hash table
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100179 * @tbl: current table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200180 */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100181static bool rht_grow_above_75(const struct rhashtable *ht,
182 const struct bucket_table *tbl)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200183{
184 /* Expand table when exceeding 75% load */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100185 return atomic_read(&ht->nelems) > (tbl->size / 4 * 3) &&
186 (!ht->p.max_shift || tbl->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
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100192 * @tbl: current table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200193 */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100194static bool rht_shrink_below_30(const struct rhashtable *ht,
195 const struct bucket_table *tbl)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200196{
197 /* Shrink table beneath 30% load */
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100198 return atomic_read(&ht->nelems) < (tbl->size * 3 / 10) &&
199 tbl->shift > ht->p.min_shift;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200200}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200201
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100202static int rhashtable_rehash_one(struct rhashtable *ht, unsigned old_hash)
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100203{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100204 struct bucket_table *new_tbl = rht_dereference(ht->future_tbl, ht);
205 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
206 struct rhash_head __rcu **pprev = &old_tbl->buckets[old_hash];
207 int err = -ENOENT;
208 struct rhash_head *head, *next, *entry;
209 spinlock_t *new_bucket_lock;
210 unsigned new_hash;
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100211
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100212 rht_for_each(entry, old_tbl, old_hash) {
213 err = 0;
214 next = rht_dereference_bucket(entry->next, old_tbl, old_hash);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100215
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100216 if (rht_is_a_nulls(next))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200217 break;
Thomas Graf97defe12015-01-02 23:00:20 +0100218
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100219 pprev = &entry->next;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200220 }
221
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100222 if (err)
223 goto out;
Thomas Graf97defe12015-01-02 23:00:20 +0100224
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100225 new_hash = head_hashfn(ht, new_tbl, entry);
Thomas Grafa5ec68e2015-02-05 02:03:32 +0100226
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100227 new_bucket_lock = bucket_lock(new_tbl, new_hash);
228
Herbert Xu8f2484b2015-03-14 13:57:21 +1100229 spin_lock_nested(new_bucket_lock, SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100230 head = rht_dereference_bucket(new_tbl->buckets[new_hash],
231 new_tbl, new_hash);
232
233 if (rht_is_a_nulls(head))
234 INIT_RHT_NULLS_HEAD(entry->next, ht, new_hash);
235 else
236 RCU_INIT_POINTER(entry->next, head);
237
238 rcu_assign_pointer(new_tbl->buckets[new_hash], entry);
239 spin_unlock(new_bucket_lock);
240
241 rcu_assign_pointer(*pprev, next);
242
243out:
244 return err;
Thomas Graf97defe12015-01-02 23:00:20 +0100245}
246
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100247static void rhashtable_rehash_chain(struct rhashtable *ht, unsigned old_hash)
Thomas Graf97defe12015-01-02 23:00:20 +0100248{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100249 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
250 spinlock_t *old_bucket_lock;
Thomas Graf7cd10db2015-02-05 02:03:35 +0100251
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100252 old_bucket_lock = bucket_lock(old_tbl, old_hash);
253
254 spin_lock_bh(old_bucket_lock);
255 while (!rhashtable_rehash_one(ht, old_hash))
256 ;
257 spin_unlock_bh(old_bucket_lock);
258}
259
260static void rhashtable_rehash(struct rhashtable *ht,
261 struct bucket_table *new_tbl)
262{
263 struct bucket_table *old_tbl = rht_dereference(ht->tbl, ht);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100264 struct rhashtable_walker *walker;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100265 unsigned old_hash;
266
267 get_random_bytes(&new_tbl->hash_rnd, sizeof(new_tbl->hash_rnd));
268
269 /* Make insertions go into the new, empty table right away. Deletions
270 * and lookups will be attempted in both tables until we synchronize.
271 * The synchronize_rcu() guarantees for the new table to be picked up
272 * so no new additions go into the old table while we relink.
273 */
274 rcu_assign_pointer(ht->future_tbl, new_tbl);
275
Herbert Xu9497df82015-03-12 22:07:49 +1100276 /* Ensure the new table is visible to readers. */
277 smp_wmb();
278
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100279 for (old_hash = 0; old_hash < old_tbl->size; old_hash++)
280 rhashtable_rehash_chain(ht, old_hash);
281
282 /* Publish the new table pointer. */
283 rcu_assign_pointer(ht->tbl, new_tbl);
284
Herbert Xueddee5ba2015-03-14 13:57:20 +1100285 list_for_each_entry(walker, &old_tbl->walkers, list)
286 walker->tbl = NULL;
287
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100288 /* Wait for readers. All new readers will see the new
289 * table, and thus no references to the old table will
290 * remain.
291 */
292 synchronize_rcu();
293
294 bucket_table_free(old_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200295}
296
297/**
298 * rhashtable_expand - Expand hash table while allowing concurrent lookups
299 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200300 *
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100301 * A secondary bucket array is allocated and the hash entries are migrated.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200302 *
303 * This function may only be called in a context where it is safe to call
304 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
305 *
Thomas Graf97defe12015-01-02 23:00:20 +0100306 * The caller must ensure that no concurrent resizing occurs by holding
307 * ht->mutex.
308 *
309 * It is valid to have concurrent insertions and deletions protected by per
310 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200311 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100312int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200313{
314 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200315
316 ASSERT_RHT_MUTEX(ht);
317
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100318 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2, old_tbl->hash_rnd);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200319 if (new_tbl == NULL)
320 return -ENOMEM;
321
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100322 rhashtable_rehash(ht, new_tbl);
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{
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100345 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200346
347 ASSERT_RHT_MUTEX(ht);
348
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100349 new_tbl = bucket_table_alloc(ht, old_tbl->size / 2, old_tbl->hash_rnd);
Thomas Graf97defe12015-01-02 23:00:20 +0100350 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200351 return -ENOMEM;
352
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100353 rhashtable_rehash(ht, new_tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200354 return 0;
355}
356EXPORT_SYMBOL_GPL(rhashtable_shrink);
357
Thomas Graf97defe12015-01-02 23:00:20 +0100358static void rht_deferred_worker(struct work_struct *work)
359{
360 struct rhashtable *ht;
361 struct bucket_table *tbl;
362
Ying Xue57699a42015-01-16 11:13:09 +0800363 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100364 mutex_lock(&ht->mutex);
Herbert Xu28134a52015-02-04 07:33:22 +1100365 if (ht->being_destroyed)
366 goto unlock;
367
Thomas Graf97defe12015-01-02 23:00:20 +0100368 tbl = rht_dereference(ht->tbl, ht);
369
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100370 if (rht_grow_above_75(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100371 rhashtable_expand(ht);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100372 else if (rht_shrink_below_30(ht, tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100373 rhashtable_shrink(ht);
Herbert Xu28134a52015-02-04 07:33:22 +1100374unlock:
Thomas Graf97defe12015-01-02 23:00:20 +0100375 mutex_unlock(&ht->mutex);
376}
377
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100378static bool __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
379 bool (*compare)(void *, void *), void *arg)
Ying Xuedb304852015-01-07 13:41:54 +0800380{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100381 struct bucket_table *tbl, *old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000382 struct rhash_head *head;
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100383 bool no_resize_running;
384 unsigned hash;
385 bool success = true;
386
387 rcu_read_lock();
388
389 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xueca84932015-03-12 14:49:39 +1100390 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100391
392 spin_lock_bh(bucket_lock(old_tbl, hash));
393
394 /* Because we have already taken the bucket lock in old_tbl,
395 * if we find that future_tbl is not yet visible then that
396 * guarantees all other insertions of the same entry will
397 * also grab the bucket lock in old_tbl because until the
398 * rehash completes ht->tbl won't be changed.
399 */
400 tbl = rht_dereference_rcu(ht->future_tbl, ht);
401 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100402 hash = head_hashfn(ht, tbl, obj);
Herbert Xu8f2484b2015-03-14 13:57:21 +1100403 spin_lock_nested(bucket_lock(tbl, hash), SINGLE_DEPTH_NESTING);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100404 }
405
406 if (compare &&
407 rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
408 compare, arg)) {
409 success = false;
410 goto exit;
411 }
412
413 no_resize_running = tbl == old_tbl;
Thomas Graf020219a2015-02-06 16:08:43 +0000414
Thomas Graf020219a2015-02-06 16:08:43 +0000415 head = rht_dereference_bucket(tbl->buckets[hash], tbl, hash);
Ying Xuedb304852015-01-07 13:41:54 +0800416
417 if (rht_is_a_nulls(head))
418 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
419 else
420 RCU_INIT_POINTER(obj->next, head);
421
422 rcu_assign_pointer(tbl->buckets[hash], obj);
423
424 atomic_inc(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100425 if (no_resize_running && rht_grow_above_75(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100426 schedule_work(&ht->run_work);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100427
428exit:
429 if (tbl != old_tbl) {
Herbert Xueca84932015-03-12 14:49:39 +1100430 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100431 spin_unlock(bucket_lock(tbl, hash));
432 }
433
Herbert Xueca84932015-03-12 14:49:39 +1100434 hash = head_hashfn(ht, old_tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100435 spin_unlock_bh(bucket_lock(old_tbl, hash));
436
437 rcu_read_unlock();
438
439 return success;
Ying Xuedb304852015-01-07 13:41:54 +0800440}
441
Thomas Graf7e1e7762014-08-02 11:47:44 +0200442/**
Ying Xuedb304852015-01-07 13:41:54 +0800443 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200444 * @ht: hash table
445 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200446 *
Thomas Graf97defe12015-01-02 23:00:20 +0100447 * Will take a per bucket spinlock to protect against mutual mutations
448 * on the same bucket. Multiple insertions may occur in parallel unless
449 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200450 *
Thomas Graf97defe12015-01-02 23:00:20 +0100451 * It is safe to call this function from atomic context.
452 *
453 * Will trigger an automatic deferred table resizing if the size grows
454 * beyond the watermark indicated by grow_decision() which can be passed
455 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200456 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100457void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200458{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100459 __rhashtable_insert(ht, obj, NULL, NULL);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460}
461EXPORT_SYMBOL_GPL(rhashtable_insert);
462
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100463static bool __rhashtable_remove(struct rhashtable *ht,
464 struct bucket_table *tbl,
465 struct rhash_head *obj)
466{
467 struct rhash_head __rcu **pprev;
468 struct rhash_head *he;
469 spinlock_t * lock;
470 unsigned hash;
471 bool ret = false;
472
Herbert Xueca84932015-03-12 14:49:39 +1100473 hash = head_hashfn(ht, tbl, obj);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100474 lock = bucket_lock(tbl, hash);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100475
476 spin_lock_bh(lock);
477
478 pprev = &tbl->buckets[hash];
479 rht_for_each(he, tbl, hash) {
480 if (he != obj) {
481 pprev = &he->next;
482 continue;
483 }
484
485 rcu_assign_pointer(*pprev, obj->next);
486 ret = true;
487 break;
488 }
489
490 spin_unlock_bh(lock);
491
492 return ret;
493}
494
Thomas Graf7e1e7762014-08-02 11:47:44 +0200495/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200496 * rhashtable_remove - remove object from hash table
497 * @ht: hash table
498 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200499 *
500 * Since the hash chain is single linked, the removal operation needs to
501 * walk the bucket chain upon removal. The removal operation is thus
502 * considerable slow if the hash table is not correctly sized.
503 *
Ying Xuedb304852015-01-07 13:41:54 +0800504 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200505 * shrink_decision function specified at rhashtable_init() returns true.
506 *
507 * The caller must ensure that no concurrent table mutations occur. It is
508 * however valid to have concurrent lookups if they are RCU protected.
509 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100510bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200511{
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100512 struct bucket_table *tbl, *old_tbl;
513 bool ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200514
Thomas Graf97defe12015-01-02 23:00:20 +0100515 rcu_read_lock();
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100516
Thomas Graf020219a2015-02-06 16:08:43 +0000517 old_tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100518 ret = __rhashtable_remove(ht, old_tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200519
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100520 /* Because we have already taken (and released) the bucket
521 * lock in old_tbl, if we find that future_tbl is not yet
522 * visible then that guarantees the entry to still be in
523 * old_tbl if it exists.
Thomas Graffe6a0432015-01-21 11:54:01 +0000524 */
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100525 tbl = rht_dereference_rcu(ht->future_tbl, ht);
526 if (!ret && old_tbl != tbl)
527 ret = __rhashtable_remove(ht, tbl, obj);
Thomas Graffe6a0432015-01-21 11:54:01 +0000528
529 if (ret) {
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100530 bool no_resize_running = tbl == old_tbl;
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100531
Thomas Graffe6a0432015-01-21 11:54:01 +0000532 atomic_dec(&ht->nelems);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100533 if (no_resize_running && rht_shrink_below_30(ht, tbl))
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100534 schedule_work(&ht->run_work);
Thomas Graffe6a0432015-01-21 11:54:01 +0000535 }
536
Thomas Graf97defe12015-01-02 23:00:20 +0100537 rcu_read_unlock();
538
Thomas Graffe6a0432015-01-21 11:54:01 +0000539 return ret;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200540}
541EXPORT_SYMBOL_GPL(rhashtable_remove);
542
Ying Xueefb975a62015-01-07 13:41:52 +0800543struct rhashtable_compare_arg {
544 struct rhashtable *ht;
545 const void *key;
546};
547
548static bool rhashtable_compare(void *ptr, void *arg)
549{
550 struct rhashtable_compare_arg *x = arg;
551 struct rhashtable *ht = x->ht;
552
553 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
554}
555
Thomas Graf7e1e7762014-08-02 11:47:44 +0200556/**
557 * rhashtable_lookup - lookup key in hash table
558 * @ht: hash table
559 * @key: pointer to key
560 *
561 * Computes the hash value for the key and traverses the bucket chain looking
562 * for a entry with an identical key. The first matching entry is returned.
563 *
564 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800565 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200566 *
Thomas Graf97defe12015-01-02 23:00:20 +0100567 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200568 */
Thomas Graf97defe12015-01-02 23:00:20 +0100569void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200570{
Ying Xueefb975a62015-01-07 13:41:52 +0800571 struct rhashtable_compare_arg arg = {
572 .ht = ht,
573 .key = key,
574 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200575
576 BUG_ON(!ht->p.key_len);
577
Ying Xueefb975a62015-01-07 13:41:52 +0800578 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200579}
580EXPORT_SYMBOL_GPL(rhashtable_lookup);
581
582/**
583 * rhashtable_lookup_compare - search hash table with compare function
584 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100585 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200586 * @compare: compare function, must return true on match
587 * @arg: argument passed on to compare function
588 *
589 * Traverses the bucket chain behind the provided hash value and calls the
590 * specified compare function for each entry.
591 *
Thomas Graf97defe12015-01-02 23:00:20 +0100592 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200593 *
594 * Returns the first entry on which the compare function returned true.
595 */
Thomas Graf97defe12015-01-02 23:00:20 +0100596void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200597 bool (*compare)(void *, void *), void *arg)
598{
Thomas Graf97defe12015-01-02 23:00:20 +0100599 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200600 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100601 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200602
Thomas Graf97defe12015-01-02 23:00:20 +0100603 rcu_read_lock();
604
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100605 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100606restart:
Herbert Xu39361942015-03-13 12:54:10 +1100607 hash = key_hashfn(ht, tbl, key);
Herbert Xu8d2b1872015-03-12 14:49:38 +1100608 rht_for_each_rcu(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200609 if (!compare(rht_obj(ht, he), arg))
610 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100611 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100612 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200613 }
614
Herbert Xu9497df82015-03-12 22:07:49 +1100615 /* Ensure we see any new tables. */
616 smp_rmb();
617
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100618 old_tbl = tbl;
619 tbl = rht_dereference_rcu(ht->future_tbl, ht);
620 if (unlikely(tbl != old_tbl))
Thomas Graf97defe12015-01-02 23:00:20 +0100621 goto restart;
Thomas Graf97defe12015-01-02 23:00:20 +0100622 rcu_read_unlock();
623
Thomas Graf7e1e7762014-08-02 11:47:44 +0200624 return NULL;
625}
626EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
627
Ying Xuedb304852015-01-07 13:41:54 +0800628/**
629 * rhashtable_lookup_insert - lookup and insert object into hash table
630 * @ht: hash table
631 * @obj: pointer to hash head inside object
632 *
633 * Locks down the bucket chain in both the old and new table if a resize
634 * is in progress to ensure that writers can't remove from the old table
635 * and can't insert to the new table during the atomic operation of search
636 * and insertion. Searches for duplicates in both the old and new table if
637 * a resize is in progress.
638 *
639 * This lookup function may only be used for fixed key hash table (key_len
640 * parameter set). It will BUG() if used inappropriately.
641 *
642 * It is safe to call this function from atomic context.
643 *
644 * Will trigger an automatic deferred table resizing if the size grows
645 * beyond the watermark indicated by grow_decision() which can be passed
646 * to rhashtable_init().
647 */
648bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
649{
Ying Xue7a868d12015-01-12 14:52:22 +0800650 struct rhashtable_compare_arg arg = {
651 .ht = ht,
652 .key = rht_obj(ht, obj) + ht->p.key_offset,
653 };
654
655 BUG_ON(!ht->p.key_len);
656
657 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
658 &arg);
659}
660EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
661
662/**
663 * rhashtable_lookup_compare_insert - search and insert object to hash table
664 * with compare function
665 * @ht: hash table
666 * @obj: pointer to hash head inside object
667 * @compare: compare function, must return true on match
668 * @arg: argument passed on to compare function
669 *
670 * Locks down the bucket chain in both the old and new table if a resize
671 * is in progress to ensure that writers can't remove from the old table
672 * and can't insert to the new table during the atomic operation of search
673 * and insertion. Searches for duplicates in both the old and new table if
674 * a resize is in progress.
675 *
676 * Lookups may occur in parallel with hashtable mutations and resizing.
677 *
678 * Will trigger an automatic deferred table resizing if the size grows
679 * beyond the watermark indicated by grow_decision() which can be passed
680 * to rhashtable_init().
681 */
682bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
683 struct rhash_head *obj,
684 bool (*compare)(void *, void *),
685 void *arg)
686{
Ying Xuedb304852015-01-07 13:41:54 +0800687 BUG_ON(!ht->p.key_len);
688
Herbert Xuaa34a6cb02015-03-11 09:43:48 +1100689 return __rhashtable_insert(ht, obj, compare, arg);
Ying Xuedb304852015-01-07 13:41:54 +0800690}
Ying Xue7a868d12015-01-12 14:52:22 +0800691EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800692
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100693/**
694 * rhashtable_walk_init - Initialise an iterator
695 * @ht: Table to walk over
696 * @iter: Hash table Iterator
697 *
698 * This function prepares a hash table walk.
699 *
700 * Note that if you restart a walk after rhashtable_walk_stop you
701 * may see the same object twice. Also, you may miss objects if
702 * there are removals in between rhashtable_walk_stop and the next
703 * call to rhashtable_walk_start.
704 *
705 * For a completely stable walk you should construct your own data
706 * structure outside the hash table.
707 *
708 * This function may sleep so you must not call it from interrupt
709 * context or with spin locks held.
710 *
711 * You must call rhashtable_walk_exit if this function returns
712 * successfully.
713 */
714int rhashtable_walk_init(struct rhashtable *ht, struct rhashtable_iter *iter)
715{
716 iter->ht = ht;
717 iter->p = NULL;
718 iter->slot = 0;
719 iter->skip = 0;
720
721 iter->walker = kmalloc(sizeof(*iter->walker), GFP_KERNEL);
722 if (!iter->walker)
723 return -ENOMEM;
724
725 mutex_lock(&ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100726 iter->walker->tbl = rht_dereference(ht->tbl, ht);
727 list_add(&iter->walker->list, &iter->walker->tbl->walkers);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100728 mutex_unlock(&ht->mutex);
729
730 return 0;
731}
732EXPORT_SYMBOL_GPL(rhashtable_walk_init);
733
734/**
735 * rhashtable_walk_exit - Free an iterator
736 * @iter: Hash table Iterator
737 *
738 * This function frees resources allocated by rhashtable_walk_init.
739 */
740void rhashtable_walk_exit(struct rhashtable_iter *iter)
741{
742 mutex_lock(&iter->ht->mutex);
Herbert Xueddee5ba2015-03-14 13:57:20 +1100743 if (iter->walker->tbl)
744 list_del(&iter->walker->list);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100745 mutex_unlock(&iter->ht->mutex);
746 kfree(iter->walker);
747}
748EXPORT_SYMBOL_GPL(rhashtable_walk_exit);
749
750/**
751 * rhashtable_walk_start - Start a hash table walk
752 * @iter: Hash table iterator
753 *
754 * Start a hash table walk. Note that we take the RCU lock in all
755 * cases including when we return an error. So you must always call
756 * rhashtable_walk_stop to clean up.
757 *
758 * Returns zero if successful.
759 *
760 * Returns -EAGAIN if resize event occured. Note that the iterator
761 * will rewind back to the beginning and you may use it immediately
762 * by calling rhashtable_walk_next.
763 */
764int rhashtable_walk_start(struct rhashtable_iter *iter)
765{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100766 struct rhashtable *ht = iter->ht;
767
768 mutex_lock(&ht->mutex);
769
770 if (iter->walker->tbl)
771 list_del(&iter->walker->list);
772
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100773 rcu_read_lock();
774
Herbert Xueddee5ba2015-03-14 13:57:20 +1100775 mutex_unlock(&ht->mutex);
776
777 if (!iter->walker->tbl) {
778 iter->walker->tbl = rht_dereference_rcu(ht->tbl, ht);
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100779 return -EAGAIN;
780 }
781
782 return 0;
783}
784EXPORT_SYMBOL_GPL(rhashtable_walk_start);
785
786/**
787 * rhashtable_walk_next - Return the next object and advance the iterator
788 * @iter: Hash table iterator
789 *
790 * Note that you must call rhashtable_walk_stop when you are finished
791 * with the walk.
792 *
793 * Returns the next object or NULL when the end of the table is reached.
794 *
795 * Returns -EAGAIN if resize event occured. Note that the iterator
796 * will rewind back to the beginning and you may continue to use it.
797 */
798void *rhashtable_walk_next(struct rhashtable_iter *iter)
799{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100800 struct bucket_table *tbl = iter->walker->tbl;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100801 struct rhashtable *ht = iter->ht;
802 struct rhash_head *p = iter->p;
803 void *obj = NULL;
804
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100805 if (p) {
806 p = rht_dereference_bucket_rcu(p->next, tbl, iter->slot);
807 goto next;
808 }
809
810 for (; iter->slot < tbl->size; iter->slot++) {
811 int skip = iter->skip;
812
813 rht_for_each_rcu(p, tbl, iter->slot) {
814 if (!skip)
815 break;
816 skip--;
817 }
818
819next:
820 if (!rht_is_a_nulls(p)) {
821 iter->skip++;
822 iter->p = p;
823 obj = rht_obj(ht, p);
824 goto out;
825 }
826
827 iter->skip = 0;
828 }
829
Herbert Xueddee5ba2015-03-14 13:57:20 +1100830 iter->walker->tbl = rht_dereference_rcu(ht->future_tbl, ht);
831 if (iter->walker->tbl != tbl) {
832 iter->slot = 0;
833 iter->skip = 0;
834 return ERR_PTR(-EAGAIN);
835 }
836
837 iter->walker->tbl = NULL;
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100838 iter->p = NULL;
839
840out:
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100841
842 return obj;
843}
844EXPORT_SYMBOL_GPL(rhashtable_walk_next);
845
846/**
847 * rhashtable_walk_stop - Finish a hash table walk
848 * @iter: Hash table iterator
849 *
850 * Finish a hash table walk.
851 */
852void rhashtable_walk_stop(struct rhashtable_iter *iter)
853{
Herbert Xueddee5ba2015-03-14 13:57:20 +1100854 struct rhashtable *ht;
855 struct bucket_table *tbl = iter->walker->tbl;
856
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100857 rcu_read_unlock();
Herbert Xueddee5ba2015-03-14 13:57:20 +1100858
859 if (!tbl)
860 return;
861
862 ht = iter->ht;
863
864 mutex_lock(&ht->mutex);
865 if (rht_dereference(ht->tbl, ht) == tbl ||
866 rht_dereference(ht->future_tbl, ht) == tbl)
867 list_add(&iter->walker->list, &tbl->walkers);
868 else
869 iter->walker->tbl = NULL;
870 mutex_unlock(&ht->mutex);
871
Herbert Xuf2dba9c2015-02-04 07:33:23 +1100872 iter->p = NULL;
873}
874EXPORT_SYMBOL_GPL(rhashtable_walk_stop);
875
Ying Xue94000172014-09-03 09:22:36 +0800876static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200877{
Ying Xue94000172014-09-03 09:22:36 +0800878 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
879 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200880}
881
882/**
883 * rhashtable_init - initialize a new hash table
884 * @ht: hash table to be initialized
885 * @params: configuration parameters
886 *
887 * Initializes a new hash table based on the provided configuration
888 * parameters. A table can be configured either with a variable or
889 * fixed length key:
890 *
891 * Configuration Example 1: Fixed length keys
892 * struct test_obj {
893 * int key;
894 * void * my_member;
895 * struct rhash_head node;
896 * };
897 *
898 * struct rhashtable_params params = {
899 * .head_offset = offsetof(struct test_obj, node),
900 * .key_offset = offsetof(struct test_obj, key),
901 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100902 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100903 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200904 * };
905 *
906 * Configuration Example 2: Variable length keys
907 * struct test_obj {
908 * [...]
909 * struct rhash_head node;
910 * };
911 *
912 * u32 my_hash_fn(const void *data, u32 seed)
913 * {
914 * struct test_obj *obj = data;
915 *
916 * return [... hash ...];
917 * }
918 *
919 * struct rhashtable_params params = {
920 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100921 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200922 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200923 * };
924 */
925int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
926{
927 struct bucket_table *tbl;
928 size_t size;
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100929 u32 hash_rnd;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200930
931 size = HASH_DEFAULT_SIZE;
932
933 if ((params->key_len && !params->hashfn) ||
934 (!params->key_len && !params->obj_hashfn))
935 return -EINVAL;
936
Thomas Graff89bd6f2015-01-02 23:00:21 +0100937 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
938 return -EINVAL;
939
Ying Xue94000172014-09-03 09:22:36 +0800940 params->min_shift = max_t(size_t, params->min_shift,
941 ilog2(HASH_MIN_SIZE));
942
Thomas Graf7e1e7762014-08-02 11:47:44 +0200943 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800944 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200945
Thomas Graf97defe12015-01-02 23:00:20 +0100946 memset(ht, 0, sizeof(*ht));
947 mutex_init(&ht->mutex);
948 memcpy(&ht->p, params, sizeof(*params));
949
950 if (params->locks_mul)
951 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
952 else
953 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
954
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100955 get_random_bytes(&hash_rnd, sizeof(hash_rnd));
956
957 tbl = bucket_table_alloc(ht, size, hash_rnd);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200958 if (tbl == NULL)
959 return -ENOMEM;
960
Ying Xue545a1482015-01-07 13:41:57 +0800961 atomic_set(&ht->nelems, 0);
Daniel Borkmanna5b68462015-03-12 15:28:40 +0100962
Thomas Graf7e1e7762014-08-02 11:47:44 +0200963 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100964 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200965
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100966 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100967
Thomas Graf7e1e7762014-08-02 11:47:44 +0200968 return 0;
969}
970EXPORT_SYMBOL_GPL(rhashtable_init);
971
972/**
973 * rhashtable_destroy - destroy hash table
974 * @ht: the hash table to destroy
975 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200976 * Frees the bucket array. This function is not rcu safe, therefore the caller
977 * has to make sure that no resizing may happen by unpublishing the hashtable
978 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200979 */
Thomas Graf97defe12015-01-02 23:00:20 +0100980void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200981{
Thomas Graf97defe12015-01-02 23:00:20 +0100982 ht->being_destroyed = true;
983
Daniel Borkmann4c4b52d2015-02-25 16:31:54 +0100984 cancel_work_sync(&ht->run_work);
Ying Xue57699a42015-01-16 11:13:09 +0800985
Thomas Graf97defe12015-01-02 23:00:20 +0100986 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100987 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100988 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200989}
990EXPORT_SYMBOL_GPL(rhashtable_destroy);