LinuxPPC-Dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] powerpc/perf: vpa-dtl: fix resource leak on allocation failure
@ 2026-11-28 14:30 mdshahid03
  2026-11-30 12:57 ` Athira Rajeev
  0 siblings, 1 reply; 2+ messages in thread
From: mdshahid03 @ 2026-11-28 14:30 UTC (permalink / raw)
  To: maddy
  Cc: mpe, npiggin, chleroy, ritesh.list, sshegde, kjain, linuxppc-dev,
	Mohammad Shahid, Athira Rajeev

From: Mohammad Shahid <mdshahid03@gmail.com>

vpa_dtl_event_init() allocates the DTL buffer while holding
dtl_global_lock. Move the allocation before acquiring the spinlock
to avoid performing memory allocation while holding a spinlock.

Since the buffer is allocated before acquiring dtl_access_lock
allocation failure can be returned directly without releasing the
semaphore.

Free the allocated buffer if acquiring dtl_access_lock fails.

Fixes: 4708fba19ade ("powerpc/vpa_dtl: Add interface to expose vpa dtl counters via perf")
Suggested-by: Athira Rajeev <atrajeev@linux.ibm.com>

Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com>
---
 arch/powerpc/perf/vpa-dtl.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/arch/powerpc/perf/vpa-dtl.c b/arch/powerpc/perf/vpa-dtl.c
index 3d34417e6aae..57bc5773949f 100644
--- a/arch/powerpc/perf/vpa-dtl.c
+++ b/arch/powerpc/perf/vpa-dtl.c
@@ -338,7 +338,7 @@ static int vpa_dtl_mem_alloc(int cpu)
 	if (!dtl_cache)
 		return -ENOMEM;
 
-	buf = kmem_cache_alloc_node(dtl_cache, GFP_ATOMIC, cpu_to_node(cpu));
+	buf = kmem_cache_alloc_node(dtl_cache, GFP_KERNEL, cpu_to_node(cpu));
 	if (!buf) {
 		pr_warn("buffer allocation failed for cpu %d\n", cpu);
 		return -ENOMEM;
@@ -350,6 +350,7 @@ static int vpa_dtl_mem_alloc(int cpu)
 static int vpa_dtl_event_init(struct perf_event *event)
 {
 	struct hw_perf_event *hwc = &event->hw;
+	struct vpa_dtl *dtl = &per_cpu(vpa_dtl_cpu, event->cpu);
 
 	/* test the event attr type for PMU enumeration */
 	if (event->attr.type != event->pmu->type)
@@ -377,6 +378,10 @@ static int vpa_dtl_event_init(struct perf_event *event)
 		return -EINVAL;
 	}
 
+	/* Allocate before taking the global spinlock */
+	if (vpa_dtl_mem_alloc(event->cpu))
+		return -ENOMEM;
+
 	spin_lock(&dtl_global_lock);
 
 	/*
@@ -391,13 +396,11 @@ static int vpa_dtl_event_init(struct perf_event *event)
 	 */
 	if (dtl_global_refc == 0 && !down_write_trylock(&dtl_access_lock)) {
 		spin_unlock(&dtl_global_lock);
-		return -EBUSY;
-	}
+		/* Release the buffer allocated above */
+		kmem_cache_free(dtl_cache, dtl->buf);
+		dtl->buf = NULL;
 
-	/* Allocate dtl buffer memory */
-	if (vpa_dtl_mem_alloc(event->cpu)) {
-		spin_unlock(&dtl_global_lock);
-		return -ENOMEM;
+		return -EBUSY;
 	}
 
 	/*
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v3] powerpc/perf: vpa-dtl: fix resource leak on allocation failure
  2026-11-28 14:30 [PATCH v3] powerpc/perf: vpa-dtl: fix resource leak on allocation failure mdshahid03
@ 2026-11-30 12:57 ` Athira Rajeev
  0 siblings, 0 replies; 2+ messages in thread
From: Athira Rajeev @ 2026-11-30 12:57 UTC (permalink / raw)
  To: mdshahid03
  Cc: maddy, mpe, npiggin, chleroy, ritesh.list, sshegde, kjain,
	linuxppc-dev



> On 28 Nov 2026, at 8:00 PM, mdshahid03@gmail.com wrote:
> 
> From: Mohammad Shahid <mdshahid03@gmail.com>
> 
> vpa_dtl_event_init() allocates the DTL buffer while holding
> dtl_global_lock. Move the allocation before acquiring the spinlock
> to avoid performing memory allocation while holding a spinlock.
> 
> Since the buffer is allocated before acquiring dtl_access_lock
> allocation failure can be returned directly without releasing the
> semaphore.
> 
> Free the allocated buffer if acquiring dtl_access_lock fails.
> 
> Fixes: 4708fba19ade ("powerpc/vpa_dtl: Add interface to expose vpa dtl counters via perf")
> Suggested-by: Athira Rajeev <atrajeev@linux.ibm.com>
> 
> Signed-off-by: Mohammad Shahid <mdshahid03@gmail.com>

Hi Mohammad,

Thanks for the V3, tested this on powerpc

./perf record -vv -a -R -e sched:*,vpa_dtl/dtl_all/ -c 1000000000 sleep 5

./perf script
            perf   15102 [000] 22143.558807:               sched:sched_stat_runtime: comm=perf pid=15102 runtime=215338 [ns]
            perf   15102 [000] 22143.558816:                     sched:sched_waking: comm=migration/0 pid=27 prio=0 target_cpu=000
            perf   15102 [000] 22143.558818:                     sched:sched_wakeup: migration/0:27 [0] CPU:000
            perf   15102 [000] 22143.558819:               sched:sched_stat_runtime: comm=perf pid=15102 runtime=12438 [ns]
            perf   15102 [000] 22143.558820:                     sched:sched_switch: perf:15102 [120] R ==> migration/0:27 [0]
     migration/0      27 [000] 22143.558824:               sched:sched_migrate_task: comm=perf pid=15102 prio=120 orig_cpu=0 dest_cpu=1
     migration/0      27 [000] 22143.558839:               sched:sched_stat_runtime: comm=migration/0 pid=27 runtime=10876 [ns]
     migration/0      27 [000] 22143.558840:                     sched:sched_switch: migration/0:27 [0] S ==> swapper/0:0 [120]
         swapper       0 [000] 22143.558850:                                vpa-dtl: timebase: 5169382455830565 dispatch_reason:priv_doorbell, preempt_reason:H_CEDE,
enqueue_to_dispatch_time:467, ready_to_enqueue_time:0,waiting_to_ready_time:14840223, processor_id: 288 c0000000001bafe8 plpar_hcall_norets_notrace+0x18 ([kernel.kallsyms])

Negative scenarios :
- Opened two vpa dtl events on same cpu to make sure alloc doesn’t overwrite the buffer
- Hold lock on /sys/kernel/debug/powerpc/dtl/cpu-0 and attempted profiling on cpu 0 via perf 

Reviewed-by: Athira Rajeev <atrajeev@linux.ibm.com>
Tested-by: Athira Rajeev <atrajeev@linux.ibm.com>

Thanks
Athira

> ---
> arch/powerpc/perf/vpa-dtl.c | 17 ++++++++++-------
> 1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/arch/powerpc/perf/vpa-dtl.c b/arch/powerpc/perf/vpa-dtl.c
> index 3d34417e6aae..57bc5773949f 100644
> --- a/arch/powerpc/perf/vpa-dtl.c
> +++ b/arch/powerpc/perf/vpa-dtl.c
> @@ -338,7 +338,7 @@ static int vpa_dtl_mem_alloc(int cpu)
> if (!dtl_cache)
> return -ENOMEM;
> 
> - buf = kmem_cache_alloc_node(dtl_cache, GFP_ATOMIC, cpu_to_node(cpu));
> + buf = kmem_cache_alloc_node(dtl_cache, GFP_KERNEL, cpu_to_node(cpu));
> if (!buf) {
> pr_warn("buffer allocation failed for cpu %d\n", cpu);
> return -ENOMEM;
> @@ -350,6 +350,7 @@ static int vpa_dtl_mem_alloc(int cpu)
> static int vpa_dtl_event_init(struct perf_event *event)
> {
> struct hw_perf_event *hwc = &event->hw;
> + struct vpa_dtl *dtl = &per_cpu(vpa_dtl_cpu, event->cpu);
> 
> /* test the event attr type for PMU enumeration */
> if (event->attr.type != event->pmu->type)
> @@ -377,6 +378,10 @@ static int vpa_dtl_event_init(struct perf_event *event)
> return -EINVAL;
> }
> 
> + /* Allocate before taking the global spinlock */
> + if (vpa_dtl_mem_alloc(event->cpu))
> + return -ENOMEM;
> +
> spin_lock(&dtl_global_lock);
> 
> /*
> @@ -391,13 +396,11 @@ static int vpa_dtl_event_init(struct perf_event *event)
> */
> if (dtl_global_refc == 0 && !down_write_trylock(&dtl_access_lock)) {
> spin_unlock(&dtl_global_lock);
> - return -EBUSY;
> - }
> + /* Release the buffer allocated above */
> + kmem_cache_free(dtl_cache, dtl->buf);
> + dtl->buf = NULL;
> 
> - /* Allocate dtl buffer memory */
> - if (vpa_dtl_mem_alloc(event->cpu)) {
> - spin_unlock(&dtl_global_lock);
> - return -ENOMEM;
> + return -EBUSY;
> }
> 
> /*
> -- 
> 2.43.0
> 



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-30 12:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-28 14:30 [PATCH v3] powerpc/perf: vpa-dtl: fix resource leak on allocation failure mdshahid03
2026-11-30 12:57 ` Athira Rajeev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox