Skip to contentSkip to conversation

drm/amdgpu: fix and cleanup shadow handling

PATCH In review Christian König 3 participants 4 messages Aug 18, 2017 – Aug 22, 2017 2 messages not in the archive

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

PATCH 1/1 drm/amdgpu: fix and cleanup shadow handling

From: Christian König <[email protected]>

Set the shadow flag on the shadow and not the parent, always bind shadow BOs during allocation instead of manually, use the reservation_object wrappers to grab the lock.

This fixes a couple of issues with binding the shadow BOs as well as correctly evicting them when memory becomes tight.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  6 ----
 drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 46 +++++++++++++++---------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     |  8 ------
 3 files changed, 23 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -6
index 9da391f..ff8f1a0 100644
@@ -2688,12 +2688,6 @@ static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
 			goto err;
 		}
 
-		r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
-		if (r) {
-			DRM_ERROR("%p bind failed\n", bo->shadow);
-			goto err;
-		}
-
 		r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
 						 NULL, fence, true);
 		if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +23 -23

77 lines not on this page · raw message

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -8
index 02c64d28..fe996bf 100644
@@ -165,14 +165,6 @@ static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
 	unsigned i;
 	int r;
 
-	if (parent->bo->shadow) {
-		struct amdgpu_bo *shadow = parent->bo->shadow;
-
-		r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
-		if (r)
-			return r;
-	}
-
 	if (use_cpu_for_update) {
 		r = amdgpu_bo_kmap(parent->bo, NULL);
 		if (r)
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 1503070160-7692-1-git-send-email-deathsimple-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

#2 lore raw

Ping? Can somebody take a look?

This is an bugfix and I would like to have it upstream before the next merge window closes.

Regards,
Christian

Am 18.08.2017 um 17:29 schrieb Christian König:

Quoted 126 lines

From: Christian König <[email protected]>

Set the shadow flag on the shadow and not the parent, always bind shadow BOs during allocation instead of manually, use the reservation_object wrappers to grab the lock.

This fixes a couple of issues with binding the shadow BOs as well as correctly evicting them when memory becomes tight.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  6 ----
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 46 +++++++++++++++---------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     |  8 ------
  3 files changed, 23 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -6
index 9da391f..ff8f1a0 100644
@@ -2688,12 +2688,6 @@ static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
  			goto err;
  		}
 
-		r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
-		if (r) {
-			DRM_ERROR("%p bind failed\n", bo->shadow);
-			goto err;
-		}
-
  		r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
  						 NULL, fence, true);
  		if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +23 -23
