Repository navigation
mtd: spinand: bound the ID table walk by its entry count - #339
Conversation
drv_nand_read_id() looks the chip's ID up in spinand_flash_ids with a loop bounded by sizeof(spinand_flash_ids), which is the size of the table in bytes. On NT9856x the table has 25 entries of 40 bytes, so the bound is 1000. A listed chip matches early and the loop breaks, but for an unlisted chip the walk continues past the last entry and reads the memory after the table as more entries. The index is a uint8_t, so it wraps at 256 before it can reach 1000, and the loop ends only if some bytes past the table happen to match the ID. In practice the probe spins forever with info->lock held. Bound the loop with ARRAY_SIZE() instead. The SPI020 driver has a copy of the same function with the same loop over its own table; change it the same way. Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M) started by kexec: the chip is found and named from the table, ECC strength and bitflip threshold read 1, and UBI attaches and both UBIFS volumes mount. The SPI020 driver is built only for NA51068; it was compile-tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PR Summary by Qodomtd: spinand: bound ID lookups by table entry count
AI Description
Merge Risk
|
Code Review by Qodo🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)
Great, no issues found!Qodo reviewed your code and found no material issues that require reviewTip of the day💡 Did you know, you can switch off images and animations for a plain-text comment |
openipc-ai
left a comment
There was a problem hiding this comment.
Approving. Thanks for the quick follow-up. I checked each claim against the branch:
iisuint8_tin both copies ofdrv_nand_read_id(), andstruct nand_flash_devis 40 bytes on ARM32. With 25 entries that makes the bound 1000, whichican never reach, so the walk wraps over the table plus about 9 KB after it, forever, withinfo->lockheld. The SPI020 table has 15 entries, so the same applies there.- Both tables are real arrays, so
ARRAY_SIZE()'s__must_be_arraycheck is satisfied. No othersizeof(spinand_flash_ids)walk is left underdrivers/mtd. - A listed chip matches before the old bound mattered, so for every board that boots today the loop does exactly the same work.
One thing to watch when you add the NULL terminator nand_scan_with_ids() expects. The sentinel adds an entry whose mfr_id/dev_id are 0, and ARRAY_SIZE() counts it. A flash that answers 00 00 (absent, or not responding) would then match the sentinel in this loop and set page_size = 0, and page_per_block divides by it. Bound this loop with ARRAY_SIZE() - 1, or stop it at name == NULL, in the same change.
nand_scan_with_ids() walks the table it is given until it reaches an entry whose name is NULL. spinand_flash_ids has no such entry, in this driver or in the SPI020 driver, so for a chip that matches no entry nand_detect() runs past the end of the table and reads the memory after it as more entries. Add the terminator to both tables. drv_nand_read_id() bounds its own walk with ARRAY_SIZE(), which now counts the terminator. Its mfr_id and dev_id are 0, so a flash that answers 00 00, because it is absent or not responding, would match it, and spinand.c would then set page_size to 0 and divide by it. The review of #339 pointed this out and suggested stopping the loop at the entry with a NULL name, which both copies now do. An ID that matched no entry still made drv_nand_read_id() return success. spinand.c then went on with page and block sizes it never set, as flash_info comes from kmalloc(). Print the ID and return -ENODEV instead, so the probe ends in drv_nand_identify() with the existing "failed to identify flash" message. That error path freed mtd, which points into info, and info comes from devm_kzalloc(). The kfree() corrupted the slab, and later in the boot the kernel oopsed in clk_debug_init(). Drop it in both drivers; devres frees info when the probe fails. Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M) started by kexec: the flash is found and named, there are 10 partitions, ECC strength and bitflip threshold read 1, UBI attaches and both UBIFS volumes mount. Tested in the nt98566 machine of OpenIPC's qemu-bench with the same camera's flash image: the FORESEE ID cd 70 probes and the root filesystem mounts; for the IDs 12 34, 00 00 and ff ff the probe fails with "flash ID 0x.. 0x.. is not in the ID table" and the boot goes on until it cannot mount root, as expected. Without this change those three IDs end in the clk_debug_init() oops. The SPI020 driver is built only for NA51068; it was compile-tested. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This is the follow-up requested in the review of #335.
drv_nand_read_id()bounded its walk overspinand_flash_idswithsizeof(), which counts bytes (1000 for the 25-entry table). With auint8_tindex the loop cannot reach that bound. For a chip missing from the table it reads the memory after the table as more entries and normally never finishes, with the driver lock held. This PR bounds the loop withARRAY_SIZE(), and makes the same change in the identical function in the SPI020 driver (spi020_nand.c).Tested on a Reolink E340 (NT98566, FORESEE F35SQA512M): the flash is detected from the table, ECC strength and bitflip threshold read 1, and UBI and UBIFS mount.
spi020_nand.cwas compile-tested only (NA51068).The table also has no NULL terminator, which
nand_scan_with_ids()expects. That is left for a separate change.🤖 Generated with Claude Code