blob: 1ee0eb636ca3afaded30b86fd43cb61bdf35490a [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
29
30#define ASSERT_RHT_MUTEX(HT) BUG_ON(!lockdep_rht_mutex_is_held(HT))
31
32#ifdef CONFIG_PROVE_LOCKING
33int lockdep_rht_mutex_is_held(const struct rhashtable *ht)
34{
Herbert Xu7b4ce232014-11-13 18:11:22 +080035 return ht->p.mutex_is_held(ht->p.parent);
Thomas Graf7e1e7762014-08-02 11:47:44 +020036}
37EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
38#endif
39
Thomas Grafc91eee52014-08-13 16:38:30 +020040static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020041{
42 return (void *) he - ht->p.head_offset;
43}
Thomas Graf7e1e7762014-08-02 11:47:44 +020044
Thomas Graf8d24c0b2015-01-02 23:00:14 +010045static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
Thomas Graf7e1e7762014-08-02 11:47:44 +020046{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010047 return hash & (tbl->size - 1);
Thomas Graf7e1e7762014-08-02 11:47:44 +020048}
49
Thomas Graf8d24c0b2015-01-02 23:00:14 +010050static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
51{
52 u32 hash;
53
54 if (unlikely(!ht->p.key_len))
55 hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
56 else
57 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
58 ht->p.hash_rnd);
59
60 return hash;
61}
62
63static u32 key_hashfn(const struct rhashtable *ht, const void *key, u32 len)
Thomas Graf7e1e7762014-08-02 11:47:44 +020064{
65 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf8d24c0b2015-01-02 23:00:14 +010066 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +020067
Thomas Graf8d24c0b2015-01-02 23:00:14 +010068 hash = ht->p.hashfn(key, len, ht->p.hash_rnd);
69
70 return rht_bucket_index(tbl, hash);
Thomas Graf7e1e7762014-08-02 11:47:44 +020071}
Thomas Graf7e1e7762014-08-02 11:47:44 +020072
73static u32 head_hashfn(const struct rhashtable *ht,
Thomas Graf8d24c0b2015-01-02 23:00:14 +010074 const struct bucket_table *tbl,
75 const struct rhash_head *he)
Thomas Graf7e1e7762014-08-02 11:47:44 +020076{
Thomas Graf8d24c0b2015-01-02 23:00:14 +010077 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
Thomas Graf7e1e7762014-08-02 11:47:44 +020078}
79
Thomas Graf6eba8222014-11-13 13:45:46 +010080static struct bucket_table *bucket_table_alloc(size_t nbuckets)
Thomas Graf7e1e7762014-08-02 11:47:44 +020081{
82 struct bucket_table *tbl;
83 size_t size;
84
85 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
Thomas Graf6eba8222014-11-13 13:45:46 +010086 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
Thomas Graf7e1e7762014-08-02 11:47:44 +020087 if (tbl == NULL)
88 tbl = vzalloc(size);
89
90 if (tbl == NULL)
91 return NULL;
92
93 tbl->size = nbuckets;
94
95 return tbl;
96}
97
98static void bucket_table_free(const struct bucket_table *tbl)
99{
100 kvfree(tbl);
101}
102
103/**
104 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
105 * @ht: hash table
106 * @new_size: new table size
107 */
108bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
109{
110 /* Expand table when exceeding 75% load */
111 return ht->nelems > (new_size / 4 * 3);
112}
113EXPORT_SYMBOL_GPL(rht_grow_above_75);
114
115/**
116 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
117 * @ht: hash table
118 * @new_size: new table size
119 */
120bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
121{
122 /* Shrink table beneath 30% load */
123 return ht->nelems < (new_size * 3 / 10);
124}
125EXPORT_SYMBOL_GPL(rht_shrink_below_30);
126
127static void hashtable_chain_unzip(const struct rhashtable *ht,
128 const struct bucket_table *new_tbl,
129 struct bucket_table *old_tbl, size_t n)
130{
131 struct rhash_head *he, *p, *next;
132 unsigned int h;
133
134 /* Old bucket empty, no work needed. */
135 p = rht_dereference(old_tbl->buckets[n], ht);
136 if (!p)
137 return;
138
139 /* Advance the old bucket pointer one or more times until it
140 * reaches a node that doesn't hash to the same bucket as the
141 * previous node p. Call the previous node p;
142 */
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100143 h = head_hashfn(ht, new_tbl, p);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200144 rht_for_each(he, p->next, ht) {
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100145 if (head_hashfn(ht, new_tbl, he) != h)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200146 break;
147 p = he;
148 }
149 RCU_INIT_POINTER(old_tbl->buckets[n], p->next);
150
151 /* Find the subsequent node which does hash to the same
152 * bucket as node P, or NULL if no such node exists.
153 */
154 next = NULL;
155 if (he) {
156 rht_for_each(he, he->next, ht) {
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100157 if (head_hashfn(ht, new_tbl, he) == h) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200158 next = he;
159 break;
160 }
161 }
162 }
163
164 /* Set p's next pointer to that subsequent node pointer,
165 * bypassing the nodes which do not hash to p's bucket
166 */
167 RCU_INIT_POINTER(p->next, next);
168}
169
170/**
171 * rhashtable_expand - Expand hash table while allowing concurrent lookups
172 * @ht: the hash table to expand
Thomas Graf7e1e7762014-08-02 11:47:44 +0200173 *
174 * A secondary bucket array is allocated and the hash entries are migrated
175 * while keeping them on both lists until the end of the RCU grace period.
176 *
177 * This function may only be called in a context where it is safe to call
178 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
179 *
180 * The caller must ensure that no concurrent table mutations take place.
181 * It is however valid to have concurrent lookups if they are RCU protected.
182 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100183int rhashtable_expand(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200184{
185 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
186 struct rhash_head *he;
187 unsigned int i, h;
188 bool complete;
189
190 ASSERT_RHT_MUTEX(ht);
191
192 if (ht->p.max_shift && ht->shift >= ht->p.max_shift)
193 return 0;
194
Thomas Graf6eba8222014-11-13 13:45:46 +0100195 new_tbl = bucket_table_alloc(old_tbl->size * 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200196 if (new_tbl == NULL)
197 return -ENOMEM;
198
199 ht->shift++;
200
201 /* For each new bucket, search the corresponding old bucket
Herbert Xu0c828f22014-11-13 13:10:48 +0800202 * for the first entry that hashes to the new bucket, and
Thomas Graf7e1e7762014-08-02 11:47:44 +0200203 * link the new bucket to that entry. Since all the entries
204 * which will end up in the new bucket appear in the same
205 * old bucket, this constructs an entirely valid new hash
206 * table, but with multiple buckets "zipped" together into a
207 * single imprecise chain.
208 */
209 for (i = 0; i < new_tbl->size; i++) {
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100210 h = rht_bucket_index(old_tbl, i);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200211 rht_for_each(he, old_tbl->buckets[h], ht) {
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100212 if (head_hashfn(ht, new_tbl, he) == i) {
Thomas Graf7e1e7762014-08-02 11:47:44 +0200213 RCU_INIT_POINTER(new_tbl->buckets[i], he);
214 break;
215 }
216 }
217 }
218
219 /* Publish the new table pointer. Lookups may now traverse
Herbert Xu0c828f22014-11-13 13:10:48 +0800220 * the new table, but they will not benefit from any
221 * additional efficiency until later steps unzip the buckets.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200222 */
223 rcu_assign_pointer(ht->tbl, new_tbl);
224
225 /* Unzip interleaved hash chains */
226 do {
227 /* Wait for readers. All new readers will see the new
228 * table, and thus no references to the old table will
229 * remain.
230 */
231 synchronize_rcu();
232
233 /* For each bucket in the old table (each of which
234 * contains items from multiple buckets of the new
235 * table): ...
236 */
237 complete = true;
238 for (i = 0; i < old_tbl->size; i++) {
239 hashtable_chain_unzip(ht, new_tbl, old_tbl, i);
240 if (old_tbl->buckets[i] != NULL)
241 complete = false;
242 }
243 } while (!complete);
244
245 bucket_table_free(old_tbl);
246 return 0;
247}
248EXPORT_SYMBOL_GPL(rhashtable_expand);
249
250/**
251 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
252 * @ht: the hash table to shrink
Thomas Graf7e1e7762014-08-02 11:47:44 +0200253 *
254 * This function may only be called in a context where it is safe to call
255 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
256 *
257 * The caller must ensure that no concurrent table mutations take place.
258 * It is however valid to have concurrent lookups if they are RCU protected.
259 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100260int rhashtable_shrink(struct rhashtable *ht)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200261{
262 struct bucket_table *ntbl, *tbl = rht_dereference(ht->tbl, ht);
263 struct rhash_head __rcu **pprev;
264 unsigned int i;
265
266 ASSERT_RHT_MUTEX(ht);
267
Ying Xue94000172014-09-03 09:22:36 +0800268 if (ht->shift <= ht->p.min_shift)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200269 return 0;
270
Thomas Graf6eba8222014-11-13 13:45:46 +0100271 ntbl = bucket_table_alloc(tbl->size / 2);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200272 if (ntbl == NULL)
273 return -ENOMEM;
274
275 ht->shift--;
276
Herbert Xu0c828f22014-11-13 13:10:48 +0800277 /* Link each bucket in the new table to the first bucket
Thomas Graf7e1e7762014-08-02 11:47:44 +0200278 * in the old table that contains entries which will hash
279 * to the new bucket.
280 */
281 for (i = 0; i < ntbl->size; i++) {
282 ntbl->buckets[i] = tbl->buckets[i];
283
Herbert Xu0c828f22014-11-13 13:10:48 +0800284 /* Link each bucket in the new table to the first bucket
Thomas Graf7e1e7762014-08-02 11:47:44 +0200285 * in the old table that contains entries which will hash
286 * to the new bucket.
287 */
288 for (pprev = &ntbl->buckets[i]; *pprev != NULL;
289 pprev = &rht_dereference(*pprev, ht)->next)
290 ;
291 RCU_INIT_POINTER(*pprev, tbl->buckets[i + ntbl->size]);
292 }
293
294 /* Publish the new, valid hash table */
295 rcu_assign_pointer(ht->tbl, ntbl);
296
297 /* Wait for readers. No new readers will have references to the
298 * old hash table.
299 */
300 synchronize_rcu();
301
302 bucket_table_free(tbl);
303
304 return 0;
305}
306EXPORT_SYMBOL_GPL(rhashtable_shrink);
307
308/**
309 * rhashtable_insert - insert object into hash hash table
310 * @ht: hash table
311 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200312 *
313 * Will automatically grow the table via rhashtable_expand() if the the
314 * grow_decision function specified at rhashtable_init() returns true.
315 *
316 * The caller must ensure that no concurrent table mutations occur. It is
317 * however valid to have concurrent lookups if they are RCU protected.
318 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100319void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200320{
321 struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
322 u32 hash;
323
324 ASSERT_RHT_MUTEX(ht);
325
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100326 hash = head_hashfn(ht, tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200327 RCU_INIT_POINTER(obj->next, tbl->buckets[hash]);
328 rcu_assign_pointer(tbl->buckets[hash], obj);
329 ht->nelems++;
330
331 if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size))
Thomas Graf6eba8222014-11-13 13:45:46 +0100332 rhashtable_expand(ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200333}
334EXPORT_SYMBOL_GPL(rhashtable_insert);
335
336/**
337 * rhashtable_remove_pprev - remove object from hash table given previous element
338 * @ht: hash table
339 * @obj: pointer to hash head inside object
340 * @pprev: pointer to previous element
Thomas Graf7e1e7762014-08-02 11:47:44 +0200341 *
342 * Identical to rhashtable_remove() but caller is alreayd aware of the element
343 * in front of the element to be deleted. This is in particular useful for
344 * deletion when combined with walking or lookup.
345 */
346void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj,
Thomas Graf6eba8222014-11-13 13:45:46 +0100347 struct rhash_head __rcu **pprev)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200348{
349 struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
350
351 ASSERT_RHT_MUTEX(ht);
352
353 RCU_INIT_POINTER(*pprev, obj->next);
354 ht->nelems--;
355
356 if (ht->p.shrink_decision &&
357 ht->p.shrink_decision(ht, tbl->size))
Thomas Graf6eba8222014-11-13 13:45:46 +0100358 rhashtable_shrink(ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200359}
360EXPORT_SYMBOL_GPL(rhashtable_remove_pprev);
361
362/**
363 * rhashtable_remove - remove object from hash table
364 * @ht: hash table
365 * @obj: pointer to hash head inside object
Thomas Graf7e1e7762014-08-02 11:47:44 +0200366 *
367 * Since the hash chain is single linked, the removal operation needs to
368 * walk the bucket chain upon removal. The removal operation is thus
369 * considerable slow if the hash table is not correctly sized.
370 *
371 * Will automatically shrink the table via rhashtable_expand() if the the
372 * shrink_decision function specified at rhashtable_init() returns true.
373 *
374 * The caller must ensure that no concurrent table mutations occur. It is
375 * however valid to have concurrent lookups if they are RCU protected.
376 */
Thomas Graf6eba8222014-11-13 13:45:46 +0100377bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200378{
379 struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
380 struct rhash_head __rcu **pprev;
381 struct rhash_head *he;
382 u32 h;
383
384 ASSERT_RHT_MUTEX(ht);
385
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100386 h = head_hashfn(ht, tbl, obj);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200387
388 pprev = &tbl->buckets[h];
389 rht_for_each(he, tbl->buckets[h], ht) {
390 if (he != obj) {
391 pprev = &he->next;
392 continue;
393 }
394
Thomas Graf6eba8222014-11-13 13:45:46 +0100395 rhashtable_remove_pprev(ht, he, pprev);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200396 return true;
397 }
398
399 return false;
400}
401EXPORT_SYMBOL_GPL(rhashtable_remove);
402
403/**
404 * rhashtable_lookup - lookup key in hash table
405 * @ht: hash table
406 * @key: pointer to key
407 *
408 * Computes the hash value for the key and traverses the bucket chain looking
409 * for a entry with an identical key. The first matching entry is returned.
410 *
411 * This lookup function may only be used for fixed key hash table (key_len
412 * paramter set). It will BUG() if used inappropriately.
413 *
414 * Lookups may occur in parallel with hash mutations as long as the lookup is
415 * guarded by rcu_read_lock(). The caller must take care of this.
416 */
417void *rhashtable_lookup(const struct rhashtable *ht, const void *key)
418{
419 const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
420 struct rhash_head *he;
421 u32 h;
422
423 BUG_ON(!ht->p.key_len);
424
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100425 h = key_hashfn(ht, key, ht->p.key_len);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200426 rht_for_each_rcu(he, tbl->buckets[h], ht) {
427 if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key,
428 ht->p.key_len))
429 continue;
430 return (void *) he - ht->p.head_offset;
431 }
432
433 return NULL;
434}
435EXPORT_SYMBOL_GPL(rhashtable_lookup);
436
437/**
438 * rhashtable_lookup_compare - search hash table with compare function
439 * @ht: hash table
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100440 * @key: the pointer to the key
Thomas Graf7e1e7762014-08-02 11:47:44 +0200441 * @compare: compare function, must return true on match
442 * @arg: argument passed on to compare function
443 *
444 * Traverses the bucket chain behind the provided hash value and calls the
445 * specified compare function for each entry.
446 *
447 * Lookups may occur in parallel with hash mutations as long as the lookup is
448 * guarded by rcu_read_lock(). The caller must take care of this.
449 *
450 * Returns the first entry on which the compare function returned true.
451 */
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100452void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200453 bool (*compare)(void *, void *), void *arg)
454{
455 const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
456 struct rhash_head *he;
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100457 u32 hash;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200458
Thomas Graf8d24c0b2015-01-02 23:00:14 +0100459 hash = key_hashfn(ht, key, ht->p.key_len);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200460 rht_for_each_rcu(he, tbl->buckets[hash], ht) {
461 if (!compare(rht_obj(ht, he), arg))
462 continue;
463 return (void *) he - ht->p.head_offset;
464 }
465
466 return NULL;
467}
468EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
469
Ying Xue94000172014-09-03 09:22:36 +0800470static size_t rounded_hashtable_size(struct rhashtable_params *params)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200471{
Ying Xue94000172014-09-03 09:22:36 +0800472 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
473 1UL << params->min_shift);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200474}
475
476/**
477 * rhashtable_init - initialize a new hash table
478 * @ht: hash table to be initialized
479 * @params: configuration parameters
480 *
481 * Initializes a new hash table based on the provided configuration
482 * parameters. A table can be configured either with a variable or
483 * fixed length key:
484 *
485 * Configuration Example 1: Fixed length keys
486 * struct test_obj {
487 * int key;
488 * void * my_member;
489 * struct rhash_head node;
490 * };
491 *
492 * struct rhashtable_params params = {
493 * .head_offset = offsetof(struct test_obj, node),
494 * .key_offset = offsetof(struct test_obj, key),
495 * .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100496 * .hashfn = jhash,
Herbert Xu1b2f3092014-11-13 18:11:20 +0800497 * #ifdef CONFIG_PROVE_LOCKING
Thomas Graf7e1e7762014-08-02 11:47:44 +0200498 * .mutex_is_held = &my_mutex_is_held,
Herbert Xu1b2f3092014-11-13 18:11:20 +0800499 * #endif
Thomas Graf7e1e7762014-08-02 11:47:44 +0200500 * };
501 *
502 * Configuration Example 2: Variable length keys
503 * struct test_obj {
504 * [...]
505 * struct rhash_head node;
506 * };
507 *
508 * u32 my_hash_fn(const void *data, u32 seed)
509 * {
510 * struct test_obj *obj = data;
511 *
512 * return [... hash ...];
513 * }
514 *
515 * struct rhashtable_params params = {
516 * .head_offset = offsetof(struct test_obj, node),
Daniel Borkmann87545892014-12-10 16:33:11 +0100517 * .hashfn = jhash,
Thomas Graf7e1e7762014-08-02 11:47:44 +0200518 * .obj_hashfn = my_hash_fn,
Herbert Xu1b2f3092014-11-13 18:11:20 +0800519 * #ifdef CONFIG_PROVE_LOCKING
Thomas Graf7e1e7762014-08-02 11:47:44 +0200520 * .mutex_is_held = &my_mutex_is_held,
Herbert Xu1b2f3092014-11-13 18:11:20 +0800521 * #endif
Thomas Graf7e1e7762014-08-02 11:47:44 +0200522 * };
523 */
524int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
525{
526 struct bucket_table *tbl;
527 size_t size;
528
529 size = HASH_DEFAULT_SIZE;
530
531 if ((params->key_len && !params->hashfn) ||
532 (!params->key_len && !params->obj_hashfn))
533 return -EINVAL;
534
Ying Xue94000172014-09-03 09:22:36 +0800535 params->min_shift = max_t(size_t, params->min_shift,
536 ilog2(HASH_MIN_SIZE));
537
Thomas Graf7e1e7762014-08-02 11:47:44 +0200538 if (params->nelem_hint)
Ying Xue94000172014-09-03 09:22:36 +0800539 size = rounded_hashtable_size(params);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200540
Thomas Graf6eba8222014-11-13 13:45:46 +0100541 tbl = bucket_table_alloc(size);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200542 if (tbl == NULL)
543 return -ENOMEM;
544
545 memset(ht, 0, sizeof(*ht));
546 ht->shift = ilog2(tbl->size);
547 memcpy(&ht->p, params, sizeof(*params));
548 RCU_INIT_POINTER(ht->tbl, tbl);
549
550 if (!ht->p.hash_rnd)
551 get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
552
553 return 0;
554}
555EXPORT_SYMBOL_GPL(rhashtable_init);
556
557/**
558 * rhashtable_destroy - destroy hash table
559 * @ht: the hash table to destroy
560 *
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200561 * Frees the bucket array. This function is not rcu safe, therefore the caller
562 * has to make sure that no resizing may happen by unpublishing the hashtable
563 * and waiting for the quiescent cycle before releasing the bucket array.
Thomas Graf7e1e7762014-08-02 11:47:44 +0200564 */
565void rhashtable_destroy(const struct rhashtable *ht)
566{
Pablo Neira Ayusoae82ddc2014-09-02 00:26:05 +0200567 bucket_table_free(ht->tbl);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200568}
569EXPORT_SYMBOL_GPL(rhashtable_destroy);
570
571/**************************************************************************
572 * Self Test
573 **************************************************************************/
574
575#ifdef CONFIG_TEST_RHASHTABLE
576
577#define TEST_HT_SIZE 8
578#define TEST_ENTRIES 2048
579#define TEST_PTR ((void *) 0xdeadbeef)
580#define TEST_NEXPANDS 4
581
Herbert Xu1b2f3092014-11-13 18:11:20 +0800582#ifdef CONFIG_PROVE_LOCKING
Herbert Xu7b4ce232014-11-13 18:11:22 +0800583static int test_mutex_is_held(void *parent)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200584{
585 return 1;
586}
Herbert Xu1b2f3092014-11-13 18:11:20 +0800587#endif
Thomas Graf7e1e7762014-08-02 11:47:44 +0200588
589struct test_obj {
590 void *ptr;
591 int value;
592 struct rhash_head node;
593};
594
595static int __init test_rht_lookup(struct rhashtable *ht)
596{
597 unsigned int i;
598
599 for (i = 0; i < TEST_ENTRIES * 2; i++) {
600 struct test_obj *obj;
601 bool expected = !(i % 2);
602 u32 key = i;
603
604 obj = rhashtable_lookup(ht, &key);
605
606 if (expected && !obj) {
607 pr_warn("Test failed: Could not find key %u\n", key);
608 return -ENOENT;
609 } else if (!expected && obj) {
610 pr_warn("Test failed: Unexpected entry found for key %u\n",
611 key);
612 return -EEXIST;
613 } else if (expected && obj) {
614 if (obj->ptr != TEST_PTR || obj->value != i) {
615 pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
616 obj->ptr, TEST_PTR, obj->value, i);
617 return -EINVAL;
618 }
619 }
620 }
621
622 return 0;
623}
624
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100625static void test_bucket_stats(struct rhashtable *ht, bool quiet)
Thomas Graf7e1e7762014-08-02 11:47:44 +0200626{
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100627 unsigned int cnt, rcu_cnt, i, total = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200628 struct test_obj *obj;
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100629 struct bucket_table *tbl;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200630
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100631 tbl = rht_dereference_rcu(ht->tbl, ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200632 for (i = 0; i < tbl->size; i++) {
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100633 rcu_cnt = cnt = 0;
Thomas Graf7e1e7762014-08-02 11:47:44 +0200634
635 if (!quiet)
636 pr_info(" [%#4x/%zu]", i, tbl->size);
637
638 rht_for_each_entry_rcu(obj, tbl->buckets[i], node) {
639 cnt++;
640 total++;
641 if (!quiet)
642 pr_cont(" [%p],", obj);
643 }
644
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100645 rht_for_each_entry_rcu(obj, tbl->buckets[i], node)
646 rcu_cnt++;
647
648 if (rcu_cnt != cnt)
649 pr_warn("Test failed: Chain count mismach %d != %d",
650 cnt, rcu_cnt);
651
Thomas Graf7e1e7762014-08-02 11:47:44 +0200652 if (!quiet)
653 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
654 i, tbl->buckets[i], cnt);
655 }
656
657 pr_info(" Traversal complete: counted=%u, nelems=%zu, entries=%d\n",
658 total, ht->nelems, TEST_ENTRIES);
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100659
660 if (total != ht->nelems || total != TEST_ENTRIES)
661 pr_warn("Test failed: Total count mismatch ^^^");
Thomas Graf7e1e7762014-08-02 11:47:44 +0200662}
663
664static int __init test_rhashtable(struct rhashtable *ht)
665{
666 struct bucket_table *tbl;
667 struct test_obj *obj, *next;
668 int err;
669 unsigned int i;
670
671 /*
672 * Insertion Test:
673 * Insert TEST_ENTRIES into table with all keys even numbers
674 */
675 pr_info(" Adding %d keys\n", TEST_ENTRIES);
676 for (i = 0; i < TEST_ENTRIES; i++) {
677 struct test_obj *obj;
678
679 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
680 if (!obj) {
681 err = -ENOMEM;
682 goto error;
683 }
684
685 obj->ptr = TEST_PTR;
686 obj->value = i * 2;
687
Thomas Graf6eba8222014-11-13 13:45:46 +0100688 rhashtable_insert(ht, &obj->node);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200689 }
690
691 rcu_read_lock();
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100692 test_bucket_stats(ht, true);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200693 test_rht_lookup(ht);
694 rcu_read_unlock();
695
696 for (i = 0; i < TEST_NEXPANDS; i++) {
697 pr_info(" Table expansion iteration %u...\n", i);
Thomas Graf6eba8222014-11-13 13:45:46 +0100698 rhashtable_expand(ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200699
700 rcu_read_lock();
701 pr_info(" Verifying lookups...\n");
702 test_rht_lookup(ht);
703 rcu_read_unlock();
704 }
705
706 for (i = 0; i < TEST_NEXPANDS; i++) {
707 pr_info(" Table shrinkage iteration %u...\n", i);
Thomas Graf6eba8222014-11-13 13:45:46 +0100708 rhashtable_shrink(ht);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200709
710 rcu_read_lock();
711 pr_info(" Verifying lookups...\n");
712 test_rht_lookup(ht);
713 rcu_read_unlock();
714 }
715
Thomas Graf3e7b2ec2014-11-24 12:37:58 +0100716 rcu_read_lock();
717 test_bucket_stats(ht, true);
718 rcu_read_unlock();
719
Thomas Graf7e1e7762014-08-02 11:47:44 +0200720 pr_info(" Deleting %d keys\n", TEST_ENTRIES);
721 for (i = 0; i < TEST_ENTRIES; i++) {
722 u32 key = i * 2;
723
724 obj = rhashtable_lookup(ht, &key);
725 BUG_ON(!obj);
726
Thomas Graf6eba8222014-11-13 13:45:46 +0100727 rhashtable_remove(ht, &obj->node);
Thomas Graf7e1e7762014-08-02 11:47:44 +0200728 kfree(obj);
729 }
730
731 return 0;
732
733error:
734 tbl = rht_dereference_rcu(ht->tbl, ht);
735 for (i = 0; i < tbl->size; i++)
736 rht_for_each_entry_safe(obj, next, tbl->buckets[i], ht, node)
737 kfree(obj);
738
739 return err;
740}
741
742static int __init test_rht_init(void)
743{
744 struct rhashtable ht;
745 struct rhashtable_params params = {
746 .nelem_hint = TEST_HT_SIZE,
747 .head_offset = offsetof(struct test_obj, node),
748 .key_offset = offsetof(struct test_obj, value),
749 .key_len = sizeof(int),
Daniel Borkmann87545892014-12-10 16:33:11 +0100750 .hashfn = jhash,
Herbert Xu1b2f3092014-11-13 18:11:20 +0800751#ifdef CONFIG_PROVE_LOCKING
Thomas Graf7e1e7762014-08-02 11:47:44 +0200752 .mutex_is_held = &test_mutex_is_held,
Herbert Xu1b2f3092014-11-13 18:11:20 +0800753#endif
Thomas Graf7e1e7762014-08-02 11:47:44 +0200754 .grow_decision = rht_grow_above_75,
755 .shrink_decision = rht_shrink_below_30,
756 };
757 int err;
758
759 pr_info("Running resizable hashtable tests...\n");
760
761 err = rhashtable_init(&ht, &params);
762 if (err < 0) {
763 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
764 err);
765 return err;
766 }
767
768 err = test_rhashtable(&ht);
769
770 rhashtable_destroy(&ht);
771
772 return err;
773}
774
775subsys_initcall(test_rht_init);
776
777#endif /* CONFIG_TEST_RHASHTABLE */