* [PATCH v3 0/4] mtdchar: per-device bounce buffer policy
@ 2026-12-04 5:46 Ping Li
2026-12-04 5:46 ` [PATCH v3 1/4] mtd: core: add per-device bounce buffer allocation helper Ping Li
` (3 more replies)
0 siblings, 4 replies; 9+ messages in thread
From: Ping Li @ 2026-12-04 5:46 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd, linux-kernel, Ping Li
This is the third revision of the RFC series, following Miquel's
review [1][2]. It implements the agreed direction: low-level drivers
decide whether they may receive non-linear-map buffers, and a helper
applies the allocation policy accordingly.
Patch 1 introduces mtd->allows_vmalloc_bufs (defaults to false,
partitions inherit it from their master) and mtd_alloc_buf()/
mtd_free_buf(). By default buffers come from mtd_kmalloc_up_to(),
which shrinks the size under fragmentation. Drivers that can
handle non-linear-map buffers may set the flag to get
kvmalloc()'s vmalloc fallback instead, so a large read()/write()
no longer degrades into many small mtd_read()/mtd_write() calls.
Patch 2 converts mtdchar_read()/mtdchar_write() to the helper.
Patches 3 and 4 add the first two users. Both the hyperbus and
Samsung OneNAND controllers handle vmalloc'd buffers by design
(CPU copy fallback and per-page DMA mapping respectively), so they
opt in. These conversions are compile-tested only.
Changes in v3:
- Drop the RFC tag.
- Add two in-tree users (patches 3 and 4), per Miquel's request.
Numbers from our platform (an IPC-backed NOR flash device where
each mtd_read()/mtd_write() is forwarded to a remote controller):
- one transaction costs t ~= 20ms + 0.3ms/KB, i.e. ~620ms for
the 2M+2K transfer this platform uses for OTA updates;
- with the opt-in, every 2M+2K read() issued during the tests
(~80 runs across clean, fragmented and severely fragmented
memory states) produced exactly one transaction, in every
single run;
- without the opt-in, the same command was observed to produce
both single full-size and halved (len=1049600, two
transactions) transfers within the same session, depending on
transient allocator state. The split is not reproducible on
demand;
- wall-clock throughput is not the benefit: a halved transfer
pays only one extra fixed cost (~3%). The benefit is
determinism: the opt-in makes the transaction count and buffer
size independent of memory state, which is what
transaction-bound backends need.
[1] https://lore.kernel.org/linux-mtd/875wxtf8uy.fsf@bootlin.com/
[2] https://lore.kernel.org/linux-mtd/87ecbyk5wo.fsf@bootlin.com/
Ping Li (4):
mtd: core: add per-device bounce buffer allocation helper
mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
mtd: hyperbus: accept vmalloc'd bounce buffers
mtd: onenand: samsung: accept vmalloc'd bounce buffers
drivers/mtd/hyperbus/hyperbus-core.c | 1 +
drivers/mtd/mtdchar.c | 24 ++++++------
drivers/mtd/mtdcore.c | 44 ++++++++++++++++++++++
drivers/mtd/mtdpart.c | 1 +
drivers/mtd/nand/onenand/onenand_samsung.c | 2 +
include/linux/mtd/mtd.h | 16 ++++++++
6 files changed, 77 insertions(+), 11 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
--
2.34.1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v3 1/4] mtd: core: add per-device bounce buffer allocation helper
2026-12-04 5:46 [PATCH v3 0/4] mtdchar: per-device bounce buffer policy Ping Li
@ 2026-12-04 5:46 ` Ping Li
2026-12-04 6:03 ` sashiko-bot
2026-12-04 5:46 ` [PATCH v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Ping Li
` (2 subsequent siblings)
3 siblings, 1 reply; 9+ messages in thread
From: Ping Li @ 2026-12-04 5:46 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.
- 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
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
2026-12-04 5:46 [PATCH v3 0/4] mtdchar: per-device bounce buffer policy Ping Li
2026-12-04 5:46 ` [PATCH v3 1/4] mtd: core: add per-device bounce buffer allocation helper Ping Li
@ 2026-12-04 5:46 ` Ping Li
2026-12-04 6:03 ` sashiko-bot
2026-12-04 5:46 ` [PATCH v3 3/4] mtd: hyperbus: accept vmalloc'd " Ping Li
2026-12-04 5:46 ` [PATCH v3 4/4] mtd: onenand: samsung: " Ping Li
3 siblings, 1 reply; 9+ messages in thread
From: Ping Li @ 2026-12-04 5:46 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, 13 insertions(+), 11 deletions(-)
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index bf01e6ac72931..0142cad65aee3 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -111,10 +111,12 @@ 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
+ * The implementation below, using mtd_alloc_buf(), 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.
+ * or if memory is highly fragmented. Devices whose driver did not
+ * opt into vmalloc'd buffers get a reduced buffer size, while
+ * vmalloc-safe drivers keep the requested size, so large transfers
+ * are not degraded into many small ones.
*
* A more complex but more memory-efficient implementation based on
* get_user_pages and iovecs to cover extents of those pages is a
@@ -148,7 +150,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
if (!count)
return 0;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;
@@ -192,7 +194,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
@@ -204,13 +206,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 */
@@ -237,7 +239,7 @@ static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t c
if (!count)
return 0;
- kbuf = mtd_kmalloc_up_to(mtd, &size);
+ kbuf = mtd_alloc_buf(mtd, &size);
if (!kbuf)
return -ENOMEM;
@@ -245,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;
}
@@ -292,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
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v3 3/4] mtd: hyperbus: accept vmalloc'd bounce buffers
2026-12-04 5:46 [PATCH v3 0/4] mtdchar: per-device bounce buffer policy Ping Li
2026-12-04 5:46 ` [PATCH v3 1/4] mtd: core: add per-device bounce buffer allocation helper Ping Li
2026-12-04 5:46 ` [PATCH v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Ping Li
@ 2026-12-04 5:46 ` Ping Li
2026-12-04 6:04 ` sashiko-bot
2026-12-04 5:46 ` [PATCH v3 4/4] mtd: onenand: samsung: " Ping Li
3 siblings, 1 reply; 9+ messages in thread
From: Ping Li @ 2026-12-04 5:46 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd, linux-kernel, Ping Li
Both current hyperbus controllers fall back to a CPU copy when the
provided buffer is not usable for their DMA engine, so vmalloc'd
bounce buffers can be handed over safely. Opt in to keep large
read()/write() transfers in one piece under fragmentation.
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
drivers/mtd/hyperbus/hyperbus-core.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/drivers/mtd/hyperbus/hyperbus-core.c b/drivers/mtd/hyperbus/hyperbus-core.c
index 4d8047d43e48e..d61bd4fbc966a 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 = true;
ret = mtd_device_register(hbdev->mtd, NULL, 0);
if (ret) {
--
2.34.1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v3 4/4] mtd: onenand: samsung: accept vmalloc'd bounce buffers
2026-12-04 5:46 [PATCH v3 0/4] mtdchar: per-device bounce buffer policy Ping Li
` (2 preceding siblings ...)
2026-12-04 5:46 ` [PATCH v3 3/4] mtd: hyperbus: accept vmalloc'd " Ping Li
@ 2026-12-04 5:46 ` Ping Li
2026-12-04 6:04 ` sashiko-bot
3 siblings, 1 reply; 9+ messages in thread
From: Ping Li @ 2026-12-04 5:46 UTC (permalink / raw)
To: miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd, linux-kernel, Ping Li
The controller's DMA path handles vmalloc'd buffers by mapping
them page by page. Opt in to keep large read()/write() transfers
in one piece under fragmentation.
Signed-off-by: Ping Li <leeonion.muyu@gmail.com>
---
drivers/mtd/nand/onenand/onenand_samsung.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/drivers/mtd/nand/onenand/onenand_samsung.c b/drivers/mtd/nand/onenand/onenand_samsung.c
index 8c2d3866a6d6d..a50009345b5cb 100644
--- a/drivers/mtd/nand/onenand/onenand_samsung.c
+++ b/drivers/mtd/nand/onenand/onenand_samsung.c
@@ -931,6 +931,8 @@ static int s3c_onenand_probe(struct platform_device *pdev)
if (s3c_read_reg(MEM_CFG_OFFSET) & ONENAND_SYS_CFG1_SYNC_READ)
dev_info(&onenand->pdev->dev, "OneNAND Sync. Burst Read enabled\n");
+ mtd->allows_vmalloc_bufs = true;
+
err = mtd_device_register(mtd, pdata ? pdata->parts : NULL,
pdata ? pdata->nr_parts : 0);
if (err) {
--
2.34.1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v3 1/4] mtd: core: add per-device bounce buffer allocation helper
2026-12-04 5:46 ` [PATCH v3 1/4] mtd: core: add per-device bounce buffer allocation helper Ping Li
@ 2026-12-04 6:03 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-04 6:03 UTC (permalink / raw)
To: Ping Li, miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mtd: mtd_alloc_buf() allows huge OOM-prone GFP_KERNEL
kvmalloc() buffers
--
commit 45e643b09417a7039e8cef4d6194c99c2a1545b9
Author: Ping Li <leeonion.muyu@gmail.com>
mtd: core: add per-device bounce buffer allocation helper
This commit adds the mtd->allows_vmalloc_bufs flag and the
mtd_alloc_buf()/mtd_free_buf() helpers so opted-in MTD drivers can
receive vmalloc-backed bounce buffers via kvmalloc() instead of
mtd_kmalloc_up_to().
> 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)
[ ... ]
> +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);
> +}
[Severity: Medium]
Can kvmalloc(*size, GFP_KERNEL) with only a KMALLOC_MAX_SIZE cap trigger the
out-of-memory killer or allocate oversized bounce buffers on large transfers?
KMALLOC_MAX_SIZE scales with MAX_PAGE_ORDER + PAGE_SHIFT, reaching 512 MiB
on arm64 with 64K pages (32 MiB with 16K pages). In mtd_kmalloc_up_to(),
that cap only bounded an optimistic __GFP_NORETRY kmalloc() attempt that
halved down under pressure.
With kvmalloc(*size, GFP_KERNEL), the vmalloc fallback allocates order-0
pages with plain GFP_KERNEL, which drives reclaim and can invoke the
out-of-memory killer instead of returning NULL cleanly.
Existing kvmalloc() bounce buffers in mtdchar_write_ioctl() cap allocations
at mtd->erasesize. Bounding *size to a smaller ceiling and passing
__GFP_RETRY_MAYFAIL or __GFP_NORETRY would avoid oversized buffers and
out-of-memory kills.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204054630.3443342-1-leeonion.muyu@gmail.com?part=1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
2026-12-04 5:46 ` [PATCH v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Ping Li
@ 2026-12-04 6:03 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-04 6:03 UTC (permalink / raw)
To: Ping Li, miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [Medium] mtd: mtdchar_read()/mtdchar_write() pass unclamped size to
mtd_alloc_buf()
- [Medium] mtd: mtdchar: mtd_alloc_buf() drops low-memory size backoff
- [Medium] mtd: mtd_alloc_buf() caps kvmalloc() at arch-dependent
KMALLOC_MAX_SIZE
- [Low] mtd: inaccurate mtdchar_read() comment on mtd_alloc_buf()
behavior
--
commit 93073d569f815fa862de74b50cb104364b26cf86
Author: Ping Li <leeonion.muyu@gmail.com>
mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
This commit switches mtdchar_read() and mtdchar_write() from
mtd_kmalloc_up_to()/kfree() to mtd_alloc_buf()/mtd_free_buf() and updates
the comment above mtdchar_read().
> diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
> index bf01e6ac72931..0142cad65aee3 100644
> --- a/drivers/mtd/mtdchar.c
> +++ b/drivers/mtd/mtdchar.c
> @@ -111,10 +111,12 @@ 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
> + * The implementation below, using mtd_alloc_buf(), 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.
> + * or if memory is highly fragmented. Devices whose driver did not
> + * opt into vmalloc'd buffers get a reduced buffer size, while
> + * vmalloc-safe drivers keep the requested size, so large transfers
> + * are not degraded into many small ones.
> *
> * A more complex but more memory-efficient implementation based on
> * get_user_pages and iovecs to cover extents of those pages is a
[Severity: Low]
Is this comment accurate about mtd_alloc_buf()?
When mtd->allows_vmalloc_bufs is set, mtd_alloc_buf() does not reduce the
buffer size under low memory; it returns NULL so userspace gets -ENOMEM.
It also caps *size at KMALLOC_MAX_SIZE rather than keeping an arbitrarily
large requested size.
> @@ -148,7 +150,7 @@ static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
> if (!count)
> return 0;
>
> - kbuf = mtd_kmalloc_up_to(mtd, &size);
> + kbuf = mtd_alloc_buf(mtd, &size);
> if (!kbuf)
> return -ENOMEM;
[Severity: Medium]
Can mtdchar_read() and mtdchar_write() allocate a much larger bounce buffer
than the actual transfer?
Both functions initialize size = count before clamping count to
mtd->size - *ppos, then pass &size to mtd_alloc_buf(). A large read() near
EOF with only a few bytes left will therefore pass the unclamped count to
kvmalloc(*size, GFP_KERNEL) up to KMALLOC_MAX_SIZE without shrinking, wasting
memory and vmalloc space or triggering spurious -ENOMEM failures.
Updating size = count after clamping count would avoid over-allocating.
[Severity: Medium]
Does switching to mtd_alloc_buf() regress low-memory behavior on
allows_vmalloc_bufs devices?
mtd_kmalloc_up_to() backed off with __GFP_NORETRY down to
max(mtd->writesize, PAGE_SIZE) so transfers could still succeed under memory
pressure, as introduced in 3e45cf5e85a4 ("mtd: mtdchar: retry large buffer
allocations").
For opt-in devices, mtd_alloc_buf() instead calls kvmalloc(*size, GFP_KERNEL)
with no size-reduction fallback, so low memory or 32-bit vmalloc exhaustion
now triggers full direct reclaim or OOM kills and fails the syscall with
-ENOMEM.
Since mtd_alloc_buf() already caps *size at KMALLOC_MAX_SIZE and both callers
already loop over chunks of size, keeping a last-resort size backoff would
preserve low-memory reliability.
[Severity: Medium]
Should the bounce buffer cap in mtd_alloc_buf() be architecture-independent
instead of KMALLOC_MAX_SIZE?
KMALLOC_MAX_SIZE scales with PAGE_SHIFT and ARCH_FORCE_MAX_ORDER, reaching
32 MiB on 16 KiB-page arm64 and 512 MiB on 64 KiB-page arm64. Even with size
clamped to mtd->size - *ppos, a full-partition transfer on a 64 MiB HyperFlash
device can trigger a tens-of-megabytes GFP_KERNEL vmalloc() and risk OOM.
Capping the bounce buffer to a fixed limit or mtd->erasesize (as
mtdchar_read_ioctl() and mtdchar_write_ioctl() do) would avoid this.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204054630.3443342-1-leeonion.muyu@gmail.com?part=2
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v3 3/4] mtd: hyperbus: accept vmalloc'd bounce buffers
2026-12-04 5:46 ` [PATCH v3 3/4] mtd: hyperbus: accept vmalloc'd " Ping Li
@ 2026-12-04 6:04 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-04 6:04 UTC (permalink / raw)
To: Ping Li, miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] mtd: hyperbus: allows_vmalloc_bufs bypasses DMA on hbmc-am654
reads
- [Medium] mtd: hyperbus: allows_vmalloc_bufs exposes unclamped mtdchar
kvmalloc()
--
commit 29c3fce1e5634ffbe21dbeb565597da55a1a074e
Author: Ping Li <leeonion.muyu@gmail.com>
mtd: hyperbus: accept vmalloc'd bounce buffers
This commit sets allows_vmalloc_bufs on MTD devices registered through
hyperbus_register_device() so that mtd_alloc_buf() can allocate vmalloc
bounce buffers for HyperBus transfers.
> diff --git a/drivers/mtd/hyperbus/hyperbus-core.c b/drivers/mtd/hyperbus/hyperbus-core.c
> index 4d8047d43e48e..d61bd4fbc966a 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 = true;
>
> ret = mtd_device_register(hbdev->mtd, NULL, 0);
> if (ret) {
[Severity: Medium]
Can setting allows_vmalloc_bufs unconditionally regress read throughput on
hbmc-am654 by bypassing DMA?
With allows_vmalloc_bufs set, mtd_alloc_buf() uses kvmalloc(*size,
GFP_KERNEL) instead of mtd_kmalloc_up_to(). Above PAGE_SIZE,
kmalloc_gfp_adjust() drops __GFP_DIRECT_RECLAIM, so modest fragmentation
sends kvmalloc() to vmalloc instead of a smaller contiguous order.
am654_hbmc_dma_read() rejects vmalloc addresses via !virt_addr_valid(to),
so am654_hbmc_read() falls back to CPU memcpy_fromio() for the entire
transfer, losing the DMA speedup from 00c9cf49a613 ("mtd: hyperbus:
hbmc-am654: Add DMA support for reads").
Making allows_vmalloc_bufs a per-controller opt-in would preserve DMA on
hbmc-am654 while letting CPU-copy controllers like renesas-rpc-if opt in.
The commit message also says both current controllers fall back from a DMA
engine, whereas renesas-rpc-if has no DMA support.
[Severity: Medium]
Does opting HyperBus into allows_vmalloc_bufs expose an unclamped
allocation in mtdchar_read() and mtdchar_write()?
Both functions capture size = count before clamping count to
mtd->size - *ppos, then pass size to mtd_alloc_buf(). With
allows_vmalloc_bufs set, mtd_alloc_buf() calls kvmalloc(*size, GFP_KERNEL)
capped only at KMALLOC_MAX_SIZE, with no size backoff under pressure.
On ARM64 kernels with 16K or 64K pages, KMALLOC_MAX_SIZE is 32 MiB or
512 MiB. A read or write with a large count when only a few bytes remain
in a HyperBus partition therefore requests up to 512 MiB of non-swappable
kernel memory, risking reclaim stalls, spurious -ENOMEM, or OOM.
Clamping size after count is bounded by mtd->size - *ppos in
mtdchar_read() and mtdchar_write() would avoid allocating more than the
transfer can use.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204054630.3443342-1-leeonion.muyu@gmail.com?part=3
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v3 4/4] mtd: onenand: samsung: accept vmalloc'd bounce buffers
2026-12-04 5:46 ` [PATCH v3 4/4] mtd: onenand: samsung: " Ping Li
@ 2026-12-04 6:04 ` sashiko-bot
0 siblings, 0 replies; 9+ messages in thread
From: sashiko-bot @ 2026-12-04 6:04 UTC (permalink / raw)
To: Ping Li, miquel.raynal, richard, vigneshr
Cc: tudor.ambarus, kyungmin.park, linux-mtd
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mtd: onenand: samsung: vmalloc buffers demote unaligned
S5PC110 reads to PIO
--
commit 66ce872bce61be56a1aea4a1bc0e7bc0d241e303
Author: Ping Li <leeonion.muyu@gmail.com>
mtd: onenand: samsung: accept vmalloc'd bounce buffers
This commit sets mtd->allows_vmalloc_bufs to true in s3c_onenand_probe()
so mtdchar transfers can allocate vmalloc bounce buffers.
> diff --git a/drivers/mtd/nand/onenand/onenand_samsung.c b/drivers/mtd/nand/onenand/onenand_samsung.c
> index 8c2d3866a6d6d..a50009345b5cb 100644
> --- a/drivers/mtd/nand/onenand/onenand_samsung.c
> +++ b/drivers/mtd/nand/onenand/onenand_samsung.c
> @@ -931,6 +931,8 @@ static int s3c_onenand_probe(struct platform_device *pdev)
> if (s3c_read_reg(MEM_CFG_OFFSET) & ONENAND_SYS_CFG1_SYNC_READ)
> dev_info(&onenand->pdev->dev, "OneNAND Sync. Burst Read enabled\n");
>
> + mtd->allows_vmalloc_bufs = true;
[Severity: Medium]
Does enabling allows_vmalloc_bufs demote unaligned S5PC110 reads from DMA
to CPU PIO?
Contrary to the commit message, s5pc110_read_bufferram() does not map a
cross-page vmalloc buffer page by page; it bails out to PIO memcpy_fromio()
whenever a single-page transfer crosses a PAGE_SIZE boundary:
drivers/mtd/nand/onenand/onenand_samsung.c:s5pc110_read_bufferram() {
...
if (buf >= high_memory) {
struct page *page;
if (((size_t) buf & PAGE_MASK) !=
((size_t) (buf + count - 1) & PAGE_MASK))
goto normal;
...
}
With allows_vmalloc_bufs set, mtdchar_read() allocates via kvmalloc()
instead of halving contiguous kmalloc() allocations, so multi-page reads
fall back to vmalloc() readily under fragmentation because
kmalloc_gfp_adjust() strips __GFP_DIRECT_RECLAIM above PAGE_SIZE.
When reading from a 4-byte-aligned, non-writesize-aligned offset,
onenand_read_ops_nolock() advances buf by writesize - column after the
initial partial page, so 50% (2 KiB) to 100% (4 KiB) of later full-page
reads cross PAGE_SIZE and demote to PIO instead of using dma_map_single().
Even for aligned reads, falling directly to vmalloc() adds vmalloc PTE/TLB
overhead, per-page vmalloc_to_page() walks, and kmap_atomic_pfn() cache
maintenance in dma_cache_maint_page() on 32-bit ARM with CONFIG_HIGHMEM.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204054630.3443342-1-leeonion.muyu@gmail.com?part=4
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2026-12-04 6:04 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 5:46 [PATCH v3 0/4] mtdchar: per-device bounce buffer policy Ping Li
2026-12-04 5:46 ` [PATCH v3 1/4] mtd: core: add per-device bounce buffer allocation helper Ping Li
2026-12-04 6:03 ` sashiko-bot
2026-12-04 5:46 ` [PATCH v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers Ping Li
2026-12-04 6:03 ` sashiko-bot
2026-12-04 5:46 ` [PATCH v3 3/4] mtd: hyperbus: accept vmalloc'd " Ping Li
2026-12-04 6:04 ` sashiko-bot
2026-12-04 5:46 ` [PATCH v3 4/4] mtd: onenand: samsung: " Ping Li
2026-12-04 6:04 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox