Skip to contentSkip to conversation

Exynos Thermal code improvement

PATCH Superseded Anand Moon 2 participants 14 messages Feb 16, 2025 – Mar 6, 2025

Subsystems (3)

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Anand Moon

author
#0 lore raw

v3 cover Exynos Thermal code improvement

Hi All,

This patch series is a rework of my previous patch series [1], where the code changes were not adequately justified.

In this new series, I have improved the commit subject and commit message to better explain the changes.

Tested on Odroid U3 amd XU4 SoC boards.

[1] https://lore.kernel.org/all/[email protected]/
[0] https://lore.kernel.org/lkml/CANAwSgS=08fVsqn95WHzSF71WTTyD2-=K2C6-BEz0tY0t6A1-g@mail.gmail.com/T/#m77e57120d230d57f34c29e1422d7fc5f5587ac30

Thanks
-Anand

Anand Moon (4):
drivers/thermal/exynos: Refactor clk_sec initialization inside
SOC-specific case
drivers/thermal/exymos: Remove redundant IS_ERR() checks for clk_sec
clock
drivers/thermal/exymos: Fixed the efuse min max value for exynos5422 drivers/thermal/exymos: Use guard notation when acquiring mutex

 drivers/thermal/samsung/exynos_tmu.c | 78 ++++++++++++++--------------
 1 file changed, 39 insertions(+), 39 deletions(-)

base-commit: ba643b6d84409e8a9057d5bdd6dd99255b1a88fe

Signature · 1 line
2.48.1
author
#1 lore raw

PATCH v3 1/4 drivers/thermal/exynos: Refactor clk_sec initialization inside SOC-specific case

Refactor the initialization of the clk_sec clock to be inside the SOC_ARCH_EXYNOS5420_TRIMINFO case. It ensures that the clk_sec clock is only initialized for the specified SOC and not for other SOCs, thereby simplifying the code.

---
v3: improve the commit message

---
 drivers/thermal/samsung/exynos_tmu.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +13 -13
index 47a99b3c5395..9c138772d380 100644
@@ -1040,19 +1040,6 @@ static int exynos_tmu_probe(struct platform_device *pdev)
 	if (IS_ERR(data->clk))
 		return dev_err_probe(dev, PTR_ERR(data->clk), "Failed to get clock\n");
 
-	data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
-	if (IS_ERR(data->clk_sec)) {
-		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
-			return dev_err_probe(dev, PTR_ERR(data->clk_sec),
-					     "Failed to get triminfo clock\n");
-	} else {
-		ret = clk_prepare(data->clk_sec);
-		if (ret) {
-			dev_err(dev, "Failed to get clock\n");
-			return ret;
-		}
-	}
-
 	ret = clk_prepare(data->clk);
 	if (ret) {
 		dev_err(dev, "Failed to get clock\n");
@@ -1060,6 +1047,19 @@ static int exynos_tmu_probe(struct platform_device *pdev)
 	}
 
 	switch (data->soc) {
+	case SOC_ARCH_EXYNOS5420_TRIMINFO:
+		data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
+		if (IS_ERR(data->clk_sec)) {
+			return dev_err_probe(dev, PTR_ERR(data->clk_sec),
+					     "Failed to get triminfo clock\n");
+		} else {
+			ret = clk_prepare(data->clk_sec);
+			if (ret) {
+				dev_err(dev, "Failed to get clock\n");
+				return ret;
+			}
+		}
+	break;
 	case SOC_ARCH_EXYNOS5433:
 	case SOC_ARCH_EXYNOS7:
 		data->sclk = devm_clk_get(dev, "tmu_sclk");
Signature · 1 line
2.48.1
author
#4 lore raw

PATCH v3 2/4 drivers/thermal/exymos: Remove redundant IS_ERR() checks for clk_sec clock

In reply to Anand Moon

Remove unnecessary IS_ERR() checks for the clk_sec clock, the clk_enable() and clk_disable() functions can handle NULL clock pointers, so the additional checks are redundant and have been removed to simplify the code.

---
v3: improve the commit message

---
 drivers/thermal/samsung/exynos_tmu.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +4 -8
index 9c138772d380..8d26000c73aa 100644
@@ -258,8 +258,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
 	mutex_lock(&data->lock);
 	clk_enable(data->clk);
-	if (!IS_ERR(data->clk_sec))
-		clk_enable(data->clk_sec);
+	clk_enable(data->clk_sec);
 
 	status = readb(data->base + EXYNOS_TMU_REG_STATUS);
 	if (!status) {
@@ -269,8 +268,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 		data->tmu_clear_irqs(data);
 	}
 
-	if (!IS_ERR(data->clk_sec))
-		clk_disable(data->clk_sec);
+	clk_disable(data->clk_sec);
 	clk_disable(data->clk);
 	mutex_unlock(&data->lock);
 
@@ -1115,8 +1113,7 @@ static int exynos_tmu_probe(struct platform_device *pdev)
 err_clk:
 	clk_unprepare(data->clk);
 err_clk_sec:
-	if (!IS_ERR(data->clk_sec))
-		clk_unprepare(data->clk_sec);
+	clk_unprepare(data->clk_sec);
 	return ret;
 }
 
@@ -1128,8 +1125,7 @@ static void exynos_tmu_remove(struct platform_device *pdev)
 
 	clk_disable_unprepare(data->sclk);
 	clk_unprepare(data->clk);
-	if (!IS_ERR(data->clk_sec))
-		clk_unprepare(data->clk_sec);
+	clk_unprepare(data->clk_sec);
 }
 
 #ifdef CONFIG_PM_SLEEP
Signature · 1 line
2.48.1
author
#6 lore raw

PATCH v3 3/4 drivers/thermal/exymos: Fixed the efuse min max value for exynos5422

In reply to Anand Moon

As per Exynos5422 user manual e-Fuse range min~max range is 16~76. if e-Fuse value is out of this range, then thermal sensor may not sense thermal data properly. Refactors the efuse value initialization logic within exynos_map_dt_data function by replacing the nested if-else statements with a switch statement. Ensures proper initialization of efuse values based on the SOC type.

