From 522abb0fe912613407978ef722a2fa414eabe689 Mon Sep 17 00:00:00 2001 From: AI Dev Date: Sun, 29 Nov 2026 08:49:53 +0000 Subject: [PATCH 1/2] mtd: spinand: allocate flash_info with the device Both SPI NAND probes allocate info->flash_info with kmalloc() and no path frees it: not the probe's error labels and not remove. Since #343 an ID that is not in the table fails the probe, so every such probe leaks the allocation, and so did every earlier failure (a READID timeout, a failed nand_scan_with_ids()). Allocate it with devm_kzalloc() on the platform device, like info itself, so devres releases it on a failed probe and on unbind. The allocation is now zeroed. Every field the probe and the I/O path read is written first except num_blocks, which nothing sets: drv_nand_identify() copies it into total_block_cnt, the number of erase_count[4096] entries the static_info proc file prints. That count was whatever kmalloc() returned, and could walk the file past the end of the array. It is now 0, so the file lists no blocks. --- drivers/mtd/spiflash/spi020_nand.c | 3 ++- drivers/mtd/spiflash/spinand.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/spiflash/spi020_nand.c b/drivers/mtd/spiflash/spi020_nand.c index 36da5276cd..96b14d10f4 100644 --- a/drivers/mtd/spiflash/spi020_nand.c +++ b/drivers/mtd/spiflash/spi020_nand.c @@ -1066,7 +1066,8 @@ static int spinand_probe(struct platform_device *pdev) info->pdev = pdev; - info->flash_info = kmalloc(sizeof(struct nvt_nand_flash), GFP_KERNEL); + info->flash_info = devm_kzalloc(&pdev->dev, sizeof(struct nvt_nand_flash), + GFP_KERNEL); if (!info->flash_info) { dev_err(&pdev->dev, "failed to allocate nvt_nand_flash\n"); return -ENOMEM; diff --git a/drivers/mtd/spiflash/spinand.c b/drivers/mtd/spiflash/spinand.c index 84775b382e..6764ae39e9 100644 --- a/drivers/mtd/spiflash/spinand.c +++ b/drivers/mtd/spiflash/spinand.c @@ -1289,7 +1289,8 @@ static int spinand_probe(struct platform_device *pdev) info->pdev = pdev; - info->flash_info = kmalloc(sizeof(struct nvt_nand_flash), GFP_KERNEL); + info->flash_info = devm_kzalloc(&pdev->dev, sizeof(struct nvt_nand_flash), + GFP_KERNEL); if (!info->flash_info) { dev_err(&pdev->dev, "failed to allocate nvt_nand_flash\n"); return -ENOMEM; From 96d67c0062d43cd9c96fdc1e2e295451e4abcfa9 Mon Sep 17 00:00:00 2001 From: AI Dev Date: Sun, 29 Nov 2026 08:50:10 +0000 Subject: [PATCH 2/2] mtd: spinand: take back the driver data the probe stored The probe stores info with platform_set_drvdata(), but remove, suspend and resume read the driver data back as a struct mtd_info and take info from mtd->priv. The pointer is the same address, since nand_chip is the first member of info and mtd the first member of nand_chip. But nothing sets mtd->priv (the probe sets chip->priv), so info comes back NULL. Unbinding the device, which sysfs allows even for this built-in driver, dereferences NULL in free_irq(info->irq, info). Past that, remove would kfree(mtd), an interior pointer of the devm_kzalloc()'d info: the same slab corruption #343 removed from the probe's error path. Read the driver data as info, get mtd with nand_to_mtd(), and drop the kfree(mtd) from remove. devres frees info. suspend and resume get the same lookup; they are built only with CONFIG_PM, which the NT9856x configs leave off. Not changed: remove leaves /proc/nvt_info/nvt_flash in place, so a second bind warns that the proc entry is already registered, logs "create nand static proc failed", and keeps the old entries. --- drivers/mtd/spiflash/spi020_nand.c | 11 ++++------- drivers/mtd/spiflash/spinand.c | 11 ++++------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/drivers/mtd/spiflash/spi020_nand.c b/drivers/mtd/spiflash/spi020_nand.c index 96b14d10f4..bd43609230 100644 --- a/drivers/mtd/spiflash/spi020_nand.c +++ b/drivers/mtd/spiflash/spi020_nand.c @@ -1206,8 +1206,8 @@ static int spinand_probe(struct platform_device *pdev) static int spinand_remove(struct platform_device *pdev) { - struct mtd_info *mtd = platform_get_drvdata(pdev); - struct drv_nand_dev_info *info = mtd->priv; + struct drv_nand_dev_info *info = platform_get_drvdata(pdev); + struct mtd_info *mtd = nand_to_mtd(&info->nand_chip); platform_set_drvdata(pdev, NULL); @@ -1217,7 +1217,6 @@ static int spinand_remove(struct platform_device *pdev) kfree(info->data_buff); - kfree(mtd); return E_OK; } @@ -1225,8 +1224,7 @@ static int spinand_remove(struct platform_device *pdev) static int spinand_suspend(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); - struct mtd_info *mtd = platform_get_drvdata(pdev); - struct drv_nand_dev_info *info = mtd->priv; + struct drv_nand_dev_info *info = platform_get_drvdata(pdev); if (!IS_ERR(info->clk)) clk_disable(info->clk); @@ -1236,8 +1234,7 @@ static int spinand_suspend(struct device *dev) static int spinand_resume(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); - struct mtd_info *mtd = platform_get_drvdata(pdev); - struct drv_nand_dev_info *info = mtd->priv; + struct drv_nand_dev_info *info = platform_get_drvdata(pdev); clk_enable(info->clk); diff --git a/drivers/mtd/spiflash/spinand.c b/drivers/mtd/spiflash/spinand.c index 6764ae39e9..1a9693f2b4 100644 --- a/drivers/mtd/spiflash/spinand.c +++ b/drivers/mtd/spiflash/spinand.c @@ -1458,8 +1458,8 @@ static int spinand_probe(struct platform_device *pdev) static int spinand_remove(struct platform_device *pdev) { - struct mtd_info *mtd = platform_get_drvdata(pdev); - struct drv_nand_dev_info *info = mtd->priv; + struct drv_nand_dev_info *info = platform_get_drvdata(pdev); + struct mtd_info *mtd = nand_to_mtd(&info->nand_chip); platform_set_drvdata(pdev, NULL); @@ -1469,7 +1469,6 @@ static int spinand_remove(struct platform_device *pdev) kfree(info->data_buff); - kfree(mtd); return E_OK; } @@ -1477,8 +1476,7 @@ static int spinand_remove(struct platform_device *pdev) static int spinand_suspend(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); - struct mtd_info *mtd = platform_get_drvdata(pdev); - struct drv_nand_dev_info *info = mtd->priv; + struct drv_nand_dev_info *info = platform_get_drvdata(pdev); if (!IS_ERR(info->clk)) clk_disable(info->clk); @@ -1488,8 +1486,7 @@ static int spinand_suspend(struct device *dev) static int spinand_resume(struct device *dev) { struct platform_device *pdev = to_platform_device(dev); - struct mtd_info *mtd = platform_get_drvdata(pdev); - struct drv_nand_dev_info *info = mtd->priv; + struct drv_nand_dev_info *info = platform_get_drvdata(pdev); clk_enable(info->clk);