[PATCH] fbdev: platinumfb: add error checking for ioremap calls

yuebingkun posted 1 patch 1 month, 1 week ago
There is a newer version of this series
drivers/video/fbdev/platinumfb.c | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
[PATCH] fbdev: platinumfb: add error checking for ioremap calls
Posted by yuebingkun 1 month, 1 week ago
The ioremap() and ioremap_wt() calls in platinumfb_probe() were not
checked for failure. If any of these mappings fail, the driver would
dereference NULL pointers, leading to a kernel panic.

Add proper error checking and cleanup for all three ioremap calls in
the probe function, ensuring that any resources already allocated are
properly released on failure.

Signed-off-by: yuebingkun <yuebingkun@kylinos.cn>
---
 drivers/video/fbdev/platinumfb.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/drivers/video/fbdev/platinumfb.c b/drivers/video/fbdev/platinumfb.c
index fa27a3a4f05b..e607d03a8adb 100644
--- a/drivers/video/fbdev/platinumfb.c
+++ b/drivers/video/fbdev/platinumfb.c
@@ -567,15 +567,40 @@ static int platinumfb_probe(struct platform_device* odev)
 	/* frame buffer - map only 4MB */
 	pinfo->frame_buffer_phys = pinfo->rsrc_fb.start;
 	pinfo->frame_buffer = ioremap_wt(pinfo->rsrc_fb.start, 0x400000);
+	if (!pinfo->frame_buffer) {
+		dev_err(&odev->dev, "failed to ioremap frame buffer\n");
+		release_mem_region(pinfo->rsrc_fb.start,
+				   resource_size(&pinfo->rsrc_fb));
+		framebuffer_release(info);
+		return -ENOMEM;
+	}
 	pinfo->base_frame_buffer = pinfo->frame_buffer;
 
 	/* registers */
 	pinfo->platinum_regs_phys = pinfo->rsrc_reg.start;
 	pinfo->platinum_regs = ioremap(pinfo->rsrc_reg.start, 0x1000);
+	if (!pinfo->platinum_regs) {
+		dev_err(&odev->dev, "failed to ioremap registers\n");
+		iounmap(pinfo->frame_buffer);
+		release_mem_region(pinfo->rsrc_fb.start,
+				   resource_size(&pinfo->rsrc_fb));
+		framebuffer_release(info);
+		return -ENOMEM;
+	}
 
 	pinfo->cmap_regs_phys = 0xf301b000;	/* XXX not in prom? */
 	request_mem_region(pinfo->cmap_regs_phys, 0x1000, "platinumfb cmap");
 	pinfo->cmap_regs = ioremap(pinfo->cmap_regs_phys, 0x1000);
+	if (!pinfo->cmap_regs) {
+		dev_err(&odev->dev, "failed to ioremap cmap registers\n");
+		iounmap(pinfo->platinum_regs);
+		iounmap(pinfo->frame_buffer);
+		release_mem_region(pinfo->cmap_regs_phys, 0x1000);
+		release_mem_region(pinfo->rsrc_fb.start,
+				   resource_size(&pinfo->rsrc_fb));
+		framebuffer_release(info);
+		return -ENOMEM;
+	}
 
 	/* Grok total video ram */
 	out_be32(&pinfo->platinum_regs->reg[16].r, (unsigned)pinfo->frame_buffer_phys);
-- 
2.43.0
Re: [PATCH] fbdev: platinumfb: add error checking for ioremap calls
Posted by Markus Elfring 1 month, 1 week ago
…
> Add proper error checking and cleanup for all three ioremap calls in
> the probe function, ensuring that any resources already allocated are
> properly released on failure.

* Please avoid duplicate source code also in the implementation of the function “platinumfb_probe”.
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/coding-style.rst?h=v7.2-rc7#n526

* How do you think about to increase the application of scope-based resource management?

* Would you like to add any tags (like “Fixes” and “Cc”) accordingly?
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v7.2-rc7#n145
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/stable-kernel-rules.rst?h=v7.2-rc7#n34


Regards,
Markus