2 * Kernel module for testing static keys.
4 * Copyright 2015 Akamai Technologies Inc. All Rights Reserved
7 * Jason Baron <jbaron@akamai.com>
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/jump_label.h>
23 struct static_key old_true_key
= STATIC_KEY_INIT_TRUE
;
24 struct static_key old_false_key
= STATIC_KEY_INIT_FALSE
;
27 DEFINE_STATIC_KEY_TRUE(true_key
);
28 DEFINE_STATIC_KEY_FALSE(false_key
);
31 extern struct static_key base_old_true_key
;
32 extern struct static_key base_inv_old_true_key
;
33 extern struct static_key base_old_false_key
;
34 extern struct static_key base_inv_old_false_key
;
37 extern struct static_key_true base_true_key
;
38 extern struct static_key_true base_inv_true_key
;
39 extern struct static_key_false base_false_key
;
40 extern struct static_key_false base_inv_false_key
;
45 struct static_key
*key
;
46 bool (*test_key
)(void);
49 #define test_key_func(key, branch) \
50 static bool key ## _ ## branch(void) \
52 return branch(&key); \
55 static void invert_key(struct static_key
*key
)
57 if (static_key_enabled(key
))
58 static_key_disable(key
);
60 static_key_enable(key
);
63 static void invert_keys(struct test_key
*keys
, int size
)
65 struct static_key
*previous
= NULL
;
68 for (i
= 0; i
< size
; i
++) {
69 if (previous
!= keys
[i
].key
) {
70 invert_key(keys
[i
].key
);
71 previous
= keys
[i
].key
;
76 static int verify_keys(struct test_key
*keys
, int size
, bool invert
)
81 for (i
= 0; i
< size
; i
++) {
82 ret
= static_key_enabled(keys
[i
].key
);
83 init
= keys
[i
].init_state
;
84 if (ret
!= (invert
? !init
: init
))
86 ret
= keys
[i
].test_key();
87 if (static_key_enabled(keys
[i
].key
)) {
98 test_key_func(old_true_key
, static_key_true
)
99 test_key_func(old_false_key
, static_key_false
)
100 test_key_func(true_key
, static_branch_likely
)
101 test_key_func(true_key
, static_branch_unlikely
)
102 test_key_func(false_key
, static_branch_likely
)
103 test_key_func(false_key
, static_branch_unlikely
)
104 test_key_func(base_old_true_key
, static_key_true
)
105 test_key_func(base_inv_old_true_key
, static_key_true
)
106 test_key_func(base_old_false_key
, static_key_false
)
107 test_key_func(base_inv_old_false_key
, static_key_false
)
108 test_key_func(base_true_key
, static_branch_likely
)
109 test_key_func(base_true_key
, static_branch_unlikely
)
110 test_key_func(base_inv_true_key
, static_branch_likely
)
111 test_key_func(base_inv_true_key
, static_branch_unlikely
)
112 test_key_func(base_false_key
, static_branch_likely
)
113 test_key_func(base_false_key
, static_branch_unlikely
)
114 test_key_func(base_inv_false_key
, static_branch_likely
)
115 test_key_func(base_inv_false_key
, static_branch_unlikely
)
117 static int __init
test_static_key_init(void)
122 struct test_key static_key_tests
[] = {
123 /* internal keys - old keys */
126 .key
= &old_true_key
,
127 .test_key
= &old_true_key_static_key_true
,
131 .key
= &old_false_key
,
132 .test_key
= &old_false_key_static_key_false
,
134 /* internal keys - new keys */
137 .key
= &true_key
.key
,
138 .test_key
= &true_key_static_branch_likely
,
142 .key
= &true_key
.key
,
143 .test_key
= &true_key_static_branch_unlikely
,
147 .key
= &false_key
.key
,
148 .test_key
= &false_key_static_branch_likely
,
152 .key
= &false_key
.key
,
153 .test_key
= &false_key_static_branch_unlikely
,
155 /* external keys - old keys */
158 .key
= &base_old_true_key
,
159 .test_key
= &base_old_true_key_static_key_true
,
163 .key
= &base_inv_old_true_key
,
164 .test_key
= &base_inv_old_true_key_static_key_true
,
168 .key
= &base_old_false_key
,
169 .test_key
= &base_old_false_key_static_key_false
,
173 .key
= &base_inv_old_false_key
,
174 .test_key
= &base_inv_old_false_key_static_key_false
,
176 /* external keys - new keys */
179 .key
= &base_true_key
.key
,
180 .test_key
= &base_true_key_static_branch_likely
,
184 .key
= &base_true_key
.key
,
185 .test_key
= &base_true_key_static_branch_unlikely
,
189 .key
= &base_inv_true_key
.key
,
190 .test_key
= &base_inv_true_key_static_branch_likely
,
194 .key
= &base_inv_true_key
.key
,
195 .test_key
= &base_inv_true_key_static_branch_unlikely
,
199 .key
= &base_false_key
.key
,
200 .test_key
= &base_false_key_static_branch_likely
,
204 .key
= &base_false_key
.key
,
205 .test_key
= &base_false_key_static_branch_unlikely
,
209 .key
= &base_inv_false_key
.key
,
210 .test_key
= &base_inv_false_key_static_branch_likely
,
214 .key
= &base_inv_false_key
.key
,
215 .test_key
= &base_inv_false_key_static_branch_unlikely
,
219 size
= ARRAY_SIZE(static_key_tests
);
221 ret
= verify_keys(static_key_tests
, size
, false);
225 invert_keys(static_key_tests
, size
);
226 ret
= verify_keys(static_key_tests
, size
, true);
230 invert_keys(static_key_tests
, size
);
231 ret
= verify_keys(static_key_tests
, size
, false);
239 static void __exit
test_static_key_exit(void)
243 module_init(test_static_key_init
);
244 module_exit(test_static_key_exit
);
246 MODULE_AUTHOR("Jason Baron <jbaron@akamai.com>");
247 MODULE_LICENSE("GPL");