[PATCH v4 3/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers

From: Ping Li

Date: Fri Dec 04 2026 - 04:17:00 EST


mtdchar_read() and mtdchar_write() so far rely on
mtd_kmalloc_up_to(), which halves the requested size until the
allocation succeeds. Under heavy memory fragmentation a
multi-megabyte read()/write() thus silently degrades into many
small mtd_read()/mtd_write() calls, which is visible when the
low-level driver cost scales with the number of transactions
(e.g. drivers forwarding each call to a remote controller).

Use mtd_alloc_buf() instead. Devices whose driver did not opt
into vmalloc'd buffers keep the current behaviour; vmalloc-safe
drivers keep the requested size. Also update the comment above
mtdchar_read(), which still described the old mitigation.

Signed-off-by: Ping Li <leeonion.muyu@xxxxxxxxx>
---
drivers/mtd/mtdchar.c | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index 2b6e70864bfbb..047540e24b01c 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -111,10 +111,10 @@ static int mtdchar_close(struct inode *inode, struct file *file)
* FIXME: This _really_ needs to die. In 2.5, we should lock the
* userspace buffer down and use it directly with readv/writev.
*
- * The implementation below, using mtd_kmalloc_up_to, mitigates
- * allocation failures when the system is under low-memory situations
- * or if memory is highly fragmented at the cost of reducing the
- * performance of the requested transfer due to a smaller buffer size.
+ * The implementation below, using mtd_alloc_buf(), mitigates
+ * allocation failures for devices that did not opt into
+ * vmalloc'd buffers by reducing the buffer size, while
+ * vmalloc-safe drivers keep the requested size.
*
* A more complex but more memory-efficient implementation based on
* get_user_pages and iovecs to cover extents of those pages is a
@@ -149,7 +149,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
return 0;

size = count;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;

@@ -193,7 +193,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);
+ mtd_free_buf(kbuf);
return -EFAULT;
}
else
@@ -205,13 +205,13 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
count = 0;
}
else {
- kfree(kbuf);
+ mtd_free_buf(kbuf);
return ret;
}

}

- kfree(kbuf);
+ mtd_free_buf(kbuf);
return total_retlen;
} /* mtdchar_read */

@@ -239,7 +239,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
return 0;

size = count;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;

@@ -247,7 +247,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);
+ mtd_free_buf(kbuf);
return -EFAULT;
}

@@ -294,12 +294,12 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
buf += retlen;
}
else {
- kfree(kbuf);
+ mtd_free_buf(kbuf);
return ret;
}
}

- kfree(kbuf);
+ mtd_free_buf(kbuf);
return total_retlen;
} /* mtdchar_write */

--
2.34.1