powerpc/perf: vpa-dtl: fix resource leak on allocation failure
PATCH 1/1 powerpc/perf: vpa-dtl: fix resource leak on allocation failure
From: Mohammad Shahid <[email protected]>
vpa_dtl_event_init acquires dtl_access_lock when initializing the first vpa_dtl PMU event.
If vpa_dtl_mem_alloc() fails,the function returns -ENOMEM without releasing the semaphore.
Release the semaphore when buffer allocation fails and no other PMU events are active.
--- arch/powerpc/perf/vpa-dtl.c | 2 ++ 1 file changed, 2 insertions(+)
arch/powerpc/perf/vpa-dtl.c +2 -0
@@ -396,6 +396,8 @@ static int vpa_dtl_event_init(struct perf_event *event)/* Allocate dtl buffer memory */ if (vpa_dtl_mem_alloc (event->cpu)) { +if (dtl_global_refc ==0 ) +up_write (&dtl_access_lock);spin_unlock (&dtl_global_lock);return -ENOMEM ; }
Signature · 1 line
2.43.0
Hi,
Could you please check this patch and give your feedback,when time permits.
Thanks
Shahid.
Quoted 34 lines · On 2026-11-12 19:14 +0530, [email protected] wrote:
From: Mohammad Shahid <[email protected]>
vpa_dtl_event_initacquiresdtl_access_lockwhen initializing the firstvpa_dtlPMU event.If
vpa_dtl_mem_alloc()fails,the function returns-ENOMEMwithout releasing the semaphore.Release the semaphore when buffer allocation fails and no other PMU events are active.
Signed-off-by Mohammad Shahid <[email protected]>--- arch/powerpc/perf/vpa-dtl.c | 2 ++ 1 file changed, 2 insertions(+)arch/powerpc/perf/vpa-dtl.c +2
-0@@ -396,6 +396,8 @@ static int vpa_dtl_event_init(struct perf_event *event)/* Allocate dtl buffer memory */ if (vpa_dtl_mem_alloc (event->cpu)) { +if (dtl_global_refc ==0 ) +up_write (&dtl_access_lock);spin_unlock (&dtl_global_lock);return -ENOMEM ; }Signature · 1 line
2.43.0
Ritesh Harjani (IBM)
reviewerQuoted 21 lines · [email protected] writes:
From: Mohammad Shahid <[email protected]>
vpa_dtl_event_initacquiresdtl_access_lockwhen initializing the firstvpa_dtlPMU event.If
vpa_dtl_mem_alloc()fails,the function returns-ENOMEMwithout releasing the semaphore.Release the semaphore when buffer allocation fails and no other PMU events are active.
Signed-off-by Mohammad Shahid <[email protected]>--- arch/powerpc/perf/vpa-dtl.c | 2 ++ 1 file changed, 2 insertions(+)arch/powerpc/perf/vpa-dtl.c +0
-0
arch/powerpc/perf/vpa-dtl.c static int vpa_dtl_event_init(struct perf_event *event) · line 403
Quoted 8 lines
arch/powerpc/perf/vpa-dtl.c +2
-0@@ -396,6 +396,8 @@ static int vpa_dtl_event_init(struct perf_event *event)/* Allocate dtl buffer memory */ if (vpa_dtl_mem_alloc (event->cpu)) { +if (dtl_global_refc ==0 ) +up_write (&dtl_access_lock);spin_unlock (&dtl_global_lock);return -ENOMEM ; }
I looked at this code and I am unable to wrap my head around it. We have dtl_access_lock and dtl_global_lock. Then we have vpa_dtl, which is a per-cpu variable anyway.
So I am not sure why we need both locks held while doing vpa_dtl_mem_alloc(). What exactly are they protecting? Can we move the allocation out of the lock and just assign dtl->buf later within the lock? In one other place I see dtl->buf = NULL without any lock. Also, do we need a spinlock to protect dtl_global_refc? Would an atomic variable not work here?
The above fix is not wrong per se, based on today's code, but it would be better if someone who owns this code takes another look and fixes its design while we are at it.
Another motivation for doing this work now is to bring in clang's context analysis for arch/powerpc/perf/, which can statically check that such locking rules are followed, provided there are proper annotations for every lock and the data it protects.
-ritesh
Quoted 49 lines · Ritesh Harjani (IBM)
Quoted 21 lines · [email protected] writes:
From: Mohammad Shahid <[email protected]>
vpa_dtl_event_initacquiresdtl_access_lockwhen initializing the firstvpa_dtlPMU event.If
vpa_dtl_mem_alloc()fails,the function returns-ENOMEMwithout releasing the semaphore.Release the semaphore when buffer allocation fails and no other PMU events are active.
Signed-off-by Mohammad Shahid <[email protected]>---
arch/powerpc/perf/vpa-dtl.c| 2 ++1 file changed, 2 insertions(+)arch/powerpc/perf/vpa-dtl.c +0
-0arch/powerpc/perf/vpa-dtl.c static int vpa_dtl_event_init(struct perf_event *event) · line 403
Quoted 8 lines
arch/powerpc/perf/vpa-dtl.c +2
-0@@ -396,6 +396,8 @@ static int vpa_dtl_event_init(struct perf_event *event)/* Allocate dtl buffer memory */ if (vpa_dtl_mem_alloc (event->cpu)) { +if (dtl_global_refc ==0 ) +up_write (&dtl_access_lock);spin_unlock (&dtl_global_lock);return -ENOMEM ; }I looked at this code and I am unable to wrap my head around it. We have
dtl_access_lockanddtl_global_lock. Then we havevpa_dtl, which is a per-cpu variable anyway.So I am not sure why we need both locks held while doing
vpa_dtl_mem_alloc(). What exactly are they protecting? Can we move the allocation out of the lock and just assigndtl->buflater within the lock? In one other place I seedtl->buf= NULL without any lock. Also, do we need a spinlock to protectdtl_global_refc? Would an atomic variable not work here?
Quoted 3 lines
The above fix is not wrong per se, based on today's code, but it would be better if someone who owns this code takes another look and fixes its design while we are at it.
Thanks Mohammad for the patch and Ritesh for sharing your comments.
Reasons for having the different locks:dtl_access_lock with down_write_trylock : There are other dtl users, example: debugfs dtl
or /proc/powerpc/vcpudispatch_stats. These locks are used to ensure at same time these
interfaces are not used, ie to exclude the debugfs dtl and vcpudispatch_stats users.
dtl_global_refc : global reference count variable. One perf record opens one event per CPU, and all of them share one acquisition of the lock. The count says when the last CPU event is gone and the lock can be released. And dtl_global_lock is used to prevent race condition on this.
But the allocation observation which Ritesh pointed out seems valid to me. It can be moved out of dtl_global_lock.
I am looking closer at the other points raised here, including the buffer lifetime and whether the spinlock/refcount can be simplified, and will respond back soon.
Thanks
Athira
Quoted 8 lines
Another motivation for doing this work now is to bring in clang's context analysis for
arch/powerpc/perf/, which can statically check that such locking rules are followed, provided there are proper annotations for every lock and the data it protects.-ritesh