[PATCH 2/2] mtd: rawnand: serialize bad-block marker writes during BBT recovery

From: James Hilliard

Date: Sun Nov 08 2026 - 16:24:07 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
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 b0e7592a2ab7..51fe991358d6 100644
--- a/drivers/mtd/nand/raw/nand_bbt.c
+++ b/drivers/mtd/nand/raw/nand_bbt.c
@@ -720,10 +720,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("nand_bbt: error %d while marking block %d bad\n",
res, block);

--
2.53.0