Skip to content

Commit

Permalink
blktrace: fix memory leak when freeing struct blk_io_trace
Browse files Browse the repository at this point in the history
Impact: fix mixed ioctl and ftrace-plugin blktrace use memory leak

When mixing the use of ioctl-based blktrace and ftrace-based blktrace,
we can leak memory in this way:

  # btrace /dev/sda > /dev/null &
  # echo 0 > /sys/block/sda/sda1/trace/enable

now we leak bt->dropped_file, bt->msg_file, bt->rchan...

Signed-off-by: Li Zefan <lizf@cn.fujitsu.com>
Acked-by: Jens Axboe <jens.axboe@oracle.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
  • Loading branch information
Li Zefan authored and Ingo Molnar committed Mar 31, 2009
1 parent 17ba97e commit ad5dd54
Showing 1 changed file with 12 additions and 17 deletions.
29 changes: 12 additions & 17 deletions kernel/trace/blktrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,19 @@ static void __blk_add_trace(struct blk_trace *bt, sector_t sector, int bytes,
static struct dentry *blk_tree_root;
static DEFINE_MUTEX(blk_tree_mutex);

static void blk_trace_cleanup(struct blk_trace *bt)
static void blk_trace_free(struct blk_trace *bt)
{
debugfs_remove(bt->msg_file);
debugfs_remove(bt->dropped_file);
relay_close(bt->rchan);
free_percpu(bt->sequence);
free_percpu(bt->msg_data);
kfree(bt);
}

static void blk_trace_cleanup(struct blk_trace *bt)
{
blk_trace_free(bt);
if (atomic_dec_and_test(&blk_probes_ref))
blk_unregister_tracepoints();
}
Expand Down Expand Up @@ -410,11 +415,11 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,
if (buts->name[i] == '/')
buts->name[i] = '_';

ret = -ENOMEM;
bt = kzalloc(sizeof(*bt), GFP_KERNEL);
if (!bt)
goto err;
return -ENOMEM;

ret = -ENOMEM;
bt->sequence = alloc_percpu(unsigned long);
if (!bt->sequence)
goto err;
Expand Down Expand Up @@ -483,17 +488,7 @@ int do_blk_trace_setup(struct request_queue *q, char *name, dev_t dev,

return 0;
err:
if (bt) {
if (bt->msg_file)
debugfs_remove(bt->msg_file);
if (bt->dropped_file)
debugfs_remove(bt->dropped_file);
free_percpu(bt->sequence);
free_percpu(bt->msg_data);
if (bt->rchan)
relay_close(bt->rchan);
kfree(bt);
}
blk_trace_free(bt);
return ret;
}

Expand Down Expand Up @@ -1091,25 +1086,25 @@ static void blk_tracer_print_header(struct seq_file *m)

static void blk_tracer_start(struct trace_array *tr)
{
blk_tracer_enabled = true;
trace_flags &= ~TRACE_ITER_CONTEXT_INFO;
}

static int blk_tracer_init(struct trace_array *tr)
{
blk_tr = tr;
blk_tracer_start(tr);
blk_tracer_enabled = true;
return 0;
}

static void blk_tracer_stop(struct trace_array *tr)
{
blk_tracer_enabled = false;
trace_flags |= TRACE_ITER_CONTEXT_INFO;
}

static void blk_tracer_reset(struct trace_array *tr)
{
blk_tracer_enabled = false;
blk_tracer_stop(tr);
}

Expand Down Expand Up @@ -1250,7 +1245,7 @@ static int blk_trace_remove_queue(struct request_queue *q)
if (atomic_dec_and_test(&blk_probes_ref))
blk_unregister_tracepoints();

kfree(bt);
blk_trace_free(bt);
return 0;
}

Expand Down

0 comments on commit ad5dd54

Please sign in to comment.