From: Ping Li <leeonion.muyu@gmail.com>
To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com
Cc: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
Ping Li <leeonion.muyu@gmail.com>
Subject: [RFC PATCH v2 1/2] mtd: core: add per-device bounce buffer allocation helper
Date: Tue, 24 Nov 2026 10:57:21 +0800 [thread overview]
Message-ID: <20261124025722.1476247-2-leeonion.muyu@gmail.com> (raw)
In-Reply-To: <20261124025722.1476247-1-leeonion.muyu@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-24 2:57 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 9:00 [RFC PATCH 0/1] mtdchar: kvmalloc() for large read/write buffers? ping.li
2026-11-19 9:08 ` [RFC PATCH 1/1] mtd: mtdchar: use kvmalloc() for large read/write buffers Ping Li
2026-11-19 9:50 ` [RFC PATCH 0/1] mtdchar: kvmalloc() for large read/write buffers? Miquel Raynal
2026-11-23 3:32 ` Ping Li
2026-11-23 8:35 ` Miquel Raynal
2026-11-24 2:57 ` [RFC PATCH v2 0/2] mtdchar: per-device bounce buffer policy Ping Li
2026-11-24 2:57 ` Ping Li [this message]
2026-11-24 2:57 ` [RFC PATCH v2 2/2] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Ping Li
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124025722.1476247-2-leeonion.muyu@gmail.com \
--to=leeonion.muyu@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=vigneshr@ti.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®