Skip to contentSkip to conversation

media: nxp: imx8-isi: remove kzalloc_objs

PATCH Superseded Rosen Penev 5 participants 13 messages Mar 6 – Mar 26

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 media: nxp: imx8-isi: remove kzalloc_objs

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 11 ++++-------
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h |  3 ++-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +4 -7
index 16392420903a..657ffecc4d7e 100644
@@ -459,25 +459,21 @@ static const struct dev_pm_ops mxc_isi_pm_ops = {
 
 static int mxc_isi_probe(struct platform_device *pdev)
 {
+	const struct mxc_isi_plat_data *pdata;
 	struct device *dev = &pdev->dev;
 	struct mxc_isi_dev *isi;
 	unsigned int dma_size;
 	unsigned int i;
 	int ret = 0;
 
-	isi = devm_kzalloc(dev, sizeof(*isi), GFP_KERNEL);
+	pdata = of_device_get_match_data(dev);
+	isi = devm_kzalloc(dev, struct_size(isi, pipes, pdata->num_channels), GFP_KERNEL);
 	if (!isi)
 		return -ENOMEM;
 
 	isi->dev = dev;
 	platform_set_drvdata(pdev, isi);
 
-	isi->pdata = of_device_get_match_data(dev);
-
-	isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);
-	if (!isi->pipes)
-		return -ENOMEM;
-
 	isi->num_clks = devm_clk_bulk_get_all(dev, &isi->clks);
 	if (isi->num_clks < 0)
 		return dev_err_probe(dev, isi->num_clks, "Failed to get clocks\n");
@@ -487,6 +483,7 @@ static int mxc_isi_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(isi->regs),
 				     "Failed to get ISI register map\n");
 
