Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH v6] mtd: spi-nor: take the flash lock around spi_nor_restore()
@ 2026-11-18 17:10 Itai Handler
  0 siblings, 0 replies; only message in thread
From: Itai Handler @ 2026-11-18 17:10 UTC (permalink / raw)
  To: mwalle, pratyush
  Cc: miquel.raynal, richard, vigneshr, takahiro.kuwano, tudor.ambarus,
	linux-mtd, linux-kernel, stable

spi_nor_restore() runs on shutdown and unbind without nor->lock, while
the MTD device is still registered. Tasks are not frozen on reboot or
kexec, so the restore can run in the middle of a program or erase. A
busy flash ignores the command to exit 4-byte address mode, so a boot
ROM that reads the flash with 3-byte addresses then fails to boot the
board, and the soft reset aborts the program or erase, which can
corrupt data.

Take the lock in spi_nor_restore(), so that it waits for the operation
in flight, and refuse later operations with -EIO, as the CFI drivers do
after their reboot reset. Otherwise, a later operation could still be
running at the reboot or kexec, and after an exit from 4-byte mode, it
would still use 4-byte addresses.

In spi_nor_remove(), unregister the MTD device before the restore, so
that its users, such as mtdblock, can still write back their data. If
the SPI controller removes itself on shutdown, spi_nor_remove() finds
the flash already restored, and does not restore it again.

The wait can be long, as spi_nor_erase() holds the lock for the whole
request, up to a full chip erase. If the lock cannot be taken, warn and
restore without it, as before.

Backport note: from v6.4 on, apply this on top of
commit f594a33f9721 ("mtd: spi-nor: Unprepare legacy controllers upon
RWW wait interruption"), which is also tagged for stable; only the
context differs, e.g. spi_nor_restore() tests nor->flags before v7.4.
Before v6.4, the lock helper is spi_nor_lock_and_prep(), and it has no
_pe and _rd variants.

Fixes: 59b356ffd0b0 ("mtd: m25p80: restore the status of SPI flash when exiting")
Cc: stable@vger.kernel.org # see patch description, needs adjustments for <= 7.3
Signed-off-by: Itai Handler <itai.handler@gmail.com>
---
Changes in v6:
- Return -EIO instead of -ESHUTDOWN, as the CFI drivers do after their
  reboot reset (Michael).
- Move the _pe and _rd unlock helpers up instead of declaring them
  (Michael).
- Take the lock in spi_nor_restore() itself, and drop the
  spi_nor_lock_and_restore() helper (Michael).
- Rename the flag to "restored", test the flag rather than the error
  code, and set it also when the lock cannot be taken (Michael). As
  another operation can then read it at the same time, use READ_ONCE()
  and WRITE_ONCE() for it.
- In spi_nor_remove(), unregister the MTD device before the restore,
  so that the unregister can still write back data, e.g. mtdblock's
  cache.
- Shorten the commit message, and update the backport note.
- Drop the Assisted-by tag, which the documentation no longer asks for
  since commit 1e035297ffda ("docs: remove Assisted-by references").
  As with v5, I used an LLM to help write the code, the commit message
  and the QEMU tests.

Testing:
- Built with W=1 for arm64 on mainline, and its backports on v7.3,
  v6.18, v6.12 and v6.6.
- In QEMU (v7.3, ZynqMP GQSPI, n25q512a11 with broken-flash-reset, and
  the flash model patched to stay busy during an erase):
  - kexec while an application erases and programs the flash, with a
    3 s and a 10 s erase: the flash obeys EX4B, nothing reaches it
    after the restore, and the secondary kernel finds the flash.
  - kexec with the GQSPI driver changed to remove itself on shutdown:
    the kexec completes, and the flash is restored once.
  - Unbind with a write still in the page cache of mtdblock0: the data
    reaches the flash. With the restore before the unregister, the
    write-back fails ("lost async page write").

v5: https://lore.kernel.org/r/20261029210153.3986211-1-itai.handler@gmail.com
v4: https://lore.kernel.org/r/20261004104311.2196527-1-itai.handler@gmail.com
v3: https://lore.kernel.org/r/20260915113811.2429311-1-itai.handler@gmail.com

 drivers/mtd/spi-nor/core.c  | 87 +++++++++++++++++++++++++------------
 include/linux/mtd/spi-nor.h |  2 +
 2 files changed, 62 insertions(+), 27 deletions(-)

diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
index a04d22d2e2e1..44cb9c4a411b 100644
--- a/drivers/mtd/spi-nor/core.c
+++ b/drivers/mtd/spi-nor/core.c
@@ -1230,6 +1230,11 @@ int spi_nor_prep_and_lock(struct spi_nor *nor)
 		ret = wait_event_killable(nor->rww.wait,
 					  spi_nor_rww_start_exclusive(nor));
 
+	if (!ret && READ_ONCE(nor->restored)) {
+		spi_nor_unlock_and_unprep(nor);
+		return -EIO;
+	}
+
 	if (ret)
 		spi_nor_unprep(nor);
 
@@ -1290,6 +1295,18 @@ static void spi_nor_rww_end_pe(struct spi_nor *nor, loff_t start, size_t len)
 	rww->ongoing_pe = false;
 }
 
