Skip to contentSkip to conversation

drm/amdgpu: fix check in gmc_v9_0_get_vm_pte()

PATCH In review Christian König 2 participants 2 messages Oct 31, 2024 – Nov 4, 2024

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

PATCH 1/1 drm/amdgpu: fix check in gmc_v9_0_get_vm_pte()

The coherency flags can only be determined when the BO is locked and that in turn is only guaranteed when the mapping is validated.

Fix the check, move the resource check into the function and add an assert that the BO is locked.

Fixes d1a372af1c3d ("drm/amdgpu: Set MTYPE in PTE based on BO flags")
---
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +8 -5
index f43ded8a0aab..50c5da3020cb 100644
@@ -1130,8 +1130,10 @@ static void gmc_v9_0_get_coherence_flags(struct amdgpu_device *adev,
 					 uint64_t *flags)
 {
 	struct amdgpu_device *bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
-	bool is_vram = bo->tbo.resource->mem_type == TTM_PL_VRAM;
-	bool coherent = bo->flags & (AMDGPU_GEM_CREATE_COHERENT | AMDGPU_GEM_CREATE_EXT_COHERENT);
+	bool is_vram = bo->tbo.resource &&
+		bo->tbo.resource->mem_type == TTM_PL_VRAM;
+	bool coherent = bo->flags & (AMDGPU_GEM_CREATE_COHERENT |
+				     AMDGPU_GEM_CREATE_EXT_COHERENT);
 	bool ext_coherent = bo->flags & AMDGPU_GEM_CREATE_EXT_COHERENT;
 	bool uncached = bo->flags & AMDGPU_GEM_CREATE_UNCACHED;
 	struct amdgpu_vm *vm = mapping->bo_va->base.vm;
@@ -1139,6 +1141,8 @@ static void gmc_v9_0_get_coherence_flags(struct amdgpu_device *adev,
 	bool snoop = false;
 	bool is_local;
 
+	dma_resv_assert_held(bo->tbo.base.resv);
+
 	switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
 	case IP_VERSION(9, 4, 1):
 	case IP_VERSION(9, 4, 2):
@@ -1257,9 +1261,8 @@ static void gmc_v9_0_get_vm_pte(struct amdgpu_device *adev,
 		*flags &= ~AMDGPU_PTE_VALID;
 	}
 
-	if (bo && bo->tbo.resource)
-		gmc_v9_0_get_coherence_flags(adev, mapping->bo_va->base.bo,
-					     mapping, flags);
+	if ((*flags & AMDGPU_PTE_VALID) && bo)
+		gmc_v9_0_get_coherence_flags(adev, bo, mapping, flags);
 }
 
 static void gmc_v9_0_override_vm_pte_flags(struct amdgpu_device *adev,
Signature · 1 line
2.34.1
Acked-by on v1 1/1
#1 lore raw
Quoted 9 lines · Christian König

The coherency flags can only be determined when the BO is locked and that in turn is only guaranteed when the mapping is validated.

Fix the check, move the resource check into the function and add an assert that the BO is locked.

Fixes d1a372af1c3d ("drm/amdgpu: Set MTYPE in PTE based on BO flags")
Quoted 45 lines
---
 drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/amd/amdgpu/gmc_v9_0.c +8 -6
index f43ded8a0aab..50c5da3020cb 100644
@@ -1130,8 +1130,10 @@ static void gmc_v9_0_get_coherence_flags(struct amdgpu_device *adev,
                                         uint64_t *flags)
 {
        struct amdgpu_device *bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
-       bool is_vram = bo->tbo.resource->mem_type == TTM_PL_VRAM;
-       bool coherent = bo->flags & (AMDGPU_GEM_CREATE_COHERENT | AMDGPU_GEM_CREATE_EXT_COHERENT);
+       bool is_vram = bo->tbo.resource &&
+               bo->tbo.resource->mem_type == TTM_PL_VRAM;
+       bool coherent = bo->flags & (AMDGPU_GEM_CREATE_COHERENT |
+                                    AMDGPU_GEM_CREATE_EXT_COHERENT);
        bool ext_coherent = bo->flags & AMDGPU_GEM_CREATE_EXT_COHERENT;
        bool uncached = bo->flags & AMDGPU_GEM_CREATE_UNCACHED;
        struct amdgpu_vm *vm = mapping->bo_va->base.vm;
@@ -1139,6 +1141,8 @@ static void gmc_v9_0_get_coherence_flags(struct amdgpu_device *adev,
        bool snoop = false;
        bool is_local;
 
+       dma_resv_assert_held(bo->tbo.base.resv);
+
        switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
        case IP_VERSION(9, 4, 1):
        case IP_VERSION(9, 4, 2):
@@ -1257,9 +1261,8 @@ static void gmc_v9_0_get_vm_pte(struct amdgpu_device *adev,
                *flags &= ~AMDGPU_PTE_VALID;
        }
 
-       if (bo && bo->tbo.resource)
-               gmc_v9_0_get_coherence_flags(adev, mapping->bo_va->base.bo,
-                                            mapping, flags);
+       if ((*flags & AMDGPU_PTE_VALID) && bo)
+               gmc_v9_0_get_coherence_flags(adev, bo, mapping, flags);
 }
 
 static void gmc_v9_0_override_vm_pte_flags(struct amdgpu_device *adev,
--

2.34.1