[RFC PATCH 1/1] mtd: mtdchar: use kvmalloc() for large read/write buffers

sashiko-bot at kernel.org sashiko-bot at kernel.org
Thu Nov 19 01:30:59 PST 2026


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 at 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



More information about the linux-mtd mailing list