diff options
Diffstat (limited to 'scripts/coccinelle/locks')
-rw-r--r-- | scripts/coccinelle/locks/call_kern.cocci | 74 | ||||
-rw-r--r-- | scripts/coccinelle/locks/double_lock.cocci | 92 | ||||
-rw-r--r-- | scripts/coccinelle/locks/flags.cocci | 80 | ||||
-rw-r--r-- | scripts/coccinelle/locks/mini_lock.cocci | 95 |
4 files changed, 341 insertions, 0 deletions
diff --git a/scripts/coccinelle/locks/call_kern.cocci b/scripts/coccinelle/locks/call_kern.cocci new file mode 100644 index 000000000000..00af5344a68f --- /dev/null +++ b/scripts/coccinelle/locks/call_kern.cocci | |||
@@ -0,0 +1,74 @@ | |||
1 | /// Find functions that refer to GFP_KERNEL but are called with locks held. | ||
2 | /// The proposed change of converting the GFP_KERNEL is not necessarily the | ||
3 | /// correct one. It may be desired to unlock the lock, or to not call the | ||
4 | /// function under the lock in the first place. | ||
5 | /// | ||
6 | // Confidence: Moderate | ||
7 | // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2. | ||
8 | // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2. | ||
9 | // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
10 | // URL: http://coccinelle.lip6.fr/ | ||
11 | // Comments: | ||
12 | // Options: -no_includes -include_headers | ||
13 | |||
14 | virtual patch | ||
15 | |||
16 | @gfp exists@ | ||
17 | identifier fn; | ||
18 | position p; | ||
19 | @@ | ||
20 | |||
21 | fn(...) { | ||
22 | ... when != read_unlock_irq(...) | ||
23 | when != write_unlock_irq(...) | ||
24 | when != read_unlock_irqrestore(...) | ||
25 | when != write_unlock_irqrestore(...) | ||
26 | when != spin_unlock(...) | ||
27 | when != spin_unlock_irq(...) | ||
28 | when != spin_unlock_irqrestore(...) | ||
29 | when != local_irq_enable(...) | ||
30 | when any | ||
31 | GFP_KERNEL@p | ||
32 | ... when any | ||
33 | } | ||
34 | |||
35 | @locked@ | ||
36 | identifier gfp.fn; | ||
37 | @@ | ||
38 | |||
39 | ( | ||
40 | read_lock_irq | ||
41 | | | ||
42 | write_lock_irq | ||
43 | | | ||
44 | read_lock_irqsave | ||
45 | | | ||
46 | write_lock_irqsave | ||
47 | | | ||
48 | spin_lock | ||
49 | | | ||
50 | spin_trylock | ||
51 | | | ||
52 | spin_lock_irq | ||
53 | | | ||
54 | spin_lock_irqsave | ||
55 | | | ||
56 | local_irq_disable | ||
57 | ) | ||
58 | (...) | ||
59 | ... when != read_unlock_irq(...) | ||
60 | when != write_unlock_irq(...) | ||
61 | when != read_unlock_irqrestore(...) | ||
62 | when != write_unlock_irqrestore(...) | ||
63 | when != spin_unlock(...) | ||
64 | when != spin_unlock_irq(...) | ||
65 | when != spin_unlock_irqrestore(...) | ||
66 | when != local_irq_enable(...) | ||
67 | fn(...) | ||
68 | |||
69 | @depends on locked@ | ||
70 | position gfp.p; | ||
71 | @@ | ||
72 | |||
73 | - GFP_KERNEL@p | ||
74 | + GFP_ATOMIC | ||
diff --git a/scripts/coccinelle/locks/double_lock.cocci b/scripts/coccinelle/locks/double_lock.cocci new file mode 100644 index 000000000000..63b24e682fad --- /dev/null +++ b/scripts/coccinelle/locks/double_lock.cocci | |||
@@ -0,0 +1,92 @@ | |||
1 | /// Find double locks. False positives may occur when some paths cannot | ||
2 | /// occur at execution, due to the values of variables, and when there is | ||
3 | /// an intervening function call that releases the lock. | ||
4 | /// | ||
5 | // Confidence: Moderate | ||
6 | // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2. | ||
7 | // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2. | ||
8 | // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
9 | // URL: http://coccinelle.lip6.fr/ | ||
10 | // Comments: | ||
11 | // Options: -no_includes -include_headers | ||
12 | |||
13 | virtual org | ||
14 | virtual report | ||
15 | |||
16 | @locked@ | ||
17 | position p1; | ||
18 | expression E1; | ||
19 | position p; | ||
20 | @@ | ||
21 | |||
22 | ( | ||
23 | mutex_lock@p1 | ||
24 | | | ||
25 | mutex_trylock@p1 | ||
26 | | | ||
27 | spin_lock@p1 | ||
28 | | | ||
29 | spin_trylock@p1 | ||
30 | | | ||
31 | read_lock@p1 | ||
32 | | | ||
33 | read_trylock@p1 | ||
34 | | | ||
35 | write_lock@p1 | ||
36 | | | ||
37 | write_trylock@p1 | ||
38 | ) (E1@p,...); | ||
39 | |||
40 | @balanced@ | ||
41 | position p1 != locked.p1; | ||
42 | position locked.p; | ||
43 | identifier lock,unlock; | ||
44 | expression x <= locked.E1; | ||
45 | expression E,locked.E1; | ||
46 | expression E2; | ||
47 | @@ | ||
48 | |||
49 | if (E) { | ||
50 | <+... when != E1 | ||
51 | lock(E1@p,...) | ||
52 | ...+> | ||
53 | } | ||
54 | ... when != E1 | ||
55 | when != \(x = E2\|&x\) | ||
56 | when forall | ||
57 | if (E) { | ||
58 | <+... when != E1 | ||
59 | unlock@p1(E1,...) | ||
60 | ...+> | ||
61 | } | ||
62 | |||
63 | @r depends on !balanced exists@ | ||
64 | expression x <= locked.E1; | ||
65 | expression locked.E1; | ||
66 | expression E2; | ||
67 | identifier lock; | ||
68 | position locked.p,p1,p2; | ||
69 | @@ | ||
70 | |||
71 | lock@p1 (E1@p,...); | ||
72 | ... when != E1 | ||
73 | when != \(x = E2\|&x\) | ||
74 | lock@p2 (E1,...); | ||
75 | |||
76 | @script:python depends on org@ | ||
77 | p1 << r.p1; | ||
78 | p2 << r.p2; | ||
79 | lock << r.lock; | ||
80 | @@ | ||
81 | |||
82 | cocci.print_main(lock,p1) | ||
83 | cocci.print_secs("second lock",p2) | ||
84 | |||
85 | @script:python depends on report@ | ||
86 | p1 << r.p1; | ||
87 | p2 << r.p2; | ||
88 | lock << r.lock; | ||
89 | @@ | ||
90 | |||
91 | msg = "second lock on line %s" % (p2[0].line) | ||
92 | coccilib.report.print_report(p1[0],msg) | ||
diff --git a/scripts/coccinelle/locks/flags.cocci b/scripts/coccinelle/locks/flags.cocci new file mode 100644 index 000000000000..b4344d838097 --- /dev/null +++ b/scripts/coccinelle/locks/flags.cocci | |||
@@ -0,0 +1,80 @@ | |||
1 | /// Find nested lock+irqsave functions that use the same flags variables | ||
2 | /// | ||
3 | // Confidence: High | ||
4 | // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2. | ||
5 | // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2. | ||
6 | // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
7 | // URL: http://coccinelle.lip6.fr/ | ||
8 | // Comments: | ||
9 | // Options: -no_includes -include_headers | ||
10 | |||
11 | virtual context | ||
12 | virtual org | ||
13 | virtual report | ||
14 | |||
15 | @r@ | ||
16 | expression lock1,lock2,flags; | ||
17 | position p1,p2; | ||
18 | @@ | ||
19 | |||
20 | ( | ||
21 | spin_lock_irqsave@p1(lock1,flags) | ||
22 | | | ||
23 | read_lock_irqsave@p1(lock1,flags) | ||
24 | | | ||
25 | write_lock_irqsave@p1(lock1,flags) | ||
26 | ) | ||
27 | ... when != flags | ||
28 | ( | ||
29 | spin_lock_irqsave(lock1,flags) | ||
30 | | | ||
31 | read_lock_irqsave(lock1,flags) | ||
32 | | | ||
33 | write_lock_irqsave(lock1,flags) | ||
34 | | | ||
35 | spin_lock_irqsave@p2(lock2,flags) | ||
36 | | | ||
37 | read_lock_irqsave@p2(lock2,flags) | ||
38 | | | ||
39 | write_lock_irqsave@p2(lock2,flags) | ||
40 | ) | ||
41 | |||
42 | @d@ | ||
43 | expression f <= r.flags; | ||
44 | expression lock1,lock2,flags; | ||
45 | position r.p1, r.p2; | ||
46 | @@ | ||
47 | |||
48 | ( | ||
49 | *spin_lock_irqsave@p1(lock1,flags) | ||
50 | | | ||
51 | *read_lock_irqsave@p1(lock1,flags) | ||
52 | | | ||
53 | *write_lock_irqsave@p1(lock1,flags) | ||
54 | ) | ||
55 | ... when != f | ||
56 | ( | ||
57 | *spin_lock_irqsave@p2(lock2,flags) | ||
58 | | | ||
59 | *read_lock_irqsave@p2(lock2,flags) | ||
60 | | | ||
61 | *write_lock_irqsave@p2(lock2,flags) | ||
62 | ) | ||
63 | |||
64 | // ---------------------------------------------------------------------- | ||
65 | |||
66 | @script:python depends on d && org@ | ||
67 | p1 << r.p1; | ||
68 | p2 << r.p2; | ||
69 | @@ | ||
70 | |||
71 | cocci.print_main("original lock",p1) | ||
72 | cocci.print_secs("nested lock+irqsave that reuses flags",p2) | ||
73 | |||
74 | @script:python depends on d && report@ | ||
75 | p1 << r.p1; | ||
76 | p2 << r.p2; | ||
77 | @@ | ||
78 | |||
79 | msg="ERROR: nested lock+irqsave that reuses flags from %s." % (p1[0].line) | ||
80 | coccilib.report.print_report(p2[0], msg) | ||
diff --git a/scripts/coccinelle/locks/mini_lock.cocci b/scripts/coccinelle/locks/mini_lock.cocci new file mode 100644 index 000000000000..7641a2925434 --- /dev/null +++ b/scripts/coccinelle/locks/mini_lock.cocci | |||
@@ -0,0 +1,95 @@ | |||
1 | /// Find missing unlocks. This semantic match considers the specific case | ||
2 | /// where the unlock is missing from an if branch, and there is a lock | ||
3 | /// before the if and an unlock after the if. False positives are due to | ||
4 | /// cases where the if branch represents a case where the function is | ||
5 | /// supposed to exit with the lock held, or where there is some preceding | ||
6 | /// function call that releases the lock. | ||
7 | /// | ||
8 | // Confidence: Moderate | ||
9 | // Copyright: (C) 2010 Nicolas Palix, DIKU. GPLv2. | ||
10 | // Copyright: (C) 2010 Julia Lawall, DIKU. GPLv2. | ||
11 | // Copyright: (C) 2010 Gilles Muller, INRIA/LiP6. GPLv2. | ||
12 | // URL: http://coccinelle.lip6.fr/ | ||
13 | // Comments: | ||
14 | // Options: -no_includes -include_headers | ||
15 | |||
16 | virtual org | ||
17 | virtual report | ||
18 | |||
19 | @prelocked@ | ||
20 | position p1,p; | ||
21 | expression E1; | ||
22 | @@ | ||
23 | |||
24 | ( | ||
25 | mutex_lock@p1 | ||
26 | | | ||
27 | mutex_trylock@p1 | ||
28 | | | ||
29 | spin_lock@p1 | ||
30 | | | ||
31 | spin_trylock@p1 | ||
32 | | | ||
33 | read_lock@p1 | ||
34 | | | ||
35 | read_trylock@p1 | ||
36 | | | ||
37 | write_lock@p1 | ||
38 | | | ||
39 | write_trylock@p1 | ||
40 | | | ||
41 | read_lock_irq@p1 | ||
42 | | | ||
43 | write_lock_irq@p1 | ||
44 | | | ||
45 | read_lock_irqsave@p1 | ||
46 | | | ||
47 | write_lock_irqsave@p1 | ||
48 | | | ||
49 | spin_lock_irq@p1 | ||
50 | | | ||
51 | spin_lock_irqsave@p1 | ||
52 | ) (E1@p,...); | ||
53 | |||
54 | @looped@ | ||
55 | position r; | ||
56 | @@ | ||
57 | |||
58 | for(...;...;...) { <+... return@r ...; ...+> } | ||
59 | |||
60 | @err@ | ||
61 | expression E1; | ||
62 | position prelocked.p; | ||
63 | position up != prelocked.p1; | ||
64 | position r!=looped.r; | ||
65 | identifier lock,unlock; | ||
66 | @@ | ||
67 | |||
68 | lock(E1@p,...); | ||
69 | <+... when != E1 | ||
70 | if (...) { | ||
71 | ... when != E1 | ||
72 | return@r ...; | ||
73 | } | ||
74 | ...+> | ||
75 | unlock@up(E1,...); | ||
76 | |||
77 | @script:python depends on org@ | ||
78 | p << prelocked.p1; | ||
79 | lock << err.lock; | ||
80 | unlock << err.unlock; | ||
81 | p2 << err.r; | ||
82 | @@ | ||
83 | |||
84 | cocci.print_main(lock,p) | ||
85 | cocci.print_secs(unlock,p2) | ||
86 | |||
87 | @script:python depends on report@ | ||
88 | p << prelocked.p1; | ||
89 | lock << err.lock; | ||
90 | unlock << err.unlock; | ||
91 | p2 << err.r; | ||
92 | @@ | ||
93 | |||
94 | msg = "preceding lock on line %s" % (p[0].line) | ||
95 | coccilib.report.print_report(p2[0],msg) | ||