Skip to contentSkip to conversation

drm/mediatek: Clean dangling pointer on bind error path

PATCH Applied Nícolas F. R. A. Prado 3 participants 5 messages Nov 22, 2022 – Jan 27, 2023

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

PATCH v2 1/1 drm/mediatek: Clean dangling pointer on bind error path

mtk_drm_bind() can fail, in which case drm_dev_put() is called, destroying the drm_device object. However a pointer to it was still being held in the private object, and that pointer would be passed along to DRM in mtk_drm_sys_prepare() if a suspend were triggered at that point, resulting in a panic. Clean the pointer when destroying the object in the error path to prevent this from happening.

Fixes 119f5173628a ("drm/mediatek: Add DRM Driver for Mediatek SoC MT8173.")

---

Changes in v2:
- Added Fixes tag

 drivers/gpu/drm/mediatek/mtk_drm_drv.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/mediatek/mtk_drm_drv.c +1 -0
index 39a42dc8fb85..a21ff1b3258c 100644
@@ -514,6 +514,7 @@ static int mtk_drm_bind(struct device *dev)
 err_deinit:
 	mtk_drm_kms_deinit(drm);
 err_free:
+	private->drm = NULL;
 	drm_dev_put(drm);
 	return ret;
 }
Signature · 1 line
2.38.1
#1 lore raw

Il 22/11/22 15:39, Nícolas F. R. A. Prado ha scritto:

Quoted 23 lines

mtk_drm_bind() can fail, in which case drm_dev_put() is called, destroying the drm_device object. However a pointer to it was still being held in the private object, and that pointer would be passed along to DRM in mtk_drm_sys_prepare() if a suspend were triggered at that point, resulting in a panic. Clean the pointer when destroying the object in the error path to prevent this from happening.

Fixes 119f5173628a ("drm/mediatek: Add DRM Driver for Mediatek SoC MT8173.")

---

Changes in v2:
- Added Fixes tag

  drivers/gpu/drm/mediatek/mtk_drm_drv.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/mediatek/mtk_drm_drv.c +0 -0
index 39a42dc8fb85..a21ff1b3258c 100644

drivers/gpu/drm/mediatek/mtk_drm_drv.c static int mtk_drm_bind(struct device *dev) · line 517

Quoted 4 lines
drivers/gpu/drm/mediatek/mtk_drm_drv.c +1 -0
@@ -514,6 +514,7 @@ static int mtk_drm_bind(struct device *dev)
  err_deinit:
  	mtk_drm_kms_deinit(drm);
  err_free:
+	private->drm = NULL;

Sorry for not noticing that in v1, but I've rechecked this function and, while this commit does indeed actually solve the described issue, I think it's incomplete.

A few lines before, we have a loop that sets

		private->all_drm_private[i]->drm = drm;

...so here you should do...

	private->drm = NULL;

	while (i--) /* a for loop will also do, your choice */
		private->all_drm_private[i]->drm = NULL;

That makes sure that you cleanup *everything* :-)

Cheers,
Angelo

#2 lore raw
Quoted 32 lines · AngeloGioacchino Del Regno

Il 22/11/22 15:39, Nícolas F. R. A. Prado ha scritto:

Quoted 23 lines

mtk_drm_bind() can fail, in which case drm_dev_put() is called, destroying the drm_device object. However a pointer to it was still being held in the private object, and that pointer would be passed along to DRM in mtk_drm_sys_prepare() if a suspend were triggered at that point, resulting in a panic. Clean the pointer when destroying the object in the error path to prevent this from happening.

Fixes 119f5173628a ("drm/mediatek: Add DRM Driver for Mediatek SoC MT8173.")

---

Changes in v2:
- Added Fixes tag

  drivers/gpu/drm/mediatek/mtk_drm_drv.c | 1 +
  1 file changed, 1 insertion(+)
drivers/gpu/drm/mediatek/mtk_drm_drv.c +0 -0
index 39a42dc8fb85..a21ff1b3258c 100644

drivers/gpu/drm/mediatek/mtk_drm_drv.c static int mtk_drm_bind(struct device *dev) · line 517

Quoted 4 lines
drivers/gpu/drm/mediatek/mtk_drm_drv.c +1 -0
@@ -514,6 +514,7 @@ static int mtk_drm_bind(struct device *dev)
  err_deinit:
  	mtk_drm_kms_deinit(drm);
  err_free:
+	private->drm = NULL;

Sorry for not noticing that in v1, but I've rechecked this function and, while this commit does indeed actually solve the described issue, I think it's incomplete.

Quoted 3 lines

A few lines before, we have a loop that sets

		private->all_drm_private[i]->drm = drm;

Actually that line only exists with [1] applied, but that commit hasn't been merged as of yet. I debated whether it would be better to send this fix as is, or ask Nancy Lin to add the tweaked fix you mention below to that series, but sending this fix as is seemed better since it can be backported to older kernel versions.

