2017-09-21 06:43:35

by Thomas Meyer

[permalink] [raw]
Subject: [PATCH 0/4] Cocci spatch "alloc_cast" - v4.14-rc1

Remove casting the values returned by memory allocation functions like
kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."

Found by coccinelle spatch "api/alloc/alloc_cast.cocci"

Run against version v4.14-rc1

Let me know when you as a maintainer are not interested in these kind of patches.
I can exclude you by path; e.g. all findings in "drivers/scsi" will never
be reported again by this semi-automatic program runs.


2017-09-21 06:42:59

by Thomas Meyer

[permalink] [raw]
Subject: [PATCH 4/4] ALSA: emu10k1: Cocci spatch "alloc_cast"

Remove casting the values returned by memory allocation functions like
kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
Found by coccinelle spatch "api/alloc/alloc_cast.cocci"

Signed-off-by: Thomas Meyer <[email protected]>
---

diff -u -p a/sound/pci/emu10k1/emufx.c b/sound/pci/emu10k1/emufx.c
--- a/sound/pci/emu10k1/emufx.c
+++ b/sound/pci/emu10k1/emufx.c
@@ -1218,7 +1218,7 @@ static int _snd_emu10k1_audigy_init_efx(
if (!icode)
return err;

- icode->gpr_map = (u_int32_t __user *) kcalloc(512 + 256 + 256 + 2 * 1024,
+ icode->gpr_map = kcalloc(512 + 256 + 256 + 2 * 1024,
sizeof(u_int32_t), GFP_KERNEL);
if (!icode->gpr_map)
goto __err_gpr;
@@ -1853,7 +1853,7 @@ static int _snd_emu10k1_init_efx(struct
if (!icode)
return err;

- icode->gpr_map = (u_int32_t __user *) kcalloc(256 + 160 + 160 + 2 * 512,
+ icode->gpr_map = kcalloc(256 + 160 + 160 + 2 * 512,
sizeof(u_int32_t), GFP_KERNEL);
if (!icode->gpr_map)
goto __err_gpr;

2017-09-21 06:43:11

by Thomas Meyer

[permalink] [raw]
Subject: [PATCH 2/4] fs: Cocci spatch "alloc_cast"

Remove casting the values returned by memory allocation functions like
kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
Found by coccinelle spatch "api/alloc/alloc_cast.cocci"

Signed-off-by: Thomas Meyer <[email protected]>
---

diff -u -p a/fs/9p/vfs_inode.c b/fs/9p/vfs_inode.c
--- a/fs/9p/vfs_inode.c
+++ b/fs/9p/vfs_inode.c
@@ -238,7 +238,7 @@ v9fs_blank_wstat(struct p9_wstat *wstat)
struct inode *v9fs_alloc_inode(struct super_block *sb)
{
struct v9fs_inode *v9inode;
- v9inode = (struct v9fs_inode *)kmem_cache_alloc(v9fs_inode_cache,
+ v9inode = kmem_cache_alloc(v9fs_inode_cache,
GFP_KERNEL);
if (!v9inode)
return NULL;

2017-09-21 06:43:33

by Thomas Meyer

[permalink] [raw]
Subject: [PATCH 3/4] VFS: normal filesystems (and lustre): Cocci spatch "alloc_cast"

Remove casting the values returned by memory allocation functions like
kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
Found by coccinelle spatch "api/alloc/alloc_cast.cocci"

Signed-off-by: Thomas Meyer <[email protected]>
---

diff -u -p a/fs/ncpfs/inode.c b/fs/ncpfs/inode.c
--- a/fs/ncpfs/inode.c
+++ b/fs/ncpfs/inode.c
@@ -53,7 +53,7 @@ static struct kmem_cache * ncp_inode_cac
static struct inode *ncp_alloc_inode(struct super_block *sb)
{
struct ncp_inode_info *ei;
- ei = (struct ncp_inode_info *)kmem_cache_alloc(ncp_inode_cachep, GFP_KERNEL);
+ ei = kmem_cache_alloc(ncp_inode_cachep, GFP_KERNEL);
if (!ei)
return NULL;
return &ei->vfs_inode;

2017-09-21 06:45:34

by Thomas Meyer

[permalink] [raw]
Subject: [PATCH 1/4] drm/amd/powerplay: Cocci spatch "alloc_cast"

Remove casting the values returned by memory allocation functions like
kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
Found by coccinelle spatch "api/alloc/alloc_cast.cocci"

Signed-off-by: Thomas Meyer <[email protected]>
---

diff -u -p a/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c b/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
--- a/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
+++ b/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
@@ -291,7 +291,7 @@ static int get_mm_clock_voltage_table(
table_size = sizeof(uint32_t) +
sizeof(phm_ppt_v1_mm_clock_voltage_dependency_record) *
mm_dependency_table->ucNumEntries;
- mm_table = (phm_ppt_v1_mm_clock_voltage_dependency_table *)
+ mm_table =
kzalloc(table_size, GFP_KERNEL);

if (!mm_table)
@@ -519,7 +519,7 @@ static int get_socclk_voltage_dependency
sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
clk_dep_table->ucNumEntries;

- clk_table = (phm_ppt_v1_clock_voltage_dependency_table *)
+ clk_table =
kzalloc(table_size, GFP_KERNEL);

if (!clk_table)
@@ -554,7 +554,7 @@ static int get_mclk_voltage_dependency_t
sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
mclk_dep_table->ucNumEntries;

- mclk_table = (phm_ppt_v1_clock_voltage_dependency_table *)
+ mclk_table =
kzalloc(table_size, GFP_KERNEL);

if (!mclk_table)
@@ -596,7 +596,7 @@ static int get_gfxclk_voltage_dependency
sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
clk_dep_table->ucNumEntries;

- clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
+ clk_table =
kzalloc(table_size, GFP_KERNEL);

if (!clk_table)
@@ -663,7 +663,7 @@ static int get_pix_clk_voltage_dependenc
sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
clk_dep_table->ucNumEntries;

- clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
+ clk_table =
kzalloc(table_size, GFP_KERNEL);

if (!clk_table)
@@ -728,7 +728,7 @@ static int get_dcefclk_voltage_dependenc
sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
num_entries;

- clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
+ clk_table =
kzalloc(table_size, GFP_KERNEL);

if (!clk_table)
@@ -772,7 +772,7 @@ static int get_pcie_table(struct pp_hwmg
sizeof(struct phm_ppt_v1_pcie_record) *
atom_pcie_table->ucNumEntries;

- pcie_table = (struct phm_ppt_v1_pcie_table *)
+ pcie_table =
kzalloc(table_size, GFP_KERNEL);

if (!pcie_table)
@@ -1026,7 +1026,7 @@ static int get_vddc_lookup_table(
table_size = sizeof(uint32_t) +
sizeof(phm_ppt_v1_voltage_lookup_record) * max_levels;

- table = (phm_ppt_v1_voltage_lookup_table *)
+ table =
kzalloc(table_size, GFP_KERNEL);

if (NULL == table)

2017-09-21 06:55:38

by Deucher, Alexander

[permalink] [raw]
Subject: RE: [PATCH 1/4] drm/amd/powerplay: Cocci spatch "alloc_cast"

> -----Original Message-----
> From: Thomas Meyer [mailto:[email protected]]
> Sent: Thursday, September 21, 2017 2:34 AM
> To: Deucher, Alexander; Koenig, Christian; [email protected]; amd-
> [email protected]; [email protected]; linux-
> [email protected]
> Subject: [PATCH 1/4] drm/amd/powerplay: Cocci spatch "alloc_cast"
>
> Remove casting the values returned by memory allocation functions like
> kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
> Found by coccinelle spatch "api/alloc/alloc_cast.cocci"
>
> Signed-off-by: Thomas Meyer <[email protected]>
> ---
>
> diff -u -p
> a/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
> b/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
> --- a/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
> +++ b/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
> @@ -291,7 +291,7 @@ static int get_mm_clock_voltage_table(
> table_size = sizeof(uint32_t) +
>
> sizeof(phm_ppt_v1_mm_clock_voltage_dependency_record) *
> mm_dependency_table->ucNumEntries;
> - mm_table = (phm_ppt_v1_mm_clock_voltage_dependency_table
> *)
> + mm_table =
> kzalloc(table_size, GFP_KERNEL);

Please fix up the whitespace here and below.

Alex

>
> if (!mm_table)
> @@ -519,7 +519,7 @@ static int get_socclk_voltage_dependency
>
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> clk_dep_table->ucNumEntries;
>
> - clk_table = (phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -554,7 +554,7 @@ static int get_mclk_voltage_dependency_t
>
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> mclk_dep_table->ucNumEntries;
>
> - mclk_table = (phm_ppt_v1_clock_voltage_dependency_table *)
> + mclk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!mclk_table)
> @@ -596,7 +596,7 @@ static int get_gfxclk_voltage_dependency
>
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> clk_dep_table->ucNumEntries;
>
> - clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -663,7 +663,7 @@ static int get_pix_clk_voltage_dependenc
>
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> clk_dep_table->ucNumEntries;
>
> - clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -728,7 +728,7 @@ static int get_dcefclk_voltage_dependenc
>
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> num_entries;
>
> - clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -772,7 +772,7 @@ static int get_pcie_table(struct pp_hwmg
> sizeof(struct phm_ppt_v1_pcie_record) *
> atom_pcie_table->ucNumEntries;
>
> - pcie_table = (struct phm_ppt_v1_pcie_table *)
> + pcie_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!pcie_table)
> @@ -1026,7 +1026,7 @@ static int get_vddc_lookup_table(
> table_size = sizeof(uint32_t) +
> sizeof(phm_ppt_v1_voltage_lookup_record) *
> max_levels;
>
> - table = (phm_ppt_v1_voltage_lookup_table *)
> + table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (NULL == table)

2017-09-21 08:00:05

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH 4/4] ALSA: emu10k1: Cocci spatch "alloc_cast"

On Thu, 21 Sep 2017 08:33:46 +0200,
Thomas Meyer wrote:
>
> Remove casting the values returned by memory allocation functions like
> kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
> Found by coccinelle spatch "api/alloc/alloc_cast.cocci"
>
> Signed-off-by: Thomas Meyer <[email protected]>

No, this cast is still required because it has __user annotation.
It's not about compiler warning but about sparse.


thanks,

Takashi

2017-09-21 17:39:34

by Al Viro

[permalink] [raw]
Subject: Re: [PATCH 4/4] ALSA: emu10k1: Cocci spatch "alloc_cast"

On Thu, Sep 21, 2017 at 08:33:46AM +0200, Thomas Meyer wrote:
> Remove casting the values returned by memory allocation functions like
> kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
> Found by coccinelle spatch "api/alloc/alloc_cast.cocci"
>
> Signed-off-by: Thomas Meyer <[email protected]>
> ---
>
> diff -u -p a/sound/pci/emu10k1/emufx.c b/sound/pci/emu10k1/emufx.c
> --- a/sound/pci/emu10k1/emufx.c
> +++ b/sound/pci/emu10k1/emufx.c
> @@ -1218,7 +1218,7 @@ static int _snd_emu10k1_audigy_init_efx(
> if (!icode)
> return err;
>
> - icode->gpr_map = (u_int32_t __user *) kcalloc(512 + 256 + 256 + 2 * 1024,
> + icode->gpr_map = kcalloc(512 + 256 + 256 + 2 * 1024,
> sizeof(u_int32_t), GFP_KERNEL);

And _this_ is a wonderful example of the reasons why that kind of
patches is bloody bad. The code you've caught is very obviously smelly -
kcalloc() does *NOT* return a userland pointer. You are whitewashing it;
a major "something weird is going on here" sign is gone (something weird
in that case is hopefully a set_fs(KERNEL_DS) somewhere nearby, and it
is asking for careful review).

FWIW, any patches of that sort anywhere near VFS are very much not welcome.

2017-09-21 20:39:16

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH 4/4] ALSA: emu10k1: Cocci spatch "alloc_cast"

On Thu, 21 Sep 2017 19:39:15 +0200,
Al Viro wrote:
>
> On Thu, Sep 21, 2017 at 08:33:46AM +0200, Thomas Meyer wrote:
> > Remove casting the values returned by memory allocation functions like
> > kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
> > Found by coccinelle spatch "api/alloc/alloc_cast.cocci"
> >
> > Signed-off-by: Thomas Meyer <[email protected]>
> > ---
> >
> > diff -u -p a/sound/pci/emu10k1/emufx.c b/sound/pci/emu10k1/emufx.c
> > --- a/sound/pci/emu10k1/emufx.c
> > +++ b/sound/pci/emu10k1/emufx.c
> > @@ -1218,7 +1218,7 @@ static int _snd_emu10k1_audigy_init_efx(
> > if (!icode)
> > return err;
> >
> > - icode->gpr_map = (u_int32_t __user *) kcalloc(512 + 256 + 256 + 2 * 1024,
> > + icode->gpr_map = kcalloc(512 + 256 + 256 + 2 * 1024,
> > sizeof(u_int32_t), GFP_KERNEL);
>
> And _this_ is a wonderful example of the reasons why that kind of
> patches is bloody bad. The code you've caught is very obviously smelly -
> kcalloc() does *NOT* return a userland pointer. You are whitewashing it;
> a major "something weird is going on here" sign is gone (something weird
> in that case is hopefully a set_fs(KERNEL_DS) somewhere nearby, and it
> is asking for careful review).

Right, the code touched by the patch is a really tricky one. It uses
the same struct that contains __user pointer which is supposed to be
filled by ioctl, but in this case, the function allocates the kernel
buffer and passes it with a special flag mentioning it being a kernel
buffer later.


thanks,

Takashi

2017-09-26 16:10:28

by Alex Deucher

[permalink] [raw]
Subject: Re: [PATCH 1/4] drm/amd/powerplay: Cocci spatch "alloc_cast"

On Thu, Sep 21, 2017 at 2:33 AM, Thomas Meyer <[email protected]> wrote:
> Remove casting the values returned by memory allocation functions like
> kmalloc, kzalloc, kmem_cache_alloc, kmem_cache_zalloc etc."
> Found by coccinelle spatch "api/alloc/alloc_cast.cocci"
>
> Signed-off-by: Thomas Meyer <[email protected]>
> ---
>
> diff -u -p a/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c b/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
> --- a/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
> +++ b/drivers/gpu/drm/amd/powerplay/hwmgr/vega10_processpptables.c
> @@ -291,7 +291,7 @@ static int get_mm_clock_voltage_table(
> table_size = sizeof(uint32_t) +
> sizeof(phm_ppt_v1_mm_clock_voltage_dependency_record) *
> mm_dependency_table->ucNumEntries;
> - mm_table = (phm_ppt_v1_mm_clock_voltage_dependency_table *)
> + mm_table =
> kzalloc(table_size, GFP_KERNEL);

Please fix up the whitespace. E.g.,
mm_table = kzalloc(table_size, GFP_KERNEL);

Alex


>
> if (!mm_table)
> @@ -519,7 +519,7 @@ static int get_socclk_voltage_dependency
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> clk_dep_table->ucNumEntries;
>
> - clk_table = (phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -554,7 +554,7 @@ static int get_mclk_voltage_dependency_t
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> mclk_dep_table->ucNumEntries;
>
> - mclk_table = (phm_ppt_v1_clock_voltage_dependency_table *)
> + mclk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!mclk_table)
> @@ -596,7 +596,7 @@ static int get_gfxclk_voltage_dependency
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> clk_dep_table->ucNumEntries;
>
> - clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -663,7 +663,7 @@ static int get_pix_clk_voltage_dependenc
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> clk_dep_table->ucNumEntries;
>
> - clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -728,7 +728,7 @@ static int get_dcefclk_voltage_dependenc
> sizeof(phm_ppt_v1_clock_voltage_dependency_record) *
> num_entries;
>
> - clk_table = (struct phm_ppt_v1_clock_voltage_dependency_table *)
> + clk_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!clk_table)
> @@ -772,7 +772,7 @@ static int get_pcie_table(struct pp_hwmg
> sizeof(struct phm_ppt_v1_pcie_record) *
> atom_pcie_table->ucNumEntries;
>
> - pcie_table = (struct phm_ppt_v1_pcie_table *)
> + pcie_table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (!pcie_table)
> @@ -1026,7 +1026,7 @@ static int get_vddc_lookup_table(
> table_size = sizeof(uint32_t) +
> sizeof(phm_ppt_v1_voltage_lookup_record) * max_levels;
>
> - table = (phm_ppt_v1_voltage_lookup_table *)
> + table =
> kzalloc(table_size, GFP_KERNEL);
>
> if (NULL == table)
> _______________________________________________
> dri-devel mailing list
> [email protected]
> https://lists.freedesktop.org/mailman/listinfo/dri-devel