Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7794553ybi; Tue, 9 Jul 2019 04:31:52 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3Wap5Oi6JV5XHQ3drNMWWygC1eyrOFPr2to5SXOxXEGojPvxqGai3stQQSztWzWcVO5Kl X-Received: by 2002:a63:5b1d:: with SMTP id p29mr28656335pgb.297.1562671912331; Tue, 09 Jul 2019 04:31:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562671912; cv=none; d=google.com; s=arc-20160816; b=NF4aLU++HEAb8Yaky2taSjGtFDcemv/A5MNRx14E5kgWbVaZAThUUF+F5Qh60Y48AC lyr+1wEU6WWo7il66SuQvHlfFMpm2g/vKH32UdI9dydVeapT11+hgC4PqnVPaYrvY9JV bdVj/pJ1sJoYcnmp8W/K8dgB5m4BwBy77tP/QlI7WTknYjyG7JpzR8LZ1tMBxdJkzoYd cL64yV0yvmaMW5QEHJqplJP0kkh8RduihOmrjjZJecBb2T2vXrREkYpY4nxFqFLoluQ0 uB436sMS1K7akhHe0dRp42cl4U8eewwRKEAAivFPwp0QvvblTRsGWtYmvXkT++X8Q/RP Z9PQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-disposition :content-transfer-encoding:mime-version:robot-unsubscribe:robot-id :git-commit-id:subject:to:references:in-reply-to:reply-to:cc :message-id:from:date:dkim-signature:dkim-filter; bh=9XEjvf7D8AKMvGlBsDGLsfHmYXQEXJKbwjUjP80HEis=; b=mjYDsXc+EgdSSmobJ/itHFWdmfSuQ0kFj10uO7oxvMUEXWVeBwML1263VNxVE08Ckj TsBQ2VnVlfRpMKeriMzea6pyhTGib1nCy7J+FNb1C+Zv4pln3oNpeFKrUHBxvJnHl4BV MCiR2eWDYyQjVhZNEgmrOiTvFVVp1VuIN+pj7HJQsyiLVHTo60ptUwfAlpy3/3jKFgRR jIbbiRap5V4jo3YHzxMA+SBWF+uH+/Otfi9n6UIF9gbBRn2AnUEFm0JfWia+H7L4otqX ouquLgN59Y8Z97SLSxUMOcWabgC9xXRbnhuUxaHsedMsXfTe3WLWBMFTb42apatvHQk5 LWHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@zytor.com header.s=2019061801 header.b=DYcpdz3y; 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=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y82si23288188pfb.58.2019.07.09.04.31.36; Tue, 09 Jul 2019 04:31:52 -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=pass header.i=@zytor.com header.s=2019061801 header.b=DYcpdz3y; 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=pass (p=NONE sp=NONE dis=NONE) header.from=zytor.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726284AbfGILag (ORCPT + 99 others); Tue, 9 Jul 2019 07:30:36 -0400 Received: from terminus.zytor.com ([198.137.202.136]:58787 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725947AbfGILaf (ORCPT ); Tue, 9 Jul 2019 07:30:35 -0400 Received: from terminus.zytor.com (localhost [127.0.0.1]) by terminus.zytor.com (8.15.2/8.15.2) with ESMTPS id x69BTvVv1892699 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 9 Jul 2019 04:29:57 -0700 DKIM-Filter: OpenDKIM Filter v2.11.0 terminus.zytor.com x69BTvVv1892699 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=zytor.com; s=2019061801; t=1562671798; bh=9XEjvf7D8AKMvGlBsDGLsfHmYXQEXJKbwjUjP80HEis=; h=Date:From:Cc:Reply-To:In-Reply-To:References:To:Subject:From; b=DYcpdz3y2iZnkv5xlnHRZ/zizgJ7TlacMQJbpMvV2fSEhowPaHwX3Nq6BHlp4OEML qdXS8s7mGoiUjgUfvxYnBXXCKxsoBHFsgwzExhryqTRVTN3NNq/x2lOwvvs/o8jCg5 AL4iGCMYAxVwcaZSngoGNs9xqe7rlrCEOlBCzDyCRZfBIh+VWov/MqzyrAcPYZfHs8 Y6gvHT1HuXcF1+TCUGpVD6qlAeXSIcT3L2i0d1+pIm3pk560oOlvLP/X59x5DKNo1L /YSb8VpSUJAPzi2eayPxc/FYU5TqhlEaTzW1GDVX0g+T7VQkioyR/euBxnYGTrPR0o pucUOhplHCSuQ== Received: (from tipbot@localhost) by terminus.zytor.com (8.15.2/8.15.2/Submit) id x69BTuV71892693; Tue, 9 Jul 2019 04:29:56 -0700 Date: Tue, 9 Jul 2019 04:29:56 -0700 X-Authentication-Warning: terminus.zytor.com: tipbot set sender to tipbot@zytor.com using -f From: tip-bot for Arnaldo Carvalho de Melo Message-ID: Cc: jolsa@kernel.org, liwei391@huawei.com, mingo@kernel.org, linux-kernel@vger.kernel.org, hpa@zytor.com, tglx@linutronix.de, adrian.hunter@intel.com, alexander.shishkin@linux.intel.com, peterz@infradead.org, acme@redhat.com, xiezhipeng1@huawei.com, namhyung@kernel.org Reply-To: hpa@zytor.com, peterz@infradead.org, linux-kernel@vger.kernel.org, mingo@kernel.org, jolsa@kernel.org, liwei391@huawei.com, xiezhipeng1@huawei.com, acme@redhat.com, tglx@linutronix.de, namhyung@kernel.org, adrian.hunter@intel.com, alexander.shishkin@linux.intel.com In-Reply-To: <20190704194355.GI10740@kernel.org> References: <20190508143648.8153-1-liwei391@huawei.com> <20190704194355.GI10740@kernel.org> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf thread: Allow references to thread objects after machine__exit() Git-Commit-ID: 4c00af0e94cd01b8c5a5e6b3323d34677b04e192 X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,BAYES_00, DATE_IN_FUTURE_06_12,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU, DKIM_VALID_EF autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on terminus.zytor.com Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: 4c00af0e94cd01b8c5a5e6b3323d34677b04e192 Gitweb: https://git.kernel.org/tip/4c00af0e94cd01b8c5a5e6b3323d34677b04e192 Author: Arnaldo Carvalho de Melo AuthorDate: Fri, 5 Jul 2019 12:11:35 -0300 Committer: Arnaldo Carvalho de Melo CommitDate: Sat, 6 Jul 2019 14:29:32 -0300 perf thread: Allow references to thread objects after machine__exit() Threads are created when we either synthesize PERF_RECORD_FORK events for pre-existing threads or when we receive PERF_RECORD_FORK events from the kernel as new threads get created. We then keep them in machine->threads[].entries rb trees till when we receive a PERF_RECORD_EXIT, i.e. that thread terminated. The thread object has a reference count that is grabbed when, for instance, we keep that thread referenced in struct hist_entry, in 'perf report' and 'perf top'. When we receive a PERF_RECORD_EXIT we remove the thread object from the rb tree and move it to the corresponding machine->threads[].dead list, then we do a thread__put(), dropping the reference we had for keeping it in the rb tree. In thread__put() we were assuming that when the reference count hit zero we should remove it from the dead list by simply doing a list_del_init(&thread->node). That works well when all the thread lifetime is during the machine that has the list heads lifetime, since we know that we can do the list_del_init() and it will update the 'dead' list_head. But in 'perf sched lat' we were doing: machine__new() (via perf_session__new) process events, grabbing refcounts to keep those thread objects in 'perf sched' local data structures. machine__exit() (via perf_session__delete) which would delete the 'dead' list heads. And then doing the final thread__put() for the refcounts 'perf sched' rightfully obtained for keeping those thread object references. b00m, since thread__put() would do the list_del_init() touching a dead dead list head. Fix it by removing all the dead threads from machine->threads[].dead at machine__exit(), since whatever is there should have refcounts taken by things like 'perf sched lat', and make thread__put() check if the thread is in a linked list before removing it from that list. Reported-by: Wei Li Link: https://lkml.kernel.org/r/20190508143648.8153-1-liwei391@huawei.com Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Jiri Olsa Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Zhipeng Xie Link: https://lkml.kernel.org/r/20190704194355.GI10740@kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/machine.c | 25 +++++++++++++++++++++++-- tools/perf/util/thread.c | 23 ++++++++++++++++++++--- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index dc7aafe45a2b..e00dc413652d 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -209,6 +209,18 @@ void machine__exit(struct machine *machine) for (i = 0; i < THREADS__TABLE_SIZE; i++) { struct threads *threads = &machine->threads[i]; + struct thread *thread, *n; + /* + * Forget about the dead, at this point whatever threads were + * left in the dead lists better have a reference count taken + * by who is using them, and then, when they drop those references + * and it finally hits zero, thread__put() will check and see that + * its not in the dead threads list and will not try to remove it + * from there, just calling thread__delete() straight away. + */ + list_for_each_entry_safe(thread, n, &threads->dead, node) + list_del_init(&thread->node); + exit_rwsem(&threads->lock); } } @@ -1758,9 +1770,11 @@ static void __machine__remove_thread(struct machine *machine, struct thread *th, if (threads->last_match == th) threads__set_last_match(threads, NULL); - BUG_ON(refcount_read(&th->refcnt) == 0); if (lock) down_write(&threads->lock); + + BUG_ON(refcount_read(&th->refcnt) == 0); + rb_erase_cached(&th->rb_node, &threads->entries); RB_CLEAR_NODE(&th->rb_node); --threads->nr; @@ -1770,9 +1784,16 @@ static void __machine__remove_thread(struct machine *machine, struct thread *th, * will be called and we will remove it from the dead_threads list. */ list_add_tail(&th->node, &threads->dead); + + /* + * We need to do the put here because if this is the last refcount, + * then we will be touching the threads->dead head when removing the + * thread. + */ + thread__put(th); + if (lock) up_write(&threads->lock); - thread__put(th); } void machine__remove_thread(struct machine *machine, struct thread *th) diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index b413ba5b9835..7bfb740d2ede 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -125,10 +125,27 @@ void thread__put(struct thread *thread) { if (thread && refcount_dec_and_test(&thread->refcnt)) { /* - * Remove it from the dead_threads list, as last reference - * is gone. + * Remove it from the dead threads list, as last reference is + * gone, if it is in a dead threads list. + * + * We may not be there anymore if say, the machine where it was + * stored was already deleted, so we already removed it from + * the dead threads and some other piece of code still keeps a + * reference. + * + * This is what 'perf sched' does and finally drops it in + * perf_sched__lat(), where it calls perf_sched__read_events(), + * that processes the events by creating a session and deleting + * it, which ends up destroying the list heads for the dead + * threads, but before it does that it removes all threads from + * it using list_del_init(). + * + * So we need to check here if it is in a dead threads list and + * if so, remove it before finally deleting the thread, to avoid + * an use after free situation. */ - list_del_init(&thread->node); + if (!list_empty(&thread->node)) + list_del_init(&thread->node); thread__delete(thread); } }