Skip to contentSkip to conversation

drm/amdgpu: Fix MMIO access page fault

PATCH In review Andrey Grodzovsky 3 participants 7 messages Sep 17, 2021 – Sep 20, 2021

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

PATCH 1/2 drm/amdgpu: Fix MMIO access page fault

Add more guards to MMIO access post device
unbind/unplug

---
 drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c |  8 ++++++--
 drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 17 +++++++++++------
 2 files changed, 17 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c +6 -2
index e6e9ef50719e..a03c0fc8338f 100644
@@ -22,6 +22,7 @@
  */
 
 #include <linux/firmware.h>
+#include <drm/drm_drv.h>
 
 #include "amdgpu.h"
 #include "amdgpu_vcn.h"
@@ -194,11 +195,14 @@ static int vcn_v2_0_sw_init(void *handle)
  */
 static int vcn_v2_0_sw_fini(void *handle)
 {
-	int r;
+	int r, idx;
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 	volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst->fw_shared_cpu_addr;
 
-	fw_shared->present_flag_0 = 0;
+	if (drm_dev_enter(&adev->ddev, &idx)) {
+		fw_shared->present_flag_0 = 0;
+		drm_dev_exit(idx);
+	}
 
 	amdgpu_virt_free_mm_table(adev);
 
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +11 -6
index 2e6b7913bf6c..1780ad1eacd6 100644
@@ -22,6 +22,7 @@
  */
 
 #include <linux/firmware.h>
+#include <drm/drm_drv.h>
 
 #include "amdgpu.h"
 #include "amdgpu_vcn.h"
@@ -235,17 +236,21 @@ static int vcn_v2_5_sw_init(void *handle)
  */
 static int vcn_v2_5_sw_fini(void *handle)
 {
-	int i, r;
+	int i, r, idx;
 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
 	volatile struct amdgpu_fw_shared *fw_shared;
 
-	for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
-		if (adev->vcn.harvest_config & (1 << i))
-			continue;
-		fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
-		fw_shared->present_flag_0 = 0;
+	if (drm_dev_enter(&adev->ddev, &idx)) {
+		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
+			if (adev->vcn.harvest_config & (1 << i))
+				continue;
+			fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
+			fw_shared->present_flag_0 = 0;
+		}
+		drm_dev_exit(idx);
 	}
 
+
 	if (amdgpu_sriov_vf(adev))
 		amdgpu_virt_free_mm_table(adev);
 
Signature · 1 line
2.25.1
#1 lore raw

PATCH 2/2 drm/amdgpu: Fix resume failures when device is gone

Problem:
When device goes into suspend and unplugged during it then all HW programming during resume fails leading to a bad SW during pci remove handling which follows. Because device is first resumed and only later removed we cannot rely on drm_dev_enter/exit here.

Fix:
Use a flag we use for PCIe error recovery to avoid accessing registres. This allows to successfully complete pm resume sequence and finish pci remove.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +4 -0
index db21af5e84ed..04fb4e74fb20 100644
@@ -1522,6 +1522,10 @@ static int amdgpu_pmops_resume(struct device *dev)
 	struct amdgpu_device *adev = drm_to_adev(drm_dev);
 	int r;
 
+	/* Avoids registers access if device is physically gone */
+	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;
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/2] drm/amdgpu: Fix resume failures when device is gone

Ping

Andrey

Quoted 32 lines · Andrey Grodzovsky

Problem:
When device goes into suspend and unplugged during it then all HW programming during resume fails leading to a bad SW during pci remove handling which follows. Because device is first resumed and only later removed we cannot rely on drm_dev_enter/exit here.

Fix:
Use a flag we use for PCIe error recovery to avoid accessing registres. This allows to successfully complete pm resume sequence and finish pci remove.

---
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 4 ++++
  1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +4 -0
index db21af5e84ed..04fb4e74fb20 100644
@@ -1522,6 +1522,10 @@ static int amdgpu_pmops_resume(struct device *dev)
  	struct amdgpu_device *adev = drm_to_adev(drm_dev);
  	int r;
 
+	/* Avoids registers access if device is physically gone */
+	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;
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] drm/amdgpu: Fix resume failures when device is gone

In reply to Andrey Grodzovsky

Series is:

Quoted 36 lines · Andrey Grodzovsky

