Skip to contentSkip to conversation

Fix a small memory leak in latency histograms

PATCH In review Carsten Emde 2 participants 4 messages Feb 14, 2014 Tracing

lore
Share & tools
lore
#0 lore raw

cover Fix a small memory leak in latency histograms

Hi Sebastian,

while we are at it ...

Here comes a fix of a small memory leak in latency histograms.

-Carsten.

#1 lore raw

PATCH 1/1 tracing/latency_hist Fix memory leak

The index_ptr memory that is allocated when printout is started currently is only returned when the printout is stopped prematurely. It is not returned when the printout regularly finishes. Fix this memory leak.

kernel/trace/latency_hist.c +1 -0
Index: linux-3.12.10-rt15-somedebug/kernel/trace/latency_hist.c
===================================================================
@@ -313,6 +313,7 @@ static void *l_next(struct seq_file *m, 
 
 	if (++*pos >= MAX_ENTRY_NUM) {
 		atomic_inc(&my_hist->hist_mode);
+		kfree(p);
 		return NULL;
 	}
 	*index_ptr = *pos;
#2 lore raw

Re: [PATCH 1/1] tracing/latency_hist Fix memory leak

Quoted 12 lines · Carsten Emde

The index_ptr memory that is allocated when printout is started currently is only returned when the printout is stopped prematurely. It is not returned when the printout regularly finishes. Fix this memory leak.

Index: linux-3.12.10-rt15-somedebug/kernel/trace/latency_hist.c
===================================================================

kernel/trace/latency_hist.c static void *l_next(struct seq_file *m, · line 319

Quoted 7 lines
kernel/trace/latency_hist.c +1 -0
@@ -313,6 +313,7 @@ static void *l_next(struct seq_file *m, 
 
 	if (++*pos >= MAX_ENTRY_NUM) {
 		atomic_inc(&my_hist->hist_mode);
+		kfree(p);
 		return NULL;
 	}
 	*index_ptr = *pos;

Sure on that? If I look at seq_read() I see that there is that ->stop() is always called after ->start() / ->next() before returning to caller. Based on this I would say that this patach will introduce a double free of p.

Quoted 1 line

Sebastian

#3 lore raw

Re: [PATCH 1/1] tracing/latency_hist Fix memory leak

Quoted 14 lines · Sebastian Andrzej Siewior
Quoted 13 lines · Carsten Emde

The index_ptr memory that is allocated when printout is started currently is only returned when the printout is stopped prematurely. It is not returned when the printout regularly finishes. Fix this memory leak.

Index: linux-3.12.10-rt15-somedebug/kernel/trace/latency_hist.c
===================================================================

kernel/trace/latency_hist.c +0 -0
@@ -313,6 +313,7 @@ static void *l_next(struct seq_file *m,
 
Quoted 11 lines

kernel/trace/latency_hist.c static void *l_next(struct seq_file *m, · line 319

Quoted 6 lines
kernel/trace/latency_hist.c +1 -0
  	if (++*pos >= MAX_ENTRY_NUM) {
  		atomic_inc(&my_hist->hist_mode);
+		kfree(p);
  		return NULL;
  	}
  	*index_ptr = *pos;

Sure on that? If I look at seq_read() I see that there is that ->stop() is always called after ->start() / ->next() before returning to caller. Based on this I would say that this patach will introduce a double free of p.

Some of the farm systems enable different levels of run time debug features including kmemleak in two of them. These two machines regularly crashed with kmemleak alloc overflow, because the system detected so many memleaks that there was no more memory available to store all of them. Since the objects were so small, the leak was not measurable in terms of memory exhaustion. I then investigated the kmemleak objects, and they clearly pointed to the index_ptr memory. This was the only place I found, and kmemleak no longer complaint or crashed after inserting this kfree(). I then patched the other farm systems some time ago where it never never did any harm. I needed to find a solution to the kmemleak machines, since otherwise I wouldn't have a usable tool to search for other memleaks. But I know that I should have done better. So please drop this patch and wait, until I find some time to more carefully analyze the entire situation.

-Carsten.