Re: [RFC PATCH 0/1] mtdchar: kvmalloc() for large read/write buffers?
From: Ping Li
Date: Sun Nov 22 2026 - 22:32:21 EST
From: leeonion.muyu@xxxxxxxxx
Hi Miquel,
Thanks for the review!
> I would be curious to know how this gets faster and why?
> Because the preferred path is obviously via DMA ...
The motivation on our platform is not per-byte CPU cost but
per-transaction cost: the low-level driver forwards the whole
transfer to a controller over a bus, so the latency of each
mtd_read()/mtd_write() call dominates. With the current
halving, a fragmented 2 MiB read silently turns into dozens
of small transactions, each paying a full round trip.
You are right that this argument does not hold for DMA-bound
drivers: for those a smaller kmalloc'd buffer given to the
DMA engine beats a vmalloc'd one bouncing through the CPU.
That is exactly why I like your suggestion: a per-device
policy avoids both failure modes -- DMA devices keep
contiguous buffers and their graceful halving degradation,
transaction-bound devices keep a large buffer regardless of
fragmentation.
While auditing I noticed that the fsmc/intel/r852 exposure
predates this RFC: since the MEMREAD/MEMWRITE conversion
(83208e106a8e8), a vmalloc'd ioctl buffer can already reach
their dma_map_single() under fragmentation, because none of
them declares NAND_USES_DMA, which is what activates
nand_base's virt_addr_valid() bounce. I would like to send
these three one-line fixes as an independent series right
away, as they fix a pre-existing issue regardless of the
direction taken here. Only powernv_flash (which implements
plain _read/_write and is unreachable from the ioctls) is
newly exposed by the proposed conversion, so it would come
as a prerequisite of the series.
The plan I would then follow:
1. Audit all mtd->_read/_write implementations for direct
__pa()/dma_map_single() use of the data buffer.
2. Introduce mtd->expects_dma_bufs and the allocation
helper, with powernv_flash declaring it.
3. Convert mtdchar_read()/mtdchar_write() to the helper.
4. Provide numbers from our platform, including the
DMA-bound counter-example.
Three questions before I start:
- Shall I wait for Richard's opinion first, or does this
plan already look reasonable enough to draft a series?
- For expects_dma_bufs=false devices, is plain kvmalloc()
acceptable (single kmalloc attempt, then vmalloc), or
should the halving stay for those too? Sashiko's
NOMMU/low-memory point suggests keeping it as a
last-resort fallback.
- One more question on the flag polarity. With a false
default (DMA drivers declare true), a forgetful DMA
driver would silently receive vmalloc'd buffers -- the
exact fsmc class of bug at the MTD layer. A true
default (vmalloc-safe drivers declare false) keeps the
failure mode benign at the cost of having no in-tree
"false" users initially. I slightly lean toward the
safe default, but I can see the dead-code concern.
Which polarity would you prefer?
Thanks,
Ping