Re: [Cocci] cocci script to convert linux-kernel allocs with BITS_TO_LONGS to bitmap_alloc

2021-07-14 Thread Julia Lawall



On Tue, 13 Jul 2021, Joe Perches wrote:

> On Tue, 2021-07-13 at 23:33 +0200, Julia Lawall wrote:
> > > > On Fri, 9 Jul 2021, Joe Perches wrote:
> > > > > Here is a cocci script to convert various types of bitmap allocations
> > > > > that use BITS_TO_LONGS to the more typical bitmap_alloc functions.
> >
> > I see that there is also a bitmap_free.  Maybe the rule should be
> > introducing that as well?
>
> Yes, but as far as I know, it's difficult for coccinelle to convert
> the kfree() calls of any previous bitmap_alloc to bitmap_free as
> most frequently the kfree() call is in a separate function.

Often the code says a->b = foo(); and then the a->b in another function is
the same one that was the result of foo().  One could check that this is
the only assignment to a->b in the file for more confidence.

I'll add it to the rule and see how it goes.

julia

>
> Please do it if you know how, you're probably the best in the world
> at coccinelle.  I don't know how...
>
> cheers, Joe
>
>
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] cocci script to convert linux-kernel allocs with BITS_TO_LONGS to bitmap_alloc

2021-07-13 Thread Joe Perches
On Tue, 2021-07-13 at 23:33 +0200, Julia Lawall wrote:
> > > On Fri, 9 Jul 2021, Joe Perches wrote:
> > > > Here is a cocci script to convert various types of bitmap allocations
> > > > that use BITS_TO_LONGS to the more typical bitmap_alloc functions.
> 
> I see that there is also a bitmap_free.  Maybe the rule should be
> introducing that as well?

Yes, but as far as I know, it's difficult for coccinelle to convert
the kfree() calls of any previous bitmap_alloc to bitmap_free as
most frequently the kfree() call is in a separate function.

Please do it if you know how, you're probably the best in the world
at coccinelle.  I don't know how...

cheers, Joe

___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] cocci script to convert linux-kernel allocs with BITS_TO_LONGS to bitmap_alloc

2021-07-13 Thread Julia Lawall



On Sat, 10 Jul 2021, Joe Perches wrote:

> On Sat, 2021-07-10 at 21:50 +0200, Julia Lawall wrote:
> > On Fri, 9 Jul 2021, Joe Perches wrote:
> >
> > > Here is a cocci script to convert various types of bitmap allocations
> > > that use BITS_TO_LONGS to the more typical bitmap_alloc functions.

I see that there is also a bitmap_free.  Maybe the rule should be
introducing that as well?

julia

> > >
> > > Perhaps something like it could be added to scripts/coccinelle.
> > > The diff produced by the script is also below.
> > >
> > > $ cat bitmap_allocs.cocci
> > > // typical uses of bitmap allocations
> []
> > > @@
> > > expression val;
> > > expression e1;
> > > expression e2;
> > > @@
> > >
> > > - val = kcalloc(BITS_TO_LONGS(e1), sizeof(*val), e2)
> > > + val = bitmap_zalloc(e1, e2)
> >
> > Is there something that guarantees that val has a type that has a size that
> > is the same as a long?
>
> no, but afaict, all do.
>
>
>
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] cocci script to convert linux-kernel allocs with BITS_TO_LONGS to bitmap_alloc

2021-07-10 Thread Julia Lawall



On Sat, 10 Jul 2021, Joe Perches wrote:

> On Sat, 2021-07-10 at 21:50 +0200, Julia Lawall wrote:
> > On Fri, 9 Jul 2021, Joe Perches wrote:
> >
> > > Here is a cocci script to convert various types of bitmap allocations
> > > that use BITS_TO_LONGS to the more typical bitmap_alloc functions.
> > >
> > > Perhaps something like it could be added to scripts/coccinelle.
> > > The diff produced by the script is also below.
> > >
> > > $ cat bitmap_allocs.cocci
> > > // typical uses of bitmap allocations
> []
> > > @@
> > > expression val;
> > > expression e1;
> > > expression e2;
> > > @@
> > >
> > > - val = kcalloc(BITS_TO_LONGS(e1), sizeof(*val), e2)
> > > + val = bitmap_zalloc(e1, e2)
> >
> > Is there something that guarantees that val has a type that has a size that
> > is the same as a long?
>
> no, but afaict, all do.

