Re: [RFC PATCH 0/1] mtdchar: kvmalloc() for large read/write buffers?

From: Miquel Raynal

Date: Mon Nov 23 2026 - 03:35:43 EST


On 23/11/2026 at 11:32:12 +08, Ping Li <leeonion.muyu@xxxxxxxxx> wrote:

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

Fine.

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

I guess you can proceed, but Richard's opinion is indeed very welcome.

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

We should keep today's default, if there is one. I don't like kvmalloc
in this case because it may hide bugs.

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

We would not have 0 users, because there are plenty drivers already
doing bounce buffering which would therefore need to be converted to
expects_dma_bufs = true.

I think the default today is that the drivers may expect DMA-able
buffers and the subsystems enforce this by bouncing the data,
no?

Miquèl