dma-direct: Fix missing sg_dma_len assignment in P2PDMA bus mappings
PATCH 1/1 dma-direct: Fix missing sg_dma_len assignment in P2PDMA bus mappings
Prior to commit a25e7962db0d7 ("PCI/P2PDMA: Refactor the p2pdma mapping helpers"), P2P segments were mapped using the pci_p2pdma_map_segment() helper. This helper was responsible for populating sg->dma_address, marking the bus address, and also setting sg_dma_len(sg).
The refactor[1] removed this helper and moved the mapping logic directly into the callers. While iommu_dma_map_sg() was correctly updated to set the length in the new flow, it was missed in dma_direct_map_sg().
Thus, in dma_direct_map_sg(), the PCI_P2PDMA_MAP_BUS_ADDR case sets the dma_address and marks the segment, but immediately executes 'continue', which causes the loop to skip the standard assignment logic at the end:
sg_dma_len(sg) = sg->length;
As a result, when CONFIG_NEED_SG_DMA_LENGTH is enabled, the dma_length field remains uninitialized (zero) for P2P bus address mappings. This breaks upper-layer drivers (for e.g. RDMA/IB) that rely on sg_dma_len() to determine the transfer size.
Fix this by explicitly setting the DMA length in the PCI_P2PDMA_MAP_BUS_ADDR case before continuing to the next scatterlist entry.
[1]
https://lore.kernel.org/all/ac14a0e94355bf898de65d023ccf8a2ad22a3ece.1746424934.git.leon@kernel.org/
--- kernel/dma/direct.c | 1 + 1 file changed, 1 insertion(+)
kernel/dma/direct.c +1 -0
@@ -481,6 +481,7 @@ int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,case PCI_P2PDMA_MAP_BUS_ADDR : sg->dma_address =pci_p2pdma_bus_addr_map (&p2pdma_state,sg_phys (sg)); +sg_dma_len (sg) = sg->length;sg_dma_mark_bus_address (sg);continue ;default :
Signature · 1 line
2.52.0.487.g5c8c507ade-goog
Quoted 3 lines · Pranjal Shrivastava
Fixes a25e7962db0d7 ("PCI/P2PDMA: Refactor the p2pdma mapping helpers")Reported-by Jacob Moroni <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>
Sad I missed that. Nice catch, thanks.
Quoted 28 lines · Pranjal Shrivastava
Prior to commit
a25e7962db0d7("PCI/P2PDMA: Refactor the p2pdma mapping helpers"), P2P segments were mapped using thepci_p2pdma_map_segment()helper. This helper was responsible for populatingsg->dma_address, marking the bus address, and also settingsg_dma_len(sg).The refactor[1] removed this helper and moved the mapping logic directly into the callers. While
iommu_dma_map_sg()was correctly updated to set the length in the new flow, it was missed indma_direct_map_sg().Thus, in
dma_direct_map_sg(), thePCI_P2PDMA_MAP_BUS_ADDRcase sets thedma_addressand marks the segment, but immediately executes 'continue', which causes the loop to skip the standard assignment logic at the end:sg_dma_len(sg) = sg->length;As a result, when
CONFIG_NEED_SG_DMA_LENGTHis enabled, thedma_lengthfield remains uninitialized (zero) for P2P bus address mappings. This breaks upper-layer drivers (for e.g. RDMA/IB) that rely onsg_dma_len()to determine the transfer size.Fix this by explicitly setting the DMA length in the
PCI_P2PDMA_MAP_BUS_ADDRcase before continuing to the next scatterlist entry.Fixes a25e7962db0d7 ("PCI/P2PDMA: Refactor the p2pdma mapping helpers")Reported-by Jacob Moroni <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>
Quoted 6 lines
[1]
https://lore.kernel.org/all/ac14a0e94355bf898de65d023ccf8a2ad22a3ece.1746424934.git.leon@kernel.org/--- kernel/dma/direct.c | 1 + 1 file changed, 1 insertion(+)
Thanks,
Shivaji Kant
Reviewed-byQuoted 37 lines · Leon Romanovsky
Quoted 28 lines · Pranjal Shrivastava
Prior to commit
a25e7962db0d7("PCI/P2PDMA: Refactor the p2pdma mapping helpers"), P2P segments were mapped using thepci_p2pdma_map_segment()helper. This helper was responsible for populatingsg->dma_address, marking the bus address, and also settingsg_dma_len(sg).The refactor[1] removed this helper and moved the mapping logic directly into the callers. While
iommu_dma_map_sg()was correctly updated to set the length in the new flow, it was missed indma_direct_map_sg().Thus, in
dma_direct_map_sg(), thePCI_P2PDMA_MAP_BUS_ADDRcase sets thedma_addressand marks the segment, but immediately executes 'continue', which causes the loop to skip the standard assignment logic at the end:sg_dma_len(sg) = sg->length;As a result, when
CONFIG_NEED_SG_DMA_LENGTHis enabled, thedma_lengthfield remains uninitialized (zero) for P2P bus address mappings. This breaks upper-layer drivers (for e.g. RDMA/IB) that rely onsg_dma_len()to determine the transfer size.Fix this by explicitly setting the DMA length in the
PCI_P2PDMA_MAP_BUS_ADDRcase before continuing to the next scatterlist entry.Fixes a25e7962db0d7 ("PCI/P2PDMA: Refactor the p2pdma mapping helpers")Reported-by Jacob Moroni <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>Quoted 6 lines
[1]
https://lore.kernel.org/all/ac14a0e94355bf898de65d023ccf8a2ad22a3ece.1746424934.git.leon@kernel.org/--- kernel/dma/direct.c | 1 + 1 file changed, 1 insertion(+)
Quoted 2 lines
Thanks,
Reviewed-by Leon Romanovsky <[email protected]>
Thanks,
Quoted 20 lines · Pranjal Shrivastava
Prior to commit
a25e7962db0d7("PCI/P2PDMA: Refactor the p2pdma mapping helpers"), P2P segments were mapped using thepci_p2pdma_map_segment()helper. This helper was responsible for populatingsg->dma_address, marking the bus address, and also settingsg_dma_len(sg).The refactor[1] removed this helper and moved the mapping logic directly into the callers. While
iommu_dma_map_sg()was correctly updated to set the length in the new flow, it was missed indma_direct_map_sg().Thus, in
dma_direct_map_sg(), thePCI_P2PDMA_MAP_BUS_ADDRcase sets thedma_addressand marks the segment, but immediately executes 'continue', which causes the loop to skip the standard assignment logic at the end:sg_dma_len(sg) = sg->length;As a result, when
CONFIG_NEED_SG_DMA_LENGTHis enabled, thedma_lengthfield remains uninitialized (zero) for P2P bus address mappings. This breaks upper-layer drivers (for e.g. RDMA/IB) that rely onsg_dma_len()to determine the transfer size.
Quoted 7 lines
Fix this by explicitly setting the DMA length in the
PCI_P2PDMA_MAP_BUS_ADDRcase before continuing to the next scatterlist entry.Fixes a25e7962db0d7 ("PCI/P2PDMA: Refactor the p2pdma mapping helpers")Reported-by Jacob Moroni <[email protected]>Signed-off-by Pranjal Shrivastava <[email protected]>
Applied to dma-mapping-fixes. Thanks!
Best regards
Signature · 2 lines
Marek Szyprowski, PhD Samsung R&D Institute Poland