It might be nicer for the val metavariable to be declared as
{long,unsigned long} val;, although that might lose some results if the
type is something else that has the same size.  I can check what is the
impact of adding that constraint.

julia
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] cocci script to convert linux-kernel allocs with BITS_TO_LONGS to bitmap_alloc

2021-07-10 Thread Joe Perches
On Sat, 2021-07-10 at 21:50 +0200, Julia Lawall wrote:
> On Fri, 9 Jul 2021, Joe Perches wrote:
> 
> > Here is a cocci script to convert various types of bitmap allocations
> > that use BITS_TO_LONGS to the more typical bitmap_alloc functions.
> > 
> > Perhaps something like it could be added to scripts/coccinelle.
> > The diff produced by the script is also below.
> > 
> > $ cat bitmap_allocs.cocci
> > // typical uses of bitmap allocations
[]
> > @@
> > expression val;
> > expression e1;
> > expression e2;
> > @@
> > 
> > -   val = kcalloc(BITS_TO_LONGS(e1), sizeof(*val), e2)
> > +   val = bitmap_zalloc(e1, e2)
> 
> Is there something that guarantees that val has a type that has a size that
> is the same as a long?

no, but afaict, all do.


___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


Re: [Cocci] cocci script to convert linux-kernel allocs with BITS_TO_LONGS to bitmap_alloc

2021-07-10 Thread Julia Lawall



On Fri, 9 Jul 2021, Joe Perches wrote:

> Here is a cocci script to convert various types of bitmap allocations
> that use BITS_TO_LONGS to the more typical bitmap_alloc functions.
>
> Perhaps something like it could be added to scripts/coccinelle.
> The diff produced by the script is also below.
>
> $ cat bitmap_allocs.cocci
> // typical uses of bitmap allocations
>
> @@
> expression val;
> expression e1;
> expression e2;
> @@
>
> - val = kcalloc(BITS_TO_LONGS(e1), sizeof(\(long\|unsigned long\)), e2)
> + val = bitmap_zalloc(e1, e2)
>
> @@
> expression val;
> expression e1;
> expression e2;
> @@
>
> - val = kcalloc(BITS_TO_LONGS(e1), sizeof(*val), e2)
> + val = bitmap_zalloc(e1, e2)

Is there something that guarantees that val has a type that has a size that
is the same as a long?

julia
___
Cocci mailing list
Cocci@systeme.lip6.fr
https://systeme.lip6.fr/mailman/listinfo/cocci


[Cocci] cocci script to convert linux-kernel allocs with BITS_TO_LONGS to bitmap_alloc

2021-07-10 Thread Joe Perches
Here is a cocci script to convert various types of bitmap allocations
that use BITS_TO_LONGS to the more typical bitmap_alloc functions.

Perhaps something like it could be added to scripts/coccinelle.
The diff produced by the script is also below.

$ cat bitmap_allocs.cocci
// typical uses of bitmap allocations

@@
expression val;
expression e1;
expression e2;
@@

