Userptr sg tables are built with sg_alloc_table_from_pages(), which
merges physically contiguous pages into segments of up to
0xfffff000 bytes. iommu_dma_map_sg() pads a segment so that the next
one does not cross the DMA segment boundary, and adds the padding to
the 32-bit sg->length. With two large contiguous segments the padded
length reaches 4 GiB and wraps to 0.
On kernels without commit 1c8f6308e557 ("iommu/dma: Catch scatterlist
length overflows") the wrapped length makes iommu_map_sg() map less
than the allocated IOVA range. The IOVA is then freed without
unmapping it, and a later map at that address fails with -EADDRINUSE.
On kernels with that commit, iommu_dma_map_sg() returns -EOVERFLOW
instead. In both cases a large userptr allocation fails to pin. On
MI300X with the default DMA-FQ IOMMU domain, the first 32 GiB
hipHostMalloc after boot built 16 segments of up to 3.7 GiB, mapped
only 15.7 GiB and failed.
The padding is shorter than the next segment, so a padded segment
still fits sg->length when no segment exceeds half of it. Limit both
userptr sg tables to that, as xe does in commit 1b1d3710380d
("drm/xe: Apply upper limit to sg element size").
Signed-off-by: William Palacek <[email protected]>
---
drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c | 8 ++++----
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c | 7 ++++---
drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h | 7 +++++++
3 files changed, 15 insertions(+), 7 deletions(-)
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
index f060b206c255..565ecd7e012a 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_amdkfd_gpuvm.c
@@ -589,10 +589,10 @@ kfd_mem_dmamap_userptr(struct kgd_mem *mem,
return -ENOMEM;
/* Same sequence as in amdgpu_ttm_tt_pin_userptr */
- ret = sg_alloc_table_from_pages(ttm->sg, src_ttm->pages,
- ttm->num_pages, 0,
- (u64)ttm->num_pages << PAGE_SHIFT,
- GFP_KERNEL);
+ ret = sg_alloc_table_from_pages_segment(ttm->sg, src_ttm->pages,
+ ttm->num_pages, 0,
+ (u64)ttm->num_pages <<
PAGE_SHIFT,
+ AMDGPU_SG_MAX_SEGMENT,
GFP_KERNEL);
if (unlikely(ret))
goto free_sg;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
index 8dfa92967b63..b0672f1747ee 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -837,9 +837,10 @@ static int amdgpu_ttm_tt_pin_userptr(struct ttm_device
*bdev,
int r;
/* Allocate an SG array and squash pages into it */
- r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
- (u64)ttm->num_pages << PAGE_SHIFT,
- GFP_KERNEL);
+ r = sg_alloc_table_from_pages_segment(ttm->sg, ttm->pages,
+ ttm->num_pages, 0,
+ (u64)ttm->num_pages << PAGE_SHIFT,
+ AMDGPU_SG_MAX_SEGMENT,
GFP_KERNEL);
if (r)
goto release_sg;
diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
index 9856a2fc2d08..da7d7621aad1 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.h
@@ -44,6 +44,13 @@
#define AMDGPU_GTT_MAX_TRANSFER_SIZE (1ULL << 22)
+/*
+ * iommu_dma_map_sg() pads a segment so that the next one does not cross the
+ * DMA segment boundary. The padding is shorter than the next segment, so the
+ * padded length still fits sg->length if no segment exceeds half of it.
+ */
+#define AMDGPU_SG_MAX_SEGMENT round_down(UINT_MAX / 2, PAGE_SIZE)
+
extern const struct attribute_group amdgpu_vram_mgr_attr_group;
extern const struct attribute_group amdgpu_gtt_mgr_attr_group;
--
2.43.0