[RFC PATCH v2 2/2] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers

From: Ping Li

Date: Mon Nov 23 2026 - 21:57:43 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, 13 insertions(+), 11 deletions(-)

diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index bf01e6ac72931..0142cad65aee3 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -111,10 +111,12 @@ 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
+ * The implementation below, using mtd_alloc_buf(), 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.
+ * or if memory is highly fragmented. Devices whose driver did not
+ * opt into vmalloc'd buffers get a reduced buffer size, while
+ * vmalloc-safe drivers keep the requested size, so large transfers
+ * are not degraded into many small ones.
*
* A more complex but more memory-efficient implementation based on
* get_user_pages and iovecs to cover extents of those pages is a
@@ -148,7 +150,7 @@ 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);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;

@@ -192,7 +194,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
@@ -204,13 +206,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 */

@@ -237,7 +239,7 @@ 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);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;

@@ -245,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;
}

@@ -292,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