Skip to contentSkip to conversation

treewide: Replace more open-coded allocation size multiplications

PATCH In review Kees Cook 4 participants 8 messages Oct 5, 2018 – Oct 7, 2018

Subsystems (19)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 treewide: Replace more open-coded allocation size multiplications

As done treewide earlier, this catches several more open-coded allocation size calculations that were added to the kernel during the merge window. This performs the following mechanical transformations using Coccinelle:

	kvmalloc(a * b, ...) -> kvmalloc_array(a, b, ...)
	kvzalloc(a * b, ...) -> kvcalloc(a, b, ...)
	devm_kzalloc(..., a * b, ...) -> devm_kcalloc(..., a, b, ...)
---
 drivers/bluetooth/hci_qca.c                 |  2 +-
 drivers/crypto/inside-secure/safexcel.c     |  8 +++++---
 drivers/gpu/drm/mediatek/mtk_drm_crtc.c     |  2 +-
 drivers/gpu/drm/msm/disp/dpu1/dpu_io_util.c |  4 ++--
 drivers/hwmon/npcm750-pwm-fan.c             |  2 +-
 drivers/md/dm-integrity.c                   |  3 ++-
 drivers/net/wireless/mediatek/mt76/usb.c    | 10 +++++-----
 drivers/pci/controller/pcie-cadence.c       |  4 ++--
 drivers/tty/serial/qcom_geni_serial.c       |  4 ++--
 net/sched/sch_cake.c                        |  2 +-
 10 files changed, 22 insertions(+), 19 deletions(-)
