[PATCH v4 1/4] mtd: core: add per-device bounce buffer allocation helper

From: Ping Li

Date: Fri Dec 04 2026 - 04:16:53 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.
__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@xxxxxxxxx>
---
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