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 v3 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script
Date: Thu, 22 Sep 2022 11:55:35 +0000 [thread overview]
Message-ID: <20220922115535.44132-3-yuancan@huawei.com> (raw)
In-Reply-To: <20220922115535.44132-1-yuancan@huawei.com>
Find spin lock inside struct which is possibly used without init,
provide the name of the struct and of the spin lock, the position
where the struct is malloced and where the spin lock get locked.
Signed-off-by: Yuan Can <yuancan@huawei.com>
---
.../locks/missing_spin_lock_init.cocci | 79 +++++++++++++++++++
1 file changed, 79 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..9724d348e28e
--- /dev/null
+++ b/scripts/coccinelle/locks/missing_spin_lock_init.cocci
@@ -0,0 +1,79 @@
+// 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.
+///
+// Confidence: Low
+// 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))
+
+@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;
+position p;
+@@
+(
+ \(spin_lock\|spin_lock_bh\|spin_trylock\|spin_lock_irq\)(&mm->fld@p)
+|
+ spin_lock_irqsave(&mm->fld@p,...)
+)
+
+@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;
+p1 << r4.p;
+s << r3.s;
+fld << r3.fld;
+@@
+msg = "Spin lock %s inside the struct %s malloced at line %s is possibly used at line %s without init." % (fld, s, p[0].line, p1[0].line);
+cocci.print_main(msg, p)
+
+@script:python depends on report@
+p << r5.p;
+p1 << r4.p;
+s << r3.s;
+fld << r3.fld;
+@@
+msg = "Spin lock %s inside the struct %s malloced at line %s is possibly used at line %s without init." % (fld, s, p[0].line, p1[0].line);
+coccilib.report.print_report(p[0], msg)
--
2.17.1
next prev parent reply other threads:[~2022-09-22 12:28 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-22 11:55 [cocci] [PATCH v3 0/2] coccinelle: Introduce cocci script to detect missing mutext and spin lock initialization Yuan Can
2022-09-22 11:55 ` [cocci] [PATCH v3 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
2022-09-22 18:11 ` Markus Elfring
2022-09-23 14:21 ` Julia Lawall
2022-09-23 17:02 ` [cocci] [v3 " Markus Elfring
2022-09-23 17:08 ` Julia Lawall
2022-09-23 17:47 ` Markus Elfring
2022-09-23 19:27 ` Julia Lawall
2022-09-24 8:15 ` Markus Elfring
2022-09-24 8:36 ` [cocci] [PATCH v3 " Markus Elfring
2022-09-24 15:40 ` Markus Elfring
2022-09-25 20:33 ` Markus Elfring
2022-09-22 11:55 ` Yuan Can [this message]
2022-09-22 17:19 ` [cocci] [PATCH v3 0/2] coccinelle: Introduce cocci script to detect missing mutext and spin lock initialization Markus Elfring
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=20220922115535.44132-3-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.