From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753975AbcEYNve (ORCPT ); Wed, 25 May 2016 09:51:34 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:37608 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751660AbcEYNvc (ORCPT ); Wed, 25 May 2016 09:51:32 -0400 From: Wang Nan To: CC: , , Wang Nan , He Kuang , "Arnaldo Carvalho de Melo" , Jiri Olsa , Masami Hiramatsu , Namhyung Kim , "Zefan Li" Subject: [PATCH v6 01/10] perf tools: Check 'base' pointer before checking refcnt when put a mmap Date: Wed, 25 May 2016 13:44:49 +0000 Message-ID: <1464183898-174512-2-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1464183898-174512-1-git-send-email-wangnan0@huawei.com> References: <1464183898-174512-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020204.5745AC66.0474,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 2ff94aa533e3787e90a8460978daab90 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org evlist->mmap[i]->refcnt could be 0 if an evlist has no evsel or 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 Cc: He Kuang Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Masami Hiramatsu Cc: Namhyung Kim Cc: Zefan Li Cc: pi3orama@163.com --- 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 fbd0d47..f916d25 100644 --- a/tools/perf/util/evlist.c +++ b/tools/perf/util/evlist.c @@ -857,9 +857,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 *mmap = &evlist->mmap[idx]; - if (atomic_dec_and_test(&evlist->mmap[idx].refcnt)) + BUG_ON(mmap->base && atomic_read(&mmap->refcnt) == 0); + + if (atomic_dec_and_test(&mmap->refcnt)) __perf_evlist__munmap(evlist, idx); } -- 1.8.3.4