xref: /openbmc/linux/scripts/coccinelle/locks/call_kern.cocci (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
17f904d7eSThomas Gleixner// SPDX-License-Identifier: GPL-2.0-only
2e105007cSNicolas Palix/// Find functions that refer to GFP_KERNEL but are called with locks held.
329a36d4dSJulia Lawall//# The proposed change of converting the GFP_KERNEL is not necessarily the
429a36d4dSJulia Lawall//# correct one.  It may be desired to unlock the lock, or to not call the
529a36d4dSJulia Lawall//# function under the lock in the first place.
6e105007cSNicolas Palix///
7e105007cSNicolas Palix// Confidence: Moderate
87f904d7eSThomas Gleixner// Copyright: (C) 2012 Nicolas Palix.
97f904d7eSThomas Gleixner// Copyright: (C) 2012 Julia Lawall, INRIA/LIP6.
107f904d7eSThomas Gleixner// Copyright: (C) 2012 Gilles Muller, INRIA/LiP6.
11*f01701ceSJulia Lawall// URL: https://coccinelle.gitlabpages.inria.fr/website
12e105007cSNicolas Palix// Comments:
1393f14468SNicolas Palix// Options: --no-includes --include-headers
14e105007cSNicolas Palix
15e105007cSNicolas Palixvirtual patch
1629a36d4dSJulia Lawallvirtual context
1729a36d4dSJulia Lawallvirtual org
1829a36d4dSJulia Lawallvirtual report
19e105007cSNicolas Palix
20e105007cSNicolas Palix@gfp exists@
21e105007cSNicolas Palixidentifier fn;
22e105007cSNicolas Palixposition p;
23e105007cSNicolas Palix@@
24e105007cSNicolas Palix
25e105007cSNicolas Palixfn(...) {
26e105007cSNicolas Palix ... when != read_unlock_irq(...)
27e105007cSNicolas Palix     when != write_unlock_irq(...)
28e105007cSNicolas Palix     when != read_unlock_irqrestore(...)
29e105007cSNicolas Palix     when != write_unlock_irqrestore(...)
30e105007cSNicolas Palix     when != spin_unlock(...)
31e105007cSNicolas Palix     when != spin_unlock_irq(...)
32e105007cSNicolas Palix     when != spin_unlock_irqrestore(...)
33e105007cSNicolas Palix     when != local_irq_enable(...)
34e105007cSNicolas Palix     when any
35e105007cSNicolas Palix GFP_KERNEL@p
36e105007cSNicolas Palix ... when any
37e105007cSNicolas Palix}
38e105007cSNicolas Palix
3929a36d4dSJulia Lawall@locked exists@
40e105007cSNicolas Palixidentifier gfp.fn;
4129a36d4dSJulia Lawallposition p1,p2;
42e105007cSNicolas Palix@@
43e105007cSNicolas Palix
44e105007cSNicolas Palix(
4529a36d4dSJulia Lawallread_lock_irq@p1
46e105007cSNicolas Palix|
4729a36d4dSJulia Lawallwrite_lock_irq@p1
48e105007cSNicolas Palix|
4929a36d4dSJulia Lawallread_lock_irqsave@p1
50e105007cSNicolas Palix|
5129a36d4dSJulia Lawallwrite_lock_irqsave@p1
52e105007cSNicolas Palix|
5329a36d4dSJulia Lawallspin_lock@p1
54e105007cSNicolas Palix|
5529a36d4dSJulia Lawallspin_trylock@p1
56e105007cSNicolas Palix|
5729a36d4dSJulia Lawallspin_lock_irq@p1
58e105007cSNicolas Palix|
5929a36d4dSJulia Lawallspin_lock_irqsave@p1
60e105007cSNicolas Palix|
6129a36d4dSJulia Lawalllocal_irq_disable@p1
62e105007cSNicolas Palix)
63e105007cSNicolas Palix (...)
64e105007cSNicolas Palix...  when != read_unlock_irq(...)
65e105007cSNicolas Palix     when != write_unlock_irq(...)
66e105007cSNicolas Palix     when != read_unlock_irqrestore(...)
67e105007cSNicolas Palix     when != write_unlock_irqrestore(...)
68e105007cSNicolas Palix     when != spin_unlock(...)
69e105007cSNicolas Palix     when != spin_unlock_irq(...)
70e105007cSNicolas Palix     when != spin_unlock_irqrestore(...)
71e105007cSNicolas Palix     when != local_irq_enable(...)
7229a36d4dSJulia Lawallfn@p2(...)
73e105007cSNicolas Palix
7429a36d4dSJulia Lawall@depends on locked && patch@
75e105007cSNicolas Palixposition gfp.p;
76e105007cSNicolas Palix@@
77e105007cSNicolas Palix
78e105007cSNicolas Palix- GFP_KERNEL@p
79e105007cSNicolas Palix+ GFP_ATOMIC
8029a36d4dSJulia Lawall
8129a36d4dSJulia Lawall@depends on locked && !patch@
8229a36d4dSJulia Lawallposition gfp.p;
8329a36d4dSJulia Lawall@@
8429a36d4dSJulia Lawall
8529a36d4dSJulia Lawall* GFP_KERNEL@p
8629a36d4dSJulia Lawall
8729a36d4dSJulia Lawall@script:python depends on !patch && org@
8829a36d4dSJulia Lawallp << gfp.p;
8929a36d4dSJulia Lawallfn << gfp.fn;
9029a36d4dSJulia Lawallp1 << locked.p1;
9129a36d4dSJulia Lawallp2 << locked.p2;
9229a36d4dSJulia Lawall@@
9329a36d4dSJulia Lawall
9429a36d4dSJulia Lawallcocci.print_main("lock",p1)
9529a36d4dSJulia Lawallcocci.print_secs("call",p2)
9629a36d4dSJulia Lawallcocci.print_secs("GFP_KERNEL",p)
9729a36d4dSJulia Lawall
9829a36d4dSJulia Lawall@script:python depends on !patch && report@
9929a36d4dSJulia Lawallp << gfp.p;
10029a36d4dSJulia Lawallfn << gfp.fn;
10129a36d4dSJulia Lawallp1 << locked.p1;
10229a36d4dSJulia Lawallp2 << locked.p2;
10329a36d4dSJulia Lawall@@
10429a36d4dSJulia Lawall
10529a36d4dSJulia Lawallmsg = "ERROR: function %s called on line %s inside lock on line %s but uses GFP_KERNEL" % (fn,p2[0].line,p1[0].line)
10629a36d4dSJulia Lawallcoccilib.report.print_report(p[0], msg)
107