Skip to content

Commit 9d6dbe1

Browse files
geertudavem330
authored andcommitted
rhashtable: Make selftest modular
Allow the selftest on the resizable hash table to be built modular, just like all other tests that do not depend on DEBUG_KERNEL. Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org> Acked-by: Thomas Graf <tgraf@suug.ch> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 207895f commit 9d6dbe1

File tree

4 files changed

+229
-206
lines changed

4 files changed

+229
-206
lines changed

lib/Kconfig.debug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,7 +1586,7 @@ config TEST_KSTRTOX
15861586
tristate "Test kstrto*() family of functions at runtime"
15871587

15881588
config TEST_RHASHTABLE
1589-
bool "Perform selftest on resizable hash table"
1589+
tristate "Perform selftest on resizable hash table"
15901590
default n
15911591
help
15921592
Enable this option to test the rhashtable functions at boot.

lib/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ obj-$(CONFIG_TEST_LKM) += test_module.o
3535
obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
3636
obj-$(CONFIG_TEST_BPF) += test_bpf.o
3737
obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
38+
obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
3839

3940
ifeq ($(CONFIG_DEBUG_KOBJECT),y)
4041
CFLAGS_kobject.o += -DDEBUG

lib/rhashtable.c

Lines changed: 0 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -935,208 +935,3 @@ void rhashtable_destroy(struct rhashtable *ht)
935935
mutex_unlock(&ht->mutex);
936936
}
937937
EXPORT_SYMBOL_GPL(rhashtable_destroy);
938-
939-
/**************************************************************************
940-
* Self Test
941-
**************************************************************************/
942-
943-
#ifdef CONFIG_TEST_RHASHTABLE
944-
945-
#define TEST_HT_SIZE 8
946-
#define TEST_ENTRIES 2048
947-
#define TEST_PTR ((void *) 0xdeadbeef)
948-
#define TEST_NEXPANDS 4
949-
950-
struct test_obj {
951-
void *ptr;
952-
int value;
953-
struct rhash_head node;
954-
};
955-
956-
static int __init test_rht_lookup(struct rhashtable *ht)
957-
{
958-
unsigned int i;
959-
960-
for (i = 0; i < TEST_ENTRIES * 2; i++) {
961-
struct test_obj *obj;
962-
bool expected = !(i % 2);
963-
u32 key = i;
964-
965-
obj = rhashtable_lookup(ht, &key);
966-
967-
if (expected && !obj) {
968-
pr_warn("Test failed: Could not find key %u\n", key);
969-
return -ENOENT;
970-
} else if (!expected && obj) {
971-
pr_warn("Test failed: Unexpected entry found for key %u\n",
972-
key);
973-
return -EEXIST;
974-
} else if (expected && obj) {
975-
if (obj->ptr != TEST_PTR || obj->value != i) {
976-
pr_warn("Test failed: Lookup value mismatch %p!=%p, %u!=%u\n",
977-
obj->ptr, TEST_PTR, obj->value, i);
978-
return -EINVAL;
979-
}
980-
}
981-
}
982-
983-
return 0;
984-
}
985-
986-
static void test_bucket_stats(struct rhashtable *ht, bool quiet)
987-
{
988-
unsigned int cnt, rcu_cnt, i, total = 0;
989-
struct rhash_head *pos;
990-
struct test_obj *obj;
991-
struct bucket_table *tbl;
992-
993-
tbl = rht_dereference_rcu(ht->tbl, ht);
994-
for (i = 0; i < tbl->size; i++) {
995-
rcu_cnt = cnt = 0;
996-
997-
if (!quiet)
998-
pr_info(" [%#4x/%zu]", i, tbl->size);
999-
1000-
rht_for_each_entry_rcu(obj, pos, tbl, i, node) {
1001-
cnt++;
1002-
total++;
1003-
if (!quiet)
1004-
pr_cont(" [%p],", obj);
1005-
}
1006-
1007-
rht_for_each_entry_rcu(obj, pos, tbl, i, node)
1008-
rcu_cnt++;
1009-
1010-
if (rcu_cnt != cnt)
1011-
pr_warn("Test failed: Chain count mismach %d != %d",
1012-
cnt, rcu_cnt);
1013-
1014-
if (!quiet)
1015-
pr_cont("\n [%#x] first element: %p, chain length: %u\n",
1016-
i, tbl->buckets[i], cnt);
1017-
}
1018-
1019-
pr_info(" Traversal complete: counted=%u, nelems=%u, entries=%d\n",
1020-
total, atomic_read(&ht->nelems), TEST_ENTRIES);
1021-
1022-
if (total != atomic_read(&ht->nelems) || total != TEST_ENTRIES)
1023-
pr_warn("Test failed: Total count mismatch ^^^");
1024-
}
1025-
1026-
static int __init test_rhashtable(struct rhashtable *ht)
1027-
{
1028-
struct bucket_table *tbl;
1029-
struct test_obj *obj;
1030-
struct rhash_head *pos, *next;
1031-
int err;
1032-
unsigned int i;
1033-
1034-
/*
1035-
* Insertion Test:
1036-
* Insert TEST_ENTRIES into table with all keys even numbers
1037-
*/
1038-
pr_info(" Adding %d keys\n", TEST_ENTRIES);
1039-
for (i = 0; i < TEST_ENTRIES; i++) {
1040-
struct test_obj *obj;
1041-
1042-
obj = kzalloc(sizeof(*obj), GFP_KERNEL);
1043-
if (!obj) {
1044-
err = -ENOMEM;
1045-
goto error;
1046-
}
1047-
1048-
obj->ptr = TEST_PTR;
1049-
obj->value = i * 2;
1050-
1051-
rhashtable_insert(ht, &obj->node);
1052-
}
1053-
1054-
rcu_read_lock();
1055-
test_bucket_stats(ht, true);
1056-
test_rht_lookup(ht);
1057-
rcu_read_unlock();
1058-
1059-
for (i = 0; i < TEST_NEXPANDS; i++) {
1060-
pr_info(" Table expansion iteration %u...\n", i);
1061-
mutex_lock(&ht->mutex);
1062-
rhashtable_expand(ht);
1063-
mutex_unlock(&ht->mutex);
1064-
1065-
rcu_read_lock();
1066-
pr_info(" Verifying lookups...\n");
1067-
test_rht_lookup(ht);
1068-
rcu_read_unlock();
1069-
}
1070-
1071-
for (i = 0; i < TEST_NEXPANDS; i++) {
1072-
pr_info(" Table shrinkage iteration %u...\n", i);
1073-
mutex_lock(&ht->mutex);
1074-
rhashtable_shrink(ht);
1075-
mutex_unlock(&ht->mutex);
1076-
1077-
rcu_read_lock();
1078-
pr_info(" Verifying lookups...\n");
1079-
test_rht_lookup(ht);
1080-
rcu_read_unlock();
1081-
}
1082-
1083-
rcu_read_lock();
1084-
test_bucket_stats(ht, true);
1085-
rcu_read_unlock();
1086-
1087-
pr_info(" Deleting %d keys\n", TEST_ENTRIES);
1088-
for (i = 0; i < TEST_ENTRIES; i++) {
1089-
u32 key = i * 2;
1090-
1091-
obj = rhashtable_lookup(ht, &key);
1092-
BUG_ON(!obj);
1093-
1094-
rhashtable_remove(ht, &obj->node);
1095-
kfree(obj);
1096-
}
1097-
1098-
return 0;
1099-
1100-
error:
1101-
tbl = rht_dereference_rcu(ht->tbl, ht);
1102-
for (i = 0; i < tbl->size; i++)
1103-
rht_for_each_entry_safe(obj, pos, next, tbl, i, node)
1104-
kfree(obj);
1105-
1106-
return err;
1107-
}
1108-
1109-
static int __init test_rht_init(void)
1110-
{
1111-
struct rhashtable ht;
1112-
struct rhashtable_params params = {
1113-
.nelem_hint = TEST_HT_SIZE,
1114-
.head_offset = offsetof(struct test_obj, node),
1115-
.key_offset = offsetof(struct test_obj, value),
1116-
.key_len = sizeof(int),
1117-
.hashfn = jhash,
1118-
.nulls_base = (3U << RHT_BASE_SHIFT),
1119-
.grow_decision = rht_grow_above_75,
1120-
.shrink_decision = rht_shrink_below_30,
1121-
};
1122-
int err;
1123-
1124-
pr_info("Running resizable hashtable tests...\n");
1125-
1126-
err = rhashtable_init(&ht, &params);
1127-
if (err < 0) {
1128-
pr_warn("Test failed: Unable to initialize hashtable: %d\n",
1129-
err);
1130-
return err;
1131-
}
1132-
1133-
err = test_rhashtable(&ht);
1134-
1135-
rhashtable_destroy(&ht);
1136-
1137-
return err;
1138-
}
1139-
1140-
subsys_initcall(test_rht_init);
1141-
1142-
#endif /* CONFIG_TEST_RHASHTABLE */

0 commit comments

Comments
 (0)