* [PATCH v4 1/4] mtd: core: add per-device bounce buffer allocation helper
2026-12-04 9:16 [PATCH v4 0/4] mtdchar: per-device bounce buffer policy Ping Li
@ 2026-12-04 9:16 ` Ping Li
2026-12-04 9:16 ` [PATCH v4 2/4] mtd: mtdchar: clamp bounce buffer size to the remaining transfer Ping Li
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Ping Li @ 2026-12-04 9:16 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd, linux-kernel, Ping Li
Introduce mtd->allows_vmalloc_bufs and two helpers for the callers:
- mtd_alloc_buf(): by default buffers come from
mtd_kmalloc_up_to(), which gracefully reduces the size under
fragmentation so that drivers never receive a buffer which is
not DMA-able. Drivers that only memcpy() the buffer, or which
bounce it internally, may set the flag in their probe to get
kvmalloc()'s vmalloc fallback instead, so the requested size is
kept even when the buddy allocator is too fragmented to serve a
contiguous block; this avoids degrading large transfers into
many small ones. The size reduction is intentionally not kept
as a last resort for those devices: an allocation failure is
reported to userspace rather than silently shrinking the
buffer, so that the declared contract cannot be quietly broken.
__GFP_NORETRY is passed so the vmalloc fallback does not
invoke the OOM killer either.
- mtd_free_buf(): the matching release, kvfree() based.
The flag defaults to false (zero-initialized) and partitions
inherit it from their master, so a driver which does not opt in
-- on the master or any partition -- keeps today's behaviour and
can never unexpectedly receive a non-DMA-able buffer.
No functional change yet; mtdchar is converted later in this series.
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
drivers/mtd/mtdcore.c | 45 +++++++++++++++++++++++++++++++++++++++++
drivers/mtd/mtdpart.c | 1 +
include/linux/mtd/mtd.h | 9 +++++++++
3 files changed, 55 insertions(+)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index b170c3144ca8f..a96eb69407758 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -2566,6 +2566,51 @@ void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
}
EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
+/**
+ * mtd_alloc_buf - allocate a bounce buffer for an MTD device
+ * @mtd: the MTD device the buffer will be used for
+ * @size: requested size, updated to the size actually allocated
+ *
+ * By default (see mtd->allows_vmalloc_bufs) buffers come from
+ * mtd_kmalloc_up_to(), which gracefully reduces the size under
+ * fragmentation so that drivers never receive a buffer which is not
+ * DMA-able.
+ *
+ * Drivers that declared themselves vmalloc-safe (they only memcpy()
+ * the buffer or bounce it internally) get kvmalloc()'s fallback
+ * instead, so the requested size is kept even when the buddy
+ * allocator is too fragmented to serve a contiguous block; this
+ * avoids degrading large transfers into many small ones. The size
+ * reduction is intentionally NOT kept as a last resort for those
+ * devices: an allocation failure is reported to userspace rather
+ * than silently shrinking the buffer, so that the declared contract
+ * cannot be quietly broken. __GFP_NORETRY is passed so the vmalloc
+ * fallback does not invoke the OOM killer either.
+ *
+ * Buffers must be released with mtd_free_buf().
+ *
+ * Returns a pointer to the allocated buffer on success; otherwise, NULL.
+ */
+void *mtd_alloc_buf(const struct mtd_info *mtd, size_t *size)
+{
+ if (!mtd->allows_vmalloc_bufs)
+ return mtd_kmalloc_up_to(mtd, size);
+
+ *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
+ return kvmalloc(*size, GFP_KERNEL | __GFP_NORETRY);
+}
+EXPORT_SYMBOL_GPL(mtd_alloc_buf);
+
+/**
+ * mtd_free_buf - release a buffer allocated with mtd_alloc_buf()
+ * @buf: buffer returned by mtd_alloc_buf()
+ */
+void mtd_free_buf(void *buf)
+{
+ kvfree(buf);
+}
+EXPORT_SYMBOL_GPL(mtd_free_buf);
+
#ifdef CONFIG_PROC_FS
/*====================================================================*/
diff --git a/drivers/mtd/mtdpart.c b/drivers/mtd/mtdpart.c
index ddded0dbe77e0..9c245ad421a55 100644
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
@@ -75,6 +75,7 @@ static struct mtd_info *allocate_partition(struct mtd_info *parent,
child->oobsize = parent->oobsize;
child->oobavail = parent->oobavail;
child->subpage_sft = parent->subpage_sft;
+ child->allows_vmalloc_bufs = parent->allows_vmalloc_bufs;
child->name = name;
child->owner = parent->owner;
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 8d10d9d2e8300..e746c9d490041 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -368,6 +368,13 @@ struct mtd_info {
*/
bool oops_panic_write;
+ /*
+ * Set if the driver can handle vmalloc'd buffers (memcpy-only
+ * or internal bounce). Defaults to false: buffers always come
+ * from mtd_kmalloc_up_to().
+ */
+ bool allows_vmalloc_bufs;
+
struct notifier_block reboot_notifier; /* default mode before reboot */
/* ECC status information */
@@ -699,6 +706,8 @@ struct mtd_notifier {
extern void register_mtd_user (struct mtd_notifier *new);
extern int unregister_mtd_user (struct mtd_notifier *old);
void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
+void *mtd_alloc_buf(const struct mtd_info *mtd, size_t *size);
+void mtd_free_buf(void *buf);
static inline int mtd_is_bitflip(int err) {
return err == -EUCLEAN;
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v4 2/4] mtd: mtdchar: clamp bounce buffer size to the remaining transfer
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
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
3 siblings, 0 replies; 5+ messages in thread
From: Ping Li @ 2026-12-04 9:16 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd, linux-kernel, Ping Li, stable
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
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v4 3/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
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 ` [PATCH v4 2/4] mtd: mtdchar: clamp bounce buffer size to the remaining transfer Ping Li
@ 2026-12-04 9:16 ` Ping Li
2026-12-04 9:16 ` [PATCH v4 4/4] mtd: hyperbus: rpc-if: accept vmalloc'd " Ping Li
3 siblings, 0 replies; 5+ messages in thread
From: Ping Li @ 2026-12-04 9:16 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd, linux-kernel, Ping Li
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@gmail.com>
---
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
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v4 4/4] mtd: hyperbus: rpc-if: accept vmalloc'd bounce buffers
2026-12-04 9:16 [PATCH v4 0/4] mtdchar: per-device bounce buffer policy Ping Li
` (2 preceding siblings ...)
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 ` Ping Li
3 siblings, 0 replies; 5+ messages in thread
From: Ping Li @ 2026-12-04 9:16 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd, linux-kernel, Ping Li
rpc-if copies data with the CPU only: bulk reads end up in
memcpy_fromio() (rpcif_dirmap_read()), and writes go through
write16()/manual transfers, so vmalloc'd bounce buffers are
safe to hand over. Opt in to keep large read()/write()
transfers in one piece under fragmentation.
Only rpc-if opts in; am654-hbmc has a DMA fast path which a
vmalloc'd buffer would bypass, so it keeps the default behaviour.
The flag is set on the hyperbus device before registration and
copied to the mtd_info before mtd_device_register(): partitions
are parsed inside that call and inherit the flag from their
master at allocation time, so setting it on the registered
master would come too late for them.
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
drivers/mtd/hyperbus/hyperbus-core.c | 1 +
drivers/mtd/hyperbus/rpc-if.c | 1 +
include/linux/mtd/hyperbus.h | 2 ++
3 files changed, 4 insertions(+)
diff --git a/drivers/mtd/hyperbus/hyperbus-core.c b/drivers/mtd/hyperbus/hyperbus-core.c
index 4d8047d43e48e..be6dbed113385 100644
--- a/drivers/mtd/hyperbus/hyperbus-core.c
+++ b/drivers/mtd/hyperbus/hyperbus-core.c
@@ -114,6 +114,7 @@ int hyperbus_register_device(struct hyperbus_device *hbdev)
hbdev->mtd->dev.parent = dev;
mtd_set_of_node(hbdev->mtd, np);
+ hbdev->mtd->allows_vmalloc_bufs = hbdev->allows_vmalloc_bufs;
ret = mtd_device_register(hbdev->mtd, NULL, 0);
if (ret) {
diff --git a/drivers/mtd/hyperbus/rpc-if.c b/drivers/mtd/hyperbus/rpc-if.c
index 9eacc9ec7382d..dc172709d1267 100644
--- a/drivers/mtd/hyperbus/rpc-if.c
+++ b/drivers/mtd/hyperbus/rpc-if.c
@@ -142,6 +142,7 @@ static int rpcif_hb_probe(struct platform_device *pdev)
hyperbus->ctlr.dev = dev;
hyperbus->ctlr.ops = &rpcif_hb_ops;
hyperbus->hbdev.ctlr = &hyperbus->ctlr;
+ hyperbus->hbdev.allows_vmalloc_bufs = true;
hyperbus->hbdev.np = of_get_next_child(pdev->dev.parent->of_node, NULL);
error = hyperbus_register_device(&hyperbus->hbdev);
if (error)
diff --git a/include/linux/mtd/hyperbus.h b/include/linux/mtd/hyperbus.h
index bb6b7121a5427..c753cade9d82f 100644
--- a/include/linux/mtd/hyperbus.h
+++ b/include/linux/mtd/hyperbus.h
@@ -32,6 +32,7 @@ enum hyperbus_memtype {
* @ctlr: pointer to HyperBus controller struct
* @memtype: type of memory device: HyperFlash or HyperRAM
* @priv: pointer to controller specific per device private data
+ * @allows_vmalloc_bufs: whether the device may receive vmalloc'd buffers
*/
struct hyperbus_device {
@@ -41,6 +42,7 @@ struct hyperbus_device {
struct hyperbus_ctlr *ctlr;
enum hyperbus_memtype memtype;
void *priv;
+ bool allows_vmalloc_bufs;
};
/**
--
2.34.1
^ permalink raw reply [flat|nested] 5+ messages in thread