-   val = kcalloc(BITS_TO_LONGS(e1), sizeof(\(long\|unsigned long\)), e2)
+   val = bitmap_zalloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kcalloc(BITS_TO_LONGS(e1), sizeof(*val), e2)
+   val = bitmap_zalloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kmalloc_array(BITS_TO_LONGS(e1), sizeof(\(long\|unsigned long\)), 
e2)
+   val = bitmap_alloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kmalloc_array(BITS_TO_LONGS(e1), sizeof(*val), e2)
+   val = bitmap_alloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kmalloc(BITS_TO_LONGS(e1), sizeof(\(long\|unsigned long\)), e2)
+   val = bitmap_alloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kmalloc(BITS_TO_LONGS(e1), sizeof(*val), e2)
+   val = bitmap_alloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kmalloc(BITS_TO_LONGS(e1) * sizeof(\(long\|unsigned long\)), e2)
+   val = bitmap_alloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kmalloc(BITS_TO_LONGS(e1) * sizeof(*val), e2)
+   val = bitmap_alloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kzalloc(BITS_TO_LONGS(e1) * sizeof(\(long\|unsigned long\)), e2)
+   val = bitmap_zalloc(e1, e2)

@@
expression val;
expression e1;
expression e2;
@@

-   val = kzalloc(BITS_TO_LONGS(e1) * sizeof(*val), e2)
+   val = bitmap_zalloc(e1, e2)

// devm_ uses of bitmap allocations

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kcalloc(dev, BITS_TO_LONGS(e1), sizeof(\(long\|unsigned 
long\)), e2)
+   val = devm_bitmap_zalloc(dev, e1, e2)

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kcalloc(dev, BITS_TO_LONGS(e1), sizeof(*val), e2)
+   val = devm_bitmap_zalloc(dev, e1, e2)

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kmalloc_array(dev, BITS_TO_LONGS(e1), 
sizeof(\(long\|unsigned long\)), e2)
+   val = devm_bitmap_alloc(dev, e1, e2)

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kmalloc_array(dev, BITS_TO_LONGS(e1), sizeof(*val), e2)
+   val = devm_bitmap_alloc(dev, e1, e2)

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kmalloc(dev, BITS_TO_LONGS(e1) * sizeof(\(long\|unsigned 
long\)), e2)
+   val = devm_bitmap_alloc(dev, e1, e2)

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kmalloc(dev, BITS_TO_LONGS(e1) * sizeof(*val), e2)
+   val = devm_bitmap_alloc(dev, e1, e2)

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kzalloc(dev, BITS_TO_LONGS(e1) * sizeof(\(long\|unsigned 
long\)), e2)
+   val = devm_bitmap_zalloc(dev, e1, e2)

@@
expression dev;
expression val;
expression e1;
expression e2;
@@

-   val = devm_kzalloc(dev, BITS_TO_LONGS(e1) * sizeof(*val), e2)
+   val = devm_bitmap_zalloc(dev, e1, e2)

$

This cocci script produces this diff against next-20210709

$ spatch -sp-file bitmap_allocs.cocci . --in-place

$ git diff --stat -p
 arch/csky/mm/asid.c|  3 +-
 arch/mips/math-emu/dsemul.c|  6 +--
 arch/mips/mm/context.c |  3 +-
 arch/powerpc/kvm/book3s_hv_uvmem.c |  3 +-
 arch/s390/pci/pci.c|  3 +-
 drivers/atm/he.c   |  4 +-
 drivers/base/regmap/regcache-rbtree.c  |  4 +-
 drivers/block/xen-blkfront.c   |  3 +-
 drivers/crypto/hisilicon/zip/zip_crypto.c  |  3 +-
 drivers/crypto/marvell/octeontx/otx_cptpf_ucode.c  |  5 +--
 .../crypto/marvell/octeontx2/otx2_cptpf_ucode.c|  5 +--
 drivers/cxl/pci.c  |  6 +--
 drivers/dma/stm32-dmamux.c |  6 +--
 drivers/dma/ti/dma-crossbar.c  |  5 +--
 drivers/dma/ti/edma.c  |  8 ++--
 drivers/dma/ti/k3-udma.c   | 46 +++---
 drivers/firmware/arm_scmi/driver.c |  4 +-
 drivers/firmware/ti_sci.c  | 11 ++
 drivers/gpu/drm/vc4/vc4_validate_shaders.c |  5 +--
 drivers/gpu/host1x/channel.c   |  4 +-
 drivers/hid/hid-multitouch.c   |  7 ++--
 drivers/infiniband/hw/cxgb4/id_table.c