Skip to contentSkip to conversation

clocksource: clps711x: Fix resource leaks in error paths

PATCH Superseded Zhen Ni 3 participants 8 messages Jul 31, 2025 – Sep 19, 2025 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Zhen Ni

author
#0 lore raw

PATCH 1/1 clocksource: clps711x: Fix resource leaks in error paths

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped via of_iomap(). This leads to resource leaks when:
1. irq_of_parse_and_map() fails
2. of_clk_get() returns an error
3. _clps711x_clkevt_init() fails for CLOCKEVENT alias
4. An unrecognized timer alias is provided

---
 drivers/clocksource/clps711x-timer.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/clocksource/clps711x-timer.c +17 -7
index e95fdc49c226..f803d694c7a4 100644
@@ -78,24 +78,34 @@ static int __init clps711x_timer_init(struct device_node *np)
 	unsigned int irq = irq_of_parse_and_map(np, 0);
 	struct clk *clock = of_clk_get(np, 0);
 	void __iomem *base = of_iomap(np, 0);
+	int ret = 0;
 
 	if (!base)
 		return -ENOMEM;
-	if (!irq)
-		return -EINVAL;
-	if (IS_ERR(clock))
-		return PTR_ERR(clock);
+	if (!irq) {
+		ret = -EINVAL;
+		goto out;
+	}
+	if (IS_ERR(clock)) {
+		ret = PTR_ERR(clock);
+		goto out;
+	}
 
 	switch (of_alias_get_id(np, "timer")) {
 	case CLPS711X_CLKSRC_CLOCKSOURCE:
 		clps711x_clksrc_init(clock, base);
 		break;
 	case CLPS711X_CLKSRC_CLOCKEVENT:
-		return _clps711x_clkevt_init(clock, base, irq);
+		ret =  _clps711x_clkevt_init(clock, base, irq);
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
+		break;
 	}
 
-	return 0;
+out:
+	if (base)
+		iounmap(base);
+	return ret;
 }
 TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init);
Signature · 1 line
2.20.1
#1 lore raw
Quoted 2 lines

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped

How do you think about 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=v6.16#n145

Quoted 1 line

via of_iomap(). This leads to resource leaks when:

Please reconsider the need for the presented enumeration once more.

See also:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.16#n94

…

Quoted 2 lines

+++ b/drivers/clocksource/clps711x-timer.c

(quoted diff) +0 -0
@@ -78,24 +78,34 @@ static int __init clps711x_timer_init(struct device_node *np)

…

drivers/clocksource/clps711x-timer.c static int __init clps711x_timer_init(struct device_node *np) · line 107

Quoted 3 lines
drivers/clocksource/clps711x-timer.c +2 -1
-	return 0;
+out:
+	if (base)

* I suggest to omit such a pointer check because it was performed already directly after the of_iomap() call.

* Would it be nicer to use the label “unmap_io”?

drivers/clocksource/clps711x-timer.c static int __init clps711x_timer_init(struct device_node *np) · line 111

Quoted 4 lines
drivers/clocksource/clps711x-timer.c +2 -0
+		iounmap(base);
+	return ret;
 }
 TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init);

…

Regards,
Markus

author
#2 lore raw

PATCH v2 1/1 clocksource: clps711x: Fix resource leaks in error paths

In reply to Zhen Ni

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped via of_iomap(). Fix of_iomap leaks in err paths.

Fixes 04410efbb6bc ("clocksource/drivers/clps711x: Convert init function to return error")
Fixes 2a6a8e2d9004 ("clocksource/drivers/clps711x: Remove board support")

---
changes in v2:
- Add tags of 'Fixes' and 'Cc'
- Reduce detailed enumeration of err paths
- Omit a pointer check before iounmap()
- Change goto target from out to unmap_io

---
 drivers/clocksource/clps711x-timer.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/clocksource/clps711x-timer.c +16 -7