+static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len)
+{
+	if (!spi_nor_use_parallel_locking(nor)) {
+		mutex_unlock(&nor->lock);
+	} else {
+		spi_nor_rww_end_pe(nor, start, len);
+		wake_up(&nor->rww.wait);
+	}
+
+	spi_nor_unprep(nor);
+}
+
 static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t len)
 {
 	int ret;
@@ -1304,24 +1321,17 @@ static int spi_nor_prep_and_lock_pe(struct spi_nor *nor, loff_t start, size_t le
 		ret = wait_event_killable(nor->rww.wait,
 					  spi_nor_rww_start_pe(nor, start, len));
 
+	if (!ret && READ_ONCE(nor->restored)) {
+		spi_nor_unlock_and_unprep_pe(nor, start, len);
+		return -EIO;
+	}
+
 	if (ret)
 		spi_nor_unprep(nor);
 
 	return ret;
 }
 
-static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len)
-{
-	if (!spi_nor_use_parallel_locking(nor)) {
-		mutex_unlock(&nor->lock);
-	} else {
-		spi_nor_rww_end_pe(nor, start, len);
-		wake_up(&nor->rww.wait);
-	}
-
-	spi_nor_unprep(nor);
-}
-
 /* Internal locking helpers for read operations */
 static bool spi_nor_rww_start_rd(struct spi_nor *nor, loff_t start, size_t len)
 {
@@ -1366,6 +1376,18 @@ static void spi_nor_rww_end_rd(struct spi_nor *nor, loff_t start, size_t len)
 	rww->ongoing_rd = false;
 }
 
+static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len)
+{
+	if (!spi_nor_use_parallel_locking(nor)) {
+		mutex_unlock(&nor->lock);
+	} else {
+		spi_nor_rww_end_rd(nor, start, len);
+		wake_up(&nor->rww.wait);
+	}
+
+	spi_nor_unprep(nor);
+}
+
 static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t len)
 {
 	int ret;
@@ -1380,24 +1402,17 @@ static int spi_nor_prep_and_lock_rd(struct spi_nor *nor, loff_t start, size_t le
 		ret = wait_event_killable(nor->rww.wait,
 					  spi_nor_rww_start_rd(nor, start, len));
 
+	if (!ret && READ_ONCE(nor->restored)) {
+		spi_nor_unlock_and_unprep_rd(nor, start, len);
+		return -EIO;
+	}
+
 	if (ret)
 		spi_nor_unprep(nor);
 
 	return ret;
 }
 
-static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len)
-{
-	if (!spi_nor_use_parallel_locking(nor)) {
-		mutex_unlock(&nor->lock);
-	} else {
-		spi_nor_rww_end_rd(nor, start, len);
-		wake_up(&nor->rww.wait);
-	}
-
-	spi_nor_unprep(nor);
-}
-
 /*
  * Initiate the erasure of a single sector
  */
@@ -3231,7 +3246,16 @@ static void spi_nor_put_device(struct mtd_info *mtd)
 static void spi_nor_restore(struct spi_nor *nor)
 {
 	struct spi_nor_flash_parameter *params = nor->params;
-	int ret;
+	int lock_ret, ret;
+
+	/* Already restored, e.g. remove() after shutdown(). */
+	if (nor->restored)
+		return;
+
+	lock_ret = spi_nor_prep_and_lock(nor);
+	if (lock_ret)
+		dev_warn(nor->dev, "Failed to lock flash, restoring anyway, err = %d\n",
+			 lock_ret);
 
 	/* restore the addressing mode */
 	if (nor->addr_nbytes == 4 && !(params->flags & SNOR_F_4B_OPCODES) &&
@@ -3248,6 +3272,12 @@ static void spi_nor_restore(struct spi_nor *nor)
 
 	if (params->flags & SNOR_F_SOFT_RESET)
 		spi_nor_soft_reset(nor);
+
+	/* Refuse any further operations. */
+	WRITE_ONCE(nor->restored, true);
+
+	if (!lock_ret)
+		spi_nor_unlock_and_unprep(nor);
 }
 
 static const struct flash_info *spi_nor_match_name(struct spi_nor *nor,
@@ -3630,11 +3660,14 @@ static int spi_nor_probe(struct spi_mem *spimem)
 static int spi_nor_remove(struct spi_mem *spimem)
 {
 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
+	int ret;
+
+	/* Clean up MTD stuff before the restore refuses further operations. */
+	ret = mtd_device_unregister(&nor->mtd);
 
 	spi_nor_restore(nor);
 
-	/* Clean up MTD stuff. */
-	return mtd_device_unregister(&nor->mtd);
+	return ret;
 }
 
 static void spi_nor_shutdown(struct spi_mem *spimem)
diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h
index b3e3c6b10186..b9f4e041c4c2 100644
--- a/include/linux/mtd/spi-nor.h
+++ b/include/linux/mtd/spi-nor.h
@@ -361,6 +361,7 @@ struct spi_nor_flash_parameter;
  * @read_opcode:	the read opcode
  * @read_dummy:		the dummy needed by the read operation
  * @program_opcode:	the program opcode
+ * @restored:		the flash was restored, refuse further operations
  * @sst_write_second:	used by the SST write operation
  * @read_proto:		the SPI protocol for read operations
  * @write_proto:	the SPI protocol for write operations
@@ -402,6 +403,7 @@ struct spi_nor {
 	enum spi_nor_protocol	read_proto;
 	enum spi_nor_protocol	write_proto;
 	enum spi_nor_protocol	reg_proto;
+	bool			restored;
 	bool			sst_write_second;
 	struct sfdp		*sfdp;
 	struct dentry		*debugfs_root;

base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-18 17:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 17:10 [PATCH v6] mtd: spi-nor: take the flash lock around spi_nor_restore() Itai Handler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox