Re: [PATCH v5 00/58] mtd: rawnand: retain chip and controller state across removal

From: James Hilliard

Date: Thu Dec 03 2026 - 15:10:06 EST


On Thu, Dec 3, 2026 at 3:46 AM Miquel Raynal <miquel.raynal@xxxxxxxxxxx> wrote:
>
> 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.

Those helpers give us a place to change the layout if needed. Embedding
mtd_info is fine as long as an MTD reference keeps the enclosing
allocation alive. With a devm-allocated nand_chip, unbind can free the
chip while an open file still holds a reference to its mtd_info.

> > 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.

Unregister already notifies MTD users, but it doesn't disconnect the
pointer saved by mtdchar_open(). Even a writable fd that never issued a
write calls mtd_sync() on close. On raw NAND that takes the chip and
controller locks. Skipping the sync alone wouldn't fix it either:
put_mtd_device() still needs a valid MTD object.

> > 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.

It is normal. By "indefinitely" I meant that userspace decides when to
close the fd, not that retaining the memory is a leak. For r852, it's
fine for an fd to keep some memory for an ejected card. What matters is
that it can be closed safely and can't read or write a replacement card.

> > 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.

The reason to try a core-owned MTD object is to keep the controller
drivers' existing devm_ cleanup. Unregister could stop new operations
from reaching the driver and drain those already in progress. The old
fd could then keep the disconnected object until close, without making
removal wait for that close.

>
> Thanks,
> Miquèl