From: sashiko-bot@kernel.org
To: "Ping Li" <leeonion.muyu@gmail.com>,
miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com
Cc: tudor.ambarus@linaro.org, kyungmin.park@samsung.com,
linux-mtd@lists.infradead.org
Subject: Re: [PATCH v3 2/4] mtd: mtdchar: use mtd_alloc_buf() for the read/write bounce buffers
Date: Fri, 04 Dec 2026 06:03:12 +0000 [thread overview]
Message-ID: <sashiko-outbox-249175@kernel.org> (raw)
In-Reply-To: <20261204054630.3443342-3-leeonion.muyu@gmail.com>
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/
next prev parent reply other threads:[~2026-12-04 6:03 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
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
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=sashiko-outbox-249175@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=kyungmin.park@samsung.com \
--cc=leeonion.muyu@gmail.com \
--cc=linux-mtd@lists.infradead.org \
--cc=miquel.raynal@bootlin.com \
--cc=richard@nod.at \
--cc=sashiko-reviews@lists.linux.dev \
--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