Re: [PATCH v5] mtd: spi-nor: take the flash lock around spi_nor_restore()

From: Itai Handler

Date: Wed Nov 18 2026 - 12:15:27 EST


On Wed Nov 18, 2026 at 2:33 PM CET, Michael Walle wrote:
>> + if (!ret && nor->shutdown) {
>> + spi_nor_unlock_and_unprep(nor);
>> + return -ESHUTDOWN;
>
> This will be propagated to userspace, correct? Not sure this is a
> valid error code outside of sockets. Maybe EIO?

Yes, it reaches user space, e.g. through the MTD char device. v6
returns -EIO, which is also what the CFI drivers return once they have
reset the flash for a reboot.

>> +static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len);
>> +
>
> Please avoid forward declarations and move the functions.

Done.

>> + ret = spi_nor_prep_and_lock(nor);
>> + if (ret == -ESHUTDOWN)
>
> This is actually really hard to follow. It's tested for even before
> it is set down below. Also probably wont work with an EIO return
> code. See more below

Agreed. v6 tests the flag instead.

>> + if (!ret) {
>> + nor->shutdown = true;
>
> Why don't you set the flag unconditionally?

No good reason. v6 sets it unconditionally. When the lock could not be
taken, another operation can then read the flag at the same time, so
v6 uses WRITE_ONCE() and READ_ONCE() for it.

> So why don't you just keep spi_nor_restore() and just add the
> locking there?
>
> What about the following:

v6 does that, as in your sketch, except that it tests the flag before
taking the lock. Your order works too, since spi_nor_prep_and_lock()
fails for a restored flash and releases the lock again, but testing
first does not rely on that. Only spi_nor_restore() sets the flag, and
it runs from ->shutdown() and ->remove(), which the driver core
serializes with the device lock, so that test needs no lock.

One more change in v6: spi_nor_remove() restored the flash before
mtd_device_unregister(), so with the flag, the unregister could no
longer write back data, e.g. mtdblock's cache. v6 unregisters first.

>> + * @shutdown: the flash was restored, refuse further operations
>
> shutdown only covers some of its meaning, it might also just get
> unbinded etc. So what about 'restored'?

Renamed to "restored".

v6 is out now.

Thanks,
Itai