---
v3: improve the commit message, fixing the warning wtth W=1

---
 drivers/thermal/samsung/exynos_tmu.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +15 -4
index 8d26000c73aa..fe090c1a93ab 100644
@@ -899,12 +899,23 @@ static int exynos_map_dt_data(struct platform_device *pdev)
 		data->gain = 8;
 		data->reference_voltage = 16;
 		data->efuse_value = 55;
-		if (data->soc != SOC_ARCH_EXYNOS5420 &&
-		    data->soc != SOC_ARCH_EXYNOS5420_TRIMINFO)
+		data->max_efuse_value = 100;
+		switch (data->soc) {
+		case SOC_ARCH_EXYNOS3250:
+		case SOC_ARCH_EXYNOS4412:
+		case SOC_ARCH_EXYNOS5250:
+		case SOC_ARCH_EXYNOS5260:
 			data->min_efuse_value = 40;
-		else
+			break;
+		case SOC_ARCH_EXYNOS5420:
+		case SOC_ARCH_EXYNOS5420_TRIMINFO:
+			data->min_efuse_value = 16;
+			data->max_efuse_value = 76;
+			break;
+		default:
 			data->min_efuse_value = 0;
-		data->max_efuse_value = 100;
+			break;
+		}
 		break;
 	case SOC_ARCH_EXYNOS5433:
 		data->tmu_set_low_temp = exynos5433_tmu_set_low_temp;
Signature · 1 line
2.48.1
author
#7 lore raw

PATCH v3 4/4 drivers/thermal/exymos: Use guard notation when acquiring mutex

In reply to Anand Moon

Using guard notation makes the code more compact and error handling more robust by ensuring that mutexes are released in all code paths when control leaves critical section.

---
v3: new patch

---
 drivers/thermal/samsung/exynos_tmu.c | 21 +++++++--------------
 1 file changed, 7 insertions(+), 14 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +7 -14
index fe090c1a93ab..a34ba3858d64 100644
@@ -256,7 +256,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 	unsigned int status;
 	int ret = 0;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	clk_enable(data->clk);
 	clk_enable(data->clk_sec);
 
@@ -270,7 +270,6 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
 	clk_disable(data->clk_sec);
 	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
 	return ret;
 }
@@ -292,13 +291,12 @@ static int exynos_thermal_zone_configure(struct platform_device *pdev)
 		return ret;
 	}
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	clk_enable(data->clk);
 
 	data->tmu_set_crit_temp(data, temp / MCELSIUS);
 
 	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
 	return 0;
 }
@@ -325,12 +323,11 @@ static void exynos_tmu_control(struct platform_device *pdev, bool on)
 {
 	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	clk_enable(data->clk);
 	data->tmu_control(pdev, on);
 	data->enabled = on;
 	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 }
 
 static void exynos_tmu_update_bit(struct exynos_tmu_data *data, int reg_off,
@@ -645,7 +642,7 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
 		 */
 		return -EAGAIN;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	clk_enable(data->clk);
 
 	value = data->tmu_read(data);
@@ -655,7 +652,6 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
 		*temp = code_to_temp(data, value) * MCELSIUS;
 
 	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
 	return ret;
 }
@@ -720,11 +716,10 @@ static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
 	if (temp && temp < MCELSIUS)
 		goto out;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	clk_enable(data->clk);
 	data->tmu_set_emulation(data, temp);
 	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 	return 0;
 out:
 	return ret;
@@ -760,14 +755,13 @@ static irqreturn_t exynos_tmu_threaded_irq(int irq, void *id)
 
 	thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	clk_enable(data->clk);
 
 	/* TODO: take action based on particular interrupt */
 	data->tmu_clear_irqs(data);
 
 	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
 	return IRQ_HANDLED;
 }
@@ -987,7 +981,7 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
 {
 	struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
 	clk_enable(data->clk);
 
 	if (low > INT_MIN)
@@ -1000,7 +994,6 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
 		data->tmu_disable_high(data);
 
 	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
 	return 0;
 }
Signature · 1 line
2.48.1
reviewer
#2 lore raw

Re: [PATCH v3 1/4] drivers/thermal/exynos: Refactor clk_sec initialization inside SOC-specific case

In reply to Anand Moon

Hi Anand,

Quoted 4 lines · Anand Moon

Refactor the initialization of the clk_sec clock to be inside the SOC_ARCH_EXYNOS5420_TRIMINFO case. It ensures that the clk_sec clock is only initialized for the specified SOC and not for other SOCs, thereby simplifying the code.

So IIUC there was no need to init that clock for other types of SoCs... Do we know that for sure (e.g. from other TRMs)?

If that was the case, then simplification here can go further, but after some fixes.

Quoted 20 lines

---
v3: improve the commit message

---
  drivers/thermal/samsung/exynos_tmu.c | 26 +++++++++++++-------------
  1 file changed, 13 insertions(+), 13 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +0 -4
index 47a99b3c5395..9c138772d380 100644
@@ -1040,19 +1040,6 @@ static int exynos_tmu_probe(struct platform_device *pdev)
  	if (IS_ERR(data->clk))
  		return dev_err_probe(dev, PTR_ERR(data->clk), "Failed to get clock\n");
 
-	data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
-	if (IS_ERR(data->clk_sec)) {
-		if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
-			return dev_err_probe(dev, PTR_ERR(data->clk_sec),

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1043

Quoted 10 lines
drivers/thermal/samsung/exynos_tmu.c +0 -9
-					     "Failed to get triminfo clock\n");
-	} else {
-		ret = clk_prepare(data->clk_sec);
-		if (ret) {
-			dev_err(dev, "Failed to get clock\n");
-			return ret;
-		}
-	}
-
  	ret = clk_prepare(data->clk);

Here the data->clk is now used in different order.

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1054

