Re: [PATCH] mtd: phram: use div_u64_rem to stop overwrite len in phram_setup

Miquel Raynal
2021-01-04T10:09:52Z

Hi yangerkun,

yangerkun <yangerkun@huawei.com> wrote on Wed, 30 Dec 2020 10:17:18
+0800:

> We now support user to set erase page size, and use do_div between len
> and erase size to determine the reasonableness for the erase size.
> However, do_div is a macro and will overwrite the value of len. Which
> results a mtd device with unexcepted size. Fix it by use div_u64_rem.

with unexpected size. Use div_u64_rem instead.

(I will fix it when applying, no need to resend)

>
> Fixes: ffad560394de ("mtd: phram: Allow the user to set the erase page size.")
> Signed-off-by: yangerkun <yangerkun@huawei.com>
> ---
> drivers/mtd/devices/phram.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/mtd/devices/phram.c b/drivers/mtd/devices/phram.c
> index cfd170946ba4..6e72b0abcb71 100644
> --- a/drivers/mtd/devices/phram.c
> +++ b/drivers/mtd/devices/phram.c
> @@ -222,6 +222,7 @@ static int phram_setup(const char *val)
> uint64_t start;
> uint64_t len;
> uint64_t erasesize = PAGE_SIZE;
> + uint32_t rem;
> int i, ret;
>
> if (strnlen(val, sizeof(buf)) >= sizeof(buf))
> @@ -263,8 +264,9 @@ static int phram_setup(const char *val)
> }
> }
>
> + div_u64_rem(len, (uint32_t)erasesize, &rem);
> if (len == 0 || erasesize == 0 || erasesize > len
> - || erasesize > UINT_MAX || do_div(len, (uint32_t)erasesize) != 0) {
> + || erasesize > UINT_MAX || rem) {
> parse_err("illegal erasesize or len\n");
> goto error;
> }

Thanks,
Miquèl