Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp4518815yba; Tue, 7 May 2019 20:48:47 -0700 (PDT) X-Google-Smtp-Source: APXvYqy4Vq+uPBEKa5OeEzciRbjzBfqtWjOcnvPQ4OxYOU+PVK5Mg4npIWfUSYspgDqjSQA2g5wv X-Received: by 2002:a65:6282:: with SMTP id f2mr43913573pgv.152.1557287326983; Tue, 07 May 2019 20:48:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557287326; cv=none; d=google.com; s=arc-20160816; b=qGRbT2OLnUrsfd4i13b2+hy9leCzx8vX5SqLyRW9Iy/HjCVaAbIhTYRSdQlxQAj/km 1DgRyXLvlszTd4NcB5ughOy0YvrqLDFowMajlaTRBM5BFQw5ScqAAVnABmBNTbfJbWM7 V59Q+i8dQJu34gAIRbfV6GOG7nRWMVeUNVca7toDcqeT8Zc+Di6Xj920ziLXxggDSvT2 KLhluXZevZoo2F4794Lpd5sRwQ1RrzB+eReyckTtFBt25L+DsXAWlcdCe0awcU12J8kt FuYNy69VYFfdcdpItM+HR7al6+BBcLaO9qPy2c0U6rb5e1YG6Y8TaPP0+Vz0/FwkKA8E bm8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:references:cc:to :subject; bh=xssN6gRXEZwBhOlzISsoKBTZpfU9KNKcK4NPkr70Xzw=; b=T3Luk1ZCZHEa4hhKqjGv2/P2xxl1+rLgQhEb1kIVREYqfpGxhx48sOyyZY1txgobdd Eoxlgp7S1LPoNDNJ1b/nZH828SLF2u0v3Y2DuMEiHGVP2iwq++10Wa5oHC2iwdOXW1nP 0zN7+L9ocjl2i/ouOaFhx7IDGSsqKweLR5ji0tFT/AR722bCB430j5QgX05DOZxIDZAV f1wH4UvR3neYccyiMhudbnSsm9U5WBvlQjAhDdbXOL5AUIvEVeqmIWK+N+ZX9SnoIrLG MwJQumH+w8KSTpQGYh6L9p5uymiFomrOdi3+P8KmxAUZ2r+BmLTZjDheeISlAMRBtznj PeYg== ARC-Authentication-Results: i=1; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h7si20663706pgj.363.2019.05.07.20.48.31; Tue, 07 May 2019 20:48:46 -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 S1727020AbfEHDq1 (ORCPT + 99 others); Tue, 7 May 2019 23:46:27 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:2580 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726688AbfEHDq1 (ORCPT ); Tue, 7 May 2019 23:46:27 -0400 Received: from DGGEMM405-HUB.china.huawei.com (unknown [172.30.72.57]) by Forcepoint Email with ESMTP id 3DD319077D78086EC061; Wed, 8 May 2019 11:46:25 +0800 (CST) Received: from dggeme754-chm.china.huawei.com (10.3.19.100) by DGGEMM405-HUB.china.huawei.com (10.3.20.213) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 8 May 2019 11:46:24 +0800 Received: from [127.0.0.1] (10.184.212.80) by dggeme754-chm.china.huawei.com (10.3.19.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10; Wed, 8 May 2019 11:46:24 +0800 Subject: Re: [PATCH] fix use-after-free in perf_sched__lat To: Jiri Olsa CC: , , , , , , References: <20190503023555.24736-1-liwei391@huawei.com> <20190507085153.GC17416@krava> From: "liwei (GF)" Message-ID: <87032042-d814-dde9-8c35-31794e91209f@huawei.com> Date: Wed, 8 May 2019 11:45:15 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20190507085153.GC17416@krava> Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.184.212.80] X-ClientProxiedBy: dggeme767-chm.china.huawei.com (10.3.19.113) To dggeme754-chm.china.huawei.com (10.3.19.100) X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Jiri, Thanks for your reply. On 2019/5/7 16:51, Jiri Olsa wrote: > On Fri, May 03, 2019 at 10:35:55AM +0800, Wei Li wrote: >> After thread is added to machine->threads[i].dead in >> __machine__remove_thread, the machine->threads[i].dead is freed >> when calling free(session) in perf_session__delete(). So it get a >> Segmentation fault when accessing it in thread__put(). >> >> In this patch, we delay the perf_session__delete until all threads >> have been deleted. >> >> This can be reproduced by following steps: >> ulimit -c unlimited >> export MALLOC_MMAP_THRESHOLD_=0 > > what's this for? When we set env "MALLOC_MMAP_THRESHOLD_=0",the memory-allocation functions employ mmap instead of increasing the program break using sbrk what may be maintained with cache. Thus it can report "Segmentation fault" immediately when going into this use-after-free code. > >> perf sched record sleep 10 >> perf sched latency --sort max >> Segmentation fault (core dumped) >> >> Signed-off-by: Zhipeng Xie >> Signed-off-by: Wei Li >> --- >> tools/perf/builtin-sched.c | 44 ++++++++++++++++++++++++++++++++++++-- >> 1 file changed, 42 insertions(+), 2 deletions(-) >> >> diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c >> index cbf39dab19c1..17849ae2eb1e 100644 >> --- a/tools/perf/builtin-sched.c >> +++ b/tools/perf/builtin-sched.c >> @@ -3130,11 +3130,48 @@ static void perf_sched__merge_lat(struct perf_sched *sched) >> static int perf_sched__lat(struct perf_sched *sched) >> { >> struct rb_node *next; >> + const struct perf_evsel_str_handler handlers[] = { >> + { "sched:sched_switch", process_sched_switch_event, }, >> + { "sched:sched_stat_runtime", process_sched_runtime_event, }, >> + { "sched:sched_wakeup", process_sched_wakeup_event, }, >> + { "sched:sched_wakeup_new", process_sched_wakeup_event, }, >> + { "sched:sched_migrate_task", process_sched_migrate_task_event, }, >> + }; >> + struct perf_session *session; >> + struct perf_data data = { >> + .file = { >> + .path = input_name, >> + }, > > I can't compile this: > > builtin-sched.c: In function ‘perf_sched__lat’: > builtin-sched.c:3144:12: error: initialization discards ‘const’ qualifier from pointer target type [-Werror=discarded-qualifiers] > .path = input_name, > Sorry, this place has been changed recently in mainline code, i will update to the latest code. > >> + .mode = PERF_DATA_MODE_READ, >> + .force = sched->force, >> + }; >> + int rc = -1; >> >> setup_pager(); >> >> - if (perf_sched__read_events(sched)) > > so it's basically perf_sched__read_events code in here now, right? > > might be better to add __perf_sched__read_events function > that would take session argument, something like: > > session = perf_session__new(&data, false, &sched->tool); > ... > __perf_sched__read_events(sched, session) > ... > perf_session__delete(session); > > to avoid the code ducplication Yes, what you suggest is reasonable, i will send a v2 with your suggestion soon. Thanks, Wei