coccinelle: locks: add missing_mutex_init.cocci script
PATCH 1/2 coccinelle: locks: add missing_mutex_init.cocci script
Find mutex used without init, and point out the
place the mutex is allocated.
--- .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci
scripts/coccinelle/locks/missing_mutex_init.cocci new file +68 -0
@@ -0,0 +1,68 @@ +// 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. +/// +// 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; +@@ + +mutex_lock(&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("Mutex inside the struct malloced here is possibly used without init.", p) + +@script:python depends on report@ +p << r5.p; +@@ + +msg = "Mutex inside the struct malloced here is possibly used without init." +coccilib.report.print_report(p[0], msg)
Signature · 1 line
2.17.1
PATCH 2/2 coccinelle: locks: add missing_spin_lock_init.cocci script
Find spin lock used without init, and point out the place the spin lock is allocated.
--- .../locks/missing_spin_lock_init.cocci | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 scripts/coccinelle/locks/missing_spin_lock_init.cocci
scripts/coccinelle/locks/missing_spin_lock_init.cocci new file +71 -0
@@ -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)
Signature · 1 line
2.17.1
…
Quoted 1 line
+++
b/scripts/coccinelle/locks/missing_mutex_init.cocci
…
Quoted 1 line
+// Comments:
Can the specification of any key words become more helpful instead of leaving such a comment field empty?
scripts/coccinelle/locks/missing_mutex_init.cocci line 41
Quoted 10 lines
+@r3@
scripts/coccinelle/locks/missing_mutex_init.cocci new file +9
-0+identifier s, fld; +position p != {r2.p}; +@@ + +struct s { + ... + struct mutex fld@p; + ... +};
Why would you like to search for another mutex?
How many mutexes will matter for the proposed source code search pattern?
Regards,
Markus
Quoted 3 lines
Find mutex used without init, and point out the
place the mutex is allocated.
I find this commit message improvable.
Would you get into the development mood to generalise and extend the source code search pattern besides proposed adjustments for the handling of spin locks?
How many data structures would need further initialisations after some memory was allocated?
Regards,
Markus
Julia Lawall
maintainerQuoted 2 lines · Yuan Can
Find mutex used without init, and point out the
place the mutex is allocated.
Thank for the semantic patches.
Traditionally, the semantic patches in the Linux kernel have a Confidence:
tag above the Coyright: line. I would suggest to put such a line in this case, based on your experience. From looking at a few results with the mutex semantic patch (I didn't get any results with the spin lock semantic patch, is that normal?), I would suggest a Low confidence. The cases I looked at seemed to involve calls across different files, and Coccinelle is looking at only one file at a time.
I would also suggest to print out more information, to help the user better undestand where the problem may be. Specifically, it would be nice to have the name of the lock field, the position of the declaration of this field, and the position of the call to mutex_lock. You already have the relevant metavariables in r3, and can add a position metavariable in r4.
Finally, you have a * at the beginning of the matched line in r5, so the rule seems to support the context mode. But if you want to support the context mode and to highlight all of the mentioned places, you would have to duplicate r3 and r4 with a depends on for r5, so that the context is only shown when all of the conditions are satisfied. For example, you could have:
@dupr3 depends on r5@
identifier s, fld;
position r3.p;
@@
struct s {
...
* struct mutex fld@p;
...
};
Are there some patches in the Linux kernel that are based on these rules?
thanks,
julia
Quoted 85 lines
Signed-off-by Yuan Can <[email protected]>--- .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocciscripts/coccinelle/locks/missing_mutex_init.cocci new file +68
-1@@ -0,0 +1,68 @@ +// 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. +/// +// 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; +@@ + +mutex_lock(&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("Mutex inside the struct malloced here is possibly used without init.", p) + +@script:python depends on report@ +p << r5.p; +@@ + +msg = "Mutex inside the struct malloced here is possibly used without init." +coccilib.report.print_report(p[0], msg) --2.17.1
Quoted 11 lines · Julia Lawall
Quoted 2 lines · Yuan Can
Find mutex used without init, and point out the
place the mutex is allocated.Thank for the semantic patches.
Traditionally, the semantic patches in the Linux kernel have a Confidence:
tag above the Coyright: line. I would suggest to put such a line in this case, based on your experience. From looking at a few results with the mutex semantic patch (I didn't get any results with the spin lock semantic patch, is that normal?), I would suggest a Low confidence. The cases I
Thanks for your suggestion, I will add Confidence tag in the v2 patch.
About the spin lock semantic, by excuting "make coccicheck COCCI=scripts/coccinelle/locks/missing_spin_lock_init.cocci MODE=report",
I get the following output:
12419 files match
./drivers/net/ethernet/mellanox/mlx4/en_ethtool.c:1851:1-4: Spin lock inside the struct malloced here is possibly used without init. ./drivers/net/ethernet/mellanox/mlx4/en_ethtool.c:1180:1-4: Spin lock inside the struct malloced here is possibly used without init. ./fs/ext4/super.c:4292:1-4: Spin lock inside the struct malloced here is possibly used without init. ./drivers/net/wan/fsl_ucc_hdlc.c:1172:1-11: Spin lock inside the struct malloced here is possibly used without init. ./drivers/usb/cdns3/cdnsp-pci.c:95:2-7: Spin lock inside the struct malloced here is possibly used without init. ./fs/cifs/smb2pdu.c:3854:1-5: Spin lock inside the struct malloced here is possibly used without init. ./drivers/net/ethernet/broadcom/bnxt/bnxt.c:7618:2-5: Spin lock inside the struct malloced here is possibly used without init. ./drivers/clk/x86/clk-cgu.c:592:2-6: Spin lock inside the struct malloced here is possibly used without init. ./drivers/clk/x86/clk-cgu.c:228:1-4: Spin lock inside the struct malloced here is possibly used without init. ./drivers/clk/x86/clk-cgu.c:346:1-5: Spin lock inside the struct malloced here is possibly used without init. ./drivers/clk/x86/clk-cgu.c:103:1-4: Spin lock inside the struct malloced here is possibly used without init. ./drivers/gpu/drm/amd/amdkfd/kfd_device.c:426:1-4: Spin lock inside the struct malloced here is possibly used without init. ./drivers/clk/x86/clk-cgu-pll.c:119:1-4: Spin lock inside the struct malloced here is possibly used without init. ./drivers/scsi/aacraid/dpcsup.c:187:7-10: Spin lock inside the struct malloced here is possibly used without init. ./drivers/scsi/aacraid/dpcsup.c:287:9-12: Spin lock inside the struct malloced here is possibly used without init. ./drivers/usb/cdns3/cdns3-plat.c:60:1-5: Spin lock inside the struct malloced here is possibly used without init. ./drivers/media/platform/renesas/vsp1/vsp1_video.c:667:2-6: Spin lock inside the struct malloced here is possibly used without init. ./drivers/net/wireless/marvell/mwifiex/main.c:83:2-18: Spin lock inside the struct malloced here is possibly used without init. ./drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:1268:1-14: Spin lock inside the struct malloced here is possibly used without init. ./drivers/mtd/ubi/build.c:896:1-4: Spin lock inside the struct malloced here is possibly used without init. ./drivers/misc/cxl/guest.c:1110:7-21: Spin lock inside the struct malloced here is possibly used without init. ./drivers/vfio/pci/mlx5/main.c:607:1-6: Spin lock inside the struct malloced here is possibly used without init.
Quoted 9 lines
looked at seemed to involve calls across different files, and Coccinelle is looking at only one file at a time.
I would also suggest to print out more information, to help the user better undestand where the problem may be. Specifically, it would be nice to have the name of the lock field, the position of the declaration of this field, and the position of the call to
mutex_lock. You already have the relevant metavariables in r3, and can add a position metavariable in r4.
Agreed, but how could I fetch the name of the lock field? I cannot find a doc describes how
to achieve this(or did I miss it?).
Quoted 13 lines
Finally, you have a * at the beginning of the matched line in r5, so the rule seems to support the context mode. But if you want to support the context mode and to highlight all of the mentioned places, you would have to duplicate r3 and r4 with a depends on for r5, so that the context is only shown when all of the conditions are satisfied. For example, you could have:
@dupr3 depends on r5@
identifier s, fld;
position r3.p;
@@
Quoted 5 lines
struct s {
...
* struct mutex fld@p;
...
};Ok, thanks for the explanation.
Quoted 2 lines
Are there some patches in the Linux kernel that are based on these rules?
Yes, there are many patches based on the two rules.
Best regards,
Yuan Can
Quoted 88 lines
thanks,
juliaQuoted 84 lines
Signed-off-by Yuan Can <[email protected]>--- .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+)create mode 100644
scripts/coccinelle/locks/missing_mutex_init.cocciscripts/coccinelle/locks/missing_mutex_init.cocci new file +68
-1@@ -0,0 +1,68 @@ +// 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. +/// +// 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; +@@ + +mutex_lock(&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("Mutex inside the struct malloced here is possibly used without init.", p) + +@script:python depends on report@ +p << r5.p; +@@ + +msg = "Mutex inside the struct malloced here is possibly used without init." +coccilib.report.print_report(p[0], msg) --2.17.1
Julia Lawall
maintainerQuoted 11 lines
Quoted 6 lines
I would also suggest to print out more information, to help the user better undestand where the problem may be. Specifically, it would be nice to have the name of the lock field, the position of the declaration of this field, and the position of the call to
mutex_lock. You already have the relevant metavariables in r3, and can add a position metavariable in r4.Agreed, but how could I fetch the name of the lock field? I cannot find a doc describes how
to achieve this(or did I miss it?).
r3.fld. r3.s could be useful also.
Perhaps your question is about how to reference them from within python? Actually, any metavaraible can be imported into the python script, not just a potision variable. So you could write
s << r3.s;
Quoted 3 lines
Quoted 1 line
Are there some patches in the Linux kernel that are based on these rules?
Yes, there are many patches based on the two rules.
OK, great, thanks.
julia
Quoted 97 lines
Best regards,
Yuan Can
Quoted 91 lines
thanks,
juliaQuoted 87 lines
Signed-off-by Yuan Can <[email protected]>--- .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+)create mode 100644
scripts/coccinelle/locks/missing_mutex_init.cocciscripts/coccinelle/locks/missing_mutex_init.cocci new file +61
-0@@ -0,0 +1,68 @@ +// 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. +/// +// 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; +@@ + +mutex_lock(&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("Mutex inside the struct malloced here is possibly usedwithout init.", p)
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +@script:python depends on report@ +p << r5.p; +@@ + +msg = "Mutex inside the struct malloced here is possibly used withoutinit."
+coccilib.report.print_report(p[0], msg)
--
2.17.1
Quoted 9 lines · Julia Lawall
Quoted 9 lines
Quoted 6 lines
I would also suggest to print out more information, to help the user better undestand where the problem may be. Specifically, it would be nice to have the name of the lock field, the position of the declaration of this field, and the position of the call to
mutex_lock. You already have the relevant metavariables in r3, and can add a position metavariable in r4.Agreed, but how could I fetch the name of the lock field? I cannot find a doc describes how
Quoted 8 lines
Quoted 1 line
to achieve this(or did I miss it?).
r3.fld. r3.s could be useful also.
Perhaps your question is about how to reference them from within python? Actually, any metavaraible can be imported into the python script, not just a potision variable. So you could write
s << r3.s;
Yes, that is exactly what I want, thank you.
Best regards,
Yuan Can
Quoted 101 lines
Quoted 3 lines
Quoted 2 lines
Are there some patches in the Linux kernel that are based on these rules?
Yes, there are many patches based on the two rules.
OK, great, thanks.
julia
Quoted 94 lines
Best regards,
Yuan Can
Quoted 90 lines
thanks,
juliaQuoted 87 lines
Signed-off-by Yuan Can <[email protected]>--- .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+)create mode 100644
scripts/coccinelle/locks/missing_mutex_init.cocciscripts/coccinelle/locks/missing_mutex_init.cocci new file +61
-0@@ -0,0 +1,68 @@ +// 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. +/// +// 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; +@@ + +mutex_lock(&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("Mutex inside the struct malloced here is possibly usedwithout init.", p)
scripts/coccinelle/locks/missing_mutex_init.cocci new file +6
-0+ +@script:python depends on report@ +p << r5.p; +@@ + +msg = "Mutex inside the struct malloced here is possibly used withoutinit."
+coccilib.report.print_report(p[0], msg)
--
2.17.1