Linux 5.1.15
[linux/fpc-iii.git] / drivers / misc / lkdtm / refcount.c
blob0a146b32da132f9f38af747372214604b08836f9
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>
9 #ifdef CONFIG_REFCOUNT_FULL
10 #define REFCOUNT_MAX (UINT_MAX - 1)
11 #define REFCOUNT_SATURATED UINT_MAX
12 #else
13 #define REFCOUNT_MAX INT_MAX
14 #define REFCOUNT_SATURATED (INT_MIN / 2)
15 #endif
17 static void overflow_check(refcount_t *ref)
19 switch (refcount_read(ref)) {
20 case REFCOUNT_SATURATED:
21 pr_info("Overflow detected: saturated\n");
22 break;
23 case REFCOUNT_MAX:
24 pr_warn("Overflow detected: unsafely reset to max\n");
25 break;
26 default:
27 pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref));
32 * A refcount_inc() above the maximum value of the refcount implementation,
33 * should at least saturate, and at most also WARN.
35 void lkdtm_REFCOUNT_INC_OVERFLOW(void)
37 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
39 pr_info("attempting good refcount_inc() without overflow\n");
40 refcount_dec(&over);
41 refcount_inc(&over);
43 pr_info("attempting bad refcount_inc() overflow\n");
44 refcount_inc(&over);
45 refcount_inc(&over);
47 overflow_check(&over);
50 /* refcount_add() should behave just like refcount_inc() above. */
51 void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
53 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
55 pr_info("attempting good refcount_add() without overflow\n");
56 refcount_dec(&over);
57 refcount_dec(&over);
58 refcount_dec(&over);
59 refcount_dec(&over);
60 refcount_add(4, &over);
62 pr_info("attempting bad refcount_add() overflow\n");
63 refcount_add(4, &over);
65 overflow_check(&over);
68 /* refcount_inc_not_zero() should behave just like refcount_inc() above. */
69 void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
71 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
73 pr_info("attempting bad refcount_inc_not_zero() overflow\n");
74 if (!refcount_inc_not_zero(&over))
75 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
77 overflow_check(&over);
80 /* refcount_add_not_zero() should behave just like refcount_inc() above. */
81 void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
83 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
85 pr_info("attempting bad refcount_add_not_zero() overflow\n");
86 if (!refcount_add_not_zero(6, &over))
87 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
89 overflow_check(&over);
92 static void check_zero(refcount_t *ref)
94 switch (refcount_read(ref)) {
95 case REFCOUNT_SATURATED:
96 pr_info("Zero detected: saturated\n");
97 break;
98 case REFCOUNT_MAX:
99 pr_warn("Zero detected: unsafely reset to max\n");
100 break;
101 case 0:
102 pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n");
103 break;
104 default:
105 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
110 * A refcount_dec(), as opposed to a refcount_dec_and_test(), when it hits
111 * zero it should either saturate (when inc-from-zero isn't protected)
112 * or stay at zero (when inc-from-zero is protected) and should WARN for both.
114 void lkdtm_REFCOUNT_DEC_ZERO(void)
116 refcount_t zero = REFCOUNT_INIT(2);
118 pr_info("attempting good refcount_dec()\n");
119 refcount_dec(&zero);
121 pr_info("attempting bad refcount_dec() to zero\n");
122 refcount_dec(&zero);
124 check_zero(&zero);
127 static void check_negative(refcount_t *ref, int start)
130 * CONFIG_REFCOUNT_FULL refuses to move a refcount at all on an
131 * over-sub, so we have to track our starting position instead of
132 * looking only at zero-pinning.
134 if (refcount_read(ref) == start) {
135 pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n",
136 start);
137 return;
140 switch (refcount_read(ref)) {
141 case REFCOUNT_SATURATED:
142 pr_info("Negative detected: saturated\n");
143 break;
144 case REFCOUNT_MAX:
145 pr_warn("Negative detected: unsafely reset to max\n");
146 break;
147 default:
148 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
152 /* A refcount_dec() going negative should saturate and may WARN. */
153 void lkdtm_REFCOUNT_DEC_NEGATIVE(void)
155 refcount_t neg = REFCOUNT_INIT(0);
157 pr_info("attempting bad refcount_dec() below zero\n");
158 refcount_dec(&neg);
160 check_negative(&neg, 0);
164 * A refcount_dec_and_test() should act like refcount_dec() above when
165 * going negative.
167 void lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void)
169 refcount_t neg = REFCOUNT_INIT(0);
171 pr_info("attempting bad refcount_dec_and_test() below zero\n");
172 if (refcount_dec_and_test(&neg))
173 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
175 check_negative(&neg, 0);
179 * A refcount_sub_and_test() should act like refcount_dec_and_test()
180 * above when going negative.
182 void lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void)
184 refcount_t neg = REFCOUNT_INIT(3);
186 pr_info("attempting bad refcount_sub_and_test() below zero\n");
187 if (refcount_sub_and_test(5, &neg))
188 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
190 check_negative(&neg, 3);
193 static void check_from_zero(refcount_t *ref)
195 switch (refcount_read(ref)) {
196 case 0:
197 pr_info("Zero detected: stayed at zero\n");
198 break;
199 case REFCOUNT_SATURATED:
200 pr_info("Zero detected: saturated\n");
201 break;
202 case REFCOUNT_MAX:
203 pr_warn("Zero detected: unsafely reset to max\n");
204 break;
205 default:
206 pr_info("Fail: zero not detected, incremented to %d\n",
207 refcount_read(ref));
212 * A refcount_inc() from zero should pin to zero or saturate and may WARN.
213 * Only CONFIG_REFCOUNT_FULL provides this protection currently.
215 void lkdtm_REFCOUNT_INC_ZERO(void)
217 refcount_t zero = REFCOUNT_INIT(0);
219 pr_info("attempting safe refcount_inc_not_zero() from zero\n");
220 if (!refcount_inc_not_zero(&zero)) {
221 pr_info("Good: zero detected\n");
222 if (refcount_read(&zero) == 0)
223 pr_info("Correctly stayed at zero\n");
224 else
225 pr_err("Fail: refcount went past zero!\n");
226 } else {
227 pr_err("Fail: Zero not detected!?\n");
230 pr_info("attempting bad refcount_inc() from zero\n");
231 refcount_inc(&zero);
233 check_from_zero(&zero);
237 * A refcount_add() should act like refcount_inc() above when starting
238 * at zero.
240 void lkdtm_REFCOUNT_ADD_ZERO(void)
242 refcount_t zero = REFCOUNT_INIT(0);
244 pr_info("attempting safe refcount_add_not_zero() from zero\n");
245 if (!refcount_add_not_zero(3, &zero)) {
246 pr_info("Good: zero detected\n");
247 if (refcount_read(&zero) == 0)
248 pr_info("Correctly stayed at zero\n");
249 else
250 pr_err("Fail: refcount went past zero\n");
251 } else {
252 pr_err("Fail: Zero not detected!?\n");
255 pr_info("attempting bad refcount_add() from zero\n");
256 refcount_add(3, &zero);
258 check_from_zero(&zero);
261 static void check_saturated(refcount_t *ref)
263 switch (refcount_read(ref)) {
264 case REFCOUNT_SATURATED:
265 pr_info("Saturation detected: still saturated\n");
266 break;
267 case REFCOUNT_MAX:
268 pr_warn("Saturation detected: unsafely reset to max\n");
269 break;
270 default:
271 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
276 * A refcount_inc() from a saturated value should at most warn about
277 * being saturated already.
279 void lkdtm_REFCOUNT_INC_SATURATED(void)
281 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
283 pr_info("attempting bad refcount_inc() from saturated\n");
284 refcount_inc(&sat);
286 check_saturated(&sat);
289 /* Should act like refcount_inc() above from saturated. */
290 void lkdtm_REFCOUNT_DEC_SATURATED(void)
292 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
294 pr_info("attempting bad refcount_dec() from saturated\n");
295 refcount_dec(&sat);
297 check_saturated(&sat);
300 /* Should act like refcount_inc() above from saturated. */
301 void lkdtm_REFCOUNT_ADD_SATURATED(void)
303 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
305 pr_info("attempting bad refcount_dec() from saturated\n");
306 refcount_add(8, &sat);
308 check_saturated(&sat);
311 /* Should act like refcount_inc() above from saturated. */
312 void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
314 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
316 pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
317 if (!refcount_inc_not_zero(&sat))
318 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
320 check_saturated(&sat);
323 /* Should act like refcount_inc() above from saturated. */
324 void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
326 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
328 pr_info("attempting bad refcount_add_not_zero() from saturated\n");
329 if (!refcount_add_not_zero(7, &sat))
330 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
332 check_saturated(&sat);
335 /* Should act like refcount_inc() above from saturated. */
336 void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
338 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
340 pr_info("attempting bad refcount_dec_and_test() from saturated\n");
341 if (refcount_dec_and_test(&sat))
342 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
344 check_saturated(&sat);
347 /* Should act like refcount_inc() above from saturated. */
348 void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
350 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
352 pr_info("attempting bad refcount_sub_and_test() from saturated\n");
353 if (refcount_sub_and_test(8, &sat))
354 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
356 check_saturated(&sat);
359 /* Used to time the existing atomic_t when used for reference counting */
360 void lkdtm_ATOMIC_TIMING(void)
362 unsigned int i;
363 atomic_t count = ATOMIC_INIT(1);
365 for (i = 0; i < INT_MAX - 1; i++)
366 atomic_inc(&count);
368 for (i = INT_MAX; i > 0; i--)
369 if (atomic_dec_and_test(&count))
370 break;
372 if (i != 1)
373 pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1);
374 else
375 pr_info("atomic timing: done\n");
379 * This can be compared to ATOMIC_TIMING when implementing fast refcount
380 * protections. Looking at the number of CPU cycles tells the real story
381 * about performance. For example:
382 * cd /sys/kernel/debug/provoke-crash
383 * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
385 void lkdtm_REFCOUNT_TIMING(void)
387 unsigned int i;
388 refcount_t count = REFCOUNT_INIT(1);
390 for (i = 0; i < INT_MAX - 1; i++)
391 refcount_inc(&count);
393 for (i = INT_MAX; i > 0; i--)
394 if (refcount_dec_and_test(&count))
395 break;
397 if (i != 1)
398 pr_err("refcount: out of sync up/down cycle: %u\n", i - 1);
399 else
400 pr_info("refcount timing: done\n");