Skip to contentSkip to conversation

clocksource/drivers/timer-atmel-pit: Fix init failure cleanup

PATCH In review Yuho Choi 2 participants 3 messages Jun 8 – Jul 28

Subsystems (2)
lore
Share & tools
lore

Yuho Choi

author
#0 lore raw

PATCH 1/1 clocksource/drivers/timer-atmel-pit: Fix init failure cleanup

After clk_prepare_enable(data->mck) succeeds, at91sam926x_pit_dt_init() can still fail while parsing the IRQ, registering the clocksource, or requesting the IRQ. These paths only free the driver data, leaving the master clock enabled.

Unwind each initialized state on failure. Stop the PIT after it has been started, dispose the IRQ mapping after it has been created, disable and put the master clock, and unmap the registers before freeing the driver data.

Fixes 699e36e5b8e9 ("clocksource/drivers/timer-atmel-pit: Enable mck clock")
---
 drivers/clocksource/timer-atmel-pit.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/clocksource/timer-atmel-pit.c +16 -5
index 888b06731e54..edd427ab93e6 100644
@@ -12,7 +12,9 @@
 #include <linux/clk.h>
 #include <linux/clockchips.h>
 #include <linux/interrupt.h>
+#include <linux/io.h>
 #include <linux/irq.h>
+#include <linux/irqdomain.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
@@ -185,13 +187,13 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
 	if (IS_ERR(data->mck)) {
 		pr_err("Unable to get mck clk\n");
 		ret = PTR_ERR(data->mck);
-		goto exit;
+		goto exit_iounmap;
 	}
 
 	ret = clk_prepare_enable(data->mck);
 	if (ret) {
 		pr_err("Unable to enable mck\n");
-		goto exit;
+		goto exit_clk_put;
 	}
 
 	/* Get the interrupts property */
@@ -199,7 +201,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
 	if (!data->irq) {
 		pr_err("Unable to get IRQ from DT\n");
 		ret = -EINVAL;
-		goto exit;
+		goto exit_clk_disable;
 	}
 
 	/*
@@ -227,7 +229,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
 	ret = clocksource_register_hz(&data->clksrc, pit_rate);
 	if (ret) {
 		pr_err("Failed to register clocksource\n");
-		goto exit;
+		goto exit_pit_disable;
 	}
 
 	/* Set up irq handler */
@@ -237,7 +239,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
 	if (ret) {
 		pr_err("Unable to setup IRQ\n");
 		clocksource_unregister(&data->clksrc);
-		goto exit;
+		goto exit_pit_disable;
 	}
 
 	/* Set up and register clockevents */
@@ -256,6 +258,15 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
 
 	return 0;
 
+exit_pit_disable:
+	pit_write(data->base, AT91_PIT_MR, 0);
+	irq_dispose_mapping(data->irq);
+exit_clk_disable:
+	clk_disable_unprepare(data->mck);
+exit_clk_put:
+	clk_put(data->mck);
+exit_iounmap:
+	iounmap(data->base);
 exit:
 	kfree(data);
 	return ret;
Signature · 1 line
2.43.0
author
#1 lore raw

Hi,

Just a gentle ping on this patch.

I would appreciate any feedback when you have a chance to review this.

Thanks

Quoted 93 lines · Yuho Choi

After clk_prepare_enable(data->mck) succeeds, at91sam926x_pit_dt_init() can still fail while parsing the IRQ, registering the clocksource, or requesting the IRQ. These paths only free the driver data, leaving the master clock enabled.

Unwind each initialized state on failure. Stop the PIT after it has been started, dispose the IRQ mapping after it has been created, disable and put the master clock, and unmap the registers before freeing the driver data.

Fixes 699e36e5b8e9 ("clocksource/drivers/timer-atmel-pit: Enable mck clock")
---
 drivers/clocksource/timer-atmel-pit.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/clocksource/timer-atmel-pit.c +16 -6
index 888b06731e54..edd427ab93e6 100644
@@ -12,7 +12,9 @@
 #include <linux/clk.h>
 #include <linux/clockchips.h>
 #include <linux/interrupt.h>
+#include <linux/io.h>
 #include <linux/irq.h>
+#include <linux/irqdomain.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
@@ -185,13 +187,13 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        if (IS_ERR(data->mck)) {
                pr_err("Unable to get mck clk\n");
                ret = PTR_ERR(data->mck);
-               goto exit;
+               goto exit_iounmap;
        }
 
        ret = clk_prepare_enable(data->mck);
        if (ret) {
                pr_err("Unable to enable mck\n");
-               goto exit;
+               goto exit_clk_put;
        }
 
        /* Get the interrupts property */
@@ -199,7 +201,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        if (!data->irq) {
                pr_err("Unable to get IRQ from DT\n");
                ret = -EINVAL;
-               goto exit;
+               goto exit_clk_disable;
        }
 
        /*
@@ -227,7 +229,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        ret = clocksource_register_hz(&data->clksrc, pit_rate);
        if (ret) {
                pr_err("Failed to register clocksource\n");
-               goto exit;
+               goto exit_pit_disable;
        }
 
        /* Set up irq handler */
