From: Randy Dunlap // modified from lib/test_bitops.c Signed-off-by: Randy Dunlap --- lib/Kconfig.debug | 11 +++ lib/Makefile | 1 lib/test_log2.c | 139 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+) --- linux-next-20201021.orig/lib/Kconfig.debug +++ linux-next-20201021/lib/Kconfig.debug @@ -2136,6 +2136,17 @@ config TEST_BITOPS If unsure, say N. +config TEST_LOG2 + tristate "Test module for log2 interfaces" + depends on m + help + This builds the "test_log2" module that does basic testing of + all interfaces. + It has no dependencies and doesn't run or load unless + explicitly requested by name. For example: modprobe test_log2. + + If unsure, say N. + config TEST_VMALLOC tristate "Test module for stress/performance analysis of vmalloc allocator" default n --- linux-next-20201021.orig/lib/Makefile +++ linux-next-20201021/lib/Makefile @@ -62,6 +62,7 @@ obj-$(CONFIG_TEST_BPF) += test_bpf.o obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o obj-$(CONFIG_TEST_BITOPS) += test_bitops.o CFLAGS_test_bitops.o += -Werror +obj-$(CONFIG_TEST_LOG2) += test_log2.o obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o obj-$(CONFIG_TEST_HASH) += test_hash.o test_siphash.o obj-$(CONFIG_TEST_IDA) += test_ida.o --- /dev/null +++ linux-next-20201021/lib/test_log2.c @@ -0,0 +1,139 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +// currently missing tests of roundup/rounddown macros; + +#include +#include +#include +#include +#include + +/* + * a tiny module for testing all interfaces in + */ + +static unsigned int test_power_of_2[][2] = { + {0, 0}, + {PAGE_SIZE, 1}, + {65535, 0}, + {65536, 1}, + {(unsigned int)~0, 0}, + {0x80000000, 1}, + {0x80008000, 0}, +}; + +static unsigned int test_ilog2_uint[][2] = { + //{0, -1}, + {1, 0}, + {2, 1}, + {4, 2}, + {(unsigned int)~0, 31}, + {0x80000000, 31}, + {0x80008000, 31}, +}; + +static unsigned long test_ilog2_ulong[][2] = { + //{0, -1}, + {1, 0}, + {2, 1}, + {4, 2}, + {(unsigned long)~0, 63}, + {0x80000000UL, 31}, + {0x80008000UL, 31}, +}; + +static unsigned int order_base2[][2] = { + {0x00000003, 2}, + {0x00000004, 2}, + {0x00001fff, 13}, + {0x00002000, 13}, + {0x50000000, 31}, + {0x80000000, 31}, + {0x80003000, 32}, +}; + +//#ifdef CONFIG_64BIT +static unsigned long order_base2_long[][2] = { + {0x0000000300000000, 34}, + {0x0000000400000000, 34}, + {0x00001fff00000000, 45}, + {0x0000200000000000, 45}, + {0x5000000000000000, 63}, + {0x8000000000000000, 63}, + {0x8000300000000000, 64}, +}; +//#endif + +static unsigned int test_bits_per[][2] = { + {0, 1}, + {1, 1}, + {2, 2}, + {3, 2}, + {4, 3}, + {0x0fff, 12}, + {0x1000, 13}, + {0x80003000, 32}, +}; + +static int __init test_log2_start(void) +{ + int i; + + pr_info("Starting log2 tests\n"); + + for (i = 0; i < ARRAY_SIZE(test_power_of_2); i++) { + if (is_power_of_2(test_power_of_2[i][0]) != test_power_of_2[i][1]) + pr_warn("is_power_of_2 wrong for %x\n", + test_power_of_2[i][0]); + } + + for (i = 0; i < ARRAY_SIZE(test_ilog2_uint); i++) { + if (ilog2(test_ilog2_uint[i][0]) != test_ilog2_uint[i][1]) + pr_warn("ilog2 wrong for uint %x\n", + test_ilog2_uint[i][0]); + } + + for (i = 0; i < ARRAY_SIZE(test_ilog2_ulong); i++) { + if (ilog2(test_ilog2_ulong[i][0]) != test_ilog2_ulong[i][1]) + pr_warn("ilog2 wrong for ulong %x\n", + test_ilog2_ulong[i][0]); + } + + for (i = 0; i < ARRAY_SIZE(order_base2); i++) { + if (order_base_2(order_base2[i][0]) != order_base2[i][1]) + pr_warn("order_base_2 wrong for %x\n", + order_base2[i][0]); + } + +//#ifdef CONFIG_64BIT + for (i = 0; i < ARRAY_SIZE(order_base2_long); i++) { + if (order_base_2(order_base2_long[i][0]) != + order_base2_long[i][1]) + pr_warn("order_base_2 wrong for %lx\n", + order_base2_long[i][0]); + } +//#endif + + for (i = 0; i < ARRAY_SIZE(test_bits_per); i++) { + if (bits_per(test_bits_per[i][0]) != test_bits_per[i][1]) + pr_warn("bits_per wrong for %x\n", + test_bits_per[i][0]); + } + + pr_info("Completed log2 tests\n"); + + return 0; +} + +static void __exit test_log2_fini(void) +{ +} + +module_init(test_log2_start); +module_exit(test_log2_fini); + +MODULE_AUTHOR("Randy Dunlap "); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("log2 test module");