drm/amdgpu/gfx: fix ref counting for ring based profile handling
Subsystems (2)
Alex Deucher
maintainerPATCH v2 1/2 drm/amdgpu/gfx: fix ref counting for ring based profile handling
We need to make sure the workload profile ref counts are balanced. This isn't currently the case because we can increment the count on submissions, but the decrement may be delayed as work comes in. Track when we enable the workload profile so the references are balanced.
v2: switch to a mutex and active flag
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 30 ++++++++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 2 ++ 2 files changed, 22 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +20 -10
@@ -2160,11 +2160,16 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)for (i =0 ; i < (AMDGPU_MAX_COMPUTE_RINGS *AMDGPU_MAX_GC_INSTANCES ); ++i) fences +=amdgpu_fence_count_emitted (&adev->gfx.compute_ring[i]);if (!fences && !atomic_read (&adev->gfx.total_submission_cnt)) { - r =amdgpu_dpm_switch_power_profile (adev, profile, false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, false); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = false; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); }else {schedule_delayed_work (&adev->gfx.idle_work,GFX_PROFILE_IDLE_TIMEOUT ); }
@@ -2184,11 +2189,16 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)atomic_inc (&adev->gfx.total_submission_cnt);if (!cancel_delayed_work_sync (&adev->gfx.idle_work)) { - r =amdgpu_dpm_switch_power_profile (adev, profile, true); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (!adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, true); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = true; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); } }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +2 -0
@@ -483,6 +483,8 @@ struct amdgpu_gfx {atomic_t total_submission_cnt;struct delayed_work idle_work; +bool workload_profile_active; +struct mutex workload_profile_mutex; };struct amdgpu_gfx_ras_reg_entry {
Signature · 1 line
2.48.1
Alex Deucher
maintainerPATCH v2 2/2 drm/amdgpu/vcn: fix ref counting for ring based profile handling
We need to make sure the workload profile ref counts are balanced. This isn't currently the case because we can increment the count on submissions, but the decrement may be delayed as work comes in. Track when we enable the workload profile so the references are balanced.
v2: switch to a mutex and active flag
--- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 26 +++++++++++++++++-------- drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h | 3 +++ drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c | 26 +++++++++++++++++-------- 3 files changed, 39 insertions(+), 16 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c +18 -8
@@ -438,10 +438,15 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work)if (!fences && !atomic_read (&vcn_inst->total_submission_cnt)) { vcn_inst->set_pg_state (vcn_inst,AMD_PG_STATE_GATE ); - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); +mutex_lock (&adev->vcn.workload_profile_mutex); +if (adev->vcn.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , + false); +if (r) +dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); + adev->vcn.workload_profile_active = false; + } +mutex_unlock (&adev->vcn.workload_profile_mutex); }else {schedule_delayed_work (&vcn_inst->idle_work,VCN_IDLE_TIMEOUT ); }
@@ -456,10 +461,15 @@ void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)atomic_inc (&vcn_inst->total_submission_cnt);if (!cancel_delayed_work_sync (&vcn_inst->idle_work)) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); +mutex_lock (&adev->vcn.workload_profile_mutex); +if (!adev->vcn.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , + true); +if (r) +dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); + adev->vcn.workload_profile_active = true; + } +mutex_unlock (&adev->vcn.workload_profile_mutex); }mutex_lock (&vcn_inst->vcn_pg_lock);
drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.h +3 -0
@@ -358,6 +358,9 @@ struct amdgpu_vcn {bool per_inst_fw;unsigned fw_version; + +bool workload_profile_active; +struct mutex workload_profile_mutex; };struct amdgpu_fw_shared_rb_ptrs_struct {
drivers/gpu/drm/amd/amdgpu/vcn_v2_5.c +18 -8
@@ -147,10 +147,15 @@ static void vcn_v2_5_idle_work_handler(struct work_struct *work)if (!fences && !atomic_read (&adev->vcn.inst[0 ].total_submission_cnt)) {amdgpu_device_ip_set_powergating_state (adev,AMD_IP_BLOCK_TYPE_VCN ,AMD_PG_STATE_GATE ); - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); +mutex_lock (&adev->vcn.workload_profile_mutex); +if (adev->vcn.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , + false); +if (r) +dev_warn (adev->dev,"(%d) failed to disable video power profile mode\n" , r); + adev->vcn.workload_profile_active = false; + } +mutex_unlock (&adev->vcn.workload_profile_mutex); }else {schedule_delayed_work (&adev->vcn.inst[0 ].idle_work,VCN_IDLE_TIMEOUT ); }
@@ -165,10 +170,15 @@ static void vcn_v2_5_ring_begin_use(struct amdgpu_ring *ring)atomic_inc (&adev->vcn.inst[0 ].total_submission_cnt);if (!cancel_delayed_work_sync (&adev->vcn.inst[0 ].idle_work)) { - r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , - true); -if (r) -dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); +mutex_lock (&adev->vcn.workload_profile_mutex); +if (!adev->vcn.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev,PP_SMC_POWER_PROFILE_VIDEO , + true); +if (r) +dev_warn (adev->dev,"(%d) failed to switch to video power profile mode\n" , r); + adev->vcn.workload_profile_active = true; + } +mutex_unlock (&adev->vcn.workload_profile_mutex); }mutex_lock (&adev->vcn.inst[0 ].vcn_pg_lock);
Signature · 1 line
2.48.1
[AMD Official Use Only - AMD Internal Distribution Only]
Series is Reviewed-by: Kenneth Feng <[email protected]>
-----Original Message-----
From: Deucher, Alexander <[email protected]>
Sent: Wednesday, March 12, 2025 10:19 PM
To: [email protected]
Cc: Deucher, Alexander <[email protected]>; Wang, Yang(Kevin) <[email protected]>; Feng, Kenneth <[email protected]>
Subject: [PATCH V2 1/2] drm/amdgpu/gfx: fix ref counting for ring based profile handling
We need to make sure the workload profile ref counts are balanced. This isn't currently the case because we can increment the count on submissions, but the decrement may be delayed as work comes in. Track when we enable the workload profile so the references are balanced.
v2: switch to a mutex and active flag
--- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 30 ++++++++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 2 ++ 2 files changed, 22 insertions(+), 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +20 -10
@@ -2160,11 +2160,16 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)for (i =0 ; i < (AMDGPU_MAX_COMPUTE_RINGS *AMDGPU_MAX_GC_INSTANCES ); ++i) fences +=amdgpu_fence_count_emitted (&adev->gfx.compute_ring[i]);if (!fences && !atomic_read (&adev->gfx.total_submission_cnt)) { - r =amdgpu_dpm_switch_power_profile (adev, profile, false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, false); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = false; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); }else {schedule_delayed_work (&adev->gfx.idle_work,GFX_PROFILE_IDLE_TIMEOUT ); }
@@ -2184,11 +2189,16 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)atomic_inc (&adev->gfx.total_submission_cnt);if (!cancel_delayed_work_sync (&adev->gfx.idle_work)) { - r =amdgpu_dpm_switch_power_profile (adev, profile, true); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (!adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, true); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = true; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); } }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +2 -0
@@ -483,6 +483,8 @@ struct amdgpu_gfx {atomic_t total_submission_cnt;struct delayed_work idle_work; +bool workload_profile_active; +struct mutex workload_profile_mutex; };struct amdgpu_gfx_ras_reg_entry {
--
2.48.1
Quoted 44 lines · Alex Deucher
We need to make sure the workload profile ref counts are balanced. This isn't currently the case because we can increment the count on submissions, but the decrement may be delayed as work comes in. Track when we enable the workload profile so the references are balanced.
v2: switch to a mutex and active flag
Fixes 8fdb3958e396 ("drm/amdgpu/gfx: add ring helpers for setting workload profile")Cc Yang Wang <[email protected]>Cc Kenneth Feng <[email protected]>Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 30 ++++++++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 2 ++ 2 files changed, 22 insertions(+), 10 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10
-5@@ -2160,11 +2160,16 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)for (i =0 ; i < (AMDGPU_MAX_COMPUTE_RINGS *AMDGPU_MAX_GC_INSTANCES ); ++i) fences +=amdgpu_fence_count_emitted (&adev->gfx.compute_ring[i]);if (!fences && !atomic_read (&adev->gfx.total_submission_cnt)) { - r =amdgpu_dpm_switch_power_profile (adev, profile, false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, false); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = false; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); }else {schedule_delayed_work (&adev->gfx.idle_work,GFX_PROFILE_IDLE_TIMEOUT ); }
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring) · line 2191
Quoted 3 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0
-0@@ -2184,11 +2189,16 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)atomic_inc (&adev->gfx.total_submission_cnt);if (!cancel_delayed_work_sync (&adev->gfx.idle_work)) {
I guess this has the same problem as mentioned in the earlier patch. AFAIU, this will switch profile only if no idle work is scheduled. If a begin_use call comes when idle_work is being executed, there is a chance that idle_work completes amdgpu_dpm_switch_power_profile(adev, profile, false). Then this would skip changing the profile.
Thanks,
Lijo
Quoted 31 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10
-5- r =amdgpu_dpm_switch_power_profile (adev, profile, true); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (!adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, true); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = true; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); } }drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +2
-0@@ -483,6 +483,8 @@ struct amdgpu_gfx {atomic_t total_submission_cnt;struct delayed_work idle_work; +bool workload_profile_active; +struct mutex workload_profile_mutex; };struct amdgpu_gfx_ras_reg_entry {
Quoted 50 lines · Lijo
Quoted 46 lines · Alex Deucher
We need to make sure the workload profile ref counts are balanced. This isn't currently the case because we can increment the count on submissions, but the decrement may be delayed as work comes in. Track when we enable the workload profile so the references are balanced.
v2: switch to a mutex and active flag
Fixes 8fdb3958e396 ("drm/amdgpu/gfx: add ring helpers for setting workload profile")Cc Yang Wang <[email protected]>Cc Kenneth Feng <[email protected]>Signed-off-by Alex Deucher <[email protected]>--- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 30 ++++++++++++++++--------- drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h | 2 ++ 2 files changed, 22 insertions(+), 10 deletions(-)drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10
-5@@ -2160,11 +2160,16 @@ void amdgpu_gfx_profile_idle_work_handler(struct work_struct *work)for (i =0 ; i < (AMDGPU_MAX_COMPUTE_RINGS *AMDGPU_MAX_GC_INSTANCES ); ++i) fences +=amdgpu_fence_count_emitted (&adev->gfx.compute_ring[i]);if (!fences && !atomic_read (&adev->gfx.total_submission_cnt)) { - r =amdgpu_dpm_switch_power_profile (adev, profile, false); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, false); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = false; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); }else {schedule_delayed_work (&adev->gfx.idle_work,GFX_PROFILE_IDLE_TIMEOUT ); }@@ -2184,11 +2189,16 @@ void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring)atomic_inc (&adev->gfx.total_submission_cnt);
Quoted 7 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c void amdgpu_gfx_profile_ring_begin_use(struct amdgpu_ring *ring) · line 2191
Quoted 1 line
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +0
-0if (!cancel_delayed_work_sync (&adev->gfx.idle_work)) {I guess this has the same problem as mentioned in the earlier patch. AFAIU, this will switch profile only if no idle work is scheduled. If a
begin_usecall comes whenidle_workis being executed, there is a chance thatidle_workcompletesamdgpu_dpm_switch_power_profile(adev, profile, false). Then this would skip changing the profile.
I think that problem exists already independent of the ref counting. I suppose there isn't actually a need to make the workload change dependent on the cancelling of the delayed work. I'll send out some patches to address this.
Alex
Quoted 36 lines
Thanks,
LijoQuoted 31 lines
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +10
-5- r =amdgpu_dpm_switch_power_profile (adev, profile, true); -if (r) -dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, - profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? -"fullscreen 3D" :"compute" ); +mutex_lock (&adev->gfx.workload_profile_mutex); +if (!adev->gfx.workload_profile_active) { + r =amdgpu_dpm_switch_power_profile (adev, profile, true); +if (r) +dev_warn (adev->dev,"(%d) failed to disable %s power profile mode\n" , r, + profile ==PP_SMC_POWER_PROFILE_FULLSCREEN3D ? +"fullscreen 3D" :"compute" ); + adev->gfx.workload_profile_active = true; + } +mutex_unlock (&adev->gfx.workload_profile_mutex); } }drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.h +2
-0@@ -483,6 +483,8 @@ struct amdgpu_gfx {atomic_t total_submission_cnt;struct delayed_work idle_work; +bool workload_profile_active; +struct mutex workload_profile_mutex; };struct amdgpu_gfx_ras_reg_entry {