blob: 0ffca990a83370d13553266be315f9373d6995da [file] [log] [blame]
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +01001/*
2 * Resizable, Scalable, Concurrent Hash Table
3 *
Thomas Graf1aa661f2015-04-30 22:37:41 +00004 * Copyright (c) 2014-2015 Thomas Graf <tgraf@suug.ch>
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +01005 * Copyright (c) 2008-2014 Patrick McHardy <kaber@trash.net>
6 *
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +01007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12/**************************************************************************
13 * Self Test
14 **************************************************************************/
15
16#include <linux/init.h>
17#include <linux/jhash.h>
18#include <linux/kernel.h>
Phil Sutterf4a3e902015-08-15 00:37:15 +020019#include <linux/kthread.h>
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010020#include <linux/module.h>
21#include <linux/rcupdate.h>
22#include <linux/rhashtable.h>
Phil Sutterf4a3e902015-08-15 00:37:15 +020023#include <linux/semaphore.h>
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010024#include <linux/slab.h>
Thomas Graf685a0152015-07-17 10:52:48 +020025#include <linux/sched.h>
Phil Sutterf4a3e902015-08-15 00:37:15 +020026#include <linux/vmalloc.h>
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010027
Thomas Graf1aa661f2015-04-30 22:37:41 +000028#define MAX_ENTRIES 1000000
Thomas Graf67b7cbf2015-04-30 22:37:45 +000029#define TEST_INSERT_FAIL INT_MAX
Thomas Graf1aa661f2015-04-30 22:37:41 +000030
31static int entries = 50000;
32module_param(entries, int, 0);
33MODULE_PARM_DESC(entries, "Number of entries to add (default: 50000)");
34
35static int runs = 4;
36module_param(runs, int, 0);
37MODULE_PARM_DESC(runs, "Number of test runs per variant (default: 4)");
38
Phil Sutter95e435a2015-11-20 18:17:19 +010039static int max_size = 0;
Thomas Graf1aa661f2015-04-30 22:37:41 +000040module_param(max_size, int, 0);
Phil Sutter3b3bf802016-08-04 12:37:17 +020041MODULE_PARM_DESC(max_size, "Maximum table size (default: calculated)");
Thomas Graf1aa661f2015-04-30 22:37:41 +000042
43static bool shrinking = false;
44module_param(shrinking, bool, 0);
45MODULE_PARM_DESC(shrinking, "Enable automatic shrinking (default: off)");
46
47static int size = 8;
48module_param(size, int, 0);
49MODULE_PARM_DESC(size, "Initial size hint of table (default: 8)");
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010050
Phil Sutterf4a3e902015-08-15 00:37:15 +020051static int tcount = 10;
52module_param(tcount, int, 0);
53MODULE_PARM_DESC(tcount, "Number of threads to spawn (default: 10)");
54
Phil Sutterd662e032015-11-20 18:17:20 +010055static bool enomem_retry = false;
56module_param(enomem_retry, bool, 0);
57MODULE_PARM_DESC(enomem_retry, "Retry insert even if -ENOMEM was returned (default: off)");
58
Phil Suttere859afe2017-07-21 16:51:31 +020059struct test_obj_val {
60 int id;
61 int tid;
62};
63
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010064struct test_obj {
Phil Suttere859afe2017-07-21 16:51:31 +020065 struct test_obj_val value;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +010066 struct rhash_head node;
67};
68
Phil Sutterf4a3e902015-08-15 00:37:15 +020069struct thread_data {
70 int id;
71 struct task_struct *task;
72 struct test_obj *objs;
73};
74
Thomas Graffcc57022015-04-30 22:37:43 +000075static struct test_obj array[MAX_ENTRIES];
76
Thomas Graf1aa661f2015-04-30 22:37:41 +000077static struct rhashtable_params test_rht_params = {
Herbert Xub182aa62015-03-20 21:57:04 +110078 .head_offset = offsetof(struct test_obj, node),
79 .key_offset = offsetof(struct test_obj, value),
Phil Suttere859afe2017-07-21 16:51:31 +020080 .key_len = sizeof(struct test_obj_val),
Herbert Xub182aa62015-03-20 21:57:04 +110081 .hashfn = jhash,
Herbert Xub182aa62015-03-20 21:57:04 +110082 .nulls_base = (3U << RHT_BASE_SHIFT),
83};
84
Phil Sutterf4a3e902015-08-15 00:37:15 +020085static struct semaphore prestart_sem;
86static struct semaphore startup_sem = __SEMAPHORE_INITIALIZER(startup_sem, 0);
87
Phil Sutter9e9089e2015-11-20 18:17:18 +010088static int insert_retry(struct rhashtable *ht, struct rhash_head *obj,
89 const struct rhashtable_params params)
90{
Phil Sutterd662e032015-11-20 18:17:20 +010091 int err, retries = -1, enomem_retries = 0;
Phil Sutter9e9089e2015-11-20 18:17:18 +010092
93 do {
94 retries++;
95 cond_resched();
96 err = rhashtable_insert_fast(ht, obj, params);
Phil Sutterd662e032015-11-20 18:17:20 +010097 if (err == -ENOMEM && enomem_retry) {
98 enomem_retries++;
99 err = -EBUSY;
100 }
Phil Sutter9e9089e2015-11-20 18:17:18 +0100101 } while (err == -EBUSY);
102
Phil Sutterd662e032015-11-20 18:17:20 +0100103 if (enomem_retries)
104 pr_info(" %u insertions retried after -ENOMEM\n",
105 enomem_retries);
106
Phil Sutter9e9089e2015-11-20 18:17:18 +0100107 return err ? : retries;
108}
109
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100110static int __init test_rht_lookup(struct rhashtable *ht)
111{
112 unsigned int i;
113
Thomas Graf1aa661f2015-04-30 22:37:41 +0000114 for (i = 0; i < entries * 2; i++) {
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100115 struct test_obj *obj;
116 bool expected = !(i % 2);
Phil Suttere859afe2017-07-21 16:51:31 +0200117 struct test_obj_val key = {
118 .id = i,
119 };
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100120
Phil Suttere859afe2017-07-21 16:51:31 +0200121 if (array[i / 2].value.id == TEST_INSERT_FAIL)
Thomas Graf67b7cbf2015-04-30 22:37:45 +0000122 expected = false;
123
Herbert Xub182aa62015-03-20 21:57:04 +1100124 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100125
126 if (expected && !obj) {
Phil Suttere859afe2017-07-21 16:51:31 +0200127 pr_warn("Test failed: Could not find key %u\n", key.id);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100128 return -ENOENT;
129 } else if (!expected && obj) {
130 pr_warn("Test failed: Unexpected entry found for key %u\n",
Phil Suttere859afe2017-07-21 16:51:31 +0200131 key.id);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100132 return -EEXIST;
133 } else if (expected && obj) {
Phil Suttere859afe2017-07-21 16:51:31 +0200134 if (obj->value.id != i) {
Thomas Grafc2c8a902015-04-30 22:37:42 +0000135 pr_warn("Test failed: Lookup value mismatch %u!=%u\n",
Phil Suttere859afe2017-07-21 16:51:31 +0200136 obj->value.id, i);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100137 return -EINVAL;
138 }
139 }
Thomas Graf685a0152015-07-17 10:52:48 +0200140
141 cond_resched_rcu();
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100142 }
143
144 return 0;
145}
146
Thomas Graf246b23a2015-04-30 22:37:44 +0000147static void test_bucket_stats(struct rhashtable *ht)
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100148{
Thomas Graf246b23a2015-04-30 22:37:44 +0000149 unsigned int err, total = 0, chain_len = 0;
150 struct rhashtable_iter hti;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100151 struct rhash_head *pos;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100152
Bob Copeland8f6fd832016-03-02 10:09:19 -0500153 err = rhashtable_walk_init(ht, &hti, GFP_KERNEL);
Thomas Graf246b23a2015-04-30 22:37:44 +0000154 if (err) {
155 pr_warn("Test failed: allocation error");
156 return;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100157 }
158
Thomas Graf246b23a2015-04-30 22:37:44 +0000159 err = rhashtable_walk_start(&hti);
160 if (err && err != -EAGAIN) {
161 pr_warn("Test failed: iterator failed: %d\n", err);
162 return;
163 }
164
165 while ((pos = rhashtable_walk_next(&hti))) {
166 if (PTR_ERR(pos) == -EAGAIN) {
167 pr_info("Info: encountered resize\n");
168 chain_len++;
169 continue;
170 } else if (IS_ERR(pos)) {
171 pr_warn("Test failed: rhashtable_walk_next() error: %ld\n",
172 PTR_ERR(pos));
173 break;
174 }
175
176 total++;
177 }
178
179 rhashtable_walk_stop(&hti);
180 rhashtable_walk_exit(&hti);
181
182 pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d, table-jumps=%u\n",
183 total, atomic_read(&ht->nelems), entries, chain_len);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100184
Thomas Graf1aa661f2015-04-30 22:37:41 +0000185 if (total != atomic_read(&ht->nelems) || total != entries)
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100186 pr_warn("Test failed: Total count mismatch ^^^");
187}
188
Thomas Graf1aa661f2015-04-30 22:37:41 +0000189static s64 __init test_rhashtable(struct rhashtable *ht)
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100190{
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100191 struct test_obj *obj;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100192 int err;
Phil Sutter9e9089e2015-11-20 18:17:18 +0100193 unsigned int i, insert_retries = 0;
Thomas Graf1aa661f2015-04-30 22:37:41 +0000194 s64 start, end;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100195
196 /*
197 * Insertion Test:
Thomas Graf1aa661f2015-04-30 22:37:41 +0000198 * Insert entries into table with all keys even numbers
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100199 */
Thomas Graf1aa661f2015-04-30 22:37:41 +0000200 pr_info(" Adding %d keys\n", entries);
201 start = ktime_get_ns();
202 for (i = 0; i < entries; i++) {
Thomas Graffcc57022015-04-30 22:37:43 +0000203 struct test_obj *obj = &array[i];
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100204
Phil Suttere859afe2017-07-21 16:51:31 +0200205 obj->value.id = i * 2;
Phil Sutter9e9089e2015-11-20 18:17:18 +0100206 err = insert_retry(ht, &obj->node, test_rht_params);
207 if (err > 0)
208 insert_retries += err;
209 else if (err)
Thomas Graffcc57022015-04-30 22:37:43 +0000210 return err;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100211 }
212
Phil Sutter9e9089e2015-11-20 18:17:18 +0100213 if (insert_retries)
214 pr_info(" %u insertions retried due to memory pressure\n",
215 insert_retries);
Thomas Graf67b7cbf2015-04-30 22:37:45 +0000216
Thomas Graf246b23a2015-04-30 22:37:44 +0000217 test_bucket_stats(ht);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100218 rcu_read_lock();
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100219 test_rht_lookup(ht);
220 rcu_read_unlock();
221
Thomas Graf246b23a2015-04-30 22:37:44 +0000222 test_bucket_stats(ht);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100223
Thomas Graf1aa661f2015-04-30 22:37:41 +0000224 pr_info(" Deleting %d keys\n", entries);
225 for (i = 0; i < entries; i++) {
Phil Sutter78369252017-07-25 13:36:21 +0200226 struct test_obj_val key = {
227 .id = i * 2,
228 };
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100229
Phil Suttere859afe2017-07-21 16:51:31 +0200230 if (array[i].value.id != TEST_INSERT_FAIL) {
Thomas Graf67b7cbf2015-04-30 22:37:45 +0000231 obj = rhashtable_lookup_fast(ht, &key, test_rht_params);
232 BUG_ON(!obj);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100233
Thomas Graf67b7cbf2015-04-30 22:37:45 +0000234 rhashtable_remove_fast(ht, &obj->node, test_rht_params);
235 }
Thomas Graf685a0152015-07-17 10:52:48 +0200236
237 cond_resched();
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100238 }
239
Thomas Graf1aa661f2015-04-30 22:37:41 +0000240 end = ktime_get_ns();
241 pr_info(" Duration of test: %lld ns\n", end - start);
242
243 return end - start;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100244}
245
Daniel Borkmannb7f5e5c2015-02-20 21:14:21 +0100246static struct rhashtable ht;
247
Phil Sutterf4a3e902015-08-15 00:37:15 +0200248static int thread_lookup_test(struct thread_data *tdata)
249{
250 int i, err = 0;
251
252 for (i = 0; i < entries; i++) {
253 struct test_obj *obj;
Phil Suttere859afe2017-07-21 16:51:31 +0200254 struct test_obj_val key = {
255 .id = i,
256 .tid = tdata->id,
257 };
Phil Sutterf4a3e902015-08-15 00:37:15 +0200258
259 obj = rhashtable_lookup_fast(&ht, &key, test_rht_params);
Phil Suttere859afe2017-07-21 16:51:31 +0200260 if (obj && (tdata->objs[i].value.id == TEST_INSERT_FAIL)) {
261 pr_err(" found unexpected object %d-%d\n", key.tid, key.id);
Phil Sutterf4a3e902015-08-15 00:37:15 +0200262 err++;
Phil Suttere859afe2017-07-21 16:51:31 +0200263 } else if (!obj && (tdata->objs[i].value.id != TEST_INSERT_FAIL)) {
264 pr_err(" object %d-%d not found!\n", key.tid, key.id);
Phil Sutterf4a3e902015-08-15 00:37:15 +0200265 err++;
Phil Suttere859afe2017-07-21 16:51:31 +0200266 } else if (obj && memcmp(&obj->value, &key, sizeof(key))) {
267 pr_err(" wrong object returned (got %d-%d, expected %d-%d)\n",
268 obj->value.tid, obj->value.id, key.tid, key.id);
Phil Sutterf4a3e902015-08-15 00:37:15 +0200269 err++;
270 }
Phil Suttercd5b3182015-11-20 18:17:17 +0100271
272 cond_resched();
Phil Sutterf4a3e902015-08-15 00:37:15 +0200273 }
274 return err;
275}
276
277static int threadfunc(void *data)
278{
Phil Sutter9e9089e2015-11-20 18:17:18 +0100279 int i, step, err = 0, insert_retries = 0;
Phil Sutterf4a3e902015-08-15 00:37:15 +0200280 struct thread_data *tdata = data;
281
282 up(&prestart_sem);
283 if (down_interruptible(&startup_sem))
284 pr_err(" thread[%d]: down_interruptible failed\n", tdata->id);
285
286 for (i = 0; i < entries; i++) {
Phil Suttere859afe2017-07-21 16:51:31 +0200287 tdata->objs[i].value.id = i;
288 tdata->objs[i].value.tid = tdata->id;
Phil Sutter9e9089e2015-11-20 18:17:18 +0100289 err = insert_retry(&ht, &tdata->objs[i].node, test_rht_params);
290 if (err > 0) {
291 insert_retries += err;
Phil Sutterf4a3e902015-08-15 00:37:15 +0200292 } else if (err) {
293 pr_err(" thread[%d]: rhashtable_insert_fast failed\n",
294 tdata->id);
295 goto out;
296 }
297 }
Phil Sutter9e9089e2015-11-20 18:17:18 +0100298 if (insert_retries)
299 pr_info(" thread[%d]: %u insertions retried due to memory pressure\n",
300 tdata->id, insert_retries);
Phil Sutterf4a3e902015-08-15 00:37:15 +0200301
302 err = thread_lookup_test(tdata);
303 if (err) {
304 pr_err(" thread[%d]: rhashtable_lookup_test failed\n",
305 tdata->id);
306 goto out;
307 }
308
309 for (step = 10; step > 0; step--) {
310 for (i = 0; i < entries; i += step) {
Phil Suttere859afe2017-07-21 16:51:31 +0200311 if (tdata->objs[i].value.id == TEST_INSERT_FAIL)
Phil Sutterf4a3e902015-08-15 00:37:15 +0200312 continue;
313 err = rhashtable_remove_fast(&ht, &tdata->objs[i].node,
314 test_rht_params);
315 if (err) {
316 pr_err(" thread[%d]: rhashtable_remove_fast failed\n",
317 tdata->id);
318 goto out;
319 }
Phil Suttere859afe2017-07-21 16:51:31 +0200320 tdata->objs[i].value.id = TEST_INSERT_FAIL;
Phil Suttercd5b3182015-11-20 18:17:17 +0100321
322 cond_resched();
Phil Sutterf4a3e902015-08-15 00:37:15 +0200323 }
324 err = thread_lookup_test(tdata);
325 if (err) {
326 pr_err(" thread[%d]: rhashtable_lookup_test (2) failed\n",
327 tdata->id);
328 goto out;
329 }
330 }
331out:
332 while (!kthread_should_stop()) {
333 set_current_state(TASK_INTERRUPTIBLE);
334 schedule();
335 }
336 return err;
337}
338
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100339static int __init test_rht_init(void)
340{
Phil Sutterf4a3e902015-08-15 00:37:15 +0200341 int i, err, started_threads = 0, failed_threads = 0;
Thomas Graf1aa661f2015-04-30 22:37:41 +0000342 u64 total_time = 0;
Phil Sutterf4a3e902015-08-15 00:37:15 +0200343 struct thread_data *tdata;
344 struct test_obj *objs;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100345
Thomas Graf1aa661f2015-04-30 22:37:41 +0000346 entries = min(entries, MAX_ENTRIES);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100347
Thomas Graf1aa661f2015-04-30 22:37:41 +0000348 test_rht_params.automatic_shrinking = shrinking;
Phil Sutter95e435a2015-11-20 18:17:19 +0100349 test_rht_params.max_size = max_size ? : roundup_pow_of_two(entries);
Thomas Graf1aa661f2015-04-30 22:37:41 +0000350 test_rht_params.nelem_hint = size;
351
352 pr_info("Running rhashtable test nelem=%d, max_size=%d, shrinking=%d\n",
353 size, max_size, shrinking);
354
355 for (i = 0; i < runs; i++) {
356 s64 time;
357
358 pr_info("Test %02d:\n", i);
Thomas Graffcc57022015-04-30 22:37:43 +0000359 memset(&array, 0, sizeof(array));
Thomas Graf1aa661f2015-04-30 22:37:41 +0000360 err = rhashtable_init(&ht, &test_rht_params);
361 if (err < 0) {
362 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
363 err);
364 continue;
365 }
366
367 time = test_rhashtable(&ht);
368 rhashtable_destroy(&ht);
369 if (time < 0) {
370 pr_warn("Test failed: return code %lld\n", time);
371 return -EINVAL;
372 }
373
374 total_time += time;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100375 }
376
Thomas Graf6decd632015-05-05 02:27:02 +0200377 do_div(total_time, runs);
378 pr_info("Average test time: %llu\n", total_time);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100379
Phil Sutterf4a3e902015-08-15 00:37:15 +0200380 if (!tcount)
381 return 0;
382
383 pr_info("Testing concurrent rhashtable access from %d threads\n",
384 tcount);
385 sema_init(&prestart_sem, 1 - tcount);
386 tdata = vzalloc(tcount * sizeof(struct thread_data));
387 if (!tdata)
388 return -ENOMEM;
389 objs = vzalloc(tcount * entries * sizeof(struct test_obj));
390 if (!objs) {
391 vfree(tdata);
392 return -ENOMEM;
393 }
394
Phil Sutter95e435a2015-11-20 18:17:19 +0100395 test_rht_params.max_size = max_size ? :
396 roundup_pow_of_two(tcount * entries);
Phil Sutterf4a3e902015-08-15 00:37:15 +0200397 err = rhashtable_init(&ht, &test_rht_params);
398 if (err < 0) {
399 pr_warn("Test failed: Unable to initialize hashtable: %d\n",
400 err);
401 vfree(tdata);
402 vfree(objs);
403 return -EINVAL;
404 }
405 for (i = 0; i < tcount; i++) {
406 tdata[i].id = i;
407 tdata[i].objs = objs + i * entries;
408 tdata[i].task = kthread_run(threadfunc, &tdata[i],
409 "rhashtable_thrad[%d]", i);
410 if (IS_ERR(tdata[i].task))
411 pr_err(" kthread_run failed for thread %d\n", i);
412 else
413 started_threads++;
414 }
415 if (down_interruptible(&prestart_sem))
416 pr_err(" down interruptible failed\n");
417 for (i = 0; i < tcount; i++)
418 up(&startup_sem);
419 for (i = 0; i < tcount; i++) {
420 if (IS_ERR(tdata[i].task))
421 continue;
422 if ((err = kthread_stop(tdata[i].task))) {
423 pr_warn("Test failed: thread %d returned: %d\n",
424 i, err);
425 failed_threads++;
426 }
427 }
428 pr_info("Started %d threads, %d failed\n",
429 started_threads, failed_threads);
430 rhashtable_destroy(&ht);
431 vfree(tdata);
432 vfree(objs);
Thomas Graf1aa661f2015-04-30 22:37:41 +0000433 return 0;
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100434}
435
Daniel Borkmann6dd0c162015-02-20 00:53:39 +0100436static void __exit test_rht_exit(void)
437{
438}
439
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100440module_init(test_rht_init);
Daniel Borkmann6dd0c162015-02-20 00:53:39 +0100441module_exit(test_rht_exit);
Geert Uytterhoeven9d6dbe12015-01-29 15:40:25 +0100442
443MODULE_LICENSE("GPL v2");