Re: [PATCH v1] mtd: nand: Fix BBT update issue

From: Miquel Raynal
Date: Fri Mar 19 2021 - 05:24:28 EST


Hi Yoshio,

+ Patrick

Yoshio Furuyama <ytc-mb-yfuruyama7@xxxxxxxxxx> wrote on Tue, 16 Feb
2021 09:37:55 +0900:

> Fixed issue of manages BBT (Bad Block Table).
> It didn't mark correctly when a specific block was bad block.
>
> This issue occurs when the bad block mark (3-bit chunk) is
> crosses over 32 bit (e.g. Block10, Block21...) unit.
>

Thanks for the patch and sorry for the very long wait period, I wanted
to understand better the issue but I didn't had the time to do it.

Would you mind having a look at Patrick's fix merged in U-Boot a year
ago:

commit 06fc4573b9d0878dd1d3b302884601263fe6e85f
Author: Doyle, Patrick <pdoyle@xxxxxxxxxx>
Date: Wed Jul 15 14:46:34 2020 +0000

Fix corner case in bad block table handling.

In the unlikely event that both blocks 10 and 11 are marked as bad (on a
32 bit machine), then the process of marking block 10 as bad stomps on
cached entry for block 11. There are (of course) other examples.

Signed-off-by: Patrick Doyle <pdoyle@xxxxxxxxxx>
Reviewed-by: Richard Weinberger <richard@xxxxxx>

diff --git a/drivers/mtd/nand/bbt.c b/drivers/mtd/nand/bbt.c
index 84d60b86521..294daee7b22 100644
--- a/drivers/mtd/nand/bbt.c
+++ b/drivers/mtd/nand/bbt.c
@@ -127,7 +127,7 @@ int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;

pos[1] &= ~GENMASK(rbits - 1, 0);
- pos[1] |= val >> rbits;
+ pos[1] |= val >> (bits_per_block - rbits);
}

return 0;

It looks both patches fix different errors but I wonder if merging Patrick's patch first would not make more sense.

Ideally, could you please send a series with the two patches, perhaps
Patrick's patch first, then yours, adding a Fixes and Cc: stable tags
to both?

> Signed-off-by: Yoshio Furuyama <ytc-mb-yfuruyama7@xxxxxxxxxx>
> ---
> drivers/mtd/nand/bbt.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/mtd/nand/bbt.c b/drivers/mtd/nand/bbt.c
> index 044adf913854..979c47e61381 100644
> --- a/drivers/mtd/nand/bbt.c
> +++ b/drivers/mtd/nand/bbt.c
> @@ -112,18 +112,20 @@ int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
> ((entry * bits_per_block) / BITS_PER_LONG);
> unsigned int offs = (entry * bits_per_block) % BITS_PER_LONG;
> unsigned long val = status & GENMASK(bits_per_block - 1, 0);
> + unsigned long shift = ((bits_per_block + offs <= BITS_PER_LONG) ?
> + (offs + bits_per_block - 1) : (BITS_PER_LONG - 1));
>
> if (entry >= nanddev_neraseblocks(nand))
> return -ERANGE;
>
> - pos[0] &= ~GENMASK(offs + bits_per_block - 1, offs);
> + pos[0] &= ~GENMASK(shift, offs);
> pos[0] |= val << offs;
>
> if (bits_per_block + offs > BITS_PER_LONG) {
> unsigned int rbits = bits_per_block + offs - BITS_PER_LONG;
>
> pos[1] &= ~GENMASK(rbits - 1, 0);
> - pos[1] |= val >> rbits;
> + pos[1] |= (val >> (BITS_PER_LONG - offs));
> }
>
> return 0;

Thanks,
Miquèl