Repository navigation
mtd: spinand: free flash_info with the device, and fix the remove path - #344
Conversation
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.
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.
PR Summary by QodoFix SPI NAND flash metadata ownership and device removal
AI Description
Merge Risk
|
andrein
left a comment
There was a problem hiding this comment.
I read both commits against the code and the claims hold. nand_chip is the first member of drv_nand_dev_info and mtd is the first member of nand_chip, so the old cast found the right address, and mtd->priv is never set, so info was NULL. Nothing else in the probe or the I/O path depends on what kmalloc() left in flash_info. Only num_blocks did. devres frees info and flash_info after remove has freed the IRQ, so nothing touches them after they are gone.
Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M), with this branch on top of 5f7f91b and started by kexec. The flash is found, ECC strength and bitflip threshold read 1, UBI attaches, both UBIFS volumes mount, and both video streams run (HEVC 2592x1944 at 30 fps, H.264 640x480). static_info prints a single empty line, as the commit message says. No oops or warning in the log.
A possible follow-up, outside this PR: drv_nand_read_id() already fills in device_size (in MiB) and block_size from the ID table, so setting num_blocks = (device_size << 20) / block_size there would make static_info list the real blocks. The largest entry in the table comes to 4096 blocks, which still fits erase_count[4096].
Follow-up to #343. Two commits, both in
spinand.cand its SPI020 copyspi020_nand.c.1.
mtd: spinand: allocate flash_info with the device. This is the leak Qodo found on #343.info->flash_infowaskmalloc()'d in the probe and never freed on any path, not in the error labels and not in remove. It is nowdevm_kzalloc()'d with the device, likeinfoitself. The allocation is also zeroed now. The only field the driver read without writing first isnum_blocks: it sizes thestatic_infoproc dump, which could therefore run pasterase_count[4096], and now lists no blocks.2.
mtd: spinand: take back the driver data the probe stored. The probe storesinfoas driver data, but remove, suspend and resume read it back as anmtd_infoand takeinfofrommtd->priv. Nothing setsmtd->priv(the probe setschip->priv), soinfois NULL there. Unbinding the device dereferences NULL infree_irq(info->irq, info). One step later, remove wouldkfree(mtd), an interior pointer of the devm allocation: the same defect #343 removed from the probe's error path. The driver data is now read back asinfo,mtdcomes fromnand_to_mtd(), and thekfree(mtd)is gone. Suspend and resume get the same lookup. They are built only withCONFIG_PM, which the NT9856x configs leave off.Tested
The tests ran in qemu-bench,
-M nt98566with a blank 128 MiB SPI NAND, on kernels built fromnt98566.nand.config(thent98566_ultimateconfig): one fromnovatek-nt9856xat 5f7f91b (base) and one from this branch (fix). The guest is a busybox initramfs.Unbind and rebind, listed flash (default W25N01GV,
ef aa 21):The rebind also warns that
nvt_info/nvt_flashis already registered and logs "create nand static proc failed". Remove never takes the proc entries down. That is unchanged here.Leak, unlisted flash (
flash-jedec=0x123400, so ID12 34).SUnreclaimfrom/proc/meminfoaround 2000bindattempts, each one a failed probe ("flash ID 0x12 0x34 is not in the ID table"), with caches dropped before each reading:The ~124 B per probe this removes is
struct nvt_nand_flash(84 bytes, so akmalloc-128object).kmemleak could not be used for this: it turns itself off at boot on this platform ("Cannot insert 0x87ff6000 into the object search tree (overlaps existing)").
The SPI020 driver (NA51068 only) was compile-tested.
Not changed
A second leak remains. The ~126 B per failed probe still left is the clock handle.
drv_nand_reset()callsclk_get(),clk_prepare()andclk_enable()on every probe and every resume, and nothing callsclk_put()or disables the clock again. Fixing it means turning the NAND controller clock off on a failed probe and on remove, which is a behaviour change of its own, so it belongs in a separate PR.Digital twin of an
nt98566_ultimatecamera. The tests above use a test initramfs. This one runs the published firmware: qemu-bench-M nt98566,sensor=sc401aiwith a 128 MiB NAND laid out as the install writes it. That is the release'su-boot-nt98566-nand.binat 0 androotfs.ubi.nt98566fromopenipc.nt98566-nand-ultimate.tgz(2026-11-28) at 0x300000, the start of UBI. The camera's U-Boot ran its ownbootflashsteps with one change: afterubi part ubi; ubifsmount ubi0:rootfs, the kernel came fromtftpbootinstead ofubifsload /boot/uImage. Thebootargsare the image's own. The kernels are uImages built fromnt98566.nand.configwith the release's version string, so the image's modules load. Both base and fix:From the host, majestic answers
/setup.htmlwith 200 and/with 302, which is right for an unclaimed camera. The two consoles fromStarting kernelon are 187 lines each and identical, apart from the order of oneubi_bgt0dline, the freshrootfs_dataUUID and the time ntpd set. Neither has a warning or an oops.The remove fix cannot be exercised on the twin: unbinding the flash under a mounted root is not something a camera does. On a camera, commit 1 changes two things: who frees the allocation, and that it starts zeroed. The only visible effect is
num_blocksinstatic_info, as above. Commit 2 changes only remove, suspend and resume, which a booting camera never runs. Not run on physical hardware.