coccinelle: locks: add missing_mutex_init.cocci script
Subsystems (2)
PATCH v2 1/2 coccinelle: locks: add missing_mutex_init.cocci script
Find mutex inside struct which is possibly used without init, provide the name of the struct and of the mutex, the position where the struct is malloced and where the mutex get locked.
---
changes in v2:
- adjust commit msg
- add Confidence: tag
- print out more information in the report
--- .../coccinelle/locks/missing_mutex_init.cocci | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci
scripts/coccinelle/locks/missing_mutex_init.cocci new file +76 -0
@@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0-only +/// report missing mutex_init() +/// +/// Report mutex used without initialize. False positives can occur +/// when the mutex 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; +@@ +mutex_init(\(&mm->fld\|&(mm->fld)\)) + +@r2@ +identifier r1.s, r1.fld; +position p; +@@ + +struct s { + ... + struct mutex fld@p; + ... +}; + +@r3@ +identifier s, fld; +position p != {r2.p}; +@@ + +struct s { + ... + struct mutex fld@p; + ... +}; + +@r4@ +identifier r3.fld; +identifier r3.s; +struct s *mm; +position p; +@@ + +mutex_lock@p(&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; +p1 << r4.p; +s << r3.s; +fld << r3.fld; +@@ +msg = "Mutex %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 = "Mutex %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)
Signature · 1 line
2.17.1
PATCH v2 2/2 coccinelle: locks: add missing_spin_lock_init.cocci script
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.
---
changes in v2:
- adjust commit msg
- add Confidence: tag
- print out more information in the report
--- .../locks/missing_spin_lock_init.cocci | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 scripts/coccinelle/locks/missing_spin_lock_init.cocci
scripts/coccinelle/locks/missing_spin_lock_init.cocci new file +79 -0
@@ -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\|&(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)
Signature · 1 line
2.17.1
Quoted 4 lines
Find mutex inside struct which is possibly used without init, provide the name of the struct and of the mutex, the position where the struct is malloced and where the mutex get locked.
I find this commit message variant also improvable.
Will terms like “data structure” and “initialisation” become relevant?
I would appreciate answers to my previous questions.
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2022-09/msg00022.html
I would expect a cover letter for patch series.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.0-rc6#n321
…
Quoted 1 line
+++
b/scripts/coccinelle/locks/missing_mutex_init.cocci
…
Quoted 1 line
+// Comments:
Why do you suggest the addition of an empty comment field?
Quoted 1 line
+mutex_init(\(&mm->fld\|&(mm->fld)\))
An extra SmPL disjunction is probably unnecessary because of an isomorphism.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/3f7496ff9c2c5d4fadae1e585aa458e1a0037972/standard.iso#L382
https://github.com/coccinelle/coccinelle/blob/19ee1697bf152d37a78a20cefe148775bf4b0e0d/standard.iso#L382
+mutex_init(&(mm->fld))
…
Review
Quoted 3 lines
+@r3@
(quoted diff) +2
-0+identifier s, fld; +position p != {r2.p};
Why do you think that such a SmPL constraint would be required?
scripts/coccinelle/locks/missing_mutex_init.cocci line 31
Quoted 7 lines
+@@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +struct s { + ... + struct mutex fld@p; + ... +};
Why would the source code search repetition matter here?
…
scripts/coccinelle/locks/missing_mutex_init.cocci line 58
Quoted 6 lines
+@r5 depends on r4@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +5
-0+identifier r3.s; +struct s *mm; +position p; +@@ +* mm@p = \(kmalloc\|kzalloc\|devm_kmalloc\|devm_kzalloc\)(...)
I would expect that the usage of the asterisk in the first column should belong to the operation mode “context”.
Will it become relevant to detect any more memory allocation function calls?
Regards,
Markus
Julia Lawall
maintainerQuoted 6 lines
Quoted 1 line
+mutex_init(\(&mm->fld\|&(mm->fld)\))
An extra SmPL disjunction is probably unnecessary because of an isomorphism.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/3f7496ff9c2c5d4fadae1e585aa458e1a0037972/standard.iso#L382
https://github.com/coccinelle/coccinelle/blob/19ee1697bf152d37a78a20cefe148775bf4b0e0d/standard.iso#L382
This is correct. It would be better to only have the version with the parentheses, because Coccinelle will consider the case without parentheses anyway.
Quoted 16 lines
+mutex_init(&(mm->fld))
…
Review
Quoted 3 lines
+@r3@
(quoted diff) +2
-0+identifier s, fld; +position p != {r2.p};Why do you think that such a SmPL constraint would be required?
Quoted 10 lines
scripts/coccinelle/locks/missing_mutex_init.cocci line 31
Quoted 7 lines
+@@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +struct s { + ... + struct mutex fld@p; + ... +};Why would the source code search repetition matter here?
He is searching for a structure that is different from the ones matched previously.
Quoted 4 lines
…
Quoted 10 lines
scripts/coccinelle/locks/missing_mutex_init.cocci line 58
Quoted 6 lines
+@r5 depends on r4@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +5
-0+identifier r3.s; +struct s *mm; +position p; +@@ +* mm@p = \(kmalloc\|kzalloc\|devm_kmalloc\|devm_kzalloc\)(...)I would expect that the usage of the asterisk in the first column should belong to the operation mode “context”.
This is correct. Either the context mode should be fully supported or the asterisk should be removed.
julia
Quoted 16 lines
Quoted 7 lines
Review
Quoted 3 lines
+@r3@
(quoted diff) +2
-0+identifier s, fld; +position p != {r2.p};Why do you think that such a SmPL constraint would be required?
Quoted 9 lines
scripts/coccinelle/locks/missing_mutex_init.cocci line 31
Quoted 7 lines
+@@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +struct s { + ... + struct mutex fld@p; + ... +};Why would the source code search repetition matter here?
Quoted 1 line
He is searching for a structure that is different from the ones matched previously.
How many mutexes (or spin locks) should be initialised before further data processing can be safely performed with corresponding structures?
Regards,
Markus
Quoted 14 lines · Markus Elfring
Quoted 14 lines
Quoted 14 lines
Review
Quoted 3 lines
+@r3@
(quoted diff) +2
-0+identifier s, fld; +position p != {r2.p};Why do you think that such a SmPL constraint would be required?
scripts/coccinelle/locks/missing_mutex_init.cocci line 31
Quoted 7 lines
+@@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +struct s { + ... + struct mutex fld@p; + ... +};Why would the source code search repetition matter here?
Quoted 3 lines
Quoted 1 line
He is searching for a structure that is different from the ones matched previously.
How many mutexes (or spin locks) should be initialised before further data processing can be safely performed with corresponding structures?
In my opinion, every mutexes and spin locks needs to be initialised before use.
Best regards,
Yuan Can
Julia Lawall
maintainerQuoted 16 lines · Yuan Can
Quoted 14 lines · Markus Elfring
Quoted 14 lines
Quoted 14 lines
Review
Quoted 3 lines
+@r3@
(quoted diff) +2
-0+identifier s, fld; +position p != {r2.p};Why do you think that such a SmPL constraint would be required?
scripts/coccinelle/locks/missing_mutex_init.cocci line 31
Quoted 7 lines
+@@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +struct s { + ... + struct mutex fld@p; + ... +};Why would the source code search repetition matter here?
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
He is searching for a structure that is different from the ones matched previously.
How many mutexes (or spin locks) should be initialised before further data processing can be safely performed with corresponding structures?
In my opinion, every mutexes and spin locks needs to be initialised before use.
I think that the concern is that you require that some spinlock is initialized in the file before you are able to find one that is not. It could be good to check this with an artificial test case.
julia
Quoted 8 lines · Julia Lawall
Quoted 5 lines
Quoted 1 line
+mutex_init(\(&mm->fld\|&(mm->fld)\))
An extra SmPL disjunction is probably unnecessary because of an isomorphism.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/3f7496ff9c2c5d4fadae1e585aa458e1a0037972/standard.iso#L382
https://github.com/coccinelle/coccinelle/blob/19ee1697bf152d37a78a20cefe148775bf4b0e0d/standard.iso#L382This is correct. It would be better to only have the version with the parentheses, because Coccinelle will consider the case without parentheses anyway.
I see, I will former one in the next version.
Quoted 39 lines
Quoted 15 lines
+mutex_init(&(mm->fld))
…
Review
Quoted 3 lines
+@r3@
(quoted diff) +2
-0+identifier s, fld; +position p != {r2.p};Why do you think that such a SmPL constraint would be required?
Quoted 9 lines
scripts/coccinelle/locks/missing_mutex_init.cocci line 31
Quoted 7 lines
+@@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +struct s { + ... + struct mutex fld@p; + ... +};Why would the source code search repetition matter here?
He is searching for a structure that is different from the ones matched previously.
Quoted 12 lines
…
scripts/coccinelle/locks/missing_mutex_init.cocci line 58
Quoted 6 lines
+@r5 depends on r4@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +5
-0+identifier r3.s; +struct s *mm; +position p; +@@ +* mm@p = \(kmalloc\|kzalloc\|devm_kmalloc\|devm_kzalloc\)(...)I would expect that the usage of the asterisk in the first column should belong to the operation mode “context”.
Quoted 2 lines
This is correct. Either the context mode should be fully supported or the asterisk should be removed.
Ok, I do not mean to support context mode, the asterisk will be remove in the next version.
Thanks for your reply.
Best regards,
Yuan Can
Quoted 9 lines · Markus Elfring
Quoted 3 lines
Find mutex inside struct which is possibly used without init, provide the name of the struct and of the mutex, the position where the struct is malloced and where the mutex get locked.
I find this commit message variant also improvable.
Will terms like “data structure” and “initialisation” become relevant?
Quoted 7 lines
I would appreciate answers to my previous questions.
https://lore.kernel.org/cocci/[email protected]/
https://sympa.inria.fr/sympa/arc/cocci/2022-09/msg00022.htmlI would expect a cover letter for patch series.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.0-rc6#n321
Thanks for the advice, I will send with cover letter in the next version.
Quoted 14 lines
…
Quoted 1 line
+++
b/scripts/coccinelle/locks/missing_mutex_init.cocci…
Quoted 1 line
+// Comments:
Why do you suggest the addition of an empty comment field?
Quoted 8 lines
Quoted 1 line
+mutex_init(\(&mm->fld\|&(mm->fld)\))
An extra SmPL disjunction is probably unnecessary because of an isomorphism.
https://gitlab.inria.fr/coccinelle/coccinelle/-/blob/3f7496ff9c2c5d4fadae1e585aa458e1a0037972/standard.iso#L382
https://github.com/coccinelle/coccinelle/blob/19ee1697bf152d37a78a20cefe148775bf4b0e0d/standard.iso#L382+mutex_init(&(mm->fld))
Thanks for pointing out this, I will remove the former one in the next version.
Quoted 36 lines
…
Review
Quoted 3 lines
+@r3@
(quoted diff) +2
-0+identifier s, fld; +position p != {r2.p};Why do you think that such a SmPL constraint would be required?
scripts/coccinelle/locks/missing_mutex_init.cocci line 31
Quoted 7 lines
+@@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +struct s { + ... + struct mutex fld@p; + ... +};Why would the source code search repetition matter here?
…
scripts/coccinelle/locks/missing_mutex_init.cocci line 58
Quoted 6 lines
+@r5 depends on r4@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +5
-0+identifier r3.s; +struct s *mm; +position p; +@@ +* mm@p = \(kmalloc\|kzalloc\|devm_kmalloc\|devm_kzalloc\)(...)I would expect that the usage of the asterisk in the first column should belong to the operation mode “context”.
Quoted 1 line
Will it become relevant to detect any more memory allocation function calls?
I do not mean to support context mode, I will remove it in the next version.
Thanks for you review!
Best regards,
Yuan Can