xref: /linux/lib/test_bitops.c (revision da5b2ad1c2f18834cb1ce429e2e5a5cf5cbdf21b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 Intel Corporation
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/cleanup.h>
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/printk.h>
12 #include <linux/slab.h>
13 
14 /* a tiny module only meant to test
15  *
16  *   set/clear_bit
17  *   get_count_order/long
18  */
19 
20 /* use an enum because that's the most common BITMAP usage */
21 enum bitops_fun {
22 	BITOPS_4 = 4,
23 	BITOPS_7 = 7,
24 	BITOPS_11 = 11,
25 	BITOPS_31 = 31,
26 	BITOPS_88 = 88,
27 	BITOPS_LAST = 255,
28 	BITOPS_LENGTH = 256
29 };
30 
31 static DECLARE_BITMAP(g_bitmap, BITOPS_LENGTH);
32 
33 static unsigned int order_comb[][2] = {
34 	{0x00000003,  2},
35 	{0x00000004,  2},
36 	{0x00001fff, 13},
37 	{0x00002000, 13},
38 	{0x50000000, 31},
39 	{0x80000000, 31},
40 	{0x80003000, 32},
41 };
42 
43 #ifdef CONFIG_64BIT
44 static unsigned long order_comb_long[][2] = {
45 	{0x0000000300000000, 34},
46 	{0x0000000400000000, 34},
47 	{0x00001fff00000000, 45},
48 	{0x0000200000000000, 45},
49 	{0x5000000000000000, 63},
50 	{0x8000000000000000, 63},
51 	{0x8000300000000000, 64},
52 };
53 #endif
54 
55 static int __init test_fns(void)
56 {
57 	static volatile __always_used unsigned long tmp __initdata;
58 	unsigned long *buf __free(kfree) = NULL;
59 	unsigned int i, n;
60 	ktime_t time;
61 
62 	buf = kmalloc_array(10000, sizeof(unsigned long), GFP_KERNEL);
63 	if (!buf)
64 		return -ENOMEM;
65 
66 	get_random_bytes(buf, 10000 * sizeof(unsigned long));
67 	time = ktime_get();
68 
69 	for (n = 0; n < BITS_PER_LONG; n++)
70 		for (i = 0; i < 10000; i++)
71 			tmp = fns(buf[i], n);
72 
73 	time = ktime_get() - time;
74 	pr_err("fns:  %18llu ns\n", time);
75 
76 	return 0;
77 }
78 
79 static int __init test_bitops_startup(void)
80 {
81 	int i, bit_set;
82 
83 	pr_info("Starting bitops test\n");
84 	set_bit(BITOPS_4, g_bitmap);
85 	set_bit(BITOPS_7, g_bitmap);
86 	set_bit(BITOPS_11, g_bitmap);
87 	set_bit(BITOPS_31, g_bitmap);
88 	set_bit(BITOPS_88, g_bitmap);
89 
90 	for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
91 		if (order_comb[i][1] != get_count_order(order_comb[i][0]))
92 			pr_warn("get_count_order wrong for %x\n",
93 				       order_comb[i][0]);
94 	}
95 
96 	for (i = 0; i < ARRAY_SIZE(order_comb); i++) {
97 		if (order_comb[i][1] != get_count_order_long(order_comb[i][0]))
98 			pr_warn("get_count_order_long wrong for %x\n",
99 				       order_comb[i][0]);
100 	}
101 
102 #ifdef CONFIG_64BIT
103 	for (i = 0; i < ARRAY_SIZE(order_comb_long); i++) {
104 		if (order_comb_long[i][1] !=
105 			       get_count_order_long(order_comb_long[i][0]))
106 			pr_warn("get_count_order_long wrong for %lx\n",
107 				       order_comb_long[i][0]);
108 	}
109 #endif
110 
111 	barrier();
112 
113 	clear_bit(BITOPS_4, g_bitmap);
114 	clear_bit(BITOPS_7, g_bitmap);
115 	clear_bit(BITOPS_11, g_bitmap);
116 	clear_bit(BITOPS_31, g_bitmap);
117 	clear_bit(BITOPS_88, g_bitmap);
118 
119 	bit_set = find_first_bit(g_bitmap, BITOPS_LAST);
120 	if (bit_set != BITOPS_LAST)
121 		pr_err("ERROR: FOUND SET BIT %d\n", bit_set);
122 
123 	test_fns();
124 
125 	pr_info("Completed bitops test\n");
126 
127 	return 0;
128 }
129 
130 static void __exit test_bitops_unstartup(void)
131 {
132 }
133 
134 module_init(test_bitops_startup);
135 module_exit(test_bitops_unstartup);
136 
137 MODULE_AUTHOR("Jesse Brandeburg <jesse.brandeburg@intel.com>, Wei Yang <richard.weiyang@gmail.com>");
138 MODULE_LICENSE("GPL");
139 MODULE_DESCRIPTION("Bit testing module");
140