refcount.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This is for all the tests related to refcount bugs (e.g. overflow,
  4. * underflow, reaching zero untested, etc).
  5. */
  6. #include "lkdtm.h"
  7. #include <linux/refcount.h>
  8. #ifdef CONFIG_REFCOUNT_FULL
  9. #define REFCOUNT_MAX (UINT_MAX - 1)
  10. #define REFCOUNT_SATURATED UINT_MAX
  11. #else
  12. #define REFCOUNT_MAX INT_MAX
  13. #define REFCOUNT_SATURATED (INT_MIN / 2)
  14. #endif
  15. static void overflow_check(refcount_t *ref)
  16. {
  17. switch (refcount_read(ref)) {
  18. case REFCOUNT_SATURATED:
  19. pr_info("Overflow detected: saturated\n");
  20. break;
  21. case REFCOUNT_MAX:
  22. pr_warn("Overflow detected: unsafely reset to max\n");
  23. break;
  24. default:
  25. pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref));
  26. }
  27. }
  28. /*
  29. * A refcount_inc() above the maximum value of the refcount implementation,
  30. * should at least saturate, and at most also WARN.
  31. */
  32. void lkdtm_REFCOUNT_INC_OVERFLOW(void)
  33. {
  34. refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
  35. pr_info("attempting good refcount_inc() without overflow\n");
  36. refcount_dec(&over);
  37. refcount_inc(&over);
  38. pr_info("attempting bad refcount_inc() overflow\n");
  39. refcount_inc(&over);
  40. refcount_inc(&over);
  41. overflow_check(&over);
  42. }
  43. /* refcount_add() should behave just like refcount_inc() above. */
  44. void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
  45. {
  46. refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
  47. pr_info("attempting good refcount_add() without overflow\n");
  48. refcount_dec(&over);
  49. refcount_dec(&over);
  50. refcount_dec(&over);
  51. refcount_dec(&over);
  52. refcount_add(4, &over);
  53. pr_info("attempting bad refcount_add() overflow\n");
  54. refcount_add(4, &over);
  55. overflow_check(&over);
  56. }
  57. /* refcount_inc_not_zero() should behave just like refcount_inc() above. */
  58. void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
  59. {
  60. refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
  61. pr_info("attempting bad refcount_inc_not_zero() overflow\n");
  62. if (!refcount_inc_not_zero(&over))
  63. pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
  64. overflow_check(&over);
  65. }
  66. /* refcount_add_not_zero() should behave just like refcount_inc() above. */
  67. void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
  68. {
  69. refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
  70. pr_info("attempting bad refcount_add_not_zero() overflow\n");
  71. if (!refcount_add_not_zero(6, &over))
  72. pr_warn("Weird: refcount_add_not_zero() reported zero\n");
  73. overflow_check(&over);
  74. }
  75. static void check_zero(refcount_t *ref)
  76. {
  77. switch (refcount_read(ref)) {
  78. case REFCOUNT_SATURATED:
  79. pr_info("Zero detected: saturated\n");
  80. break;
  81. case REFCOUNT_MAX:
  82. pr_warn("Zero detected: unsafely reset to max\n");
  83. break;
  84. case 0:
  85. pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n");
  86. break;
  87. default:
  88. pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
  89. }
  90. }
  91. /*
  92. * A refcount_dec(), as opposed to a refcount_dec_and_test(), when it hits
  93. * zero it should either saturate (when inc-from-zero isn't protected)
  94. * or stay at zero (when inc-from-zero is protected) and should WARN for both.
  95. */
  96. void lkdtm_REFCOUNT_DEC_ZERO(void)
  97. {
  98. refcount_t zero = REFCOUNT_INIT(2);
  99. pr_info("attempting good refcount_dec()\n");
  100. refcount_dec(&zero);
  101. pr_info("attempting bad refcount_dec() to zero\n");
  102. refcount_dec(&zero);
  103. check_zero(&zero);
  104. }
  105. static void check_negative(refcount_t *ref, int start)
  106. {
  107. /*
  108. * CONFIG_REFCOUNT_FULL refuses to move a refcount at all on an
  109. * over-sub, so we have to track our starting position instead of
  110. * looking only at zero-pinning.
  111. */
  112. if (refcount_read(ref) == start) {
  113. pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n",
  114. start);
  115. return;
  116. }
  117. switch (refcount_read(ref)) {
  118. case REFCOUNT_SATURATED:
  119. pr_info("Negative detected: saturated\n");
  120. break;
  121. case REFCOUNT_MAX:
  122. pr_warn("Negative detected: unsafely reset to max\n");
  123. break;
  124. default:
  125. pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
  126. }
  127. }
  128. /* A refcount_dec() going negative should saturate and may WARN. */
  129. void lkdtm_REFCOUNT_DEC_NEGATIVE(void)
  130. {
  131. refcount_t neg = REFCOUNT_INIT(0);
  132. pr_info("attempting bad refcount_dec() below zero\n");
  133. refcount_dec(&neg);
  134. check_negative(&neg, 0);
  135. }
  136. /*
  137. * A refcount_dec_and_test() should act like refcount_dec() above when
  138. * going negative.
  139. */
  140. void lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void)
  141. {
  142. refcount_t neg = REFCOUNT_INIT(0);
  143. pr_info("attempting bad refcount_dec_and_test() below zero\n");
  144. if (refcount_dec_and_test(&neg))
  145. pr_warn("Weird: refcount_dec_and_test() reported zero\n");
  146. check_negative(&neg, 0);
  147. }
  148. /*
  149. * A refcount_sub_and_test() should act like refcount_dec_and_test()
  150. * above when going negative.
  151. */
  152. void lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void)
  153. {
  154. refcount_t neg = REFCOUNT_INIT(3);
  155. pr_info("attempting bad refcount_sub_and_test() below zero\n");
  156. if (refcount_sub_and_test(5, &neg))
  157. pr_warn("Weird: refcount_sub_and_test() reported zero\n");
  158. check_negative(&neg, 3);
  159. }
  160. static void check_from_zero(refcount_t *ref)
  161. {
  162. switch (refcount_read(ref)) {
  163. case 0:
  164. pr_info("Zero detected: stayed at zero\n");
  165. break;
  166. case REFCOUNT_SATURATED:
  167. pr_info("Zero detected: saturated\n");
  168. break;
  169. case REFCOUNT_MAX:
  170. pr_warn("Zero detected: unsafely reset to max\n");
  171. break;
  172. default:
  173. pr_info("Fail: zero not detected, incremented to %d\n",
  174. refcount_read(ref));
  175. }
  176. }
  177. /*
  178. * A refcount_inc() from zero should pin to zero or saturate and may WARN.
  179. * Only CONFIG_REFCOUNT_FULL provides this protection currently.
  180. */
  181. void lkdtm_REFCOUNT_INC_ZERO(void)
  182. {
  183. refcount_t zero = REFCOUNT_INIT(0);
  184. pr_info("attempting safe refcount_inc_not_zero() from zero\n");
  185. if (!refcount_inc_not_zero(&zero)) {
  186. pr_info("Good: zero detected\n");
  187. if (refcount_read(&zero) == 0)
  188. pr_info("Correctly stayed at zero\n");
  189. else
  190. pr_err("Fail: refcount went past zero!\n");
  191. } else {
  192. pr_err("Fail: Zero not detected!?\n");
  193. }
  194. pr_info("attempting bad refcount_inc() from zero\n");
  195. refcount_inc(&zero);
  196. check_from_zero(&zero);
  197. }
  198. /*
  199. * A refcount_add() should act like refcount_inc() above when starting
  200. * at zero.
  201. */
  202. void lkdtm_REFCOUNT_ADD_ZERO(void)
  203. {
  204. refcount_t zero = REFCOUNT_INIT(0);
  205. pr_info("attempting safe refcount_add_not_zero() from zero\n");
  206. if (!refcount_add_not_zero(3, &zero)) {
  207. pr_info("Good: zero detected\n");
  208. if (refcount_read(&zero) == 0)
  209. pr_info("Correctly stayed at zero\n");
  210. else
  211. pr_err("Fail: refcount went past zero\n");
  212. } else {
  213. pr_err("Fail: Zero not detected!?\n");
  214. }
  215. pr_info("attempting bad refcount_add() from zero\n");
  216. refcount_add(3, &zero);
  217. check_from_zero(&zero);
  218. }
  219. static void check_saturated(refcount_t *ref)
  220. {
  221. switch (refcount_read(ref)) {
  222. case REFCOUNT_SATURATED:
  223. pr_info("Saturation detected: still saturated\n");
  224. break;
  225. case REFCOUNT_MAX:
  226. pr_warn("Saturation detected: unsafely reset to max\n");
  227. break;
  228. default:
  229. pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
  230. }
  231. }
  232. /*
  233. * A refcount_inc() from a saturated value should at most warn about
  234. * being saturated already.
  235. */
  236. void lkdtm_REFCOUNT_INC_SATURATED(void)
  237. {
  238. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  239. pr_info("attempting bad refcount_inc() from saturated\n");
  240. refcount_inc(&sat);
  241. check_saturated(&sat);
  242. }
  243. /* Should act like refcount_inc() above from saturated. */
  244. void lkdtm_REFCOUNT_DEC_SATURATED(void)
  245. {
  246. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  247. pr_info("attempting bad refcount_dec() from saturated\n");
  248. refcount_dec(&sat);
  249. check_saturated(&sat);
  250. }
  251. /* Should act like refcount_inc() above from saturated. */
  252. void lkdtm_REFCOUNT_ADD_SATURATED(void)
  253. {
  254. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  255. pr_info("attempting bad refcount_dec() from saturated\n");
  256. refcount_add(8, &sat);
  257. check_saturated(&sat);
  258. }
  259. /* Should act like refcount_inc() above from saturated. */
  260. void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
  261. {
  262. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  263. pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
  264. if (!refcount_inc_not_zero(&sat))
  265. pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
  266. check_saturated(&sat);
  267. }
  268. /* Should act like refcount_inc() above from saturated. */
  269. void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
  270. {
  271. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  272. pr_info("attempting bad refcount_add_not_zero() from saturated\n");
  273. if (!refcount_add_not_zero(7, &sat))
  274. pr_warn("Weird: refcount_add_not_zero() reported zero\n");
  275. check_saturated(&sat);
  276. }
  277. /* Should act like refcount_inc() above from saturated. */
  278. void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
  279. {
  280. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  281. pr_info("attempting bad refcount_dec_and_test() from saturated\n");
  282. if (refcount_dec_and_test(&sat))
  283. pr_warn("Weird: refcount_dec_and_test() reported zero\n");
  284. check_saturated(&sat);
  285. }
  286. /* Should act like refcount_inc() above from saturated. */
  287. void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
  288. {
  289. refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
  290. pr_info("attempting bad refcount_sub_and_test() from saturated\n");
  291. if (refcount_sub_and_test(8, &sat))
  292. pr_warn("Weird: refcount_sub_and_test() reported zero\n");
  293. check_saturated(&sat);
  294. }
  295. /* Used to time the existing atomic_t when used for reference counting */
  296. void lkdtm_ATOMIC_TIMING(void)
  297. {
  298. unsigned int i;
  299. atomic_t count = ATOMIC_INIT(1);
  300. for (i = 0; i < INT_MAX - 1; i++)
  301. atomic_inc(&count);
  302. for (i = INT_MAX; i > 0; i--)
  303. if (atomic_dec_and_test(&count))
  304. break;
  305. if (i != 1)
  306. pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1);
  307. else
  308. pr_info("atomic timing: done\n");
  309. }
  310. /*
  311. * This can be compared to ATOMIC_TIMING when implementing fast refcount
  312. * protections. Looking at the number of CPU cycles tells the real story
  313. * about performance. For example:
  314. * cd /sys/kernel/debug/provoke-crash
  315. * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
  316. */
  317. void lkdtm_REFCOUNT_TIMING(void)
  318. {
  319. unsigned int i;
  320. refcount_t count = REFCOUNT_INIT(1);
  321. for (i = 0; i < INT_MAX - 1; i++)
  322. refcount_inc(&count);
  323. for (i = INT_MAX; i > 0; i--)
  324. if (refcount_dec_and_test(&count))
  325. break;
  326. if (i != 1)
  327. pr_err("refcount: out of sync up/down cycle: %u\n", i - 1);
  328. else
  329. pr_info("refcount timing: done\n");
  330. }