Skip to contentSkip to conversation

memory: fsl_ifc: Clear global controller pointer on probe failure and remove

PATCH In review Reid Downing 3 participants 6 messages Oct 25 – Oct 30

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 memory: fsl_ifc: Clear global controller pointer on probe failure and remove

fsl_ifc_ctrl_dev is assigned early in fsl_ifc_ctrl_probe() to a devm-allocated structure. If probe fails afterwards, or when the device is removed, that memory is freed and the registers are unmapped, but the global still points at it. fsl_ifc_find() and fsl_ifc_nand_probe() only check it for NULL, so a later call can dereference freed memory or an unmapped register region.

Clear the pointer on the probe error paths and in remove, once the IRQs have been released. The global cannot be published later in probe instead, because ifc_in32()/ifc_out32() and the NAND child probed from of_platform_default_populate() depend on it.

---
Changes in v3:
- Use my real name in Author and Signed-off-by (earlier versions had a handle appended).
- Generate the patch with git against drivers/memory/fsl_ifc.c; the earlier versions used a path that could not be applied. Sorry for the noise, and for sending two unversioned copies. Those were v1 and v2, and this v3 supersedes them.
- Change of approach: v1 and v2 delayed publishing fsl_ifc_ctrl_dev until the end of probe, but ifc_in32()/ifc_out32() and fsl_ifc_nand_probe() use the global during probe, so that would break the driver. v3 keeps the early assignment and instead clears the pointer on probe failure and in remove.
- Dropped the nand_irq guard from the earlier versions; it was unrelated to this fix.

Not compile-tested (no PowerPC build setup); the change is four assignments of NULL.

 drivers/memory/fsl_ifc.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/memory/fsl_ifc.c +4 -0
index e89e0c6cc..abacd90d6 100644
@@ -95,6 +95,8 @@ static void fsl_ifc_ctrl_remove(struct platform_device *dev)
 	irq_dispose_mapping(ctrl->nand_irq);
 	irq_dispose_mapping(ctrl->irq);
 
+	/* Children and IRQs are gone; stop lookups before unmapping. */
+	fsl_ifc_ctrl_dev = NULL;
 	iounmap(ctrl->gregs);
 
 	dev_set_drvdata(&dev->dev, NULL);
@@ -218,6 +220,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	fsl_ifc_ctrl_dev->gregs = of_iomap(dev->dev.of_node, 0);
 	if (!fsl_ifc_ctrl_dev->gregs) {
 		dev_err(&dev->dev, "failed to get memory region\n");
+		fsl_ifc_ctrl_dev = NULL;
 		return -ENODEV;
 	}
 
@@ -300,6 +303,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	irq_dispose_mapping(fsl_ifc_ctrl_dev->irq);
 err:
 	iounmap(fsl_ifc_ctrl_dev->gregs);
+	fsl_ifc_ctrl_dev = NULL;
 	return ret;
 }
 
Signature · 1 line
2.53.0
#1 lore raw
Quoted 29 lines · Reid Downing

fsl_ifc_ctrl_dev is assigned early in fsl_ifc_ctrl_probe() to a devm-allocated structure. If probe fails afterwards, or when the device is removed, that memory is freed and the registers are unmapped, but the global still points at it. fsl_ifc_find() and fsl_ifc_nand_probe() only check it for NULL, so a later call can dereference freed memory or an unmapped register region.

Clear the pointer on the probe error paths and in remove, once the IRQs have been released. The global cannot be published later in probe instead, because ifc_in32()/ifc_out32() and the NAND child probed from of_platform_default_populate() depend on it.

---
Changes in v3:
- Use my real name in Author and Signed-off-by (earlier versions had a handle appended).
- Generate the patch with git against drivers/memory/fsl_ifc.c; the earlier versions used a path that could not be applied. Sorry for the noise, and for sending two unversioned copies. Those were v1 and v2, and this v3 supersedes them.
- Change of approach: v1 and v2 delayed publishing fsl_ifc_ctrl_dev until the end of probe, but ifc_in32()/ifc_out32() and fsl_ifc_nand_probe() use the global during probe, so that would break the driver. v3 keeps the early assignment and instead clears the pointer on probe failure and in remove.
- Dropped the nand_irq guard from the earlier versions; it was unrelated to this fix.

