Coccinelle archive mirror
 help / color / mirror / Atom feed
From: Yuan Can <yuancan@huawei.com>
To: <Julia.Lawall@inria.fr>, <nicolas.palix@imag.fr>, <cocci@inria.fr>
Cc: <yuancan@huawei.com>
Subject: [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script
Date: Fri, 16 Sep 2022 06:19:23 +0000	[thread overview]
Message-ID: <20220916061923.29759-2-yuancan@huawei.com> (raw)
In-Reply-To: <20220916061923.29759-1-yuancan@huawei.com>

Find spin lock used without init, and point out the
place the spin lock is allocated.

Signed-off-by: Yuan Can <yuancan@huawei.com>
---
 .../locks/missing_spin_lock_init.cocci        | 71 +++++++++++++++++++
 1 file changed, 71 insertions(+)
 create mode 100644 scripts/coccinelle/locks/missing_spin_lock_init.cocci

diff --git a/scripts/coccinelle/locks/missing_spin_lock_init.cocci b/scripts/coccinelle/locks/missing_spin_lock_init.cocci
new file mode 100644
index 000000000000..1447f9fe9b16
--- /dev/null
+++ b/scripts/coccinelle/locks/missing_spin_lock_init.cocci
@@ -0,0 +1,71 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// report missing spin_lock_init()
+///
+/// Report spin lock used without initialize. False positives
+/// can occur when the spin lock allocation and initialization
+/// happens in two different files.
+///
+// Copyright: (C) 2022 Huawei Technologies Co, Ltd.
+// Comments:
+// Options: --include-headers
+
+virtual org
+virtual report
+
+@r1@
+identifier s, fld;
+struct s *mm;
+@@
+  spin_lock_init(\(&mm->fld\|&(mm->fld)\))
+
+@r2@
+identifier r1.s, r1.fld;
+position p;
+@@
+
+struct s {
+  ...
+  spinlock_t fld@p;
+  ...
+};
+
+@r3@
+identifier s, fld;
+position p != {r2.p};
+@@
+
+struct s {
+  ...
+  spinlock_t fld@p;
+  ...
+};
+
+@r4@
+identifier r3.fld;
+identifier r3.s;
+struct s *mm;
+@@
+(
+ \(spin_lock\|spin_lock_bh\|spin_trylock\|spin_lock_irq\)(&mm->fld)
+|
+ spin_lock_irqsave(&mm->fld,...)
+)
+
+@r5 depends on r4@
+identifier r3.s;
+struct s *mm;
+position p;
+@@
+* mm@p = \(kmalloc\|kzalloc\|devm_kmalloc\|devm_kzalloc\)(...)
+
+@script:python depends on org@
+p << r5.p;
+@@
+cocci.print_main("Spin lock inside the struct malloced here is possibly used without init.", p)
+
+@script:python depends on report@
+p << r5.p;
+@@
+
+msg = "Spin lock inside the struct malloced here is possibly used without init."
+coccilib.report.print_report(p[0], msg)
-- 
2.17.1


  reply	other threads:[~2022-09-16  9:34 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-16  6:19 [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
2022-09-16  6:19 ` Yuan Can [this message]
2022-09-16 14:43 ` Markus Elfring
2022-09-17  6:40 ` Markus Elfring
2022-09-18 19:39 ` Julia Lawall
2022-09-19  7:55   ` Yuan Can
2022-09-19  8:01     ` Julia Lawall
2022-09-19  8:10       ` Yuan Can

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220916061923.29759-2-yuancan@huawei.com \
    --to=yuancan@huawei.com \
    --cc=Julia.Lawall@inria.fr \
    --cc=cocci@inria.fr \
    --cc=nicolas.palix@imag.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).