Skip to contentSkip to conversation

Revert "drm/amd: Stop evicting resources on APUs in suspend"

PATCH In review Alex Deucher 3 participants 10 messages May 1, 2025 – May 2, 2025

Subsystems (2)
lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/2 Revert "drm/amd: Stop evicting resources on APUs in suspend"

This reverts commit 3a9626c816db901def438dc2513622e281186d39.

This breaks S4 because we end up setting the s3/s0ix flags even when we are entering s4 since prepare is used by both flows. The causes both the S3/s0ix and s4 flags to be set which breaks several checks in the driver which assume they are mutually exclusive.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h        |  2 --
 drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c   | 18 ------------------
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 11 ++---------
 3 files changed, 2 insertions(+), 29 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -2
index cc26cf1bd843e..85063bcadeef8 100644
@@ -1656,11 +1656,9 @@ static inline void amdgpu_acpi_get_backlight_caps(struct amdgpu_dm_backlight_cap
 #if defined(CONFIG_ACPI) && defined(CONFIG_SUSPEND)
 bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev);
 bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev);
-void amdgpu_choose_low_power_state(struct amdgpu_device *adev);
 #else
 static inline bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev) { return false; }
 static inline bool amdgpu_acpi_is_s3_active(struct amdgpu_device *adev) { return false; }
-static inline void amdgpu_choose_low_power_state(struct amdgpu_device *adev) { }
 #endif
 
 void amdgpu_register_gpu_instance(struct amdgpu_device *adev);
drivers/gpu/drm/amd/amdgpu/amdgpu_acpi.c +0 -18
index b7f8f2ff143dd..707e131f89d23 100644
@@ -1533,22 +1533,4 @@ bool amdgpu_acpi_is_s0ix_active(struct amdgpu_device *adev)
 #endif /* CONFIG_AMD_PMC */
 }
 
-/**
- * amdgpu_choose_low_power_state
- *
- * @adev: amdgpu_device_pointer
- *
- * Choose the target low power state for the GPU
- */
-void amdgpu_choose_low_power_state(struct amdgpu_device *adev)
-{
-	if (adev->in_runpm)
-		return;
-
-	if (amdgpu_acpi_is_s0ix_active(adev))
-		adev->in_s0ix = true;
-	else if (amdgpu_acpi_is_s3_active(adev))
-		adev->in_s3 = true;
-}
-
 #endif /* CONFIG_SUSPEND */
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +2 -9
index b29d1bd1de07d..71d95f16067a4 100644
@@ -5016,15 +5016,13 @@ int amdgpu_device_prepare(struct drm_device *dev)
 	struct amdgpu_device *adev = drm_to_adev(dev);
 	int i, r;
 
-	amdgpu_choose_low_power_state(adev);
-
 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
 		return 0;
 
 	/* Evict the majority of BOs before starting suspend sequence */
 	r = amdgpu_device_evict_resources(adev);
 	if (r)
-		goto unprepare;
+		return r;
 
 	flush_delayed_work(&adev->gfx.gfx_off_delay_work);
 
@@ -5035,15 +5033,10 @@ int amdgpu_device_prepare(struct drm_device *dev)
 			continue;
 		r = adev->ip_blocks[i].version->funcs->prepare_suspend(&adev->ip_blocks[i]);
 		if (r)
-			goto unprepare;
+			return r;
 	}
 
 	return 0;
-
-unprepare:
-	adev->in_s0ix = adev->in_s3 = adev->in_s4 = false;
-
-	return r;
 }
 
 /**
Signature · 1 line
2.49.0
author maintainer
#1 lore raw

PATCH 2/2 drm/amdgpu: fix pm notifier handling

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
 2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +13 -12
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
  * @data: data
  *
  * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
  */
 static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
 				     void *data)
 {
 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-	int r;
 
 	switch (mode) {
 	case PM_HIBERNATION_PREPARE:
 		adev->in_s4 = true;
-		fallthrough;
+		break;
+	case PM_POST_HIBERNATION:
+		adev->in_s4 = false;
+		break;
 	case PM_SUSPEND_PREPARE:
-		r = amdgpu_device_evict_resources(adev);
-		/*
-		 * This is considered non-fatal at this time because
-		 * amdgpu_device_prepare() will also fatally evict resources.
-		 * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-		 */
-		if (r)
-			drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+		if (amdgpu_acpi_is_s0ix_active(adev))
+			adev->in_s0ix = true;
+		else if (amdgpu_acpi_is_s3_active(adev))
+			adev->in_s3 = true;
+		break;
+	case PM_POST_SUSPEND:
+		adev->in_s0ix = adev->in_s3 = false;
 		break;
 	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
 	struct drm_device *drm_dev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-	if (amdgpu_acpi_is_s0ix_active(adev))
-		adev->in_s0ix = true;
-	else if (amdgpu_acpi_is_s3_active(adev))
-		adev->in_s3 = true;
 	if (!adev->in_s0ix && !adev->in_s3) {
 		/* don't allow going deep first time followed by s2idle the next time */
 		if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
 {
 	struct drm_device *drm_dev = dev_get_drvdata(dev);
 	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-	int r;
 
 	if (!adev->in_s0ix && !adev->in_s3)
 		return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
 	if (!pci_device_is_present(adev->pdev))
 		adev->no_hw_access = true;
 
-	r = amdgpu_device_resume(drm_dev, true);
-	if (amdgpu_acpi_is_s0ix_active(adev))
-		adev->in_s0ix = false;
-	else
-		adev->in_s3 = false;
-	return r;
+	return amdgpu_device_resume(drm_dev, true);
 }
 
 static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
 static int amdgpu_pmops_thaw(struct device *dev)
 {
 	struct drm_device *drm_dev = dev_get_drvdata(dev);
-	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-	int r;
 
-	r = amdgpu_device_resume(drm_dev, true);
-	adev->in_s4 = false;
-
-	return r;
+	return amdgpu_device_resume(drm_dev, true);
 }
 
 static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
 static int amdgpu_pmops_restore(struct device *dev)
 {
 	struct drm_device *drm_dev = dev_get_drvdata(dev);
-	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-	adev->in_s4 = false;
 
 	return amdgpu_device_resume(drm_dev, true);
 }
Signature · 1 line
2.49.0
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

Quoted 11 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>

Rather than removing the callbacks (and thus introducing the "other" bugs with memory pressure), I've sent an alternative series. LMK what you think of this instead.

https://lore.kernel.org/amd-gfx/[email protected]/T/#m6cdc075af911868667ea6fc849bcd196477d6c20

Quoted 113 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
  2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +13 -12
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
   * @data: data
   *
   * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
   */
  static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
  				     void *data)
  {
  	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-	int r;
 
  	switch (mode) {
  	case PM_HIBERNATION_PREPARE:
  		adev->in_s4 = true;
-		fallthrough;
+		break;
+	case PM_POST_HIBERNATION:
+		adev->in_s4 = false;
+		break;
  	case PM_SUSPEND_PREPARE:
-		r = amdgpu_device_evict_resources(adev);
-		/*
-		 * This is considered non-fatal at this time because
-		 * amdgpu_device_prepare() will also fatally evict resources.
-		 * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-		 */
-		if (r)
-			drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+		if (amdgpu_acpi_is_s0ix_active(adev))
+			adev->in_s0ix = true;
+		else if (amdgpu_acpi_is_s3_active(adev))
+			adev->in_s3 = true;
+		break;
+	case PM_POST_SUSPEND:
+		adev->in_s0ix = adev->in_s3 = false;
  		break;
  	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
  	struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-	if (amdgpu_acpi_is_s0ix_active(adev))
-		adev->in_s0ix = true;
-	else if (amdgpu_acpi_is_s3_active(adev))
-		adev->in_s3 = true;
  	if (!adev->in_s0ix && !adev->in_s3) {
  		/* don't allow going deep first time followed by s2idle the next time */
  		if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
  {
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
  	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-	int r;
 
  	if (!adev->in_s0ix && !adev->in_s3)
  		return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
  	if (!pci_device_is_present(adev->pdev))
  		adev->no_hw_access = true;
 
-	r = amdgpu_device_resume(drm_dev, true);
-	if (amdgpu_acpi_is_s0ix_active(adev))
-		adev->in_s0ix = false;
-	else
-		adev->in_s3 = false;
-	return r;
+	return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
  static int amdgpu_pmops_thaw(struct device *dev)
  {
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
-	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-	int r;
 
-	r = amdgpu_device_resume(drm_dev, true);
-	adev->in_s4 = false;
-
-	return r;
+	return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
  static int amdgpu_pmops_restore(struct device *dev)
  {
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
-	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-	adev->in_s4 = false;
 
  	return amdgpu_device_resume(drm_dev, true);
  }
#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

Quoted 14 lines · Mario Limonciello
Quoted 11 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>
Quoted 5 lines

Rather than removing the callbacks (and thus introducing the "other" bugs with memory pressure), I've sent an alternative series. LMK what you think of this instead.

https://lore.kernel.org/amd-gfx/[email protected]/T/#m6cdc075af911868667ea6fc849bcd196477d6c20

Series looks good to me, but I think a variant of this patch (with the evictions still in place) still makes sense so that we can properly set the suspend and hibernate flags in amdgpu so that we can optimize the evictions for suspend on APUs. Will respin.

Alex

Quoted 115 lines
Quoted 114 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
  2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +13 -12
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
   * @data: data
   *
   * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
   */
  static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
                                   void *data)
  {
      struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-     int r;
 
      switch (mode) {
      case PM_HIBERNATION_PREPARE:
              adev->in_s4 = true;
-             fallthrough;
+             break;
+     case PM_POST_HIBERNATION:
+             adev->in_s4 = false;
+             break;
      case PM_SUSPEND_PREPARE:
-             r = amdgpu_device_evict_resources(adev);
-             /*
-              * This is considered non-fatal at this time because
-              * amdgpu_device_prepare() will also fatally evict resources.
-              * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-              */
-             if (r)
-                     drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+             if (amdgpu_acpi_is_s0ix_active(adev))
+                     adev->in_s0ix = true;
+             else if (amdgpu_acpi_is_s3_active(adev))
+                     adev->in_s3 = true;
+             break;
+     case PM_POST_SUSPEND:
+             adev->in_s0ix = adev->in_s3 = false;
              break;
      }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
      struct drm_device *drm_dev = dev_get_drvdata(dev);
      struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = true;
-     else if (amdgpu_acpi_is_s3_active(adev))
-             adev->in_s3 = true;
      if (!adev->in_s0ix && !adev->in_s3) {
              /* don't allow going deep first time followed by s2idle the next time */
              if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
      struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
      if (!adev->in_s0ix && !adev->in_s3)
              return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
      if (!pci_device_is_present(adev->pdev))
              adev->no_hw_access = true;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = false;
-     else
-             adev->in_s3 = false;
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
  static int amdgpu_pmops_thaw(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     adev->in_s4 = false;
-
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
  static int amdgpu_pmops_restore(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-     adev->in_s4 = false;
 
      return amdgpu_device_resume(drm_dev, true);
  }
#4 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

Quoted 21 lines · Alex Deucher
Quoted 18 lines · Mario Limonciello
Quoted 11 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>

Rather than removing the callbacks (and thus introducing the "other" bugs with memory pressure), I've sent an alternative series. LMK what you think of this instead.

Quoted 6 lines
Quoted 1 line

https://lore.kernel.org/amd-gfx/[email protected]/T/#m6cdc075af911868667ea6fc849bcd196477d6c20

Series looks good to me, but I think a variant of this patch (with the evictions still in place) still makes sense so that we can properly set the suspend and hibernate flags in amdgpu so that we can optimize the evictions for suspend on APUs. Will respin.

I think this set still makes sense for stable, but then if your patch series lands, we can apply the attached patch on top of that.

Alex

Quoted 118 lines

Alex

Quoted 115 lines
Quoted 114 lines
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
  2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +13 -12
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
   * @data: data
   *
   * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
   */
  static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
                                   void *data)
  {
      struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-     int r;
 
      switch (mode) {
      case PM_HIBERNATION_PREPARE:
              adev->in_s4 = true;
-             fallthrough;
+             break;
+     case PM_POST_HIBERNATION:
+             adev->in_s4 = false;
+             break;
      case PM_SUSPEND_PREPARE:
-             r = amdgpu_device_evict_resources(adev);
-             /*
-              * This is considered non-fatal at this time because
-              * amdgpu_device_prepare() will also fatally evict resources.
-              * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-              */
-             if (r)
-                     drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+             if (amdgpu_acpi_is_s0ix_active(adev))
+                     adev->in_s0ix = true;
+             else if (amdgpu_acpi_is_s3_active(adev))
+                     adev->in_s3 = true;
+             break;
+     case PM_POST_SUSPEND:
+             adev->in_s0ix = adev->in_s3 = false;
              break;
      }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
      struct drm_device *drm_dev = dev_get_drvdata(dev);
      struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = true;
-     else if (amdgpu_acpi_is_s3_active(adev))
-             adev->in_s3 = true;
      if (!adev->in_s0ix && !adev->in_s3) {
              /* don't allow going deep first time followed by s2idle the next time */
              if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
      struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
      if (!adev->in_s0ix && !adev->in_s3)
              return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
      if (!pci_device_is_present(adev->pdev))
              adev->no_hw_access = true;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = false;
-     else
-             adev->in_s3 = false;
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
  static int amdgpu_pmops_thaw(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     adev->in_s4 = false;
-
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
  static int amdgpu_pmops_restore(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-     adev->in_s4 = false;
 
      return amdgpu_device_resume(drm_dev, true);
  }

From 90ffa23604b70cae9345380adfb374683eaab6cf Mon Sep 17 00:00:00 2001
From: Alex Deucher <[email protected]>
Date: Fri, 2 May 2025 09:09:33 -0400
Subject: [PATCH] drm/amdgpu: handle eviction in pm notifier

Use the new PM_SUSPEND_POST_FREEZE and PM_HIBERNATE_POST_FREEZE actions to handle buffer eviction. Processes have been frozen so we can safely evict resources while swap is still active so we have less risk of running out of memory to evict VRAM.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +19 -2
index d232e4a26d7bf..5a8f023fa201e 100644
@@ -4981,19 +4981,36 @@ static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mo
 				     void *data)
 {
 	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
+	int r;
 
 	switch (mode) {
-	case PM_HIBERNATION_PREPARE:
+	case PM_HIBERNATE_POST_FREEZE:
 		adev->in_s4 = true;
+		r = amdgpu_device_evict_resources(adev);
+		/*
+		 * This is considered non-fatal at this time because
+		 * amdgpu_device_prepare() will also fatally evict resources.
+		 * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
+		 */
+		if (r)
+			drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
 		break;
 	case PM_POST_HIBERNATION:
 		adev->in_s4 = false;
 		break;
-	case PM_SUSPEND_PREPARE:
+	case PM_SUSPEND_POST_FREEZE:
 		if (amdgpu_acpi_is_s0ix_active(adev))
 			adev->in_s0ix = true;
 		else if (amdgpu_acpi_is_s3_active(adev))
 			adev->in_s3 = true;
+		r = amdgpu_device_evict_resources(adev);
+		/*
+		 * This is considered non-fatal at this time because
+		 * amdgpu_device_prepare() will also fatally evict resources.
+		 * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
+		 */
+		if (r)
+			drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
 		break;
 	case PM_POST_SUSPEND:
 		adev->in_s0ix = adev->in_s3 = false;
Signature · 1 line
2.49.0
#5 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

Quoted 24 lines · Alex Deucher
Quoted 23 lines · Alex Deucher
Quoted 19 lines · Mario Limonciello
Quoted 11 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>

Rather than removing the callbacks (and thus introducing the "other" bugs with memory pressure), I've sent an alternative series. LMK what you think of this instead.

https://lore.kernel.org/amd-gfx/[email protected]/T/#m6cdc075af911868667ea6fc849bcd196477d6c20

Quoted 7 lines
Quoted 4 lines

Series looks good to me, but I think a variant of this patch (with the evictions still in place) still makes sense so that we can properly set the suspend and hibernate flags in amdgpu so that we can optimize the evictions for suspend on APUs. Will respin.

I think this set still makes sense for stable, but then if your patch series lands, we can apply the attached patch on top of that.

OK. Let me re-look through your series once more and I'll leave comments or a tag.

Regarding the patch to re-introduced it you attached, I suppose the drm_warn() message doesn't need to talk about freezing processes as a solution to eviction failure because freezing is implied with the new notifier in use.

Quoted 121 lines

Alex

Quoted 118 lines

Alex

Quoted 115 lines
Quoted 114 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
   drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
   2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +13 -12
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
    * @data: data
    *
    * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
    */
   static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
                                    void *data)
   {
       struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-     int r;
 
       switch (mode) {
       case PM_HIBERNATION_PREPARE:
               adev->in_s4 = true;
-             fallthrough;
+             break;
+     case PM_POST_HIBERNATION:
+             adev->in_s4 = false;
+             break;
       case PM_SUSPEND_PREPARE:
-             r = amdgpu_device_evict_resources(adev);
-             /*
-              * This is considered non-fatal at this time because
-              * amdgpu_device_prepare() will also fatally evict resources.
-              * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-              */
-             if (r)
-                     drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+             if (amdgpu_acpi_is_s0ix_active(adev))
+                     adev->in_s0ix = true;
+             else if (amdgpu_acpi_is_s3_active(adev))
+                     adev->in_s3 = true;
+             break;
+     case PM_POST_SUSPEND:
+             adev->in_s0ix = adev->in_s3 = false;
               break;
       }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
       struct drm_device *drm_dev = dev_get_drvdata(dev);
       struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = true;
-     else if (amdgpu_acpi_is_s3_active(adev))
-             adev->in_s3 = true;
       if (!adev->in_s0ix && !adev->in_s3) {
               /* don't allow going deep first time followed by s2idle the next time */
               if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
   {
       struct drm_device *drm_dev = dev_get_drvdata(dev);
       struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
       if (!adev->in_s0ix && !adev->in_s3)
               return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
       if (!pci_device_is_present(adev->pdev))
               adev->no_hw_access = true;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = false;
-     else
-             adev->in_s3 = false;
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
   }
 
   static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
   static int amdgpu_pmops_thaw(struct device *dev)
   {
       struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     adev->in_s4 = false;
-
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
   }
 
   static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
   static int amdgpu_pmops_restore(struct device *dev)
   {
       struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-     adev->in_s4 = false;
 
       return amdgpu_device_resume(drm_dev, true);
   }
#6 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

Quoted 31 lines · Mario Limonciello
Quoted 29 lines · Alex Deucher
Quoted 27 lines · Alex Deucher
Quoted 14 lines · Mario Limonciello
Quoted 11 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>
Quoted 5 lines

Rather than removing the callbacks (and thus introducing the "other" bugs with memory pressure), I've sent an alternative series. LMK what you think of this instead.

https://lore.kernel.org/amd-gfx/[email protected]/T/#m6cdc075af911868667ea6fc849bcd196477d6c20

Series looks good to me, but I think a variant of this patch (with the evictions still in place) still makes sense so that we can properly set the suspend and hibernate flags in amdgpu so that we can optimize the evictions for suspend on APUs. Will respin.

Quoted 5 lines
Quoted 2 lines

I think this set still makes sense for stable, but then if your patch series lands, we can apply the attached patch on top of that.

OK. Let me re-look through your series once more and I'll leave comments or a tag.

Thanks.

Quoted 5 lines

Regarding the patch to re-introduced it you attached, I suppose the drm_warn() message doesn't need to talk about freezing processes as a solution to eviction failure because freezing is implied with the new notifier in use.

Will fix.

Thanks

Alex

Quoted 123 lines
Quoted 122 lines

Alex

Quoted 118 lines

Alex

Quoted 115 lines
Quoted 114 lines
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
   drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
   2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +13 -12
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
    * @data: data
    *
    * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
    */
   static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
                                    void *data)
   {
       struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-     int r;
 
       switch (mode) {
       case PM_HIBERNATION_PREPARE:
               adev->in_s4 = true;
-             fallthrough;
+             break;
+     case PM_POST_HIBERNATION:
+             adev->in_s4 = false;
+             break;
       case PM_SUSPEND_PREPARE:
-             r = amdgpu_device_evict_resources(adev);
-             /*
-              * This is considered non-fatal at this time because
-              * amdgpu_device_prepare() will also fatally evict resources.
-              * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-              */
-             if (r)
-                     drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+             if (amdgpu_acpi_is_s0ix_active(adev))
+                     adev->in_s0ix = true;
+             else if (amdgpu_acpi_is_s3_active(adev))
+                     adev->in_s3 = true;
+             break;
+     case PM_POST_SUSPEND:
+             adev->in_s0ix = adev->in_s3 = false;
               break;
       }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
       struct drm_device *drm_dev = dev_get_drvdata(dev);
       struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = true;
-     else if (amdgpu_acpi_is_s3_active(adev))
-             adev->in_s3 = true;
       if (!adev->in_s0ix && !adev->in_s3) {
               /* don't allow going deep first time followed by s2idle the next time */
               if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
   {
       struct drm_device *drm_dev = dev_get_drvdata(dev);
       struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
       if (!adev->in_s0ix && !adev->in_s3)
               return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
       if (!pci_device_is_present(adev->pdev))
               adev->no_hw_access = true;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = false;
-     else
-             adev->in_s3 = false;
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
   }
 
   static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
   static int amdgpu_pmops_thaw(struct device *dev)
   {
       struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     adev->in_s4 = false;
-
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
   }
 
   static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
   static int amdgpu_pmops_restore(struct device *dev)
   {
       struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-     adev->in_s4 = false;
 
       return amdgpu_device_resume(drm_dev, true);
   }
#7 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

In reply to Alex Deucher
Quoted 43 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
  2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -4
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
   * @data: data
   *
   * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
   */
  static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
  				     void *data)
  {
  	struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-	int r;
 
  	switch (mode) {
  	case PM_HIBERNATION_PREPARE:
  		adev->in_s4 = true;
-		fallthrough;
+		break;
+	case PM_POST_HIBERNATION:
+		adev->in_s4 = false;
+		break;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_evict_resources(struct amdgpu_device *adev) · line 4993

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -8
  	case PM_SUSPEND_PREPARE:
-		r = amdgpu_device_evict_resources(adev);
-		/*
-		 * This is considered non-fatal at this time because
-		 * amdgpu_device_prepare() will also fatally evict resources.
-		 * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-		 */
-		if (r)
-			drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+		if (amdgpu_acpi_is_s0ix_active(adev))

I don't believe this is valid "this early".

pm_suspend()
->enter_state()
->->suspend_prepare()
->->-> Call notification chains for PM_SUSPEND_PREPARE ->->suspend_devices_and_enter()
->->-> Set pm_suspend_target_state

Quoted 71 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+			adev->in_s0ix = true;
+		else if (amdgpu_acpi_is_s3_active(adev))
+			adev->in_s3 = true;
+		break;
+	case PM_POST_SUSPEND:
+		adev->in_s0ix = adev->in_s3 = false;
  		break;
  	}
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
  	struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-	if (amdgpu_acpi_is_s0ix_active(adev))
-		adev->in_s0ix = true;
-	else if (amdgpu_acpi_is_s3_active(adev))
-		adev->in_s3 = true;
  	if (!adev->in_s0ix && !adev->in_s3) {
  		/* don't allow going deep first time followed by s2idle the next time */
  		if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
  {
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
  	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-	int r;
 
  	if (!adev->in_s0ix && !adev->in_s3)
  		return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
  	if (!pci_device_is_present(adev->pdev))
  		adev->no_hw_access = true;
 
-	r = amdgpu_device_resume(drm_dev, true);
-	if (amdgpu_acpi_is_s0ix_active(adev))
-		adev->in_s0ix = false;
-	else
-		adev->in_s3 = false;
-	return r;
+	return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
  static int amdgpu_pmops_thaw(struct device *dev)
  {
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
-	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-	int r;
 
-	r = amdgpu_device_resume(drm_dev, true);
-	adev->in_s4 = false;
-
-	return r;
+	return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
  static int amdgpu_pmops_restore(struct device *dev)
  {
  	struct drm_device *drm_dev = dev_get_drvdata(dev);
-	struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-	adev->in_s4 = false;
 
  	return amdgpu_device_resume(drm_dev, true);
  }
#8 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

Quoted 58 lines · Mario Limonciello
Quoted 43 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
  2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -4
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
   * @data: data
   *
   * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
   */
  static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
                                   void *data)
  {
      struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-     int r;
 
      switch (mode) {
      case PM_HIBERNATION_PREPARE:
              adev->in_s4 = true;
-             fallthrough;
+             break;
+     case PM_POST_HIBERNATION:
+             adev->in_s4 = false;
+             break;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_evict_resources(struct amdgpu_device *adev) · line 4993

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -8
      case PM_SUSPEND_PREPARE:
-             r = amdgpu_device_evict_resources(adev);
-             /*
-              * This is considered non-fatal at this time because
-              * amdgpu_device_prepare() will also fatally evict resources.
-              * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-              */
-             if (r)
-                     drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+             if (amdgpu_acpi_is_s0ix_active(adev))

I don't believe this is valid "this early".

Quoted 6 lines

pm_suspend()
->enter_state()
->->suspend_prepare()
->->-> Call notification chains for PM_SUSPEND_PREPARE ->->suspend_devices_and_enter()
->->-> Set pm_suspend_target_state

hmmm. Is there a way to determine whether we are going into hibernate vs. suspend in the pm prepare function? I guess we could set adev->in_s4 here and then check if in_s4 is set in pm prepare, and if not, then call this logic to set the suspend flags in the prepare callback.

Alex

Quoted 73 lines
Quoted 72 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -0
+                     adev->in_s0ix = true;
+             else if (amdgpu_acpi_is_s3_active(adev))
+                     adev->in_s3 = true;
+             break;
+     case PM_POST_SUSPEND:
+             adev->in_s0ix = adev->in_s3 = false;
              break;
      }
 
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -20
index cec041a556013..6599fb6313220 100644
@@ -2572,10 +2572,6 @@ static int amdgpu_pmops_suspend(struct device *dev)
      struct drm_device *drm_dev = dev_get_drvdata(dev);
      struct amdgpu_device *adev = drm_to_adev(drm_dev);
 
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = true;
-     else if (amdgpu_acpi_is_s3_active(adev))
-             adev->in_s3 = true;
      if (!adev->in_s0ix && !adev->in_s3) {
              /* don't allow going deep first time followed by s2idle the next time */
              if (adev->last_suspend_state != PM_SUSPEND_ON &&
@@ -2608,7 +2604,6 @@ static int amdgpu_pmops_resume(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
      struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
      if (!adev->in_s0ix && !adev->in_s3)
              return 0;
@@ -2617,12 +2612,7 @@ static int amdgpu_pmops_resume(struct device *dev)
      if (!pci_device_is_present(adev->pdev))
              adev->no_hw_access = true;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     if (amdgpu_acpi_is_s0ix_active(adev))
-             adev->in_s0ix = false;
-     else
-             adev->in_s3 = false;
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_freeze(struct device *dev)
@@ -2643,13 +2633,8 @@ static int amdgpu_pmops_freeze(struct device *dev)
  static int amdgpu_pmops_thaw(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-     int r;
 
-     r = amdgpu_device_resume(drm_dev, true);
-     adev->in_s4 = false;
-
-     return r;
+     return amdgpu_device_resume(drm_dev, true);
  }
 
  static int amdgpu_pmops_poweroff(struct device *dev)
@@ -2662,9 +2647,6 @@ static int amdgpu_pmops_poweroff(struct device *dev)
  static int amdgpu_pmops_restore(struct device *dev)
  {
      struct drm_device *drm_dev = dev_get_drvdata(dev);
-     struct amdgpu_device *adev = drm_to_adev(drm_dev);
-
-     adev->in_s4 = false;
 
      return amdgpu_device_resume(drm_dev, true);
  }
#9 lore raw

Re: [PATCH 2/2] drm/amdgpu: fix pm notifier handling

Quoted 67 lines · Alex Deucher
Quoted 58 lines · Mario Limonciello
Quoted 43 lines · Alex Deucher

Set the s3/s0ix and s4 flags in the pm notifier so that we can skip the resource evictions properly in pm prepare based on whether we are suspending or hibernating. Drop the eviction as processes are not frozen at this time, we we can end up getting stuck trying to evict VRAM while applications continue to submit work which causes the buffers to get pulled back into VRAM.

Fixes 2965e6355dcd ("drm/amd: Add Suspend/Hibernate notification callback support")
Cc Mario Limonciello <[email protected]>
---
   drivers/gpu/drm/amd/amdgpu/amdgpu_device.c | 25 +++++++++++-----------
   drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c    | 22 ++-----------------
   2 files changed, 15 insertions(+), 32 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +6 -4
index 71d95f16067a4..d232e4a26d7bf 100644
@@ -4974,28 +4974,29 @@ static int amdgpu_device_evict_resources(struct amdgpu_device *adev)
    * @data: data
    *
    * This function is called when the system is about to suspend or hibernate.
- * It is used to evict resources from the device before the system goes to
- * sleep while there is still access to swap.
+ * It is used to set the appropriate flags so that eviction can be optimized
+ * in the pm prepare callback.
    */
   static int amdgpu_device_pm_notifier(struct notifier_block *nb, unsigned long mode,
                                    void *data)
   {
       struct amdgpu_device *adev = container_of(nb, struct amdgpu_device, pm_nb);
-     int r;
 
       switch (mode) {
       case PM_HIBERNATION_PREPARE:
               adev->in_s4 = true;
-             fallthrough;
+             break;
+     case PM_POST_HIBERNATION:
+             adev->in_s4 = false;
+             break;

drivers/gpu/drm/amd/amdgpu/amdgpu_device.c static int amdgpu_device_evict_resources(struct amdgpu_device *adev) · line 4993

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_device.c +1 -8
       case PM_SUSPEND_PREPARE:
-             r = amdgpu_device_evict_resources(adev);
-             /*
-              * This is considered non-fatal at this time because
-              * amdgpu_device_prepare() will also fatally evict resources.
-              * See https://gitlab.freedesktop.org/drm/amd/-/issues/3781
-              */
-             if (r)
-                     drm_warn(adev_to_drm(adev), "Failed to evict resources, freeze active processes if problems occur: %d\n", r);
+             if (amdgpu_acpi_is_s0ix_active(adev))

I don't believe this is valid "this early".

Quoted 6 lines

pm_suspend()
->enter_state()
->->suspend_prepare()
->->-> Call notification chains for PM_SUSPEND_PREPARE ->->suspend_devices_and_enter()
->->-> Set pm_suspend_target_state

Quoted 6 lines

hmmm. Is there a way to determine whether we are going into hibernate vs. suspend in the pm prepare function? I guess we could set adev->in_s4 here and then check if in_s4 is set in pm prepare, and if not, then call this logic to set the suspend flags in the prepare callback.

Yeah; I think setting is_s4 here makes a lot of sense and then use that as a hint later in the sequence.