2022-03-12 11:18:05

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 0/6] use kzalloc

Use kzalloc instead of kmalloc + memset.

---

drivers/net/ethernet/mellanox/mlx4/en_rx.c | 3 +--
drivers/net/wireless/zydas/zd1201.c | 3 +--
drivers/scsi/lpfc/lpfc_debugfs.c | 9 ++-------
drivers/usb/gadget/legacy/raw_gadget.c | 3 +--
fs/cifs/transport.c | 3 +--
sound/core/seq/oss/seq_oss_init.c | 3 +--
6 files changed, 7 insertions(+), 17 deletions(-)


2022-03-12 11:18:05

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 4/6] scsi: lpfc: use kzalloc

Use kzalloc instead of kmalloc + memset.

The semantic patch that makes this change is:
(https://coccinelle.gitlabpages.inria.fr/website/)

//<smpl>
@@
expression res, size, flag;
@@
- res = kmalloc(size, flag);
+ res = kzalloc(size, flag);
...
- memset(res, 0, size);
//</smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
drivers/scsi/lpfc/lpfc_debugfs.c | 9 ++-------
1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/lpfc/lpfc_debugfs.c b/drivers/scsi/lpfc/lpfc_debugfs.c
index 30fac2f6fb06..7c4a71703065 100644
--- a/drivers/scsi/lpfc/lpfc_debugfs.c
+++ b/drivers/scsi/lpfc/lpfc_debugfs.c
@@ -6272,10 +6272,8 @@ lpfc_debugfs_initialize(struct lpfc_vport *vport)
phba->hba_debugfs_root,
phba, &lpfc_debugfs_op_slow_ring_trc);
if (!phba->slow_ring_trc) {
- phba->slow_ring_trc = kmalloc(
- (sizeof(struct lpfc_debugfs_trc) *
- lpfc_debugfs_max_slow_ring_trc),
- GFP_KERNEL);
+ phba->slow_ring_trc = kzalloc((sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_slow_ring_trc),
+ GFP_KERNEL);
if (!phba->slow_ring_trc) {
lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
"0416 Cannot create debugfs "
@@ -6283,9 +6281,6 @@ lpfc_debugfs_initialize(struct lpfc_vport *vport)
goto debug_failed;
}
atomic_set(&phba->slow_ring_trc_cnt, 0);
- memset(phba->slow_ring_trc, 0,
- (sizeof(struct lpfc_debugfs_trc) *
- lpfc_debugfs_max_slow_ring_trc));
}

snprintf(name, sizeof(name), "nvmeio_trc");

2022-03-12 11:40:19

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 1/6] cifs: use kzalloc

Use kzalloc instead of kmalloc + memset.

The semantic patch that makes this change is:
(https://coccinelle.gitlabpages.inria.fr/website/)

//<smpl>
@@
expression res, size, flag;
@@
- res = kmalloc(size, flag);
+ res = kzalloc(size, flag);
...
- memset(res, 0, size);
//</smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
fs/cifs/transport.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fs/cifs/transport.c b/fs/cifs/transport.c
index eeb1a699bd6f..4ff8e165a180 100644
--- a/fs/cifs/transport.c
+++ b/fs/cifs/transport.c
@@ -464,13 +464,12 @@ smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
return -EIO;
}

- tr_hdr = kmalloc(sizeof(*tr_hdr), GFP_NOFS);
+ tr_hdr = kzalloc(sizeof(*tr_hdr), GFP_NOFS);
if (!tr_hdr)
return -ENOMEM;

memset(&cur_rqst[0], 0, sizeof(cur_rqst));
memset(&iov, 0, sizeof(iov));
- memset(tr_hdr, 0, sizeof(*tr_hdr));

iov.iov_base = tr_hdr;
iov.iov_len = sizeof(*tr_hdr);

2022-03-12 11:40:19

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 5/6] zd1201: use kzalloc

Use kzalloc instead of kmalloc + memset.

