Skip to contentSkip to conversation

drm/amdgpu: Make the submission path memory reclaim safe

PATCH In review Tvrtko Ursulin 6 participants 12 messages Nov 13, 2024 – Dec 17, 2024

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

PATCH 1/1 drm/amdgpu: Make the submission path memory reclaim safe

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

[ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
[ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
[ ] Call Trace:
[ ] <TASK>
...
[ ] ? check_flush_dependency+0xf5/0x110
...

 [ ]  cancel_delayed_work_sync+0x6e/0x80
 [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
 [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
 [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
 [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
 [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
 [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
 [ ]  process_one_work+0x217/0x720

...
[ ] </TASK>

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--
 3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
 
 extern int amdgpu_wbrf;
 
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
 #define AMDGPU_VM_MAX_NUM_CTX			4096
 #define AMDGPU_SG_THRESHOLD			(256*1024*1024)
 #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS	        3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +25 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer amdgpu_watchdog_timer = {
 	.period = 0x0, /* default to 0x0 (timeout disable) */
 };
 
+struct workqueue_struct *amdgpu_reclaim_wq;
+
 /**
  * DOC: vramlimit (int)
  * Restrict the total amount of VRAM in MiB for testing.  The default is 0 (Use full VRAM).
@@ -2971,6 +2973,21 @@ static struct pci_driver amdgpu_kms_pci_driver = {
 	.dev_groups = amdgpu_sysfs_groups,
 };
 
+static int amdgpu_wq_init(void)
+{
+	amdgpu_reclaim_wq =
+		alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+	if (!amdgpu_reclaim_wq)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+	destroy_workqueue(amdgpu_reclaim_wq);
+}
+
 static int __init amdgpu_init(void)
 {
 	int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
 	if (drm_firmware_drivers_only())
 		return -EINVAL;
 
+	r = amdgpu_wq_init();
+	if (r)
+		goto error_wq;
+
 	r = amdgpu_sync_init();
 	if (r)
 		goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
 	amdgpu_sync_fini();
 
 error_sync:
+	amdgpu_wq_fini();
+
+error_wq:
 	return r;
 }
 
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
 	amdgpu_acpi_release();
 	amdgpu_sync_fini();
 	amdgpu_fence_slab_fini();
+	amdgpu_wq_fini();
 	mmu_notifier_synchronize();
 	amdgpu_xcp_drv_release();
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
 						AMD_IP_BLOCK_TYPE_GFX, true))
 					adev->gfx.gfx_off_state = true;
 			} else {
-				schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-					      delay);
+				queue_delayed_work(amdgpu_reclaim_wq,
+						   &adev->gfx.gfx_off_delay_work,
+						   delay);
 			}
 		}
 	} else {
Signature · 1 line
2.46.0
maintainer
#1 lore raw

Am 13.11.24 um 14:48 schrieb Tvrtko Ursulin:

Quoted 28 lines

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

[ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
[ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
[ ] Call Trace:
[ ] <TASK>
...
[ ] ? check_flush_dependency+0xf5/0x110
...

  [ ]  cancel_delayed_work_sync+0x6e/0x80
  [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
  [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
  [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
  [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
  [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
  [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
  [ ]  process_one_work+0x217/0x720
Quoted 5 lines

...
[ ] </TASK>

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

Oh well, that is a really good catch! I wasn't aware the workqueues could be blocked by memory reclaim as well.

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

Apart from that looks good to me.

Regards,
Christian.

Quoted 109 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25 +++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--
  3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
 
  extern int amdgpu_wbrf;
 
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
  #define AMDGPU_VM_MAX_NUM_CTX			4096
  #define AMDGPU_SG_THRESHOLD			(256*1024*1024)
  #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS	        3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +25 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer amdgpu_watchdog_timer = {
  	.period = 0x0, /* default to 0x0 (timeout disable) */
  };
 
+struct workqueue_struct *amdgpu_reclaim_wq;
+
  /**
   * DOC: vramlimit (int)
   * Restrict the total amount of VRAM in MiB for testing.  The default is 0 (Use full VRAM).
@@ -2971,6 +2973,21 @@ static struct pci_driver amdgpu_kms_pci_driver = {
  	.dev_groups = amdgpu_sysfs_groups,
  };
 
+static int amdgpu_wq_init(void)
+{
+	amdgpu_reclaim_wq =
+		alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+	if (!amdgpu_reclaim_wq)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+	destroy_workqueue(amdgpu_reclaim_wq);
+}
+
  static int __init amdgpu_init(void)
  {
  	int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
  	if (drm_firmware_drivers_only())
  		return -EINVAL;
 
+	r = amdgpu_wq_init();
+	if (r)
+		goto error_wq;
+
  	r = amdgpu_sync_init();
  	if (r)
  		goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
  	amdgpu_sync_fini();
 
  error_sync:
+	amdgpu_wq_fini();
+
+error_wq:
  	return r;
  }
 
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
  	amdgpu_acpi_release();
  	amdgpu_sync_fini();
  	amdgpu_fence_slab_fini();
+	amdgpu_wq_fini();
  	mmu_notifier_synchronize();
  	amdgpu_xcp_drv_release();
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
  						AMD_IP_BLOCK_TYPE_GFX, true))
  					adev->gfx.gfx_off_state = true;
  			} else {
-				schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-					      delay);
+				queue_delayed_work(amdgpu_reclaim_wq,
+						   &adev->gfx.gfx_off_delay_work,
+						   delay);
  			}
  		}
  	} else {
#2 lore raw
Quoted 32 lines · Christian König

Am 13.11.24 um 14:48 schrieb Tvrtko Ursulin:

Quoted 31 lines

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

  [ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
  [ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
  [ ] Call Trace:
  [ ]  <TASK>
...
  [ ]  ? check_flush_dependency+0xf5/0x110
...
  [ ]  cancel_delayed_work_sync+0x6e/0x80
  [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
  [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
  [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
  [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
  [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
  [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
  [ ]  process_one_work+0x217/0x720
...
  [ ]  </TASK>

Quoted 4 lines
Quoted 2 lines

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

Oh well, that is a really good catch! I wasn't aware the workqueues could be blocked by memory reclaim as well.

Only credit I can take is for the habit that I often run with many kernel debugging aids enabled.

Quoted 1 line

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

I wondered the same but did not find any. Only ones I am aware of are system_wq&co created in workqueue_init_early().

Regards,

Tvrtko

Quoted 110 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 104 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25 +++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--

  3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
   extern int amdgpu_wbrf;
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
   #define AMDGPU_VM_MAX_NUM_CTX            4096
   #define AMDGPU_SG_THRESHOLD            (256*1024*1024)
   #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS            3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer amdgpu_watchdog_timer 
 = {

      .period = 0x0, /* default to 0x0 (timeout disable) */ };

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+struct workqueue_struct *amdgpu_reclaim_wq;
+

  /**
   * DOC: vramlimit (int)
   * Restrict the total amount of VRAM in MiB for testing.  The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +19 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver amdgpu_kms_pci_driver = {
       .dev_groups = amdgpu_sysfs_groups,
   };
+static int amdgpu_wq_init(void)
+{
+    amdgpu_reclaim_wq =
+        alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+    if (!amdgpu_reclaim_wq)
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+    destroy_workqueue(amdgpu_reclaim_wq);
+}
+
   static int __init amdgpu_init(void)
   {
       int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
       if (drm_firmware_drivers_only())
           return -EINVAL;
+    r = amdgpu_wq_init();
+    if (r)
+        goto error_wq;
+
       r = amdgpu_sync_init();
       if (r)
           goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
       amdgpu_sync_fini();

  error_sync:
+    amdgpu_wq_fini();

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+
+error_wq:

      return r;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
       amdgpu_acpi_release();
       amdgpu_sync_fini();
       amdgpu_fence_slab_fini();
+    amdgpu_wq_fini();
       mmu_notifier_synchronize();
       amdgpu_xcp_drv_release();
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device 
 *adev, bool enable)
                           AMD_IP_BLOCK_TYPE_GFX, true))
                       adev->gfx.gfx_off_state = true;
               } else {
-                schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-                          delay);
+                queue_delayed_work(amdgpu_reclaim_wq,
+                           &adev->gfx.gfx_off_delay_work,
+                           delay);
               }
           }

      } else {

#3 lore raw
Quoted 34 lines · Tvrtko Ursulin
Quoted 32 lines · Christian König

Am 13.11.24 um 14:48 schrieb Tvrtko Ursulin:

Quoted 31 lines

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

  [ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
  [ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
  [ ] Call Trace:
  [ ]  <TASK>
...
  [ ]  ? check_flush_dependency+0xf5/0x110
...
  [ ]  cancel_delayed_work_sync+0x6e/0x80
  [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
  [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
  [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
  [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
  [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
  [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
  [ ]  process_one_work+0x217/0x720
...
  [ ]  </TASK>

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

Oh well, that is a really good catch! I wasn't aware the workqueues could be blocked by memory reclaim as well.

Only credit I can take is for the habit that I often run with many kernel debugging aids enabled.

Although this one actually isn't even under "Kernel hacking".

Quoted 3 lines
Quoted 1 line

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

I wondered the same but did not find any. Only ones I am aware of are system_wq&co created in workqueue_init_early().

Gentle ping on this. I don't have any better ideas that creating a new wq.

Regards,

Tvrtko

Quoted 111 lines
Quoted 111 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 105 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25 +++++++++++++++++++++++++ drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--

  3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
   extern int amdgpu_wbrf;
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
   #define AMDGPU_VM_MAX_NUM_CTX            4096
   #define AMDGPU_SG_THRESHOLD            (256*1024*1024)
   #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS            3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer 
 amdgpu_watchdog_timer = {

      .period = 0x0, /* default to 0x0 (timeout disable) */ };

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+struct workqueue_struct *amdgpu_reclaim_wq;
+

  /**
   * DOC: vramlimit (int)
   * Restrict the total amount of VRAM in MiB for testing.  The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +19 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver amdgpu_kms_pci_driver 
 = {
       .dev_groups = amdgpu_sysfs_groups,
   };
+static int amdgpu_wq_init(void)
+{
+    amdgpu_reclaim_wq =
+        alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+    if (!amdgpu_reclaim_wq)
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+    destroy_workqueue(amdgpu_reclaim_wq);
+}
+
   static int __init amdgpu_init(void)
   {
       int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
       if (drm_firmware_drivers_only())
           return -EINVAL;
+    r = amdgpu_wq_init();
+    if (r)
+        goto error_wq;
+
       r = amdgpu_sync_init();
       if (r)
           goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
       amdgpu_sync_fini();

  error_sync:
+    amdgpu_wq_fini();

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+
+error_wq:

      return r;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
       amdgpu_acpi_release();
       amdgpu_sync_fini();
       amdgpu_fence_slab_fini();
+    amdgpu_wq_fini();
       mmu_notifier_synchronize();
       amdgpu_xcp_drv_release();
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device 
 *adev, bool enable)
                           AMD_IP_BLOCK_TYPE_GFX, true))
                       adev->gfx.gfx_off_state = true;
               } else {
-                schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-                          delay);
+                queue_delayed_work(amdgpu_reclaim_wq,
+                           &adev->gfx.gfx_off_delay_work,
+                           delay);
               }
           }

      } else {

maintainer
#4 lore raw

Am 22.11.24 um 12:34 schrieb Tvrtko Ursulin:

Quoted 41 lines · Tvrtko Ursulin
Quoted 33 lines · Christian König
Quoted 32 lines

Am 13.11.24 um 14:48 schrieb Tvrtko Ursulin:

Quoted 31 lines

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

  [ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
  [ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
  [ ] Call Trace:
  [ ]  <TASK>
...
  [ ]  ? check_flush_dependency+0xf5/0x110
...
  [ ]  cancel_delayed_work_sync+0x6e/0x80
  [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
  [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
  [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
  [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
  [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
  [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
  [ ]  process_one_work+0x217/0x720
...
  [ ]  </TASK>

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

Oh well, that is a really good catch! I wasn't aware the workqueues could be blocked by memory reclaim as well.

Only credit I can take is for the habit that I often run with many kernel debugging aids enabled.

Quoted 7 lines

Although this one actually isn't even under "Kernel hacking".

Quoted 3 lines
Quoted 1 line

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

I wondered the same but did not find any. Only ones I am aware of are system_wq&co created in workqueue_init_early().

Gentle ping on this. I don't have any better ideas that creating a new wq.

It took me a moment to realize, but I now think this warning message is a false positive.

What happens is that the code calls cancel_delayed_work_sync().

If the work item never run because of lack of memory then it can just be canceled.

If the work item is running then we will block for it to finish.

There is no need to use WQ_MEM_RECLAIM for the workqueue or do I miss something?

If I'm not completely mistaken you stumbled over a bug in the warning code instead :)

Regards,
Christian.

Quoted 116 lines

Regards,

Tvrtko

Quoted 112 lines
Quoted 112 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 106 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25
+++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--

  3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
   extern int amdgpu_wbrf;
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
   #define AMDGPU_VM_MAX_NUM_CTX            4096
   #define AMDGPU_SG_THRESHOLD            (256*1024*1024)
   #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS            3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer 
 amdgpu_watchdog_timer = {

      .period = 0x0, /* default to 0x0 (timeout disable) */ };

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+struct workqueue_struct *amdgpu_reclaim_wq;
+

  /**
   * DOC: vramlimit (int)
   * Restrict the total amount of VRAM in MiB for testing. The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +19 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver 
 amdgpu_kms_pci_driver = {
       .dev_groups = amdgpu_sysfs_groups,
   };
+static int amdgpu_wq_init(void)
+{
+    amdgpu_reclaim_wq =
+        alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+    if (!amdgpu_reclaim_wq)
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+    destroy_workqueue(amdgpu_reclaim_wq);
+}
+
   static int __init amdgpu_init(void)
   {
       int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
       if (drm_firmware_drivers_only())
           return -EINVAL;
+    r = amdgpu_wq_init();
+    if (r)
+        goto error_wq;
+
       r = amdgpu_sync_init();
       if (r)
           goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
       amdgpu_sync_fini();

  error_sync:
+    amdgpu_wq_fini();

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+
+error_wq:

      return r;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
       amdgpu_acpi_release();
       amdgpu_sync_fini();
       amdgpu_fence_slab_fini();
+    amdgpu_wq_fini();
       mmu_notifier_synchronize();
       amdgpu_xcp_drv_release();
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device 
 *adev, bool enable)
                           AMD_IP_BLOCK_TYPE_GFX, true))
                       adev->gfx.gfx_off_state = true;
               } else {
- schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-                          delay);
+                queue_delayed_work(amdgpu_reclaim_wq,
+ &adev->gfx.gfx_off_delay_work,
+                           delay);
               }
           }

      } else {

#5 lore raw
Quoted 54 lines · Christian König

Am 22.11.24 um 12:34 schrieb Tvrtko Ursulin:

Quoted 41 lines · Tvrtko Ursulin
Quoted 33 lines · Christian König
Quoted 32 lines

Am 13.11.24 um 14:48 schrieb Tvrtko Ursulin:

Quoted 31 lines

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

  [ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
  [ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
  [ ] Call Trace:
  [ ]  <TASK>
...
  [ ]  ? check_flush_dependency+0xf5/0x110
...
  [ ]  cancel_delayed_work_sync+0x6e/0x80
  [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
  [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
  [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
  [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
  [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
  [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
  [ ]  process_one_work+0x217/0x720
...
  [ ]  </TASK>

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

Oh well, that is a really good catch! I wasn't aware the workqueues could be blocked by memory reclaim as well.

Only credit I can take is for the habit that I often run with many kernel debugging aids enabled.

Quoted 7 lines

Although this one actually isn't even under "Kernel hacking".

Quoted 3 lines
Quoted 1 line

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

I wondered the same but did not find any. Only ones I am aware of are system_wq&co created in workqueue_init_early().

Gentle ping on this. I don't have any better ideas that creating a new wq.

It took me a moment to realize, but I now think this warning message is a false positive.

What happens is that the code calls cancel_delayed_work_sync().

Quoted 7 lines

If the work item never run because of lack of memory then it can just be canceled.

If the work item is running then we will block for it to finish.

There is no need to use WQ_MEM_RECLAIM for the workqueue or do I miss something?

If I'm not completely mistaken you stumbled over a bug in the warning code instead :)

Hmm your thinking sounds convincing.

Adding Tejun if he has time to help brainstorm this.

Question is - does check_flush_dependency() need to skip the !WQ_MEM_RECLAIM flushing WQ_MEM_RECLAIM warning *if* the work is already running *and* it was called from cancel_delayed_work_sync()?

Regards,

Tvrtko

Quoted 113 lines
Quoted 112 lines
Quoted 112 lines
Quoted 112 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 106 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>

---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25
+++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--

  3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
   extern int amdgpu_wbrf;
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
   #define AMDGPU_VM_MAX_NUM_CTX            4096
   #define AMDGPU_SG_THRESHOLD            (256*1024*1024)
   #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS            3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer 
 amdgpu_watchdog_timer = {

      .period = 0x0, /* default to 0x0 (timeout disable) */ };

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+struct workqueue_struct *amdgpu_reclaim_wq;
+

  /**
   * DOC: vramlimit (int)
   * Restrict the total amount of VRAM in MiB for testing. The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +19 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver 
 amdgpu_kms_pci_driver = {
       .dev_groups = amdgpu_sysfs_groups,
   };
+static int amdgpu_wq_init(void)
+{
+    amdgpu_reclaim_wq =
+        alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+    if (!amdgpu_reclaim_wq)
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+    destroy_workqueue(amdgpu_reclaim_wq);
+}
+
   static int __init amdgpu_init(void)
   {
       int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
       if (drm_firmware_drivers_only())
           return -EINVAL;
+    r = amdgpu_wq_init();
+    if (r)
+        goto error_wq;
+
       r = amdgpu_sync_init();
       if (r)
           goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
       amdgpu_sync_fini();

  error_sync:
+    amdgpu_wq_fini();

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+
+error_wq:

      return r;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
       amdgpu_acpi_release();
       amdgpu_sync_fini();
       amdgpu_fence_slab_fini();
+    amdgpu_wq_fini();
       mmu_notifier_synchronize();
       amdgpu_xcp_drv_release();
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device 
 *adev, bool enable)
                           AMD_IP_BLOCK_TYPE_GFX, true))
                       adev->gfx.gfx_off_state = true;
               } else {
- schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-                          delay);
+                queue_delayed_work(amdgpu_reclaim_wq,
+ &adev->gfx.gfx_off_delay_work,
+                           delay);
               }
           }

      } else {

#6 lore raw
Quoted 66 lines · Tvrtko Ursulin
Quoted 64 lines · Christian König

Am 22.11.24 um 12:34 schrieb Tvrtko Ursulin:

Quoted 51 lines · Tvrtko Ursulin
Quoted 42 lines · Christian König
Quoted 41 lines

Am 13.11.24 um 14:48 schrieb Tvrtko Ursulin:

Quoted 39 lines

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")
points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"),
any workqueue flushing done from the job submission path must only involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

  [ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
  [ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
  [ ] Call Trace:
  [ ]  <TASK>
...
  [ ]  ? check_flush_dependency+0xf5/0x110
...
  [ ]  cancel_delayed_work_sync+0x6e/0x80
  [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
  [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
  [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
  [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched] [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
  [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched] [ ]  process_one_work+0x217/0x720
...
  [ ]  </TASK>

Fix this by creating a memory reclaim safe driver
workqueue and make the
submission path use it.

Quoted 5 lines
Quoted 2 lines

Oh well, that is a really good catch! I wasn't aware the workqueues could be blocked by memory reclaim as well.

Only credit I can take is for the habit that I often run with many kernel debugging aids enabled.

Although this one actually isn't even under "Kernel hacking".

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

I wondered the same but did not find any. Only ones I am aware of are system_wq&co created in workqueue_init_early().

Gentle ping on this. I don't have any better ideas that creating a new wq.

It took me a moment to realize, but I now think this warning message is a false positive.

Quoted 7 lines
Quoted 7 lines

What happens is that the code calls cancel_delayed_work_sync().

If the work item never run because of lack of memory then it can just be canceled.

If the work item is running then we will block for it to finish.

Apologies for the late reply. Alex responded to another thread and CC'd me, which reminded me to reply here.

The execution of the non-reclaim worker could have led to a few scenarios:

- It might have triggered reclaim through its own memory allocation.
- It could have been running and then context-switched out, with reclaim being triggered elsewhere in the mean time, pausing the execution of the non-reclaim worker.

In either case, during reclaim, if you wait on a DMA fence that depends on the DRM scheduler worker, and that worker attempts to flush the above non-reclaim worker, it will result in a deadlock.

The annotation appears correct to me, and I believe Tvrtko's patch is indeed accurate. For what it's worth, we encountered several similar bugs in Xe that emerged once we added the correct work queue annotations.

Quoted 10 lines
Quoted 5 lines

There is no need to use WQ_MEM_RECLAIM for the workqueue or do I miss something?

If I'm not completely mistaken you stumbled over a bug in the warning code instead :)

Hmm your thinking sounds convincing.

Adding Tejun if he has time to help brainstorm this.

Tejun could likely provide insight into whether my above assessment is correct.

Matt

Quoted 127 lines

Question is - does check_flush_dependency() need to skip the !WQ_MEM_RECLAIM flushing WQ_MEM_RECLAIM warning *if* the work is already running *and* it was called from cancel_delayed_work_sync()?

Regards,

Tvrtko

Quoted 119 lines
Quoted 118 lines
Quoted 118 lines
Quoted 118 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 112 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler

to use a work queue rather than kthread")
Cc: [email protected]
Cc: Matthew Brost <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: Philipp Stanner <[email protected]>
Cc: Alex Deucher <[email protected]>
Cc: Christian König <[email protected]>
---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25
+++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--

  3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
   extern int amdgpu_wbrf;
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
   #define AMDGPU_VM_MAX_NUM_CTX            4096
   #define AMDGPU_SG_THRESHOLD            (256*1024*1024)
   #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS            3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer
 amdgpu_watchdog_timer = {

      .period = 0x0, /* default to 0x0 (timeout disable) */ };

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+struct workqueue_struct *amdgpu_reclaim_wq;
+

  /**
   * DOC: vramlimit (int)
   * Restrict the total amount of VRAM in MiB for
testing. The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +19 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver
 amdgpu_kms_pci_driver = {
       .dev_groups = amdgpu_sysfs_groups,
   };
+static int amdgpu_wq_init(void)
+{
+    amdgpu_reclaim_wq =
+        alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+    if (!amdgpu_reclaim_wq)
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+    destroy_workqueue(amdgpu_reclaim_wq);
+}
+
   static int __init amdgpu_init(void)
   {
       int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
       if (drm_firmware_drivers_only())
           return -EINVAL;
+    r = amdgpu_wq_init();
+    if (r)
+        goto error_wq;
+
       r = amdgpu_sync_init();
       if (r)
           goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
       amdgpu_sync_fini();

  error_sync:
+    amdgpu_wq_fini();

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+
+error_wq:

      return r;
  }

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
       amdgpu_acpi_release();
       amdgpu_sync_fini();
       amdgpu_fence_slab_fini();
+    amdgpu_wq_fini();
       mmu_notifier_synchronize();
       amdgpu_xcp_drv_release();
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct
 amdgpu_device *adev, bool enable)
                           AMD_IP_BLOCK_TYPE_GFX, true))
                       adev->gfx.gfx_off_state = true;
               } else {
- schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-                          delay);
+                queue_delayed_work(amdgpu_reclaim_wq,
+ &adev->gfx.gfx_off_delay_work,
+                           delay);
               }
           }

      } else {

maintainer
#7 lore raw

Am 17.12.24 um 01:26 schrieb Matthew Brost:

Quoted 16 lines · Tvrtko Ursulin
Quoted 15 lines

[SNIP]

Quoted 14 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

I wondered the same but did not find any. Only ones I am aware of are system_wq&co created in workqueue_init_early().

Gentle ping on this. I don't have any better ideas that creating a new wq.

It took me a moment to realize, but I now think this warning message is a false positive.

What happens is that the code calls cancel_delayed_work_sync().

If the work item never run because of lack of memory then it can just be canceled.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

If the work item is running then we will block for it to finish.

Apologies for the late reply. Alex responded to another thread and CC'd me, which reminded me to reply here.

The execution of the non-reclaim worker could have led to a few scenarios:

- It might have triggered reclaim through its own memory allocation.

That is unrelated and has nothing todo with WQ_MEM_RECLAIM.

What we should do is to make sure that the lockdep annotation covers all workers who play a role in fence signaling.

Quoted 3 lines

- It could have been running and then context-switched out, with reclaim being triggered elsewhere in the mean time, pausing the execution of the non-reclaim worker.

As far as I know non-reclaim workers are not paused because a reclaim worker is running, that would be really new to me.

What happens is that here (from workqueue.c):

* Workqueue rescuer thread function. There's one rescuer for each * workqueue which has WQ_MEM_RECLAIM set. * * Regular work processing on a pool may block trying to create a new * worker which uses GFP_KERNEL allocation which has slight chance of * developing into deadlock if some works currently on the same queue * need to be processed to satisfy the GFP_KERNEL allocation. This is * the problem rescuer solves.

Quoted 3 lines

In either case, during reclaim, if you wait on a DMA fence that depends on the DRM scheduler worker,and that worker attempts to flush the above non-reclaim worker, it will result in a deadlock.

Well that is only partially correct.

It's true that the worker we wait for can't wait for DMA-fence or do memory allocations who wait for DMA-fences. But WQ_MEM_RECLAIM is not related to any DMA fence annotation.

What happens instead is that the kernel always keeps a kernel thread pre-allocated so that it can guarantee that the worker can start without allocating memory.

As soon as the worker runs there shouldn't be any difference in the handling as far as I know.

Quoted 4 lines

The annotation appears correct to me, and I believe Tvrtko's patch is indeed accurate. For what it's worth, we encountered several similar bugs in Xe that emerged once we added the correct work queue annotations.

I think you mean something different. This is the lockdep annotation for the workers and not WQ_MEM_RECLAIM.

Regards,
Christian.

Quoted 138 lines
Quoted 9 lines
Quoted 5 lines

There is no need to use WQ_MEM_RECLAIM for the workqueue or do I miss something?

If I'm not completely mistaken you stumbled over a bug in the warning code instead :)

Hmm your thinking sounds convincing.

Adding Tejun if he has time to help brainstorm this.

Tejun could likely provide insight into whether my above assessment is correct.

Matt

Quoted 124 lines

Question is - does check_flush_dependency() need to skip the !WQ_MEM_RECLAIM flushing WQ_MEM_RECLAIM warning *if* the work is already running *and* it was called from cancel_delayed_work_sync()?

Regards,

Tvrtko

Quoted 116 lines
Quoted 116 lines
Quoted 116 lines
Quoted 116 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 111 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler

to use a work queue rather than kthread") Cc:[email protected]
Cc: Matthew Brost<[email protected]>
Cc: Danilo Krummrich<[email protected]>
Cc: Philipp Stanner<[email protected]>
Cc: Alex Deucher<[email protected]>
Cc: Christian König<[email protected]>

---
   drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++
   drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25
+++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--
   3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
   extern int amdgpu_wbrf;
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
   #define AMDGPU_VM_MAX_NUM_CTX            4096
   #define AMDGPU_SG_THRESHOLD            (256*1024*1024)
   #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS            3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer
 amdgpu_watchdog_timer = {
       .period = 0x0, /* default to 0x0 (timeout disable) */
   };
+struct workqueue_struct *amdgpu_reclaim_wq;
+
   /**
    * DOC: vramlimit (int)
    * Restrict the total amount of VRAM in MiB for

testing. The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +23 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver
 amdgpu_kms_pci_driver = {
       .dev_groups = amdgpu_sysfs_groups,
   };
+static int amdgpu_wq_init(void)
+{
+    amdgpu_reclaim_wq =
+        alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+    if (!amdgpu_reclaim_wq)
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+    destroy_workqueue(amdgpu_reclaim_wq);
+}
+
   static int __init amdgpu_init(void)
   {
       int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
       if (drm_firmware_drivers_only())
           return -EINVAL;
+    r = amdgpu_wq_init();
+    if (r)
+        goto error_wq;
+
       r = amdgpu_sync_init();
       if (r)
           goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
       amdgpu_sync_fini();
   error_sync:
+    amdgpu_wq_fini();
+
+error_wq:
       return r;
   }
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
       amdgpu_acpi_release();
       amdgpu_sync_fini();
       amdgpu_fence_slab_fini();
+    amdgpu_wq_fini();
       mmu_notifier_synchronize();
       amdgpu_xcp_drv_release();
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct
 amdgpu_device *adev, bool enable)
                           AMD_IP_BLOCK_TYPE_GFX, true))
                       adev->gfx.gfx_off_state = true;
               } else {
- schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-                          delay);
+                queue_delayed_work(amdgpu_reclaim_wq,
+ &adev->gfx.gfx_off_delay_work,
+                           delay);
               }
           }

      } else {

maintainer
#8 lore raw

PATCH 1/1 drm/amdgpu: Make the submission path memory reclaim safe

Sending it out to the mailing lists once more because AMD mail servers tried to convert it to HTML :(

Am 17.12.24 um 01:26 schrieb Matthew Brost:

Quoted 16 lines · Tvrtko Ursulin
Quoted 15 lines

[SNIP]

Quoted 14 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Do we have system wide workqueues for that? It seems a bit overkill that amdgpu has to allocate one on his own.

I wondered the same but did not find any. Only ones I am aware of are system_wq&co created in workqueue_init_early().

Gentle ping on this. I don't have any better ideas that creating a new wq.

It took me a moment to realize, but I now think this warning message is a false positive.

What happens is that the code calls cancel_delayed_work_sync().

If the work item never run because of lack of memory then it can just be canceled.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

If the work item is running then we will block for it to finish.

Apologies for the late reply. Alex responded to another thread and CC'd me, which reminded me to reply here.

The execution of the non-reclaim worker could have led to a few scenarios:

- It might have triggered reclaim through its own memory allocation.

That is unrelated and has nothing todo with WQ_MEM_RECLAIM.

What we should do is to make sure that the lockdep annotation covers all workers who play a role in fence signaling.

Quoted 3 lines

- It could have been running and then context-switched out, with reclaim being triggered elsewhere in the mean time, pausing the execution of the non-reclaim worker.

As far as I know non-reclaim workers are not paused because a reclaim worker is running, that would be really new to me.

What happens is that here (from workqueue.c):

* Workqueue rescuer thread function. There's one rescuer for each * workqueue which has WQ_MEM_RECLAIM set. * * Regular work processing on a pool may block trying to create a new * worker which uses GFP_KERNEL allocation which has slight chance of * developing into deadlock if some works currently on the same queue * need to be processed to satisfy the GFP_KERNEL allocation. This is * the problem rescuer solves.

Quoted 3 lines

In either case, during reclaim, if you wait on a DMA fence that depends on the DRM scheduler worker,and that worker attempts to flush the above non-reclaim worker, it will result in a deadlock.

Well that is only partially correct.

It's true that the worker we wait for can't wait for DMA-fence or do memory allocations who wait for DMA-fences. But WQ_MEM_RECLAIM is not related to any DMA fence annotation.

What happens instead is that the kernel always keeps a kernel thread pre-allocated so that it can guarantee that the worker can start without allocating memory.

As soon as the worker runs there shouldn't be any difference in the handling as far as I know.

Quoted 4 lines

The annotation appears correct to me, and I believe Tvrtko's patch is indeed accurate. For what it's worth, we encountered several similar bugs in Xe that emerged once we added the correct work queue annotations.

I think you mean something different. This is the lockdep annotation for the workers and not WQ_MEM_RECLAIM.

Regards,
Christian.

Quoted 137 lines
Quoted 9 lines
Quoted 5 lines

There is no need to use WQ_MEM_RECLAIM for the workqueue or do I miss something?

If I'm not completely mistaken you stumbled over a bug in the warning code instead :)

Hmm your thinking sounds convincing.

Adding Tejun if he has time to help brainstorm this.

Tejun could likely provide insight into whether my above assessment is correct.
Matt

Quoted 124 lines

Question is - does check_flush_dependency() need to skip the !WQ_MEM_RECLAIM flushing WQ_MEM_RECLAIM warning *if* the work is already running *and* it was called from cancel_delayed_work_sync()?

Regards,

Tvrtko

Quoted 116 lines
Quoted 116 lines
Quoted 116 lines
Quoted 116 lines

Apart from that looks good to me.

Regards,
Christian.

Quoted 111 lines

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler

to use a work queue rather than kthread") Cc:[email protected]
Cc: Matthew Brost<[email protected]>
Cc: Danilo Krummrich<[email protected]>
Cc: Philipp Stanner<[email protected]>
Cc: Alex Deucher<[email protected]>
Cc: Christian König<[email protected]>

---
   drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++
   drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25
+++++++++++++++++++++++++
   drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--
   3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
   extern int amdgpu_wbrf;
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
   #define AMDGPU_VM_MAX_NUM_CTX            4096
   #define AMDGPU_SG_THRESHOLD            (256*1024*1024)
   #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS            3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer
 amdgpu_watchdog_timer = {
       .period = 0x0, /* default to 0x0 (timeout disable) */
   };
