Re: [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations

From: Miquel Raynal

Date: Thu Nov 26 2026 - 12:34:24 EST


On 24/11/2026 at 09:10:06 -07, James Hilliard <james.hilliard1@xxxxxxxxx> wrote:

> On Tue, Nov 24, 2026 at 7:55 AM Miquel Raynal <miquel.raynal@xxxxxxxxxxx> wrote:
>>
>>
>> > OTP I/O can also run while the chip is suspended.
>>
>> Really? Where does that come from?
>
> I was referring to chip->suspended in nand_base.c. nand_suspend() sets
> that flag even when the chip has no suspend hook. Normal NAND I/O waits
> for resume in nand_get_device(), but MTD calls the Macronix user-OTP
> read and write callbacks directly, bypassing that check.
>
> I haven't observed OTP I/O during system suspend. Userspace is normally
> frozen before device suspend, and the Macronix driver doesn't install
> its deep-power-down hook on the MX30LFxG18AC parts.
>
>>
>> [...]
>>
>> > --- a/drivers/mtd/nand/raw/nand_base.c
>> > +++ b/drivers/mtd/nand/raw/nand_base.c
>> > @@ -207,7 +207,7 @@ EXPORT_SYMBOL_GPL(nand_deselect_target);
>> > *
>> > * Release chip lock and wake up anyone waiting on the device.
>> > */
>> > -static void nand_release_device(struct nand_chip *chip)
>> > +void nand_release_device(struct nand_chip *chip)
>>
>> You export a core helper...
>>
>> > {
>> > /* Release the controller and the chip */
>> > mutex_unlock(&chip->controller->lock);
>> > @@ -336,7 +336,7 @@ static int nand_isbad_bbm(struct nand_chip *chip, loff_t ofs)
>> > *
>> > * Lock the device and its controller for exclusive access
>> > */
>> > -static void nand_get_device(struct nand_chip *chip)
>> > +void nand_get_device(struct nand_chip *chip)
>> > {
>> > /* Wait until the device is resumed. */
>> > while (1) {
>> > diff --git a/drivers/mtd/nand/raw/nand_macronix.c b/drivers/mtd/nand/raw/nand_macronix.c
>> > index 16847f28b19d..d7f5a9e7c240 100644
>> > --- a/drivers/mtd/nand/raw/nand_macronix.c
>> > +++ b/drivers/mtd/nand/raw/nand_macronix.c
>> > @@ -380,6 +380,7 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
>> > int ret, cleanup_ret;
>> >
>> > nand = mtd_to_nand(mtd);
>> > + nand_get_device(nand);
>>
>> ...and you use it across the entire OTP operation...
>
> The race doesn't require suspend: an ordinary page read on the same
> chip can issue its command after the OTP callback has enabled OTP mode
> and before it restores normal mode. Locking each command separately
> would still leave that window open.

?

You are using nand_get_device()/nand_release_device(), I am not
disputing that choice. You are using them in each and every otp op
implementation, which does not make any sense. It does not matter if
there is a single user for now.

>> > nand_select_target(nand, 0);
>> >
>> > ret = macronix_30lfxg18ac_otp_enable(nand);
>> > @@ -426,6 +427,7 @@ static int __macronix_30lfxg18ac_rw_otp(struct mtd_info *mtd,
>> > write ? "write" : "read");
>> >
>> > nand_deselect_target(nand);
>> > + nand_release_device(nand);
>> >
>> > return ret ? ret : cleanup_ret;
>> > }
>>
>> ...and in the commit log you mention:
>>
>> > Make the existing NAND serialization helpers available within the raw
>> > NAND core and hold them across the entire OTP transaction
>>
>> Why are you doing the serialization in one vendor driver then? Aren't
>> all the vendors affected by the exact same issue? Why isn't taking the
>> lock once for all in the core a better idea?
>
> Macronix is the only in-tree raw NAND manufacturer that installs MTD
> user-OTP read and write callbacks.

That's not a reason, we don't want the race to re-open every time
someone adds new OTP ops, right?

> Both use the same helper, so taking
> the locks there covers both paths without changing the interface.

You wouldn't be "changing the interface" whatever that actually means.

> A core wrapper could take the chip and controller locks around a
> manufacturer's OTP callback, much as nand_lock() does for
> chip->ops.lock_area. That would give future implementations the same
> locking, but it would mean adding raw NAND OTP hooks where MTD currently
> calls the one implementation directly. Is that the approach you had in
> mind?

That penultimate sentence does not mean anything, you should really
write your own answers. Anyway, I think the answer I should give here is
"yes".