Quoted 2 lines

Not compile-tested (no PowerPC build setup); the change is four assignments of NULL.

Independently I want through Sashiko reports and tried to fix this with LLM... and then remembered about your patch. Unfortunately I am not taking unbuilt code, especially that build testing it is trivial.

Quoted 18 lines
 drivers/memory/fsl_ifc.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/memory/fsl_ifc.c +2 -0
index e89e0c6cc..abacd90d6 100644
@@ -95,6 +95,8 @@ static void fsl_ifc_ctrl_remove(struct platform_device *dev)
 	irq_dispose_mapping(ctrl->nand_irq);
 	irq_dispose_mapping(ctrl->irq);
 
+	/* Children and IRQs are gone; stop lookups before unmapping. */
+	fsl_ifc_ctrl_dev = NULL;
 	iounmap(ctrl->gregs);
 
 	dev_set_drvdata(&dev->dev, NULL);

drivers/memory/fsl_ifc.c static int fsl_ifc_ctrl_probe(struct platform_device *dev) · line 223

Quoted 4 lines
drivers/memory/fsl_ifc.c +1 -0
@@ -218,6 +220,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	fsl_ifc_ctrl_dev->gregs = of_iomap(dev->dev.of_node, 0);
 	if (!fsl_ifc_ctrl_dev->gregs) {
 		dev_err(&dev->dev, "failed to get memory region\n");
+		fsl_ifc_ctrl_dev = NULL;

This is supposed to be goto error path.

drivers/memory/fsl_ifc.c static int fsl_ifc_ctrl_probe(struct platform_device *dev) · line 307

Quoted 9 lines
drivers/memory/fsl_ifc.c +1 -0
 		return -ENODEV;
 	}
 
@@ -300,6 +303,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	irq_dispose_mapping(fsl_ifc_ctrl_dev->irq);
 err:
 	iounmap(fsl_ifc_ctrl_dev->gregs);
+	fsl_ifc_ctrl_dev = NULL;
 	return ret;

Please let me know if you intend to send v4 which is going to be at least built or I take what LLM produced me.

Best regards,
Krzysztof

#2 lore raw
Quoted 66 lines · Krzysztof Kozlowski
Quoted 29 lines · Reid Downing

fsl_ifc_ctrl_dev is assigned early in fsl_ifc_ctrl_probe() to a devm-allocated structure. If probe fails afterwards, or when the device is removed, that memory is freed and the registers are unmapped, but the global still points at it. fsl_ifc_find() and fsl_ifc_nand_probe() only check it for NULL, so a later call can dereference freed memory or an unmapped register region.

Clear the pointer on the probe error paths and in remove, once the IRQs have been released. The global cannot be published later in probe instead, because ifc_in32()/ifc_out32() and the NAND child probed from of_platform_default_populate() depend on it.

---
Changes in v3:
- Use my real name in Author and Signed-off-by (earlier versions had a handle appended).
- Generate the patch with git against drivers/memory/fsl_ifc.c; the earlier versions used a path that could not be applied. Sorry for the noise, and for sending two unversioned copies. Those were v1 and v2, and this v3 supersedes them.
- Change of approach: v1 and v2 delayed publishing fsl_ifc_ctrl_dev until the end of probe, but ifc_in32()/ifc_out32() and fsl_ifc_nand_probe() use the global during probe, so that would break the driver. v3 keeps the early assignment and instead clears the pointer on probe failure and in remove.
- Dropped the nand_irq guard from the earlier versions; it was unrelated to this fix.

Quoted 2 lines

Not compile-tested (no PowerPC build setup); the change is four assignments of NULL.

Independently I want through Sashiko reports and tried to fix this with LLM... and then remembered about your patch. Unfortunately I am not taking unbuilt code, especially that build testing it is trivial.

Quoted 18 lines
 drivers/memory/fsl_ifc.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/memory/fsl_ifc.c +2 -0
