Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ping Li <leeonion.muyu@gmail.com>
To: miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com
Cc: tudor.ambarus@linaro.org, kyungmin.park@samsung.com,
	linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org,
	Ping Li <leeonion.muyu@gmail.com>
Subject: [PATCH v4 1/4] mtd: core: add per-device bounce buffer allocation helper
Date: Fri,  4 Dec 2026 17:16:37 +0800	[thread overview]
Message-ID: <20261204091640.444442-2-leeonion.muyu@gmail.com> (raw)
In-Reply-To: <20261204091640.444442-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.
    __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


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2026-12-04  9:17 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04  9:16 [PATCH v4 0/4] mtdchar: per-device bounce buffer policy Ping Li
2026-12-04  9:16 ` Ping Li [this message]
2026-12-04  9:39   ` [PATCH v4 1/4] mtd: core: add per-device bounce buffer allocation helper sashiko-bot
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:25   ` sashiko-bot
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:39   ` sashiko-bot
2026-12-04  9:16 ` [PATCH v4 4/4] mtd: hyperbus: rpc-if: accept vmalloc'd " Ping Li
2026-12-04  9:57   ` sashiko-bot

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=20261204091640.444442-2-leeonion.muyu@gmail.com \
    --to=leeonion.muyu@gmail.com \
    --cc=kyungmin.park@samsung.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miquel.raynal@bootlin.com \
    --cc=richard@nod.at \
    --cc=tudor.ambarus@linaro.org \
    --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