From: Zhihao Cheng <chengzhihao1@huawei.com>
To: zhouminqiang <zhouminqiang2@huawei.com>,
<miquel.raynal@bootlin.com>, <richard@nod.at>, <vigneshr@ti.com>,
<Artem.Bityutskiy@nokia.com>, <lumag@kernel.org>
Cc: <linux-mtd@lists.infradead.org>, <linux-kernel@vger.kernel.org>,
<yangerkun@huawei.com>, <yi.zhang@huawei.com>
Subject: Re: [PATCH] mtd: part: reject MTDPART_OFS_RETAIN in mtd_add_partition()
Date: Thu, 16 Jul 2026 21:31:40 +0800 [thread overview]
Message-ID: <3789ac0c-d9b0-99f8-4e5e-e10820e6518d@huawei.com> (raw)
In-Reply-To: <20260716131432.2355777-1-zhouminqiang2@huawei.com>
在 2026/7/16 21:14, zhouminqiang 写道:
> mtd_add_partition() does not reject the special offset value
> MTDPART_OFS_RETAIN (-3), which leads to a WARN_ON in
> add_mtd_device() when called through the BLKPG ioctl on NAND
> devices. The RETAIN value depends on cur_offset being the end of
> the previous partition, but in the dynamic partition path
> cur_offset equals the offset argument itself, causing undefined
> behavior.
>
> Commit 5daa7b21496a ("mtd: prepare partition add and del functions
> for ioctl requests") introduced mtd_add_partition() and correctly
> rejected MTDPART_OFS_APPEND (-1) and MTDPART_OFS_NXTBLK (-2),
> since those special offsets rely on cur_offset tracking the
> previous partition's end. However, commit 1a31368bf92e ("mtd: add a flags
> for partitions which should just leave smth. after them")
> later added MTDPART_OFS_RETAIN (-3) for the static
> partition table path without updating mtd_add_partition() to
> also reject this value.
>
> With offset=-3 passed via BLKPG, the RETAIN size calculation in
> allocate_partition() underflows (parent_size - 0xFFFFFFFFFFFFFFFD
> = parent_size + 3). If the underflow result does not appear to
> leave enough space, allocate_partition() jumps to out_register via
> goto, skipping erasesize initialization. This results in
> erasesize=0, which triggers:
>
> WARN_ON((!mtd->erasesize || !master->_erase) &&
> !(mtd->flags & MTD_NO_ERASE))
>
> in add_mtd_device(). If the underflow result appears to leave
> enough space, a bogus partition size is calculated, but the
> "out of reach" sanity check catches the invalid offset and
> creates a disabled empty partition (offset=0, size=0) instead
> of returning an error.
>
> Fix this by adding MTDPART_OFS_RETAIN to the rejection list in
> mtd_add_partition(), consistent with the existing handling of
> APPEND and NXTBLK.
>
> Fixes: 1a31368bf92e ("mtd: add a flags for partitions which should just leave smth. after them")
>
> Signed-off-by: zhouminqiang <zhouminqiang2@huawei.com>
> ---
> drivers/mtd/mtdpart.c | 3 ++-
> 1 file changed, 2 insertions(+), 1 deletion(-)
Reviewed-by: Zhihao Cheng <chengzhihao1@huawei.com>
>
> diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
> index 795a94e6b482..11cab777a1ac 100644
> --- a/drivers/mtd/mtdpart.c
> +++ b/drivers/mtd/mtdpart.c
> @@ -255,7 +255,8 @@ int mtd_add_partition(struct mtd_info *parent, const char *name,
>
> /* the direct offset is expected */
> if (offset == MTDPART_OFS_APPEND ||
> - offset == MTDPART_OFS_NXTBLK)
> + offset == MTDPART_OFS_NXTBLK ||
> + offset == MTDPART_OFS_RETAIN)
> return -EINVAL;
>
> if (length == MTDPART_SIZ_FULL)
>
next prev parent reply other threads:[~2026-07-16 13:31 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-16 13:14 zhouminqiang
2026-07-16 13:31 ` Zhihao Cheng [this message]
2026-07-17 15:50 ` 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=3789ac0c-d9b0-99f8-4e5e-e10820e6518d@huawei.com \
--to=chengzhihao1@huawei.com \
--cc=Artem.Bityutskiy@nokia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=lumag@kernel.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=vigneshr@ti.com \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
--cc=zhouminqiang2@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