1ef349abdSMatthias Maennich// SPDX-License-Identifier: GPL-2.0-only 2b56e5355SElena Reshetova// Check if refcount_t type and API should be used 3b56e5355SElena Reshetova// instead of atomic_t type when dealing with refcounters 4b56e5355SElena Reshetova// 5b56e5355SElena Reshetova// Copyright (c) 2016-2017, Elena Reshetova, Intel Corporation 6b56e5355SElena Reshetova// 7b56e5355SElena Reshetova// Confidence: Moderate 8f01701ceSJulia Lawall// URL: https://coccinelle.gitlabpages.inria.fr/website 9b56e5355SElena Reshetova// Options: --include-headers --very-quiet 10b56e5355SElena Reshetova 11b56e5355SElena Reshetovavirtual report 12b56e5355SElena Reshetova 13b56e5355SElena Reshetova@r1 exists@ 14b56e5355SElena Reshetovaidentifier a, x; 15b56e5355SElena Reshetovaposition p1, p2; 16b56e5355SElena Reshetovaidentifier fname =~ ".*free.*"; 17b56e5355SElena Reshetovaidentifier fname2 =~ ".*destroy.*"; 18b56e5355SElena Reshetovaidentifier fname3 =~ ".*del.*"; 19b56e5355SElena Reshetovaidentifier fname4 =~ ".*queue_work.*"; 20b56e5355SElena Reshetovaidentifier fname5 =~ ".*schedule_work.*"; 21b56e5355SElena Reshetovaidentifier fname6 =~ ".*call_rcu.*"; 22b56e5355SElena Reshetova 23b56e5355SElena Reshetova@@ 24b56e5355SElena Reshetova 25b56e5355SElena Reshetova( 26b56e5355SElena Reshetova atomic_dec_and_test@p1(&(a)->x) 27b56e5355SElena Reshetova| 28b56e5355SElena Reshetova atomic_dec_and_lock@p1(&(a)->x, ...) 29b56e5355SElena Reshetova| 30b56e5355SElena Reshetova atomic_long_dec_and_lock@p1(&(a)->x, ...) 31b56e5355SElena Reshetova| 32b56e5355SElena Reshetova atomic_long_dec_and_test@p1(&(a)->x) 33b56e5355SElena Reshetova| 34b56e5355SElena Reshetova atomic64_dec_and_test@p1(&(a)->x) 35b56e5355SElena Reshetova| 36b56e5355SElena Reshetova local_dec_and_test@p1(&(a)->x) 37b56e5355SElena Reshetova) 38b56e5355SElena Reshetova... 39b56e5355SElena Reshetova( 40b56e5355SElena Reshetova fname@p2(a, ...); 41b56e5355SElena Reshetova| 42b56e5355SElena Reshetova fname2@p2(...); 43b56e5355SElena Reshetova| 44b56e5355SElena Reshetova fname3@p2(...); 45b56e5355SElena Reshetova| 46b56e5355SElena Reshetova fname4@p2(...); 47b56e5355SElena Reshetova| 48b56e5355SElena Reshetova fname5@p2(...); 49b56e5355SElena Reshetova| 50b56e5355SElena Reshetova fname6@p2(...); 51b56e5355SElena Reshetova) 52b56e5355SElena Reshetova 53b56e5355SElena Reshetova 54b56e5355SElena Reshetova@script:python depends on report@ 55b56e5355SElena Reshetovap1 << r1.p1; 56b56e5355SElena Reshetovap2 << r1.p2; 57b56e5355SElena Reshetova@@ 58*bab55037SDeepak R Varmamsg = "WARNING: atomic_dec_and_test variation before object free at line %s." 59b56e5355SElena Reshetovacoccilib.report.print_report(p1[0], msg % (p2[0].line)) 60b56e5355SElena Reshetova 61b56e5355SElena Reshetova@r4 exists@ 62b56e5355SElena Reshetovaidentifier a, x, y; 63b56e5355SElena Reshetovaposition p1, p2; 64b56e5355SElena Reshetovaidentifier fname =~ ".*free.*"; 65b56e5355SElena Reshetova 66b56e5355SElena Reshetova@@ 67b56e5355SElena Reshetova 68b56e5355SElena Reshetova( 69b56e5355SElena Reshetova atomic_dec_and_test@p1(&(a)->x) 70b56e5355SElena Reshetova| 71b56e5355SElena Reshetova atomic_dec_and_lock@p1(&(a)->x, ...) 72b56e5355SElena Reshetova| 73b56e5355SElena Reshetova atomic_long_dec_and_lock@p1(&(a)->x, ...) 74b56e5355SElena Reshetova| 75b56e5355SElena Reshetova atomic_long_dec_and_test@p1(&(a)->x) 76b56e5355SElena Reshetova| 77b56e5355SElena Reshetova atomic64_dec_and_test@p1(&(a)->x) 78b56e5355SElena Reshetova| 79b56e5355SElena Reshetova local_dec_and_test@p1(&(a)->x) 80b56e5355SElena Reshetova) 81b56e5355SElena Reshetova... 82b56e5355SElena Reshetovay=a 83b56e5355SElena Reshetova... 84b56e5355SElena Reshetovafname@p2(y, ...); 85b56e5355SElena Reshetova 86b56e5355SElena Reshetova 87b56e5355SElena Reshetova@script:python depends on report@ 88b56e5355SElena Reshetovap1 << r4.p1; 89b56e5355SElena Reshetovap2 << r4.p2; 90b56e5355SElena Reshetova@@ 91*bab55037SDeepak R Varmamsg = "WARNING: atomic_dec_and_test variation before object free at line %s." 92b56e5355SElena Reshetovacoccilib.report.print_report(p1[0], msg % (p2[0].line)) 93b56e5355SElena Reshetova 94b56e5355SElena Reshetova@r2 exists@ 95b56e5355SElena Reshetovaidentifier a, x; 96b56e5355SElena Reshetovaposition p1; 97b56e5355SElena Reshetova@@ 98b56e5355SElena Reshetova 99b56e5355SElena Reshetova( 100b56e5355SElena Reshetovaatomic_add_unless(&(a)->x,-1,1)@p1 101b56e5355SElena Reshetova| 102b56e5355SElena Reshetovaatomic_long_add_unless(&(a)->x,-1,1)@p1 103b56e5355SElena Reshetova| 104b56e5355SElena Reshetovaatomic64_add_unless(&(a)->x,-1,1)@p1 105b56e5355SElena Reshetova) 106b56e5355SElena Reshetova 107b56e5355SElena Reshetova@script:python depends on report@ 108b56e5355SElena Reshetovap1 << r2.p1; 109b56e5355SElena Reshetova@@ 110*bab55037SDeepak R Varmamsg = "WARNING: atomic_add_unless" 111b56e5355SElena Reshetovacoccilib.report.print_report(p1[0], msg) 112b56e5355SElena Reshetova 113b56e5355SElena Reshetova@r3 exists@ 114b56e5355SElena Reshetovaidentifier x; 115b56e5355SElena Reshetovaposition p1; 116b56e5355SElena Reshetova@@ 117b56e5355SElena Reshetova 118b56e5355SElena Reshetova( 119b56e5355SElena Reshetovax = atomic_add_return@p1(-1, ...); 120b56e5355SElena Reshetova| 121b56e5355SElena Reshetovax = atomic_long_add_return@p1(-1, ...); 122b56e5355SElena Reshetova| 123b56e5355SElena Reshetovax = atomic64_add_return@p1(-1, ...); 124b56e5355SElena Reshetova) 125b56e5355SElena Reshetova 126b56e5355SElena Reshetova@script:python depends on report@ 127b56e5355SElena Reshetovap1 << r3.p1; 128b56e5355SElena Reshetova@@ 129*bab55037SDeepak R Varmamsg = "WARNING: x = atomic_add_return(-1, ...)" 130b56e5355SElena Reshetovacoccilib.report.print_report(p1[0], msg) 131