Skip to contentSkip to conversation

pwm: atmel: add missing clk_disable_unprepare()

PATCH In review Hari Prasath Gujulan Elango 3 participants 4 messages Sep 2, 2023 – Dec 4, 2023

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

PATCH v2 1/1 pwm: atmel: add missing clk_disable_unprepare()

Fix the below smatch warning:

drivers/pwm/pwm-atmel-hlcdc.c:167 atmel_hlcdc_pwm_apply() warn: 'new_clk' from clk_prepare_enable() not released on lines: 112,137,142,149.

'Fixes: 2b4984bef47a5 ("pwm: atmel-hlcdc: Convert to the atomic PWM API")'

changelog of v2:

- moved the clk_disable_unprepare to single point of return.
- cur_clk set to NULL before return.

---
 drivers/pwm/pwm-atmel-hlcdc.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
drivers/pwm/pwm-atmel-hlcdc.c +7 -5
index 96a709a9d49a..4d35b838203f 100644
@@ -44,7 +44,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
 	struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
 	struct atmel_hlcdc *hlcdc = chip->hlcdc;
 	unsigned int status;
-	int ret;
+	int ret = 0;
 
 	if (state->enabled) {
 		struct clk *new_clk = hlcdc->slow_clk;
@@ -109,7 +109,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
 						 ATMEL_HLCDC_CLKPWMSEL,
 						 gencfg);
 			if (ret)
-				return ret;
+				goto disable_new_clk;
 		}
 
 		do_div(pwmcval, state->period);
@@ -134,18 +134,20 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
 					 ATMEL_HLCDC_PWMPOL,
 					 pwmcfg);
 		if (ret)
-			return ret;
+			goto disable_new_clk;
 
 		ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN,
 				   ATMEL_HLCDC_PWM);
 		if (ret)
-			return ret;
+			goto disable_new_clk;
 
 		ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
 					       status,
 					       status & ATMEL_HLCDC_PWM,
 					       10, 0);
-		if (ret)
+disable_new_clk:
+			clk_disable_unprepare(new_clk);
+			chip->cur_clk = NULL;
 			return ret;
 	} else {
 		ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS,
Signature · 1 line
2.34.1
#1 lore raw

Le 02/09/2023 à 08:32, Hari Prasath Gujulan Elango a écrit :

Quoted 5 lines

Fix the below smatch warning:

drivers/pwm/pwm-atmel-hlcdc.c:167 atmel_hlcdc_pwm_apply() warn: 'new_clk' from clk_prepare_enable() not released on lines: 112,137,142,149.

'Fixes: 2b4984bef47a5 ("pwm: atmel-hlcdc: Convert to the atomic PWM API")'

Hi,

There shouldn't be ' before Fixes:, neither at the end. Commit id should be 12 chars, not 13.
There shouldn't be a blank line between Fixes and Signed-off-by.

I think that the Fixes tag should be 2b4984bef47a ("pwm: add support for atmel-hlcdc-pwm device". The commit you point you have touched this code, be part of what you change was already there before that.

Quoted 3 lines

There should be a --- between the signed-of-by and the below changelog, so that the changelog will not be merged in the git history.

Also, it is also useful to add the link at lore.kernel.org of previous versions.

Here, it would be something like:
v1: https://lore.kernel.org/all/[email protected]/

Quoted 13 lines

changelog of v2:

- moved the clk_disable_unprepare to single point of return.
- cur_clk set to NULL before return.

---
  drivers/pwm/pwm-atmel-hlcdc.c | 12 +++++++-----
  1 file changed, 7 insertions(+), 5 deletions(-)
drivers/pwm/pwm-atmel-hlcdc.c +0 -0
index 96a709a9d49a..4d35b838203f 100644

drivers/pwm/pwm-atmel-hlcdc.c static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm, · line 47

Quoted 5 lines
drivers/pwm/pwm-atmel-hlcdc.c +1 -1
@@ -44,7 +44,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
  	struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
  	struct atmel_hlcdc *hlcdc = chip->hlcdc;
  	unsigned int status;
-	int ret;
+	int ret = 0;

This initialization looks un-needed and un-related to your changes.

Quoted 20 lines
drivers/pwm/pwm-atmel-hlcdc.c +2 -2
  	if (state->enabled) {
  		struct clk *new_clk = hlcdc->slow_clk;
@@ -109,7 +109,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
  						 ATMEL_HLCDC_CLKPWMSEL,
  						 gencfg);
  			if (ret)
-				return ret;
+				goto disable_new_clk;
  		}
 
  		do_div(pwmcval, state->period);
@@ -134,18 +134,20 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm,
  					 ATMEL_HLCDC_PWMPOL,
  					 pwmcfg);
  		if (ret)
-			return ret;
+			goto disable_new_clk;
 
  		ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN,

drivers/pwm/pwm-atmel-hlcdc.c static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm, · line 148

Quoted 10 lines
drivers/pwm/pwm-atmel-hlcdc.c +1 -2
  				   ATMEL_HLCDC_PWM);
  		if (ret)
-			return ret;
+			goto disable_new_clk;
 
  		ret = regmap_read_poll_timeout(hlcdc->regmap, ATMEL_HLCDC_SR,
  					       status,
  					       status & ATMEL_HLCDC_PWM,
  					       10, 0);
-		if (ret)

Removing this test looks wrong.

drivers/pwm/pwm-atmel-hlcdc.c static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm, · line 151

