Skip to contentSkip to conversation

drm/amdgpu: zero-initialize GART table on allocation

PATCH In review Philip Yang 2 participants 2 messages Apr 28 – Apr 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: zero-initialize GART table on allocation

GART TLB is flushed after unmapping but not after mapping. Since amdgpu_bo_create_kernel() does not zero-initialize the buffer, when a single PTE is written the TLB may speculatively load other uninitialized entries from the same cacheline. Those garbage entries can appear valid, and a subsequent write to another PTE in the same cacheline may cause the GPU to use a stale garbage PTE from the TLB.

Fix this by calling memset() to zero-initialize the GART table with gart_pte_flags immediately after allocation.

Using AMDGPU_GEM_CREATE_VRAM_CLEARED (SDMA-based clear) was considered, but is overkill here: the table is only ~1MB and initialized only once.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +10 -3
index 4d884180cf61..f09337bac0e5 100644
@@ -262,12 +262,19 @@ void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
  */
 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
 {
+	int r;
+
 	if (adev->gart.bo != NULL)
 		return 0;
 
-	return amdgpu_bo_create_kernel(adev,  adev->gart.table_size, PAGE_SIZE,
-				       AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.bo,
-				       NULL, (void *)&adev->gart.ptr);
+	r = amdgpu_bo_create_kernel(adev,  adev->gart.table_size, PAGE_SIZE,
+				    AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.bo,
+				    NULL, (void *)&adev->gart.ptr);
+	if (r)
+		return r;
+
+	memset(adev->gart.ptr, adev->gart.gart_pte_flags, adev->gart.table_size);
+	return 0;
 }
 
 /**
Signature · 1 line
2.50.1
maintainer
#1 lore raw
Quoted 9 lines · Philip Yang

GART TLB is flushed after unmapping but not after mapping. Since amdgpu_bo_create_kernel() does not zero-initialize the buffer, when a single PTE is written the TLB may speculatively load other uninitialized entries from the same cacheline. Those garbage entries can appear valid, and a subsequent write to another PTE in the same cacheline may cause the GPU to use a stale garbage PTE from the TLB.

Fix this by calling memset() to zero-initialize the GART table with gart_pte_flags immediately after allocation.

I'm surprised that we didn't previously had clearing of the GART table during initialization.

Quoted 2 lines

Using AMDGPU_GEM_CREATE_VRAM_CLEARED (SDMA-based clear) was considered, but is overkill here: the table is only ~1MB and initialized only once.

Well the main problem is that the SDMA needs the GART to be initialized to work.

Quoted 19 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +2 -0
index 4d884180cf61..f09337bac0e5 100644
@@ -262,12 +262,19 @@ void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
  */
 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
 {
+	int r;
+
 	if (adev->gart.bo != NULL)
 		return 0;
 

drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c void amdgpu_gart_table_ram_free(struct amdgpu_device *adev) · line 276

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +7 -3
-	return amdgpu_bo_create_kernel(adev,  adev->gart.table_size, PAGE_SIZE,
-				       AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.bo,
-				       NULL, (void *)&adev->gart.ptr);
+	r = amdgpu_bo_create_kernel(adev,  adev->gart.table_size, PAGE_SIZE,
+				    AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.bo,
+				    NULL, (void *)&adev->gart.ptr);
+	if (r)
+		return r;
+
+	memset(adev->gart.ptr, adev->gart.gart_pte_flags, adev->gart.table_size);

Please use memset_io() here, apart from that Reviewed-by: Christian König <[email protected]>

Regards,
Christian.

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c +1 -0
+	return 0;
 }
 
 /**