Received: by 2002:a25:86ce:0:0:0:0:0 with SMTP id y14csp124267ybm; Tue, 21 May 2019 23:57:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqwHWBf5MDLAZhtFo3nZ3jZYcIrCLkR2JS7gfgVYkrwNGDTn96IvlaEslbA9BzTbi0Qwu3GQ X-Received: by 2002:a62:2cc2:: with SMTP id s185mr54485147pfs.106.1558508256286; Tue, 21 May 2019 23:57:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558508256; cv=none; d=google.com; s=arc-20160816; b=wh24OknB9AQwBgXUfYnbMY6mp7gO5n/se4OosqToMFzNOqLFaxacKnBY21JAT38EG5 Uxi7/fXBOrAcNtkVnMf3z3IJCJPI8qd3suO5xPA014ur8GV8XsgIOPl0L5Mu3BLmg5zp A12OWP2JKyLI516gnl+bFWgOx1xuY8fRtOiswQHUT4G2RYdiJYW3gjoXsIwz77+rwyXt IIjZgFOJe5QK0q9aY57nCHlJ43gNgNg+EQHuoyAq5iUvKXs5xtFAtug4ozWTH2190/kP CzceXfRFcV7wJw8Jx6ToFMaS3Kh3ClWsVRsBppJ68PjJ9Bo4RyRiv5Yoj2ZWdxKf159x bJ0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=xNNjO3Z10SyEiIZGGK+eEhwTEkIQuQOZC/mTNRdPXMU=; b=i9ZER6R8MJtLPVlUY5ngHgfV3hW6ZlIE/gMzDB5X6HAsMOOA4jtuZZUDDNndxK5Bh3 5BUWSEPJQnrxncQZ1G5Uzxrfboo9wZ07FUM70XXlDQ6AadZdH4OTztbJSTsoXiTXcLgj SezsNtZKv/XTogSewZ5wBTsJFtNPzmbNTL+8uO/1BAH8lu2dAA3z2B6iHEtjIb9dDPH9 7aaKtPDrXP+rSKMk20DjPYRdEuoMEtJNf83LtckTSDn+Q48SkN08TCRiuFNAriyKAQRB U+lZXhIQm1VwBdqpzHUy0EtuZ9h8uqlu/BMi4rBXpWaGVnThbZkzSq4VOUiTBGWLpKkX tJxA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Iyvhpnu7; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y10si26597020pfl.130.2019.05.21.23.57.20; Tue, 21 May 2019 23:57:36 -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; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Iyvhpnu7; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727946AbfEVG4S (ORCPT + 99 others); Wed, 22 May 2019 02:56:18 -0400 Received: from mail-pl1-f193.google.com ([209.85.214.193]:37342 "EHLO mail-pl1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726217AbfEVG4S (ORCPT ); Wed, 22 May 2019 02:56:18 -0400 Received: by mail-pl1-f193.google.com with SMTP id p15so597604pll.4 for ; Tue, 21 May 2019 23:56:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=xNNjO3Z10SyEiIZGGK+eEhwTEkIQuQOZC/mTNRdPXMU=; b=Iyvhpnu71BRoO8hxoyP6STb8UG8Grq+OW2XWQAJiPJKGG+N0ts+3Dt85OfQUrcfglQ 20ADlhUvnoMyWj7IAUtuOMRSVbnYDtjY98rr/pFHgI4rH+uQqFlXWrdW+qwLmJf3WwE6 HzWUfXF0jo0qvhCK/PIaDl+pwaDxzBTuMszpAcbwY7G4hEUPWJeUJS0mYSw6huJxebDr NnhriCcgxrhzsxE3lq+I47+EVL+HyMqFNH6r8t0HDw7ID87kb82CqNojHWbKiiBtNXIL 3WV7LGOqDl7SPsIoQQhGcZ65Xz32nA5xAr8x2Pi2+cYICqb9MXjUDU/1DcXdBQl+9RqX Bi1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=xNNjO3Z10SyEiIZGGK+eEhwTEkIQuQOZC/mTNRdPXMU=; b=i4zyLPfWDPB5G7xCO4hFVjQBAs2rNSZjdb/VBH6sXlr3J+kuMdmZTJUok+iXYh/jm9 iunhHQHAmN0MNvwFOsu7ApvmDkl6VBrGZ1JAJ6ZWoPdEHcgCREWsPFpuVpojJ4CDpojR NK4d79HFxyb796ISbgw9cxrlk/McPsTXx5WUXt2VYfxQF11abRDB9ft5gld2JlQ9wvXT fBmY3Eeg3NXuhytjk0eDD6gsb/ZyTWoeMi3mHdhXfARdAAV+VArsGpr1dkW/PwJqWrEE qxD2FCpaMm7WG4BXzBtVjzKwkimraTHlOvSJ0JwLLxpK3clej5kf+wBRSoiFcQ6/6Rpp 6e5Q== X-Gm-Message-State: APjAAAVuY3MmoL2KBp7guPqxNDc7OISgkFqW4PLnSCnZbIy1MJG9/GMo U5REOsvNOw2aNgXWuJPzUSo= X-Received: by 2002:a17:902:298a:: with SMTP id h10mr62396632plb.6.1558508176523; Tue, 21 May 2019 23:56:16 -0700 (PDT) Received: from google.com ([2401:fa00:d:10:75ad:a5d:715f:f6d8]) by smtp.gmail.com with ESMTPSA id x7sm19159629pfm.82.2019.05.21.23.56.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 21 May 2019 23:56:15 -0700 (PDT) Date: Wed, 22 May 2019 15:56:10 +0900 From: Namhyung Kim To: Wei Li Cc: acme@kernel.org, jolsa@redhat.com, alexander.shishkin@linux.intel.com, peterz@infradead.org, mingo@redhat.com, linux-kernel@vger.kernel.org, xiezhipeng1@huawei.com Subject: Re: [PATCH v2] fix use-after-free in perf_sched__lat Message-ID: <20190522065555.GA206606@google.com> References: <20190508143648.8153-1-liwei391@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190508143648.8153-1-liwei391@huawei.com> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, May 08, 2019 at 10:36:48PM +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 > perf sched record sleep 10 > perf sched latency --sort max > Segmentation fault (core dumped) > > Signed-off-by: Zhipeng Xie > Signed-off-by: Wei Li Acked-by: Namhyung Kim Thahks, Namhyung > --- > tools/perf/builtin-sched.c | 63 ++++++++++++++++++++++++++------------ > 1 file changed, 43 insertions(+), 20 deletions(-) > > diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c > index 275f2d92a7bf..8a4841fa124c 100644 > --- a/tools/perf/builtin-sched.c > +++ b/tools/perf/builtin-sched.c > @@ -1774,7 +1774,8 @@ static int perf_sched__process_comm(struct perf_tool *tool __maybe_unused, > return 0; > } > > -static int perf_sched__read_events(struct perf_sched *sched) > +static int __perf_sched__read_events(struct perf_sched *sched, > + struct perf_session *session) > { > const struct perf_evsel_str_handler handlers[] = { > { "sched:sched_switch", process_sched_switch_event, }, > @@ -1783,30 +1784,17 @@ static int perf_sched__read_events(struct perf_sched *sched) > { "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 = { > - .path = input_name, > - .mode = PERF_DATA_MODE_READ, > - .force = sched->force, > - }; > - int rc = -1; > - > - 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; > + return -1; > > 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; > + return -1; > } > > sched->nr_events = session->evlist->stats.nr_events[0]; > @@ -1814,9 +1802,28 @@ static int perf_sched__read_events(struct perf_sched *sched) > sched->nr_lost_chunks = session->evlist->stats.nr_events[PERF_RECORD_LOST]; > } > > - rc = 0; > -out_delete: > + return 0; > +} > + > +static int perf_sched__read_events(struct perf_sched *sched) > +{ > + struct perf_session *session; > + struct perf_data data = { > + .path = input_name, > + .mode = PERF_DATA_MODE_READ, > + .force = sched->force, > + }; > + int rc; > + > + session = perf_session__new(&data, false, &sched->tool); > + if (session == NULL) { > + pr_debug("No Memory for session\n"); > + return -1; > + } > + > + rc = __perf_sched__read_events(sched, session); > perf_session__delete(session); > + > return rc; > } > > @@ -3130,12 +3137,25 @@ static void perf_sched__merge_lat(struct perf_sched *sched) > > static int perf_sched__lat(struct perf_sched *sched) > { > + struct perf_session *session; > + struct perf_data data = { > + .path = input_name, > + .mode = PERF_DATA_MODE_READ, > + .force = sched->force, > + }; > struct rb_node *next; > + 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; > + } > + > + if (__perf_sched__read_events(sched, session)) > + goto out_delete; > > perf_sched__merge_lat(sched); > perf_sched__sort_lat(sched); > @@ -3164,7 +3184,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 >