mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "Liang, Kan" <kan.liang@linux.intel.com>
To: Liao Yuanhong <liaoyuanhong@vivo.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	"maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)"
	<x86@kernel.org>, "H. Peter Anvin" <hpa@zytor.com>,
	"open list:PERFORMANCE EVENTS SUBSYSTEM"
	<linux-perf-users@vger.kernel.org>,
	"open list:PERFORMANCE EVENTS SUBSYSTEM"
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] perf/x86: Use guard(mutex) instead of mutex_lock() to simplify code
Date: Tue, 2 Sep 2025 09:30:07 -0700	[thread overview]
Message-ID: <3599c0c7-d385-45fa-9ffa-f20737165827@linux.intel.com> (raw)
In-Reply-To: <20250829114816.650576-1-liaoyuanhong@vivo.com>



On 2025-08-29 4:48 a.m., Liao Yuanhong wrote:
> Using guard(mutex) and scoped_guard() instead of mutex_lock/mutex_unlock
> pair. Simplifies the error handling to just return in case of error. No
> need for the fail_unlock: label anymore so remove it.
> 
> Signed-off-by: Liao Yuanhong <liaoyuanhong@vivo.com>
> ---
>  arch/x86/events/core.c | 21 +++++++--------------
>  1 file changed, 7 insertions(+), 14 deletions(-)
> 
> diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> index 745caa6c15a3..107bed5c9d71 100644
> --- a/arch/x86/events/core.c
> +++ b/arch/x86/events/core.c
> @@ -411,7 +411,7 @@ int x86_reserve_hardware(void)
>  	int err = 0;
>  
>  	if (!atomic_inc_not_zero(&pmc_refcount)) {
> -		mutex_lock(&pmc_reserve_mutex);
> +		guard(mutex)(&pmc_reserve_mutex);

Shouldn't it be a scoped_guard() as well?

Thanks,
Kan

>  		if (atomic_read(&pmc_refcount) == 0) {
>  			if (!reserve_pmc_hardware()) {
>  				err = -EBUSY;
> @@ -422,7 +422,6 @@ int x86_reserve_hardware(void)
>  		}
>  		if (!err)
>  			atomic_inc(&pmc_refcount);
> -		mutex_unlock(&pmc_reserve_mutex);
>  	}
>  
>  	return err;
> @@ -444,8 +443,6 @@ void x86_release_hardware(void)
>   */
>  int x86_add_exclusive(unsigned int what)
>  {
> -	int i;
> -
>  	/*
>  	 * When lbr_pt_coexist we allow PT to coexist with either LBR or BTS.
>  	 * LBR and BTS are still mutually exclusive.
> @@ -454,22 +451,18 @@ int x86_add_exclusive(unsigned int what)
>  		goto out;
>  
>  	if (!atomic_inc_not_zero(&x86_pmu.lbr_exclusive[what])) {
> -		mutex_lock(&pmc_reserve_mutex);
> -		for (i = 0; i < ARRAY_SIZE(x86_pmu.lbr_exclusive); i++) {
> -			if (i != what && atomic_read(&x86_pmu.lbr_exclusive[i]))
> -				goto fail_unlock;
> +		scoped_guard(mutex, &pmc_reserve_mutex) {
> +			for (int i = 0; i < ARRAY_SIZE(x86_pmu.lbr_exclusive); i++) {
> +				if (i != what && atomic_read(&x86_pmu.lbr_exclusive[i]))
> +					return -EBUSY;
> +			}
> +			atomic_inc(&x86_pmu.lbr_exclusive[what]);
>  		}
> -		atomic_inc(&x86_pmu.lbr_exclusive[what]);
> -		mutex_unlock(&pmc_reserve_mutex);
>  	}
>  
>  out:
>  	atomic_inc(&active_events);
>  	return 0;
> -
> -fail_unlock:
> -	mutex_unlock(&pmc_reserve_mutex);
> -	return -EBUSY;
>  }
>  
>  void x86_del_exclusive(unsigned int what)


      reply	other threads:[~2025-09-02 16:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-29 11:48 Liao Yuanhong
2025-09-02 16:30 ` Liang, Kan [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3599c0c7-d385-45fa-9ffa-f20737165827@linux.intel.com \
    --to=kan.liang@linux.intel.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=hpa@zytor.com \
    --cc=irogers@google.com \
    --cc=jolsa@kernel.org \
    --cc=liaoyuanhong@vivo.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®