Re: [PATCH] hw breakpoint: Fix possible memory leak

Ingo Molnar
2012-02-27T10:45:05Z

* Peter Zijlstra <a.p.zijlstra@chello.nl> wrote:

> On Mon, 2012-02-27 at 12:02 +0900, Namhyung Kim wrote:
> > If kzalloc() for TYPE_DATA failed on a given cpu, previous chunk
> > will be leaked. Fix it.
>
> so why not fix the error loop? wouldn't putting that err_cpu == cpu
> break after the kfree sort it?

I edited that code earlier today - is the form below OK, or can
you see a simpler method? It's not yet pushed out so can still
edit it.

Thanks,

Ingo

>From f019669c93da6c2094326d07735320d3bf223ffe Mon Sep 17 00:00:00 2001
From: Namhyung Kim <namhyung.kim@lge.com>
Date: Mon, 27 Feb 2012 12:02:19 +0900
Subject: [PATCH] hw breakpoints: Fix possible memory leak

If kzalloc() for TYPE_DATA failed on a given cpu, previous chunk
will be leaked. Fix it.

Signed-off-by: Namhyung Kim <namhyung.kim@lge.com>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
Link: http://lkml.kernel.org/r/1330311739-24302-1-git-send-email-namhyung.kim@lge.com
[ rearranged the code to have a clearer flow ]
Signed-off-by: Ingo Molnar <mingo@elte.hu>
---
kernel/events/hw_breakpoint.c | 8 +++++---
1 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/kernel/events/hw_breakpoint.c b/kernel/events/hw_breakpoint.c
index b7971d6..867032d 100644
--- a/kernel/events/hw_breakpoint.c
+++ b/kernel/events/hw_breakpoint.c
@@ -636,10 +636,9 @@ int __init init_hw_breakpoint(void)
for_each_possible_cpu(cpu) {
for (i = 0; i < TYPE_MAX; i++) {
task_bp_pinned = &per_cpu(nr_task_bp_pinned[i], cpu);
- *task_bp_pinned = kzalloc(sizeof(int) * nr_slots[i],
- GFP_KERNEL);
+ *task_bp_pinned = kzalloc(sizeof(int) * nr_slots[i], GFP_KERNEL);
if (!*task_bp_pinned)
- goto err_alloc;
+ goto err_alloc_pinned;
}
}

@@ -649,6 +648,9 @@ int __init init_hw_breakpoint(void)

return register_die_notifier(&hw_breakpoint_exceptions_nb);

+ err_alloc_pinned:
+ while (--i >= 0)
+ kfree(per_cpu(nr_task_bp_pinned[i], cpu));
err_alloc:
for_each_possible_cpu(err_cpu) {
if (err_cpu == cpu)