+struct workqueue_struct *amdgpu_reclaim_wq;
+
   /**
    * DOC: vramlimit (int)
    * Restrict the total amount of VRAM in MiB for

testing. The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +23 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver
 amdgpu_kms_pci_driver = {
       .dev_groups = amdgpu_sysfs_groups,
   };
+static int amdgpu_wq_init(void)
+{
+    amdgpu_reclaim_wq =
+        alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+    if (!amdgpu_reclaim_wq)
+        return -ENOMEM;
+
+    return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+    destroy_workqueue(amdgpu_reclaim_wq);
+}
+
   static int __init amdgpu_init(void)
   {
       int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
       if (drm_firmware_drivers_only())
           return -EINVAL;
+    r = amdgpu_wq_init();
+    if (r)
+        goto error_wq;
+
       r = amdgpu_sync_init();
       if (r)
           goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
       amdgpu_sync_fini();
   error_sync:
+    amdgpu_wq_fini();
+
+error_wq:
       return r;
   }
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
       amdgpu_acpi_release();
       amdgpu_sync_fini();
       amdgpu_fence_slab_fini();
+    amdgpu_wq_fini();
       mmu_notifier_synchronize();
       amdgpu_xcp_drv_release();
   }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct
 amdgpu_device *adev, bool enable)
                           AMD_IP_BLOCK_TYPE_GFX, true))
                       adev->gfx.gfx_off_state = true;
               } else {
- schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-                          delay);
+                queue_delayed_work(amdgpu_reclaim_wq,
+ &adev->gfx.gfx_off_delay_work,
+                           delay);
               }
           }

      } else {

Tested-by on v1 1/1
#9 lore raw
In reply to Tvrtko Ursulin
Quoted 132 lines · středoevropský standardní čas Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

[ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
[ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
[ ] Call Trace:
[ ] <TASK>
...
[ ] ? check_flush_dependency+0xf5/0x110
...

 [ ]  cancel_delayed_work_sync+0x6e/0x80
 [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
 [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
 [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
 [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
 [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
 [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
 [ ]  process_one_work+0x217/0x720

...
[ ] </TASK>

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++
 drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25 +++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--
 3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
 
 extern int amdgpu_wbrf;
 
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
 #define AMDGPU_VM_MAX_NUM_CTX			4096
 #define AMDGPU_SG_THRESHOLD			(256*1024*1024)
 #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS	        3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +25 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer amdgpu_watchdog_timer = {
 	.period = 0x0, /* default to 0x0 (timeout disable) */
 };
 
+struct workqueue_struct *amdgpu_reclaim_wq;
+
 /**
  * DOC: vramlimit (int)
  * Restrict the total amount of VRAM in MiB for testing.  The default is 0 (Use full VRAM).
@@ -2971,6 +2973,21 @@ static struct pci_driver amdgpu_kms_pci_driver = {
 	.dev_groups = amdgpu_sysfs_groups,
 };
 
+static int amdgpu_wq_init(void)
+{
+	amdgpu_reclaim_wq =
+		alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+	if (!amdgpu_reclaim_wq)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+	destroy_workqueue(amdgpu_reclaim_wq);
+}
+
 static int __init amdgpu_init(void)
 {
 	int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
 	if (drm_firmware_drivers_only())
 		return -EINVAL;
 
+	r = amdgpu_wq_init();
+	if (r)
+		goto error_wq;
+
 	r = amdgpu_sync_init();
 	if (r)
 		goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
 	amdgpu_sync_fini();
 
 error_sync:
+	amdgpu_wq_fini();
+
+error_wq:
 	return r;
 }
 
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
 	amdgpu_acpi_release();
 	amdgpu_sync_fini();
 	amdgpu_fence_slab_fini();
+	amdgpu_wq_fini();
 	mmu_notifier_synchronize();
 	amdgpu_xcp_drv_release();
 }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
 						AMD_IP_BLOCK_TYPE_GFX, true))

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable) · line 798

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
 					adev->gfx.gfx_off_state = true;
 			} else {
-				schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-					      delay);
+				queue_delayed_work(amdgpu_reclaim_wq,
+						   &adev->gfx.gfx_off_delay_work,
+						   delay);
 			}
 		}
 	} else {

I can confirm this fixed the warning for me.

What's the fate of this submission?

Thank you.

Signature · 1 line
Oleksandr Natalenko, MSE
maintainer
#10 lore raw

Am 03.12.24 um 15:24 schrieb Oleksandr Natalenko:

Quoted 145 lines · středoevropský standardní čas Tvrtko Ursulin
Quoted 132 lines

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM") points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"), any workqueue flushing done from the job submission path must only
involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

[ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
[ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
[ ] Call Trace:
[ ] <TASK>
...
[ ] ? check_flush_dependency+0xf5/0x110
...

  [ ]  cancel_delayed_work_sync+0x6e/0x80
  [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
  [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
  [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
  [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched]
  [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
  [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched]
  [ ]  process_one_work+0x217/0x720

...
[ ] </TASK>

Fix this by creating a memory reclaim safe driver workqueue and make the submission path use it.

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread")
Cc Matthew Brost <[email protected]>
Cc Danilo Krummrich <[email protected]>
Cc Philipp Stanner <[email protected]>
Cc Alex Deucher <[email protected]>
Cc Christian König <[email protected]>
---
  drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++
  drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25 +++++++++++++++++++++++++
  drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--
  3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;
 
  extern int amdgpu_wbrf;
 
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+
  #define AMDGPU_VM_MAX_NUM_CTX			4096
  #define AMDGPU_SG_THRESHOLD			(256*1024*1024)
  #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS	        3000
drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +25 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer amdgpu_watchdog_timer = {
  	.period = 0x0, /* default to 0x0 (timeout disable) */
  };
 
