]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - lib/rhashtable.c
rhashtable: Convert bucket iterators to take table and index
[mirror_ubuntu-artful-kernel.git] / lib / rhashtable.c
CommitLineData
7e1e7763
TG
1/*
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>
87545899 23#include <linux/jhash.h>
7e1e7763
TG
24#include <linux/random.h>
25#include <linux/rhashtable.h>
7e1e7763
TG
26
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{
7b4ce235 35 return ht->p.mutex_is_held(ht->p.parent);
7e1e7763
TG
36}
37EXPORT_SYMBOL_GPL(lockdep_rht_mutex_is_held);
88d6ed15
TG
38
39int lockdep_rht_bucket_is_held(const struct bucket_table *tbl, u32 hash)
40{
41 return 1;
42}
43EXPORT_SYMBOL_GPL(lockdep_rht_bucket_is_held);
7e1e7763
TG
44#endif
45
c91eee56 46static void *rht_obj(const struct rhashtable *ht, const struct rhash_head *he)
7e1e7763
TG
47{
48 return (void *) he - ht->p.head_offset;
49}
7e1e7763 50
8d24c0b4 51static u32 rht_bucket_index(const struct bucket_table *tbl, u32 hash)
7e1e7763 52{
8d24c0b4 53 return hash & (tbl->size - 1);
7e1e7763 54}
7e1e7763 55
8d24c0b4 56static u32 obj_raw_hashfn(const struct rhashtable *ht, const void *ptr)
7e1e7763 57{
8d24c0b4 58 u32 hash;
7e1e7763 59
8d24c0b4
TG
60 if (unlikely(!ht->p.key_len))
61 hash = ht->p.obj_hashfn(ptr, ht->p.hash_rnd);
62 else
63 hash = ht->p.hashfn(ptr + ht->p.key_offset, ht->p.key_len,
64 ht->p.hash_rnd);
7e1e7763 65
8d24c0b4 66 return hash;
7e1e7763
TG
67}
68
8d24c0b4 69static u32 key_hashfn(const struct rhashtable *ht, const void *key, u32 len)
7e1e7763
TG
70{
71 struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
8d24c0b4
TG
72 u32 hash;
73
74 hash = ht->p.hashfn(key, len, ht->p.hash_rnd);
7e1e7763 75
8d24c0b4 76 return rht_bucket_index(tbl, hash);
7e1e7763 77}
7e1e7763
TG
78
79static u32 head_hashfn(const struct rhashtable *ht,
8d24c0b4
TG
80 const struct bucket_table *tbl,
81 const struct rhash_head *he)
7e1e7763 82{
8d24c0b4 83 return rht_bucket_index(tbl, obj_raw_hashfn(ht, rht_obj(ht, he)));
7e1e7763
TG
84}
85
6eba8224 86static struct bucket_table *bucket_table_alloc(size_t nbuckets)
7e1e7763
TG
87{
88 struct bucket_table *tbl;
89 size_t size;
90
91 size = sizeof(*tbl) + nbuckets * sizeof(tbl->buckets[0]);
6eba8224 92 tbl = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
7e1e7763
TG
93 if (tbl == NULL)
94 tbl = vzalloc(size);
95
96 if (tbl == NULL)
97 return NULL;
98
99 tbl->size = nbuckets;
100
101 return tbl;
102}
103
104static void bucket_table_free(const struct bucket_table *tbl)
105{
106 kvfree(tbl);
107}
108
109/**
110 * rht_grow_above_75 - returns true if nelems > 0.75 * table-size
111 * @ht: hash table
112 * @new_size: new table size
113 */
114bool rht_grow_above_75(const struct rhashtable *ht, size_t new_size)
115{
116 /* Expand table when exceeding 75% load */
117 return ht->nelems > (new_size / 4 * 3);
118}
119EXPORT_SYMBOL_GPL(rht_grow_above_75);
120
121/**
122 * rht_shrink_below_30 - returns true if nelems < 0.3 * table-size
123 * @ht: hash table
124 * @new_size: new table size
125 */
126bool rht_shrink_below_30(const struct rhashtable *ht, size_t new_size)
127{
128 /* Shrink table beneath 30% load */
129 return ht->nelems < (new_size * 3 / 10);
130}
131EXPORT_SYMBOL_GPL(rht_shrink_below_30);
132
133static void hashtable_chain_unzip(const struct rhashtable *ht,
134 const struct bucket_table *new_tbl,
135 struct bucket_table *old_tbl, size_t n)
136{
137 struct rhash_head *he, *p, *next;
138 unsigned int h;
139
140 /* Old bucket empty, no work needed. */
141 p = rht_dereference(old_tbl->buckets[n], ht);
142 if (!p)
143 return;
144
145 /* Advance the old bucket pointer one or more times until it
146 * reaches a node that doesn't hash to the same bucket as the
147 * previous node p. Call the previous node p;
148 */
8d24c0b4 149 h = head_hashfn(ht, new_tbl, p);
88d6ed15 150 rht_for_each_continue(he, p->next, old_tbl, n) {
8d24c0b4 151 if (head_hashfn(ht, new_tbl, he) != h)
7e1e7763
TG
152 break;
153 p = he;
154 }
155 RCU_INIT_POINTER(old_tbl->buckets[n], p->next);
156
157 /* Find the subsequent node which does hash to the same
158 * bucket as node P, or NULL if no such node exists.
159 */
160 next = NULL;
161 if (he) {
88d6ed15 162 rht_for_each_continue(he, he->next, old_tbl, n) {
8d24c0b4 163 if (head_hashfn(ht, new_tbl, he) == h) {
7e1e7763
TG
164 next = he;
165 break;
166 }
167 }
168 }
169
170 /* Set p's next pointer to that subsequent node pointer,
171 * bypassing the nodes which do not hash to p's bucket
172 */
173 RCU_INIT_POINTER(p->next, next);
174}
175
176/**
177 * rhashtable_expand - Expand hash table while allowing concurrent lookups
178 * @ht: the hash table to expand
7e1e7763
TG
179 *
180 * A secondary bucket array is allocated and the hash entries are migrated
181 * while keeping them on both lists until the end of the RCU grace period.
182 *
183 * This function may only be called in a context where it is safe to call
184 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
185 *
186 * The caller must ensure that no concurrent table mutations take place.
187 * It is however valid to have concurrent lookups if they are RCU protected.
188 */
6eba8224 189int rhashtable_expand(struct rhashtable *ht)
7e1e7763
TG
190{
191 struct bucket_table *new_tbl, *old_tbl = rht_dereference(ht->tbl, ht);
192 struct rhash_head *he;
193 unsigned int i, h;
194 bool complete;
195
196 ASSERT_RHT_MUTEX(ht);
197
198 if (ht->p.max_shift && ht->shift >= ht->p.max_shift)
199 return 0;
200
6eba8224 201 new_tbl = bucket_table_alloc(old_tbl->size * 2);
7e1e7763
TG
202 if (new_tbl == NULL)
203 return -ENOMEM;
204
205 ht->shift++;
206
207 /* For each new bucket, search the corresponding old bucket
0c828f2f 208 * for the first entry that hashes to the new bucket, and
7e1e7763
TG
209 * link the new bucket to that entry. Since all the entries
210 * which will end up in the new bucket appear in the same
211 * old bucket, this constructs an entirely valid new hash
212 * table, but with multiple buckets "zipped" together into a
213 * single imprecise chain.
214 */
215 for (i = 0; i < new_tbl->size; i++) {
8d24c0b4 216 h = rht_bucket_index(old_tbl, i);
88d6ed15 217 rht_for_each(he, old_tbl, h) {
8d24c0b4 218 if (head_hashfn(ht, new_tbl, he) == i) {
7e1e7763
TG
219 RCU_INIT_POINTER(new_tbl->buckets[i], he);
220 break;
221 }
222 }
223 }
224
225 /* Publish the new table pointer. Lookups may now traverse
0c828f2f
HX
226 * the new table, but they will not benefit from any
227 * additional efficiency until later steps unzip the buckets.
7e1e7763
TG
228 */
229 rcu_assign_pointer(ht->tbl, new_tbl);
230
231 /* Unzip interleaved hash chains */
232 do {
233 /* Wait for readers. All new readers will see the new
234 * table, and thus no references to the old table will
235 * remain.
236 */
237 synchronize_rcu();
238
239 /* For each bucket in the old table (each of which
240 * contains items from multiple buckets of the new
241 * table): ...
242 */
243 complete = true;
244 for (i = 0; i < old_tbl->size; i++) {
245 hashtable_chain_unzip(ht, new_tbl, old_tbl, i);
246 if (old_tbl->buckets[i] != NULL)
247 complete = false;
248 }
249 } while (!complete);
250
251 bucket_table_free(old_tbl);
252 return 0;
253}
254EXPORT_SYMBOL_GPL(rhashtable_expand);
255
256/**
257 * rhashtable_shrink - Shrink hash table while allowing concurrent lookups
258 * @ht: the hash table to shrink
7e1e7763
TG
259 *
260 * This function may only be called in a context where it is safe to call
261 * synchronize_rcu(), e.g. not within a rcu_read_lock() section.
262 *
263 * The caller must ensure that no concurrent table mutations take place.
264 * It is however valid to have concurrent lookups if they are RCU protected.
265 */
6eba8224 266int rhashtable_shrink(struct rhashtable *ht)
7e1e7763
TG
267{
268 struct bucket_table *ntbl, *tbl = rht_dereference(ht->tbl, ht);
269 struct rhash_head __rcu **pprev;
270 unsigned int i;
271
272 ASSERT_RHT_MUTEX(ht);
273
94000176 274 if (ht->shift <= ht->p.min_shift)
7e1e7763
TG
275 return 0;
276
6eba8224 277 ntbl = bucket_table_alloc(tbl->size / 2);
7e1e7763
TG
278 if (ntbl == NULL)
279 return -ENOMEM;
280
281 ht->shift--;
282
0c828f2f 283 /* Link each bucket in the new table to the first bucket
7e1e7763
TG
284 * in the old table that contains entries which will hash
285 * to the new bucket.
286 */
287 for (i = 0; i < ntbl->size; i++) {
288 ntbl->buckets[i] = tbl->buckets[i];
289
0c828f2f 290 /* Link each bucket in the new table to the first bucket
7e1e7763
TG
291 * in the old table that contains entries which will hash
292 * to the new bucket.
293 */
294 for (pprev = &ntbl->buckets[i]; *pprev != NULL;
88d6ed15 295 pprev = &rht_dereference_bucket(*pprev, ntbl, i)->next)
7e1e7763
TG
296 ;
297 RCU_INIT_POINTER(*pprev, tbl->buckets[i + ntbl->size]);
298 }
299
300 /* Publish the new, valid hash table */
301 rcu_assign_pointer(ht->tbl, ntbl);
302
303 /* Wait for readers. No new readers will have references to the
304 * old hash table.
305 */
306 synchronize_rcu();
307
308 bucket_table_free(tbl);
309
310 return 0;
311}
312EXPORT_SYMBOL_GPL(rhashtable_shrink);
313
314/**
315 * rhashtable_insert - insert object into hash hash table
316 * @ht: hash table
317 * @obj: pointer to hash head inside object
7e1e7763
TG
318 *
319 * Will automatically grow the table via rhashtable_expand() if the the
320 * grow_decision function specified at rhashtable_init() returns true.
321 *
322 * The caller must ensure that no concurrent table mutations occur. It is
323 * however valid to have concurrent lookups if they are RCU protected.
324 */
6eba8224 325void rhashtable_insert(struct rhashtable *ht, struct rhash_head *obj)
7e1e7763
TG
326{
327 struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
328 u32 hash;
329
330 ASSERT_RHT_MUTEX(ht);
331
8d24c0b4 332 hash = head_hashfn(ht, tbl, obj);
7e1e7763
TG
333 RCU_INIT_POINTER(obj->next, tbl->buckets[hash]);
334 rcu_assign_pointer(tbl->buckets[hash], obj);
335 ht->nelems++;
336
337 if (ht->p.grow_decision && ht->p.grow_decision(ht, tbl->size))
6eba8224 338 rhashtable_expand(ht);
7e1e7763
TG
339}
340EXPORT_SYMBOL_GPL(rhashtable_insert);
341
342/**
343 * rhashtable_remove_pprev - remove object from hash table given previous element
344 * @ht: hash table
345 * @obj: pointer to hash head inside object
346 * @pprev: pointer to previous element
7e1e7763
TG
347 *
348 * Identical to rhashtable_remove() but caller is alreayd aware of the element
349 * in front of the element to be deleted. This is in particular useful for
350 * deletion when combined with walking or lookup.
351 */
352void rhashtable_remove_pprev(struct rhashtable *ht, struct rhash_head *obj,
6eba8224 353 struct rhash_head __rcu **pprev)
7e1e7763
TG
354{
355 struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
356
357 ASSERT_RHT_MUTEX(ht);
358
359 RCU_INIT_POINTER(*pprev, obj->next);
360 ht->nelems--;
361
362 if (ht->p.shrink_decision &&
363 ht->p.shrink_decision(ht, tbl->size))
6eba8224 364 rhashtable_shrink(ht);
7e1e7763
TG
365}
366EXPORT_SYMBOL_GPL(rhashtable_remove_pprev);
367
368/**
369 * rhashtable_remove - remove object from hash table
370 * @ht: hash table
371 * @obj: pointer to hash head inside object
7e1e7763
TG
372 *
373 * Since the hash chain is single linked, the removal operation needs to
374 * walk the bucket chain upon removal. The removal operation is thus
375 * considerable slow if the hash table is not correctly sized.
376 *
377 * Will automatically shrink the table via rhashtable_expand() if the the
378 * shrink_decision function specified at rhashtable_init() returns true.
379 *
380 * The caller must ensure that no concurrent table mutations occur. It is
381 * however valid to have concurrent lookups if they are RCU protected.
382 */
6eba8224 383bool rhashtable_remove(struct rhashtable *ht, struct rhash_head *obj)
7e1e7763
TG
384{
385 struct bucket_table *tbl = rht_dereference(ht->tbl, ht);
386 struct rhash_head __rcu **pprev;
387 struct rhash_head *he;
388 u32 h;
389
390 ASSERT_RHT_MUTEX(ht);
391
8d24c0b4 392 h = head_hashfn(ht, tbl, obj);
7e1e7763
TG
393
394 pprev = &tbl->buckets[h];
88d6ed15 395 rht_for_each(he, tbl, h) {
7e1e7763
TG
396 if (he != obj) {
397 pprev = &he->next;
398 continue;
399 }
400
6eba8224 401 rhashtable_remove_pprev(ht, he, pprev);
7e1e7763
TG
402 return true;
403 }
404
405 return false;
406}
407EXPORT_SYMBOL_GPL(rhashtable_remove);
408
409/**
410 * rhashtable_lookup - lookup key in hash table
411 * @ht: hash table
412 * @key: pointer to key
413 *
414 * Computes the hash value for the key and traverses the bucket chain looking
415 * for a entry with an identical key. The first matching entry is returned.
416 *
417 * This lookup function may only be used for fixed key hash table (key_len
418 * paramter set). It will BUG() if used inappropriately.
419 *
420 * Lookups may occur in parallel with hash mutations as long as the lookup is
421 * guarded by rcu_read_lock(). The caller must take care of this.
422 */
423void *rhashtable_lookup(const struct rhashtable *ht, const void *key)
424{
425 const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
426 struct rhash_head *he;
427 u32 h;
428
429 BUG_ON(!ht->p.key_len);
430
8d24c0b4 431 h = key_hashfn(ht, key, ht->p.key_len);
88d6ed15 432 rht_for_each_rcu(he, tbl, h) {
7e1e7763
TG
433 if (memcmp(rht_obj(ht, he) + ht->p.key_offset, key,
434 ht->p.key_len))
435 continue;
a4b18cda 436 return rht_obj(ht, he);
7e1e7763
TG
437 }
438
439 return NULL;
440}
441EXPORT_SYMBOL_GPL(rhashtable_lookup);
442
443/**
444 * rhashtable_lookup_compare - search hash table with compare function
445 * @ht: hash table
8d24c0b4 446 * @key: the pointer to the key
7e1e7763
TG
447 * @compare: compare function, must return true on match
448 * @arg: argument passed on to compare function
449 *
450 * Traverses the bucket chain behind the provided hash value and calls the
451 * specified compare function for each entry.
452 *
453 * Lookups may occur in parallel with hash mutations as long as the lookup is
454 * guarded by rcu_read_lock(). The caller must take care of this.
455 *
456 * Returns the first entry on which the compare function returned true.
457 */
8d24c0b4 458void *rhashtable_lookup_compare(const struct rhashtable *ht, const void *key,
7e1e7763
TG
459 bool (*compare)(void *, void *), void *arg)
460{
461 const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
462 struct rhash_head *he;
8d24c0b4 463 u32 hash;
7e1e7763 464
8d24c0b4 465 hash = key_hashfn(ht, key, ht->p.key_len);
88d6ed15 466 rht_for_each_rcu(he, tbl, hash) {
7e1e7763
TG
467 if (!compare(rht_obj(ht, he), arg))
468 continue;
a4b18cda 469 return rht_obj(ht, he);
7e1e7763
TG
470 }
471
472 return NULL;
473}
474EXPORT_SYMBOL_GPL(rhashtable_lookup_compare);
475
94000176 476static size_t rounded_hashtable_size(struct rhashtable_params *params)
7e1e7763 477{
94000176
YX
478 return max(roundup_pow_of_two(params->nelem_hint * 4 / 3),
479 1UL << params->min_shift);
7e1e7763
TG
480}
481
482/**
483 * rhashtable_init - initialize a new hash table
484 * @ht: hash table to be initialized
485 * @params: configuration parameters
486 *
487 * Initializes a new hash table based on the provided configuration
488 * parameters. A table can be configured either with a variable or
489 * fixed length key:
490 *
491 * Configuration Example 1: Fixed length keys
492 * struct test_obj {
493 * int key;
494 * void * my_member;
495 * struct rhash_head node;
496 * };
497 *
498 * struct rhashtable_params params = {
499 * .head_offset = offsetof(struct test_obj, node),
500 * .key_offset = offsetof(struct test_obj, key),
501 * .key_len = sizeof(int),
87545899 502 * .hashfn = jhash,
1b2f309d 503 * #ifdef CONFIG_PROVE_LOCKING
7e1e7763 504 * .mutex_is_held = &my_mutex_is_held,
1b2f309d 505 * #endif
7e1e7763
TG
506 * };
507 *
508 * Configuration Example 2: Variable length keys
509 * struct test_obj {
510 * [...]
511 * struct rhash_head node;
512 * };
513 *
514 * u32 my_hash_fn(const void *data, u32 seed)
515 * {
516 * struct test_obj *obj = data;
517 *
518 * return [... hash ...];
519 * }
520 *
521 * struct rhashtable_params params = {
522 * .head_offset = offsetof(struct test_obj, node),
87545899 523 * .hashfn = jhash,
7e1e7763 524 * .obj_hashfn = my_hash_fn,
1b2f309d 525 * #ifdef CONFIG_PROVE_LOCKING
7e1e7763 526 * .mutex_is_held = &my_mutex_is_held,
1b2f309d 527 * #endif
7e1e7763
TG
528 * };
529 */
530int rhashtable_init(struct rhashtable *ht, struct rhashtable_params *params)
531{
532 struct bucket_table *tbl;
533 size_t size;
534
535 size = HASH_DEFAULT_SIZE;
536
537 if ((params->key_len && !params->hashfn) ||
538 (!params->key_len && !params->obj_hashfn))
539 return -EINVAL;
540
94000176
YX
541 params->min_shift = max_t(size_t, params->min_shift,
542 ilog2(HASH_MIN_SIZE));
543
7e1e7763 544 if (params->nelem_hint)
94000176 545 size = rounded_hashtable_size(params);
7e1e7763 546
6eba8224 547 tbl = bucket_table_alloc(size);
7e1e7763
TG
548 if (tbl == NULL)
549 return -ENOMEM;
550
551 memset(ht, 0, sizeof(*ht));
552 ht->shift = ilog2(tbl->size);
553 memcpy(&ht->p, params, sizeof(*params));
554 RCU_INIT_POINTER(ht->tbl, tbl);
555
556 if (!ht->p.hash_rnd)
557 get_random_bytes(&ht->p.hash_rnd, sizeof(ht->p.hash_rnd));
558
559 return 0;
560}
561EXPORT_SYMBOL_GPL(rhashtable_init);
562
563/**
564 * rhashtable_destroy - destroy hash table
565 * @ht: the hash table to destroy
566 *
ae82ddcf
PNA
567 * Frees the bucket array. This function is not rcu safe, therefore the caller
568 * has to make sure that no resizing may happen by unpublishing the hashtable
569 * and waiting for the quiescent cycle before releasing the bucket array.
7e1e7763
TG
570 */
571void rhashtable_destroy(const struct rhashtable *ht)
572{
ae82ddcf 573 bucket_table_free(ht->tbl);
7e1e7763
TG
574}
575EXPORT_SYMBOL_GPL(rhashtable_destroy);
576
577/**************************************************************************
578 * Self Test
579 **************************************************************************/
580
581#ifdef CONFIG_TEST_RHASHTABLE
582
583#define TEST_HT_SIZE 8
584#define TEST_ENTRIES 2048
585#define TEST_PTR ((void *) 0xdeadbeef)
586#define TEST_NEXPANDS 4
587
1b2f309d 588#ifdef CONFIG_PROVE_LOCKING
7b4ce235 589static int test_mutex_is_held(void *parent)
7e1e7763
TG
590{
591 return 1;
592}
1b2f309d 593#endif
7e1e7763
TG
594
595struct test_obj {
596 void *ptr;
597 int value;
598 struct rhash_head node;
599};
600
601static int __init test_rht_lookup(struct rhashtable *ht)
602{
603 unsigned int i;
604
605 for (i = 0; i < TEST_ENTRIES * 2; i++) {
606 struct test_obj *obj;
607 bool expected = !(i % 2);
608 u32 key = i;
609
610 obj = rhashtable_lookup(ht, &key);
611
612 if (expected && !obj) {
613 pr_warn("Test failed: Could not find key %u\n", key);
614 return -ENOENT;
615 } else if (!expected && obj) {
616 pr_warn("Test failed: Unexpected entry found for key %u\n",
617 key);
618 return -EEXIST;
619 } else if (expected && obj) {
620 if (obj->ptr != TEST_PTR || obj->value != i) {
621 pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
622 obj->ptr, TEST_PTR, obj->value, i);
623 return -EINVAL;
624 }
625 }
626 }
627
628 return 0;
629}
630
3e7b2ec4 631static void test_bucket_stats(struct rhashtable *ht, bool quiet)
7e1e7763 632{
3e7b2ec4 633 unsigned int cnt, rcu_cnt, i, total = 0;
88d6ed15 634 struct rhash_head *pos;
7e1e7763 635 struct test_obj *obj;
3e7b2ec4 636 struct bucket_table *tbl;
7e1e7763 637
3e7b2ec4 638 tbl = rht_dereference_rcu(ht->tbl, ht);
7e1e7763 639 for (i = 0; i < tbl->size; i++) {
3e7b2ec4 640 rcu_cnt = cnt = 0;
7e1e7763
TG
641
642 if (!quiet)
643 pr_info(" [%#4x/%zu]", i, tbl->size);
644
88d6ed15 645 rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
7e1e7763
TG
646 cnt++;
647 total++;
648 if (!quiet)
649 pr_cont(" [%p],", obj);
650 }
651
88d6ed15 652 rht_for_each_entry_rcu(obj, pos, tbl, i, node)
3e7b2ec4
TG
653 rcu_cnt++;
654
655 if (rcu_cnt != cnt)
656 pr_warn("Test failed: Chain count mismach %d != %d",
657 cnt, rcu_cnt);
658
7e1e7763
TG
659 if (!quiet)
660 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
661 i, tbl->buckets[i], cnt);
662 }
663
664 pr_info(" Traversal complete: counted=%u, nelems=%zu, entries=%d\n",
665 total, ht->nelems, TEST_ENTRIES);
3e7b2ec4
TG
666
667 if (total != ht->nelems || total != TEST_ENTRIES)
668 pr_warn("Test failed: Total count mismatch ^^^");
7e1e7763
TG
669}
670
671static int __init test_rhashtable(struct rhashtable *ht)
672{
673 struct bucket_table *tbl;
88d6ed15
TG
674 struct test_obj *obj;
675 struct rhash_head *pos, *next;
7e1e7763
TG
676 int err;
677 unsigned int i;
678
679 /*
680 * Insertion Test:
681 * Insert TEST_ENTRIES into table with all keys even numbers
682 */
683 pr_info(" Adding %d keys\n", TEST_ENTRIES);
684 for (i = 0; i < TEST_ENTRIES; i++) {
685 struct test_obj *obj;
686
687 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
688 if (!obj) {
689 err = -ENOMEM;
690 goto error;
691 }
692
693 obj->ptr = TEST_PTR;
694 obj->value = i * 2;
695
6eba8224 696 rhashtable_insert(ht, &obj->node);
7e1e7763
TG
697 }
698
699 rcu_read_lock();
3e7b2ec4 700 test_bucket_stats(ht, true);
7e1e7763
TG
701 test_rht_lookup(ht);
702 rcu_read_unlock();
703
704 for (i = 0; i < TEST_NEXPANDS; i++) {
705 pr_info(" Table expansion iteration %u...\n", i);
6eba8224 706 rhashtable_expand(ht);
7e1e7763
TG
707
708 rcu_read_lock();
709 pr_info(" Verifying lookups...\n");
710 test_rht_lookup(ht);
711 rcu_read_unlock();
712 }
713
714 for (i = 0; i < TEST_NEXPANDS; i++) {
715 pr_info(" Table shrinkage iteration %u...\n", i);
6eba8224 716 rhashtable_shrink(ht);
7e1e7763
TG
717
718 rcu_read_lock();
719 pr_info(" Verifying lookups...\n");
720 test_rht_lookup(ht);
721 rcu_read_unlock();
722 }
723
3e7b2ec4
TG
724 rcu_read_lock();
725 test_bucket_stats(ht, true);
726 rcu_read_unlock();
727
7e1e7763
TG
728 pr_info(" Deleting %d keys\n", TEST_ENTRIES);
729 for (i = 0; i < TEST_ENTRIES; i++) {
730 u32 key = i * 2;
731
732 obj = rhashtable_lookup(ht, &key);
733 BUG_ON(!obj);
734
6eba8224 735 rhashtable_remove(ht, &obj->node);
7e1e7763
TG
736 kfree(obj);
737 }
738
739 return 0;
740
741error:
742 tbl = rht_dereference_rcu(ht->tbl, ht);
743 for (i = 0; i < tbl->size; i++)
88d6ed15 744 rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
7e1e7763
TG
745 kfree(obj);
746
747 return err;
748}
749
750static int __init test_rht_init(void)
751{
752 struct rhashtable ht;
753 struct rhashtable_params params = {
754 .nelem_hint = TEST_HT_SIZE,
755 .head_offset = offsetof(struct test_obj, node),
756 .key_offset = offsetof(struct test_obj, value),
757 .key_len = sizeof(int),
87545899 758 .hashfn = jhash,
1b2f309d 759#ifdef CONFIG_PROVE_LOCKING
7e1e7763 760 .mutex_is_held = &test_mutex_is_held,
1b2f309d 761#endif
7e1e7763
TG
762 .grow_decision = rht_grow_above_75,
763 .shrink_decision = rht_shrink_below_30,
764 };
765 int err;
766
767 pr_info("Running resizable hashtable tests...\n");
768
769 err = rhashtable_init(&ht, &params);
770 if (err < 0) {
771 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
772 err);
773 return err;
774 }
775
776 err = test_rhashtable(&ht);
777
778 rhashtable_destroy(&ht);
779
780 return err;
781}
782
783subsys_initcall(test_rht_init);
784
785#endif /* CONFIG_TEST_RHASHTABLE */