Quoted 11 lines
drivers/thermal/samsung/exynos_tmu.c +5 -0
  	if (ret) {
  		dev_err(dev, "Failed to get clock\n");
@@ -1060,6 +1047,19 @@ static int exynos_tmu_probe(struct platform_device *pdev)
  	}
 
  	switch (data->soc) {
+	case SOC_ARCH_EXYNOS5420_TRIMINFO:
+		data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
+		if (IS_ERR(data->clk_sec)) {
+			return dev_err_probe(dev, PTR_ERR(data->clk_sec),
+					     "Failed to get triminfo clock\n");

Then here you shouldn't simply copy the old code. Now the data->clk is first, so should be 'clk_unprepare()' before return of the function.

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1055

Quoted 1 line
drivers/thermal/samsung/exynos_tmu.c +1 -0
+		} else {

You can get rid of this 'else' above and still be safe in your refactoring.

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1060

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +5 -0
+			ret = clk_prepare(data->clk_sec);
+			if (ret) {
+				dev_err(dev, "Failed to get clock\n");
+				return ret;
+			}

Here you can further simplify this to something like:
-----------------------8<-------------------------------------

+ case SOC_ARCH_EXYNOS5420_TRIMINFO:
+ data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
+ if (IS_ERR(data->clk_sec)) {
+ clk_unprepare(data->clk); ///// <----
+ return dev_err_probe(dev, PTR_ERR(data->clk_sec),
+ "Failed to get triminfo clock\n");
+ }
+ ret = clk_prepare(data->clk_sec);
+ if (ret) {
+ dev_err(dev, "Failed to get clock\n");
+ clk_unprepare(data->clk); ///// <----
+ return ret;
+ }
+
+ break;

--------------------------->8---------------------------------

Or with better 'goto' flow.

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1065

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +2 -0
+		}
+	break;
  	case SOC_ARCH_EXYNOS5433:
  	case SOC_ARCH_EXYNOS7:
  		data->sclk = devm_clk_get(dev, "tmu_sclk");

Also, you should revisit the 'goto' cleanup section at the bottom.

Regards,
Lukasz

author
#3 lore raw

Re: [PATCH v3 1/4] drivers/thermal/exynos: Refactor clk_sec initialization inside SOC-specific case

Hi Lukasz,

Thanks for your review comments

Quoted 11 lines · Lukasz Luba

Hi Anand,

Quoted 4 lines · Anand Moon

Refactor the initialization of the clk_sec clock to be inside the SOC_ARCH_EXYNOS5420_TRIMINFO case. It ensures that the clk_sec clock is only initialized for the specified SOC and not for other SOCs, thereby simplifying the code.

So IIUC there was no need to init that clock for other types of SoCs... Do we know that for sure (e.g. from other TRMs)?

This clock (clk) is utilized for the Thermal Management Unit (TMU) and the GPU in the Exynos 542x,
as specified in the user manual.

Quoted 20 lines

If that was the case, then simplification here can go further, but after some fixes.

Quoted 16 lines

---
v3: improve the commit message

---
  drivers/thermal/samsung/exynos_tmu.c | 26 +++++++++++++-------------
  1 file changed, 13 insertions(+), 13 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +0 -0
index 47a99b3c5395..9c138772d380 100644
@@ -1040,19 +1040,6 @@ static int exynos_tmu_probe(struct platform_device *pdev)
      if (IS_ERR(data->clk))
              return dev_err_probe(dev, PTR_ERR(data->clk), "Failed to get clock\n");
 
Quoted 16 lines
Quoted 4 lines
drivers/thermal/samsung/exynos_tmu.c +0 -4
-     data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
-     if (IS_ERR(data->clk_sec)) {
-             if (data->soc == SOC_ARCH_EXYNOS5420_TRIMINFO)
-                     return dev_err_probe(dev, PTR_ERR(data->clk_sec),

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1043

Quoted 10 lines
drivers/thermal/samsung/exynos_tmu.c +0 -9
-                                          "Failed to get triminfo clock\n");
-     } else {
-             ret = clk_prepare(data->clk_sec);
-             if (ret) {
-                     dev_err(dev, "Failed to get clock\n");
-                     return ret;
-             }
-     }
-
      ret = clk_prepare(data->clk);

Here the data->clk is now used in different order.

Ok.

Quoted 35 lines

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1054

Quoted 12 lines
drivers/thermal/samsung/exynos_tmu.c +5 -0
      if (ret) {
              dev_err(dev, "Failed to get clock\n");
@@ -1060,6 +1047,19 @@ static int exynos_tmu_probe(struct platform_device *pdev)
      }
 
      switch (data->soc) {
+     case SOC_ARCH_EXYNOS5420_TRIMINFO:
+             data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
+             if (IS_ERR(data->clk_sec)) {
+                     return dev_err_probe(dev, PTR_ERR(data->clk_sec),
+                                          "Failed to get triminfo clock\n");

Then here you shouldn't simply copy the old code. Now the data->clk is first, so should be 'clk_unprepare()' before return of the function.

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1055

Quoted 1 line
drivers/thermal/samsung/exynos_tmu.c +1 -0
+             } else {

You can get rid of this 'else' above and still be safe in your refactoring.

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1060

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +5 -0
+                     ret = clk_prepare(data->clk_sec);
+                     if (ret) {
+                             dev_err(dev, "Failed to get clock\n");
+                             return ret;
+                     }

Here you can further simplify this to something like:
-----------------------8<-------------------------------------

drivers/thermal/samsung/exynos_tmu.c +5 -0
+       case SOC_ARCH_EXYNOS5420_TRIMINFO:
+               data->clk_sec = devm_clk_get(dev, "tmu_triminfo_apbif");
+               if (IS_ERR(data->clk_sec)) {
+                       clk_unprepare(data->clk); ///// <----
+                       return dev_err_probe(dev, PTR_ERR(data->clk_sec),

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1062

Quoted 10 lines
drivers/thermal/samsung/exynos_tmu.c +10 -0
+                                            "Failed to get triminfo clock\n");
+               }
+               ret = clk_prepare(data->clk_sec);
+               if (ret) {
+                       dev_err(dev, "Failed to get clock\n");
+                       clk_unprepare(data->clk); ///// <----
+                       return ret;
+               }
+
+       break;

Ok

Quoted 5 lines

--------------------------->8---------------------------------

Or with better 'goto' flow.

Quoted 8 lines

drivers/thermal/samsung/exynos_tmu.c static int exynos_tmu_probe(struct platform_device *pdev) · line 1065

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +2 -0
+             }
+     break;
      case SOC_ARCH_EXYNOS5433:
      case SOC_ARCH_EXYNOS7:
              data->sclk = devm_clk_get(dev, "tmu_sclk");

Also, you should revisit the 'goto' cleanup section at the bottom.

Thanks. I will recheck and update the code for the next version.

Quoted 3 lines

Regards,
Lukasz

Thanks
-Anand

reviewer
#5 lore raw

Re: [PATCH v3 2/4] drivers/thermal/exymos: Remove redundant IS_ERR() checks for clk_sec clock

In reply to Anand Moon
Quoted 4 lines · Anand Moon

Remove unnecessary IS_ERR() checks for the clk_sec clock, the clk_enable() and clk_disable() functions can handle NULL clock pointers, so the additional checks are redundant and have been removed to simplify the code.

This patch looks sane, just rework the 'goto' stuff in the exynos_tmu_probe() maybe in the patch 1/4 so won't be needed here.

reviewer
#8 lore raw

Re: [PATCH v3 4/4] drivers/thermal/exymos: Use guard notation when acquiring mutex

In reply to Anand Moon
Quoted 117 lines · Anand Moon

Using guard notation makes the code more compact and error handling more robust by ensuring that mutexes are released in all code paths when control leaves critical section.

---
v3: new patch

---
  drivers/thermal/samsung/exynos_tmu.c | 21 +++++++--------------
  1 file changed, 7 insertions(+), 14 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +7 -13
index fe090c1a93ab..a34ba3858d64 100644
@@ -256,7 +256,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
  	unsigned int status;
  	int ret = 0;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
  	clk_enable(data->clk);
  	clk_enable(data->clk_sec);
 
@@ -270,7 +270,6 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
  	clk_disable(data->clk_sec);
  	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
  	return ret;
  }
@@ -292,13 +291,12 @@ static int exynos_thermal_zone_configure(struct platform_device *pdev)
  		return ret;
  	}
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
  	clk_enable(data->clk);
 
  	data->tmu_set_crit_temp(data, temp / MCELSIUS);
 
  	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
  	return 0;
  }