The semantic patch that makes this change is:
(https://coccinelle.gitlabpages.inria.fr/website/)

//<smpl>
@@
expression res, size, flag;
@@
- res = kmalloc(size, flag);
+ res = kzalloc(size, flag);
...
- memset(res, 0, size);
//</smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
drivers/net/wireless/zydas/zd1201.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/net/wireless/zydas/zd1201.c b/drivers/net/wireless/zydas/zd1201.c
index e64e4e579518..82bc0d44212e 100644
--- a/drivers/net/wireless/zydas/zd1201.c
+++ b/drivers/net/wireless/zydas/zd1201.c
@@ -521,7 +521,7 @@ static int zd1201_setconfig(struct zd1201 *zd, int rid, const void *buf, int len
zd->rxdatas = 0;
zd->rxlen = 0;
for (seq=0; len > 0; seq++) {
- request = kmalloc(16, gfp_mask);
+ request = kzalloc(16, gfp_mask);
if (!request)
return -ENOMEM;
urb = usb_alloc_urb(0, gfp_mask);
@@ -529,7 +529,6 @@ static int zd1201_setconfig(struct zd1201 *zd, int rid, const void *buf, int len
kfree(request);
return -ENOMEM;
}
- memset(request, 0, 16);
reqlen = len>12 ? 12 : len;
request[0] = ZD1201_USB_RESREQ;
request[1] = seq;

2022-03-12 13:31:24

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 2/6] net/mlx4_en: use kzalloc

Use kzalloc instead of kmalloc + memset.

The semantic patch that makes this change is:
(https://coccinelle.gitlabpages.inria.fr/website/)

//<smpl>
@@
expression res, size, flag;
@@
- res = kmalloc(size, flag);
+ res = kzalloc(size, flag);
...
- memset(res, 0, size);
//</smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
drivers/net/ethernet/mellanox/mlx4/en_rx.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/mellanox/mlx4/en_rx.c b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
index 8cfc649f226b..8f762fc170b3 100644
--- a/drivers/net/ethernet/mellanox/mlx4/en_rx.c
+++ b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
@@ -1067,7 +1067,7 @@ static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
struct mlx4_qp_context *context;
int err = 0;

- context = kmalloc(sizeof(*context), GFP_KERNEL);
+ context = kzalloc(sizeof(*context), GFP_KERNEL);
if (!context)
return -ENOMEM;

@@ -1078,7 +1078,6 @@ static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
}
qp->event = mlx4_en_sqp_event;

- memset(context, 0, sizeof(*context));
mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
qpn, ring->cqn, -1, context);
context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);

2022-03-12 15:50:01

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 3/6] ALSA: seq: oss: use kzalloc

Use kzalloc instead of kmalloc + memset.

