Skip to contentSkip to conversation

EDAC/altera: Address sashiko reviews part 1(v4)

PATCH Superseded Dinh Nguyen 2 participants 7 messages Sep 9

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Dinh Nguyen

author maintainer
#0 lore raw

v4 cover EDAC/altera: Address sashiko reviews part 1(v4)

Hi EDAC maintainers,

This is version 4 of the patch series that addresses some of the sashiko reviews for the Altera EDAC driver.

Here's what has changed from v3:

I rebased the series to edac-urgent branch because of patch 9868f5c077df ("EDAC/altera: Use parent device for devres in altr_portb_setup()") will cause conflicts.

Thanks,
Dinh

Dinh Nguyen (4):
EDAC/altera: Do not allow driver unbinding EDAC/altera: Drop __init from ECC setup paths for re-probe safety EDAC/altera: Fix code leak on dci allocation failure EDAC/altera: Fix use-after-free in error paths

 drivers/edac/altera_edac.c | 60 ++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 31 deletions(-)
Signature · 1 line
2.42.0.411.g813d9a9188
author maintainer
#1 lore raw

PATCH v4 1/4 EDAC/altera: Do not allow driver unbinding

The EDAC driver must remain bound; unbinding and re-binding it would erase active system memory.

Remove the .remove functions because they will not ever get used.

Fixes 588cb03ea208 ("EDAC, altera: Add Arria10 L2 Cache ECC handling")

---
v4: rebased to edac-urgent
v3: simplify commit messsage
v2: reworked v1 to include only true sashiko hits

---
 drivers/edac/altera_edac.c | 25 ++++---------------------
 1 file changed, 4 insertions(+), 21 deletions(-)
drivers/edac/altera_edac.c +4 -21
index 68846f583eeef..fe501c89dd0dc 100644
@@ -453,15 +453,6 @@ static int altr_sdram_probe(struct platform_device *pdev)
 	return res;
 }
 