drivers/bluetooth/hci_qca.c +1 -1
index e182f6019f68..2fee65886d50 100644
@@ -1322,7 +1322,7 @@ static int qca_init_regulators(struct qca_power *qca,
 {
 	int i;
 
-	qca->vreg_bulk = devm_kzalloc(qca->dev, num_vregs *
+	qca->vreg_bulk = devm_kcalloc(qca->dev, num_vregs,
 				      sizeof(struct regulator_bulk_data),
 				      GFP_KERNEL);
 	if (!qca->vreg_bulk)
drivers/crypto/inside-secure/safexcel.c +5 -3
index 7e71043457a6..86c699c14f84 100644
@@ -1044,7 +1044,8 @@ static int safexcel_probe(struct platform_device *pdev)
 
 	safexcel_configure(priv);
 
-	priv->ring = devm_kzalloc(dev, priv->config.rings * sizeof(*priv->ring),
+	priv->ring = devm_kcalloc(dev, priv->config.rings,
+				  sizeof(*priv->ring),
 				  GFP_KERNEL);
 	if (!priv->ring) {
 		ret = -ENOMEM;
@@ -1063,8 +1064,9 @@ static int safexcel_probe(struct platform_device *pdev)
 		if (ret)
 			goto err_reg_clk;
 
-		priv->ring[i].rdr_req = devm_kzalloc(dev,
-			sizeof(priv->ring[i].rdr_req) * EIP197_DEFAULT_RING_SIZE,
+		priv->ring[i].rdr_req = devm_kcalloc(dev,
+			EIP197_DEFAULT_RING_SIZE,
+			sizeof(priv->ring[i].rdr_req),
 			GFP_KERNEL);
 		if (!priv->ring[i].rdr_req) {
 			ret = -ENOMEM;
drivers/gpu/drm/mediatek/mtk_drm_crtc.c +1 -1
index 0b976dfd04df..92ecb9bf982c 100644
@@ -600,7 +600,7 @@ int mtk_drm_crtc_create(struct drm_device *drm_dev,
 	}
 
 	mtk_crtc->layer_nr = mtk_ddp_comp_layer_nr(mtk_crtc->ddp_comp[0]);
-	mtk_crtc->planes = devm_kzalloc(dev, mtk_crtc->layer_nr *
+	mtk_crtc->planes = devm_kcalloc(dev, mtk_crtc->layer_nr,
 					sizeof(struct drm_plane),
 					GFP_KERNEL);
 
drivers/gpu/drm/msm/disp/dpu1/dpu_io_util.c +2 -2
index 790d39f816dc..b557687b1964 100644
@@ -153,8 +153,8 @@ int msm_dss_parse_clock(struct platform_device *pdev,
 		return 0;
 	}
 
-	mp->clk_config = devm_kzalloc(&pdev->dev,
-				      sizeof(struct dss_clk) * num_clk,
+	mp->clk_config = devm_kcalloc(&pdev->dev,
+				      num_clk, sizeof(struct dss_clk),
 				      GFP_KERNEL);
 	if (!mp->clk_config)
 		return -ENOMEM;
drivers/hwmon/npcm750-pwm-fan.c +1 -1
index 8474d601aa63..b998f9fbed41 100644
@@ -908,7 +908,7 @@ static int npcm7xx_en_pwm_fan(struct device *dev,
 	if (fan_cnt < 1)
 		return -EINVAL;
 
-	fan_ch = devm_kzalloc(dev, sizeof(*fan_ch) * fan_cnt, GFP_KERNEL);
+	fan_ch = devm_kcalloc(dev, fan_cnt, sizeof(*fan_ch), GFP_KERNEL);
 	if (!fan_ch)
 		return -ENOMEM;
 
drivers/md/dm-integrity.c +2 -1
index 89ccb64342de..e1fa6baf4e8e 100644
@@ -3462,7 +3462,8 @@ static int dm_integrity_ctr(struct dm_target *ti, unsigned argc, char **argv)
 			r = -ENOMEM;
 			goto bad;
 		}
-		ic->recalc_tags = kvmalloc((RECALC_SECTORS >> ic->sb->log2_sectors_per_block) * ic->tag_size, GFP_KERNEL);
+		ic->recalc_tags = kvmalloc_array(RECALC_SECTORS >> ic->sb->log2_sectors_per_block,
+						 ic->tag_size, GFP_KERNEL);
 		if (!ic->recalc_tags) {
 			ti->error = "Cannot allocate tags for recalculating";
 			r = -ENOMEM;
drivers/net/wireless/mediatek/mt76/usb.c +5 -5
index 7780b07543bb..79e59f2379a2 100644
@@ -258,7 +258,7 @@ int mt76u_buf_alloc(struct mt76_dev *dev, struct mt76u_buf *buf,
 	if (!buf->urb)
 		return -ENOMEM;
 
-	buf->urb->sg = devm_kzalloc(dev->dev, nsgs * sizeof(*buf->urb->sg),
+	buf->urb->sg = devm_kcalloc(dev->dev, nsgs, sizeof(*buf->urb->sg),
 				    gfp);
 	if (!buf->urb->sg)
 		return -ENOMEM;
@@ -464,8 +464,8 @@ static int mt76u_alloc_rx(struct mt76_dev *dev)
 	int i, err, nsgs;
 
 	spin_lock_init(&q->lock);
-	q->entry = devm_kzalloc(dev->dev,
-				MT_NUM_RX_ENTRIES * sizeof(*q->entry),
+	q->entry = devm_kcalloc(dev->dev,
+				MT_NUM_RX_ENTRIES, sizeof(*q->entry),
 				GFP_KERNEL);
 	if (!q->entry)
 		return -ENOMEM;
@@ -717,8 +717,8 @@ static int mt76u_alloc_tx(struct mt76_dev *dev)
 		INIT_LIST_HEAD(&q->swq);
 		q->hw_idx = q2hwq(i);
 
-		q->entry = devm_kzalloc(dev->dev,
-					MT_NUM_TX_ENTRIES * sizeof(*q->entry),
+		q->entry = devm_kcalloc(dev->dev,
+					MT_NUM_TX_ENTRIES, sizeof(*q->entry),
 					GFP_KERNEL);
 		if (!q->entry)
 			return -ENOMEM;
drivers/pci/controller/pcie-cadence.c +2 -2
index 86f1b002c846..975bcdd6b5c0 100644
@@ -180,11 +180,11 @@ int cdns_pcie_init_phy(struct device *dev, struct cdns_pcie *pcie)
 		return 0;
 	}
 
-	phy = devm_kzalloc(dev, sizeof(*phy) * phy_count, GFP_KERNEL);
+	phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
 	if (!phy)
 		return -ENOMEM;
 
-	link = devm_kzalloc(dev, sizeof(*link) * phy_count, GFP_KERNEL);
+	link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
 	if (!link)
 		return -ENOMEM;
 
drivers/tty/serial/qcom_geni_serial.c +2 -2
index 29ec34387246..1515074e18fb 100644
@@ -868,8 +868,8 @@ static int qcom_geni_serial_port_setup(struct uart_port *uport)
 	geni_se_init(&port->se, port->rx_wm, port->rx_rfr);
 	geni_se_select_mode(&port->se, port->xfer_mode);
 	if (!uart_console(uport)) {
-		port->rx_fifo = devm_kzalloc(uport->dev,
-			port->rx_fifo_depth * sizeof(u32), GFP_KERNEL);
+		port->rx_fifo = devm_kcalloc(uport->dev,
+			port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
 		if (!port->rx_fifo)
 			return -ENOMEM;
 	}
net/sched/sch_cake.c +1 -1
index c07c30b916d5..793016d722ec 100644
@@ -2644,7 +2644,7 @@ static int cake_init(struct Qdisc *sch, struct nlattr *opt,
 	for (i = 1; i <= CAKE_QUEUES; i++)
 		quantum_div[i] = 65535 / i;
 
-	q->tins = kvzalloc(CAKE_MAX_TINS * sizeof(struct cake_tin_data),
+	q->tins = kvcalloc(CAKE_MAX_TINS, sizeof(struct cake_tin_data),
 			   GFP_KERNEL);
 	if (!q->tins)
 		goto nomem;
Signature · 6 lines
2.17.1


-- 
Kees Cook
Pixel Security
maintainer
#1 lore raw
Quoted 10 lines · Kees Cook

As done treewide earlier, this catches several more open-coded allocation size calculations that were added to the kernel during the merge window. This performs the following mechanical transformations using Coccinelle:

	kvmalloc(a * b, ...) -> kvmalloc_array(a, b, ...)
	kvzalloc(a * b, ...) -> kvcalloc(a, b, ...)
	devm_kzalloc(..., a * b, ...) -> devm_kcalloc(..., a, b, ...)

Has this had any testing in linux-next?

And when was "earlier"?

thanks,

greg k-h

author
#2 lore raw
Quoted 6 lines · Greg KH
Quoted 5 lines · Kees Cook

As done treewide earlier, this catches several more open-coded allocation size calculations that were added to the kernel during the merge window. This performs the following mechanical transformations using Coccinelle:

Quoted 7 lines
Quoted 5 lines
      kvmalloc(a * b, ...) -> kvmalloc_array(a, b, ...)
      kvzalloc(a * b, ...) -> kvcalloc(a, b, ...)
      devm_kzalloc(..., a * b, ...) -> devm_kcalloc(..., a, b, ...)

Has this had any testing in linux-next?

No; they're mechanical transformations (though I did build test them). If you want I could add this to linux-next for a week?

Quoted 1 line

And when was "earlier"?

v4.18, when all of these were originally eliminated:

026f05079b00 treewide: Use array_size() in f2fs_kzalloc() c86065938aab treewide: Use array_size() in f2fs_kmalloc() 76e43e37a407 treewide: Use array_size() in sock_kmalloc() 84ca176bf54a treewide: Use array_size() in kvzalloc_node() fd7becedb1f0 treewide: Use array_size() in vzalloc_node() fad953ce0b22 treewide: Use array_size() in vzalloc() 42bc47b35320 treewide: Use array_size() in vmalloc() a86854d0c599 treewide: devm_kzalloc() -> devm_kcalloc() 3c4211ba8ad8 treewide: devm_kmalloc() -> devm_kmalloc_array() 778e1cdd81bb treewide: kvzalloc() -> kvcalloc() 344476e16acb treewide: kvmalloc() -> kvmalloc_array() 590b5b7d8671 treewide: kzalloc_node() -> kcalloc_node() 6396bb221514 treewide: kzalloc() -> kcalloc() 6da2ec56059c treewide: kmalloc() -> kmalloc_array()

The new patch is catching new open-coded multiplications introduced in v4.19.

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
maintainer
#3 lore raw
Quoted 11 lines · Kees Cook
Quoted 10 lines · Greg KH
Quoted 9 lines · Kees Cook

As done treewide earlier, this catches several more open-coded allocation size calculations that were added to the kernel during the merge window. This performs the following mechanical transformations using Coccinelle:

      kvmalloc(a * b, ...) -> kvmalloc_array(a, b, ...)
      kvzalloc(a * b, ...) -> kvcalloc(a, b, ...)
      devm_kzalloc(..., a * b, ...) -> devm_kcalloc(..., a, b, ...)
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Has this had any testing in linux-next?

No; they're mechanical transformations (though I did build test them). If you want I could add this to linux-next for a week?

That would be good, thanks.

Quoted 19 lines
Quoted 1 line

And when was "earlier"?

v4.18, when all of these were originally eliminated:

026f05079b00 treewide: Use array_size() in f2fs_kzalloc() c86065938aab treewide: Use array_size() in f2fs_kmalloc() 76e43e37a407 treewide: Use array_size() in sock_kmalloc() 84ca176bf54a treewide: Use array_size() in kvzalloc_node() fd7becedb1f0 treewide: Use array_size() in vzalloc_node() fad953ce0b22 treewide: Use array_size() in vzalloc() 42bc47b35320 treewide: Use array_size() in vmalloc() a86854d0c599 treewide: devm_kzalloc() -> devm_kcalloc() 3c4211ba8ad8 treewide: devm_kmalloc() -> devm_kmalloc_array() 778e1cdd81bb treewide: kvzalloc() -> kvcalloc() 344476e16acb treewide: kvmalloc() -> kvmalloc_array() 590b5b7d8671 treewide: kzalloc_node() -> kcalloc_node() 6396bb221514 treewide: kzalloc() -> kcalloc() 6da2ec56059c treewide: kmalloc() -> kmalloc_array()

Quoted 1 line

The new patch is catching new open-coded multiplications introduced in v4.19.

As this is getting smaller, why not just break it up and do it through all of the different subsystems instead of one large patch?

And do we have a way to add a rule to 0-day to catch these so that they get a warning when they are added again?

thanks,

greg k-h

#4 lore raw
Quoted 40 lines · Greg KH
Quoted 11 lines · Kees Cook
Quoted 10 lines · Greg KH
Quoted 9 lines · Kees Cook

As done treewide earlier, this catches several more open-coded allocation size calculations that were added to the kernel during the merge window. This performs the following mechanical transformations using Coccinelle:

      kvmalloc(a * b, ...) -> kvmalloc_array(a, b, ...)
      kvzalloc(a * b, ...) -> kvcalloc(a, b, ...)
      devm_kzalloc(..., a * b, ...) -> devm_kcalloc(..., a, b, ...)
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Has this had any testing in linux-next?

No; they're mechanical transformations (though I did build test them). If you want I could add this to linux-next for a week?

That would be good, thanks.

Quoted 19 lines
Quoted 1 line

And when was "earlier"?

v4.18, when all of these were originally eliminated:

026f05079b00 treewide: Use array_size() in f2fs_kzalloc() c86065938aab treewide: Use array_size() in f2fs_kmalloc() 76e43e37a407 treewide: Use array_size() in sock_kmalloc() 84ca176bf54a treewide: Use array_size() in kvzalloc_node() fd7becedb1f0 treewide: Use array_size() in vzalloc_node() fad953ce0b22 treewide: Use array_size() in vzalloc() 42bc47b35320 treewide: Use array_size() in vmalloc() a86854d0c599 treewide: devm_kzalloc() -> devm_kcalloc() 3c4211ba8ad8 treewide: devm_kmalloc() -> devm_kmalloc_array() 778e1cdd81bb treewide: kvzalloc() -> kvcalloc() 344476e16acb treewide: kvmalloc() -> kvmalloc_array() 590b5b7d8671 treewide: kzalloc_node() -> kcalloc_node() 6396bb221514 treewide: kzalloc() -> kcalloc() 6da2ec56059c treewide: kmalloc() -> kmalloc_array()

Quoted 7 lines
Quoted 1 line

The new patch is catching new open-coded multiplications introduced in v4.19.

As this is getting smaller, why not just break it up and do it through all of the different subsystems instead of one large patch?

And do we have a way to add a rule to 0-day to catch these so that they get a warning when they are added again?

They could just be added to scripts/coccinelle and 0-day will report them?

For example, 0-day ran scripts/coccinelle/api/platform_no_drv_owner.cocci on a recently submitted patch and reported it here:
https://lore.kernel.org/lkml/201808301856.vMNJerSs%[email protected]/

But I'm not sure if 0-day runs make coccicheck on specific semantic patches, or runs all of them (CC'd Fengguang).

thanks,

- Joel

#5 lore raw
Quoted 49 lines · Joel Fernandes
Quoted 40 lines · Greg KH
Quoted 11 lines · Kees Cook
Quoted 10 lines · Greg KH
Quoted 9 lines · Kees Cook

As done treewide earlier, this catches several more open-coded allocation size calculations that were added to the kernel during the merge window. This performs the following mechanical transformations using Coccinelle:

      kvmalloc(a * b, ...) -> kvmalloc_array(a, b, ...)
      kvzalloc(a * b, ...) -> kvcalloc(a, b, ...)
      devm_kzalloc(..., a * b, ...) -> devm_kcalloc(..., a, b, ...)
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Has this had any testing in linux-next?

No; they're mechanical transformations (though I did build test them). If you want I could add this to linux-next for a week?

That would be good, thanks.

Quoted 19 lines
Quoted 1 line

And when was "earlier"?

v4.18, when all of these were originally eliminated:

026f05079b00 treewide: Use array_size() in f2fs_kzalloc() c86065938aab treewide: Use array_size() in f2fs_kmalloc() 76e43e37a407 treewide: Use array_size() in sock_kmalloc() 84ca176bf54a treewide: Use array_size() in kvzalloc_node() fd7becedb1f0 treewide: Use array_size() in vzalloc_node() fad953ce0b22 treewide: Use array_size() in vzalloc() 42bc47b35320 treewide: Use array_size() in vmalloc() a86854d0c599 treewide: devm_kzalloc() -> devm_kcalloc() 3c4211ba8ad8 treewide: devm_kmalloc() -> devm_kmalloc_array() 778e1cdd81bb treewide: kvzalloc() -> kvcalloc() 344476e16acb treewide: kvmalloc() -> kvmalloc_array() 590b5b7d8671 treewide: kzalloc_node() -> kcalloc_node() 6396bb221514 treewide: kzalloc() -> kcalloc() 6da2ec56059c treewide: kmalloc() -> kmalloc_array()

Quoted 7 lines
Quoted 1 line

The new patch is catching new open-coded multiplications introduced in v4.19.

As this is getting smaller, why not just break it up and do it through all of the different subsystems instead of one large patch?

And do we have a way to add a rule to 0-day to catch these so that they get a warning when they are added again?

Quoted 8 lines

They could just be added to scripts/coccinelle and 0-day will report them?

For example, 0-day ran scripts/coccinelle/api/platform_no_drv_owner.cocci on a recently submitted patch and reported it here:
https://lore.kernel.org/lkml/201808301856.vMNJerSs%[email protected]/

But I'm not sure if 0-day runs make coccicheck on specific semantic patches, or runs all of them (CC'd Fengguang).

0-day runs all coccinelle scripts. However only auto report out warnings that are known to have low false positives.

So if you add new coccinelle scripts that emit accurate enough warnings, it'd be good to inform the LKP team to add the new warnings to our auto-report-out white list.

Thanks,
Fengguang

author
#6 lore raw
Quoted 19 lines · Fengguang Wu
Quoted 12 lines · Joel Fernandes
Quoted 2 lines · Greg KH

And do we have a way to add a rule to 0-day to catch these so that they get a warning when they are added again?

They could just be added to scripts/coccinelle and 0-day will report them?

For example, 0-day ran scripts/coccinelle/api/platform_no_drv_owner.cocci on
a recently submitted patch and reported it here:

Quoted 5 lines

https://lore.kernel.org/lkml/201808301856.vMNJerSs%[email protected]/

But I'm not sure if 0-day runs make coccicheck on specific semantic patches,
or runs all of them (CC'd Fengguang).

Quoted 6 lines

0-day runs all coccinelle scripts. However only auto report out warnings that are known to have low false positives.

So if you add new coccinelle scripts that emit accurate enough warnings, it'd be good to inform the LKP team to add the new warnings to our auto-report-out white list.

It runs with MODE=report by default, yes? I'd need to expand the cases to cover that (it is patch-only currently) so that would be a roughly 10,000 line Coccinelle script. :)

-Kees

Signature · 2 lines
Kees Cook
Pixel Security
#7 lore raw
Quoted 23 lines · Kees Cook
Quoted 22 lines · Fengguang Wu
Quoted 12 lines · Joel Fernandes
Quoted 2 lines · Greg KH

And do we have a way to add a rule to 0-day to catch these so that they get a warning when they are added again?

They could just be added to scripts/coccinelle and 0-day will report them?

For example, 0-day ran scripts/coccinelle/api/platform_no_drv_owner.cocci on
a recently submitted patch and reported it here:

Quoted 5 lines

https://lore.kernel.org/lkml/201808301856.vMNJerSs%[email protected]/

But I'm not sure if 0-day runs make coccicheck on specific semantic patches,
or runs all of them (CC'd Fengguang).

0-day runs all coccinelle scripts. However only auto report out warnings that are known to have low false positives.

Quoted 7 lines
Quoted 3 lines

So if you add new coccinelle scripts that emit accurate enough warnings, it'd be good to inform the LKP team to add the new warnings to our auto-report-out white list.

It runs with MODE=report by default, yes? I'd need to expand the cases to cover that (it is patch-only currently) so that would be a roughly 10,000 line Coccinelle script. :)

It first runs with "-D report", then with "-D patch" to create possible patches.

Thanks,
Fengguang