powerpc/perf: vpa-dtl: fix resource leak on allocation failure
PATCH v2 1/1 powerpc/perf: vpa-dtl: fix resource leak on allocation failure
From: Mohammad Shahid <[email protected]>
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.
--- arch/powerpc/perf/vpa-dtl.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
arch/powerpc/perf/vpa-dtl.c +10 -9
@@ -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_KERNEL |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,15 +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)) { -if (dtl_global_refc ==0 ) -up_write (&dtl_access_lock); -spin_unlock (&dtl_global_lock); -return -ENOMEM ; +return -EBUSY ; }/*
Signature · 1 line
2.43.0
Ritesh Harjani (IBM)
reviewerQuoted 12 lines · [email protected] writes:
From: Mohammad Shahid <[email protected]>
vpa_dtl_event_init()allocates the DTL buffer while holdingdtl_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_lockallocation failure can be returned directly without releasing the semaphore.Free the allocated buffer if acquiring
dtl_access_lockfails.
Quoted 7 lines
Fixes 4708fba19ade ("powerpc/vpa_dtl: Add interface to expose vpa dtl counters via perf")Suggested-by Athira Rajeev <[email protected]>Signed-off-by Mohammad Shahid <[email protected]>--- arch/powerpc/perf/vpa-dtl.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
I am unable to apply this patch on any of the upstream trees. Could you please rebase your patch on top of powerpc/next-test [2]?
[2]: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git/log/?h=next-test
arch/powerpc/perf/vpa-dtl.c static int vpa_dtl_mem_alloc(int cpu) · line 341
Quoted 11 lines
arch/powerpc/perf/vpa-dtl.c +1
-1@@ -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_KERNEL |GFP_ATOMIC ,cpu_to_node (cpu)); + buf =kmem_cache_alloc_node (dtl_cache,GFP_KERNEL ,cpu_to_node (cpu));
E.g. in this commit [1], we already had changed this above allocation to GFP_ATOMIC, which is even merged in mainline. So it seems your base tree is old. Please rebase your changes to powerpc/next-test as pointed above and resend.
-ritesh