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