index e89e0c6cc..abacd90d6 100644
@@ -95,6 +95,8 @@ static void fsl_ifc_ctrl_remove(struct platform_device *dev)
 	irq_dispose_mapping(ctrl->nand_irq);
 	irq_dispose_mapping(ctrl->irq);
 
+	/* Children and IRQs are gone; stop lookups before unmapping. */
+	fsl_ifc_ctrl_dev = NULL;
 	iounmap(ctrl->gregs);
 
 	dev_set_drvdata(&dev->dev, NULL);

drivers/memory/fsl_ifc.c static int fsl_ifc_ctrl_probe(struct platform_device *dev) · line 223

Quoted 4 lines
drivers/memory/fsl_ifc.c +1 -0
@@ -218,6 +220,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	fsl_ifc_ctrl_dev->gregs = of_iomap(dev->dev.of_node, 0);
 	if (!fsl_ifc_ctrl_dev->gregs) {
 		dev_err(&dev->dev, "failed to get memory region\n");
+		fsl_ifc_ctrl_dev = NULL;

This is supposed to be goto error path.

Quoted 3 lines
drivers/memory/fsl_ifc.c +0 -0
 		return -ENODEV;
 	}
 
Quoted 10 lines

drivers/memory/fsl_ifc.c static int fsl_ifc_ctrl_probe(struct platform_device *dev) · line 307

Quoted 6 lines
drivers/memory/fsl_ifc.c +1 -0
@@ -300,6 +303,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	irq_dispose_mapping(fsl_ifc_ctrl_dev->irq);
 err:
 	iounmap(fsl_ifc_ctrl_dev->gregs);
+	fsl_ifc_ctrl_dev = NULL;
 	return ret;

Please let me know if you intend to send v4 which is going to be at least built or I take what LLM produced me.

If v4, then please credit also Sashiko:

Best regards,
Krzysztof

#3 lore raw
In reply to Krzysztof Kozlowski

Hi Krzysztof,

I completely understand. I wasn't able to test the patch, so please feel free to disregard my submission and proceed with your fix.

Thank you for taking the time to review it.

Reid Downing

#4 lore raw
Quoted 3 lines · Reid Downing

Hi Krzysztof,

I completely understand. I wasn't able to test the patch, so please feel free to disregard my submission and proceed with your fix.

Well, you can still build it - just setup proper cross compile.

Best regards,
Krzysztof

#5 lore raw
In reply to Reid Downing

…

Quoted 2 lines

Clear the pointer on the probe error paths and in remove, once the IRQs have been released. The global cannot be published later in probe

…

                          ???

Is there a need to refine the change description another bit?

…

Quoted 2 lines

---
Changes in v3:

…

Quoted 2 lines

Not compile-tested (no PowerPC build setup); the change is four assignments of NULL.

It seems that I am counting differently.

…

Quoted 1 line

+++ b/drivers/memory/fsl_ifc.c

…

Quoted 9 lines
drivers/memory/fsl_ifc.c +1 -0
@@ -218,6 +220,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	fsl_ifc_ctrl_dev->gregs = of_iomap(dev->dev.of_node, 0);
 	if (!fsl_ifc_ctrl_dev->gregs) {
 		dev_err(&dev->dev, "failed to get memory region\n");
+		fsl_ifc_ctrl_dev = NULL;
 		return -ENODEV;
 	}
 

drivers/memory/fsl_ifc.c static int fsl_ifc_ctrl_probe(struct platform_device *dev) · line 308

Quoted 6 lines
drivers/memory/fsl_ifc.c +1 -0
@@ -300,6 +303,7 @@ static int fsl_ifc_ctrl_probe(struct platform_device *dev)
 	irq_dispose_mapping(fsl_ifc_ctrl_dev->irq);
 err:
 	iounmap(fsl_ifc_ctrl_dev->gregs);
+	fsl_ifc_ctrl_dev = NULL;
 	return ret;
 }

How do you think about to avoid duplicate source code a bit more?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.3#n526
https://elixir.bootlin.com/linux/v7.3/source/drivers/memory/fsl_ifc.c#L194-L304

Regards,
Markus