Each cooling device has two owners: devres unregisters it on removal, and qmp_remove() and the registration error path also unregister it by hand. The cooling array is freed by hand in the registration error path and when the device tree has no cooling children, but qmp_remove() still reads it, and dereferences it when the allocation failed. Let devres own the cooling devices and their array. Queue qmp_teardown_channel() as a devres action after qmp_open(), so the channel is closed and freed after the cooling devices are released, the same order as before. With CONFIG_THERMAL_DEBUGFS=y (v6.8 and later), the second unregistration reads a cooling device that has already been freed. A failed registration during probe passes the ERR_PTR slot to thermal_cooling_device_unregister(), which can oops in the same configuration. If a later cooling device fails to register, the earlier ones now stay registered until removal, and probe still succeeds. The driver sets suppress_bind_attrs, so there is no sysfs bind or unbind file for AOSS itself. With fw_devlink on (the default, not permissive), the "mboxes" property makes AOSS a device-link consumer of its mailbox provider, qcom-apcs-ipc. Unbinding that provider through sysfs unbinds AOSS too, and the removal path runs. Unloading a CONFIG_QCOM_AOSS_QMP=m module runs it as well. Fixes: 05589b30b21a ("soc: qcom: Extend AOSS QMP driver to support resources that are used to wake up the SoC.") Fixes: a89f355e469d ("soc: qcom: aoss: Fix the out of bound usage of cooling_devs") Cc: stable@vger.kernel.org Signed-off-by: Anup Vishwakarma --- drivers/soc/qcom/qcom_aoss.c | 45 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/drivers/soc/qcom/qcom_aoss.c b/drivers/soc/qcom/qcom_aoss.c index 2d248c7bee0d..44b0c457e4ac 100644 --- a/drivers/soc/qcom/qcom_aoss.c +++ b/drivers/soc/qcom/qcom_aoss.c @@ -413,29 +413,10 @@ static int qmp_cooling_devices_register(struct qmp *qmp) ret = qmp_cooling_device_add(qmp, &qmp->cooling_devs[count++], child); if (ret) - goto unroll; + return ret; } - if (!count) - devm_kfree(qmp->dev, qmp->cooling_devs); - return 0; - -unroll: - while (--count >= 0) - thermal_cooling_device_unregister - (qmp->cooling_devs[count].cdev); - devm_kfree(qmp->dev, qmp->cooling_devs); - - return ret; -} - -static void qmp_cooling_devices_remove(struct qmp *qmp) -{ - int i; - - for (i = 0; i < QMP_NUM_COOLING_RESOURCES; i++) - thermal_cooling_device_unregister(qmp->cooling_devs[i].cdev); } /** @@ -572,6 +553,14 @@ static void qmp_debugfs_create(struct qmp *qmp) } } +static void qmp_teardown_channel(void *data) +{ + struct qmp *qmp = data; + + qmp_close(qmp); + mbox_free_channel(qmp->mbox_chan); +} + static int qmp_probe(struct platform_device *pdev) { struct qmp *qmp; @@ -608,9 +597,17 @@ static int qmp_probe(struct platform_device *pdev) if (ret < 0) goto err_free_mbox; + /* + * Registered before the cooling devices, so devres closes the channel + * after they are released, and also on any later probe failure. + */ + ret = devm_add_action_or_reset(&pdev->dev, qmp_teardown_channel, qmp); + if (ret) + return ret; + ret = qmp_qdss_clk_add(qmp); if (ret) - goto err_close_qmp; + return ret; ret = qmp_cooling_devices_register(qmp); if (ret) @@ -622,8 +619,6 @@ static int qmp_probe(struct platform_device *pdev) return 0; -err_close_qmp: - qmp_close(qmp); err_free_mbox: mbox_free_channel(qmp->mbox_chan); @@ -637,10 +632,6 @@ static void qmp_remove(struct platform_device *pdev) debugfs_remove_recursive(qmp->debugfs_root); qmp_qdss_clk_remove(qmp); - qmp_cooling_devices_remove(qmp); - - qmp_close(qmp); - mbox_free_channel(qmp->mbox_chan); } static const struct of_device_id qmp_dt_match[] = { -- 2.43.0