Skip to contentSkip to conversation

use kzalloc

PATCH Applied Julia Lawall 8 participants 16 messages Mar 12, 2022 – Mar 16, 2022

Subsystems (14)
Networking Network drivers SCSI USB +10 more subsystems
lore
Share & tools
lore
#0 lore raw

cover 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(-)
#1 lore raw

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>

---
 fs/cifs/transport.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/cifs/transport.c +1 -2
index eeb1a699bd6f..4ff8e165a180 100644
@@ -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);
#2 lore raw

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

Quoted 42 lines · Julia Lawall

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>

---
 fs/cifs/transport.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/cifs/transport.c +1 -2
index eeb1a699bd6f..4ff8e165a180 100644
@@ -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);
Signature · 3 lines
Thanks,

Steve
#3 lore raw

PATCH 2/6 net/mlx4_en: use kzalloc

In reply to Julia Lawall

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>

---
 drivers/net/ethernet/mellanox/mlx4/en_rx.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_rx.c +1 -2
index 8cfc649f226b..8f762fc170b3 100644
@@ -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);
Reviewed-by on v1 2/6
#4 lore raw

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

Quoted 36 lines · Julia Lawall

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>

---
  drivers/net/ethernet/mellanox/mlx4/en_rx.c |    3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlx4/en_rx.c +1 -1
index 8cfc649f226b..8f762fc170b3 100644
@@ -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;
 

drivers/net/ethernet/mellanox/mlx4/en_rx.c static int mlx4_en_config_rss_qp(struct mlx4_en_priv *priv, int qpn, · line 1083

Quoted 7 lines
drivers/net/ethernet/mellanox/mlx4/en_rx.c +0 -1
@@ -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.

#5 lore raw

PATCH 3/6 ALSA: seq: oss: use kzalloc

In reply to Julia Lawall

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>

---
 sound/core/seq/oss/seq_oss_init.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
sound/core/seq/oss/seq_oss_init.c +1 -2
index 0ee4a5081fd6..04a3376a6e66 100644
@@ -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 */
#6 lore raw

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

Quoted 1 line · Julia Lawall

Use kzalloc instead of kmalloc + memset.

[]

sound/core/seq/oss/seq_oss_init.c

Quoted 1 line
sound/core/seq/oss/seq_oss_init.c +0 -0

[]

sound/core/seq/oss/seq_oss_init.c snd_seq_oss_create_client(void) · line 83

Quoted 4 lines
sound/core/seq/oss/seq_oss_init.c +0 -0
@@ -81,7 +81,6 @@ snd_seq_oss_create_client(void)
 	system_client = rc;
 
 	/* create annoucement receiver port */

unrelated trivia: typo of announcement above

Quoted 5 lines
sound/core/seq/oss/seq_oss_init.c +0 -1
-	memset(port, 0, sizeof(*port));
 	strcpy(port->name, "Receiver");
 	port->addr.client = system_client;
 	port->capability = SNDRV_SEQ_PORT_CAP_WRITE; /* receive only */
Applied
#7 lore raw

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

In reply to Julia Lawall
Quoted 16 lines · Julia Lawall

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>

Quoted 1 line

Applied, thanks.

Takashi

#8 lore raw

PATCH 4/6 scsi: lpfc: use kzalloc

In reply to Julia Lawall

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>

---
 drivers/scsi/lpfc/lpfc_debugfs.c |    9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/scsi/lpfc/lpfc_debugfs.c +2 -7
index 30fac2f6fb06..7c4a71703065 100644
@@ -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");
#9 lore raw

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

Quoted 1 line · Julia Lawall

Use kzalloc instead of kmalloc + memset.

[]

drivers/scsi/lpfc/lpfc_debugfs.c

Quoted 1 line
drivers/scsi/lpfc/lpfc_debugfs.c +0 -0

[]

drivers/scsi/lpfc/lpfc_debugfs.c lpfc_debugfs_initialize(struct lpfc_vport *vport) · line 6276

Quoted 10 lines
drivers/scsi/lpfc/lpfc_debugfs.c +2 -4
@@ -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

#10 lore raw

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

Quoted 5 lines · Joe Perches
Quoted 1 line · Julia Lawall

Use kzalloc instead of kmalloc + memset.

[]

drivers/scsi/lpfc/lpfc_debugfs.c

Quoted 1 line
drivers/scsi/lpfc/lpfc_debugfs.c +0 -0

[]

Quoted 13 lines

drivers/scsi/lpfc/lpfc_debugfs.c lpfc_debugfs_initialize(struct lpfc_vport *vport) · line 6276

Quoted 10 lines
drivers/scsi/lpfc/lpfc_debugfs.c +2 -4
@@ -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

#11 lore raw

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

Quoted 20 lines · Dan Carpenter
Quoted 5 lines · Joe Perches
Quoted 1 line · Julia Lawall

Use kzalloc instead of kmalloc + memset.

[]

drivers/scsi/lpfc/lpfc_debugfs.c

Quoted 1 line
drivers/scsi/lpfc/lpfc_debugfs.c +0 -0

[]

Quoted 13 lines

drivers/scsi/lpfc/lpfc_debugfs.c lpfc_debugfs_initialize(struct lpfc_vport *vport) · line 6276

Quoted 10 lines
drivers/scsi/lpfc/lpfc_debugfs.c +2 -4
@@ -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

Quoted 2 lines

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

#12 lore raw

PATCH 5/6 zd1201: use kzalloc

In reply to Julia Lawall

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>

---
 drivers/net/wireless/zydas/zd1201.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/net/wireless/zydas/zd1201.c +1 -2
index e64e4e579518..82bc0d44212e 100644
@@ -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;
#13 lore raw

Re: [PATCH 5/6] zd1201: use kzalloc

Quoted 15 lines · Julia Lawall

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>

Quoted 1 line

Patch applied to wireless-next.git, thanks.

3c0e3ca6028b zd1201: use kzalloc

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches
#14 lore raw

PATCH 6/6 usb: raw-gadget: use kzalloc

In reply to Julia Lawall

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>

---
 drivers/usb/gadget/legacy/raw_gadget.c |    3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/usb/gadget/legacy/raw_gadget.c +1 -2
index d86c3a36441e..135e75b891f6 100644
@@ -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,
#15 lore raw
In reply to Julia Lawall
This series was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (master) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Sat, 12 Mar 2022 11:26:59 +0100 you wrote:

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

Here is the summary with links:
- [2/6] net/mlx4_en: use kzalloc

    https://git.kernel.org/netdev/net-next/c/3c2dfb735b4a

- [5/6] zd1201: use kzalloc
(no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html