@@ -325,12 +323,11 @@ static void exynos_tmu_control(struct platform_device *pdev, bool on)
  {
  	struct exynos_tmu_data *data = platform_get_drvdata(pdev);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
  	clk_enable(data->clk);
  	data->tmu_control(pdev, on);
  	data->enabled = on;
  	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
  }
 
  static void exynos_tmu_update_bit(struct exynos_tmu_data *data, int reg_off,
@@ -645,7 +642,7 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
  		 */
  		return -EAGAIN;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
  	clk_enable(data->clk);
 
  	value = data->tmu_read(data);
@@ -655,7 +652,6 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
  		*temp = code_to_temp(data, value) * MCELSIUS;
 
  	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
  	return ret;
  }
@@ -720,11 +716,10 @@ static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
  	if (temp && temp < MCELSIUS)
  		goto out;
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
  	clk_enable(data->clk);
  	data->tmu_set_emulation(data, temp);
  	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
  	return 0;
  out:
  	return ret;
@@ -760,14 +755,13 @@ static irqreturn_t exynos_tmu_threaded_irq(int irq, void *id)
 
  	thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
  	clk_enable(data->clk);
 
  	/* TODO: take action based on particular interrupt */
  	data->tmu_clear_irqs(data);
 
  	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
  	return IRQ_HANDLED;
  }
@@ -987,7 +981,7 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
  {
  	struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
 
-	mutex_lock(&data->lock);
+	guard(mutex)(&data->lock);
  	clk_enable(data->clk);
 
  	if (low > INT_MIN)

drivers/thermal/samsung/exynos_tmu.c static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high) · line 999

Quoted 7 lines
drivers/thermal/samsung/exynos_tmu.c +0 -1
@@ -1000,7 +994,6 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
  		data->tmu_disable_high(data);
 
  	clk_disable(data->clk);
-	mutex_unlock(&data->lock);
 
  	return 0;
  }

IMO you should be able to even use something like we have core framework:

guard(thermal_zone)(tz);

Your mutex name is simply 'lock' in the struct exynos_tmu_data so you should be able to leverage this by:

guard(exynos_tmu_data)(data);

Please try that.

author
#9 lore raw

Re: [PATCH v3 4/4] drivers/thermal/exymos: Use guard notation when acquiring mutex

Hi Lukasz,

Quoted 123 lines · Lukasz Luba
Quoted 119 lines · Anand Moon

Using guard notation makes the code more compact and error handling more robust by ensuring that mutexes are released in all code paths when control leaves critical section.

---
v3: new patch

---
  drivers/thermal/samsung/exynos_tmu.c | 21 +++++++--------------
  1 file changed, 7 insertions(+), 14 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +7 -13
index fe090c1a93ab..a34ba3858d64 100644
@@ -256,7 +256,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
      unsigned int status;
      int ret = 0;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
      clk_enable(data->clk_sec);
 
@@ -270,7 +270,6 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
      clk_disable(data->clk_sec);
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return ret;
  }
@@ -292,13 +291,12 @@ static int exynos_thermal_zone_configure(struct platform_device *pdev)
              return ret;
      }
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      data->tmu_set_crit_temp(data, temp / MCELSIUS);
 
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return 0;
  }
@@ -325,12 +323,11 @@ static void exynos_tmu_control(struct platform_device *pdev, bool on)
  {
      struct exynos_tmu_data *data = platform_get_drvdata(pdev);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
      data->tmu_control(pdev, on);
      data->enabled = on;
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
  }
 
  static void exynos_tmu_update_bit(struct exynos_tmu_data *data, int reg_off,
@@ -645,7 +642,7 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
               */
              return -EAGAIN;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      value = data->tmu_read(data);
@@ -655,7 +652,6 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
              *temp = code_to_temp(data, value) * MCELSIUS;
 
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return ret;
  }
