Commit 1f403699 authored by Ma Ke's avatar Ma Ke Committed by Chun-Kuang Hu
Browse files

drm/mediatek: Fix device/node reference count leaks in mtk_drm_get_all_drm_priv



Using device_find_child() and of_find_device_by_node() to locate
devices could cause an imbalance in the device's reference count.
device_find_child() and of_find_device_by_node() both call
get_device() to increment the reference count of the found device
before returning the pointer. In mtk_drm_get_all_drm_priv(), these
references are never released through put_device(), resulting in
permanent reference count increments. Additionally, the
for_each_child_of_node() iterator fails to release node references in
all code paths. This leaks device node references when loop
termination occurs before reaching MAX_CRTC. These reference count
leaks may prevent device/node resources from being properly released
during driver unbind operations.

As comment of device_find_child() says, 'NOTE: you will need to drop
the reference with put_device() after use'.

Cc: stable@vger.kernel.org
Fixes: 1ef7ed48 ("drm/mediatek: Modify mediatek-drm for mt8195 multi mmsys support")
Signed-off-by: default avatarMa Ke <make24@iscas.ac.cn>
Reviewed-by: default avatarCK Hu <ck.hu@mediatek.com>
Link: https://patchwork.kernel.org/project/dri-devel/patch/20250812071932.471730-1-make24@iscas.ac.cn/


Signed-off-by: default avatarChun-Kuang Hu <chunkuang.hu@kernel.org>
parent f5b18191
Loading
Loading
Loading
Loading
+14 −7
Original line number Diff line number Diff line
@@ -387,19 +387,19 @@ static bool mtk_drm_get_all_drm_priv(struct device *dev)

		of_id = of_match_node(mtk_drm_of_ids, node);
		if (!of_id)
			continue;
			goto next_put_node;

		pdev = of_find_device_by_node(node);
		if (!pdev)
			continue;
			goto next_put_node;

		drm_dev = device_find_child(&pdev->dev, NULL, mtk_drm_match);
		if (!drm_dev)
			continue;
			goto next_put_device_pdev_dev;

		temp_drm_priv = dev_get_drvdata(drm_dev);
		if (!temp_drm_priv)
			continue;
			goto next_put_device_drm_dev;

		if (temp_drm_priv->data->main_len)
			all_drm_priv[CRTC_MAIN] = temp_drm_priv;
@@ -411,11 +411,18 @@ static bool mtk_drm_get_all_drm_priv(struct device *dev)
		if (temp_drm_priv->mtk_drm_bound)
			cnt++;

		if (cnt == MAX_CRTC) {
next_put_device_drm_dev:
		put_device(drm_dev);

next_put_device_pdev_dev:
		put_device(&pdev->dev);

next_put_node:
		of_node_put(node);

		if (cnt == MAX_CRTC)
			break;
	}
	}

	if (drm_priv->data->mmsys_dev_num == cnt) {
		for (i = 0; i < cnt; i++)