linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Miquel Raynal <miquel.raynal@bootlin.com>
To: Wang Zhaolong <wangzhaolong@huaweicloud.com>
Cc: richard@nod.at,  vigneshr@ti.com,  linux-mtd@lists.infradead.org,
	linux-kernel@vger.kernel.org,  chengzhihao1@huawei.com,
	yi.zhang@huawei.com,  yangerkun@huawei.com
Subject: Re: [PATCH] mtd: core: only increment ecc_stats.badblocks on confirmed good->bad transition
Date: Tue, 02 Sep 2025 10:31:44 +0200	[thread overview]
Message-ID: <87ms7dntlb.fsf@bootlin.com> (raw)
In-Reply-To: <20250902080117.3658372-1-wangzhaolong@huaweicloud.com> (Wang Zhaolong's message of "Tue, 2 Sep 2025 16:01:17 +0800")

Hello Wang,

> @@ -2349,17 +2351,35 @@ int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
>  		return -EROFS;
>  
>  	if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
>  		ofs = (loff_t)mtd_div_by_eb(ofs, mtd) * master->erasesize;
>  
> -	ret = master->_block_markbad(master, mtd_get_master_ofs(mtd, ofs));
> +	moffs = mtd_get_master_ofs(mtd, ofs);
> +
> +	/* Pre-check: remember current state if available. */
> +	if (master->_block_isbad)
> +		oldbad = master->_block_isbad(master, moffs);
> +
> +	ret = master->_block_markbad(master, moffs);
>  	if (ret)
>  		return ret;
>  
> -	while (mtd->parent) {
> -		mtd->ecc_stats.badblocks++;
> -		mtd = mtd->parent;
> +	/*
> +	 * Post-check and bump stats only on a confirmed good->bad transition.
> +	 * If _block_isbad is not implemented, be conservative and do not bump.
> +	 */
> +	if (master->_block_isbad) {
> +		/* If it was already bad, nothing to do. */
> +		if (oldbad > 0)
> +			return 0;
> +
> +		if (master->_block_isbad(master, moffs) > 0) {
> +			while (mtd->parent) {
> +				mtd->ecc_stats.badblocks++;
> +				mtd = mtd->parent;
> +			}
> +		}

I don't think you can assume the block was already bad and must not be
accounted as a new bad block if you cannot verify that. In this case we
must remain conservative and tell userspace a new block was marked bad,
I believe.

Said otherwise, the { while () badblocks++ } block shall remain outside
of the if (_block_isbad) condition and remain untouched. Just bail out
early if you are sure this is not needed.

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2025-09-02  8:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-02  8:01 [PATCH] mtd: core: only increment ecc_stats.badblocks on confirmed good->bad transition Wang Zhaolong
2025-09-02  8:31 ` Miquel Raynal [this message]
2025-09-02  9:03   ` Wang Zhaolong
2025-09-02  9:20     ` Miquel Raynal

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ms7dntlb.fsf@bootlin.com \
    --to=miquel.raynal@bootlin.com \
    --cc=chengzhihao1@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=richard@nod.at \
    --cc=vigneshr@ti.com \
    --cc=wangzhaolong@huaweicloud.com \
    --cc=yangerkun@huawei.com \
    --cc=yi.zhang@huawei.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).