* [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure @ 2026-11-12 13:44 mdshahid03 2026-11-18 3:56 ` mdshahid03 2026-11-18 11:32 ` Ritesh Harjani 0 siblings, 2 replies; 5+ messages in thread From: mdshahid03 @ 2026-11-12 13:44 UTC (permalink / raw) To: maddy Cc: mpe, npiggin, chleroy, ritesh.list, sshegde, linuxppc-dev, linux-kernel, Mohammad Shahid From: Mohammad Shahid <mdshahid03@gmail.com> 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. Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com> --- arch/powerpc/perf/vpa-dtl.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/arch/powerpc/perf/vpa-dtl.c b/arch/powerpc/perf/vpa-dtl.c index 3e3d65b6c796..20eafeaffa6b 100644 --- a/arch/powerpc/perf/vpa-dtl.c +++ b/arch/powerpc/perf/vpa-dtl.c @@ -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; } -- 2.43.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure 2026-11-12 13:44 [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure mdshahid03 @ 2026-11-18 3:56 ` mdshahid03 2026-11-18 11:32 ` Ritesh Harjani 1 sibling, 0 replies; 5+ messages in thread From: mdshahid03 @ 2026-11-18 3:56 UTC (permalink / raw) To: mdshahid03 Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, linuxppc-dev, linux-kernel Hi, Could you please check this patch and give your feedback,when time permits. Thanks Shahid. On 2026-11-12 19:14 +0530, mdshahid03@gmail.com wrote: > From: Mohammad Shahid <mdshahid03@gmail.com> > > 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. > > Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com> > --- > arch/powerpc/perf/vpa-dtl.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/arch/powerpc/perf/vpa-dtl.c b/arch/powerpc/perf/vpa-dtl.c > index 3e3d65b6c796..20eafeaffa6b 100644 > --- a/arch/powerpc/perf/vpa-dtl.c > +++ b/arch/powerpc/perf/vpa-dtl.c > @@ -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; > } > -- > 2.43.0 > > > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure 2026-11-12 13:44 [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure mdshahid03 2026-11-18 3:56 ` mdshahid03 @ 2026-11-18 11:32 ` Ritesh Harjani 2026-11-22 16:17 ` Athira Rajeev 1 sibling, 1 reply; 5+ messages in thread From: Ritesh Harjani @ 2026-11-18 11:32 UTC (permalink / raw) To: mdshahid03, maddy Cc: mpe, npiggin, chleroy, sshegde, linuxppc-dev, linux-kernel, Mohammad Shahid, Athira Rajeev, Tanushree Shah mdshahid03@gmail.com writes: > From: Mohammad Shahid <mdshahid03@gmail.com> > > 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. > > Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com> > --- > arch/powerpc/perf/vpa-dtl.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/arch/powerpc/perf/vpa-dtl.c b/arch/powerpc/perf/vpa-dtl.c > index 3e3d65b6c796..20eafeaffa6b 100644 > --- a/arch/powerpc/perf/vpa-dtl.c > +++ b/arch/powerpc/perf/vpa-dtl.c > @@ -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 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure 2026-11-18 11:32 ` Ritesh Harjani @ 2026-11-22 16:17 ` Athira Rajeev 2026-11-25 16:03 ` Athira Rajeev 0 siblings, 1 reply; 5+ messages in thread From: Athira Rajeev @ 2026-11-22 16:17 UTC (permalink / raw) To: Ritesh Harjani (IBM), mdshahid03 Cc: maddy, mpe, npiggin, chleroy, sshegde, linuxppc-dev, linux-kernel, Tanushree Shah > On 18 Nov 2026, at 5:02 PM, Ritesh Harjani (IBM) <ritesh.list@gmail.com> wrote: > > > mdshahid03@gmail.com writes: > >> From: Mohammad Shahid <mdshahid03@gmail.com> >> >> 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. >> >> Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com> >> --- >> arch/powerpc/perf/vpa-dtl.c | 2 ++ >> 1 file changed, 2 insertions(+) >> >> diff --git a/arch/powerpc/perf/vpa-dtl.c b/arch/powerpc/perf/vpa-dtl.c >> index 3e3d65b6c796..20eafeaffa6b 100644 >> --- a/arch/powerpc/perf/vpa-dtl.c >> +++ b/arch/powerpc/perf/vpa-dtl.c >> @@ -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. 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 > > 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 > ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure 2026-11-22 16:17 ` Athira Rajeev @ 2026-11-25 16:03 ` Athira Rajeev 0 siblings, 0 replies; 5+ messages in thread From: Athira Rajeev @ 2026-11-25 16:03 UTC (permalink / raw) To: Ritesh Harjani (IBM), mdshahid03 Cc: maddy, mpe, npiggin, chleroy, sshegde, linuxppc-dev, linux-kernel, Tanushree Shah > On 22 Nov 2026, at 9:47 PM, Athira Rajeev <atrajeev@linux.ibm.com> wrote: > > > >> On 18 Nov 2026, at 5:02 PM, Ritesh Harjani (IBM) <ritesh.list@gmail.com> wrote: >> >> >> mdshahid03@gmail.com writes: >> >>> From: Mohammad Shahid <mdshahid03@gmail.com> >>> >>> 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. >>> >>> Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com> >>> --- >>> arch/powerpc/perf/vpa-dtl.c | 2 ++ >>> 1 file changed, 2 insertions(+) >>> >>> diff --git a/arch/powerpc/perf/vpa-dtl.c b/arch/powerpc/perf/vpa-dtl.c >>> index 3e3d65b6c796..20eafeaffa6b 100644 >>> --- a/arch/powerpc/perf/vpa-dtl.c >>> +++ b/arch/powerpc/perf/vpa-dtl.c >>> @@ -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. > > 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. Hi Mohammad, Ritesh Please find my points below: - dtl->buf = NULL in event_del() without a lock: vpa_dtl_cpu is per-CPU, and event->add/event->del run on the event's CPU, so no lock is needed there. event_init can run on any CPU, but it only writes dtl->buf for the target CPU before the event is added. - Moving vpa_dtl_mem_alloc() out of the lock: Agreed. dtl_global_lock only needs to protect dtl_global_refc and the first-user/last-user handling. of dtl_access_lock. The allocation doesn't need it. - Atomic for dtl_global_refc: One perf record opens one event per CPU, and all of them share a single acquisition of dtl_access_lock. The count tells us when the last event is gone, so the lock can be released. The "refc == 0, then down_write_trylock(), then refc++" sequence, and the “refc--, then refc == 0, then up_write()" sequence, have to be atomic with respect to each other. An atomic variable doesn't cover that, so dtl_global_lock is still needed. Redesign changes: Agreed that the locking and buffer lifetime need cleanup, including the clang context analysis annotations. I'd like to handle that as a separate follow-up series, so it doesn't hold up this fix. Mohammad, could you post a v2 of this patch as a minimal fix ? A few changes: 1) Move the allocation before spin_lock(&dtl_global_lock). If it fails, return -ENOMEM. Then no up_write() special case is needed, and the if (dtl_global_refc == 0) check goes away. 2) If we move the allocation out of the lock, take care to free the buffer if try lock fails 3) We might not need GFP_ATOMIC in vpa_dtl_mem_alloc once it is moved out of the spin lock. 4) please add Fixes tag I'll take the broader redesign, including the clang context analysis annotations, as a follow-up. Thanks Athira > > Thanks > Athira > >> >> 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 ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-25 16:04 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2026-11-12 13:44 [PATCH] powerpc/perf: vpa-dtl: fix resource leak on allocation failure mdshahid03 2026-11-18 3:56 ` mdshahid03 2026-11-18 11:32 ` Ritesh Harjani 2026-11-22 16:17 ` Athira Rajeev 2026-11-25 16:03 ` Athira Rajeev
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®