Quoted 4 lines
drivers/pwm/pwm-atmel-hlcdc.c +3 -0
+disable_new_clk:
+			clk_disable_unprepare(new_clk);
+			chip->cur_clk = NULL;
  			return ret;

This is a really unusual pattern.
Usually, an error handling path is added at the end of the function, not in the middle.

CJ

Quoted 2 lines
drivers/pwm/pwm-atmel-hlcdc.c +0 -0
  	} else {
  		ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS,
#2 lore raw

Hello Christophe,

Quoted 15 lines · Christophe JAILLET

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 8 lines · Hari Prasath Gujulan Elango

Fix the below smatch warning:

drivers/pwm/pwm-atmel-hlcdc.c:167 atmel_hlcdc_pwm_apply() warn:
'new_clk' from clk_prepare_enable() not released on lines:
112,137,142,149.

'Fixes: 2b4984bef47a5 ("pwm: atmel-hlcdc: Convert to the atomic PWM API")'

Hi,

Quoted 9 lines

There shouldn't be ' before Fixes:, neither at the end. Commit id should be 12 chars, not 13.
There shouldn't be a blank line between Fixes and Signed-off-by.

I think that the Fixes tag should be 2b4984bef47a ("pwm: add support for atmel-hlcdc-pwm device".
The commit you point you have touched this code, be part of what you change was already there before that.

Thank you, I admit that I have messes up this part. Its been quite a while sending patches upstream and I seem to have forgotten the basics. I will take time to send the v3 paying more attention to these small details.

Quoted 22 lines
Quoted 3 lines

There should be a --- between the signed-of-by and the below changelog, so that the changelog will not be merged in the git history.

Also, it is also useful to add the link at lore.kernel.org of previous versions.

Here, it would be something like:
v1:
https://lore.kernel.org/all/[email protected]/

Quoted 8 lines

changelog of v2:

          - moved the clk_disable_unprepare to single point of return.
          - cur_clk set to NULL before return.
---
  drivers/pwm/pwm-atmel-hlcdc.c | 12 +++++++-----

  1 file changed, 7 insertions(+), 5 deletions(-)
Quoted 15 lines

drivers/pwm/pwm-atmel-hlcdc.c static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, · line 48

Quoted 12 lines
drivers/pwm/pwm-atmel-hlcdc.c +1 -1
index 96a709a9d49a..4d35b838203f 100644
@@ -44,7 +44,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, 
 struct pwm_device *pwm,
       struct atmel_hlcdc_pwm *chip = to_atmel_hlcdc_pwm(c);
       struct atmel_hlcdc *hlcdc = chip->hlcdc;
       unsigned int status;
-     int ret;
+     int ret = 0;

This initialization looks un-needed and un-related to your changes.

Though the kernel API's used below return 0 upon success but just thought I will initialize it to 0.

Quoted 27 lines
Quoted 27 lines
drivers/pwm/pwm-atmel-hlcdc.c +1 -1
       if (state->enabled) {
               struct clk *new_clk = hlcdc->slow_clk;
@@ -109,7 +109,7 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip 
 *c, struct pwm_device *pwm,
                                                ATMEL_HLCDC_CLKPWMSEL,
                                                gencfg);
                       if (ret)
-                             return ret;
+                             goto disable_new_clk;
               }
 

              do_div(pwmcval, state->period);

drivers/pwm/pwm-atmel-hlcdc.c +2 -2
@@ -134,18 +134,20 @@ static int atmel_hlcdc_pwm_apply(struct pwm_chip 
 *c, struct pwm_device *pwm,
                                        ATMEL_HLCDC_PWMPOL,
                                        pwmcfg);
               if (ret)
-                     return ret;
+                     goto disable_new_clk;
 
               ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_EN,
                                  ATMEL_HLCDC_PWM);
               if (ret)
-                     return ret;
+                     goto disable_new_clk;
 
Quoted 8 lines

drivers/pwm/pwm-atmel-hlcdc.c static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm, · line 148

Quoted 6 lines
drivers/pwm/pwm-atmel-hlcdc.c +0 -1
               ret = regmap_read_poll_timeout(hlcdc->regmap, 
 ATMEL_HLCDC_SR,
                                              status,
                                              status & ATMEL_HLCDC_PWM,
                                              10, 0);
-             if (ret)

Removing this test looks wrong.

Will add it back and include a 'goto'

Quoted 11 lines

drivers/pwm/pwm-atmel-hlcdc.c static int atmel_hlcdc_pwm_apply(struct pwm_chip *c, struct pwm_device *pwm, · line 151

Quoted 5 lines
drivers/pwm/pwm-atmel-hlcdc.c +3 -0
+disable_new_clk:
+                     clk_disable_unprepare(new_clk);
+                     chip->cur_clk = NULL;
                       return ret;

This is a really unusual pattern.
Usually, an error handling path is added at the end of the function, not in the middle.

CJ

I will move this towards the end as it's done usually.

-Hari

Quoted 4 lines
Quoted 3 lines

      } else {
              ret = regmap_write(hlcdc->regmap, ATMEL_HLCDC_DIS,

#3 lore raw

Hello Hari,

Quoted 4 lines · On Wed, Sep 06, 2023 at 06:15:36AM +0000, [email protected] wrote:

Thank you, I admit that I have messes up this part. Its been quite a while sending patches upstream and I seem to have forgotten the basics. I will take time to send the v3 paying more attention to these small details.

You never followed up with the promised v3. Is this still on your radar? Would be great to get this addressed as it fixes a clk imbalance, right?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |