[PATCH] perf/x86/core: Simplify hybrid PMU registration failure path

Thorsten Blum posted 1 patch 3 weeks ago
arch/x86/events/core.c | 18 ++++++++----------
1 file changed, 8 insertions(+), 10 deletions(-)
[PATCH] perf/x86/core: Simplify hybrid PMU registration failure path
Posted by Thorsten Blum 3 weeks ago
Handle perf_pmu_register() errors directly instead of breaking out of
the registration loop and checking the index separately. Reuse the loop
index i to unregister any previously registered PMUs in reverse order
and drop the extra index j.

Also log the failing PMU's name and error code.

Signed-off-by: Thorsten Blum <blum@kernel.org>
---
 arch/x86/events/core.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 8eb14add44c8..0a8fcfd52ad0 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -2256,7 +2256,7 @@ static int __init init_hw_perf_events(void)
 			goto out2;
 	} else {
 		struct x86_hybrid_pmu *hybrid_pmu;
-		int i, j;
+		int i;
 
 		for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) {
 			hybrid_pmu = &x86_pmu.hybrid_pmu[i];
@@ -2268,15 +2268,13 @@ static int __init init_hw_perf_events(void)
 
 			err = perf_pmu_register(&hybrid_pmu->pmu, hybrid_pmu->name,
 						(hybrid_pmu->pmu_type == hybrid_big) ? PERF_TYPE_RAW : -1);
-			if (err)
-				break;
-		}
-
-		if (i < x86_pmu.num_hybrid_pmus) {
-			for (j = 0; j < i; j++)
-				perf_pmu_unregister(&x86_pmu.hybrid_pmu[j].pmu);
-			pr_warn("Failed to register hybrid PMUs\n");
-			goto out2;
+			if (err) {
+				while (i--)
+					perf_pmu_unregister(&x86_pmu.hybrid_pmu[i].pmu);
+				pr_warn("Failed to register hybrid PMU %s: %d\n",
+					hybrid_pmu->name, err);
+				goto out2;
+			}
 		}
 	}
Re: [PATCH] perf/x86/core: Simplify hybrid PMU registration failure path
Posted by Mi, Dapeng 2 weeks, 3 days ago
LGTM. Thanks.

Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>


On 9/5/2026 4:13 AM, Thorsten Blum wrote:
> Handle perf_pmu_register() errors directly instead of breaking out of
> the registration loop and checking the index separately. Reuse the loop
> index i to unregister any previously registered PMUs in reverse order
> and drop the extra index j.
>
> Also log the failing PMU's name and error code.
>
> Signed-off-by: Thorsten Blum <blum@kernel.org>
> ---
>  arch/x86/events/core.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> index 8eb14add44c8..0a8fcfd52ad0 100644
> --- a/arch/x86/events/core.c
> +++ b/arch/x86/events/core.c
> @@ -2256,7 +2256,7 @@ static int __init init_hw_perf_events(void)
>  			goto out2;
>  	} else {
>  		struct x86_hybrid_pmu *hybrid_pmu;
> -		int i, j;
> +		int i;
>  
>  		for (i = 0; i < x86_pmu.num_hybrid_pmus; i++) {
>  			hybrid_pmu = &x86_pmu.hybrid_pmu[i];
> @@ -2268,15 +2268,13 @@ static int __init init_hw_perf_events(void)
>  
>  			err = perf_pmu_register(&hybrid_pmu->pmu, hybrid_pmu->name,
>  						(hybrid_pmu->pmu_type == hybrid_big) ? PERF_TYPE_RAW : -1);
> -			if (err)
> -				break;
> -		}
> -
> -		if (i < x86_pmu.num_hybrid_pmus) {
> -			for (j = 0; j < i; j++)
> -				perf_pmu_unregister(&x86_pmu.hybrid_pmu[j].pmu);
> -			pr_warn("Failed to register hybrid PMUs\n");
> -			goto out2;
> +			if (err) {
> +				while (i--)
> +					perf_pmu_unregister(&x86_pmu.hybrid_pmu[i].pmu);
> +				pr_warn("Failed to register hybrid PMU %s: %d\n",
> +					hybrid_pmu->name, err);
> +				goto out2;
> +			}
>  		}
>  	}
>