* [PATCH v2 0/2] mtd: rawnand: serialize OTP and BBT recovery operations
@ 2026-11-22 9:25 James Hilliard
2026-11-22 9:25 ` [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations James Hilliard
2026-11-22 9:25 ` [PATCH v2 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery James Hilliard
0 siblings, 2 replies; 6+ messages in thread
From: James Hilliard @ 2026-11-22 9:25 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Arseniy Krasnov, Boris Brezillon, Kyle Roeschley
Cc: linux-mtd, linux-kernel, James Hilliard, stable
Use the existing chip/controller serialization for the Macronix OTP
transaction and for marker writes after a BBT erase or write failure.
Both paths issue NAND commands outside the ordinary MTD read/write
wrappers, so they must acquire the same locks and wait for suspended
chips themselves.
The first patch makes the existing helpers available within the raw
NAND core and uses them for Macronix OTP. The second uses them for BBT
recovery.
Assisted-by: Codex:gpt-6.2-astra
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
---
Changes in v2:
- Rebase on nand/next after the BBT logging change.
- Apply the Macronix locking change on top of the posted OTP error
propagation fix; both touch the OTP cleanup path.
- Link to v1: https://patch.msgid.link/20261108-submit-rawnand-operation-locking-v1-v1-0-565010fc5f8b@gmail.com
---
James Hilliard (2):
mtd: rawnand: macronix: serialize OTP access with NAND operations
mtd: rawnand: serialize bad-block marker writes during BBT recovery
drivers/mtd/nand/raw/internals.h | 2 ++
drivers/mtd/nand/raw/nand_base.c | 4 ++--
drivers/mtd/nand/raw/nand_bbt.c | 2 ++
drivers/mtd/nand/raw/nand_macronix.c | 2 ++
4 files changed, 8 insertions(+), 2 deletions(-)
---
base-commit: ec1e844b9486302a488abc03ea918db60709766e
base-tree: git://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git nand/next
change-id: 20261108-submit-rawnand-operation-locking-v1-1a0b8fe786d4
prerequisite-message-id: <20261108-submit-macronix-otp-errors-v1-v1-1-8f64a2ad5a10@gmail.com>
prerequisite-patch-id: a542c9cd698d2d4c090932b8d74dc6f520a71166
Best regards,
--
James Hilliard <james.hilliard1@gmail.com>
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations 2026-11-22 9:25 [PATCH v2 0/2] mtd: rawnand: serialize OTP and BBT recovery operations James Hilliard @ 2026-11-22 9:25 ` James Hilliard 2026-11-24 14:55 ` Miquel Raynal 2026-11-22 9:25 ` [PATCH v2 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery James Hilliard 1 sibling, 1 reply; 6+ messages in thread From: James Hilliard @ 2026-11-22 9:25 UTC (permalink / raw) To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, Arseniy Krasnov, Boris Brezillon, Kyle Roeschley Cc: linux-mtd, linux-kernel, James Hilliard, stable The Macronix OTP callbacks select a target and issue feature and page commands without taking the chip or controller locks. Normal NAND I/O can interleave with these commands, use a different target or access pages while OTP mode is enabled. OTP I/O can also run while the chip is suspended. Make the existing NAND serialization helpers available within the raw NAND core and hold them across the entire OTP transaction, including restoring normal mode and deselecting the target. This uses the same lock ordering and suspend handling as normal NAND I/O. Fixes: e87161321a40 ("mtd: rawnand: macronix: OTP access for MX30LFxG18AC") Cc: stable@vger.kernel.org Cc: Arseniy Krasnov <AVKrasnov@sberdevices.ru> Signed-off-by: James Hilliard <james.hilliard1@gmail.com> --- drivers/mtd/nand/raw/internals.h | 2 ++ drivers/mtd/nand/raw/nand_base.c | 4 ++-- drivers/mtd/nand/raw/nand_macronix.c | 2 ++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/mtd/nand/raw/internals.h b/drivers/mtd/nand/raw/internals.h index b8b29341b88c..2668f527e637 100644 --- a/drivers/mtd/nand/raw/internals.h +++ b/drivers/mtd/nand/raw/internals.h @@ -81,6 +81,8 @@ extern const struct nand_manufacturer_ops toshiba_nand_manuf_ops; extern const struct mtd_pairing_scheme dist3_pairing_scheme; /* Core functions */ +void nand_get_device(struct nand_chip *chip); +void nand_release_device(struct nand_chip *chip); const struct nand_manufacturer_desc *nand_get_manufacturer_desc(u8 id); int nand_bbm_get_next_page(struct nand_chip *chip, int page); int nand_markbad_bbm(struct nand_chip *chip, loff_t ofs); diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c index 58dbcb004a62..2f1c973b8d3b 100644 --- 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) { /* 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); 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; } -- 2.53.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations 2026-11-22 9:25 ` [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations James Hilliard @ 2026-11-24 14:55 ` Miquel Raynal 2026-11-24 16:10 ` James Hilliard 0 siblings, 1 reply; 6+ messages in thread From: Miquel Raynal @ 2026-11-24 14:55 UTC (permalink / raw) To: James Hilliard Cc: Richard Weinberger, Vignesh Raghavendra, Arseniy Krasnov, Boris Brezillon, Kyle Roeschley, linux-mtd, linux-kernel, stable > OTP I/O can also run while the chip is suspended. Really? Where does that come from? [...] > --- 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... > 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? Miquèl ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations 2026-11-24 14:55 ` Miquel Raynal @ 2026-11-24 16:10 ` James Hilliard 2026-11-26 17:34 ` Miquel Raynal 0 siblings, 1 reply; 6+ messages in thread From: James Hilliard @ 2026-11-24 16:10 UTC (permalink / raw) To: Miquel Raynal Cc: Richard Weinberger, Vignesh Raghavendra, Arseniy Krasnov, Boris Brezillon, Kyle Roeschley, linux-mtd, linux-kernel, stable On Tue, Nov 24, 2026 at 7:55 AM Miquel Raynal <miquel.raynal@bootlin.com> 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. > > > 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. Both use the same helper, so taking the locks there covers both paths without changing the interface. 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? > > Miquèl ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations 2026-11-24 16:10 ` James Hilliard @ 2026-11-26 17:34 ` Miquel Raynal 0 siblings, 0 replies; 6+ messages in thread From: Miquel Raynal @ 2026-11-26 17:34 UTC (permalink / raw) To: James Hilliard Cc: Richard Weinberger, Vignesh Raghavendra, Arseniy Krasnov, Boris Brezillon, Kyle Roeschley, linux-mtd, linux-kernel, stable On 24/11/2026 at 09:10:06 -07, James Hilliard <james.hilliard1@gmail.com> wrote: > On Tue, Nov 24, 2026 at 7:55 AM Miquel Raynal <miquel.raynal@bootlin.com> 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". ^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v2 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery 2026-11-22 9:25 [PATCH v2 0/2] mtd: rawnand: serialize OTP and BBT recovery operations James Hilliard 2026-11-22 9:25 ` [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations James Hilliard @ 2026-11-22 9:25 ` James Hilliard 1 sibling, 0 replies; 6+ messages in thread From: James Hilliard @ 2026-11-22 9:25 UTC (permalink / raw) To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra, Arseniy Krasnov, Boris Brezillon, Kyle Roeschley Cc: linux-mtd, linux-kernel, James Hilliard, stable When erasing or writing a flash-based BBT fails, write_bbt() marks the failed block bad before trying another reserved block. The erase and write helpers release the NAND locks before returning, but mark_bbt_block_bad() calls nand_markbad_bbm() without acquiring them. A BBT update triggered by a bad-block marking request can therefore issue the recovery marker commands concurrently with other NAND I/O or while the chip is suspended. The chip selection and shared page/OOB buffers are not protected on this path. Take the chip and controller locks around the recovery marker write, as nand_block_markbad_lowlevel() already does for normal marker writes. Fixes: 10ffd570f117 ("mtd: nand_bbt: scan for next free bbt block if writing bbt fails") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard <james.hilliard1@gmail.com> --- drivers/mtd/nand/raw/nand_bbt.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/drivers/mtd/nand/raw/nand_bbt.c b/drivers/mtd/nand/raw/nand_bbt.c index a18e10284b9f..a56597dfcea0 100644 --- a/drivers/mtd/nand/raw/nand_bbt.c +++ b/drivers/mtd/nand/raw/nand_bbt.c @@ -721,10 +721,12 @@ static void mark_bbt_block_bad(struct nand_chip *this, loff_t to; int res; + nand_get_device(this); bbt_mark_entry(this, block, BBT_BLOCK_WORN); to = (loff_t)block << this->bbt_erase_shift; res = nand_markbad_bbm(this, to); + nand_release_device(this); if (res) pr_warn("error %d while marking block %d bad\n", res, block); -- 2.53.0 ^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-26 17:34 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-22 9:25 [PATCH v2 0/2] mtd: rawnand: serialize OTP and BBT recovery operations James Hilliard 2026-11-22 9:25 ` [PATCH v2 1/2] mtd: rawnand: macronix: serialize OTP access with NAND operations James Hilliard 2026-11-24 14:55 ` Miquel Raynal 2026-11-24 16:10 ` James Hilliard 2026-11-26 17:34 ` Miquel Raynal 2026-11-22 9:25 ` [PATCH v2 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery James Hilliard
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox