Re: [PATCH v5 00/58] mtd: rawnand: retain chip and controller state across removal
From: Miquel Raynal
Date: Thu Dec 03 2026 - 05:46:58 EST
Hi,
>> Maybe the problem is MTD refcounting? Maybe the problem is the devm_
>> orderging? I don't know, I haven't looked in depth at all the
>> patches. But the current approach is clearly not acceptable.
>
> The issue is what the MTD reference owns. The change in [1] defers MTD
> core cleanup until the last reference is dropped. In raw NAND, though,
> the mtd_info is embedded in the nand_chip. An MTD reference doesn't
> keep the driver's allocation containing that chip alive. Reordering
> devres cleanup can't protect a close that happens after detach.
>
> One way to keep the fix in the core would be to give consumers an MTD
> object whose storage is separate from the chip. At unregister, the core
> could disconnect that object from the driver and drain operations that
> already have access to it, much like NVMEM does for provider operations
> [2]. The driver could then free its private state. Reads and writes on
> the old file would return -ENODEV, and close would drop the reference
> without calling the driver. The main change for raw NAND is that it
> could no longer recover the chip from the consumer's mtd_info by
> assuming one is embedded in the other.
We already have helpers (nand_to_mtd, mtd_to_nand) doing that, so the
transition could be rather smooth. But storing the framework device in
the driver data is very common practice, so there must be another way to
handle the problem.
> The other way to free the chip at removal is to keep the current layout
> and have every consumer drop its MTD pointer at unregister.
There is already an mtd_unregister* call, so that would be pretty self
contained.
> That could
> work for mtdchar, but it would also have to cover in-kernel consumers
> such as UBI. Fixing the close path alone wouldn't be enough.
>
> A shared allocation helper or a per-provider reference, as proposed for
> SPI NOR [3], would keep the allocation until the last put. That's not a
> permanent leak, but an idle file could still pin driver-private memory
> indefinitely.
What's the problem? Isn't that normal behavior? If there is an user,
there is an user. I don't get why that's a problem.
> Waiting for the last put during removal would instead let
> the same file block removal indefinitely.
>
> Would it make sense to pursue the core-owned MTD object, so an open
> file can survive removal without keeping any driver-private memory
> alive?
You can try, but I will definitely discard v1's idea.
Thanks,
Miquèl