2022-05-16 17:15:44

by Michał Kępień

[permalink] [raw]
Subject: [PATCH 2/2] mtdchar: use kvmalloc() for potentially large allocations

mtdchar_write_ioctl() calls kmalloc() with the 'size' argument set to
the smaller of two values: the write request's data/OOB length provided
by user space and the erase block size of the MTD device. If the latter
is large, kmalloc() may not be able to serve such allocation requests.
Use kvmalloc() instead. Correspondingly, replace kfree() calls with
kvfree() calls.

Suggested-by: Richard Weinberger <[email protected]>
Signed-off-by: Michał Kępień <[email protected]>
---
drivers/mtd/mtdchar.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index b2700f8467ff..05860288a7af 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -623,16 +623,16 @@ static int mtdchar_write_ioctl(struct mtd_info *mtd,

datbuf_len = min_t(size_t, req.len, mtd->erasesize);
if (datbuf_len > 0) {
- datbuf = kmalloc(datbuf_len, GFP_KERNEL);
+ datbuf = kvmalloc(datbuf_len, GFP_KERNEL);
if (!datbuf)
return -ENOMEM;
}

oobbuf_len = min_t(size_t, req.ooblen, mtd->erasesize);
if (oobbuf_len > 0) {
- oobbuf = kmalloc(oobbuf_len, GFP_KERNEL);
+ oobbuf = kvmalloc(oobbuf_len, GFP_KERNEL);
if (!oobbuf) {
- kfree(datbuf);
+ kvfree(datbuf);
return -ENOMEM;
}
}
@@ -682,8 +682,8 @@ static int mtdchar_write_ioctl(struct mtd_info *mtd,
usr_oob += ops.oobretlen;
}

- kfree(datbuf);
- kfree(oobbuf);
+ kvfree(datbuf);
+ kvfree(oobbuf);

return ret;
}
--
2.36.1




2022-06-08 06:16:56

by Richard Weinberger

[permalink] [raw]
Subject: Re: [PATCH 2/2] mtdchar: use kvmalloc() for potentially large allocations

----- Ursprüngliche Mail -----
> Von: "Michał Kępień" <[email protected]>
> An: "Miquel Raynal" <[email protected]>, "richard" <[email protected]>, "Vignesh Raghavendra" <[email protected]>
> CC: "linux-mtd" <[email protected]>, "linux-kernel" <[email protected]>
> Gesendet: Montag, 16. Mai 2022 09:06:01
> Betreff: [PATCH 2/2] mtdchar: use kvmalloc() for potentially large allocations

> mtdchar_write_ioctl() calls kmalloc() with the 'size' argument set to
> the smaller of two values: the write request's data/OOB length provided
> by user space and the erase block size of the MTD device. If the latter
> is large, kmalloc() may not be able to serve such allocation requests.
> Use kvmalloc() instead. Correspondingly, replace kfree() calls with
> kvfree() calls.
>
> Suggested-by: Richard Weinberger <[email protected]>
> Signed-off-by: Michał Kępień <[email protected]>

Looks good to me.
Acked-by: Richard Weinberger <[email protected]>

Thanks,
//richard

2022-06-09 13:26:33

by Miquel Raynal

[permalink] [raw]
Subject: Re: [PATCH 2/2] mtdchar: use kvmalloc() for potentially large allocations

On Mon, 2022-05-16 at 07:06:01 UTC, =?utf-8?b?TWljaGHFgiBLxJlwaWXFhA==?= wrote:
> mtdchar_write_ioctl() calls kmalloc() with the 'size' argument set to
> the smaller of two values: the write request's data/OOB length provided
> by user space and the erase block size of the MTD device. If the latter
> is large, kmalloc() may not be able to serve such allocation requests.
> Use kvmalloc() instead. Correspondingly, replace kfree() calls with
> kvfree() calls.
>
> Suggested-by: Richard Weinberger <[email protected]>
> Signed-off-by: Michał Kępień <[email protected]>
> Acked-by: Richard Weinberger <[email protected]>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel