From: Ping Li <leeonion.muyu@gmail.com>
To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com
Cc: tudor.ambarus@linaro.org, kyungmin.park@samsung.com,
linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
Ping Li <leeonion.muyu@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH v4 2/4] mtd: mtdchar: clamp bounce buffer size to the remaining transfer
Date: Fri, 4 Dec 2026 17:16:38 +0800 [thread overview]
Message-ID: <20261204091640.444442-3-leeonion.muyu@gmail.com> (raw)
In-Reply-To: <20261204091640.444442-1-leeonion.muyu@gmail.com>
mtdchar_read() and mtdchar_write() initialize the bounce buffer
size with the raw count from userspace, then clamp count to what
actually remains in the device. The bounce buffer therefore gets
allocated with the unclamped size: a read() of a few MiB near the
end of a small device allocates a few MiB of bounce buffer to
carry only a few KiB, which wastes memory and vmalloc space and
may even fail with -ENOMEM for a trivially small transfer.
Set the bounce buffer size after count has been clamped.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
drivers/mtd/mtdchar.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index bf01e6ac72931..2b6e70864bfbb 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -133,7 +133,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
size_t total_retlen=0;
int ret=0;
int len;
- size_t size = count;
+ size_t size;
char *kbuf;
pr_debug("MTD_read\n");
@@ -148,6 +148,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
if (!count)
return 0;
+ size = count;
kbuf = mtd_kmalloc_up_to(mtd, &size);
if (!kbuf)
return -ENOMEM;
@@ -219,7 +220,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
{
struct mtd_file_info *mfi = file->private_data;
struct mtd_info *mtd = mfi->mtd;
- size_t size = count;
+ size_t size;
char *kbuf;
size_t retlen;
size_t total_retlen=0;
@@ -237,6 +238,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
if (!count)
return 0;
+ size = count;
kbuf = mtd_kmalloc_up_to(mtd, &size);
if (!kbuf)
return -ENOMEM;
--
2.34.1
next prev parent reply other threads:[~2026-12-04 9:16 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 9:16 [PATCH v4 0/4] mtdchar: per-device bounce buffer policy Ping Li
2026-12-04 9:16 ` [PATCH v4 1/4] mtd: core: add per-device bounce buffer allocation helper Ping Li
2026-12-04 9:16 ` Ping Li [this message]
2026-12-04 9:16 ` [PATCH v4 3/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Ping Li
2026-12-04 9:16 ` [PATCH v4 4/4] mtd: hyperbus: rpc-if: accept vmalloc'd " Ping Li
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=20261204091640.444442-3-leeonion.muyu@gmail.com \
--to=leeonion.muyu@gmail.com \
--cc=kyungmin.park@samsung.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=stable@vger.kernel.org \
--cc=tudor.ambarus@linaro.org \
--cc=vigneshr@ti.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
all inboxes | Powered by JetHome®