[PATCH] perf/x86/intel: Use guard() instead of mutex_lock() to simplify code

Liao Yuanhong posted 1 patch 3 months, 1 week ago
arch/x86/events/intel/core.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
[PATCH] perf/x86/intel: Use guard() instead of mutex_lock() to simplify code
Posted by Liao Yuanhong 3 months, 1 week ago
Using guard(mutex) instead of mutex_lock/mutex_unlock pair. Simplifies the
error handling to just return in case of error. No need for the 'done'
label anymore so remove it.

Signed-off-by: Liao Yuanhong <liaoyuanhong@vivo.com>
---
 arch/x86/events/intel/core.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 28f5468a6ea3..ac88d9535e85 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -6107,18 +6107,16 @@ static ssize_t freeze_on_smi_store(struct device *cdev,
 	if (val > 1)
 		return -EINVAL;
 
-	mutex_lock(&freeze_on_smi_mutex);
+	guard(mutex)(&freeze_on_smi_mutex);
 
 	if (x86_pmu.attr_freeze_on_smi == val)
-		goto done;
+		return count;
 
 	x86_pmu.attr_freeze_on_smi = val;
 
 	cpus_read_lock();
 	on_each_cpu(flip_smm_bit, &val, 1);
 	cpus_read_unlock();
-done:
-	mutex_unlock(&freeze_on_smi_mutex);
 
 	return count;
 }
-- 
2.34.1
Re: [PATCH] perf/x86/intel: Use guard() instead of mutex_lock() to simplify code
Posted by Markus Elfring 3 months, 1 week ago
> Using guard(mutex) instead of mutex_lock/mutex_unlock pair. Simplifies the
…
  Use?                          Parentheses behind identifiers?


…
> +++ b/arch/x86/events/intel/core.c
> @@ -6107,18 +6107,16 @@ static ssize_t freeze_on_smi_store(struct device *cdev,
…
>  	cpus_read_lock();
>  	on_each_cpu(flip_smm_bit, &val, 1);
>  	cpus_read_unlock();
> -done:
> -	mutex_unlock(&freeze_on_smi_mutex);
…

How do you think about to apply another lock guard by a subsequent update step?
https://elixir.bootlin.com/linux/v6.17-rc4/source/include/linux/cpuhplock.h#L47

Regards,
Markus
Re: [PATCH] perf/x86/intel: Use guard() instead of mutex_lock() to simplify code
Posted by Liang, Kan 3 months, 1 week ago

On 2025-09-01 6:17 a.m., Liao Yuanhong wrote:
> Using guard(mutex) instead of mutex_lock/mutex_unlock pair. Simplifies the
> error handling to just return in case of error. No need for the 'done'
> label anymore so remove it.
> 
> Signed-off-by: Liao Yuanhong <liaoyuanhong@vivo.com>

Reviewed-by: Kan Liang <kan.liang@linux.intel.com>

Thanks,
Kan

> ---
>  arch/x86/events/intel/core.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
> index 28f5468a6ea3..ac88d9535e85 100644
> --- a/arch/x86/events/intel/core.c
> +++ b/arch/x86/events/intel/core.c
> @@ -6107,18 +6107,16 @@ static ssize_t freeze_on_smi_store(struct device *cdev,
>  	if (val > 1)
>  		return -EINVAL;
>  
> -	mutex_lock(&freeze_on_smi_mutex);
> +	guard(mutex)(&freeze_on_smi_mutex);
>  
>  	if (x86_pmu.attr_freeze_on_smi == val)
> -		goto done;
> +		return count;
>  
>  	x86_pmu.attr_freeze_on_smi = val;
>  
>  	cpus_read_lock();
>  	on_each_cpu(flip_smm_bit, &val, 1);
>  	cpus_read_unlock();
> -done:
> -	mutex_unlock(&freeze_on_smi_mutex);
>  
>  	return count;
>  }