From patchwork Wed May 25 05:03:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Nan X-Patchwork-Id: 68551 Delivered-To: patch@linaro.org Received: by 10.140.92.199 with SMTP id b65csp1011822qge; Tue, 24 May 2016 22:04:48 -0700 (PDT) X-Received: by 10.98.29.196 with SMTP id d187mr2794953pfd.142.1464152683361; Tue, 24 May 2016 22:04:43 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o193si3981187pfo.226.2016.05.24.22.04.43; Tue, 24 May 2016 22:04:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754920AbcEYFEh (ORCPT + 30 others); Wed, 25 May 2016 01:04:37 -0400 Received: from szxga01-in.huawei.com ([58.251.152.64]:1100 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754857AbcEYFEf (ORCPT ); Wed, 25 May 2016 01:04:35 -0400 Received: from 172.24.1.60 (EHLO SZXEML429-HUB.china.huawei.com) ([172.24.1.60]) by szxrg01-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DLD27042; Wed, 25 May 2016 13:03:58 +0800 (CST) Received: from linux-4hy3.site (10.107.193.248) by SZXEML429-HUB.china.huawei.com (10.82.67.184) with Microsoft SMTP Server id 14.3.235.1; Wed, 25 May 2016 13:03:48 +0800 From: Wang Nan To: CC: , , Wang Nan , He Kuang , "Arnaldo Carvalho de Melo" , Jiri Olsa , Masami Hiramatsu , Namhyung Kim , "Zefan Li" Subject: [PATCH v5 1/8] perf tools: Check 'base' pointer before checking refcnt when put a mmap Date: Wed, 25 May 2016 05:03:36 +0000 Message-ID: <1464152623-171824-2-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1464152623-171824-1-git-send-email-wangnan0@huawei.com> References: <1464152623-171824-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A090204.5745323E.00AE, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 7468b3f91ee7a236b2cb81b56b2395c9 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk 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(-) -- 1.8.3.4 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); }