-static void altr_sdram_remove(struct platform_device *pdev)
-{
-	struct mem_ctl_info *mci = platform_get_drvdata(pdev);
-
-	edac_mc_del_mc(&pdev->dev);
-	edac_mc_free(mci);
-	platform_set_drvdata(pdev, NULL);
-}
-
 /*
  * If you want to suspend, need to disable EDAC by removing it
  * from the device tree or defconfig.
@@ -481,13 +472,13 @@ static const struct dev_pm_ops altr_sdram_pm_ops = {
 
 static struct platform_driver altr_sdram_edac_driver = {
 	.probe = altr_sdram_probe,
-	.remove = altr_sdram_remove,
 	.driver = {
 		.name = "altr_sdram_edac",
 #ifdef CONFIG_PM
 		.pm = &altr_sdram_pm_ops,
 #endif
 		.of_match_table = altr_sdram_ctrl_of_match,
+		.suppress_bind_attrs = true,
 	},
 };
 
@@ -517,6 +508,7 @@ static struct platform_driver altr_edac_driver = {
 	.driver = {
 		.name = "socfpga_ecc_manager",
 		.of_match_table = altr_edac_of_match,
+		.suppress_bind_attrs = true,
 	},
 };
 module_platform_driver(altr_edac_driver);
@@ -803,22 +795,12 @@ static int altr_edac_device_probe(struct platform_device *pdev)
 	return res;
 }
 
-static void altr_edac_device_remove(struct platform_device *pdev)
-{
-	struct edac_device_ctl_info *dci = platform_get_drvdata(pdev);
-	struct altr_edac_device_dev *drvdata = dci->pvt_info;
-
-	debugfs_remove_recursive(drvdata->debugfs_dir);
-	edac_device_del_device(&pdev->dev);
-	edac_device_free_ctl_info(dci);
-}
-
 static struct platform_driver altr_edac_device_driver = {
 	.probe =  altr_edac_device_probe,
-	.remove = altr_edac_device_remove,
 	.driver = {
 		.name = "altr_edac_device",
 		.of_match_table = altr_edac_device_of_match,
+		.suppress_bind_attrs = true,
 	},
 };
 module_platform_driver(altr_edac_device_driver);
@@ -2214,6 +2196,7 @@ static struct platform_driver altr_edac_a10_driver = {
 	.driver = {
 		.name = "socfpga_a10_ecc_manager",
 		.of_match_table = altr_edac_a10_of_match,
+		.suppress_bind_attrs = true,
 	},
 };
 module_platform_driver(altr_edac_a10_driver);
Signature · 1 line
2.42.0.411.g813d9a9188
author maintainer
#2 lore raw

PATCH v4 2/4 EDAC/altera: Drop __init from ECC setup paths for re-probe safety

In reply to Dinh Nguyen

sashiko reports: Does suppressing sysfs unbinding fully prevent the execution of freed __init memory? If altr_sysmgr_regmap_lookup_by_phandle() returns -EPROBE_DEFER, the probe is deferred until after __init memory is freed.

The a10 EDAC .setup callbacks (sdmmc, ethernet, nand, dma, usb, qspi) and their helpers (altr_init_a10_ecc_device_type, altr_init_a10_ecc_block) were marked __init. These run from the probe path, which may execute after init memory is freed -- e.g. a probe deferred via -EPROBE_DEFER that only succeeds once a late/module dependency appears, or a manual unbind/rebind. Calling __init code then dereferences freed memory. Remove __init so these functions remain valid at runtime.

Assisted-by Cursor:claude-4.8-opus
Fixes 788586efd116 ("EDAC/altera: Initialize peripheral FIFOs in probe())

---
v4: rebased to edac-urgent
v3: no changes
v2: reworked v1 to include only true sashiko hits

---
 drivers/edac/altera_edac.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/edac/altera_edac.c +8 -8
index fe501c89dd0dc..d8a3438fb5f44 100644
@@ -926,7 +926,7 @@ static int __maybe_unused altr_init_memory_port(void __iomem *ioaddr, int port)
 	return ret;
 }
 
-static __init int __maybe_unused
+static int __maybe_unused
 altr_init_a10_ecc_block(struct device_node *np, u32 irq_mask,
 			u32 ecc_ctrl_en_mask, bool dual_port)
 {
@@ -1001,7 +1001,7 @@ altr_init_a10_ecc_block(struct device_node *np, u32 irq_mask,
 
 static int validate_parent_available(struct device_node *np);
 static const struct of_device_id altr_edac_a10_device_of_match[];
-static int __init __maybe_unused altr_init_a10_ecc_device_type(char *compat)
+static int __maybe_unused altr_init_a10_ecc_device_type(char *compat)
 {
 	int irq;
 	struct device_node *child, *np;
@@ -1330,7 +1330,7 @@ static const struct edac_device_prv_data a10_l2ecc_data = {
 
 #ifdef CONFIG_EDAC_ALTERA_ETHERNET
 
-static int __init socfpga_init_ethernet_ecc(struct altr_edac_device_dev *dev)
+static int socfpga_init_ethernet_ecc(struct altr_edac_device_dev *dev)
 {
 	int ret;
 
@@ -1360,7 +1360,7 @@ static const struct edac_device_prv_data a10_enetecc_data = {
 
 #ifdef CONFIG_EDAC_ALTERA_NAND
 
-static int __init socfpga_init_nand_ecc(struct altr_edac_device_dev *device)
+static int socfpga_init_nand_ecc(struct altr_edac_device_dev *device)
 {
 	int ret;
 
@@ -1390,7 +1390,7 @@ static const struct edac_device_prv_data a10_nandecc_data = {
 
 #ifdef CONFIG_EDAC_ALTERA_DMA
 
-static int __init socfpga_init_dma_ecc(struct altr_edac_device_dev *device)
+static int socfpga_init_dma_ecc(struct altr_edac_device_dev *device)
 {
 	int ret;
 
@@ -1420,7 +1420,7 @@ static const struct edac_device_prv_data a10_dmaecc_data = {
 
 #ifdef CONFIG_EDAC_ALTERA_USB
 
-static int __init socfpga_init_usb_ecc(struct altr_edac_device_dev *device)
+static int socfpga_init_usb_ecc(struct altr_edac_device_dev *device)
 {
 	int ret;
 
@@ -1450,7 +1450,7 @@ static const struct edac_device_prv_data a10_usbecc_data = {
 
 #ifdef CONFIG_EDAC_ALTERA_QSPI
 
-static int __init socfpga_init_qspi_ecc(struct altr_edac_device_dev *device)
+static int socfpga_init_qspi_ecc(struct altr_edac_device_dev *device)
 {
 	int ret;
 
@@ -1598,7 +1598,7 @@ static int altr_portb_setup(struct altr_edac_device_dev *device)
 	return rc;
 }
 
-static int __init socfpga_init_sdmmc_ecc(struct altr_edac_device_dev *device)
+static int socfpga_init_sdmmc_ecc(struct altr_edac_device_dev *device)
 {
 	int rc = -ENODEV;
 	struct device_node *child;
Signature · 1 line
2.42.0.411.g813d9a9188
author maintainer
#3 lore raw

PATCH v4 3/4 EDAC/altera: Fix code leak on dci allocation failure

In reply to Dinh Nguyen

sashiko reports: If devres_open_group() fails, the function returns -ENOMEM without freeing the dci structure allocated earlier with edac_device_alloc_ctl_info().

Free the dci structure if devres_open_group() fails.

Fixes c3eea1942a16 ("EDAC, altera: Add Altera L2 cache and OCRAM support")

---
v4: rebased to edac-urgent
v3: No changes
v2: reworked v1 to include only true sashiko hits

---
 drivers/edac/altera_edac.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/edac/altera_edac.c +2 -0
index d8a3438fb5f44..4b289b90186f9 100644
@@ -1517,7 +1517,9 @@ static int altr_portb_setup(struct altr_edac_device_dev *device)
 	*altdev = *device;
 
 	if (!devres_open_group(device->edac->dev, altr_portb_setup, GFP_KERNEL))
+		edac_device_free_ctl_info(dci);
 		return -ENOMEM;
+	}
 
 	/* Update PortB specific values */
 	altdev->edac_dev_name = ecc_name;
