xref: /linux/scripts/coccinelle/locks/mini_lock.cocci (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
17f904d7eSThomas Gleixner// SPDX-License-Identifier: GPL-2.0-only
254c05628SNicolas Palix/// Find missing unlocks.  This semantic match considers the specific case
354c05628SNicolas Palix/// where the unlock is missing from an if branch, and there is a lock
454c05628SNicolas Palix/// before the if and an unlock after the if.  False positives are due to
554c05628SNicolas Palix/// cases where the if branch represents a case where the function is
654c05628SNicolas Palix/// supposed to exit with the lock held, or where there is some preceding
754c05628SNicolas Palix/// function call that releases the lock.
854c05628SNicolas Palix///
954c05628SNicolas Palix// Confidence: Moderate
107f904d7eSThomas Gleixner// Copyright: (C) 2010-2012 Nicolas Palix.
117f904d7eSThomas Gleixner// Copyright: (C) 2010-2012 Julia Lawall, INRIA/LIP6.
127f904d7eSThomas Gleixner// Copyright: (C) 2010-2012 Gilles Muller, INRIA/LiP6.
13*f01701ceSJulia Lawall// URL: https://coccinelle.gitlabpages.inria.fr/website
1454c05628SNicolas Palix// Comments:
1593f14468SNicolas Palix// Options: --no-includes --include-headers
1654c05628SNicolas Palix
1729a36d4dSJulia Lawallvirtual context
1854c05628SNicolas Palixvirtual org
1954c05628SNicolas Palixvirtual report
2054c05628SNicolas Palix
2154c05628SNicolas Palix@prelocked@
2254c05628SNicolas Palixposition p1,p;
2354c05628SNicolas Palixexpression E1;
2454c05628SNicolas Palix@@
2554c05628SNicolas Palix
2654c05628SNicolas Palix(
2754c05628SNicolas Palixmutex_lock@p1
2854c05628SNicolas Palix|
2954c05628SNicolas Palixmutex_trylock@p1
3054c05628SNicolas Palix|
3154c05628SNicolas Palixspin_lock@p1
3254c05628SNicolas Palix|
3354c05628SNicolas Palixspin_trylock@p1
3454c05628SNicolas Palix|
3554c05628SNicolas Palixread_lock@p1
3654c05628SNicolas Palix|
3754c05628SNicolas Palixread_trylock@p1
3854c05628SNicolas Palix|
3954c05628SNicolas Palixwrite_lock@p1
4054c05628SNicolas Palix|
4154c05628SNicolas Palixwrite_trylock@p1
4254c05628SNicolas Palix|
4354c05628SNicolas Palixread_lock_irq@p1
4454c05628SNicolas Palix|
4554c05628SNicolas Palixwrite_lock_irq@p1
4654c05628SNicolas Palix|
4754c05628SNicolas Palixread_lock_irqsave@p1
4854c05628SNicolas Palix|
4954c05628SNicolas Palixwrite_lock_irqsave@p1
5054c05628SNicolas Palix|
5154c05628SNicolas Palixspin_lock_irq@p1
5254c05628SNicolas Palix|
5354c05628SNicolas Palixspin_lock_irqsave@p1
5454c05628SNicolas Palix) (E1@p,...);
5554c05628SNicolas Palix
5654c05628SNicolas Palix@looped@
5754c05628SNicolas Palixposition r;
5854c05628SNicolas Palix@@
5954c05628SNicolas Palix
6054c05628SNicolas Palixfor(...;...;...) { <+... return@r ...; ...+> }
6154c05628SNicolas Palix
6229a36d4dSJulia Lawall@err exists@
6354c05628SNicolas Palixexpression E1;
6454c05628SNicolas Palixposition prelocked.p;
6554c05628SNicolas Palixposition up != prelocked.p1;
6654c05628SNicolas Palixposition r!=looped.r;
6754c05628SNicolas Palixidentifier lock,unlock;
6854c05628SNicolas Palix@@
6954c05628SNicolas Palix
7029a36d4dSJulia Lawall*lock(E1@p,...);
7132d0572aSJulia Lawall... when != E1
7232d0572aSJulia Lawall    when any
7354c05628SNicolas Palixif (...) {
7454c05628SNicolas Palix  ... when != E1
7529a36d4dSJulia Lawall*  return@r ...;
7654c05628SNicolas Palix}
7732d0572aSJulia Lawall... when != E1
7832d0572aSJulia Lawall    when any
7929a36d4dSJulia Lawall*unlock@up(E1,...);
8054c05628SNicolas Palix
8154c05628SNicolas Palix@script:python depends on org@
8254c05628SNicolas Palixp << prelocked.p1;
8354c05628SNicolas Palixlock << err.lock;
8454c05628SNicolas Palixunlock << err.unlock;
8554c05628SNicolas Palixp2 << err.r;
8654c05628SNicolas Palix@@
8754c05628SNicolas Palix
8854c05628SNicolas Palixcocci.print_main(lock,p)
8954c05628SNicolas Palixcocci.print_secs(unlock,p2)
9054c05628SNicolas Palix
9154c05628SNicolas Palix@script:python depends on report@
9254c05628SNicolas Palixp << prelocked.p1;
9354c05628SNicolas Palixlock << err.lock;
9454c05628SNicolas Palixunlock << err.unlock;
9554c05628SNicolas Palixp2 << err.r;
9654c05628SNicolas Palix@@
9754c05628SNicolas Palix
9854c05628SNicolas Palixmsg = "preceding lock on line %s" % (p[0].line)
9954c05628SNicolas Palixcoccilib.report.print_report(p2[0],msg)
100