^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /// Find functions that refer to GFP_KERNEL but are called with locks held.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) //# The proposed change of converting the GFP_KERNEL is not necessarily the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) //# correct one. It may be desired to unlock the lock, or to not call the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) //# function under the lock in the first place.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) ///
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) // Confidence: Moderate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) // Copyright: (C) 2012 Nicolas Palix.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) // Copyright: (C) 2012 Julia Lawall, INRIA/LIP6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) // Copyright: (C) 2012 Gilles Muller, INRIA/LiP6.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) // URL: http://coccinelle.lip6.fr/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) // Comments:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) // Options: --no-includes --include-headers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) virtual patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) virtual context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) virtual org
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) virtual report
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) @gfp exists@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) identifier fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) position p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) fn(...) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) ... when != read_unlock_irq(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) when != write_unlock_irq(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) when != read_unlock_irqrestore(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) when != write_unlock_irqrestore(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) when != spin_unlock(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) when != spin_unlock_irq(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) when != spin_unlock_irqrestore(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) when != local_irq_enable(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) when any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) GFP_KERNEL@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) ... when any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) @locked exists@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) identifier gfp.fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) position p1,p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) (
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) read_lock_irq@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) write_lock_irq@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) read_lock_irqsave@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) write_lock_irqsave@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) spin_lock@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) spin_trylock@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) spin_lock_irq@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) spin_lock_irqsave@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) local_irq_disable@p1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) )
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) (...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ... when != read_unlock_irq(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) when != write_unlock_irq(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) when != read_unlock_irqrestore(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) when != write_unlock_irqrestore(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) when != spin_unlock(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) when != spin_unlock_irq(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) when != spin_unlock_irqrestore(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) when != local_irq_enable(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) fn@p2(...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) @depends on locked && patch@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) position gfp.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) - GFP_KERNEL@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) + GFP_ATOMIC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) @depends on locked && !patch@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) position gfp.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * GFP_KERNEL@p
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) @script:python depends on !patch && org@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) p << gfp.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) fn << gfp.fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) p1 << locked.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) p2 << locked.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) cocci.print_main("lock",p1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) cocci.print_secs("call",p2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) cocci.print_secs("GFP_KERNEL",p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) @script:python depends on !patch && report@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) p << gfp.p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) fn << gfp.fn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) p1 << locked.p1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) p2 << locked.p2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) @@
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) msg = "ERROR: function %s called on line %s inside lock on line %s but uses GFP_KERNEL" % (fn,p2[0].line,p1[0].line)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) coccilib.report.print_report(p[0], msg)