[RFC PATCH 1/1] mtd: mtdchar: use kvmalloc() for large read/write buffers
From: Ping Li
Date: Thu Nov 19 2026 - 04:08:26 EST
mtdchar_read() and mtdchar_write() use mtd_kmalloc_up_to() for their
bounce buffer. Under memory fragmentation this helper halves the
requested size until kmalloc() succeeds, so a userspace read()/write()
of a few megabytes may silently degrade into many small mtd_read()/
mtd_write() calls: more per-chunk overhead, and device drivers whose
backend cost scales with the number of transactions pay a visible
throughput penalty.
The ioctl paths (MEMREAD/MEMWRITE) already use kvmalloc() for the same
purpose (83208e106a8e8 "mtdchar: use kvmalloc() for potentially large
allocations"). Use kvmalloc() in the read/write paths too: it tries
kmalloc() first and falls back to vmalloc() when the buddy allocator
cannot serve the request contiguously, so the buffer keeps its original
size instead of shrinking.
The size is still capped at KMALLOC_MAX_SIZE, as mtd_kmalloc_up_to()
did.
Most low-level MTD drivers only memcpy() the buffer, so vmalloc'd
buffers are fine there; controllers that DMA directly into the buffer
(brcmnand, gpmi, cadence, atmel, ...) check is_vmalloc_addr()/
virt_addr_valid() and fall back to a bounce path already.
Signed-off-by: Ping Li <leeonion.muyu@xxxxxxxxx>
---
drivers/mtd/mtdchar.c | 28 ++++++++++++++++++++--------
1 file changed, 20 insertions(+), 8 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index bf01e6ac7293..442fac076940 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -148,7 +148,13 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
if (!count)
return 0;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ /*
+ * Cap the buffer like mtd_kmalloc_up_to() used to; kvmalloc()
+ * then falls back to vmalloc() when the fragmented buddy
+ * allocator cannot serve the request contiguously.
+ */
+ size = min_t(size_t, count, KMALLOC_MAX_SIZE);
+ kbuf = kvmalloc(size, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;
@@ -192,7 +198,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
*ppos += retlen;
if (copy_to_user(buf, kbuf, retlen)) {
- kfree(kbuf);
+ kvfree(kbuf);
return -EFAULT;
}
else
@@ -204,13 +210,13 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
count = 0;
}
else {
- kfree(kbuf);
+ kvfree(kbuf);
return ret;
}
}
- kfree(kbuf);
+ kvfree(kbuf);
return total_retlen;
} /* mtdchar_read */
@@ -237,7 +243,13 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
if (!count)
return 0;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ /*
+ * Cap the buffer like mtd_kmalloc_up_to() used to; kvmalloc()
+ * then falls back to vmalloc() when the fragmented buddy
+ * allocator cannot serve the request contiguously.
+ */
+ size = min_t(size_t, count, KMALLOC_MAX_SIZE);
+ kbuf = kvmalloc(size, GFP_KERNEL);
if (!kbuf)
return -ENOMEM;
@@ -245,7 +257,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
len = min_t(size_t, count, size);
if (copy_from_user(kbuf, buf, len)) {
- kfree(kbuf);
+ kvfree(kbuf);
return -EFAULT;
}
@@ -292,12 +304,12 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
buf += retlen;
}
else {
- kfree(kbuf);
+ kvfree(kbuf);
return ret;
}
}
- kfree(kbuf);
+ kvfree(kbuf);
return total_retlen;
} /* mtdchar_write */
--
2.34.1