Problem:
When device goes into suspend and unplugged during it then all HW programming during resume fails leading to a bad SW during pci remove handling which follows. Because device is first resumed and only later removed we cannot rely on drm_dev_enter/exit here.

Fix:
Use a flag we use for PCIe error recovery to avoid accessing registres. This allows to successfully complete pm resume sequence and finish pci remove.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +4 -1
index db21af5e84ed..04fb4e74fb20 100644
@@ -1522,6 +1522,10 @@ static int amdgpu_pmops_resume(struct device *dev)
        struct amdgpu_device *adev = drm_to_adev(drm_dev);
        int r;
 
+       /* Avoids registers access if device is physically gone */
+       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;
--

2.25.1

#4 lore raw
In reply to Andrey Grodzovsky

Hi Andrey

Can you apply this improvement  on vcn_v3_0_sw_init also?

With this adding, This patch is Reviewed-by: James Zhu <[email protected]>

Thanks & Best Regards!

James

Quoted 79 lines · Andrey Grodzovsky

Add more guards to MMIO access post device
unbind/unplug

---
  drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c |  8 ++++++--
  drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 17 +++++++++++------
  2 files changed, 17 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c +6 -2
index e6e9ef50719e..a03c0fc8338f 100644
@@ -22,6 +22,7 @@
   */
 
  #include <linux/firmware.h>
+#include <drm/drm_drv.h>
 
  #include "amdgpu.h"
  #include "amdgpu_vcn.h"
