blob: a2ba6adb60a2d0c57510c4926fe2308e486acc4c [file] [log] [blame]
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +01001/*
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/**************************************************************************
18 * Self Test
19 **************************************************************************/
20
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>
28
29
30#define TEST_HT_SIZE 8
31#define TEST_ENTRIES 2048
32#define TEST_PTR ((void *) 0xdeadbeef)
33#define TEST_NEXPANDS 4
34
35struct test_obj {
36 void *ptr;
37 int value;
38 struct rhash_head node;
39};
40
Herbert Xub182aa62015-03-20 21:57:04 +110041static 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),
46 .hashfn = jhash,
47 .max_size = 2, /* we expand/shrink manually here */
48 .nulls_base = (3U << RHT_BASE_SHIFT),
49};
50
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010051static int __init test_rht_lookup(struct rhashtable *ht)
52{
53 unsigned int i;
54
55 for (i = 0; i < TEST_ENTRIES * 2; i++) {
56 struct test_obj *obj;
57 bool expected = !(i % 2);
58 u32 key = i;
59
Herbert Xub182aa62015-03-20 21:57:04 +110060 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010061
62 if (expected && !obj) {
63 pr_warn("Test failed: Could not find key %u\n", key);
64 return -ENOENT;
65 } else if (!expected && obj) {
66 pr_warn("Test failed: Unexpected entry found for key %u\n",
67 key);
68 return -EEXIST;
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);
73 return -EINVAL;
74 }
75 }
76 }
77
78 return 0;
79}
80
81static void test_bucket_stats(struct rhashtable *ht, bool quiet)
82{
83 unsigned int cnt, rcu_cnt, i, total = 0;
84 struct rhash_head *pos;
85 struct test_obj *obj;
86 struct bucket_table *tbl;
87
88 tbl = rht_dereference_rcu(ht->tbl, ht);
89 for (i = 0; i < tbl->size; i++) {
90 rcu_cnt = cnt = 0;
91
92 if (!quiet)
Herbert Xu63d512d2015-03-14 13:57:24 +110093 pr_info(" [%#4x/%u]", i, tbl->size);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010094
95 rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
96 cnt++;
97 total++;
98 if (!quiet)
99 pr_cont(" [%p],", obj);
100 }
101
102 rht_for_each_entry_rcu(obj, pos, tbl, i, node)
103 rcu_cnt++;
104
105 if (rcu_cnt != cnt)
106 pr_warn("Test failed: Chain count mismach %d != %d",
107 cnt, rcu_cnt);
108
109 if (!quiet)
110 pr_cont("\n [%#x] first element: %p, chain length: %u\n",
111 i, tbl->buckets[i], cnt);
112 }
113
114 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
115 total, atomic_read(&ht->nelems), TEST_ENTRIES);
116
117 if (total != atomic_read(&ht->nelems) || total != TEST_ENTRIES)
118 pr_warn("Test failed: Total count mismatch ^^^");
119}
120
121static int __init test_rhashtable(struct rhashtable *ht)
122{
123 struct bucket_table *tbl;
124 struct test_obj *obj;
125 struct rhash_head *pos, *next;
126 int err;
127 unsigned int i;
128
129 /*
130 * Insertion Test:
131 * Insert TEST_ENTRIES into table with all keys even numbers
132 */
133 pr_info(" Adding %d keys\n", TEST_ENTRIES);
134 for (i = 0; i < TEST_ENTRIES; i++) {
135 struct test_obj *obj;
136
137 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
138 if (!obj) {
139 err = -ENOMEM;
140 goto error;
141 }
142
143 obj->ptr = TEST_PTR;
144 obj->value = i * 2;
145
Herbert Xub182aa62015-03-20 21:57:04 +1100146 err = rhashtable_insert_fast(ht, &obj->node, test_rht_params);
147 if (err) {
148 kfree(obj);
149 goto error;
150 }
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100151 }
152
153 rcu_read_lock();
154 test_bucket_stats(ht, true);
155 test_rht_lookup(ht);
156 rcu_read_unlock();
157
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);
163
164 rcu_read_lock();
165 pr_info(" Verifying lookups...\n");
166 test_rht_lookup(ht);
167 rcu_read_unlock();
168 }
169
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);
175
176 rcu_read_lock();
177 pr_info(" Verifying lookups...\n");
178 test_rht_lookup(ht);
179 rcu_read_unlock();
180 }
181
182 rcu_read_lock();
183 test_bucket_stats(ht, true);
184 rcu_read_unlock();
185
186 pr_info(" Deleting %d keys\n", TEST_ENTRIES);
187 for (i = 0; i < TEST_ENTRIES; i++) {
188 u32 key = i * 2;
189
Herbert Xub182aa62015-03-20 21:57:04 +1100190 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100191 BUG_ON(!obj);
192
Herbert Xub182aa62015-03-20 21:57:04 +1100193 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100194 kfree(obj);
195 }
196
197 return 0;
198
199error:
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)
203 kfree(obj);
204
205 return err;
206}
207
Daniel Borkmannb7f5e5c2015-02-20 21:14:21 +0100208static struct rhashtable ht;
209
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100210static int __init test_rht_init(void)
211{
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100212 int err;
213
214 pr_info("Running resizable hashtable tests...\n");
215
Herbert Xub182aa62015-03-20 21:57:04 +1100216 err = rhashtable_init(&ht, &test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100217 if (err < 0) {
218 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
219 err);
220 return err;
221 }
222
223 err = test_rhashtable(&ht);
224
225 rhashtable_destroy(&ht);
226
227 return err;
228}
229
Daniel Borkmann6dd0c162015-02-20 00:53:39 +0100230static void __exit test_rht_exit(void)
231{
232}
233
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100234module_init(test_rht_init);
Daniel Borkmann6dd0c162015-02-20 00:53:39 +0100235module_exit(test_rht_exit);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100236
237MODULE_LICENSE("GPL v2");