From: sashiko-bot@kernel.org
To: "Ping Li" <leeonion.muyu@gmail.com>,
miquel.raynal@bootlin.com, richard@nod.at, vigneshr@ti.com
Cc: linux-mtd@lists.infradead.org
Subject: Re: [RFC PATCH 1/1] mtd: mtdchar: use kvmalloc() for large read/write buffers
Date: Thu, 19 Nov 2026 09:30:59 +0000 [thread overview]
Message-ID: <sashiko-outbox-222838@kernel.org> (raw)
In-Reply-To: <20261119090814.1355651-1-leeonion.muyu@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] mtd: mtdchar: vmalloc buffers from kvmalloc() break __pa() and
DMA drivers
- [Medium] mtd: mtdchar: removing mtd_kmalloc_up_to() backoff breaks
NOMMU and low-memory IO
- [Medium] mtd: mtdchar: premature vmalloc fallback degrades SPI-NOR and
DMA controllers
--
commit bc32b04018c61d2e1915dc5e166a083d5fe4285b
Author: Ping Li <leeonion.muyu@gmail.com>
mtd: mtdchar: use kvmalloc() for large read/write buffers
This commit replaces mtd_kmalloc_up_to() and kfree() in mtdchar_read() and
mtdchar_write() with kvmalloc() and kvfree(), capping the buffer size at
KMALLOC_MAX_SIZE.
> diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
> index bf01e6ac72931..442fac0769406 100644
> --- a/drivers/mtd/mtdchar.c
> +++ b/drivers/mtd/mtdchar.c
> @@ -148,7 +148,13 @@ 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);
> + /*
> + * Cap the buffer like mtd_kmalloc_up_to() used to; kvmalloc()
> + * then falls back to vmalloc() when the fragmented buddy
> + * allocator cannot serve the request contiguously.
> + */
> + size = min_t(size_t, count, KMALLOC_MAX_SIZE);
> + kbuf = kvmalloc(size, GFP_KERNEL);
> if (!kbuf)
> return -ENOMEM;
[Severity: High]
Can passing a vmalloc-backed kbuf from mtdchar_read() and mtdchar_write()
corrupt memory or leak uninitialized kernel memory in MTD drivers that
require physically contiguous linear-map buffers?
When count > PAGE_SIZE and a contiguous block of the full size is not
immediately free, kvmalloc() falls back to vmalloc(). Unlike the MEMREAD
and MEMWRITE ioctls (which require master->_read_oob and
master->_write_oob), mtd_read() and mtd_write() reach drivers that only
implement _read and _write, such as powernv_flash:
drivers/mtd/devices/powernv_flash.c:powernv_flash_async_op() {
switch (op) {
case FLASH_OP_READ:
rc = opal_flash_read(info->id, offset, __pa(buf), len, token);
break;
case FLASH_OP_WRITE:
rc = opal_flash_write(info->id, offset, __pa(buf), len, token);
break;
}
Running __pa() on a multi-page vmalloc address passes a bogus, physically
non-contiguous address to OPAL firmware, corrupting memory or leaving kbuf
uninitialized before copy_to_user().
Several raw NAND paths reachable from mtdchar_read() and mtdchar_write()
also call dma_map_single() directly on kbuf without bouncing vmalloc
addresses: fsmc_nand (dma_xfer()), intel-nand-controller (ebu_dma_start())
and r852 (r852_do_dma()) do not set NAND_USES_DMA or check
virt_addr_valid(), and MTD_FILE_MODE_OTP_USER calls
__macronix_30lfxg18ac_rw_otp() -> nand_read_page_op() /
nand_prog_page_op(), bypassing nand_base.c's NAND_USES_DMA bounce check.
[Severity: Medium]
Does dropping mtd_kmalloc_up_to()'s size backoff cause large reads and
writes to fail with -ENOMEM on NOMMU or low-memory systems?
mtd_kmalloc_up_to() halved the allocation size down to
max(mtd->writesize, PAGE_SIZE) with __GFP_NOWARN | __GFP_NORETRY before
falling back to GFP_KERNEL. On !CONFIG_MMU, kvmalloc() still calls
kmalloc() for the full size (up to KMALLOC_MAX_SIZE) with no size
reduction, so fragmented systems now fail with -ENOMEM instead of using a
smaller buffer. On CONFIG_MMU systems where free memory or 32-bit vmalloc
space cannot satisfy a multi-megabyte buffer at once, a single full-sized
GFP_KERNEL allocation can trigger the OOM killer or return -ENOMEM where a
smaller buffer previously succeeded.
Also, the comment block right above mtdchar_read() still states that
mtdchar_read() uses mtd_kmalloc_up_to() to mitigate low-memory and
fragmentation failures by reducing the buffer size.
[Severity: Medium]
Can falling back to vmalloc() on the first failed max-order kmalloc()
degrade throughput on SPI-NOR and DMA controllers compared to an
intermediate-sized kmalloc() buffer?
For size > PAGE_SIZE, kvmalloc() strips __GFP_DIRECT_RECLAIM from its
single kmalloc() attempt; if a full-size (e.g. 4 MiB) contiguous block is
not immediately free, it returns a vmalloc buffer even when 256 KiB or
512 KiB contiguous blocks are available. On SPI-NOR, a vmalloc buffer
triggers spi_nor_spimem_bounce(), clamping every transfer to
nor->bouncebuf_size (PAGE_SIZE) and adding a memcpy() per page. On
controllers that check virt_addr_valid() without an internal bounce buffer
-- including atmel_nand_data_in() / atmel_nand_data_out() (mentioned in
the commit message) and am654_hbmc_dma_read() -- a vmalloc buffer disables
DMA and falls back to PIO or MMIO memcpy_fromio().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119090055.1255028-1-ping.li@horizon.auto?part=1
______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
next prev parent reply other threads:[~2026-11-19 9:31 UTC|newest]
Thread overview: 13+ 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:00 ` 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:08 ` Ping Li
2026-11-19 9:30 ` sashiko-bot [this message]
2026-11-19 9:37 ` Ping Li
2026-11-19 9:37 ` Ping Li
2026-11-19 9:38 ` Ping Li
2026-11-19 9:38 ` Ping Li
2026-11-19 9:39 ` Ping Li
2026-11-19 9:39 ` Ping Li
2026-11-19 9:50 ` [RFC PATCH 0/1] mtdchar: kvmalloc() for large read/write buffers? Miquel Raynal
2026-11-19 9:50 ` Miquel Raynal
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-222838@kernel.org \
--to=sashiko-bot@kernel.org \
--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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.