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 ({bool func(void) { return branch(key); } func; })
52 static void invert_key(struct static_key
*key
)
54 if (static_key_enabled(key
))
55 static_key_disable(key
);
57 static_key_enable(key
);
60 static void invert_keys(struct test_key
*keys
, int size
)
62 struct static_key
*previous
= NULL
;
65 for (i
= 0; i
< size
; i
++) {
66 if (previous
!= keys
[i
].key
) {
67 invert_key(keys
[i
].key
);
68 previous
= keys
[i
].key
;
73 static int verify_keys(struct test_key
*keys
, int size
, bool invert
)
78 for (i
= 0; i
< size
; i
++) {
79 ret
= static_key_enabled(keys
[i
].key
);
80 init
= keys
[i
].init_state
;
81 if (ret
!= (invert
? !init
: init
))
83 ret
= keys
[i
].test_key();
84 if (static_key_enabled(keys
[i
].key
)) {
95 static int __init
test_static_key_init(void)
100 struct test_key static_key_tests
[] = {
101 /* internal keys - old keys */
104 .key
= &old_true_key
,
105 .test_key
= test_key_func(&old_true_key
, static_key_true
),
109 .key
= &old_false_key
,
110 .test_key
= test_key_func(&old_false_key
, static_key_false
),
112 /* internal keys - new keys */
115 .key
= &true_key
.key
,
116 .test_key
= test_key_func(&true_key
, static_branch_likely
),
120 .key
= &true_key
.key
,
121 .test_key
= test_key_func(&true_key
, static_branch_unlikely
),
125 .key
= &false_key
.key
,
126 .test_key
= test_key_func(&false_key
, static_branch_likely
),
130 .key
= &false_key
.key
,
131 .test_key
= test_key_func(&false_key
, static_branch_unlikely
),
133 /* external keys - old keys */
136 .key
= &base_old_true_key
,
137 .test_key
= test_key_func(&base_old_true_key
, static_key_true
),
141 .key
= &base_inv_old_true_key
,
142 .test_key
= test_key_func(&base_inv_old_true_key
, static_key_true
),
146 .key
= &base_old_false_key
,
147 .test_key
= test_key_func(&base_old_false_key
, static_key_false
),
151 .key
= &base_inv_old_false_key
,
152 .test_key
= test_key_func(&base_inv_old_false_key
, static_key_false
),
154 /* external keys - new keys */
157 .key
= &base_true_key
.key
,
158 .test_key
= test_key_func(&base_true_key
, static_branch_likely
),
162 .key
= &base_true_key
.key
,
163 .test_key
= test_key_func(&base_true_key
, static_branch_unlikely
),
167 .key
= &base_inv_true_key
.key
,
168 .test_key
= test_key_func(&base_inv_true_key
, static_branch_likely
),
172 .key
= &base_inv_true_key
.key
,
173 .test_key
= test_key_func(&base_inv_true_key
, static_branch_unlikely
),
177 .key
= &base_false_key
.key
,
178 .test_key
= test_key_func(&base_false_key
, static_branch_likely
),
182 .key
= &base_false_key
.key
,
183 .test_key
= test_key_func(&base_false_key
, static_branch_unlikely
),
187 .key
= &base_inv_false_key
.key
,
188 .test_key
= test_key_func(&base_inv_false_key
, static_branch_likely
),
192 .key
= &base_inv_false_key
.key
,
193 .test_key
= test_key_func(&base_inv_false_key
, static_branch_unlikely
),
197 size
= ARRAY_SIZE(static_key_tests
);
199 ret
= verify_keys(static_key_tests
, size
, false);
203 invert_keys(static_key_tests
, size
);
204 ret
= verify_keys(static_key_tests
, size
, true);
208 invert_keys(static_key_tests
, size
);
209 ret
= verify_keys(static_key_tests
, size
, false);
217 static void __exit
test_static_key_exit(void)
221 module_init(test_static_key_init
);
222 module_exit(test_static_key_exit
);
224 MODULE_AUTHOR("Jason Baron <jbaron@akamai.com>");
225 MODULE_LICENSE("GPL");