index e7e8991..9e495da 100644
@@ -91,7 +91,10 @@ static void amdgpu_ttm_placement_init(struct amdgpu_device *adev,
 
  	if (domain & AMDGPU_GEM_DOMAIN_GTT) {
  		places[c].fpfn = 0;
-		places[c].lpfn = 0;
+		if (flags & AMDGPU_GEM_CREATE_SHADOW)
+			places[c].lpfn = adev->mc.gart_size >> PAGE_SHIFT;
+		else
+			places[c].lpfn = 0;
  		places[c].flags = TTM_PL_FLAG_TT;
  		if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
  			places[c].flags |= TTM_PL_FLAG_WC |
@@ -446,17 +449,16 @@ static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
  	if (bo->shadow)
  		return 0;
 
-	bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
-	memset(&placements, 0,
-	       (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
-
-	amdgpu_ttm_placement_init(adev, &placement,
-				  placements, AMDGPU_GEM_DOMAIN_GTT,
-				  AMDGPU_GEM_CREATE_CPU_GTT_USWC);
+	memset(&placements, 0, sizeof(placements));
+	amdgpu_ttm_placement_init(adev, &placement, placements,
+				  AMDGPU_GEM_DOMAIN_GTT,
+				  AMDGPU_GEM_CREATE_CPU_GTT_USWC |
+				  AMDGPU_GEM_CREATE_SHADOW);
 
  	r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
  					AMDGPU_GEM_DOMAIN_GTT,
-					AMDGPU_GEM_CREATE_CPU_GTT_USWC,
+					AMDGPU_GEM_CREATE_CPU_GTT_USWC |
+					AMDGPU_GEM_CREATE_SHADOW,
  					NULL, &placement,
  					bo->tbo.resv,
  					0,
@@ -484,30 +486,28 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
  {
  	struct ttm_placement placement = {0};
  	struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
+	uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
  	int r;
 
-	memset(&placements, 0,
-	       (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
+	memset(&placements, 0, sizeof(placements));
+	amdgpu_ttm_placement_init(adev, &placement, placements,
+				  domain, parent_flags);
 
-	amdgpu_ttm_placement_init(adev, &placement,
-				  placements, domain, flags);
-
-	r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
-					domain, flags, sg, &placement,
-					resv, init_value, bo_ptr);
+	r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel, domain,
+					parent_flags, sg, &placement, resv,
+					init_value, bo_ptr);
  	if (r)
  		return r;
 
-	if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) {
-		if (!resv) {
-			r = ww_mutex_lock(&(*bo_ptr)->tbo.resv->lock, NULL);
-			WARN_ON(r != 0);
-		}
+	if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) {
+		if (!resv)
+			WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
+							NULL));
 
  		r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr));
 
  		if (!resv)
-			ww_mutex_unlock(&(*bo_ptr)->tbo.resv->lock);
+			reservation_object_unlock((*bo_ptr)->tbo.resv);
 
  		if (r)
  			amdgpu_bo_unref(bo_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -1
index 02c64d28..fe996bf 100644
@@ -165,14 +165,6 @@ static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
  	unsigned i;
  	int r;
 
-	if (parent->bo->shadow) {

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent, · line 170

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -7
-		struct amdgpu_bo *shadow = parent->bo->shadow;
-
-		r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
-		if (r)
-			return r;
-	}
-
  	if (use_cpu_for_update) {
  		r = amdgpu_bo_kmap(parent->bo, NULL);
  		if (r)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

A message that is not in the archive (sent off-list or not yet received): 6d3c9e84-29ef-736a-fa20-9ee5810bd0ac-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org

Reviewed-by
#4 lore raw
Quoted 4 lines · Christian König

Ping? Can somebody take a look?

This is an bugfix and I would like to have it upstream before the next merge window closes.

Quoted 149 lines

Regards,
Christian

Am 18.08.2017 um 17:29 schrieb Christian König:

Quoted 143 lines

From: Christian König <[email protected]>

Set the shadow flag on the shadow and not the parent, always bind shadow BOs
during allocation instead of manually, use the reservation_object wrappers to grab the lock.

This fixes a couple of issues with binding the shadow BOs as well as correctly
evicting them when memory becomes tight.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  6 ----
  drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 46
+++++++++++++++---------------
  drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     |  8 ------
  3 files changed, 23 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -5
index 9da391f..ff8f1a0 100644
@@ -2688,12 +2688,6 @@ static int amdgpu_recover_vram_from_shadow(struct
 amdgpu_device *adev,
                        goto err;
                }
  -             r = amdgpu_ttm_bind(&bo->shadow->tbo,
 &bo->shadow->tbo.mem);
-               if (r) {
-                       DRM_ERROR("%p bind failed\n", bo->shadow);
-                       goto err;
-               }
-
                r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
                                                 NULL, fence, true);
                if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +23 -19
index e7e8991..9e495da 100644
@@ -91,7 +91,10 @@ static void amdgpu_ttm_placement_init(struct
 amdgpu_device *adev,
        if (domain & AMDGPU_GEM_DOMAIN_GTT) {
                places[c].fpfn = 0;
-               places[c].lpfn = 0;
+               if (flags & AMDGPU_GEM_CREATE_SHADOW)
+                       places[c].lpfn = adev->mc.gart_size >> PAGE_SHIFT;
+               else
+                       places[c].lpfn = 0;
                places[c].flags = TTM_PL_FLAG_TT;
                if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
                        places[c].flags |= TTM_PL_FLAG_WC |
@@ -446,17 +449,16 @@ static int amdgpu_bo_create_shadow(struct
 amdgpu_device *adev,
        if (bo->shadow)
                return 0;
  -     bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
-       memset(&placements, 0,
-              (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
-
-       amdgpu_ttm_placement_init(adev, &placement,
-                                 placements, AMDGPU_GEM_DOMAIN_GTT,
-                                 AMDGPU_GEM_CREATE_CPU_GTT_USWC);
+       memset(&placements, 0, sizeof(placements));
+       amdgpu_ttm_placement_init(adev, &placement, placements,
+                                 AMDGPU_GEM_DOMAIN_GTT,
+                                 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
+                                 AMDGPU_GEM_CREATE_SHADOW);
        r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
                                        AMDGPU_GEM_DOMAIN_GTT,
-                                       AMDGPU_GEM_CREATE_CPU_GTT_USWC,
+                                       AMDGPU_GEM_CREATE_CPU_GTT_USWC |
+                                       AMDGPU_GEM_CREATE_SHADOW,
                                        NULL, &placement,
                                        bo->tbo.resv,
                                        0,
@@ -484,30 +486,28 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
  {
        struct ttm_placement placement = {0};
        struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
+       uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
        int r;
  -     memset(&placements, 0,
-              (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
+       memset(&placements, 0, sizeof(placements));
+       amdgpu_ttm_placement_init(adev, &placement, placements,
+                                 domain, parent_flags);
  -     amdgpu_ttm_placement_init(adev, &placement,
-                                 placements, domain, flags);
-
-       r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
-                                       domain, flags, sg, &placement,
-                                       resv, init_value, bo_ptr);
+       r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
 domain,
+                                       parent_flags, sg, &placement,
 resv,
+                                       init_value, bo_ptr);
        if (r)
                return r;
  -     if (amdgpu_need_backup(adev) && (flags &
 AMDGPU_GEM_CREATE_SHADOW)) {
-               if (!resv) {
-                       r = ww_mutex_lock(&(*bo_ptr)->tbo.resv->lock,
 NULL);
-                       WARN_ON(r != 0);
-               }
+       if ((flags & AMDGPU_GEM_CREATE_SHADOW) &&
 amdgpu_need_backup(adev)) {
+               if (!resv)
+
 WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
+                                                       NULL));
                r = amdgpu_bo_create_shadow(adev, size, byte_align,
 (*bo_ptr));
                if (!resv)
-                       ww_mutex_unlock(&(*bo_ptr)->tbo.resv->lock);
+                       reservation_object_unlock((*bo_ptr)->tbo.resv);
                if (r)
                        amdgpu_bo_unref(bo_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -7
index 02c64d28..fe996bf 100644
@@ -165,14 +165,6 @@ static int amdgpu_vm_validate_level(struct
 amdgpu_vm_pt *parent,
        unsigned i;
        int r;
  -     if (parent->bo->shadow) {
-               struct amdgpu_bo *shadow = parent->bo->shadow;
-
-               r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
-               if (r)
-                       return r;
-       }
-
        if (use_cpu_for_update) {
                r = amdgpu_bo_kmap(parent->bo, NULL);
Quoted 8 lines
Quoted 1 line
                if (r)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Reviewed-by Chunming Zhou
#5 lore raw
Quoted 7 lines · Alex Deucher
Quoted 4 lines · Christian König

Ping? Can somebody take a look?

This is an bugfix and I would like to have it upstream before the next merge window closes.

Quoted 156 lines
Quoted 148 lines

Regards,
Christian

Am 18.08.2017 um 17:29 schrieb Christian König:

Quoted 141 lines

From: Christian König <[email protected]>

Set the shadow flag on the shadow and not the parent, always bind shadow BOs
during allocation instead of manually, use the reservation_object wrappers to grab the lock.

This fixes a couple of issues with binding the shadow BOs as well as correctly
evicting them when memory becomes tight.

---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c |  6 ----
   drivers/gpu/drm/amd/amdgpu/amdgpu_object.c | 46
+++++++++++++++---------------
   drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c     |  8 ------
   3 files changed, 23 insertions(+), 37 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +0 -5
index 9da391f..ff8f1a0 100644
@@ -2688,12 +2688,6 @@ static int amdgpu_recover_vram_from_shadow(struct
 amdgpu_device *adev,
                         goto err;
                 }
   -             r = amdgpu_ttm_bind(&bo->shadow->tbo,
 &bo->shadow->tbo.mem);
-               if (r) {
-                       DRM_ERROR("%p bind failed\n", bo->shadow);
-                       goto err;
-               }
-
                 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
                                                  NULL, fence, true);
                 if (r) {
drivers/gpu/drm/amd/amdgpu/amdgpu_object.c +23 -19
index e7e8991..9e495da 100644
@@ -91,7 +91,10 @@ static void amdgpu_ttm_placement_init(struct
 amdgpu_device *adev,
         if (domain & AMDGPU_GEM_DOMAIN_GTT) {
                 places[c].fpfn = 0;
-               places[c].lpfn = 0;
+               if (flags & AMDGPU_GEM_CREATE_SHADOW)
+                       places[c].lpfn = adev->mc.gart_size >> PAGE_SHIFT;
+               else
+                       places[c].lpfn = 0;
                 places[c].flags = TTM_PL_FLAG_TT;
                 if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
                         places[c].flags |= TTM_PL_FLAG_WC |
@@ -446,17 +449,16 @@ static int amdgpu_bo_create_shadow(struct
 amdgpu_device *adev,
         if (bo->shadow)
                 return 0;
   -     bo->flags |= AMDGPU_GEM_CREATE_SHADOW;
-       memset(&placements, 0,
-              (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
-
-       amdgpu_ttm_placement_init(adev, &placement,
-                                 placements, AMDGPU_GEM_DOMAIN_GTT,
-                                 AMDGPU_GEM_CREATE_CPU_GTT_USWC);
+       memset(&placements, 0, sizeof(placements));
+       amdgpu_ttm_placement_init(adev, &placement, placements,
+                                 AMDGPU_GEM_DOMAIN_GTT,
+                                 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
+                                 AMDGPU_GEM_CREATE_SHADOW);
         r = amdgpu_bo_create_restricted(adev, size, byte_align, true,
                                         AMDGPU_GEM_DOMAIN_GTT,
-                                       AMDGPU_GEM_CREATE_CPU_GTT_USWC,
+                                       AMDGPU_GEM_CREATE_CPU_GTT_USWC |
+                                       AMDGPU_GEM_CREATE_SHADOW,
                                         NULL, &placement,
                                         bo->tbo.resv,
                                         0,
@@ -484,30 +486,28 @@ int amdgpu_bo_create(struct amdgpu_device *adev,
   {
         struct ttm_placement placement = {0};
         struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1];
+       uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW;
         int r;
   -     memset(&placements, 0,
-              (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place));
+       memset(&placements, 0, sizeof(placements));
+       amdgpu_ttm_placement_init(adev, &placement, placements,
+                                 domain, parent_flags);
   -     amdgpu_ttm_placement_init(adev, &placement,
-                                 placements, domain, flags);
-
-       r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
-                                       domain, flags, sg, &placement,
-                                       resv, init_value, bo_ptr);
+       r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel,
 domain,
+                                       parent_flags, sg, &placement,
 resv,
+                                       init_value, bo_ptr);
         if (r)
                 return r;
   -     if (amdgpu_need_backup(adev) && (flags &
 AMDGPU_GEM_CREATE_SHADOW)) {
-               if (!resv) {
-                       r = ww_mutex_lock(&(*bo_ptr)->tbo.resv->lock,
 NULL);
-                       WARN_ON(r != 0);
-               }
+       if ((flags & AMDGPU_GEM_CREATE_SHADOW) &&
 amdgpu_need_backup(adev)) {
+               if (!resv)
+
 WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv,
+                                                       NULL));
                 r = amdgpu_bo_create_shadow(adev, size, byte_align,
 (*bo_ptr));
                 if (!resv)
-                       ww_mutex_unlock(&(*bo_ptr)->tbo.resv->lock);
+                       reservation_object_unlock((*bo_ptr)->tbo.resv);
                 if (r)
                         amdgpu_bo_unref(bo_ptr);
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -7
index 02c64d28..fe996bf 100644
@@ -165,14 +165,6 @@ static int amdgpu_vm_validate_level(struct
 amdgpu_vm_pt *parent,
         unsigned i;
         int r;
   -     if (parent->bo->shadow) {
-               struct amdgpu_bo *shadow = parent->bo->shadow;
-
-               r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
-               if (r)
-                       return r;
-       }
-
         if (use_cpu_for_update) {
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent, · line 170

Quoted 2 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_vm.c +0 -0
                 r = amdgpu_bo_kmap(parent->bo, NULL);
                 if (r)

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

Quoted 4 lines

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx