Re: [PATCHv3 4/4] powerpc/kvm-hv-pmu: Free hostwide stats buffers if perf_pmu_register() fails

From: IBM

Date: Wed Nov 25 2026 - 04:58:44 EST


Mukesh Kumar Chaurasiya <mkchauras@xxxxxxxxx> writes:

> On Mon, Nov 23, 2026 at 07:21:53PM +0530, Ritesh Harjani (IBM) wrote:
>> kvmppc_register_pmu() calls kvmppc_init_hostwide(), which allocates
>> gsm_l0_stats and gsb_l0_stats. It then registers the pmu. If
>> perf_pmu_register() fails we return without freeing them.
>>
>> Call kvmppc_cleanup_hostwide() on that error path.
>>
>> Fixes: f0c9c49c5067 ("powerpc/kvm-hv-pmu: Implement GSB message-ops for hostwide counters")
>> Cc: stable@xxxxxxxxxxxxxxx
>> Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@xxxxxxxxx>
>> ---
>> arch/powerpc/perf/kvm-hv-pmu.c | 4 +++-
>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/powerpc/perf/kvm-hv-pmu.c b/arch/powerpc/perf/kvm-hv-pmu.c
>> index a58634f656e9..ec05b4855736 100644
>> --- a/arch/powerpc/perf/kvm-hv-pmu.c
>> +++ b/arch/powerpc/perf/kvm-hv-pmu.c
>> @@ -402,8 +402,10 @@ static int __init kvmppc_register_pmu(void)
>>
>> /* Register the pmu */
>> rc = perf_pmu_register(&kvmppc_pmu, kvmppc_pmu.name, -1);
>> - if (rc)
>> + if (rc) {
>> + kvmppc_cleanup_hostwide();
> Just a question.
> Inside this function, should we also take lock just to set the pointers
> NULL rather than putting the whole logic(free also) inside the lock like
> we did in previous patch?
>

In the previous patch, we brought GFP_KERNEL (sleepable) allocation
outside of atomic context. However, in here, there is no harm in keeping
the lock to assign the null values to pointers. But I guess what you are
suggesting is - Do we need to keep the lock around in the cleanup? Looks
like no, we don't, since if the perf pmu register failed then we won't
get a perf pmu update event for this - which is the only other user of
this lock which can race.

However, this patch is mostly an error handling fix (reported as a bug
by Sashiko). One can pickup the cleanup of that path as a separate
patch. There is also a powerpc wide clang's context analysis work that
is going on, so as part of that - we could maybe optimize away taking of
the lock in kvmppc_cleanup_hostwide() as well. As for this patch series,
I think let's go with this, as most of the patches in the series are
with Fixes tag to be backported.


> Apart from this, all looks good.
>
> Reviewed-by: Mukesh Kumar Chaurasiya (IBM) <mkchauras@xxxxxxxxx>

Thanks for the review!