@@ -720,11 +716,10 @@ static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
      if (temp && temp < MCELSIUS)
              goto out;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
      data->tmu_set_emulation(data, temp);
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
      return 0;
  out:
      return ret;
@@ -760,14 +755,13 @@ static irqreturn_t exynos_tmu_threaded_irq(int irq, void *id)
 
      thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      /* TODO: take action based on particular interrupt */
      data->tmu_clear_irqs(data);
 
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return IRQ_HANDLED;
  }
@@ -987,7 +981,7 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
  {
      struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      if (low > INT_MIN)
@@ -1000,7 +994,6 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
              data->tmu_disable_high(data);
 
Quoted 5 lines

drivers/thermal/samsung/exynos_tmu.c static int exynos_thermal_zone_configure(struct platform_device *pdev) · line 302

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +0 -1
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return 0;
  }

Thanks for your review comments.

Quoted 4 lines

IMO you should be able to even use something like we have core framework:

Quoted 8 lines

guard(thermal_zone)(tz);

Your mutex name is simply 'lock' in the struct exynos_tmu_data so you should be able to leverage this by:

guard(exynos_tmu_data)(data);

Please try that.

Ok, I will check this

Thanks
-Anand

author
#10 lore raw

Re: [PATCH v3 4/4] drivers/thermal/exymos: Use guard notation when acquiring mutex

Hi Lukasz,

Quoted 127 lines · Anand Moon

Hi Lukasz,

Quoted 123 lines · Lukasz Luba
Quoted 119 lines · Anand Moon

Using guard notation makes the code more compact and error handling more robust by ensuring that mutexes are released in all code paths when control leaves critical section.

---
v3: new patch

---
  drivers/thermal/samsung/exynos_tmu.c | 21 +++++++--------------
  1 file changed, 7 insertions(+), 14 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +7 -13
index fe090c1a93ab..a34ba3858d64 100644
@@ -256,7 +256,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
      unsigned int status;
      int ret = 0;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
      clk_enable(data->clk_sec);
 
@@ -270,7 +270,6 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
      clk_disable(data->clk_sec);
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return ret;
  }
@@ -292,13 +291,12 @@ static int exynos_thermal_zone_configure(struct platform_device *pdev)
              return ret;
      }
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      data->tmu_set_crit_temp(data, temp / MCELSIUS);
 
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return 0;
  }
@@ -325,12 +323,11 @@ static void exynos_tmu_control(struct platform_device *pdev, bool on)
  {
      struct exynos_tmu_data *data = platform_get_drvdata(pdev);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
      data->tmu_control(pdev, on);
      data->enabled = on;
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
  }
 
  static void exynos_tmu_update_bit(struct exynos_tmu_data *data, int reg_off,
@@ -645,7 +642,7 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
               */
              return -EAGAIN;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      value = data->tmu_read(data);
@@ -655,7 +652,6 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
              *temp = code_to_temp(data, value) * MCELSIUS;
 
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return ret;
  }
@@ -720,11 +716,10 @@ static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
      if (temp && temp < MCELSIUS)
              goto out;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
      data->tmu_set_emulation(data, temp);
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
      return 0;
  out:
      return ret;
@@ -760,14 +755,13 @@ static irqreturn_t exynos_tmu_threaded_irq(int irq, void *id)
 
      thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      /* TODO: take action based on particular interrupt */
      data->tmu_clear_irqs(data);
 
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return IRQ_HANDLED;
  }
@@ -987,7 +981,7 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
  {
      struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
      clk_enable(data->clk);
 
      if (low > INT_MIN)
@@ -1000,7 +994,6 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
              data->tmu_disable_high(data);
 
Quoted 18 lines
Quoted 5 lines

drivers/thermal/samsung/exynos_tmu.c static int exynos_thermal_zone_configure(struct platform_device *pdev) · line 302

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +0 -1
      clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
      return 0;
  }

Thanks for your review comments.

Quoted 11 lines

IMO you should be able to even use something like we have core framework:

guard(thermal_zone)(tz);

Your mutex name is simply 'lock' in the struct exynos_tmu_data so you should be able to leverage this by:

guard(exynos_tmu_data)(data);

If I introduce the guard it creates a compilation error

amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ vi
drivers/thermal/samsung/exynos_tmu.c +306
amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ make -j$(nproc)
ARCH=arm CROSS_COMPILE=arm-none-eabi- LOCALVERSION=-u3ml dtbs zImage
modules
  CALL    scripts/checksyscalls.sh
  CHK     kernel/kheaders_data.tar.xz
  CC      drivers/thermal/samsung/exynos_tmu.o
  CC [M]  drivers/md/raid10.o
In file included from ./include/linux/irqflags.h:17,
                 from ./arch/arm/include/asm/bitops.h:28,
                 from ./include/linux/bitops.h:68,
                 from ./include/linux/kernel.h:23,
                 from ./include/linux/clk.h:13,
                 from drivers/thermal/samsung/exynos_tmu.c:14:
drivers/thermal/samsung/exynos_tmu.c: In function 'exynos_tmu_update_bit':
./include/linux/cleanup.h:258:9: error: unknown type name
'class_exynos_tmu_data_t'
  258 |         class_##_name##_t var
__cleanup(class_##_name##_destructor) =   \
      |         ^~~~~~
./include/linux/cleanup.h:309:9: note: in expansion of macro 'CLASS'
  309 |         CLASS(_name, __UNIQUE_ID(guard))
      |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: note: in expansion of macro 'guard'
  338 |         guard(exynos_tmu_data)(data);
      |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: error: cleanup argument
not a function
  CC [M]  drivers/md/raid5.o
./include/linux/cleanup.h:259:17: error: implicit declaration of
function 'class_exynos_tmu_data_constructor'
[-Wimplicit-function-declaration]
  259 |                 class_##_name##_constructor
      |                 ^~~~~~
./include/linux/cleanup.h:309:9: note: in expansion of macro 'CLASS'
  309 |         CLASS(_name, __UNIQUE_ID(guard))
      |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: note: in expansion of macro 'guard'
  338 |         guard(exynos_tmu_data)(data);
      |         ^~~~~
./include/linux/compiler.h:166:45: warning: unused variable
'__UNIQUE_ID_guard572' [-Wunused-variable]
  166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_,
prefix), __COUNTER__)
      |                                             ^~~~~~~~~~~~
