Re: [RFC PATCH 08/10] dmaengine: reintroduce DMA_MEMCPY_SG

From: Daniel Byrne

Date: Thu Dec 03 2026 - 16:22:09 EST


(cc trimmed to the lists.)

On Tue, 1 Dec 2026 19:51:13 +0000, Frank Li wrote:
> The key is how resolve src_nents != dst_ndents case. and each sg is miss
> matched, for example:

Hi Frank,

Thanks for taking a look at this.

I kept the rule that provider.rst still has for this type:

No matter what the overall size of the combined chunks for source and
destination is, only as many bytes as the smallest of the two will be
transmitted. That means the number and size of the scatter-gather
buffers in both lists need not be the same, and that the operation
functionally is equivalent to a strncpy where the count argument
equals the smallest total size of the two scatter-gather list
buffers.

So the transfer ends where the shorter of the two lists ends.

The way I handle a mismatch is that the provider walks both lists
together. Each piece is as long as what is left of the current source
entry or of the current destination entry, whichever is smaller, and
then the entry that ran out moves on to the next one. That gives at
most src_nents + dst_nents - 1 pieces, so 7 for the 3 and 5 entries in
your example. If a provider cannot do the whole transfer, it returns
NULL from the prep call and the client falls back to the CPU.

Since there isn't a provider in this series, the patch doesn't show
this. I have an idxd implementation that builds one DSA batch
descriptor with one copy per piece. It is not posted yet because it
depends on idxd fixes that are not all posted, but I would be happy to
share it. This is the general idea:

dst_avail = sg_dma_len(dst_sg);
src_avail = sg_dma_len(src_sg);

for (i = 0; i < batch_max; i++) {
dma_dst = sg_dma_address(dst_sg) + sg_dma_len(dst_sg) -
dst_avail;
dma_src = sg_dma_address(src_sg) + sg_dma_len(src_sg) -
src_avail;

len = min_t(size_t, dst_avail, src_avail);

/* one MEMMOVE element of the batch: dma_src, dma_dst, len */

/* a list moves to its next entry when this one is used up */
dst_nents -= fetch_sg_and_pos(&dst_sg, &dst_avail, len);
src_nents -= fetch_sg_and_pos(&src_sg, &src_avail, len);

/* Stop when either scatter list is consumed. */
if (!dst_nents || !src_nents ||
!min_t(size_t, dst_avail, src_avail))
break;
}

I can send the whole patch as well if that helps. The client in patch
10 does not rely on mismatched lists either. Its two lists have one
entry per folio, with the same length on both sides.

Thanks,
Daniel