WIP FPC-III support
[linux/fpc-iii.git] / drivers / misc / lkdtm / refcount.c
blobde7c5ab528d9efa6ef1d633ef37de2fe61b44304
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 static void overflow_check(refcount_t *ref)
11 switch (refcount_read(ref)) {
12 case REFCOUNT_SATURATED:
13 pr_info("Overflow detected: saturated\n");
14 break;
15 case REFCOUNT_MAX:
16 pr_warn("Overflow detected: unsafely reset to max\n");
17 break;
18 default:
19 pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref));
24 * A refcount_inc() above the maximum value of the refcount implementation,
25 * should at least saturate, and at most also WARN.
27 void lkdtm_REFCOUNT_INC_OVERFLOW(void)
29 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1);
31 pr_info("attempting good refcount_inc() without overflow\n");
32 refcount_dec(&over);
33 refcount_inc(&over);
35 pr_info("attempting bad refcount_inc() overflow\n");
36 refcount_inc(&over);
37 refcount_inc(&over);
39 overflow_check(&over);
42 /* refcount_add() should behave just like refcount_inc() above. */
43 void lkdtm_REFCOUNT_ADD_OVERFLOW(void)
45 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);
54 pr_info("attempting bad refcount_add() overflow\n");
55 refcount_add(4, &over);
57 overflow_check(&over);
60 /* refcount_inc_not_zero() should behave just like refcount_inc() above. */
61 void lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void)
63 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
65 pr_info("attempting bad refcount_inc_not_zero() overflow\n");
66 if (!refcount_inc_not_zero(&over))
67 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
69 overflow_check(&over);
72 /* refcount_add_not_zero() should behave just like refcount_inc() above. */
73 void lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void)
75 refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX);
77 pr_info("attempting bad refcount_add_not_zero() overflow\n");
78 if (!refcount_add_not_zero(6, &over))
79 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
81 overflow_check(&over);
84 static void check_zero(refcount_t *ref)
86 switch (refcount_read(ref)) {
87 case REFCOUNT_SATURATED:
88 pr_info("Zero detected: saturated\n");
89 break;
90 case REFCOUNT_MAX:
91 pr_warn("Zero detected: unsafely reset to max\n");
92 break;
93 case 0:
94 pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n");
95 break;
96 default:
97 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
102 * A refcount_dec(), as opposed to a refcount_dec_and_test(), when it hits
103 * zero it should either saturate (when inc-from-zero isn't protected)
104 * or stay at zero (when inc-from-zero is protected) and should WARN for both.
106 void lkdtm_REFCOUNT_DEC_ZERO(void)
108 refcount_t zero = REFCOUNT_INIT(2);
110 pr_info("attempting good refcount_dec()\n");
111 refcount_dec(&zero);
113 pr_info("attempting bad refcount_dec() to zero\n");
114 refcount_dec(&zero);
116 check_zero(&zero);
119 static void check_negative(refcount_t *ref, int start)
122 * refcount_t refuses to move a refcount at all on an
123 * over-sub, so we have to track our starting position instead of
124 * looking only at zero-pinning.
126 if (refcount_read(ref) == start) {
127 pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n",
128 start);
129 return;
132 switch (refcount_read(ref)) {
133 case REFCOUNT_SATURATED:
134 pr_info("Negative detected: saturated\n");
135 break;
136 case REFCOUNT_MAX:
137 pr_warn("Negative detected: unsafely reset to max\n");
138 break;
139 default:
140 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
144 /* A refcount_dec() going negative should saturate and may WARN. */
145 void lkdtm_REFCOUNT_DEC_NEGATIVE(void)
147 refcount_t neg = REFCOUNT_INIT(0);
149 pr_info("attempting bad refcount_dec() below zero\n");
150 refcount_dec(&neg);
152 check_negative(&neg, 0);
156 * A refcount_dec_and_test() should act like refcount_dec() above when
157 * going negative.
159 void lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void)
161 refcount_t neg = REFCOUNT_INIT(0);
163 pr_info("attempting bad refcount_dec_and_test() below zero\n");
164 if (refcount_dec_and_test(&neg))
165 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
167 check_negative(&neg, 0);
171 * A refcount_sub_and_test() should act like refcount_dec_and_test()
172 * above when going negative.
174 void lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void)
176 refcount_t neg = REFCOUNT_INIT(3);
178 pr_info("attempting bad refcount_sub_and_test() below zero\n");
179 if (refcount_sub_and_test(5, &neg))
180 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
182 check_negative(&neg, 3);
185 static void check_from_zero(refcount_t *ref)
187 switch (refcount_read(ref)) {
188 case 0:
189 pr_info("Zero detected: stayed at zero\n");
190 break;
191 case REFCOUNT_SATURATED:
192 pr_info("Zero detected: saturated\n");
193 break;
194 case REFCOUNT_MAX:
195 pr_warn("Zero detected: unsafely reset to max\n");
196 break;
197 default:
198 pr_info("Fail: zero not detected, incremented to %d\n",
199 refcount_read(ref));
204 * A refcount_inc() from zero should pin to zero or saturate and may WARN.
206 void lkdtm_REFCOUNT_INC_ZERO(void)
208 refcount_t zero = REFCOUNT_INIT(0);
210 pr_info("attempting safe refcount_inc_not_zero() from zero\n");
211 if (!refcount_inc_not_zero(&zero)) {
212 pr_info("Good: zero detected\n");
213 if (refcount_read(&zero) == 0)
214 pr_info("Correctly stayed at zero\n");
215 else
216 pr_err("Fail: refcount went past zero!\n");
217 } else {
218 pr_err("Fail: Zero not detected!?\n");
221 pr_info("attempting bad refcount_inc() from zero\n");
222 refcount_inc(&zero);
224 check_from_zero(&zero);
228 * A refcount_add() should act like refcount_inc() above when starting
229 * at zero.
231 void lkdtm_REFCOUNT_ADD_ZERO(void)
233 refcount_t zero = REFCOUNT_INIT(0);
235 pr_info("attempting safe refcount_add_not_zero() from zero\n");
236 if (!refcount_add_not_zero(3, &zero)) {
237 pr_info("Good: zero detected\n");
238 if (refcount_read(&zero) == 0)
239 pr_info("Correctly stayed at zero\n");
240 else
241 pr_err("Fail: refcount went past zero\n");
242 } else {
243 pr_err("Fail: Zero not detected!?\n");
246 pr_info("attempting bad refcount_add() from zero\n");
247 refcount_add(3, &zero);
249 check_from_zero(&zero);
252 static void check_saturated(refcount_t *ref)
254 switch (refcount_read(ref)) {
255 case REFCOUNT_SATURATED:
256 pr_info("Saturation detected: still saturated\n");
257 break;
258 case REFCOUNT_MAX:
259 pr_warn("Saturation detected: unsafely reset to max\n");
260 break;
261 default:
262 pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref));
267 * A refcount_inc() from a saturated value should at most warn about
268 * being saturated already.
270 void lkdtm_REFCOUNT_INC_SATURATED(void)
272 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
274 pr_info("attempting bad refcount_inc() from saturated\n");
275 refcount_inc(&sat);
277 check_saturated(&sat);
280 /* Should act like refcount_inc() above from saturated. */
281 void lkdtm_REFCOUNT_DEC_SATURATED(void)
283 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
285 pr_info("attempting bad refcount_dec() from saturated\n");
286 refcount_dec(&sat);
288 check_saturated(&sat);
291 /* Should act like refcount_inc() above from saturated. */
292 void lkdtm_REFCOUNT_ADD_SATURATED(void)
294 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
296 pr_info("attempting bad refcount_dec() from saturated\n");
297 refcount_add(8, &sat);
299 check_saturated(&sat);
302 /* Should act like refcount_inc() above from saturated. */
303 void lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void)
305 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
307 pr_info("attempting bad refcount_inc_not_zero() from saturated\n");
308 if (!refcount_inc_not_zero(&sat))
309 pr_warn("Weird: refcount_inc_not_zero() reported zero\n");
311 check_saturated(&sat);
314 /* Should act like refcount_inc() above from saturated. */
315 void lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void)
317 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
319 pr_info("attempting bad refcount_add_not_zero() from saturated\n");
320 if (!refcount_add_not_zero(7, &sat))
321 pr_warn("Weird: refcount_add_not_zero() reported zero\n");
323 check_saturated(&sat);
326 /* Should act like refcount_inc() above from saturated. */
327 void lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void)
329 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
331 pr_info("attempting bad refcount_dec_and_test() from saturated\n");
332 if (refcount_dec_and_test(&sat))
333 pr_warn("Weird: refcount_dec_and_test() reported zero\n");
335 check_saturated(&sat);
338 /* Should act like refcount_inc() above from saturated. */
339 void lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void)
341 refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED);
343 pr_info("attempting bad refcount_sub_and_test() from saturated\n");
344 if (refcount_sub_and_test(8, &sat))
345 pr_warn("Weird: refcount_sub_and_test() reported zero\n");
347 check_saturated(&sat);
350 /* Used to time the existing atomic_t when used for reference counting */
351 void lkdtm_ATOMIC_TIMING(void)
353 unsigned int i;
354 atomic_t count = ATOMIC_INIT(1);
356 for (i = 0; i < INT_MAX - 1; i++)
357 atomic_inc(&count);
359 for (i = INT_MAX; i > 0; i--)
360 if (atomic_dec_and_test(&count))
361 break;
363 if (i != 1)
364 pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1);
365 else
366 pr_info("atomic timing: done\n");
370 * This can be compared to ATOMIC_TIMING when implementing fast refcount
371 * protections. Looking at the number of CPU cycles tells the real story
372 * about performance. For example:
373 * cd /sys/kernel/debug/provoke-crash
374 * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT
376 void lkdtm_REFCOUNT_TIMING(void)
378 unsigned int i;
379 refcount_t count = REFCOUNT_INIT(1);
381 for (i = 0; i < INT_MAX - 1; i++)
382 refcount_inc(&count);
384 for (i = INT_MAX; i > 0; i--)
385 if (refcount_dec_and_test(&count))
386 break;
388 if (i != 1)
389 pr_err("refcount: out of sync up/down cycle: %u\n", i - 1);
390 else
391 pr_info("refcount timing: done\n");