./include/linux/cleanup.h:258:27: note: in definition of macro 'CLASS'
  258 |         class_##_name##_t var
__cleanup(class_##_name##_destructor) =   \
      |                           ^~~
././include/linux/compiler_types.h:84:22: note: in expansion of macro '___PASTE'
   84 | #define __PASTE(a,b) ___PASTE(a,b)
      |                      ^~~~~~~~
./include/linux/compiler.h:166:29: note: in expansion of macro '__PASTE'
  166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_,
prefix), __COUNTER__)
      |                             ^~~~~~~
././include/linux/compiler_types.h:84:22: note: in expansion of macro '___PASTE'
   84 | #define __PASTE(a,b) ___PASTE(a,b)
      |                      ^~~~~~~~
./include/linux/compiler.h:166:37: note: in expansion of macro '__PASTE'
  166 | #define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_,
prefix), __COUNTER__)
      |                                     ^~~~~~~
./include/linux/cleanup.h:309:22: note: in expansion of macro '__UNIQUE_ID'
  309 |         CLASS(_name, __UNIQUE_ID(guard))
      |                      ^~~~~~~~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: note: in expansion of macro 'guard'
  338 |         guard(exynos_tmu_data)(data);

Thanks
-Anand

reviewer
#11 lore raw

Re: [PATCH v3 4/4] drivers/thermal/exymos: Use guard notation when acquiring mutex

Quoted 172 lines · Anand Moon

Hi Lukasz,

Quoted 127 lines · Anand Moon

Hi Lukasz,

Quoted 123 lines · Lukasz Luba
Quoted 119 lines · Anand Moon

Using guard notation makes the code more compact and error handling more robust by ensuring that mutexes are released in all code paths when control leaves critical section.

---
v3: new patch

---
   drivers/thermal/samsung/exynos_tmu.c | 21 +++++++--------------
   1 file changed, 7 insertions(+), 14 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +7 -13
index fe090c1a93ab..a34ba3858d64 100644
@@ -256,7 +256,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
       unsigned int status;
       int ret = 0;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
       clk_enable(data->clk_sec);
 
@@ -270,7 +270,6 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
       clk_disable(data->clk_sec);
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return ret;
   }
@@ -292,13 +291,12 @@ static int exynos_thermal_zone_configure(struct platform_device *pdev)
               return ret;
       }
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       data->tmu_set_crit_temp(data, temp / MCELSIUS);
 
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return 0;
   }
@@ -325,12 +323,11 @@ static void exynos_tmu_control(struct platform_device *pdev, bool on)
   {
       struct exynos_tmu_data *data = platform_get_drvdata(pdev);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
       data->tmu_control(pdev, on);
       data->enabled = on;
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
   }
 
   static void exynos_tmu_update_bit(struct exynos_tmu_data *data, int reg_off,
@@ -645,7 +642,7 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
                */
               return -EAGAIN;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       value = data->tmu_read(data);
@@ -655,7 +652,6 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
               *temp = code_to_temp(data, value) * MCELSIUS;
 
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return ret;
   }
@@ -720,11 +716,10 @@ static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
       if (temp && temp < MCELSIUS)
               goto out;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
       data->tmu_set_emulation(data, temp);
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
       return 0;
   out:
       return ret;
@@ -760,14 +755,13 @@ static irqreturn_t exynos_tmu_threaded_irq(int irq, void *id)
 
       thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       /* TODO: take action based on particular interrupt */
       data->tmu_clear_irqs(data);
 
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return IRQ_HANDLED;
   }
@@ -987,7 +981,7 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
   {
       struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       if (low > INT_MIN)
@@ -1000,7 +994,6 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
               data->tmu_disable_high(data);
 
Quoted 18 lines
Quoted 5 lines

drivers/thermal/samsung/exynos_tmu.c static int exynos_thermal_zone_configure(struct platform_device *pdev) · line 302

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +0 -1
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return 0;
   }

Thanks for your review comments.

Quoted 11 lines

IMO you should be able to even use something like we have core framework:

guard(thermal_zone)(tz);

Your mutex name is simply 'lock' in the struct exynos_tmu_data so you should be able to leverage this by:

guard(exynos_tmu_data)(data);

If I introduce the guard it creates a compilation error

amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ vi
drivers/thermal/samsung/exynos_tmu.c +306
amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ make -j$(nproc)
ARCH=arm CROSS_COMPILE=arm-none-eabi- LOCALVERSION=-u3ml dtbs zImage
modules
   CALL    scripts/checksyscalls.sh
   CHK     kernel/kheaders_data.tar.xz
   CC      drivers/thermal/samsung/exynos_tmu.o
   CC [M]  drivers/md/raid10.o
In file included from ./include/linux/irqflags.h:17,
                  from ./arch/arm/include/asm/bitops.h:28,
                  from ./include/linux/bitops.h:68,
                  from ./include/linux/kernel.h:23,
                  from ./include/linux/clk.h:13,
                  from drivers/thermal/samsung/exynos_tmu.c:14:
drivers/thermal/samsung/exynos_tmu.c: In function 'exynos_tmu_update_bit':
./include/linux/cleanup.h:258:9: error: unknown type name
'class_exynos_tmu_data_t'
   258 |         class_##_name##_t var