+	isi->pdata = pdata;
 	if (isi->pdata->gasket_ops) {
 		isi->gasket = syscon_regmap_lookup_by_phandle(dev->of_node,
 							      "fsl,blk-ctrl");
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +2 -1
index 3cbd35305af0..99532efa4e41 100644
@@ -286,7 +286,6 @@ struct mxc_isi_dev {
 	struct regmap			*gasket;
 
 	struct mxc_isi_crossbar		crossbar;
-	struct mxc_isi_pipe		*pipes;
 	struct mxc_isi_m2m		m2m;
 
 	struct media_device		media_dev;
@@ -294,6 +293,8 @@ struct mxc_isi_dev {
 	struct v4l2_async_notifier	notifier;
 
 	struct dentry			*debugfs_root;
+
+	struct mxc_isi_pipe		pipes[];
 };
 
 extern const struct mxc_gasket_ops mxc_imx8_gasket_ops;
Signature · 1 line
2.53.0
maintainer
#1 lore raw

On Thu, Mar 05, 2026 at 08:45:36PM -0800, Rosen Penev wrote:

Subjest: use flexible array to fix memory leak

Quoted 1 line

Use a flexible arraay member to combine allocations.

typo arraay

Quoted 3 lines

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

Remove "It looks like". The pipes never ...

Because fix memory leak, need fixes tag here.

Frank

Quoted 1 line
#2 lore raw
Quoted 8 lines · Frank Li

On Thu, Mar 05, 2026 at 08:45:36PM -0800, Rosen Penev wrote:

Subjest: use flexible array to fix memory leak

Quoted 1 line

Use a flexible arraay member to combine allocations.

typo array

Will fix.

Quoted 7 lines
Quoted 3 lines

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

Remove "It looks like". The pipes never ...

Because fix memory leak, need fixes tag here.

Since its inception. Lovely.

That had no struct_size AFAIK.

Quoted 3 lines

Frank

Quoted 1 line
#3 lore raw
In reply to Rosen Penev
Quoted 4 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

It's a devm_kzalloc(), which means it's automatically free()ed after the remove callback.

Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
#4 lore raw
Quoted 9 lines · Marc Kleine-Budde
Quoted 4 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

It's a devm_kzalloc(), which means it's automatically free()ed after the remove callback.

Yes but pipes does not use devm.

Quoted 8 lines

Marc

--

Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |

Vertretung Nürnberg | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |

#5 lore raw
Quoted 11 lines · Rosen Penev
Quoted 9 lines · Marc Kleine-Budde
Quoted 4 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

It's a devm_kzalloc(), which means it's automatically free()ed after the remove callback.

Yes but pipes does not use devm.

Doh! Sorry, I should not review patches before first tee in the morning.

regards,
Marc

Signature · 4 lines
Pengutronix e.K.                 | Marc Kleine-Budde          |
Embedded Linux                   | https://www.pengutronix.de |
Vertretung Nürnberg              | Phone: +49-5121-206917-129 |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-9   |
maintainer
#6 lore raw
In reply to Rosen Penev
Quoted 26 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 11 ++++-------
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h |  3 ++-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +1 -0
index 16392420903a..657ffecc4d7e 100644
@@ -459,25 +459,21 @@ static const struct dev_pm_ops mxc_isi_pm_ops = {
 
 static int mxc_isi_probe(struct platform_device *pdev)
 {
+	const struct mxc_isi_plat_data *pdata;
 	struct device *dev = &pdev->dev;
 	struct mxc_isi_dev *isi;
 	unsigned int dma_size;
 	unsigned int i;
 	int ret = 0;
 

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 475

Quoted 10 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +2 -2
-	isi = devm_kzalloc(dev, sizeof(*isi), GFP_KERNEL);
+	pdata = of_device_get_match_data(dev);
+	isi = devm_kzalloc(dev, struct_size(isi, pipes, pdata->num_channels), GFP_KERNEL);
 	if (!isi)
 		return -ENOMEM;
 
 	isi->dev = dev;
 	platform_set_drvdata(pdev, isi);
 
-	isi->pdata = of_device_get_match_data(dev);

I'd keep

	isi->pdata = pdata;

here and not below.

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 477

Quoted 2 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -2
-
-	isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);

Wouldn't it be simpler to just replace this with devm_kcalloc() ?

	isi->pipes = devm_kcalloc(dev, isi->pdata->num_channels,
				  sizeof(isi->pipes[0]), GFP_KERNEL);

No other change would be needed.

Quoted 35 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +1 -3
-	if (!isi->pipes)
-		return -ENOMEM;
-
 	isi->num_clks = devm_clk_bulk_get_all(dev, &isi->clks);
 	if (isi->num_clks < 0)
 		return dev_err_probe(dev, isi->num_clks, "Failed to get clocks\n");
@@ -487,6 +483,7 @@ static int mxc_isi_probe(struct platform_device *pdev)
 		return dev_err_probe(dev, PTR_ERR(isi->regs),
 				     "Failed to get ISI register map\n");
 
+	isi->pdata = pdata;
 	if (isi->pdata->gasket_ops) {
 		isi->gasket = syscon_regmap_lookup_by_phandle(dev->of_node,
 							      "fsl,blk-ctrl");
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +2 -1
index 3cbd35305af0..99532efa4e41 100644
@@ -286,7 +286,6 @@ struct mxc_isi_dev {
 	struct regmap			*gasket;
 
 	struct mxc_isi_crossbar		crossbar;
-	struct mxc_isi_pipe		*pipes;
 	struct mxc_isi_m2m		m2m;
 
 	struct media_device		media_dev;
@@ -294,6 +293,8 @@ struct mxc_isi_dev {
 	struct v4l2_async_notifier	notifier;
 
 	struct dentry			*debugfs_root;
+
+	struct mxc_isi_pipe		pipes[];
 };
 
 extern const struct mxc_gasket_ops mxc_imx8_gasket_ops;
Signature · 3 lines
Regards,

Laurent Pinchart
#7 lore raw
Quoted 37 lines · Laurent Pinchart
Quoted 35 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 11 ++++-------
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h |  3 ++-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +3 -1
index 16392420903a..657ffecc4d7e 100644
@@ -459,25 +459,21 @@ static const struct dev_pm_ops mxc_isi_pm_ops = {
 
 static int mxc_isi_probe(struct platform_device *pdev)
 {
+     const struct mxc_isi_plat_data *pdata;
      struct device *dev = &pdev->dev;
      struct mxc_isi_dev *isi;
      unsigned int dma_size;
      unsigned int i;
      int ret = 0;
 
-     isi = devm_kzalloc(dev, sizeof(*isi), GFP_KERNEL);
+     pdata = of_device_get_match_data(dev);
+     isi = devm_kzalloc(dev, struct_size(isi, pipes, pdata->num_channels), GFP_KERNEL);
      if (!isi)
              return -ENOMEM;
 
      isi->dev = dev;
      platform_set_drvdata(pdev, isi);
 
Quoted 7 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 475

Quoted 1 line
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -1
-     isi->pdata = of_device_get_match_data(dev);

I'd keep

        isi->pdata = pdata;

here and not below.

Sure

Quoted 5 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 477

Quoted 3 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -2
-
-     isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);

Wouldn't it be simpler to just replace this with devm_kcalloc() ?

That would only make sense if pipes remained a pointer. A flex array member allows combining allocations since it doesn't take up extra space.

Technically kzalloc_flex is simpler but I assume a devm version of that will be handled in another treewide commit.

Quoted 5 lines
        isi->pipes = devm_kcalloc(dev, isi->pdata->num_channels,
                                  sizeof(isi->pipes[0]), GFP_KERNEL);

No other change would be needed.

With a flex array member, that's a compile time error, which I use as a hint to complete the conversion.

Quoted 41 lines
Quoted 28 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +1 -3
-     if (!isi->pipes)
-             return -ENOMEM;
-
      isi->num_clks = devm_clk_bulk_get_all(dev, &isi->clks);
      if (isi->num_clks < 0)
              return dev_err_probe(dev, isi->num_clks, "Failed to get clocks\n");
@@ -487,6 +483,7 @@ static int mxc_isi_probe(struct platform_device *pdev)
              return dev_err_probe(dev, PTR_ERR(isi->regs),
                                   "Failed to get ISI register map\n");
 
+     isi->pdata = pdata;
      if (isi->pdata->gasket_ops) {
              isi->gasket = syscon_regmap_lookup_by_phandle(dev->of_node,
                                                            "fsl,blk-ctrl");
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +0 -1
index 3cbd35305af0..99532efa4e41 100644
@@ -286,7 +286,6 @@ struct mxc_isi_dev {
      struct regmap                   *gasket;
 
      struct mxc_isi_crossbar         crossbar;
-     struct mxc_isi_pipe             *pipes;
      struct mxc_isi_m2m              m2m;
 
      struct media_device             media_dev;

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h struct mxc_isi_dev { · line 300

Quoted 8 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +2 -0
@@ -294,6 +293,8 @@ struct mxc_isi_dev {
      struct v4l2_async_notifier      notifier;
 
      struct dentry                   *debugfs_root;
+
+     struct mxc_isi_pipe             pipes[];
 };
 
 extern const struct mxc_gasket_ops mxc_imx8_gasket_ops;

--
Regards,

Laurent Pinchart

maintainer
#8 lore raw
Quoted 47 lines · Rosen Penev
Quoted 36 lines · Laurent Pinchart
Quoted 35 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 11 ++++-------
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h |  3 ++-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +3 -1
index 16392420903a..657ffecc4d7e 100644
@@ -459,25 +459,21 @@ static const struct dev_pm_ops mxc_isi_pm_ops = {
 
 static int mxc_isi_probe(struct platform_device *pdev)
 {
+     const struct mxc_isi_plat_data *pdata;
      struct device *dev = &pdev->dev;
      struct mxc_isi_dev *isi;
      unsigned int dma_size;
      unsigned int i;
      int ret = 0;
 
-     isi = devm_kzalloc(dev, sizeof(*isi), GFP_KERNEL);
+     pdata = of_device_get_match_data(dev);
+     isi = devm_kzalloc(dev, struct_size(isi, pipes, pdata->num_channels), GFP_KERNEL);
      if (!isi)
              return -ENOMEM;
 
      isi->dev = dev;
      platform_set_drvdata(pdev, isi);
 
Quoted 7 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 475

Quoted 1 line
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -1
-     isi->pdata = of_device_get_match_data(dev);

I'd keep

        isi->pdata = pdata;

here and not below.

Sure

Quoted 8 lines
Quoted 4 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 477

Quoted 2 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -2
-
-     isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);

Wouldn't it be simpler to just replace this with devm_kcalloc() ?

That would only make sense if pipes remained a pointer. A flex array member allows combining allocations since it doesn't take up extra space.

Yes, I understand that. My point was that with devm_kzalloc() here you won't need to change the mxc_isi_dev structure. The leak fix will be a one-liner patch.

Quoted 46 lines

Technically kzalloc_flex is simpler but I assume a devm version of that will be handled in another treewide commit.

Quoted 5 lines
        isi->pipes = devm_kcalloc(dev, isi->pdata->num_channels,
                                  sizeof(isi->pipes[0]), GFP_KERNEL);

No other change would be needed.

With a flex array member, that's a compile time error, which I use as a hint to complete the conversion.

Quoted 35 lines
Quoted 35 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +1 -3
-     if (!isi->pipes)
-             return -ENOMEM;
-
      isi->num_clks = devm_clk_bulk_get_all(dev, &isi->clks);
      if (isi->num_clks < 0)
              return dev_err_probe(dev, isi->num_clks, "Failed to get clocks\n");
@@ -487,6 +483,7 @@ static int mxc_isi_probe(struct platform_device *pdev)
              return dev_err_probe(dev, PTR_ERR(isi->regs),
                                   "Failed to get ISI register map\n");
 
+     isi->pdata = pdata;
      if (isi->pdata->gasket_ops) {
              isi->gasket = syscon_regmap_lookup_by_phandle(dev->of_node,
                                                            "fsl,blk-ctrl");
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +2 -1
index 3cbd35305af0..99532efa4e41 100644
@@ -286,7 +286,6 @@ struct mxc_isi_dev {
      struct regmap                   *gasket;
 
      struct mxc_isi_crossbar         crossbar;
-     struct mxc_isi_pipe             *pipes;
      struct mxc_isi_m2m              m2m;
 
      struct media_device             media_dev;
@@ -294,6 +293,8 @@ struct mxc_isi_dev {
      struct v4l2_async_notifier      notifier;
 
      struct dentry                   *debugfs_root;
+
+     struct mxc_isi_pipe             pipes[];
 };
 
 extern const struct mxc_gasket_ops mxc_imx8_gasket_ops;
Signature · 3 lines
Regards,

Laurent Pinchart
#9 lore raw
Quoted 54 lines · Laurent Pinchart
Quoted 52 lines · Rosen Penev
Quoted 36 lines · Laurent Pinchart
Quoted 35 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 11 ++++-------
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h |  3 ++-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +3 -1
index 16392420903a..657ffecc4d7e 100644
@@ -459,25 +459,21 @@ static const struct dev_pm_ops mxc_isi_pm_ops = {
 
 static int mxc_isi_probe(struct platform_device *pdev)
 {
+     const struct mxc_isi_plat_data *pdata;
      struct device *dev = &pdev->dev;
      struct mxc_isi_dev *isi;
      unsigned int dma_size;
      unsigned int i;
      int ret = 0;
 
-     isi = devm_kzalloc(dev, sizeof(*isi), GFP_KERNEL);
+     pdata = of_device_get_match_data(dev);
+     isi = devm_kzalloc(dev, struct_size(isi, pipes, pdata->num_channels), GFP_KERNEL);
      if (!isi)
              return -ENOMEM;
 
      isi->dev = dev;
      platform_set_drvdata(pdev, isi);
 
Quoted 7 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 475

Quoted 1 line
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -1
-     isi->pdata = of_device_get_match_data(dev);

I'd keep

        isi->pdata = pdata;

here and not below.

Sure

Quoted 4 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 477

Quoted 2 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -2
-
-     isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);

Wouldn't it be simpler to just replace this with devm_kcalloc() ?

Quoted 7 lines
Quoted 3 lines

That would only make sense if pipes remained a pointer. A flex array member allows combining allocations since it doesn't take up extra space.

Yes, I understand that. My point was that with devm_kzalloc() here you won't need to change the mxc_isi_dev structure. The leak fix will be a one-liner patch.

Leak fix is not the point of the patch to be fair.

Quoted 52 lines
Quoted 41 lines

Technically kzalloc_flex is simpler but I assume a devm version of that will be handled in another treewide commit.

Quoted 5 lines
        isi->pipes = devm_kcalloc(dev, isi->pdata->num_channels,
                                  sizeof(isi->pipes[0]), GFP_KERNEL);

No other change would be needed.

With a flex array member, that's a compile time error, which I use as a hint to complete the conversion.

Quoted 29 lines
Quoted 29 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +1 -3
-     if (!isi->pipes)
-             return -ENOMEM;
-
      isi->num_clks = devm_clk_bulk_get_all(dev, &isi->clks);
      if (isi->num_clks < 0)
              return dev_err_probe(dev, isi->num_clks, "Failed to get clocks\n");
@@ -487,6 +483,7 @@ static int mxc_isi_probe(struct platform_device *pdev)
              return dev_err_probe(dev, PTR_ERR(isi->regs),
                                   "Failed to get ISI register map\n");
 
+     isi->pdata = pdata;
      if (isi->pdata->gasket_ops) {
              isi->gasket = syscon_regmap_lookup_by_phandle(dev->of_node,
                                                            "fsl,blk-ctrl");
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +0 -1
index 3cbd35305af0..99532efa4e41 100644
@@ -286,7 +286,6 @@ struct mxc_isi_dev {
      struct regmap                   *gasket;
 
      struct mxc_isi_crossbar         crossbar;
-     struct mxc_isi_pipe             *pipes;
      struct mxc_isi_m2m              m2m;
 
      struct media_device             media_dev;
@@ -294,6 +293,8 @@ struct mxc_isi_dev {
      struct v4l2_async_notifier      notifier;
 
Quoted 6 lines
Quoted 6 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h struct mxc_isi_dev { · line 300

Quoted 6 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +2 -0
      struct dentry                   *debugfs_root;
+
+     struct mxc_isi_pipe             pipes[];
 };
 
 extern const struct mxc_gasket_ops mxc_imx8_gasket_ops;

--
Regards,

Laurent Pinchart

maintainer
#10 lore raw
Quoted 58 lines · Rosen Penev
Quoted 57 lines · Laurent Pinchart
Quoted 55 lines · Rosen Penev
Quoted 36 lines · Laurent Pinchart
Quoted 35 lines · Rosen Penev

Use a flexible arraay member to combine allocations.

It looks like pipes never gets freed anywhere. Meaning this effectively fixes a memory leak.

---
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c | 11 ++++-------
 drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h |  3 ++-
 2 files changed, 6 insertions(+), 8 deletions(-)
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +3 -1
index 16392420903a..657ffecc4d7e 100644
@@ -459,25 +459,21 @@ static const struct dev_pm_ops mxc_isi_pm_ops = {
 
 static int mxc_isi_probe(struct platform_device *pdev)
 {
+     const struct mxc_isi_plat_data *pdata;
      struct device *dev = &pdev->dev;
      struct mxc_isi_dev *isi;
      unsigned int dma_size;
      unsigned int i;
      int ret = 0;
 
-     isi = devm_kzalloc(dev, sizeof(*isi), GFP_KERNEL);
+     pdata = of_device_get_match_data(dev);
+     isi = devm_kzalloc(dev, struct_size(isi, pipes, pdata->num_channels), GFP_KERNEL);
      if (!isi)
              return -ENOMEM;
 
      isi->dev = dev;
      platform_set_drvdata(pdev, isi);
 
Quoted 7 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 475

Quoted 1 line
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -1
-     isi->pdata = of_device_get_match_data(dev);

I'd keep

        isi->pdata = pdata;

here and not below.

Sure

Quoted 4 lines

drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c static const struct dev_pm_ops mxc_isi_pm_ops = { · line 477

Quoted 2 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +0 -2
-
-     isi->pipes = kzalloc_objs(isi->pipes[0], isi->pdata->num_channels);

Wouldn't it be simpler to just replace this with devm_kcalloc() ?

That would only make sense if pipes remained a pointer. A flex array member allows combining allocations since it doesn't take up extra space.

Quoted 5 lines
Quoted 3 lines

Yes, I understand that. My point was that with devm_kzalloc() here you won't need to change the mxc_isi_dev structure. The leak fix will be a one-liner patch.

Leak fix is not the point of the patch to be fair.

If I understand correctly, your main point is to combine two allocations.

I don't liek this much in this specific case. mxc_isi_dev is the top-level driver structure. As part of all the data it stores, there is a pointer to a dynamically-allocated array, and the current implementation happens to not have any other dynamically allocated member. That is however not an intrinsic property of the mxc_isi_dev structure. Treating pipes as a flex arrat within mxc_isi_dev won't scale to a second similar member, and there's no reason to treat pipes in a special way. I prefer fixing the leak and avoiding the flex array.

Quoted 46 lines
Quoted 46 lines
Quoted 46 lines

Technically kzalloc_flex is simpler but I assume a devm version of that will be handled in another treewide commit.

Quoted 5 lines
        isi->pipes = devm_kcalloc(dev, isi->pdata->num_channels,
                                  sizeof(isi->pipes[0]), GFP_KERNEL);

No other change would be needed.

With a flex array member, that's a compile time error, which I use as a hint to complete the conversion.

Quoted 35 lines
Quoted 35 lines
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.c +1 -3
-     if (!isi->pipes)
-             return -ENOMEM;
-
      isi->num_clks = devm_clk_bulk_get_all(dev, &isi->clks);
      if (isi->num_clks < 0)
              return dev_err_probe(dev, isi->num_clks, "Failed to get clocks\n");
@@ -487,6 +483,7 @@ static int mxc_isi_probe(struct platform_device *pdev)
              return dev_err_probe(dev, PTR_ERR(isi->regs),
                                   "Failed to get ISI register map\n");
 
+     isi->pdata = pdata;
      if (isi->pdata->gasket_ops) {
              isi->gasket = syscon_regmap_lookup_by_phandle(dev->of_node,
                                                            "fsl,blk-ctrl");
drivers/media/platform/nxp/imx8-isi/imx8-isi-core.h +2 -1
index 3cbd35305af0..99532efa4e41 100644
@@ -286,7 +286,6 @@ struct mxc_isi_dev {
      struct regmap                   *gasket;
 
      struct mxc_isi_crossbar         crossbar;
-     struct mxc_isi_pipe             *pipes;
      struct mxc_isi_m2m              m2m;
 
      struct media_device             media_dev;
@@ -294,6 +293,8 @@ struct mxc_isi_dev {
      struct v4l2_async_notifier      notifier;
 
      struct dentry                   *debugfs_root;
+
+     struct mxc_isi_pipe             pipes[];
 };
 
 extern const struct mxc_gasket_ops mxc_imx8_gasket_ops;
Signature · 3 lines
Regards,

Laurent Pinchart
#11 lore raw
In reply to Rosen Penev
Quoted 2 lines · Rosen Penev

Technically kzalloc_flex is simpler but I assume a devm version of that will be handled in another treewide commit.

Yup, that's coming. I wanted to get the "first pass" of the base kmalloc family converted first. There are a few classes of "kmalloc wrappers" like devm_* that I'm hoping to convert to type-based allocations. devm is next. :)

Signature · 1 line
Kees Cook
#12 lore raw
Quoted 9 lines · Kees Cook
Quoted 2 lines · Rosen Penev

Technically kzalloc_flex is simpler but I assume a devm version of that will be handled in another treewide commit.

Yup, that's coming. I wanted to get the "first pass" of the base kmalloc family converted first. There are a few classes of "kmalloc wrappers" like devm_* that I'm hoping to convert to type-based allocations. devm is next. :)

As an aside, I recently noticed a pattern of kzalloc + kmemdup_array. As kzalloc_flex is basically, kzalloc + kcalloc, Would a kmemdup_flex make sense?

Quoted 3 lines

--
Kees Cook