18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * This is for all the tests related to refcount bugs (e.g. overflow, 48c2ecf20Sopenharmony_ci * underflow, reaching zero untested, etc). 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#include "lkdtm.h" 78c2ecf20Sopenharmony_ci#include <linux/refcount.h> 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_cistatic void overflow_check(refcount_t *ref) 108c2ecf20Sopenharmony_ci{ 118c2ecf20Sopenharmony_ci switch (refcount_read(ref)) { 128c2ecf20Sopenharmony_ci case REFCOUNT_SATURATED: 138c2ecf20Sopenharmony_ci pr_info("Overflow detected: saturated\n"); 148c2ecf20Sopenharmony_ci break; 158c2ecf20Sopenharmony_ci case REFCOUNT_MAX: 168c2ecf20Sopenharmony_ci pr_warn("Overflow detected: unsafely reset to max\n"); 178c2ecf20Sopenharmony_ci break; 188c2ecf20Sopenharmony_ci default: 198c2ecf20Sopenharmony_ci pr_err("Fail: refcount wrapped to %d\n", refcount_read(ref)); 208c2ecf20Sopenharmony_ci } 218c2ecf20Sopenharmony_ci} 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci/* 248c2ecf20Sopenharmony_ci * A refcount_inc() above the maximum value of the refcount implementation, 258c2ecf20Sopenharmony_ci * should at least saturate, and at most also WARN. 268c2ecf20Sopenharmony_ci */ 278c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_INC_OVERFLOW(void) 288c2ecf20Sopenharmony_ci{ 298c2ecf20Sopenharmony_ci refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1); 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci pr_info("attempting good refcount_inc() without overflow\n"); 328c2ecf20Sopenharmony_ci refcount_dec(&over); 338c2ecf20Sopenharmony_ci refcount_inc(&over); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_inc() overflow\n"); 368c2ecf20Sopenharmony_ci refcount_inc(&over); 378c2ecf20Sopenharmony_ci refcount_inc(&over); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci overflow_check(&over); 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci/* refcount_add() should behave just like refcount_inc() above. */ 438c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_ADD_OVERFLOW(void) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX - 1); 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci pr_info("attempting good refcount_add() without overflow\n"); 488c2ecf20Sopenharmony_ci refcount_dec(&over); 498c2ecf20Sopenharmony_ci refcount_dec(&over); 508c2ecf20Sopenharmony_ci refcount_dec(&over); 518c2ecf20Sopenharmony_ci refcount_dec(&over); 528c2ecf20Sopenharmony_ci refcount_add(4, &over); 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_add() overflow\n"); 558c2ecf20Sopenharmony_ci refcount_add(4, &over); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci overflow_check(&over); 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci/* refcount_inc_not_zero() should behave just like refcount_inc() above. */ 618c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_INC_NOT_ZERO_OVERFLOW(void) 628c2ecf20Sopenharmony_ci{ 638c2ecf20Sopenharmony_ci refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX); 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_inc_not_zero() overflow\n"); 668c2ecf20Sopenharmony_ci if (!refcount_inc_not_zero(&over)) 678c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_inc_not_zero() reported zero\n"); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci overflow_check(&over); 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* refcount_add_not_zero() should behave just like refcount_inc() above. */ 738c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_ADD_NOT_ZERO_OVERFLOW(void) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci refcount_t over = REFCOUNT_INIT(REFCOUNT_MAX); 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_add_not_zero() overflow\n"); 788c2ecf20Sopenharmony_ci if (!refcount_add_not_zero(6, &over)) 798c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_add_not_zero() reported zero\n"); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci overflow_check(&over); 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic void check_zero(refcount_t *ref) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci switch (refcount_read(ref)) { 878c2ecf20Sopenharmony_ci case REFCOUNT_SATURATED: 888c2ecf20Sopenharmony_ci pr_info("Zero detected: saturated\n"); 898c2ecf20Sopenharmony_ci break; 908c2ecf20Sopenharmony_ci case REFCOUNT_MAX: 918c2ecf20Sopenharmony_ci pr_warn("Zero detected: unsafely reset to max\n"); 928c2ecf20Sopenharmony_ci break; 938c2ecf20Sopenharmony_ci case 0: 948c2ecf20Sopenharmony_ci pr_warn("Still at zero: refcount_inc/add() must not inc-from-0\n"); 958c2ecf20Sopenharmony_ci break; 968c2ecf20Sopenharmony_ci default: 978c2ecf20Sopenharmony_ci pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref)); 988c2ecf20Sopenharmony_ci } 998c2ecf20Sopenharmony_ci} 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/* 1028c2ecf20Sopenharmony_ci * A refcount_dec(), as opposed to a refcount_dec_and_test(), when it hits 1038c2ecf20Sopenharmony_ci * zero it should either saturate (when inc-from-zero isn't protected) 1048c2ecf20Sopenharmony_ci * or stay at zero (when inc-from-zero is protected) and should WARN for both. 1058c2ecf20Sopenharmony_ci */ 1068c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_DEC_ZERO(void) 1078c2ecf20Sopenharmony_ci{ 1088c2ecf20Sopenharmony_ci refcount_t zero = REFCOUNT_INIT(2); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci pr_info("attempting good refcount_dec()\n"); 1118c2ecf20Sopenharmony_ci refcount_dec(&zero); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_dec() to zero\n"); 1148c2ecf20Sopenharmony_ci refcount_dec(&zero); 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci check_zero(&zero); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_cistatic void check_negative(refcount_t *ref, int start) 1208c2ecf20Sopenharmony_ci{ 1218c2ecf20Sopenharmony_ci /* 1228c2ecf20Sopenharmony_ci * refcount_t refuses to move a refcount at all on an 1238c2ecf20Sopenharmony_ci * over-sub, so we have to track our starting position instead of 1248c2ecf20Sopenharmony_ci * looking only at zero-pinning. 1258c2ecf20Sopenharmony_ci */ 1268c2ecf20Sopenharmony_ci if (refcount_read(ref) == start) { 1278c2ecf20Sopenharmony_ci pr_warn("Still at %d: refcount_inc/add() must not inc-from-0\n", 1288c2ecf20Sopenharmony_ci start); 1298c2ecf20Sopenharmony_ci return; 1308c2ecf20Sopenharmony_ci } 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci switch (refcount_read(ref)) { 1338c2ecf20Sopenharmony_ci case REFCOUNT_SATURATED: 1348c2ecf20Sopenharmony_ci pr_info("Negative detected: saturated\n"); 1358c2ecf20Sopenharmony_ci break; 1368c2ecf20Sopenharmony_ci case REFCOUNT_MAX: 1378c2ecf20Sopenharmony_ci pr_warn("Negative detected: unsafely reset to max\n"); 1388c2ecf20Sopenharmony_ci break; 1398c2ecf20Sopenharmony_ci default: 1408c2ecf20Sopenharmony_ci pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref)); 1418c2ecf20Sopenharmony_ci } 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci/* A refcount_dec() going negative should saturate and may WARN. */ 1458c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_DEC_NEGATIVE(void) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci refcount_t neg = REFCOUNT_INIT(0); 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_dec() below zero\n"); 1508c2ecf20Sopenharmony_ci refcount_dec(&neg); 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci check_negative(&neg, 0); 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci/* 1568c2ecf20Sopenharmony_ci * A refcount_dec_and_test() should act like refcount_dec() above when 1578c2ecf20Sopenharmony_ci * going negative. 1588c2ecf20Sopenharmony_ci */ 1598c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_DEC_AND_TEST_NEGATIVE(void) 1608c2ecf20Sopenharmony_ci{ 1618c2ecf20Sopenharmony_ci refcount_t neg = REFCOUNT_INIT(0); 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_dec_and_test() below zero\n"); 1648c2ecf20Sopenharmony_ci if (refcount_dec_and_test(&neg)) 1658c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_dec_and_test() reported zero\n"); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci check_negative(&neg, 0); 1688c2ecf20Sopenharmony_ci} 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci/* 1718c2ecf20Sopenharmony_ci * A refcount_sub_and_test() should act like refcount_dec_and_test() 1728c2ecf20Sopenharmony_ci * above when going negative. 1738c2ecf20Sopenharmony_ci */ 1748c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_SUB_AND_TEST_NEGATIVE(void) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci refcount_t neg = REFCOUNT_INIT(3); 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_sub_and_test() below zero\n"); 1798c2ecf20Sopenharmony_ci if (refcount_sub_and_test(5, &neg)) 1808c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_sub_and_test() reported zero\n"); 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci check_negative(&neg, 3); 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci/* 1868c2ecf20Sopenharmony_ci * A refcount_sub_and_test() by zero when the counter is at zero should act like 1878c2ecf20Sopenharmony_ci * refcount_sub_and_test() above when going negative. 1888c2ecf20Sopenharmony_ci */ 1898c2ecf20Sopenharmony_cistatic void lkdtm_REFCOUNT_SUB_AND_TEST_ZERO(void) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci refcount_t neg = REFCOUNT_INIT(0); 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_sub_and_test() at zero\n"); 1948c2ecf20Sopenharmony_ci if (refcount_sub_and_test(0, &neg)) 1958c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_sub_and_test() reported zero\n"); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci check_negative(&neg, 0); 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cistatic void check_from_zero(refcount_t *ref) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci switch (refcount_read(ref)) { 2038c2ecf20Sopenharmony_ci case 0: 2048c2ecf20Sopenharmony_ci pr_info("Zero detected: stayed at zero\n"); 2058c2ecf20Sopenharmony_ci break; 2068c2ecf20Sopenharmony_ci case REFCOUNT_SATURATED: 2078c2ecf20Sopenharmony_ci pr_info("Zero detected: saturated\n"); 2088c2ecf20Sopenharmony_ci break; 2098c2ecf20Sopenharmony_ci case REFCOUNT_MAX: 2108c2ecf20Sopenharmony_ci pr_warn("Zero detected: unsafely reset to max\n"); 2118c2ecf20Sopenharmony_ci break; 2128c2ecf20Sopenharmony_ci default: 2138c2ecf20Sopenharmony_ci pr_info("Fail: zero not detected, incremented to %d\n", 2148c2ecf20Sopenharmony_ci refcount_read(ref)); 2158c2ecf20Sopenharmony_ci } 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci/* 2198c2ecf20Sopenharmony_ci * A refcount_inc() from zero should pin to zero or saturate and may WARN. 2208c2ecf20Sopenharmony_ci */ 2218c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_INC_ZERO(void) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci refcount_t zero = REFCOUNT_INIT(0); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci pr_info("attempting safe refcount_inc_not_zero() from zero\n"); 2268c2ecf20Sopenharmony_ci if (!refcount_inc_not_zero(&zero)) { 2278c2ecf20Sopenharmony_ci pr_info("Good: zero detected\n"); 2288c2ecf20Sopenharmony_ci if (refcount_read(&zero) == 0) 2298c2ecf20Sopenharmony_ci pr_info("Correctly stayed at zero\n"); 2308c2ecf20Sopenharmony_ci else 2318c2ecf20Sopenharmony_ci pr_err("Fail: refcount went past zero!\n"); 2328c2ecf20Sopenharmony_ci } else { 2338c2ecf20Sopenharmony_ci pr_err("Fail: Zero not detected!?\n"); 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_inc() from zero\n"); 2378c2ecf20Sopenharmony_ci refcount_inc(&zero); 2388c2ecf20Sopenharmony_ci 2398c2ecf20Sopenharmony_ci check_from_zero(&zero); 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci/* 2438c2ecf20Sopenharmony_ci * A refcount_add() should act like refcount_inc() above when starting 2448c2ecf20Sopenharmony_ci * at zero. 2458c2ecf20Sopenharmony_ci */ 2468c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_ADD_ZERO(void) 2478c2ecf20Sopenharmony_ci{ 2488c2ecf20Sopenharmony_ci refcount_t zero = REFCOUNT_INIT(0); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci pr_info("attempting safe refcount_add_not_zero() from zero\n"); 2518c2ecf20Sopenharmony_ci if (!refcount_add_not_zero(3, &zero)) { 2528c2ecf20Sopenharmony_ci pr_info("Good: zero detected\n"); 2538c2ecf20Sopenharmony_ci if (refcount_read(&zero) == 0) 2548c2ecf20Sopenharmony_ci pr_info("Correctly stayed at zero\n"); 2558c2ecf20Sopenharmony_ci else 2568c2ecf20Sopenharmony_ci pr_err("Fail: refcount went past zero\n"); 2578c2ecf20Sopenharmony_ci } else { 2588c2ecf20Sopenharmony_ci pr_err("Fail: Zero not detected!?\n"); 2598c2ecf20Sopenharmony_ci } 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_add() from zero\n"); 2628c2ecf20Sopenharmony_ci refcount_add(3, &zero); 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci check_from_zero(&zero); 2658c2ecf20Sopenharmony_ci} 2668c2ecf20Sopenharmony_ci 2678c2ecf20Sopenharmony_cistatic void check_saturated(refcount_t *ref) 2688c2ecf20Sopenharmony_ci{ 2698c2ecf20Sopenharmony_ci switch (refcount_read(ref)) { 2708c2ecf20Sopenharmony_ci case REFCOUNT_SATURATED: 2718c2ecf20Sopenharmony_ci pr_info("Saturation detected: still saturated\n"); 2728c2ecf20Sopenharmony_ci break; 2738c2ecf20Sopenharmony_ci case REFCOUNT_MAX: 2748c2ecf20Sopenharmony_ci pr_warn("Saturation detected: unsafely reset to max\n"); 2758c2ecf20Sopenharmony_ci break; 2768c2ecf20Sopenharmony_ci default: 2778c2ecf20Sopenharmony_ci pr_err("Fail: refcount went crazy: %d\n", refcount_read(ref)); 2788c2ecf20Sopenharmony_ci } 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci/* 2828c2ecf20Sopenharmony_ci * A refcount_inc() from a saturated value should at most warn about 2838c2ecf20Sopenharmony_ci * being saturated already. 2848c2ecf20Sopenharmony_ci */ 2858c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_INC_SATURATED(void) 2868c2ecf20Sopenharmony_ci{ 2878c2ecf20Sopenharmony_ci refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_inc() from saturated\n"); 2908c2ecf20Sopenharmony_ci refcount_inc(&sat); 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci check_saturated(&sat); 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci/* Should act like refcount_inc() above from saturated. */ 2968c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_DEC_SATURATED(void) 2978c2ecf20Sopenharmony_ci{ 2988c2ecf20Sopenharmony_ci refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED); 2998c2ecf20Sopenharmony_ci 3008c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_dec() from saturated\n"); 3018c2ecf20Sopenharmony_ci refcount_dec(&sat); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci check_saturated(&sat); 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci/* Should act like refcount_inc() above from saturated. */ 3078c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_ADD_SATURATED(void) 3088c2ecf20Sopenharmony_ci{ 3098c2ecf20Sopenharmony_ci refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED); 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_dec() from saturated\n"); 3128c2ecf20Sopenharmony_ci refcount_add(8, &sat); 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci check_saturated(&sat); 3158c2ecf20Sopenharmony_ci} 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci/* Should act like refcount_inc() above from saturated. */ 3188c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_INC_NOT_ZERO_SATURATED(void) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED); 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_inc_not_zero() from saturated\n"); 3238c2ecf20Sopenharmony_ci if (!refcount_inc_not_zero(&sat)) 3248c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_inc_not_zero() reported zero\n"); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci check_saturated(&sat); 3278c2ecf20Sopenharmony_ci} 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci/* Should act like refcount_inc() above from saturated. */ 3308c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_ADD_NOT_ZERO_SATURATED(void) 3318c2ecf20Sopenharmony_ci{ 3328c2ecf20Sopenharmony_ci refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED); 3338c2ecf20Sopenharmony_ci 3348c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_add_not_zero() from saturated\n"); 3358c2ecf20Sopenharmony_ci if (!refcount_add_not_zero(7, &sat)) 3368c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_add_not_zero() reported zero\n"); 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci check_saturated(&sat); 3398c2ecf20Sopenharmony_ci} 3408c2ecf20Sopenharmony_ci 3418c2ecf20Sopenharmony_ci/* Should act like refcount_inc() above from saturated. */ 3428c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_DEC_AND_TEST_SATURATED(void) 3438c2ecf20Sopenharmony_ci{ 3448c2ecf20Sopenharmony_ci refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED); 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_dec_and_test() from saturated\n"); 3478c2ecf20Sopenharmony_ci if (refcount_dec_and_test(&sat)) 3488c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_dec_and_test() reported zero\n"); 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci check_saturated(&sat); 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci/* Should act like refcount_inc() above from saturated. */ 3548c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_SUB_AND_TEST_SATURATED(void) 3558c2ecf20Sopenharmony_ci{ 3568c2ecf20Sopenharmony_ci refcount_t sat = REFCOUNT_INIT(REFCOUNT_SATURATED); 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci pr_info("attempting bad refcount_sub_and_test() from saturated\n"); 3598c2ecf20Sopenharmony_ci if (refcount_sub_and_test(8, &sat)) 3608c2ecf20Sopenharmony_ci pr_warn("Weird: refcount_sub_and_test() reported zero\n"); 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci check_saturated(&sat); 3638c2ecf20Sopenharmony_ci} 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci/* Used to time the existing atomic_t when used for reference counting */ 3668c2ecf20Sopenharmony_civoid lkdtm_ATOMIC_TIMING(void) 3678c2ecf20Sopenharmony_ci{ 3688c2ecf20Sopenharmony_ci unsigned int i; 3698c2ecf20Sopenharmony_ci atomic_t count = ATOMIC_INIT(1); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci for (i = 0; i < INT_MAX - 1; i++) 3728c2ecf20Sopenharmony_ci atomic_inc(&count); 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci for (i = INT_MAX; i > 0; i--) 3758c2ecf20Sopenharmony_ci if (atomic_dec_and_test(&count)) 3768c2ecf20Sopenharmony_ci break; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci if (i != 1) 3798c2ecf20Sopenharmony_ci pr_err("atomic timing: out of sync up/down cycle: %u\n", i - 1); 3808c2ecf20Sopenharmony_ci else 3818c2ecf20Sopenharmony_ci pr_info("atomic timing: done\n"); 3828c2ecf20Sopenharmony_ci} 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci/* 3858c2ecf20Sopenharmony_ci * This can be compared to ATOMIC_TIMING when implementing fast refcount 3868c2ecf20Sopenharmony_ci * protections. Looking at the number of CPU cycles tells the real story 3878c2ecf20Sopenharmony_ci * about performance. For example: 3888c2ecf20Sopenharmony_ci * cd /sys/kernel/debug/provoke-crash 3898c2ecf20Sopenharmony_ci * perf stat -B -- cat <(echo REFCOUNT_TIMING) > DIRECT 3908c2ecf20Sopenharmony_ci */ 3918c2ecf20Sopenharmony_civoid lkdtm_REFCOUNT_TIMING(void) 3928c2ecf20Sopenharmony_ci{ 3938c2ecf20Sopenharmony_ci unsigned int i; 3948c2ecf20Sopenharmony_ci refcount_t count = REFCOUNT_INIT(1); 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci for (i = 0; i < INT_MAX - 1; i++) 3978c2ecf20Sopenharmony_ci refcount_inc(&count); 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci for (i = INT_MAX; i > 0; i--) 4008c2ecf20Sopenharmony_ci if (refcount_dec_and_test(&count)) 4018c2ecf20Sopenharmony_ci break; 4028c2ecf20Sopenharmony_ci 4038c2ecf20Sopenharmony_ci if (i != 1) 4048c2ecf20Sopenharmony_ci pr_err("refcount: out of sync up/down cycle: %u\n", i - 1); 4058c2ecf20Sopenharmony_ci else 4068c2ecf20Sopenharmony_ci pr_info("refcount timing: done\n"); 4078c2ecf20Sopenharmony_ci} 408