[tip:perf/core] perf evlist: Check 'base' pointer before checking refcnt when put a mmap

From: tip-bot for Wang Nan
Date: Thu Jun 02 2016 - 02:34:33 EST

Commit-ID: e10e4ef63b54912feffb1dc48ff7d03d931b1647
Gitweb: http://git.kernel.org/tip/e10e4ef63b54912feffb1dc48ff7d03d931b1647
Author: Wang Nan <wangnan0@xxxxxxxxxx>
AuthorDate: Wed, 25 May 2016 13:44:49 +0000
Committer: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
CommitDate: Mon, 30 May 2016 12:41:45 -0300

perf evlist: Check 'base' pointer before checking refcnt when put a mmap

evlist->mmap[i]->refcnt could be 0 if an evlist has no evsel or if all
evsels don't match the evlist during mmap. For example, when all evsels
are overwritable but the evlist itself is normal. To avoid crashing,
perf should check 'base' pointer before checking refcnt, and raise bug
only when base is not NULL.

Signed-off-by: Wang Nan <wangnan0@xxxxxxxxxx>
Cc: He Kuang <hekuang@xxxxxxxxxx>
Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Masami Hiramatsu <mhiramat@xxxxxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Zefan Li <lizefan@xxxxxxxxxx>
Cc: pi3orama@xxxxxxx
Link: http://lkml.kernel.org/r/1464183898-174512-2-git-send-email-wangnan0@xxxxxxxxxx
[ Renamed 'mmap' variable, it is reserved in old distros such as Ubuntu 12.04, breaking the build ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
tools/perf/util/evlist.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 50d7b80..58ede32 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -856,9 +856,11 @@ static void perf_evlist__mmap_get(struct perf_evlist *evlist, int idx)

static void perf_evlist__mmap_put(struct perf_evlist *evlist, int idx)
- BUG_ON(atomic_read(&evlist->mmap[idx].refcnt) == 0);
+ struct perf_mmap *md = &evlist->mmap[idx];
+ BUG_ON(md->base && atomic_read(&md->refcnt) == 0);

- if (atomic_dec_and_test(&evlist->mmap[idx].refcnt))
+ if (atomic_dec_and_test(&md->refcnt))
__perf_evlist__munmap(evlist, idx);