[RFC PATCH v2 1/2] mtd: core: add per-device bounce buffer allocation helper
From: Ping Li
Date: Mon Nov 23 2026 - 21:57:39 EST
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.
- 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 in the following
patch.
Signed-off-by: Ping Li <leeonion.muyu@xxxxxxxxx>
---
drivers/mtd/mtdcore.c | 44 +++++++++++++++++++++++++++++++++++++++++
drivers/mtd/mtdpart.c | 1 +
include/linux/mtd/mtd.h | 16 +++++++++++++++
3 files changed, 61 insertions(+)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index b170c3144ca8f..83365c0fbf063 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -2566,6 +2566,50 @@ 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.
+ *
+ * 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);
+}
+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..4296d255c15d8 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -368,6 +368,20 @@ struct mtd_info {
*/
bool oops_panic_write;
+ /*
+ * Low-level drivers that only memcpy() the data buffers received
+ * from the MTD layer, or which handle non-linear-map buffers
+ * themselves (internal bounce), may set this to receive vmalloc'd
+ * buffers, keeping the requested size intact even under heavy
+ * memory fragmentation.
+ *
+ * Defaults to false: buffers always come from
+ * mtd_kmalloc_up_to(), which gracefully shrinks the size under
+ * fragmentation, so drivers never unexpectedly receive a buffer
+ * that is not DMA-able.
+ */
+ bool allows_vmalloc_bufs;
+
struct notifier_block reboot_notifier; /* default mode before reboot */
/* ECC status information */
@@ -699,6 +713,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;
--
2.34.1