+struct workqueue_struct *amdgpu_reclaim_wq;
+
  /**
   * DOC: vramlimit (int)
   * Restrict the total amount of VRAM in MiB for testing.  The default is 0 (Use full VRAM).
@@ -2971,6 +2973,21 @@ static struct pci_driver amdgpu_kms_pci_driver = {
  	.dev_groups = amdgpu_sysfs_groups,
  };
 
+static int amdgpu_wq_init(void)
+{
+	amdgpu_reclaim_wq =
+		alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM, 0);
+	if (!amdgpu_reclaim_wq)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+	destroy_workqueue(amdgpu_reclaim_wq);
+}
+
  static int __init amdgpu_init(void)
  {
  	int r;
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
  	if (drm_firmware_drivers_only())
  		return -EINVAL;
 
+	r = amdgpu_wq_init();
+	if (r)
+		goto error_wq;
+
  	r = amdgpu_sync_init();
  	if (r)
  		goto error_sync;
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
  	amdgpu_sync_fini();
 
  error_sync:
+	amdgpu_wq_fini();
+
+error_wq:
  	return r;
  }
 
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
  	amdgpu_acpi_release();
  	amdgpu_sync_fini();
  	amdgpu_fence_slab_fini();
+	amdgpu_wq_fini();
  	mmu_notifier_synchronize();
  	amdgpu_xcp_drv_release();
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable)
  						AMD_IP_BLOCK_TYPE_GFX, true))

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_gfx_off_ctrl(struct amdgpu_device *adev, bool enable) · line 798

Quoted 10 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +3 -2
  					adev->gfx.gfx_off_state = true;
  			} else {
-				schedule_delayed_work(&adev->gfx.gfx_off_delay_work,
-					      delay);
+				queue_delayed_work(amdgpu_reclaim_wq,
+						   &adev->gfx.gfx_off_delay_work,
+						   delay);
  			}
  		}
  	} else {

I can confirm this fixed the warning for me.

Quoted 3 lines

What's the fate of this submission?

It turned out that the warning is actually a false negative.

E.g. the warning shouldn't be printed in the first place.

Tvrtko pinged the relevant maintainer, but for far I haven't seen a reply.

Regards,
Christian.

Quoted 3 lines

Thank you.

#11 lore raw
In reply to Tvrtko Ursulin

[+cc Krzysztof, who I think witnessed a possibly related Kernel crash in the wild]

P.

Quoted 163 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

As commit 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")
points out, ever since
a6149f039369 ("drm/sched: Convert drm scheduler to use a work queue rather than kthread"),
any workqueue flushing done from the job submission path must only involve memory reclaim safe workqueues to be safe against reclaim deadlocks.

This is also pointed out by workqueue sanity checks:

 [ ] workqueue: WQ_MEM_RECLAIM sdma0:drm_sched_run_job_work [gpu_sched] is flushing !WQ_MEM_RECLAIM events:amdgpu_device_delay_enable_gfx_off [amdgpu]
...
 [ ] Workqueue: sdma0 drm_sched_run_job_work [gpu_sched]
...
 [ ] Call Trace:
 [ ]  <TASK>
...
 [ ]  ? check_flush_dependency+0xf5/0x110
...
 [ ]  cancel_delayed_work_sync+0x6e/0x80
 [ ]  amdgpu_gfx_off_ctrl+0xab/0x140 [amdgpu]
 [ ]  amdgpu_ring_alloc+0x40/0x50 [amdgpu]
 [ ]  amdgpu_ib_schedule+0xf4/0x810 [amdgpu]
 [ ]  ? drm_sched_run_job_work+0x22c/0x430 [gpu_sched] [ ]  amdgpu_job_run+0xaa/0x1f0 [amdgpu]
 [ ]  drm_sched_run_job_work+0x257/0x430 [gpu_sched] [ ]  process_one_work+0x217/0x720
...
 [ ]  </TASK>

Fix this by creating a memory reclaim safe driver workqueue and make the
submission path use it.

References: 746ae46c1113 ("drm/sched: Mark scheduler work queues with WQ_MEM_RECLAIM")

Fixes a6149f039369 ("drm/sched: Convert drm scheduler to use a work

queue rather than kthread")
Cc: [email protected]
Cc: Matthew Brost <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: Philipp Stanner <[email protected]>
Cc: Alex Deucher <[email protected]>
Cc: Christian König <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu.h     |  2 ++ drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c | 25
+++++++++++++++++++++++++
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c |  5 +++--

 3 files changed, 30 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu.h +0 -0
index 7645e498faa4..a6aad687537e 100644
@@ -268,6 +268,8 @@ extern int amdgpu_agp;


 extern int amdgpu_wbrf;

drivers/gpu/drm/amd/amdgpu/amdgpu.h +2 -0
+extern struct workqueue_struct *amdgpu_reclaim_wq;
+

 #define AMDGPU_VM_MAX_NUM_CTX 4096
 #define AMDGPU_SG_THRESHOLD (256*1024*1024)
 #define AMDGPU_WAIT_IDLE_TIMEOUT_IN_MS         3000

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
index 38686203bea6..f5b7172e8042 100644
@@ -255,6 +255,8 @@ struct amdgpu_watchdog_timer
 amdgpu_watchdog_timer = {

  .period = 0x0, /* default to 0x0 (timeout disable) */ };

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +2 -0
+struct workqueue_struct *amdgpu_reclaim_wq;
+

 /**
  * DOC: vramlimit (int)
  * Restrict the total amount of VRAM in MiB for testing.  The default is 0 (Use full VRAM).

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
@@ -2971,6 +2973,21 @@ static struct pci_driver amdgpu_kms_pci_driver
 = {
  	.dev_groups = amdgpu_sysfs_groups,
  };

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +4 -0
+static int amdgpu_wq_init(void)
+{
+	amdgpu_reclaim_wq =
+		alloc_workqueue("amdgpu-reclaim", WQ_MEM_RECLAIM,

0);

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +11 -0
+	if (!amdgpu_reclaim_wq)
+		return -ENOMEM;
+
+	return 0;
+}
+
+static void amdgpu_wq_fini(void)
+{
+	destroy_workqueue(amdgpu_reclaim_wq);
+}
+

 static int __init amdgpu_init(void)
 {
  int r;

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
@@ -2978,6 +2995,10 @@ static int __init amdgpu_init(void)
  	if (drm_firmware_drivers_only())
  		return -EINVAL;

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +4 -0
+	r = amdgpu_wq_init();
+	if (r)
+		goto error_wq;
+

  r = amdgpu_sync_init();
  if (r)
  goto error_sync;

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +0 -0
@@ -3006,6 +3027,9 @@ static int __init amdgpu_init(void)
  	amdgpu_sync_fini();


 error_sync:

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +3 -0
+	amdgpu_wq_fini();
+
+error_wq:

  return r;
 }

drivers/gpu/drm/amd/amdgpu/amdgpu_drv.c +1 -0
@@ -3017,6 +3041,7 @@ static void __exit amdgpu_exit(void)
  	amdgpu_acpi_release();
  	amdgpu_sync_fini();
  	amdgpu_fence_slab_fini();
+	amdgpu_wq_fini();
  	mmu_notifier_synchronize();
  	amdgpu_xcp_drv_release();
  }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -0
index 2f3f09dfb1fd..f8fd71d9382f 100644
@@ -790,8 +790,9 @@ void amdgpu_gfx_off_ctrl(struct amdgpu_device
 *adev, bool enable)

  AMD_IP_BLOCK_TYPE_GF X, true))
  adev->gfx.gfx_off_state = true;
  } else {

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0 -1
-				schedule_delayed_work(&adev-
Quoted 1 line

gfx.gfx_off_delay_work,

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +1 -1
-					      delay);
+				queue_delayed_work(amdgpu_reclaim_wq

,

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +1 -0
+						   &adev-
Quoted 1 line

gfx.gfx_off_delay_work,

drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +1 -0
+						   delay);

  }
  }
  } else {