Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp52865yba; Thu, 2 May 2019 19:35:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqzfXC+fUEmIvmzVC3PNjcnCcYewwCs4dobnOwzWGssbUKOOI3lDc4iT/OP7vvNwQabfOJPW X-Received: by 2002:a17:902:968b:: with SMTP id n11mr7305396plp.118.1556850920396; Thu, 02 May 2019 19:35:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556850920; cv=none; d=google.com; s=arc-20160816; b=uz8LJ1lsK+Z4pHfnyzHJBEZNr/jEDzgmutk/xj/ydVi9g15suk1zcyT139u4j3U6DF VoN7s8CIA4cPrHeBhOzsheg3OHoGTELcFO0cXozQfBgvlgvOWK7LwtFCiQ7w3uRl8rm3 fZKg/+ULO7LUm17U0SR0DsMjWqgDuTkYHTHoM1bJUnXUN8KXfBlZFqECssSfMHqtGghS qlfNnbs3M2c6nrun/hb3Yf+B2NN10c7sgiuwp/02oj2SdW5prR9xUT4erWWCKucOWctC 7bqSIiLUtXfnFXgu+cwglBjmrViQAZSoIdfIMhzpb8DPO1CycrbbSD4yOUZy40BVdRCp 6SKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=fwHem7s15IbNl0CfP5Z7BoWSk0CHc9601CRjJoGrLTI=; b=vEjRi9BzIN9Nm/UaAvjljJTi2dDmli+x/AX4NgYzHg23APH7lVcGOVVOwcHyG4jlmL R8RxVQAF9hKYdROHW8eNyEQfL4EGAKy1VVHiSkkDQJI5RV63WQwWvzmVUAQJDqqtV17A kGU8UuTaYIPTdMvJvESfSdMCFVfrPDVV8oUit/d5axBx6d8wv1Z5CjnxtlkPbi5vnrPU 2I7e7b05CYmw/A1tGzKBIzv8xJh2tUARBpVG7lntUkIbli7ftReRnSW9BXZNzFiTgF+b uiFPlumJ0KxpG1pOOn/dXNpXRzH0LzFO/3oxEmkfk9ctW7I9JR3Dl5ridKlRDcqYNFl6 c/2A== 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 h29si774070pfd.180.2019.05.02.19.35.05; Thu, 02 May 2019 19:35:20 -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 S1726209AbfECCbb (ORCPT + 99 others); Thu, 2 May 2019 22:31:31 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:41422 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726022AbfECCba (ORCPT ); Thu, 2 May 2019 22:31:30 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id BEEB9C71FAE21CCD323E; Fri, 3 May 2019 10:31:28 +0800 (CST) Received: from euler.huawei.com (10.175.104.193) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.439.0; Fri, 3 May 2019 10:31:20 +0800 From: Wei Li To: , , , , , CC: , Subject: [PATCH] fix use-after-free in perf_sched__lat Date: Fri, 3 May 2019 10:35:55 +0800 Message-ID: <20190503023555.24736-1-liwei391@huawei.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.104.193] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 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, + }, + .mode = PERF_DATA_MODE_READ, + .force = sched->force, + }; + int rc = -1; setup_pager(); - if (perf_sched__read_events(sched)) + session = perf_session__new(&data, false, &sched->tool); + if (session == NULL) { + pr_debug("No Memory for session\n"); return -1; + } + + symbol__init(&session->header.env); + + if (perf_session__set_tracepoints_handlers(session, handlers)) + goto out_delete; + + if (perf_session__has_traces(session, "record -R")) { + int err = perf_session__process_events(session); + + if (err) { + pr_err("Failed to process events, error %d", err); + goto out_delete; + } + + sched->nr_events = session->evlist->stats.nr_events[0]; + sched->nr_lost_events = session->evlist->stats.total_lost; + sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; + } perf_sched__merge_lat(sched); perf_sched__sort_lat(sched); @@ -3163,7 +3200,10 @@ static int perf_sched__lat(struct perf_sched *sched) print_bad_events(sched); printf("\n"); - return 0; + rc = 0; +out_delete: + perf_session__delete(session); + return rc; } static int setup_map_cpus(struct perf_sched *sched) -- 2.17.1