__cleanup(class_##_name##_destructor) =   \
       |         ^~~~~~
Quoted 8 lines
./include/linux/cleanup.h:309:9: note: in expansion of macro 'CLASS'
   309 |         CLASS(_name, __UNIQUE_ID(guard))
       |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: note: in expansion of macro 'guard'
   338 |         guard(exynos_tmu_data)(data);
       |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: error: cleanup argument
not a function

[snip]

Right, you're missing the definition at the begging, like:

DEFINE_GUARD(exynos_tmu_data, struct exynos_tmu_data *, mutex_lock(&_T->lock),

             mutex_unlock(&_T->lock))

below the struct exynos_tmu_data definition.

Also, make sure you include the cleanup.h (it might not complain, but it would be explicit and more clear)

author
#12 lore raw

Re: [PATCH v3 4/4] drivers/thermal/exymos: Use guard notation when acquiring mutex

Hi Lukasz,

Quoted 189 lines · Lukasz Luba
Quoted 172 lines · Anand Moon

Hi Lukasz,

Quoted 127 lines · Anand Moon

Hi Lukasz,

Quoted 123 lines · Lukasz Luba
Quoted 119 lines · Anand Moon

Using guard notation makes the code more compact and error handling more robust by ensuring that mutexes are released in all code paths when control leaves critical section.

---
v3: new patch

---
   drivers/thermal/samsung/exynos_tmu.c | 21 +++++++--------------
   1 file changed, 7 insertions(+), 14 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +7 -13
index fe090c1a93ab..a34ba3858d64 100644
@@ -256,7 +256,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
       unsigned int status;
       int ret = 0;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
       clk_enable(data->clk_sec);
 
@@ -270,7 +270,6 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
       clk_disable(data->clk_sec);
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return ret;
   }
@@ -292,13 +291,12 @@ static int exynos_thermal_zone_configure(struct platform_device *pdev)
               return ret;
       }
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       data->tmu_set_crit_temp(data, temp / MCELSIUS);
 
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return 0;
   }
@@ -325,12 +323,11 @@ static void exynos_tmu_control(struct platform_device *pdev, bool on)
   {
       struct exynos_tmu_data *data = platform_get_drvdata(pdev);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
       data->tmu_control(pdev, on);
       data->enabled = on;
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
   }
 
   static void exynos_tmu_update_bit(struct exynos_tmu_data *data, int reg_off,
@@ -645,7 +642,7 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
                */
               return -EAGAIN;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       value = data->tmu_read(data);
@@ -655,7 +652,6 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
               *temp = code_to_temp(data, value) * MCELSIUS;
 
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return ret;
   }
@@ -720,11 +716,10 @@ static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
       if (temp && temp < MCELSIUS)
               goto out;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
       data->tmu_set_emulation(data, temp);
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
       return 0;
   out:
       return ret;
@@ -760,14 +755,13 @@ static irqreturn_t exynos_tmu_threaded_irq(int irq, void *id)
 
       thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       /* TODO: take action based on particular interrupt */
       data->tmu_clear_irqs(data);
 
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return IRQ_HANDLED;
   }
@@ -987,7 +981,7 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
   {
       struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
       clk_enable(data->clk);
 
       if (low > INT_MIN)
@@ -1000,7 +994,6 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
               data->tmu_disable_high(data);
 
Quoted 18 lines
Quoted 5 lines

drivers/thermal/samsung/exynos_tmu.c static int exynos_thermal_zone_configure(struct platform_device *pdev) · line 302

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +0 -1
       clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
       return 0;
   }

Thanks for your review comments.

Quoted 11 lines

IMO you should be able to even use something like we have core framework:

guard(thermal_zone)(tz);

Your mutex name is simply 'lock' in the struct exynos_tmu_data so you should be able to leverage this by:

guard(exynos_tmu_data)(data);

If I introduce the guard it creates a compilation error

amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ vi
drivers/thermal/samsung/exynos_tmu.c +306
amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ make -j$(nproc)
ARCH=arm CROSS_COMPILE=arm-none-eabi- LOCALVERSION=-u3ml dtbs zImage
modules
   CALL    scripts/checksyscalls.sh
   CHK     kernel/kheaders_data.tar.xz
   CC      drivers/thermal/samsung/exynos_tmu.o
   CC [M]  drivers/md/raid10.o
In file included from ./include/linux/irqflags.h:17,
                  from ./arch/arm/include/asm/bitops.h:28,
                  from ./include/linux/bitops.h:68,
                  from ./include/linux/kernel.h:23,
                  from ./include/linux/clk.h:13,
                  from drivers/thermal/samsung/exynos_tmu.c:14:
drivers/thermal/samsung/exynos_tmu.c: In function 'exynos_tmu_update_bit':
./include/linux/cleanup.h:258:9: error: unknown type name
'class_exynos_tmu_data_t'
   258 |         class_##_name##_t var
__cleanup(class_##_name##_destructor) =   \
       |         ^~~~~~
Quoted 8 lines
./include/linux/cleanup.h:309:9: note: in expansion of macro 'CLASS'
   309 |         CLASS(_name, __UNIQUE_ID(guard))
       |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: note: in expansion of macro 'guard'
   338 |         guard(exynos_tmu_data)(data);
       |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: error: cleanup argument
not a function

[snip]

Right, you're missing the definition at the begging, like:

Quoted 8 lines

DEFINE_GUARD(exynos_tmu_data, struct exynos_tmu_data *, mutex_lock(&_T->lock),

              mutex_unlock(&_T->lock))

below the struct exynos_tmu_data definition.

Also, make sure you include the cleanup.h (it might not complain, but it would be explicit and more clear)

Thanks for this tip.
However, incorporating guard(exynos_tmu_data)(data); results in a recursive deadlock with the mutex during initialization, as this data structure is common to all the code configurations of Exynos TMU

Thanks
-Anand

reviewer
#13 lore raw

Re: [PATCH v3 4/4] drivers/thermal/exymos: Use guard notation when acquiring mutex

Quoted 198 lines · Anand Moon

Hi Lukasz,

Quoted 195 lines · Lukasz Luba
Quoted 172 lines · Anand Moon

Hi Lukasz,

Quoted 127 lines · Anand Moon

Hi Lukasz,

Quoted 123 lines · Lukasz Luba
Quoted 119 lines · Anand Moon

Using guard notation makes the code more compact and error handling more robust by ensuring that mutexes are released in all code paths when control leaves critical section.

---
v3: new patch

---
    drivers/thermal/samsung/exynos_tmu.c | 21 +++++++--------------
    1 file changed, 7 insertions(+), 14 deletions(-)
drivers/thermal/samsung/exynos_tmu.c +7 -13
index fe090c1a93ab..a34ba3858d64 100644
@@ -256,7 +256,7 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
        unsigned int status;
        int ret = 0;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
        clk_enable(data->clk);
        clk_enable(data->clk_sec);
 
@@ -270,7 +270,6 @@ static int exynos_tmu_initialize(struct platform_device *pdev)
 
        clk_disable(data->clk_sec);
        clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
        return ret;
    }
