cocci.inria.fr archive mirror
 help / color / mirror / Atom feed
* [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
@ 2022-09-16  6:19 Yuan Can
  2022-09-16  6:19 ` [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Yuan Can @ 2022-09-16  6:19 UTC (permalink / raw)
  To: Julia.Lawall, nicolas.palix, cocci; +Cc: yuancan

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

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

diff --git a/scripts/coccinelle/locks/missing_mutex_init.cocci b/scripts/coccinelle/locks/missing_mutex_init.cocci
new file mode 100644
index 000000000000..bec8ce60d0c5
--- /dev/null
+++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
@@ -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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script
  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
  2022-09-16 14:43 ` [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Markus Elfring
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Yuan Can @ 2022-09-16  6:19 UTC (permalink / raw)
  To: Julia.Lawall, nicolas.palix, cocci; +Cc: yuancan

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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
  2022-09-16  6:19 [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
  2022-09-16  6:19 ` [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
@ 2022-09-16 14:43 ` Markus Elfring
  2022-09-17  6:40 ` Markus Elfring
  2022-09-18 19:39 ` Julia Lawall
  3 siblings, 0 replies; 8+ messages in thread
From: Markus Elfring @ 2022-09-16 14:43 UTC (permalink / raw)
  To: Yuan Can; +Cc: cocci, Nicolas Palix


…

> +++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
> +// Comments:


Can the specification of any key words become more helpful
instead of leaving such a comment field empty?



> +@r3@
> +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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
  2022-09-16  6:19 [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
  2022-09-16  6:19 ` [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
  2022-09-16 14:43 ` [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Markus Elfring
@ 2022-09-17  6:40 ` Markus Elfring
  2022-09-18 19:39 ` Julia Lawall
  3 siblings, 0 replies; 8+ messages in thread
From: Markus Elfring @ 2022-09-17  6:40 UTC (permalink / raw)
  To: Yuan Can, cocci; +Cc: Julia Lawall, Nicolas Palix, kernel-janitors


> 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


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
  2022-09-16  6:19 [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
                   ` (2 preceding siblings ...)
  2022-09-17  6:40 ` Markus Elfring
@ 2022-09-18 19:39 ` Julia Lawall
  2022-09-19  7:55   ` Yuan Can
  3 siblings, 1 reply; 8+ messages in thread
From: Julia Lawall @ 2022-09-18 19:39 UTC (permalink / raw)
  To: Yuan Can; +Cc: nicolas.palix, cocci

On Fri, 16 Sep 2022, Yuan Can wrote:

> 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

>
> Signed-off-by: Yuan Can <yuancan@huawei.com>
> ---
>  .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++
>  1 file changed, 68 insertions(+)
>  create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci
>
> diff --git a/scripts/coccinelle/locks/missing_mutex_init.cocci b/scripts/coccinelle/locks/missing_mutex_init.cocci
> new file mode 100644
> index 000000000000..bec8ce60d0c5
> --- /dev/null
> +++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
> @@ -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
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
  2022-09-18 19:39 ` Julia Lawall
@ 2022-09-19  7:55   ` Yuan Can
  2022-09-19  8:01     ` Julia Lawall
  0 siblings, 1 reply; 8+ messages in thread
From: Yuan Can @ 2022-09-19  7:55 UTC (permalink / raw)
  To: Julia Lawall; +Cc: nicolas.palix, cocci


在 2022/9/19 3:39, Julia Lawall 写道:
> On Fri, 16 Sep 2022, Yuan Can wrote:
>
>> 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.

> 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?).

>
> 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;
>    ...
> };
Ok, thanks for the explanation.
>
> 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

>
> thanks,
> julia
>
>> Signed-off-by: Yuan Can <yuancan@huawei.com>
>> ---
>>   .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++
>>   1 file changed, 68 insertions(+)
>>   create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci
>>
>> diff --git a/scripts/coccinelle/locks/missing_mutex_init.cocci b/scripts/coccinelle/locks/missing_mutex_init.cocci
>> new file mode 100644
>> index 000000000000..bec8ce60d0c5
>> --- /dev/null
>> +++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
>> @@ -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
>>
>>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
  2022-09-19  7:55   ` Yuan Can
@ 2022-09-19  8:01     ` Julia Lawall
  2022-09-19  8:10       ` Yuan Can
  0 siblings, 1 reply; 8+ messages in thread
From: Julia Lawall @ 2022-09-19  8:01 UTC (permalink / raw)
  To: Yuan Can; +Cc: nicolas.palix, cocci

> > 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;

> > 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

>
> Best regards,
>
> Yuan Can
>
> >
> > thanks,
> > julia
> >
> > > Signed-off-by: Yuan Can <yuancan@huawei.com>
> > > ---
> > >   .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++
> > >   1 file changed, 68 insertions(+)
> > >   create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci
> > >
> > > diff --git a/scripts/coccinelle/locks/missing_mutex_init.cocci
> > > b/scripts/coccinelle/locks/missing_mutex_init.cocci
> > > new file mode 100644
> > > index 000000000000..bec8ce60d0c5
> > > --- /dev/null
> > > +++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
> > > @@ -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
> > >
> > >
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script
  2022-09-19  8:01     ` Julia Lawall
@ 2022-09-19  8:10       ` Yuan Can
  0 siblings, 0 replies; 8+ messages in thread
From: Yuan Can @ 2022-09-19  8:10 UTC (permalink / raw)
  To: Julia Lawall; +Cc: nicolas.palix, cocci


在 2022/9/19 16:01, Julia Lawall 写道:
>>> 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;

Yes, that is exactly what I want, thank you.

Best regards,

Yuan Can

>
>>> 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
>
>> Best regards,
>>
>> Yuan Can
>>
>>> thanks,
>>> julia
>>>
>>>> Signed-off-by: Yuan Can <yuancan@huawei.com>
>>>> ---
>>>>    .../coccinelle/locks/missing_mutex_init.cocci | 68 +++++++++++++++++++
>>>>    1 file changed, 68 insertions(+)
>>>>    create mode 100644 scripts/coccinelle/locks/missing_mutex_init.cocci
>>>>
>>>> diff --git a/scripts/coccinelle/locks/missing_mutex_init.cocci
>>>> b/scripts/coccinelle/locks/missing_mutex_init.cocci
>>>> new file mode 100644
>>>> index 000000000000..bec8ce60d0c5
>>>> --- /dev/null
>>>> +++ b/scripts/coccinelle/locks/missing_mutex_init.cocci
>>>> @@ -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
>>>>
>>>>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-09-19  8:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-16  6:19 [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script Yuan Can
2022-09-16  6:19 ` [cocci] [PATCH 2/2] coccinelle: locks: add missing_spin_lock_init.cocci script Yuan Can
2022-09-16 14:43 ` [cocci] [PATCH 1/2] coccinelle: locks: add missing_mutex_init.cocci script 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

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 NNTP newsgroup(s).