Re: [PATCH v2 3/5] rust: scatterlist: Add type-state abstraction for sg_table
From: Jason Gunthorpe
Date: Sat Aug 23 2025 - 10:33:44 EST
On Sat, Aug 23, 2025 at 10:22:47PM +0900, Alexandre Courbot wrote:
> For reasons I am not completely clear about, the number of mapped
> segments on the device side can be smaller than the number of
> scatterlists provided by the sg_table. This is highlighted by the
> documentation for `dma_map_sg_attrs` [1] ("Returns the number of mapped
> entries (which can be less than nents) on success") and `sg_dma_address`
> [2] ("You should only work with the number of sg entries dma_map_sg
> returns, or alternatively stop on the first sg_dma_len(sg) which is 0.")
> So only calling `sg_next` until we reach the end of the list carries the
> risk that we iterate on more items than we should, with the extra ones
> having their length at 0
Correct, this is misusing the API, and I don't know if the lengths are
even guarenteed to be zero. To iterate the DMA list you must use the
length of the DMA list returned by dma_map_sg() and nothing else as
the stop condition.
To repeat again, the scatterlist data structure is "optimized" and
contains two completely different lists - the CPU list and the DMA
list. The DMA list is always <= the size of the CPU list.
For all purposes they are completely seperate things and we have a
unique set of iterators and accessors for the CPU vs DMA data.
Jason