Skip to contentSkip to conversation

drm/amdgpu: fix NULL pointer dereference in amdgpu_gmc_filter_faults_remove

PATCH In review Alex Deucher 5 participants 8 messages Jan 21 – Jan 23

Subsystems (3)
lore
Share & tools
lore

Alex Deucher

author maintainer
#0 lore raw

PATCH 1/1 drm/amdgpu: fix NULL pointer dereference in amdgpu_gmc_filter_faults_remove

From: Jon Doron <[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

  BUG: kernel NULL pointer dereference, address: 0000000000000004
  RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
  Call Trace:
   amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
   svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
   amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
   gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
   amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
   amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal"). This is needed if the hardware doesn't support secondary HW IH rings.

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +6 -1
index 8e65fec9f534e..243d75917458a 100644
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr,
 
 	if (adev->irq.retry_cam_enabled)
 		return;
+	else if (adev->irq.ih1.ring_size)
+		ih = &adev->irq.ih1;
+	else if (adev->irq.ih_soft.enabled)
+		ih = &adev->irq.ih_soft;
+	else
+		return;
 
-	ih = &adev->irq.ih1;
 	/* Get the WPTR of the last entry in IH ring */
 	last_wptr = amdgpu_ih_get_wptr(adev, ih);
 	/* Order wptr with ring data. */
Signature · 1 line
2.52.0
#1 lore raw
Quoted 52 lines · Alex Deucher

From: Jon Doron <[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

   BUG: kernel NULL pointer dereference, address: 0000000000000004
   RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
   Call Trace:
    amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
    svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
    amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
    gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
    amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
    amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal"). This is needed if the hardware doesn't support secondary HW IH rings.

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index 8e65fec9f534e..243d75917458a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr, · line 504

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +4 -0
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr,
 
  	if (adev->irq.retry_cam_enabled)
  		return;
+	else if (adev->irq.ih1.ring_size)
+		ih = &adev->irq.ih1;
+	else if (adev->irq.ih_soft.enabled)
+		ih = &adev->irq.ih_soft;

Faults are delegated to soft ring when retry_cam is enabled - https://gitlab.freedesktop.org/agd5f/linux/-/blob/amd-staging-drm-next/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c#L541

That matches with the original logic in d299441654f ("drm/amdgpu: Rework retry fault removal").

To match exactly with the logic in above commit, I think it should use soft ring only when retry cam is enabled. Presently, it's returning without doing anything.

Thanks,
Lijo

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +2 -1
+	else
+		return;
 
-	ih = &adev->irq.ih1;
  	/* Get the WPTR of the last entry in IH ring */
  	last_wptr = amdgpu_ih_get_wptr(adev, ih);
  	/* Order wptr with ring data. */
Reviewed-by Christian König
#2 lore raw
Quoted 67 lines · Lijo
Quoted 52 lines · Alex Deucher

From: Jon Doron <[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

   BUG: kernel NULL pointer dereference, address: 0000000000000004
   RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
   Call Trace:
    amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
    svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
    amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
    gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
    amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
    amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal"). This is needed if the hardware doesn't support secondary HW IH rings.

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
index 8e65fec9f534e..243d75917458a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr, · line 504

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +4 -0
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr,
 
      if (adev->irq.retry_cam_enabled)
              return;
+     else if (adev->irq.ih1.ring_size)
+             ih = &adev->irq.ih1;
+     else if (adev->irq.ih_soft.enabled)
+             ih = &adev->irq.ih_soft;

Faults are delegated to soft ring when retry_cam is enabled -
https://gitlab.freedesktop.org/agd5f/linux/-/blob/amd-staging-drm-next/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c#L541

Quoted 6 lines

That matches with the original logic in d299441654f ("drm/amdgpu: Rework retry fault removal").

To match exactly with the logic in above commit, I think it should use soft ring only when retry cam is enabled. Presently, it's returning without doing anything.

+ Philip

That logic was changed in:
commit e61801f162ddcf8874c820639483ec4849b0fb0b

Author: Philip Yang <[email protected]>
Date:   Mon Aug 28 14:05:55 2023 -0400

drm/amdkfd: Don't use sw fault filter if retry cam enabled

If retry cam enabled, we don't use sw retry fault filter and add fault into sw filter ring, so we shouldn't remove fault from sw filter.

So I retained that logic.

Alex

Quoted 12 lines

Thanks,
Lijo

Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +2 -1
+     else
+             return;
 
-     ih = &adev->irq.ih1;
      /* Get the WPTR of the last entry in IH ring */
      last_wptr = amdgpu_ih_get_wptr(adev, ih);
      /* Order wptr with ring data. */
#3 lore raw
In reply to Lazar, Lijo
Quoted 61 lines · Lijo
Quoted 60 lines · Alex Deucher

From: Jon Doron <[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

   BUG: kernel NULL pointer dereference, address: 0000000000000004
   RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
   
   Call Trace:
    amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
    svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
    amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
    gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
    amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
    amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal"). This is needed if the hardware doesn't support secondary HW IH rings.

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")

---

  drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-
  1 file changed, 6 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c index
8e65fec9f534e..243d75917458a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct

amdgpu_device *adev, uint64_t addr,>
if (adev->irq.retry_cam_enabled)

return;

Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr, · line 504

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +4 -0
+	else if (adev->irq.ih1.ring_size)
+		ih = &adev->irq.ih1;
+	else if (adev->irq.ih_soft.enabled)
+		ih = &adev->irq.ih_soft;

Faults are delegated to soft ring when retry_cam is enabled -
https://gitlab.freedesktop.org/agd5f/linux/-/blob/amd-staging-drm-next/drive
rs/gpu/drm/amd/amdgpu/amdgpu_gmc.c#L541

Hi,

As far as I know the retry CAM is not available on APUs. Please correct me if I'm wrong.

Thanks,
Timur

Quoted 19 lines

That matches with the original logic in d299441654f ("drm/amdgpu: Rework retry fault removal").

To match exactly with the logic in above commit, I think it should use soft ring only when retry cam is enabled. Presently, it's returning without doing anything.

Thanks,
Lijo

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +2 -1
+	else
+		return;
 
-	ih = &adev->irq.ih1;
 
  	/* Get the WPTR of the last entry in IH ring */
  	last_wptr = amdgpu_ih_get_wptr(adev, ih);
  	/* Order wptr with ring data. */
Reviewed-by
#4 lore raw
Quoted 70 lines · Timur Kristóf
Quoted 61 lines · Lijo
Quoted 60 lines · Alex Deucher

From: Jon Doron<[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

    BUG: kernel NULL pointer dereference, address: 0000000000000004
    RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
    
    Call Trace:
     amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
     svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
     amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
     gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
     amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
     amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal"). This is needed if the hardware doesn't support secondary HW IH rings.

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")

---

   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-
   1 file changed, 6 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c index
8e65fec9f534e..243d75917458a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct

amdgpu_device *adev, uint64_t addr,>
if (adev->irq.retry_cam_enabled)

   		return;
Quoted 7 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr, · line 504

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +4 -0
+	else if (adev->irq.ih1.ring_size)
+		ih = &adev->irq.ih1;
+	else if (adev->irq.ih_soft.enabled)
+		ih = &adev->irq.ih_soft;

Faults are delegated to soft ring when retry_cam is enabled -
https://gitlab.freedesktop.org/agd5f/linux/-/blob/amd-staging-drm-next/drive
rs/gpu/drm/amd/amdgpu/amdgpu_gmc.c#L541

Quoted 4 lines

Hi,

As far as I know the retry CAM is not available on APUs. Please correct me if I'm wrong.

Yes, that is correct, only on ASICs without CAM (the retry page fault hw filter), we use sw retry fault filter to drop duplicate faults, from the dedicated retry fault ring IH1 or delegate from IH ring to ih_soft ring. Without IH1 ring, the retry fault on IH ring may cause ring overflow and drop other important interrupts, we should not enable XNACK on it. With that said, the fix looks good to me.

Quoted 25 lines

Thanks,
Timur

Quoted 18 lines

That matches with the original logic in d299441654f ("drm/amdgpu: Rework retry fault removal").

To match exactly with the logic in above commit, I think it should use soft ring only when retry cam is enabled. Presently, it's returning without doing anything.

Thanks,
Lijo

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +2 -1
+	else
+		return;
 
-	ih = &adev->irq.ih1;
 
   	/* Get the WPTR of the last entry in IH ring */
   	last_wptr = amdgpu_ih_get_wptr(adev, ih);
   	/* Order wptr with ring data. */
#5 lore raw
In reply to Timur Kristóf
Quoted 72 lines · Timur Kristóf
Quoted 61 lines · Lijo
Quoted 60 lines · Alex Deucher

From: Jon Doron <[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

    BUG: kernel NULL pointer dereference, address: 0000000000000004
    RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
    
    Call Trace:
     amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
     svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
     amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
     gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
     amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
     amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal"). This is needed if the hardware doesn't support secondary HW IH rings.

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")

---

   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-
   1 file changed, 6 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c index
8e65fec9f534e..243d75917458a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct

amdgpu_device *adev, uint64_t addr,>
if (adev->irq.retry_cam_enabled)

   		return;
Quoted 8 lines

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c void amdgpu_gmc_filter_faults_remove(struct amdgpu_device *adev, uint64_t addr, · line 504

Quoted 4 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +4 -0
+	else if (adev->irq.ih1.ring_size)
+		ih = &adev->irq.ih1;
+	else if (adev->irq.ih_soft.enabled)
+		ih = &adev->irq.ih_soft;

Faults are delegated to soft ring when retry_cam is enabled -
https://gitlab.freedesktop.org/agd5f/linux/-/blob/amd-staging-drm-next/drive
rs/gpu/drm/amd/amdgpu/amdgpu_gmc.c#L541

Quoted 5 lines

Hi,

As far as I know the retry CAM is not available on APUs. Please correct me if I'm wrong.

Retry CAM filter is available on APUs as well.

Thanks,
Lijo

Quoted 26 lines

Thanks,
Timur

Quoted 19 lines

That matches with the original logic in d299441654f ("drm/amdgpu: Rework retry fault removal").

To match exactly with the logic in above commit, I think it should use soft ring only when retry cam is enabled. Presently, it's returning without doing anything.

Thanks,
Lijo

Quoted 8 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +2 -1
+	else
+		return;
 
-	ih = &adev->irq.ih1;
 
   	/* Get the WPTR of the last entry in IH ring */
   	last_wptr = amdgpu_ih_get_wptr(adev, ih);
   	/* Order wptr with ring data. */
#6 lore raw
Quoted 74 lines · Lijo
Quoted 71 lines · Timur Kristóf
Quoted 68 lines · Lijo
Quoted 57 lines · Alex Deucher

From: Jon Doron <[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

    BUG: kernel NULL pointer dereference, address: 0000000000000004
    RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
    Call Trace:
     amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
     svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
     amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
     gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
     amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
     amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal").  This is needed if the hardware doesn't support secondary HW IH rings.

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")

---

   drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-

   1 file changed, 6 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c index
8e65fec9f534e..243d75917458a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +0 -0
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct

amdgpu_device *adev, uint64_t addr,>
       if (adev->irq.retry_cam_enabled)

Quoted 6 lines

           return;

+    else if (adev->irq.ih1.ring_size)
+        ih = &adev->irq.ih1;
+    else if (adev->irq.ih_soft.enabled)
+        ih = &adev->irq.ih_soft;

Faults are delegated to soft ring when retry_cam is enabled -
https://gitlab.freedesktop.org/agd5f/linux/-/blob/amd-staging-drm- next/drive rs/gpu/drm/amd/amdgpu/amdgpu_gmc.c#L541

Quoted 8 lines
Quoted 5 lines

Hi,

As far as I know the retry CAM is not available on APUs. Please correct me if I'm wrong.

Retry CAM filter is available on APUs as well.

Correction - this is no longer true for newer ones.

Thanks,
Lijo

Quoted 30 lines

Thanks,
Lijo

Quoted 26 lines

Thanks,
Timur

Quoted 19 lines

That matches with the original logic in d299441654f ("drm/amdgpu: Rework retry fault removal").

To match exactly with the logic in above commit, I think it should use soft ring only when retry cam is enabled. Presently, it's returning without doing anything.

Thanks,
Lijo

Quoted 8 lines

+    else
+        return;

-    ih = &adev->irq.ih1;

       /* Get the WPTR of the last entry in IH ring */ last_wptr = amdgpu_ih_get_wptr(adev, ih); /* Order wptr with ring data. */

Reviewed-by on v1 1/1
#7 lore raw
In reply to Alex Deucher
Quoted 36 lines · Central European Standard Time Alex Deucher

From: Jon Doron <[email protected]>

On APUs such as Raven and Renoir (GC 9.1.0, 9.2.2, 9.3.0), the ih1 and ih2 interrupt ring buffers are not initialized. This is by design, as these secondary IH rings are only available on discrete GPUs. See vega10_ih_sw_init() which explicitly skips ih1/ih2 initialization when AMD_IS_APU is set.

However, amdgpu_gmc_filter_faults_remove() unconditionally uses ih1 to get the timestamp of the last interrupt entry. When retry faults are enabled on APUs (noretry=0), this function is called from the SVM page fault recovery path, resulting in a NULL pointer dereference when amdgpu_ih_decode_iv_ts_helper() attempts to access ih->ring[].

The crash manifests as:

  BUG: kernel NULL pointer dereference, address: 0000000000000004
  RIP: 0010:amdgpu_ih_decode_iv_ts_helper+0x22/0x40 [amdgpu]
  Call Trace:
   amdgpu_gmc_filter_faults_remove+0x60/0x130 [amdgpu]
   svm_range_restore_pages+0xae5/0x11c0 [amdgpu]
   amdgpu_vm_handle_fault+0xc8/0x340 [amdgpu]
   gmc_v9_0_process_interrupt+0x191/0x220 [amdgpu]
   amdgpu_irq_dispatch+0xed/0x2c0 [amdgpu]
   amdgpu_ih_process+0x84/0x100 [amdgpu]

This issue was exposed by commit 1446226d32a4 ("drm/amdgpu: Remove GC HW IP 9.3.0 from noretry=1") which changed the default for Renoir APU from noretry=1 to noretry=0, enabling retry fault handling and thus exercising the buggy code path.

Fix this by adding a check for ih1.ring_size before attempting to use it. Also restore the soft_ih support from commit dd299441654f ("drm/amdgpu:
Rework retry fault removal"). This is needed if the hardware doesn't support secondary HW IH rings.

Quoted 7 lines

v2: additional updates (Alex)

Fixes dd299441654f ("drm/amdgpu: Rework retry fault removal")

Thank you for taking care of this!

Quoted 25 lines
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
(quoted diff) +0 -0

b/drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c index
8e65fec9f534e..243d75917458a 100644

drivers/gpu/drm/amd/amdgpu/amdgpu_gmc.c +6 -1
@@ -498,8 +498,13 @@ void amdgpu_gmc_filter_faults_remove(struct
 amdgpu_device *adev, uint64_t addr,
 
 	if (adev->irq.retry_cam_enabled)
 		return;
+	else if (adev->irq.ih1.ring_size)
+		ih = &adev->irq.ih1;
+	else if (adev->irq.ih_soft.enabled)
+		ih = &adev->irq.ih_soft;
+	else
+		return;
 
-	ih = &adev->irq.ih1;
 	/* Get the WPTR of the last entry in IH ring */
 	last_wptr = amdgpu_ih_get_wptr(adev, ih);

/* Order wptr with ring data. */