@@ -292,13 +291,12 @@ static int exynos_thermal_zone_configure(struct platform_device *pdev)
                return ret;
        }
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
        clk_enable(data->clk);
 
        data->tmu_set_crit_temp(data, temp / MCELSIUS);
 
        clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
        return 0;
    }
@@ -325,12 +323,11 @@ static void exynos_tmu_control(struct platform_device *pdev, bool on)
    {
        struct exynos_tmu_data *data = platform_get_drvdata(pdev);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
        clk_enable(data->clk);
        data->tmu_control(pdev, on);
        data->enabled = on;
        clk_disable(data->clk);
-     mutex_unlock(&data->lock);
    }
 
    static void exynos_tmu_update_bit(struct exynos_tmu_data *data, int reg_off,
@@ -645,7 +642,7 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
                 */
                return -EAGAIN;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
        clk_enable(data->clk);
 
        value = data->tmu_read(data);
@@ -655,7 +652,6 @@ static int exynos_get_temp(struct thermal_zone_device *tz, int *temp)
                *temp = code_to_temp(data, value) * MCELSIUS;
 
        clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
        return ret;
    }
@@ -720,11 +716,10 @@ static int exynos_tmu_set_emulation(struct thermal_zone_device *tz, int temp)
        if (temp && temp < MCELSIUS)
                goto out;
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
        clk_enable(data->clk);
        data->tmu_set_emulation(data, temp);
        clk_disable(data->clk);
-     mutex_unlock(&data->lock);
        return 0;
    out:
        return ret;
@@ -760,14 +755,13 @@ static irqreturn_t exynos_tmu_threaded_irq(int irq, void *id)
 
        thermal_zone_device_update(data->tzd, THERMAL_EVENT_UNSPECIFIED);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
        clk_enable(data->clk);
 
        /* TODO: take action based on particular interrupt */
        data->tmu_clear_irqs(data);
 
        clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
        return IRQ_HANDLED;
    }
@@ -987,7 +981,7 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
    {
        struct exynos_tmu_data *data = thermal_zone_device_priv(tz);
 
-     mutex_lock(&data->lock);
+     guard(mutex)(&data->lock);
        clk_enable(data->clk);
 
        if (low > INT_MIN)
@@ -1000,7 +994,6 @@ static int exynos_set_trips(struct thermal_zone_device *tz, int low, int high)
                data->tmu_disable_high(data);
 
Quoted 18 lines
Quoted 5 lines

drivers/thermal/samsung/exynos_tmu.c static int exynos_thermal_zone_configure(struct platform_device *pdev) · line 302

Quoted 5 lines
drivers/thermal/samsung/exynos_tmu.c +0 -1
        clk_disable(data->clk);
-     mutex_unlock(&data->lock);
 
        return 0;
    }

Thanks for your review comments.

Quoted 11 lines

IMO you should be able to even use something like we have core framework:

guard(thermal_zone)(tz);

Your mutex name is simply 'lock' in the struct exynos_tmu_data so you should be able to leverage this by:

guard(exynos_tmu_data)(data);

If I introduce the guard it creates a compilation error

amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ vi
drivers/thermal/samsung/exynos_tmu.c +306
amoon@anand-m920q:~/mainline/linux-exynos-6.y-devel$ make -j$(nproc)
ARCH=arm CROSS_COMPILE=arm-none-eabi- LOCALVERSION=-u3ml dtbs zImage
modules
    CALL    scripts/checksyscalls.sh
    CHK     kernel/kheaders_data.tar.xz
    CC      drivers/thermal/samsung/exynos_tmu.o
    CC [M]  drivers/md/raid10.o
In file included from ./include/linux/irqflags.h:17,
                   from ./arch/arm/include/asm/bitops.h:28,
                   from ./include/linux/bitops.h:68,
                   from ./include/linux/kernel.h:23,
                   from ./include/linux/clk.h:13,
                   from drivers/thermal/samsung/exynos_tmu.c:14:
drivers/thermal/samsung/exynos_tmu.c: In function 'exynos_tmu_update_bit':
./include/linux/cleanup.h:258:9: error: unknown type name
'class_exynos_tmu_data_t'
    258 |         class_##_name##_t var
__cleanup(class_##_name##_destructor) =   \
        |         ^~~~~~
Quoted 8 lines
./include/linux/cleanup.h:309:9: note: in expansion of macro 'CLASS'
    309 |         CLASS(_name, __UNIQUE_ID(guard))
        |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: note: in expansion of macro 'guard'
    338 |         guard(exynos_tmu_data)(data);
        |         ^~~~~
drivers/thermal/samsung/exynos_tmu.c:338:9: error: cleanup argument
not a function

[snip]

Right, you're missing the definition at the begging, like:

DEFINE_GUARD(exynos_tmu_data, struct exynos_tmu_data *, mutex_lock(&_T->lock),

               mutex_unlock(&_T->lock))

below the struct exynos_tmu_data definition.

Quoted 8 lines
Quoted 2 lines

Also, make sure you include the cleanup.h (it might not complain, but it would be explicit and more clear)

Thanks for this tip.
However, incorporating guard(exynos_tmu_data)(data); results in a recursive deadlock with the mutex during initialization, as this data structure is common to all the code configurations of Exynos TMU

Fair enough, it would be just a cosmetic change, so do fight with it too much.

Please continue in v4 with your former approach with the 'guard'.