index e95fdc49c226..bbceb0289d45 100644
@@ -78,24 +78,33 @@ static int __init clps711x_timer_init(struct device_node *np)
 	unsigned int irq = irq_of_parse_and_map(np, 0);
 	struct clk *clock = of_clk_get(np, 0);
 	void __iomem *base = of_iomap(np, 0);
+	int ret = 0;
 
 	if (!base)
 		return -ENOMEM;
-	if (!irq)
-		return -EINVAL;
-	if (IS_ERR(clock))
-		return PTR_ERR(clock);
+	if (!irq) {
+		ret = -EINVAL;
+		goto unmap_io;
+	}
+	if (IS_ERR(clock)) {
+		ret = PTR_ERR(clock);
+		goto unmap_io;
+	}
 
 	switch (of_alias_get_id(np, "timer")) {
 	case CLPS711X_CLKSRC_CLOCKSOURCE:
 		clps711x_clksrc_init(clock, base);
 		break;
 	case CLPS711X_CLKSRC_CLOCKEVENT:
-		return _clps711x_clkevt_init(clock, base, irq);
+		ret =  _clps711x_clkevt_init(clock, base, irq);
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
+		break;
 	}
 
-	return 0;
+unmap_io:
+	iounmap(base);
+	return ret;
 }
 TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init);
Signature · 1 line
2.20.1
#3 lore raw
Quoted 3 lines

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped via of_iomap(). Fix of_iomap leaks in err paths.

error?

Regards,
Markus

A message that is not in the archive (sent off-list or not yet received): [email protected]

#5 lore raw

Hi,

this patch does not seem to have reached lkml@, making impossible to extract via git b4 neither adding the Link tag.

Quoted 65 lines · Zhen Ni

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped via of_iomap(). Fix of_iomap leaks in error paths.

Fixes 04410efbb6bc ("clocksource/drivers/clps711x: Convert init function to return error")
Fixes 2a6a8e2d9004 ("clocksource/drivers/clps711x: Remove board support")

---
changes in v3:
- Change "err" to "error" in the commit message.
changes in v2:
- Add tags of 'Fixes' and 'Cc'
- Reduce detailed enumeration of err paths
- Omit a pointer check before iounmap()
- Change goto target from out to unmap_io

---
  drivers/clocksource/clps711x-timer.c | 23 ++++++++++++++++-------
  1 file changed, 16 insertions(+), 7 deletions(-)
drivers/clocksource/clps711x-timer.c +16 -7
index e95fdc49c226..bbceb0289d45 100644
@@ -78,24 +78,33 @@ static int __init clps711x_timer_init(struct device_node *np)
  	unsigned int irq = irq_of_parse_and_map(np, 0);
  	struct clk *clock = of_clk_get(np, 0);
  	void __iomem *base = of_iomap(np, 0);
+	int ret = 0;
 
  	if (!base)
  		return -ENOMEM;
-	if (!irq)
-		return -EINVAL;
-	if (IS_ERR(clock))
-		return PTR_ERR(clock);
+	if (!irq) {
+		ret = -EINVAL;
+		goto unmap_io;
+	}
+	if (IS_ERR(clock)) {
+		ret = PTR_ERR(clock);
+		goto unmap_io;
+	}
 
  	switch (of_alias_get_id(np, "timer")) {
  	case CLPS711X_CLKSRC_CLOCKSOURCE:
  		clps711x_clksrc_init(clock, base);
  		break;
  	case CLPS711X_CLKSRC_CLOCKEVENT:
-		return _clps711x_clkevt_init(clock, base, irq);
+		ret =  _clps711x_clkevt_init(clock, base, irq);
+		break;
  	default:
-		return -EINVAL;
+		ret = -EINVAL;
+		break;
  	}
 
-	return 0;
+unmap_io:
+	iounmap(base);
+	return ret;
  }
  TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init);
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author
#6 lore raw

PATCH v3 1/1 clocksource: clps711x: Fix resource leaks in error paths