@@ -237,7 +239,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        if (ret) {
                pr_err("Unable to setup IRQ\n");
                clocksource_unregister(&data->clksrc);
-               goto exit;
+               goto exit_pit_disable;
        }
 
        /* Set up and register clockevents */
@@ -256,6 +258,15 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
 
        return 0;
 
+exit_pit_disable:
+       pit_write(data->base, AT91_PIT_MR, 0);
+       irq_dispose_mapping(data->irq);
+exit_clk_disable:
+       clk_disable_unprepare(data->mck);
+exit_clk_put:
+       clk_put(data->mck);
+exit_iounmap:
+       iounmap(data->base);
 exit:
        kfree(data);
        return ret;
--

2.43.0

#2 lore raw

Hello,

Quoted 20 lines · 최유호

Hi,

Just a gentle ping on this patch.

I would appreciate any feedback when you have a chance to review this.

Thanks

Quoted 11 lines · Yuho Choi

After clk_prepare_enable(data->mck) succeeds, at91sam926x_pit_dt_init() can still fail while parsing the IRQ, registering the clocksource, or requesting the IRQ. These paths only free the driver data, leaving the master clock enabled.

Unwind each initialized state on failure. Stop the PIT after it has been started, dispose the IRQ mapping after it has been created, disable and put the master clock, and unmap the registers before freeing the driver data.

This is a bit moot because if anything in the probe fails, then he platform will probably not boot at all.

Quoted 28 lines
Quoted 28 lines
Fixes 699e36e5b8e9 ("clocksource/drivers/timer-atmel-pit: Enable mck clock")
---
 drivers/clocksource/timer-atmel-pit.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
drivers/clocksource/timer-atmel-pit.c +3 -1
index 888b06731e54..edd427ab93e6 100644
@@ -12,7 +12,9 @@
 #include <linux/clk.h>
 #include <linux/clockchips.h>
 #include <linux/interrupt.h>
+#include <linux/io.h>
 #include <linux/irq.h>
+#include <linux/irqdomain.h>
 #include <linux/kernel.h>
 #include <linux/of.h>
 #include <linux/of_address.h>
@@ -185,13 +187,13 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        if (IS_ERR(data->mck)) {
                pr_err("Unable to get mck clk\n");
                ret = PTR_ERR(data->mck);
-               goto exit;
+               goto exit_iounmap;
        }
 
Quoted 6 lines

drivers/clocksource/timer-atmel-pit.c static int __init at91sam926x_pit_dt_init(struct device_node *node) · line 197

Quoted 6 lines
drivers/clocksource/timer-atmel-pit.c +1 -1
        ret = clk_prepare_enable(data->mck);
        if (ret) {
                pr_err("Unable to enable mck\n");
-               goto exit;
+               goto exit_clk_put;
        }

Switching to devm_clk_get_enabled would avoid most of the error handling. Similarly, devm_ioremap could be used earlier.

Quoted 28 lines
Quoted 28 lines
drivers/clocksource/timer-atmel-pit.c +3 -3
        /* Get the interrupts property */
@@ -199,7 +201,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        if (!data->irq) {
                pr_err("Unable to get IRQ from DT\n");
                ret = -EINVAL;
-               goto exit;
+               goto exit_clk_disable;
        }
 
        /*
@@ -227,7 +229,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        ret = clocksource_register_hz(&data->clksrc, pit_rate);
        if (ret) {
                pr_err("Failed to register clocksource\n");
-               goto exit;
+               goto exit_pit_disable;
        }
 
        /* Set up irq handler */
@@ -237,7 +239,7 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
        if (ret) {
                pr_err("Unable to setup IRQ\n");
                clocksource_unregister(&data->clksrc);
-               goto exit;
+               goto exit_pit_disable;
        }
 
Quoted 7 lines

drivers/clocksource/timer-atmel-pit.c static int __init at91sam926x_pit_dt_init(struct device_node *node) · line 262

Quoted 7 lines
drivers/clocksource/timer-atmel-pit.c +2 -0
        /* Set up and register clockevents */
@@ -256,6 +258,15 @@ static int __init at91sam926x_pit_dt_init(struct device_node *node)
 
        return 0;
 
+exit_pit_disable:
+       pit_write(data->base, AT91_PIT_MR, 0);

I doubt this write is useful.

Quoted 13 lines
Quoted 13 lines
drivers/clocksource/timer-atmel-pit.c +7 -1
+       irq_dispose_mapping(data->irq);
+exit_clk_disable:
+       clk_disable_unprepare(data->mck);
+exit_clk_put:
+       clk_put(data->mck);
+exit_iounmap:
+       iounmap(data->base);
 exit:
        kfree(data);
        return ret;
--

2.43.0

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com