So assuming this commit gets merged first, Nancy should make that tweak you mentioned below to ensure all the pointers are zeroed out, which is why I've added Nancy to CC. (As a side note, given that only the mmsys with drm_master = true would ever call the drm suspend helper, even this patch as is is enough to avoid the panic even with that series applied, still we should zero out all pointers for good measure)

[1] https://lore.kernel.org/linux-mediatek/[email protected]/

Thanks,
Nícolas

Quoted 13 lines

...so here you should do...

	private->drm = NULL;

	while (i--) /* a for loop will also do, your choice */
		private->all_drm_private[i]->drm = NULL;

That makes sure that you cleanup *everything* :-)

Cheers,
Angelo

maintainer Reviewed-by
#3 lore raw

Il 25/11/22 17:34, Nícolas F. R. A. Prado ha scritto:

Quoted 43 lines · AngeloGioacchino Del Regno
Quoted 32 lines

Il 22/11/22 15:39, Nícolas F. R. A. Prado ha scritto:

Quoted 23 lines

mtk_drm_bind() can fail, in which case drm_dev_put() is called, destroying the drm_device object. However a pointer to it was still being held in the private object, and that pointer would be passed along to DRM in mtk_drm_sys_prepare() if a suspend were triggered at that point, resulting in a panic. Clean the pointer when destroying the object in the error path to prevent this from happening.

Fixes 119f5173628a ("drm/mediatek: Add DRM Driver for Mediatek SoC MT8173.")

---

Changes in v2:
- Added Fixes tag

   drivers/gpu/drm/mediatek/mtk_drm_drv.c | 1 +
   1 file changed, 1 insertion(+)
drivers/gpu/drm/mediatek/mtk_drm_drv.c +0 -0
index 39a42dc8fb85..a21ff1b3258c 100644

drivers/gpu/drm/mediatek/mtk_drm_drv.c static int mtk_drm_bind(struct device *dev) · line 517

Quoted 4 lines
drivers/gpu/drm/mediatek/mtk_drm_drv.c +1 -0
@@ -514,6 +514,7 @@ static int mtk_drm_bind(struct device *dev)
   err_deinit:
   	mtk_drm_kms_deinit(drm);
   err_free:
+	private->drm = NULL;

Sorry for not noticing that in v1, but I've rechecked this function and, while this commit does indeed actually solve the described issue, I think it's incomplete.

Quoted 3 lines

A few lines before, we have a loop that sets

		private->all_drm_private[i]->drm = drm;

Actually that line only exists with [1] applied, but that commit hasn't been merged as of yet. I debated whether it would be better to send this fix as is, or ask Nancy Lin to add the tweaked fix you mention below to that series, but sending this fix as is seemed better since it can be backported to older kernel versions.

Quoted 9 lines

So assuming this commit gets merged first, Nancy should make that tweak you mentioned below to ensure all the pointers are zeroed out, which is why I've added Nancy to CC. (As a side note, given that only the mmsys with drm_master = true would ever call the drm suspend helper, even this patch as is is enough to avoid the panic even with that series applied, still we should zero out all pointers for good measure)

[1] https://lore.kernel.org/linux-mediatek/[email protected]/

Sorry about that, you're right - the proposed fix is for the commit you linked, I got confused somehow.

This means that you get my

Cheers!

maintainer Applied
#4 lore raw
In reply to Nícolas F. R. A. Prado

Hi, Nicolas:

Nícolas F. R. A. Prado <[email protected]> 於 2022年11月22日 週二 下午10:39寫道:

Quoted 7 lines

mtk_drm_bind() can fail, in which case drm_dev_put() is called, destroying the drm_device object. However a pointer to it was still being held in the private object, and that pointer would be passed along to DRM in mtk_drm_sys_prepare() if a suspend were triggered at that point, resulting in a panic. Clean the pointer when destroying the object in the error path to prevent this from happening.

Applied to mediatek-drm-next [1], thanks.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/chunkuang.hu/linux.git/log/?h=mediatek-drm-next

Regards,
Chun-Kuang.

Quoted 27 lines
Fixes 119f5173628a ("drm/mediatek: Add DRM Driver for Mediatek SoC MT8173.")

---

Changes in v2:
- Added Fixes tag

 drivers/gpu/drm/mediatek/mtk_drm_drv.c | 1 +
 1 file changed, 1 insertion(+)
drivers/gpu/drm/mediatek/mtk_drm_drv.c +1 -1
index 39a42dc8fb85..a21ff1b3258c 100644
@@ -514,6 +514,7 @@ static int mtk_drm_bind(struct device *dev)
 err_deinit:
        mtk_drm_kms_deinit(drm);
 err_free:
+       private->drm = NULL;
        drm_dev_put(drm);
        return ret;
 }
--

2.38.1