test_static_keys.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Kernel module for testing static keys.
  3. *
  4. * Copyright 2015 Akamai Technologies Inc. All Rights Reserved
  5. *
  6. * Authors:
  7. * Jason Baron <jbaron@akamai.com>
  8. *
  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.
  12. *
  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.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/jump_label.h>
  20. /* old keys */
  21. struct static_key old_true_key = STATIC_KEY_INIT_TRUE;
  22. struct static_key old_false_key = STATIC_KEY_INIT_FALSE;
  23. /* new api */
  24. DEFINE_STATIC_KEY_TRUE(true_key);
  25. DEFINE_STATIC_KEY_FALSE(false_key);
  26. /* external */
  27. extern struct static_key base_old_true_key;
  28. extern struct static_key base_inv_old_true_key;
  29. extern struct static_key base_old_false_key;
  30. extern struct static_key base_inv_old_false_key;
  31. /* new api */
  32. extern struct static_key_true base_true_key;
  33. extern struct static_key_true base_inv_true_key;
  34. extern struct static_key_false base_false_key;
  35. extern struct static_key_false base_inv_false_key;
  36. struct test_key {
  37. bool init_state;
  38. struct static_key *key;
  39. bool (*test_key)(void);
  40. };
  41. #define test_key_func(key, branch) \
  42. static bool key ## _ ## branch(void) \
  43. { \
  44. return branch(&key); \
  45. }
  46. static void invert_key(struct static_key *key)
  47. {
  48. if (static_key_enabled(key))
  49. static_key_disable(key);
  50. else
  51. static_key_enable(key);
  52. }
  53. static void invert_keys(struct test_key *keys, int size)
  54. {
  55. struct static_key *previous = NULL;
  56. int i;
  57. for (i = 0; i < size; i++) {
  58. if (previous != keys[i].key) {
  59. invert_key(keys[i].key);
  60. previous = keys[i].key;
  61. }
  62. }
  63. }
  64. static int verify_keys(struct test_key *keys, int size, bool invert)
  65. {
  66. int i;
  67. bool ret, init;
  68. for (i = 0; i < size; i++) {
  69. ret = static_key_enabled(keys[i].key);
  70. init = keys[i].init_state;
  71. if (ret != (invert ? !init : init))
  72. return -EINVAL;
  73. ret = keys[i].test_key();
  74. if (static_key_enabled(keys[i].key)) {
  75. if (!ret)
  76. return -EINVAL;
  77. } else {
  78. if (ret)
  79. return -EINVAL;
  80. }
  81. }
  82. return 0;
  83. }
  84. test_key_func(old_true_key, static_key_true)
  85. test_key_func(old_false_key, static_key_false)
  86. test_key_func(true_key, static_branch_likely)
  87. test_key_func(true_key, static_branch_unlikely)
  88. test_key_func(false_key, static_branch_likely)
  89. test_key_func(false_key, static_branch_unlikely)
  90. test_key_func(base_old_true_key, static_key_true)
  91. test_key_func(base_inv_old_true_key, static_key_true)
  92. test_key_func(base_old_false_key, static_key_false)
  93. test_key_func(base_inv_old_false_key, static_key_false)
  94. test_key_func(base_true_key, static_branch_likely)
  95. test_key_func(base_true_key, static_branch_unlikely)
  96. test_key_func(base_inv_true_key, static_branch_likely)
  97. test_key_func(base_inv_true_key, static_branch_unlikely)
  98. test_key_func(base_false_key, static_branch_likely)
  99. test_key_func(base_false_key, static_branch_unlikely)
  100. test_key_func(base_inv_false_key, static_branch_likely)
  101. test_key_func(base_inv_false_key, static_branch_unlikely)
  102. static int __init test_static_key_init(void)
  103. {
  104. int ret;
  105. int size;
  106. struct test_key static_key_tests[] = {
  107. /* internal keys - old keys */
  108. {
  109. .init_state = true,
  110. .key = &old_true_key,
  111. .test_key = &old_true_key_static_key_true,
  112. },
  113. {
  114. .init_state = false,
  115. .key = &old_false_key,
  116. .test_key = &old_false_key_static_key_false,
  117. },
  118. /* internal keys - new keys */
  119. {
  120. .init_state = true,
  121. .key = &true_key.key,
  122. .test_key = &true_key_static_branch_likely,
  123. },
  124. {
  125. .init_state = true,
  126. .key = &true_key.key,
  127. .test_key = &true_key_static_branch_unlikely,
  128. },
  129. {
  130. .init_state = false,
  131. .key = &false_key.key,
  132. .test_key = &false_key_static_branch_likely,
  133. },
  134. {
  135. .init_state = false,
  136. .key = &false_key.key,
  137. .test_key = &false_key_static_branch_unlikely,
  138. },
  139. /* external keys - old keys */
  140. {
  141. .init_state = true,
  142. .key = &base_old_true_key,
  143. .test_key = &base_old_true_key_static_key_true,
  144. },
  145. {
  146. .init_state = false,
  147. .key = &base_inv_old_true_key,
  148. .test_key = &base_inv_old_true_key_static_key_true,
  149. },
  150. {
  151. .init_state = false,
  152. .key = &base_old_false_key,
  153. .test_key = &base_old_false_key_static_key_false,
  154. },
  155. {
  156. .init_state = true,
  157. .key = &base_inv_old_false_key,
  158. .test_key = &base_inv_old_false_key_static_key_false,
  159. },
  160. /* external keys - new keys */
  161. {
  162. .init_state = true,
  163. .key = &base_true_key.key,
  164. .test_key = &base_true_key_static_branch_likely,
  165. },
  166. {
  167. .init_state = true,
  168. .key = &base_true_key.key,
  169. .test_key = &base_true_key_static_branch_unlikely,
  170. },
  171. {
  172. .init_state = false,
  173. .key = &base_inv_true_key.key,
  174. .test_key = &base_inv_true_key_static_branch_likely,
  175. },
  176. {
  177. .init_state = false,
  178. .key = &base_inv_true_key.key,
  179. .test_key = &base_inv_true_key_static_branch_unlikely,
  180. },
  181. {
  182. .init_state = false,
  183. .key = &base_false_key.key,
  184. .test_key = &base_false_key_static_branch_likely,
  185. },
  186. {
  187. .init_state = false,
  188. .key = &base_false_key.key,
  189. .test_key = &base_false_key_static_branch_unlikely,
  190. },
  191. {
  192. .init_state = true,
  193. .key = &base_inv_false_key.key,
  194. .test_key = &base_inv_false_key_static_branch_likely,
  195. },
  196. {
  197. .init_state = true,
  198. .key = &base_inv_false_key.key,
  199. .test_key = &base_inv_false_key_static_branch_unlikely,
  200. },
  201. };
  202. size = ARRAY_SIZE(static_key_tests);
  203. ret = verify_keys(static_key_tests, size, false);
  204. if (ret)
  205. goto out;
  206. invert_keys(static_key_tests, size);
  207. ret = verify_keys(static_key_tests, size, true);
  208. if (ret)
  209. goto out;
  210. invert_keys(static_key_tests, size);
  211. ret = verify_keys(static_key_tests, size, false);
  212. if (ret)
  213. goto out;
  214. return 0;
  215. out:
  216. return ret;
  217. }
  218. static void __exit test_static_key_exit(void)
  219. {
  220. }
  221. module_init(test_static_key_init);
  222. module_exit(test_static_key_exit);
  223. MODULE_AUTHOR("Jason Baron <jbaron@akamai.com>");
  224. MODULE_LICENSE("GPL");