Skip to content

Commit ff288b2

Browse files
fweisbecIngo Molnar
authored andcommitted
tracing/ftrace: fix a memory leak in stat tracing
Impact: fix memory leak This patch fixes a memory leak inside reset_stat_list(). The freeing loop iterated only once. Also turn the stat_list into a simple struct list_head, which simplify the code and avoid an unused static pointer. Reported-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Signed-off-by: Steven Rostedt <srostedt@redhat.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 431aa3f commit ff288b2

1 file changed

Lines changed: 15 additions & 24 deletions

File tree

‎kernel/trace/trace_stat.c‎

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct trace_stat_list {
2121
void *stat;
2222
};
2323

24-
static struct trace_stat_list stat_list;
24+
static LIST_HEAD(stat_list);
2525

2626
/*
2727
* This is a copy of the current tracer to avoid racy
@@ -39,22 +39,12 @@ static DEFINE_MUTEX(stat_list_mutex);
3939

4040
static void reset_stat_list(void)
4141
{
42-
struct trace_stat_list *node;
43-
struct list_head *next;
42+
struct trace_stat_list *node, *next;
4443

45-
if (list_empty(&stat_list.list))
46-
return;
47-
48-
node = list_entry(stat_list.list.next, struct trace_stat_list, list);
49-
next = node->list.next;
50-
51-
while (&node->list != next) {
44+
list_for_each_entry_safe(node, next, &stat_list, list)
5245
kfree(node);
53-
node = list_entry(next, struct trace_stat_list, list);
54-
}
55-
kfree(node);
5646

57-
INIT_LIST_HEAD(&stat_list.list);
47+
INIT_LIST_HEAD(&stat_list);
5848
}
5949

6050
void init_tracer_stat(struct tracer *trace)
@@ -107,7 +97,7 @@ static int stat_seq_init(void)
10797
}
10898

10999
INIT_LIST_HEAD(&new_entry->list);
110-
list_add(&new_entry->list, &stat_list.list);
100+
list_add(&new_entry->list, &stat_list);
111101
new_entry->stat = current_tracer.stat_start();
112102

113103
prev_stat = new_entry->stat;
@@ -130,7 +120,7 @@ static int stat_seq_init(void)
130120
if (!new_entry->stat)
131121
break;
132122

133-
list_for_each_entry(iter_entry, &stat_list.list, list) {
123+
list_for_each_entry(iter_entry, &stat_list, list) {
134124
/* Insertion with a descendent sorting */
135125
if (current_tracer.stat_cmp(new_entry->stat,
136126
iter_entry->stat) > 0) {
@@ -141,7 +131,7 @@ static int stat_seq_init(void)
141131

142132
/* The current smaller value */
143133
} else if (list_is_last(&iter_entry->list,
144-
&stat_list.list)) {
134+
&stat_list)) {
145135
list_add(&new_entry->list, &iter_entry->list);
146136
break;
147137
}
@@ -162,7 +152,7 @@ static int stat_seq_init(void)
162152

163153
static void *stat_seq_start(struct seq_file *s, loff_t *pos)
164154
{
165-
struct trace_stat_list *l = (struct trace_stat_list *)s->private;
155+
struct list_head *l = (struct list_head *)s->private;
166156

167157
/* Prevent from tracer switch or stat_list modification */
168158
mutex_lock(&stat_list_mutex);
@@ -171,14 +161,14 @@ static void *stat_seq_start(struct seq_file *s, loff_t *pos)
171161
if (!*pos && current_tracer.stat_headers)
172162
current_tracer.stat_headers(s);
173163

174-
return seq_list_start(&l->list, *pos);
164+
return seq_list_start(l, *pos);
175165
}
176166

177167
static void *stat_seq_next(struct seq_file *s, void *p, loff_t *pos)
178168
{
179-
struct trace_stat_list *l = (struct trace_stat_list *)s->private;
169+
struct list_head *l = (struct list_head *)s->private;
180170

181-
return seq_list_next(p, &l->list, pos);
171+
return seq_list_next(p, l, pos);
182172
}
183173

184174
static void stat_seq_stop(struct seq_file *m, void *p)
@@ -188,8 +178,10 @@ static void stat_seq_stop(struct seq_file *m, void *p)
188178

189179
static int stat_seq_show(struct seq_file *s, void *v)
190180
{
191-
struct trace_stat_list *l = list_entry(v, struct trace_stat_list, list);
192-
return current_tracer.stat_show(s, l->stat);
181+
struct trace_stat_list *entry =
182+
list_entry(v, struct trace_stat_list, list);
183+
184+
return current_tracer.stat_show(s, entry->stat);
193185
}
194186

195187
static const struct seq_operations trace_stat_seq_ops = {
@@ -237,7 +229,6 @@ static int __init tracing_stat_init(void)
237229
struct dentry *d_tracing;
238230
struct dentry *entry;
239231

240-
INIT_LIST_HEAD(&stat_list.list);
241232
d_tracing = tracing_init_dentry();
242233

243234
entry = debugfs_create_file("trace_stat", 0444, d_tracing,

0 commit comments

Comments
 (0)