In reply to Zhen Ni

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped via of_iomap(). Fix of_iomap leaks in error paths.

Fixes 04410efbb6bc ("clocksource/drivers/clps711x: Convert init function to return error")
Fixes 2a6a8e2d9004 ("clocksource/drivers/clps711x: Remove board support")

---
changes in v3:
- Change "err" to "error" in the commit message.
changes in v2:
- Add tags of 'Fixes' and 'Cc'
- Reduce detailed enumeration of err paths
- Omit a pointer check before iounmap()
- Change goto target from out to unmap_io

---
 drivers/clocksource/clps711x-timer.c | 23 ++++++++++++++++-------
 1 file changed, 16 insertions(+), 7 deletions(-)
drivers/clocksource/clps711x-timer.c +16 -7
index e95fdc49c226..bbceb0289d45 100644
@@ -78,24 +78,33 @@ static int __init clps711x_timer_init(struct device_node *np)
 	unsigned int irq = irq_of_parse_and_map(np, 0);
 	struct clk *clock = of_clk_get(np, 0);
 	void __iomem *base = of_iomap(np, 0);
+	int ret = 0;
 
 	if (!base)
 		return -ENOMEM;
-	if (!irq)
-		return -EINVAL;
-	if (IS_ERR(clock))
-		return PTR_ERR(clock);
+	if (!irq) {
+		ret = -EINVAL;
+		goto unmap_io;
+	}
+	if (IS_ERR(clock)) {
+		ret = PTR_ERR(clock);
+		goto unmap_io;
+	}
 
 	switch (of_alias_get_id(np, "timer")) {
 	case CLPS711X_CLKSRC_CLOCKSOURCE:
 		clps711x_clksrc_init(clock, base);
 		break;
 	case CLPS711X_CLKSRC_CLOCKEVENT:
-		return _clps711x_clkevt_init(clock, base, irq);
+		ret =  _clps711x_clkevt_init(clock, base, irq);
+		break;
 	default:
-		return -EINVAL;
+		ret = -EINVAL;
+		break;
 	}
 
-	return 0;
+unmap_io:
+	iounmap(base);
+	return ret;
 }
 TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init);
Signature · 1 line
2.20.1
#7 lore raw
Quoted 25 lines · Zhen Ni

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped via of_iomap(). Fix of_iomap leaks in error paths.

Fixes 04410efbb6bc ("clocksource/drivers/clps711x: Convert init function to return error")
Fixes 2a6a8e2d9004 ("clocksource/drivers/clps711x: Remove board support")

---
changes in v3:
- Change "err" to "error" in the commit message.
changes in v2:
- Add tags of 'Fixes' and 'Cc'
- Reduce detailed enumeration of err paths
- Omit a pointer check before iounmap()
- Change goto target from out to unmap_io

---
  drivers/clocksource/clps711x-timer.c | 23 ++++++++++++++++-------
  1 file changed, 16 insertions(+), 7 deletions(-)
drivers/clocksource/clps711x-timer.c +0 -0
index e95fdc49c226..bbceb0289d45 100644

drivers/clocksource/clps711x-timer.c static int __init clps711x_timer_init(struct device_node *np) · line 81

Quoted 4 lines
drivers/clocksource/clps711x-timer.c +1 -0
@@ -78,24 +78,33 @@ static int __init clps711x_timer_init(struct device_node *np)
  	unsigned int irq = irq_of_parse_and_map(np, 0);
  	struct clk *clock = of_clk_get(np, 0);
  	void __iomem *base = of_iomap(np, 0);
+	int ret = 0;
	int ret = -EINVAL;

drivers/clocksource/clps711x-timer.c static int __init clps711x_timer_init(struct device_node *np) · line 86

Quoted 8 lines
drivers/clocksource/clps711x-timer.c +2 -4
  	if (!base)
  		return -ENOMEM;
