Skip to contentSkip to conversation

perf core: Fix a mmap and munmap mismatched bug

PATCH In review Chenggang Qin 2 participants 6 messages Sep 1, 2013 – Sep 3, 2013 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 perf core: Fix a mmap and munmap mismatched bug

From: root <[email protected]>

In function filename__read_debuglink(), while the ELF file is opend and mmapped in elf_begin(), but if this file is considered to not be usable during the following code, we will goto the close(fd) directly. The elf_end() is skipped. So, the mmaped ELF file cannot be munmapped. The memory areas are mmapped is exist during the life of perf. This is a memory leak.
This patch fixed this bug.
Thanks.

Cc David Ahern <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Arjan van de Ven <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Yanmin Zhang <[email protected]>
Cc Wu Fengguang <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Andrew Morton <[email protected]>
---
 tools/perf/util/symbol-elf.c |   10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)
tools/perf/util/symbol-elf.c +5 -5
index 4b12bf8..b4df870 100644
@@ -471,27 +471,27 @@ int filename__read_debuglink(const char *filename, char *debuglink,
 
 	ek = elf_kind(elf);
 	if (ek != ELF_K_ELF)
-		goto out_close;
+		goto out_elf_end;
 
 	if (gelf_getehdr(elf, &ehdr) == NULL) {
 		pr_err("%s: cannot get elf header.\n", __func__);
-		goto out_close;
+		goto out_elf_end;
 	}
 
 	sec = elf_section_by_name(elf, &ehdr, &shdr,
 				  ".gnu_debuglink", NULL);
 	if (sec == NULL)
-		goto out_close;
+		goto out_elf_end;
 
 	data = elf_getdata(sec, NULL);
 	if (data == NULL)
-		goto out_close;
+		goto out_elf_end;
 
 	/* the start of this section is a zero-terminated string */
 	strncpy(debuglink, data->d_buf, size);
 
+out_elf_end:
 	elf_end(elf);
-
 out_close:
 	close(fd);
 out:
Signature · 1 line
1.7.8.rc2.5.g815b
#1 lore raw

PATCH 2/3 perf core: Fix a mmap & munmap mismatches bug in dso__load

From: Chenggang Qin <[email protected]>

Some dsos' symsrc is neither syms_ss or runtime_ss. In this situation, the corresponding ELF file is opened and mmapped in symsrc__init(), but they will be not closed and munmapped in any place.
This bug can lead to mmap & munmap mismatched, the mmap areas will exist during the life of perf. We can think this is a memory leak.
This patch fixed the bug. symsrc__destroy() is called while the opened and mmaped ELF file has neither symtlb section nor dynsym section, and opdsec section.
Thanks.

Cc David Ahern <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Arjan van de Ven <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Yanmin Zhang <[email protected]>
Cc Wu Fengguang <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Andrew Morton <[email protected]>
---
 tools/perf/util/symbol.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
tools/perf/util/symbol.c +2 -1
index d5528e1..9675866 100644
@@ -828,7 +828,8 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
 
 			if (syms_ss && runtime_ss)
 				break;
-		}
+		} else
+			symsrc__destroy(ss);
 
 	}
 
Signature · 1 line
1.7.8.rc2.5.g815b
maintainer Acked-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] perf core: Fix a mmap & munmap mismatches bug in dso__load

Quoted 11 lines · Chenggang Qin

From: Chenggang Qin <[email protected]>

Some dsos' symsrc is neither syms_ss or runtime_ss. In this situation, the corresponding ELF file is opened and mmapped in symsrc__init(), but they will be not closed and munmapped in any place.
This bug can lead to mmap & munmap mismatched, the mmap areas will exist during the life of perf. We can think this is a memory leak.
This patch fixed the bug. symsrc__destroy() is called while the opened and mmaped ELF file has neither symtlb section nor dynsym section, and opdsec section.
Thanks.

Thanks,
Namhyung

#3 lore raw

PATCH 3/3 perf core: Fix a memory leak bug because symbol__delete is ignored

In reply to Chenggang Qin

From: Chenggang Qin <[email protected]>

In function symbols__fixup_duplicate(), while the duplicated symbols are found, only the rb_node are deleted. The symbol structures themself are ignored. Then, these memory areas are lost.
This patch fixed the bug.
Thanks.

Cc David Ahern <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Arjan van de Ven <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Yanmin Zhang <[email protected]>
Cc Wu Fengguang <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Andrew Morton <[email protected]>
---
 tools/perf/util/symbol.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
tools/perf/util/symbol.c +2 -0
index 9675866..3c9aa6f 100644
@@ -148,10 +148,12 @@ again:
 
 		if (choose_best_symbol(curr, next) == SYMBOL_A) {
 			rb_erase(&next->rb_node, symbols);
+			symbol__delete(next);
 			goto again;
 		} else {
 			nd = rb_next(&curr->rb_node);
 			rb_erase(&curr->rb_node, symbols);
+			symbol__delete(curr);
 		}
 	}
 }
Signature · 1 line
1.7.8.rc2.5.g815b
maintainer Acked-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] perf core: Fix a memory leak bug because symbol__delete is ignored

Quoted 7 lines · Chenggang Qin

From: Chenggang Qin <[email protected]>

In function symbols__fixup_duplicate(), while the duplicated symbols are found, only the rb_node are deleted. The symbol structures themself are ignored. Then, these memory areas are lost.
This patch fixed the bug.
Thanks.

Thanks,
Namhyung

maintainer Reviewed-by on v1 1/3
#5 lore raw
In reply to Chenggang Qin

Hi Chenggang,

Quoted 9 lines · Chenggang Qin

From: root <[email protected]>

In function filename__read_debuglink(), while the ELF file is opend and mmapped in elf_begin(), but if this file is considered to not be usable during the following code, we will goto the close(fd) directly. The elf_end() is skipped. So, the mmaped ELF file cannot be munmapped. The memory areas are mmapped is exist during the life of perf. This is a memory leak.
This patch fixed this bug.
Thanks.

Thanks,
Namhyung