@@ -194,11 +195,14 @@ static int vcn_v2_0_sw_init(void *handle)
   */
  static int vcn_v2_0_sw_fini(void *handle)
  {
-	int r;
+	int r, idx;
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  	volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst->fw_shared_cpu_addr;
 
-	fw_shared->present_flag_0 = 0;
+	if (drm_dev_enter(&adev->ddev, &idx)) {
+		fw_shared->present_flag_0 = 0;
+		drm_dev_exit(idx);
+	}
 
  	amdgpu_virt_free_mm_table(adev);
 
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +11 -6
index 2e6b7913bf6c..1780ad1eacd6 100644
@@ -22,6 +22,7 @@
   */
 
  #include <linux/firmware.h>
+#include <drm/drm_drv.h>
 
  #include "amdgpu.h"
  #include "amdgpu_vcn.h"
@@ -235,17 +236,21 @@ static int vcn_v2_5_sw_init(void *handle)
   */
  static int vcn_v2_5_sw_fini(void *handle)
  {
-	int i, r;
+	int i, r, idx;
  	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  	volatile struct amdgpu_fw_shared *fw_shared;
 
-	for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
-		if (adev->vcn.harvest_config & (1 << i))
-			continue;
-		fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
-		fw_shared->present_flag_0 = 0;
+	if (drm_dev_enter(&adev->ddev, &idx)) {
+		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
+			if (adev->vcn.harvest_config & (1 << i))
+				continue;
+			fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
+			fw_shared->present_flag_0 = 0;
+		}
+		drm_dev_exit(idx);
  	}
 
+
  	if (amdgpu_sriov_vf(adev))
  		amdgpu_virt_free_mm_table(adev);
 
#5 lore raw

typo. vcn_v3_0_sw_init   -->  vcn_v3_0_sw_fini

Quoted 81 lines · James Zhu

Hi Andrey

Can you apply this improvement  on vcn_v3_0_sw_init also?

With this adding, This patch is Reviewed-by: James Zhu <[email protected]>

Thanks & Best Regards!

James

Quoted 70 lines · Andrey Grodzovsky

Add more guards to MMIO access post device
unbind/unplug

---
  drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c |  8 ++++++-- drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 17 +++++++++++------

  2 files changed, 17 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c +6 -1
index e6e9ef50719e..a03c0fc8338f 100644
@@ -22,6 +22,7 @@
    */
     #include <linux/firmware.h>
+#include <drm/drm_drv.h>
     #include "amdgpu.h"
   #include "amdgpu_vcn.h"
@@ -194,11 +195,14 @@ static int vcn_v2_0_sw_init(void *handle)
    */
   static int vcn_v2_0_sw_fini(void *handle)
   {
-    int r;
+    int r, idx;
       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
       volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst->fw_shared_cpu_addr;
   -    fw_shared->present_flag_0 = 0;
+    if (drm_dev_enter(&adev->ddev, &idx)) {
+        fw_shared->present_flag_0 = 0;
+        drm_dev_exit(idx);
+    }
         amdgpu_virt_free_mm_table(adev);

  diff --git a/drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c b/drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c index 2e6b7913bf6c..1780ad1eacd6 100644

drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +10 -5
@@ -22,6 +22,7 @@
    */
     #include <linux/firmware.h>
+#include <drm/drm_drv.h>
     #include "amdgpu.h"
   #include "amdgpu_vcn.h"
@@ -235,17 +236,21 @@ static int vcn_v2_5_sw_init(void *handle)
    */
   static int vcn_v2_5_sw_fini(void *handle)
   {
-    int i, r;
+    int i, r, idx;
       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
       volatile struct amdgpu_fw_shared *fw_shared;
   -    for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
-        if (adev->vcn.harvest_config & (1 << i))
-            continue;
-        fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
-        fw_shared->present_flag_0 = 0;
+    if (drm_dev_enter(&adev->ddev, &idx)) {
+        for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
+            if (adev->vcn.harvest_config & (1 << i))
+                continue;
+            fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
+            fw_shared->present_flag_0 = 0;
+        }
+        drm_dev_exit(idx);
       }

  +
      if (amdgpu_sriov_vf(adev))
          amdgpu_virt_free_mm_table(adev);

#6 lore raw

Note that it already has this protection.

Andrey

Quoted 84 lines · James Zhu

typo. vcn_v3_0_sw_init   -->  vcn_v3_0_sw_fini

Quoted 81 lines · James Zhu

Hi Andrey

Can you apply this improvement  on vcn_v3_0_sw_init also?

With this adding, This patch is Reviewed-by: James Zhu <[email protected]>

Thanks & Best Regards!

James

Quoted 70 lines · Andrey Grodzovsky

Add more guards to MMIO access post device
unbind/unplug

---
  drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c |  8 ++++++-- drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 17 +++++++++++------

  2 files changed, 17 insertions(+), 8 deletions(-)
drivers/gpu/drm/amd/amdgpu/vcn_v2_0.c +6 -1
index e6e9ef50719e..a03c0fc8338f 100644
@@ -22,6 +22,7 @@
    */
     #include <linux/firmware.h>
+#include <drm/drm_drv.h>
     #include "amdgpu.h"
   #include "amdgpu_vcn.h"
@@ -194,11 +195,14 @@ static int vcn_v2_0_sw_init(void *handle)
    */
   static int vcn_v2_0_sw_fini(void *handle)
   {
-    int r;
+    int r, idx;
       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
       volatile struct amdgpu_fw_shared *fw_shared = adev->vcn.inst->fw_shared_cpu_addr;
   -    fw_shared->present_flag_0 = 0;
+    if (drm_dev_enter(&adev->ddev, &idx)) {
+        fw_shared->present_flag_0 = 0;
+        drm_dev_exit(idx);
+    }
         amdgpu_virt_free_mm_table(adev);

  diff --git a/drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c b/drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c index 2e6b7913bf6c..1780ad1eacd6 100644

drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +10 -5
@@ -22,6 +22,7 @@
    */
     #include <linux/firmware.h>
+#include <drm/drm_drv.h>
     #include "amdgpu.h"
   #include "amdgpu_vcn.h"
@@ -235,17 +236,21 @@ static int vcn_v2_5_sw_init(void *handle)
    */
   static int vcn_v2_5_sw_fini(void *handle)
   {
-    int i, r;
+    int i, r, idx;
       struct amdgpu_device *adev = (struct amdgpu_device *)handle;
       volatile struct amdgpu_fw_shared *fw_shared;
   -    for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
-        if (adev->vcn.harvest_config & (1 << i))
-            continue;
-        fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
-        fw_shared->present_flag_0 = 0;
+    if (drm_dev_enter(&adev->ddev, &idx)) {
+        for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
+            if (adev->vcn.harvest_config & (1 << i))
+                continue;
+            fw_shared = adev->vcn.inst[i].fw_shared_cpu_addr;
+            fw_shared->present_flag_0 = 0;
+        }
+        drm_dev_exit(idx);
       }

  +
      if (amdgpu_sriov_vf(adev))
          amdgpu_virt_free_mm_table(adev);