2 * Resizable, Scalable, Concurrent Hash Table
4 * Copyright (c) 2014 Thomas Graf <tgraf@suug.ch>
5 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
7 * Based on the following paper:
8 * https://www.usenix.org/legacy/event/atc11/tech/final_files/Triplett.pdf
10 * Code partially derived from nft_hash
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.
17 /**************************************************************************
19 **************************************************************************/
21 #include <linux/init.h>
22 #include <linux/jhash.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/rcupdate.h>
26 #include <linux/rhashtable.h>
27 #include <linux/slab.h>
30 #define TEST_HT_SIZE 8
31 #define TEST_ENTRIES 2048
32 #define TEST_PTR ((void *) 0xdeadbeef)
33 #define TEST_NEXPANDS 4
38 struct rhash_head node;
41 static const struct rhashtable_params test_rht_params = {
42 .nelem_hint = TEST_HT_SIZE,
43 .head_offset = offsetof(struct test_obj, node),
44 .key_offset = offsetof(struct test_obj, value),
45 .key_len = sizeof(int),
47 .max_size = 2, /* we expand/shrink manually here */
48 .nulls_base = (3U << RHT_BASE_SHIFT),
51 static int __init test_rht_lookup(struct rhashtable *ht)
55 for (i = 0; i < TEST_ENTRIES * 2; i++) {
57 bool expected = !(i % 2);
60 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
62 if (expected && !obj) {
63 pr_warn("Test failed: Could not find key %u\n", key);
65 } else if (!expected && obj) {
66 pr_warn("Test failed: Unexpected entry found for key %u\n",
69 } else if (expected && obj) {
70 if (obj->ptr != TEST_PTR || obj->value != i) {
71 pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
72 obj->ptr, TEST_PTR, obj->value, i);
81 static void test_bucket_stats(struct rhashtable *ht, bool quiet)
83 unsigned int cnt, rcu_cnt, i, total = 0;
84 struct rhash_head *pos;
86 struct bucket_table *tbl;
88 tbl = rht_dereference_rcu(ht->tbl, ht);
89 for (i = 0; i < tbl->size; i++) {
93 pr_info(" [%#4x/%u]", i, tbl->size);
95 rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
99 pr_cont(" [%p],", obj);
102 rht_for_each_entry_rcu(obj, pos, tbl, i, node)
106 pr_warn("Test failed: Chain count mismach %d != %d",
110 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
111 i, tbl->buckets[i], cnt);
114 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
115 total, atomic_read(&ht->nelems), TEST_ENTRIES);
117 if (total != atomic_read(&ht->nelems) || total != TEST_ENTRIES)
118 pr_warn("Test failed: Total count mismatch ^^^");
121 static int __init test_rhashtable(struct rhashtable *ht)
123 struct bucket_table *tbl;
124 struct test_obj *obj;
125 struct rhash_head *pos, *next;
131 * Insert TEST_ENTRIES into table with all keys even numbers
133 pr_info(" Adding %d keys\n", TEST_ENTRIES);
134 for (i = 0; i < TEST_ENTRIES; i++) {
135 struct test_obj *obj;
137 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
146 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
154 test_bucket_stats(ht, true);
158 for (i = 0; i < TEST_NEXPANDS; i++) {
159 pr_info(" Table expansion iteration %u...\n", i);
160 mutex_lock(&ht->mutex);
161 rhashtable_expand(ht);
162 mutex_unlock(&ht->mutex);
165 pr_info(" Verifying lookups...\n");
170 for (i = 0; i < TEST_NEXPANDS; i++) {
171 pr_info(" Table shrinkage iteration %u...\n", i);
172 mutex_lock(&ht->mutex);
173 rhashtable_shrink(ht);
174 mutex_unlock(&ht->mutex);
177 pr_info(" Verifying lookups...\n");
183 test_bucket_stats(ht, true);
186 pr_info(" Deleting %d keys\n", TEST_ENTRIES);
187 for (i = 0; i < TEST_ENTRIES; i++) {
190 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
193 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
200 tbl = rht_dereference_rcu(ht->tbl, ht);
201 for (i = 0; i < tbl->size; i++)
202 rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
208 static struct rhashtable ht;
210 static int __init test_rht_init(void)
214 pr_info("Running resizable hashtable tests...\n");
216 err = rhashtable_init(&ht, &test_rht_params);
218 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
223 err = test_rhashtable(&ht);
225 rhashtable_destroy(&ht);
230 static void __exit test_rht_exit(void)
234 module_init(test_rht_init);
235 module_exit(test_rht_exit);
237 MODULE_LICENSE("GPL v2");