Signature · 1 line
2.42.0.411.g813d9a9188
#4 lore raw

Re: [PATCHv4 3/4] EDAC/altera: Fix code leak on dci allocation failure

Hi Dinh,

drivers/edac/altera_edac.c static int altr_portb_setup(struct altr_edac_device_dev *device) · line 1522

Quoted 4 lines
        if (!devres_open_group(device->edac->dev, altr_portb_setup, GFP_KERNEL))
drivers/edac/altera_edac.c +2 -0
+               edac_device_free_ctl_info(dci);
                return -ENOMEM;
+       }

This file will not compile since the braces are wrong, there is no starting {.

Also, another thing that I noticed is that 2/4's Fixes tag is missing its closing
quote after "probe()".

I will rebase and resend my of_node_put fix once this series lands.

Thanks,
Rounak

author maintainer
#5 lore raw

Re: [PATCHv4 3/4] EDAC/altera: Fix code leak on dci allocation failure

Quoted 7 lines · Rounak Das

Hi Dinh,

drivers/edac/altera_edac.c static int altr_portb_setup(struct altr_edac_device_dev *device) · line 1522

Quoted 4 lines
         if (!devres_open_group(device->edac->dev, altr_portb_setup, GFP_KERNEL))
drivers/edac/altera_edac.c +2 -0
+               edac_device_free_ctl_info(dci);
                 return -ENOMEM;
+       }
Quoted 5 lines

This file will not compile since the braces are wrong, there is no starting {.

Also, another thing that I noticed is that 2/4's Fixes tag is missing its closing
quote after "probe()".

Argh..Thank you for catching that!

Quoted 3 lines

I will rebase and resend my of_node_put fix once this series lands.

Dinh

author maintainer
#6 lore raw

PATCH v4 4/4 EDAC/altera: Fix use-after-free in error paths

In reply to Dinh Nguyen

In both altr_edac_a10_device_add() and altr_portb_setup(), the error path freed the dci structure before releasing the devres group. Since the managed single and double bit IRQ handlers use altdev(dci->pvt_info) as their data, an IRQ firing between freeing dci and unregistering the IRQs could dereference the freed memory.

Release the devres group first so the managed IRQs are unregistered before the dci structure is freed.

Assisted-by Cursor:claude-4.8-opus
Fixes 911049845d70 ("EDAC, altera: Add Arria10 SD-MMC EDAC support")
Fixes 588cb03ea208 ("EDAC, altera: Add Arria10 L2 Cache ECC handling")

---
v4: rebased to edac-urgent
v3: No changes
v2: reworked v1 to include only true sashiko hits

---
 drivers/edac/altera_edac.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
drivers/edac/altera_edac.c +14 -1
index 4b289b90186f9..6fc474ec3811d 100644
@@ -1593,8 +1593,13 @@ static int altr_portb_setup(struct altr_edac_device_dev *device)
 	return 0;
 
 err_release_group_1:
-	edac_device_free_ctl_info(dci);
+	/*
+	 * Release the devres group first so the managed IRQs are
+	 * unregistered before dci (which contains the IRQ handler's
+	 * data via dci->pvt_info) is freed, avoiding a use-after-free.
+	 */
 	devres_release_group(device->edac->dev, altr_portb_setup);
+	edac_device_free_ctl_info(dci);
 	edac_printk(KERN_ERR, EDAC_DEVICE,
 		    "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
 	return rc;
@@ -1997,9 +2002,17 @@ static int altr_edac_a10_device_add(struct altr_arria10_edac *edac,
 	return 0;
 
 err_release_group1:
+	/*
+	 * Release the devres group first so the managed IRQs are
+	 * unregistered before dci (which contains the IRQ handler's
+	 * data via dci->pvt_info) is freed, avoiding a use-after-free.
+	 */
+	devres_release_group(edac->dev, NULL);
 	edac_device_free_ctl_info(dci);
+	goto err_print;
 err_release_group:
 	devres_release_group(edac->dev, NULL);
+err_print:
 	edac_printk(KERN_ERR, EDAC_DEVICE,
 		    "%s:Error setting up EDAC device: %d\n", ecc_name, rc);
 
Signature · 1 line
2.42.0.411.g813d9a9188