The semantic patch that makes this change is:
(https://coccinelle.gitlabpages.inria.fr/website/)

//<smpl>
@@
expression res, size, flag;
@@
- res = kmalloc(size, flag);
+ res = kzalloc(size, flag);
...
- memset(res, 0, size);
//</smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
sound/core/seq/oss/seq_oss_init.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/sound/core/seq/oss/seq_oss_init.c b/sound/core/seq/oss/seq_oss_init.c
index 0ee4a5081fd6..04a3376a6e66 100644
--- a/sound/core/seq/oss/seq_oss_init.c
+++ b/sound/core/seq/oss/seq_oss_init.c
@@ -66,7 +66,7 @@ snd_seq_oss_create_client(void)
struct snd_seq_port_info *port;
struct snd_seq_port_callback port_callback;

- port = kmalloc(sizeof(*port), GFP_KERNEL);
+ port = kzalloc(sizeof(*port), GFP_KERNEL);
if (!port) {
rc = -ENOMEM;
goto __error;
@@ -81,7 +81,6 @@ snd_seq_oss_create_client(void)
system_client = rc;

/* create annoucement receiver port */
- memset(port, 0, sizeof(*port));
strcpy(port->name, "Receiver");
port->addr.client = system_client;
port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* receive only */

2022-03-12 23:41:29

by Julia Lawall

[permalink] [raw]
Subject: [PATCH 6/6] usb: raw-gadget: use kzalloc

Use kzalloc instead of kmalloc + memset.

The semantic patch that makes this change is:
(https://coccinelle.gitlabpages.inria.fr/website/)

//<smpl>
@@
expression res, size, flag;
@@
- res = kmalloc(size, flag);
+ res = kzalloc(size, flag);
...
- memset(res, 0, size);
//</smpl>

Signed-off-by: Julia Lawall <[email protected]>

---
drivers/usb/gadget/legacy/raw_gadget.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/legacy/raw_gadget.c b/drivers/usb/gadget/legacy/raw_gadget.c
index d86c3a36441e..135e75b891f6 100644
--- a/drivers/usb/gadget/legacy/raw_gadget.c
+++ b/drivers/usb/gadget/legacy/raw_gadget.c
@@ -1157,7 +1157,7 @@ static int raw_ioctl_eps_info(struct raw_dev *dev, unsigned long value)
struct usb_raw_eps_info *info;
struct raw_ep *ep;

- info = kmalloc(sizeof(*info), GFP_KERNEL);
+ info = kzalloc(sizeof(*info), GFP_KERNEL);
if (!info) {
ret = -ENOMEM;
goto out;
@@ -1177,7 +1177,6 @@ static int raw_ioctl_eps_info(struct raw_dev *dev, unsigned long value)
goto out_free;
}

- memset(info, 0, sizeof(*info));
for (i = 0; i < dev->eps_num; i++) {
ep = &dev->eps[i];
strscpy(&info->eps[i].name[0], ep->ep->name,

2022-03-13 11:11:54

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH 3/6] ALSA: seq: oss: use kzalloc

On Sat, 2022-03-12 at 11:27 +0100, Julia Lawall wrote:
> Use kzalloc instead of kmalloc + memset.
[]
> diff --git a/sound/core/seq/oss/seq_oss_init.c b/sound/core/seq/oss/seq_oss_init.c
[]
> @@ -81,7 +81,6 @@ snd_seq_oss_create_client(void)
> system_client = rc;
>
> /* create annoucement receiver port */

unrelated trivia: typo of announcement above

> - memset(port, 0, sizeof(*port));
> strcpy(port->name, "Receiver");
> port->addr.client = system_client;
> port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* receive only */
>


2022-03-13 14:00:51

by Joe Perches

[permalink] [raw]
Subject: Re: [PATCH 4/6] scsi: lpfc: use kzalloc

On Sat, 2022-03-12 at 11:27 +0100, Julia Lawall wrote:
> Use kzalloc instead of kmalloc + memset.
[]
> diff --git a/drivers/scsi/lpfc/lpfc_debugfs.c b/drivers/scsi/lpfc/lpfc_debugfs.c
[]
> @@ -6272,10 +6272,8 @@ lpfc_debugfs_initialize(struct lpfc_vport *vport)
> phba->hba_debugfs_root,
> phba, &lpfc_debugfs_op_slow_ring_trc);
> if (!phba->slow_ring_trc) {
> - phba->slow_ring_trc = kmalloc(
> - (sizeof(struct lpfc_debugfs_trc) *
> - lpfc_debugfs_max_slow_ring_trc),
> - GFP_KERNEL);
> + phba->slow_ring_trc = kzalloc((sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_slow_ring_trc),
> + GFP_KERNEL);

kcalloc


2022-03-14 11:09:32

by Takashi Iwai

[permalink] [raw]
Subject: Re: [PATCH 3/6] ALSA: seq: oss: use kzalloc

On Sat, 12 Mar 2022 11:27:02 +0100,
Julia Lawall wrote:
>
> Use kzalloc instead of kmalloc + memset.
>
> The semantic patch that makes this change is:
> (https://coccinelle.gitlabpages.inria.fr/website/)
>
> //<smpl>
> @@
> expression res, size, flag;
> @@
> - res = kmalloc(size, flag);
> + res = kzalloc(size, flag);
> ...
> - memset(res, 0, size);
> //</smpl>
>
> Signed-off-by: Julia Lawall <[email protected]>

Applied, thanks.


Takashi

2022-03-14 14:47:42

by Julia Lawall

[permalink] [raw]
Subject: Re: [PATCH 4/6] scsi: lpfc: use kzalloc



On Mon, 14 Mar 2022, Dan Carpenter wrote:

> On Sat, Mar 12, 2022 at 01:45:01PM -0800, Joe Perches wrote:
> > On Sat, 2022-03-12 at 11:27 +0100, Julia Lawall wrote:
> > > Use kzalloc instead of kmalloc + memset.
> > []
> > > diff --git a/drivers/scsi/lpfc/lpfc_debugfs.c b/drivers/scsi/lpfc/lpfc_debugfs.c
> > []
> > > @@ -6272,10 +6272,8 @@ lpfc_debugfs_initialize(struct lpfc_vport *vport)
> > > phba->hba_debugfs_root,
> > > phba, &lpfc_debugfs_op_slow_ring_trc);
> > > if (!phba->slow_ring_trc) {
> > > - phba->slow_ring_trc = kmalloc(
> > > - (sizeof(struct lpfc_debugfs_trc) *
> > > - lpfc_debugfs_max_slow_ring_trc),
> > > - GFP_KERNEL);
> > > + phba->slow_ring_trc = kzalloc((sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_slow_ring_trc),
> > > + GFP_KERNEL);
> >
> > kcalloc
> >
>
> Did someone have a Coccinelle script that converted kzalloc() to
> kcalloc()?

Not sure if I have ever done that. A long time ago, I made one that
starts with kmalloc and picks kzalloc or kcalloc. Perhaps Kees did such a
thing?

I'll see if it would be useful.

julia

2022-03-14 17:28:01

by Dan Carpenter

[permalink] [raw]
Subject: Re: [PATCH 4/6] scsi: lpfc: use kzalloc

On Sat, Mar 12, 2022 at 01:45:01PM -0800, Joe Perches wrote:
> On Sat, 2022-03-12 at 11:27 +0100, Julia Lawall wrote:
> > Use kzalloc instead of kmalloc + memset.
> []
> > diff --git a/drivers/scsi/lpfc/lpfc_debugfs.c b/drivers/scsi/lpfc/lpfc_debugfs.c
> []
> > @@ -6272,10 +6272,8 @@ lpfc_debugfs_initialize(struct lpfc_vport *vport)
> > phba->hba_debugfs_root,
> > phba, &lpfc_debugfs_op_slow_ring_trc);
> > if (!phba->slow_ring_trc) {
> > - phba->slow_ring_trc = kmalloc(
> > - (sizeof(struct lpfc_debugfs_trc) *
> > - lpfc_debugfs_max_slow_ring_trc),
> > - GFP_KERNEL);
> > + phba->slow_ring_trc = kzalloc((sizeof(struct lpfc_debugfs_trc) * lpfc_debugfs_max_slow_ring_trc),
> > + GFP_KERNEL);
>
> kcalloc
>

Did someone have a Coccinelle script that converted kzalloc() to
kcalloc()?

regards,
dan carpenter

2022-03-14 19:47:32

by Tariq Toukan

[permalink] [raw]
Subject: Re: [PATCH 2/6] net/mlx4_en: use kzalloc



On 3/12/2022 12:27 PM, Julia Lawall wrote:
> Use kzalloc instead of kmalloc + memset.
>
> The semantic patch that makes this change is:
> (https://coccinelle.gitlabpages.inria.fr/website/)
>
> //<smpl>
> @@
> expression res, size, flag;
> @@
> - res = kmalloc(size, flag);
> + res = kzalloc(size, flag);
> ...
> - memset(res, 0, size);
> //</smpl>
>
> Signed-off-by: Julia Lawall <[email protected]>
>
> ---
> drivers/net/ethernet/mellanox/mlx4/en_rx.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/net/ethernet/mellanox/mlx4/en_rx.c b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
> index 8cfc649f226b..8f762fc170b3 100644
> --- a/drivers/net/ethernet/mellanox/mlx4/en_rx.c
> +++ b/drivers/net/ethernet/mellanox/mlx4/en_rx.c
> @@ -1067,7 +1067,7 @@ static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
> struct mlx4_qp_context *context;
> int err = 0;
>
> - context = kmalloc(sizeof(*context), GFP_KERNEL);
> + context = kzalloc(sizeof(*context), GFP_KERNEL);
> if (!context)
> return -ENOMEM;
>
> @@ -1078,7 +1078,6 @@ static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn,
> }
> qp->event = mlx4_en_sqp_event;
>
> - memset(context, 0, sizeof(*context));
> mlx4_en_fill_qp_context(priv, ring->actual_size, ring->stride, 0, 0,
> qpn, ring->cqn, -1, context);
> context->db_rec_addr = cpu_to_be64(ring->wqres.db.dma);
>

Thanks for your patch.

Reviewed-by: Tariq Toukan <[email protected]>

2022-03-16 14:20:22

by Steve French

[permalink] [raw]
Subject: Re: [PATCH 1/6] cifs: use kzalloc

Which tree should this be merged from? cifs-2.6.git for-next ... or
do you prefer that these all go together through a different tree

On Sun, Mar 13, 2022 at 11:36 AM Julia Lawall <[email protected]> wrote:
>
> Use kzalloc instead of kmalloc + memset.
>
> The semantic patch that makes this change is:
> (https://coccinelle.gitlabpages.inria.fr/website/)
>
> //<smpl>
> @@
> expression res, size, flag;
> @@
> - res = kmalloc(size, flag);
> + res = kzalloc(size, flag);
> ...
> - memset(res, 0, size);
> //</smpl>
>
> Signed-off-by: Julia Lawall <[email protected]>
>
> ---
> fs/cifs/transport.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/fs/cifs/transport.c b/fs/cifs/transport.c
> index eeb1a699bd6f..4ff8e165a180 100644
> --- a/fs/cifs/transport.c
> +++ b/fs/cifs/transport.c
> @@ -464,13 +464,12 @@ smb_send_rqst(struct TCP_Server_Info *server, int num_rqst,
> return -EIO;
> }
>
> - tr_hdr = kmalloc(sizeof(*tr_hdr), GFP_NOFS);
> + tr_hdr = kzalloc(sizeof(*tr_hdr), GFP_NOFS);
> if (!tr_hdr)
> return -ENOMEM;
>
> memset(&cur_rqst[0], 0, sizeof(cur_rqst));
> memset(&iov, 0, sizeof(iov));
> - memset(tr_hdr, 0, sizeof(*tr_hdr));
>
> iov.iov_base = tr_hdr;
> iov.iov_len = sizeof(*tr_hdr);
>


--
Thanks,

Steve