-	if (!irq)
-		return -EINVAL;
-	if (IS_ERR(clock))
-		return PTR_ERR(clock);
+	if (!irq) {
+		ret = -EINVAL;

Remove the above

Quoted 8 lines
drivers/clocksource/clps711x-timer.c +6 -0
+		goto unmap_io;
+	}
+	if (IS_ERR(clock)) {
+		ret = PTR_ERR(clock);
+		goto unmap_io;
+	}
 
  	switch (of_alias_get_id(np, "timer")) {

drivers/clocksource/clps711x-timer.c static int __init clps711x_timer_init(struct device_node *np) · line 102

Quoted 10 lines
drivers/clocksource/clps711x-timer.c +3 -2
  	case CLPS711X_CLKSRC_CLOCKSOURCE:
  		clps711x_clksrc_init(clock, base);
  		break;
  	case CLPS711X_CLKSRC_CLOCKEVENT:
-		return _clps711x_clkevt_init(clock, base, irq);
+		ret =  _clps711x_clkevt_init(clock, base, irq);
+		break;
  	default:
-		return -EINVAL;
+		ret = -EINVAL;

Remove the above

Quoted 9 lines
drivers/clocksource/clps711x-timer.c +4 -1
+		break;
  	}
 
-	return 0;
+unmap_io:
+	iounmap(base);
+	return ret;
  }
  TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init);
Signature · 5 lines
<http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs

Follow Linaro:  <http://www.facebook.com/pages/Linaro> Facebook |
<http://twitter.com/#!/linaroorg> Twitter |
<http://www.linaro.org/linaro-blog/> Blog
author
#8 lore raw

PATCH v4 1/1 clocksource: clps711x: Fix resource leaks in error paths

In reply to Zhen Ni

The current implementation of clps711x_timer_init() has multiple error paths that directly return without releasing the base I/O memory mapped via of_iomap(). Fix of_iomap leaks in error paths.

Fixes 04410efbb6bc ("clocksource/drivers/clps711x: Convert init function to return error")
Fixes 2a6a8e2d9004 ("clocksource/drivers/clps711x: Remove board support")

---
changes in v4:
- Change the default value of ret from 0 to -INVAL
- Do not release the iomap in success paths changes in v3:
- Change "err" to "error" in the commit message. changes in v2:
- Add tags of 'Fixes' and 'Cc'
- Reduce detailed enumeration of err paths
- Omit a pointer check before iounmap()
- Change goto target from out to unmap_io

---
 drivers/clocksource/clps711x-timer.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/clocksource/clps711x-timer.c +15 -6
index e95fdc49c226..84dbcd049c25 100644
@@ -78,24 +78,33 @@ static int __init clps711x_timer_init(struct device_node *np)
 	unsigned int irq = irq_of_parse_and_map(np, 0);
 	struct clk *clock = of_clk_get(np, 0);
 	void __iomem *base = of_iomap(np, 0);
+	int ret = -EINVAL;
 
 	if (!base)
 		return -ENOMEM;
 	if (!irq)
-		return -EINVAL;
-	if (IS_ERR(clock))
-		return PTR_ERR(clock);
+		goto unmap_io;
+	if (IS_ERR(clock)) {
+		ret = PTR_ERR(clock);
+		goto unmap_io;
+	}
 
 	switch (of_alias_get_id(np, "timer")) {
 	case CLPS711X_CLKSRC_CLOCKSOURCE:
 		clps711x_clksrc_init(clock, base);
+		ret = 0;
 		break;
 	case CLPS711X_CLKSRC_CLOCKEVENT:
-		return _clps711x_clkevt_init(clock, base, irq);
+		ret =  _clps711x_clkevt_init(clock, base, irq);
+		break;
 	default:
-		return -EINVAL;
+		break;
 	}
+	if (!ret)
+		return ret;
 
-	return 0;
+unmap_io:
+	iounmap(base);
+	return ret;
 }
 TIMER_OF_DECLARE(clps711x, "cirrus,ep7209-timer", clps711x_timer_init);
Signature · 1 line
2.20.1