accel/amdxdna: Fix dead lock for suspend and resume
Lizhi Hou
maintainerPATCH v2 1/1 accel/amdxdna: Fix dead lock for suspend and resume
When an application issues a query IOCTL while auto suspend is running, a deadlock can occur. The query path holds dev_lock and then calls pm_runtime_resume_and_get(), which waits for the ongoing suspend to complete. Meanwhile, the suspend callback attempts to acquire dev_lock and blocks, resulting in a deadlock.
Fix this by releasing dev_lock before calling pm_runtime_resume_and_get() and reacquiring it after the call completes. Also acquire dev_lock in the resume callback to keep the locking consistent.
--- drivers/accel/amdxdna/aie2_ctx.c | 4 ++-- drivers/accel/amdxdna/aie2_pci.c | 7 +++---- drivers/accel/amdxdna/aie2_pm.c | 2 +- drivers/accel/amdxdna/amdxdna_ctx.c | 19 +++++++------------ drivers/accel/amdxdna/amdxdna_pm.c | 2 ++ drivers/accel/amdxdna/amdxdna_pm.h | 11 +++++++++++ 6 files changed, 26 insertions(+), 19 deletions(-)
drivers/accel/amdxdna/aie2_ctx.c +2 -2
@@ -629,7 +629,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) goto free_entity; } - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto free_col_list;
@@ -760,7 +760,7 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 sizeif (!hwctx->cus)return -ENOMEM ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto free_cus;
drivers/accel/amdxdna/aie2_pci.c +3 -4
@@ -451,7 +451,6 @@ static int aie2_hw_suspend(struct amdxdna_dev *xdna) {struct amdxdna_client *client; -guard (mutex)(&xdna->dev_lock);list_for_each_entry (client, &xdna->client_list, node)aie2_hwctx_suspend (client);
@@ -951,7 +950,7 @@ static int aie2_get_info(struct amdxdna_client *client, struct amdxdna_drm_get_iif (!drm_dev_enter (&xdna->ddev, &idx))return -ENODEV ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto dev_exit;
@@ -1044,7 +1043,7 @@ static int aie2_get_array(struct amdxdna_client *client,if (!drm_dev_enter (&xdna->ddev, &idx))return -ENODEV ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto dev_exit;
@@ -1134,7 +1133,7 @@ static int aie2_set_state(struct amdxdna_client *client,if (!drm_dev_enter (&xdna->ddev, &idx))return -ENODEV ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto dev_exit;
drivers/accel/amdxdna/aie2_pm.c +1 -1
@@ -31,7 +31,7 @@ int aie2_pm_set_dpm(struct amdxdna_dev_hdl *ndev, u32 dpm_level) {int ret; - ret =amdxdna_pm_resume_get (ndev->xdna); + ret =amdxdna_pm_resume_get_locked (ndev->xdna);if (ret)return ret;
drivers/accel/amdxdna/amdxdna_ctx.c +7 -12
@@ -266,9 +266,9 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drstruct amdxdna_drm_config_hwctx *args = data;struct amdxdna_dev *xdna =to_xdna_dev (dev);struct amdxdna_hwctx *hwctx; -int ret, idx;u32 buf_size;void *buf; +int ret;u64 val;if (XDNA_MBZ_DBG (xdna, &args->pad,sizeof (args->pad)))
@@ -310,20 +310,17 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drreturn -EINVAL ; } -mutex_lock (&xdna->dev_lock); - idx =srcu_read_lock (&client->hwctx_srcu); +guard (mutex)(&xdna->dev_lock); hwctx =xa_load (&client->hwctx_xa, args->handle);if (!hwctx) {XDNA_DBG (xdna,"PID %d failed to get hwctx %d" , client->pid, args->handle); ret = -EINVAL ; - goto unlock_srcu; + goto free_buf; } ret = xdna->dev_info->ops->hwctx_config (hwctx, args->param_type, val, buf, buf_size); -unlock_srcu: -srcu_read_unlock (&client->hwctx_srcu, idx); -mutex_unlock (&xdna->dev_lock); +free_buf:kfree (buf);return ret; }
@@ -334,7 +331,7 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)struct amdxdna_hwctx *hwctx;struct amdxdna_gem_obj *abo;struct drm_gem_object *gobj; -int ret, idx; +int ret;if (!xdna->dev_info->ops->hwctx_sync_debug_bo)return -EOPNOTSUPP ;
@@ -345,17 +342,15 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl) abo =to_xdna_obj (gobj);guard (mutex)(&xdna->dev_lock); - idx =srcu_read_lock (&client->hwctx_srcu); hwctx =xa_load (&client->hwctx_xa, abo->assigned_hwctx);if (!hwctx) { ret = -EINVAL ; - goto unlock_srcu; + goto put_obj; } ret = xdna->dev_info->ops->hwctx_sync_debug_bo (hwctx, debug_bo_hdl); -unlock_srcu: -srcu_read_unlock (&client->hwctx_srcu, idx); +put_obj:drm_gem_object_put (gobj);return ret; }
drivers/accel/amdxdna/amdxdna_pm.c +2 -0
@@ -16,6 +16,7 @@ int amdxdna_pm_suspend(struct device *dev)struct amdxdna_dev *xdna =to_xdna_dev (dev_get_drvdata (dev));int ret = -EOPNOTSUPP ; +guard (mutex)(&xdna->dev_lock);if (xdna->dev_info->ops->suspend) ret = xdna->dev_info->ops->suspend (xdna);
@@ -28,6 +29,7 @@ int amdxdna_pm_resume(struct device *dev)struct amdxdna_dev *xdna =to_xdna_dev (dev_get_drvdata (dev));int ret = -EOPNOTSUPP ; +guard (mutex)(&xdna->dev_lock);if (xdna->dev_info->ops->resume) ret = xdna->dev_info->ops->resume (xdna);
drivers/accel/amdxdna/amdxdna_pm.h +11 -0
@@ -15,4 +15,15 @@ void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna);void amdxdna_pm_init (struct amdxdna_dev *xdna);void amdxdna_pm_fini (struct amdxdna_dev *xdna); +static inline int amdxdna_pm_resume_get_locked (struct amdxdna_dev *xdna) +{ +int ret; + +mutex_unlock (&xdna->dev_lock); + ret =amdxdna_pm_resume_get (xdna); +mutex_lock (&xdna->dev_lock); + +return ret; +} +#endif /* _AMDXDNA_PM_H_ */
Signature · 1 line
2.34.1
Quoted 190 lines · Lizhi Hou
When an application issues a query IOCTL while auto suspend is running, a deadlock can occur. The query path holds
dev_lockand then callspm_runtime_resume_and_get(), which waits for the ongoing suspend to complete. Meanwhile, the suspend callback attempts to acquiredev_lockand blocks, resulting in a deadlock.Fix this by releasing
dev_lockbefore callingpm_runtime_resume_and_get()and reacquiring it after the call completes. Also acquiredev_lockin the resume callback to keep the locking consistent.Fixes 063db451832b ("accel/amdxdna: Enhance runtime power management")Signed-off-by Lizhi Hou <[email protected]>--- drivers/accel/amdxdna/aie2_ctx.c | 4 ++-- drivers/accel/amdxdna/aie2_pci.c | 7 +++---- drivers/accel/amdxdna/aie2_pm.c | 2 +- drivers/accel/amdxdna/amdxdna_ctx.c | 19 +++++++------------ drivers/accel/amdxdna/amdxdna_pm.c | 2 ++ drivers/accel/amdxdna/amdxdna_pm.h | 11 +++++++++++ 6 files changed, 26 insertions(+), 19 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +2
-2@@ -629,7 +629,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) goto free_entity; } - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto free_col_list;@@ -760,7 +760,7 @@ static int aie2_hwctx_cu_config(struct amdxdna_hwctx *hwctx, void *buf, u32 sizeif (!hwctx->cus)return -ENOMEM ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto free_cus;drivers/accel/amdxdna/aie2_pci.c +3
-4@@ -451,7 +451,6 @@ static int aie2_hw_suspend(struct amdxdna_dev *xdna) {struct amdxdna_client *client; -guard (mutex)(&xdna->dev_lock);list_for_each_entry (client, &xdna->client_list, node)aie2_hwctx_suspend (client);@@ -951,7 +950,7 @@ static int aie2_get_info(struct amdxdna_client *client, struct amdxdna_drm_get_iif (!drm_dev_enter (&xdna->ddev, &idx))return -ENODEV ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto dev_exit;@@ -1044,7 +1043,7 @@ static int aie2_get_array(struct amdxdna_client *client,if (!drm_dev_enter (&xdna->ddev, &idx))return -ENODEV ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto dev_exit;@@ -1134,7 +1133,7 @@ static int aie2_set_state(struct amdxdna_client *client,if (!drm_dev_enter (&xdna->ddev, &idx))return -ENODEV ; - ret =amdxdna_pm_resume_get (xdna); + ret =amdxdna_pm_resume_get_locked (xdna);if (ret) goto dev_exit;drivers/accel/amdxdna/aie2_pm.c +1
-1@@ -31,7 +31,7 @@ int aie2_pm_set_dpm(struct amdxdna_dev_hdl *ndev, u32 dpm_level) {int ret; - ret =amdxdna_pm_resume_get (ndev->xdna); + ret =amdxdna_pm_resume_get_locked (ndev->xdna);if (ret)return ret;drivers/accel/amdxdna/amdxdna_ctx.c +7
-12@@ -266,9 +266,9 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drstruct amdxdna_drm_config_hwctx *args = data;struct amdxdna_dev *xdna =to_xdna_dev (dev);struct amdxdna_hwctx *hwctx; -int ret, idx;u32 buf_size;void *buf; +int ret;u64 val;if (XDNA_MBZ_DBG (xdna, &args->pad,sizeof (args->pad)))@@ -310,20 +310,17 @@ int amdxdna_drm_config_hwctx_ioctl(struct drm_device *dev, void *data, struct drreturn -EINVAL ; } -mutex_lock (&xdna->dev_lock); - idx =srcu_read_lock (&client->hwctx_srcu); +guard (mutex)(&xdna->dev_lock); hwctx =xa_load (&client->hwctx_xa, args->handle);if (!hwctx) {XDNA_DBG (xdna,"PID %d failed to get hwctx %d" , client->pid, args->handle); ret = -EINVAL ; - goto unlock_srcu; + goto free_buf; } ret = xdna->dev_info->ops->hwctx_config (hwctx, args->param_type, val, buf, buf_size); -unlock_srcu: -srcu_read_unlock (&client->hwctx_srcu, idx); -mutex_unlock (&xdna->dev_lock); +free_buf:kfree (buf);return ret; }@@ -334,7 +331,7 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl)struct amdxdna_hwctx *hwctx;struct amdxdna_gem_obj *abo;struct drm_gem_object *gobj; -int ret, idx; +int ret;if (!xdna->dev_info->ops->hwctx_sync_debug_bo)return -EOPNOTSUPP ;@@ -345,17 +342,15 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl) abo =to_xdna_obj (gobj);guard (mutex)(&xdna->dev_lock); - idx =srcu_read_lock (&client->hwctx_srcu); hwctx =xa_load (&client->hwctx_xa, abo->assigned_hwctx);if (!hwctx) { ret = -EINVAL ; - goto unlock_srcu; + goto put_obj; } ret = xdna->dev_info->ops->hwctx_sync_debug_bo (hwctx, debug_bo_hdl); -unlock_srcu: -srcu_read_unlock (&client->hwctx_srcu, idx); +put_obj:drm_gem_object_put (gobj);return ret; }drivers/accel/amdxdna/amdxdna_pm.c +2
-0@@ -16,6 +16,7 @@ int amdxdna_pm_suspend(struct device *dev)struct amdxdna_dev *xdna =to_xdna_dev (dev_get_drvdata (dev));int ret = -EOPNOTSUPP ; +guard (mutex)(&xdna->dev_lock);if (xdna->dev_info->ops->suspend) ret = xdna->dev_info->ops->suspend (xdna);@@ -28,6 +29,7 @@ int amdxdna_pm_resume(struct device *dev)struct amdxdna_dev *xdna =to_xdna_dev (dev_get_drvdata (dev));int ret = -EOPNOTSUPP ; +guard (mutex)(&xdna->dev_lock);if (xdna->dev_info->ops->resume) ret = xdna->dev_info->ops->resume (xdna);drivers/accel/amdxdna/amdxdna_pm.h +0
-0@@ -15,4 +15,15 @@ void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna);void amdxdna_pm_init (struct amdxdna_dev *xdna);void amdxdna_pm_fini (struct amdxdna_dev *xdna);
drivers/accel/amdxdna/amdxdna_pm.h void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna); · line 26
Quoted 10 lines
drivers/accel/amdxdna/amdxdna_pm.h +9
-0+static inline int amdxdna_pm_resume_get_locked (struct amdxdna_dev *xdna) +{ +int ret; + +mutex_unlock (&xdna->dev_lock); + ret =amdxdna_pm_resume_get (xdna); +mutex_lock (&xdna->dev_lock); + +return ret;
Like the other code you added in this patch, couldn't this be simplified with a guard(mutex)?
Quoted 3 lines
drivers/accel/amdxdna/amdxdna_pm.h +2
-0+} +#endif /* _AMDXDNA_PM_H_ */
Lizhi Hou
maintainerQuoted 174 lines · Mario Limonciello
Quoted 173 lines · Lizhi Hou
When an application issues a query IOCTL while auto suspend is running, a deadlock can occur. The query path holds
dev_lockand then callspm_runtime_resume_and_get(), which waits for the ongoing suspend to complete. Meanwhile, the suspend callback attempts to acquiredev_lockand blocks, resulting in a deadlock.Fix this by releasing
dev_lockbefore callingpm_runtime_resume_and_get()and reacquiring it after the call completes. Also acquiredev_lockin the resume callback to keep the locking consistent.Fixes 063db451832b ("accel/amdxdna: Enhance runtime power management")Signed-off-by Lizhi Hou <[email protected]>---
drivers/accel/amdxdna/aie2_ctx.c | 4 ++-- drivers/accel/amdxdna/aie2_pci.c | 7 +++---- drivers/accel/amdxdna/aie2_pm.c | 2 +-drivers/accel/amdxdna/amdxdna_ctx.c| 19 +++++++------------ drivers/accel/amdxdna/amdxdna_pm.c | 2 ++ drivers/accel/amdxdna/amdxdna_pm.h | 11 +++++++++++6 files changed, 26 insertions(+), 19 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +1
-0@@ -629,7 +629,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) goto free_entity; } - ret = amdxdna_pm_resume_get(xdna); + ret = amdxdna_pm_resume_get_locked(xdna); if (ret) goto free_col_list;@@ -760,7 +760,7 @@ static int aie2_hwctx_cu_config(struct
amdxdna_hwctx*hwctx, void *buf, u32 size if (!hwctx->cus)
return-ENOMEM;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotofree_cus;
diff --gita/drivers/accel/amdxdna/aie2_pci.cb/drivers/accel/amdxdna/aie2_pci.cindexf70ccf0f3c01..5b326e4610e6100644drivers/accel/amdxdna/aie2_pci.c +0
-0@@ -451,7 +451,6 @@ static int aie2_hw_suspend(struct amdxdna_dev *xdna) { struct amdxdna_client *client; - guard(mutex)(&xdna->dev_lock); list_for_each_entry(client, &xdna->client_list, node) aie2_hwctx_suspend(client);@@ -951,7 +950,7 @@ static int aie2_get_info(struct
amdxdna_client*client, structamdxdna_drm_get_iif (!drm_dev_enter(&xdna->ddev, &idx))
return-ENODEV;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotodev_exit;
@@ -1044,7 +1043,7 @@ static int aie2_get_array(structamdxdna_client*client,
if (!drm_dev_enter(&xdna->ddev, &idx))
return-ENODEV;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotodev_exit;
@@ -1134,7 +1133,7 @@ static int aie2_set_state(structamdxdna_client*client,
if (!drm_dev_enter(&xdna->ddev, &idx))
return-ENODEV;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotodev_exit;
diff --gita/drivers/accel/amdxdna/aie2_pm.cb/drivers/accel/amdxdna/aie2_pm.cindex579b8be13b18..29bd4403a94d100644drivers/accel/amdxdna/aie2_pm.c +1
-0@@ -31,7 +31,7 @@ int aie2_pm_set_dpm(struct amdxdna_dev_hdl *ndev, u32 dpm_level) { int ret; - ret = amdxdna_pm_resume_get(ndev->xdna); + ret = amdxdna_pm_resume_get_locked(ndev->xdna); if (ret) return ret;diff --git
a/drivers/accel/amdxdna/amdxdna_ctx.cb/drivers/accel/amdxdna/amdxdna_ctx.cindexd17aef89a0ad..db3aa26fb55f100644drivers/accel/amdxdna/amdxdna_ctx.c +0
-0@@ -266,9 +266,9 @@ int amdxdna_drm_config_hwctx_ioctl(struct
drm_device*dev, void *data, struct dr
structamdxdna_drm_config_hwctx*args = data; structamdxdna_dev*xdna =to_xdna_dev(dev); structamdxdna_hwctx*hwctx;
- int ret, idx;
u32buf_size;
void *buf;
+ int ret;
u64 val;
if (XDNA_MBZ_DBG(xdna, &args->pad, sizeof(args->pad)))drivers/accel/amdxdna/amdxdna_ctx.c +0
-0@@ -310,20 +310,17 @@ int amdxdna_drm_config_hwctx_ioctl(struct
drm_device*dev, void *data, struct dr
return-EINVAL;
}
-mutex_lock(&xdna->dev_lock);
- idx =srcu_read_lock(&client->hwctx_srcu);
+ guard(mutex)(&xdna->dev_lock);
hwctx =xa_load(&client->hwctx_xa, args->handle);
if (!hwctx) {
XDNA_DBG(xdna, "PID %d failed to get hwctx %d", client->pid, args->handle); ret =-EINVAL;
- gotounlock_srcu;
+ gotofree_buf;
}
ret =xdna->dev_info->ops->hwctx_config(hwctx, args->param_type, val, buf, buf_size); -unlock_srcu:
-srcu_read_unlock(&client->hwctx_srcu, idx);
-mutex_unlock(&xdna->dev_lock);
+free_buf:
kfree(buf);
return ret;
}drivers/accel/amdxdna/amdxdna_ctx.c +2
-3@@ -334,7 +331,7 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl) struct amdxdna_hwctx *hwctx; struct amdxdna_gem_obj *abo; struct drm_gem_object *gobj; - int ret, idx; + int ret; if (!xdna->dev_info->ops->hwctx_sync_debug_bo) return -EOPNOTSUPP;@@ -345,17 +342,15 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl) abo = to_xdna_obj(gobj); guard(mutex)(&xdna->dev_lock); - idx = srcu_read_lock(&client->hwctx_srcu); hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx); if (!hwctx) { ret = -EINVAL; - goto unlock_srcu; + goto put_obj; } ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, debug_bo_hdl);-unlock_srcu:
-srcu_read_unlock(&client->hwctx_srcu, idx);
+put_obj:
drm_gem_object_put(gobj);
return ret;
}drivers/accel/amdxdna/amdxdna_pm.c +0
-0@@ -16,6 +16,7 @@ int amdxdna_pm_suspend(struct device *dev) struct amdxdna_dev *xdna = to_xdna_dev(dev_get_drvdata(dev)); int ret = -EOPNOTSUPP; + guard(mutex)(&xdna->dev_lock); if (xdna->dev_info->ops->suspend) ret = xdna->dev_info->ops->suspend(xdna); @@ -28,6 +29,7 @@ int amdxdna_pm_resume(struct device *dev)struct
amdxdna_dev*xdna = to_xdna_dev(dev_get_drvdata(dev));
int ret =-EOPNOTSUPP;
+ guard(mutex)(&xdna->dev_lock);
if (xdna->dev_info->ops->resume)
ret =xdna->dev_info->ops->resume(xdna);
diff --gita/drivers/accel/amdxdna/amdxdna_pm.hb/drivers/accel/amdxdna/amdxdna_pm.hindex77b2d6e45570..3d26b973e0e3100644drivers/accel/amdxdna/amdxdna_pm.h +2
-0@@ -15,4 +15,15 @@ void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna); void amdxdna_pm_init(struct amdxdna_dev *xdna); void amdxdna_pm_fini(struct amdxdna_dev *xdna); +static inline int amdxdna_pm_resume_get_locked(struct amdxdna_dev *xdna) +{ + int ret;
Quoted 8 lines
drivers/accel/amdxdna/amdxdna_pm.h void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna); · line 26
Quoted 6 lines
drivers/accel/amdxdna/amdxdna_pm.h +6
-0+ + mutex_unlock(&xdna->dev_lock); + ret = amdxdna_pm_resume_get(xdna); + mutex_lock(&xdna->dev_lock); + + return ret;Like the other code you added in this patch, couldn't this be simplified with a guard(mutex)?
This is unlock before resume_get, then lock after. guard(mutex) might not help here.
Lizhi
Quoted 5 lines
Quoted 4 lines
drivers/accel/amdxdna/amdxdna_pm.h +2
-0+} +#endif /* _AMDXDNA_PM_H_ */
Mario Limonciello
Reviewed-by Mario Limonciello (AMD)Quoted 187 lines · Lizhi Hou
Quoted 176 lines · Mario Limonciello
Quoted 175 lines · Lizhi Hou
When an application issues a query IOCTL while auto suspend is running, a deadlock can occur. The query path holds
dev_lockand then callspm_runtime_resume_and_get(), which waits for the ongoing suspend to complete. Meanwhile, the suspend callback attempts to acquiredev_lockand blocks, resulting in a deadlock.Fix this by releasing
dev_lockbefore callingpm_runtime_resume_and_get()and reacquiring it after the call completes. Also acquiredev_lockin the resume callback to keep the locking consistent.Fixes 063db451832b ("accel/amdxdna: Enhance runtime power management")Signed-off-by Lizhi Hou <[email protected]>---
drivers/accel/amdxdna/aie2_ctx.c | 4 ++-- drivers/accel/amdxdna/aie2_pci.c | 7 +++---- drivers/accel/amdxdna/aie2_pm.c | 2 +-drivers/accel/amdxdna/amdxdna_ctx.c| 19 +++++++------------ drivers/accel/amdxdna/amdxdna_pm.c | 2 ++ drivers/accel/amdxdna/amdxdna_pm.h | 11 +++++++++++6 files changed, 26 insertions(+), 19 deletions(-)drivers/accel/amdxdna/aie2_ctx.c +1
-0@@ -629,7 +629,7 @@ int aie2_hwctx_init(struct amdxdna_hwctx *hwctx) goto free_entity; } - ret = amdxdna_pm_resume_get(xdna); + ret = amdxdna_pm_resume_get_locked(xdna); if (ret) goto free_col_list;@@ -760,7 +760,7 @@ static int aie2_hwctx_cu_config(struct
amdxdna_hwctx*hwctx, void *buf, u32 size if (!hwctx->cus)
return-ENOMEM;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotofree_cus;
diff --gita/drivers/accel/amdxdna/aie2_pci.cb/drivers/accel/amdxdna/aie2_pci.cindexf70ccf0f3c01..5b326e4610e6100644drivers/accel/amdxdna/aie2_pci.c +0
-0@@ -451,7 +451,6 @@ static int aie2_hw_suspend(struct amdxdna_dev *xdna) { struct amdxdna_client *client; - guard(mutex)(&xdna->dev_lock); list_for_each_entry(client, &xdna->client_list, node) aie2_hwctx_suspend(client);@@ -951,7 +950,7 @@ static int aie2_get_info(struct
amdxdna_client*client, structamdxdna_drm_get_iif (!drm_dev_enter(&xdna->ddev, &idx))
return-ENODEV;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotodev_exit;
@@ -1044,7 +1043,7 @@ static int aie2_get_array(structamdxdna_client*client,
if (!drm_dev_enter(&xdna->ddev, &idx))
return-ENODEV;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotodev_exit;
@@ -1134,7 +1133,7 @@ static int aie2_set_state(structamdxdna_client*client,
if (!drm_dev_enter(&xdna->ddev, &idx))
return-ENODEV;
- ret =amdxdna_pm_resume_get(xdna);
+ ret =amdxdna_pm_resume_get_locked(xdna);
if (ret)
gotodev_exit;
diff --gita/drivers/accel/amdxdna/aie2_pm.cb/drivers/accel/amdxdna/aie2_pm.cindex579b8be13b18..29bd4403a94d100644drivers/accel/amdxdna/aie2_pm.c +1
-0@@ -31,7 +31,7 @@ int aie2_pm_set_dpm(struct amdxdna_dev_hdl *ndev, u32 dpm_level) { int ret; - ret = amdxdna_pm_resume_get(ndev->xdna); + ret = amdxdna_pm_resume_get_locked(ndev->xdna); if (ret) return ret;diff --git
a/drivers/accel/amdxdna/amdxdna_ctx.cb/drivers/accel/amdxdna/amdxdna_ctx.cindexd17aef89a0ad..db3aa26fb55f100644drivers/accel/amdxdna/amdxdna_ctx.c +0
-0@@ -266,9 +266,9 @@ int amdxdna_drm_config_hwctx_ioctl(struct
drm_device*dev, void *data, struct dr
structamdxdna_drm_config_hwctx*args = data; structamdxdna_dev*xdna =to_xdna_dev(dev); structamdxdna_hwctx*hwctx;
- int ret, idx;
u32buf_size;
void *buf;
+ int ret;
u64 val;
if (XDNA_MBZ_DBG(xdna, &args->pad, sizeof(args->pad)))drivers/accel/amdxdna/amdxdna_ctx.c +0
-0@@ -310,20 +310,17 @@ int amdxdna_drm_config_hwctx_ioctl(struct
drm_device*dev, void *data, struct dr
return-EINVAL;
}
-mutex_lock(&xdna->dev_lock);
- idx =srcu_read_lock(&client->hwctx_srcu);
+ guard(mutex)(&xdna->dev_lock);
hwctx =xa_load(&client->hwctx_xa, args->handle); if (!hwctx) {
XDNA_DBG(xdna, "PID %d failed to get hwctx %d", client-Quoted 1 line
pid,
args->handle);ret =
-EINVAL;
- gotounlock_srcu;
+ gotofree_buf;
}
ret = xdna->dev_info->ops->hwctx_config(hwctx, args-Quoted 1 line
param_type, val, buf,buf_size);-unlock_srcu:
-srcu_read_unlock(&client->hwctx_srcu, idx);
-mutex_unlock(&xdna->dev_lock);
+free_buf:
kfree(buf);
return ret;
}drivers/accel/amdxdna/amdxdna_ctx.c +2
-3@@ -334,7 +331,7 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl) struct amdxdna_hwctx *hwctx; struct amdxdna_gem_obj *abo; struct drm_gem_object *gobj; - int ret, idx; + int ret; if (!xdna->dev_info->ops->hwctx_sync_debug_bo) return -EOPNOTSUPP;@@ -345,17 +342,15 @@ int amdxdna_hwctx_sync_debug_bo(struct amdxdna_client *client, u32 debug_bo_hdl) abo = to_xdna_obj(gobj); guard(mutex)(&xdna->dev_lock); - idx = srcu_read_lock(&client->hwctx_srcu); hwctx = xa_load(&client->hwctx_xa, abo->assigned_hwctx); if (!hwctx) { ret = -EINVAL; - goto unlock_srcu; + goto put_obj; } ret = xdna->dev_info->ops->hwctx_sync_debug_bo(hwctx, debug_bo_hdl);-unlock_srcu:
-srcu_read_unlock(&client->hwctx_srcu, idx);
+put_obj:
drm_gem_object_put(gobj);
return ret;
}drivers/accel/amdxdna/amdxdna_pm.c +0
-0@@ -16,6 +16,7 @@ int amdxdna_pm_suspend(struct device *dev) struct amdxdna_dev *xdna = to_xdna_dev(dev_get_drvdata(dev)); int ret = -EOPNOTSUPP; + guard(mutex)(&xdna->dev_lock); if (xdna->dev_info->ops->suspend) ret = xdna->dev_info->ops->suspend(xdna); @@ -28,6 +29,7 @@ int amdxdna_pm_resume(struct device *dev)struct
amdxdna_dev*xdna = to_xdna_dev(dev_get_drvdata(dev));
int ret =-EOPNOTSUPP;
+ guard(mutex)(&xdna->dev_lock);
if (xdna->dev_info->ops->resume)
ret =xdna->dev_info->ops->resume(xdna);
diff --gita/drivers/accel/amdxdna/amdxdna_pm.hb/drivers/accel/amdxdna/amdxdna_pm.hindex77b2d6e45570..3d26b973e0e3100644drivers/accel/amdxdna/amdxdna_pm.h +2
-0@@ -15,4 +15,15 @@ void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna); void amdxdna_pm_init(struct amdxdna_dev *xdna); void amdxdna_pm_fini(struct amdxdna_dev *xdna); +static inline int amdxdna_pm_resume_get_locked(struct amdxdna_dev *xdna) +{ + int ret;Quoted 8 lines
drivers/accel/amdxdna/amdxdna_pm.h void amdxdna_pm_suspend_put(struct amdxdna_dev *xdna); · line 26
Quoted 6 lines
drivers/accel/amdxdna/amdxdna_pm.h +6
-0+ + mutex_unlock(&xdna->dev_lock); + ret = amdxdna_pm_resume_get(xdna); + mutex_lock(&xdna->dev_lock); + + return ret;Like the other code you added in this patch, couldn't this be simplified with a guard(mutex)?
Quoted 2 lines
This is unlock before
resume_get, then lock after. guard(mutex) might not help here.
Oh right; it's reversed. Agreed!
Quoted 7 lines
Lizhi
Quoted 5 lines
Quoted 4 lines
drivers/accel/amdxdna/amdxdna_pm.h +2
-0+} +#endif /* _AMDXDNA_PM_H_ */