blob: 84a78e396a566c96efb723eb3a35598a73a468f8 [file] [log] [blame]
Thomas Graf7e1e7762014-08-02 11:47:44 +02001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5 * 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>
20#include <linux/slab.h>
21#include <linux/vmalloc.h>
22#include <linux/mm.h>
Daniel Borkmann87545892014-12-10 16:33:11 +010023#include <linux/jhash.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020024#include <linux/random.h>
25#include <linux/rhashtable.h>
Thomas Graf7e1e7762014-08-02 11:47:44 +020026
27#define HASH_DEFAULT_SIZE 64UL
28#define HASH_MIN_SIZE 4UL
Thomas Graf97defe12015-01-02 23:00:20 +010029#define BUCKET_LOCKS_PER_CPU 128UL
30
Thomas Graff89bd6f2015-01-02 23:00:21 +010031/* Base bits plus 1 bit for nulls marker */
32#define HASH_RESERVED_SPACE (RHT_BASE_BITS + 1)
33
Thomas Graf97defe12015-01-02 23:00:20 +010034enum {
35 RHT_LOCK_NORMAL,
36 RHT_LOCK_NESTED,
37 RHT_LOCK_NESTED2,
38};
39
40/* The bucket lock is selected based on the hash and protects mutations
41 * on a group of hash buckets.
42 *
43 * IMPORTANT: When holding the bucket lock of both the old and new table
44 * during expansions and shrinking, the old bucket lock must always be
45 * acquired first.
46 */
47static spinlock_t *bucket_lock(const struct bucket_table *tbl, u32 hash)
48{
49 return &tbl->locks[hash & tbl->locks_mask];
50}
Thomas Graf7e1e7762014-08-02 11:47:44 +020051
52#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
Thomas Graf97defe12015-01-02 23:00:20 +010053#define ASSERT_BUCKET_LOCK(TBL, HASH) \
54 BUG_ON(!lockdep_rht_bucket_is_held(TBL, HASH))
Thomas Graf7e1e7762014-08-02 11:47:44 +020055
56#ifdef CONFIG_PROVE_LOCKING
Thomas Graf97defe12015-01-02 23:00:20 +010057int lockdep_rht_mutex_is_held(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +020058{
Thomas Graf97defe12015-01-02 23:00:20 +010059 return (debug_locks) ? lockdep_is_held(&ht->mutex) : 1;
Thomas Graf7e1e7762014-08-02 11:47:44 +020060}
61EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
Thomas Graf88d6ed12015-01-02 23:00:16 +010062
63int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
64{
Thomas Graf97defe12015-01-02 23:00:20 +010065 spinlock_t *lock = bucket_lock(tbl, hash);
66
67 return (debug_locks) ? lockdep_is_held(lock) : 1;
Thomas Graf88d6ed12015-01-02 23:00:16 +010068}
69EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
Thomas Graf7e1e7762014-08-02 11:47:44 +020070#endif
71
Thomas Grafc91eee52014-08-13 16:38:30 +020072static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020073{
74 return (void *) he - ht->p.head_offset;
75}
Thomas Graf7e1e7762014-08-02 11:47:44 +020076
Thomas Graf8d24c0b2015-01-02 23:00:14 +010077static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020078{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010079 return hash & (tbl->size - 1);
Thomas Graf7e1e7762014-08-02 11:47:44 +020080}
81
Thomas Graf8d24c0b2015-01-02 23:00:14 +010082static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
83{
84 u32 hash;
85
86 if (unlikely(!ht->p.key_len))
87 hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
88 else
89 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
90 ht->p.hash_rnd);
91
Thomas Graff89bd6f2015-01-02 23:00:21 +010092 return hash >> HASH_RESERVED_SPACE;
Thomas Graf8d24c0b2015-01-02 23:00:14 +010093}
94
Thomas Graf97defe12015-01-02 23:00:20 +010095static u32 key_hashfn(struct rhashtable *ht, const void *key, u32 len)
Thomas Graf7e1e7762014-08-02 11:47:44 +020096{
97 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010098 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +020099
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100100 hash = ht->p.hashfn(key, len, ht->p.hash_rnd);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100101 hash >>= HASH_RESERVED_SPACE;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100102
103 return rht_bucket_index(tbl, hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200104}
Thomas Graf7e1e7762014-08-02 11:47:44 +0200105
106static u32 head_hashfn(const struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100107 const struct bucket_table *tbl,
108 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200109{
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100110 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200111}
112
Thomas Grafb8e19432015-01-02 23:00:17 +0100113static struct rhash_head __rcu **bucket_tail(struct bucket_table *tbl, u32 n)
114{
115 struct rhash_head __rcu **pprev;
116
117 for (pprev = &tbl->buckets[n];
Thomas Graff89bd6f2015-01-02 23:00:21 +0100118 !rht_is_a_nulls(rht_dereference_bucket(*pprev, tbl, n));
Thomas Grafb8e19432015-01-02 23:00:17 +0100119 pprev = &rht_dereference_bucket(*pprev, tbl, n)->next)
120 ;
121
122 return pprev;
123}
124
Thomas Graf97defe12015-01-02 23:00:20 +0100125static int alloc_bucket_locks(struct rhashtable *ht, struct bucket_table *tbl)
126{
127 unsigned int i, size;
128#if defined(CONFIG_PROVE_LOCKING)
129 unsigned int nr_pcpus = 2;
130#else
131 unsigned int nr_pcpus = num_possible_cpus();
132#endif
133
134 nr_pcpus = min_t(unsigned int, nr_pcpus, 32UL);
135 size = roundup_pow_of_two(nr_pcpus * ht->p.locks_mul);
136
137 /* Never allocate more than one lock per bucket */
138 size = min_t(unsigned int, size, tbl->size);
139
140 if (sizeof(spinlock_t) != 0) {
141#ifdef CONFIG_NUMA
142 if (size * sizeof(spinlock_t) > PAGE_SIZE)
143 tbl->locks = vmalloc(size * sizeof(spinlock_t));
144 else
145#endif
146 tbl->locks = kmalloc_array(size, sizeof(spinlock_t),
147 GFP_KERNEL);
148 if (!tbl->locks)
149 return -ENOMEM;
150 for (i = 0; i < size; i++)
151 spin_lock_init(&tbl->locks[i]);
152 }
153 tbl->locks_mask = size - 1;
154
155 return 0;
156}
157
158static void bucket_table_free(const struct bucket_table *tbl)
159{
160 if (tbl)
161 kvfree(tbl->locks);
162
163 kvfree(tbl);
164}
165
166static struct bucket_table *bucket_table_alloc(struct rhashtable *ht,
167 size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200168{
169 struct bucket_table *tbl;
170 size_t size;
Thomas Graff89bd6f2015-01-02 23:00:21 +0100171 int i;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200172
173 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Thomas Graf6eba8222014-11-13 13:45:46 +0100174 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200175 if (tbl == NULL)
176 tbl = vzalloc(size);
177
178 if (tbl == NULL)
179 return NULL;
180
181 tbl->size = nbuckets;
182
Thomas Graf97defe12015-01-02 23:00:20 +0100183 if (alloc_bucket_locks(ht, tbl) < 0) {
184 bucket_table_free(tbl);
185 return NULL;
186 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200187
Thomas Graff89bd6f2015-01-02 23:00:21 +0100188 for (i = 0; i < nbuckets; i++)
189 INIT_RHT_NULLS_HEAD(tbl->buckets[i], ht, i);
190
Thomas Graf97defe12015-01-02 23:00:20 +0100191 return tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200192}
193
194/**
195 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
196 * @ht: hash table
197 * @new_size: new table size
198 */
199bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
200{
201 /* Expand table when exceeding 75% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800202 return atomic_read(&ht->nelems) > (new_size / 4 * 3) &&
203 (ht->p.max_shift && atomic_read(&ht->shift) < ht->p.max_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200204}
205EXPORT_SYMBOL_GPL(rht_grow_above_75);
206
207/**
208 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
209 * @ht: hash table
210 * @new_size: new table size
211 */
212bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
213{
214 /* Shrink table beneath 30% load */
Ying Xuec0c09bf2015-01-07 13:41:56 +0800215 return atomic_read(&ht->nelems) < (new_size * 3 / 10) &&
216 (atomic_read(&ht->shift) > ht->p.min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200217}
218EXPORT_SYMBOL_GPL(rht_shrink_below_30);
219
220static void hashtable_chain_unzip(const struct rhashtable *ht,
221 const struct bucket_table *new_tbl,
Thomas Graf97defe12015-01-02 23:00:20 +0100222 struct bucket_table *old_tbl,
223 size_t old_hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200224{
225 struct rhash_head *he, *p, *next;
Thomas Graf97defe12015-01-02 23:00:20 +0100226 spinlock_t *new_bucket_lock, *new_bucket_lock2 = NULL;
227 unsigned int new_hash, new_hash2;
228
229 ASSERT_BUCKET_LOCK(old_tbl, old_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200230
231 /* Old bucket empty, no work needed. */
Thomas Graf97defe12015-01-02 23:00:20 +0100232 p = rht_dereference_bucket(old_tbl->buckets[old_hash], old_tbl,
233 old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100234 if (rht_is_a_nulls(p))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200235 return;
236
Thomas Graf97defe12015-01-02 23:00:20 +0100237 new_hash = new_hash2 = head_hashfn(ht, new_tbl, p);
238 new_bucket_lock = bucket_lock(new_tbl, new_hash);
239
Thomas Graf7e1e7762014-08-02 11:47:44 +0200240 /* Advance the old bucket pointer one or more times until it
241 * reaches a node that doesn't hash to the same bucket as the
242 * previous node p. Call the previous node p;
243 */
Thomas Graf97defe12015-01-02 23:00:20 +0100244 rht_for_each_continue(he, p->next, old_tbl, old_hash) {
245 new_hash2 = head_hashfn(ht, new_tbl, he);
246 if (new_hash != new_hash2)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200247 break;
248 p = he;
249 }
Thomas Graf97defe12015-01-02 23:00:20 +0100250 rcu_assign_pointer(old_tbl->buckets[old_hash], p->next);
251
252 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
253
254 /* If we have encountered an entry that maps to a different bucket in
255 * the new table, lock down that bucket as well as we might cut off
256 * the end of the chain.
257 */
258 new_bucket_lock2 = bucket_lock(new_tbl, new_hash);
259 if (new_bucket_lock != new_bucket_lock2)
260 spin_lock_bh_nested(new_bucket_lock2, RHT_LOCK_NESTED2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200261
262 /* Find the subsequent node which does hash to the same
263 * bucket as node P, or NULL if no such node exists.
264 */
Thomas Graff89bd6f2015-01-02 23:00:21 +0100265 INIT_RHT_NULLS_HEAD(next, ht, old_hash);
266 if (!rht_is_a_nulls(he)) {
Thomas Graf97defe12015-01-02 23:00:20 +0100267 rht_for_each_continue(he, he->next, old_tbl, old_hash) {
268 if (head_hashfn(ht, new_tbl, he) == new_hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200269 next = he;
270 break;
271 }
272 }
273 }
274
275 /* Set p's next pointer to that subsequent node pointer,
276 * bypassing the nodes which do not hash to p's bucket
277 */
Thomas Graf97defe12015-01-02 23:00:20 +0100278 rcu_assign_pointer(p->next, next);
279
280 if (new_bucket_lock != new_bucket_lock2)
281 spin_unlock_bh(new_bucket_lock2);
282 spin_unlock_bh(new_bucket_lock);
283}
284
285static void link_old_to_new(struct bucket_table *new_tbl,
286 unsigned int new_hash, struct rhash_head *entry)
287{
288 spinlock_t *new_bucket_lock;
289
290 new_bucket_lock = bucket_lock(new_tbl, new_hash);
291
292 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
293 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash), entry);
294 spin_unlock_bh(new_bucket_lock);
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 *
301 * A secondary bucket array is allocated and the hash entries are migrated
302 * while keeping them on both lists until the end of the RCU grace period.
303 *
304 * This function may only be called in a context where it is safe to call
305 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
306 *
Thomas Graf97defe12015-01-02 23:00:20 +0100307 * The caller must ensure that no concurrent resizing occurs by holding
308 * ht->mutex.
309 *
310 * It is valid to have concurrent insertions and deletions protected by per
311 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200312 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100313int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200314{
315 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
316 struct rhash_head *he;
Thomas Graf97defe12015-01-02 23:00:20 +0100317 spinlock_t *old_bucket_lock;
318 unsigned int new_hash, old_hash;
319 bool complete = false;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200320
321 ASSERT_RHT_MUTEX(ht);
322
Thomas Graf97defe12015-01-02 23:00:20 +0100323 new_tbl = bucket_table_alloc(ht, old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200324 if (new_tbl == NULL)
325 return -ENOMEM;
326
Ying Xuec0c09bf2015-01-07 13:41:56 +0800327 atomic_inc(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200328
Thomas Graf97defe12015-01-02 23:00:20 +0100329 /* Make insertions go into the new, empty table right away. Deletions
330 * and lookups will be attempted in both tables until we synchronize.
331 * The synchronize_rcu() guarantees for the new table to be picked up
332 * so no new additions go into the old table while we relink.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200333 */
Thomas Graf97defe12015-01-02 23:00:20 +0100334 rcu_assign_pointer(ht->future_tbl, new_tbl);
335 synchronize_rcu();
336
337 /* For each new bucket, search the corresponding old bucket for the
338 * first entry that hashes to the new bucket, and link the end of
339 * newly formed bucket chain (containing entries added to future
340 * table) to that entry. Since all the entries which will end up in
341 * the new bucket appear in the same old bucket, this constructs an
342 * entirely valid new hash table, but with multiple buckets
343 * "zipped" together into a single imprecise chain.
344 */
345 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
346 old_hash = rht_bucket_index(old_tbl, new_hash);
347 old_bucket_lock = bucket_lock(old_tbl, old_hash);
348
349 spin_lock_bh(old_bucket_lock);
350 rht_for_each(he, old_tbl, old_hash) {
351 if (head_hashfn(ht, new_tbl, he) == new_hash) {
352 link_old_to_new(new_tbl, new_hash, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200353 break;
354 }
355 }
Thomas Graf97defe12015-01-02 23:00:20 +0100356 spin_unlock_bh(old_bucket_lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200357 }
358
359 /* Publish the new table pointer. Lookups may now traverse
Herbert Xu0c828f22014-11-13 13:10:48 +0800360 * the new table, but they will not benefit from any
361 * additional efficiency until later steps unzip the buckets.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200362 */
363 rcu_assign_pointer(ht->tbl, new_tbl);
364
365 /* Unzip interleaved hash chains */
Thomas Graf97defe12015-01-02 23:00:20 +0100366 while (!complete && !ht->being_destroyed) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200367 /* Wait for readers. All new readers will see the new
368 * table, and thus no references to the old table will
369 * remain.
370 */
371 synchronize_rcu();
372
373 /* For each bucket in the old table (each of which
374 * contains items from multiple buckets of the new
375 * table): ...
376 */
377 complete = true;
Thomas Graf97defe12015-01-02 23:00:20 +0100378 for (old_hash = 0; old_hash < old_tbl->size; old_hash++) {
Thomas Graff89bd6f2015-01-02 23:00:21 +0100379 struct rhash_head *head;
380
Thomas Graf97defe12015-01-02 23:00:20 +0100381 old_bucket_lock = bucket_lock(old_tbl, old_hash);
382 spin_lock_bh(old_bucket_lock);
383
384 hashtable_chain_unzip(ht, new_tbl, old_tbl, old_hash);
Thomas Graff89bd6f2015-01-02 23:00:21 +0100385 head = rht_dereference_bucket(old_tbl->buckets[old_hash],
386 old_tbl, old_hash);
387 if (!rht_is_a_nulls(head))
Thomas Graf7e1e7762014-08-02 11:47:44 +0200388 complete = false;
Thomas Graf97defe12015-01-02 23:00:20 +0100389
390 spin_unlock_bh(old_bucket_lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200391 }
Thomas Graf97defe12015-01-02 23:00:20 +0100392 }
Thomas Graf7e1e7762014-08-02 11:47:44 +0200393
394 bucket_table_free(old_tbl);
395 return 0;
396}
397EXPORT_SYMBOL_GPL(rhashtable_expand);
398
399/**
400 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
401 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200402 *
403 * This function may only be called in a context where it is safe to call
404 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
405 *
Thomas Graf97defe12015-01-02 23:00:20 +0100406 * The caller must ensure that no concurrent resizing occurs by holding
407 * ht->mutex.
408 *
Thomas Graf7e1e7762014-08-02 11:47:44 +0200409 * The caller must ensure that no concurrent table mutations take place.
410 * It is however valid to have concurrent lookups if they are RCU protected.
Thomas Graf97defe12015-01-02 23:00:20 +0100411 *
412 * It is valid to have concurrent insertions and deletions protected by per
413 * bucket locks or concurrent RCU protected lookups and traversals.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200414 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100415int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200416{
Thomas Graf97defe12015-01-02 23:00:20 +0100417 struct bucket_table *new_tbl, *tbl = rht_dereference(ht->tbl, ht);
418 spinlock_t *new_bucket_lock, *old_bucket_lock1, *old_bucket_lock2;
419 unsigned int new_hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200420
421 ASSERT_RHT_MUTEX(ht);
422
Thomas Graf97defe12015-01-02 23:00:20 +0100423 new_tbl = bucket_table_alloc(ht, tbl->size / 2);
424 if (new_tbl == NULL)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200425 return -ENOMEM;
426
Thomas Graf97defe12015-01-02 23:00:20 +0100427 rcu_assign_pointer(ht->future_tbl, new_tbl);
428 synchronize_rcu();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200429
Thomas Graf97defe12015-01-02 23:00:20 +0100430 /* Link the first entry in the old bucket to the end of the
431 * bucket in the new table. As entries are concurrently being
432 * added to the new table, lock down the new bucket. As we
433 * always divide the size in half when shrinking, each bucket
434 * in the new table maps to exactly two buckets in the old
435 * table.
436 *
437 * As removals can occur concurrently on the old table, we need
438 * to lock down both matching buckets in the old table.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200439 */
Thomas Graf97defe12015-01-02 23:00:20 +0100440 for (new_hash = 0; new_hash < new_tbl->size; new_hash++) {
441 old_bucket_lock1 = bucket_lock(tbl, new_hash);
442 old_bucket_lock2 = bucket_lock(tbl, new_hash + new_tbl->size);
443 new_bucket_lock = bucket_lock(new_tbl, new_hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200444
Thomas Graf97defe12015-01-02 23:00:20 +0100445 spin_lock_bh(old_bucket_lock1);
Thomas Graf80ca8c32015-01-12 23:58:21 +0000446
447 /* Depending on the lock per buckets mapping, the bucket in
448 * the lower and upper region may map to the same lock.
449 */
450 if (old_bucket_lock1 != old_bucket_lock2) {
451 spin_lock_bh_nested(old_bucket_lock2, RHT_LOCK_NESTED);
452 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED2);
453 } else {
454 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
455 }
Thomas Graf97defe12015-01-02 23:00:20 +0100456
457 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
458 tbl->buckets[new_hash]);
459 rcu_assign_pointer(*bucket_tail(new_tbl, new_hash),
460 tbl->buckets[new_hash + new_tbl->size]);
461
462 spin_unlock_bh(new_bucket_lock);
Thomas Graf80ca8c32015-01-12 23:58:21 +0000463 if (old_bucket_lock1 != old_bucket_lock2)
464 spin_unlock_bh(old_bucket_lock2);
Thomas Graf97defe12015-01-02 23:00:20 +0100465 spin_unlock_bh(old_bucket_lock1);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200466 }
467
468 /* Publish the new, valid hash table */
Thomas Graf97defe12015-01-02 23:00:20 +0100469 rcu_assign_pointer(ht->tbl, new_tbl);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800470 atomic_dec(&ht->shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200471
472 /* Wait for readers. No new readers will have references to the
473 * old hash table.
474 */
475 synchronize_rcu();
476
477 bucket_table_free(tbl);
478
479 return 0;
480}
481EXPORT_SYMBOL_GPL(rhashtable_shrink);
482
Thomas Graf97defe12015-01-02 23:00:20 +0100483static void rht_deferred_worker(struct work_struct *work)
484{
485 struct rhashtable *ht;
486 struct bucket_table *tbl;
487
Ying Xue57699a42015-01-16 11:13:09 +0800488 ht = container_of(work, struct rhashtable, run_work);
Thomas Graf97defe12015-01-02 23:00:20 +0100489 mutex_lock(&ht->mutex);
490 tbl = rht_dereference(ht->tbl, ht);
491
492 if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size))
493 rhashtable_expand(ht);
494 else if (ht->p.shrink_decision && ht->p.shrink_decision(ht, tbl->size))
495 rhashtable_shrink(ht);
496
497 mutex_unlock(&ht->mutex);
498}
499
Ying Xue54c5b7d2015-01-07 13:41:53 +0800500static void rhashtable_wakeup_worker(struct rhashtable *ht)
501{
502 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
503 struct bucket_table *new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
504 size_t size = tbl->size;
505
506 /* Only adjust the table if no resizing is currently in progress. */
507 if (tbl == new_tbl &&
508 ((ht->p.grow_decision && ht->p.grow_decision(ht, size)) ||
509 (ht->p.shrink_decision && ht->p.shrink_decision(ht, size))))
Ying Xue57699a42015-01-16 11:13:09 +0800510 schedule_work(&ht->run_work);
Ying Xue54c5b7d2015-01-07 13:41:53 +0800511}
512
Ying Xuedb304852015-01-07 13:41:54 +0800513static void __rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj,
514 struct bucket_table *tbl, u32 hash)
515{
516 struct rhash_head *head = rht_dereference_bucket(tbl->buckets[hash],
517 tbl, hash);
518
519 if (rht_is_a_nulls(head))
520 INIT_RHT_NULLS_HEAD(obj->next, ht, hash);
521 else
522 RCU_INIT_POINTER(obj->next, head);
523
524 rcu_assign_pointer(tbl->buckets[hash], obj);
525
526 atomic_inc(&ht->nelems);
527
528 rhashtable_wakeup_worker(ht);
529}
530
Thomas Graf7e1e7762014-08-02 11:47:44 +0200531/**
Ying Xuedb304852015-01-07 13:41:54 +0800532 * rhashtable_insert - insert object into hash table
Thomas Graf7e1e7762014-08-02 11:47:44 +0200533 * @ht: hash table
534 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200535 *
Thomas Graf97defe12015-01-02 23:00:20 +0100536 * Will take a per bucket spinlock to protect against mutual mutations
537 * on the same bucket. Multiple insertions may occur in parallel unless
538 * they map to the same bucket lock.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200539 *
Thomas Graf97defe12015-01-02 23:00:20 +0100540 * It is safe to call this function from atomic context.
541 *
542 * Will trigger an automatic deferred table resizing if the size grows
543 * beyond the watermark indicated by grow_decision() which can be passed
544 * to rhashtable_init().
Thomas Graf7e1e7762014-08-02 11:47:44 +0200545 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100546void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200547{
Thomas Graf97defe12015-01-02 23:00:20 +0100548 struct bucket_table *tbl;
549 spinlock_t *lock;
550 unsigned hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200551
Thomas Graf97defe12015-01-02 23:00:20 +0100552 rcu_read_lock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200553
Thomas Graf97defe12015-01-02 23:00:20 +0100554 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100555 hash = head_hashfn(ht, tbl, obj);
Thomas Graf97defe12015-01-02 23:00:20 +0100556 lock = bucket_lock(tbl, hash);
557
558 spin_lock_bh(lock);
Ying Xuedb304852015-01-07 13:41:54 +0800559 __rhashtable_insert(ht, obj, tbl, hash);
Thomas Graf97defe12015-01-02 23:00:20 +0100560 spin_unlock_bh(lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200561
Thomas Graf97defe12015-01-02 23:00:20 +0100562 rcu_read_unlock();
Thomas Graf7e1e7762014-08-02 11:47:44 +0200563}
564EXPORT_SYMBOL_GPL(rhashtable_insert);
565
566/**
Thomas Graf7e1e7762014-08-02 11:47:44 +0200567 * rhashtable_remove - remove object from hash table
568 * @ht: hash table
569 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200570 *
571 * Since the hash chain is single linked, the removal operation needs to
572 * walk the bucket chain upon removal. The removal operation is thus
573 * considerable slow if the hash table is not correctly sized.
574 *
Ying Xuedb304852015-01-07 13:41:54 +0800575 * Will automatically shrink the table via rhashtable_expand() if the
Thomas Graf7e1e7762014-08-02 11:47:44 +0200576 * shrink_decision function specified at rhashtable_init() returns true.
577 *
578 * The caller must ensure that no concurrent table mutations occur. It is
579 * however valid to have concurrent lookups if they are RCU protected.
580 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100581bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200582{
Thomas Graf97defe12015-01-02 23:00:20 +0100583 struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200584 struct rhash_head __rcu **pprev;
585 struct rhash_head *he;
Thomas Graf97defe12015-01-02 23:00:20 +0100586 spinlock_t *lock;
587 unsigned int hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200588
Thomas Graf97defe12015-01-02 23:00:20 +0100589 rcu_read_lock();
590 tbl = rht_dereference_rcu(ht->tbl, ht);
591 hash = head_hashfn(ht, tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200592
Thomas Graf97defe12015-01-02 23:00:20 +0100593 lock = bucket_lock(tbl, hash);
594 spin_lock_bh(lock);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200595
Thomas Graf97defe12015-01-02 23:00:20 +0100596restart:
597 pprev = &tbl->buckets[hash];
598 rht_for_each(he, tbl, hash) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200599 if (he != obj) {
600 pprev = &he->next;
601 continue;
602 }
603
Thomas Graf97defe12015-01-02 23:00:20 +0100604 rcu_assign_pointer(*pprev, obj->next);
605 atomic_dec(&ht->nelems);
Thomas Graf897362e2015-01-02 23:00:18 +0100606
Thomas Graf97defe12015-01-02 23:00:20 +0100607 spin_unlock_bh(lock);
608
Ying Xue54c5b7d2015-01-07 13:41:53 +0800609 rhashtable_wakeup_worker(ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100610
611 rcu_read_unlock();
Thomas Graf897362e2015-01-02 23:00:18 +0100612
Thomas Graf7e1e7762014-08-02 11:47:44 +0200613 return true;
614 }
615
Ying Xuebd6d4db2015-01-07 13:41:55 +0800616 if (tbl != rht_dereference_rcu(ht->future_tbl, ht)) {
Thomas Graf97defe12015-01-02 23:00:20 +0100617 spin_unlock_bh(lock);
618
Ying Xuebd6d4db2015-01-07 13:41:55 +0800619 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf97defe12015-01-02 23:00:20 +0100620 hash = head_hashfn(ht, tbl, obj);
621
622 lock = bucket_lock(tbl, hash);
623 spin_lock_bh(lock);
624 goto restart;
625 }
626
627 spin_unlock_bh(lock);
628 rcu_read_unlock();
629
Thomas Graf7e1e7762014-08-02 11:47:44 +0200630 return false;
631}
632EXPORT_SYMBOL_GPL(rhashtable_remove);
633
Ying Xueefb975a62015-01-07 13:41:52 +0800634struct rhashtable_compare_arg {
635 struct rhashtable *ht;
636 const void *key;
637};
638
639static bool rhashtable_compare(void *ptr, void *arg)
640{
641 struct rhashtable_compare_arg *x = arg;
642 struct rhashtable *ht = x->ht;
643
644 return !memcmp(ptr + ht->p.key_offset, x->key, ht->p.key_len);
645}
646
Thomas Graf7e1e7762014-08-02 11:47:44 +0200647/**
648 * rhashtable_lookup - lookup key in hash table
649 * @ht: hash table
650 * @key: pointer to key
651 *
652 * Computes the hash value for the key and traverses the bucket chain looking
653 * for a entry with an identical key. The first matching entry is returned.
654 *
655 * This lookup function may only be used for fixed key hash table (key_len
Ying Xuedb304852015-01-07 13:41:54 +0800656 * parameter set). It will BUG() if used inappropriately.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200657 *
Thomas Graf97defe12015-01-02 23:00:20 +0100658 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200659 */
Thomas Graf97defe12015-01-02 23:00:20 +0100660void *rhashtable_lookup(struct rhashtable *ht, const void *key)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200661{
Ying Xueefb975a62015-01-07 13:41:52 +0800662 struct rhashtable_compare_arg arg = {
663 .ht = ht,
664 .key = key,
665 };
Thomas Graf7e1e7762014-08-02 11:47:44 +0200666
667 BUG_ON(!ht->p.key_len);
668
Ying Xueefb975a62015-01-07 13:41:52 +0800669 return rhashtable_lookup_compare(ht, key, &rhashtable_compare, &arg);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200670}
671EXPORT_SYMBOL_GPL(rhashtable_lookup);
672
673/**
674 * rhashtable_lookup_compare - search hash table with compare function
675 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100676 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200677 * @compare: compare function, must return true on match
678 * @arg: argument passed on to compare function
679 *
680 * Traverses the bucket chain behind the provided hash value and calls the
681 * specified compare function for each entry.
682 *
Thomas Graf97defe12015-01-02 23:00:20 +0100683 * Lookups may occur in parallel with hashtable mutations and resizing.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200684 *
685 * Returns the first entry on which the compare function returned true.
686 */
Thomas Graf97defe12015-01-02 23:00:20 +0100687void *rhashtable_lookup_compare(struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200688 bool (*compare)(void *, void *), void *arg)
689{
Thomas Graf97defe12015-01-02 23:00:20 +0100690 const struct bucket_table *tbl, *old_tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200691 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100692 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200693
Thomas Graf97defe12015-01-02 23:00:20 +0100694 rcu_read_lock();
695
696 old_tbl = rht_dereference_rcu(ht->tbl, ht);
697 tbl = rht_dereference_rcu(ht->future_tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100698 hash = key_hashfn(ht, key, ht->p.key_len);
Thomas Graf97defe12015-01-02 23:00:20 +0100699restart:
700 rht_for_each_rcu(he, tbl, rht_bucket_index(tbl, hash)) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200701 if (!compare(rht_obj(ht, he), arg))
702 continue;
Thomas Graf97defe12015-01-02 23:00:20 +0100703 rcu_read_unlock();
Thomas Grafa4b18cd2015-01-02 23:00:15 +0100704 return rht_obj(ht, he);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200705 }
706
Thomas Graf97defe12015-01-02 23:00:20 +0100707 if (unlikely(tbl != old_tbl)) {
708 tbl = old_tbl;
709 goto restart;
710 }
711 rcu_read_unlock();
712
Thomas Graf7e1e7762014-08-02 11:47:44 +0200713 return NULL;
714}
715EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
716
Ying Xuedb304852015-01-07 13:41:54 +0800717/**
718 * rhashtable_lookup_insert - lookup and insert object into hash table
719 * @ht: hash table
720 * @obj: pointer to hash head inside object
721 *
722 * Locks down the bucket chain in both the old and new table if a resize
723 * is in progress to ensure that writers can't remove from the old table
724 * and can't insert to the new table during the atomic operation of search
725 * and insertion. Searches for duplicates in both the old and new table if
726 * a resize is in progress.
727 *
728 * This lookup function may only be used for fixed key hash table (key_len
729 * parameter set). It will BUG() if used inappropriately.
730 *
731 * It is safe to call this function from atomic context.
732 *
733 * Will trigger an automatic deferred table resizing if the size grows
734 * beyond the watermark indicated by grow_decision() which can be passed
735 * to rhashtable_init().
736 */
737bool rhashtable_lookup_insert(struct rhashtable *ht, struct rhash_head *obj)
738{
Ying Xue7a868d12015-01-12 14:52:22 +0800739 struct rhashtable_compare_arg arg = {
740 .ht = ht,
741 .key = rht_obj(ht, obj) + ht->p.key_offset,
742 };
743
744 BUG_ON(!ht->p.key_len);
745
746 return rhashtable_lookup_compare_insert(ht, obj, &rhashtable_compare,
747 &arg);
748}
749EXPORT_SYMBOL_GPL(rhashtable_lookup_insert);
750
751/**
752 * rhashtable_lookup_compare_insert - search and insert object to hash table
753 * with compare function
754 * @ht: hash table
755 * @obj: pointer to hash head inside object
756 * @compare: compare function, must return true on match
757 * @arg: argument passed on to compare function
758 *
759 * Locks down the bucket chain in both the old and new table if a resize
760 * is in progress to ensure that writers can't remove from the old table
761 * and can't insert to the new table during the atomic operation of search
762 * and insertion. Searches for duplicates in both the old and new table if
763 * a resize is in progress.
764 *
765 * Lookups may occur in parallel with hashtable mutations and resizing.
766 *
767 * Will trigger an automatic deferred table resizing if the size grows
768 * beyond the watermark indicated by grow_decision() which can be passed
769 * to rhashtable_init().
770 */
771bool rhashtable_lookup_compare_insert(struct rhashtable *ht,
772 struct rhash_head *obj,
773 bool (*compare)(void *, void *),
774 void *arg)
775{
Ying Xuedb304852015-01-07 13:41:54 +0800776 struct bucket_table *new_tbl, *old_tbl;
777 spinlock_t *new_bucket_lock, *old_bucket_lock;
778 u32 new_hash, old_hash;
779 bool success = true;
780
781 BUG_ON(!ht->p.key_len);
782
783 rcu_read_lock();
784
785 old_tbl = rht_dereference_rcu(ht->tbl, ht);
786 old_hash = head_hashfn(ht, old_tbl, obj);
787 old_bucket_lock = bucket_lock(old_tbl, old_hash);
788 spin_lock_bh(old_bucket_lock);
789
790 new_tbl = rht_dereference_rcu(ht->future_tbl, ht);
791 new_hash = head_hashfn(ht, new_tbl, obj);
792 new_bucket_lock = bucket_lock(new_tbl, new_hash);
793 if (unlikely(old_tbl != new_tbl))
794 spin_lock_bh_nested(new_bucket_lock, RHT_LOCK_NESTED);
795
Ying Xue7a868d12015-01-12 14:52:22 +0800796 if (rhashtable_lookup_compare(ht, rht_obj(ht, obj) + ht->p.key_offset,
797 compare, arg)) {
Ying Xuedb304852015-01-07 13:41:54 +0800798 success = false;
799 goto exit;
800 }
801
802 __rhashtable_insert(ht, obj, new_tbl, new_hash);
803
804exit:
805 if (unlikely(old_tbl != new_tbl))
806 spin_unlock_bh(new_bucket_lock);
807 spin_unlock_bh(old_bucket_lock);
808
809 rcu_read_unlock();
810
811 return success;
812}
Ying Xue7a868d12015-01-12 14:52:22 +0800813EXPORT_SYMBOL_GPL(rhashtable_lookup_compare_insert);
Ying Xuedb304852015-01-07 13:41:54 +0800814
Ying Xue94000172014-09-03 09:22:36 +0800815static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200816{
Ying Xue94000172014-09-03 09:22:36 +0800817 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
818 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200819}
820
821/**
822 * rhashtable_init - initialize a new hash table
823 * @ht: hash table to be initialized
824 * @params: configuration parameters
825 *
826 * Initializes a new hash table based on the provided configuration
827 * parameters. A table can be configured either with a variable or
828 * fixed length key:
829 *
830 * Configuration Example 1: Fixed length keys
831 * struct test_obj {
832 * int key;
833 * void * my_member;
834 * struct rhash_head node;
835 * };
836 *
837 * struct rhashtable_params params = {
838 * .head_offset = offsetof(struct test_obj, node),
839 * .key_offset = offsetof(struct test_obj, key),
840 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100841 * .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +0100842 * .nulls_base = (1U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +0200843 * };
844 *
845 * Configuration Example 2: Variable length keys
846 * struct test_obj {
847 * [...]
848 * struct rhash_head node;
849 * };
850 *
851 * u32 my_hash_fn(const void *data, u32 seed)
852 * {
853 * struct test_obj *obj = data;
854 *
855 * return [... hash ...];
856 * }
857 *
858 * struct rhashtable_params params = {
859 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100860 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200861 * .obj_hashfn = my_hash_fn,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200862 * };
863 */
864int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
865{
866 struct bucket_table *tbl;
867 size_t size;
868
869 size = HASH_DEFAULT_SIZE;
870
871 if ((params->key_len && !params->hashfn) ||
872 (!params->key_len && !params->obj_hashfn))
873 return -EINVAL;
874
Thomas Graff89bd6f2015-01-02 23:00:21 +0100875 if (params->nulls_base && params->nulls_base < (1U << RHT_BASE_SHIFT))
876 return -EINVAL;
877
Ying Xue94000172014-09-03 09:22:36 +0800878 params->min_shift = max_t(size_t, params->min_shift,
879 ilog2(HASH_MIN_SIZE));
880
Thomas Graf7e1e7762014-08-02 11:47:44 +0200881 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800882 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200883
Thomas Graf97defe12015-01-02 23:00:20 +0100884 memset(ht, 0, sizeof(*ht));
885 mutex_init(&ht->mutex);
886 memcpy(&ht->p, params, sizeof(*params));
887
888 if (params->locks_mul)
889 ht->p.locks_mul = roundup_pow_of_two(params->locks_mul);
890 else
891 ht->p.locks_mul = BUCKET_LOCKS_PER_CPU;
892
893 tbl = bucket_table_alloc(ht, size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200894 if (tbl == NULL)
895 return -ENOMEM;
896
Ying Xue545a1482015-01-07 13:41:57 +0800897 atomic_set(&ht->nelems, 0);
Ying Xuec0c09bf2015-01-07 13:41:56 +0800898 atomic_set(&ht->shift, ilog2(tbl->size));
Thomas Graf7e1e7762014-08-02 11:47:44 +0200899 RCU_INIT_POINTER(ht->tbl, tbl);
Thomas Graf97defe12015-01-02 23:00:20 +0100900 RCU_INIT_POINTER(ht->future_tbl, tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200901
902 if (!ht->p.hash_rnd)
903 get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
904
Thomas Graf97defe12015-01-02 23:00:20 +0100905 if (ht->p.grow_decision || ht->p.shrink_decision)
Ying Xue57699a42015-01-16 11:13:09 +0800906 INIT_WORK(&ht->run_work, rht_deferred_worker);
Thomas Graf97defe12015-01-02 23:00:20 +0100907
Thomas Graf7e1e7762014-08-02 11:47:44 +0200908 return 0;
909}
910EXPORT_SYMBOL_GPL(rhashtable_init);
911
912/**
913 * rhashtable_destroy - destroy hash table
914 * @ht: the hash table to destroy
915 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200916 * Frees the bucket array. This function is not rcu safe, therefore the caller
917 * has to make sure that no resizing may happen by unpublishing the hashtable
918 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200919 */
Thomas Graf97defe12015-01-02 23:00:20 +0100920void rhashtable_destroy(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200921{
Thomas Graf97defe12015-01-02 23:00:20 +0100922 ht->being_destroyed = true;
923
Ying Xue57699a42015-01-16 11:13:09 +0800924 if (ht->p.grow_decision || ht->p.shrink_decision)
925 cancel_work_sync(&ht->run_work);
926
Thomas Graf97defe12015-01-02 23:00:20 +0100927 mutex_lock(&ht->mutex);
Thomas Graf97defe12015-01-02 23:00:20 +0100928 bucket_table_free(rht_dereference(ht->tbl, ht));
Thomas Graf97defe12015-01-02 23:00:20 +0100929 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200930}
931EXPORT_SYMBOL_GPL(rhashtable_destroy);
932
933/**************************************************************************
934 * Self Test
935 **************************************************************************/
936
937#ifdef CONFIG_TEST_RHASHTABLE
938
939#define TEST_HT_SIZE 8
940#define TEST_ENTRIES 2048
941#define TEST_PTR ((void *) 0xdeadbeef)
942#define TEST_NEXPANDS 4
943
Thomas Graf7e1e7762014-08-02 11:47:44 +0200944struct test_obj {
945 void *ptr;
946 int value;
947 struct rhash_head node;
948};
949
950static int __init test_rht_lookup(struct rhashtable *ht)
951{
952 unsigned int i;
953
954 for (i = 0; i < TEST_ENTRIES * 2; i++) {
955 struct test_obj *obj;
956 bool expected = !(i % 2);
957 u32 key = i;
958
959 obj = rhashtable_lookup(ht, &key);
960
961 if (expected && !obj) {
962 pr_warn("Test failed: Could not find key %u\n", key);
963 return -ENOENT;
964 } else if (!expected && obj) {
965 pr_warn("Test failed: Unexpected entry found for key %u\n",
966 key);
967 return -EEXIST;
968 } else if (expected && obj) {
969 if (obj->ptr != TEST_PTR || obj->value != i) {
970 pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
971 obj->ptr, TEST_PTR, obj->value, i);
972 return -EINVAL;
973 }
974 }
975 }
976
977 return 0;
978}
979
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100980static void test_bucket_stats(struct rhashtable *ht, bool quiet)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200981{
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100982 unsigned int cnt, rcu_cnt, i, total = 0;
Thomas Graf88d6ed12015-01-02 23:00:16 +0100983 struct rhash_head *pos;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200984 struct test_obj *obj;
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100985 struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200986
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100987 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200988 for (i = 0; i < tbl->size; i++) {
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100989 rcu_cnt = cnt = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200990
991 if (!quiet)
992 pr_info(" [%#4x/%zu]", i, tbl->size);
993
Thomas Graf88d6ed12015-01-02 23:00:16 +0100994 rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200995 cnt++;
996 total++;
997 if (!quiet)
998 pr_cont(" [%p],", obj);
999 }
1000
Thomas Graf88d6ed12015-01-02 23:00:16 +01001001 rht_for_each_entry_rcu(obj, pos, tbl, i, node)
Thomas Graf3e7b2ec2014-11-24 12:37:58 +01001002 rcu_cnt++;
1003
1004 if (rcu_cnt != cnt)
1005 pr_warn("Test failed: Chain count mismach %d != %d",
1006 cnt, rcu_cnt);
1007
Thomas Graf7e1e7762014-08-02 11:47:44 +02001008 if (!quiet)
1009 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
1010 i, tbl->buckets[i], cnt);
1011 }
1012
Thomas Graf97defe12015-01-02 23:00:20 +01001013 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
1014 total, atomic_read(&ht->nelems), TEST_ENTRIES);
Thomas Graf3e7b2ec2014-11-24 12:37:58 +01001015
Thomas Graf97defe12015-01-02 23:00:20 +01001016 if (total != atomic_read(&ht->nelems) || total != TEST_ENTRIES)
Thomas Graf3e7b2ec2014-11-24 12:37:58 +01001017 pr_warn("Test failed: Total count mismatch ^^^");
Thomas Graf7e1e7762014-08-02 11:47:44 +02001018}
1019
1020static int __init test_rhashtable(struct rhashtable *ht)
1021{
1022 struct bucket_table *tbl;
Thomas Graf88d6ed12015-01-02 23:00:16 +01001023 struct test_obj *obj;
1024 struct rhash_head *pos, *next;
Thomas Graf7e1e7762014-08-02 11:47:44 +02001025 int err;
1026 unsigned int i;
1027
1028 /*
1029 * Insertion Test:
1030 * Insert TEST_ENTRIES into table with all keys even numbers
1031 */
1032 pr_info(" Adding %d keys\n", TEST_ENTRIES);
1033 for (i = 0; i < TEST_ENTRIES; i++) {
1034 struct test_obj *obj;
1035
1036 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
1037 if (!obj) {
1038 err = -ENOMEM;
1039 goto error;
1040 }
1041
1042 obj->ptr = TEST_PTR;
1043 obj->value = i * 2;
1044
Thomas Graf6eba8222014-11-13 13:45:46 +01001045 rhashtable_insert(ht, &obj->node);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001046 }
1047
1048 rcu_read_lock();
Thomas Graf3e7b2ec2014-11-24 12:37:58 +01001049 test_bucket_stats(ht, true);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001050 test_rht_lookup(ht);
1051 rcu_read_unlock();
1052
1053 for (i = 0; i < TEST_NEXPANDS; i++) {
1054 pr_info(" Table expansion iteration %u...\n", i);
Thomas Graf97defe12015-01-02 23:00:20 +01001055 mutex_lock(&ht->mutex);
Thomas Graf6eba8222014-11-13 13:45:46 +01001056 rhashtable_expand(ht);
Thomas Graf97defe12015-01-02 23:00:20 +01001057 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001058
1059 rcu_read_lock();
1060 pr_info(" Verifying lookups...\n");
1061 test_rht_lookup(ht);
1062 rcu_read_unlock();
1063 }
1064
1065 for (i = 0; i < TEST_NEXPANDS; i++) {
1066 pr_info(" Table shrinkage iteration %u...\n", i);
Thomas Graf97defe12015-01-02 23:00:20 +01001067 mutex_lock(&ht->mutex);
Thomas Graf6eba8222014-11-13 13:45:46 +01001068 rhashtable_shrink(ht);
Thomas Graf97defe12015-01-02 23:00:20 +01001069 mutex_unlock(&ht->mutex);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001070
1071 rcu_read_lock();
1072 pr_info(" Verifying lookups...\n");
1073 test_rht_lookup(ht);
1074 rcu_read_unlock();
1075 }
1076
Thomas Graf3e7b2ec2014-11-24 12:37:58 +01001077 rcu_read_lock();
1078 test_bucket_stats(ht, true);
1079 rcu_read_unlock();
1080
Thomas Graf7e1e7762014-08-02 11:47:44 +02001081 pr_info(" Deleting %d keys\n", TEST_ENTRIES);
1082 for (i = 0; i < TEST_ENTRIES; i++) {
1083 u32 key = i * 2;
1084
1085 obj = rhashtable_lookup(ht, &key);
1086 BUG_ON(!obj);
1087
Thomas Graf6eba8222014-11-13 13:45:46 +01001088 rhashtable_remove(ht, &obj->node);
Thomas Graf7e1e7762014-08-02 11:47:44 +02001089 kfree(obj);
1090 }
1091
1092 return 0;
1093
1094error:
1095 tbl = rht_dereference_rcu(ht->tbl, ht);
1096 for (i = 0; i < tbl->size; i++)
Thomas Graf88d6ed12015-01-02 23:00:16 +01001097 rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
Thomas Graf7e1e7762014-08-02 11:47:44 +02001098 kfree(obj);
1099
1100 return err;
1101}
1102
1103static int __init test_rht_init(void)
1104{
1105 struct rhashtable ht;
1106 struct rhashtable_params params = {
1107 .nelem_hint = TEST_HT_SIZE,
1108 .head_offset = offsetof(struct test_obj, node),
1109 .key_offset = offsetof(struct test_obj, value),
1110 .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +01001111 .hashfn = jhash,
Thomas Graff89bd6f2015-01-02 23:00:21 +01001112 .nulls_base = (3U << RHT_BASE_SHIFT),
Thomas Graf7e1e7762014-08-02 11:47:44 +02001113 .grow_decision = rht_grow_above_75,
1114 .shrink_decision = rht_shrink_below_30,
1115 };
1116 int err;
1117
1118 pr_info("Running resizable hashtable tests...\n");
1119
1120 err = rhashtable_init(&ht, &params);
1121 if (err < 0) {
1122 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
1123 err);
1124 return err;
1125 }
1126
1127 err = test_rhashtable(&ht);
1128
1129 rhashtable_destroy(&ht);
1130
1131 return err;
1132}
1133
1134subsys_initcall(test_rht_init);
1135
1136#endif /* CONFIG_TEST_RHASHTABLE */